<jupyter_start><jupyter_text>Importing the required libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
import PIL
import tensorflow as tf
from tensorflow.python import keras
import warnings 
import argparse
warnings.filterwarnings('ignore')
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img
from sklearn.preprocessing import LabelBinarizer, StandardScaler
from sklearn.model_selection import train_test_split
from tensorflow.keras.utils import to_categorical
fig,axes = plt.subplots(1,5, figsize=(18,18))
images = os.listdir(os.path.join(base_dir, 'train/Apple___Black_rot'))
for _ in range(5):
  ax = axes[_]
  image_path = base_dir+'/train/Apple___Black_rot/'+images[_]
  img = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
  ax.imshow(img)
  ax.axis('off')
plt.show()<jupyter_output><empty_output><jupyter_text>Data Augmentation transformations for the train dataset<jupyter_code>train_datagen = ImageDataGenerator(rescale=1./255,
                                   shear_range=0.2,
                                   zoom_range=0.2,
                                   width_shift_range=0.2,
                                   height_shift_range=0.2,
                                   fill_mode='nearest')

# Data augmentation for validation dataset
validation_datagen = ImageDataGenerator(rescale = 1./255)<jupyter_output><empty_output><jupyter_text>Loading the images from their directories<jupyter_code>BATCH_SIZE = 32

train_set_from_dir = train_datagen.flow_from_directory(os.path.join(base_dir, 'train'),
                                                       target_size = (224,224),
                                                       batch_size = BATCH_SIZE,
                                                       class_mode = 'categorical'
                                                       )

validation_set_from_dir = validation_datagen.flow_from_directory(os.path.join(base_dir, 'valid'),
                                                                 target_size = (224,224),
                                                                 batch_size = BATCH_SIZE,
                                                                 class_mode = 'categorical'
)
TRAIN_SIZE = train_set_from_dir.n
VALID_SIZE = validation_set_from_dir.n
classes_dict = train_set_from_dir.class_indices
img = train_set_from_dir.filepaths[np.random.random_integers(low=0, high=train_set_from_dir.samples)]
img = cv2.imread(img)
plt.imshow(img)
import json
# with open('/content/drive/My Drive/PLANT DISEASE RECOGNITION/class_indices.json','w') as f:
  # json.dump(classes_dict, f)<jupyter_output><empty_output><jupyter_text>Building a simple CNN ArchitectureREFERENCES :- https://heartbeat.fritz.ai/regularization-techniques-for-image-processing-using-tensorflow-56c5b365bc17<jupyter_code>from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
from keras.regularizers import L1L2
from keras.layers.normalization import BatchNormalization
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
model = Sequential()
# Convolutional 1st layer
model.add(Conv2D(32, kernel_size = (7,7), activation='relu', input_shape=(224,224,3)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))

# Convolutional 2nd layer
model.add(Conv2D(64, kernel_size = (5,5), activation='relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.2))

# Convolutional 3rd layer
model.add(Conv2D(128, kernel_size=(3,3), activation = 'relu', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size= (2,2)))
model.add(Dropout(0.2))

# Convolutional 4th layer
model.add(Conv2D(128, kernel_size=(3,3), activation= 'relu', padding='same'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.2))

# Fully connected 1st layer
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.2))

# Fully connected 2nd layer
model.add(Dense(1600, activation='relu'))
model.add(Dropout(0.2))

# Fully connected 3rd layer
model.add(Dense(1500, activation='relu'))
model.add(Dropout(0.2))

# Fully connected semi-final layer
model.add(Dense(300, activation='relu'))
model.add(Dropout(0.2))

# Fully connected final layer (for 38 distinct categories hence 38 neurons)
model.add(Dense(38, activation = 'softmax'))
INIT_LR = 3e-4
model.compile(optimizer = Adam(learning_rate = INIT_LR),
              loss = 'categorical_crossentropy',
              metrics = ['accuracy']
)
model.summary()
## Setting up callbacks for our model

callbacks = [
           ModelCheckpoint('/content/drive/My Drive/PLANT DISEASE RECOGNITION/checkpoints/cnnmodel_plantdiseases.h5', save_best_only=True, monitor='val_accuracy'),
           EarlyStopping(monitor='val_loss', patience=10, verbose=1),
           ReduceLROnPlateau(factor=0.1, patience=10, min_lr=0.00001, verbose=1) 
]
N_EPOCHS = 16

history = model.fit(train_set_from_dir,
          validation_data = validation_set_from_dir,
          epochs = N_EPOCHS,
          # # Use 128 random batches for training set 
          steps_per_epoch = 128, # 150 x 32 =  random samples
          # # Use 64 random batches for training validation set
          validation_steps = 64, # 100 x 32 = 3200 random samples 
          callbacks = callbacks
          )<jupyter_output><empty_output><jupyter_text>Transfer learning with MobileNet architecture<jupyter_code># Importing the required libraries for modelling 
