{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-07-23T00:05:45.236327Z","iopub.execute_input":"2022-07-23T00:05:45.237086Z","iopub.status.idle":"2022-07-23T00:05:49.096129Z","shell.execute_reply.started":"2022-07-23T00:05:45.236959Z","shell.execute_reply":"2022-07-23T00:05:49.088714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nimport cv2\nimport os\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.098983Z","iopub.status.idle":"2022-07-23T00:05:49.102325Z","shell.execute_reply.started":"2022-07-23T00:05:49.102036Z","shell.execute_reply":"2022-07-23T00:05:49.102060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = pd.read_csv(\"/kaggle/input/plant-pathology-2021-fgvc8/train.csv\") \n#data_test = pd.read_csv(\"/kaggle/input/spaceship-titanic/test.csv\")\ndata.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.107168Z","iopub.status.idle":"2022-07-23T00:05:49.111348Z","shell.execute_reply.started":"2022-07-23T00:05:49.111061Z","shell.execute_reply":"2022-07-23T00:05:49.111085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data.describe().T","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.116056Z","iopub.status.idle":"2022-07-23T00:05:49.119350Z","shell.execute_reply.started":"2022-07-23T00:05:49.117219Z","shell.execute_reply":"2022-07-23T00:05:49.117242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import cv2\nimg_folder='/kaggle/input/plant-pathology-2021-fgvc8/train_images/8002cb321f8bfcdf.jpg'\ninput = cv2.imread(img_folder)\nprint(input.shape)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.121439Z","iopub.status.idle":"2022-07-23T00:05:49.123616Z","shell.execute_reply.started":"2022-07-23T00:05:49.123368Z","shell.execute_reply":"2022-07-23T00:05:49.123393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1=pd.read_csv('/kaggle/input/plant-pathology-2021-fgvc8/train.csv')\ndf1.head()\n#we have stored the locations frome where labels are taken","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.127308Z","iopub.status.idle":"2022-07-23T00:05:49.128233Z","shell.execute_reply.started":"2022-07-23T00:05:49.127962Z","shell.execute_reply":"2022-07-23T00:05:49.127988Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_file='/kaggle/input/plant-pathology-2021-fgvc8/train_images/'\n#we have stored the location of images","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.130670Z","iopub.status.idle":"2022-07-23T00:05:49.132640Z","shell.execute_reply.started":"2022-07-23T00:05:49.132369Z","shell.execute_reply":"2022-07-23T00:05:49.132391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=df1.assign(img_path=lambda x: img_file + x['image'])\ndf.head()\n#loding of images","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.135827Z","iopub.status.idle":"2022-07-23T00:05:49.142603Z","shell.execute_reply.started":"2022-07-23T00:05:49.142357Z","shell.execute_reply":"2022-07-23T00:05:49.142380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.labels.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.143813Z","iopub.status.idle":"2022-07-23T00:05:49.146026Z","shell.execute_reply.started":"2022-07-23T00:05:49.145754Z","shell.execute_reply":"2022-07-23T00:05:49.145778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax=pd.value_counts(df['labels'],ascending=True).plot(kind='barh',fontsize=\"20\",title=\"Class Distribution\",figsize=(20,30))\nax.set(xlabel=\"Images per class\", ylabel=\"Classes\")\nax.xaxis.label.set_size(10)\nax.yaxis.label.set_size(10)\nax.title.set_size(20)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.147296Z","iopub.status.idle":"2022-07-23T00:05:49.148026Z","shell.execute_reply.started":"2022-07-23T00:05:49.147790Z","shell.execute_reply":"2022-07-23T00:05:49.147813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\ntrain_df = pd.read_csv(\"../input/plant-pathology-2021-fgvc8/train.csv\")\nTRAIN_PATH = \"../input/plant-pathology-2021-fgvc8/train_images\"\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    validation_split = 0.2,\n    zoom_range = 0.2,\n    shear_range = 0.2,\n    vertical_flip = False)\n\n\nHEIGHT = 256\nWIDTH=256\nSEED = 143\nBATCH_SIZE=32\ntrain_ds = train_datagen.flow_from_dataframe(\n    train_df,\n    directory = TRAIN_PATH,\n    x_col = \"image\",\n    y_col = \"labels\",\n    target_size = (HEIGHT,WIDTH),\n    class_mode='categorical',\n    batch_size = BATCH_SIZE,\n    subset = \"training\",\n    shuffle = True,\n    seed = SEED,\n    validate_filenames = False\n)\n\n\nval_ds = train_datagen.flow_from_dataframe(\n    train_df,\n    directory = TRAIN_PATH,\n    x_col = \"image\",\n    y_col = \"labels\",\n    target_size = (HEIGHT,WIDTH),\n    class_mode='categorical',\n    batch_size = BATCH_SIZE,\n    subset = \"validation\",\n    shuffle = True,\n    seed = SEED,\n    validate_filenames = False\n)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.150888Z","iopub.status.idle":"2022-07-23T00:05:49.151577Z","shell.execute_reply.started":"2022-07-23T00:05:49.151347Z","shell.execute_reply":"2022-07-23T00:05:49.151369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow import keras\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow.keras.models import Sequential, load_model, Model\nfrom tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.applications.inception_resnet_v2 import InceptionResNetV2, preprocess_input\nfrom keras.losses import categorical_crossentropy","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.156195Z","iopub.status.idle":"2022-07-23T00:05:49.156879Z","shell.execute_reply.started":"2022-07-23T00:05:49.156655Z","shell.execute_reply":"2022-07-23T00:05:49.156677Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential \nmodel = Sequential()\nmodel.add(Conv2D(32, kernel_size = (3, 3), activation = 'selu', input_shape=(HEIGHT,WIDTH,3)))\nmodel.add(MaxPooling2D(pool_size = (2, 2)))\nmodel.add(Conv2D(64, kernel_size = (3,3), activation = 'relu'))\nmodel.add(MaxPooling2D(pool_size = (2, 2)))\nmodel.add(Conv2D(128, kernel_size = (3,3), activation = 'selu'))\nmodel.add(Flatten())\nmodel.add(Dense(40, activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(20, activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(12, activation = 'softmax'))\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.160161Z","iopub.status.idle":"2022-07-23T00:05:49.162614Z","shell.execute_reply.started":"2022-07-23T00:05:49.162367Z","shell.execute_reply":"2022-07-23T00:05:49.162390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss=categorical_crossentropy,optimizer='adam',metrics=['accuracy'])\n#model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.164161Z","iopub.status.idle":"2022-07-23T00:05:49.165699Z","shell.execute_reply.started":"2022-07-23T00:05:49.165455Z","shell.execute_reply":"2022-07-23T00:05:49.165478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_history=model.fit(train_ds,validation_data=val_ds,steps_per_epoch=train_ds.samples//256,\n                                 validation_steps=val_ds.samples//256,\n                                 epochs=3)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.172753Z","iopub.status.idle":"2022-07-23T00:05:49.173455Z","shell.execute_reply.started":"2022-07-23T00:05:49.173204Z","shell.execute_reply":"2022-07-23T00:05:49.173225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dir = '/kaggle/input/plant-pathology-2021-fgvc8/test_images/'\ntest_df = pd.DataFrame()\ntest_df['image'] = os.listdir(test_dir)\nbatch=1\ntestgen = ImageDataGenerator(\n            preprocessing_function = tf.keras.applications.efficientnet.preprocess_input,\n            horizontal_flip=True,\n            vertical_flip=True,\n            brightness_range=(0.8, 1.2),\n            rescale=1/255.0)\ntest_set = testgen.flow_from_dataframe(dataframe=test_df,\n                                    directory=test_dir,\n                                    x_col=\"image\",\n                                    y_col=None,\n                                    batch=batch,\n                                    seed=42,\n                                    shuffle=False,\n                                    class_mode=None,\n                                    target_size=(256,256))","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.176757Z","iopub.status.idle":"2022-07-23T00:05:49.177464Z","shell.execute_reply.started":"2022-07-23T00:05:49.177191Z","shell.execute_reply":"2022-07-23T00:05:49.177212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set.reset()\narr=model.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.178701Z","iopub.status.idle":"2022-07-23T00:05:49.180001Z","shell.execute_reply.started":"2022-07-23T00:05:49.179129Z","shell.execute_reply":"2022-07-23T00:05:49.179150Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"xx=model.evaluate(val_ds)\nprint(xx)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.184860Z","iopub.status.idle":"2022-07-23T00:05:49.185582Z","shell.execute_reply.started":"2022-07-23T00:05:49.185349Z","shell.execute_reply":"2022-07-23T00:05:49.185371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_test = pd.read_csv(\"/kaggle/input/plant-pathology-2021-fgvc8/sample_submission.csv\")\ndata_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.188462Z","iopub.status.idle":"2022-07-23T00:05:49.189147Z","shell.execute_reply.started":"2022-07-23T00:05:49.188920Z","shell.execute_reply":"2022-07-23T00:05:49.188942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"min=0.0\nar=[]\nfor i in range(0,3):\n  min=arr[i,0]\n  \n  for j in range(0,3):\n    if(arr[i,j]>min):\n      min=arr[i,j]\n  \n  for j in range(0,3):\n    if(arr[i,j]==min):\n      ar.append(j) ","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.195116Z","iopub.status.idle":"2022-07-23T00:05:49.195804Z","shell.execute_reply.started":"2022-07-23T00:05:49.195576Z","shell.execute_reply":"2022-07-23T00:05:49.195598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"n_label = ['complex', 'frog_eye_leaf_spot', 'healthy', 'powdery_mildew', 'rust', 'scab','complex', 'k', 'healthy', 'powdery_mildew', 'rust', 'scab']\nte=[]\nfor i in range(0,3):\n    te.append(n_label[ar[i]])","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.196968Z","iopub.status.idle":"2022-07-23T00:05:49.198646Z","shell.execute_reply.started":"2022-07-23T00:05:49.198378Z","shell.execute_reply":"2022-07-23T00:05:49.198403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nsubmission = pd.read_csv(\"/kaggle/input/plant-pathology-2021-fgvc8//sample_submission.csv\")\nsubmission.loc[:,\"labels\"] = te\nsubmission","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.202407Z","iopub.status.idle":"2022-07-23T00:05:49.203527Z","shell.execute_reply.started":"2022-07-23T00:05:49.203180Z","shell.execute_reply":"2022-07-23T00:05:49.203206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=None)","metadata":{"execution":{"iopub.status.busy":"2022-07-23T00:05:49.208878Z","iopub.status.idle":"2022-07-23T00:05:49.210052Z","shell.execute_reply.started":"2022-07-23T00:05:49.209804Z","shell.execute_reply":"2022-07-23T00:05:49.209828Z"},"trusted":true},"execution_count":null,"outputs":[]}]}