{"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":"# **Import Libraries**","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport os\nimport tensorflow as tf\nimport pandas as pd\nimport glob\nimport math\nimport matplotlib.pyplot as plt\nimport tensorflow_addons as tfa\nfrom PIL import Image\nfrom collections import Counter","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:45:28.974868Z","iopub.execute_input":"2022-05-21T09:45:28.975133Z","iopub.status.idle":"2022-05-21T09:45:33.864377Z","shell.execute_reply.started":"2022-05-21T09:45:28.975057Z","shell.execute_reply":"2022-05-21T09:45:33.863447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Train data preprocessing","metadata":{}},{"cell_type":"code","source":"train_cultivar= pd.read_csv('/kaggle/input/sorghum-id-fgvc-9/train_cultivar_mapping.csv')\ntrain_cultivar.dropna(inplace=True)\ntrain_cultivar['cultivar']=train_cultivar['cultivar'].astype(str)\ntrain_cultivar.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:50:15.632275Z","iopub.execute_input":"2022-05-21T09:50:15.63258Z","iopub.status.idle":"2022-05-21T09:50:15.672953Z","shell.execute_reply.started":"2022-05-21T09:50:15.63255Z","shell.execute_reply":"2022-05-21T09:50:15.672221Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=list(np.unique(train_cultivar['cultivar']))","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:50:22.044332Z","iopub.execute_input":"2022-05-21T09:50:22.044591Z","iopub.status.idle":"2022-05-21T09:50:22.072308Z","shell.execute_reply.started":"2022-05-21T09:50:22.04456Z","shell.execute_reply":"2022-05-21T09:50:22.071561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\na=pd.DataFrame({'cultivar':train_cultivar['cultivar']})\nplt.figure(figsize=(30,10))\nsns.histplot(a,x='cultivar')\nplt.xticks(rotation=45)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T20:25:09.804666Z","iopub.execute_input":"2022-05-16T20:25:09.805091Z","iopub.status.idle":"2022-05-16T20:25:11.905757Z","shell.execute_reply.started":"2022-05-16T20:25:09.805059Z","shell.execute_reply":"2022-05-16T20:25:11.904758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen=tf.keras.preprocessing.image.ImageDataGenerator(horizontal_flip=True, \n                                                              rotation_range=0.9,\n                                                              brightness_range=(0.1,0.9))\ntrain_data=train_datagen.flow_from_dataframe(train_cultivar, \n                                             directory=\"/kaggle/input/sorghum-id-fgvc-9/train_images/\",\n                                             color_mode='rgb', \n                                             batch_size=32, \n                                             x_col='image', \n                                             y_col='cultivar', \n                                             classes=labels, \n                                             class_mode='categorical', \n                                             target_size=(512, 512))","metadata":{"execution":{"iopub.status.busy":"2022-05-16T20:27:40.420711Z","iopub.execute_input":"2022-05-16T20:27:40.421028Z","iopub.status.idle":"2022-05-16T20:27:49.804661Z","shell.execute_reply.started":"2022-05-16T20:27:40.420995Z","shell.execute_reply":"2022-05-16T20:27:49.803524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len_traindata=len(train_cultivar)\n\nlen_traindata","metadata":{"execution":{"iopub.status.busy":"2022-05-16T20:27:49.806021Z","iopub.execute_input":"2022-05-16T20:27:49.806345Z","iopub.status.idle":"2022-05-16T20:27:49.8139Z","shell.execute_reply.started":"2022-05-16T20:27:49.806316Z","shell.execute_reply":"2022-05-16T20:27:49.813038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **EfficientnetB2(Base Model)**","metadata":{}},{"cell_type":"code","source":"base_model= tf.keras.applications.efficientnet.EfficientNetB2(include_top=False, \n                                                              weights='imagenet', \n                                                              input_shape=(512,512,3))\n","metadata":{"execution":{"iopub.status.busy":"2022-05-16T20:27:58.259574Z","iopub.execute_input":"2022-05-16T20:27:58.260414Z","iopub.status.idle":"2022-05-16T20:28:00.704552Z","shell.execute_reply.started":"2022-05-16T20:27:58.260354Z","shell.execute_reply":"2022-05-16T20:28:00.703596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_batch,label_image= next(iter(train_data))\nfeature_batch=base_model(image_batch)\nfeature_batch.shape","metadata":{"execution":{"iopub.status.busy":"2022-05-16T20:28:01.804246Z","iopub.execute_input":"2022-05-16T20:28:01.804561Z","iopub.status.idle":"2022-05-16T20:28:06.32193Z","shell.execute_reply.started":"2022-05-16T20:28:01.804507Z","shell.execute_reply":"2022-05-16T20:28:06.321052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Design the Model**","metadata":{}},{"cell_type":"code","source":"inputs=tf.keras.Input(shape=(512,512,3))\nx=base_model(inputs, training=False)\nx = tf.keras.layers.GlobalAveragePooling2D()(x)\nx = tf.keras.layers.Dropout(0.5)(x)\noutputs=tf.keras.layers.Dense(100, activation='softmax')(x)\nmodel=tf.keras.Model(inputs, outputs)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T20:28:14.173687Z","iopub.execute_input":"2022-05-16T20:28:14.174574Z","iopub.status.idle":"2022-05-16T20:28:14.827787Z","shell.execute_reply.started":"2022-05-16T20:28:14.174496Z","shell.execute_reply":"2022-05-16T20:28:14.827133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Callbacks**","metadata":{}},{"cell_type":"code","source":"class callback1(tf.keras.callbacks.Callback):\n    def on_epoch_end(self, epochs, logs={}):\n        if (logs.get('accuracy')>0.99):\n            print('\\nStopping training since train accuracy is greater than 97%')\n            self.model.stop_training=True","metadata":{"execution":{"iopub.status.busy":"2022-05-11T20:25:11.538304Z","iopub.execute_input":"2022-05-11T20:25:11.539416Z","iopub.status.idle":"2022-05-11T20:25:11.547643Z","shell.execute_reply.started":"2022-05-11T20:25:11.539378Z","shell.execute_reply":"2022-05-11T20:25:11.546808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback2=tf.keras.callbacks.EarlyStopping(monitor='accuracy', patience=2, restore_best_weights=True)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Cyclical Learning Rate**","metadata":{}},{"cell_type":"code","source":"\nbatch_size=32\nif abs(len_traindata/batch_size-round(len_traindata/batch_size))>0.5:\n    total_steps=round(len_traindata/batch_size)\nelse:\n    total_steps=round(len_traindata/batch_size)+1\nclr = tfa.optimizers.CyclicalLearningRate(initial_learning_rate=1e-4,\n    maximal_learning_rate=1e-3,\n    scale_fn=lambda x: 1/(2.**(x-1)),\n    step_size=2 * total_steps\n)\n","metadata":{"execution":{"iopub.status.busy":"2022-05-16T20:30:41.227731Z","iopub.execute_input":"2022-05-16T20:30:41.228484Z","iopub.status.idle":"2022-05-16T20:30:41.234334Z","shell.execute_reply.started":"2022-05-16T20:30:41.228447Z","shell.execute_reply":"2022-05-16T20:30:41.233751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Compile the model**","metadata":{}},{"cell_type":"code","source":"for layer in base_model.layers[:270]:\n    layer.trainable=False","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-05-16T20:33:48.60377Z","iopub.execute_input":"2022-05-16T20:33:48.604086Z","iopub.status.idle":"2022-05-16T20:33:48.627989Z","shell.execute_reply.started":"2022-05-16T20:33:48.604026Z","shell.execute_reply":"2022-05-16T20:33:48.625298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=tf.keras.losses.CategoricalCrossentropy(), optimizer=tf.keras.optimizers.Adam(learning_rate=clr), metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-05-16T20:34:07.765161Z","iopub.execute_input":"2022-05-16T20:34:07.765598Z","iopub.status.idle":"2022-05-16T20:34:07.783606Z","shell.execute_reply.started":"2022-05-16T20:34:07.765567Z","shell.execute_reply":"2022-05-16T20:34:07.78292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Fit the model and Visualize**","metadata":{}},{"cell_type":"code","source":"tf.keras.utils.plot_model(model)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T20:34:10.274524Z","iopub.execute_input":"2022-05-16T20:34:10.274959Z","iopub.status.idle":"2022-05-16T20:34:11.518625Z","shell.execute_reply.started":"2022-05-16T20:34:10.274929Z","shell.execute_reply":"2022-05-16T20:34:11.517919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=model.fit(train_data, epochs=15, callbacks=[callback1(), callback2])","metadata":{"execution":{"iopub.status.busy":"2022-05-10T20:08:47.770677Z","iopub.execute_input":"2022-05-10T20:08:47.770975Z","iopub.status.idle":"2022-05-10T20:18:06.902348Z","shell.execute_reply.started":"2022-05-10T20:08:47.77093Z","shell.execute_reply":"2022-05-10T20:18:06.899154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(history.history).plot()\nplt.ylabel('loss & accuracy')\nplt.xlabel('epochs')","metadata":{"execution":{"iopub.status.busy":"2022-05-10T20:07:50.023316Z","iopub.execute_input":"2022-05-10T20:07:50.023939Z","iopub.status.idle":"2022-05-10T20:07:50.047481Z","shell.execute_reply.started":"2022-05-10T20:07:50.023889Z","shell.execute_reply":"2022-05-10T20:07:50.046288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Fine Tuning**","metadata":{}},{"cell_type":"code","source":"'''count=0\nfor i in base_model.layers:\n    count+=1\ncount'''","metadata":{"execution":{"iopub.status.busy":"2022-05-14T13:44:00.460622Z","iopub.execute_input":"2022-05-14T13:44:00.461199Z","iopub.status.idle":"2022-05-14T13:44:00.495131Z","shell.execute_reply.started":"2022-05-14T13:44:00.461096Z","shell.execute_reply":"2022-05-14T13:44:00.494111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"for layer1 in base_model.layers:\n    layer1.trainable=False\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-05-11T20:30:33.86694Z","iopub.execute_input":"2022-05-11T20:30:33.867218Z","iopub.status.idle":"2022-05-11T20:30:33.883576Z","shell.execute_reply.started":"2022-05-11T20:30:33.867188Z","shell.execute_reply":"2022-05-11T20:30:33.882851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''model.summary()'''","metadata":{"execution":{"iopub.status.busy":"2022-05-11T20:30:40.954167Z","iopub.execute_input":"2022-05-11T20:30:40.954829Z","iopub.status.idle":"2022-05-11T20:30:40.980276Z","shell.execute_reply.started":"2022-05-11T20:30:40.954772Z","shell.execute_reply":"2022-05-11T20:30:40.979518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''model.compile(loss=tf.keras.losses.CategoricalCrossentropy(), optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5), metrics=['accuracy'])'''","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''history_fine=model.fit(train_data, epochs=25,initial_epoch=history.epoch[-1],validation_data=valid_data,callbacks=[callback1, callback3, callback4()])'''","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"'''pd.DataFrame(history_fine.history).plot()\nplt.ylabel('loss & accuracy')\nplt.xlabel('epochs')'''","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Prepare Test Data**","metadata":{}},{"cell_type":"code","source":"test_data=pd.read_csv('/kaggle/input/sorghum-id-fgvc-9/sample_submission.csv').drop('cultivar',axis=1)\ntest_data","metadata":{"execution":{"iopub.status.busy":"2022-05-16T20:39:16.499695Z","iopub.execute_input":"2022-05-16T20:39:16.49999Z","iopub.status.idle":"2022-05-16T20:39:16.550805Z","shell.execute_reply.started":"2022-05-16T20:39:16.49996Z","shell.execute_reply":"2022-05-16T20:39:16.549856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_datagen=tf.keras.preprocessing.image.ImageDataGenerator()\ntest_dataset=test_datagen.flow_from_dataframe(test_data,directory=\"/kaggle/input/sorghum-id-fgvc-9/test/\", x_col='filename',y_col=None, color_mode='rgb', target_size=(512,512), shuffle=False, class_mode=None)","metadata":{"execution":{"iopub.status.busy":"2022-05-16T20:39:31.540885Z","iopub.execute_input":"2022-05-16T20:39:31.541513Z","iopub.status.idle":"2022-05-16T20:40:07.453599Z","shell.execute_reply.started":"2022-05-16T20:39:31.541478Z","shell.execute_reply":"2022-05-16T20:40:07.452979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Predict**","metadata":{}},{"cell_type":"code","source":"predict1=model.predict(test_dataset)\n","metadata":{"execution":{"iopub.status.busy":"2022-04-30T12:33:34.343219Z","iopub.execute_input":"2022-04-30T12:33:34.34356Z","iopub.status.idle":"2022-04-30T13:01:01.893894Z","shell.execute_reply.started":"2022-04-30T12:33:34.343526Z","shell.execute_reply":"2022-04-30T13:01:01.892068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict1.shape","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:03:24.036575Z","iopub.execute_input":"2022-04-30T13:03:24.036842Z","iopub.status.idle":"2022-04-30T13:03:24.042697Z","shell.execute_reply.started":"2022-04-30T13:03:24.036814Z","shell.execute_reply":"2022-04-30T13:03:24.041898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict2=np.argmax(predict1, axis=-1)","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:36:36.289078Z","iopub.execute_input":"2022-04-30T13:36:36.289353Z","iopub.status.idle":"2022-04-30T13:36:36.303071Z","shell.execute_reply.started":"2022-04-30T13:36:36.289323Z","shell.execute_reply":"2022-04-30T13:36:36.302194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions=[]\nfor k in predict2:\n    predictions.append(labels[k])\n  \n","metadata":{"execution":{"iopub.status.busy":"2022-04-30T13:36:43.483531Z","iopub.execute_input":"2022-04-30T13:36:43.483784Z","iopub.status.idle":"2022-04-30T13:36:51.24699Z","shell.execute_reply.started":"2022-04-30T13:36:43.483757Z","shell.execute_reply":"2022-04-30T13:36:51.246065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final=pd.DataFrame({'filename': test_data.filename, 'cultivar': predictions})\nfinal.to_csv('submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]}]}