{"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-20T15:58:33.402452Z","iopub.execute_input":"2022-07-20T15:58:33.402894Z","iopub.status.idle":"2022-07-20T15:58:33.408135Z","shell.execute_reply.started":"2022-07-20T15:58:33.402852Z","shell.execute_reply":"2022-07-20T15:58:33.406978Z"},"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-20T15:58:33.410268Z","iopub.execute_input":"2022-07-20T15:58:33.411074Z","iopub.status.idle":"2022-07-20T15:58:33.436033Z","shell.execute_reply.started":"2022-07-20T15:58:33.410971Z","shell.execute_reply":"2022-07-20T15:58:33.435127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels_dataframe","metadata":{"execution":{"iopub.status.busy":"2022-07-20T15:58:33.437825Z","iopub.execute_input":"2022-07-20T15:58:33.438170Z","iopub.status.idle":"2022-07-20T15:58:33.453122Z","shell.execute_reply.started":"2022-07-20T15:58:33.438135Z","shell.execute_reply":"2022-07-20T15:58:33.452132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2022-07-20T15:58:33.455172Z","iopub.execute_input":"2022-07-20T15:58:33.455948Z","iopub.status.idle":"2022-07-20T15:58:33.470029Z","shell.execute_reply.started":"2022-07-20T15:58:33.455912Z","shell.execute_reply":"2022-07-20T15:58:33.468579Z"},"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-20T15:58:33.471611Z","iopub.execute_input":"2022-07-20T15:58:33.472156Z","iopub.status.idle":"2022-07-20T15:58:33.483081Z","shell.execute_reply.started":"2022-07-20T15:58:33.472120Z","shell.execute_reply":"2022-07-20T15:58:33.481990Z"},"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-20T15:58:33.485312Z","iopub.execute_input":"2022-07-20T15:58:33.485904Z","iopub.status.idle":"2022-07-20T15:58:35.392875Z","shell.execute_reply.started":"2022-07-20T15:58:33.485866Z","shell.execute_reply":"2022-07-20T15:58:35.391773Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data","metadata":{"execution":{"iopub.status.busy":"2022-07-20T15:58:35.397712Z","iopub.execute_input":"2022-07-20T15:58:35.398569Z","iopub.status.idle":"2022-07-20T15:58:35.405433Z","shell.execute_reply.started":"2022-07-20T15:58:35.398524Z","shell.execute_reply":"2022-07-20T15:58:35.404424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_data","metadata":{"execution":{"iopub.status.busy":"2022-07-20T15:58:35.408228Z","iopub.execute_input":"2022-07-20T15:58:35.408871Z","iopub.status.idle":"2022-07-20T15:58:35.421151Z","shell.execute_reply.started":"2022-07-20T15:58:35.408835Z","shell.execute_reply":"2022-07-20T15:58:35.420215Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import models,layers","metadata":{"execution":{"iopub.status.busy":"2022-07-20T15:58:35.422651Z","iopub.execute_input":"2022-07-20T15:58:35.423356Z","iopub.status.idle":"2022-07-20T15:58:35.432884Z","shell.execute_reply.started":"2022-07-20T15:58:35.423320Z","shell.execute_reply":"2022-07-20T15:58:35.431783Z"},"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-20T15:58:35.434312Z","iopub.execute_input":"2022-07-20T15:58:35.434919Z","iopub.status.idle":"2022-07-20T15:58:37.574444Z","shell.execute_reply.started":"2022-07-20T15:58:35.434883Z","shell.execute_reply":"2022-07-20T15:58:37.573359Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"resnet_model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-20T15:58:37.575879Z","iopub.execute_input":"2022-07-20T15:58:37.576205Z","iopub.status.idle":"2022-07-20T15:58:37.596279Z","shell.execute_reply.started":"2022-07-20T15:58:37.576170Z","shell.execute_reply":"2022-07-20T15:58:37.595440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tf.keras.utils.plot_model(resnet_model)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T15:58:37.597760Z","iopub.execute_input":"2022-07-20T15:58:37.598205Z","iopub.status.idle":"2022-07-20T15:58:37.791016Z","shell.execute_reply.started":"2022-07-20T15:58:37.598165Z","shell.execute_reply":"2022-07-20T15:58:37.789900Z"},"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=20)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T15:58:37.794852Z","iopub.execute_input":"2022-07-20T15:58:37.795527Z","iopub.status.idle":"2022-07-20T16:46:48.334519Z","shell.execute_reply.started":"2022-07-20T15:58:37.795486Z","shell.execute_reply":"2022-07-20T16:46:48.333404Z"},"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-20T16:46:48.335998Z","iopub.execute_input":"2022-07-20T16:46:48.336328Z","iopub.status.idle":"2022-07-20T16:46:51.088719Z","shell.execute_reply.started":"2022-07-20T16:46:48.336294Z","shell.execute_reply":"2022-07-20T16:46:51.087190Z"},"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-20T16:46:51.090203Z","iopub.execute_input":"2022-07-20T16:46:51.090521Z","iopub.status.idle":"2022-07-20T16:47:32.819895Z","shell.execute_reply.started":"2022-07-20T16:46:51.090487Z","shell.execute_reply":"2022-07-20T16:47:32.818807Z"},"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-20T16:47:32.821743Z","iopub.execute_input":"2022-07-20T16:47:32.822118Z","iopub.status.idle":"2022-07-20T16:47:34.393113Z","shell.execute_reply.started":"2022-07-20T16:47:32.822081Z","shell.execute_reply":"2022-07-20T16:47:34.392028Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions[:10]","metadata":{"execution":{"iopub.status.busy":"2022-07-20T16:47:34.395252Z","iopub.execute_input":"2022-07-20T16:47:34.396105Z","iopub.status.idle":"2022-07-20T16:47:34.403153Z","shell.execute_reply.started":"2022-07-20T16:47:34.396065Z","shell.execute_reply":"2022-07-20T16:47:34.402290Z"},"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-20T16:47:34.404663Z","iopub.execute_input":"2022-07-20T16:47:34.405290Z","iopub.status.idle":"2022-07-20T16:47:34.431076Z","shell.execute_reply.started":"2022-07-20T16:47:34.405253Z","shell.execute_reply":"2022-07-20T16:47:34.430087Z"},"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-20T16:47:34.432236Z","iopub.execute_input":"2022-07-20T16:47:34.432712Z","iopub.status.idle":"2022-07-20T16:47:34.460186Z","shell.execute_reply.started":"2022-07-20T16:47:34.432681Z","shell.execute_reply":"2022-07-20T16:47:34.458267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('Paddy Doctor.csv',index=None)","metadata":{"execution":{"iopub.status.busy":"2022-07-20T16:47:34.463960Z","iopub.execute_input":"2022-07-20T16:47:34.464666Z","iopub.status.idle":"2022-07-20T16:47:34.480694Z","shell.execute_reply.started":"2022-07-20T16:47:34.464631Z","shell.execute_reply":"2022-07-20T16:47:34.479645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}