예제 #1
0
def features_pyramid(x, n_layers):
    """Generate features pyramid from the output of the 
    last layer of a backbone network (e.g. ResNetv1 or v2)

    Arguments:
        x (tensor): Output feature maps of a backbone network
        n_layers (int): Number of additional pyramid layers

    Return:
        outputs (list): Features pyramid 
    """
    outputs = [x]
    conv = AveragePooling2D(pool_size=2, name='pool1')(x)
    outputs.append(conv)
    prev_conv = conv
    n_filters = 512

    # additional feature map layers
    for i in range(n_layers - 1):
        postfix = "_layer" + str(i+2)
        conv = conv_layer(prev_conv,
                          n_filters,
                          kernel_size=3,
                          strides=2,
                          use_maxpool=False,
                          postfix=postfix)
        outputs.append(conv)
        prev_conv = conv

    return outputs
 def __init__(self, batch=1024, parallel=False, dropout=False):
     super(res_cnn, self).__init__()
     self.batch = batch
     self.conv = conv_layer()
     if parallel:
         self.conv = nn.DataParallel(self.conv)
     # self.linear = nn.Sequential(
     #        nn.Linear(256,256),
     #        nn.ReLU(inplace = True)
     #        )
     linearLayer = nn.ModuleList()
     linearLayer.append(nn.Linear(16384, 256))
     if dropout:
         linearLayer.append(nn.Dropout(0.5, inplace=True))
     linearLayer.append(nn.Linear(256, 11))
     if dropout:
         linearLayer.append(nn.Dropout(0.5, inplace=True))
     self.linearLayer = linearLayer
예제 #3
0
    def __init__(self, in_channels, n_layers):
        super(features_pyramid, self).__init__()

        pool_size = 2
        self.avg_pool = nn.AvgPool2d(pool_size)

        n_layers = n_layers
        n_filters = 512

        self.conv_layers = nn.ModuleList()
        for i in range(n_layers - 1):
            postfix = "_layer" + str(i + 2)
            layer = conv_layer(in_channels,
                               filters=n_filters,
                               kernel_size=3,
                               strides=2,
                               use_maxpool=False,
                               postfix=postfix)
            self.conv_layers.append(layer)
            in_channels = n_filters
예제 #4
0
def resnet_v1(input_shape, depth, num_classes=10):
    """ResNet Version 1 Model builder [a]

    Stacks of 2 x (3 x 3) Conv2D-BN-ReLU
    Last ReLU is after the shortcut connection.
    At the beginning of each stage, the feature map size is halved (downsampled)
    by a convolutional layer with strides=2, while the number of filters is
    doubled. Within each stage, the layers have the same number filters and the
    same number of filters.
    Features maps sizes:
    stage 0: 32x32, 16
    stage 1: 16x16, 32
    stage 2:  8x8,  64
    The Number of parameters is approx the same as Table 6 of [a]:
    ResNet20 0.27M
    ResNet32 0.46M
    ResNet44 0.66M
    ResNet56 0.85M
    ResNet110 1.7M

    # Arguments
        input_shape (tensor): Shape of input image tensor
        depth (int): Number of core convolutional layers
        num_classes (int): Number of classes (CIFAR10 has 10)

    # Returns
        model (Model): Keras model instance
    """
    if (depth - 2) % 6 != 0:
        raise ValueError('depth should be 6n+2 (eg 20, 32, 44 in [a])')
    # Start model definition.
    num_filters = 16
    num_res_blocks = int((depth - 2) / 6)

    inputs = Input(shape=input_shape)
    x = resnet_layer(inputs=inputs)
    # Instantiate the stack of residual units
    for stack in range(3):
        for res_block in range(num_res_blocks):
            strides = 1
            if stack > 0 and res_block == 0:  # first layer but not first stack
                strides = 2  # downsample
            y = resnet_layer(inputs=x,
                             num_filters=num_filters,
                             strides=strides)
            y = resnet_layer(inputs=y,
                             num_filters=num_filters,
                             activation=None)
            if stack > 0 and res_block == 0:  # first layer but not first stack
                # linear projection residual shortcut connection to match
                # changed dims
                x = resnet_layer(inputs=x,
                                 num_filters=num_filters,
                                 kernel_size=1,
                                 strides=strides,
                                 activation=None,
                                 batch_normalization=False)
            x = Add()([x, y])
            x = Activation('relu')(x)
        num_filters *= 2

    # 1st feature map layer
    conv = AveragePooling2D(pool_size=4, name='pool1')(x)

    outputs = [conv]
    prev_conv = conv
    n_filters = 64

    # additional feature map layers
    for i in range(n_layers - 1):
        postfix = "_layer" + str(i + 2)
        conv = conv_layer(prev_conv,
                          n_filters,
                          kernel_size=3,
                          strides=2,
                          use_maxpool=False,
                          postfix=postfix)
        outputs.append(conv)
        prev_conv = conv
        n_filters *= 2

    # instantiate model
    name = 'ResNet%dv1' % (depth)
    model = Model(inputs=inputs, outputs=outputs, name=name)
    return model
