{"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-23T18:40:39.173268Z","iopub.execute_input":"2022-11-23T18:40:39.173654Z","iopub.status.idle":"2022-11-23T18:40:39.179059Z","shell.execute_reply.started":"2022-11-23T18:40:39.173619Z","shell.execute_reply":"2022-11-23T18:40:39.177707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import basic libraries from tensorflow\nimport tensorflow as tf\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:40:42.861096Z","iopub.execute_input":"2022-11-23T18:40:42.861442Z","iopub.status.idle":"2022-11-23T18:40:42.866336Z","shell.execute_reply.started":"2022-11-23T18:40:42.86141Z","shell.execute_reply":"2022-11-23T18:40:42.865153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Analysis Stage\n\nSee the dataframe features\n\nThe dataframe has in the same row all the possible diseases, we are going to split the labels","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv('../input/plant-pathology-2021-fgvc8/train.csv')\ntrain.tail()","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-11-23T18:40:46.184362Z","iopub.execute_input":"2022-11-23T18:40:46.184744Z","iopub.status.idle":"2022-11-23T18:40:46.214485Z","shell.execute_reply.started":"2022-11-23T18:40:46.184712Z","shell.execute_reply":"2022-11-23T18:40:46.213555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2022-11-23T16:21:53.456385Z","iopub.execute_input":"2022-11-23T16:21:53.456798Z","iopub.status.idle":"2022-11-23T16:21:53.475874Z","shell.execute_reply.started":"2022-11-23T16:21:53.456765Z","shell.execute_reply":"2022-11-23T16:21:53.474701Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"train.isna.sum(","metadata":{}},{"cell_type":"code","source":"train.labels.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-11-23T16:10:23.722321Z","iopub.execute_input":"2022-11-23T16:10:23.722781Z","iopub.status.idle":"2022-11-23T16:10:23.731652Z","shell.execute_reply.started":"2022-11-23T16:10:23.722745Z","shell.execute_reply":"2022-11-23T16:10:23.730712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"temp = ['scab frog_eye_leaf_spot',\n'scab frog_eye_leaf_spot complex',\n'frog_eye_leaf_spot complex',\n'rust frog_eye_leaf_spot',\n'rust complex',\n'powdery_mildew complex'] ","metadata":{"execution":{"iopub.status.busy":"2022-11-23T16:36:06.005194Z","iopub.execute_input":"2022-11-23T16:36:06.005553Z","iopub.status.idle":"2022-11-23T16:36:06.010127Z","shell.execute_reply.started":"2022-11-23T16:36:06.005523Z","shell.execute_reply":"2022-11-23T16:36:06.009168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.replace(to_replace= temp,value =pd.NA,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T16:36:44.598039Z","iopub.execute_input":"2022-11-23T16:36:44.598414Z","iopub.status.idle":"2022-11-23T16:36:44.615945Z","shell.execute_reply.started":"2022-11-23T16:36:44.598381Z","shell.execute_reply":"2022-11-23T16:36:44.61496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Extract all the labels available in the dataframe","metadata":{}},{"cell_type":"code","source":"train.dropna(axis=0,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T16:39:01.582481Z","iopub.execute_input":"2022-11-23T16:39:01.582945Z","iopub.status.idle":"2022-11-23T16:39:01.595352Z","shell.execute_reply.started":"2022-11-23T16:39:01.582904Z","shell.execute_reply":"2022-11-23T16:39:01.594168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.reset_index(drop=True,inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T16:41:31.375706Z","iopub.execute_input":"2022-11-23T16:41:31.376907Z","iopub.status.idle":"2022-11-23T16:41:31.382013Z","shell.execute_reply.started":"2022-11-23T16:41:31.37686Z","shell.execute_reply":"2022-11-23T16:41:31.38092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.labels.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-11-23T16:41:36.720156Z","iopub.execute_input":"2022-11-23T16:41:36.720504Z","iopub.status.idle":"2022-11-23T16:41:36.728977Z","shell.execute_reply.started":"2022-11-23T16:41:36.720474Z","shell.execute_reply":"2022-11-23T16:41:36.728039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cl = pd.get_dummies(train, columns=['labels'])\ntrain_cl['labels'] = train.labels\n","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:03:13.412806Z","iopub.execute_input":"2022-11-23T18:03:13.413191Z","iopub.status.idle":"2022-11-23T18:03:13.426138Z","shell.execute_reply.started":"2022-11-23T18:03:13.413153Z","shell.execute_reply":"2022-11-23T18:03:13.425147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cl","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:03:19.298414Z","iopub.execute_input":"2022-11-23T18:03:19.298786Z","iopub.status.idle":"2022-11-23T18:03:19.314657Z","shell.execute_reply.started":"2022-11-23T18:03:19.298754Z","shell.execute_reply":"2022-11-23T18:03:19.313628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cl.info()","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:03:26.587506Z","iopub.execute_input":"2022-11-23T18:03:26.58854Z","iopub.status.idle":"2022-11-23T18:03:26.605653Z","shell.execute_reply.started":"2022-11-23T18:03:26.588492Z","shell.execute_reply":"2022-11-23T18:03:26.604503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntrain_cl.rename(columns  = {'labels_complex':'complex',\n                     'labels_frog_eye_leaf_spot':'frog_eye_leaf_spot',\n                     'labels_healthy':'healthy',\n                     'labels_powdery_mildew':'powdery_mildew',\n                     'labels_rust':'rust',\n                     'labels_scab':'scab'},inplace=True)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:03:42.077086Z","iopub.execute_input":"2022-11-23T18:03:42.077454Z","iopub.status.idle":"2022-11-23T18:03:42.083706Z","shell.execute_reply.started":"2022-11-23T18:03:42.077421Z","shell.execute_reply":"2022-11-23T18:03:42.082604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cl","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:03:47.40942Z","iopub.execute_input":"2022-11-23T18:03:47.410264Z","iopub.status.idle":"2022-11-23T18:03:47.427559Z","shell.execute_reply.started":"2022-11-23T18:03:47.410133Z","shell.execute_reply":"2022-11-23T18:03:47.426615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cl.describe()","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:03:55.137878Z","iopub.execute_input":"2022-11-23T18:03:55.138329Z","iopub.status.idle":"2022-11-23T18:03:55.183556Z","shell.execute_reply.started":"2022-11-23T18:03:55.13829Z","shell.execute_reply":"2022-11-23T18:03:55.182535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_cl.labels.value_counts().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:04:11.745419Z","iopub.execute_input":"2022-11-23T18:04:11.746104Z","iopub.status.idle":"2022-11-23T18:04:11.935228Z","shell.execute_reply.started":"2022-11-23T18:04:11.746068Z","shell.execute_reply":"2022-11-23T18:04:11.934273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\n#  Categorical Data\na = 2  # number of rows\nb = 3  # number of columns\nc = 1  # initialize plot counter\n\nfig = plt.figure(figsize=(10,8))\n\nfor i in train_cl.labels.unique():\n    plt.subplot(a, b, c)\n    sns.countplot(x =train_cl[i])\n    c = c + 1\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:04:25.266903Z","iopub.execute_input":"2022-11-23T18:04:25.267271Z","iopub.status.idle":"2022-11-23T18:04:25.851391Z","shell.execute_reply.started":"2022-11-23T18:04:25.267238Z","shell.execute_reply":"2022-11-23T18:04:25.850493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Engineering - Part 2\n","metadata":{}},{"cell_type":"markdown","source":"### Split data into test and validation\n","metadata":{}},{"cell_type":"code","source":"val = train_cl[14000:]\ntrain = train_cl[:14000]","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:11:21.663043Z","iopub.execute_input":"2022-11-23T18:11:21.663407Z","iopub.status.idle":"2022-11-23T18:11:21.668797Z","shell.execute_reply.started":"2022-11-23T18:11:21.663375Z","shell.execute_reply":"2022-11-23T18:11:21.667626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:11:26.300857Z","iopub.execute_input":"2022-11-23T18:11:26.30131Z","iopub.status.idle":"2022-11-23T18:11:26.317602Z","shell.execute_reply.started":"2022-11-23T18:11:26.301269Z","shell.execute_reply":"2022-11-23T18:11:26.316168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:11:30.578375Z","iopub.execute_input":"2022-11-23T18:11:30.578745Z","iopub.status.idle":"2022-11-23T18:11:30.595702Z","shell.execute_reply.started":"2022-11-23T18:11:30.578713Z","shell.execute_reply":"2022-11-23T18:11:30.594742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Create an ImageDataGenerator\n\nThis is made because the dataset does not have enough data in some targets\n\nAlso, this generator helps to load the files","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ntrain_datagen = ImageDataGenerator(rescale = 1/255,\n                                rotation_range=20,\n                                width_shift_range=0.2,\n                                height_shift_range=0.2,\n                                horizontal_flip=True,\n                                vertical_flip = True)\n\nval_datagen = ImageDataGenerator(rescale = 1/255,\n                                 rotation_range=20,\n                                width_shift_range=0.2,\n                                height_shift_range=0.2,\n                                horizontal_flip=True,\n                                vertical_flip = True)\n\ntrain_dataset = train_datagen.flow_from_dataframe(\n    train,\n    directory = '../input/plant-pathology-2021-fgvc8/train_images',\n    x_col = \"image\",\n    y_col = 'labels',\n    target_size = (300,300),\n    class_mode='categorical',\n    batch_size = 64,\n    shuffle = True,\n)\n\nval_dataset = val_datagen.flow_from_dataframe(\n    val,\n    directory = '../input/plant-pathology-2021-fgvc8/train_images',\n    x_col = \"image\",\n    y_col = 'labels',\n    target_size = (300,300),\n    class_mode='categorical',\n    batch_size = 64,\n    shuffle = True,\n)\n","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:11:52.548089Z","iopub.execute_input":"2022-11-23T18:11:52.548448Z","iopub.status.idle":"2022-11-23T18:12:27.2196Z","shell.execute_reply.started":"2022-11-23T18:11:52.548417Z","shell.execute_reply":"2022-11-23T18:12:27.218544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_dataset.class_indices","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:15:08.793594Z","iopub.execute_input":"2022-11-23T18:15:08.793971Z","iopub.status.idle":"2022-11-23T18:15:08.800407Z","shell.execute_reply.started":"2022-11-23T18:15:08.79394Z","shell.execute_reply":"2022-11-23T18:15:08.799497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset.class_indices","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:16:02.277329Z","iopub.execute_input":"2022-11-23T18:16:02.277736Z","iopub.status.idle":"2022-11-23T18:16:02.28365Z","shell.execute_reply.started":"2022-11-23T18:16:02.277702Z","shell.execute_reply":"2022-11-23T18:16:02.282706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Create the CNN architecture","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, BatchNormalization, Dense, Flatten, Dropout","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:16:16.034219Z","iopub.execute_input":"2022-11-23T18:16:16.034598Z","iopub.status.idle":"2022-11-23T18:16:16.043429Z","shell.execute_reply.started":"2022-11-23T18:16:16.034544Z","shell.execute_reply":"2022-11-23T18:16:16.042464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n# Convolutional layer #1\nmodel.add(Conv2D(filters=32,kernel_size=(3,3),padding='same',activation='relu',strides=(1,1),input_shape=(300,300,3)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n# Convolutional layer #2\nmodel.add(Conv2D(filters=64,kernel_size=(3,3),padding='same',activation='relu',strides=(1,1)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n# Convolutional layer #3\nmodel.add(Conv2D(filters=128,kernel_size=(3,3),padding='same',activation='relu',strides=(1,1)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n# Convolutional layer #4\nmodel.add(Conv2D(filters=264,kernel_size=(3,3),padding='same',activation='relu',strides=(1,1)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n# Convolutional layer #5\nmodel.add(Conv2D(filters=264,kernel_size=(3,3),padding='same',activation='relu',strides=(1,1)))\nmodel.add(MaxPooling2D(pool_size=(2,2)))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.2))\n# add a flatten layers\nmodel.add(Flatten())\n# add dense layer #1\nmodel.add(Dense(units=50, activation='relu'))\nmodel.add(Dropout(0.2))\n# add dense layer #2\nmodel.add(Dense(units=6, activation='softmax'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:16:32.112071Z","iopub.execute_input":"2022-11-23T18:16:32.113013Z","iopub.status.idle":"2022-11-23T18:16:34.986927Z","shell.execute_reply.started":"2022-11-23T18:16:32.112975Z","shell.execute_reply":"2022-11-23T18:16:34.985947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from tensorflow.keras.callbacks import ModelCheckpoint\n# chekcpoint = ModelCheckpoint('my_model.hdf5',verbose=1,save_best_only=True, monitor = 'val_accuracy')","metadata":{"execution":{"iopub.status.busy":"2022-08-08T19:42:41.884567Z","iopub.execute_input":"2022-08-08T19:42:41.88576Z","iopub.status.idle":"2022-08-08T19:42:41.891555Z","shell.execute_reply.started":"2022-08-08T19:42:41.885718Z","shell.execute_reply":"2022-08-08T19:42:41.890439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='categorical_crossentropy', metrics='accuracy')\nhistory = model.fit(train_dataset,epochs=2,validation_data=val_dataset)","metadata":{"execution":{"iopub.status.busy":"2022-11-23T18:21:20.711392Z","iopub.execute_input":"2022-11-23T18:21:20.711773Z","iopub.status.idle":"2022-11-23T18:29:13.39243Z","shell.execute_reply.started":"2022-11-23T18:21:20.71174Z","shell.execute_reply":"2022-11-23T18:29:13.38931Z"},"trusted":true},"execution_count":null,"outputs":[]}]}