def make_encode_decode_net(inputs,
                           num_classes,
                           n_blocks,
                           base=32,
                           filter_list=None):
    """Flexible semantic segmentation architecture using seperable convolutions"""

    if filter_list is None:
        filter_list = list()
        for i in range(n_blocks):
            filter_list.append(base * (2**i))

    x0 = l.Conv2D(base, 3, activation='relu', padding='same')(inputs)
    x0 = l.BatchNormalization()(x0)

    encode_layers = list()
    for i in range(n_blocks):
        if i == 0:
            encode_layers.append(encode_block(x0, filter_list[i]))
        else:
            encode_layers.append(
                encode_block(encode_layers[i - 1], filter_list[i]))

    encode_layers = [x0] + encode_layers
    encode_layers = list(reversed(encode_layers))
    filter_list = list(reversed(filter_list))

    for e, layer in enumerate(encode_layers[1:]):
        if e == 0:
            x = decode_block(encode_layers[0], layer, filter_list[e])
        else:
            x = decode_block(x, layer, filter_list[e])

    return l.Conv2D(num_classes, 3, activation='sigmoid', padding='same')(x)
Example #2
0
def conv1d(x,
           filters,
           kernel_size=3,
           stride=1,
           padding='same',
           activation_=None,
           is_training=True):
    """

    :param x: (bs, length, channel)
    :param filters: int
    :param kernel_size: int
    :param stride: int
    :param padding: same or valid
    :param activation_:
    :param is_training:
    :return: (bs, length, new_channel)
    """
    _x = tf.expand_dims(x, axis=2)
    _x = activation(
        kl.Conv2D(filters, (kernel_size, 1), (stride, 1),
                  padding,
                  activation=None,
                  trainable=is_training)(_x), activation_)
    _x = tf.squeeze(_x, axis=2)
    return _x
Example #3
0
def fcn_model(inputs, num_classes):

    # TODO Add Encoder Blocks.
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    encoded_layer1 = encoder_block(inputs, filters=16, strides=2)
    encoded_layer2 = encoder_block(encoded_layer1, filters=32, strides=2)
    encoded_layer3 = encoder_block(encoded_layer2, filters=64, strides=2)
    print(' | -> encoded_layer1:', encoded_layer1)
    print('  | -> encoded_layer2:', encoded_layer2)
    print('   | -> encoded_layer3:', encoded_layer3)

    # TODO Add 1x1 Convolution layer using conv2d_batchnorm().
    layer_1x1 = conv2d_batchnorm(encoded_layer3, 128, kernel_size=1, strides=1)
    print('    | -> layer_1x1:', layer_1x1)

    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks
    decoded_layer1 = decoder_block(layer_1x1, encoded_layer2, filters=64)
    decoded_layer2 = decoder_block(decoded_layer1, encoded_layer1, filters=32)
    decoded_layer3 = decoder_block(decoded_layer2, inputs, filters=16)
    print('   | -> decoded_layer1:', decoded_layer1)
    print('  | -> decoded_layer2:', decoded_layer2)
    print(' | -> decoded_layer3:', decoded_layer3)

    x = decoded_layer3

    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 1, activation='softmax',
                         padding='same')(x)
Example #4
0
def fcn_model(inputs, num_classes):

    # TODO Add Encoder Blocks.
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    # hint:  new_height = (input_height/S + 1
    #        new_width = (input_width)/S + 1

    # 128x128x3 -> 64x64x32
    encoder_1 = encoder_block(inputs, 32, 2)
    # 64x64x32 -> 32x32x64
    encoder_2 = encoder_block(encoder_1, 64, 2)
    # 32x32x64-> 16x16x128
    encoder_3 = encoder_block(encoder_2, 128, 2)
    # TODO Add 1x1 Convolution layer using conv2d_batchnorm().
    comprimed = conv2d_batchnorm(encoder_3, 128, kernel_size=1, strides=1)
    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks
    # 16x16x128 -> 32x32x128
    decoder_1 = decoder_block(comprimed, encoder_2, 128)
    # 32x32x128 -> 64x64x64
    decoder_2 = decoder_block(decoder_1, encoder_1, 64)
    # 64x64x3 -> 128x128x32
    decoder_3 = decoder_block(decoder_2, inputs, 32)

    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 3, activation='softmax',
                         padding='same')(decoder_3)
