コード例 #1
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 inputs 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':
        warnings.warn(
            '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(),
                                      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)

    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
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((100,10))*2
y = np.random.randint(2, size=(100,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', metrics=['accuracy'])

from tensorflow.contrib.keras.python.keras import callbacks

from tensorflow.contrib.keras.python.keras.callbacks import EarlyStopping

early_stopping = EarlyStopping(monitor='val_loss', patience=5) # patience=2

hist1=model.fit(x, y, validation_split=0.2, callbacks=[early_stopping], epochs=100)
print(hist1.history)

hist2=model.fit(x, y, validation_split=0.3, epochs=10)
# checkout all the losses and metrics
print(hist2.history)
コード例 #3
0
roi_input = Input(shape=(C.num_rois, 4))

# define the base network (resnet here, can be VGG, Inception, etc)
shared_layers = nn.nn_base(img_input, trainable=True)

# define the RPN, built on the base layers
num_anchors = len(C.anchor_box_scales) * len(C.anchor_box_ratios)
rpn = nn.rpn(shared_layers, num_anchors)

classifier = nn.classifier(shared_layers,
                           roi_input,
                           C.num_rois,
                           nb_classes=len(classes_count),
                           trainable=True)

model_rpn = Model(img_input, rpn[:2])
model_classifier = Model([img_input, roi_input], classifier)

# this is a model that holds both the RPN and the classifier, used to load/save weights for the models
model_all = Model([img_input, roi_input], rpn[:2] + classifier)

try:
    dprint('loading weights from {}'.format(C.base_net_weights))
    model_rpn.load_weights(C.base_net_weights, by_name=True)
    model_classifier.load_weights(C.base_net_weights, by_name=True)
except:
    dprint(
        'Could not load pretrained model weights. Weights can be found at {} and {}'
        .format(
            'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_th_dim_ordering_th_kernels_notop.h5',
            'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
コード例 #4
0
def Run(self, img_path, model_name):

    # config variables
    weights = 'imagenet'
    include_top = 0
    train_path = 'jpg'
    classfier_file = 'output/flowers_17/' + model_name + '/classifier.cpickle'

    # create the pretrained models
    # check for pretrained weight usage or not
    # check for top layers to be included or not
    if model_name == "vgg16":
        from vgg16 import VGG16, preprocess_input
        base_model = VGG16(weights=weights)
        model = Model(inputs=base_model.input,
                      outputs=base_model.get_layer('fc1').output)
        image_size = (224, 224)
    elif model_name == "vgg19":
        from vgg19 import VGG19, preprocess_input
        base_model = VGG19(weights=weights)
        model = Model(inputs=base_model.input,
                      outputs=base_model.get_layer('fc1').output)
        image_size = (224, 224)
    elif model_name == "resnet50":
        from resnet50 import ResNet50, preprocess_input
        base_model = ResNet50(weights=weights)
        model = Model(inputs=base_model.input,
                      outputs=base_model.get_layer('avg_pool').output)
        image_size = (224, 224)
    elif model_name == "inceptionv3":
        from inception_v3 import InceptionV3, preprocess_input
        base_model = InceptionV3(weights=weights)
        model = Model(inputs=base_model.input,
                      outputs=base_model.get_layer('mixed9').output)
        image_size = (299, 299)
    elif model_name == "xception":
        from xception import Xception, preprocess_input
        base_model = Xception(weights=weights)
        model = Model(inputs=base_model.input,
                      outputs=base_model.get_layer('avg_pool').output)
        image_size = (299, 299)
    else:
        base_model = None

    img = image.load_img(img_path, target_size=image_size)
    img_array = image.img_to_array(img)
    img_array = np.expand_dims(img_array, axis=0)
    img_array = preprocess_input(img_array)
    feature = model.predict(img_array)
    feature = feature.flatten()
    with open(classfier_file, 'rb') as f:
        model2 = pickle.load(f)

    pred = model2.predict(feature)
    prob = model2.predict_proba(np.atleast_2d(feature))[0]

    return pred, prob[0]
コード例 #5
0
def InceptionV3(include_top=True,
                weights='imagenet',
                input_tensor=None,
                input_shape=None,
                pooling=None,
                classes=1000):
  """Instantiates the Inception v3 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.
  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)` (with `channels_last` data format)
          or `(3, 299, 299)` (with `channels_first` data format).
          It should have exactly 3 inputs channels,
          and width and height should be no smaller than 139.
          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.
  """
  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=299,
      min_size=139,
      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_first':
    channel_axis = 1
  else:
    channel_axis = 3

  x = conv2d_bn(img_input, 32, 3, 3, strides=(2, 2), padding='valid')
  x = conv2d_bn(x, 32, 3, 3, padding='valid')
  x = conv2d_bn(x, 64, 3, 3)
  x = MaxPooling2D((3, 3), strides=(2, 2))(x)

  x = conv2d_bn(x, 80, 1, 1, padding='valid')
  x = conv2d_bn(x, 192, 3, 3, padding='valid')
  x = MaxPooling2D((3, 3), strides=(2, 2))(x)

  # mixed 0, 1, 2: 35 x 35 x 256
  branch1x1 = conv2d_bn(x, 64, 1, 1)

  branch5x5 = conv2d_bn(x, 48, 1, 1)
  branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)

  branch3x3dbl = conv2d_bn(x, 64, 1, 1)
  branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
  branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)

  branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
  branch_pool = conv2d_bn(branch_pool, 32, 1, 1)
  x = layers.concatenate(
      [branch1x1, branch5x5, branch3x3dbl, branch_pool],
      axis=channel_axis,
      name='mixed0')

  # mixed 1: 35 x 35 x 256
  branch1x1 = conv2d_bn(x, 64, 1, 1)

  branch5x5 = conv2d_bn(x, 48, 1, 1)
  branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)

  branch3x3dbl = conv2d_bn(x, 64, 1, 1)
  branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
  branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)

  branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
  branch_pool = conv2d_bn(branch_pool, 64, 1, 1)
  x = layers.concatenate(
      [branch1x1, branch5x5, branch3x3dbl, branch_pool],
      axis=channel_axis,
      name='mixed1')

  # mixed 2: 35 x 35 x 256
  branch1x1 = conv2d_bn(x, 64, 1, 1)

  branch5x5 = conv2d_bn(x, 48, 1, 1)
  branch5x5 = conv2d_bn(branch5x5, 64, 5, 5)

  branch3x3dbl = conv2d_bn(x, 64, 1, 1)
  branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
  branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)

  branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
  branch_pool = conv2d_bn(branch_pool, 64, 1, 1)
  x = layers.concatenate(
      [branch1x1, branch5x5, branch3x3dbl, branch_pool],
      axis=channel_axis,
      name='mixed2')

  # mixed 3: 17 x 17 x 768
  branch3x3 = conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='valid')

  branch3x3dbl = conv2d_bn(x, 64, 1, 1)
  branch3x3dbl = conv2d_bn(branch3x3dbl, 96, 3, 3)
  branch3x3dbl = conv2d_bn(
      branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='valid')

  branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x)
  x = layers.concatenate(
      [branch3x3, branch3x3dbl, branch_pool], axis=channel_axis, name='mixed3')

  # mixed 4: 17 x 17 x 768
  branch1x1 = conv2d_bn(x, 192, 1, 1)

  branch7x7 = conv2d_bn(x, 128, 1, 1)
  branch7x7 = conv2d_bn(branch7x7, 128, 1, 7)
  branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)

  branch7x7dbl = conv2d_bn(x, 128, 1, 1)
  branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1)
  branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 1, 7)
  branch7x7dbl = conv2d_bn(branch7x7dbl, 128, 7, 1)
  branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)

  branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
  branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
  x = layers.concatenate(
      [branch1x1, branch7x7, branch7x7dbl, branch_pool],
      axis=channel_axis,
      name='mixed4')

  # mixed 5, 6: 17 x 17 x 768
  for i in range(2):
    branch1x1 = conv2d_bn(x, 192, 1, 1)

    branch7x7 = conv2d_bn(x, 160, 1, 1)
    branch7x7 = conv2d_bn(branch7x7, 160, 1, 7)
    branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)

    branch7x7dbl = conv2d_bn(x, 160, 1, 1)
    branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)
    branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 1, 7)
    branch7x7dbl = conv2d_bn(branch7x7dbl, 160, 7, 1)
    branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)

    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
    branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
    x = layers.concatenate(
        [branch1x1, branch7x7, branch7x7dbl, branch_pool],
        axis=channel_axis,
        name='mixed' + str(5 + i))

  # mixed 7: 17 x 17 x 768
  branch1x1 = conv2d_bn(x, 192, 1, 1)

  branch7x7 = conv2d_bn(x, 192, 1, 1)
  branch7x7 = conv2d_bn(branch7x7, 192, 1, 7)
  branch7x7 = conv2d_bn(branch7x7, 192, 7, 1)

  branch7x7dbl = conv2d_bn(x, 192, 1, 1)
  branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1)
  branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)
  branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 7, 1)
  branch7x7dbl = conv2d_bn(branch7x7dbl, 192, 1, 7)

  branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
  branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
  x = layers.concatenate(
      [branch1x1, branch7x7, branch7x7dbl, branch_pool],
      axis=channel_axis,
      name='mixed7')

  # mixed 8: 8 x 8 x 1280
  branch3x3 = conv2d_bn(x, 192, 1, 1)
  branch3x3 = conv2d_bn(branch3x3, 320, 3, 3, strides=(2, 2), padding='valid')

  branch7x7x3 = conv2d_bn(x, 192, 1, 1)
  branch7x7x3 = conv2d_bn(branch7x7x3, 192, 1, 7)
  branch7x7x3 = conv2d_bn(branch7x7x3, 192, 7, 1)
  branch7x7x3 = conv2d_bn(
      branch7x7x3, 192, 3, 3, strides=(2, 2), padding='valid')

  branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x)
  x = layers.concatenate(
      [branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name='mixed8')

  # mixed 9: 8 x 8 x 2048
  for i in range(2):
    branch1x1 = conv2d_bn(x, 320, 1, 1)

    branch3x3 = conv2d_bn(x, 384, 1, 1)
    branch3x3_1 = conv2d_bn(branch3x3, 384, 1, 3)
    branch3x3_2 = conv2d_bn(branch3x3, 384, 3, 1)
    branch3x3 = layers.concatenate(
        [branch3x3_1, branch3x3_2], axis=channel_axis, name='mixed9_' + str(i))

    branch3x3dbl = conv2d_bn(x, 448, 1, 1)
    branch3x3dbl = conv2d_bn(branch3x3dbl, 384, 3, 3)
    branch3x3dbl_1 = conv2d_bn(branch3x3dbl, 384, 1, 3)
    branch3x3dbl_2 = conv2d_bn(branch3x3dbl, 384, 3, 1)
    branch3x3dbl = layers.concatenate(
        [branch3x3dbl_1, branch3x3dbl_2], axis=channel_axis)

    branch_pool = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(x)
    branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
    x = layers.concatenate(
        [branch1x1, branch3x3, branch3x3dbl, branch_pool],
        axis=channel_axis,
        name='mixed' + str(9 + i))
  if include_top:
    # Classification block
    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='inception_v3')

  # load weights
  if weights == 'imagenet':
    if K.image_data_format() == '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.')
    if include_top:
      weights_path = get_file(
          'inception_v3_weights_tf_dim_ordering_tf_kernels.h5',
          WEIGHTS_PATH,
          cache_subdir='models',
          md5_hash='9a0d58056eeedaa3f26cb7ebd46da564')
    else:
      weights_path = get_file(
          'inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5',
          WEIGHTS_PATH_NO_TOP,
          cache_subdir='models',
          md5_hash='bcbd6486424b2319ff4ef7d526e38f63')
    model.load_weights(weights_path)
    if K.backend() == 'theano':
      convert_all_kernels_in_model(model)
  return model
