{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"def prepare_tf():\n    !pip uninstall tensorflow -y\n    !pip install tensorflow-gpu==2.0.0-alpha0\n    from tensorflow.python.ops import control_flow_util\n    control_flow_util.ENABLE_CONTROL_FLOW_V2 = True\n    from tensorflow.python.client import device_lib \n    print(device_lib.list_local_devices())","execution_count":93,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nprint(tf.__version__)","execution_count":29,"outputs":[{"output_type":"stream","text":"1.13.1\n","name":"stdout"}]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nprint(os.listdir(\"../input\"))\nprint(os.listdir(\"../input/train/\"))\nprint(len(os.listdir(\"../input/train/train/\")))\nsample_submission_file = pd.read_csv(\"../input/sample_submission_file.csv\")\n","execution_count":94,"outputs":[{"output_type":"stream","text":"['extraimages', 'test', 'train', 'sample_submission_file.csv']\n['train']\n5\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(len(os.listdir(\"../input/extraimages/extraimages/\")))","execution_count":95,"outputs":[{"output_type":"stream","text":"12595\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_sample_submission_file.head()","execution_count":96,"outputs":[{"output_type":"execute_result","execution_count":96,"data":{"text/plain":"  Category              Id\n0     cbsd  test-img-0.jpg\n1      cmd  test-img-1.jpg\n2      cbb  test-img-2.jpg\n3      cmd  test-img-3.jpg\n4     cbsd  test-img-4.jpg","text/html":"<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Category</th>\n      <th>Id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>cbsd</td>\n      <td>test-img-0.jpg</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>cmd</td>\n      <td>test-img-1.jpg</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>cbb</td>\n      <td>test-img-2.jpg</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>cmd</td>\n      <td>test-img-3.jpg</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>cbsd</td>\n      <td>test-img-4.jpg</td>\n    </tr>\n  </tbody>\n</table>\n</div>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"def append_ext(fn):\n    return fn+\".jpg\"\n\ntestdf=pd.read_csv(\"../input/sample_submission_file.csv\",dtype=str)\ntestdf[\"Id\"]=testdf[\"Id\"].apply(append_ext)","execution_count":97,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"CLASS_MODE = 'categorical'","execution_count":98,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from pathlib import Path\nfrom PIL import Image\n\ndef read_pil_image(img_path, height, width):\n        with open(img_path, 'rb') as f:\n            return np.array(Image.open(f).convert('RGB').resize((width, height)))\n\ndef load_all_images(dataset_path, height, width, img_ext='jpg'):\n    return np.array([read_pil_image(str(p), height, width) for p in \n                                    Path(dataset_path).rglob(\"*.\"+img_ext)]) \n\n","execution_count":99,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"IMAGE_HT_WID=96\nBATCH_SIZE = 32 #100 \n\nfrom keras_preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.optimizers import RMSprop\ntrain_datagen = ImageDataGenerator(\n                               rotation_range=8,\n                               width_shift_range=0.1,\n                               height_shift_range=0.1,\n                               shear_range=0.01,\n                               zoom_range=[0.9, 1.25],\n                               horizontal_flip=True,\n                               vertical_flip=False,\n                              channel_shift_range = 0.1,\n                              fill_mode='nearest',\n                              #brightness_range=[0.5, 1.5],\n                               validation_split=0.25,\n                               rescale=1./255)\n\n","execution_count":144,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_generator=train_datagen.flow_from_directory(\n                    directory=\"../input/train/train/\",\n                    subset=\"training\",\n                   batch_size=BATCH_SIZE,\n                    seed=42,\n                    shuffle=True,\n                    class_mode='categorical',\n                    target_size=(IMAGE_HT_WID,IMAGE_HT_WID))\n\nvalid_generator=train_datagen.flow_from_directory(\n                    directory=\"../input/train/train/\",\n                    subset=\"validation\",\n                    batch_size=BATCH_SIZE,\n                    seed=42,\n                    shuffle=True,\n                    class_mode='categorical',\n                    target_size=(IMAGE_HT_WID,IMAGE_HT_WID))\n","execution_count":145,"outputs":[{"output_type":"stream","text":"Found 4244 images belonging to 5 classes.\nFound 1412 images belonging to 5 classes.\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_datagen=ImageDataGenerator(rescale=1./255.)\ntest_generator=test_datagen.flow_from_dataframe(\ndataframe=testdf,\ndirectory=\"../input/test/test\",\nx_col=\"Id\",\ny_col=None,\nbatch_size=32,\nseed=42,\nshuffle=False,\nclass_mode=None,\ntarget_size=(IMAGE_HT_WID,IMAGE_HT_WID))","execution_count":146,"outputs":[{"output_type":"stream","text":"Found 0 images.\n","name":"stdout"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"import tensorflow as tf\nkeras = tf.keras","execution_count":114,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"np.random.seed(42)\nimport random as rn\nrn.seed(12345)\nsession_conf = tf.ConfigProto(intra_op_parallelism_threads=1,\n                           inter_op_parallelism_threads=1)\n\nfrom keras import backend as K\n\ntf.set_random_seed(1234)\n\nsess = tf.Session(graph=tf.get_default_graph(), config=session_conf)\nK.set_session(sess)\n","execution_count":115,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"#https://www.tensorflow.org/tutorials/images/transfer_learning\n\nfrom tensorflow import keras\nimport tensorflow as tf\nkeras = tf.keras\nbase_model = tf.keras.applications.MobileNetV2(input_shape=(IMAGE_HT_WID,IMAGE_HT_WID, 3),\n                                               include_top=False, \n                                               weights='imagenet')\nbase_model.trainable = False\nprint(base_model.summary())\nmodel = tf.keras.Sequential([\n  base_model,\n  keras.layers.GaussianNoise(0.2),  \n  keras.layers.GlobalAveragePooling2D(),\n  keras.layers.Dense(5, activation='sigmoid')\n])\n\nmodel.compile(loss='categorical_crossentropy', optimizer=tf.keras.optimizers.RMSprop(lr=0.0001), metrics=['accuracy'])\n\nprint(model.summary())\nlen(base_model.layers)","execution_count":116,"outputs":[{"output_type":"stream","text":"Downloading data from https://github.com/JonathanCMitchell/mobilenet_v2_keras/releases/download/v1.1/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_96_no_top.h5\n9412608/9406464 [==============================] - 1s 0us/step\n__________________________________________________________________________________________________\nLayer (type)                    Output Shape         Param #     Connected to                     \n==================================================================================================\ninput_7 (InputLayer)            (None, 96, 96, 3)    0                                            \n__________________________________________________________________________________________________\nConv1_pad (ZeroPadding2D)       (None, 97, 97, 3)    0           input_7[0][0]                    \n__________________________________________________________________________________________________\nConv1 (Conv2D)                  (None, 48, 48, 32)   864         Conv1_pad[0][0]                  \n__________________________________________________________________________________________________\nbn_Conv1 (BatchNormalizationV1) (None, 48, 48, 32)   128         Conv1[0][0]                      \n__________________________________________________________________________________________________\nConv1_relu (ReLU)               (None, 48, 48, 32)   0           bn_Conv1[0][0]                   \n__________________________________________________________________________________________________\nexpanded_conv_depthwise (Depthw (None, 48, 48, 32)   288         Conv1_relu[0][0]                 \n__________________________________________________________________________________________________\nexpanded_conv_depthwise_BN (Bat (None, 48, 48, 32)   128         expanded_conv_depthwise[0][0]    \n__________________________________________________________________________________________________\nexpanded_conv_depthwise_relu (R (None, 48, 48, 32)   0           expanded_conv_depthwise_BN[0][0] \n__________________________________________________________________________________________________\nexpanded_conv_project (Conv2D)  (None, 48, 48, 16)   512         expanded_conv_depthwise_relu[0][0\n__________________________________________________________________________________________________\nexpanded_conv_project_BN (Batch (None, 48, 48, 16)   64          expanded_conv_project[0][0]      \n__________________________________________________________________________________________________\nblock_1_expand (Conv2D)         (None, 48, 48, 96)   1536        expanded_conv_project_BN[0][0]   \n__________________________________________________________________________________________________\nblock_1_expand_BN (BatchNormali (None, 48, 48, 96)   384         block_1_expand[0][0]             \n__________________________________________________________________________________________________\nblock_1_expand_relu (ReLU)      (None, 48, 48, 96)   0           block_1_expand_BN[0][0]          \n__________________________________________________________________________________________________\nblock_1_pad (ZeroPadding2D)     (None, 49, 49, 96)   0           block_1_expand_relu[0][0]        \n__________________________________________________________________________________________________\nblock_1_depthwise (DepthwiseCon (None, 24, 24, 96)   864         block_1_pad[0][0]                \n__________________________________________________________________________________________________\nblock_1_depthwise_BN (BatchNorm (None, 24, 24, 96)   384         block_1_depthwise[0][0]          \n__________________________________________________________________________________________________\nblock_1_depthwise_relu (ReLU)   (None, 24, 24, 96)   0           block_1_depthwise_BN[0][0]       \n__________________________________________________________________________________________________\nblock_1_project (Conv2D)        (None, 24, 24, 24)   2304        block_1_depthwise_relu[0][0]     \n__________________________________________________________________________________________________\nblock_1_project_BN (BatchNormal (None, 24, 24, 24)   96          block_1_project[0][0]            \n__________________________________________________________________________________________________\nblock_2_expand (Conv2D)         (None, 24, 24, 144)  3456        block_1_project_BN[0][0]         \n__________________________________________________________________________________________________\nblock_2_expand_BN (BatchNormali (None, 24, 24, 144)  576         block_2_expand[0][0]             \n__________________________________________________________________________________________________\nblock_2_expand_relu (ReLU)      (None, 24, 24, 144)  0           block_2_expand_BN[0][0]          \n__________________________________________________________________________________________________\nblock_2_depthwise (DepthwiseCon (None, 24, 24, 144)  1296        block_2_expand_relu[0][0]        \n__________________________________________________________________________________________________\nblock_2_depthwise_BN (BatchNorm (None, 24, 24, 144)  576         block_2_depthwise[0][0]          \n__________________________________________________________________________________________________\nblock_2_depthwise_relu (ReLU)   (None, 24, 24, 144)  0           block_2_depthwise_BN[0][0]       \n__________________________________________________________________________________________________\nblock_2_project (Conv2D)        (None, 24, 24, 24)   3456        block_2_depthwise_relu[0][0]     \n__________________________________________________________________________________________________\nblock_2_project_BN (BatchNormal (None, 24, 24, 24)   96          block_2_project[0][0]            \n__________________________________________________________________________________________________\nblock_2_add (Add)               (None, 24, 24, 24)   0           block_1_project_BN[0][0]         \n                                                                 block_2_project_BN[0][0]         \n__________________________________________________________________________________________________\nblock_3_expand (Conv2D)         (None, 24, 24, 144)  3456        block_2_add[0][0]                \n__________________________________________________________________________________________________\nblock_3_expand_BN (BatchNormali (None, 24, 24, 144)  576         block_3_expand[0][0]             \n__________________________________________________________________________________________________\nblock_3_expand_relu (ReLU)      (None, 24, 24, 144)  0           block_3_expand_BN[0][0]          \n__________________________________________________________________________________________________\nblock_3_pad (ZeroPadding2D)     (None, 25, 25, 144)  0           block_3_expand_relu[0][0]        \n__________________________________________________________________________________________________\nblock_3_depthwise (DepthwiseCon (None, 12, 12, 144)  1296        block_3_pad[0][0]                \n__________________________________________________________________________________________________\nblock_3_depthwise_BN (BatchNorm (None, 12, 12, 144)  576         block_3_depthwise[0][0]          \n__________________________________________________________________________________________________\nblock_3_depthwise_relu (ReLU)   (None, 12, 12, 144)  0           block_3_depthwise_BN[0][0]       \n__________________________________________________________________________________________________\nblock_3_project (Conv2D)        (None, 12, 12, 32)   4608        block_3_depthwise_relu[0][0]     \n__________________________________________________________________________________________________\nblock_3_project_BN (BatchNormal (None, 12, 12, 32)   128         block_3_project[0][0]            \n__________________________________________________________________________________________________\nblock_4_expand (Conv2D)         (None, 12, 12, 192)  6144        block_3_project_BN[0][0]         \n__________________________________________________________________________________________________\nblock_4_expand_BN (BatchNormali (None, 12, 12, 192)  768         block_4_expand[0][0]             \n__________________________________________________________________________________________________\nblock_4_expand_relu (ReLU)      (None, 12, 12, 192)  0           block_4_expand_BN[0][0]          \n__________________________________________________________________________________________________\nblock_4_depthwise (DepthwiseCon (None, 12, 12, 192)  1728        block_4_expand_relu[0][0]        \n__________________________________________________________________________________________________\nblock_4_depthwise_BN (BatchNorm (None, 12, 12, 192)  768         block_4_depthwise[0][0]          \n__________________________________________________________________________________________________\nblock_4_depthwise_relu (ReLU)   (None, 12, 12, 192)  0           block_4_depthwise_BN[0][0]       \n__________________________________________________________________________________________________\nblock_4_project (Conv2D)        (None, 12, 12, 32)   6144        block_4_depthwise_relu[0][0]     \n__________________________________________________________________________________________________\nblock_4_project_BN (BatchNormal (None, 12, 12, 32)   128         block_4_project[0][0]            \n__________________________________________________________________________________________________\nblock_4_add (Add)               (None, 12, 12, 32)   0           block_3_project_BN[0][0]         \n                                                                 block_4_project_BN[0][0]         \n__________________________________________________________________________________________________\nblock_5_expand (Conv2D)         (None, 12, 12, 192)  6144        block_4_add[0][0]                \n__________________________________________________________________________________________________\nblock_5_expand_BN (BatchNormali (None, 12, 12, 192)  768         block_5_expand[0][0]             \n__________________________________________________________________________________________________\nblock_5_expand_relu (ReLU)      (None, 12, 12, 192)  0           block_5_expand_BN[0][0]          \n__________________________________________________________________________________________________\nblock_5_depthwise (DepthwiseCon (None, 12, 12, 192)  1728        block_5_expand_relu[0][0]        \n__________________________________________________________________________________________________\nblock_5_depthwise_BN (BatchNorm (None, 12, 12, 192)  768         block_5_depthwise[0][0]          \n__________________________________________________________________________________________________\nblock_5_depthwise_relu (ReLU)   (None, 12, 12, 192)  0           block_5_depthwise_BN[0][0]       \n__________________________________________________________________________________________________\nblock_5_project (Conv2D)        (None, 12, 12, 32)   6144        block_5_depthwise_relu[0][0]     \n__________________________________________________________________________________________________\nblock_5_project_BN (BatchNormal (None, 12, 12, 32)   128         block_5_project[0][0]            \n__________________________________________________________________________________________________\nblock_5_add (Add)               (None, 12, 12, 32)   0           block_4_add[0][0]                \n                                                                 block_5_project_BN[0][0]         \n__________________________________________________________________________________________________\nblock_6_expand (Conv2D)         (None, 12, 12, 192)  6144        block_5_add[0][0]                \n__________________________________________________________________________________________________\nblock_6_expand_BN (BatchNormali (None, 12, 12, 192)  768         block_6_expand[0][0]             \n__________________________________________________________________________________________________\nblock_6_expand_relu (ReLU)      (None, 12, 12, 192)  0           block_6_expand_BN[0][0]          \n__________________________________________________________________________________________________\nblock_6_pad (ZeroPadding2D)     (None, 13, 13, 192)  0           block_6_expand_relu[0][0]        \n__________________________________________________________________________________________________\nblock_6_depthwise (DepthwiseCon (None, 6, 6, 192)    1728        block_6_pad[0][0]                \n__________________________________________________________________________________________________\nblock_6_depthwise_BN (BatchNorm (None, 6, 6, 192)    768         block_6_depthwise[0][0]          \n__________________________________________________________________________________________________\nblock_6_depthwise_relu (ReLU)   (None, 6, 6, 192)    0           block_6_depthwise_BN[0][0]       \n__________________________________________________________________________________________________\nblock_6_project (Conv2D)        (None, 6, 6, 64)     12288       block_6_depthwise_relu[0][0]     \n__________________________________________________________________________________________________\nblock_6_project_BN (BatchNormal (None, 6, 6, 64)     256         block_6_project[0][0]            \n__________________________________________________________________________________________________\nblock_7_expand (Conv2D)         (None, 6, 6, 384)    24576       block_6_project_BN[0][0]         \n__________________________________________________________________________________________________\nblock_7_expand_BN (BatchNormali (None, 6, 6, 384)    1536        block_7_expand[0][0]             \n__________________________________________________________________________________________________\nblock_7_expand_relu (ReLU)      (None, 6, 6, 384)    0           block_7_expand_BN[0][0]          \n__________________________________________________________________________________________________\nblock_7_depthwise (DepthwiseCon (None, 6, 6, 384)    3456        block_7_expand_relu[0][0]        \n__________________________________________________________________________________________________\nblock_7_depthwise_BN (BatchNorm (None, 6, 6, 384)    1536        block_7_depthwise[0][0]          \n__________________________________________________________________________________________________\nblock_7_depthwise_relu (ReLU)   (None, 6, 6, 384)    0           block_7_depthwise_BN[0][0]       \n__________________________________________________________________________________________________\nblock_7_project (Conv2D)        (None, 6, 6, 64)     24576       block_7_depthwise_relu[0][0]     \n__________________________________________________________________________________________________\nblock_7_project_BN (BatchNormal (None, 6, 6, 64)     256         block_7_project[0][0]            \n__________________________________________________________________________________________________\nblock_7_add (Add)               (None, 6, 6, 64)     0           block_6_project_BN[0][0]         \n                                                                 block_7_project_BN[0][0]         \n__________________________________________________________________________________________________\nblock_8_expand (Conv2D)         (None, 6, 6, 384)    24576       block_7_add[0][0]                \n__________________________________________________________________________________________________\nblock_8_expand_BN (BatchNormali (None, 6, 6, 384)    1536        block_8_expand[0][0]             \n__________________________________________________________________________________________________\nblock_8_expand_relu (ReLU)      (None, 6, 6, 384)    0           block_8_expand_BN[0][0]          \n__________________________________________________________________________________________________\nblock_8_depthwise (DepthwiseCon (None, 6, 6, 384)    3456        block_8_expand_relu[0][0]        \n__________________________________________________________________________________________________\nblock_8_depthwise_BN (BatchNorm (None, 6, 6, 384)    1536        block_8_depthwise[0][0]          \n__________________________________________________________________________________________________\nblock_8_depthwise_relu (ReLU)   (None, 6, 6, 384)    0           block_8_depthwise_BN[0][0]       \n__________________________________________________________________________________________________\nblock_8_project (Conv2D)        (None, 6, 6, 64)     24576       block_8_depthwise_relu[0][0]     \n__________________________________________________________________________________________________\nblock_8_project_BN (BatchNormal (None, 6, 6, 64)     256         block_8_project[0][0]            \n__________________________________________________________________________________________________\nblock_8_add (Add)               (None, 6, 6, 64)     0           block_7_add[0][0]                \n                                                                 block_8_project_BN[0][0]         \n__________________________________________________________________________________________________\nblock_9_expand (Conv2D)         (None, 6, 6, 384)    24576       block_8_add[0][0]                \n__________________________________________________________________________________________________\nblock_9_expand_BN (BatchNormali (None, 6, 6, 384)    1536        block_9_expand[0][0]             \n__________________________________________________________________________________________________\nblock_9_expand_relu (ReLU)      (None, 6, 6, 384)    0           block_9_expand_BN[0][0]          \n__________________________________________________________________________________________________\nblock_9_depthwise (DepthwiseCon (None, 6, 6, 384)    3456        block_9_expand_relu[0][0]        \n__________________________________________________________________________________________________\nblock_9_depthwise_BN (BatchNorm (None, 6, 6, 384)    1536        block_9_depthwise[0][0]          \n__________________________________________________________________________________________________\nblock_9_depthwise_relu (ReLU)   (None, 6, 6, 384)    0           block_9_depthwise_BN[0][0]       \n__________________________________________________________________________________________________\nblock_9_project (Conv2D)        (None, 6, 6, 64)     24576       block_9_depthwise_relu[0][0]     \n__________________________________________________________________________________________________\nblock_9_project_BN (BatchNormal (None, 6, 6, 64)     256         block_9_project[0][0]            \n__________________________________________________________________________________________________\nblock_9_add (Add)               (None, 6, 6, 64)     0           block_8_add[0][0]                \n                                                                 block_9_project_BN[0][0]         \n__________________________________________________________________________________________________\nblock_10_expand (Conv2D)        (None, 6, 6, 384)    24576       block_9_add[0][0]                \n__________________________________________________________________________________________________\nblock_10_expand_BN (BatchNormal (None, 6, 6, 384)    1536        block_10_expand[0][0]            \n__________________________________________________________________________________________________\nblock_10_expand_relu (ReLU)     (None, 6, 6, 384)    0           block_10_expand_BN[0][0]         \n__________________________________________________________________________________________________\nblock_10_depthwise (DepthwiseCo (None, 6, 6, 384)    3456        block_10_expand_relu[0][0]       \n__________________________________________________________________________________________________\nblock_10_depthwise_BN (BatchNor (None, 6, 6, 384)    1536        block_10_depthwise[0][0]         \n__________________________________________________________________________________________________\nblock_10_depthwise_relu (ReLU)  (None, 6, 6, 384)    0           block_10_depthwise_BN[0][0]      \n__________________________________________________________________________________________________\nblock_10_project (Conv2D)       (None, 6, 6, 96)     36864       block_10_depthwise_relu[0][0]    \n__________________________________________________________________________________________________\nblock_10_project_BN (BatchNorma (None, 6, 6, 96)     384         block_10_project[0][0]           \n__________________________________________________________________________________________________\nblock_11_expand (Conv2D)        (None, 6, 6, 576)    55296       block_10_project_BN[0][0]        \n__________________________________________________________________________________________________\nblock_11_expand_BN (BatchNormal (None, 6, 6, 576)    2304        block_11_expand[0][0]            \n__________________________________________________________________________________________________\nblock_11_expand_relu (ReLU)     (None, 6, 6, 576)    0           block_11_expand_BN[0][0]         \n__________________________________________________________________________________________________\nblock_11_depthwise (DepthwiseCo (None, 6, 6, 576)    5184        block_11_expand_relu[0][0]       \n__________________________________________________________________________________________________\nblock_11_depthwise_BN (BatchNor (None, 6, 6, 576)    2304        block_11_depthwise[0][0]         \n__________________________________________________________________________________________________\nblock_11_depthwise_relu (ReLU)  (None, 6, 6, 576)    0           block_11_depthwise_BN[0][0]      \n__________________________________________________________________________________________________\nblock_11_project (Conv2D)       (None, 6, 6, 96)     55296       block_11_depthwise_relu[0][0]    \n__________________________________________________________________________________________________\nblock_11_project_BN (BatchNorma (None, 6, 6, 96)     384         block_11_project[0][0]           \n__________________________________________________________________________________________________\nblock_11_add (Add)              (None, 6, 6, 96)     0           block_10_project_BN[0][0]        \n                                                                 block_11_project_BN[0][0]        \n__________________________________________________________________________________________________\nblock_12_expand (Conv2D)        (None, 6, 6, 576)    55296       block_11_add[0][0]               \n__________________________________________________________________________________________________\nblock_12_expand_BN (BatchNormal (None, 6, 6, 576)    2304        block_12_expand[0][0]            \n__________________________________________________________________________________________________\nblock_12_expand_relu (ReLU)     (None, 6, 6, 576)    0           block_12_expand_BN[0][0]         \n__________________________________________________________________________________________________\nblock_12_depthwise (DepthwiseCo (None, 6, 6, 576)    5184        block_12_expand_relu[0][0]       \n__________________________________________________________________________________________________\nblock_12_depthwise_BN (BatchNor (None, 6, 6, 576)    2304        block_12_depthwise[0][0]         \n__________________________________________________________________________________________________\nblock_12_depthwise_relu (ReLU)  (None, 6, 6, 576)    0           block_12_depthwise_BN[0][0]      \n__________________________________________________________________________________________________\nblock_12_project (Conv2D)       (None, 6, 6, 96)     55296       block_12_depthwise_relu[0][0]    \n__________________________________________________________________________________________________\nblock_12_project_BN (BatchNorma (None, 6, 6, 96)     384         block_12_project[0][0]           \n__________________________________________________________________________________________________\nblock_12_add (Add)              (None, 6, 6, 96)     0           block_11_add[0][0]               \n                                                                 block_12_project_BN[0][0]        \n__________________________________________________________________________________________________\nblock_13_expand (Conv2D)        (None, 6, 6, 576)    55296       block_12_add[0][0]               \n__________________________________________________________________________________________________\nblock_13_expand_BN (BatchNormal (None, 6, 6, 576)    2304        block_13_expand[0][0]            \n__________________________________________________________________________________________________\nblock_13_expand_relu (ReLU)     (None, 6, 6, 576)    0           block_13_expand_BN[0][0]         \n__________________________________________________________________________________________________\nblock_13_pad (ZeroPadding2D)    (None, 7, 7, 576)    0           block_13_expand_relu[0][0]       \n__________________________________________________________________________________________________\nblock_13_depthwise (DepthwiseCo (None, 3, 3, 576)    5184        block_13_pad[0][0]               \n__________________________________________________________________________________________________\nblock_13_depthwise_BN (BatchNor (None, 3, 3, 576)    2304        block_13_depthwise[0][0]         \n__________________________________________________________________________________________________\nblock_13_depthwise_relu (ReLU)  (None, 3, 3, 576)    0           block_13_depthwise_BN[0][0]      \n__________________________________________________________________________________________________\nblock_13_project (Conv2D)       (None, 3, 3, 160)    92160       block_13_depthwise_relu[0][0]    \n__________________________________________________________________________________________________\nblock_13_project_BN (BatchNorma (None, 3, 3, 160)    640         block_13_project[0][0]           \n__________________________________________________________________________________________________\nblock_14_expand (Conv2D)        (None, 3, 3, 960)    153600      block_13_project_BN[0][0]        \n__________________________________________________________________________________________________\nblock_14_expand_BN (BatchNormal (None, 3, 3, 960)    3840        block_14_expand[0][0]            \n__________________________________________________________________________________________________\nblock_14_expand_relu (ReLU)     (None, 3, 3, 960)    0           block_14_expand_BN[0][0]         \n__________________________________________________________________________________________________\nblock_14_depthwise (DepthwiseCo (None, 3, 3, 960)    8640        block_14_expand_relu[0][0]       \n__________________________________________________________________________________________________\nblock_14_depthwise_BN (BatchNor (None, 3, 3, 960)    3840        block_14_depthwise[0][0]         \n__________________________________________________________________________________________________\nblock_14_depthwise_relu (ReLU)  (None, 3, 3, 960)    0           block_14_depthwise_BN[0][0]      \n__________________________________________________________________________________________________\nblock_14_project (Conv2D)       (None, 3, 3, 160)    153600      block_14_depthwise_relu[0][0]    \n__________________________________________________________________________________________________\nblock_14_project_BN (BatchNorma (None, 3, 3, 160)    640         block_14_project[0][0]           \n__________________________________________________________________________________________________\nblock_14_add (Add)              (None, 3, 3, 160)    0           block_13_project_BN[0][0]        \n                                                                 block_14_project_BN[0][0]        \n__________________________________________________________________________________________________\nblock_15_expand (Conv2D)        (None, 3, 3, 960)    153600      block_14_add[0][0]               \n__________________________________________________________________________________________________\nblock_15_expand_BN (BatchNormal (None, 3, 3, 960)    3840        block_15_expand[0][0]            \n__________________________________________________________________________________________________\nblock_15_expand_relu (ReLU)     (None, 3, 3, 960)    0           block_15_expand_BN[0][0]         \n__________________________________________________________________________________________________\nblock_15_depthwise (DepthwiseCo (None, 3, 3, 960)    8640        block_15_expand_relu[0][0]       \n__________________________________________________________________________________________________\nblock_15_depthwise_BN (BatchNor (None, 3, 3, 960)    3840        block_15_depthwise[0][0]         \n__________________________________________________________________________________________________\nblock_15_depthwise_relu (ReLU)  (None, 3, 3, 960)    0           block_15_depthwise_BN[0][0]      \n__________________________________________________________________________________________________\nblock_15_project (Conv2D)       (None, 3, 3, 160)    153600      block_15_depthwise_relu[0][0]    \n__________________________________________________________________________________________________\nblock_15_project_BN (BatchNorma (None, 3, 3, 160)    640         block_15_project[0][0]           \n__________________________________________________________________________________________________\nblock_15_add (Add)              (None, 3, 3, 160)    0           block_14_add[0][0]               \n                                                                 block_15_project_BN[0][0]        \n__________________________________________________________________________________________________\nblock_16_expand (Conv2D)        (None, 3, 3, 960)    153600      block_15_add[0][0]               \n__________________________________________________________________________________________________\nblock_16_expand_BN (BatchNormal (None, 3, 3, 960)    3840        block_16_expand[0][0]            \n__________________________________________________________________________________________________\nblock_16_expand_relu (ReLU)     (None, 3, 3, 960)    0           block_16_expand_BN[0][0]         \n__________________________________________________________________________________________________\nblock_16_depthwise (DepthwiseCo (None, 3, 3, 960)    8640        block_16_expand_relu[0][0]       \n__________________________________________________________________________________________________\nblock_16_depthwise_BN (BatchNor (None, 3, 3, 960)    3840        block_16_depthwise[0][0]         \n__________________________________________________________________________________________________\nblock_16_depthwise_relu (ReLU)  (None, 3, 3, 960)    0           block_16_depthwise_BN[0][0]      \n__________________________________________________________________________________________________\nblock_16_project (Conv2D)       (None, 3, 3, 320)    307200      block_16_depthwise_relu[0][0]    \n__________________________________________________________________________________________________\nblock_16_project_BN (BatchNorma (None, 3, 3, 320)    1280        block_16_project[0][0]           \n__________________________________________________________________________________________________\nConv_1 (Conv2D)                 (None, 3, 3, 1280)   409600      block_16_project_BN[0][0]        \n__________________________________________________________________________________________________\nConv_1_bn (BatchNormalizationV1 (None, 3, 3, 1280)   5120        Conv_1[0][0]                     \n__________________________________________________________________________________________________\nout_relu (ReLU)                 (None, 3, 3, 1280)   0           Conv_1_bn[0][0]                  \n==================================================================================================\nTotal params: 2,257,984\nTrainable params: 0\nNon-trainable params: 2,257,984\n__________________________________________________________________________________________________\nNone\n","name":"stdout"},{"output_type":"stream","text":"_________________________________________________________________\nLayer (type)                 Output Shape              Param #   \n=================================================================\nmobilenetv2_1.00_96 (Model)  (None, 3, 3, 1280)        2257984   \n_________________________________________________________________\ngaussian_noise_6 (GaussianNo (None, 3, 3, 1280)        0         \n_________________________________________________________________\nglobal_average_pooling2d_6 ( (None, 1280)              0         \n_________________________________________________________________\ndense_6 (Dense)              (None, 5)                 6405      \n=================================================================\nTotal params: 2,264,389\nTrainable params: 6,405\nNon-trainable params: 2,257,984\n_________________________________________________________________\nNone\n","name":"stdout"},{"output_type":"execute_result","execution_count":116,"data":{"text/plain":"155"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"reduce_lr = keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.7, \n                                                                    patience=3, verbose=2, mode='auto',\n                                                                    min_lr=1e-6)\n\nearly_stop = tf.keras.callbacks.EarlyStopping(monitor='val_loss', \n                                            # min_delta=0, \n                                             patience=6, verbose=2, mode='auto',\n                                             baseline=None, restore_best_weights=True)","execution_count":117,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras_preprocessing.image import ImageDataGenerator\nfrom keras.layers import Dense, Activation, Flatten, Dropout, BatchNormalization\nfrom keras.layers import Conv2D, MaxPooling2D\nfrom keras import regularizers, optimizers\nmodel = Sequential()\nmodel.add(Conv2D(32, (3, 3), padding='same',\n                 input_shape=(IMAGE_HT_WID,IMAGE_HT_WID,3)))\nmodel.add(Activation('relu'))\nmodel.add(Conv2D(32, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Conv2D(64, (3, 3), padding='same'))\nmodel.add(Activation('relu'))\nmodel.add(Conv2D(64, (3, 3)))\nmodel.add(Activation('relu'))\nmodel.add(MaxPooling2D(pool_size=(2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(512))\nmodel.add(Activation('relu'))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(output_dim=5, activation='sigmoid'))\nmodel.compile(optimizers.rmsprop(lr=0.0001, decay=1e-6),loss=\"categorical_crossentropy\",metrics=[\"accuracy\"])","execution_count":147,"outputs":[{"output_type":"stream","text":"/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:24: UserWarning: Update your `Dense` call to the Keras 2 API: `Dense(activation=\"sigmoid\", units=5)`\n","name":"stderr"}]},{"metadata":{"trusted":true},"cell_type":"code","source":"STEP_SIZE_TRAIN=train_generator.n//train_generator.batch_size\nSTEP_SIZE_VALID=valid_generator.n//valid_generator.batch_size\nSTEP_SIZE_TEST=test_generator.n//test_generator.batch_size\nmodel.fit_generator(generator=train_generator,\n                    steps_per_epoch=STEP_SIZE_TRAIN,\n                    validation_data=valid_generator,\n                    validation_steps=STEP_SIZE_VALID,\n                    epochs=10\n                   )","execution_count":148,"outputs":[{"output_type":"stream","text":"Epoch 1/10\n132/132 [==============================] - 62s 470ms/step - loss: 1.3538 - acc: 0.4664 - val_loss: 1.3336 - val_acc: 0.4702\nEpoch 2/10\n132/132 [==============================] - 48s 363ms/step - loss: 1.2786 - acc: 0.5006 - val_loss: 1.2273 - val_acc: 0.5290\nEpoch 3/10\n132/132 [==============================] - 48s 363ms/step - loss: 1.2175 - acc: 0.5588 - val_loss: 1.1778 - val_acc: 0.5761\nEpoch 4/10\n132/132 [==============================] - 48s 360ms/step - loss: 1.1868 - acc: 0.5649 - val_loss: 1.2478 - val_acc: 0.5471\nEpoch 5/10\n132/132 [==============================] - 48s 362ms/step - loss: 1.1537 - acc: 0.5822 - val_loss: 1.1286 - val_acc: 0.5978\nEpoch 6/10\n132/132 [==============================] - 48s 363ms/step - loss: 1.1329 - acc: 0.5848 - val_loss: 1.1273 - val_acc: 0.5652\nEpoch 7/10\n132/132 [==============================] - 48s 362ms/step - loss: 1.1099 - acc: 0.5882 - val_loss: 1.1011 - val_acc: 0.6058\nEpoch 8/10\n132/132 [==============================] - 48s 362ms/step - loss: 1.1000 - acc: 0.5971 - val_loss: 1.1155 - val_acc: 0.6065\nEpoch 9/10\n132/132 [==============================] - 48s 361ms/step - loss: 1.0953 - acc: 0.6005 - val_loss: 1.1091 - val_acc: 0.6036\nEpoch 10/10\n132/132 [==============================] - 48s 362ms/step - loss: 1.0634 - acc: 0.6089 - val_loss: 1.0467 - val_acc: 0.6101\n","name":"stdout"},{"output_type":"execute_result","execution_count":148,"data":{"text/plain":"<keras.callbacks.History at 0x7f2d610c7438>"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.evaluate_generator(generator=valid_generator,\nsteps=STEP_SIZE_VALID)","execution_count":153,"outputs":[{"output_type":"execute_result","execution_count":153,"data":{"text/plain":"[1.0141543695892112, 0.6231884057971014]"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_generator.reset()\npred=model.predict_generator(test_generator,\nsteps=STEP_SIZE_TEST,\nverbose=1)","execution_count":169,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"batch_size=32\nnb_validation_samples=1412\nsteps = nb_validation_samples / batch_size\npredicted_class_indices = model.predict_generator(valid_generator, steps)","execution_count":182,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = (train_generator.class_indices)\nlabels","execution_count":177,"outputs":[{"output_type":"execute_result","execution_count":177,"data":{"text/plain":"{'cbb': 0, 'cbsd': 1, 'cgm': 2, 'cmd': 3, 'healthy': 4}"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"labels = dict((v,k) for k,v in labels.items())\nlabels","execution_count":179,"outputs":[{"output_type":"execute_result","execution_count":179,"data":{"text/plain":"{'cbb': 0, 'cbsd': 1, 'cgm': 2, 'cmd': 3, 'healthy': 4}"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"predicted_class_indices[:10]\n","execution_count":184,"outputs":[{"output_type":"execute_result","execution_count":184,"data":{"text/plain":"array([[0.31993294, 0.6769719 , 0.33859402, 0.25180233, 0.26553005],\n       [0.03223047, 0.0704419 , 0.18639359, 0.92599463, 0.03299016],\n       [0.08881357, 0.13175213, 0.24488991, 0.6542265 , 0.05238658],\n       [0.09822261, 0.15259108, 0.2889521 , 0.613848  , 0.07576728],\n       [0.02713665, 0.05733129, 0.24481037, 0.9522648 , 0.05741864],\n       [0.04019421, 0.15594706, 0.18759722, 0.9167464 , 0.08049977],\n       [0.31286317, 0.6999869 , 0.25967228, 0.50729376, 0.11121324],\n       [0.15261668, 0.35476708, 0.34530762, 0.8189937 , 0.2821557 ],\n       [0.04533574, 0.07218051, 0.27139014, 0.9071514 , 0.06227484],\n       [0.06894866, 0.08273396, 0.36047387, 0.91041434, 0.11430457]],\n      dtype=float32)"},"metadata":{}}]},{"metadata":{"trusted":true},"cell_type":"code","source":"import numpy as np\npredicted_class_indices=hash((np.array(predicted_class_indices[:])))\npredictions = [labels[k] for k in predicted_class_indices]\nfilenames=test_generator.filenames\nresults=pd.DataFrame({\"Category\":predictions,\n                      \"id\":filenames})\n#subm = pd.merge(df_test, results, on='file_name')[['id','Category']]\n","execution_count":191,"outputs":[{"output_type":"error","ename":"TypeError","evalue":"unhashable type: 'numpy.ndarray'","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)","\u001b[0;32m<ipython-input-191-dd55bc95d65c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mpredicted_class_indices\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mhash\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpredicted_class_indices\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0mpredictions\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0mlabels\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mpredicted_class_indices\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[0mfilenames\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtest_generator\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfilenames\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m results=pd.DataFrame({\"Category\":predictions,\n","\u001b[0;31mTypeError\u001b[0m: unhashable type: 'numpy.ndarray'"]}]},{"metadata":{"trusted":true},"cell_type":"code","source":"results.head()","execution_count":92,"outputs":[{"output_type":"error","ename":"NameError","evalue":"name 'results' is not defined","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)","\u001b[0;32m<ipython-input-92-0f9ba5b4964d>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mresults\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m","\u001b[0;31mNameError\u001b[0m: name 'results' is not defined"]}]},{"metadata":{"trusted":true},"cell_type":"code","source":"results.Category.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results.loc[:,'id'] = results.id.str.replace('0/','')\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"results.to_csv(\"submission.csv\",index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}