def fcn_model(inputs, num_classes):

    filters = 36
    strides = 2
    #Input size:

    # Encoder Blocks.
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.

    # Layer 1 size:
    layer_1 = encoder_block(inputs, filters, strides)
    # Layer 2 size:
    layer_2 = encoder_block(layer_1, filters * 2, strides)
    # Layer 3 size:
    layer_3 = encoder_block(layer_2, filters * 4, strides)

    # 1x1 Convolution layer using conv2d_batchnorm()
    # Convolution layer size:
    convolution_layer = conv2d_batchnorm(layer_3,
                                         filters * 16,
                                         kernel_size=1,
                                         strides=1)

    # Decoder Blocks
    # Layer 4 size:
    layer_4 = decoder_block(convolution_layer, layer_2, filters * 4)
    # Layer 5 size
    layer_5 = decoder_block(layer_4, layer_1, filters * 2)
    # Layer 6 size:
    layer_6 = decoder_block(layer_5, inputs, filters)

    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 1, activation='softmax',
                         padding='same')(layer_6)
Example #6
0
def fcn_model_best(inputs, num_classes):

    # TODO Add Encoder Blocks.
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    encod1 = encoder_block(inputs, 32, 2)
    encod2 = encoder_block(encod1, 64, 2)
    encod3 = encoder_block(encod2, 128, 2)
    encod4 = encoder_block(encod3, 256, 2)
    print('encod1 layer size',encod1.get_shape().as_list())
    print('encod2 layer size',encod2.get_shape().as_list())
    print('encod3 layer size',encod3.get_shape().as_list())
    print('encod4 layer size',encod4.get_shape().as_list())

    # TODO Add 1x1 Convolution layer using conv2d_batchnorm().
    conv_layer = conv2d_batchnorm(encod4, 256, kernel_size=1, strides=1)

    print('conv_layer layer size',conv_layer.get_shape().as_list())
    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks
    decod1 = decoder_block(conv_layer, encod3, 256)
    decod2 = decoder_block(decod1, encod2, 128)
    decod3 = decoder_block(decod2, encod1, 64)
    decod4 = decoder_block(decod3, inputs, 32)
    print('decoder1  layer size',decod1.get_shape().as_list())
    print('decoder2  layer size',decod2.get_shape().as_list())
    print('decoder3  layer size',decod3.get_shape().as_list())
    print('decoder4  layer size',decod4.get_shape().as_list())
    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 3, activation='softmax', padding='same')(decod4)
Example #7
0
def fcn_model(inputs, num_classes):

    # TODO Add Encoder Blocks.
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    filters=[16,32,64, 96, 128]
    strides=[2]
    outputs = [None]*(len(filters)+1)
    outputs[0] = inputs
    for i,filter in enumerate(filters):
        outputs[i+1] = encoder_block(outputs[i], filter, strides[0])
        print ('encoder_{} shape:{}'.format(i, outputs[i+1].get_shape().as_list()))

    # TODO Add 1x1 Convolution layer using conv2d_batchnorm().
    final_output = conv2d_batchnorm(outputs[-1], 128, kernel_size=1)
    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks

    # The advise was to concatenate the higher blocks not all.
    # 3rd and fifth block
    decoded_output = final_output
    reversed_arr = list(range(len(filters)))[::-1]
    for i in reversed_arr:
        print ('decoder_{}'.format(i))
        decoded_output = decoder_block(decoded_output, outputs[i], filters[i])

    x = decoded_output
    # x = decoder_block(decoded_output,inputs,filters[0])
    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 3, activation='softmax', padding='same')(x)
