{"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 keras,os\nfrom keras.models import Sequential #so that all layers are arranged in sequence\nfrom keras.layers import Dense, Conv2D, MaxPool2D , Flatten\nfrom keras.preprocessing.image import ImageDataGenerator\nimport numpy as np","metadata":{"id":"wwAX6PRKRJM4","execution":{"iopub.status.busy":"2023-03-31T03:53:30.883311Z","iopub.execute_input":"2023-03-31T03:53:30.883768Z","iopub.status.idle":"2023-03-31T03:53:30.890079Z","shell.execute_reply.started":"2023-03-31T03:53:30.883726Z","shell.execute_reply":"2023-03-31T03:53:30.888672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# from PIL import Image\n\n# # set the path to your images folder\n# train_dir = \"/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images\"\n\n# datagen = ImageDataGenerator(validation_split=0.2)\n\n# train_generator = datagen.flow_from_directory(\n#     train_dir,\n#     target_size=(224, 224),\n#     batch_size=32,\n#     class_mode='categorical',\n#     subset='training')\n\n# val_generator = datagen.flow_from_directory(\n#     train_dir,\n#     target_size=(224, 224),\n#     batch_size=32,\n#     class_mode='categorical',\n#     subset='validation')\n\n\n","metadata":{"id":"tJbrxKhv9Zs8","outputId":"058f1782-2036-4267-f6c3-ca940d3d2797","execution":{"iopub.status.busy":"2023-03-14T19:06:14.120714Z","iopub.execute_input":"2023-03-14T19:06:14.121844Z","iopub.status.idle":"2023-03-14T19:06:32.480273Z","shell.execute_reply.started":"2023-03-14T19:06:14.121802Z","shell.execute_reply":"2023-03-14T19:06:32.479297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**CNN model**","metadata":{}},{"cell_type":"code","source":"# model = Sequential()\n# model.add(Conv2D(input_shape=(224,224,3),filters=64,kernel_size=(3,3),padding=\"same\", activation=\"relu\"))\n# model.add(Conv2D(filters=64,kernel_size=(3,3),padding=\"same\", activation=\"relu\"))\n# model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\n# model.add(Conv2D(filters=128, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# model.add(Conv2D(filters=128, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\n# model.add(Conv2D(filters=256, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# model.add(Conv2D(filters=256, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# model.add(Conv2D(filters=256, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\n# model.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# model.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# model.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\n# model.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# model.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# model.add(Conv2D(filters=512, kernel_size=(3,3), padding=\"same\", activation=\"relu\"))\n# model.add(MaxPool2D(pool_size=(2,2),strides=(2,2)))\n# #Add fully connected layers\n# model.add(Flatten())\n# model.add(Dense(units=4096,activation=\"relu\"))\n# model.add(Dense(units=4096,activation=\"relu\"))\n# model.add(Dense(units=3116, activation=\"softmax\"))\n# #Show the structure of the CNN\n# model.summary()","metadata":{"id":"diWSxxbJ_o9G","outputId":"be45210a-5211-41ed-c9ba-e40496c65fa4","execution":{"iopub.status.busy":"2023-03-14T19:06:37.891310Z","iopub.execute_input":"2023-03-14T19:06:37.891696Z","iopub.status.idle":"2023-03-14T19:06:40.729158Z","shell.execute_reply.started":"2023-03-14T19:06:37.891660Z","shell.execute_reply":"2023-03-14T19:06:40.727919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Optimization to reach global minima during training","metadata":{"id":"kxZ_qV4u_5l2"}},{"cell_type":"code","source":"# from keras.optimizers import Adam\n# opt = Adam(learning_rate=0.001)\n# model.compile(optimizer=opt, loss=keras.losses.categorical_crossentropy, metrics=['accuracy'])","metadata":{"id":"7F8ur7Re_qC7","execution":{"iopub.status.busy":"2023-03-14T19:06:51.963825Z","iopub.execute_input":"2023-03-14T19:06:51.964422Z","iopub.status.idle":"2023-03-14T19:06:51.976970Z","shell.execute_reply.started":"2023-03-14T19:06:51.964384Z","shell.execute_reply":"2023-03-14T19:06:51.975857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Monitoring validation accuracy\nThe model will only be saved to disk if the validation accuracy of the model in current epoch is greater than what it was in the last epoch.","metadata":{"id":"uJg1XSayAaY5"}},{"cell_type":"code","source":"# from keras.callbacks import ModelCheckpoint, EarlyStopping\n# checkpoint = ModelCheckpoint(\"vgg16_1.h5\", monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', save_freq=1)\n# #early = EarlyStopping(monitor='val_loss', min_delta=0, patience=20, verbose=1, mode='auto')\n# hist = model.fit(train_generator, epochs=100, steps_per_epoch=100, validation_data=val_generator, validation_steps=10)\n","metadata":{"id":"UOIa2PryAiCA","outputId":"aaaeb5f7-df04-42d2-89e6-e796636aa9cb","execution":{"iopub.status.busy":"2023-03-14T19:07:01.451281Z","iopub.execute_input":"2023-03-14T19:07:01.452004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Save the model**","metadata":{}},{"cell_type":"code","source":"# model.save(\"try_model.h5\")\n","metadata":{"execution":{"iopub.status.busy":"2023-03-13T23:09:36.484255Z","iopub.execute_input":"2023-03-13T23:09:36.484750Z","iopub.status.idle":"2023-03-13T23:09:41.661076Z","shell.execute_reply.started":"2023-03-13T23:09:36.484706Z","shell.execute_reply":"2023-03-13T23:09:41.657280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Saving as json file**","metadata":{}},{"cell_type":"code","source":"# from keras.models import model_from_json\n# # serialize model to JSON\n# model_json = model.to_json()\n# with open(\"model.json\", \"w\") as json_file:\n#     json_file.write(model_json)\n# # serialize weights to HDF5\n# model.save_weights(\"model.h5\")\n","metadata":{"execution":{"iopub.status.busy":"2023-03-13T23:50:56.274587Z","iopub.execute_input":"2023-03-13T23:50:56.275494Z","iopub.status.idle":"2023-03-13T23:50:56.287922Z","shell.execute_reply.started":"2023-03-13T23:50:56.275444Z","shell.execute_reply":"2023-03-13T23:50:56.285512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Load json file**","metadata":{}},{"cell_type":"code","source":"# # load json and create model\n# json_file = open('model.json', 'r')\n# loaded_model_json = json_file.read()\n# json_file.close()\n# loaded_model = model_from_json(loaded_model_json)\n# # load weights into new model\n# loaded_model.load_weights(\"model.h5\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Creating zip folder**","metadata":{}},{"cell_type":"code","source":"#!zip -r file.zip \"/kaggle/working/try_model.h5\"","metadata":{"execution":{"iopub.status.busy":"2023-03-13T23:20:20.405598Z","iopub.execute_input":"2023-03-13T23:20:20.406082Z","iopub.status.idle":"2023-03-13T23:22:17.298812Z","shell.execute_reply.started":"2023-03-13T23:20:20.406034Z","shell.execute_reply":"2023-03-13T23:22:17.296668Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Load the model**","metadata":{}},{"cell_type":"code","source":"# from tensorflow.keras.models import load_model\n# loaded_model = load_model(\"try_model.h5\")\n# loaded_model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-13T23:09:47.133561Z","iopub.execute_input":"2023-03-13T23:09:47.134777Z","iopub.status.idle":"2023-03-13T23:10:19.452961Z","shell.execute_reply.started":"2023-03-13T23:09:47.134726Z","shell.execute_reply":"2023-03-13T23:10:19.451190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Accuracy**","metadata":{}},{"cell_type":"code","source":"# scores = model.evaluate(val_generator)\n# print(\"\\n%s: %.2f%%\" % (model.metrics_names[1], scores[1]*100))\n# print(\"SUMMARY--\")\n# print(model.summary())","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualise training/validation accuracy and loss**","metadata":{}},{"cell_type":"code","source":"# import matplotlib.pyplot as plt\n# plt.plot(hist.history[\"accuracy\"])\n# plt.plot(hist.history['val_accuracy'])\n# plt.plot(hist.history['loss'])\n# plt.plot(hist.history['val_loss'])\n# plt.title(\"model accuracy\")\n# plt.ylabel(\"Accuracy\")\n# plt.xlabel(\"Epoch\")\n# plt.legend([\"Accuracy\",\"Validation Accuracy\",\"loss\",\"Validation Loss\"])\n# plt.show()","metadata":{"id":"-OhT5JgLAtoU","outputId":"1db80900-a3b7-4156-c3d4-44f0795a430f","execution":{"iopub.status.busy":"2023-03-13T23:18:58.912004Z","iopub.execute_input":"2023-03-13T23:18:58.912755Z","iopub.status.idle":"2023-03-13T23:18:59.254306Z","shell.execute_reply.started":"2023-03-13T23:18:58.912683Z","shell.execute_reply":"2023-03-13T23:18:59.252839Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Submission**","metadata":{}},{"cell_type":"code","source":"# import pandas as pd\n# from keras.preprocessing import image\n\n# # Load the model\n\n# model = keras.models.load_model('/kaggle/input/try-model-lucy/try_model.h5')\n\n# # Path to the test set images directory\n# test_dir = \"/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images\"\n\n# # Get a list of all the image filenames in the test set\n# test_images = os.listdir(test_dir)\n\n# # Create an empty DataFrame to store the predictions\n# predictions_df = pd.DataFrame(columns=['image', 'hotel_id'])\n\n# # Loop over each image in the test set and make a prediction\n# for img_filename in test_images:\n#     # Load the image\n#     img = image.load_img(os.path.join(test_dir, img_filename), target_size=(224, 224))\n#     # Convert the image to a numpy array\n#     img_arr = image.img_to_array(img)\n#     # Scale the image pixel values to between 0 and 1\n#     img_arr = img_arr / 255.\n#     # Add a batch dimension to the image\n#     img_arr = np.expand_dims(img_arr, axis=0)\n#     # Make a prediction on the image\n#     pred = model.predict(img_arr)\n#     # Get the top 5 predicted hotel IDs\n#     top5_hotel_ids = np.argsort(pred[0])[::-1][:5]\n#     # Create a string of the hotel IDs, separated by spaces\n#     hotel_ids_str = ' '.join([str(hotel_id) for hotel_id in top5_hotel_ids])\n#     # Add the prediction to the DataFrame\n#     predictions_df = predictions_df.append({'image': img_filename, 'hotel_id': hotel_ids_str}, ignore_index=True)\n\n# # Save the predictions to a CSV file\n# predictions_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T03:53:39.556720Z","iopub.execute_input":"2023-03-31T03:53:39.557130Z","iopub.status.idle":"2023-03-31T03:54:02.457391Z","shell.execute_reply.started":"2023-03-31T03:53:39.557097Z","shell.execute_reply":"2023-03-31T03:54:02.455762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nfrom tensorflow.keras.preprocessing import image\nfrom tensorflow import keras\n\n# Load the model\nmodel = keras.models.load_model('/kaggle/input/try-model-lucy/try_model.h5')\n\n# Path to the test set images directory\ntest_dir = \"/kaggle/input/hotel-id-to-combat-human-trafficking-2022-fgvc9/train_images\"\n\n# Create an empty DataFrame to store the predictions\npredictions_df = pd.DataFrame(columns=['image', 'hotel_id'])\n\n# Loop over each subdirectory in the test set and each image in the subdirectory\nfor sub_dir in os.listdir(test_dir):\n    sub_dir_path = os.path.join(test_dir, sub_dir)\n    for img_filename in os.listdir(sub_dir_path):\n        # Load the image\n        img_path = os.path.join(sub_dir_path, img_filename)\n        img = image.load_img(img_path, target_size=(224, 224))\n        # Convert the image to a numpy array\n        img_arr = image.img_to_array(img)\n        # Scale the image pixel values to between 0 and 1\n        img_arr = img_arr / 255.\n        # Add a batch dimension to the image\n        img_arr = np.expand_dims(img_arr, axis=0)\n        # Make a prediction on the image\n        pred = model.predict(img_arr)\n        # Get the top 5 predicted hotel IDs\n        top5_hotel_ids = np.argsort(pred[0])[::-1][:5]\n        # Create a string of the hotel IDs, separated by spaces\n        hotel_ids_str = ' '.join([str(hotel_id) for hotel_id in top5_hotel_ids])\n        # Add the prediction to the DataFrame\n        predictions_df = predictions_df.append({'image': os.path.join(sub_dir, img_filename), 'hotel_id': hotel_ids_str}, ignore_index=True)\n\n# Save the predictions to a CSV file\npredictions_df.to_csv('submission.csv', index=False)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T03:59:07.403474Z","iopub.execute_input":"2023-03-31T03:59:07.404805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}