{"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 pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nimport warnings\nwarnings.filterwarnings(action='ignore')","metadata":{"execution":{"iopub.status.busy":"2022-07-19T12:33:19.443776Z","iopub.execute_input":"2022-07-19T12:33:19.444799Z","iopub.status.idle":"2022-07-19T12:33:19.450639Z","shell.execute_reply.started":"2022-07-19T12:33:19.444758Z","shell.execute_reply":"2022-07-19T12:33:19.449521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe=pd.read_csv('../input/paddy-disease-classification/train.csv')\nsub_df=pd.read_csv('../input/paddy-disease-classification/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-19T12:33:19.601749Z","iopub.execute_input":"2022-07-19T12:33:19.602379Z","iopub.status.idle":"2022-07-19T12:33:19.633204Z","shell.execute_reply.started":"2022-07-19T12:33:19.602340Z","shell.execute_reply":"2022-07-19T12:33:19.632200Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe","metadata":{"execution":{"iopub.status.busy":"2022-07-19T12:33:19.643243Z","iopub.execute_input":"2022-07-19T12:33:19.643613Z","iopub.status.idle":"2022-07-19T12:33:19.659585Z","shell.execute_reply.started":"2022-07-19T12:33:19.643582Z","shell.execute_reply":"2022-07-19T12:33:19.658381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2022-07-19T12:33:19.793700Z","iopub.execute_input":"2022-07-19T12:33:19.794489Z","iopub.status.idle":"2022-07-19T12:33:19.810902Z","shell.execute_reply.started":"2022-07-19T12:33:19.794438Z","shell.execute_reply":"2022-07-19T12:33:19.809969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"diseases_types=sorted(list(set(labels_dataframe['label'])))\nn_classes=len(diseases_types)\nprint(n_classes)\nprint(diseases_types)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T12:33:19.813269Z","iopub.execute_input":"2022-07-19T12:33:19.813717Z","iopub.status.idle":"2022-07-19T12:33:19.820661Z","shell.execute_reply.started":"2022-07-19T12:33:19.813673Z","shell.execute_reply":"2022-07-19T12:33:19.819639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data= tf.keras.utils.image_dataset_from_directory(\"/kaggle/input/paddy-disease-classification/train_images/\",\n                                                        labels='inferred',\n                                                        label_mode='categorical',\n                                                        class_names=diseases_types,\n                                                        color_mode='rgb',\n                                                        image_size=(680,480),\n                                                        shuffle=True,\n                                                        validation_split=0.2,\n                                                        subset='training',\n                                                        seed=42)\n\nval_data= tf.keras.utils.image_dataset_from_directory(\"/kaggle/input/paddy-disease-classification/train_images/\",\n                                                        labels='inferred',\n                                                        label_mode='categorical',\n                                                        class_names=diseases_types,   \n                                                        color_mode='rgb',\n                                                        image_size=(680,480),\n                                                        shuffle=True,\n                                                        validation_split=0.2,\n                                                        subset='validation',\n                                                        seed=42)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T12:33:19.905687Z","iopub.execute_input":"2022-07-19T12:33:19.906289Z","iopub.status.idle":"2022-07-19T12:33:21.161280Z","shell.execute_reply.started":"2022-07-19T12:33:19.906255Z","shell.execute_reply":"2022-07-19T12:33:21.160357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2022-07-19T12:33:21.163449Z","iopub.execute_input":"2022-07-19T12:33:21.163889Z","iopub.status.idle":"2022-07-19T12:33:21.171972Z","shell.execute_reply.started":"2022-07-19T12:33:21.163844Z","shell.execute_reply":"2022-07-19T12:33:21.170810Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_data","metadata":{"execution":{"iopub.status.busy":"2022-07-19T12:33:21.174138Z","iopub.execute_input":"2022-07-19T12:33:21.174639Z","iopub.status.idle":"2022-07-19T12:33:21.183184Z","shell.execute_reply.started":"2022-07-19T12:33:21.174604Z","shell.execute_reply":"2022-07-19T12:33:21.182129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import models,layers","metadata":{"execution":{"iopub.status.busy":"2022-07-19T12:33:21.185597Z","iopub.execute_input":"2022-07-19T12:33:21.186466Z","iopub.status.idle":"2022-07-19T12:33:21.192604Z","shell.execute_reply.started":"2022-07-19T12:33:21.186430Z","shell.execute_reply":"2022-07-19T12:33:21.191655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model=models.Sequential()\n\npretrained_model=tf.keras.applications.ResNet50(include_top=False,input_shape=(680,480,3),pooling='avg',classes=10,weights='imagenet')\n    \nfor layer in pretrained_model.layers:\n    layer.trainable=False\n    \n    \nresnet_model.add(pretrained_model)\nresnet_model.add(layers.Flatten())\nresnet_model.add(layers.Dense(512,activation='relu'))\nresnet_model.add(layers.Dense(99,activation='relu'))\nresnet_model.add(layers.Dense(10,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-07-19T12:33:21.195126Z","iopub.execute_input":"2022-07-19T12:33:21.195881Z","iopub.status.idle":"2022-07-19T12:33:23.282383Z","shell.execute_reply.started":"2022-07-19T12:33:21.195846Z","shell.execute_reply":"2022-07-19T12:33:23.281280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T12:33:23.283874Z","iopub.execute_input":"2022-07-19T12:33:23.284286Z","iopub.status.idle":"2022-07-19T12:33:23.304036Z","shell.execute_reply.started":"2022-07-19T12:33:23.284227Z","shell.execute_reply":"2022-07-19T12:33:23.302987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(resnet_model)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T12:33:23.305542Z","iopub.execute_input":"2022-07-19T12:33:23.306090Z","iopub.status.idle":"2022-07-19T12:33:23.717409Z","shell.execute_reply.started":"2022-07-19T12:33:23.306051Z","shell.execute_reply":"2022-07-19T12:33:23.716347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])\nresnet_model.fit(train_data,validation_data=val_data,epochs=15)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T14:21:26.403190Z","iopub.execute_input":"2022-07-19T14:21:26.404132Z","iopub.status.idle":"2022-07-19T14:57:37.795392Z","shell.execute_reply.started":"2022-07-19T14:21:26.404080Z","shell.execute_reply":"2022-07-19T14:57:37.794461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data=tf.keras.utils.image_dataset_from_directory(\"/kaggle/input/paddy-disease-classification/test_images/\",\n                                                        labels=None,\n                                                        label_mode=None,\n                                                        color_mode='rgb',\n                                                        image_size=(680,480),\n                                                        shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T14:08:09.221486Z","iopub.execute_input":"2022-07-19T14:08:09.222553Z","iopub.status.idle":"2022-07-19T14:08:09.955955Z","shell.execute_reply.started":"2022-07-19T14:08:09.222506Z","shell.execute_reply":"2022-07-19T14:08:09.954796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict=tf.argmax(resnet_model.predict(test_data),axis=-1)\npredict","metadata":{"execution":{"iopub.status.busy":"2022-07-19T14:11:34.828525Z","iopub.execute_input":"2022-07-19T14:11:34.829123Z","iopub.status.idle":"2022-07-19T14:12:08.430009Z","shell.execute_reply.started":"2022-07-19T14:11:34.829087Z","shell.execute_reply":"2022-07-19T14:12:08.429045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions=[]\nfor i in predict:\n    predictions.append(diseases_types[i])","metadata":{"execution":{"iopub.status.busy":"2022-07-19T14:12:34.444035Z","iopub.execute_input":"2022-07-19T14:12:34.444410Z","iopub.status.idle":"2022-07-19T14:12:35.601284Z","shell.execute_reply.started":"2022-07-19T14:12:34.444377Z","shell.execute_reply":"2022-07-19T14:12:35.600236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[:10]","metadata":{"execution":{"iopub.status.busy":"2022-07-19T14:12:48.732687Z","iopub.execute_input":"2022-07-19T14:12:48.733046Z","iopub.status.idle":"2022-07-19T14:12:48.740324Z","shell.execute_reply.started":"2022-07-19T14:12:48.733018Z","shell.execute_reply":"2022-07-19T14:12:48.739262Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df=sub_df.drop(columns='label',axis=1)\nsub_df","metadata":{"execution":{"iopub.status.busy":"2022-07-19T14:13:53.138464Z","iopub.execute_input":"2022-07-19T14:13:53.138820Z","iopub.status.idle":"2022-07-19T14:13:53.164340Z","shell.execute_reply.started":"2022-07-19T14:13:53.138789Z","shell.execute_reply":"2022-07-19T14:13:53.163204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.DataFrame({'image_id':sub_df['image_id'],'label':predictions})\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-19T14:15:27.553135Z","iopub.execute_input":"2022-07-19T14:15:27.553506Z","iopub.status.idle":"2022-07-19T14:15:27.567057Z","shell.execute_reply.started":"2022-07-19T14:15:27.553476Z","shell.execute_reply":"2022-07-19T14:15:27.565895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('paddy_docto2.csv',index=None)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T14:16:11.933186Z","iopub.execute_input":"2022-07-19T14:16:11.933962Z","iopub.status.idle":"2022-07-19T14:16:11.949446Z","shell.execute_reply.started":"2022-07-19T14:16:11.933921Z","shell.execute_reply":"2022-07-19T14:16:11.948496Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}