Example #8
0
def fcn_model_9layer(inputs, num_classes, filter_set):
    enc1_filter_num = filter_set[0]
    enc2_filter_num = filter_set[1]
    enc3_filter_num = filter_set[2]
    enc4_filter_num = filter_set[3]
    one_by_one_filter_num = filter_set[4]
    dec1_filter_num = filter_set[3]
    dec2_filter_num = filter_set[2]
    dec3_filter_num = filter_set[1]

    encoder_block1 = encoder_block(inputs, enc1_filter_num, strides=2)
    encoder_block2 = encoder_block(encoder_block1, enc2_filter_num, strides=2)
    encoder_block3 = encoder_block(encoder_block2, enc3_filter_num, strides=2)
    encoder_block4 = encoder_block(encoder_block3, enc4_filter_num, strides=2)

    one_by_one_conv = conv2d_batchnorm(encoder_block4,
                                       one_by_one_filter_num,
                                       kernel_size=1,
                                       strides=1)

    decoder_block1 = decoder_block(one_by_one_conv, encoder_block3,
                                   dec1_filter_num)
    decoder_block2 = decoder_block(decoder_block1, encoder_block2,
                                   dec2_filter_num)
    decoder_block3 = decoder_block(decoder_block2, encoder_block1,
                                   dec3_filter_num)
    x = decoder_block(decoder_block3, inputs, num_classes)

    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 1, activation='softmax',
                         padding='same')(x)
def fcn_model(inputs, num_classes):

    # 160x160x3 -> 80x80x32
    encoder_1 = encoder_block(inputs,32,2)
#    encoder_1 = encoder_block(encoder_1,32,1)
    # 80x80x32 -> 40x40x64
    encoder_2 = encoder_block(encoder_1,64,2)
#    encoder_2 = encoder_block(encoder_2,64,1)

    # 40x40x64-> 20x20x128
    encoder_3 = encoder_block(encoder_2,128,2)
#    encoder_3 = encoder_block(encoder_3,128,1)

    # TODO Add 1x1 Convolution layer using conv2d_batchnorm().
    comprimed = conv2d_batchnorm(encoder_3,256,kernel_size=1,strides=1)
    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks
    # 20x20x256 -> 40x40x128
    decoder_1 = decoder_block(comprimed,encoder_2,128)
    # 40x40x128 -> 80x80x64
    decoder_2 = decoder_block(decoder_1,encoder_1,64)
    # 80x80x64 -> 128x128x32
    decoder_3 = decoder_block(decoder_2,inputs,32)

    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 3, activation='softmax', padding='same')(decoder_3)
Example #10
0
def build_segmentation_network(config_dict):
    lists = process_maybe_lists(config_dict)

    n_blocks = config_dict['n_blocks_per_side']
    im_edge = config_dict['image_resolution']
    inputs = l.Input((im_edge, im_edge, 3))

    encode_blocks = list()
    x = conv2d_layer(inputs, lists[0][0], True)

    encode_blocks.append(x)
    for e, tup in enumerate(lists):
        if e < n_blocks:
            layer_fn = make_partial_layer(config_dict['convolution_type'],
                                          tup[0], tup[2])
            x = encode_block(x, layer_fn, config_dict['downsample_method'],
                             tup[1])
            encode_blocks.append(x)
        else:
            layer_fn = make_partial_layer(config_dict['convolution_type'],
                                          tup[0], tup[2])
            skip_layer = encode_blocks[n_blocks - e - 2]
            x = decode_block(x, skip_layer, layer_fn,
                             config_dict['upsample_method'], tup[1])

    x = l.Conv2D(config_dict['n_classes'],
                 1,
                 activation=config_dict['last_layer_activation'],
                 padding='same')(x)
    return x, inputs
