{"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\nimport keras\nfrom keras.models import Sequential, Model\nfrom keras import layers, applications, optimizers\nfrom keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.applications import EfficientNetB3\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport os","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.status.busy":"2022-05-08T04:13:41.741911Z","iopub.execute_input":"2022-05-08T04:13:41.742736Z","iopub.status.idle":"2022-05-08T04:13:47.164743Z","shell.execute_reply.started":"2022-05-08T04:13:41.742633Z","shell.execute_reply":"2022-05-08T04:13:47.164016Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_size = 350\nbatch_size = 16\nsave_model_filename = 'effnet_(1).h5'","metadata":{"execution":{"iopub.status.busy":"2022-05-08T04:13:47.166440Z","iopub.execute_input":"2022-05-08T04:13:47.166699Z","iopub.status.idle":"2022-05-08T04:13:47.171840Z","shell.execute_reply.started":"2022-05-08T04:13:47.166665Z","shell.execute_reply":"2022-05-08T04:13:47.171131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\ntrain_path = '../input/cassava-leaf-disease-classification/train_images'","metadata":{"execution":{"iopub.status.busy":"2022-05-08T04:13:47.173310Z","iopub.execute_input":"2022-05-08T04:13:47.173585Z","iopub.status.idle":"2022-05-08T04:13:47.211285Z","shell.execute_reply.started":"2022-05-08T04:13:47.173547Z","shell.execute_reply":"2022-05-08T04:13:47.210666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def image_path(image):\n    return os.path.join(train_path,image)\n\ntrain['image_id'] = train['image_id'].apply(image_path)","metadata":{"execution":{"iopub.status.busy":"2022-05-08T04:13:47.213259Z","iopub.execute_input":"2022-05-08T04:13:47.213506Z","iopub.status.idle":"2022-05-08T04:13:47.262727Z","shell.execute_reply.started":"2022-05-08T04:13:47.213472Z","shell.execute_reply":"2022-05-08T04:13:47.262119Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['label'] = train['label'].astype('str')","metadata":{"execution":{"iopub.status.busy":"2022-05-08T04:13:47.263708Z","iopub.execute_input":"2022-05-08T04:13:47.264008Z","iopub.status.idle":"2022-05-08T04:13:47.288860Z","shell.execute_reply.started":"2022-05-08T04:13:47.263972Z","shell.execute_reply":"2022-05-08T04:13:47.288111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_gen = ImageDataGenerator(preprocessing_function = tf.keras.applications.efficientnet.preprocess_input,\n                                horizontal_flip=True, vertical_flip=True, fill_mode='nearest', brightness_range=[0.7, 1.3],\n                                rotation_range=270, zoom_range=0.2, shear_range=10, width_shift_range=0.2, height_shift_range=0.2,\n                                validation_split=0.2, rescale = 1./255)\n\ntest_gen = ImageDataGenerator(rescale=1./255)","metadata":{"execution":{"iopub.status.busy":"2022-05-08T04:13:47.290182Z","iopub.execute_input":"2022-05-08T04:13:47.290559Z","iopub.status.idle":"2022-05-08T04:13:47.297720Z","shell.execute_reply.started":"2022-05-08T04:13:47.290501Z","shell.execute_reply":"2022-05-08T04:13:47.296743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = image_gen.flow_from_dataframe(dataframe=train, directory=None, x_col='image_id', y_col='label',\n                                                subset='training', batch_size=batch_size, seed=1,\n                                                shuffle=True, class_mode='categorical', target_size=(image_size,image_size))\n\nvalidation_generator = image_gen.flow_from_dataframe(dataframe=train, directory=None, x_col='image_id', y_col='label',\n                                                   subset='validation', batch_size=batch_size, seed=1,\n                                                   shuffle=False, class_mode='categorical', target_size=(image_size,image_size))","metadata":{"execution":{"iopub.status.busy":"2022-05-08T04:13:47.299212Z","iopub.execute_input":"2022-05-08T04:13:47.299515Z","iopub.status.idle":"2022-05-08T04:14:31.242939Z","shell.execute_reply.started":"2022-05-08T04:13:47.299478Z","shell.execute_reply":"2022-05-08T04:14:31.242205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def build_efficientnet_b3():\n    model = Sequential()\n    model.add(EfficientNetB3(input_shape=(image_size,image_size,3), include_top=False, weights='imagenet'))\n    model.add(GlobalAveragePooling2D())\n    model.add(Dense(256, activation='relu'))\n    model.add(Dropout(0.5))\n    model.add(Dense(5, activation='softmax'))\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2022-05-08T04:14:31.244043Z","iopub.execute_input":"2022-05-08T04:14:31.244455Z","iopub.status.idle":"2022-05-08T04:14:31.251411Z","shell.execute_reply.started":"2022-05-08T04:14:31.244416Z","shell.execute_reply":"2022-05-08T04:14:31.250572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_efficientnet_b3()\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-08T04:14:31.252979Z","iopub.execute_input":"2022-05-08T04:14:31.253247Z","iopub.status.idle":"2022-05-08T04:14:37.401335Z","shell.execute_reply.started":"2022-05-08T04:14:31.253211Z","shell.execute_reply":"2022-05-08T04:14:37.400635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    loss='categorical_crossentropy',\n    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),\n    metrics=['accuracy']\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-08T04:14:37.405240Z","iopub.execute_input":"2022-05-08T04:14:37.405432Z","iopub.status.idle":"2022-05-08T04:14:37.423441Z","shell.execute_reply.started":"2022-05-08T04:14:37.405407Z","shell.execute_reply":"2022-05-08T04:14:37.422802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau\n\nearly_stopping = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=5, restore_best_weights=True)\nmodel_checkpoint = ModelCheckpoint(save_model_filename, monitor='val_loss', mode='min', verbose=1, save_best_only=True)\nreduce_lr = ReduceLROnPlateau(monitor='val_loss', mode='min', factor=0.2, patience=2, min_lr=0, verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-05-08T04:14:37.424754Z","iopub.execute_input":"2022-05-08T04:14:37.424990Z","iopub.status.idle":"2022-05-08T04:14:37.432755Z","shell.execute_reply.started":"2022-05-08T04:14:37.424957Z","shell.execute_reply":"2022-05-08T04:14:37.432037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n\nsns.countplot(train['label'])\nplt.title('Count of disease types')\nplt.grid()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-08T04:14:37.434024Z","iopub.execute_input":"2022-05-08T04:14:37.434280Z","iopub.status.idle":"2022-05-08T04:14:38.228147Z","shell.execute_reply.started":"2022-05-08T04:14:37.434245Z","shell.execute_reply":"2022-05-08T04:14:38.227463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epoch = 30\n\nhistory = model.fit(\n    train_generator,\n    epochs=epoch,\n    validation_data=validation_generator,\n    verbose=1,\n    callbacks=[early_stopping, model_checkpoint, reduce_lr]\n)","metadata":{"execution":{"iopub.status.busy":"2022-05-08T04:14:38.229605Z","iopub.execute_input":"2022-05-08T04:14:38.230078Z","iopub.status.idle":"2022-05-08T10:50:58.603952Z","shell.execute_reply.started":"2022-05-08T04:14:38.230039Z","shell.execute_reply":"2022-05-08T10:50:58.603200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('train loss:', history.history['loss'][-1])\nprint('train accuracy:', history.history['accuracy'][-1])\n\nprint('dev loss:', history.history['val_loss'][-1])\nprint('dev accuracy:', history.history['val_accuracy'][-1])\n\nresults = pd.DataFrame(history.history)\n\nfig, axs = plt.subplots(1,2,figsize=(15,5))\n\naxs[0].plot(results[['loss', 'val_loss']])\naxs[0].set_title('Model Loss')\n\naxs[1].plot(results[['accuracy', 'val_accuracy']])\naxs[1].set_title('Model Accuracy')\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-08T10:50:58.605132Z","iopub.execute_input":"2022-05-08T10:50:58.605371Z","iopub.status.idle":"2022-05-08T10:50:58.867840Z","shell.execute_reply.started":"2022-05-08T10:50:58.605336Z","shell.execute_reply":"2022-05-08T10:50:58.867108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(validation_generator)\npredictions = np.argmax(pred, axis=1)\nactual = validation_generator.classes\n\nfrom sklearn.metrics import classification_report\n\nreport = classification_report(actual, predictions, target_names=['CBB', 'CBSD', 'CGM', 'CMD', 'Healthy'])\nprint(report)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-05-08T10:50:58.870155Z","iopub.execute_input":"2022-05-08T10:50:58.870577Z","iopub.status.idle":"2022-05-08T10:53:30.118263Z","shell.execute_reply.started":"2022-05-08T10:50:58.870536Z","shell.execute_reply":"2022-05-08T10:53:30.117573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/cassava-leaf-disease-classification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-05-08T10:53:30.119415Z","iopub.execute_input":"2022-05-08T10:53:30.119702Z","iopub.status.idle":"2022-05-08T10:53:30.130277Z","shell.execute_reply.started":"2022-05-08T10:53:30.119662Z","shell.execute_reply":"2022-05-08T10:53:30.129353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_path = '../input/cassava-leaf-disease-classification/test_images'\n\ndef test_image_path(image):\n    return os.path.join(test_path,image)\n\ntest['image_id'] = test['image_id'].apply(test_image_path)\n\ntest['label'] = test['label'].astype('str')","metadata":{"execution":{"iopub.status.busy":"2022-05-08T10:53:30.131436Z","iopub.execute_input":"2022-05-08T10:53:30.132543Z","iopub.status.idle":"2022-05-08T10:53:30.139242Z","shell.execute_reply.started":"2022-05-08T10:53:30.132482Z","shell.execute_reply":"2022-05-08T10:53:30.138534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = test_gen.flow_from_dataframe(dataframe=test, directory=None, x_col='image_id', y_col='label',\n                                              preprocessing_function=applications.efficientnet.preprocess_input,\n                                              class_mode='categorical', target_size=(image_size,image_size))","metadata":{"execution":{"iopub.status.busy":"2022-05-08T10:53:30.140700Z","iopub.execute_input":"2022-05-08T10:53:30.141294Z","iopub.status.idle":"2022-05-08T10:53:30.156795Z","shell.execute_reply.started":"2022-05-08T10:53:30.141256Z","shell.execute_reply":"2022-05-08T10:53:30.156171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = model.predict(test_generator)","metadata":{"execution":{"iopub.status.busy":"2022-05-08T10:53:30.157879Z","iopub.execute_input":"2022-05-08T10:53:30.158574Z","iopub.status.idle":"2022-05-08T10:53:30.468147Z","shell.execute_reply.started":"2022-05-08T10:53:30.158531Z","shell.execute_reply":"2022-05-08T10:53:30.467298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame()\nsubmission['image_id'] = list(os.listdir(test_path))\nsubmission['label'] = np.argmax(output, axis=1)\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-08T10:53:30.469680Z","iopub.execute_input":"2022-05-08T10:53:30.469904Z","iopub.status.idle":"2022-05-08T10:53:30.480233Z","shell.execute_reply.started":"2022-05-08T10:53:30.469872Z","shell.execute_reply":"2022-05-08T10:53:30.479546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-08T10:53:30.481491Z","iopub.execute_input":"2022-05-08T10:53:30.481768Z","iopub.status.idle":"2022-05-08T10:53:30.492875Z","shell.execute_reply.started":"2022-05-08T10:53:30.481729Z","shell.execute_reply":"2022-05-08T10:53:30.492236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('model_final.h5')","metadata":{"execution":{"iopub.status.busy":"2022-05-08T10:53:30.494054Z","iopub.execute_input":"2022-05-08T10:53:30.494419Z","iopub.status.idle":"2022-05-08T10:53:31.519114Z","shell.execute_reply.started":"2022-05-08T10:53:30.494386Z","shell.execute_reply":"2022-05-08T10:53:31.518372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}