예제 #1
0
    def _depthwise_conv_block(self,
                              inputs,
                              pointwise_conv_filters,
                              alpha,
                              depth_multiplier=1,
                              strides=(1, 1),
                              block_id=1):
        pointwise_conv_filters = int(pointwise_conv_filters * alpha)
        x = DepthwiseConv2D((3, 3),
                            padding='same',
                            depth_multiplier=depth_multiplier,
                            strides=strides,
                            use_bias=False,
                            name='student_conv_dw_%d' % block_id)(inputs)
        x = BatchNormalization(axis=-1,
                               name='student_conv_dw_%d_bn' % block_id)(x)
        x = Activation(self.relu6,
                       name='student_conv_dw_%d_relu' % block_id)(x)

        x = Conv2D(pointwise_conv_filters, (1, 1),
                   padding='same',
                   use_bias=False,
                   strides=(1, 1),
                   name='student_conv_pw_%d' % block_id)(x)
        x = BatchNormalization(axis=-1,
                               name='student_conv_pw_%d_bn' % block_id)(x)
        return Activation(self.relu6,
                          name='student_conv_pw_%d_relu' % block_id)(x)
예제 #2
0
def conv_block(input_tensor,
               kernel_size,
               filters,
               stage,
               block,
               strides=(2, 2)):
    """conv_block is the block that has a conv layer at shortcut.

  Arguments:
      input_tensor: input tensor
      kernel_size: default 3, the kernel size of middle conv layer at main path
      filters: list of integers, the filterss of 3 conv layer at main path
      stage: integer, current stage label, used for generating layer names
      block: 'a','b'..., current block label, used for generating layer names
      strides: Tuple of integers.

  Returns:
      Output tensor for the block.

  Note that from stage 3, the first conv layer at main path is with
  strides=(2,2)
  And the shortcut should have strides=(2,2) as well
  """
    filters1, filters2, filters3 = filters
    if K.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = Conv2D(filters1, (1, 1), strides=strides,
               name=conv_name_base + '2a')(input_tensor)
    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x)
    x = Activation('relu')(x)

    x = Conv2D(filters2,
               kernel_size,
               padding='same',
               name=conv_name_base + '2b')(x)
    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x)
    x = Activation('relu')(x)

    x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x)
    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x)

    shortcut = Conv2D(filters3, (1, 1),
                      strides=strides,
                      name=conv_name_base + '1')(input_tensor)
    shortcut = BatchNormalization(axis=bn_axis,
                                  name=bn_name_base + '1')(shortcut)

    x = layers.add([x, shortcut])
    x = Activation('relu')(x)
    return x
예제 #3
0
def _conv_block(inputs, filters, alpha, kernel=(3, 3), strides=(1, 1)):
    """Adds an initial convolution layer (with batch normalization and relu6).

  Arguments:
      inputs: Input tensor of shape `(rows, cols, 3)`
          (with `channels_last` data format) or
          (3, rows, cols) (with `channels_first` data format).
          It should have exactly 3 inputs channels,
          and width and height should be no smaller than 32.
          E.g. `(224, 224, 3)` would be one valid value.
      filters: Integer, the dimensionality of the output space
          (i.e. the number output of filters in the convolution).
      alpha: controls the width of the network.
          - If `alpha` < 1.0, proportionally decreases the number
              of filters in each layer.
          - If `alpha` > 1.0, proportionally increases the number
              of filters in each layer.
          - If `alpha` = 1, default number of filters from the paper
               are used at each layer.
      kernel: An integer or tuple/list of 2 integers, specifying the
          width and height of the 2D convolution window.
          Can be a single integer to specify the same value for
          all spatial dimensions.
      strides: An integer or tuple/list of 2 integers,
          specifying the strides of the convolution along the width and height.
          Can be a single integer to specify the same value for
          all spatial dimensions.
          Specifying any stride value != 1 is incompatible with specifying
          any `dilation_rate` value != 1.

  Input shape:
      4D tensor with shape:
      `(samples, channels, rows, cols)` if data_format='channels_first'
      or 4D tensor with shape:
      `(samples, rows, cols, channels)` if data_format='channels_last'.

  Output shape:
      4D tensor with shape:
      `(samples, filters, new_rows, new_cols)` if data_format='channels_first'
      or 4D tensor with shape:
      `(samples, new_rows, new_cols, filters)` if data_format='channels_last'.
      `rows` and `cols` values might have changed due to stride.

  Returns:
      Output tensor of block.
  """
    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
    filters = int(filters * alpha)
    x = Conv2D(filters,
               kernel,
               padding='same',
               use_bias=False,
               strides=strides,
               name='conv1')(inputs)
    x = BatchNormalization(axis=channel_axis, name='conv1_bn')(x)
    return Activation(relu6, name='conv1_relu')(x)
예제 #4
0
파일: resnet.py 프로젝트: lychahaha/pycode
    def identity_block(self, input, kernel, filters, stage, block):
        conv_name_base = 'res' + str(stage) + block + '_branch'
        bn_name_base = 'bn' + str(stage) + block + '_branch'

        x = input
        x = Conv3D(filters[0], (1, 1, 1), name=conv_name_base + '2a')(x)
        x = BatchNormalization(axis=4, name=bn_name_base + '2a')(x)
        x = Activation('relu')(x)

        x = Conv3D(filters[1],
                   kernel,
                   padding='same',
                   name=conv_name_base + '2b')(x)
        x = BatchNormalization(axis=4, name=bn_name_base + '2b')(x)
        x = Activation('relu')(x)

        x = Conv3D(filters[2], (1, 1, 1), name=conv_name_base + '2c')(x)
        x = BatchNormalization(axis=4, name=bn_name_base + '2c')(x)

        x = layers.add([x, input])
        x = Activation('relu')(x)
        return x
예제 #5
0
def identity_block(input_tensor, kernel_size, filters, stage, block):
    """The identity block is the block that has no conv layer at shortcut.

  Arguments:
      input_tensor: input tensor
      kernel_size: default 3, the kernel size of middle conv layer at main path
      filters: list of integers, the filterss of 3 conv layer at main path
      stage: integer, current stage label, used for generating layer names
      block: 'a','b'..., current block label, used for generating layer names

  Returns:
      Output tensor for the block.
  """
    filters1, filters2, filters3 = filters
    if K.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'

    x = Conv2D(filters1, (1, 1), name=conv_name_base + '2a')(input_tensor)
    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2a')(x)
    x = Activation('relu')(x)

    x = Conv2D(filters2,
               kernel_size,
               padding='same',
               name=conv_name_base + '2b')(x)
    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2b')(x)
    x = Activation('relu')(x)

    x = Conv2D(filters3, (1, 1), name=conv_name_base + '2c')(x)
    x = BatchNormalization(axis=bn_axis, name=bn_name_base + '2c')(x)

    x = layers.add([x, input_tensor])
    x = Activation('relu')(x)
    return x
예제 #6
0
파일: resnet.py 프로젝트: lychahaha/pycode
    def conv_block(self,
                   input,
                   kernel,
                   filters,
                   stage,
                   block,
                   strides=(2, 2, 2)):
        conv_name_base = 'res' + str(stage) + block + '_branch'
        bn_name_base = 'bn' + str(stage) + block + '_branch'

        x = input
        x = Conv3D(filters[0], (1, 1, 1),
                   strides=strides,
                   name=conv_name_base + '2a')(x)
        x = BatchNormalization(axis=4, name=bn_name_base + '2a')(x)
        x = Activation('relu')(x)

        x = Conv3D(filters[1],
                   kernel,
                   padding='same',
                   name=conv_name_base + '2b')(x)
        x = BatchNormalization(axis=4, name=bn_name_base + '2b')(x)
        x = Activation('relu')(x)

        x = Conv3D(filters[2], (1, 1, 1), name=conv_name_base + '2c')(x)
        x = BatchNormalization(axis=4, name=bn_name_base + '2c')(x)

        shortcut = input
        shortcut = Conv3D(filters[2], (1, 1, 1),
                          strides=strides,
                          name=conv_name_base + '1')(shortcut)
        shortcut = BatchNormalization(axis=3,
                                      name=bn_name_base + '1')(shortcut)

        x = layers.add([x, shortcut])
        x = Activation('relu')(x)
        return x
예제 #7
0
def generator_model():
    model = Sequential()
    model.add(Dense(1024,input_dim=100 ))
    model.add(Activation('tanh'))
    model.add(Dense(128*7*7))
    model.add(BatchNormalization())
    model.add(Activation('tanh'))
    model.add(Reshape((7, 7, 128), input_shape=(128*7*7,)))
    model.add(UpSampling2D(size=(2, 2)))
    model.add(Conv2D(64, (5, 5), padding='same'))
    model.add(Activation('tanh'))
    model.add(UpSampling2D(size=(2, 2)))
    model.add(Conv2D(1, (5, 5), padding='same'))
    model.add(Activation('tanh'))
    return model
    def build(self,alpha, img_input, temp_softmax):

        shape = (1, 1, int(1024 * alpha))
	"""
	This looks dangerous. Not sure how the model would get affected with the laarning_phase variable set to True.
	"""
        
        K.set_learning_phase(True)

	with tf.name_scope('teacher') as scope:

	    self.conv1 = Conv2D(
                        int(32*alpha),
                        (3,3),
                        padding='same',
                        use_bias=False,
                        strides=(1,1),
                        name='teacher_conv1', trainable=self.trainable)(img_input)
            self.conv2 = BatchNormalization(axis=-1, name='teacher_conv1_bn', trainable=self.trainable)(self.conv1)
            self.conv3 = Activation(self.relu6, name='teacher_conv1_relu', trainable=self.trainable)(self.conv2)

	    self.conv4 = self._depthwise_conv_block(self.conv3, 64, alpha, depth_multiplier, block_id = 15)
	    self.conv5 = self._depthwise_conv_block(self.conv4, 128, alpha, depth_multiplier,strides=(2, 2), block_id =16)
	    self.conv6 =self. _depthwise_conv_block(self.conv5, 128, alpha, depth_multiplier,block_id =17)
	    self.conv7 = self._depthwise_conv_block(self.conv6, 256, alpha, depth_multiplier, strides=(2,2),block_id =18)
	    self.conv8 = self._depthwise_conv_block(self.conv7, 256, alpha, depth_multiplier, block_id =19)
	    self.conv9 = self._depthwise_conv_block(self.conv8, 512, alpha, depth_multiplier, strides = (2,2), block_id =20)
	    self.conv10 = self._depthwise_conv_block(self.conv9, 512, alpha, depth_multiplier, block_id =21)
	    self.conv11 = self._depthwise_conv_block(self.conv10, 512, alpha, depth_multiplier, block_id =22)
	    self.conv12 = self._depthwise_conv_block(self.conv11, 512, alpha, depth_multiplier, block_id =23)
	    self.conv13 = self._depthwise_conv_block(self.conv12, 512, alpha, depth_multiplier, block_id =24)
	    self.conv14 = self._depthwise_conv_block(self.conv13, 512, alpha, depth_multiplier, block_id =25)
	    self.conv15 = self._depthwise_conv_block(self.conv14, 1024, alpha, depth_multiplier,strides=(2,2), block_id =26)
	    self.conv16 = self._depthwise_conv_block(self.conv15, 1024, alpha, depth_multiplier, block_id =27)

            self.conv17 = GlobalAveragePooling2D()(self.conv16)
            self.conv18 = Reshape(shape, name='teacher_reshape_1', trainable=self.trainable)(self.conv17)
	
            self.conv19 = Dropout(0.5, name='teacher_dropout', trainable=self.trainable)(self.conv18)
            self.conv20 = Conv2D(self.num_classes, (1, 1), padding='same', name='teacher_conv_preds', trainable=self.trainable)(self.conv18)
            self.conv21 = Activation('softmax', name='teacher_act_softmax', trainable=self.trainable)(tf.divide(self.conv20, temp_softmax))
            self.conv22 = Reshape((self.num_classes,), name='teacher_reshape_2', trainable=self.trainable)(self.conv21)

        return self