Example #11
0
def fcn_model(inputs, num_classes):
    # print("num_classes", num_classes)
    # TODO Add Encoder Blocks.
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    encoder_block1 = encoder_block(inputs, 8, strides=2)
    encoder_block2 = encoder_block(encoder_block1, 16, strides=2)
    #     encoder_block3 = encoder_block(encoder_block2, 32, strides=2)
    #     encoder_block4 = encoder_block(encoder_block3, 64, strides=2)

    # TODO Add 1x1 Convolution layer using conv2d_batchnorm().
    one_by_one_conv = conv2d_batchnorm(encoder_block2,
                                       32,
                                       kernel_size=1,
                                       strides=1)

    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks
    decoder_block1 = decoder_block(one_by_one_conv, encoder_block1, 16)
    # print("done decode 1")
    #     decoder_block2 = decoder_block(decoder_block1, encoder_block2, 32)
    # print("done decode 2")
    #     decoder_block3 = decoder_block(decoder_block2, encoder_block1, 16)
    # print("done decode 3")
    x = decoder_block(decoder_block1, inputs, num_classes)

    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 1, activation='softmax',
                         padding='same')(x)
Example #12
0
def fcn_model(inputs, num_classes):
    # TODO Add Encoder Blocks.
    filters = 16
    keep_prob = 1
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    l1 = encoder_block(inputs, filters)
    l1 = keras.layers.core.Dropout(keep_prob)(l1)
    l2 = encoder_block(l1, filters * 2)
    l2 = keras.layers.core.Dropout(keep_prob)(l2)
    l3 = encoder_block(l2, filters * 3)
    l3 = keras.layers.core.Dropout(keep_prob)(l3)
    l4 = encoder_block(l3, filters * 4)

    fcn = conv2d_batchnorm(l4, filters * 4, strides=1, kernel=1)

    # decoder
    x = decoder_block(fcn, filters * 4)
    x = decoder_block(x, filters * 3)
    x = keras.layers.core.Dropout(keep_prob)(x)

    x = decoder_block(x, filters * 2)
    x = layers.concatenate([x, l1])
    x = keras.layers.core.Dropout(keep_prob)(x)

    x = decoder_block(x, filters)
    x = layers.concatenate([x, inputs])

    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 3, activation='softmax',
                         padding='same')(x)
Example #13
0
def conv2d_batchnorm(input_layer, filters, kernel_size=3, strides=1):
    output_layer = layers.Conv2D(filters=filters,
                                 kernel_size=kernel_size,
                                 strides=strides,
                                 padding='same',
                                 activation='relu')(input_layer)
    output_layer = layers.BatchNormalization()(output_layer)
    return output_layer
Example #14
0
def conv2d(x,
           filters,
           kernel_size=(3, 3),
           strides=(1, 1),
           padding='same',
           activation_=None):
    return activation(
        kl.Conv2D(filters, kernel_size, strides, padding, activation=None)(x),
        activation_)
Example #15
0
def fcn_model2(inputs, num_classes):

    # TODO Add Encoder Blocks.
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    encod1 = encoder_block(inputs, 2, 2)
    input_shape = inputs.get_shape().as_list()
    conv_layer = conv2d_batchnorm(encod1, 2, kernel_size=1, strides=1)
    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks
    decod1 = decoder_block(conv_layer, inputs, 2)

    return layers.Conv2D(num_classes, 3, activation='softmax', padding='same')(decod1)
Example #16
0
def fcn_model_3layer(inputs, num_classes, filter_set):
    enc1_filter_num = filter_set[0]
    one_by_one_filter_num = filter_set[1]

    encoder_block1 = encoder_block(inputs, enc1_filter_num, strides=2)
    one_by_one_conv = conv2d_batchnorm(encoder_block1,
                                       one_by_one_filter_num,
                                       kernel_size=1,
                                       strides=1)
    x = decoder_block(one_by_one_conv, inputs, num_classes)

    return layers.Conv2D(num_classes, 1, activation='softmax',
                         padding='same')(x)
