{"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\n\"\"\"import 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        break\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-09-11T15:39:28.403331Z","iopub.execute_input":"2022-09-11T15:39:28.404702Z","iopub.status.idle":"2022-09-11T15:39:28.414181Z","shell.execute_reply.started":"2022-09-11T15:39:28.404660Z","shell.execute_reply":"2022-09-11T15:39:28.413069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport cv2 as cv\nimport os\nimport warnings\nwarnings.filterwarnings('ignore')\nimport tensorflow as tf\nfrom keras import regularizers\nfrom tensorflow.keras.layers import Dense,Conv2D,MaxPooling2D,Activation,Flatten,Dropout\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:41:11.899701Z","iopub.execute_input":"2022-09-11T15:41:11.900205Z","iopub.status.idle":"2022-09-11T15:41:19.382356Z","shell.execute_reply.started":"2022-09-11T15:41:11.900167Z","shell.execute_reply":"2022-09-11T15:41:19.381093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_path = '../input/plant-pathology-2021-fgvc8/train_images'\ntest_image_path = '../input/plant-pathology-2021-fgvc8/test_images'\ntrain_df_path = '../input/plant-pathology-2021-fgvc8/train.csv'\ntest_df_path = '../input/plant-pathology-2021-fgvc8/sample_submission.csv'","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:41:20.458642Z","iopub.execute_input":"2022-09-11T15:41:20.459608Z","iopub.status.idle":"2022-09-11T15:41:20.464980Z","shell.execute_reply.started":"2022-09-11T15:41:20.459569Z","shell.execute_reply":"2022-09-11T15:41:20.463821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainDF = pd.read_csv(train_df_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:41:36.407869Z","iopub.execute_input":"2022-09-11T15:41:36.408339Z","iopub.status.idle":"2022-09-11T15:41:36.445669Z","shell.execute_reply.started":"2022-09-11T15:41:36.408302Z","shell.execute_reply":"2022-09-11T15:41:36.444310Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testDF = pd.read_csv(test_df_path)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:41:42.648751Z","iopub.execute_input":"2022-09-11T15:41:42.649147Z","iopub.status.idle":"2022-09-11T15:41:42.660926Z","shell.execute_reply.started":"2022-09-11T15:41:42.649115Z","shell.execute_reply":"2022-09-11T15:41:42.659754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainDF.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:41:52.487137Z","iopub.execute_input":"2022-09-11T15:41:52.488189Z","iopub.status.idle":"2022-09-11T15:41:52.510124Z","shell.execute_reply.started":"2022-09-11T15:41:52.488144Z","shell.execute_reply":"2022-09-11T15:41:52.508980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testDF.head()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:42:00.044867Z","iopub.execute_input":"2022-09-11T15:42:00.045258Z","iopub.status.idle":"2022-09-11T15:42:00.055496Z","shell.execute_reply.started":"2022-09-11T15:42:00.045218Z","shell.execute_reply":"2022-09-11T15:42:00.054584Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainDF.labels.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:42:10.031892Z","iopub.execute_input":"2022-09-11T15:42:10.032307Z","iopub.status.idle":"2022-09-11T15:42:10.047943Z","shell.execute_reply.started":"2022-09-11T15:42:10.032274Z","shell.execute_reply":"2022-09-11T15:42:10.047087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"HEIGHT = 128\nWIDTH = 128\nBATCH_SIZE = 128\nINPUT_SIZE = (HEIGHT,WIDTH,3)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:42:18.525339Z","iopub.execute_input":"2022-09-11T15:42:18.525730Z","iopub.status.idle":"2022-09-11T15:42:18.530787Z","shell.execute_reply.started":"2022-09-11T15:42:18.525697Z","shell.execute_reply":"2022-09-11T15:42:18.529880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagenTrain = ImageDataGenerator(rescale=1/255.,\n                                  validation_split = 0.2,)\n\ndatagenTest = ImageDataGenerator(rescale=1/255.)","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:43:27.709191Z","iopub.execute_input":"2022-09-11T15:43:27.709636Z","iopub.status.idle":"2022-09-11T15:43:27.715576Z","shell.execute_reply.started":"2022-09-11T15:43:27.709573Z","shell.execute_reply":"2022-09-11T15:43:27.714489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator = datagenTrain.flow_from_dataframe(trainDF,\n                                   directory=train_image_path,\n                                   x_col = \"image\",\n                                   y_col = \"labels\",\n                                   class_mode = 'categorical',\n                                   batch_size = BATCH_SIZE,\n                                   target_size=(HEIGHT,WIDTH),\n                                   subset = \"training\",\n                                   shuffle = True, \n                                   seed = 42,                          \n                                   )","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:43:38.463575Z","iopub.execute_input":"2022-09-11T15:43:38.464209Z","iopub.status.idle":"2022-09-11T15:44:02.247308Z","shell.execute_reply.started":"2022-09-11T15:43:38.464166Z","shell.execute_reply":"2022-09-11T15:44:02.246255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"validation_generator = datagenTrain.flow_from_dataframe(trainDF,\n                                    directory = train_image_path,\n                                    x_col = \"image\",\n                                    y_col = \"labels\",\n                                    class_mode = 'categorical',\n                                    batch_size = BATCH_SIZE,\n                                    target_size = (HEIGHT,WIDTH),\n                                    subset = \"validation\",\n                                    shuffle = True,\n                                    seed=42,\n                                    )","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:44:02.248947Z","iopub.execute_input":"2022-09-11T15:44:02.249877Z","iopub.status.idle":"2022-09-11T15:44:09.549717Z","shell.execute_reply.started":"2022-09-11T15:44:02.249816Z","shell.execute_reply":"2022-09-11T15:44:09.548168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_generator = datagenTest.flow_from_dataframe(testDF,\n                                    directory = test_image_path,\n                                    x_col = \"image\",\n                                    y_col = None,\n                                    class_mode = None,\n                                    target_size = (HEIGHT,WIDTH),\n                                    shuffle=False\n                                    )","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:44:09.552021Z","iopub.execute_input":"2022-09-11T15:44:09.552497Z","iopub.status.idle":"2022-09-11T15:44:09.567437Z","shell.execute_reply.started":"2022-09-11T15:44:09.552449Z","shell.execute_reply":"2022-09-11T15:44:09.566436Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator.class_indices.items()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:44:14.653153Z","iopub.execute_input":"2022-09-11T15:44:14.653560Z","iopub.status.idle":"2022-09-11T15:44:14.660512Z","shell.execute_reply.started":"2022-09-11T15:44:14.653530Z","shell.execute_reply":"2022-09-11T15:44:14.659521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"model = Sequential()\nmodel.add(Conv2D(32,(3,3),activation=\"relu\",padding=\"same\",input_shape= INPUT_SIZE ))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Conv2D(64,(3,3),activation=\"relu\",padding=\"same\",kernel_regularizer=tf.contrib.layers.l2_regularizer(0.001)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(64,(3,3),activation=\"relu\",padding=\"same\",kernel_regularizer=tf.contrib.layers.l2_regularizer(0.001)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Conv2D(128,(3,3),activation=\"relu\",padding=\"same\",kernel_regularizer=tf.contrib.layers.l2_regularizer(0.001)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(128,(3,3),activation=\"relu\",padding=\"same\",kernel_regularizer=tf.contrib.layers.l2_regularizer(0.001)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Conv2D(256,(3,3),activation=\"relu\",padding=\"same\",kernel_regularizer=tf.contrib.layers.l2_regularizer(0.001)))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(256,(3,3),activation=\"relu\",padding=\"same\",kernel_regularizer=tf.contrib.layers.l2_regularizer(0.001)))\nmodel.add(BatchNormalization())\nmodel.add(MaxPooling2D(2,2))\nmodel.add(Flatten())\nmodel.add(Dense(12, activation=\"softmax\"))\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:23:53.241716Z","iopub.execute_input":"2022-09-05T10:23:53.242098Z","iopub.status.idle":"2022-09-05T10:23:53.295195Z","shell.execute_reply.started":"2022-09-05T10:23:53.242067Z","shell.execute_reply":"2022-09-05T10:23:53.294339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"model.compile(loss=\"categorical_crossentropy\",\n             optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),\n             metrics=[\"accuracy\"])\n\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-09-11T15:45:09.608026Z","iopub.execute_input":"2022-09-11T15:45:09.608475Z","iopub.status.idle":"2022-09-11T15:45:09.614753Z","shell.execute_reply.started":"2022-09-11T15:45:09.608437Z","shell.execute_reply":"2022-09-11T15:45:09.613535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"checkpoint_loss = tf.keras.callbacks.ModelCheckpoint(filepath=\"./Plant_Pathalogy_model.h5\",\n                                                 monitor=\"val_loss\",\n                                                 verbose=1,\n                                                 save_best_only=True,\n                                                 mode=\"min\")\nearly_stopping = tf.keras.callbacks.EarlyStopping(monitor=\"val_loss\",\n                                                 min_delta=0.0,\n                                                 patience=5,\n                                                 verbose=1)\n\"\"\"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"history = model.fit_generator(train_generator,\n                             validation_data=validation_generator,\n                             epochs=10,\n                             steps_per_epoch=train_generator.samples//128,\n                             validation_steps=validation_generator.samples//128,\n                             callbacks=[checkpoint_loss,early_stopping])\n\"\"\"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"import keras\nfrom matplotlib import pyplot as plt\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model Loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()\n\"\"\"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"import keras\nfrom matplotlib import pyplot as plt\nplt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()\n\"\"\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nmodelP = tf.keras.models.load_model(\"../input/models/Plant_Pathalogy_model.h5\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = modelP.predict(test_generator)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions","metadata":{"execution":{"iopub.status.busy":"2022-09-05T10:53:34.985759Z","iopub.execute_input":"2022-09-05T10:53:34.986176Z","iopub.status.idle":"2022-09-05T10:53:34.993852Z","shell.execute_reply.started":"2022-09-05T10:53:34.986143Z","shell.execute_reply":"2022-09-05T10:53:34.992811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices = np.argmax(predictions,axis=1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"indices","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_generator.class_indices.items()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def labelled_value(label):\n    for key,value in train_generator.class_indices.items():\n        if value==label:\n            return key","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(indices)):\n    testDF['labels'][i] = labelled_value(indices[i])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testDF.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testDF.to_csv('./submission.csv',index=False)","metadata":{},"execution_count":null,"outputs":[]}]}