예제 #9
0
파일: resnet.py 프로젝트: lychahaha/pycode
    def split_block(self, x, split_name):
        split_name = '_' + split_name

        ##x = ZeroPadding2D((3,3))(x)
        x = Conv3D(16, (7, 3, 3), strides=(2, 2, 1),
                   name='conv1' + split_name)(x)
        x = BatchNormalization(axis=4, name='bn_conv1' + split_name)(x)
        x = Activation('relu')(x)
        x = MaxPooling3D((3, 2, 2), strides=(2, 2, 2))(x)

        x = self.conv_block(x,
                            3, [8, 8, 32],
                            stage=2,
                            block='a' + split_name,
                            strides=(1, 1, 1))
        x = self.identity_block(x,
                                3, [8, 8, 32],
                                stage=2,
                                block='b' + split_name)
        x = self.identity_block(x,
                                3, [8, 8, 32],
                                stage=2,
                                block='c' + split_name)

        x = self.conv_block(x,
                            3, [16, 16, 64],
                            stage=3,
                            block='a' + split_name)
        x = self.identity_block(x,
                                3, [16, 16, 64],
                                stage=3,
                                block='b' + split_name)
        x = self.identity_block(x,
                                3, [16, 16, 64],
                                stage=3,
                                block='c' + split_name)
        x = self.identity_block(x,
                                3, [16, 16, 64],
                                stage=3,
                                block='d' + split_name)

        return x
예제 #10
0
def conv2d_bn(x,
              filters,
              num_row,
              num_col,
              padding='same',
              strides=(1, 1),
              name=None):
  """Utility function to apply conv + BN.

  Arguments:
      x: input tensor.
      filters: filters in `Conv2D`.
      num_row: height of the convolution kernel.
      num_col: width of the convolution kernel.
      padding: padding mode in `Conv2D`.
      strides: strides in `Conv2D`.
      name: name of the ops; will become `name + '_conv'`
          for the convolution and `name + '_bn'` for the
          batch norm layer.

  Returns:
      Output tensor after applying `Conv2D` and `BatchNormalization`.
  """
  if name is not None:
    bn_name = name + '_bn'
    conv_name = name + '_conv'
  else:
    bn_name = None
    conv_name = None
  if K.image_data_format() == 'channels_first':
    bn_axis = 1
  else:
    bn_axis = 3
  x = Conv2D(
      filters, (num_row, num_col),
      strides=strides,
      padding=padding,
      use_bias=False,
      name=conv_name)(x)
  x = BatchNormalization(axis=bn_axis, scale=False, name=bn_name)(x)
  x = Activation('relu', name=name)(x)
  return x
예제 #11
0
def fcn_class(learning=True):
    model = fcn(2, learning=learning)
    model.load_weights('./fcn_weights_f2.h5')
    model.pop()
    model.pop()

    for layer in model.layers:
        layer.trainable = False

    kernel_size = 3
    act = 'relu'
    pad = 'same'
    model.add(
        Conv2DTranspose(7, kernel_size, strides=2, padding=pad,
                        activation=act))
    model.add(BatchNormalization())
    model.add(
        Conv2DTranspose(7, kernel_size, strides=1, padding=pad,
                        activation=act))
    model.add(Reshape((-1, 7)))
    return model
예제 #12
0
 def __init__(self, input_shape, lr=0.01, n_layers=2, n_hidden=8, rate_dropout=0.2, loss=risk_estimation):
     print("initializing..., learing rate %s, n_layers %s, n_hidden %s, dropout rate %s." % (
     lr, n_layers, n_hidden, rate_dropout))
     self.model = Sequential()
     self.model.add(Dropout(rate=rate_dropout, input_shape=(input_shape[0], input_shape[1])))
     for i in range(0, n_layers - 1):
         self.model.add(LSTM(n_hidden * 4, return_sequences=True, activation='tanh',
                             recurrent_activation='hard_sigmoid', kernel_initializer='glorot_uniform',
                             recurrent_initializer='orthogonal', bias_initializer='zeros',
                             dropout=rate_dropout, recurrent_dropout=rate_dropout))
     self.model.add(LSTM(n_hidden, return_sequences=False, activation='tanh',
                         recurrent_activation='hard_sigmoid', kernel_initializer='glorot_uniform',
                         recurrent_initializer='orthogonal', bias_initializer='zeros',
                         dropout=rate_dropout, recurrent_dropout=rate_dropout))
     self.model.add(Dense(1, kernel_initializer=initializers.glorot_uniform()))
     # self.model.add(BatchNormalization(axis=-1, moving_mean_initializer=Constant(value=0.5),
     #               moving_variance_initializer=Constant(value=0.25)))
     self.model.add(BatchNormalization(axis=-1))
     self.model.add(Activation("relu(alpha=0., max_value=1.0)"))
     opt = RMSprop(lr=lr)
     self.model.compile(loss=loss,
                        optimizer=opt,
                        metrics=['accuracy'])
예제 #13
0
def conv2d_bn(x,
              filters,
              kernel_size,
              strides=1,
              padding='same',
              activation='relu',
              use_bias=False,
              name=None):
    """Utility function to apply conv + BN.
    # Arguments
        x: input tensor.
        filters: filters in `Conv2D`.
        kernel_size: kernel size as in `Conv2D`.
        strides: strides in `Conv2D`.
        padding: padding mode in `Conv2D`.
        activation: activation in `Conv2D`.
        use_bias: whether to use a bias in `Conv2D`.
        name: name of the ops; will become `name + '_ac'` for the activation
            and `name + '_bn'` for the batch norm layer.
    # Returns
        Output tensor after applying `Conv2D` and `BatchNormalization`.
    """
    x = Conv2D(filters,
               kernel_size,
               strides=strides,
               padding=padding,
               use_bias=use_bias,
               name=name)(x)
    if not use_bias:
        bn_axis = 1 if K.image_data_format() == 'channels_first' else 3
        bn_name = None if name is None else name + '_bn'
        x = BatchNormalization(axis=bn_axis, scale=False, name=bn_name)(x)
    if activation is not None:
        ac_name = None if name is None else name + '_ac'
        x = Activation(activation, name=ac_name)(x)
    return x
예제 #14
0
def fcn(num_classes=2, learning=True) -> Sequential:
    K.set_learning_phase(learning)
    reg = tf.contrib.layers.l2_regularizer(1e-3)
    kernel_size = 3
    pad = 'same'
    act2 = 'relu'
    model = Sequential()
    model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(200, 400, 3)))
    model.add(
        Conv2D(32,
               kernel_size,
               1,
               padding=pad,
               activation=act2,
               kernel_regularizer=reg))
    model.add(
        Conv2D(32,
               kernel_size,
               1,
               padding=pad,
               activation=act2,
               kernel_regularizer=reg))
    model.add(MaxPooling2D((2, 2), 2))
    model.add(
        Conv2D(64,
               kernel_size,
               1,
               padding=pad,
               activation=act2,
               kernel_regularizer=reg))
    model.add(
        Conv2D(64,
               kernel_size,
               1,
               padding=pad,
               activation=act2,
               kernel_regularizer=reg))
    model.add(MaxPooling2D((2, 2), 2))
    model.add(
        Conv2D(128,
               kernel_size,
               1,
               padding=pad,
               activation=act2,
               kernel_regularizer=reg))
    model.add(
        Conv2D(128,
               kernel_size,
               1,
               padding=pad,
               activation=act2,
               kernel_regularizer=reg))
    model.add(MaxPooling2D((2, 2), 2))
    model.add(Conv2D(num_classes, 1, 1, padding=pad, kernel_regularizer=reg))
    model.add(
        Conv2DTranspose(num_classes,
                        kernel_size,
                        strides=2,
                        activation=act2,
                        padding=pad))
    model.add(BatchNormalization())
    model.add(
        Conv2DTranspose(num_classes,
                        kernel_size,
                        strides=2,
                        activation=act2,
                        padding=pad))
    model.add(BatchNormalization())
    model.add(
        Conv2DTranspose(num_classes,
                        kernel_size,
                        strides=2,
                        activation=act2,
                        padding=pad,
                        name=LOGITS))
    model.add(Reshape((-1, num_classes)))
    return model
- BatchNormalization layer output arrays on test and train mode