from keras import Input, Model
from keras.applications import MobileNet
from keras.layers.core import Flatten, Dense,Dropout
from keras.layers import GlobalAveragePooling2D
from keras.layers.normalization import BatchNormalization
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau
from keras.models import Model, model_from_json
from keras.optimizers import Adam
from sklearn.metrics import classification_report, roc_auc_score
base_model = MobileNet(
    #Load weights into the pre-trained MobileNet model
    weights="imagenet",
    input_shape=(224, 224, 3),
    #Exclude the ImageNet classifier at the top of the model
    include_top=False
)  
base_model.summary()
head_model = base_model.output
head_model = GlobalAveragePooling2D()(head_model)
# Regularization by applying DropOut
head_model = Dropout(0.2)(head_model)
outputs = Dense(38, activation="softmax")(head_model)
mobilenet_model = Model(base_model.input, outputs, name='pretrained_mobilenet' )

for layer in mobilenet_model.layers:
    layer.trainable = False

# or if we want to set the first 20 layers of the network to be non-trainable
for layer in model.layers[:20]:
    layer.trainable=False
for layer in model.layers[20:]:
    layer.trainable=True
# Compiling the model with the optimizer and loss function 

mobilenet_model.compile(optimizer = Adam(),
              loss = 'categorical_crossentropy',
              metrics = ['accuracy']
)
mobilenet_model.summary()
for idx, layer in enumerate(mobilenet_model.layers):
    print(idx, layer.name, layer.trainable)
## Setting up callbacks for our model

callbacks = [
           ModelCheckpoint('content/drive/My Drive/PLANT DISEASE RECOGNITION/checkpoints/mobilenet_plantdiseases.h5', save_best_only=True, monitor='val_acc'),
           EarlyStopping(monitor='val_loss', patience=2, verbose=1),
           ReduceLROnPlateau(factor=0.1, patience=10, min_lr=0.00001, verbose=1) 
]
N_EPOCHS = 10

history = mobilenet_model.fit(train_set_from_dir,
          validation_data = validation_set_from_dir,
          epochs = N_EPOCHS,
          # Use 128 random batches for training set 
          steps_per_epoch = 128, # 128 x 32 = 2**12 random samples
          # Use 64 random batches for training validation set
          validation_steps = 100, # 100 x 32 = 3200 random samples 
          callbacks = callbacks
          )
n = 6
plt.figure(figsize = (8,5))
plt.plot(np.arange(1,n+1), history.history['loss'], label = 'train_loss')
plt.plot(np.arange(1,n+1), history.history['val_loss'], label = 'val_loss')
plt.plot(np.arange(1,n+1), history.history['accuracy'], label = 'train_accuracy')
plt.plot(np.arange(1,n+1), history.history['val_accuracy'], label = 'val_accuracy')

plt.grid(True)
plt.legend(loc = "best")
plt.savefig('/content/drive/My Drive/PLANT DISEASE RECOGNITION/performance.jpg')
plt.show()
# Evaluate the model on the Validation dataset
results = mobilenet_model.evaluate(validation_set_from_dir)
print("Validation Loss :-", results[0])
print("="*30)
print("Validation Accuracy :-", results[1])
cd '/content/drive/My Drive/PLANT DISEASE RECOGNITION'
mkdir models
# Save model in HDF5 format
mobilenet_model.save('models/mobilenet_model.h5')
# Testing on a random image from the test images directory
from PIL import Image
np.random.seed(200)
idx = np.random.randint(30)
test_images_dir = os.path.join('datasets/test', 'test')
test1 = Image.open(os.path.join(test_images_dir, os.listdir(test_images_dir)[idx]))
plt.imshow(test1)
plt.title(os.listdir(test_images_dir)[idx])
test1 = test1.resize((224,224))
test1_scaled = np.expand_dims(np.asarray(test1), axis = 0) / 255
predictions = mobilenet_model.predict(test1_scaled)
print(predictions)
classes_dict = train_set_from_dir.class_indices
classes_dict = { v:k for (k,v) in classes_dict.items() }
classes_dict[np.argmax(predictions)]
from tensorflow.python.keras.models import load_model
mobilenet_model = load_model('models/mobilenet_model.h5')
mobilenet_model.summary()
import tensorflow as tf
converter = tf.lite.TFLiteConverter.from_keras_model(mobilenet_model)
tflite_model = converter.convert()
open("models/converted_model.tflite", "wb").write(tflite_model)<jupyter_output><empty_output><jupyter_text>Accurate !  Converting model to Tensorflow js<jupyter_code>!pip install tensorflowjs
!pwd
!mkdir tensorflowjs-model
!tensorflowjs_converter --input_format keras models/mobilenet_model.h5 tensorflowjs-model/
!zip -r tensorflowjs-model.zip tensorflowjs-model<jupyter_output><empty_output><jupyter_text>And we are done !<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10493947676688793, 'fraction_numerical': 0.027918781725888325, 'mean_word_length': 4.112275449101796, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '196548', 'license': 'MIT', 'n_tokens_mistral': 3492, 'n_tokens_neox': 3262, 'n_words': 684, 'path': 'notebook/plantdiseaserecognition.ipynb', 'repo_name': 'gshanbhag525/farmdoctor', 'size': '10244'}