{"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\n# import os\n# for 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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"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'] = train_df['chain'].astype(str) + '/' + train_df['image']\ntrain_df","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Filter Folder and Images to use in a new Dataframe\nmodelName='Equal90v2' #For Pickle File Name\ntrain_df_subset=train_df.loc[train_df['chain']==90]\ntrain_df_subset['hotel_id']=train_df_subset['hotel_id'].astype(str)\ncategory_count=train_df_subset['hotel_id'].nunique()\ntrain_df_subset.head(5)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n#Flow from Dataframe looks for image in filepath + x_col\nfilepath='../input/hotel-id-2021-fgvc8/train_images/'\ndatagen = ImageDataGenerator(validation_split=0.2, rescale=1./255)\n\ntraining=datagen.flow_from_dataframe(\n    dataframe=train_df_subset,\n    directory=filepath,\n    x_col=\"/chain/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=None,\n    class_mode=\"categorical\",\n    batch_size=32,\n    shuffle=True,\n    seed=None,\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=datagen.flow_from_dataframe(\n    dataframe=train_df_subset,\n    directory=filepath,\n    x_col=\"/chain/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=None,\n    class_mode=\"categorical\",\n    batch_size=32,\n    shuffle=True,\n    seed=None,\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":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\n\n\ntry:\n    model = tf.keras.models.load_model('./'+modelName)\n\nexcept:\n    model = tf.keras.applications.EfficientNetB0(\n        include_top=True,\n        weights=None,\n        input_tensor=None,\n        input_shape=None,\n        pooling=None,\n        classes=category_count,\n        classifier_activation=\"softmax\"\n    )\n    \n    model.compile(optimizer=\"adam\", loss=\"categorical_crossentropy\", metrics=[\"accuracy\"])\n    print('done model generation')","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":"model.fit(training)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.evaluate(testing)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save(modelName)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# f = open(\"listouput.txt\",mode='x')\n# f.write(hotelLabels)\n# f.close()\n\nimport pickle\n\nwith open('listOutput.pickel', 'x') as f:\n    pickle.dump(hotelLabels, f)","metadata":{"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]}]}