{"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\nimport cv2\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(filename)\nimport warnings\nwarnings.filterwarnings('ignore')\nos.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' \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-11-10T23:09:46.035497Z","iopub.execute_input":"2022-11-10T23:09:46.036070Z","iopub.status.idle":"2022-11-10T23:09:46.287233Z","shell.execute_reply.started":"2022-11-10T23:09:46.035933Z","shell.execute_reply":"2022-11-10T23:09:46.286336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport tensorflow as tf\nimport tensorflow_datasets as tfds\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\n\nbatch_size = 32\nimg_height = 180\nimg_width = 180","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:09:46.289423Z","iopub.execute_input":"2022-11-10T23:09:46.289817Z","iopub.status.idle":"2022-11-10T23:09:53.752161Z","shell.execute_reply.started":"2022-11-10T23:09:46.289780Z","shell.execute_reply":"2022-11-10T23:09:53.751117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#setting the path of directories\nimport pathlib\ndata_dir = '/kaggle/input/finding-disney/train'\ndata_dir = pathlib.Path(data_dir)\ntest_dir = '/kaggle/input/finding-disney/test'\ntest_dir = pathlib.Path(test_dir)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:09:53.753971Z","iopub.execute_input":"2022-11-10T23:09:53.754705Z","iopub.status.idle":"2022-11-10T23:09:53.764536Z","shell.execute_reply.started":"2022-11-10T23:09:53.754667Z","shell.execute_reply":"2022-11-10T23:09:53.762682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Data Cleaning**","metadata":{}},{"cell_type":"code","source":"#new directories to save cleaned data\nos.mkdir('/kaggle/working/train')\nos.mkdir('/kaggle/working/train/donald')\nos.mkdir('/kaggle/working/train/mickey')\nos.mkdir('/kaggle/working/train/minion')\nos.mkdir('/kaggle/working/train/olaf')\nos.mkdir('/kaggle/working/train/pooh')\nos.mkdir('/kaggle/working/train/pumba')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:09:53.768751Z","iopub.execute_input":"2022-11-10T23:09:53.769669Z","iopub.status.idle":"2022-11-10T23:09:53.791333Z","shell.execute_reply.started":"2022-11-10T23:09:53.769623Z","shell.execute_reply":"2022-11-10T23:09:53.790424Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#function to clean data and save it in new directory\ndef clean_data(char_name):\n    for dirname, _, filenames in os.walk(f\"{data_dir}/{char_name}\"):\n        for filename in filenames:\n            img2 = cv2.imread(os.path.join(dirname, filename))\n            if img2 is not None:\n                img2 = cv2.resize(img2,(img_height,img_width))\n                cv2.imwrite(f\"/kaggle/working/train/{char_name}/{filename}\",img2)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:09:53.792622Z","iopub.execute_input":"2022-11-10T23:09:53.793018Z","iopub.status.idle":"2022-11-10T23:09:53.802080Z","shell.execute_reply.started":"2022-11-10T23:09:53.792983Z","shell.execute_reply":"2022-11-10T23:09:53.801146Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clean_data('mickey')\nclean_data('pooh')\nclean_data('olaf')\nclean_data('minion')\nclean_data('pumba')\nclean_data('donald')","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:09:53.804381Z","iopub.execute_input":"2022-11-10T23:09:53.805024Z","iopub.status.idle":"2022-11-10T23:11:02.877117Z","shell.execute_reply.started":"2022-11-10T23:09:53.804989Z","shell.execute_reply":"2022-11-10T23:11:02.876068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#defining new data directories\nclean_data_dir = '/kaggle/working/train'\nclean_data_dir = pathlib.Path(clean_data_dir)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:11:02.878740Z","iopub.execute_input":"2022-11-10T23:11:02.880063Z","iopub.status.idle":"2022-11-10T23:11:02.885303Z","shell.execute_reply.started":"2022-11-10T23:11:02.880005Z","shell.execute_reply":"2022-11-10T23:11:02.884092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_ds = tf.keras.utils.image_dataset_from_directory(\n#   clean_data_dir,\n#   seed=123,\n#   image_size=(img_height, img_width),\n#   batch_size=batch_size)\n# train_ds.take(1)","metadata":{"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-11-10T23:11:02.886746Z","iopub.execute_input":"2022-11-10T23:11:02.887809Z","iopub.status.idle":"2022-11-10T23:11:02.896020Z","shell.execute_reply.started":"2022-11-10T23:11:02.887775Z","shell.execute_reply":"2022-11-10T23:11:02.895237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Creating Datasets**","metadata":{}},{"cell_type":"code","source":"#making training dataset with 80% random train samples\ntrain_ds = tf.keras.utils.image_dataset_from_directory(\n  clean_data_dir,\n  validation_split=0.2,\n  subset=\"training\",\n  seed=123,\n  image_size=(img_height, img_width),\n  batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:11:02.897457Z","iopub.execute_input":"2022-11-10T23:11:02.898159Z","iopub.status.idle":"2022-11-10T23:11:07.508141Z","shell.execute_reply.started":"2022-11-10T23:11:02.898120Z","shell.execute_reply":"2022-11-10T23:11:07.507077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#making validation dataset with 20% remaining samples\nval_ds = tf.keras.utils.image_dataset_from_directory(\n  clean_data_dir,\n  validation_split=0.2,\n  subset=\"validation\",\n  seed=123,\n  image_size=(img_height, img_width),\n  batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:11:07.513819Z","iopub.execute_input":"2022-11-10T23:11:07.514166Z","iopub.status.idle":"2022-11-10T23:11:07.642680Z","shell.execute_reply.started":"2022-11-10T23:11:07.514136Z","shell.execute_reply":"2022-11-10T23:11:07.641622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nprint(class_names)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:11:07.643880Z","iopub.execute_input":"2022-11-10T23:11:07.644240Z","iopub.status.idle":"2022-11-10T23:11:07.651363Z","shell.execute_reply.started":"2022-11-10T23:11:07.644206Z","shell.execute_reply":"2022-11-10T23:11:07.650266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import cv2\n# def check_images( s_dir, ext_list):\n#     bad_images=[]\n#     bad_ext=[]\n#     s_list= os.listdir(s_dir)\n#     for klass in s_list:\n#         klass_path=os.path.join (s_dir, klass)\n#         print ('processing class directory ', klass)\n#         if os.path.isdir(klass_path):\n#             file_list=os.listdir(klass_path)\n#             for f in file_list:               \n#                 f_path=os.path.join (klass_path,f)\n#                 index=f.rfind('.')\n#                 ext=f[index+1:].lower()\n#                 if ext not in ext_list:\n#                     print('file ', f_path, ' has an invalid extension ', ext)\n#                     bad_ext.append(f_path)\n#                 if os.path.isfile(f_path):\n#                     try:\n#                         img=cv2.imread(f_path)\n#                         shape=img.shape\n#                     except:\n#                         print('file ', f_path, ' is not a valid image file')\n#                         bad_images.append(f_path)\n#                 else:\n#                     print('*** fatal error, you a sub directory ', f, ' in class directory ', klass)\n#         else:\n#             print ('*** WARNING*** you have files in ', s_dir, ' it should only contain sub directories')\n#     return bad_images, bad_ext\n\n# good_exts=['jpg', 'png', 'jpeg', 'gif', 'bmp' ] # list of acceptable extensions\n# bad_file_list, bad_ext_list=check_images(clean_data_dir, good_exts)\n# if len(bad_file_list) !=0:\n#     print('improper image files are listed below')\n#     for i in range (len(bad_file_list)):\n#         print (bad_file_list[i])\n# else:\n#     print(' no improper image files were found')","metadata":{"_kg_hide-input":true,"jupyter":{"source_hidden":true},"execution":{"iopub.status.busy":"2022-11-10T23:11:07.653212Z","iopub.execute_input":"2022-11-10T23:11:07.653959Z","iopub.status.idle":"2022-11-10T23:11:07.662398Z","shell.execute_reply.started":"2022-11-10T23:11:07.653922Z","shell.execute_reply":"2022-11-10T23:11:07.661381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Some Images in dataset","metadata":{}},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.figure(figsize=(10, 10))\nfor images, labels in train_ds.take(1):\n  for i in range(9):\n    ax = plt.subplot(3, 3, i + 1)\n    plt.imshow(images[i].numpy().astype(\"uint8\"))\n    plt.title(class_names[labels[i]])\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:11:07.664177Z","iopub.execute_input":"2022-11-10T23:11:07.664544Z","iopub.status.idle":"2022-11-10T23:11:08.558908Z","shell.execute_reply.started":"2022-11-10T23:11:07.664509Z","shell.execute_reply":"2022-11-10T23:11:08.558044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model tuning techniques","metadata":{}},{"cell_type":"code","source":"AUTOTUNE = tf.data.AUTOTUNE\n\ntrain_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE) ","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:11:08.559960Z","iopub.execute_input":"2022-11-10T23:11:08.562381Z","iopub.status.idle":"2022-11-10T23:11:08.568597Z","shell.execute_reply.started":"2022-11-10T23:11:08.562327Z","shell.execute_reply":"2022-11-10T23:11:08.567685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_augmentation = keras.Sequential(\n  [\n    layers.RandomFlip(\"horizontal\",\n                      input_shape=(img_height,\n                                  img_width,\n                                  3)),\n    layers.RandomRotation(0.2),\n    layers.RandomZoom(0.1),\n    layers.RandomContrast(0.1),\n      \n  ]\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:11:08.570253Z","iopub.execute_input":"2022-11-10T23:11:08.571213Z","iopub.status.idle":"2022-11-10T23:11:08.742890Z","shell.execute_reply.started":"2022-11-10T23:11:08.571177Z","shell.execute_reply":"2022-11-10T23:11:08.741931Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#plotting data augmented images\nplt.figure(figsize=(10, 10))\nfor images, _ in train_ds.take(1):\n  for i in range(9):\n    augmented_images = data_augmentation(images)\n    ax = plt.subplot(3, 3, i + 1)\n    plt.imshow(augmented_images[0].numpy().astype(\"uint8\"))\n    plt.axis(\"off\")","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:11:08.744185Z","iopub.execute_input":"2022-11-10T23:11:08.745231Z","iopub.status.idle":"2022-11-10T23:11:09.756905Z","shell.execute_reply.started":"2022-11-10T23:11:08.745192Z","shell.execute_reply":"2022-11-10T23:11:09.756071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Neural Network**","metadata":{}},{"cell_type":"code","source":"num_classes = 6\n\nmodel = Sequential([\n  data_augmentation,\n  layers.Rescaling(1./255),\n  layers.Conv2D(70, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(80, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(110, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(145, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(175, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(210, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Dropout(0.3),\n  layers.Flatten(),\n  layers.Dense(200, activation='relu'),\n  layers.Dropout(0.2),\n  layers.Dense(160, activation='relu'),\n  layers.Dropout(0.2),\n  layers.Dense(135, activation='relu'),\n  layers.Dropout(0.2),\n  layers.Dense(105, activation='relu'),\n  layers.Dropout(0.2),\n  layers.Dense(79, activation='relu'),\n  layers.Dropout(0.2),\n  layers.Dense(59, activation='relu'),\n  layers.Dropout(0.2),\n  layers.Dense(36, activation='relu'),\n  layers.Dropout(0.2),\n  layers.Dense(num_classes, name=\"outputs\")\n])","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:11:09.758523Z","iopub.execute_input":"2022-11-10T23:11:09.759252Z","iopub.status.idle":"2022-11-10T23:11:10.054081Z","shell.execute_reply.started":"2022-11-10T23:11:09.759201Z","shell.execute_reply":"2022-11-10T23:11:10.053088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Model Training**","metadata":{}},{"cell_type":"code","source":"model.compile(\n  optimizer='adam',\n  loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n  metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:11:10.055651Z","iopub.execute_input":"2022-11-10T23:11:10.056019Z","iopub.status.idle":"2022-11-10T23:11:10.069836Z","shell.execute_reply.started":"2022-11-10T23:11:10.055984Z","shell.execute_reply":"2022-11-10T23:11:10.068909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=250\nhistory = model.fit(\n  train_ds,\n  validation_data=val_ds,\n  epochs=epochs\n)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:11:10.071635Z","iopub.execute_input":"2022-11-10T23:11:10.071974Z","iopub.status.idle":"2022-11-10T23:45:27.773088Z","shell.execute_reply.started":"2022-11-10T23:11:10.071941Z","shell.execute_reply":"2022-11-10T23:45:27.772057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:45:27.774745Z","iopub.execute_input":"2022-11-10T23:45:27.775245Z","iopub.status.idle":"2022-11-10T23:45:27.785492Z","shell.execute_reply.started":"2022-11-10T23:45:27.775206Z","shell.execute_reply":"2022-11-10T23:45:27.784060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training vs Validation Graph","metadata":{}},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(epochs)\n\nplt.figure(figsize=(8, 8))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:45:27.787246Z","iopub.execute_input":"2022-11-10T23:45:27.788363Z","iopub.status.idle":"2022-11-10T23:45:28.123477Z","shell.execute_reply.started":"2022-11-10T23:45:27.788320Z","shell.execute_reply":"2022-11-10T23:45:28.122597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Predicting output for test data","metadata":{}},{"cell_type":"code","source":"df = pd.DataFrame(columns = ['character_id', 'label'])","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:45:28.124714Z","iopub.execute_input":"2022-11-10T23:45:28.126301Z","iopub.status.idle":"2022-11-10T23:45:28.140682Z","shell.execute_reply.started":"2022-11-10T23:45:28.126255Z","shell.execute_reply":"2022-11-10T23:45:28.139649Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from pathlib import Path\nfor dirname, _, filenames in os.walk(test_dir):\n    for filename in filenames:\n        img = tf.keras.utils.load_img(os.path.join(dirname, filename),\n                                      target_size=(img_height, img_width))\n        img_array = tf.keras.utils.img_to_array(img)\n        img_array = tf.expand_dims(img_array, 0) # Create a batch\n\n        predictions = model.predict(img_array)\n        score = tf.nn.softmax(predictions[0])\n\n#         print(int(Path(filename).stem))\n#             \"This image most likely belongs to {} with a {:.2f} percent confidence.\"\n#             .format(class_names[np.argmax(score)], 100 * np.max(score))\n#         )\n        df = df.append({'character_id':int(Path(filename).stem),'label': class_names[np.argmax(score)]},ignore_index = True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:45:28.142184Z","iopub.execute_input":"2022-11-10T23:45:28.142666Z","iopub.status.idle":"2022-11-10T23:45:55.417824Z","shell.execute_reply.started":"2022-11-10T23:45:28.142626Z","shell.execute_reply":"2022-11-10T23:45:55.416836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:45:55.419365Z","iopub.execute_input":"2022-11-10T23:45:55.419718Z","iopub.status.idle":"2022-11-10T23:45:55.437724Z","shell.execute_reply.started":"2022-11-10T23:45:55.419678Z","shell.execute_reply":"2022-11-10T23:45:55.436411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_csv('/kaggle/working/submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T23:45:55.439061Z","iopub.execute_input":"2022-11-10T23:45:55.439481Z","iopub.status.idle":"2022-11-10T23:45:55.448376Z","shell.execute_reply.started":"2022-11-10T23:45:55.439446Z","shell.execute_reply":"2022-11-10T23:45:55.447460Z"},"trusted":true},"execution_count":null,"outputs":[]}]}