<jupyter_start><jupyter_text>INTELLIENT GARBAGE MANAGEMENT SYSTEM FOR SMART CITIES CNN Based Model Development   Importing Libraries<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import cv2
import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.preprocessing import image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import RMSprop
import keras
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.models import load_model
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint

from google.colab import drive
drive.mount("/content/gdrive")<jupyter_output>Drive already mounted at /content/gdrive; to attempt to forcibly remount, call drive.mount("/content/gdrive", force_remount=True).<jupyter_text>Checking if database is properly connected.<jupyter_code>img = image.load_img("/content/gdrive/My Drive/EPIC Project - Links and Papers/Dataset & Architecute Used in All papers/Garbage Dataset Collection/Dataset_Merged/Train/Garbage/[duckduckgo.com][44610].jpg")
plt.imshow(img)
cv2.imread("/content/gdrive/My Drive/EPIC Project - Links and Papers/Dataset & Architecute Used in All papers/Garbage Dataset Collection/Dataset_Merged/Train/Garbage/[duckduckgo.com][44610].jpg").shape
train_data_path = "/content/gdrive/My Drive/EPIC Project - Links and Papers/Dataset & Architecute Used in All papers/Garbage Dataset Collection/Dataset_Merged/Train/"
validation_data_path = "/content/gdrive/My Drive/EPIC Project - Links and Papers/Dataset & Architecute Used in All papers/Garbage Dataset Collection/Dataset_Merged/Val/"
# getting data
base_dir = '/content/gdrive/My Drive/EPIC Project - Links and Papers/Dataset & Architecute Used in All papers/Garbage Dataset Collection/Dataset_Merged/'
train_dir = os.path.join(base_dir, 'Train')
validation_dir = os.path.join(base_dir, 'Val')

train_Garbage = os.path.join(train_dir, 'Garbage')
train_NonGarbage = os.path.join(train_dir, 'NonGarbage')
validation_Garbage = os.path.join(validation_dir, 'Garbage')
validation_NonGarbage = os.path.join(validation_dir, 'NonGarbage')

num_garbage_tr = len(os.listdir(train_Garbage))
num_nongarbage_tr = len(os.listdir(train_NonGarbage))
num_garbage_val = len(os.listdir(validation_Garbage))
num_nongarbage_val = len(os.listdir(validation_NonGarbage))

total_train = num_garbage_tr + num_nongarbage_tr
total_val = num_garbage_val + num_nongarbage_val
BATCH_SIZE = 30
IMG_SHAPE = 128 #match image dimension to mobile net input
# It generate more images using below parameters
#CNN doesn't take care of rotation, scaling etc..
#The objective of ImageDataGenerator is to import data with labels easily into the model. It is a very useful class as it has many function to rescale, rotate, zoom, flip etc. 
#ImageDataGenerator will automatically label all the data inside folders
#>ImageDataGenerator will automatically label all the data inside folders
training_datagen = ImageDataGenerator(rescale=1./255,
                                      rotation_range=40,
                                      width_shift_range=0.2,
                                      height_shift_range=0.2,
                                      shear_range=0.2,
                                      zoom_range=0.2,
                                      horizontal_flip=True,
                                      fill_mode='nearest')


# this is the augmentation configuration we will use for validation:
# only rescaling
valid_datagen = ImageDataGenerator(rescale=1./255)
 
# this is a generator that will read pictures found in
# at train_data_path, and indefinitely generate
# batches of augmented image data
training_data = training_datagen.flow_from_directory(train_data_path, # this is the target directory
                                      batch_size=BATCH_SIZE,
                                      target_size=(IMG_SHAPE, IMG_SHAPE),
                                      shuffle=True,
                                      class_mode='binary')  # since we use binary_crossentropy loss, we need binary labels
 

 
training_data.class_indices
#this is a similar generator, for validation data
valid_data = valid_datagen.flow_from_directory(validation_data_path,
                                  batch_size=BATCH_SIZE,
                                  target_size=(IMG_SHAPE, IMG_SHAPE),
                                  shuffle=False,
                                  class_mode='binary')<jupyter_output>Found 924 images belonging to 2 classes.<jupyter_text>CNN<jupyter_code>import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