コード例 #6
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()
コード例 #7
0
def main():
    training_images, training_labels, test_images, test_labels = load_dataset()

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

    N = training_labels.size
    Nt = test_labels.size
    perm_train = np.random.permutation(N)
    training_labels = training_labels[perm_train]
    training_images = training_images[perm_train, :, :] / 255.0
    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
    epochs = 30

    # Defiining the placeholders
    input_data = tf.placeholder(dtype=tf.float32,
                                shape=[None, HEIGHT, WIDTH, 1],
                                name='data')
    input_labels = tf.placeholder(dtype=tf.float32,
                                  shape=[None, NUM_CLASSES],
                                  name='labels')
    do_rate = tf.placeholder(dtype=tf.float32, name='dropout_rate')
    # pdb.set_trace()

    with tf.name_scope('conv1'):
        with tf.variable_scope('conv1'):
            W_conv1 = tf.get_variable('w', [3, 3, 1, 32])
            b_conv1 = tf.get_variable('b', [32])
        conv1 = tf.nn.conv2d(input=input_data,
                             filter=W_conv1,
                             strides=[1, 1, 1, 1],
                             padding='SAME')
        relu1 = tf.nn.relu(conv1 + b_conv1)

    with tf.name_scope('pool1'):
        pool1 = tf.nn.max_pool(value=relu1,
                               ksize=[1, 2, 2, 1],
                               strides=[1, 2, 2, 1],
                               padding='SAME')

    with tf.name_scope('conv2'):
        with tf.variable_scope('conv2'):
            W_conv2 = tf.get_variable('w', [3, 3, 32, 32])
            b_conv2 = tf.get_variable('b', [32])
        conv2 = tf.nn.conv2d(input=pool1,
                             filter=W_conv2,
                             strides=[1, 1, 1, 1],
                             padding='VALID')
        relu2 = tf.nn.relu(conv2 + b_conv2)

    with tf.name_scope('pool2'):
        pool2 = tf.nn.max_pool(value=relu2,
                               ksize=[1, 2, 2, 1],
                               strides=[1, 2, 2, 1],
                               padding='SAME')

    with tf.name_scope('dense1'):
        with tf.variable_scope('dense1'):
            W_dense1 = tf.get_variable('w', [6 * 6 * 32, 128])
            b_dense1 = tf.get_variable('b', 128)
        flat = tf.reshape(pool2, [-1, 6 * 6 * 32], name='reshape')
        dense1 = tf.matmul(flat, W_dense1)
        relu3 = tf.nn.relu(dense1 + b_dense1)

    with tf.name_scope('dropout'):
        dropout = tf.nn.dropout(relu3, do_rate)

    with tf.name_scope('output'):
        with tf.variable_scope('output'):
            W_out = tf.get_variable('w', [128, NUM_CLASSES])
            b_out = tf.get_variable('b', [NUM_CLASSES])
        output = tf.matmul(dropout, W_out) + b_out
    '''
    ################################################################
    """ Using Keras layers instead """
    #input_layer = Input(shape=(HEIGHT, WIDTH, 1), name='input_layer')
    Kcnn1 = Conv2D(filters=32, kernel_size=3, strides=(1,1), padding='same', activation='relu')(input_data)
    Kmaxpool1 = MaxPooling2D(pool_size=2)(Kcnn1)
    Kcnn2 = Conv2D(filters=32, kernel_size=3, strides=(1,1), padding='valid', activation='relu')(Kmaxpool1)
    Kmaxpool2 = MaxPooling2D(pool_size=2)(Kcnn2)
    Kflat = Flatten()(Kmaxpool2)
    Kdense1 = Dense(units=128, activation='relu')(Kflat)
    Kdropout = Dropout(.5)(Kdense1)
    output = Dense(units=NUM_CLASSES, activation='softmax')(Kdropout)
    """ The rest of the code is almost the same as in pure_tf_mnist.py,
    except for the feed_dict, where instead of do_rate in tensorflow,
    we need to provide keras specific dropout tensor 'learning_phase'
    in the backend of Keras. """
    ################################################################
    '''

    print('\n\n')
    print('-------------------------------------------------------')
    print('--------------- Trainable parameters ------------------')
    print('-------------------------------------------------------')
    total_parameters = 0
    for v in tf.trainable_variables():
        shape = v.get_shape()
        print(shape)
        #pdb.set_trace()
        params = 1
        for dim in shape:
            params *= dim.value
        total_parameters += params
    print('total_parameters = {}'.format(total_parameters))
    print('-------------------------------------------------------\n\n')

    loss = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(labels=input_labels,
                                                logits=output,
                                                name='loss'))
    train_op = tf.train.AdamOptimizer(1e-4).minimize(loss)
    accuracy = tf.cast(
        tf.equal(tf.argmax(input_labels, 1), tf.argmax(output, 1)), tf.float32)

    print('')
    print('-------------------------------------------------------')
    print('---------- Starting a TF session ----------------------')
    print('-------------------------------------------------------')
    print('')

    tf_weights = []
    tf.set_random_seed(1234)
    # Training:
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        writer = tf.summary.FileWriter('graph', sess.graph)

        print('-------------------------------------------------------')
        print('--------------- Training phase ------------------------')
        print('-------------------------------------------------------')
        for i in range(epochs):
            steps = (int)(np.ceil(float(N) / float(BATCH_SIZE)))
            total_l = 0
            total_acc = 0
            for step in range(steps):
                x_in, y_in = get_batch(step, BATCH_SIZE, training_images,
                                       training_labels)
                l, acc, _ = sess.run([loss, accuracy, train_op], {
                    input_data: x_in,
                    input_labels: y_in,
                    do_rate: 0.5
                })
                total_l += l
                total_acc += np.sum(acc)
                #pdb.set_trace()
            total_acc /= np.float32(N)
            print(
                "Epoch {}: Training loss = {}, Training accuracy = {}".format(
                    i, total_l, total_acc))

        # Test:
        total_acc = 0
        steps = (int)(np.ceil(float(Nt) / float(BATCH_SIZE)))
        for step in range(steps):
            x_in, y_in = get_batch(step, BATCH_SIZE, test_images, test_labels)
            acc = sess.run([accuracy], {
                input_data: x_in,
                input_labels: y_in,
                do_rate: 1
            })
            total_acc += np.sum(acc)
        total_acc /= np.float32(Nt)
        print('\n-----------------------')
        print("Test accuracy = {}".format(total_acc))
        print('-------------------------------------------------------')

        #################################################################
        ### Exporting the trained weights into a list of numpy vectors
        for v in tf.trainable_variables():
            tf_weights.append(sess.run(v))

        writer.close()

    print('')
    print('-------------------------------------------------------')
    print('---------- Starting a Keras session -------------------')
    print('-------------------------------------------------------')
    print('')

    #################################################################
    """ Building a Keras Model """
    input_layer = Input(shape=(HEIGHT, WIDTH, 1), name='input_layer')
    Kkcnn1 = Conv2D(filters=32,
                    kernel_size=3,
                    strides=(1, 1),
                    padding='same',
                    activation='relu')(input_layer)
    Kkmaxpool1 = MaxPooling2D(pool_size=2)(Kkcnn1)
    Kkcnn2 = Conv2D(filters=32,
                    kernel_size=3,
                    strides=(1, 1),
                    padding='valid',
                    activation='relu')(Kkmaxpool1)
    Kkmaxpool2 = MaxPooling2D(pool_size=2)(Kkcnn2)
    Kkflat = Flatten()(Kkmaxpool2)
    Kkdense1 = Dense(units=128, activation='relu')(Kkflat)
    Kkdropout = Dropout(.5)(Kkdense1)
    output_layer = Dense(units=NUM_CLASSES, activation='softmax')(Kkdropout)
    model = Model(inputs=input_layer, outputs=output_layer)
    model.compile(optimizer=tf.train.AdamOptimizer(),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    #################################################################

    #################################################################
    ### Loarding the already trained weights, onto the keras layers
    c = 0  # counter for iterating over tensorflow trainable variables
    #pdb.set_trace()
    for l in model.layers:
        trainable_weights = l.trainable_weights
        if not trainable_weights:
            # empty trainable weight list in this keras layer; so move on to the next layer.
            continue
        len_w = len(
            trainable_weights
        )  # e.g. for a normal conv layer, it is two: weight and bias.
        l.set_weights(tf_weights[c:c + len_w])
        c += len_w

    accuracy = model.evaluate(x=test_images,
                              y=test_labels,
                              batch_size=BATCH_SIZE)
    print('\n')
    print('Keras test score = {}'.format(accuracy))
    print('\n')
コード例 #8
0
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()
bn_array.shape  # compare to see for equality
(bn_array == bn_array_train).sum()  # total differ

#### Access Dropout layer's output as array in both test and train mode

# test mode from Model method
コード例 #9
0
```python
frozen_layer = Dense(32, trainable=False)
```

Additionally, you can set the `trainable` property of a layer to `True` or `False` after instantiation. For this to take effect, you will need to call `compile()` on your model after modifying the `trainable` property. Here's an example:
"""
from tensorflow.contrib.keras.python.keras.layers import Input, Dense
from tensorflow.contrib.keras.python.keras.models import Model
import numpy as np

input_tensor = Input(shape=(32, ))
layer = Dense(5)
layer.trainable = False
tensor_no_train = layer(input_tensor)

frozen_model = Model(input_tensor, tensor_no_train)
# in the model below, the weights of `layer` will not be updated during training
frozen_model.compile(optimizer='rmsprop', loss='mse')

layer.trainable = True
tensor_do_train = layer(input_tensor)
trainable_model = Model(input_tensor, tensor_do_train)
# with this model the weights of the layer will be updated during training
# (which will also affect the above model since it uses the same layer instance)
trainable_model.compile(optimizer='rmsprop', loss='mse')

# create dataset
data = np.random.random((100, 32))  # check on source
labels = np.random.randint(5, size=(100, 5))  # check source

frozen_model.fit(data, labels, epochs=5)  # no validation_set, then no val_loss
コード例 #10
0
ファイル: test_frcnn.py プロジェクト: htamakos/keras-frcnn
feature_map_input = Input(shape=input_shape_features)

# define the base network (resnet here, can be VGG, Inception, etc)
shared_layers = nn.nn_base(img_input, trainable=True)

# define the RPN, built on the base layers
num_anchors = len(C.anchor_box_scales) * len(C.anchor_box_ratios)
rpn_layers = nn.rpn(shared_layers, num_anchors)

classifier = nn.classifier(feature_map_input,
                           roi_input,
                           C.num_rois,
                           nb_classes=len(class_mapping),
                           trainable=True)

model_rpn = Model(img_input, rpn_layers)
model_classifier_only = Model([feature_map_input, roi_input], classifier)

model_classifier = Model([feature_map_input, roi_input], classifier)

model_rpn.load_weights(C.model_path, by_name=True)
model_classifier.load_weights(C.model_path, by_name=True)

model_rpn.compile(optimizer='sgd', loss='mse')
model_classifier.compile(optimizer='sgd', loss='mse')

all_imgs = []

classes = {}

bbox_threshold = 0.8
コード例 #11
0
"""
"""
todo: K.learning_phase()???

One simple way is to create a new `Model` that will output the layers that you are interested in:
"""

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

input_tensor = Input(shape=(100, ), name="input_tensor")
inter_tensor = Dense(30, name="my_layer")(input_tensor)
final_tensor = Dense(30, name="final_layer")(inter_tensor)
model = Model(input_tensor, final_tensor)  # create the original model

layer_name = 'my_layer'
intermediate_layer_model = Model(inputs=model.input,
                                 outputs=model.get_layer(layer_name).output)

# must compile before predict? No, but must compile before training
input_array1 = np.random.random((1000, 100)) * 9
input_tensor1 = K.constant(value=input_array1)
intermediate_output = intermediate_layer_model.predict(
    input_array1)  # return array
intermediate_output1 = intermediate_layer_model(
    input_tensor1
)  # return tensor not array; tensor is no use and a long way to go to reach array
"""
Alternatively, you can build a Keras function that will return the output of a certain layer given a certain input, for example:
コード例 #12
0
def SSD300(input_shape, num_classes=21):
    """SSD300 architecture.

    # Arguments
        input_shape: Shape of the input image,
            expected to be either (300, 300, 3) or (3, 300, 300)(not tested).
        num_classes: Number of classes including background.

    # References
        https://arxiv.org/abs/1512.02325
    """
    net = {}
    # Block 1
    input_tensor = input_tensor = Input(shape=input_shape)
    img_size = (input_shape[1], input_shape[0])
    net['input'] = input_tensor
    net['conv1_1'] = Convolution2D(64, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   name='conv1_1')(net['input'])

    net['conv1_2'] = Convolution2D(64, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   name='conv1_2')(net['conv1_1'])

    net['pool1'] = MaxPooling2D((2, 2),
                                strides=(2, 2),
                                padding='same',
                                name='pool1')(net['conv1_2'])
    # Block 2
    net['conv2_1'] = Convolution2D(128, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   name='conv2_1')(net['pool1'])

    net['conv2_2'] = Convolution2D(128, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   name='conv2_2')(net['conv2_1'])
    net['pool2'] = MaxPooling2D((2, 2),
                                strides=(2, 2),
                                padding='same',
                                name='pool2')(net['conv2_2'])

    # Block 3
    net['conv3_1'] = Convolution2D(256, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   name='conv3_1')(net['pool2'])
    net['conv3_2'] = Convolution2D(256, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   name='conv3_2')(net['conv3_1'])
    net['conv3_3'] = Convolution2D(256, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   name='conv3_3')(net['conv3_2'])
    net['pool3'] = MaxPooling2D((2, 2),
                                strides=(2, 2),
                                padding='same',
                                name='pool3')(net['conv3_3'])
    # Block 4
    net['conv4_1'] = Convolution2D(512, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   name='conv4_1')(net['pool3'])
    net['conv4_2'] = Convolution2D(512, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   name='conv4_2')(net['conv4_1'])
    net['conv4_3'] = Convolution2D(512, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   name='conv4_3')(net['conv4_2'])
    net['pool4'] = MaxPooling2D((2, 2),
                                strides=(2, 2),
                                padding='same',
                                name='pool4')(net['conv4_3'])
    # Block 5
    net['conv5_1'] = Convolution2D(512, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   name='conv5_1')(net['pool4'])
    net['conv5_2'] = Convolution2D(512, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   name='conv5_2')(net['conv5_1'])
    net['conv5_3'] = Convolution2D(512, (3, 3),
                                   activation='relu',
                                   padding='same',
                                   name='conv5_3')(net['conv5_2'])
    net['pool5'] = MaxPooling2D((3, 3),
                                strides=(1, 1),
                                padding='same',
                                name='pool5')(net['conv5_3'])
    # FC6
    net['fc6'] = Convolution2D(1024, (3, 3),
                               dilation_rate=(6, 6),
                               activation='relu',
                               padding='same',
                               name='fc6')(net['pool5'])
    # x = Dropout(0.5, name='drop6')(x)
    # FC7
    net['fc7'] = Convolution2D(1024, (1, 1),
                               activation='relu',
                               padding='same',
                               name='fc7')(net['fc6'])
    # x = Dropout(0.5, name='drop7')(x)
    # Block 6
    net['conv6_1'] = Convolution2D(256, (1, 1),
                                   activation='relu',
                                   padding='same',
                                   name='conv6_1')(net['fc7'])

    net['conv6_2'] = Convolution2D(512, (3, 3),
                                   strides=(2, 2),
                                   activation='relu',
                                   padding='same',
                                   name='conv6_2')(net['conv6_1'])
    # Block 7
    net['conv7_1'] = Convolution2D(128, (1, 1),
                                   activation='relu',
                                   padding='same',
                                   name='conv7_1')(net['conv6_2'])
    net['conv7_2'] = ZeroPadding2D()(net['conv7_1'])
    net['conv7_2'] = Convolution2D(256, (3, 3),
                                   strides=(2, 2),
                                   activation='relu',
                                   padding='valid',
                                   name='conv7_2')(net['conv7_2'])
    # Block 8
    net['conv8_1'] = Convolution2D(128, (1, 1),
                                   activation='relu',
                                   padding='same',
                                   name='conv8_1')(net['conv7_2'])
    net['conv8_2'] = Convolution2D(256, (3, 3),
                                   strides=(2, 2),
                                   activation='relu',
                                   padding='same',
                                   name='conv8_2')(net['conv8_1'])
    # Last Pool
    net['pool6'] = GlobalAveragePooling2D(name='pool6')(net['conv8_2'])
    # Prediction from conv4_3
    net['conv4_3_norm'] = Normalize(20, name='conv4_3_norm')(net['conv4_3'])
    num_priors = 3
    x = Convolution2D(num_priors * 4, (3, 3),
                      padding='same',
                      name='conv4_3_norm_mbox_loc')(net['conv4_3_norm'])
    net['conv4_3_norm_mbox_loc'] = x
    flatten = Flatten(name='conv4_3_norm_mbox_loc_flat')
    net['conv4_3_norm_mbox_loc_flat'] = flatten(net['conv4_3_norm_mbox_loc'])
    name = 'conv4_3_norm_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    x = Convolution2D(num_priors * num_classes, (3, 3),
                      padding='same',
                      name=name)(net['conv4_3_norm'])
    net['conv4_3_norm_mbox_conf'] = x
    flatten = Flatten(name='conv4_3_norm_mbox_conf_flat')
    net['conv4_3_norm_mbox_conf_flat'] = flatten(net['conv4_3_norm_mbox_conf'])
    priorbox = PriorBox(img_size,
                        30.0,
                        aspect_ratios=[2],
                        variances=[0.1, 0.1, 0.2, 0.2],
                        name='conv4_3_norm_mbox_priorbox')
    net['conv4_3_norm_mbox_priorbox'] = priorbox(net['conv4_3_norm'])
    # Prediction from fc7
    num_priors = 6
    net['fc7_mbox_loc'] = Convolution2D(num_priors * 4, (3, 3),
                                        padding='same',
                                        name='fc7_mbox_loc')(net['fc7'])
    flatten = Flatten(name='fc7_mbox_loc_flat')
    net['fc7_mbox_loc_flat'] = flatten(net['fc7_mbox_loc'])
    name = 'fc7_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    net['fc7_mbox_conf'] = Convolution2D(num_priors * num_classes, (3, 3),
                                         padding='same',
                                         name=name)(net['fc7'])
    flatten = Flatten(name='fc7_mbox_conf_flat')
    net['fc7_mbox_conf_flat'] = flatten(net['fc7_mbox_conf'])
    priorbox = PriorBox(img_size,
                        60.0,
                        max_size=114.0,
                        aspect_ratios=[2, 3],
                        variances=[0.1, 0.1, 0.2, 0.2],
                        name='fc7_mbox_priorbox')
    net['fc7_mbox_priorbox'] = priorbox(net['fc7'])
    # Prediction from conv6_2
    num_priors = 6
    x = Convolution2D(num_priors * 4, (3, 3),
                      padding='same',
                      name='conv6_2_mbox_loc')(net['conv6_2'])
    net['conv6_2_mbox_loc'] = x
    flatten = Flatten(name='conv6_2_mbox_loc_flat')
    net['conv6_2_mbox_loc_flat'] = flatten(net['conv6_2_mbox_loc'])
    name = 'conv6_2_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    x = Convolution2D(num_priors * num_classes, (3, 3),
                      padding='same',
                      name=name)(net['conv6_2'])
    net['conv6_2_mbox_conf'] = x
    flatten = Flatten(name='conv6_2_mbox_conf_flat')
    net['conv6_2_mbox_conf_flat'] = flatten(net['conv6_2_mbox_conf'])
    priorbox = PriorBox(img_size,
                        114.0,
                        max_size=168.0,
                        aspect_ratios=[2, 3],
                        variances=[0.1, 0.1, 0.2, 0.2],
                        name='conv6_2_mbox_priorbox')
    net['conv6_2_mbox_priorbox'] = priorbox(net['conv6_2'])
    # Prediction from conv7_2
    num_priors = 6
    x = Convolution2D(num_priors * 4, (3, 3),
                      padding='same',
                      name='conv7_2_mbox_loc')(net['conv7_2'])
    net['conv7_2_mbox_loc'] = x
    flatten = Flatten(name='conv7_2_mbox_loc_flat')
    net['conv7_2_mbox_loc_flat'] = flatten(net['conv7_2_mbox_loc'])
    name = 'conv7_2_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    x = Convolution2D(num_priors * num_classes, (3, 3),
                      padding='same',
                      name=name)(net['conv7_2'])
    net['conv7_2_mbox_conf'] = x
    flatten = Flatten(name='conv7_2_mbox_conf_flat')
    net['conv7_2_mbox_conf_flat'] = flatten(net['conv7_2_mbox_conf'])
    priorbox = PriorBox(img_size,
                        168.0,
                        max_size=222.0,
                        aspect_ratios=[2, 3],
                        variances=[0.1, 0.1, 0.2, 0.2],
                        name='conv7_2_mbox_priorbox')
    net['conv7_2_mbox_priorbox'] = priorbox(net['conv7_2'])
    # Prediction from conv8_2
    num_priors = 6
    x = Convolution2D(num_priors * 4, (3, 3),
                      padding='same',
                      name='conv8_2_mbox_loc')(net['conv8_2'])
    net['conv8_2_mbox_loc'] = x
    flatten = Flatten(name='conv8_2_mbox_loc_flat')
    net['conv8_2_mbox_loc_flat'] = flatten(net['conv8_2_mbox_loc'])
    name = 'conv8_2_mbox_conf'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    x = Convolution2D(num_priors * num_classes, (3, 3),
                      padding='same',
                      name=name)(net['conv8_2'])
    net['conv8_2_mbox_conf'] = x
    flatten = Flatten(name='conv8_2_mbox_conf_flat')
    net['conv8_2_mbox_conf_flat'] = flatten(net['conv8_2_mbox_conf'])
    priorbox = PriorBox(img_size,
                        222.0,
                        max_size=276.0,
                        aspect_ratios=[2, 3],
                        variances=[0.1, 0.1, 0.2, 0.2],
                        name='conv8_2_mbox_priorbox')
    net['conv8_2_mbox_priorbox'] = priorbox(net['conv8_2'])
    # Prediction from pool6
    num_priors = 6
    x = Dense(num_priors * 4, name='pool6_mbox_loc_flat')(net['pool6'])
    net['pool6_mbox_loc_flat'] = x
    name = 'pool6_mbox_conf_flat'
    if num_classes != 21:
        name += '_{}'.format(num_classes)
    x = Dense(num_priors * num_classes, name=name)(net['pool6'])
    net['pool6_mbox_conf_flat'] = x
    priorbox = PriorBox(img_size,
                        276.0,
                        max_size=330.0,
                        aspect_ratios=[2, 3],
                        variances=[0.1, 0.1, 0.2, 0.2],
                        name='pool6_mbox_priorbox')
    target_shape = (1, 1, 256)
    net['pool6_reshaped'] = Reshape(target_shape,
                                    name='pool6_reshaped')(net['pool6'])
    net['pool6_mbox_priorbox'] = priorbox(net['pool6_reshaped'])
    # Gather all predictions
    net['mbox_loc'] = concatenate([
        net['conv4_3_norm_mbox_loc_flat'], net['fc7_mbox_loc_flat'],
        net['conv6_2_mbox_loc_flat'], net['conv7_2_mbox_loc_flat'],
        net['conv8_2_mbox_loc_flat'], net['pool6_mbox_loc_flat']
    ],
                                  axis=1,
                                  name='mbox_loc')
    net['mbox_conf'] = concatenate([
        net['conv4_3_norm_mbox_conf_flat'], net['fc7_mbox_conf_flat'],
        net['conv6_2_mbox_conf_flat'], net['conv7_2_mbox_conf_flat'],
        net['conv8_2_mbox_conf_flat'], net['pool6_mbox_conf_flat']
    ],
                                   axis=1,
                                   name='mbox_conf')
    net['mbox_priorbox'] = concatenate([
        net['conv4_3_norm_mbox_priorbox'], net['fc7_mbox_priorbox'],
        net['conv6_2_mbox_priorbox'], net['conv7_2_mbox_priorbox'],
        net['conv8_2_mbox_priorbox'], net['pool6_mbox_priorbox']
    ],
                                       axis=1,
                                       name='mbox_priorbox')
    num_boxes = K.int_shape(net['mbox_loc'])[-1] // 4
    net['mbox_loc'] = Reshape((num_boxes, 4),
                              name='mbox_loc_final')(net['mbox_loc'])
    net['mbox_conf'] = Reshape((num_boxes, num_classes),
                               name='mbox_conf_logits')(net['mbox_conf'])
    net['mbox_conf'] = Activation('softmax',
                                  name='mbox_conf_final')(net['mbox_conf'])
    net['predictions'] = concatenate(
        [net['mbox_loc'], net['mbox_conf'], net['mbox_priorbox']],
        axis=2,
        #axis = 0,
        name='predictions')
    model = Model(net['input'], net['predictions'])
    return model
コード例 #13
0
ファイル: mobilenet.py プロジェクト: jiayouwyhit/tensorflow
def MobileNet(input_shape=None,  # pylint: disable=invalid-name
              alpha=1.0,
              depth_multiplier=1,
              dropout=1e-3,
              include_top=True,
              weights='imagenet',
              input_tensor=None,
              pooling=None,
              classes=1000):
  """Instantiates the MobileNet architecture.

  Note that only TensorFlow is supported for now,
  therefore it only works with the data format
  `image_data_format='channels_last'` in your Keras config
  at `~/.keras/keras.json`.

  To load a MobileNet model via `load_model`, import the custom
  objects `relu6` and `DepthwiseConv2D` and pass them to the
  `custom_objects` parameter.
  E.g.
  model = load_model('mobilenet.h5', custom_objects={
                     'relu6': mobilenet.relu6,
                     'DepthwiseConv2D': mobilenet.DepthwiseConv2D})

  Arguments:
      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 input channels,
          and width and height should be no smaller than 32.
          E.g. `(200, 200, 3)` would be one valid value.
      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: depth multiplier for depthwise convolution
          (also called the resolution multiplier)
      dropout: dropout rate
      include_top: whether to include the fully-connected
          layer at the top of the network.
      weights: `None` (random initialization) or
          `imagenet` (ImageNet weights)
      input_tensor: optional Keras tensor (i.e. output of
          `layers.Input()`)
          to use as image input for the model.
      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 K.backend() != 'tensorflow':
    raise RuntimeError('Only TensorFlow backend is currently supported, '
                       'as other backends do not support '
                       'depthwise convolution.')

  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.
  if input_shape is None:
    default_size = 224
  else:
    if K.image_data_format() == 'channels_first':
      rows = input_shape[1]
      cols = input_shape[2]
    else:
      rows = input_shape[0]
      cols = input_shape[1]
    if rows == cols and rows in [128, 160, 192, 224]:
      default_size = rows
    else:
      default_size = 224
  input_shape = _obtain_input_shape(
      input_shape,
      default_size=default_size,
      min_size=32,
      data_format=K.image_data_format(),
      require_flatten=include_top,
      weights=weights)
  if K.image_data_format() == 'channels_last':
    row_axis, col_axis = (0, 1)
  else:
    row_axis, col_axis = (1, 2)
  rows = input_shape[row_axis]
  cols = input_shape[col_axis]

  if weights == 'imagenet':
    if depth_multiplier != 1:
      raise ValueError('If imagenet weights are being loaded, '
                       'depth multiplier must be 1')

    if alpha not in [0.25, 0.50, 0.75, 1.0]:
      raise ValueError('If imagenet weights are being loaded, '
                       'alpha can be one of'
                       '`0.25`, `0.50`, `0.75` or `1.0` only.')

    if rows != cols or rows not in [128, 160, 192, 224]:
      raise ValueError('If imagenet weights are being loaded, '
                       'input must have a static square shape (one of '
                       '(128,128), (160,160), (192,192), or (224, 224)).'
                       ' Input shape provided = %s' % (input_shape,))

  if K.image_data_format() != 'channels_last':
    warnings.warn('The MobileNet family of models 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

  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

  x = _conv_block(img_input, 32, alpha, strides=(2, 2))
  x = _depthwise_conv_block(x, 64, alpha, depth_multiplier, block_id=1)

  x = _depthwise_conv_block(
      x, 128, alpha, depth_multiplier, strides=(2, 2), block_id=2)
  x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3)

  x = _depthwise_conv_block(
      x, 256, alpha, depth_multiplier, strides=(2, 2), block_id=4)
  x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, block_id=5)

  x = _depthwise_conv_block(
      x, 512, alpha, depth_multiplier, strides=(2, 2), block_id=6)
  x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=7)
  x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=8)
  x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=9)
  x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=10)
  x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=11)

  x = _depthwise_conv_block(
      x, 1024, alpha, depth_multiplier, strides=(2, 2), block_id=12)
  x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, block_id=13)

  if include_top:
    if K.image_data_format() == 'channels_first':
      shape = (int(1024 * alpha), 1, 1)
    else:
      shape = (1, 1, int(1024 * alpha))

    x = GlobalAveragePooling2D()(x)
    x = Reshape(shape, name='reshape_1')(x)
    x = Dropout(dropout, name='dropout')(x)
    x = Conv2D(classes, (1, 1), padding='same', name='conv_preds')(x)
    x = Activation('softmax', name='act_softmax')(x)
    x = Reshape((classes,), name='reshape_2')(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='mobilenet_%0.2f_%s' % (alpha, rows))

  # load weights
  if weights == 'imagenet':
    if K.image_data_format() == 'channels_first':
      raise ValueError('Weights for "channels_last" format '
                       'are not available.')
    if alpha == 1.0:
      alpha_text = '1_0'
    elif alpha == 0.75:
      alpha_text = '7_5'
    elif alpha == 0.50:
      alpha_text = '5_0'
    else:
      alpha_text = '2_5'

    if include_top:
      model_name = 'mobilenet_%s_%d_tf.h5' % (alpha_text, rows)
      weigh_path = BASE_WEIGHT_PATH + model_name
      weights_path = get_file(model_name, weigh_path, cache_subdir='models')
    else:
      model_name = 'mobilenet_%s_%d_tf_no_top.h5' % (alpha_text, rows)
      weigh_path = BASE_WEIGHT_PATH + model_name
      weights_path = get_file(model_name, weigh_path, cache_subdir='models')
    model.load_weights(weights_path)

  if old_data_format:
    K.set_image_data_format(old_data_format)
  return model
