{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\nimport os\nimport glob\n\nimport sklearn\nfrom sklearn.metrics import confusion_matrix","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:11.167641Z","iopub.execute_input":"2022-02-18T14:05:11.168139Z","iopub.status.idle":"2022-02-18T14:05:18.315122Z","shell.execute_reply.started":"2022-02-18T14:05:11.168013Z","shell.execute_reply":"2022-02-18T14:05:18.314054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(\"../input/cassava-leaf-disease-classification/train.csv\")\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:18.318552Z","iopub.execute_input":"2022-02-18T14:05:18.319178Z","iopub.status.idle":"2022-02-18T14:05:18.369743Z","shell.execute_reply.started":"2022-02-18T14:05:18.319131Z","shell.execute_reply":"2022-02-18T14:05:18.368729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:18.371038Z","iopub.execute_input":"2022-02-18T14:05:18.372456Z","iopub.status.idle":"2022-02-18T14:05:18.404492Z","shell.execute_reply.started":"2022-02-18T14:05:18.372420Z","shell.execute_reply":"2022-02-18T14:05:18.403385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data['label'] = data['label'].astype('string')\ndata['iamge_id'] = data['image_id'].astype('string')\n\ndata.head()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-02-18T14:05:18.407384Z","iopub.execute_input":"2022-02-18T14:05:18.407814Z","iopub.status.idle":"2022-02-18T14:05:18.450941Z","shell.execute_reply.started":"2022-02-18T14:05:18.407764Z","shell.execute_reply":"2022-02-18T14:05:18.449892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def zoom_images(tensor):\n    tensor = tf.image.central_crop(tensor, 0.80)\n    return tensor","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:18.452557Z","iopub.execute_input":"2022-02-18T14:05:18.453476Z","iopub.status.idle":"2022-02-18T14:05:18.458879Z","shell.execute_reply.started":"2022-02-18T14:05:18.453430Z","shell.execute_reply":"2022-02-18T14:05:18.457821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for image in os.listdir('../input/cassava-leaf-disease-classification/train_images')[4:5]:\n    i = plt.imread(os.path.join('../input/cassava-leaf-disease-classification/train_images', image)) / 255.0\n    i = tf.image.resize(i, (400, 400))\n    print(zoom_images(i).shape)\n    plt.imshow(zoom_images(i))","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:18.460649Z","iopub.execute_input":"2022-02-18T14:05:18.461305Z","iopub.status.idle":"2022-02-18T14:05:22.201456Z","shell.execute_reply.started":"2022-02-18T14:05:18.461256Z","shell.execute_reply":"2022-02-18T14:05:22.200471Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_indexes = np.random.randint(0, 2000, 8)\n__dir = os.listdir('../input/cassava-leaf-disease-classification/train_images')\nli = [__dir[i] for i in random_indexes]\nfig, axes = plt.subplots(8, 2, figsize=(50, 50))\ncount = 0\nfor k in range(8):\n    img = plt.imread('../input/cassava-leaf-disease-classification/train_images/' + li[count]) / 255.0\n    for j in range(2):\n        axes[k][j].imshow(img)\n        img = zoom_images(img)\n        axes[k][j].imshow(img)\n    count += 1","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:22.202808Z","iopub.execute_input":"2022-02-18T14:05:22.203083Z","iopub.status.idle":"2022-02-18T14:05:28.963258Z","shell.execute_reply.started":"2022-02-18T14:05:22.203042Z","shell.execute_reply":"2022-02-18T14:05:28.961621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255., horizontal_flip=True, vertical_flip=True, fill_mode='nearest')\n\ntrain_generator = train_datagen.flow_from_dataframe(data[:int(len(data)*0.8)], x_col='image_id', y_col='label',\n                                                    directory='../input/cassava-leaf-disease-classification/train_images', \n                                                    batch_size=256, class_mode='categorical', validate_filenames=False, \n                                                    target_size=(400, 400), preprocessing_function=zoom_images)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:28.964944Z","iopub.execute_input":"2022-02-18T14:05:28.965531Z","iopub.status.idle":"2022-02-18T14:05:29.158612Z","shell.execute_reply.started":"2022-02-18T14:05:28.965482Z","shell.execute_reply":"2022-02-18T14:05:29.157355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_datagen = ImageDataGenerator(rescale=1./255.)\n\nvalidation_generator = validation_datagen.flow_from_dataframe(data[int(len(data)*0.8):], x_col='image_id', y_col='label', \n                                                              directory='../input/cassava-leaf-disease-classification/train_images', \n                                                              batch_size=256, class_mode='categorical', validate_filenames=False, \n                                                              target_size=(400, 400), preprocessing_function=zoom_images)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:29.160720Z","iopub.execute_input":"2022-02-18T14:05:29.161062Z","iopub.status.idle":"2022-02-18T14:05:29.203993Z","shell.execute_reply.started":"2022-02-18T14:05:29.161017Z","shell.execute_reply":"2022-02-18T14:05:29.202952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.applications.mobilenet_v2.MobileNetV2(weights='imagenet', include_top=False, input_shape=(320, 320, 3))","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:29.207642Z","iopub.execute_input":"2022-02-18T14:05:29.207932Z","iopub.status.idle":"2022-02-18T14:05:30.929351Z","shell.execute_reply.started":"2022-02-18T14:05:29.207892Z","shell.execute_reply":"2022-02-18T14:05:30.928344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_model(pre_trained_model):\n    \n    for layer in pre_trained_model.layers:\n        layer.trainable=False\n    \n    \n    \n    x = pre_trained_model.outputs[0]\n    x = layers.GlobalAveragePooling2D()(x)\n    x = layers.Dense(512, activation='relu')(x)\n    outputs = layers.Dense(5, activation='softmax')(x)\n    \n    model = tf.keras.Model(inputs=[pre_trained_model.inputs], outputs=[outputs], name='test_model')\n    return model\n\nmodel = build_model(model)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:30.930969Z","iopub.execute_input":"2022-02-18T14:05:30.931288Z","iopub.status.idle":"2022-02-18T14:05:30.980567Z","shell.execute_reply.started":"2022-02-18T14:05:30.931239Z","shell.execute_reply":"2022-02-18T14:05:30.979612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:30.982367Z","iopub.execute_input":"2022-02-18T14:05:30.982738Z","iopub.status.idle":"2022-02-18T14:05:31.102328Z","shell.execute_reply.started":"2022-02-18T14:05:30.982671Z","shell.execute_reply":"2022-02-18T14:05:31.101372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"METRICS = [\n      tf.keras.metrics.TruePositives(name='tp'),\n      tf.keras.metrics.FalsePositives(name='fp'),\n      tf.keras.metrics.TrueNegatives(name='tn'),\n      tf.keras.metrics.FalseNegatives(name='fn'), \n      tf.keras.metrics.BinaryAccuracy(name='accuracy'),\n      tf.keras.metrics.Precision(name='precision'),\n      tf.keras.metrics.Recall(name='recall'),\n      tf.keras.metrics.AUC(name='auc'),\n      tf.keras.metrics.AUC(name='prc', curve='PR'), # precision-recall curve\n]","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:31.103511Z","iopub.execute_input":"2022-02-18T14:05:31.104054Z","iopub.status.idle":"2022-02-18T14:05:31.143836Z","shell.execute_reply.started":"2022-02-18T14:05:31.104006Z","shell.execute_reply":"2022-02-18T14:05:31.142814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = tf.keras.callbacks.EarlyStopping(\n    monitor='recall', patience=2, mode='max', restore_best_weights=True, verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:31.145465Z","iopub.execute_input":"2022-02-18T14:05:31.145836Z","iopub.status.idle":"2022-02-18T14:05:31.151265Z","shell.execute_reply.started":"2022-02-18T14:05:31.145790Z","shell.execute_reply":"2022-02-18T14:05:31.150117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(),\n             loss=tf.keras.losses.CategoricalCrossentropy(),\n             metrics=METRICS)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:31.152920Z","iopub.execute_input":"2022-02-18T14:05:31.153481Z","iopub.status.idle":"2022-02-18T14:05:31.176486Z","shell.execute_reply.started":"2022-02-18T14:05:31.153431Z","shell.execute_reply":"2022-02-18T14:05:31.175518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_generator, epochs=10, callbacks=[early_stopping], validation_data=validation_generator)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T14:05:31.178758Z","iopub.execute_input":"2022-02-18T14:05:31.179281Z","iopub.status.idle":"2022-02-18T15:00:51.490927Z","shell.execute_reply.started":"2022-02-18T14:05:31.179235Z","shell.execute_reply":"2022-02-18T15:00:51.489956Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = np.arange(len(history.history['loss']))\nfig, axes = plt.subplots(1, 2, figsize=(15, 10))\naxes[0].plot(epochs, history.history['loss'], color='r', label='training_loss')\naxes[0].plot(epochs, history.history['val_loss'], color='b', label='validation_loss')\naxes[0].legend()\n\naxes[1].plot(epochs, history.history['accuracy'], color='r', label='training_acuuracy')\naxes[1].plot(epochs, history.history['val_accuracy'], color='b', label='validation_accuracy')\naxes[1].legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-18T15:00:51.492488Z","iopub.execute_input":"2022-02-18T15:00:51.493279Z","iopub.status.idle":"2022-02-18T15:00:51.910919Z","shell.execute_reply.started":"2022-02-18T15:00:51.493243Z","shell.execute_reply":"2022-02-18T15:00:51.909859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test = pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-18T15:02:10.480460Z","iopub.execute_input":"2022-02-18T15:02:10.480798Z","iopub.status.idle":"2022-02-18T15:02:10.499595Z","shell.execute_reply.started":"2022-02-18T15:02:10.480767Z","shell.execute_reply":"2022-02-18T15:02:10.498619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def final_predict(test_img, model):\n    test_image = plt.imread(test_img) / 255.0\n    test_image = tf.image.resize(test_image, (400, 400))\n    test_image = zoom_images(test_image)\n    test_image = tf.expand_dims(test_image, 0)\n    prediction = np.argmax(model.predict(test_image))\n    pred_df = pd.DataFrame({\"image_id\": ['2216849948.jpg'], \"label\": [prediction]})\n    \n    return pred_df\n\nprediction_df = final_predict('../input/cassava-leaf-disease-classification/test_images/2216849948.jpg', model)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-18T15:17:06.841423Z","iopub.execute_input":"2022-02-18T15:17:06.841746Z","iopub.status.idle":"2022-02-18T15:17:06.930838Z","shell.execute_reply.started":"2022-02-18T15:17:06.841675Z","shell.execute_reply":"2022-02-18T15:17:06.929878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_df.to_csv('Final_Submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-18T15:18:05.559835Z","iopub.execute_input":"2022-02-18T15:18:05.560217Z","iopub.status.idle":"2022-02-18T15:18:05.570107Z","shell.execute_reply.started":"2022-02-18T15:18:05.560186Z","shell.execute_reply":"2022-02-18T15:18:05.568884Z"},"trusted":true},"execution_count":null,"outputs":[]}]}