# model

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(IMG_SHAPE, IMG_SHAPE, 3)), # RGB
    tf.keras.layers.MaxPooling2D(2,2),
    
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    
    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    
    tf.keras.layers.Conv2D(128, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    
    tf.keras.layers.Dropout(0.5), # 1/2 of neurons will be turned off randomly
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(256, activation='relu'),
    
    tf.keras.layers.Dense(2, activation='softmax') #[0, 1] or [1, 0]
    
    ])


model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.summary()

EPOCHS = 50
# .fit_generator() function first accepts a batch of the dataset, then performs backpropagation on it, and then updates the weights in our model.
history = model.fit_generator(
    training_data,
    steps_per_epoch=(total_train)//BATCH_SIZE,
    epochs=EPOCHS,
    validation_data=valid_data,
    validation_steps=(total_val)//BATCH_SIZE
    )<jupyter_output><empty_output><jupyter_text>Analysis of CNN Model<jupyter_code>acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(EPOCHS)

plt.figure(figsize=(8, 8)) # pLotting accuracies 
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2) # Plotting losses
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()<jupyter_output><empty_output><jupyter_text>VGG16<jupyter_code>training_data_vgg = training_datagen.flow_from_directory(train_data_path, # this is the target directory
                                      batch_size=BATCH_SIZE,
                                      target_size=(224, 224),
                                      shuffle=True,
                                      class_mode='binary')
#this is a similar generator, for validation data
valid_data_vgg = valid_datagen.flow_from_directory(validation_data_path,
                                  batch_size=BATCH_SIZE,
                                  target_size=(224, 224),
                                  shuffle=False,
                                  class_mode='binary')
from keras.applications.vgg16 import VGG16
from keras.models import Model
from keras.applications.vgg16 import preprocess_input

# add preprocessing layer to the front of VGG
vgg = VGG16(input_shape=(224, 224, 3), weights='imagenet', include_top=False)

# don't train existing weights
for layer in vgg.layers:
  layer.trainable = False
  
x = Flatten()(vgg.output)
# x = Dense(1000, activation='relu')(x)
prediction = Dense(2, activation='softmax')(x)

# create a model object
model = Model(inputs=vgg.input, outputs=prediction)

# view the structure of the model
model.summary()

model.compile(optimizer='adam', #Adam is a replacement optimization algorithm for stochastic gradient descent for training deep learning models.
              loss='sparse_categorical_crossentropy', #When doing multi-class classification, categorical cross entropy loss is used
              metrics=['accuracy']) #A metric is a function that is used to judge the performance of your model.