コード例 #14
0
from tensorflow.contrib.keras.python.keras.layers import Input, Dense
from tensorflow.contrib.keras.python.keras.models import Model
from tensorflow.contrib.keras.python.keras.utils import to_categorical
import numpy as np

# create an input tensor (placeholder)
inputs = Input(shape=(784, ))  # 1-D this case

# a layer instance is callable on a tensor, and returns a tensor
x = Dense(64, activation='relu')(inputs)
x = Dense(64, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)

# Create a model needs an input tensor and an output tensor
model = Model(inputs=inputs, outputs=predictions)

# before training, a model has to have optimizer, loss and metrics
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# Generate dummy data
import numpy as np
data = np.random.random((1000, 784))
labels = np.random.randint(10, size=(1000, 1))
labels = to_categorical(labels, num_classes=10)

model.fit(data, labels, validation_split=0.2)  # starts training
"""
Use_2: model as layer
コード例 #15
0
ファイル: resnet50.py プロジェクト: AlbertXiebnu/tensorflow
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:
    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')

  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 = get_file(
          'resnet50_weights_tf_dim_ordering_tf_kernels.h5',
          WEIGHTS_PATH,
          cache_subdir='models',
          md5_hash='a7b3fe01876f51b976af0dea6bc144eb')
    else:
      weights_path = get_file(
          'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
          WEIGHTS_PATH_NO_TOP,
          cache_subdir='models',
          md5_hash='a268eb855778b3df3c7506639542a6af')
    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
コード例 #16
0
def MobileNet(
        input_shape=None,  # pylint: disable=invalid-name
        alpha=1.0,
        depth_multiplier=1,
        dropout=1e-3,
        include_top=True,
        weights='imagenet',
        input_tensor=None,
        pooling=None,
        classes=1000):
    """Instantiates the MobileNet architecture.

  Note that only TensorFlow is supported for now,
  therefore it only works with the data format
  `image_data_format='channels_last'` in your Keras config
  at `~/.keras/keras.json`.

  To load a MobileNet model via `load_model`, import the custom
  objects `relu6` and `DepthwiseConv2D` and pass them to the
  `custom_objects` parameter.
  E.g.
  model = load_model('mobilenet.h5', custom_objects={
                     'relu6': mobilenet.relu6,
                     'DepthwiseConv2D': mobilenet.DepthwiseConv2D})

  Arguments:
      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 32.
          E.g. `(200, 200, 3)` would be one valid value.
      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: depth multiplier for depthwise convolution
          (also called the resolution multiplier)
      dropout: dropout rate
      include_top: whether to include the fully-connected
          layer at the top of the network.
      weights: `None` (random initialization) or
          `imagenet` (ImageNet weights)
      input_tensor: optional Keras tensor (i.e. output of
          `layers.Input()`)
          to use as image input for the model.
      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 K.backend() != 'tensorflow':
        raise RuntimeError('Only TensorFlow backend is currently supported, '
                           'as other backends do not support '
                           'depthwise convolution.')

    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=32,
                                      data_format=K.image_data_format(),
                                      include_top=include_top or weights)
    if K.image_data_format() == 'channels_last':
        row_axis, col_axis = (0, 1)
    else:
        row_axis, col_axis = (1, 2)
    rows = input_shape[row_axis]
    cols = input_shape[col_axis]

    if weights == 'imagenet':
        if depth_multiplier != 1:
            raise ValueError('If imagenet weights are being loaded, '
                             'depth multiplier must be 1')

        if alpha not in [0.25, 0.50, 0.75, 1.0]:
            raise ValueError('If imagenet weights are being loaded, '
                             'alpha can be one of'
                             '`0.25`, `0.50`, `0.75` or `1.0` only.')

        if rows != cols or rows not in [128, 160, 192, 224]:
            raise ValueError('If imagenet weights are being loaded, '
                             'input must have a static square shape (one of '
                             '(128,128), (160,160), (192,192), or (224, 224)).'
                             ' Input shape provided = %s' % (input_shape, ))

    if K.image_data_format() != 'channels_last':
        warnings.warn('The MobileNet family of models 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

    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

    x = _conv_block(img_input, 32, alpha, strides=(2, 2))
    x = _depthwise_conv_block(x, 64, alpha, depth_multiplier, block_id=1)

    x = _depthwise_conv_block(x,
                              128,
                              alpha,
                              depth_multiplier,
                              strides=(2, 2),
                              block_id=2)
    x = _depthwise_conv_block(x, 128, alpha, depth_multiplier, block_id=3)

    x = _depthwise_conv_block(x,
                              256,
                              alpha,
                              depth_multiplier,
                              strides=(2, 2),
                              block_id=4)
    x = _depthwise_conv_block(x, 256, alpha, depth_multiplier, block_id=5)

    x = _depthwise_conv_block(x,
                              512,
                              alpha,
                              depth_multiplier,
                              strides=(2, 2),
                              block_id=6)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=7)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=8)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=9)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=10)
    x = _depthwise_conv_block(x, 512, alpha, depth_multiplier, block_id=11)

    x = _depthwise_conv_block(x,
                              1024,
                              alpha,
                              depth_multiplier,
                              strides=(2, 2),
                              block_id=12)
    x = _depthwise_conv_block(x, 1024, alpha, depth_multiplier, block_id=13)

    if include_top:
        if K.image_data_format() == 'channels_first':
            shape = (int(1024 * alpha), 1, 1)
        else:
            shape = (1, 1, int(1024 * alpha))

        x = GlobalAveragePooling2D()(x)
        x = Reshape(shape, name='reshape_1')(x)
        x = Dropout(dropout, name='dropout')(x)
        x = Conv2D(classes, (1, 1), padding='same', name='conv_preds')(x)
        x = Activation('softmax', name='act_softmax')(x)
        x = Reshape((classes, ), name='reshape_2')(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='mobilenet_%0.2f_%s' % (alpha, rows))

    # load weights
    if weights == 'imagenet':
        if K.image_data_format() == 'channels_first':
            raise ValueError('Weights for "channels_last" format '
                             'are not available.')
        if alpha == 1.0:
            alpha_text = '1_0'
        elif alpha == 0.75:
            alpha_text = '7_5'
        elif alpha == 0.50:
            alpha_text = '5_0'
        else:
            alpha_text = '2_5'

        if include_top:
            model_name = 'mobilenet_%s_%d_tf.h5' % (alpha_text, rows)
            weigh_path = BASE_WEIGHT_PATH + model_name
            weights_path = get_file(model_name,
                                    weigh_path,
                                    cache_subdir='models')
        else:
            model_name = 'mobilenet_%s_%d_tf_no_top.h5' % (alpha_text, rows)
            weigh_path = BASE_WEIGHT_PATH + model_name
            weights_path = get_file(model_name,
                                    weigh_path,
                                    cache_subdir='models')
        model.load_weights(weights_path)

    if old_data_format:
        K.set_image_data_format(old_data_format)
    return model