- Dropout layer output arrays on test and train mode
"""

from tensorflow.contrib.keras.python.keras.layers import Dropout, BatchNormalization, Input, Dense
from tensorflow.contrib.keras.python.keras.models import Model, Sequential, load_model
import numpy as np
from tensorflow.contrib.keras.python.keras import backend as K

input_array_small = np.random.random((500, 10)) * 2
target_small = np.random.random((500, 1))

input_tensor = Input(shape=(10, ))
bn_tensor = BatchNormalization()(input_tensor)
dp_tensor = Dropout(0.7)(input_tensor)

#### Access BatchNormalization layer's output as arrays in both test, train mode

# test mode from Model method
model_bn = Model(input_tensor, bn_tensor)
bn_array = model_bn.predict(input_array_small)

# test and train mode from K.function method
k_bn = K.function([input_tensor, K.learning_phase()], [bn_tensor])
bn_array_test = k_bn([input_array_small, 0])[0]
bn_array_train = k_bn([input_array_small, 1])[0]

# are test mode the same? and test mode array differ from train mode array
(bn_array == bn_array_test).sum()
예제 #16
0
    def build(self, rgb, num_classes, temp_softmax, train_mode):
        K.set_learning_phase(True)
        # conv1_1
        with tf.name_scope('mentor_conv1_1') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 3, 64],
                                                     dtype=tf.float32,
                                                     stddev=1e-2),
                                 trainable=self.trainable,
                                 name='mentor_weights')
            conv = tf.nn.conv2d(rgb, kernel, [1, 1, 1, 1], padding='SAME')
            biases = tf.Variable(tf.constant(0.0, shape=[64],
                                             dtype=tf.float32),
                                 trainable=self.trainable,
                                 name='mentor_biases')
            #	out = tf.nn.bias_add(conv, biases)
            #       mean , var = tf.nn.moments(out, axes= [0])
            #      out = (out - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.conv1_1 = tf.nn.relu(conv, name=scope)
            self.conv1_1 = BatchNormalization(axis=-1,
                                              name='mentor_bn_conv1_1')(
                                                  self.conv1_1)
            #self.conv1_1 = Dropout((0.4))(self.conv1_1)
            self.parameters += [kernel, biases]

        with tf.name_scope('mentor_conv1_2') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 64],
                                                     dtype=tf.float32,
                                                     stddev=1e-2),
                                 trainable=self.trainable,
                                 name='mentor_weights')
            conv = tf.nn.conv2d(self.conv1_1,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            biases = tf.Variable(tf.constant(0.0, shape=[64],
                                             dtype=tf.float32),
                                 trainable=self.trainable,
                                 name='mentor_biases')
            #out = tf.nn.bias_add(conv, biases)
            #mean , var = tf.nn.moments(out, axes= [0])
            #out = (out - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.conv1_2 = tf.nn.relu(conv, name=scope)
            self.conv1_2 = BatchNormalization(axis=-1,
                                              name='mentor_bn_conv1_2')(
                                                  self.conv1_2)
            self.parameters += [kernel, biases]

        self.pool1 = tf.nn.max_pool(self.conv1_2,
                                    ksize=[1, 2, 2, 1],
                                    strides=[1, 2, 2, 1],
                                    padding='SAME',
                                    name='mentor_pool1')
        with tf.name_scope('mentor_conv2_1') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 128],
                                                     dtype=tf.float32,
                                                     stddev=1e-2),
                                 trainable=self.trainable,
                                 name='mentor_weights')
            conv = tf.nn.conv2d(self.pool1,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[128],
                                             dtype=tf.float32),
                                 trainable=self.trainable,
                                 name='mentor_biases')
            #out = tf.nn.bias_add(conv, biases)
            #mean , var = tf.nn.moments(out, axes= [0])
            #out = (out - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.conv2_1 = tf.nn.relu(conv, name=scope)
            self.conv2_1 = BatchNormalization(axis=-1,
                                              name='mentor_bn_conv2_1')(
                                                  self.conv2_1)
            #self.conv2_1 = Dropout((0.4))(self.conv2_1)
            self.parameters += [kernel, biases]

        with tf.name_scope('mentor_conv2_2') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 128, 128],
                                                     dtype=tf.float32,
                                                     stddev=1e-2),
                                 trainable=self.trainable,
                                 name='mentor_weights')
            conv = tf.nn.conv2d(self.conv2_1,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[128],
                                             dtype=tf.float32),
                                 trainable=self.trainable,
                                 name='mentor_biases')
            #out = tf.nn.bias_add(conv, biases)
            #mean , var = tf.nn.moments(out, axes= [0])
            #out = (out - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.conv2_2 = tf.nn.relu(conv, name=scope)
            self.conv2_2 = BatchNormalization(axis=-1,
                                              name='mentor_bn_conv2_2')(
                                                  self.conv2_2)
            self.parameters += [kernel, biases]

        self.pool2 = tf.nn.max_pool(self.conv2_2,
                                    ksize=[1, 2, 2, 1],
                                    strides=[1, 2, 2, 1],
                                    padding='SAME',
                                    name='mentor_pool2')

        with tf.name_scope('mentor_conv3_1') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 128, 256],
                                                     dtype=tf.float32,
                                                     stddev=1e-2),
                                 trainable=self.trainable,
                                 name='mentor_weights')
            conv = tf.nn.conv2d(self.pool2,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[256],
                                             dtype=tf.float32),
                                 trainable=self.trainable,
                                 name='mentor_biases')
            #out = tf.nn.bias_add(conv, biases)
            # mean , var = tf.nn.moments(out, axes= [0])
            #out = (out - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.conv3_1 = tf.nn.relu(conv, name=scope)
            self.conv3_1 = BatchNormalization(axis=-1,
                                              name='mentor_bn_conv3_1')(
                                                  self.conv3_1)
            #self.conv3_1 = Dropout((0.4))(self.conv3_1)
            self.parameters += [kernel, biases]

        with tf.name_scope('mentor_conv3_2') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256],
                                                     dtype=tf.float32,
                                                     stddev=1e-2),
                                 trainable=self.trainable,
                                 name='mentor_weights')
            conv = tf.nn.conv2d(self.conv3_1,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[256],
                                             dtype=tf.float32),
                                 trainable=self.trainable,
                                 name='mentor_biases')
            #out = tf.nn.bias_add(conv, biases)
            #mean , var = tf.nn.moments(out, axes= [0])
            #out = (out - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.conv3_2 = tf.nn.relu(conv, name=scope)
            self.conv3_2 = BatchNormalization(axis=-1,
                                              name='mentor_bn_conv3_2')(
                                                  self.conv3_2)
            #self.conv3_2 = Dropout((0.4))(self.conv3_2)
            self.parameters += [kernel, biases]

        with tf.name_scope('mentor_conv3_3') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256],
                                                     dtype=tf.float32,
                                                     stddev=1e-2),
                                 trainable=self.trainable,
                                 name='mentor_weights')
            conv = tf.nn.conv2d(self.conv3_2,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[256],
                                             dtype=tf.float32),
                                 trainable=self.trainable,
                                 name='mentor_biases')
            #out = tf.nn.bias_add(conv, biases)
            #mean , var = tf.nn.moments(out, axes= [0])
            #out = (out - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.conv3_3 = tf.nn.relu(conv, name=scope)
            self.conv3_3 = BatchNormalization(axis=-1,
                                              name='mentor_bn_conv3_3')(
                                                  self.conv3_3)
            self.parameters += [kernel, biases]
        self.pool3 = tf.nn.max_pool(self.conv3_3,
                                    ksize=[1, 2, 2, 1],
                                    strides=[1, 2, 2, 1],
                                    padding='SAME',
                                    name='mentor_pool3')

        with tf.name_scope('mentor_conv4_1') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 512],
                                                     dtype=tf.float32,
                                                     stddev=1e-2),
                                 trainable=self.trainable,
                                 name='mentor_weights')
            conv = tf.nn.conv2d(self.pool3,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[512],
                                             dtype=tf.float32),
                                 trainable=self.trainable,
                                 name='mentor_biases')
            #out = tf.nn.bias_add(conv, biases)
            #mean , var = tf.nn.moments(out, axes= [0])
            #out = (out - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.conv4_1 = tf.nn.relu(conv, name=scope)
            self.conv4_1 = BatchNormalization(axis=-1,
                                              name='mentor_bn_conv4_1')(
                                                  self.conv4_1)
            #self.conv4_1 = Dropout((0.4))(self.conv4_1)
            self.parameters += [kernel, biases]

    # conv5_1
        with tf.name_scope('mentor_conv4_2') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512],
                                                     dtype=tf.float32,
                                                     stddev=1e-2),
                                 trainable=self.trainable,
                                 name='mentor_weights')
            conv = tf.nn.conv2d(self.conv4_1,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[512],
                                             dtype=tf.float32),
                                 trainable=self.trainable,
                                 name='mentor_biases')
            #out = tf.nn.bias_add(conv, biases)
            #mean , var = tf.nn.moments(out, axes= [0])
            #out = (out - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.conv4_2 = tf.nn.relu(conv, name=scope)
            self.conv4_2 = BatchNormalization(axis=-1,
                                              name='mentor_bn_conv4_2')(
                                                  self.conv4_2)
            #self.conv4_2 = Dropout((0.4))(self.conv4_2)
            self.parameters += [kernel, biases]

        with tf.name_scope('mentor_conv4_3') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512],
                                                     dtype=tf.float32,
                                                     stddev=1e-2),
                                 trainable=self.trainable,
                                 name='mentor_weights')
            conv = tf.nn.conv2d(self.conv4_2,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[512],
                                             dtype=tf.float32),
                                 trainable=self.trainable,
                                 name='mentor_biases')
            #out = tf.nn.bias_add(conv, biases)
            #mean , var = tf.nn.moments(out, axes= [0])
            #out = (out - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.conv4_3 = tf.nn.relu(conv, name=scope)
            self.conv4_3 = BatchNormalization(axis=-1,
                                              name='mentor_bn_conv4_3')(
                                                  self.conv4_3)
            self.parameters += [kernel, biases]
        self.pool4 = tf.nn.max_pool(self.conv4_3,
                                    ksize=[1, 2, 2, 1],
                                    strides=[1, 2, 2, 1],
                                    padding='SAME',
                                    name='mentor_pool4')
        with tf.name_scope('mentor_conv5_1') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512],
                                                     dtype=tf.float32,
                                                     stddev=1e-2),
                                 trainable=self.trainable,
                                 name='mentor_weights')
            conv = tf.nn.conv2d(self.pool4,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[512],
                                             dtype=tf.float32),
                                 trainable=self.trainable,
                                 name='mentor_biases')
            #out = tf.nn.bias_add(conv, biases)
            #mean , var = tf.nn.moments(out, axes= [0])
            #out = (out - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.conv5_1 = tf.nn.relu(conv, name=scope)
            self.conv5_1 = BatchNormalization(axis=-1,
                                              name='mentor_bn_conv5_1')(
                                                  self.conv5_1)
            #self.conv5_1 = Dropout((0.4))(self.conv5_1)
            self.parameters += [kernel, biases]
        with tf.name_scope('mentor_conv5_2') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512],
                                                     dtype=tf.float32,
                                                     stddev=1e-2),
                                 trainable=self.trainable,
                                 name='mentor_weights')
            conv = tf.nn.conv2d(self.conv5_1,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[512],
                                             dtype=tf.float32),
                                 trainable=self.trainable,
                                 name='mentor_biases')
            #out = tf.nn.bias_add(conv, biases)
            #mean , var = tf.nn.moments(out, axes= [0])
            #out = (out - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.conv5_2 = tf.nn.relu(conv, name=scope)
            self.conv5_2 = BatchNormalization(
                axis=-1, name='mentor_batch_norm_conv5_2')(self.conv5_2)
            #self.conv5_2 = Dropout((0.4))(self.conv5_2)
            self.parameters += [kernel, biases]

        with tf.name_scope('mentor_conv5_3') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512],
                                                     dtype=tf.float32,
                                                     stddev=1e-2),
                                 trainable=self.trainable,
                                 name='mentor_weights')
            conv = tf.nn.conv2d(self.conv5_2,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[512],
                                             dtype=tf.float32),
                                 trainable=self.trainable,
                                 name='mentor_biases')
            #out = tf.nn.bias_add(conv, biases)
            #mean , var = tf.nn.moments(out, axes= [0])
            #out = (out - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.conv5_3 = tf.nn.relu(conv, name=scope)
            self.conv5_3 = BatchNormalization(
                axis=-1, name='mentor_batch_norm_conv5_3')(self.conv5_3)
            self.parameters += [kernel, biases]

        self.pool5 = tf.nn.max_pool(self.conv5_3,
                                    ksize=[1, 2, 2, 1],
                                    strides=[1, 2, 2, 1],
                                    padding='SAME',
                                    name='mentor_pool5')
        # fc1
        with tf.name_scope('mentor_fc1') as scope:
            shape = int(np.prod(self.pool5.get_shape()[1:]))
            fc1w = tf.Variable(tf.truncated_normal([shape, 4096],
                                                   dtype=tf.float32,
                                                   stddev=1e-2),
                               trainable=self.trainable,
                               name='mentor_weights')
            fc1b = tf.Variable(tf.constant(1.0, shape=[4096],
                                           dtype=tf.float32),
                               trainable=self.trainable,
                               name='mentor_biases')
            pool5_flat = tf.reshape(self.pool5, [-1, shape])
            fc1l = tf.nn.bias_add(tf.matmul(pool5_flat, fc1w), fc1b)
            #mean , var = tf.nn.moments(fc1l, axes= [0])
            #fc1l = (fc1l - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.fc1 = tf.nn.relu(fc1l)
            self.fc1 = BatchNormalization(axis=-1,
                                          name='mentor_batch_norm_fc1')(
                                              self.fc1)
            # self.fc1 = Dropout((0.4))(self.fc1)
            #self.fc1 = tf.nn.dropout(self.fc1, 0.5)
            self.parameters += [fc1w, fc1b]

        with tf.name_scope('mentor_fc2') as scope:
            fc2w = tf.Variable(tf.truncated_normal([4096, 4096],
                                                   dtype=tf.float32,
                                                   stddev=1e-2),
                               trainable=self.trainable,
                               name='mentor_weights')
            fc2b = tf.Variable(tf.constant(1.0, shape=[4096],
                                           dtype=tf.float32),
                               trainable=self.trainable,
                               name='mentor_biases')
            fc2l = tf.nn.bias_add(tf.matmul(self.fc1, fc2w), fc2b)
            #mean , var = tf.nn.moments(fc2l, axes= [0])
            #fc2l = (fc2l - mean)/tf.sqrt(var + tf.Variable(1e-10))
            self.fc2 = tf.nn.relu(fc2l)
            self.fc2 = BatchNormalization(axis=-1,
                                          name='mentor_batch_norm_fc2')(
                                              self.fc2)
            if train_mode == True:
                self.fc2 = tf.nn.dropout(self.fc2, 0.5)
            self.parameters += [fc2w, fc2b]

        with tf.name_scope('mentor_fc3') as scope:
            fc3w = tf.Variable(tf.truncated_normal([4096, num_classes],
                                                   dtype=tf.float32,
                                                   stddev=1e-2),
                               trainable=self.trainable,
                               name='mentor_weights')
            fc3b = tf.Variable(tf.constant(1.0,
                                           shape=[num_classes],
                                           dtype=tf.float32),
                               trainable=self.trainable,
                               name='mentor_biases')
            self.fc3l = tf.nn.bias_add(tf.matmul(self.fc2, fc3w), fc3b)
            #self.fc3l = tf.nn.relu(fc3l)
            self.parameters += [fc3w, fc3b]

        self.softmax = tf.nn.softmax(self.fc3l / temp_softmax)

        return self
예제 #17
0
- model.fit(batch_size), model.fit_generator() are to train a small batch at a time until all dataset are trained

"""

