{"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 all necessary libraries\nimport numpy as np \nimport pandas as pd\nimport os\nimport tensorflow as tf\nfrom tensorflow.keras.layers import Dense, Dropout\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\n# from kaggle_datasets import KaggleDatasets\nfrom tensorflow.keras.applications import EfficientNetB0\nfrom tensorflow.keras import models\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('../input/hotel-id-2021-fgvc8/train.csv')\ntrain_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['chain_image'] = '../input/hotel-id-2021-fgvc8/train_images/'+train_df['chain'].astype(str) + '/' + train_df['image']\ntrain_df\ntrain_df['hotel_id']=train_df['hotel_id'].astype(str)\ntrain_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df_subset = train_df.loc[train_df['chain']==90]\ntrain_df_subset['hotel_id'] = train_df_subset['hotel_id'].astype(str)\ntrain_df_subset","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # detect and init the TPU\n# tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\n\n# # instantiate a distribution strategy\n# tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)\n\n# DIR = KaggleDatasets().get_gcs_path()\n# Train_PATH = DIR + \"/train_images/90\"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classes = train_df_subset.hotel_id.unique()\nclasses = list(classes)\nlen(classes)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"filepath='../input/hotel-id-2021-fgvc8/train_images/90'\n\ntrain_datgen = ImageDataGenerator(rescale = 1./255,\n                           shear_range=0.2,\n                           zoom_range=0.2,\n                           horizontal_flip=True,\n                           rotation_range=0.1,\n                           validation_split=0.1)\n\n\ntraining= train_datgen.flow_from_dataframe(\n    dataframe=train_df_subset,\n    directory=filepath,\n    x_col=\"image\",\n    y_col=\"hotel_id\",\n    weight_col=None,\n    #target_size=(256, 256),\n    target_size=(224, 224),\n    color_mode=\"rgb\",\n    classes=classes,\n    class_mode=\"categorical\",\n    batch_size=32,\n    shuffle=True,\n    seed=11,\n    save_to_dir=None,\n    save_prefix=\"\",\n    save_format=\"png\",\n    subset='training',\n    interpolation=\"nearest\",\n    validate_filenames=True\n)\n\ntesting = train_datgen.flow_from_dataframe(\n    dataframe=train_df_subset,\n    directory=filepath,\n    x_col=\"image\",\n    y_col=\"hotel_id\",\n    weight_col=None,\n    #target_size=(256, 256),\n    target_size=(224, 224),\n    color_mode=\"rgb\",\n    classes=classes,\n    class_mode=\"categorical\",\n    batch_size=32,\n    shuffle=True,\n    seed=11,\n    save_to_dir=None,\n    save_prefix=\"\",\n    save_format=\"png\",\n    subset='validation',\n    interpolation=\"nearest\",\n    validate_filenames=True\n)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training.class_indices","metadata":{"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testing.class_indices","metadata":{"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testing.class_indices==training.class_indices","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nEffNet = EfficientNetB0(include_top=False,\n                             weights='imagenet',\n                             input_tensor=None,\n                             input_shape=([224, 224, 3]),\n                             pooling='avg'\n                       )\n\nmodel_EffNet = models.Sequential()\nmodel_EffNet.add(EffNet)\nmodel_EffNet.add(Dropout(0.5))\nmodel_EffNet.add(Dense(270, activation='softmax'))\n\n\nmodel_EffNet.compile(optimizer=\"adam\", loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])\n\nearly_stop = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=3)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_EffNet.fit(training, callbacks=[early_stop], epochs=1, validation_data=testing)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(model_EffNet.predict(testing), axis=-1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"code_test=model_EffNet.predict(testing)\ncode_test[:5]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"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(testing.class_indices.keys())\nval_list = list(testing.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=(224, 224), \n            interpolation=\"nearest\"\n        )\n\n        submission_arr = tf.keras.preprocessing.image.img_to_array(submission)\n        submission_arr = np.array([submission_arr])  # Convert single image to a batch.\n        predictions = model_EffNet.predict(submission_arr)\n        return replace2DwthHotelID(listOfTop5(predictions))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_filepath='../input/hotel-id-2021-fgvc8/test_images/99e91ad5f2870678.jpg'\nsingle_image_prediction(sample_filepath)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_EffNet.predict(submission)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_EffNet.save('model_EffNetv2.h5')","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}