{"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":"markdown","source":"### Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport os\nfrom re import search\nimport shutil\nfrom PIL import Image\nimport matplotlib.pyplot as plt\nfrom tqdm import tqdm\nimport cv2\n\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.callbacks import ModelCheckpoint,EarlyStopping\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.layers import Dense,Activation,Flatten, Conv2D, MaxPooling2D\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:37:05.566956Z","iopub.execute_input":"2021-05-20T16:37:05.567392Z","iopub.status.idle":"2021-05-20T16:37:07.613695Z","shell.execute_reply.started":"2021-05-20T16:37:05.567321Z","shell.execute_reply":"2021-05-20T16:37:07.611645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#IMAGE PATH & DATAFRAME:\n\nTRAIN_PATH = \"../input/plant-pathology-2021-fgvc8/train_images\"\ntrain_df = pd.read_csv(\"../input/plant-pathology-2021-fgvc8/train.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:37:07.618656Z","iopub.execute_input":"2021-05-20T16:37:07.619051Z","iopub.status.idle":"2021-05-20T16:37:07.650309Z","shell.execute_reply.started":"2021-05-20T16:37:07.619009Z","shell.execute_reply":"2021-05-20T16:37:07.6494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"count_dict = train_df.labels.value_counts()\nclasses = list(count_dict.index)\nclasses_count = list(count_dict.values)\nprint(\"Number of unique labels: \",len(classes))\nprint(\"-------------------------------------------\")\ntrain_df.head()\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:37:08.419171Z","iopub.execute_input":"2021-05-20T16:37:08.41962Z","iopub.status.idle":"2021-05-20T16:37:08.445879Z","shell.execute_reply.started":"2021-05-20T16:37:08.419582Z","shell.execute_reply":"2021-05-20T16:37:08.445019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(35,15))\nplt.bar(classes,classes_count)\nplt.title(\"Number of instances per class\",fontweight=\"bold\",fontsize=40)\nplt.xlabel(\"Classes\",fontsize = 30)\nplt.xticks(rotation=20,fontsize = 20,fontweight = \"bold\")\nplt.xticks(fontsize = 20,fontweight = \"bold\")\nplt.ylabel(\"Count\",fontsize=30)\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:37:21.300943Z","iopub.execute_input":"2021-05-20T16:37:21.30131Z","iopub.status.idle":"2021-05-20T16:37:21.711311Z","shell.execute_reply.started":"2021-05-20T16:37:21.301278Z","shell.execute_reply":"2021-05-20T16:37:21.710536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Use the Image Data Generator to import the images from the dataset\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n\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 = 124\nWIDTH=124\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)\n","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:37:42.324209Z","iopub.execute_input":"2021-05-20T16:37:42.324829Z","iopub.status.idle":"2021-05-20T16:37:42.464881Z","shell.execute_reply.started":"2021-05-20T16:37:42.324762Z","shell.execute_reply":"2021-05-20T16:37:42.463585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:39:33.137516Z","iopub.execute_input":"2021-05-20T16:39:33.137872Z","iopub.status.idle":"2021-05-20T16:39:34.08561Z","shell.execute_reply.started":"2021-05-20T16:39:33.137843Z","shell.execute_reply":"2021-05-20T16:39:34.084734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2021-05-20T16:39:37.289748Z","iopub.execute_input":"2021-05-20T16:39:37.29014Z","iopub.status.idle":"2021-05-20T16:39:37.454868Z","shell.execute_reply.started":"2021-05-20T16:39:37.290105Z","shell.execute_reply":"2021-05-20T16:39:37.453682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2021-05-20T16:42:03.276152Z","iopub.execute_input":"2021-05-20T16:42:03.276726Z","iopub.status.idle":"2021-05-20T16:42:03.722661Z","shell.execute_reply.started":"2021-05-20T16:42:03.276691Z","shell.execute_reply":"2021-05-20T16:42:03.721569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"3a94ec9c45f58e7bc62bfeee6c2cdf06d7d92d92","scrolled":true,"execution":{"iopub.status.busy":"2021-05-20T16:52:39.101445Z","iopub.execute_input":"2021-05-20T16:52:39.101852Z","iopub.status.idle":"2021-05-20T16:52:39.13229Z","shell.execute_reply.started":"2021-05-20T16:52:39.101816Z","shell.execute_reply":"2021-05-20T16:52:39.131165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}