from tensorflow.contrib.keras.python.keras.layers import Dropout, BatchNormalization, Input, Dense
from tensorflow.contrib.keras.python.keras.models import Model
import numpy as np
from tensorflow.contrib.keras.python.keras import backend as K
from tensorflow.contrib.keras.python.keras import losses

x = np.random.random((10, 10)) * 2
y = np.random.randint(2, size=(10, 1))

input_tensor = Input(shape=(10, ))
bn_tensor = BatchNormalization()(input_tensor)
dp_tensor = Dropout(0.7)(bn_tensor)
final_tensor = Dense(1)(dp_tensor)

model = Model(input_tensor, final_tensor)
model.compile(optimizer='SGD', loss='mse')

# x, y won't get into batches for training here
loss_on_batch = model.train_on_batch(x, y)  # dive in for details
"""
('Runs a single gradient update on a single batch of data.\n'
 '\n'
 'Arguments:\n'
 '    x: Numpy array of training data,\n'
 '        or list of Numpy arrays if the model has multiple inputs.\n'
 '        If all inputs in the model are named,\n'
예제 #18
0
def main():
    training_images, training_labels, test_images, test_labels = load_dataset()

    # plt.imshow(training_images[:,:,0], cmap='gray')
    # plt.show()

    perm_train = np.random.permutation(training_labels.size)
    training_labels = training_labels[perm_train]
    training_images = (training_images[perm_train, :, :] - 127.5) / 127.5
    training_images = np.expand_dims(training_images, -1)
    print(training_images.shape)
    test_images = test_images / 255.0
    test_images = np.expand_dims(test_images, -1)

    # pdb.set_trace()

    #    training_labels = to_categorical(training_labels, NUM_CLASSES)
    #    test_labels = to_categorical(test_labels, NUM_CLASSES)

    BATCH_SIZE = 32 * 8
    WIDTH, HEIGHT = 28, 28
    adam_lr = 0.0002
    adam_beta_1 = 0.5

    #####################################
    ### Defiining the Discriminator:
    #####################################
    input_D = Input(shape=(HEIGHT, WIDTH, 1), name='input_D')
    x = Conv2D(filters=32,
               kernel_size=3,
               strides=(2, 2),
               padding='same',
               name='conv1_D')(input_D)
    #x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(filters=32,
               kernel_size=3,
               strides=(2, 2),
               padding='same',
               name='conv2_D')(x)
    #x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Flatten()(x)
    x = Dense(128, activation='relu', name='dense1_D')(x)
    output_D = Dense(1, activation='sigmoid', name='output_D')(x)
    model_D = Model(inputs=input_D, outputs=output_D)
    model_D.compile(loss='binary_crossentropy',
                    optimizer=tf.train.AdamOptimizer(learning_rate=adam_lr,
                                                     beta1=adam_beta_1),
                    metrics=['accuracy'])

    #####################################
    ### Defiining the Generator:
    #####################################
    LATENT_SIZE = 100
    input_G = Input(shape=(LATENT_SIZE, ), name='input_gen')
    x = Dense(7 * 7 * 32, activation='linear', name='Dense1_G')(input_G)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Reshape((7, 7, 32))(x)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(filters=32,
               kernel_size=3,
               strides=(1, 1),
               padding='same',
               name='conv1_gen')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(filters=32,
               kernel_size=3,
               strides=(1, 1),
               padding='same',
               name='conv2_gen')(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Conv2D(filters=1,
               kernel_size=1,
               strides=(1, 1),
               padding='same',
               name='conv3_gen')(x)
    img_G = Activation('tanh')(x)
    model_G = Model(inputs=input_G, outputs=img_G)
    model_G.compile(loss='binary_crossentropy',
                    optimizer=tf.train.AdamOptimizer(learning_rate=adam_lr,
                                                     beta1=adam_beta_1))

    #####################################
    ### Defiining the Combined GAN:
    #####################################
    model_D.trainable = False  # Since model_D is already compiled, thediscriminator model remains trainble,
    # but here in the combined model it becomes non-trainable
    input_main = Input(
        shape=(LATENT_SIZE, ), name='input_main'
    )  # Note that this input should be different from the input to Generator
    combined = Model(inputs=input_main, outputs=model_D(model_G(input_main)))
    combined.compile(loss='binary_crossentropy',
                     optimizer=tf.train.AdamOptimizer(learning_rate=adam_lr,
                                                      beta1=adam_beta_1),
                     metrics=['accuracy'])

    print(combined.summary())

    #####################################
    ### Training:
    #####################################
    bar = InitBar()
    N = training_images.shape[0]
    for iter in range(100):
        fake_input = np.random.randn(1, LATENT_SIZE)
        fake_image = model_G.predict(fake_input)
        loss_G, acc_G, loss_D, acc_D = 0, 0, 0, 0
        steps = (int)(np.ceil(float(N) / float(BATCH_SIZE)))
        for batch_iter in range(steps):
            bar(100.0 * batch_iter / float(steps))
            real_image, _ = get_batch(batch_iter, BATCH_SIZE / 2,
                                      training_images, training_labels)
            ####################
            ## Discriminator Training
            ####################
            #  Note that if using BN layer in Discriminator, minibatch should contain only real images or fake images.
            fake_input = np.random.randn(BATCH_SIZE / 2, LATENT_SIZE)
            fake_image = model_G.predict(fake_input)
            #real_image = get_real_mbatch(batch_sz=BATCH_SIZE/2, data=training_images)
            agg_input = np.concatenate((fake_image, real_image), axis=0)
            agg_output = np.zeros((BATCH_SIZE, ))
            agg_output[BATCH_SIZE / 2:] = 1
            perm = np.random.permutation(BATCH_SIZE)
            agg_input = agg_input[perm]
            agg_output = agg_output[perm]
            #pdb.set_trace()
            tr = model_D.train_on_batch(x=agg_input, y=agg_output)
            loss_D += tr[0]
            acc_D += tr[1]
            #####################
            ## Generator Training
            #####################
            fake_input = np.random.randn(BATCH_SIZE, LATENT_SIZE)
            fake_label = np.ones(BATCH_SIZE, )
            tr = combined.train_on_batch(x=fake_input, y=fake_label)
            loss_G += tr[0]
            acc_G += tr[1]
        print('\nG_loss = {}, G_acc = {}\nD_loss = {}, D_acc = {}'.format(
            loss_G / float(steps), acc_G / float(steps), loss_D / float(steps),
            acc_D / float(steps)))

    for iter in range(10):
        fake_input = np.random.randn(1, LATENT_SIZE)
        fake_image = model_G.predict(fake_input)
        plt.imshow(fake_image[0, :, :, 0])
        plt.show()
예제 #19
0
    net.trainable = val
    for l in net.layers:
        l.trainable = val


shp = X_train.shape[1:]
dropout_rate = 0.25
opt = Adam(lr=1e-4)
dopt = Adam(lr=1e-3)

# Build Generative model ...
nch = 200
g_input = Input(shape=[100])
H = Dense(nch * 14 * 14,
          kernel_initializer=init_ops.glorot_normal_initializer())(g_input)
H = BatchNormalization()(H)
H = Activation('relu')(H)
H = Reshape([14, 14, nch])(H)
H = UpSampling2D(size=(2, 2))(H)
H = Conv2D(nch / 2,
           kernel_size=(3, 3),
           padding='same',
           kernel_initializer=init_ops.glorot_normal_initializer(),
           name='Convolution_1')(H)
H = BatchNormalization()(H)
H = Activation('relu')(H)
H = Conv2D(nch / 4,
           kernel_size=(3, 3),
           padding='same',
           kernel_initializer=init_ops.glorot_normal_initializer(),
           name='Convolution_2')(H)
예제 #20
0
def ResNet50(include_top=True,
             weights='imagenet',
             input_tensor=None,
             input_shape=None,
             pooling=None,
             classes=1000):
    """Instantiates the ResNet50 architecture.

    Optionally loads weights pre-trained
    on ImageNet. Note that when using TensorFlow,
    for best performance you should set
    `image_data_format="channels_last"` in your Keras config
    at ~/.keras/keras.json.

    The model and the weights are compatible with both
    TensorFlow and Theano. The data format
    convention used by the model is the one
    specified in your Keras config file.

    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization)
            or "imagenet" (pre-training on ImageNet).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 244)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 197.
            E.g. `(200, 200, 3)` would be one valid value.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    if weights not in {'imagenet', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `imagenet` '
                         '(pre-training on ImageNet).')

    if weights == 'imagenet' and include_top and classes != 1000:
        raise ValueError('If using `weights` as imagenet with `include_top`'
                         ' as true, `classes` should be 1000')

    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=224,
                                      min_size=197,
                                      data_format=K.image_data_format(),
                                      include_top=include_top)

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor
    if K.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1

    x = ZeroPadding2D((3, 3))(img_input)
    x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
    x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2))(x)

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')

    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

    x = AveragePooling2D((7, 7), name='avg_pool')(x)

    if include_top:
        x = Flatten()(x)
        x = Dense(classes, activation='softmax', name='fc1000')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input
    # Create model.
    model = Model(inputs, x, name='resnet50')

    # load weights
    if weights == 'imagenet':
        if include_top:
            weights_path = WEIGHTS_PATH
        else:
            weights_path = WEIGHTS_PATH_NO_TOP
        model.load_weights(weights_path)
        if K.backend() == 'theano':
            layer_utils.convert_all_kernels_in_model(model)

        if K.image_data_format() == 'channels_first':
            if include_top:
                maxpool = model.get_layer(name='avg_pool')
                shape = maxpool.output_shape[1:]
                dense = model.get_layer(name='fc1000')
                layer_utils.convert_dense_weights_data_format(
                    dense, shape, 'channels_first')

            if K.backend() == 'tensorflow':
                warnings.warn('You are using the TensorFlow backend, yet you '
                              'are using the Theano '
                              'image data format convention '
                              '(`image_data_format="channels_first"`). '
                              'For best performance, set '
                              '`image_data_format="channels_last"` in '
                              'your Keras config '
                              'at ~/.keras/keras.json.')
    return model