예제 #5
0
def resnet_v2(input_shape, depth, n_layers=4):
    """ResNet Version 2 Model builder [b]

    Stacks of (1 x 1)-(3 x 3)-(1 x 1) BN-ReLU-Conv2D or also known as
    bottleneck layer
    First shortcut connection per layer is 1 x 1 Conv2D.
    Second and onwards shortcut connection is identity.
    At the beginning of each stage, the feature map size is halved (downsampled)
    by a convolutional layer with strides=2, while the number of filter maps is
    doubled. Within each stage, the layers have the same number filters and the
    same filter map sizes.
    Features maps sizes:
    conv1  : 32x32,  16
    stage 0: 32x32,  64
    stage 1: 16x16, 128
    stage 2:  8x8,  256

    # Arguments
        input_shape (tensor): Shape of input image tensor
        depth (int): Number of core convolutional layers
        num_classes (int): Number of classes (CIFAR10 has 10)

    # Returns
        model (Model): Keras model instance
    """
    if (depth - 2) % 9 != 0:
        raise ValueError('depth should be 9n+2 (eg 56 or 110 in [b])')
    # Start model definition.
    num_filters_in = 16
    num_res_blocks = int((depth - 2) / 9)

    inputs = Input(shape=input_shape)
    # v2 performs Conv2D with BN-ReLU on input before splitting into 2 paths
    x = resnet_layer(inputs=inputs,
                     num_filters=num_filters_in,
                     conv_first=True)

    # Instantiate the stack of residual units
    for stage in range(3):
        for res_block in range(num_res_blocks):
            activation = 'relu'
            batch_normalization = True
            strides = 1
            if stage == 0:
                num_filters_out = num_filters_in * 4
                if res_block == 0:  # first layer and first stage
                    activation = None
                    batch_normalization = False
            else:
                num_filters_out = num_filters_in * 2
                if res_block == 0:  # first layer but not first stage
                    strides = 2  # downsample

            # bottleneck residual unit
            y = resnet_layer(inputs=x,
                             num_filters=num_filters_in,
                             kernel_size=1,
                             strides=strides,
                             activation=activation,
                             batch_normalization=batch_normalization,
                             conv_first=False)
            y = resnet_layer(inputs=y,
                             num_filters=num_filters_in,
                             conv_first=False)
            y = resnet_layer(inputs=y,
                             num_filters=num_filters_out,
                             kernel_size=1,
                             conv_first=False)
            if res_block == 0:
                # linear projection residual shortcut connection to match
                # changed dims
                x = resnet_layer(inputs=x,
                                 num_filters=num_filters_out,
                                 kernel_size=1,
                                 strides=strides,
                                 activation=None,
                                 batch_normalization=False)
            x = Add()([x, y])

        num_filters_in = num_filters_out

    # v2 has BN-ReLU before Pooling
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    # 1st feature map layer
    conv = AveragePooling2D(pool_size=4, name='pool1')(x)
    outputs = [conv]
    prev_conv = conv
    n_filters = 64

    # additional feature map layers
    for i in range(n_layers - 1):
        postfix = "_layer" + str(i + 2)
        conv = conv_layer(prev_conv,
                          n_filters,
                          kernel_size=3,
                          strides=2,
                          use_maxpool=False,
                          postfix=postfix)
        outputs.append(conv)
        prev_conv = conv
        n_filters *= 2

    # instantiate model.
    name = 'ResNet%dv2' % (depth)
    model = Model(inputs=inputs, outputs=outputs, name=name)
    return model
예제 #6
0
파일: train.py 프로젝트: lzb863/Fast-SCNN-2
import os
import tensorflow as tf
import numpy as np
from model import conv_layer, bottelneck, ppm
from new_datagen import DataGen

NO_OF_EPOCHS = 1000
BATCH_SIZE = 2
SAMPLES = 2975
VAL_SAMPLES = 500

# input image
input_layer = tf.keras.layers.Input(shape=(256, 512, 3))
# learning to down sample
lds_layer = conv_layer(input_layer, 'conv', 32, (3, 3),
                       (2, 2))  # size = (1024, 512, 32)
lds_layer = conv_layer(lds_layer, 'ds', 48, (3, 3),
                       (2, 2))  # size = (512, 256, 48)
lds_layer = conv_layer(lds_layer, 'ds', 64, (3, 3),
                       (2, 2))  # size = (256, 128, 64)
# global feature extractor
gfe_layer = bottelneck(lds_layer, 64, (3, 3), 6, 3,
                       (2, 2))  # size = (128, 64, 128)
gfe_layer = bottelneck(gfe_layer, 96, (3, 3), 6, 3,
                       (2, 2))  # size = (128, 64, 128)
gfe_layer = bottelneck(gfe_layer, 128, (3, 3), 6, 3,
                       (1, 1))  # size = (128, 64, 128)
ppm_layer = ppm(gfe_layer, [2, 4, 6, 8])
# Feature Fusion
ff_layer1 = conv_layer(
    lds_layer, 'conv', 128, (1, 1), (1, 1), relu=False