{"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 tensorflow as tf\nimport pandas as pd\nimport numpy as np\nimport os\nimport matplotlib.pyplot as plt\nimport warnings\nwarnings.filterwarnings(\"ignore\")\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nAUTOTUNE = tf.data.experimental.AUTOTUNE","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-28T06:36:13.656926Z","iopub.execute_input":"2022-02-28T06:36:13.657276Z","iopub.status.idle":"2022-02-28T06:36:18.839764Z","shell.execute_reply.started":"2022-02-28T06:36:13.657187Z","shell.execute_reply":"2022-02-28T06:36:18.838992Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = {\n    0: \"buildings\",\n    1: \"forest\",\n    2: \"glacier\",\n    3: \"mountain\",\n    4: \"sea\",\n    5: \"street\"\n}","metadata":{"execution":{"iopub.status.busy":"2022-02-28T06:36:29.466578Z","iopub.execute_input":"2022-02-28T06:36:29.466851Z","iopub.status.idle":"2022-02-28T06:36:29.47231Z","shell.execute_reply.started":"2022-02-28T06:36:29.466823Z","shell.execute_reply":"2022-02-28T06:36:29.471384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_path = \"../input/ml-olympiad-landscape-image-classification/train\"","metadata":{"execution":{"iopub.status.busy":"2022-02-27T18:57:00.978351Z","iopub.execute_input":"2022-02-27T18:57:00.978678Z","iopub.status.idle":"2022-02-27T18:57:00.988305Z","shell.execute_reply.started":"2022-02-27T18:57:00.978641Z","shell.execute_reply":"2022-02-27T18:57:00.987638Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_datagen = ImageDataGenerator(\n#         rescale = 1/255.0,\n#         rotation_range=20,\n#         zoom_range=0.05,\n#         width_shift_range=0.05,\n#         height_shift_range=0.05,\n#         shear_range=0.05,\n#         horizontal_flip=True,\n#         fill_mode=\"nearest\",\n#         validation_split=0.20)\n\n# # test_datagen = ImageDataGenerator(rescale = 1/255.0)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T18:57:00.991902Z","iopub.execute_input":"2022-02-27T18:57:00.992101Z","iopub.status.idle":"2022-02-27T18:57:00.997377Z","shell.execute_reply.started":"2022-02-27T18:57:00.992078Z","shell.execute_reply":"2022-02-27T18:57:00.996576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_generator = train_datagen.flow_from_directory(\n#     directory=train_path,\n#     target_size=(256, 256),\n#     color_mode=\"rgb\",\n#     batch_size=32,\n#     class_mode=\"categorical\",\n#     subset='training',\n#     shuffle=True,\n#     seed=42\n# )\n\n# val_generator = train_datagen.flow_from_directory(\n#     directory=train_path,\n#     target_size=(100, 100),\n#     color_mode=\"rgb\",\n#     batch_size=32,\n#     class_mode=\"categorical\",\n#     subset='validation',\n#     shuffle=True,\n#     seed=42\n# )","metadata":{"execution":{"iopub.status.busy":"2022-02-27T18:57:00.998588Z","iopub.execute_input":"2022-02-27T18:57:00.999069Z","iopub.status.idle":"2022-02-27T18:57:01.004807Z","shell.execute_reply.started":"2022-02-27T18:57:00.999025Z","shell.execute_reply":"2022-02-27T18:57:01.004099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = tf.keras.preprocessing.image_dataset_from_directory(\n    directory='../input/ml-olympiad-landscape-image-classification/train',\n    labels='inferred',\n    label_mode='categorical',\n    batch_size=64,\n    image_size=(256, 256),\n    validation_split=0.2,\n    subset=\"training\",\n    seed=1024\n)\n\nval_ds = tf.keras.preprocessing.image_dataset_from_directory(\n    directory='../input/ml-olympiad-landscape-image-classification/train',\n    labels='inferred',\n    label_mode='categorical',\n    batch_size=64,\n    image_size=(256, 256),\n    validation_split=0.2,\n    subset=\"validation\",\n    seed=1024\n)","metadata":{"execution":{"iopub.status.busy":"2022-02-28T06:37:09.456854Z","iopub.execute_input":"2022-02-28T06:37:09.457156Z","iopub.status.idle":"2022-02-28T06:37:31.340255Z","shell.execute_reply.started":"2022-02-28T06:37:09.457128Z","shell.execute_reply":"2022-02-28T06:37:31.339675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.applications import EfficientNetB7","metadata":{"execution":{"iopub.status.busy":"2022-02-28T06:37:31.341409Z","iopub.execute_input":"2022-02-28T06:37:31.341942Z","iopub.status.idle":"2022-02-28T06:37:31.345967Z","shell.execute_reply.started":"2022-02-28T06:37:31.341913Z","shell.execute_reply":"2022-02-28T06:37:31.345227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model = EfficientNetB7(\n    input_shape=(256,256,3),\n    include_top=False,\n    weights=\"imagenet\")","metadata":{"execution":{"iopub.status.busy":"2022-02-28T06:37:31.347575Z","iopub.execute_input":"2022-02-28T06:37:31.348033Z","iopub.status.idle":"2022-02-28T06:37:41.436493Z","shell.execute_reply.started":"2022-02-28T06:37:31.347984Z","shell.execute_reply":"2022-02-28T06:37:41.435503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_model.trainable = True","metadata":{"execution":{"iopub.status.busy":"2022-02-28T06:37:41.438845Z","iopub.execute_input":"2022-02-28T06:37:41.439092Z","iopub.status.idle":"2022-02-28T06:37:41.469914Z","shell.execute_reply.started":"2022-02-28T06:37:41.439065Z","shell.execute_reply":"2022-02-28T06:37:41.469098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for layer in base_model.layers[:400]:\n  layer.trainable =  False","metadata":{"execution":{"iopub.status.busy":"2022-02-28T06:37:41.471073Z","iopub.execute_input":"2022-02-28T06:37:41.47168Z","iopub.status.idle":"2022-02-28T06:37:41.489544Z","shell.execute_reply.started":"2022-02-28T06:37:41.471639Z","shell.execute_reply":"2022-02-28T06:37:41.488597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_model():\n    model = tf.keras.Sequential([base_model,\n    layers.RandomFlip(\"horizontal_and_vertical\"),\n    layers.RandomRotation(0.2),\n    layers.GlobalAveragePooling2D(),\n    layers.Dense(64, activation=\"elu\"),\n    layers.Dense(32, activation=\"elu\"),\n    layers.Dense(16, activation=\"elu\"),\n    layers.Dense(6, activation=\"softmax\")], name='convnet')\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2022-02-28T06:44:35.877185Z","iopub.execute_input":"2022-02-28T06:44:35.877614Z","iopub.status.idle":"2022-02-28T06:44:35.883439Z","shell.execute_reply.started":"2022-02-28T06:44:35.877584Z","shell.execute_reply":"2022-02-28T06:44:35.882871Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = create_model()\n\noptimizer = tf.keras.optimizers.Adam()","metadata":{"execution":{"iopub.status.busy":"2022-02-28T06:44:47.221868Z","iopub.execute_input":"2022-02-28T06:44:47.222644Z","iopub.status.idle":"2022-02-28T06:44:47.265066Z","shell.execute_reply.started":"2022-02-28T06:44:47.222604Z","shell.execute_reply":"2022-02-28T06:44:47.263841Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=tf.keras.optimizers.Adam(lr=0.0001/10),loss='categorical_crossentropy',metrics=['accuracy'])","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\nes = EarlyStopping(monitor='val_accuracy', mode='max', patience=5,  restore_best_weights=True)  \nreduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,\n                              patience=5, min_lr=(0.0001/10)/100000)\n","metadata":{"execution":{"iopub.status.busy":"2022-02-27T20:14:22.432228Z","iopub.execute_input":"2022-02-27T20:14:22.432469Z","iopub.status.idle":"2022-02-27T20:14:22.43733Z","shell.execute_reply.started":"2022-02-27T20:14:22.432436Z","shell.execute_reply":"2022-02-27T20:14:22.436642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-27T20:14:22.43836Z","iopub.execute_input":"2022-02-27T20:14:22.439139Z","iopub.status.idle":"2022-02-27T20:14:22.486212Z","shell.execute_reply.started":"2022-02-27T20:14:22.439101Z","shell.execute_reply":"2022-02-27T20:14:22.485476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_ds, epochs=10, validation_data=val_ds, callbacks=[es, reduce_lr], verbose=1)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T20:14:22.487971Z","iopub.execute_input":"2022-02-27T20:14:22.488367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image_names = os.listdir('../input/ml-olympiad-landscape-image-classification/test')\n\nlabel = []\n\nfor image_path in test_image_names:\n    image_array = tf.keras.preprocessing.image.img_to_array(\n                      tf.keras.preprocessing.image.load_img(\n                          os.path.join('../input/ml-olympiad-landscape-image-classification/test', image_path)\n                            )\n    )\n    image_array_with_batchdim = image_array[np.newaxis, :]\n    \n    model_prediction = np.argmax(model.predict(image_array_with_batchdim), axis=-1)[0]\n    \n    label.append(labels[model_prediction])","metadata":{"execution":{"iopub.status.busy":"2022-02-27T19:43:14.349623Z","iopub.status.idle":"2022-02-27T19:43:14.350211Z","shell.execute_reply.started":"2022-02-27T19:43:14.349941Z","shell.execute_reply":"2022-02-27T19:43:14.349968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame(data={\n    \"image\": test_image_names,\n    \"label\": label\n})\n\ndf.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-02-27T19:43:14.351422Z","iopub.status.idle":"2022-02-27T19:43:14.351966Z","shell.execute_reply.started":"2022-02-27T19:43:14.351729Z","shell.execute_reply":"2022-02-27T19:43:14.351756Z"},"trusted":true},"execution_count":null,"outputs":[]}]}