예제 #21
0
def _depthwise_conv_block(inputs,
                          pointwise_conv_filters,
                          alpha,
                          depth_multiplier=1,
                          strides=(1, 1),
                          block_id=1):
    """Adds a depthwise convolution block.

  A depthwise convolution block consists of a depthwise conv,
  batch normalization, relu6, pointwise convolution,
  batch normalization and relu6 activation.

  Arguments:
      inputs: Input tensor of shape `(rows, cols, channels)`
          (with `channels_last` data format) or
          (channels, rows, cols) (with `channels_first` data format).
      pointwise_conv_filters: Integer, the dimensionality of the output space
          (i.e. the number output of filters in the pointwise convolution).
      alpha: controls the width of the network.
          - If `alpha` < 1.0, proportionally decreases the number
              of filters in each layer.
          - If `alpha` > 1.0, proportionally increases the number
              of filters in each layer.
          - If `alpha` = 1, default number of filters from the paper
               are used at each layer.
      depth_multiplier: The number of depthwise convolution output channels
          for each input channel.
          The total number of depthwise convolution output
          channels will be equal to `filters_in * depth_multiplier`.
      strides: An integer or tuple/list of 2 integers,
          specifying the strides of the convolution along the width and height.
          Can be a single integer to specify the same value for
          all spatial dimensions.
          Specifying any stride value != 1 is incompatible with specifying
          any `dilation_rate` value != 1.
      block_id: Integer, a unique identification designating the block number.

  Input shape:
      4D tensor with shape:
      `(batch, channels, rows, cols)` if data_format='channels_first'
      or 4D tensor with shape:
      `(batch, rows, cols, channels)` if data_format='channels_last'.

  Output shape:
      4D tensor with shape:
      `(batch, filters, new_rows, new_cols)` if data_format='channels_first'
      or 4D tensor with shape:
      `(batch, new_rows, new_cols, filters)` if data_format='channels_last'.
      `rows` and `cols` values might have changed due to stride.

  Returns:
      Output tensor of block.
  """
    channel_axis = 1 if K.image_data_format() == 'channels_first' else -1
    pointwise_conv_filters = int(pointwise_conv_filters * alpha)

    x = DepthwiseConv2D(  # pylint: disable=not-callable
        (3, 3),
        padding='same',
        depth_multiplier=depth_multiplier,
        strides=strides,
        use_bias=False,
        name='conv_dw_%d' % block_id)(inputs)
    x = BatchNormalization(axis=channel_axis,
                           name='conv_dw_%d_bn' % block_id)(x)
    x = Activation(relu6, name='conv_dw_%d_relu' % block_id)(x)

    x = Conv2D(pointwise_conv_filters, (1, 1),
               padding='same',
               use_bias=False,
               strides=(1, 1),
               name='conv_pw_%d' % block_id)(x)
    x = BatchNormalization(axis=channel_axis,
                           name='conv_pw_%d_bn' % block_id)(x)
    return Activation(relu6, name='conv_pw_%d_relu' % block_id)(x)
