{"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\nimport tensorflow_addons as tfa\n\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\nfrom sklearn.metrics import f1_score\nfrom tensorflow.keras.callbacks import EarlyStopping\nfrom keras.preprocessing.image import ImageDataGenerator\n\nfrom PIL import Image\nfrom sklearn.metrics import confusion_matrix,classification_report\nimport cv2\nimport os\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-08T08:26:10.898785Z","iopub.execute_input":"2023-11-08T08:26:10.899115Z","iopub.status.idle":"2023-11-08T08:26:22.646835Z","shell.execute_reply.started":"2023-11-08T08:26:10.899042Z","shell.execute_reply":"2023-11-08T08:26:22.645862Z"},"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/sample_submission.csv\"\ntest = pd.read_csv(test_path)\ntest","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:26:22.648119Z","iopub.execute_input":"2023-11-08T08:26:22.648419Z","iopub.status.idle":"2023-11-08T08:26:22.716729Z","shell.execute_reply.started":"2023-11-08T08:26:22.648390Z","shell.execute_reply":"2023-11-08T08:26:22.715941Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15,30))\ni=1\nfor idx,s in train_df.head(9).iterrows():\n    img_path = os.path.join(train_path,s['image'])\n    img=cv2.imread(img_path)\n    img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)\n    \n    fig=plt.subplot(9,3,i)\n    fig.imshow(img)\n    fig.set_title(s['labels'])\n    i+=1","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:26:22.718432Z","iopub.execute_input":"2023-11-08T08:26:22.718734Z","iopub.status.idle":"2023-11-08T08:26:34.829588Z","shell.execute_reply.started":"2023-11-08T08:26:22.718702Z","shell.execute_reply":"2023-11-08T08:26:34.828714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(9)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:26:34.831122Z","iopub.execute_input":"2023-11-08T08:26:34.831417Z","iopub.status.idle":"2023-11-08T08:26:34.842232Z","shell.execute_reply.started":"2023-11-08T08:26:34.831387Z","shell.execute_reply":"2023-11-08T08:26:34.841098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:26:34.843518Z","iopub.execute_input":"2023-11-08T08:26:34.843864Z","iopub.status.idle":"2023-11-08T08:26:34.869429Z","shell.execute_reply.started":"2023-11-08T08:26:34.843834Z","shell.execute_reply":"2023-11-08T08:26:34.868657Z"},"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":"2023-11-08T08:26:34.870435Z","iopub.execute_input":"2023-11-08T08:26:34.870713Z","iopub.status.idle":"2023-11-08T08:26:34.883562Z","shell.execute_reply.started":"2023-11-08T08:26:34.870687Z","shell.execute_reply":"2023-11-08T08:26:34.882620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels)\nprint(data)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:26:34.884917Z","iopub.execute_input":"2023-11-08T08:26:34.885333Z","iopub.status.idle":"2023-11-08T08:26:34.894778Z","shell.execute_reply.started":"2023-11-08T08:26:34.885297Z","shell.execute_reply":"2023-11-08T08:26:34.893944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_num = len(train_df[\"labels\"].value_counts())\nprint(class_num)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:26:34.897326Z","iopub.execute_input":"2023-11-08T08:26:34.897595Z","iopub.status.idle":"2023-11-08T08:26:34.911334Z","shell.execute_reply.started":"2023-11-08T08:26:34.897571Z","shell.execute_reply":"2023-11-08T08:26:34.910495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(rescale=1/255.0,\n                            rotation_range=5,\n                            zoom_range=0.1,\n                            shear_range=0.05,\n                            horizontal_flip=True,\n                            validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:26:34.912984Z","iopub.execute_input":"2023-11-08T08:26:34.913229Z","iopub.status.idle":"2023-11-08T08:26:34.921907Z","shell.execute_reply.started":"2023-11-08T08:26:34.913207Z","shell.execute_reply":"2023-11-08T08:26:34.921174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = datagen.flow_from_dataframe(\n    train_df,\n    directory='../input/resized-plant2021/img_sz_256',\n    subset='training',\n    x_col='image',\n    y_col='labels',\n    target_size=(224,224),\n    color_mode='rgb',\n    class_mode='categorical',\n    batch_size=32,\n    shuffle=True,\n    seed=444\n    )\n#'../input/plant-pathology-2021-fgvc8/train_images'\nvalid_ds = datagen.flow_from_dataframe(\n    train_df,\n    directory='../input/resized-plant2021/img_sz_256',\n    subset='validation',\n    x_col='image',\n    y_col='labels',\n    target_size=(224,224),\n    color_mode='rgb',\n    class_mode='categorical',\n    batch_size=32,\n    shuffle=True,\n    seed=444\n    )\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:26:34.923004Z","iopub.execute_input":"2023-11-08T08:26:34.923302Z","iopub.status.idle":"2023-11-08T08:27:33.966331Z","shell.execute_reply.started":"2023-11-08T08:26:34.923272Z","shell.execute_reply":"2023-11-08T08:27:33.965428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x,y = next(train_ds)\nx.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:27:33.967511Z","iopub.execute_input":"2023-11-08T08:27:33.967812Z","iopub.status.idle":"2023-11-08T08:27:34.452999Z","shell.execute_reply.started":"2023-11-08T08:27:33.967783Z","shell.execute_reply":"2023-11-08T08:27:34.452171Z"},"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":"2023-11-08T08:27:34.454106Z","iopub.execute_input":"2023-11-08T08:27:34.454383Z","iopub.status.idle":"2023-11-08T08:27:34.458287Z","shell.execute_reply.started":"2023-11-08T08:27:34.454357Z","shell.execute_reply":"2023-11-08T08:27:34.457434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg = VGG16()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:27:34.459228Z","iopub.execute_input":"2023-11-08T08:27:34.459478Z","iopub.status.idle":"2023-11-08T08:27:42.802040Z","shell.execute_reply.started":"2023-11-08T08:27:34.459455Z","shell.execute_reply":"2023-11-08T08:27:42.801195Z"},"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":"2023-11-08T08:27:42.803242Z","iopub.execute_input":"2023-11-08T08:27:42.803555Z","iopub.status.idle":"2023-11-08T08:27:42.899926Z","shell.execute_reply.started":"2023-11-08T08:27:42.803527Z","shell.execute_reply":"2023-11-08T08:27:42.899265Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for l in vgg_layers:\n    l.trainable = False","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:27:42.901033Z","iopub.execute_input":"2023-11-08T08:27:42.901398Z","iopub.status.idle":"2023-11-08T08:27:42.906269Z","shell.execute_reply.started":"2023-11-08T08:27:42.901363Z","shell.execute_reply":"2023-11-08T08:27:42.905438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Dense(class_num , activation = \"softmax\"))","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:27:42.907318Z","iopub.execute_input":"2023-11-08T08:27:42.907570Z","iopub.status.idle":"2023-11-08T08:27:42.926796Z","shell.execute_reply.started":"2023-11-08T08:27:42.907547Z","shell.execute_reply":"2023-11-08T08:27:42.925974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:27:42.928042Z","iopub.execute_input":"2023-11-08T08:27:42.928460Z","iopub.status.idle":"2023-11-08T08:27:42.942805Z","shell.execute_reply.started":"2023-11-08T08:27:42.928424Z","shell.execute_reply":"2023-11-08T08:27:42.941958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_addons as tfa\nimport keras as keras\n\n\nf1 = tfa.metrics.F1Score(num_classes=6,average='macro', name='f1score')\n\nmetrics = [\n    keras.metrics.CategoricalAccuracy(name='accuracy'),\n    keras.metrics.Precision(name='precision'),\n    keras.metrics.Recall(name='recall')\n    ] \n\nmodel.compile(optimizer='adam', loss='binary_crossentropy',metrics=[metrics])\nbatch_size = 32\n\nes=EarlyStopping(patience=4, monitor=f1, mode='max',restore_best_weights=True)\nhist = model.fit_generator(generator=train_ds,\n                           validation_data=valid_ds,\n                           epochs=2,\n                           steps_per_epoch=train_ds.samples//128,\n                           validation_steps=valid_ds.samples//128,\n                           callbacks=[es])\nf1_result = f1.result().numpy()\nprint(f'F1 Score at the end: {f1_result:.4f}')","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:27:42.944039Z","iopub.execute_input":"2023-11-08T08:27:42.944366Z","iopub.status.idle":"2023-11-08T08:30:34.912088Z","shell.execute_reply.started":"2023-11-08T08:27:42.944333Z","shell.execute_reply":"2023-11-08T08:30:34.911070Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = datagen.flow_from_dataframe(\n    test,\n    directory='../input/plant-pathology-2021-fgvc8/test_images',\n    x_col='image',\n    y_col=None,\n    color_mode='rgb',\n    target_size=(224,224),\n    class_mode=None,\n    shuffle=False\n)\npredictions = model.predict(test_data)\nprint(predictions)","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:30:34.913765Z","iopub.execute_input":"2023-11-08T08:30:34.914166Z","iopub.status.idle":"2023-11-08T08:30:37.314021Z","shell.execute_reply.started":"2023-11-08T08:30:34.914129Z","shell.execute_reply":"2023-11-08T08:30:37.313057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"<a id=\"4\"> </a>\n# Visualize Model Results","metadata":{}},{"cell_type":"code","source":"print(hist.history.keys())\n\nprint(hist.history['accuracy'])\nprint(hist.history['loss'])\nprint(hist.history['recall'])\nprint(hist.history['precision'])\n","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:30:37.323773Z","iopub.execute_input":"2023-11-08T08:30:37.324117Z","iopub.status.idle":"2023-11-08T08:30:37.337377Z","shell.execute_reply.started":"2023-11-08T08:30:37.324087Z","shell.execute_reply":"2023-11-08T08:30:37.336501Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f1 = 2*((np.array(hist.history['precision'])*np.array(hist.history['recall']))/np.array(hist.history['precision'])+np.array(hist.history['recall']))\nhist.history['f1_score'] = f1.tolist()\nresult5 = sum(f1)\nprint(f\"Mean F1-Score : {result5/len(f1)}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:30:37.338691Z","iopub.execute_input":"2023-11-08T08:30:37.339053Z","iopub.status.idle":"2023-11-08T08:30:37.350266Z","shell.execute_reply.started":"2023-11-08T08:30:37.339017Z","shell.execute_reply":"2023-11-08T08:30:37.349349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_values = np.max(predictions, axis=1)\nargmax_values = np.argmax(predictions, axis=1)\nsub = pd.read_csv('../input/plant-pathology-2021-fgvc8/sample_submission.csv')\nsub['labels'] = labels[argmax_values]\nsub.to_csv('submission.csv', index=False)\nsub","metadata":{"execution":{"iopub.status.busy":"2023-11-08T08:45:21.305569Z","iopub.execute_input":"2023-11-08T08:45:21.306105Z","iopub.status.idle":"2023-11-08T08:45:21.325355Z","shell.execute_reply.started":"2023-11-08T08:45:21.306055Z","shell.execute_reply":"2023-11-08T08:45:21.324616Z"},"trusted":true},"execution_count":null,"outputs":[]}]}