{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":25563,"databundleVersionId":2094376,"sourceType":"competition"},{"sourceId":2032065,"sourceType":"datasetVersion","datasetId":1216613}],"dockerImageVersionId":30616,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\n# import tensorflow.keras as keras\nimport PIL\nimport cv2\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport os\nimport random\nfrom tqdm import tqdm\nimport tensorflow_addons as tfa\nimport random\nfrom sklearn.preprocessing import MultiLabelBinarizer\nimport matplotlib.pyplot as plt\nimport keras\nfrom keras.preprocessing import image\nfrom keras.models import Sequential\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array, smart_resize\nfrom keras.layers import Dense, Dropout, Flatten, BatchNormalization, Activation\n# from keras.constraints import maxnorm\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D\nfrom tensorflow.keras.optimizers import Adam\nfrom keras.preprocessing.image import load_img, img_to_array\nfrom keras.models import load_model\nfrom keras.metrics import AUC\nfrom tqdm.auto import tqdm\nsns.set_style('darkgrid')\npd.set_option(\"display.max_columns\", None)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-03-04T18:40:34.247948Z","iopub.execute_input":"2024-03-04T18:40:34.248210Z","iopub.status.idle":"2024-03-04T18:40:48.985904Z","shell.execute_reply.started":"2024-03-04T18:40:34.248186Z","shell.execute_reply":"2024-03-04T18:40:48.985052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir= '../input/resized-plant2021/img_sz_384'\ntest_dir =  '../input/plant-pathology-2021-fgvc8/test_images'\ntrain = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')\n","metadata":{"execution":{"iopub.status.busy":"2024-03-04T18:40:48.987486Z","iopub.execute_input":"2024-03-04T18:40:48.988023Z","iopub.status.idle":"2024-03-04T18:40:49.033904Z","shell.execute_reply.started":"2024-03-04T18:40:48.987994Z","shell.execute_reply":"2024-03-04T18:40:49.033111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train['labels'] = train['labels'].apply(lambda string: string.split(' '))\n","metadata":{"execution":{"iopub.status.busy":"2024-03-04T18:40:49.034969Z","iopub.execute_input":"2024-03-04T18:40:49.035326Z","iopub.status.idle":"2024-03-04T18:40:49.117095Z","shell.execute_reply.started":"2024-03-04T18:40:49.035293Z","shell.execute_reply":"2024-03-04T18:40:49.116160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"s = list(train['labels'])\nmlb = MultiLabelBinarizer()\ntrainx = pd.DataFrame(mlb.fit_transform(s), columns=mlb.classes_, index=train.index)\nprint(trainx.columns)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-04T18:40:49.119743Z","iopub.execute_input":"2024-03-04T18:40:49.120073Z","iopub.status.idle":"2024-03-04T18:40:49.158496Z","shell.execute_reply.started":"2024-03-04T18:40:49.120048Z","shell.execute_reply":"2024-03-04T18:40:49.157688Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.concat([train['image'], trainx], axis=1)\nlabels.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-03-04T18:40:49.159410Z","iopub.execute_input":"2024-03-04T18:40:49.159700Z","iopub.status.idle":"2024-03-04T18:40:49.176062Z","shell.execute_reply.started":"2024-03-04T18:40:49.159646Z","shell.execute_reply":"2024-03-04T18:40:49.175181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_datagen = ImageDataGenerator(\n    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.1\n    \n)\nIMAGE = (256, 256)\nBATCH_SIZE = 64","metadata":{"execution":{"iopub.status.busy":"2024-03-04T18:40:49.177040Z","iopub.execute_input":"2024-03-04T18:40:49.177289Z","iopub.status.idle":"2024-03-04T18:40:49.182203Z","shell.execute_reply.started":"2024-03-04T18:40:49.177266Z","shell.execute_reply":"2024-03-04T18:40:49.181280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = image_datagen.flow_from_dataframe(\n    dataframe=labels,\n    directory= '../input/resized-plant2021/img_sz_512',\n    x_col=\"image\",\n    y_col=labels.columns.tolist()[1:],\n    color_mode=\"rgb\",\n    target_size = IMAGE,\n    class_mode=\"raw\",\n    #class_mode=\"categorical\",\n    subset = \"training\",\n    batch_size=BATCH_SIZE\n)\n\ntest_data = image_datagen.flow_from_dataframe(\n    dataframe=labels,\n    directory= '../input/resized-plant2021/img_sz_512',\n    x_col=\"image\",\n    y_col=labels.columns.tolist()[1:],\n    color_mode=\"rgb\",\n    target_size = IMAGE,\n    class_mode=\"raw\",\n    #class_mode=\"categorical\",\n    subset = \"validation\",\n    batch_size=BATCH_SIZE\n)","metadata":{"execution":{"iopub.status.busy":"2024-03-04T18:40:49.183586Z","iopub.execute_input":"2024-03-04T18:40:49.183968Z","iopub.status.idle":"2024-03-04T18:41:16.422000Z","shell.execute_reply.started":"2024-03-04T18:40:49.183935Z","shell.execute_reply":"2024-03-04T18:41:16.421206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"inputs = tf.keras.Input(shape=(256, 256, 3))\n\n#x = tf.keras.applications.ResNet50(include_top=False,weights=\"imagenet\")(inputs)\n#x = tf.keras.applications.ResNet50(include_top=False)(inputs)\n#ResNet50(input_shape=[124,124, 3],include_top=False, weights='imagenet')\n\n#x = tf.keras.applications.MobileNet(include_top=False)(inputs)\n#model = tf.keras.applications.MobileNet(input_shape=(150,150,3),include_top=False,weights=\"imagenet\")\n\n#x = tf.keras.applications.MobileNetV2(include_top=False,weights=\"imagenet\")(inputs)\nx = tf.keras.applications.MobileNetV2(include_top=False)(inputs)\n\nx = tf.keras.layers.GlobalAveragePooling2D()(x)\nx=tf.keras.layers.Dense(128,activation='relu')(x)\nx=tf.keras.layers.Dropout(0.4)(x)\nx=tf.keras.layers.Dense(64,activation='relu')(x)\noutputs = tf.keras.layers.Dense(6, activation='sigmoid')(x)\n\nmodel = tf.keras.models.Model(inputs, outputs)\nmodel.summary()\ntf.keras.utils.plot_model(model, show_shapes=True)\n","metadata":{"execution":{"iopub.status.busy":"2024-03-04T18:43:25.097236Z","iopub.execute_input":"2024-03-04T18:43:25.097619Z","iopub.status.idle":"2024-03-04T18:43:27.015208Z","shell.execute_reply.started":"2024-03-04T18:43:25.097589Z","shell.execute_reply":"2024-03-04T18:43:27.014394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow_addons as tfa \n    #tf.keras.metrics.CategoricalAccuracy(name='categorical_accuracy')\nmetrics = [       \n    'accuracy',\n    keras.metrics.Precision(name='precision'),\n    keras.metrics.Recall(name='recall'),\n    tfa.metrics.F1Score(num_classes = 6,average = \"macro\",name = \"f1_score\",threshold=0.5)]\n# metrics = ['accuracy', tfa.metrics.F1Score(num_classes = 6,average = \"macro\",name = \"f1_score\",threshold=0.5)]\n# # metrics = ['accuracy']\n\n","metadata":{"execution":{"iopub.status.busy":"2024-03-04T18:43:46.967083Z","iopub.execute_input":"2024-03-04T18:43:46.967792Z","iopub.status.idle":"2024-03-04T18:43:46.985668Z","shell.execute_reply.started":"2024-03-04T18:43:46.967757Z","shell.execute_reply":"2024-03-04T18:43:46.984844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='binary_crossentropy', \n              optimizer=tf.keras.optimizers.Adam(lr=1e-4),\n              metrics=metrics)","metadata":{"execution":{"iopub.status.busy":"2024-03-04T18:44:09.436885Z","iopub.execute_input":"2024-03-04T18:44:09.437494Z","iopub.status.idle":"2024-03-04T18:44:09.453674Z","shell.execute_reply.started":"2024-03-04T18:44:09.437459Z","shell.execute_reply":"2024-03-04T18:44:09.452753Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rlp = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_f1_score',\n                                           mode='max',\n                                           patience=2, \n                                           verbose=0, \n                                           factor=0.01)\nearlystop = tf.keras.callbacks.EarlyStopping(monitor='val_f1_score', \n                                             mode='max',\n                                             patience=5, \n                                             verbose=1, \n                                             restore_best_weights=True)\ncallbacks=[rlp,earlystop]","metadata":{"execution":{"iopub.status.busy":"2024-03-04T18:44:21.511937Z","iopub.execute_input":"2024-03-04T18:44:21.512336Z","iopub.status.idle":"2024-03-04T18:44:21.518374Z","shell.execute_reply.started":"2024-03-04T18:44:21.512305Z","shell.execute_reply":"2024-03-04T18:44:21.517466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_history = model.fit(train_data, \n                          validation_data=test_data, \n                          validation_steps = test_data.n // BATCH_SIZE,\n                          epochs=50, \n                          callbacks=callbacks)","metadata":{"execution":{"iopub.status.busy":"2024-03-04T18:44:24.175458Z","iopub.execute_input":"2024-03-04T18:44:24.175846Z","iopub.status.idle":"2024-03-04T18:45:22.586970Z","shell.execute_reply.started":"2024-03-04T18:44:24.175814Z","shell.execute_reply":"2024-03-04T18:45:22.584701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fix, ax = plt.subplots(figsize=(20, 6))\npd.DataFrame(model_history.history)[['loss', 'val_loss']].plot(ax=ax, title='Model Loss Curve')\n","metadata":{"execution":{"iopub.status.busy":"2023-12-12T22:01:24.045141Z","iopub.execute_input":"2023-12-12T22:01:24.045555Z","iopub.status.idle":"2023-12-12T22:01:24.511430Z","shell.execute_reply.started":"2023-12-12T22:01:24.045516Z","shell.execute_reply":"2023-12-12T22:01:24.510309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"###### fix, ax = plt.subplots(figsize=(20, 6))\npd.DataFrame(model_history.history)[['f1_score', 'val_f1_score']].plot(ax=ax, title='Model F1_score Curve')\n","metadata":{}},{"cell_type":"code","source":"fix, ax = plt.subplots(figsize=(20, 6))\npd.DataFrame(model_history.history)[['f1_score', 'val_f1_score']].plot(ax=ax, title='Model F1_score Curve')\n","metadata":{},"execution_count":null,"outputs":[]}]}