<jupyter_start><jupyter_text><jupyter_code>9+9
import numpy as np
from keras import layers
from tensorflow.nn import avg_pool2d, max_pool2d
from keras.layers import Input, Add, Dense, Activation, ZeroPadding2D, BatchNormalization, Flatten, Conv2D, AveragePooling2D, MaxPooling2D, GlobalMaxPooling2D
from keras.models import Model, load_model
from keras.models import Sequential
from keras.preprocessing import image
from keras.utils import layer_utils
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import preprocess_input
import pydot
import numpy as np
from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
from keras.utils import plot_model
from keras.initializers import glorot_uniform
import scipy.misc
from matplotlib.pyplot import imshow
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.callbacks import TensorBoard
#from tensorflow.keras.applications import ResNet50
%matplotlib inline

import keras.backend as K
K.set_image_data_format('channels_last')
K.set_learning_phase(1)
nvidia-smi
from tensorflow.python.client import device_lib
device_lib.list_local_devices()
def identity_block(X, f, filters, stage, block):
 
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    
    F1, F2, F3 = filters
    
    X_shortcut = X
    
    X = Conv2D(filters = F1, kernel_size = (1, 1), strides = (1,1), padding = 'valid', name = conv_name_base + '2a', kernel_initializer = glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X)
    X = Activation('relu')(X)
    
    

    X = Conv2D(filters=F2, kernel_size=(f, f), strides=(1, 1), padding='same', name=conv_name_base + '2b', kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
    X = Activation('relu')(X)

    
    X = Conv2D(filters=F3, kernel_size=(1, 1), strides=(1, 1), padding='valid', name=conv_name_base + '2c', kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2c')(X)

    
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)
    
    
    return X
def convolutional_block(X, f, filters, stage, block, s = 2):

    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'
    
    F1, F2, F3 = filters
    
    X_shortcut = X

 
    X = Conv2D(F1, (1, 1), strides = (s,s), name = conv_name_base + '2a', kernel_initializer = glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis = 3, name = bn_name_base + '2a')(X)
    X = Activation('relu')(X)
    

    X = Conv2D(filters=F2, kernel_size=(f, f), strides=(1, 1), padding='same', name=conv_name_base + '2b', kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
    X = Activation('relu')(X)

    X = Conv2D(filters=F3, kernel_size=(1, 1), strides=(1, 1), padding='valid', name=conv_name_base + '2c', kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2c')(X)


    X_shortcut = Conv2D(filters=F3, kernel_size=(1, 1), strides=(s, s), padding='valid', name=conv_name_base + '1', kernel_initializer=glorot_uniform(seed=0))(X_shortcut)
    X_shortcut = BatchNormalization(axis=3, name=bn_name_base + '1')(X_shortcut)


    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)

    
    return X
def ResNet50(input_shape = (224, 224, 3), classes = 2):
  
    X_input = Input(input_shape)

    
    X = ZeroPadding2D((3, 3))(X_input)
    
    X = Conv2D(64, (7, 7), strides = (2, 2), name = 'conv1', kernel_initializer = glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis = 3, name = 'bn_conv1')(X)
    X = Activation('relu')(X)
    X = MaxPooling2D((3, 3), strides=(2, 2))(X)

    X = convolutional_block(X, f = 3, filters = [64, 64, 256], stage = 2, block='a', s = 1)
    X = identity_block(X, 3, [64, 64, 256], stage=2, block='b')
    X = identity_block(X, 3, [64, 64, 256], stage=2, block='c')


    X = convolutional_block(X, f=3, filters=[128, 128, 512], stage=3, block='a', s=2)
    X = identity_block(X, 3, [128, 128, 512], stage=3, block='b')
    X = identity_block(X, 3, [128, 128, 512], stage=3, block='c')
    X = identity_block(X, 3, [128, 128, 512], stage=3, block='d')

    X = convolutional_block(X, f=3, filters=[256, 256, 1024], stage=4, block='a', s=2)
    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='b')
    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='c')
    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='d')
    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='e')
    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='f')

    X = X = convolutional_block(X, f=3, filters=[512, 512, 2048], stage=5, block='a', s=2)
    X = identity_block(X, 3, [512, 512, 2048], stage=5, block='b')
    X = identity_block(X, 3, [512, 512, 2048], stage=5, block='c')

    X = AveragePooling2D(pool_size=(2, 2), padding='same')(X)

    X = Flatten()(X)
    #X = Dense(256, activation='relu')(X)
    X = Dense(classes, activation='softmax', name='fc' + str(classes), kernel_initializer = glorot_uniform(seed=0))(X)
    
    
    model = Model(inputs = X_input, outputs = X, name='ResNet50')

    return model