コード例 #17
0
def VGG16(include_top=True,
          weights='imagenet',
          input_tensor=None,
          input_shape=None,
          pooling=None,
          classes=1000):
    """Instantiates the VGG16 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 3 fully-connected
          layers 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 48.
          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.
  """
    ### how many weights option can we be allowed
    if weights not in {'imagenet', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `imagenet` '
                         '(pre-training on ImageNet).')

    ### if use imagenet weights and add last 3 dense layers, then class should be 1000
    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')

    ### set input shape : (224, 224, 3)
    # default input shape for VGG16 model, designed for imagenet dataset
    input_shape = _obtain_input_shape(
        input_shape,  # if set must be a tuple of 3 integers (50, 50, 3)
        default_size=224,  # if input_shape set, here must be None
        min_size=48,  # 48, but freely change it to your need
        data_format=K.image_data_format(
        ),  # 'channels_first' or 'channels_last'
        include_top=include_top
    )  # True, then must use 224 or False to be other number

    ### Create input tensor: real tensor or container?
    if input_tensor is None:
        # create input tensor placeholder
        img_input = Input(shape=input_shape)
    else:
        img_input = Input(tensor=input_tensor, shape=input_shape)

    # Block 1
    x = Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               name='block1_conv1')(img_input)

    ## how to access weights of each layer
    block1_conv1 = x
    block1_conv1_bias = block1_conv1.graph._collections['trainable_variables'][
        -1]  # bias
    block1_conv1_kernel = block1_conv1.graph._collections[
        'trainable_variables'][-2]  # kernel

    x = Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               name='block1_conv2')(x)
    block1_conv2 = x
    block1_conv2_bias = block1_conv2.graph._collections['trainable_variables'][
        -1]  # bias
    block1_conv2_kernel = block1_conv2.graph._collections[
        'trainable_variables'][-2]  # kernel

    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
    block1_pool = x
    # access trainable_variables or weights with biases
    block1_pool.graph._collections['variables'][-1]  # bias
    block1_pool.graph._collections['variables'][-2]  # kernel

    # Block 2
    x = Conv2D(128, (3, 3),
               activation='relu',
               padding='same',
               name='block2_conv1')(x)
    block2_conv1 = x

    x = Conv2D(128, (3, 3),
               activation='relu',
               padding='same',
               name='block2_conv2')(x)
    block2_conv2 = x

    x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
    block2_pool = x

    # Block 3
    x = Conv2D(256, (3, 3),
               activation='relu',
               padding='same',
               name='block3_conv1')(x)
    block3_conv1 = x

    x = Conv2D(256, (3, 3),
               activation='relu',
               padding='same',
               name='block3_conv2')(x)
    block3_conv2 = x

    x = Conv2D(256, (3, 3),
               activation='relu',
               padding='same',
               name='block3_conv3')(x)
    block3_conv3 = x

    x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
    block3_pool = x

    # Block 4
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block4_conv1')(x)
    block4_conv1 = x

    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block4_conv2')(x)
    block4_conv2 = x

    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block4_conv3')(x)
    block4_conv3 = x

    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
    block4_pool = x

    # Block 5
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block5_conv1')(x)
    block5_conv1 = x

    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block5_conv2')(x)
    block5_conv2 = x

    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block5_conv3')(x)
    block5_conv3 = x

    x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)
    block5_pool = x

    if include_top:
        # Classification block
        x = Flatten(name='flatten')(x)
        flatten = x
        x = Dense(4096, activation='relu', name='fc1')(x)
        fc1 = x
        x = Dense(4096, activation='relu', name='fc2')(x)
        fc2 = x
        x = Dense(classes, activation='softmax', name='predictions')(x)
        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='vgg16')

    # load weights
    if weights == 'imagenet':
        if include_top:
            weights_path = get_file(
                'vgg16_weights_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH,
                cache_subdir='models')
        else:
            weights_path = get_file(
                'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
                WEIGHTS_PATH_NO_TOP,
                cache_subdir='models')
        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='block5_pool')
                shape = maxpool.output_shape[1:]
                dense = model.get_layer(name='fc1')
                layer_utils.convert_dense_weights_data_format(
                    dense, shape, 'channels_first')
    return model