Example #17
0
def fcn_model(inputs, num_classes):
    encode_layer_1 = encoder_block(inputs, 32, 2)
    encode_layer_2 = encoder_block(encode_layer_1, 64, 2)
    encode_layer_3 = encoder_block(encode_layer_2, 128, 2)
    convol_layer_1 = conv2d_batchnorm(encode_layer_3,
                                      256,
                                      kernel_size=1,
                                      strides=1)
    decode_layer_1 = decoder_block(convol_layer_1, encode_layer_2, 128)
    decode_layer_2 = decoder_block(decode_layer_1, encode_layer_1, 64)
    decode_layer_3 = decoder_block(decode_layer_2, inputs, 32)
    return layers.Conv2D(num_classes, 1, activation='softmax',
                         padding='same')(decode_layer_3)
Example #18
0
def conv2d(x,
           filters,
           kernel_size=(3, 3),
           strides=(1, 1),
           padding='same',
           activation_=None,
           is_training=True):
    return activation(
        kl.Conv2D(filters,
                  kernel_size,
                  strides,
                  padding,
                  activation=None,
                  trainable=is_training)(x), activation_)
def fcn_model(inputs, num_classes):

    # TODO Add Encoder Blocks.
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    en_L1 = encoder_block(inputs, 64, 2)
    en_L2 = encoder_block(en_L1, 128, 2)

    # TODO Add 1x1 Convolution layer using conv2d_batchnorm().
    conv1 = conv2d_batchnorm(en_L2, 256, 1, 1)

    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks
    dec_L1 = decoder_block(conv1, en_L1, 128)
    x = decoder_block(dec_L1, inputs, 64)

    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 3, activation='softmax',
                         padding='same')(x)
Example #20
0
def fcn_model(inputs, num_classes):
    # Add Encoder Blocks.
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    skip_0 = inputs
    x = encoder_block(inputs, 32, 2)
    skip_1 = x
    x = encoder_block(x, 64, 2)

    # Add 1x1 Convolution layer using conv2d_batchnorm().
    x = conv2d_batchnorm(x, 128, kernel_size=1, strides=1)

    # Add the same number of Decoder Blocks as the number of Encoder Blocks
    x = decoder_block(x, skip_1, 64)
    x = decoder_block(x, skip_0, 32)

    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 3, activation='softmax',
                         padding='same')(x)
def fcn_model(inputs, num_classes):
    
    # TODO Add Encoder Blocks. 
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    enc1 = encoder_block(inputs, filters=32, strides=2)
    enc2 = encoder_block(enc1, filters=64, strides=2)
    enc3 = encoder_block(enc2, filters=128, strides=2)

    # TODO Add 1x1 Convolution layer using conv2d_batchnorm().
    conv1x1 = conv2d_batchnorm(enc3, filters=64, kernel_size=1, strides=1)
    
    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks
    dec1 = decoder_block(conv1x1, enc2, filters=128)
    dec2 = decoder_block(dec1, enc1, filters=64)
    dec3 = decoder_block(dec2, inputs, filters=32)
        
    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 1, activation='softmax', padding='same')(dec3)
