{"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","scrolled":true,"_kg_hide-output":true,"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-10-26T09:21:05.690189Z","iopub.execute_input":"2022-10-26T09:21:05.690689Z","iopub.status.idle":"2022-10-26T09:21:05.766706Z","shell.execute_reply.started":"2022-10-26T09:21:05.690657Z","shell.execute_reply":"2022-10-26T09:21:05.765786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## 1. Importing libraries and image dataset","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport random\nimport os\nimport pathlib\nfrom PIL import ImageFile\n\nimport tensorflow as tf\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout, Rescaling\nfrom keras.models import Sequential","metadata":{"execution":{"iopub.status.busy":"2022-10-26T09:59:24.859464Z","iopub.execute_input":"2022-10-26T09:59:24.859831Z","iopub.status.idle":"2022-10-26T09:59:24.870639Z","shell.execute_reply.started":"2022-10-26T09:59:24.859801Z","shell.execute_reply":"2022-10-26T09:59:24.869492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = \"../input/finding-disney\"\ntrain_dir = \"../input/finding-disney/train/\"\ntest_dir = \"../input/finding-disney/test/\"","metadata":{"execution":{"iopub.status.busy":"2022-10-26T09:32:36.617675Z","iopub.execute_input":"2022-10-26T09:32:36.618064Z","iopub.status.idle":"2022-10-26T09:32:36.623151Z","shell.execute_reply.started":"2022-10-26T09:32:36.618015Z","shell.execute_reply":"2022-10-26T09:32:36.621893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(os.listdir(train_dir)), len(os.listdir(test_dir))","metadata":{"execution":{"iopub.status.busy":"2022-10-26T09:32:40.274816Z","iopub.execute_input":"2022-10-26T09:32:40.275168Z","iopub.status.idle":"2022-10-26T09:32:40.288460Z","shell.execute_reply.started":"2022-10-26T09:32:40.275140Z","shell.execute_reply":"2022-10-26T09:32:40.287344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = sorted(os.listdir(train_dir))\nclasses","metadata":{"execution":{"iopub.status.busy":"2022-10-26T09:32:41.788320Z","iopub.execute_input":"2022-10-26T09:32:41.789037Z","iopub.status.idle":"2022-10-26T09:32:41.797922Z","shell.execute_reply.started":"2022-10-26T09:32:41.789001Z","shell.execute_reply":"2022-10-26T09:32:41.796811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for dirpath, dirnames, filenames in os.walk(root_dir):\n    print(f\"There are {len(dirnames)} directories and {len(filenames)} images in '{dirpath}'.\")","metadata":{"execution":{"iopub.status.busy":"2022-10-26T09:32:45.471900Z","iopub.execute_input":"2022-10-26T09:32:45.472292Z","iopub.status.idle":"2022-10-26T09:32:45.498864Z","shell.execute_reply.started":"2022-10-26T09:32:45.472257Z","shell.execute_reply":"2022-10-26T09:32:45.497926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View random images in the dataset\ndef view_random_images(train=True):\n    random_images = []\n    labels = []\n    for i in range(50):\n        random_class = random.choice(classes)\n        if train:\n            random_class_path = root_dir + \"/train/\" + random_class\n            labels.append(random_class)\n        else:\n            random_class_path = root_dir + \"/test/\" + random_class\n        random_image = random.sample(os.listdir(random_class_path), 1)\n        random_images.append(random_class_path + '/' + random_image[0])\n\n    plt.figure(figsize=(15, 10))\n    plt.suptitle(\"Image Dataset\", fontsize=20)\n\n    for i in range(1, 51):\n        plt.subplot(5, 10, i)\n        img = mpimg.imread(random_images[i-1])\n        plt.imshow(img, aspect=\"auto\")\n        if train:\n            plt.title(labels[i-1])\n        plt.axis(False);","metadata":{"execution":{"iopub.status.busy":"2022-10-26T09:32:54.794357Z","iopub.execute_input":"2022-10-26T09:32:54.795368Z","iopub.status.idle":"2022-10-26T09:32:54.804148Z","shell.execute_reply.started":"2022-10-26T09:32:54.795331Z","shell.execute_reply":"2022-10-26T09:32:54.803013Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Observing the images\nview_random_images()","metadata":{"execution":{"iopub.status.busy":"2022-10-26T09:32:58.320506Z","iopub.execute_input":"2022-10-26T09:32:58.320860Z","iopub.status.idle":"2022-10-26T09:33:06.558069Z","shell.execute_reply.started":"2022-10-26T09:32:58.320832Z","shell.execute_reply":"2022-10-26T09:33:06.555333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Observing the images\nview_random_images()","metadata":{"execution":{"iopub.status.busy":"2022-10-26T09:33:06.560157Z","iopub.execute_input":"2022-10-26T09:33:06.560810Z","iopub.status.idle":"2022-10-26T09:33:15.028126Z","shell.execute_reply.started":"2022-10-26T09:33:06.560776Z","shell.execute_reply":"2022-10-26T09:33:15.027326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Declaring global variables\nIMG_SIZE = (224, 224)\nBATCH_SIZE = 32\nEPOCHS = 100","metadata":{"execution":{"iopub.status.busy":"2022-10-26T10:32:10.523343Z","iopub.execute_input":"2022-10-26T10:32:10.523721Z","iopub.status.idle":"2022-10-26T10:32:10.528980Z","shell.execute_reply.started":"2022-10-26T10:32:10.523690Z","shell.execute_reply":"2022-10-26T10:32:10.527816Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datagen = ImageDataGenerator(rescale=1./255,\n                                   shear_range=0.2,\n                                   zoom_range=0.2,\n                                   validation_split=0.20,\n                                   horizontal_flip=True)\n\ntest_datagen = ImageDataGenerator(rescale=1./255)\n\ntrain_data = train_datagen.flow_from_directory(train_dir,\n                                            target_size=IMG_SIZE,\n                                            batch_size=BATCH_SIZE,\n                                            seed=42,\n                                            subset = 'training',\n                                            shuffle=True,\n                                            class_mode='categorical')\n\nvalidation_data = train_datagen.flow_from_directory(train_dir,\n                                                target_size=IMG_SIZE,\n                                                batch_size=BATCH_SIZE,\n                                                seed=42,\n                                                subset = 'validation',\n                                                shuffle=True,\n                                                class_mode='categorical')","metadata":{"execution":{"iopub.status.busy":"2022-10-26T10:32:14.406799Z","iopub.execute_input":"2022-10-26T10:32:14.407156Z","iopub.status.idle":"2022-10-26T10:32:14.730285Z","shell.execute_reply.started":"2022-10-26T10:32:14.407126Z","shell.execute_reply":"2022-10-26T10:32:14.729127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# AUTOTUNE = tf.data.AUTOTUNE\n# train_data = train_data.cache().shuffle(1000).prefetch(buffer_size = AUTOTUNE)\n# validation_data = validation_data.prefetch(buffer_size = AUTOTUNE)\n# test_data = test_data.prefetch(buffer_size = AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2022-10-26T08:12:22.474031Z","iopub.execute_input":"2022-10-26T08:12:22.474394Z","iopub.status.idle":"2022-10-26T08:12:22.479046Z","shell.execute_reply.started":"2022-10-26T08:12:22.474364Z","shell.execute_reply":"2022-10-26T08:12:22.477983Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**MODEL_:1 CNN with Data Augmentation**","metadata":{}},{"cell_type":"code","source":"IMAGE_SHAPE = IMG_SIZE + (3, )\ninputs = tf.keras.Input(shape = IMAGE_SHAPE)\nx = Rescaling(1./255)(inputs)\nx = Conv2D(filters = 4, kernel_size =(3, 3), padding = 'same', strides = (1, 1), activation='relu')(x)\nx = MaxPool2D(pool_size = (2, 2), strides = 2)(x)\nx = Flatten()(x)\nx = Dense(256, activation='relu')(x)\nx = Dense(len(classes), activation = 'softmax')(x)\nmodel_1 = tf.keras.Model(inputs = inputs, outputs = x)","metadata":{"execution":{"iopub.status.busy":"2022-10-26T11:05:09.268266Z","iopub.execute_input":"2022-10-26T11:05:09.268876Z","iopub.status.idle":"2022-10-26T11:05:09.323315Z","shell.execute_reply.started":"2022-10-26T11:05:09.268837Z","shell.execute_reply":"2022-10-26T11:05:09.322494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1.summary()","metadata":{"execution":{"iopub.status.busy":"2022-10-26T11:05:11.035128Z","iopub.execute_input":"2022-10-26T11:05:11.035500Z","iopub.status.idle":"2022-10-26T11:05:11.041835Z","shell.execute_reply.started":"2022-10-26T11:05:11.035466Z","shell.execute_reply":"2022-10-26T11:05:11.040778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1.compile(optimizer = tf.keras.optimizers.Adam(0.0001), \n                loss = 'categorical_crossentropy',\n                metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-10-26T11:06:42.319804Z","iopub.execute_input":"2022-10-26T11:06:42.320230Z","iopub.status.idle":"2022-10-26T11:06:42.333052Z","shell.execute_reply.started":"2022-10-26T11:06:42.320185Z","shell.execute_reply":"2022-10-26T11:06:42.332099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ImageFile.LOAD_TRUNCATED_IMAGES = True\nhistory_1 = model_1.fit_generator(train_data,\n                                validation_data = validation_data,\n                                steps_per_epoch = int(train_data.n//train_data.batch_size),\n                                validation_steps = int(validation_data.n//validation_data.batch_size),\n                                epochs=EPOCHS)","metadata":{"execution":{"iopub.status.busy":"2022-10-26T11:06:44.760290Z","iopub.execute_input":"2022-10-26T11:06:44.761001Z","iopub.status.idle":"2022-10-26T14:29:38.897883Z","shell.execute_reply.started":"2022-10-26T11:06:44.760964Z","shell.execute_reply":"2022-10-26T14:29:38.896884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1.save('model_1.h5')","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:33:17.307290Z","iopub.execute_input":"2022-10-26T14:33:17.308020Z","iopub.status.idle":"2022-10-26T14:33:17.528116Z","shell.execute_reply.started":"2022-10-26T14:33:17.307974Z","shell.execute_reply":"2022-10-26T14:33:17.527116Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def plot_results(history):\n    plt.figure(figsize=(10, 5))\n    \n    plt.subplot(1, 2, 1)\n    plt.plot(history.history['accuracy'])\n    plt.plot(history.history['val_accuracy'])\n    plt.title('model accuracy')\n    plt.ylabel('accuracy')\n    plt.xlabel('epochs')\n    plt.legend(['train', 'validation'], loc='upper left')\n    \n    plt.subplot(1, 2, 2)\n    plt.plot(history.history['loss'])\n    plt.plot(history.history['val_loss'])\n    plt.title('model loss')\n    plt.ylabel('loss')\n    plt.xlabel('epochs')\n    plt.legend(['train', 'validation'], loc='upper left')\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:34:23.588583Z","iopub.execute_input":"2022-10-26T14:34:23.588953Z","iopub.status.idle":"2022-10-26T14:34:23.596699Z","shell.execute_reply.started":"2022-10-26T14:34:23.588919Z","shell.execute_reply":"2022-10-26T14:34:23.595626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plot_results(history_1)","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:34:24.385114Z","iopub.execute_input":"2022-10-26T14:34:24.387289Z","iopub.status.idle":"2022-10-26T14:34:24.704326Z","shell.execute_reply.started":"2022-10-26T14:34:24.387254Z","shell.execute_reply":"2022-10-26T14:34:24.703380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(\"./\")","metadata":{"execution":{"iopub.status.busy":"2022-10-26T14:35:01.259756Z","iopub.execute_input":"2022-10-26T14:35:01.260178Z","iopub.status.idle":"2022-10-26T14:35:01.271161Z","shell.execute_reply.started":"2022-10-26T14:35:01.260144Z","shell.execute_reply":"2022-10-26T14:35:01.270146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_1.save('model_1.h5')","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}