コード例 #18
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
コード例 #19
0
def InceptionResNetV2(include_top=True,
                      weights='imagenet',
                      input_tensor=None,
                      input_shape=None,
                      pooling=None,
                      classes=1000):
    """Instantiates the Inception-ResNet v2 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 TensorFlow, Theano and
    CNTK backends. The data format convention used by the model is
    the one specified in your Keras config file.
    Note that the default input image size for this model is 299x299, instead
    of 224x224 as in the VGG16 and ResNet models. Also, the input preprocessing
    function is different (i.e., do not use `imagenet_utils.preprocess_input()`
    with this model. Use `preprocess_input()` defined in this module instead).
    # 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)` (with `'channels_last'` data format)
            or `(3, 299, 299)` (with `'channels_first'` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 139.
            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.
    """
    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=299,
                                      min_size=139,
                                      data_format=K.image_data_format(),
                                      require_flatten=False,
                                      weights=weights)

    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

    # Stem block: 35 x 35 x 192
    x = conv2d_bn(img_input, 32, 3, strides=2, padding='valid')
    x = conv2d_bn(x, 32, 3, padding='valid')
    x = conv2d_bn(x, 64, 3)
    x = MaxPooling2D(3, strides=2)(x)
    x = conv2d_bn(x, 80, 1, padding='valid')
    x = conv2d_bn(x, 192, 3, padding='valid')
    x = MaxPooling2D(3, strides=2)(x)

    # Mixed 5b (Inception-A block): 35 x 35 x 320
    branch_0 = conv2d_bn(x, 96, 1)
    branch_1 = conv2d_bn(x, 48, 1)
    branch_1 = conv2d_bn(branch_1, 64, 5)
    branch_2 = conv2d_bn(x, 64, 1)
    branch_2 = conv2d_bn(branch_2, 96, 3)
    branch_2 = conv2d_bn(branch_2, 96, 3)
    branch_pool = AveragePooling2D(3, strides=1, padding='same')(x)
    branch_pool = conv2d_bn(branch_pool, 64, 1)
    branches = [branch_0, branch_1, branch_2, branch_pool]
    channel_axis = 1 if K.image_data_format() == 'channels_first' else 3
    x = Concatenate(axis=channel_axis, name='mixed_5b')(branches)

    # 10x block35 (Inception-ResNet-A block): 35 x 35 x 320
    for block_idx in range(1, 11):
        x = inception_resnet_block(x,
                                   scale=0.17,
                                   block_type='block35',
                                   block_idx=block_idx)

    # Mixed 6a (Reduction-A block): 17 x 17 x 1088
    branch_0 = conv2d_bn(x, 384, 3, strides=2, padding='valid')
    branch_1 = conv2d_bn(x, 256, 1)
    branch_1 = conv2d_bn(branch_1, 256, 3)
    branch_1 = conv2d_bn(branch_1, 384, 3, strides=2, padding='valid')
    branch_pool = MaxPooling2D(3, strides=2, padding='valid')(x)
    branches = [branch_0, branch_1, branch_pool]
    x = Concatenate(axis=channel_axis, name='mixed_6a')(branches)

    # 20x block17 (Inception-ResNet-B block): 17 x 17 x 1088
    for block_idx in range(1, 21):
        x = inception_resnet_block(x,
                                   scale=0.1,
                                   block_type='block17',
                                   block_idx=block_idx)

    # Mixed 7a (Reduction-B block): 8 x 8 x 2080
    branch_0 = conv2d_bn(x, 256, 1)
    branch_0 = conv2d_bn(branch_0, 384, 3, strides=2, padding='valid')
    branch_1 = conv2d_bn(x, 256, 1)
    branch_1 = conv2d_bn(branch_1, 288, 3, strides=2, padding='valid')
    branch_2 = conv2d_bn(x, 256, 1)
    branch_2 = conv2d_bn(branch_2, 288, 3)
    branch_2 = conv2d_bn(branch_2, 320, 3, strides=2, padding='valid')
    branch_pool = MaxPooling2D(3, strides=2, padding='valid')(x)
    branches = [branch_0, branch_1, branch_2, branch_pool]
    x = Concatenate(axis=channel_axis, name='mixed_7a')(branches)

    # 10x block8 (Inception-ResNet-C block): 8 x 8 x 2080
    for block_idx in range(1, 10):
        x = inception_resnet_block(x,
                                   scale=0.2,
                                   block_type='block8',
                                   block_idx=block_idx)
    x = inception_resnet_block(x,
                               scale=1.,
                               activation=None,
                               block_type='block8',
                               block_idx=10)

    # Final convolution block: 8 x 8 x 1536
    x = conv2d_bn(x, 1536, 1, name='conv_7b')

    if include_top:
        # Classification block
        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='inception_resnet_v2')

    # Load weights
    if weights == 'imagenet':
        if K.image_data_format() == '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.')
        if include_top:
            weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5'
            weights_path = get_file(
                weights_filename,
                BASE_WEIGHT_URL + weights_filename,
                cache_subdir='models',
                file_hash='e693bd0210a403b3192acc6073ad2e96')
        else:
            weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5'
            weights_path = get_file(
                weights_filename,
                BASE_WEIGHT_URL + weights_filename,
                cache_subdir='models',
                file_hash='d19885ff4a710c122648d3b5c3b684e4')
        model.load_weights(weights_path)

    return model