def fcn_model(inputs, num_classes, filters=32):
    logging.info('inputs : {}'.format(inputs.shape))
    # TODO Add Encoder Blocks.
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    input_layer1 = encoder_block(inputs, filters, 2)
    input_layer2 = encoder_block(input_layer1, filters * 2, 2)
    input_layer3 = encoder_block(input_layer2, filters * 4, 2)
    input_layer4 = encoder_block(input_layer3, filters * 8, 2)
    input_layer5 = encoder_block(input_layer4, filters * 16, 2)
    input_layer6 = encoder_block(input_layer5, filters * 32, 2)
    # print out
    logging.info('input_layer 1 : {}'.format(input_layer1.shape))
    logging.info('input_layer 2 : {}'.format(input_layer2.shape))
    logging.info('input_layer 3 : {}'.format(input_layer3.shape))
    logging.info('input_layer 4 : {}'.format(input_layer4.shape))
    logging.info('input_layer 5 : {}'.format(input_layer5.shape))
    logging.info('input_layer 6 : {}'.format(input_layer6.shape))

    # TODO Add 1x1 Convolution layer using conv2d_batchnorm().
    #    small_ip_layer = conv2d_batchnorm(input_layer5, filters*32, kernel_size=1, strides=1)
    small_ip_layer = conv2d_batchnorm(input_layer6,
                                      filters * 64,
                                      kernel_size=1,
                                      strides=1)
    logging.info('small_ip_layer : {}'.format(small_ip_layer.shape))

    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks
    output_layer6 = decoder_block(small_ip_layer, input_layer5, filters * 32)
    logging.info('output_layer 6: {}'.format(output_layer6.shape))
    output_layer5 = decoder_block(output_layer6, input_layer4, filters * 16)
    #    output_layer5 = decoder_block(small_ip_layer, input_layer4, filters*16)
    logging.info('output_layer 5: {}'.format(output_layer5.shape))
    output_layer4 = decoder_block(output_layer5, input_layer3, filters * 8)
    logging.info('output_layer 4: {}'.format(output_layer4.shape))
    output_layer3 = decoder_block(output_layer4, input_layer2, filters * 4)
    logging.info('output_layer 3: {}'.format(output_layer3.shape))
    output_layer2 = decoder_block(output_layer3, input_layer1, filters * 2)
    logging.info('output_layer 2: {}'.format(output_layer2.shape))
    output_layer1 = decoder_block(output_layer2, None, 3)
    logging.info('output_layer 1: {}'.format(output_layer1.shape))

    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 1, activation='softmax',
                         padding='same')(output_layer1)
Example #23
0
def fcn_model(inputs, num_classes):
    filters = 64
    strides = 2
    # TODO Add Encoder Blocks.
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    encoder_layer_1 = encoder_block(inputs, filters, strides)
    encoder_layer_2 = encoder_block(encoder_layer_1, 2 * filters, strides)
    encoder_layer_3 = encoder_block(encoder_layer_2, 4 * filters, strides)

    # TODO Add 1x1 Convolution layer using conv2d_batchnorm().
    conv2D_layer = conv2d_batchnorm(encoder_layer_3,
                                    4 * filters,
                                    kernel_size=1,
                                    strides=1)

    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks
    decoder_layer_1 = decoder_block(conv2D_layer, encoder_layer_2, 4 * filters)
    decoder_layer_2 = decoder_block(decoder_layer_1, encoder_layer_1,
                                    2 * filters)
    decoder_layer_3 = decoder_block(decoder_layer_2, inputs, filters)

    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    outputs = layers.Conv2D(num_classes,
                            1,
                            activation='softmax',
                            padding='same')(decoder_layer_3)

    # Print network shapes
    print(inputs)
    print(encoder_layer_1)
    print(encoder_layer_2)
    print(encoder_layer_3)
    print(conv2D_layer)
    print(decoder_layer_1)
    print(decoder_layer_2)
    print(decoder_layer_3)
    print(outputs)

    return outputs
def fcn_model(inputs, num_classes, layers_num, external_features,
              internal_features, conv_layers_num, conv_features):

    v = inputs

    layer_list = []
    for _ in range(layers_num):
        layer_list.append(v)

        if v == input:
            v = encoder_block(v, external_features, 2)
        else:
            v = encoder_block(v, internal_features, 2)

    for _ in range(conv_layers_num):
        v = conv2d_batchnorm(v, conv_features)

    for _ in range(layers_num):
        v = decoder_block(v, layer_list.pop(), internal_features)

    return layers.Conv2D(num_classes, 3, activation='softmax',
                         padding='same')(v)
Example #25
0
def conv2d_layer(x, filters, kernel_size=3, strides=1, batch_norm=True):
    x = l.Conv2D(filters, kernel_size, strides=strides, padding='same')(x)
    if batch_norm:
        x = l.BatchNormalization()(x)
    return x