model=Sequential()
model.add(ResNet50(input_shape = (224, 224, 3), classes = 2))
import datetime, os
logdir = os.path.join("logs", datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
tensorboard_callback =TensorBoard(logdir, histogram_freq=1)
model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy',
                   metrics = ['accuracy'])
model.summary()
from google.colab import drive
drive.mount("/content/drive", force_remount=True)
!unzip -uq "/content/drive/My Drive/DATASET" -d "/content/drive/My Drive/"
train_datagen = ImageDataGenerator(rescale = 1./255)
test_datagen = ImageDataGenerator(rescale = 1./255)
train= train_datagen.flow_from_directory('/content/drive/My Drive/cnn/DATASET/TRAIN',
                                                 target_size = (224, 224),
                                                 batch_size = 32,
                                                 class_mode = 'binary')
test=train_datagen.flow_from_directory('/content/drive/My Drive/cnn/DATASET/TEST',
                                                 target_size = (224, 224),
                                                 batch_size = 32,
                                                 class_mode = 'binary')
def convert_to_one_hot(Y, C):
    Y = np.eye(C)[Y.reshape(-1)].T
    return Y
X_train_orig,Y_train_orig=train.next()
X_test_orig,Y_test_orig=test.next()

Y_train_orig=Y_train_orig.astype(int)
Y_test_orig=Y_test_orig.astype(int)

#X_train_orig= keras.utils.to_categorical(X_train_orig, NB_CLASSES)
#X_test_orig = keras.utils.to_categorical(X_test_orig, NB_CLASSES)

Y_train = convert_to_one_hot(Y_train_orig, 2).T
Y_test = convert_to_one_hot(Y_test_orig, 2).T

print(X_train_orig.shape)
print(X_test_orig.shape)
print(Y_train.shape)
print(Y_test.shape)

hist = model.fit_generator(train, epochs = 5, validation_data = test)
from tensorflow.keras.applications import ResNet50
res = ResNet50(input_shape = (224, 224, 3), include_top = True, weights = 'imagenet')
res.summary()


import tensorflow as tf
model = tf.keras.models.Sequential()
model.add(res)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(256, activation='relu'))
model.add(tf.keras.layers.Dense(2, activation='softmax'))
model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics = ['acc'])
model.summary()
  


hist = model.fit_generator(train, epochs = 5, validation_data = test)
model.save("waste_224.h5")
pwd

cd 
pwd
cd '/content/sample_data'
model.summary()
import tensorflow.python.keras
model=load_model('/content/waste_224.h5')
/content/waste_224.h5
model.summary()
hist = model.fit_generator(train, epochs = 15, validation_data = test)
model.save("model_2242.h5")
model.evaluate(X_test_orig,Y_test_orig)
import matplotlib.pyplot as plt

%matplotlib inline
acc = hist.history['acc']
loss = hist.history['loss']
val_acc = hist.history['val_acc']
val_loss = hist.history['val_loss']

epochs = range(len(acc))

plt.plot(epochs, acc, 'r', "Training Accuracy")
plt.plot(epochs, val_acc, 'b', "Testing Accuracy")
plt.title('Training vs Testing Accuracy')
plt.figure()

plt.plot(epochs, loss, 'r', "Training Loss")
plt.plot(epochs, val_loss, 'b', "Testing Loss")
plt.title('Training vs Testing Loss')
plt.show()
import tensorflow as tf
import h5py
tf.keras.models.save_model(model,"/content/colab_model.h5")
new_model=tf.keras.models.load_model(
    "/content/colab_model.h5",
    custom_objects=None,
    compile=True
)
new_model.summary()
from PIL import Image
image = Image.open('veg.jpg')
# summarize some details about the image
print(image.format)
print(image.mode)
print(image.size)
# show the image
image.show()
#size = 224, 224
#image.thumbnail(size, Image.ANTIALIAS)

cd ;
import matplotlib.pyplot as plt
import tensorflow as tf
img_array=tf.keras.preprocessing.image.img_to_array(image)
# print(img_array)
#print(img_array)
img_array=img_array/255.
#print(img_array)
img_array.resize([224,224,3])
print(img_array.shape)
img2=tf.keras.preprocessing.image.array_to_img(img_array)
plt.imshow(img_array)
print(img2.mode)
inputt=[]
inputt.append([img_array])
new_model.predict(inputt)
from PIL import Image<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14796238244514107, 'fraction_numerical': 0.04148380355276907, 'mean_word_length': 4.346927374301676, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '288071', 'license': 'Apache-2.0', 'n_tokens_mistral': 3843, 'n_tokens_neox': 3508, 'n_words': 740, 'path': '224_model.ipynb', 'repo_name': 'mohanmohadikar/WASTE-SEGREGATION-USING-IMAGE-CLASSIFICATION', 'size': '9570'}