{"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\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        pass\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","execution":{"iopub.status.busy":"2021-06-08T12:06:13.066398Z","iopub.execute_input":"2021-06-08T12:06:13.066906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tensorflow\n!pip install tensorflow==2.0.0a0","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nfrom matplotlib import pyplot as plt\n%pylab inline\n\nimport os\nimport gc\nimport sys\nimport json\nimport glob\nimport random\nfrom pathlib import Path\nimport cv2\nimport itertools\nfrom tqdm import tqdm\nfrom imgaug import augmenters as iaa\nfrom sklearn.model_selection import StratifiedKFold, KFold\n\nDATA_DIR = Path('/kaggle/input')\nROOT_DIR = Path('/kaggle/working')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # pretrained weight\n# !wget http://mi.eng.cam.ac.uk/research/projects/VideoRec/CamSeq01/CamSeq01.zip","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# DeepLabV3 Model 재구성\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport tensorflow as tf\n\nfrom tensorflow.python.keras.models import Model\nfrom tensorflow.python.keras import layers\nfrom tensorflow.python.keras.layers import Input\nfrom tensorflow.python.keras.layers import Lambda\nfrom tensorflow.python.keras.layers import Activation\nfrom tensorflow.python.keras.layers import Concatenate\nfrom tensorflow.python.keras.layers import Add\nfrom tensorflow.python.keras.layers import Dropout\nfrom tensorflow.python.keras.layers import BatchNormalization\nfrom tensorflow.python.keras.layers import Conv2D\nfrom tensorflow.python.keras.layers import DepthwiseConv2D\nfrom tensorflow.python.keras.layers import ZeroPadding2D\nfrom tensorflow.python.keras.layers import GlobalAveragePooling2D\nfrom tensorflow.python.keras.utils.layer_utils import get_source_inputs\nfrom tensorflow.python.keras.utils.data_utils import get_file\nfrom tensorflow.python.keras import backend as K\nfrom tensorflow.python.keras.applications.imagenet_utils import preprocess_input\n\n# pretrained-weight\nWEIGHTS_PATH_X = \"https://github.com/bonlime/keras-deeplab-v3-plus/releases/download/1.1/deeplabv3_xception_tf_dim_ordering_tf_kernels.h5\"\nWEIGHTS_PATH_MOBILE = \"https://github.com/bonlime/keras-deeplab-v3-plus/releases/download/1.1/deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels.h5\"\nWEIGHTS_PATH_X_CS = \"https://github.com/bonlime/keras-deeplab-v3-plus/releases/download/1.2/deeplabv3_xception_tf_dim_ordering_tf_kernels_cityscapes.h5\"\nWEIGHTS_PATH_MOBILE_CS = \"https://github.com/bonlime/keras-deeplab-v3-plus/releases/download/1.2/deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels_cityscapes.h5\"\n\n# Calculate the number of padding, whether hw needs to shrink\n\n# \"\"\" SepConv with BN between depthwise & pointwise. Optionally add activation after BN\n#         Implements right \"same\" padding for even kernel sizes\n#         Args:\n#             x: input tensor\n#             filters: num of filters in pointwise convolution\n#             prefix: prefix before name\n#             stride: stride at depthwise conv\n#             kernel_size: kernel size for depthwise convolution\n#             rate: atrous rate for depthwise convolution\n#             depth_activation: flag to use activation between depthwise & poinwise convs\n#             epsilon: epsilon to use in BN layer\n#     \"\"\"\n\n\ndef SepConv_BN(x, filters, prefix, stride=1, kernel_size=3, rate=1, depth_activation=False, epsilon=1e-3):\n\n    if stride == 1:\n        depth_padding = 'same'\n    else:\n        kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1)\n        pad_total = kernel_size_effective - 1\n        pad_beg = pad_total // 2\n        pad_end = pad_total - pad_beg\n        x = ZeroPadding2D((pad_beg, pad_end))(x)\n        depth_padding = 'valid'\n\n    if not depth_activation:\n        x = Activation(tf.nn.relu)(x)\n    x = DepthwiseConv2D((kernel_size, kernel_size), strides=(stride, stride), dilation_rate=(rate, rate),\n                        padding=depth_padding, use_bias=False, name=prefix + '_depthwise')(x)\n    x = BatchNormalization(name=prefix + '_depthwise_BN', epsilon=epsilon)(x)\n    if depth_activation:\n        x = Activation(tf.nn.relu)(x)\n    x = Conv2D(filters, (1, 1), padding='same',\n               use_bias=False, name=prefix + '_pointwise')(x)\n    x = BatchNormalization(name=prefix + '_pointwise_BN', epsilon=epsilon)(x)\n    if depth_activation:\n        x = Activation(tf.nn.relu)(x)\n\n    return x\n\n\n#   \"\"\"Implements right 'same' padding for even kernel sizes\n#         Without this there is a 1 pixel drift when stride = 2\n#         Args:\n#             x: input tensor\n#             filters: num of filters in pointwise convolution\n#             prefix: prefix before name\n#             stride: stride at depthwise conv\n#             kernel_size: kernel size for depthwise convolution\n#             rate: atrous rate for depthwise convolution\n#     \"\"\"\n\ndef _conv2d_same(x, filters, prefix, stride=1, kernel_size=3, rate=1):\n  \n    if stride == 1:\n        return Conv2D(filters,\n                      (kernel_size, kernel_size),\n                      strides=(stride, stride),\n                      padding='same', use_bias=False,\n                      dilation_rate=(rate, rate),\n                      name=prefix)(x)\n    else:\n        kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1)\n        pad_total = kernel_size_effective - 1\n        pad_beg = pad_total // 2\n        pad_end = pad_total - pad_beg\n        x = ZeroPadding2D((pad_beg, pad_end))(x)\n        return Conv2D(filters,\n                      (kernel_size, kernel_size),\n                      strides=(stride, stride),\n                      padding='valid', use_bias=False,\n                      dilation_rate=(rate, rate),\n                      name=prefix)(x)\n\n#     \"\"\" Basic building block of modified Xception network\n#         Args:\n#             inputs: input tensor\n#             depth_list: number of filters in each SepConv layer. len(depth_list) == 3\n#             prefix: prefix before name\n#             skip_connection_type: one of {'conv','sum','none'}\n#             stride: stride at last depthwise conv\n#             rate: atrous rate for depthwise convolution\n#             depth_activation: flag to use activation between depthwise & pointwise convs\n#             return_skip: flag to return additional tensor after 2 SepConvs for decoder\n#             \"\"\"\n    \n    \ndef _xception_block(inputs, depth_list, prefix, skip_connection_type, stride,\n                    rate=1, depth_activation=False, return_skip=False):\n\n    residual = inputs\n    for i in range(3):\n        residual = SepConv_BN(residual,\n                              depth_list[i],\n                              prefix + '_separable_conv{}'.format(i + 1),\n                              stride=stride if i == 2 else 1,\n                              rate=rate,\n                              depth_activation=depth_activation)\n        if i == 1:\n            skip = residual\n    if skip_connection_type == 'conv':\n        shortcut = _conv2d_same(inputs, depth_list[-1], prefix + '_shortcut',\n                                kernel_size=1,\n                                stride=stride)\n        shortcut = BatchNormalization(name=prefix + '_shortcut_BN')(shortcut)\n        outputs = layers.add([residual, shortcut])\n    elif skip_connection_type == 'sum':\n        outputs = layers.add([residual, inputs])\n    elif skip_connection_type == 'none':\n        outputs = residual\n    if return_skip:\n        return outputs, skip\n    else:\n        return outputs\n\n\ndef _make_divisible(v, divisor, min_value=None):\n    if min_value is None:\n        min_value = divisor\n    new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)\n    # Make sure that round down does not go down by more than 10%.\n    if new_v < 0.9 * v:\n        new_v += divisor\n    return new_v\n\n\ndef _inverted_res_block(inputs, expansion, stride, alpha, filters, block_id, skip_connection, rate=1):\n    in_channels = inputs.shape[-1] #.value  # inputs._keras_shape[-1]\n    pointwise_conv_filters = int(filters * alpha)\n    pointwise_filters = _make_divisible(pointwise_conv_filters, 8)\n    x = inputs\n    prefix = 'expanded_conv_{}_'.format(block_id)\n    if block_id:\n        # Expand\n\n        x = Conv2D(expansion * in_channels, kernel_size=1, padding='same',\n                   use_bias=False, activation=None,\n                   name=prefix + 'expand')(x)\n        x = BatchNormalization(epsilon=1e-3, momentum=0.999,\n                               name=prefix + 'expand_BN')(x)\n        x = Activation(tf.nn.relu6, name=prefix + 'expand_relu')(x)\n    else:\n        prefix = 'expanded_conv_'\n    # Depthwise\n    x = DepthwiseConv2D(kernel_size=3, strides=stride, activation=None,\n                        use_bias=False, padding='same', dilation_rate=(rate, rate),\n                        name=prefix + 'depthwise')(x)\n    x = BatchNormalization(epsilon=1e-3, momentum=0.999,\n                           name=prefix + 'depthwise_BN')(x)\n\n    x = Activation(tf.nn.relu6, name=prefix + 'depthwise_relu')(x)\n\n    # Project\n    x = Conv2D(pointwise_filters,\n               kernel_size=1, padding='same', use_bias=False, activation=None,\n               name=prefix + 'project')(x)\n    x = BatchNormalization(epsilon=1e-3, momentum=0.999,\n                           name=prefix + 'project_BN')(x)\n\n    if skip_connection:\n        return Add(name=prefix + 'add')([inputs, x])\n\n    # if in_channels == pointwise_filters and stride == 1:\n    #    return Add(name='res_connect_' + str(block_id))([inputs, x])\n\n    return x\n\n# weight 는 pre-trained\n#  \"\"\"\n#     # Arguments\n#         weights: one of 'pascal_voc' (pre-trained on pascal voc),\n#             'cityscapes' (pre-trained on cityscape) or None (random initialization)\n#         input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)\n#             to use as image input for the model.\n#         input_shape: shape of input image. format HxWxC\n#             PASCAL VOC model was trained on (512,512,3) images. None is allowed as shape/width\n#         classes: number of desired classes. \n#             If number of classes not aligned with the weights used, last layer is initialized randomly\n#         backbone: backbone to use. one of {'xception','mobilenetv2'}\n#         activation: optional activation to add to the top of the network.\n#             One of 'softmax', 'sigmoid' or None\n#         OS: determines input_shape/feature_extractor_output ratio. One of {8,16}.\n#             Used only for xception backbone.\n#         alpha: controls the width of the MobileNetV2 network. This is known as the\n#             width multiplier in the MobileNetV2 paper.\n#                 - If `alpha` < 1.0, proportionally decreases the number\n#                     of filters in each layer.\n#                 - If `alpha` > 1.0, proportionally increases the number\n#                     of filters in each layer.\n#                 - If `alpha` = 1, default number of filters from the paper\n#                     are used at each layer.\n#             Used only for mobilenetv2 backbone. Pretrained is only available for alpha=1.\n#     # Returns\n#         A Keras model instance.\n#     # Raises\n#         RuntimeError: If attempting to run this model with a\n#             backend that does not support separable convolutions.\n#         ValueError: in case of invalid argument for `weights` or `backbone`\n#     \"\"\"\n\ndef Deeplabv3(weights='pascal_voc', input_tensor=None, input_shape=(512, 512, 3), classes=21, backbone='mobilenetv2',\n              OS=16, alpha=1., activation=None):\n\n\n    if not (weights in {'pascal_voc', 'cityscapes', None}):\n        raise ValueError('The `weights` argument should be either '\n                         '`None` (random initialization), `pascal_voc`, or `cityscapes` '\n                         '(pre-trained on PASCAL VOC)')\n\n    if not (backbone in {'xception', 'mobilenetv2'}):\n        raise ValueError('The `backbone` argument should be either '\n                         '`xception`  or `mobilenetv2` ')\n\n    if input_tensor is None:\n        img_input = Input(shape=input_shape)\n    else:\n        img_input = input_tensor\n\n    if backbone == 'xception':\n        if OS == 8:\n            entry_block3_stride = 1\n            middle_block_rate = 2  # ! Not mentioned in paper, but required\n            exit_block_rates = (2, 4)\n            atrous_rates = (12, 24, 36)\n        else:\n            entry_block3_stride = 2\n            middle_block_rate = 1\n            exit_block_rates = (1, 2)\n            atrous_rates = (6, 12, 18)\n\n        x = Conv2D(32, (3, 3), strides=(2, 2),\n                   name='entry_flow_conv1_1', use_bias=False, padding='same')(img_input)\n        x = BatchNormalization(name='entry_flow_conv1_1_BN')(x)\n        x = Activation(tf.nn.relu)(x)\n\n        x = _conv2d_same(x, 64, 'entry_flow_conv1_2', kernel_size=3, stride=1)\n        x = BatchNormalization(name='entry_flow_conv1_2_BN')(x)\n        x = Activation(tf.nn.relu)(x)\n\n        x = _xception_block(x, [128, 128, 128], 'entry_flow_block1',\n                            skip_connection_type='conv', stride=2,\n                            depth_activation=False)\n        x, skip1 = _xception_block(x, [256, 256, 256], 'entry_flow_block2',\n                                   skip_connection_type='conv', stride=2,\n                                   depth_activation=False, return_skip=True)\n\n        x = _xception_block(x, [728, 728, 728], 'entry_flow_block3',\n                            skip_connection_type='conv', stride=entry_block3_stride,\n                            depth_activation=False)\n        for i in range(16):\n            x = _xception_block(x, [728, 728, 728], 'middle_flow_unit_{}'.format(i + 1),\n                                skip_connection_type='sum', stride=1, rate=middle_block_rate,\n                                depth_activation=False)\n\n        x = _xception_block(x, [728, 1024, 1024], 'exit_flow_block1',\n                            skip_connection_type='conv', stride=1, rate=exit_block_rates[0],\n                            depth_activation=False)\n        x = _xception_block(x, [1536, 1536, 2048], 'exit_flow_block2',\n                            skip_connection_type='none', stride=1, rate=exit_block_rates[1],\n                            depth_activation=True)\n\n    else:\n        OS = 8\n        first_block_filters = _make_divisible(32 * alpha, 8)\n        x = Conv2D(first_block_filters,\n                   kernel_size=3,\n                   strides=(2, 2), padding='same',\n                   use_bias=False, name='Conv')(img_input)\n        x = BatchNormalization(\n            epsilon=1e-3, momentum=0.999, name='Conv_BN')(x)\n        x = Activation(tf.nn.relu6, name='Conv_Relu6')(x)\n\n        x = _inverted_res_block(x, filters=16, alpha=alpha, stride=1,\n                                expansion=1, block_id=0, skip_connection=False)\n\n        x = _inverted_res_block(x, filters=24, alpha=alpha, stride=2,\n                                expansion=6, block_id=1, skip_connection=False)\n        x = _inverted_res_block(x, filters=24, alpha=alpha, stride=1,\n                                expansion=6, block_id=2, skip_connection=True)\n\n        x = _inverted_res_block(x, filters=32, alpha=alpha, stride=2,\n                                expansion=6, block_id=3, skip_connection=False)\n        x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1,\n                                expansion=6, block_id=4, skip_connection=True)\n        x = _inverted_res_block(x, filters=32, alpha=alpha, stride=1,\n                                expansion=6, block_id=5, skip_connection=True)\n\n        # stride in block 6 changed from 2 -> 1, so we need to use rate = 2\n        x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1,  # 1!\n                                expansion=6, block_id=6, skip_connection=False)\n        x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, rate=2,\n                                expansion=6, block_id=7, skip_connection=True)\n        x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, rate=2,\n                                expansion=6, block_id=8, skip_connection=True)\n        x = _inverted_res_block(x, filters=64, alpha=alpha, stride=1, rate=2,\n                                expansion=6, block_id=9, skip_connection=True)\n\n        x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, rate=2,\n                                expansion=6, block_id=10, skip_connection=False)\n        x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, rate=2,\n                                expansion=6, block_id=11, skip_connection=True)\n        x = _inverted_res_block(x, filters=96, alpha=alpha, stride=1, rate=2,\n                                expansion=6, block_id=12, skip_connection=True)\n\n        x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, rate=2,  # 1!\n                                expansion=6, block_id=13, skip_connection=False)\n        x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, rate=4,\n                                expansion=6, block_id=14, skip_connection=True)\n        x = _inverted_res_block(x, filters=160, alpha=alpha, stride=1, rate=4,\n                                expansion=6, block_id=15, skip_connection=True)\n\n        x = _inverted_res_block(x, filters=320, alpha=alpha, stride=1, rate=4,\n                                expansion=6, block_id=16, skip_connection=False)\n\n    # feature extractor 끝\n\n    #  Atrous Spatial Pyramid Pooling branch\n    # Image Feature branch\n    shape_before = tf.shape(x)\n    b4 = GlobalAveragePooling2D()(x)\n    # from (b_size, channels)->(b_size, 1, 1, channels)\n    b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4)\n    b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4)\n    b4 = Conv2D(256, (1, 1), padding='same',\n                use_bias=False, name='image_pooling')(b4)\n    b4 = BatchNormalization(name='image_pooling_BN', epsilon=1e-5)(b4)\n    b4 = Activation(tf.nn.relu)(b4)\n    \n    # upsampleing. have to use compat because of the option align_corners\n    size_before = tf.keras.backend.int_shape(x)\n    b4 = Lambda(lambda x: tf.compat.v1.image.resize(x, size_before[1:3],\n                                                    method='bilinear', align_corners=True))(b4)\n    # simple 1x1\n    b0 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='aspp0')(x)\n    b0 = BatchNormalization(name='aspp0_BN', epsilon=1e-5)(b0)\n    b0 = Activation(tf.nn.relu, name='aspp0_activation')(b0)\n\n    # 2 branches in mobilenetV2 \n    if backbone == 'xception':\n        # rate = 6 (12)\n        b1 = SepConv_BN(x, 256, 'aspp1',\n                        rate=atrous_rates[0], depth_activation=True, epsilon=1e-5)\n        # rate = 12 (24)\n        b2 = SepConv_BN(x, 256, 'aspp2',\n                        rate=atrous_rates[1], depth_activation=True, epsilon=1e-5)\n        # rate = 18 (36)\n        b3 = SepConv_BN(x, 256, 'aspp3',\n                        rate=atrous_rates[2], depth_activation=True, epsilon=1e-5)\n\n        # concatenate ASPP branches + project\n        x = Concatenate()([b4, b0, b1, b2, b3])\n    else:\n        x = Concatenate()([b4, b0])\n\n    x = Conv2D(256, (1, 1), padding='same',\n               use_bias=False, name='concat_projection')(x)\n    x = BatchNormalization(name='concat_projection_BN', epsilon=1e-5)(x)\n    x = Activation(tf.nn.relu)(x)\n    x = Dropout(0.1)(x)\n    # DeepLab v.3+ decoder\n    \n    # cross-channel correlation과 spatial correlation 매핑 완전 분리\n    if backbone == 'xception':\n        # Feature projection\n        # x4 (x2) block\n        skip_size = tf.keras.backend.int_shape(skip1)\n        x = Lambda(lambda xx: tf.compat.v1.image.resize(xx,\n                                                        skip_size[1:3],\n                                                        method='bilinear', align_corners=True))(x)\n\n        dec_skip1 = Conv2D(48, (1, 1), padding='same',\n                           use_bias=False, name='feature_projection0')(skip1)\n        dec_skip1 = BatchNormalization(\n            name='feature_projection0_BN', epsilon=1e-5)(dec_skip1)\n        dec_skip1 = Activation(tf.nn.relu)(dec_skip1)\n        x = Concatenate()([x, dec_skip1])\n        x = SepConv_BN(x, 256, 'decoder_conv0',\n                       depth_activation=True, epsilon=1e-5)\n        x = SepConv_BN(x, 256, 'decoder_conv1',\n                       depth_activation=True, epsilon=1e-5)\n\n    # you can use it with arbitary number of classes\n    if (weights == 'pascal_voc' and classes == 21) or (weights == 'cityscapes' and classes == 19) :\n        last_layer_name = 'logits_semantic'\n    else:\n        last_layer_name = 'custom_logits_semantic'\n\n    x = Conv2D(classes, (1, 1), padding='same', name=last_layer_name)(x)\n    size_before3 = tf.keras.backend.int_shape(img_input)\n    x = Lambda(lambda xx: tf.compat.v1.image.resize(xx,\n                                                    size_before3[1:3],\n                                                    method='bilinear', align_corners=True))(x)\n\n    # Ensure that the model takes into account\n    # any potential predecessors of `input_tensor`.\n    if input_tensor is not None:\n        inputs = get_source_inputs(input_tensor)\n    else:\n        inputs = img_input\n\n    if activation in {'softmax', 'sigmoid'}:\n        x = tf.keras.layers.Activation(activation)(x)\n\n    model = Model(inputs, x, name='deeplabv3plus')\n\n    # load weights\n\n    if weights == 'pascal_voc':\n        if backbone == 'xception':\n            weights_path = get_file('deeplabv3_xception_tf_dim_ordering_tf_kernels.h5',\n                                    WEIGHTS_PATH_X,\n                                    cache_subdir='models')\n        else:\n            weights_path = get_file('deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels.h5',\n                                    WEIGHTS_PATH_MOBILE,\n                                    cache_subdir='models')\n        model.load_weights(weights_path, by_name=True)\n    elif weights == 'cityscapes':\n        if backbone == 'xception':\n            weights_path = get_file('deeplabv3_xception_tf_dim_ordering_tf_kernels_cityscapes.h5',\n                                    WEIGHTS_PATH_X_CS,\n                                    cache_subdir='models')\n        else:\n            weights_path = get_file('deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels_cityscapes.h5',\n                                    WEIGHTS_PATH_MOBILE_CS,\n                                    cache_subdir='models')\n        model.load_weights(weights_path, by_name=True)\n    return model\n\ndef preprocess_input(x):\n#     \"\"\"Preprocesses a numpy array encoding a batch of images.\n#     # Arguments\n#         x: a 4D numpy array consists of RGB values within [0, 255].\n#     # Returns\n#         Input array scaled to [-1.,1.]\n#     \"\"\"\n    return preprocess_input(x, mode='tf')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# 파라미터 조정\ndeeplab_model = Deeplabv3()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# mask\ndef get_mask(image, model):\n    trained_image_width=512 \n    mean_subtraction_value=127.5\n\n    # add 3-th dimension if needed\n    if len(image.shape) == 2:\n        image = np.tile(image[..., None], (1, 1, 3))\n        \n    # training dataset image max demension resize \n    w, h, _ = image.shape\n    ratio = float(trained_image_width) / np.max([w, h])\n    resized_image = np.array(Image.fromarray(image.astype('uint8')).resize((int(ratio * h), int(ratio * w))))\n\n    # apply normalization for trained dataset images\n    resized_image = (resized_image / mean_subtraction_value) - 1.\n\n    # pad array to square image to match training images\n    pad_x = int(trained_image_width - resized_image.shape[0])\n    pad_y = int(trained_image_width - resized_image.shape[1])\n    resized_image = np.pad(resized_image, ((0, pad_x), (0, pad_y), (0, 0)), mode='constant')\n\n    # make prediction\n    res = model.predict(np.expand_dims(resized_image, 0))\n    labels = np.argmax(res.squeeze(), -1)\n\n    # remove padding and resize back to original image\n    if pad_x > 0:\n        labels = labels[:-pad_x]\n    if pad_y > 0:\n        labels = labels[:, :-pad_y]\n    labels = np.array(Image.fromarray(labels.astype('uint8')).resize((h, w)))\n    \n    return (labels == 15).astype('uint8')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n# samp_own dataset\nplt.figure(figsize=(15,10))\nimg = np.array(Image.open(\"../input/imaterialist-fashion-2019-FGVC6/train/000c9b4926cd78edd4c19cbc6beba111.jpg\"))\n# label = np.array(Image.open(\"../input/imaterialist-fashion-2019-FGVC6/train/0006ea84499fd9a06fefbdf47a5eb4c0.jpg\"))\n\nplt.subplot(1,3, 1)\nplt.imshow(img)\nplt.title(\"Image\")\n# plt.subplot(1,3, 2)\n# plt.imshow(label < 1)\n# plt.title(\"Label\")\nmask = get_mask(img, deeplab_model)\nplt.subplot(1,3, 3)\nplt.imshow(mask)\nplt.title(\"Predict\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"# xception 적용\ndeeplab_model = Deeplabv3(backbone='xception', OS=8)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# xception 적용 확인\nplt.figure(figsize=(15,10))\nimg = np.array(Image.open(\"../input/imaterialist-fashion-2019-FGVC6/train/001d7807a696231045860eaee97e825b.jpg\"))\n# label = np.array(Image.open(\"../input/imaterialist-fashion-2019-FGVC6/train/0006ea84499fd9a06fefbdf47a5eb4c0.jpg\"))\n\nplt.subplot(1,3, 1)\nplt.imshow(img)\nplt.title(\"Image\")\n# plt.subplot(1,3, 2)\n# plt.imshow(label < 1)\n# plt.title(\"Label\")\nmask = get_mask(img, deeplab_model)\nplt.subplot(1,3, 3)\nplt.imshow(mask)\nplt.title(\"Predict\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Training","metadata":{}},{"cell_type":"code","source":"from vision.references.detection import *\nimport vision.references.detection.utils","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"Train a DeepLab v3 model using tf.estimator API.\"\"\"\n\nfrom __future__ import absolute_import\nfrom __future__ import division\nfrom __future__ import print_function\n\nimport argparse\nimport os\nimport sys\n\nimport tensorflow as tf\n# import deeplab_model\nfrom utils import preprocessing\nfrom tensorflow.python import debug as tf_debug\n\nimport shutil\n\nparser = argparse.ArgumentParser()\n\nparser.add_argument('--model_dir', type=str, default='./model',\n                    help='Base directory for the model.')\n\nparser.add_argument('--clean_model_dir', action='store_true',\n                    help='Whether to clean up the model directory if present.')\n\nparser.add_argument('--train_epochs', type=int, default=26,\n                    help='Number of training epochs: '\n                         'For 30K iteration with batch size 6, train_epoch = 17.01 (= 30K * 6 / 10,582). '\n                         'For 30K iteration with batch size 8, train_epoch = 22.68 (= 30K * 8 / 10,582). '\n                         'For 30K iteration with batch size 10, train_epoch = 25.52 (= 30K * 10 / 10,582). '\n                         'For 30K iteration with batch size 11, train_epoch = 31.19 (= 30K * 11 / 10,582). '\n                         'For 30K iteration with batch size 15, train_epoch = 42.53 (= 30K * 15 / 10,582). '\n                         'For 30K iteration with batch size 16, train_epoch = 45.36 (= 30K * 16 / 10,582).')\n\nparser.add_argument('--epochs_per_eval', type=int, default=1,\n                    help='The number of training epochs to run between evaluations.')\n\nparser.add_argument('--tensorboard_images_max_outputs', type=int, default=6,\n                    help='Max number of batch elements to generate for Tensorboard.')\n\nparser.add_argument('--batch_size', type=int, default=10,\n                    help='Number of examples per batch.')\n\nparser.add_argument('--learning_rate_policy', type=str, default='poly',\n                    choices=['poly', 'piecewise'],\n                    help='Learning rate policy to optimize loss.')\n\nparser.add_argument('--max_iter', type=int, default=30000,\n                    help='Number of maximum iteration used for \"poly\" learning rate policy.')\n\nparser.add_argument('--data_dir', type=str, default='./dataset/',\n                    help='Path to the directory containing the PASCAL VOC data tf record.')\n\nparser.add_argument('--base_architecture', type=str, default='resnet_v2_101',\n                    choices=['resnet_v2_50', 'resnet_v2_101'],\n                    help='The architecture of base Resnet building block.')\n\nparser.add_argument('--pre_trained_model', type=str, default='./ini_checkpoints/resnet_v2_101/resnet_v2_101.ckpt',\n                    help='Path to the pre-trained model checkpoint.')\n\nparser.add_argument('--output_stride', type=int, default=16,\n                    choices=[8, 16],\n                    help='Output stride for DeepLab v3. Currently 8 or 16 is supported.')\n\nparser.add_argument('--freeze_batch_norm', action='store_true',\n                    help='Freeze batch normalization parameters during the training.')\n\nparser.add_argument('--initial_learning_rate', type=float, default=7e-3,\n                    help='Initial learning rate for the optimizer.')\n\nparser.add_argument('--end_learning_rate', type=float, default=1e-6,\n                    help='End learning rate for the optimizer.')\n\nparser.add_argument('--initial_global_step', type=int, default=0,\n                    help='Initial global step for controlling learning rate when fine-tuning model.')\n\nparser.add_argument('--weight_decay', type=float, default=2e-4,\n                    help='The weight decay to use for regularizing the model.')\n\nparser.add_argument('--debug', action='store_true',\n                    help='Whether to use debugger to track down bad values during training.')\n\n_NUM_CLASSES = 47\n_HEIGHT = 513\n_WIDTH = 513\n_DEPTH = 3\n_MIN_SCALE = 0.5\n_MAX_SCALE = 2.0\n_IGNORE_LABEL = 255\n\n_POWER = 0.9\n_MOMENTUM = 0.9\n\n_BATCH_NORM_DECAY = 0.9997\n\n_NUM_IMAGES = {\n    'train': 10582,\n    'validation': 1449,\n}\n\n\ndef get_filenames(is_training, data_dir):\n  \"\"\"Return a list of filenames.\n  Args:\n    is_training: A boolean denoting whether the input is for training.\n    data_dir: path to the the directory containing the input data.\n  Returns:\n    A list of file names.\n  \"\"\"\n  if is_training:\n    return [os.path.join(data_dir, 'voc_train.record')]\n  else:\n    return [os.path.join(data_dir, 'voc_val.record')]\n\n\ndef parse_record(raw_record):\n  \"\"\"Parse PASCAL image and label from a tf record.\"\"\"\n  keys_to_features = {\n      'image/height':\n      tf.FixedLenFeature((), tf.int64),\n      'image/width':\n      tf.FixedLenFeature((), tf.int64),\n      'image/encoded':\n      tf.FixedLenFeature((), tf.string, default_value=''),\n      'image/format':\n      tf.FixedLenFeature((), tf.string, default_value='jpeg'),\n      'label/encoded':\n      tf.FixedLenFeature((), tf.string, default_value=''),\n      'label/format':\n      tf.FixedLenFeature((), tf.string, default_value='png'),\n  }\n\n  parsed = tf.parse_single_example(raw_record, keys_to_features)\n\n  # height = tf.cast(parsed['image/height'], tf.int32)\n  # width = tf.cast(parsed['image/width'], tf.int32)\n\n  image = tf.image.decode_image(\n      tf.reshape(parsed['image/encoded'], shape=[]), _DEPTH)\n  image = tf.to_float(tf.image.convert_image_dtype(image, dtype=tf.uint8))\n  image.set_shape([None, None, 3])\n\n  label = tf.image.decode_image(\n      tf.reshape(parsed['label/encoded'], shape=[]), 1)\n  label = tf.to_int32(tf.image.convert_image_dtype(label, dtype=tf.uint8))\n  label.set_shape([None, None, 1])\n\n  return image, label\n\n\ndef preprocess_image(image, label, is_training):\n  \"\"\"Preprocess a single image of layout [height, width, depth].\"\"\"\n  if is_training:\n    # Randomly scale the image and label.\n    image, label = preprocessing.random_rescale_image_and_label(\n        image, label, _MIN_SCALE, _MAX_SCALE)\n\n    # Randomly crop or pad a [_HEIGHT, _WIDTH] section of the image and label.\n    image, label = preprocessing.random_crop_or_pad_image_and_label(\n        image, label, _HEIGHT, _WIDTH, _IGNORE_LABEL)\n\n    # Randomly flip the image and label horizontally.\n    image, label = preprocessing.random_flip_left_right_image_and_label(\n        image, label)\n\n    image.set_shape([_HEIGHT, _WIDTH, 3])\n    label.set_shape([_HEIGHT, _WIDTH, 1])\n\n  image = preprocessing.mean_image_subtraction(image)\n\n  return image, label\n\n\ndef input_fn(is_training, data_dir, batch_size, num_epochs=1):\n  \"\"\"Input_fn using the tf.data input pipeline for CIFAR-10 dataset.\n  Args:\n    is_training: A boolean denoting whether the input is for training.\n    data_dir: The directory containing the input data.\n    batch_size: The number of samples per batch.\n    num_epochs: The number of epochs to repeat the dataset.\n  Returns:\n    A tuple of images and labels.\n  \"\"\"\n  dataset = tf.data.Dataset.from_tensor_slices(get_filenames(is_training, data_dir))\n  dataset = dataset.flat_map(tf.data.TFRecordDataset)\n\n  if is_training:\n    # When choosing shuffle buffer sizes, larger sizes result in better\n    # randomness, while smaller sizes have better performance.\n    # is a relatively small dataset, we choose to shuffle the full epoch.\n    dataset = dataset.shuffle(buffer_size=_NUM_IMAGES['train'])\n\n  dataset = dataset.map(parse_record)\n  dataset = dataset.map(\n      lambda image, label: preprocess_image(image, label, is_training))\n  dataset = dataset.prefetch(batch_size)\n\n  # We call repeat after shuffling, rather than before, to prevent separate\n  # epochs from blending together.\n  dataset = dataset.repeat(num_epochs)\n  dataset = dataset.batch(batch_size)\n\n  iterator = dataset.make_one_shot_iterator()\n  images, labels = iterator.get_next()\n\n  return images, labels\n\n\ndef main(unused_argv):\n  # Using the Winograd non-fused algorithms provides a small performance boost.\n  os.environ['TF_ENABLE_WINOGRAD_NONFUSED'] = '1'\n\n  if FLAGS.clean_model_dir:\n    shutil.rmtree(FLAGS.model_dir, ignore_errors=True)\n\n  # Set up a RunConfig to only save checkpoints once per training cycle.\n  run_config = tf.estimator.RunConfig().replace(save_checkpoints_secs=1e9)\n  model = tf.estimator.Estimator(\n      model_fn=deeplab_model.deeplabv3_model_fn,\n      model_dir=FLAGS.model_dir,\n      config=run_config,\n      params={\n          'output_stride': FLAGS.output_stride,\n          'batch_size': FLAGS.batch_size,\n          'base_architecture': FLAGS.base_architecture,\n          'pre_trained_model': FLAGS.pre_trained_model,\n          'batch_norm_decay': _BATCH_NORM_DECAY,\n          'num_classes': _NUM_CLASSES,\n          'tensorboard_images_max_outputs': FLAGS.tensorboard_images_max_outputs,\n          'weight_decay': FLAGS.weight_decay,\n          'learning_rate_policy': FLAGS.learning_rate_policy,\n          'num_train': _NUM_IMAGES['train'],\n          'initial_learning_rate': FLAGS.initial_learning_rate,\n          'max_iter': FLAGS.max_iter,\n          'end_learning_rate': FLAGS.end_learning_rate,\n          'power': _POWER,\n          'momentum': _MOMENTUM,\n          'freeze_batch_norm': FLAGS.freeze_batch_norm,\n          'initial_global_step': FLAGS.initial_global_step\n      })\n\n  for _ in range(FLAGS.train_epochs // FLAGS.epochs_per_eval):\n    tensors_to_log = {\n      'learning_rate': 'learning_rate',\n      'cross_entropy': 'cross_entropy',\n      'train_px_accuracy': 'train_px_accuracy',\n      'train_mean_iou': 'train_mean_iou',\n    }\n\n    logging_hook = tf.train.LoggingTensorHook(\n        tensors=tensors_to_log, every_n_iter=10)\n    train_hooks = [logging_hook]\n    eval_hooks = None\n\n    if FLAGS.debug:\n      debug_hook = tf_debug.LocalCLIDebugHook()\n      train_hooks.append(debug_hook)\n      eval_hooks = [debug_hook]\n\n    tf.logging.info(\"Start training.\")\n    model.train(\n        input_fn=lambda: input_fn(True, FLAGS.data_dir, FLAGS.batch_size, FLAGS.epochs_per_eval),\n        hooks=train_hooks,\n        # steps=1  # For debug\n    )\n\n    tf.logging.info(\"Start evaluation.\")\n    # Evaluate the model and print results\n    eval_results = model.evaluate(\n        # Batch size must be 1 for testing because the images' size differs\n        input_fn=lambda: input_fn(False, FLAGS.data_dir, 1),\n        hooks=eval_hooks,\n        # steps=1  # For debug\n    )\n    print(eval_results)\n\n\nif __name__ == '__main__':\n  tf.logging.set_verbosity(tf.logging.INFO)\n  FLAGS, unparsed = parser.parse_known_args()\n  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('hi')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv('../input/imaterialist-fashion-2019-FGVC6/sample_submission.csv')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Encode Pixel\n# def mask_to_rle(mask):\n#     mask_flat = mask.flatten('F')\n#     flag = 0\n#     rle_list = list()\n#     for i in range(mask_flat.shape[0]):\n#         if flag == 0:\n#             if mask_flat[i] == 1:\n#                 flag = 1\n#                 starts = i+1\n#                 rle_list.append(starts)\n#         else:\n#             if mask_flat[i] == 0:\n#                 flag = 0\n#                 ends = i\n#                 rle_list.append(ends-starts+1)\n#     if flag == 1:\n#         ends = mask_flat.shape[0]\n#         rle_list.append(ends-starts+1)\n#     #sanity check\n#     if len(rle_list) % 2 != 0:\n#         print('NG')\n#     if len(rle_list) == 0:\n#         rle = np.nan\n#     else:\n#         rle = ' '.join(map(str,rle_list))\n#     return rle","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Test Prediction\n\n# submit_rle_arr = []\n\n# for img_id in sample_submission.ImageId.values:\n#     image = np.array(Image.open(f'../input/imaterialist-fashion-2019-FGVC6/test/{img_id}'))\n#     mask_out = get_mask(image, deeplab_model)\n#     rle = mask_to_rle(mask_out) \n#     submit_rle_arr.append(rle)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# sample_submission['EncodedPixels'] = submit_rle_arr\n# sample_submission.to_csv('submission.csv', index=False)\n# sample_submission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"####################################################","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ['PYTHONPATH'] += ':/kaggle/working/models/research/:/kaggle/working/models/research/slim/'","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":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}