{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        os.path.join(dirname, filename)\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-28T13:52:54.748194Z","iopub.execute_input":"2022-02-28T13:52:54.748834Z","iopub.status.idle":"2022-02-28T13:53:20.660352Z","shell.execute_reply.started":"2022-02-28T13:52:54.748722Z","shell.execute_reply":"2022-02-28T13:53:20.659572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-28T13:53:20.662167Z","iopub.execute_input":"2022-02-28T13:53:20.66245Z","iopub.status.idle":"2022-02-28T13:53:25.852558Z","shell.execute_reply.started":"2022-02-28T13:53:20.662413Z","shell.execute_reply":"2022-02-28T13:53:25.851783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(r\"../input/cassava-leaf-disease-classification/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-02-28T13:53:25.859141Z","iopub.execute_input":"2022-02-28T13:53:25.859613Z","iopub.status.idle":"2022-02-28T13:53:25.894333Z","shell.execute_reply.started":"2022-02-28T13:53:25.859576Z","shell.execute_reply":"2022-02-28T13:53:25.893696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-02-28T13:53:25.895575Z","iopub.execute_input":"2022-02-28T13:53:25.895816Z","iopub.status.idle":"2022-02-28T13:53:25.914902Z","shell.execute_reply.started":"2022-02-28T13:53:25.895783Z","shell.execute_reply":"2022-02-28T13:53:25.914296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.info()","metadata":{"execution":{"iopub.status.busy":"2022-02-28T13:53:25.915987Z","iopub.execute_input":"2022-02-28T13:53:25.916297Z","iopub.status.idle":"2022-02-28T13:53:25.939631Z","shell.execute_reply.started":"2022-02-28T13:53:25.916261Z","shell.execute_reply":"2022-02-28T13:53:25.938969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['image_id'] = df['image_id'].astype('string')\ndf['label'] = df['label'].astype('string')","metadata":{"execution":{"iopub.status.busy":"2022-02-28T13:53:25.940729Z","iopub.execute_input":"2022-02-28T13:53:25.941115Z","iopub.status.idle":"2022-02-28T13:53:25.96985Z","shell.execute_reply.started":"2022-02-28T13:53:25.941079Z","shell.execute_reply":"2022-02-28T13:53:25.969223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import Input,Lambda,Dense,Flatten\nfrom keras.models import Model\nfrom keras.preprocessing import image\nfrom keras.preprocessing.image import ImageDataGenerator\nimport numpy as np\nfrom glob import glob\nimport matplotlib.pyplot as plt\nimport warnings\nfrom PIL import Image\nwarnings.filterwarnings(\"ignore\",category=FutureWarning)\nimport os\nimport random\nfrom keras.preprocessing.image import load_img\nimport tensorflow as tf\nfrom PIL import Image\nimport cv2","metadata":{"execution":{"iopub.status.busy":"2022-02-28T13:53:25.971045Z","iopub.execute_input":"2022-02-28T13:53:25.971279Z","iopub.status.idle":"2022-02-28T13:53:26.194829Z","shell.execute_reply.started":"2022-02-28T13:53:25.971247Z","shell.execute_reply":"2022-02-28T13:53:26.194195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(\n                                     rescale=1./255,\n                                     validation_split = 0.2,\n                                     zoom_range = 0.2,\n                                     horizontal_flip = True,\n                                     vertical_flip = True,\n                                     fill_mode = 'nearest',\n                                     shear_range = 0.1,\n                                     height_shift_range = 0.1,\n                                     width_shift_range = 0.1)\n\ntrain_generator = train_datagen.flow_from_dataframe(df,\n                         directory = \"../input/cassava-leaf-disease-classification/train_images\",\n                         subset = \"training\",\n                         x_col = \"image_id\",\n                         y_col = \"label\",\n                         target_size = (400,400),\n                         batch_size = 256,\n                         class_mode = \"categorical\")\n\nvalidation_datagen = ImageDataGenerator(rescale=1./255,\n                                        validation_split = 0.2)\n\nvalidation_generator = validation_datagen.flow_from_dataframe(df,\n                         directory = \"../input/cassava-leaf-disease-classification/train_images\",\n                         subset = \"validation\",\n                         x_col = \"image_id\",\n                         y_col = \"label\",\n                         target_size = (400,400),\n                         batch_size = 256,\n                         class_mode = \"categorical\")","metadata":{"execution":{"iopub.status.busy":"2022-02-28T13:53:26.197539Z","iopub.execute_input":"2022-02-28T13:53:26.197762Z","iopub.status.idle":"2022-02-28T13:53:42.544006Z","shell.execute_reply.started":"2022-02-28T13:53:26.19773Z","shell.execute_reply":"2022-02-28T13:53:42.543253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2022-02-28T13:53:42.54526Z","iopub.execute_input":"2022-02-28T13:53:42.54576Z","iopub.status.idle":"2022-02-28T13:53:42.671107Z","shell.execute_reply.started":"2022-02-28T13:53:42.54572Z","shell.execute_reply":"2022-02-28T13:53:42.670408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12,6))\nsns.set_style('white')\nsns.countplot(x=\"label\",data=df)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-28T13:53:42.672407Z","iopub.execute_input":"2022-02-28T13:53:42.672667Z","iopub.status.idle":"2022-02-28T13:53:42.90264Z","shell.execute_reply.started":"2022-02-28T13:53:42.672631Z","shell.execute_reply":"2022-02-28T13:53:42.901958Z"},"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-28T13:53:42.903831Z","iopub.execute_input":"2022-02-28T13:53:42.904627Z","iopub.status.idle":"2022-02-28T13:53:46.265551Z","shell.execute_reply.started":"2022-02-28T13:53:42.904585Z","shell.execute_reply":"2022-02-28T13:53:46.264817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in model.layers:\n        layer.trainable=False","metadata":{"execution":{"iopub.status.busy":"2022-02-28T13:53:46.266738Z","iopub.execute_input":"2022-02-28T13:53:46.266993Z","iopub.status.idle":"2022-02-28T13:53:46.278299Z","shell.execute_reply.started":"2022-02-28T13:53:46.266961Z","shell.execute_reply":"2022-02-28T13:53:46.277569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = model.outputs[0]\nx = layers.GlobalAveragePooling2D()(x)\nx = layers.Dense(512, activation='relu')(x)\nmodel_output = layers.Dense(5, activation='softmax')(x)\nmodel = Model(inputs=model.input, outputs=model_output)","metadata":{"execution":{"iopub.status.busy":"2022-02-28T13:53:46.279863Z","iopub.execute_input":"2022-02-28T13:53:46.28042Z","iopub.status.idle":"2022-02-28T13:53:46.314129Z","shell.execute_reply.started":"2022-02-28T13:53:46.280384Z","shell.execute_reply":"2022-02-28T13:53:46.313507Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-28T13:53:46.317328Z","iopub.execute_input":"2022-02-28T13:53:46.317526Z","iopub.status.idle":"2022-02-28T13:53:46.388035Z","shell.execute_reply.started":"2022-02-28T13:53:46.317503Z","shell.execute_reply":"2022-02-28T13:53:46.387366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='Adam',\n              loss='categorical_crossentropy',\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-02-28T13:53:46.388988Z","iopub.execute_input":"2022-02-28T13:53:46.389402Z","iopub.status.idle":"2022-02-28T13:53:46.406555Z","shell.execute_reply.started":"2022-02-28T13:53:46.389366Z","shell.execute_reply":"2022-02-28T13:53:46.405903Z"},"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-28T13:53:46.407597Z","iopub.execute_input":"2022-02-28T13:53:46.407849Z","iopub.status.idle":"2022-02-28T13:53:46.412007Z","shell.execute_reply.started":"2022-02-28T13:53:46.407815Z","shell.execute_reply":"2022-02-28T13:53:46.411287Z"},"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-28T13:53:46.413078Z","iopub.execute_input":"2022-02-28T13:53:46.413736Z","iopub.status.idle":"2022-02-28T15:54:57.805589Z","shell.execute_reply.started":"2022-02-28T13:53:46.4137Z","shell.execute_reply":"2022-02-28T15:54:57.804774Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'],label='train loss')\nplt.plot(history.history['val_loss'],label = 'val loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-28T15:54:57.808462Z","iopub.execute_input":"2022-02-28T15:54:57.8088Z","iopub.status.idle":"2022-02-28T15:54:58.026789Z","shell.execute_reply.started":"2022-02-28T15:54:57.80876Z","shell.execute_reply":"2022-02-28T15:54:58.026144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'],label='train acc')\nplt.plot(history.history['val_accuracy'],label='val acc')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-28T15:54:58.028137Z","iopub.execute_input":"2022-02-28T15:54:58.028613Z","iopub.status.idle":"2022-02-28T15:54:58.243859Z","shell.execute_reply.started":"2022-02-28T15:54:58.028576Z","shell.execute_reply":"2022-02-28T15:54:58.243181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss = pd.read_csv(os.path.join(\"../input/cassava-leaf-disease-classification\", \"sample_submission.csv\"))\nss","metadata":{"execution":{"iopub.status.busy":"2022-02-28T16:00:49.153868Z","iopub.execute_input":"2022-02-28T16:00:49.154157Z","iopub.status.idle":"2022-02-28T16:00:49.179333Z","shell.execute_reply.started":"2022-02-28T16:00:49.154126Z","shell.execute_reply":"2022-02-28T16:00:49.178526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = []\n\nfor image_id in ss.image_id:\n    image = Image.open(os.path.join(\"../input/cassava-leaf-disease-classification\",  \"test_images\", image_id))\n    image = image.resize((320,320))\n    image = np.expand_dims(image, axis = 0)\n    preds.append(np.argmax(model.predict(image)))\n\nss['label'] = preds\nss","metadata":{"execution":{"iopub.status.busy":"2022-02-28T16:01:21.924453Z","iopub.execute_input":"2022-02-28T16:01:21.925222Z","iopub.status.idle":"2022-02-28T16:01:22.718192Z","shell.execute_reply.started":"2022-02-28T16:01:21.925182Z","shell.execute_reply":"2022-02-28T16:01:22.717481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss.to_csv('submission.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2022-02-28T16:01:26.050973Z","iopub.execute_input":"2022-02-28T16:01:26.051337Z","iopub.status.idle":"2022-02-28T16:01:26.059531Z","shell.execute_reply.started":"2022-02-28T16:01:26.051277Z","shell.execute_reply":"2022-02-28T16:01:26.058717Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}