{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Efficientnet B6 inference only","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"!pip install -q git+https://github.com/qubvel/efficientnet","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport json\nimport cv2\nimport glob\nimport datetime\nfrom tqdm import tqdm\nimport matplotlib.pyplot as plt\nfrom sklearn import metrics\n\nimport efficientnet.tfkeras as efn\nimport numpy as np\nimport random as rn\nimport pandas as pd\nfrom kaggle_datasets import KaggleDatasets\n\nimport tensorflow as tf\nfrom tensorflow.keras import backend as K, layers, losses, optimizers, initializers, constraints, callbacks, models, utils, applications","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"# GCS_DS_PATH_Test = KaggleDatasets().get_gcs_path('alaska2-ds-0512-test')\nGCS_DS_PATH_Test = '../input/alaska2-ds-0512-test'","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"q95 = np.array([[[ 2.,  2.,  2.],\n        [ 1.,  2.,  2.],\n        [ 1.,  2.,  2.],\n        [ 2.,  5.,  5.],\n        [ 2., 10., 10.],\n        [ 4., 10., 10.],\n        [ 5., 10., 10.],\n        [ 6., 10., 10.]],\n\n       [[ 1.,  2.,  2.],\n        [ 1.,  2.,  2.],\n        [ 1.,  3.,  3.],\n        [ 2.,  7.,  7.],\n        [ 3., 10., 10.],\n        [ 6., 10., 10.],\n        [ 6., 10., 10.],\n        [ 6., 10., 10.]],\n\n       [[ 1.,  2.,  2.],\n        [ 1.,  3.,  3.],\n        [ 2.,  6.,  6.],\n        [ 2., 10., 10.],\n        [ 4., 10., 10.],\n        [ 6., 10., 10.],\n        [ 7., 10., 10.],\n        [ 6., 10., 10.]],\n\n       [[ 1.,  5.,  5.],\n        [ 2.,  7.,  7.],\n        [ 2., 10., 10.],\n        [ 3., 10., 10.],\n        [ 5., 10., 10.],\n        [ 9., 10., 10.],\n        [ 8., 10., 10.],\n        [ 6., 10., 10.]],\n\n       [[ 2., 10., 10.],\n        [ 2., 10., 10.],\n        [ 4., 10., 10.],\n        [ 6., 10., 10.],\n        [ 7., 10., 10.],\n        [11., 10., 10.],\n        [10., 10., 10.],\n        [ 8., 10., 10.]],\n\n       [[ 2., 10., 10.],\n        [ 4., 10., 10.],\n        [ 6., 10., 10.],\n        [ 6., 10., 10.],\n        [ 8., 10., 10.],\n        [10., 10., 10.],\n        [11., 10., 10.],\n        [ 9., 10., 10.]],\n\n       [[ 5., 10., 10.],\n        [ 6., 10., 10.],\n        [ 8., 10., 10.],\n        [ 9., 10., 10.],\n        [10., 10., 10.],\n        [12., 10., 10.],\n        [12., 10., 10.],\n        [10., 10., 10.]],\n\n       [[ 7., 10., 10.],\n        [ 9., 10., 10.],\n        [10., 10., 10.],\n        [10., 10., 10.],\n        [11., 10., 10.],\n        [10., 10., 10.],\n        [10., 10., 10.],\n        [10., 10., 10.]]])\n\nq90 = np.array([[[ 3.,  3.,  3.],\n        [ 2.,  4.,  4.],\n        [ 2.,  5.,  5.],\n        [ 3.,  9.,  9.],\n        [ 5., 20., 20.],\n        [ 8., 20., 20.],\n        [10., 20., 20.],\n        [12., 20., 20.]],\n\n       [[ 2.,  4.,  4.],\n        [ 2.,  4.,  4.],\n        [ 3.,  5.,  5.],\n        [ 4., 13., 13.],\n        [ 5., 20., 20.],\n        [12., 20., 20.],\n        [12., 20., 20.],\n        [11., 20., 20.]],\n\n       [[ 3.,  5.,  5.],\n        [ 3.,  5.,  5.],\n        [ 3., 11., 11.],\n        [ 5., 20., 20.],\n        [ 8., 20., 20.],\n        [11., 20., 20.],\n        [14., 20., 20.],\n        [11., 20., 20.]],\n\n       [[ 3.,  9.,  9.],\n        [ 3., 13., 13.],\n        [ 4., 20., 20.],\n        [ 6., 20., 20.],\n        [10., 20., 20.],\n        [17., 20., 20.],\n        [16., 20., 20.],\n        [12., 20., 20.]],\n\n       [[ 4., 20., 20.],\n        [ 4., 20., 20.],\n        [ 7., 20., 20.],\n        [11., 20., 20.],\n        [14., 20., 20.],\n        [22., 20., 20.],\n        [21., 20., 20.],\n        [15., 20., 20.]],\n\n       [[ 5., 20., 20.],\n        [ 7., 20., 20.],\n        [11., 20., 20.],\n        [13., 20., 20.],\n        [16., 20., 20.],\n        [21., 20., 20.],\n        [23., 20., 20.],\n        [18., 20., 20.]],\n\n       [[10., 20., 20.],\n        [13., 20., 20.],\n        [16., 20., 20.],\n        [17., 20., 20.],\n        [21., 20., 20.],\n        [24., 20., 20.],\n        [24., 20., 20.],\n        [20., 20., 20.]],\n\n       [[14., 20., 20.],\n        [18., 20., 20.],\n        [19., 20., 20.],\n        [20., 20., 20.],\n        [22., 20., 20.],\n        [20., 20., 20.],\n        [21., 20., 20.],\n        [20., 20., 20.]]])\n\nq75 = np.array([[[ 8.,  9.,  9.],\n        [ 6.,  9.,  9.],\n        [ 5., 12., 12.],\n        [ 8., 24., 24.],\n        [12., 50., 50.],\n        [20., 50., 50.],\n        [26., 50., 50.],\n        [31., 50., 50.]],\n\n       [[ 6.,  9.,  9.],\n        [ 6., 11., 11.],\n        [ 7., 13., 13.],\n        [10., 33., 33.],\n        [13., 50., 50.],\n        [29., 50., 50.],\n        [30., 50., 50.],\n        [28., 50., 50.]],\n\n       [[ 7., 12., 12.],\n        [ 7., 13., 13.],\n        [ 8., 28., 28.],\n        [12., 50., 50.],\n        [20., 50., 50.],\n        [29., 50., 50.],\n        [35., 50., 50.],\n        [28., 50., 50.]],\n\n       [[ 7., 24., 24.],\n        [ 9., 33., 33.],\n        [11., 50., 50.],\n        [15., 50., 50.],\n        [26., 50., 50.],\n        [44., 50., 50.],\n        [40., 50., 50.],\n        [31., 50., 50.]],\n\n       [[ 9., 50., 50.],\n        [11., 50., 50.],\n        [19., 50., 50.],\n        [28., 50., 50.],\n        [34., 50., 50.],\n        [55., 50., 50.],\n        [52., 50., 50.],\n        [39., 50., 50.]],\n\n       [[12., 50., 50.],\n        [18., 50., 50.],\n        [28., 50., 50.],\n        [32., 50., 50.],\n        [41., 50., 50.],\n        [52., 50., 50.],\n        [57., 50., 50.],\n        [46., 50., 50.]],\n\n       [[25., 50., 50.],\n        [32., 50., 50.],\n        [39., 50., 50.],\n        [44., 50., 50.],\n        [52., 50., 50.],\n        [61., 50., 50.],\n        [60., 50., 50.],\n        [51., 50., 50.]],\n\n       [[36., 50., 50.],\n        [46., 50., 50.],\n        [48., 50., 50.],\n        [49., 50., 50.],\n        [56., 50., 50.],\n        [50., 50., 50.],\n        [52., 50., 50.],\n        [50., 50., 50.]]])\nq95 = tf.cast(tf.tile(q95, (64,64,1)), dtype=tf.float32)\nq90 = tf.cast(tf.tile(q90, (64,64,1)), dtype=tf.float32)\nq75 = tf.cast(tf.tile(q75, (64,64,1)), dtype=tf.float32)\n\n[col, row] = np.meshgrid(range(8), range(8))\nT = 0.5 * np.cos(np.pi * (2 * col + 1) * row / (2 * 8))\nT[0, :] = T[0, :] / np.sqrt(2)\nbroadcast_dims = (64, 8, 64, 8)\nt = np.broadcast_to(T.reshape(1, 8, 1, 8), broadcast_dims)\nt = tf.constant(t, dtype=tf.float32)\n\na = tf.transpose(t, (0, 2, 3, 1))\nc = tf.transpose(t, (0, 2, 1, 3))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def dct_2_ycc(dct):\n    dct = tf.reshape(dct, (64, 8, 64, 8))\n    dct = tf.transpose(dct, (0,2,1,3))\n    ycc = tf.matmul(tf.matmul(a, dct), c)\n    ycc = tf.transpose(ycc, (0, 2, 1, 3))\n    ycc = tf.reshape(ycc, (512, 512))\n    return ycc\n\ndef read_test_tfrecord(data):\n    features = {\n        \"image\": tf.io.FixedLenFeature([], tf.string),\n        \"dct1\": tf.io.FixedLenFeature([], tf.string), \n        \"dct2\": tf.io.FixedLenFeature([], tf.string), \n        \"q\": tf.io.FixedLenFeature([], tf.int64), \n    }\n\n    # decode the TFRecord\n    tf_record = tf.io.parse_single_example(data, features)\n    \n    q = tf.cast(tf_record['q'], tf.float32)\n    \n    dct1 = tf.image.decode_png(tf_record['dct1'])\n    dct2 = tf.image.decode_png(tf_record['dct2'])\n    dct = tf.cast(dct1, tf.int32) * 256 + tf.cast(dct2, tf.int32)\n    dct = tf.cast(dct, tf.int16)\n    \n    dct = tf.cast(dct, tf.float32)\n    dct = tf.case([(tf.math.equal(q, tf.constant(0, dtype=tf.float32)), lambda: dct * q95), \n                (tf.math.equal(q, tf.constant(1, dtype=tf.float32)), lambda: dct * q90),\n                (tf.math.equal(q, tf.constant(2, dtype=tf.float32)), lambda: dct * q75),\n                ])\n    \n    ycc = tf.stack([dct_2_ycc(dct[:,:,0]), dct_2_ycc(dct[:,:,1]), dct_2_ycc(dct[:,:,2])], axis=2) / 255.\n    \n    return (ycc, q), (q, q)\n\ndef tta_flip_left_right(i, o):\n    image, q = i\n    image = tf.image.flip_left_right(image)\n    image = tf.reshape(image, [512, 512, 3])\n    return (image, q), o\n\ndef tta_flip_up_down(i, o):\n    image, q = i\n    image = tf.image.flip_up_down(image)\n    image = tf.reshape(image, [512, 512, 3])\n    return (image, q), o\n\ndef tta_rot90(i, o):\n    image, q = i\n    image = tf.image.rot90(image, k=1)\n    image = tf.reshape(image, [512, 512, 3])\n    return (image, q), o\n\ndef tta_rot180(i, o):\n    image, q = i\n    image = tf.image.rot90(image, k=2)\n    image = tf.reshape(image, [512, 512, 3])\n    return (image, q), o\n\ndef tta_rot270(i, o):\n    image, q = i\n    image = tf.image.rot90(image, k=3)\n    image = tf.reshape(image, [512, 512, 3])\n    return (image, q), o\n\ndef tta_rot90_flip_left_right(i, o):\n    image, q = i\n    image = tf.image.rot90(image, k=1)\n    image = tf.image.flip_left_right(image)\n    image = tf.reshape(image, [512, 512, 3])\n    return (image, q), o\n\ndef tta_rot90_flip_up_down(i, o):\n    image, q = i\n    image = tf.image.rot90(image, k=1)\n    image = tf.image.flip_up_down(image)\n    image = tf.reshape(image, [512, 512, 3])\n    return (image, q), o","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_test_dataset(tta=None):\n    filenames = sorted(tf.io.gfile.glob(GCS_DS_PATH_Test + '/*.tfrec'))\n    dataset = tf.data.TFRecordDataset(filenames)\n    dataset = dataset.map(read_test_tfrecord)\n    if tta is not None:\n        dataset = dataset.map(tta)\n    dataset = dataset.batch(32)\n    return dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_model():\n    q_input = layers.Input(shape=(1,))\n    x_input = layers.Input(shape=(512, 512, 3))\n    x = x_input\n    q = tf.reshape(q_input, (-1,1,1,1))\n    q = tf.tile(q, (1,512,512,1))\n    x = layers.Concatenate()([x, q])\n    x = layers.BatchNormalization(fused=True)(x)\n    \n    mt = efn.EfficientNetB6(\n                input_tensor=x,\n                weights=None,\n                include_top=False\n            )\n    x = mt.layers[-1].output\n    \n    gp = layers.GlobalAveragePooling2D()(x)\n    l = layers.Dense(128, activation='relu', name='ds_l_1')(gp)\n    l = layers.Dense(4, activation='softmax', name='label')(l)\n    \n    p = layers.Dense(128, activation='relu', name='ds_p_1')(gp)\n    p = layers.Dense(1, activation='relu', name='payload')(p)\n    \n    model = models.Model(inputs=[x_input, q_input], outputs=[l, p])\n    return model\n\nmodel = create_model()\nmodel.load_weights('../input/alaska2-ef6-0929/ycc_ef6_f0_l2_ep038_0.54500.h5')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ids = ['{:04d}.jpg'.format(i+1) for i in range(5000)]\nsub = pd.DataFrame(columns=['Id', 'Label'])\nsub['Id'] = ids\n\npsa = []\npsa.append(1 -  model.predict(get_test_dataset(), verbose=1)[0][:, 0])\npsa.append(1 -  model.predict(get_test_dataset(tta_flip_left_right), verbose=1)[0][:, 0])\npsa.append(1 -  model.predict(get_test_dataset(tta_flip_up_down), verbose=1)[0][:, 0])\npsa.append(1 -  model.predict(get_test_dataset(tta_rot180), verbose=1)[0][:, 0])\n\nsub['Label'] = np.mean(psa, axis=0)\nsub.to_csv('submission_tta.csv', index=None)","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}