{"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 os\nimport sklearn\nimport cv2\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow import keras\nimport keras.callbacks as callbacks\nimport matplotlib.pyplot as plt\nimport tensorflow.keras.backend as K\nimport pandas as pd\n# Display\nfrom IPython.display import Image, display\nimport matplotlib.cm as cm\nimport shutil\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.metrics import plot_confusion_matrix\nfrom sklearn.metrics import confusion_matrix, classification_report\nimport seaborn as sn\nimport pydicom as dicom\nimport subprocess\nimport gc\n\nsize = 224","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-06T18:20:09.169284Z","iopub.execute_input":"2022-08-06T18:20:09.169579Z","iopub.status.idle":"2022-08-06T18:20:09.177563Z","shell.execute_reply.started":"2022-08-06T18:20:09.169549Z","shell.execute_reply":"2022-08-06T18:20:09.175388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset:\n    def __init__(self):\n        self.x_path = \"../input/plant-seedlings-classification/train\"\n        self.classes = os.listdir(self.x_path)\n        self.x_data = []\n        self.y_data = []\n        \n    def __preprocessImage(self, image):\n        image = np.array(image, dtype = \"float32\")\n        image = cv2.resize(image, (size, size))\n        image = np.reshape(image, (size, size, 3)) \n        image /= 255.0\n        \n        return np.array(image, dtype = \"float32\")\n    \n    def __extractImages(self, path):\n        images = os.listdir(path)\n        for image_path in images:\n            image = cv2.imread(path + \"/\" + image_path)\n            self.x_data.append(self.__preprocessImage(image))\n            self.y_data.append(self.classes.index(path.split(\"/\")[-1]))\n            \n    def load_data(self):\n        i = 0\n        for index in self.classes:\n            i += 1\n            self.__extractImages(self.x_path + \"/\" + index)\n            print(f\"{(i / len(self.classes)) * 100}% Data Loaded, Index = {i}\", end = \"\\r\")\n        \n        #self.x_data = np.reshape(self.x_data, (np.shape(self.x_data)[0], 1, size, size, 3))\n        self.x_data = np.array(self.x_data, dtype = \"float32\")\n        self.y_data = to_categorical(self.y_data)\n        self.y_data = np.array(self.y_data, dtype = \"float32\")\n        \n        print(\"Data Successfully Loaded\")\n        return train_test_split(self.x_data, self.y_data, random_state = 42)\n    \ndataset = Dataset()\nx_train, x_test, y_train, y_test = dataset.load_data()\ny_train = tf.constant(y_train)\ny_test = tf.constant(y_test)\n\ndel dataset\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T18:20:09.179504Z","iopub.execute_input":"2022-08-06T18:20:09.180018Z","iopub.status.idle":"2022-08-06T18:21:51.422836Z","shell.execute_reply.started":"2022-08-06T18:20:09.179959Z","shell.execute_reply":"2022-08-06T18:21:51.421928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train.shape)\nprint(x_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-06T18:21:51.424121Z","iopub.execute_input":"2022-08-06T18:21:51.424455Z","iopub.status.idle":"2022-08-06T18:21:51.432109Z","shell.execute_reply.started":"2022-08-06T18:21:51.424420Z","shell.execute_reply":"2022-08-06T18:21:51.430874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\n\ndef ImageGenerator():\n    return ImageDataGenerator(\n        rotation_range=20,\n        zoom_range=0.15,\n        width_shift_range=0.2,\n        height_shift_range=0.2,\n        shear_range=0.15,\n        horizontal_flip=True,\n        fill_mode=\"nearest\")","metadata":{"execution":{"iopub.status.busy":"2022-08-06T18:21:51.433440Z","iopub.execute_input":"2022-08-06T18:21:51.433981Z","iopub.status.idle":"2022-08-06T18:21:51.441749Z","shell.execute_reply.started":"2022-08-06T18:21:51.433945Z","shell.execute_reply":"2022-08-06T18:21:51.440906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CNNLSTM:\n    def __init__(self, shape, cnn):\n        self.shape = shape\n        self.model_input = keras.layers.Input(shape = (self.shape[0], self.shape[1], self.shape[2]), ragged = True, dtype = 'float32')\n        self.cnn = cnn(weights = \"imagenet\", include_top = False, input_tensor = self.model_input)\n        for layer in self.cnn.layers[: len(self.cnn.layers) // 2 ]:\n            layer.trainable = True\n        for layer in self.cnn.layers[len(self.cnn.layers) // 2 :]:\n            layer.trainable = False\n        self.model = 0\n        \n        self.save_path = \"./cnnlstm.h5\"\n        self.__build_model()\n\n    def __build_model(self):\n        \n        #print(self.cnn.output.shape)\n        x = keras.layers.Flatten()(self.cnn.output)\n        x = keras.layers.Dense(1024, activation =\"selu\", kernel_initializer = \"lecun_normal\")(x)\n        x = keras.layers.Dropout(0.1)(x)\n        x = keras.layers.Dense(512, activation = \"selu\", kernel_initializer = \"lecun_normal\")(x)\n        x = keras.layers.Dropout(0.1)(x)\n        x = keras.layers.Dense(256, activation = \"selu\", kernel_initializer = \"lecun_normal\")(x)\n        x = keras.layers.Dropout(0.1)(x)\n        x = keras.layers.Dense(128, activation = \"selu\", kernel_initializer = \"lecun_normal\")(x)\n        x = keras.layers.Dropout(0.1)(x)\n        x = keras.layers.Dense(64, activation =  \"selu\", kernel_initializer = \"lecun_normal\")(x)\n        x = keras.layers.Dropout(0.1)(x)\n        x = keras.layers.Dense(32, activation =  \"selu\", kernel_initializer = \"lecun_normal\")(x)\n        x = keras.layers.Dropout(0.1)(x)\n        \n        output = keras.layers.Dense(12, activation = \"softmax\")(x)\n        \n        self.model = keras.models.Model(inputs = self.model_input,\n                                  outputs = output, name = \"CNNLSTM\")\n        #display(keras.utils.plot_model(self.model, show_shapes=True, show_layer_names=True))\n        \n    def plot_loss_acc(self):\n        #Loss vs Epochs\n        plt.plot(self.model.history.history[\"loss\"], label='train')\n        plt.plot(self.model.history.history[\"val_loss\"], label='test')\n        plt.xlabel(\"Epochs\")\n        plt.ylabel(\"Loss\")\n        plt.title(f\"{self.name} Loss vs Epochs\")\n        plt.legend()\n        plt.show()\n        #Accuracy vs Epochs\n        plt.plot(self.model.history.history['accuracy'], label='train')\n        plt.plot(self.model.history.history['val_accuracy'], label='test')\n        plt.xlabel(\"Epochs\")\n        plt.ylabel(\"Accuracy\")\n        plt.title(f\"{self.name} ACCURACY vs Epochs\")\n        plt.legend()\n        plt.show()\n        \n    def scoring(self):\n        y_test_0 = [np.argmax(i) for i in y_test]\n        y_pred_0 = [np.argmax(i) for i in self.model.predict(x_test)]\n        cm = confusion_matrix(y_test_0, y_pred_0)\n        print(classification_report(y_test_0,y_pred_0))\n        df_cm = pd.DataFrame(cm)\n        plt.figure(figsize = (10,7))\n        sn.heatmap(df_cm, annot=True)\n        \n    def compile_and_run(self, lr = 1e-4, epochs = 100):\n        optimizer = keras.optimizers.Adam(learning_rate = lr)\n        checkpoint = keras.callbacks.ModelCheckpoint(self.save_path, save_best_only = True)\n        self.model.compile(loss=\"categorical_crossentropy\", optimizer = optimizer, metrics = [\"accuracy\"])\n        self.model.fit(x = ImageGenerator().flow(x_train,y_train), epochs = epochs,\n                       validation_data = (x_test, y_test),shuffle=True, workers = -1,\n                       batch_size = 32, callbacks = [checkpoint])\n        self.scoring()\n        #self.plot_loss_acc()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T20:50:41.788794Z","iopub.execute_input":"2022-08-06T20:50:41.789132Z","iopub.status.idle":"2022-08-06T20:50:41.805797Z","shell.execute_reply.started":"2022-08-06T20:50:41.789100Z","shell.execute_reply":"2022-08-06T20:50:41.804866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnnlstm = CNNLSTM((size, size, 3), keras.applications.resnet.ResNet101)\ncnnlstm.compile_and_run()","metadata":{"execution":{"iopub.status.busy":"2022-08-06T18:21:51.464184Z","iopub.execute_input":"2022-08-06T18:21:51.464541Z","iopub.status.idle":"2022-08-06T20:16:45.063886Z","shell.execute_reply.started":"2022-08-06T18:21:51.464506Z","shell.execute_reply":"2022-08-06T20:16:45.062922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def __preprocessImage(image):\n    image = np.array(image, dtype = \"float32\")\n    image = cv2.resize(image, (size, size))\n    image = np.reshape(image, (size, size, 3)) \n    image /= 255.0\n    \n    return np.array(image, dtype = \"float32\")\n\nx_data_test = []\n    \ndef __extractImages(path):\n    global x_data_test\n    image = cv2.imread(path)\n    x_data_test.append(__preprocessImage(image))\n\nx_path = \"../input/plant-seedlings-classification/test\"\nimages = os.listdir(x_path)\n\nfor image in images:\n    __extractImages(x_path + \"/\" + image)\nx_data_test = np.array(x_data_test, dtype = \"float32\")","metadata":{"execution":{"iopub.status.busy":"2022-08-06T20:16:45.065955Z","iopub.execute_input":"2022-08-06T20:16:45.066487Z","iopub.status.idle":"2022-08-06T20:16:54.947575Z","shell.execute_reply.started":"2022-08-06T20:16:45.066448Z","shell.execute_reply":"2022-08-06T20:16:54.946697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = os.listdir(\"../input/plant-seedlings-classification/train\")\ncnnlstm.model = keras.models.load_model(\"./cnnlstm.h5\")\npredictions = cnnlstm.model.predict(x_data_test)\ndf = pd.DataFrame()\ndf['file'] = images\ndf['species'] = [classes[np.argmax(pred)] for pred in predictions]\ndf.index = df['file']\ndf = df.drop('file', axis = 1)\ndf.to_csv(\"./submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-08-06T20:16:54.948972Z","iopub.execute_input":"2022-08-06T20:16:54.949313Z","iopub.status.idle":"2022-08-06T20:17:06.176085Z","shell.execute_reply.started":"2022-08-06T20:16:54.949278Z","shell.execute_reply":"2022-08-06T20:17:06.175217Z"},"trusted":true},"execution_count":null,"outputs":[]}]}