{"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":"markdown","source":"<br>\n<h1 style = \"font-size:40px; font-family:Garamond ; font-weight : normal; background-color: #C66363 ; color : #E8D6D8; text-align: center; border-radius: 100px 100px;\">CONTENT </h1>\n<br>\n\n* [Add Libaries](#1)\n* [Preproccess](#2)\n* [Prepare And Run Model](#3)\n* [Visualize Model Results](#4)","metadata":{}},{"cell_type":"markdown","source":"<a id=\"1\"> </a>\n# Add Libaries\n","metadata":{}},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom collections import Counter\nfrom tensorflow import keras\nfrom keras.models import Sequential,load_model\nfrom keras.layers import Dense,GlobalAveragePooling2D,Flatten,Conv2D,BatchNormalization,Dropout,MaxPooling2D,Activation\nfrom keras.preprocessing import image\nfrom keras.preprocessing.image import ImageDataGenerator as Imgen\n\nfrom PIL import Image\nfrom sklearn.metrics import confusion_matrix,classification_report\n\nimport os\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-29T09:20:39.705611Z","iopub.execute_input":"2021-07-29T09:20:39.706070Z","iopub.status.idle":"2021-07-29T09:20:47.395152Z","shell.execute_reply.started":"2021-07-29T09:20:39.706027Z","shell.execute_reply":"2021-07-29T09:20:47.394276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"2\"> </a>\n# Preproccess\n","metadata":{}},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/plant-pathology-2021-fgvc8/train.csv\")\ntrain_path = \"../input/plant-pathology-2021-fgvc8/train_images\"\ntest_path = \"../input/plant-pathology-2021-fgvc8/test_images\"","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:20:48.850286Z","iopub.execute_input":"2021-07-29T09:20:48.850906Z","iopub.status.idle":"2021-07-29T09:20:48.900686Z","shell.execute_reply.started":"2021-07-29T09:20:48.850862Z","shell.execute_reply":"2021-07-29T09:20:48.899172Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(3)","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:34:26.244287Z","iopub.execute_input":"2021-07-28T17:34:26.244693Z","iopub.status.idle":"2021-07-28T17:34:26.266754Z","shell.execute_reply.started":"2021-07-28T17:34:26.244651Z","shell.execute_reply":"2021-07-28T17:34:26.265850Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:34:26.268341Z","iopub.execute_input":"2021-07-28T17:34:26.268689Z","iopub.status.idle":"2021-07-28T17:34:26.287865Z","shell.execute_reply.started":"2021-07-28T17:34:26.268655Z","shell.execute_reply":"2021-07-28T17:34:26.286875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = train_df[\"labels\"].value_counts().index\ndata = train_df[\"labels\"].value_counts().values","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:21:42.026521Z","iopub.execute_input":"2021-07-29T09:21:42.027036Z","iopub.status.idle":"2021-07-29T09:21:42.042685Z","shell.execute_reply.started":"2021-07-29T09:21:42.026995Z","shell.execute_reply":"2021-07-29T09:21:42.041590Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:21:46.630593Z","iopub.execute_input":"2021-07-29T09:21:46.631371Z","iopub.status.idle":"2021-07-29T09:21:46.637663Z","shell.execute_reply.started":"2021-07-29T09:21:46.631331Z","shell.execute_reply":"2021-07-29T09:21:46.636567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"explode = (0, 0.1, 0, 0,0,0,0,0,0,0,0,0)\npie, ax = plt.subplots(figsize=[10,6])\nplt.pie(x=data, autopct=\"%.1f%%\",explode=explode, labels=labels, pctdistance=0.5)\nplt.title(\"Delivery Tips by type\", fontsize=14);\npie.savefig(\"DeliveryPieChart.png\")","metadata":{"execution":{"iopub.status.busy":"2021-07-29T09:30:04.599991Z","iopub.execute_input":"2021-07-29T09:30:04.600555Z","iopub.status.idle":"2021-07-29T09:30:04.868630Z","shell.execute_reply.started":"2021-07-29T09:30:04.600518Z","shell.execute_reply":"2021-07-29T09:30:04.867474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_num = len(train_df[\"labels\"].value_counts())","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:34:26.302030Z","iopub.execute_input":"2021-07-28T17:34:26.302426Z","iopub.status.idle":"2021-07-28T17:34:26.312364Z","shell.execute_reply.started":"2021-07-28T17:34:26.302390Z","shell.execute_reply":"2021-07-28T17:34:26.311481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = Imgen(preprocessing_function=keras.applications.xception.preprocess_input,\n                 rotation_range=4,\n                  shear_range=0.2,\n                  zoom_range=0.15,\n                  horizontal_flip=True,\n                  validation_split=0.2\n                 )","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:34:26.313675Z","iopub.execute_input":"2021-07-28T17:34:26.314050Z","iopub.status.idle":"2021-07-28T17:34:26.321611Z","shell.execute_reply.started":"2021-07-28T17:34:26.314014Z","shell.execute_reply":"2021-07-28T17:34:26.320801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = datagen.flow_from_dataframe(\n    train_df,\n    directory = train_path, \n    x_col = 'image',\n    y_col = 'labels',\n    subset=\"training\",\n    color_mode=\"rgb\",\n    target_size = (224,224),\n    class_mode=\"categorical\",\n    batch_size=32,\n    shuffle=True,\n    seed=123\n)\n\n\nval_ds = datagen.flow_from_dataframe(\n    train_df,\n    directory = train_path,\n    x_col = 'image',\n    y_col = 'labels',\n    subset=\"validation\",\n    color_mode=\"rgb\",\n    target_size = (224,224),\n    class_mode=\"categorical\",\n    batch_size=32,\n    shuffle=True,\n    seed=123\n)","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:34:26.325576Z","iopub.execute_input":"2021-07-28T17:34:26.325970Z","iopub.status.idle":"2021-07-28T17:34:47.925529Z","shell.execute_reply.started":"2021-07-28T17:34:26.325938Z","shell.execute_reply":"2021-07-28T17:34:47.924701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x,y = next(train_ds)\nx.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:34:47.927142Z","iopub.execute_input":"2021-07-28T17:34:47.927417Z","iopub.status.idle":"2021-07-28T17:34:53.216801Z","shell.execute_reply.started":"2021-07-28T17:34:47.927390Z","shell.execute_reply":"2021-07-28T17:34:53.216015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"3\"> </a>\n# Prepare And Run Model","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg16 import VGG16","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:34:53.218102Z","iopub.execute_input":"2021-07-28T17:34:53.218460Z","iopub.status.idle":"2021-07-28T17:34:53.222557Z","shell.execute_reply.started":"2021-07-28T17:34:53.218424Z","shell.execute_reply":"2021-07-28T17:34:53.221557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg = VGG16()","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:34:53.224152Z","iopub.execute_input":"2021-07-28T17:34:53.224701Z","iopub.status.idle":"2021-07-28T17:35:05.750130Z","shell.execute_reply.started":"2021-07-28T17:34:53.224660Z","shell.execute_reply":"2021-07-28T17:35:05.749245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nvgg_layers = vgg.layers\n\nfor i in range(len(vgg_layers)-1):\n    model.add(vgg_layers[i])","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:35:05.751459Z","iopub.execute_input":"2021-07-28T17:35:05.751805Z","iopub.status.idle":"2021-07-28T17:35:05.984633Z","shell.execute_reply.started":"2021-07-28T17:35:05.751753Z","shell.execute_reply":"2021-07-28T17:35:05.983841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:35:05.985917Z","iopub.execute_input":"2021-07-28T17:35:05.986280Z","iopub.status.idle":"2021-07-28T17:35:06.002935Z","shell.execute_reply.started":"2021-07-28T17:35:05.986245Z","shell.execute_reply":"2021-07-28T17:35:06.001857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for l in vgg_layers:\n    l.trainable = False","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:35:06.004115Z","iopub.execute_input":"2021-07-28T17:35:06.004461Z","iopub.status.idle":"2021-07-28T17:35:06.009410Z","shell.execute_reply.started":"2021-07-28T17:35:06.004426Z","shell.execute_reply":"2021-07-28T17:35:06.008382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Dense(class_num , activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:35:06.010733Z","iopub.execute_input":"2021-07-28T17:35:06.011131Z","iopub.status.idle":"2021-07-28T17:35:06.026120Z","shell.execute_reply.started":"2021-07-28T17:35:06.011096Z","shell.execute_reply":"2021-07-28T17:35:06.025372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:35:06.027185Z","iopub.execute_input":"2021-07-28T17:35:06.027501Z","iopub.status.idle":"2021-07-28T17:35:06.041814Z","shell.execute_reply.started":"2021-07-28T17:35:06.027468Z","shell.execute_reply":"2021-07-28T17:35:06.039214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss = \"categorical_crossentropy\" , optimizer = \"adam\" , metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:35:06.043071Z","iopub.execute_input":"2021-07-28T17:35:06.043499Z","iopub.status.idle":"2021-07-28T17:35:06.058164Z","shell.execute_reply.started":"2021-07-28T17:35:06.043464Z","shell.execute_reply":"2021-07-28T17:35:06.057268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 32","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:35:06.059418Z","iopub.execute_input":"2021-07-28T17:35:06.059837Z","iopub.status.idle":"2021-07-28T17:35:06.064074Z","shell.execute_reply.started":"2021-07-28T17:35:06.059798Z","shell.execute_reply":"2021-07-28T17:35:06.063062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hist = model.fit_generator(train_ds,\n                           steps_per_epoch = 14900//batch_size,\n                           epochs = 15,\n                           validation_data = val_ds,\n                           validation_steps = 3700//batch_size)","metadata":{"execution":{"iopub.status.busy":"2021-07-28T17:35:06.065392Z","iopub.execute_input":"2021-07-28T17:35:06.065822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"> </a>\n# Visualize Model Results","metadata":{}},{"cell_type":"code","source":"plt.plot(hist.history[\"acc\"],label = \"training acc\")\nplt.plot(hist.history[\"val_acc\"],label = \"validation acc\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}