{"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 numpy as np\nimport pandas as pd\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nsns.set_style('darkgrid')\n\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","metadata":{"execution":{"iopub.status.busy":"2021-06-03T05:19:26.254839Z","iopub.execute_input":"2021-06-03T05:19:26.255196Z","iopub.status.idle":"2021-06-03T05:19:26.262249Z","shell.execute_reply.started":"2021-06-03T05:19:26.255165Z","shell.execute_reply":"2021-06-03T05:19:26.261026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/plant-pathology-2021-fgvc8/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-06-03T05:08:34.111784Z","iopub.execute_input":"2021-06-03T05:08:34.112155Z","iopub.status.idle":"2021-06-03T05:08:34.137282Z","shell.execute_reply.started":"2021-06-03T05:08:34.112121Z","shell.execute_reply":"2021-06-03T05:08:34.136659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-03T05:08:34.139481Z","iopub.execute_input":"2021-06-03T05:08:34.139826Z","iopub.status.idle":"2021-06-03T05:08:34.146635Z","shell.execute_reply.started":"2021-06-03T05:08:34.139792Z","shell.execute_reply":"2021-06-03T05:08:34.145706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.labels.value_counts()","metadata":{"execution":{"iopub.status.busy":"2021-06-03T05:08:34.148964Z","iopub.execute_input":"2021-06-03T05:08:34.149335Z","iopub.status.idle":"2021-06-03T05:08:34.172929Z","shell.execute_reply.started":"2021-06-03T05:08:34.149290Z","shell.execute_reply":"2021-06-03T05:08:34.172007Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')\nsample.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-03T05:08:34.174120Z","iopub.execute_input":"2021-06-03T05:08:34.174612Z","iopub.status.idle":"2021-06-03T05:08:34.202414Z","shell.execute_reply.started":"2021-06-03T05:08:34.174574Z","shell.execute_reply":"2021-06-03T05:08:34.201601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Image Data Gen**","metadata":{}},{"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.2,\n                  horizontal_flip=True,\n                  validation_split=0.2\n                 )","metadata":{"execution":{"iopub.status.busy":"2021-06-03T05:08:34.206712Z","iopub.execute_input":"2021-06-03T05:08:34.208805Z","iopub.status.idle":"2021-06-03T05:08:34.218153Z","shell.execute_reply.started":"2021-06-03T05:08:34.208767Z","shell.execute_reply":"2021-06-03T05:08:34.216229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Data Generation**","metadata":{}},{"cell_type":"code","source":"train_ds = datagen.flow_from_dataframe(\n    train_df,\n    directory = '../input/resized-plant2021/img_sz_256', \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 = '../input/resized-plant2021/img_sz_256',\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-06-03T05:28:20.346451Z","iopub.execute_input":"2021-06-03T05:28:20.346818Z","iopub.status.idle":"2021-06-03T05:29:10.014224Z","shell.execute_reply.started":"2021-06-03T05:28:20.346782Z","shell.execute_reply":"2021-06-03T05:29:10.013295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"a = train_ds.class_indices\nclass_names = list(a.keys())\nclass_names","metadata":{"execution":{"iopub.status.busy":"2021-06-03T05:29:17.105404Z","iopub.execute_input":"2021-06-03T05:29:17.105720Z","iopub.status.idle":"2021-06-03T05:29:17.111469Z","shell.execute_reply.started":"2021-06-03T05:29:17.105689Z","shell.execute_reply":"2021-06-03T05:29:17.110635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x,y = next(train_ds)\nx.shape","metadata":{"execution":{"iopub.status.busy":"2021-06-03T05:29:17.143224Z","iopub.execute_input":"2021-06-03T05:29:17.143497Z","iopub.status.idle":"2021-06-03T05:29:17.790953Z","shell.execute_reply.started":"2021-06-03T05:29:17.143470Z","shell.execute_reply":"2021-06-03T05:29:17.789996Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualize some images**","metadata":{}},{"cell_type":"code","source":"#plot function\ndef plot_images(img,labels):\n    plt.figure(figsize=(15,10))\n    for i in range(25):\n        plt.subplot(5,5,i+1)\n        plt.imshow(img[i])\n        plt.title(class_names[np.argmax(labels[i])])\n        plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2021-06-03T05:29:17.792550Z","iopub.execute_input":"2021-06-03T05:29:17.792801Z","iopub.status.idle":"2021-06-03T05:29:17.801138Z","shell.execute_reply.started":"2021-06-03T05:29:17.792777Z","shell.execute_reply":"2021-06-03T05:29:17.800323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_images(x,y)","metadata":{"execution":{"iopub.status.busy":"2021-06-03T05:29:17.803608Z","iopub.execute_input":"2021-06-03T05:29:17.803942Z","iopub.status.idle":"2021-06-03T05:29:19.583746Z","shell.execute_reply.started":"2021-06-03T05:29:17.803892Z","shell.execute_reply":"2021-06-03T05:29:19.582987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## **MODEL**","metadata":{}},{"cell_type":"code","source":"model = Sequential([\n    \n    Conv2D(32,(3,3),activation='relu',input_shape=(224,224,3)),\n    MaxPooling2D((2,2)),\n    \n    Conv2D(64,(3,3),activation='relu'),\n    MaxPooling2D((2,2)),\n    \n    Conv2D(64,(3,3),activation='relu'),\n    MaxPooling2D((2,2)),\n    \n    Conv2D(128,(3,3),activation='relu'),\n    MaxPooling2D((2,2)),\n    \n    Flatten(),\n    \n    Dense(128,activation='relu'),\n    Dropout(0.5),\n    \n    Dense(64,activation='relu'),\n    Dropout(0.5),\n    \n    Dense(12,activation='softmax')\n    \n])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2021-06-03T06:09:40.754899Z","iopub.execute_input":"2021-06-03T06:09:40.755257Z","iopub.status.idle":"2021-06-03T06:09:40.836311Z","shell.execute_reply.started":"2021-06-03T06:09:40.755225Z","shell.execute_reply":"2021-06-03T06:09:40.835412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"keras.utils.plot_model(model,\n                      show_shapes=True,\n                      show_dtype=True,\n                      show_layer_names=True)","metadata":{"execution":{"iopub.status.busy":"2021-06-03T06:09:45.305407Z","iopub.execute_input":"2021-06-03T06:09:45.305729Z","iopub.status.idle":"2021-06-03T06:09:45.642203Z","shell.execute_reply.started":"2021-06-03T06:09:45.305699Z","shell.execute_reply":"2021-06-03T06:09:45.641276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-06-03T06:09:51.488722Z","iopub.execute_input":"2021-06-03T06:09:51.489156Z","iopub.status.idle":"2021-06-03T06:09:51.506921Z","shell.execute_reply.started":"2021-06-03T06:09:51.489116Z","shell.execute_reply":"2021-06-03T06:09:51.505921Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_calls = [keras.callbacks.EarlyStopping(monitor='val_accuracy',patience=2),\n            keras.callbacks.ModelCheckpoint(\"Model_xcp.h5\",verbose=1,save_best_only=True)]","metadata":{"execution":{"iopub.status.busy":"2021-06-03T06:09:51.674313Z","iopub.execute_input":"2021-06-03T06:09:51.674652Z","iopub.status.idle":"2021-06-03T06:09:51.679398Z","shell.execute_reply.started":"2021-06-03T06:09:51.674619Z","shell.execute_reply":"2021-06-03T06:09:51.678369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Train**","metadata":{}},{"cell_type":"code","source":"hist = model.fit(train_ds,epochs=25,validation_data=val_ds,callbacks=my_calls)","metadata":{"execution":{"iopub.status.busy":"2021-06-03T06:10:42.323291Z","iopub.execute_input":"2021-06-03T06:10:42.323628Z","iopub.status.idle":"2021-06-03T06:50:46.790660Z","shell.execute_reply.started":"2021-06-03T06:10:42.323593Z","shell.execute_reply":"2021-06-03T06:50:46.789683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,6))\n\nplt.subplot(1,2,1)\nplt.plot(hist.epoch,hist.history['accuracy'],label = 'Training')\nplt.plot(hist.epoch,hist.history['val_accuracy'],label = 'validation')\n\nplt.title(\"Accuracy\")\nplt.legend()\n\nplt.subplot(1,2,2)\nplt.plot(hist.epoch,hist.history['loss'],label = 'Training')\nplt.plot(hist.epoch,hist.history['val_loss'],label = 'validation')\n\nplt.title(\"Loss\")\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-06-03T07:23:46.386774Z","iopub.execute_input":"2021-06-03T07:23:46.387178Z","iopub.status.idle":"2021-06-03T07:23:46.744520Z","shell.execute_reply.started":"2021-06-03T07:23:46.387134Z","shell.execute_reply":"2021-06-03T07:23:46.743460Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample.head()","metadata":{"execution":{"iopub.status.busy":"2021-06-03T07:24:13.206177Z","iopub.execute_input":"2021-06-03T07:24:13.206569Z","iopub.status.idle":"2021-06-03T07:24:13.216484Z","shell.execute_reply.started":"2021-06-03T07:24:13.206533Z","shell.execute_reply":"2021-06-03T07:24:13.215619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = Imgen(preprocessing_function=keras.applications.xception.preprocess_input)\n\ntest_ds = test_gen.flow_from_dataframe(sample,\n                                      directory='../input/plant-pathology-2021-fgvc8/test_images',\n                                      x_col = 'image',\n                                       y_col = None,\n                                       class_mode=None,\n                                       target_size=(224,224)\n                                      )","metadata":{"execution":{"iopub.status.busy":"2021-06-03T07:27:48.206512Z","iopub.execute_input":"2021-06-03T07:27:48.206822Z","iopub.status.idle":"2021-06-03T07:27:48.221154Z","shell.execute_reply.started":"2021-06-03T07:27:48.206793Z","shell.execute_reply":"2021-06-03T07:27:48.220286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.predict(test_ds)\npred","metadata":{"execution":{"iopub.status.busy":"2021-06-03T07:29:42.854548Z","iopub.execute_input":"2021-06-03T07:29:42.854875Z","iopub.status.idle":"2021-06-03T07:29:43.727548Z","shell.execute_reply.started":"2021-06-03T07:29:42.854837Z","shell.execute_reply":"2021-06-03T07:29:43.726833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = [np.argmax(i) for i in pred]\nfor i in pred:\n    print(class_names[i])","metadata":{"execution":{"iopub.status.busy":"2021-06-03T07:29:45.124866Z","iopub.execute_input":"2021-06-03T07:29:45.125204Z","iopub.status.idle":"2021-06-03T07:29:45.132288Z","shell.execute_reply.started":"2021-06-03T07:29:45.125173Z","shell.execute_reply":"2021-06-03T07:29:45.129155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}