コード例 #20
0
           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)
H = BatchNormalization()(H)
H = Activation('relu')(H)
H = Conv2D(1, 1, 1, padding='same', kernel_initializer='random_normal')(H)
g_V = Activation('sigmoid')(H)
generator = Model(inputs=g_input, outputs=g_V)
generator.compile(loss='binary_crossentropy', optimizer=opt)
generator.summary()

# Build Discriminative model ...
d_input = Input(shape=shp)
H = Convolution2D(256,
                  kernel_size=(5, 5),
                  strides=(2, 2),
                  padding='same',
                  activation='relu')(d_input)
H = LeakyReLU(0.2)(H)
H = Dropout(dropout_rate)(H)
H = Convolution2D(512,
                  kernel_size=(5, 5),
                  strides=(2, 2),
コード例 #21
0
def VGG19(include_top=True,
          weights='imagenet',
          input_tensor=None,
          input_shape=None,
          pooling=None,
          classes=1000):
  """Instantiates the VGG19 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 3 fully-connected
          layers 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 input channels,
          and width and height should be no smaller than 48.
          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=48,
      data_format=K.image_data_format(),
      require_flatten=include_top,
      weights=weights)

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

  # Block 1
  x = Conv2D(
      64, (3, 3), activation='relu', padding='same',
      name='block1_conv1')(img_input)
  x = Conv2D(
      64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)
  x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)

  # Block 2
  x = Conv2D(
      128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)
  x = Conv2D(
      128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)
  x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)

  # Block 3
  x = Conv2D(
      256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)
  x = Conv2D(
      256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)
  x = Conv2D(
      256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)
  x = Conv2D(
      256, (3, 3), activation='relu', padding='same', name='block3_conv4')(x)
  x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)

  # Block 4
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block4_conv4')(x)
  x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)

  # Block 5
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block5_conv4')(x)
  x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)

  if include_top:
    # Classification block
    x = Flatten(name='flatten')(x)
    x = Dense(4096, activation='relu', name='fc1')(x)
    x = Dense(4096, activation='relu', name='fc2')(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='vgg19')

  # load weights
  if weights == 'imagenet':
    if include_top:
      weights_path = get_file(
          'vgg19_weights_tf_dim_ordering_tf_kernels.h5',
          WEIGHTS_PATH,
          cache_subdir='models')
    else:
      weights_path = get_file(
          'vgg19_weights_tf_dim_ordering_tf_kernels_notop.h5',
          WEIGHTS_PATH_NO_TOP,
          cache_subdir='models')
    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='block5_pool')
        shape = maxpool.output_shape[1:]
        dense = model.get_layer(name='fc1')
        layer_utils.convert_dense_weights_data_format(dense, shape,
                                                      'channels_first')
  return model
def create_AlexNet(num_fc_neurons,
                   dropout_rate,
                   num_classes=24,
                   img_height=224,
                   img_width=224,
                   include_loc='all',
                   activation='softmax'):
    weight_decay = 0.0005
    kernel_regularizer = regularizers.l2(weight_decay)
    bias_regularizer = regularizers.l2(weight_decay)

    # build a convolutional model
    base_model = Sequential()
    base_model.add(
        Conv2D(96, (11, 11),
               strides=(4, 4),
               padding='valid',
               activation='relu',
               kernel_regularizer=kernel_regularizer,
               bias_regularizer=bias_regularizer,
               name='conv1',
               input_shape=get_input_shape(img_height, img_width)))
    base_model.add(LRN2D(name='lrn1'))
    base_model.add(MaxPooling2D((3, 3), strides=(2, 2), name='pool1'))

    base_model.add(
        Conv2D(256, (5, 5),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_regularizer=kernel_regularizer,
               bias_regularizer=bias_regularizer,
               name='conv2'))
    base_model.add(LRN2D(name='lrn2'))
    base_model.add(MaxPooling2D((3, 3), strides=(2, 2), name='pool2'))

    base_model.add(
        Conv2D(384, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_regularizer=kernel_regularizer,
               bias_regularizer=bias_regularizer,
               name='conv3'))
    base_model.add(
        Conv2D(384, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_regularizer=kernel_regularizer,
               bias_regularizer=bias_regularizer,
               name='conv4'))
    base_model.add(
        Conv2D(256, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_regularizer=kernel_regularizer,
               bias_regularizer=bias_regularizer,
               name='conv5'))
    base_model.add(MaxPooling2D((3, 3), strides=(2, 2), name='pool3'))

    # build a classifier model to put on top of the convolutional model
    top_model = Sequential()
    top_model.add(Flatten(input_shape=base_model.output_shape[1:]))
    for i in range(6, 8):
        top_model.add(
            Dense(num_fc_neurons,
                  kernel_regularizer=kernel_regularizer,
                  bias_regularizer=bias_regularizer,
                  name='fc' + str(i)))
        #top_model.add(BatchNormalization(axis=1, name='fc'+str(i)+'_bn'))
        top_model.add(Activation('relu', name='fc' + str(i) + '_ac'))
        top_model.add(Dropout(dropout_rate))
    top_model.add(
        Dense(num_classes,
              activation=activation,
              kernel_regularizer=kernel_regularizer,
              bias_regularizer=bias_regularizer,
              name='predictions'))

    if include_loc == 'base':
        model = base_model
    elif include_loc == 'top':
        model = top_model
    elif include_loc == 'all':  # add the model on top of the convolutional base
        model = Model(inputs=base_model.input,
                      outputs=top_model(base_model.output))
    else:
        raise ValueError('Only "base", "top" and "all" can be included.')
    return model
コード例 #23
0
def mobilenet_clf_head(inputs, num_classes):
    x = mobilenet_base(inputs)
    x = l.GlobalAvgPool2D(name='avg_pool1')(x)
    x = l.Dense(num_classes, activation='softmax', name='dense_pred')(x)
    model = Model(inputs, x)
    return model
コード例 #24
0
# Training
sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH, ), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)

x = Conv1D(128, 5, activation='relu')(embedded_sequences)

print('sequence_input: ', sequence_input)
print('embedded_sequences: ', embedded_sequences)
print('x: ', x)

x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = MaxPooling1D(35)(x)  # global max pooling
x = Flatten()(x)
x = Dense(128, activation='relu')(x)
preds = Dense(len(labels_index), activation='softmax')(x)

model = Model(sequence_input, preds)
model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['acc'])

# happy learning!
model.fit(x_train,
          y_train,
          validation_data=(x_val, y_val),
          epochs=2,
          batch_size=128)
コード例 #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 input 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(),
      require_flatten=include_top,
      weights=weights)

  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 = Conv2D(64, (7, 7),
             strides=(2, 2), padding='same', name='conv1')(img_input)
  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 = get_file(
          'resnet50_weights_tf_dim_ordering_tf_kernels.h5',
          WEIGHTS_PATH,
          cache_subdir='models',
          md5_hash='a7b3fe01876f51b976af0dea6bc144eb')
    else:
      weights_path = get_file(
          'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
          WEIGHTS_PATH_NO_TOP,
          cache_subdir='models',
          md5_hash='a268eb855778b3df3c7506639542a6af')
    model.load_weights(weights_path)
  return model
コード例 #26
0
# In[8]:


from tensorflow.contrib.keras.python.keras.models import Sequential


# In[9]:


# create the pretrained models 
# check for pretrained weight usage or not
# check for top layers to be included or not
if model_name == "vgg16":
	base_model = VGG16(weights=weights)
	model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output)
	image_size = (224, 224)
elif model_name == "vgg19":
	base_model = VGG19(weights=weights)
	model = Model(inputs=base_model.input, outputs=base_model.get_layer('fc1').output)
	image_size = (224, 224)
elif model_name == "resnet50":
	base_model = ResNet50(weights=weights)
	model = Model(inputs=base_model.input, outputs=base_model.get_layer('flatten').output)
	image_size = (224, 224)
elif model_name == "inceptionv3":
	base_model = InceptionV3(weights=weights)
	model = Model(inputs=base_model.input, outputs=base_model.get_layer('mixed9').output)
	image_size = (299, 299)
elif model_name == "xception":
	base_model = Xception(weights=weights)
コード例 #27
0
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'
 '        you can also pass a dictionary\n'
 '        mapping input names to Numpy arrays.\n'
 '    y: Numpy array of target data,\n'
 '        or list of Numpy arrays if the model has multiple outputs.\n'
コード例 #28
0
#######################################################
# check whether 3 models are the same or not
model = Sequential()
model.add(Dense(2, input_dim=2,
                name='dense1'))  # 2 nodes reaches 50% accuracy, 8 nodes 100%
model.add(Activation('relu', name='dense1_act'))
model.add(Dense(1, name='dense2'))
model.add(Activation('sigmoid', name='dense2_act'))  # output shaped by sigmoid
model.summary()  # see each layer of a model

# use Model instead of Sequential
input_tensor = Input(shape=(2, ), name='input')
hidden = Dense(2, activation='relu', name='dense1_relu')(input_tensor)
output = Dense(1, activation='sigmoid',
               name='dense2_sigm')(hidden)  # output shaped by sigmoid
model1 = Model(inputs=input_tensor, outputs=output)
model1.summary()  # see each layer of a model
"""
# use Model to split layer and activation
input_tensor = Input(shape=(2,))
hidden = Dense(2)(input_tensor)
relu_hid = K.relu(hidden)
dense_out = Dense(1)(relu_hid) # output shaped by sigmoid
sigmoid_out = K.sigmoid(dense_out)

# inputs and outputs must be layer tensor, not just tensor made from K
model2 = Model(inputs=input_tensor, outputs=sigmoid_out)

model2.summary() # see each layer of a model
"""
コード例 #29
0
x = Dropout(0.3)(x)

x = Conv2D(64, (8, 1), padding='same', use_bias=False)(inputs)
x = Conv2D(64, (8, 1), padding='same', use_bias=False, activation='relu')(x)
x = BatchNormalization(axis=1)(x)
x = Dropout(0.3)(x)

x = Conv2D(64, (1, 5), padding='valid', use_bias=False, activation='relu')(x)

x = Reshape((128, 64))(x)
x = Bidirectional(LSTM(20, dropout=0.2, return_sequences=False))(x)

#x = Flatten()(x)

x = Dense(256, use_bias=False, activation='relu')(x)
x = BatchNormalization(axis=-1)(x)
x = Dropout(0.5)(x)
logits = Dense(2, use_bias=False)(x)

model = Model(inputs=inputs, outputs=logits)
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.summary()
history = model.fit(train,
                    train_label,
                    validation_split=2 / 7,
                    batch_size=batch_size,
                    epochs=epochs,
                    shuffle=True)
コード例 #30
0
  "data_format='channels_first'\n",
  '      or 4D tensor with shape:\n',
  '      `(samples, new_rows, new_cols, filters)` if '
  "data_format='channels_last'.\n",
  '      `rows` and `cols` values might have changed due to padding.\n',
"""

# create a flattened placeholder with conv2d tensor is a must, if to link to a dense layer
# no additional args are needed for Flatten layer
flattened = layers.Flatten(name='flatten')(conv2d_tensor)

# create a dense placeholder with flatten tensor
dense_2_cls = layers.Dense(2, activation='softmax', name='dense_2_cls')(flattened)

# create a simple model start from input layer, a conv2d layer, flatten layer, to dense layer
model = Model(input_tensor, dense_2_cls, name='con2d_simdl')

# see the model
model.summary()

# compile the model
lr = 0.001
model.compile(optimizer=Adam(lr=lr),
		loss='categorical_crossentropy', metrics=['accuracy'])

"""
def compile(self,\n',
  '              optimizer,\n',
  '              loss,\n',
  '              metrics=None,\n',
  '              loss_weights=None,\n',
コード例 #31
0
ファイル: xception.py プロジェクト: jiayouwyhit/tensorflow
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
コード例 #32
0
# load pretrained model
model_inception_v3 = inception_v3.InceptionV3(include_top=False,
                                              weights='imagenet',
                                              input_tensor=None,
                                              input_shape=None,
                                              pooling=None)
# add a global spatial average pooling layer
x = model_inception_v3.output
x = GlobalAveragePooling2D()(x)
# add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer
predictions = Dense(5, activation='softmax')(x)
# the model for transform learning
model = Model(inputs=model_inception_v3.input, outputs=predictions)
print model.summary()

# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers
for layer in model_inception_v3.layers:
    layer.trainable = False

# load dataset
# flowers_dataset = create_image_dataset(FLAGS.image_dir, training_percentage=FLAGS.training_percentage,
#                                        testing_percentage=FLAGS.testing_percentage)
image_data_generator = ImageDataGenerator()
train_data = image_data_generator.flow_from_directory(
    '/home/meizu/WORK/code/YF_baidu_ML/dataset/flowers/flower_photos/train',
    target_size=(FLAGS.input_image_size, FLAGS.input_image_size),
    batch_size=FLAGS.batch_size,
コード例 #33
0
ファイル: vgg16.py プロジェクト: jiayouwyhit/tensorflow
def VGG16(include_top=True,
          weights='imagenet',
          input_tensor=None,
          input_shape=None,
          pooling=None,
          classes=1000):
  """Instantiates the VGG16 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 3 fully-connected
          layers 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 input channels,
          and width and height should be no smaller than 48.
          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=48,
      data_format=K.image_data_format(),
      require_flatten=include_top,
      weights=weights)

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

  # Block 1
  x = Conv2D(
      64, (3, 3), activation='relu', padding='same',
      name='block1_conv1')(img_input)
  x = Conv2D(
      64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)
  x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)

  # Block 2
  x = Conv2D(
      128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)
  x = Conv2D(
      128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)
  x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)

  # Block 3
  x = Conv2D(
      256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)
  x = Conv2D(
      256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)
  x = Conv2D(
      256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)
  x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)

  # Block 4
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)
  x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)

  # Block 5
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)
  x = Conv2D(
      512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)
  x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)

  if include_top:
    # Classification block
    x = Flatten(name='flatten')(x)
    x = Dense(4096, activation='relu', name='fc1')(x)
    x = Dense(4096, activation='relu', name='fc2')(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='vgg16')

  # load weights
  if weights == 'imagenet':
    if include_top:
      weights_path = get_file(
          'vgg16_weights_tf_dim_ordering_tf_kernels.h5',
          WEIGHTS_PATH,
          cache_subdir='models')
    else:
      weights_path = get_file(
          'vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',
          WEIGHTS_PATH_NO_TOP,
          cache_subdir='models')
    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='block5_pool')
        shape = maxpool.output_shape[1:]
        dense = model.get_layer(name='fc1')
        layer_utils.convert_dense_weights_data_format(dense, shape,
                                                      'channels_first')
  return model
コード例 #34
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