예제 #22
0
def Xception(include_top=True,
             weights='imagenet',
             input_tensor=None,
             input_shape=None,
             pooling=None,
             classes=1000):
    """Instantiates the Xception architecture.

  Optionally loads weights pre-trained
  on ImageNet. This model is available for TensorFlow only,
  and can only be used with inputs following the TensorFlow
  data format `(width, height, channels)`.
  You should set `image_data_format="channels_last"` in your Keras config
  located at ~/.keras/keras.json.

  Note that the default input image size for this model is 299x299.

  Arguments:
      include_top: whether to include the fully-connected
          layer at the top of the network.
      weights: one of `None` (random initialization)
          or "imagenet" (pre-training on ImageNet).
      input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
          to use as image input for the model.
      input_shape: optional shape tuple, only to be specified
          if `include_top` is False (otherwise the input shape
          has to be `(299, 299, 3)`.
          It should have exactly 3 input channels,
          and width and height should be no smaller than 71.
          E.g. `(150, 150, 3)` would be one valid value.
      pooling: Optional pooling mode for feature extraction
          when `include_top` is `False`.
          - `None` means that the output of the model will be
              the 4D tensor output of the
              last convolutional layer.
          - `avg` means that global average pooling
              will be applied to the output of the
              last convolutional layer, and thus
              the output of the model will be a 2D tensor.
          - `max` means that global max pooling will
              be applied.
      classes: optional number of classes to classify images
          into, only to be specified if `include_top` is True, and
          if no `weights` argument is specified.

  Returns:
      A Keras model instance.

  Raises:
      ValueError: in case of invalid argument for `weights`,
          or invalid input shape.
      RuntimeError: If attempting to run this model with a
          backend that does not support separable convolutions.
  """
    if weights not in {'imagenet', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `imagenet` '
                         '(pre-training on ImageNet).')

    if weights == 'imagenet' and include_top and classes != 1000:
        raise ValueError('If using `weights` as imagenet with `include_top`'
                         ' as true, `classes` should be 1000')

    if K.backend() != 'tensorflow':
        raise RuntimeError('The Xception model is only available with '
                           'the TensorFlow backend.')
    if K.image_data_format() != 'channels_last':
        logging.warning(
            'The Xception model is only available for the '
            'input data format "channels_last" '
            '(width, height, channels). '
            'However your settings specify the default '
            'data format "channels_first" (channels, width, height). '
            'You should set `image_data_format="channels_last"` in your Keras '
            'config located at ~/.keras/keras.json. '
            'The model being returned right now will expect inputs '
            'to follow the "channels_last" data format.')
        K.set_image_data_format('channels_last')
        old_data_format = 'channels_first'
    else:
        old_data_format = None

    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=299,
                                      min_size=71,
                                      data_format=K.image_data_format(),
                                      require_flatten=False,
                                      weights=weights)

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        img_input = Input(tensor=input_tensor, shape=input_shape)

    x = Conv2D(32, (3, 3), strides=(2, 2), use_bias=False,
               name='block1_conv1')(img_input)
    x = BatchNormalization(name='block1_conv1_bn')(x)
    x = Activation('relu', name='block1_conv1_act')(x)
    x = Conv2D(64, (3, 3), use_bias=False, name='block1_conv2')(x)
    x = BatchNormalization(name='block1_conv2_bn')(x)
    x = Activation('relu', name='block1_conv2_act')(x)

    residual = Conv2D(128, (1, 1),
                      strides=(2, 2),
                      padding='same',
                      use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = SeparableConv2D(128, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block2_sepconv1')(x)
    x = BatchNormalization(name='block2_sepconv1_bn')(x)
    x = Activation('relu', name='block2_sepconv2_act')(x)
    x = SeparableConv2D(128, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block2_sepconv2')(x)
    x = BatchNormalization(name='block2_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3),
                     strides=(2, 2),
                     padding='same',
                     name='block2_pool')(x)
    x = layers.add([x, residual])

    residual = Conv2D(256, (1, 1),
                      strides=(2, 2),
                      padding='same',
                      use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block3_sepconv1_act')(x)
    x = SeparableConv2D(256, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block3_sepconv1')(x)
    x = BatchNormalization(name='block3_sepconv1_bn')(x)
    x = Activation('relu', name='block3_sepconv2_act')(x)
    x = SeparableConv2D(256, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block3_sepconv2')(x)
    x = BatchNormalization(name='block3_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3),
                     strides=(2, 2),
                     padding='same',
                     name='block3_pool')(x)
    x = layers.add([x, residual])

    residual = Conv2D(728, (1, 1),
                      strides=(2, 2),
                      padding='same',
                      use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block4_sepconv1_act')(x)
    x = SeparableConv2D(728, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block4_sepconv1')(x)
    x = BatchNormalization(name='block4_sepconv1_bn')(x)
    x = Activation('relu', name='block4_sepconv2_act')(x)
    x = SeparableConv2D(728, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block4_sepconv2')(x)
    x = BatchNormalization(name='block4_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3),
                     strides=(2, 2),
                     padding='same',
                     name='block4_pool')(x)
    x = layers.add([x, residual])

    for i in range(8):
        residual = x
        prefix = 'block' + str(i + 5)

        x = Activation('relu', name=prefix + '_sepconv1_act')(x)
        x = SeparableConv2D(728, (3, 3),
                            padding='same',
                            use_bias=False,
                            name=prefix + '_sepconv1')(x)
        x = BatchNormalization(name=prefix + '_sepconv1_bn')(x)
        x = Activation('relu', name=prefix + '_sepconv2_act')(x)
        x = SeparableConv2D(728, (3, 3),
                            padding='same',
                            use_bias=False,
                            name=prefix + '_sepconv2')(x)
        x = BatchNormalization(name=prefix + '_sepconv2_bn')(x)
        x = Activation('relu', name=prefix + '_sepconv3_act')(x)
        x = SeparableConv2D(728, (3, 3),
                            padding='same',
                            use_bias=False,
                            name=prefix + '_sepconv3')(x)
        x = BatchNormalization(name=prefix + '_sepconv3_bn')(x)

        x = layers.add([x, residual])

    residual = Conv2D(1024, (1, 1),
                      strides=(2, 2),
                      padding='same',
                      use_bias=False)(x)
    residual = BatchNormalization()(residual)

    x = Activation('relu', name='block13_sepconv1_act')(x)
    x = SeparableConv2D(728, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block13_sepconv1')(x)
    x = BatchNormalization(name='block13_sepconv1_bn')(x)
    x = Activation('relu', name='block13_sepconv2_act')(x)
    x = SeparableConv2D(1024, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block13_sepconv2')(x)
    x = BatchNormalization(name='block13_sepconv2_bn')(x)

    x = MaxPooling2D((3, 3),
                     strides=(2, 2),
                     padding='same',
                     name='block13_pool')(x)
    x = layers.add([x, residual])

    x = SeparableConv2D(1536, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block14_sepconv1')(x)
    x = BatchNormalization(name='block14_sepconv1_bn')(x)
    x = Activation('relu', name='block14_sepconv1_act')(x)

    x = SeparableConv2D(2048, (3, 3),
                        padding='same',
                        use_bias=False,
                        name='block14_sepconv2')(x)
    x = BatchNormalization(name='block14_sepconv2_bn')(x)
    x = Activation('relu', name='block14_sepconv2_act')(x)

    if include_top:
        x = GlobalAveragePooling2D(name='avg_pool')(x)
        x = Dense(classes, activation='softmax', name='predictions')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input
    # Create model.
    model = Model(inputs, x, name='xception')

    # load weights
    if weights == 'imagenet':
        if include_top:
            weights_path = get_file(
                'xception_weights_tf_dim_ordering_tf_kernels.h5',
                TF_WEIGHTS_PATH,
                cache_subdir='models')
        else:
            weights_path = get_file(
                'xception_weights_tf_dim_ordering_tf_kernels_notop.h5',
                TF_WEIGHTS_PATH_NO_TOP,
                cache_subdir='models')
        model.load_weights(weights_path)

    if old_data_format:
        K.set_image_data_format(old_data_format)
    return model
예제 #23
0
def resnet50_deeplab():
    """ Building a resnet50 model fr semantic segmentation
    :return : returns thekeras implementation of deeplab architecture
    """
    ################################################
    ######## Building the model ####################
    ################################################
    input_layer = Input(shape=(None, None, 3), name='input_layer')
    conv1_1 = Conv2D(filters=64,
                     kernel_size=7,
                     strides=(2, 2),
                     use_bias=False,
                     padding='same',
                     name='conv1')(input_layer)
    bn1_1 = BatchNormalization(name='bn_conv1')(conv1_1)
    relu1_1 = Activation('relu')(bn1_1)
    mxp1_1 = MaxPooling2D(pool_size=3, strides=(2, 2))(relu1_1)
    conv1_2 = Conv2D(filters=256,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res2a_branch1')(mxp1_1)
    bn1_2 = BatchNormalization(name='bn2a_branch1')(conv1_2)

    conv2_1 = Conv2D(filters=64,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res2a_branch2a')(mxp1_1)
    bn2_1 = BatchNormalization(name='bn2a_branch2a')(conv2_1)
    relu2_1 = Activation('relu')(bn2_1)
    conv2_2 = Conv2D(filters=64,
                     kernel_size=3,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res2a_branch2b')(relu2_1)
    bn2_2 = BatchNormalization(name='bn2a_branch2b')(conv2_2)
    relu2_2 = Activation('relu')(bn2_2)
    conv2_3 = Conv2D(filters=256,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res2a_branch2c')(relu2_2)
    bn2_3 = BatchNormalization(name='bn2a_branch2c')(conv2_3)

    merge3 = Add()([bn1_2, bn2_3])
    relu3_1 = Activation('relu')(merge3)
    conv3_1 = Conv2D(filters=64,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res2b_branch2a')(relu3_1)
    bn3_1 = BatchNormalization(name='bn2b_branch2a')(conv3_1)
    relu3_2 = Activation('relu')(bn3_1)
    conv3_2 = Conv2D(filters=64,
                     kernel_size=3,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res2b_branch2b')(relu3_2)
    bn3_2 = BatchNormalization(name='bn2b_branch2b')(conv3_2)
    relu3_3 = Activation('relu')(bn3_2)
    conv3_3 = Conv2D(filters=256,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res2b_branch2c')(relu3_3)
    bn3_3 = BatchNormalization(name='bn2b_branch2c')(conv3_3)

    merge4 = Add()([relu3_1, bn3_3])
    relu4_1 = Activation('relu')(merge4)
    conv4_1 = Conv2D(filters=64,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res2c_branch2a')(relu4_1)
    bn4_1 = BatchNormalization(name='bn2c_branch2a')(conv4_1)
    relu4_2 = Activation('relu')(bn4_1)
    conv4_2 = Conv2D(filters=64,
                     kernel_size=3,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res2c_branch2b')(relu4_2)
    bn4_2 = BatchNormalization(name='bn2c_branch2b')(conv4_2)
    relu4_3 = Activation('relu')(bn4_2)
    conv4_3 = Conv2D(filters=256,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res2c_branch2c')(relu4_3)
    bn4_3 = BatchNormalization(name='bn2c_branch2c')(conv4_3)

    merge5 = Add()([relu4_1, bn4_3])
    relu5_1 = Activation('relu')(merge5)
    conv5_1 = Conv2D(filters=512,
                     kernel_size=1,
                     strides=(2, 2),
                     use_bias=False,
                     padding='same',
                     name='res3a_branch1')(relu5_1)
    bn5_1 = BatchNormalization(name='bn3a_branch1')(conv5_1)

    conv6_1 = Conv2D(filters=128,
                     kernel_size=1,
                     strides=(2, 2),
                     use_bias=False,
                     padding='same',
                     name='res3a_branch2a')(relu5_1)
    bn6_1 = BatchNormalization(name='bn3a_branch2a')(conv6_1)
    relu6_1 = Activation('relu')(bn6_1)
    conv6_2 = Conv2D(filters=128,
                     kernel_size=3,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res3a_branch2b')(relu6_1)
    bn6_2 = BatchNormalization(name='bn3a_branch2b')(conv6_2)
    relu6_2 = Activation('relu')(bn6_2)
    conv6_3 = Conv2D(filters=512,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res3a_branch2c')(relu6_2)
    bn6_3 = BatchNormalization(name='bn3a_branch2c')(conv6_3)

    merge7 = Add()([bn5_1, bn6_3])
    relu7_1 = Activation('relu', name='res3a_relu')(merge7)
    conv7_1 = Conv2D(filters=128,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res3b1_branch2a')(relu7_1)
    bn7_1 = BatchNormalization(name='bn3b1_branch2a')(conv7_1)
    relu7_2 = Activation('relu')(bn7_1)
    conv7_2 = Conv2D(filters=128,
                     kernel_size=3,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res3b1_branch2b')(relu7_2)
    bn7_2 = BatchNormalization(name='bn3b1_branch2b')(conv7_2)
    relu7_3 = Activation('relu')(bn7_2)
    conv7_3 = Conv2D(filters=512,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res3b1_branch2c')(relu7_3)
    bn7_3 = BatchNormalization(name='bn3b1_branch2c')(conv7_3)

    merge8 = Add()([relu7_1, bn7_3])
    relu8_1 = Activation('relu', name='res3b1_relu')(merge8)
    conv8_1 = Conv2D(filters=128,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res3b2_branch2a')(relu8_1)
    bn8_1 = BatchNormalization(name='bn3b2_branch2a')(conv8_1)
    relu8_2 = Activation('relu')(bn8_1)
    conv8_2 = Conv2D(filters=128,
                     kernel_size=3,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res3b2_branch2b')(relu8_2)
    bn8_2 = BatchNormalization(name='bn3b2_branch2b')(conv8_2)
    relu8_3 = Activation('relu')(bn8_2)
    conv8_3 = Conv2D(filters=512,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res3b2_branch2c')(relu8_3)
    bn8_3 = BatchNormalization(name='bn3b2_branch2c')(conv8_3)

    merge9 = Add()([relu8_1, bn8_3])
    relu9_1 = Activation('relu', name='res3b2_relu')(merge9)
    conv9_1 = Conv2D(filters=128,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res3b3_branch2a')(relu9_1)
    bn9_1 = BatchNormalization(name='bn3b3_branch2a')(conv9_1)
    relu9_2 = Activation('relu')(bn9_1)
    conv9_2 = Conv2D(filters=128,
                     kernel_size=3,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res3b3_branch2b')(relu9_2)
    bn9_2 = BatchNormalization(name='bn3b3_branch2b')(conv9_2)
    relu9_3 = Activation('relu')(bn9_2)
    conv9_3 = Conv2D(filters=512,
                     kernel_size=1,
                     strides=(1, 1),
                     use_bias=False,
                     padding='same',
                     name='res3b3_branch2c')(relu9_3)
    bn9_3 = BatchNormalization(name='bn3b3_branch2c')(conv9_3)

    merge10 = Add()([relu9_1, bn9_3])
    relu10_1 = Activation('relu', name='res3b3_relu')(merge10)
    conv10_1 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4a_branch1')(relu10_1)
    bn10_1 = BatchNormalization(name='bn4a_branch1')(conv10_1)

    conv11_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4a_branch2a')(relu10_1)
    bn11_1 = BatchNormalization(name='bn4a_branch2a')(conv11_1)
    relu11_1 = Activation('relu')(bn11_1)
    at_conv11_2 = Conv2D(filters=256,
                         kernel_size=3,
                         dilation_rate=(2, 2),
                         use_bias=False,
                         padding='same',
                         name='res4a_branch2b')(relu11_1)
    bn11_2 = BatchNormalization(name='bn4a_branch2b')(at_conv11_2)
    relu11_2 = Activation('relu')(bn11_2)
    conv11_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4a_branch2c')(relu11_2)
    bn11_3 = BatchNormalization(name='bn4a_branch2c')(conv11_3)

    merge12 = Add()([bn10_1, bn11_3])
    relu12_1 = Activation('relu', name='res4a_relu')(merge12)
    conv12_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b1_branch2a')(relu12_1)
    bn12_1 = BatchNormalization(name='bn4b1_branch2a')(conv12_1)
    relu12_2 = Activation('relu')(bn12_1)
    conv12_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b1_branch2b')(relu12_2)
    bn12_2 = BatchNormalization(name='bn4b1_branch2b')(conv12_2)
    relu12_3 = Activation('relu')(bn12_2)
    conv12_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b1_branch2c')(relu12_3)
    bn12_3 = BatchNormalization(name='bn4b1_branch2c')(conv12_3)

    merge13 = Add()([relu12_1, bn12_3])
    relu13_1 = Activation('relu', name='res4b1_relu')(merge13)
    conv13_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b2_branch2a')(relu13_1)
    bn13_1 = BatchNormalization(name='bn4b2_branch2a')(conv13_1)
    relu13_2 = Activation('relu')(bn13_1)
    conv13_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b2_branch2b')(relu13_2)
    bn13_2 = BatchNormalization(name='bn4b2_branch2b')(conv13_2)
    relu13_3 = Activation('relu')(bn13_2)
    conv13_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b2_branch2c')(relu13_3)
    bn13_3 = BatchNormalization(name='bn4b2_branch2c')(conv13_3)

    merge14 = Add()([relu13_1, bn13_3])
    relu14_1 = Activation('relu', name='res4b2_relu')(merge14)
    conv14_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b3_branch2a')(relu14_1)
    bn14_1 = BatchNormalization(name='bn4b3_branch2a')(conv14_1)
    relu14_2 = Activation('relu')(bn14_1)
    conv14_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b3_branch2b')(relu14_2)
    bn14_2 = BatchNormalization(name='bn4b3_branch2b')(conv14_2)
    relu14_3 = Activation('relu')(bn14_2)
    conv14_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b3_branch2c')(relu14_3)
    bn14_3 = BatchNormalization(name='bn4b3_branch2c')(conv14_3)

    merge15 = Add()([relu14_1, bn14_3])
    relu15_1 = Activation('relu', name='res4b3_relu')(merge15)
    conv15_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b4_branch2a')(relu15_1)
    bn15_1 = BatchNormalization(name='bn4b4_branch2a')(conv15_1)
    relu15_2 = Activation('relu')(bn15_1)
    conv15_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b4_branch2b')(relu15_2)
    bn15_2 = BatchNormalization(name='bn4b4_branch2b')(conv15_2)
    relu15_3 = Activation('relu')(bn15_2)
    conv15_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b4_branch2c')(relu15_3)
    bn15_3 = BatchNormalization(name='bn4b4_branch2c')(conv15_3)

    merge16 = Add()([relu15_1, bn15_3])
    relu16_1 = Activation('relu', name='res4b4_relu')(merge16)
    conv16_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b5_branch2a')(relu16_1)
    bn16_1 = BatchNormalization(name='bn4b5_branch2a')(conv16_1)
    relu16_2 = Activation('relu')(bn16_1)
    conv16_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b5_branch2b')(relu16_2)
    bn16_2 = BatchNormalization(name='bn4b5_branch2b')(conv16_2)
    relu16_3 = Activation('relu')(bn16_2)
    conv16_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b5_branch2c')(relu16_3)
    bn16_3 = BatchNormalization(name='bn4b5_branch2c')(conv16_3)

    merge17 = Add()([relu16_1, bn16_3])
    relu17_1 = Activation('relu', name='res4b5_relu')(merge17)
    conv17_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b6_branch2a')(relu17_1)
    bn17_1 = BatchNormalization(name='bn4b6_branch2a')(conv17_1)
    relu17_2 = Activation('relu')(bn17_1)
    conv17_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b6_branch2b')(relu17_2)
    bn17_2 = BatchNormalization(name='bn4b6_branch2b')(conv17_2)
    relu17_3 = Activation('relu')(bn17_2)
    conv17_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b6_branch2c')(relu17_3)
    bn17_3 = BatchNormalization(name='bn4b6_branch2c')(conv17_3)

    merge18 = Add()([relu17_1, bn17_3])
    relu18_1 = Activation('relu', name='res4b6_relu')(merge18)
    conv18_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b7_branch2a')(relu18_1)
    bn18_1 = BatchNormalization(name='bn4b7_branch2a')(conv18_1)
    relu18_2 = Activation('relu')(bn18_1)
    conv18_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b7_branch2b')(relu18_2)
    bn18_2 = BatchNormalization(name='bn4b7_branch2b')(conv18_2)
    relu18_3 = Activation('relu')(bn18_2)
    conv18_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b7_branch2c')(relu18_3)
    bn18_3 = BatchNormalization(name='bn4b7_branch2c')(conv18_3)

    merge19 = Add()([relu18_1, bn18_3])
    relu19_1 = Activation('relu', name='res4b7_relu')(merge19)
    conv19_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b8_branch2a')(relu19_1)
    bn19_1 = BatchNormalization(name='bn4b8_branch2a')(conv19_1)
    relu19_2 = Activation('relu')(bn19_1)
    conv19_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b8_branch2b')(relu19_2)
    bn19_2 = BatchNormalization(name='bn4b8_branch2b')(conv19_2)
    relu19_3 = Activation('relu')(bn19_2)
    conv19_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b8_branch2c')(relu19_3)
    bn19_3 = BatchNormalization(name='bn4b8_branch2c')(conv19_3)

    merge20 = Add()([relu19_1, bn19_3])
    relu20_1 = Activation('relu', name='res4b8_relu')(merge20)
    conv20_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b9_branch2a')(relu20_1)
    bn20_1 = BatchNormalization(name='bn4b9_branch2a')(conv20_1)
    relu20_2 = Activation('relu')(bn20_1)
    conv20_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b9_branch2b')(relu20_2)
    bn20_2 = BatchNormalization(name='bn4b9_branch2b')(conv20_2)
    relu20_3 = Activation('relu')(bn20_2)
    conv20_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b9_branch2c')(relu20_3)
    bn20_3 = BatchNormalization(name='bn4b9_branch2c')(conv20_3)

    merge21 = Add()([relu20_1, bn20_3])
    relu21_1 = Activation('relu', name='res4b9_relu')(merge21)
    conv21_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b10_branch2a')(relu21_1)
    bn21_1 = BatchNormalization(name='bn4b10_branch2a')(conv21_1)
    relu21_2 = Activation('relu')(bn21_1)
    conv21_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b10_branch2b')(relu21_2)
    bn21_2 = BatchNormalization(name='bn4b10_branch2b')(conv21_2)
    relu21_3 = Activation('relu')(bn21_2)
    conv21_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b10_branch2c')(relu21_3)
    bn21_3 = BatchNormalization(name='bn4b10_branch2c')(conv21_3)

    merge22 = Add()([relu21_1, bn21_3])
    relu22_1 = Activation('relu', name='res4b10_relu')(merge22)
    conv22_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b11_branch2a')(relu22_1)
    bn22_1 = BatchNormalization(name='bn4b11_branch2a')(conv22_1)
    relu22_2 = Activation('relu')(bn22_1)
    conv22_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b11_branch2b')(relu22_2)
    bn22_2 = BatchNormalization(name='bn4b11_branch2b')(conv22_2)
    relu22_3 = Activation('relu')(bn22_2)
    conv22_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b11_branch2c')(relu22_3)
    bn22_3 = BatchNormalization(name='bn4b11_branch2c')(conv22_3)

    merge23 = Add()([relu22_1, bn22_3])
    relu23_1 = Activation('relu', name='res4b11_relu')(merge23)
    conv23_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b12_branch2a')(relu23_1)
    bn23_1 = BatchNormalization(name='bn4b12_branch2a')(conv23_1)
    relu23_2 = Activation('relu')(bn23_1)
    conv23_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b12_branch2b')(relu23_2)
    bn23_2 = BatchNormalization(name='bn4b12_branch2b')(conv23_2)
    relu23_3 = Activation('relu')(bn23_2)
    conv23_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b12_branch2c')(relu23_3)
    bn23_3 = BatchNormalization(name='bn4b12_branch2c')(conv23_3)

    merge24 = Add()([relu23_1, bn23_3])
    relu24_1 = Activation('relu', name='res4b12_relu')(merge24)
    conv24_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b13_branch2a')(relu24_1)
    bn24_1 = BatchNormalization(name='bn4b13_branch2a')(conv24_1)
    relu24_2 = Activation('relu')(bn24_1)
    conv24_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b13_branch2b')(relu24_2)
    bn24_2 = BatchNormalization(name='bn4b13_branch2b')(conv24_2)
    relu24_3 = Activation('relu')(bn24_2)
    conv24_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b13_branch2c')(relu24_3)
    bn24_3 = BatchNormalization(name='bn4b13_branch2c')(conv24_3)

    merge25 = Add()([relu24_1, bn24_3])
    relu25_1 = Activation('relu', name='res4b13_relu')(merge25)
    conv25_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b14_branch2a')(relu25_1)
    bn25_1 = BatchNormalization(name='bn4b14_branch2a')(conv25_1)
    relu25_2 = Activation('relu')(bn25_1)
    conv25_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b14_branch2b')(relu25_2)
    bn25_2 = BatchNormalization(name='bn4b14_branch2b')(conv25_2)
    relu25_3 = Activation('relu')(bn25_2)
    conv25_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b14_branch2c')(relu25_3)
    bn25_3 = BatchNormalization(name='bn4b14_branch2c')(conv25_3)

    merge26 = Add()([relu25_1, bn25_3])
    relu26_1 = Activation('relu', name='res4b14_relu')(merge26)
    conv26_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b15_branch2a')(relu26_1)
    bn26_1 = BatchNormalization(name='bn4b15_branch2a')(conv26_1)
    relu26_2 = Activation('relu')(bn26_1)
    conv26_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b15_branch2b')(relu26_2)
    bn26_2 = BatchNormalization(name='bn4b15_branch2b')(conv26_2)
    relu26_3 = Activation('relu')(bn26_2)
    conv26_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b15_branch2c')(relu26_3)
    bn26_3 = BatchNormalization(name='bn4b15_branch2c')(conv26_3)

    merge27 = Add()([relu26_1, bn26_3])
    relu27_1 = Activation('relu', name='res4b15_relu')(merge27)
    conv27_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b16_branch2a')(relu27_1)
    bn27_1 = BatchNormalization(name='bn4b16_branch2a')(conv27_1)
    relu27_2 = Activation('relu')(bn27_1)
    conv27_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b16_branch2b')(relu27_2)
    bn27_2 = BatchNormalization(name='bn4b16_branch2b')(conv27_2)
    relu27_3 = Activation('relu')(bn27_2)
    conv27_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b16_branch2c')(relu27_3)
    bn27_3 = BatchNormalization(name='bn4b16_branch2c')(conv27_3)

    merge28 = Add()([relu27_1, bn27_3])
    relu28_1 = Activation('relu', name='res4b16_relu')(merge28)
    conv28_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b17_branch2a')(relu28_1)
    bn28_1 = BatchNormalization(name='bn4b17_branch2a')(conv28_1)
    relu28_2 = Activation('relu')(bn28_1)
    conv28_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b17_branch2b')(relu28_2)
    bn28_2 = BatchNormalization(name='bn4b17_branch2b')(conv28_2)
    relu28_3 = Activation('relu')(bn28_2)
    conv28_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b17_branch2c')(relu28_3)
    bn28_3 = BatchNormalization(name='bn4b17_branch2c')(conv28_3)

    merge29 = Add()([relu28_1, bn28_3])
    relu29_1 = Activation('relu', name='res4b17_relu')(merge29)
    conv29_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b18_branch2a')(relu29_1)
    bn29_1 = BatchNormalization(name='bn4b18_branch2a')(conv29_1)
    relu29_2 = Activation('relu')(bn29_1)
    conv29_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b18_branch2b')(relu29_2)
    bn29_2 = BatchNormalization(name='bn4b18_branch2b')(conv29_2)
    relu29_3 = Activation('relu')(bn29_2)
    conv29_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b18_branch2c')(relu29_3)
    bn29_3 = BatchNormalization(name='bn4b18_branch2c')(conv29_3)

    merge30 = Add()([relu29_1, bn29_3])
    relu30_1 = Activation('relu', name='res4b18_relu')(merge30)
    conv30_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b19_branch2a')(relu30_1)
    bn30_1 = BatchNormalization(name='bn4b19_branch2a')(conv30_1)
    relu30_2 = Activation('relu')(bn30_1)
    conv30_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b19_branch2b')(relu30_2)
    bn30_2 = BatchNormalization(name='bn4b19_branch2b')(conv30_2)
    relu30_3 = Activation('relu')(bn30_2)
    conv30_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b19_branch2c')(relu30_3)
    bn30_3 = BatchNormalization(name='bn4b19_branch2c')(conv30_3)

    merge31 = Add()([relu30_1, bn30_3])
    relu31_1 = Activation('relu', name='res4b19_relu')(merge31)
    conv31_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b20_branch2a')(relu31_1)
    bn31_1 = BatchNormalization(name='bn4b20_branch2a')(conv31_1)
    relu31_2 = Activation('relu')(bn31_1)
    conv31_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b20_branch2b')(relu31_2)
    bn31_2 = BatchNormalization(name='bn4b20_branch2b')(conv31_2)
    relu31_3 = Activation('relu')(bn31_2)
    conv31_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b20_branch2c')(relu31_3)
    bn31_3 = BatchNormalization(name='bn4b20_branch2c')(conv31_3)

    merge32 = Add()([relu31_1, bn31_3])
    relu32_1 = Activation('relu', name='res4b20_relu')(merge32)
    conv32_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b21_branch2a')(relu32_1)
    bn32_1 = BatchNormalization(name='bn4b21_branch2a')(conv32_1)
    relu32_2 = Activation('relu')(bn32_1)
    conv32_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b21_branch2b')(relu32_2)
    bn32_2 = BatchNormalization(name='bn4b21_branch2b')(conv32_2)
    relu32_3 = Activation('relu')(bn32_2)
    conv32_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b21_branch2c')(relu32_3)
    bn32_3 = BatchNormalization(name='bn4b21_branch2c')(conv32_3)

    merge33 = Add()([relu32_1, bn32_3])
    relu33_1 = Activation('relu', name='res4b21_relu')(merge33)
    conv33_1 = Conv2D(filters=256,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b22_branch2a')(relu33_1)
    bn33_1 = BatchNormalization(name='bn4b22_branch2a')(conv33_1)
    relu33_2 = Activation('relu')(bn33_1)
    conv33_2 = Conv2D(filters=256,
                      kernel_size=3,
                      dilation_rate=(2, 2),
                      use_bias=False,
                      padding='same',
                      name='res4b22_branch2b')(relu33_2)
    bn33_2 = BatchNormalization(name='bn4b22_branch2b')(conv33_2)
    relu33_3 = Activation('relu')(bn33_2)
    conv33_3 = Conv2D(filters=1024,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res4b22_branch2c')(relu33_3)
    bn33_3 = BatchNormalization(name='bn4b22_branch2c')(conv33_3)

    merge34 = Add()([relu33_1, bn33_3])
    relu34_1 = Activation('relu', name='res4b22_relu')(merge34)
    conv34_1 = Conv2D(filters=2048,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res5a_branch1')(relu34_1)
    bn34_1 = BatchNormalization(name='bn5a_branch1')(conv34_1)

    conv35_1 = Conv2D(filters=512,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res5a_branch2a')(relu34_1)
    bn35_1 = BatchNormalization(name='bn5a_branch2a')(conv35_1)
    relu35_2 = Activation('relu')(bn35_1)
    conv35_2 = Conv2D(filters=512,
                      kernel_size=3,
                      dilation_rate=(4, 4),
                      use_bias=False,
                      padding='same',
                      name='res5a_branch2b')(relu35_2)
    bn35_2 = BatchNormalization(name='bn5a_branch2b')(conv35_2)
    relu35_3 = Activation('relu')(bn35_2)
    conv35_3 = Conv2D(filters=2048,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res5a_branch2c')(relu35_3)
    bn35_3 = BatchNormalization(name='bn5a_branch2c')(conv35_3)

    merge36 = Add()([bn34_1, bn35_3])
    relu36_1 = Activation('relu', name='res5a_relu')(merge36)
    conv36_1 = Conv2D(filters=512,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res5b_branch2a')(relu36_1)
    bn36_1 = BatchNormalization(name='bn5b_branch2a')(conv36_1)
    relu36_2 = Activation('relu')(bn36_1)
    conv36_2 = Conv2D(filters=512,
                      kernel_size=3,
                      dilation_rate=(4, 4),
                      use_bias=False,
                      padding='same',
                      name='res5b_branch2b')(relu36_2)
    bn36_2 = BatchNormalization(name='bn5b_branch2b')(conv36_2)
    relu36_3 = Activation('relu')(bn36_2)
    conv36_3 = Conv2D(filters=2048,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res5b_branch2c')(relu36_3)
    bn36_3 = BatchNormalization(name='bn5b_branch2c')(conv36_3)

    merge37 = Add()([relu36_1, bn36_3])
    relu37_1 = Activation('relu', name='res5b_relu')(merge37)
    conv37_1 = Conv2D(filters=512,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res5c_branch2a')(relu37_1)
    bn37_1 = BatchNormalization(name='bn5c_branch2a')(conv37_1)
    relu37_2 = Activation('relu')(bn37_1)
    conv37_2 = Conv2D(filters=512,
                      kernel_size=3,
                      dilation_rate=(4, 4),
                      use_bias=False,
                      padding='same',
                      name='res5c_branch2b')(relu37_2)
    bn37_2 = BatchNormalization(name='bn5c_branch2b')(conv37_2)
    relu37_3 = Activation('relu')(bn37_2)
    conv37_3 = Conv2D(filters=2048,
                      kernel_size=1,
                      strides=(1, 1),
                      use_bias=False,
                      padding='same',
                      name='res5c_branch2c')(relu37_3)
    bn37_3 = BatchNormalization(name='bn5c_branch2c')(conv37_3)

    merge38 = Add()([relu37_1, bn37_3])
    relu38_1 = Activation('relu', name='res5c_relu')(merge38)
    conv38_1 = Conv2D(filters=NUM_CLASSES,
                      kernel_size=3,
                      dilation_rate=(6, 6),
                      padding='same',
                      name='fc1_voc12_c0')(relu38_1)
    conv38_2 = Conv2D(filters=NUM_CLASSES,
                      kernel_size=3,
                      dilation_rate=(12, 12),
                      padding='same',
                      name='fc1_voc12_c1')(relu38_1)
    conv38_3 = Conv2D(filters=NUM_CLASSES,
                      kernel_size=3,
                      dilation_rate=(18, 18),
                      padding='same',
                      name='fc1_voc12_c2')(relu38_1)
    conv38_4 = Conv2D(filters=NUM_CLASSES,
                      kernel_size=3,
                      dilation_rate=(24, 24),
                      padding='same',
                      name='fc1_voc12_c3')(relu38_1)

    output = Add(name='fc1_voc12')([conv38_1, conv38_2, conv38_3, conv38_4])
    output = Lambda(lambda image: tf.image.resize_images(image, (H, W)))(
        output)
    #output = UpSampling2D((3,3))(output)
    #output = MaxPooling2D(pool_size=(2,2), strides=(2,2))(output)
    #output   = Activation('softmax')(output)

    model = Model(inputs=input_layer, outputs=output)
    model.compile(optimizer=tf.train.AdamOptimizer(),
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])

    return model
예제 #24
0
# with a Sequential model
get_3rd_layer_output = K.function([model.layers[0].input],
                                  [model.layers[1].output])
layer_output = get_3rd_layer_output([input_array1
                                     ])[0]  # [0] due to return a list
"""
Similarly, you could build a Theano and TensorFlow function directly.

Note that if your model has a different behavior in training and testing phase (e.g. if it uses `Dropout`, `BatchNormalization`, etc.), you will need
to pass the learning phase flag to your function:
"""
from tensorflow.contrib.keras.python.keras.layers import Dropout, BatchNormalization

input_tensor = Input(shape=(100, ), name="input_tensor")
inter_tensor = Dense(300, name="my_layer")(input_tensor)
bn_tensor = BatchNormalization()(inter_tensor)
drop_tensor = Dropout(0.7)(bn_tensor)
final_tensor = Dense(30, name="final_layer")(drop_tensor)
model_dp_bn = Model(input_tensor, final_tensor)  # create the original model

# K.function can help distinct test_mode and training mode; as in training_mode, BatchNormalization is working, whereas in test_mode, BatchNormalization is not applied
get_3rd_layer_output = K.function(
    [model_dp_bn.layers[0].input,
     K.learning_phase()], [model_dp_bn.layers[3].output])

# output in test mode = 0: no zeros in output
layer_output_0 = get_3rd_layer_output([input_array1, 0])[0]
layer_output_0.max()
layer_output_0.min()

# output in train mode = 1: lost of zeros in output
예제 #25
0
def ResNet50(include_top=True,
             weights='imagenet',
             input_tensor=None,
             input_shape=None,
             pooling=None,
             classes=1000):
  """Instantiates the ResNet50 architecture.

  Optionally loads weights pre-trained
  on ImageNet. Note that when using TensorFlow,
  for best performance you should set
  `image_data_format="channels_last"` in your Keras config
  at ~/.keras/keras.json.

  The model and the weights are compatible with both
  TensorFlow and Theano. The data format
  convention used by the model is the one
  specified in your Keras config file.

  Arguments:
      include_top: whether to include the fully-connected
          layer at the top of the network.
      weights: one of `None` (random initialization)
          or "imagenet" (pre-training on ImageNet).
      input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
          to use as image input for the model.
      input_shape: optional shape tuple, only to be specified
          if `include_top` is False (otherwise the input shape
          has to be `(224, 224, 3)` (with `channels_last` data format)
          or `(3, 224, 224)` (with `channels_first` data format).
          It should have exactly 3 inputs channels,
          and width and height should be no smaller than 197.
          E.g. `(200, 200, 3)` would be one valid value.
      pooling: Optional pooling mode for feature extraction
          when `include_top` is `False`.
          - `None` means that the output of the model will be
              the 4D tensor output of the
              last convolutional layer.
          - `avg` means that global average pooling
              will be applied to the output of the
              last convolutional layer, and thus
              the output of the model will be a 2D tensor.
          - `max` means that global max pooling will
              be applied.
      classes: optional number of classes to classify images
          into, only to be specified if `include_top` is True, and
          if no `weights` argument is specified.

  Returns:
      A Keras model instance.

  Raises:
      ValueError: in case of invalid argument for `weights`,
          or invalid input shape.
  """
  if weights not in {'imagenet', None}:
    raise ValueError('The `weights` argument should be either '
                     '`None` (random initialization) or `imagenet` '
                     '(pre-training on ImageNet).')

  if weights == 'imagenet' and include_top and classes != 1000:
    raise ValueError('If using `weights` as imagenet with `include_top`'
                     ' as true, `classes` should be 1000')

  # Determine proper input shape
  input_shape = _obtain_input_shape(
      input_shape,
      default_size=224,
      min_size=197,
      data_format=K.image_data_format(),
      include_top=include_top)

  if input_tensor is None:
    img_input = Input(shape=input_shape)
  else:
    img_input = Input(tensor=input_tensor, shape=input_shape)

  if K.image_data_format() == 'channels_last':
    bn_axis = 3
  else:
    bn_axis = 1

  x = ZeroPadding2D((3, 3))(img_input)
  x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
  x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
  x = Activation('relu')(x)
  x = MaxPooling2D((3, 3), strides=(2, 2))(x)

  x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
  x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
  x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

  x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
  x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
  x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
  x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')

  x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
  x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
  x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
  x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
  x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
  x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')
  x0 = x

  x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
  x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
  x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')


  # Ensure that the model takes into account
  # any potential predecessors of `input_tensor`.
  if input_tensor is not None:
    inputs = get_source_inputs(input_tensor)
  else:
    inputs = img_input
  # Create model.
  model = Model(inputs, [x, x0], name='resnet50')

  # load weights
  if weights == 'imagenet':
    if include_top:
      weights_path = get_file(
          'resnet50_weights_tf_dim_ordering_tf_kernels.h5',
          WEIGHTS_PATH,
          cache_subdir='models',
          md5_hash='a7b3fe01876f51b976af0dea6bc144eb')
    else:
      weights_path = "./saved_model/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5"
    model.load_weights(weights_path)
  return model
예제 #26
0
def BN(x):
    return BatchNormalization()(x)
예제 #27
0
import numpy as np
import pylab as plt

#NN to create movie

from tensorflow.contrib.keras.python.keras.layers import ConvLSTM2D, BatchNormalization, Conv3D
from tensorflow.contrib.keras.python.keras.models import Sequential

seq = Sequential()
seq.add(
    ConvLSTM2D(filters=40,
               kernel_size=(3, 3),
               input_shape=(None, 40, 40, 1),
               padding='same',
               return_sequences=True))
seq.add(BatchNormalization())

seq.add(
    ConvLSTM2D(filters=40,
               kernel_size=(3, 3),
               padding='same',
               return_sequences=True))
seq.add(BatchNormalization())

seq.add(
    ConvLSTM2D(filters=40,
               kernel_size=(3, 3),
               padding='same',
               return_sequences=True))
seq.add(BatchNormalization())
예제 #28
0
    input_shape = (img_rows, img_cols, channels)

x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

inputs = Input(shape=input_shape)
x = Conv2D(num_filters,
           kernel_size=7,
           padding='same',
           strides=2,
           kernel_initializer='he_normal',
           kernel_regularizer=l2(1e-4))(inputs)
x = BatchNormalization()(x)
x = Activation('relu')(x)

if use_max_pool:
    x = MaxPooling2D(pool_size=3, strides=2, padding='same')(x)
    num_blocks = 3

# convolutional base (stack of blocks).
for i in range(num_blocks):
    for j in range(num_sub_blocks):
        strides = 1
        is_first_layer_but_not_first_block = j == 0 and i > 0

        if is_first_layer_but_not_first_block:
            strides = 2