{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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\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 5GB 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","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow.keras as keras\n\nprint(tf.__version__)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"N_CLASSES =  2000 # Number of classes to keep\nBATCH_SIZE = 32","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/landmark-recognition-2020/train.csv\")\ntrain[\"filename\"] = train.id.str[0]+\"/\"+train.id.str[1]+\"/\"+train.id.str[2]+\"/\"+train.id+\".jpg\"\ntrain[\"label\"] = train.landmark_id.astype(str)\ntrain","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y = train.landmark_id.values\nn_classes = np.max(y)\nprint(n_classes)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"top_classes = train['landmark_id'].value_counts()[:N_CLASSES].index.tolist()\ntrain_keep = train[train['landmark_id'].isin(top_classes)]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"base = tf.keras.applications.ResNet50(\n    include_top=False,\n    weights=\"/kaggle/input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5\",\n    input_tensor=None,\n    input_shape=None,\n    pooling=None,\n    #classes=1000,\n    #classifier_activation=\"softmax\",\n)\nbase.trainable = False\n\ninputs = keras.Input(shape=(224, 224, 3))\nx = base(inputs, training=False)\nx = keras.layers.GlobalAveragePooling2D()(x)\nx = keras.layers.Dropout(0.2)(x)  # Regularize with dropout\nout = tf.keras.layers.Dense(N_CLASSES, activation='softmax')(x)\nmodel = keras.Model(inputs, out)\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer=\"adam\", loss=\"categorical_crossentropy\", metrics=[\"categorical_accuracy\"])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"idg = tf.keras.preprocessing.image.ImageDataGenerator(\n    featurewise_center=False,\n    samplewise_center=False,\n    featurewise_std_normalization=False,\n    samplewise_std_normalization=False,\n    zca_whitening=False,\n    zca_epsilon=1e-06,\n    rotation_range=15,\n    width_shift_range=0.0,\n    height_shift_range=0.0,\n    brightness_range=(0.95, 1.05),\n    shear_range=0.0,\n    zoom_range=(0.95, 1.05),\n    channel_shift_range=0.0,\n    fill_mode='nearest',\n    cval=0.0,\n    horizontal_flip=False,\n    vertical_flip=False,\n    rescale=1/.255,\n    preprocessing_function=None,\n    data_format=None,\n    validation_split=0.1,\n    dtype=None\n)\n\n\ntrain_gen = idg.flow_from_dataframe(\n    train_keep,\n    directory=\"/kaggle/input/landmark-recognition-2020/train/\",\n    x_col=\"filename\",\n    y_col=\"label\",\n    weight_col=None,\n    target_size=(224, 224),\n    color_mode=\"rgb\",\n    classes=None,\n    class_mode=\"categorical\",\n    batch_size=BATCH_SIZE,\n    shuffle=True,\n    subset=\"training\",\n    interpolation=\"nearest\",\n    validate_filenames=False)\n    \nval_gen = idg.flow_from_dataframe(\n    train_keep,\n    directory=\"/kaggle/input/landmark-recognition-2020/train/\",\n    x_col=\"filename\",\n    y_col=\"label\",\n    weight_col=None,\n    target_size=(224, 224),\n    color_mode=\"rgb\",\n    classes=None,\n    class_mode=\"categorical\",\n    batch_size=BATCH_SIZE,\n    shuffle=True,\n    subset=\"validation\",\n    interpolation=\"nearest\",\n    validate_filenames=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# training parameters\nEPOCHS = 20\ntrain_steps = int(len(train_keep)*(1-0.1))//BATCH_SIZE\nval_steps = int(len(train_keep)*0.1)//BATCH_SIZE\n\ntrain_steps = train_steps * 0.2\nval_steps = val_steps * 0.2","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"history = model.fit(train_gen, validation_data=val_gen, steps_per_epoch=train_steps, validation_steps=val_steps)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Predicting","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"sub = pd.read_csv(\"/kaggle/input/landmark-recognition-2020/sample_submission.csv\")\nsub[\"filename\"] = sub.id.str[0]+\"/\"+sub.id.str[1]+\"/\"+sub.id.str[2]+\"/\"+sub.id+\".jpg\"\n\ntest_gen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1/.255).flow_from_dataframe(\n    sub,\n    directory=\"/kaggle/input/landmark-recognition-2020/test/\",\n    x_col=\"filename\",\n    y_col=None,\n    weight_col=None,\n    target_size=(224, 224),\n    color_mode=\"rgb\",\n    classes=None,\n    class_mode=None,\n    batch_size=1,\n    shuffle=True,\n    subset=None,\n    interpolation=\"nearest\",\n    validate_filenames=False)\n\ny_pred = model.predict(test_gen, verbose=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_prob = np.max(y_pred, axis=-1)\ny_pred = np.argmax(y_pred, axis=-1)\nprint(y_pred.shape, y_prob.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"thresh = 0.5\n\nfor i in range(len(sub)):\n    prob = y_prob[i]\n    pred = y_pred[i]\n    if prob >= 0.3:\n        st = str(pred)+\" \"+str(prob)\n    else:\n        st = \"\"\n    sub.loc[i, \"landmarks\"] = st\nsub = sub.drop(columns=\"filename\")\nsub.to_csv(\"submission.csv\", index=False)\nsub","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"prob","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}