{"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/hotel-id-to-combat-human-trafficking-2022-fgvc9/test_images/'):\n    for filename in filenames:\n        print(\"filename: \", filename)\n#         print(\"middle::\", _)\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-04-24T14:44:27.877459Z","iopub.execute_input":"2022-04-24T14:44:27.878647Z","iopub.status.idle":"2022-04-24T14:44:27.936127Z","shell.execute_reply.started":"2022-04-24T14:44:27.87851Z","shell.execute_reply":"2022-04-24T14:44:27.935029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd \nimport os\nimport PIL\nimport tensorflow as tf\n\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\nimport tensorflow_datasets as tfds","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:44:27.938298Z","iopub.execute_input":"2022-04-24T14:44:27.943243Z","iopub.status.idle":"2022-04-24T14:44:35.737012Z","shell.execute_reply.started":"2022-04-24T14:44:27.943193Z","shell.execute_reply":"2022-04-24T14:44:35.736178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pathlib\n# dataset_url = \"/kaggle/\"\n# data_dir = tf.keras.utils.get_file('../input/hotel-id-to-combat-human-trafficking-2022-fgvc9', untar=True)\n# data_dir = pathlib.Path(data_dir)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:44:35.739105Z","iopub.execute_input":"2022-04-24T14:44:35.739428Z","iopub.status.idle":"2022-04-24T14:44:35.748619Z","shell.execute_reply.started":"2022-04-24T14:44:35.739371Z","shell.execute_reply":"2022-04-24T14:44:35.747485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_height = 512\nimg_width = 512\nbatch_size = 128","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:44:35.750194Z","iopub.execute_input":"2022-04-24T14:44:35.750736Z","iopub.status.idle":"2022-04-24T14:44:35.774409Z","shell.execute_reply.started":"2022-04-24T14:44:35.75069Z","shell.execute_reply":"2022-04-24T14:44:35.773423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = tf.keras.utils.image_dataset_from_directory(\n    '../input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images/', \n    validation_split=0.2, subset='training', \n    seed=100,\n    image_size=(img_height, img_width)\n    #batch_size=batch_size\n)\nval_ds = tf.keras.utils.image_dataset_from_directory(\n    '../input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images/', \n    validation_split=0.2, \n    subset='validation', \n    seed=100,\n    image_size=(img_height, img_width)\n    #batch_size=batch_size\n)\n# test_ds = tfds.folder_dataset.ImageFolder('../input/hotel-id-to-combat-human-trafficking-2022-fgvc9/test_images/')\n# train_mask =  tf.keras.utils.image_dataset_from_directory('../input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_masks/')","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:44:35.776444Z","iopub.execute_input":"2022-04-24T14:44:35.777023Z","iopub.status.idle":"2022-04-24T14:44:57.789721Z","shell.execute_reply.started":"2022-04-24T14:44:35.776978Z","shell.execute_reply":"2022-04-24T14:44:57.788669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class_names = train_ds.class_names\nprint(len(class_names))","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:44:57.791578Z","iopub.execute_input":"2022-04-24T14:44:57.79213Z","iopub.status.idle":"2022-04-24T14:44:57.79985Z","shell.execute_reply.started":"2022-04-24T14:44:57.792051Z","shell.execute_reply":"2022-04-24T14:44:57.798448Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = len(class_names)\n\nmodel = Sequential([\n  layers.Rescaling(1./255, input_shape=(img_height, img_width, 3)),\n  layers.Conv2D(16, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(32, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(64, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Flatten(),\n  layers.Dense(512, activation='relu'),\n  layers.Dense(num_classes)\n])","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:44:57.802477Z","iopub.execute_input":"2022-04-24T14:44:57.80345Z","iopub.status.idle":"2022-04-24T14:44:57.939806Z","shell.execute_reply.started":"2022-04-24T14:44:57.803397Z","shell.execute_reply":"2022-04-24T14:44:57.93881Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:44:57.941611Z","iopub.execute_input":"2022-04-24T14:44:57.941903Z","iopub.status.idle":"2022-04-24T14:44:57.96292Z","shell.execute_reply.started":"2022-04-24T14:44:57.941859Z","shell.execute_reply":"2022-04-24T14:44:57.961944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:44:57.964823Z","iopub.execute_input":"2022-04-24T14:44:57.965172Z","iopub.status.idle":"2022-04-24T14:44:57.982833Z","shell.execute_reply.started":"2022-04-24T14:44:57.965125Z","shell.execute_reply":"2022-04-24T14:44:57.981022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=3\nhistory = model.fit(\n  train_ds,\n  validation_data=val_ds,\n  epochs=epochs,\n  #batch_size = batch_size\n  steps_per_epoch=32\n)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:44:57.984849Z","iopub.execute_input":"2022-04-24T14:44:57.985215Z","iopub.status.idle":"2022-04-24T14:57:20.542135Z","shell.execute_reply.started":"2022-04-24T14:44:57.985169Z","shell.execute_reply":"2022-04-24T14:57:20.540897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-04-24T14:57:20.548926Z","iopub.execute_input":"2022-04-24T14:57:20.549411Z","iopub.status.idle":"2022-04-24T14:57:21.014019Z","shell.execute_reply.started":"2022-04-24T14:57:20.549353Z","shell.execute_reply":"2022-04-24T14:57:21.012929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# code_test=model.predict(test_ds)\n# test_ds.class_indices\n# train_ds.class_names\n\nclass_indices = {k: v for v, k in enumerate(train_ds.class_names)}\n    \n# print(\"class_indices::\", class_indices)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:57:21.072449Z","iopub.execute_input":"2022-04-24T14:57:21.073803Z","iopub.status.idle":"2022-04-24T14:57:21.082007Z","shell.execute_reply.started":"2022-04-24T14:57:21.073755Z","shell.execute_reply":"2022-04-24T14:57:21.080802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#create test ds\nimport os\ntest_ds_path = []\nfor dirname, _, filenames in os.walk('/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/test_images/'):\n    path_dict = {}\n    for filename in filenames:\n        \n#         print(os.path.join(dirname, filename))\n        path_dict[filename] = os.path.join(dirname, filename)\n    test_ds_path.append(path_dict)\n# print(\"test_ds_path::\", test_ds_path)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:57:21.084669Z","iopub.execute_input":"2022-04-24T14:57:21.085721Z","iopub.status.idle":"2022-04-24T14:57:21.101875Z","shell.execute_reply.started":"2022-04-24T14:57:21.085671Z","shell.execute_reply":"2022-04-24T14:57:21.100449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"raw","source":"test_ds_path","metadata":{}},{"cell_type":"code","source":"def get_top5(oneD_list):\n    return np.argpartition(oneD_list,-5)[-5:]\n\ndef listOfTop5(twoD_list):\n    return [get_top5(row) for row in twoD_list]\n\nkey_list = list(class_indices.keys())\nval_list = list(class_indices.values())\n\ndef replaceWithHotelID(oneRow):\n    return [key_list[i] for i in oneRow]\n\ndef replace2DwthHotelID(twoDArray):\n    return [replaceWithHotelID(row) for row in twoDArray]\n\n#Single Image Prediction Code\ndef single_image_prediction(submission_filepath):\n    \n        submission = tf.keras.preprocessing.image.load_img(\n            submission_filepath, \n            grayscale=False, \n            color_mode=\"rgb\", \n            target_size=(512, 512), \n            interpolation=\"nearest\"\n        )\n\n        submission_arr = tf.keras.preprocessing.image.img_to_array(submission)\n#         print(\"submission_arr::\",submission_arr.shape)\n        submission_arr = np.array([submission_arr])  # Convert single image to a batch.\n        predictions = model.predict(submission_arr)\n        print(\"predictions::\", predictions.shape)\n        return replace2DwthHotelID(listOfTop5(predictions))","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:57:21.105302Z","iopub.execute_input":"2022-04-24T14:57:21.106476Z","iopub.status.idle":"2022-04-24T14:57:21.122909Z","shell.execute_reply.started":"2022-04-24T14:57:21.106425Z","shell.execute_reply":"2022-04-24T14:57:21.121563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# single_image_prediction('../input/hotel-id-to-combat-human-trafficking-2022-fgvc9/test_images/abc.jpg')\n\nprediction_list = []\n\nfor path_dict in test_ds_path:\n    prediction_dict = {}\n    for image, path in path_dict.items():\n        prediction_dict[\"image_id\"] = image\n#         prediction_dict[\"hotel_id\"] = ''.join(single_image_prediction(path)[0])\n        prediction_dict[\"hotel_id\"] = single_image_prediction(path)[0][0]\n    prediction_list.append(prediction_dict)\n#         print(\"image::\", path)\n#     prediction_list['']\nprint(\"prediction_list::\",  prediction_list)\n\nprediction_df = pd.DataFrame(prediction_list)","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:57:21.12651Z","iopub.execute_input":"2022-04-24T14:57:21.128153Z","iopub.status.idle":"2022-04-24T14:57:21.808518Z","shell.execute_reply.started":"2022-04-24T14:57:21.128085Z","shell.execute_reply":"2022-04-24T14:57:21.806787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction_df.to_csv('submission.csv', index=False)\n# prediction_df","metadata":{"execution":{"iopub.status.busy":"2022-04-24T14:57:21.815959Z","iopub.execute_input":"2022-04-24T14:57:21.817032Z","iopub.status.idle":"2022-04-24T14:57:21.840768Z","shell.execute_reply.started":"2022-04-24T14:57:21.816958Z","shell.execute_reply":"2022-04-24T14:57:21.839408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}