{"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 \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-04T08:51:58.313828Z","iopub.execute_input":"2023-11-04T08:51:58.314225Z","iopub.status.idle":"2023-11-04T08:51:58.322937Z","shell.execute_reply.started":"2023-11-04T08:51:58.314191Z","shell.execute_reply":"2023-11-04T08:51:58.320587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-04T08:51:58.32585Z","iopub.execute_input":"2023-11-04T08:51:58.326224Z","iopub.status.idle":"2023-11-04T08:51:58.37378Z","shell.execute_reply.started":"2023-11-04T08:51:58.326195Z","shell.execute_reply":"2023-11-04T08:51:58.372518Z"},"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-04T08:51:58.375382Z","iopub.execute_input":"2023-11-04T08:51:58.375783Z","iopub.status.idle":"2023-11-04T08:52:14.289884Z","shell.execute_reply.started":"2023-11-04T08:51:58.375754Z","shell.execute_reply":"2023-11-04T08:52:14.288715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head(9)","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:14.292615Z","iopub.execute_input":"2023-11-04T08:52:14.293009Z","iopub.status.idle":"2023-11-04T08:52:14.3036Z","shell.execute_reply.started":"2023-11-04T08:52:14.292981Z","shell.execute_reply":"2023-11-04T08:52:14.302264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.info()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:14.304971Z","iopub.execute_input":"2023-11-04T08:52:14.305306Z","iopub.status.idle":"2023-11-04T08:52:14.326866Z","shell.execute_reply.started":"2023-11-04T08:52:14.305276Z","shell.execute_reply":"2023-11-04T08:52:14.325626Z"},"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-04T08:52:14.328263Z","iopub.execute_input":"2023-11-04T08:52:14.328739Z","iopub.status.idle":"2023-11-04T08:52:14.351651Z","shell.execute_reply.started":"2023-11-04T08:52:14.328714Z","shell.execute_reply":"2023-11-04T08:52:14.350143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(labels)\nprint(data)","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:14.35273Z","iopub.execute_input":"2023-11-04T08:52:14.353011Z","iopub.status.idle":"2023-11-04T08:52:14.379545Z","shell.execute_reply.started":"2023-11-04T08:52:14.352983Z","shell.execute_reply":"2023-11-04T08:52:14.378317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_num = len(train_df[\"labels\"].value_counts())","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:14.38055Z","iopub.execute_input":"2023-11-04T08:52:14.381669Z","iopub.status.idle":"2023-11-04T08:52:14.392939Z","shell.execute_reply.started":"2023-11-04T08:52:14.381644Z","shell.execute_reply":"2023-11-04T08:52:14.39121Z"},"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-04T08:52:14.394364Z","iopub.execute_input":"2023-11-04T08:52:14.395494Z","iopub.status.idle":"2023-11-04T08:52:14.414488Z","shell.execute_reply.started":"2023-11-04T08:52:14.395377Z","shell.execute_reply":"2023-11-04T08:52:14.413472Z"},"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    )","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:14.41767Z","iopub.execute_input":"2023-11-04T08:52:14.418205Z","iopub.status.idle":"2023-11-04T08:52:14.613568Z","shell.execute_reply.started":"2023-11-04T08:52:14.418141Z","shell.execute_reply":"2023-11-04T08:52:14.612036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x,y = next(train_ds)\nx.shape","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:14.615241Z","iopub.execute_input":"2023-11-04T08:52:14.615677Z","iopub.status.idle":"2023-11-04T08:52:14.624525Z","shell.execute_reply.started":"2023-11-04T08:52:14.61563Z","shell.execute_reply":"2023-11-04T08:52:14.622893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications.vgg16 import VGG16\n\n","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:54:59.56063Z","iopub.execute_input":"2023-11-04T08:54:59.561029Z","iopub.status.idle":"2023-11-04T08:54:59.566931Z","shell.execute_reply.started":"2023-11-04T08:54:59.561001Z","shell.execute_reply":"2023-11-04T08:54:59.565644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vgg = VGG16()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:55:01.131517Z","iopub.execute_input":"2023-11-04T08:55:01.131845Z","iopub.status.idle":"2023-11-04T08:55:03.210213Z","shell.execute_reply.started":"2023-11-04T08:55:01.131822Z","shell.execute_reply":"2023-11-04T08:55:03.208566Z"},"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-04T08:55:04.979214Z","iopub.execute_input":"2023-11-04T08:55:04.979833Z","iopub.status.idle":"2023-11-04T08:55:05.092234Z","shell.execute_reply.started":"2023-11-04T08:55:04.979797Z","shell.execute_reply":"2023-11-04T08:55:05.091511Z"},"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-04T08:52:19.762217Z","iopub.execute_input":"2023-11-04T08:52:19.762589Z","iopub.status.idle":"2023-11-04T08:52:19.769078Z","shell.execute_reply.started":"2023-11-04T08:52:19.762561Z","shell.execute_reply":"2023-11-04T08:52:19.767553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Dense(class_num , activation = \"softmax\"))\n","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:19.770537Z","iopub.execute_input":"2023-11-04T08:52:19.770919Z","iopub.status.idle":"2023-11-04T08:52:19.798191Z","shell.execute_reply.started":"2023-11-04T08:52:19.770889Z","shell.execute_reply":"2023-11-04T08:52:19.796953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()\n","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:19.799569Z","iopub.execute_input":"2023-11-04T08:52:19.799922Z","iopub.status.idle":"2023-11-04T08:52:19.855513Z","shell.execute_reply.started":"2023-11-04T08:52:19.799892Z","shell.execute_reply":"2023-11-04T08:52:19.853332Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_addons as tfa\nimport keras as keras\n\nf1 = tfa.metrics.F1Score(num_classes=6,average='macro')\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=200,\n                           steps_per_epoch=train_ds.samples//128,\n                           validation_steps=valid_ds.samples//128,\n                           callbacks=[es])","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:54:02.653672Z","iopub.execute_input":"2023-11-04T08:54:02.654124Z","iopub.status.idle":"2023-11-04T08:54:02.734472Z","shell.execute_reply.started":"2023-11-04T08:54:02.654096Z","shell.execute_reply":"2023-11-04T08:54:02.733218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nplt.rc('font', size=20)\nplt.figure(figsize=(15,12))\nepoch_list = list(range(1, len(hist.history['accuracy']) + 1))\nplt.plot(epoch_list, hist.history['accuracy'],label='accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:19.870236Z","iopub.status.idle":"2023-11-04T08:52:19.870649Z","shell.execute_reply.started":"2023-11-04T08:52:19.870456Z","shell.execute_reply":"2023-11-04T08:52:19.870476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nplt.rc('font', size=20)\nplt.figure(figsize=(15,12))\nepoch_list = list(range(1, len(hist.history['loss']) + 1))\nplt.plot(epoch_list, hist.history['loss'],label='loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:19.872637Z","iopub.status.idle":"2023-11-04T08:52:19.873303Z","shell.execute_reply.started":"2023-11-04T08:52:19.873005Z","shell.execute_reply":"2023-11-04T08:52:19.87303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nplt.rc('font', size=20)\nplt.figure(figsize=(15,12))\nepoch_list = list(range(1, len(hist.history['recall']) + 1))\nplt.plot(epoch_list, hist.history['recall'],label='recall')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:19.875271Z","iopub.status.idle":"2023-11-04T08:52:19.875731Z","shell.execute_reply.started":"2023-11-04T08:52:19.875506Z","shell.execute_reply":"2023-11-04T08:52:19.875527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%matplotlib inline\nplt.rc('font', size=20)\nplt.figure(figsize=(15,12))\nepoch_list = list(range(1, len(hist.history['precision']) + 1))\nplt.plot(epoch_list, hist.history['precision'],label='precision')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:19.878127Z","iopub.status.idle":"2023-11-04T08:52:19.878551Z","shell.execute_reply.started":"2023-11-04T08:52:19.87835Z","shell.execute_reply":"2023-11-04T08:52:19.87837Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#f1_score\n\"\"\"plt.figure(figsize=(15,6))\nepoch_list = list(range(1,len(hist.history['f1_score'])+1))\nplt.plot(epoch_list, hist.history['f1_score'],label='f1_score')\nplt.plot(epoch_list, hist.history['val_f1_score'],label='val_f1_score')\nplt.xlabel('epoches')\nplt.ylabel('f1')\nplt.legend()\nplt.show()\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:19.87992Z","iopub.status.idle":"2023-11-04T08:52:19.880315Z","shell.execute_reply.started":"2023-11-04T08:52:19.880124Z","shell.execute_reply":"2023-11-04T08:52:19.880143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#av\n''''arr1 = hist.history['loss']\nresult1 = sum(arr1)\nprint(f\"loss_av : {result1 / len(arr1)}\")\n\narr2 = hist.history['accuracy']\nresult2 = sum(arr2)\nprint(f\"accuracy_av : {result2 / len(arr2)}\")\n\narr3 = hist.history['val_loss']\nresult3 = sum(arr3)\nprint(f\"val_loss_av : {result3 / len(arr3)}\")\n\narr4 = hist.history['val_accuracy']\nresult4 = sum(arr4)\nprint(f\"val_accuracy_av : {result4 / len(arr4)}\")'''","metadata":{"execution":{"iopub.status.busy":"2023-11-04T08:52:19.881855Z","iopub.status.idle":"2023-11-04T08:52:19.882241Z","shell.execute_reply.started":"2023-11-04T08:52:19.882051Z","shell.execute_reply":"2023-11-04T08:52:19.88207Z"},"trusted":true},"execution_count":null,"outputs":[]}]}