history_vgg16 = model.fit_generator(
  training_data_vgg,
  validation_data=valid_data_vgg,
  epochs=5,
  steps_per_epoch=(total_train)//BATCH_SIZE,
  validation_steps=(total_val)//BATCH_SIZE 
)<jupyter_output>/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:1844: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
  warnings.warn('`Model.fit_generator` is deprecated and '<jupyter_text>Analysis of VGG16<jupyter_code># analysis of VGG16

# accuracies
plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(history_vgg16.history['accuracy'], label='train acc')
plt.plot(history_vgg16.history['val_accuracy'], label='val acc')
plt.legend()
plt.title('Training and Validation Accuracy')


#Losses

plt.subplot(1, 2, 2)
plt.plot(history_vgg16.history['loss'], label='train loss')
plt.plot(history_vgg16.history['val_loss'], label='val loss')
plt.legend()
plt.title('Training and Validation Loss')
plt.savefig('LossVal_loss')
plt.show()<jupyter_output><empty_output><jupyter_text>MobileNet<jupyter_code># getting MobileNet
#URL = "https://tfhub.dev/google/tf2-preview/mobilenet_v2/feature_vector/4"
URL = "https://tfhub.dev/google/imagenet/mobilenet_v1_100_128/feature_vector/4" #Feature vectors of images with MobileNet V1 (depth multiplier 1.00) trained on ImageNet.
mobile_net = hub.KerasLayer(URL, input_shape=(IMG_SHAPE, IMG_SHAPE, 3))

mobile_net.trainable = False  #Making sure that inner layers weights are not going to be trained since they are already trained on imagenet.

model = tf.keras.models.Sequential([
    mobile_net,
    tf.keras.layers.Dense(2, activation='softmax') #adding dense layer in the last for out binary classification of garbage and non garbage images.
    ])
#MobileNet_v1
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.summary()
EPOCHS = 6

history = model.fit_generator(
    training_data,
    steps_per_epoch=int(np.ceil(total_train / float(BATCH_SIZE))),
    epochs=EPOCHS,
    validation_data=valid_data,
    validation_steps=int(np.ceil(total_val / float(BATCH_SIZE)))
    )<jupyter_output>/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:1844: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators.
  warnings.warn('`Model.fit_generator` is deprecated and '<jupyter_text>Analysis of MobileNet<jupyter_code># analysis of MobileNet_v1
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']

loss = history.history['loss']
val_loss = history.history['val_loss']

epochs_range = range(EPOCHS)

plt.figure(figsize=(8, 8))
plt.subplot(1, 2, 1)
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, val_acc, label='Validation Accuracy')
plt.legend(loc='lower right')
plt.title('Training and Validation Accuracy')

plt.subplot(1, 2, 2)
plt.plot(epochs_range, loss, label='Training Loss')
plt.plot(epochs_range, val_loss, label='Validation Loss')
plt.legend(loc='upper right')
plt.title('Training and Validation Loss')
plt.show()<jupyter_output><empty_output><jupyter_text>Checking if model is able to predict on the new test images which are not from training as well as validation data.<jupyter_code>#Image loaded is:
loaded_image = load_img('/content/gdrive/My Drive/EPIC Project - Links and Papers/Dataset & Architecute Used in All papers/Garbage Dataset Collection/Dataset_Merged/Test/download.jpg')
plt.imshow(loaded_image)
test_image = load_img('/content/gdrive/My Drive/EPIC Project - Links and Papers/Dataset & Architecute Used in All papers/Garbage Dataset Collection/Dataset_Merged/Test/Clean_road.jpg' ,target_size=(128,128))
test_image = img_to_array(test_image)/255 # convert image to np array and scaling it
test_image = np.expand_dims(test_image, axis = 0) # change dimention 3D to 4D, Expand the shape of an array.
result = model.predict(test_image).round(3) # predict garbage or not garbage in a float value.
pred = np.argmax(result) # get the index of max value, finding the class with the largest predicted probability.
if pred == 0:
  print("Image contains Garbage")
elif pred == 1:
  print("Image does not contain Garbage")

loaded_image = load_img('/content/gdrive/My Drive/EPIC Project - Links and Papers/Dataset & Architecute Used in All papers/Garbage Dataset Collection/Dataset_Merged/Test/Clean_road.jpg')
plt.imshow(loaded_image)
model.save('models/EPICSTrial3.h5') #saving the model.<jupyter_output><empty_output><jupyter_text>Conversion of MobileNet .h5 model to tflite.<jupyter_code>pwd
ls
def get_file_size(file_path):
    size = os.path.getsize(file_path)
    return size
def convert_bytes(size, unit=None):
    if unit == "KB":
        return print('File size: ' + str(round(size / 1024, 3)) + ' Kilobytes')
    elif unit == "MB":
        return print('File size: ' + str(round(size / (1024 * 1024), 3)) + ' Megabytes')
    else:
        return print('File size: ' + str(size) + ' bytes')
convert_bytes(get_file_size('EPICSTrial3.h5'), "MB")
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quant_model = converter.convert()
#checking size of the of converted model.
tflite_model_name = "tf_lite_model.tflite"
open(tflite_model_name, "wb").write(tflite_quant_model)
convert_bytes(get_file_size(tflite_model_name), "KB")<jupyter_output>File size: 3428.547 Kilobytes
{'fraction_non_alphanumeric': 0.10059502975148757, 'fraction_numerical': 0.01666083304165208, 'mean_word_length': 4.135154565061107, 'pattern_counts': {'":': 2, '<': 36, '<?xml version=': 0, '>': 37, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '437924', 'license': 'MIT', 'n_tokens_mistral': 4744, 'n_tokens_neox': 4476, 'n_words': 1080, 'path': 'Machine Learning and Data Science/Advanced/Detox/Intelligent_Garbage_Management_System_Model_Development.ipynb', 'repo_name': 'Satyam26-09/Project-Guidance', 'size': '14285'}