{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"from shutil import copyfile\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport os\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nimport keras\nfrom PIL import Image","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"## train path => /kaggle/input/cassava-leaf-disease-classification/train_images\n## train csv path => /kaggle/input/cassava-leaf-disease-classification/train.csv\n## test images => /kaggle/input/cassava-leaf-disease-classification/test_images\ntrainFrame = pd.read_csv('/kaggle/input/cassava-leaf-disease-classification/train.csv')\ntrainFrame.label = trainFrame.label.astype(str)\n\nmp1 = {'0': 'Cassava Bacterial Blight (CBB)',\n '1': 'Cassava Brown Streak Disease (CBSD)',\n '2': 'Cassava Green Mottle (CGM)',\n '3': 'Cassava Mosaic Disease (CMD)',\n '4': 'Healthy'}\n\ntrainFrame.label = trainFrame.label.map(mp1)\n\n#uniqueBreeds = set (trainFrame['label'].values)\n#print ('no. of breeds:{}'.format(len(uniqueBreeds)))\n\n#os.makedirs('../trainDir')\n#for mem in uniqueBreeds:\n#    os.makedirs('../trainDir/' + str(mem))\n\n    \n#balanceCounter = 0\n\n#for i in range(len(trainFrame)):\n#    imgName = trainFrame.loc[i].values[0]\n#    dirName = trainFrame.loc[i].values[1]\n    \n    #if dirName == 3:\n        #if balanceCounter < 5000:\n            #balanceCounter += 1\n        #else:\n            #continue\n    \n    \n#    sourcePathImg = '/kaggle/input/cassava-leaf-disease-classification/train_images/'\n#    destPathImg = '../trainDir/'\n    \n#    sourcePathImg = sourcePathImg + imgName \n#    destPathImg = destPathImg + str(dirName) + '/' + imgName \n#    print (sourcePathImg + '   === >' + destPathImg)\n    \n#    copyfile(sourcePathImg, destPathImg)\n\ndata_generator = tf.keras.preprocessing.image.ImageDataGenerator(\n    rescale=1/255.,\n    validation_split=0.30,\n    rotation_range=360,\n    horizontal_flip=True,\n    vertical_flip=True,\n)\n\ntrain_data_loader = data_generator.flow_from_dataframe(\n    trainFrame,\n    directory= '/kaggle/input/cassava-leaf-disease-classification/train_images',\n    x_col=\"image_id\",\n    y_col=\"label\",\n    target_size=(224, 224),\n    subset='training',\n    class_mode = \"sparse\"\n)\n\nval_data_loader = data_generator.flow_from_dataframe(\n    trainFrame,\n    directory= '/kaggle/input/cassava-leaf-disease-classification/train_images',\n    x_col=\"image_id\",\n    y_col=\"label\",\n    target_size=(224, 224),\n    subset='validation',\n    class_mode = \"sparse\"\n)\n\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#import matplotlib.pyplot as plt\n#import tensorflow as tf\nfrom tensorflow.keras import layers\n\n#batch_size = 100\n#img_height = 256\n#img_width = 256\n\n#training_ds = tf.keras.preprocessing.image_dataset_from_directory(\n #   '../trainDir',\n  #  validation_split=0.2,\n   # subset= \"training\",\n    #seed=42,\n    #image_size= (img_height, img_width),\n    #batch_size=batch_size\n\n#)\n\n#validation_ds = tf.keras.preprocessing.image_dataset_from_directory(\n#   '../trainDir',\n#    validation_split=0.2,\n#    subset= \"validation\",\n#    seed=42,\n#    image_size= (img_height, img_width),\n#    batch_size=batch_size\n\n#)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#class_names = training_ds.class_names\n#AUTOTUNE = tf.data.experimental.AUTOTUNE\n#training_ds = training_ds.cache().prefetch(buffer_size=AUTOTUNE)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## Defining Cnn\n## lets define our CNN\n### this model will not be used but other will\nMyCnn1 =  tf.keras.models.Sequential([\n    layers.Input(shape = (224, 224, 3), name = 'I1'),\n    layers.BatchNormalization(),\n    layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu'),\n    layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu'),\n    layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu'),\n    layers.MaxPooling2D(),\n    \n    layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu'),\n    layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu'),\n    layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu'),\n    layers.MaxPooling2D(),\n    \n    layers.Conv2D(filters=128, kernel_size=(3,3), activation='relu'),\n    layers.Conv2D(filters=128, kernel_size=(3,3), activation='relu'),\n    layers.Conv2D(filters=128, kernel_size=(3,3), activation='relu'),\n    layers.MaxPooling2D(),\n    \n    layers.Conv2D(filters=256, kernel_size=(3,3), activation='relu'),\n    layers.Conv2D(filters=256, kernel_size=(3,3), activation='relu'),\n    layers.Conv2D(filters=256, kernel_size=(3,3), activation='relu'),\n    layers.MaxPooling2D(),\n    \n    layers.Conv2D(filters=512, kernel_size=(3,3), activation='relu'),\n    layers.Conv2D(filters=512, kernel_size=(3,3), activation='relu'),\n    layers.Conv2D(filters=512, kernel_size=(3,3), activation='relu'),\n    layers.MaxPooling2D(),\n    \n    layers.Flatten(),\n    \n    layers.Dense(5, activation='softmax')\n])\nMyCnn1.compile(optimizer='adam',loss='categorical_crossentropy', metrics=['accuracy'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data_augmentation = tf.keras.Sequential([\n  tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal'),\n  tf.keras.layers.experimental.preprocessing.RandomRotation(0.2),\n])\n\n#preprocess_input = tf.keras.applications.mobilenet_v2.preprocess_input\npreprocess_input = tf.keras.applications.efficientnet.preprocess_input\nrescale = tf.keras.layers.experimental.preprocessing.Rescaling(1./127.5, offset= -1)\n\nIMG_SHAPE = (224, 224) + (3,)\nbase_model = tf.keras.applications.EfficientNetB7(input_shape=IMG_SHAPE,\n                                               include_top=False,\n                                               weights='../input/tfkerasefficientnetimagenetnotop/efficientnetb7_notop.h5')\n\nglobal_average_layer = tf.keras.layers.GlobalAveragePooling2D()\nprediction_layer = tf.keras.layers.Dense(5)\nbase_model.trainable = False\n\ninputs = tf.keras.Input(shape=(224, 224, 3))\nx = data_augmentation(inputs)\nx = rescale(x)\nx = preprocess_input(x)\nx = base_model(x, training=False)\nx = global_average_layer(x)\nx = tf.keras.layers.Dropout(.02)(x)\noutputs = prediction_layer(x)\nMyCnn = tf.keras.Model(inputs, outputs)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.utils.vis_utils import plot_model\nplot_model(MyCnn, to_file='model_plot.png', show_shapes=True, show_layer_names=True)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def scheduler(epoch, lr):\n    if epoch < 10:\n        return lr\n    else:\n        return lr * 0.3\n\n    \ncallback = tf.keras.callbacks.LearningRateScheduler(scheduler)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## training our model \nMyCnn.compile(optimizer= tf.keras.optimizers.Adam(lr=2e-5),\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\nretVal = MyCnn.fit(train_data_loader, validation_data= val_data_loader,callbacks=[callback], epochs = 4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(retVal.history['loss'], label = 'training loss')\nplt.plot(retVal.history['accuracy'], label = 'training accuracy')\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.plot(retVal.history['val_loss'], label = 'validation loss')\nplt.plot(retVal.history['val_accuracy'], label = 'validation accuracy')\nplt.legend()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"AnsFrame = {'image_id' : [],\n            'label' : []\n           }\n\ndef predictor():\n    path = '/kaggle/input/cassava-leaf-disease-classification/test_images'\n    files = os.listdir(path)\n    for mem in files:\n        img = Image.open(path + '/' + mem)\n        img = cv2.resize(np.asarray(img),(224, 224))\n        img = np.array([img/255])\n        snakeName = np.argmax (MyCnn.predict(img))\n        AnsFrame['image_id'].append(mem)\n        AnsFrame['label'].append (snakeName)\n        #print (snakeName)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"predictor()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"AnsFrame = pd.DataFrame(AnsFrame)\nAnsFrame.to_csv('submission.csv', index= False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"AnsFrame","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}