def fcn_model(inputs, num_classes, depth_=32, keepProb=0.6):
    # TODO Add Encoder Blocks.
    # Remember that with each encoder layer, the depth of your model (the number of filters) increases.
    '''
    Basic encode/decode
    
    x0 = encoder_block(inputs, depth_, 2)
    x1 = encoder_block(x0, depth_*2, 2)
    
    #x1_2 = conv2d_batchnorm(x1, depth_*4, kernel_size=1, strides=1)
    
    x2 = encoder_block(x1, depth_*4, 2)
    #x3 = encoder_block(x2, 256, 2)
    
    # TODO Add 1x1 Convolution layer using conv2d_batchnorm().
    x3 = conv2d_batchnorm(x2, depth_*8, kernel_size=1, strides=1)
    
    #x3=layers.Dropout(keepProb)(x3)
    
    
    # TODO: Add the same number of Decoder Blocks as the number of Encoder Blocks
    #x3 = decoder_block(x4, x2, 256)
    x2 = decoder_block(x3, x1, depth_*4)
    x1 = decoder_block(x2, x0, depth_*2)
    x = decoder_block(x1, inputs, depth_)
    
    #x=layers.Dropout(keepProb)(x)
    inputs=x
    '''
    '''
    # Encode 1
    x0 = encoder_block(inputs, depth_, 2)
    # Encode 2
    x1 = encoder_block(x0, depth_*2, 2)
    
    # Encode 2.1
    x1_2 = conv2d_batchnorm(x1, depth_*4, kernel_size=1, strides=1)
    
    # Encode 3
    x2 = encoder_block(x1_2, depth_*4, 2)
    
    # Encode 4
    x3 = encoder_block(x2, depth_*8, 2)
    
    # Fully connected
    x4 = conv2d_batchnorm(x3, depth_*16, kernel_size=1, strides=1)
    
    # Decode 1
    x3 = decoder_block(x4, x2, depth_*8)
    # Decode 2
    x2 = decoder_block(x3, x1, depth_*4)
    # Decode 3
    x1 = decoder_block(x2, x0, depth_*2)
    # Decode 4
    x = decoder_block(x1, inputs, depth_)
    
    
    # Decode 4 - update variable
    inputs=x
    
    # Encode 1
    x0 = encoder_block(inputs, depth_, 2)
    # Encode 2
    x1 = encoder_block(x0, depth_*2, 2)
    # Encode 3
    x2 = encoder_block(x1, depth_*4, 2)
    

    # Fully connected
    x3 = conv2d_batchnorm(x2, depth_*8, kernel_size=1, strides=1)
    
    # Decode 1
    x2 = decoder_block(x3, x1_2, depth_*4)
    # Decode 2
    x1 = decoder_block(x2, x0, depth_*2)
    # Decode 3
    x = decoder_block(x1, inputs, depth_)
    '''

    # Architecture bellow
    # https://arxiv.org/abs/1708.01692

    # Encode 1
    x0 = encoder_block(inputs, depth_, 2)
    # Full conn - 1.1
    x0_2 = conv2d_batchnorm(x0, depth_ * 2, kernel_size=1, strides=1)
    x0_3 = conv2d_batchnorm(x0_2, depth_ * 2, kernel_size=1, strides=1)

    # Encode 2
    x1 = encoder_block(x0_3, depth_ * 2, 2)
    # Full conn - 2.1
    x1_2 = conv2d_batchnorm(x1, depth_ * 2, kernel_size=1, strides=1)
    x1_3 = conv2d_batchnorm(x1_2, depth_ * 2, kernel_size=1, strides=1)

    # Encode 3
    x2 = encoder_block(x1_3, depth_ * 4, 2)
    # Full conn - 3.1
    x2_2 = conv2d_batchnorm(x2, depth_ * 4, kernel_size=1, strides=1)
    x2_3 = conv2d_batchnorm(x2_2, depth_ * 4, kernel_size=1, strides=1)

    # Encode 4
    x3 = encoder_block(x2_3, depth_ * 8, 2)
    # Full conn - 3.1
    x3_2 = conv2d_batchnorm(x3, depth_ * 8, kernel_size=1, strides=1)
    x3_3 = conv2d_batchnorm(x3_2, depth_ * 8, kernel_size=1, strides=1)

    # Encode 5
    x4 = encoder_block(x3_3, depth_ * 16, 2)
    # Full conn - 3.1
    x4_2 = conv2d_batchnorm(x4, depth_ * 16, kernel_size=1, strides=1)
    x4_3 = conv2d_batchnorm(x4_2, depth_ * 16, kernel_size=1, strides=1)
    x4_4 = conv2d_batchnorm(x4_3, depth_ * 16, kernel_size=1, strides=1)

    # Fully connected
    x5 = conv2d_batchnorm(x4_4, depth_ * 16, kernel_size=1, strides=1)
    # Full conn - 4.1
    x5_2 = conv2d_batchnorm(x5, depth_ * 16, kernel_size=1, strides=1)
    x5_3 = conv2d_batchnorm(x5_2, depth_ * 16, kernel_size=1, strides=1)
    x5_4 = conv2d_batchnorm(x5_3, depth_ * 16, kernel_size=1, strides=1)

    # Decode 1
    x4 = decoder_block(x5_4, x3_3, depth_ * 8)
    # Full conn - 1.1
    x4_2 = conv2d_batchnorm(x4, depth_ * 8, kernel_size=1, strides=1)
    x4_3 = conv2d_batchnorm(x4_2, depth_ * 8, kernel_size=1, strides=1)
    x4_4 = conv2d_batchnorm(x4_3, depth_ * 8, kernel_size=1, strides=1)

    # Decode 2
    x3 = decoder_block(x4_4, x2_3, depth_ * 4)
    # Full conn - 2.1
    x3_2 = conv2d_batchnorm(x3, depth_ * 4, kernel_size=1, strides=1)
    x3_3 = conv2d_batchnorm(x3_2, depth_ * 4, kernel_size=1, strides=1)
    x3_4 = conv2d_batchnorm(x3_3, depth_ * 4, kernel_size=1, strides=1)

    # Decode 3
    x2 = decoder_block(x3_4, x1_3, depth_ * 4)
    # Full conn - 3.1
    x2_2 = conv2d_batchnorm(x2, depth_ * 2, kernel_size=1, strides=1)
    x2_3 = conv2d_batchnorm(x2_2, depth_ * 2, kernel_size=1, strides=1)
    x2_4 = conv2d_batchnorm(x2_3, depth_ * 2, kernel_size=1, strides=1)

    # Decode 4
    x2 = decoder_block(x2_4, x0_3, depth_ * 2)

    # Decode 5
    x = decoder_block(x2, inputs, depth_)

    ## How to use encode/decode

    #x = encode_decode(inputs, 3, depth_=depth_, keepProb=keepProb)
    #x = encode_decode(x, 3, depth_=depth_, keepProb=keepProb)
    #x = encode_decode(x, 3, depth_=depth_, keepProb=keepProb)

    # The function returns the output layer of your model. "x" is the final layer obtained from the last decoder_block()
    return layers.Conv2D(num_classes, 1, activation='softmax',
                         padding='same')(x)
Example #27
0
  With default values, it returns element-wise `max(x, 0)`

  '  Arguments:\n',
  '      x: A tensor or variable.\n',
  '      alpha: A scalar, slope of negative section (default=`0.`).\n',
  '      max_value: Saturation threshold.\n',

  '  Returns:\n',
  '      A tensor.\n',
"""

# create Conv2D tensor placeholder with input tensor
conv2d_tensor = layers.Conv2D(
	filters=2,
	kernel_size=(3, 3),
	activation='relu',
	padding='same',
	data_format='channels_last',
	# data_format='channels_first', # .keras/keras.json is determined
	name='block1_conv1')(input_tensor)

"""
~/.keras/keras.json
{
    "image_dim_ordering": "tf",
    "image_data_format": "channels_last",
    "epsilon": 1e-07,
    "floatx": "float32",
    "backend": "tensorflow"
}
"""