Ejemplo n.º 1
0
# # model.add(TimeDistributed(Dropout(0.5)))
# # model.add(TimeDistributed(Dense(2)))
# # model.add(TimeDistributed(Activation('sigmoid')))

# # model.add(LSTM(64, return_sequences = True, dropout=0.2, recurrent_dropout=0.2, activation='relu'))
# model.add(LSTM(64, dropout=0.2, recurrent_dropout=0.2, activation='relu'))
# model.add(Dense(512))
# model.add(Dropout(0.5))
# model.add(Dense(2))
# model.add(Activation('sigmoid'))

#CLDNN
model.add(
    TimeDistributed(Conv2D(16, kernel_size=7, strides=2, padding='same'),
                    input_shape=(498, 20, 20, 1)))
model.add(TimeDistributed(BatchNormalization()))
model.add(TimeDistributed(Activation("relu")))

model.add(TimeDistributed(Conv2D(32, kernel_size=5, strides=2,
                                 padding='same')))
model.add(TimeDistributed(BatchNormalization()))
model.add(TimeDistributed(Activation("relu")))

model.add(TimeDistributed(Conv2D(64, kernel_size=3, strides=2,
                                 padding='same')))
model.add(TimeDistributed(BatchNormalization()))
model.add(TimeDistributed(Activation("relu")))

model.add(TimeDistributed(Reshape((64, 9))))
model.add(TimeDistributed(Flatten()))
Ejemplo n.º 2
0
def build_3d_cnn(w, h, d, s, num_outputs):
    #Credit: https://github.com/jessecha/DNRacing/blob/master/3D_CNN_Model/model.py
    '''
        w : width
        h : height
        d : depth
        s : n_stacked
    '''
    input_shape=(s, h, w, d)

    model = Sequential()
    #First layer
    #model.add(Cropping3D(cropping=((0,0), (50,10), (0,0)), input_shape=input_shape) ) #trim pixels off top
    
    # Second layer
    model.add(Conv3D(
        filters=16, kernel_size=(3,3,3), strides=(1,3,3),
        data_format='channels_last', padding='same', input_shape=input_shape)
    )
    model.add(Activation('relu'))
    model.add(MaxPooling3D(
        pool_size=(1,2,2), strides=(1,2,2), padding='valid', data_format=None)
    )
    # Third layer
    model.add(Conv3D(
        filters=32, kernel_size=(3,3,3), strides=(1,1,1),
        data_format='channels_last', padding='same')
    )
    model.add(Activation('relu'))
    model.add(MaxPooling3D(
        pool_size=(1, 2, 2), strides=(1,2,2), padding='valid', data_format=None)
    )
    # Fourth layer
    model.add(Conv3D(
        filters=64, kernel_size=(3,3,3), strides=(1,1,1),
        data_format='channels_last', padding='same')
    )
    model.add(Activation('relu'))
    model.add(MaxPooling3D(
        pool_size=(1,2,2), strides=(1,2,2), padding='valid', data_format=None)
    )
    # Fifth layer
    model.add(Conv3D(
        filters=128, kernel_size=(3,3,3), strides=(1,1,1),
        data_format='channels_last', padding='same')
    )
    model.add(Activation('relu'))
    model.add(MaxPooling3D(
        pool_size=(1,2,2), strides=(1,2,2), padding='valid', data_format=None)
    )
    # Fully connected layer
    model.add(Flatten())

    model.add(Dense(256))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(256))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Dropout(0.5))

    model.add(Dense(num_outputs))
    #model.add(Activation('tanh'))

    return model
Ejemplo n.º 3
0
 def d_layer(layer_input, filters, f_size=4, bn=True):
     d = Conv2D(filters, kernel_size=f_size, strides=2, padding='same')(layer_input)
     if bn:
         d = BatchNormalization(momentum=0.8)(d)
     return d
Ejemplo n.º 4
0
    def block2_modul3_4(self, net, name):
        # 1x1
        net_1x1 = Conv2D(filters=192,
                         kernel_size=1,
                         padding='same',
                         activation='relu',
                         name=name + "_1x1_conv1",
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net)
        net_1x1 = BatchNormalization()(net_1x1)

        # 1x7
        net_1x7 = Conv2D(filters=160,
                         kernel_size=(1, 1),
                         padding='same',
                         activation='relu',
                         name=name + '_1x7_conv1',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net)
        net_1x7 = BatchNormalization()(net_1x7)
        net_1x7 = Conv2D(filters=160,
                         kernel_size=(1, 7),
                         padding='same',
                         activation='relu',
                         name=name + '_1x7_conv2',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_1x7)
        net_1x7 = BatchNormalization()(net_1x7)
        net_1x7 = Conv2D(filters=192,
                         kernel_size=(7, 1),
                         padding='same',
                         activation='relu',
                         name=name + '_1x7_conv3',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_1x7)
        net_1x7 = BatchNormalization()(net_1x7)

        net_7x1 = Conv2D(filters=160,
                         kernel_size=(1, 1),
                         padding='same',
                         activation='relu',
                         name=name + '_7x1_conv1',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net)
        net_7x1 = Conv2D(filters=160,
                         kernel_size=(7, 1),
                         padding='same',
                         activation='relu',
                         name=name + '_7x1_conv2',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_7x1)
        net_7x1 = Conv2D(filters=160,
                         kernel_size=(7, 1),
                         padding='same',
                         activation='relu',
                         name=name + '_7x1_conv3',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_7x1)
        net_7x1 = Conv2D(filters=192,
                         kernel_size=(1, 7),
                         padding='same',
                         activation='relu',
                         name=name + '_7x1_conv4',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_7x1)

        net_avg = AvgPool2D(pool_size=3,
                            strides=1,
                            padding='same',
                            name=name + '_1x1_avg')(net)
        net_avg = Conv2D(filters=192,
                         kernel_size=1,
                         padding='same',
                         activation='relu',
                         name=name + '_1x1_avg_conv1',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_avg)
        net = concatenate([net_1x1, net_1x7, net_7x1, net_avg], axis=-1)

        return net
Ejemplo n.º 5
0
    def block1_module2(self, net, name):
        # 1x1
        net_1x1 = Conv2D(filters=64,
                         kernel_size=1,
                         padding='same',
                         activation='relu',
                         name=name + '_1x1',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net)
        net_1x1 = BatchNormalization()(net_1x1)

        # 5x5
        net_5x5 = Conv2D(filters=48,
                         kernel_size=1,
                         padding='same',
                         activation='relu',
                         name=name + '_5x5',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net)
        net_5x5 = BatchNormalization(net_5x5)
        net_5x5 = Conv2D(filters=64,
                         kernel_size=5,
                         padding='same',
                         activation='relu',
                         name=name + '_5x5_2',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_5x5)
        net_5x5 = BatchNormalization()(net_5x5)

        # 3x3
        net_3x3 = Conv2D(filters=64,
                         kernel_size=1,
                         padding='same',
                         activation='relu',
                         name=name + '_3x3',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net)
        net_3x3 = BatchNormalization()(net_3x3)
        net_3x3 = Conv2D(filters=96,
                         kernel_size=3,
                         activation='relu',
                         padding='same',
                         name=name + '_3x3_2',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_3x3)
        net_3x3 = BatchNormalization()(net_3x3)
        net_3x3 = Conv2D(filters=96,
                         kernel_size=3,
                         activation='relu',
                         padding='same',
                         name=name + '_3x3_3',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_3x3)
        net_3x3 = BatchNormalization()(net_3x3)

        # 1x1xavg
        net_1x1_avg = AvgPool2D(pool_size=3,
                                strides=1,
                                padding='same',
                                name=name + '_net_1x1_avg')(net)
        net_1x1_avg = Conv2D(filters=64,
                             kernel_size=1,
                             activation='relu',
                             padding='same',
                             name=name + '_net_1x1_avg_conv1',
                             kernel_regularizer=regularizers.l2(
                                 self.weight_decay))(net_1x1_avg)
        net = concatenate([net_1x1, net_5x5, net_3x3, net_1x1_avg],
                          axis=-1,
                          name=name + '_mixed')
        return net
Ejemplo n.º 6
0
def _adjust_block(p, ip, filters, block_id=None):
    """Adjusts the input `previous path` to match the shape of the `input`.

  Used in situations where the output number of filters needs to be changed.

  Arguments:
      p: Input tensor which needs to be modified
      ip: Input tensor whose shape needs to be matched
      filters: Number of output filters to be matched
      block_id: String block_id

  Returns:
      Adjusted Keras tensor
  """
    channel_dim = 1 if K.image_data_format() == 'channels_first' else -1
    img_dim = 2 if K.image_data_format() == 'channels_first' else -2

    ip_shape = K.int_shape(ip)

    if p is not None:
        p_shape = K.int_shape(p)

    with K.name_scope('adjust_block'):
        if p is None:
            p = ip

        elif p_shape[img_dim] != ip_shape[img_dim]:
            with K.name_scope('adjust_reduction_block_%s' % block_id):
                p = Activation('relu', name='adjust_relu_1_%s' % block_id)(p)

                p1 = AveragePooling2D(
                    (1, 1),
                    strides=(2, 2),
                    padding='valid',
                    name='adjust_avg_pool_1_%s' % block_id)(p)
                p1 = Conv2D(filters // 2, (1, 1),
                            padding='same',
                            use_bias=False,
                            name='adjust_conv_1_%s' % block_id,
                            kernel_initializer='he_normal')(p1)

                p2 = ZeroPadding2D(padding=((0, 1), (0, 1)))(p)
                p2 = Cropping2D(cropping=((1, 0), (1, 0)))(p2)
                p2 = AveragePooling2D(
                    (1, 1),
                    strides=(2, 2),
                    padding='valid',
                    name='adjust_avg_pool_2_%s' % block_id)(p2)
                p2 = Conv2D(filters // 2, (1, 1),
                            padding='same',
                            use_bias=False,
                            name='adjust_conv_2_%s' % block_id,
                            kernel_initializer='he_normal')(p2)

                p = concatenate([p1, p2], axis=channel_dim)
                p = BatchNormalization(axis=channel_dim,
                                       momentum=0.9997,
                                       epsilon=1e-3,
                                       name='adjust_bn_%s' % block_id)(p)

        elif p_shape[channel_dim] != filters:
            with K.name_scope('adjust_projection_block_%s' % block_id):
                p = Activation('relu')(p)
                p = Conv2D(filters, (1, 1),
                           strides=(1, 1),
                           padding='same',
                           name='adjust_conv_projection_%s' % block_id,
                           use_bias=False,
                           kernel_initializer='he_normal')(p)
                p = BatchNormalization(axis=channel_dim,
                                       momentum=0.9997,
                                       epsilon=1e-3,
                                       name='adjust_bn_%s' % block_id)(p)
    return p
Ejemplo n.º 7
0
def NASNet(input_shape=None,
           penultimate_filters=4032,
           num_blocks=6,
           stem_block_filters=96,
           skip_reduction=True,
           filter_multiplier=2,
           include_top=True,
           weights=None,
           input_tensor=None,
           pooling=None,
           classes=1000,
           default_size=None):
    """Instantiates a NASNet model.

  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`.

  Arguments:
      input_shape: Optional shape tuple, the input shape
          is by default `(331, 331, 3)` for NASNetLarge and
          `(224, 224, 3)` for NASNetMobile.
          It should have exactly 3 inputs channels,
          and width and height should be no smaller than 32.
          E.g. `(224, 224, 3)` would be one valid value.
      penultimate_filters: Number of filters in the penultimate layer.
          NASNet models use the notation `NASNet (N @ P)`, where:
              -   N is the number of blocks
              -   P is the number of penultimate filters
      num_blocks: Number of repeated blocks of the NASNet model.
          NASNet models use the notation `NASNet (N @ P)`, where:
              -   N is the number of blocks
              -   P is the number of penultimate filters
      stem_block_filters: Number of filters in the initial stem block
      skip_reduction: Whether to skip the reduction step at the tail
          end of the network. Set to `False` for CIFAR models.
      filter_multiplier: Controls the width of the network.
          - If `filter_multiplier` < 1.0, proportionally decreases the number
              of filters in each layer.
          - If `filter_multiplier` > 1.0, proportionally increases the number
              of filters in each layer.
          - If `filter_multiplier` = 1, default number of filters from the
               paper are used at each layer.
      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.
      default_size: Specifies the default image size of the model

  Returns:
      A Keras model instance.

  Raises:
      ValueError: In case of invalid argument for `weights`,
          invalid input shape or invalid `penultimate_filters` value.
      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 '
                           'separable convolution.')

    if not (weights in {'imagenet', None} or os.path.exists(weights)):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization), `imagenet` '
                         '(pre-training on ImageNet), '
                         'or the path to the weights file to be loaded.')

    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 (isinstance(input_shape, tuple) and None in input_shape
            and weights == 'imagenet'):
        raise ValueError('When specifying the input shape of a NASNet'
                         ' and loading `ImageNet` weights, '
                         'the input_shape argument must be static '
                         '(no None entries). Got: `input_shape=' +
                         str(input_shape) + '`.')

    if default_size is None:
        default_size = 331

    # Determine proper input shape and default size.
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=default_size,
                                      min_size=32,
                                      data_format=K.image_data_format(),
                                      require_flatten=False,
                                      weights=weights)

    if K.image_data_format() != 'channels_last':
        logging.warning(
            'The NASNet 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

    if penultimate_filters % 24 != 0:
        raise ValueError(
            'For NASNet-A models, the value of `penultimate_filters` '
            'needs to be divisible by 24. Current value: %d' %
            penultimate_filters)

    channel_dim = 1 if K.image_data_format() == 'channels_first' else -1
    filters = penultimate_filters // 24

    if not skip_reduction:
        x = Conv2D(stem_block_filters, (3, 3),
                   strides=(2, 2),
                   padding='valid',
                   use_bias=False,
                   name='stem_conv1',
                   kernel_initializer='he_normal')(img_input)
    else:
        x = Conv2D(stem_block_filters, (3, 3),
                   strides=(1, 1),
                   padding='same',
                   use_bias=False,
                   name='stem_conv1',
                   kernel_initializer='he_normal')(img_input)

    x = BatchNormalization(axis=channel_dim,
                           momentum=0.9997,
                           epsilon=1e-3,
                           name='stem_bn1')(x)

    p = None
    if not skip_reduction:  # imagenet / mobile mode
        x, p = _reduction_a_cell(x,
                                 p,
                                 filters // (filter_multiplier**2),
                                 block_id='stem_1')
        x, p = _reduction_a_cell(x,
                                 p,
                                 filters // filter_multiplier,
                                 block_id='stem_2')

    for i in range(num_blocks):
        x, p = _normal_a_cell(x, p, filters, block_id='%d' % (i))

    x, p0 = _reduction_a_cell(x,
                              p,
                              filters * filter_multiplier,
                              block_id='reduce_%d' % (num_blocks))

    p = p0 if not skip_reduction else p

    for i in range(num_blocks):
        x, p = _normal_a_cell(x,
                              p,
                              filters * filter_multiplier,
                              block_id='%d' % (num_blocks + i + 1))

    x, p0 = _reduction_a_cell(x,
                              p,
                              filters * filter_multiplier**2,
                              block_id='reduce_%d' % (2 * num_blocks))

    p = p0 if not skip_reduction else p

    for i in range(num_blocks):
        x, p = _normal_a_cell(x,
                              p,
                              filters * filter_multiplier**2,
                              block_id='%d' % (2 * num_blocks + i + 1))

    x = Activation('relu')(x)

    if include_top:
        x = GlobalAveragePooling2D()(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 = layer_utils.get_source_inputs(input_tensor)
    else:
        inputs = img_input

    model = Model(inputs, x, name='NASNet')

    # load weights
    if weights == 'imagenet':
        if default_size == 224:  # mobile version
            if include_top:
                weight_path = NASNET_MOBILE_WEIGHT_PATH
                model_name = 'nasnet_mobile.h5'
            else:
                weight_path = NASNET_MOBILE_WEIGHT_PATH_NO_TOP
                model_name = 'nasnet_mobile_no_top.h5'

            weights_file = get_file(model_name,
                                    weight_path,
                                    cache_subdir='models')
            model.load_weights(weights_file)

        elif default_size == 331:  # large version
            if include_top:
                weight_path = NASNET_LARGE_WEIGHT_PATH
                model_name = 'nasnet_large.h5'
            else:
                weight_path = NASNET_LARGE_WEIGHT_PATH_NO_TOP
                model_name = 'nasnet_large_no_top.h5'

            weights_file = get_file(model_name,
                                    weight_path,
                                    cache_subdir='models')
            model.load_weights(weights_file)
        else:
            raise ValueError(
                'ImageNet weights can only be loaded with NASNetLarge'
                ' or NASNetMobile')
    elif weights is not None:
        model.load_weights(weights)

    if old_data_format:
        K.set_image_data_format(old_data_format)

    return model
Ejemplo n.º 8
0
def conv_block(input_tensor,
               kernel_size,
               filters,
               stage,
               block,
               strides=(2, 2),
               renorm=False):
    """A block that has a conv layer at shortcut.

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

    Returns:
        Output tensor for the block.

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

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

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

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

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

    x = layers.add([x, shortcut])
    x = Activation('relu')(x)
    return x
Ejemplo n.º 9
0
def ResNet50(include_top=True,
             weights='imagenet',
             input_tensor=None,
             input_shape=None,
             pooling=None,
             classes=1000,
             renorm=False):
    """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),
              'imagenet' (pre-training on ImageNet),
              or the path to the weights file to be loaded.
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 197.
            E.g. `(200, 200, 3)` would be one valid value.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.

    Returns:
        A Keras model instance.

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

    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=128,
                                      data_format=K.image_data_format(),
                                      require_flatten=include_top,
                                      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
    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', renorm=renorm)(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

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

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

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

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

    x = AveragePooling2D((2, 2), 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 = layer_utils.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, by_name=True)
    elif weights is not None:
        model.load_weights(weights)

    return model
Ejemplo n.º 10
0
def creat_net(train_generator, validation_generator, batch_size, image_lengh,
              image_width):

    base_model = applications.resnet50.ResNet50(weights='imagenet',
                                                include_top=False,
                                                layers=tf.keras.layers,
                                                input_shape=(image_width,
                                                             image_lengh, 3))
    x = base_model.output
    x = GlobalAveragePooling2D(name='average_pool')(x)
    x = Flatten(name='flatten')(x)
    x = Dense(
        2048,
        activation='relu',
        kernel_regularizer=regularizers.l2(0.0001),
    )(x)
    x = BatchNormalization()(x)
    x = Dense(1024,
              activation='relu',
              kernel_regularizer=regularizers.l2(0.0001))(x)
    x = BatchNormalization(name='bn_fc_01')(x)
    predictions = Dense(4, activation='softmax')(x)
    model = Model(inputs=base_model.input, outputs=predictions)
    sgd = optimizers.Adam(lr=0.01, decay=1e-6)
    # Reduce=ReduceLROnPlateau(monitor='val_accuracy',
    #                          factor=0.1,
    #                          patience=2,
    #                          verbose=1,
    #                          mode='auto',
    #                          epsilon=0.0001,
    #                          cooldown=0,
    #                          min_lr=0)
    model.compile(loss='categorical_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    #保存最优模型
    filepath = './模型/resnet_50_weights-improvement-{epoch:02d}-{val_accuracy:.2f}.hdf5'
    checkpoint = callbacks.ModelCheckpoint(filepath,
                                           monitor='val_accuracy',
                                           verbose=1,
                                           save_best_only=True,
                                           mode='max')

    model.fit_generator(train_generator,
                        epochs=200,
                        steps_per_epoch=1707 // batch_size,
                        validation_data=validation_generator,
                        validation_steps=264 // batch_size,
                        callbacks=[checkpoint])  #Reduce])

    #绘制误差和准确率曲线
    loss = model.history.history['loss']
    val_loss = model.history.history['val_loss']
    epoches = range(1, len(loss) + 1)
    acc = model.history.history['accuracy']
    val_acc = model.history.history['val_accuracy']
    plt.subplot(121)
    plt.plot(epoches, loss, 'bo', label='training_loss')
    plt.plot(epoches, val_loss, 'r', label='validation_loss')
    plt.xlabel('epoches')
    plt.ylabel('loss')
    plt.title('losses of train and val')
    plt.legend()
    plt.subplot(122)
    plt.plot(epoches, acc, 'bo', label='training_acc')
    plt.plot(epoches, val_acc, 'r', label='validation_acc')
    plt.xlabel('epoches')
    plt.ylabel('acc')
    plt.title('accuracy of train and val')
    plt.legend()
    plt.show()
 def __set_second_block(self):
     conv2d = Conv2D(filters=64, kernel_size=3, strides=2,
                     padding="same")(self.__set_first_block())
     batch_normalization = BatchNormalization()(conv2d)
     relu = ReLU()(batch_normalization)
     return relu
Ejemplo n.º 12
0
def test_model():
    s_cols = ['C1', 'C2', 'C3']
    d_cols = ['I1', 'I2']
    # sparse
    print('----------- sparse features ------------')
    sparse_input = []
    lr_embedding = []
    for s_col in s_cols:
        _input = Input(shape=(1, ))
        sparse_input.append(_input)
        print('sparse input: ', sparse_input)
        nums = pd.concat((x_train[s_col], x_valid[s_col])).nunique() + 1
        embed = Flatten()(Embedding(
            nums,
            4,
            input_length=1,
            embeddings_regularizer=tf.keras.regularizers.l2(0.5))(
                _input))  # shape=(None, 1, 4) -> shape=(None, 4)
        lr_embedding.append(embed)
    print('\nsparse emb: ', lr_embedding)
    fst_order_sparse_layer = concatenate(lr_embedding)
    print('\nsparse layer: ', fst_order_sparse_layer)
    # dense
    print('----------- dense features ------------')
    dense_input = []
    for d_col in d_cols:
        _input = Input(shape=(1, ))
        dense_input.append(_input)
        print('dense input: ', dense_input)
    concat_dense_input = concatenate(dense_input)
    print('\nfinal dense input: ', concat_dense_input)
    fst_order_dense_layer = Dense(4, activation='relu')(concat_dense_input)
    print('\ndense layer: ', fst_order_dense_layer)

    # linear concat
    print('----------- linear part ------------')
    linear_part = concatenate([fst_order_dense_layer, fst_order_sparse_layer])
    print('linear part: ', linear_part)

    #######dnn layer##########
    print('----------- dnn part ------------')
    print('dnn input layer: ', linear_part)
    fc_layer = Dropout(0.2)(Activation(activation="relu")(BatchNormalization()(
        Dense(128)(linear_part))))
    print('full conection layer1: ', fc_layer)
    fc_layer = Dropout(0.2)(Activation(activation="relu")(BatchNormalization()(
        Dense(64)(fc_layer))))
    print('full conection layer2: ', fc_layer)
    fc_layer = Dropout(0.2)(Activation(activation="relu")(BatchNormalization()(
        Dense(32)(fc_layer))))
    print('full conection layer3: ', fc_layer)

    ######## output layer ##########
    print('----------- output part ------------')
    print('linear layer: ', linear_part)
    print('dnn layer: ', fc_layer)
    output_layer = concatenate([linear_part, fc_layer])
    print('hidden layer to output: ', output_layer)
    output_layer = Dense(1, activation='sigmoid')(output_layer)
    print('output layer: ', output_layer)

    ######## model ##########
    print('----------- model ------------')
    model = Model(inputs=sparse_input + dense_input, outputs=output_layer)
    print('input: ', sparse_input + dense_input)
    print('\noutput: ', output_layer)
    print('\nmodel: ', model)
Ejemplo n.º 13
0
def DenseNet(blocks,
             include_top=True,
             weights='imagenet',
             input_tensor=None,
             input_shape=None,
             pooling=None,
             classes=1000):
    """Instantiates the DenseNet 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. The data format
  convention used by the model is the one
  specified in your Keras config file.

  Arguments:
      blocks: numbers of building blocks for the four dense layers.
      include_top: whether to include the fully-connected
          layer at the top of the network.
      weights: one of `None` (random initialization),
            'imagenet' (pre-training on ImageNet),
            or the path to the weights file to be loaded.
      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.
      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 not (weights in {'imagenet', None} or os.path.exists(weights)):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization), `imagenet` '
                         '(pre-training on ImageNet), '
                         'or the path to the weights file to be loaded.')

    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=128,
                                      data_format=K.image_data_format(),
                                      require_flatten=include_top,
                                      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

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

    x = ZeroPadding2D(padding=((3, 3), (3, 3)))(img_input)
    x = Conv2D(64, 7, strides=2, use_bias=False, name='conv1/conv')(x)
    x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='conv1/bn')(x)
    x = Activation('relu', name='conv1/relu')(x)
    x = ZeroPadding2D(padding=((1, 1), (1, 1)))(x)
    x = MaxPooling2D(3, strides=2, name='pool1')(x)

    x = dense_block(x, blocks[0], name='conv2')
    x = transition_block(x, 0.5, name='pool2')
    x = dense_block(x, blocks[1], name='conv3')
    x = transition_block(x, 0.5, name='pool3')
    x = dense_block(x, blocks[2], name='conv4')
    x = transition_block(x, 0.5, name='pool4')
    x = dense_block(x, blocks[3], name='conv5')

    x = BatchNormalization(axis=bn_axis, epsilon=1.001e-5, name='bn')(x)

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

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = layer_utils.get_source_inputs(input_tensor)
    else:
        inputs = img_input

    # Create model.
    if blocks == [6, 12, 24, 16]:
        model = Model(inputs, x, name='densenet121')
    elif blocks == [6, 12, 32, 32]:
        model = Model(inputs, x, name='densenet169')
    elif blocks == [6, 12, 48, 32]:
        model = Model(inputs, x, name='densenet201')
    else:
        model = Model(inputs, x, name='densenet')

    # Load weights.
    if weights == 'imagenet':
        if include_top:
            if blocks == [6, 12, 24, 16]:
                weights_path = get_file(
                    'densenet121_weights_tf_dim_ordering_tf_kernels.h5',
                    DENSENET121_WEIGHT_PATH,
                    cache_subdir='models',
                    file_hash='0962ca643bae20f9b6771cb844dca3b0')
            elif blocks == [6, 12, 32, 32]:
                weights_path = get_file(
                    'densenet169_weights_tf_dim_ordering_tf_kernels.h5',
                    DENSENET169_WEIGHT_PATH,
                    cache_subdir='models',
                    file_hash='bcf9965cf5064a5f9eb6d7dc69386f43')
            elif blocks == [6, 12, 48, 32]:
                weights_path = get_file(
                    'densenet201_weights_tf_dim_ordering_tf_kernels.h5',
                    DENSENET201_WEIGHT_PATH,
                    cache_subdir='models',
                    file_hash='7bb75edd58cb43163be7e0005fbe95ef')
        else:
            if blocks == [6, 12, 24, 16]:
                weights_path = get_file(
                    'densenet121_weights_tf_dim_ordering_tf_kernels_notop.h5',
                    DENSENET121_WEIGHT_PATH_NO_TOP,
                    cache_subdir='models',
                    file_hash='4912a53fbd2a69346e7f2c0b5ec8c6d3')
            elif blocks == [6, 12, 32, 32]:
                weights_path = get_file(
                    'densenet169_weights_tf_dim_ordering_tf_kernels_notop.h5',
                    DENSENET169_WEIGHT_PATH_NO_TOP,
                    cache_subdir='models',
                    file_hash='50662582284e4cf834ce40ab4dfa58c6')
            elif blocks == [6, 12, 48, 32]:
                weights_path = get_file(
                    'densenet201_weights_tf_dim_ordering_tf_kernels_notop.h5',
                    DENSENET201_WEIGHT_PATH_NO_TOP,
                    cache_subdir='models',
                    file_hash='1c2de60ee40562448dbac34a0737e798')
        model.load_weights(weights_path)
    elif weights is not None:
        model.load_weights(weights)

    return model
Ejemplo n.º 14
0
        for start in range(0, len(ids), val_batch_size):
            x_batch = []
            y_batch = []
            end = min(start + val_batch_size, len(ids))
            i_val_batch = ids[start:end]
            for i in i_val_batch:
                x_batch.append(process_wav_file(valid_df.wav_file.values[i]))
                y_batch.append(valid_df.label_id.values[i])
            x_batch = np.array(x_batch)
            y_batch = to_categorical(y_batch, num_classes=len(POSSIBLE_LABELS))


yield x_batch, y_batch

x_in = Input(shape=(257, 98, 2))
x = BatchNormalization()(x_in)
for i in range(4):
    x = Conv2D(16 * (2**i), (3, 3))(x)
    x = Activation('elu')(x)
    x = BatchNormalization()(x)
    x = MaxPooling2D((2, 2))(x)
x = Conv2D(128, (1, 1))(x)
x_branch_1 = GlobalAveragePooling2D()(x)
x_branch_2 = GlobalMaxPool2D()(x)
x = concatenate([x_branch_1, x_branch_2])
x = Dense(256, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(len(POSSIBLE_LABELS), activation='sigmoid')(x)
model = Model(inputs=x_in, outputs=x)
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
 def block(x):
     x = Conv2D(filters, kernel_size=kernel_size, strides=strides, padding=padding)(x)
     x = LeakyReLU(leaky_relu_slope)(x)
     x = BatchNormalization()(x)
     return x
Ejemplo n.º 16
0
    def build(width, height, depth, classes):
        # initialize the model along with the input shape to be
        # "channels last" and the channels dimension itself
        model = Sequential()
        inputShape = (height, width, depth)
        chanDim = -1

        # if we are using "channels first", update the input shape
        # and channels dimension
        if image_data_format() == "channels_first":
            inputShape = (depth, height, width)
            chanDim = 1
        # CONV => RELU => POOL layer set
        model.add(Conv2D(64, (3, 3), padding="same",
                         input_shape=inputShape))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(64, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
        model.add(Dropout(0.25))
        # (CONV => RELU) * 2 => POOL layer set
        model.add(Conv2D(128, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(128, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
        model.add(Dropout(0.25))
        # (CONV => RELU) * 3 => POOL layer set
        model.add(Conv2D(256, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(256, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(256, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(256, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
        model.add(Dropout(0.25))
        # (CONV => RELU) * 4 => POOL layer set
        model.add(Conv2D(512, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(512, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(512, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(512, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
        model.add(Dropout(0.25))
        # (CONV => RELU) * 5 => POOL layer set
        model.add(Conv2D(512, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(512, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(512, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(512, (3, 3), padding="same"))
        model.add(Activation("relu"))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
        model.add(Dropout(0.25))
        #FC => RELU layers
        model.add(Flatten())
        model.add(Dense(4096))
        model.add(Activation("relu"))
        model.add(BatchNormalization())
        model.add(Dense(4096))
        model.add(Activation("relu"))
        model.add(BatchNormalization())
        model.add(Dropout(0.25))

        # softmax classifier
        model.add(Dense(classes))
        model.add(Activation("softmax"))

        # return the constructed network architecture
        return model
Ejemplo n.º 17
0
    def __construct_network__(self, orders, connections):
        """
        Constructs the network according to config file.

        :param orders: Order of the connections.
        :type orders: List
        :param connections: Connections of the network.
        :type connections: Dictionary
        """
        x = input_image = Input(shape=(self.width, self.height, 3))
        self.true_boxes = Input(shape=(1, 1, 1, self.max_box_per_image, 4))
        for order in orders:
            conn = connections[order]
            if "convolution" in order:
                x = Conv2D(conn["filters"],
                           conn["size"],
                           strides=conn["stride"],
                           padding='same',
                           use_bias=False)(x)

                if conn["batch_normalize"]:
                    x = BatchNormalization()(x)

                if conn["activation"] == "leaky":
                    x = LeakyReLU(alpha=0.1)(x)

                elif conn["activation"] == "linear":
                    pass

            if "maxpool" in order:
                x = MaxPooling2D(pool_size=conn["size"],
                                 strides=conn["stride"],
                                 padding='same')(x)

            if "region" in order:
                # Last layer. Region layer's properties are implemented in loss function.
                # Instead define 'last(object detection)' layer
                # Object detection layer -> Conv - Reshape - Lambda

                self.feature_extractor = Model(input_image, x)
                if not self._custom_weights:
                    self.feature_extractor.load_weights(
                        self.cfg["feature_extractor_weights"])
                features = self.feature_extractor(input_image)
                self.grid_h, self.grid_w = self.feature_extractor.get_output_shape_at(
                    -1)[1:3]

                filter_num = (conn["coords"] + 1 + self.classes) * conn["num"]
                x = Conv2D(filter_num,
                           1,
                           strides=1,
                           padding="same",
                           kernel_initializer="lecun_normal")(features)
                x = Reshape((self.grid_h, self.grid_w, conn["num"],
                             conn["coords"] + 1 + self.classes))(x)
                x = Lambda(lambda args: args[0])([x, self.true_boxes])

        self.model = Model([input_image, self.true_boxes], x)

        # Initialize trainable params.
        layer = self.model.layers[-4]
        weights = layer.get_weights()

        # for lay in self.model.layers[:-4]:
        #     lay.trainable = False

        new_kernel = np.random.normal(size=weights[0].shape) / (self.grid_h *
                                                                self.grid_w)
        new_bias = np.random.normal(size=weights[1].shape) / (self.grid_h *
                                                              self.grid_w)

        layer.set_weights([new_kernel, new_bias])
Ejemplo n.º 18
0
 def layer(x):
     x = Conv3D(filters, 1, padding='same', data_format=DATA_FORMAT)(x)
     x = BatchNormalization()(x)
     return x
Ejemplo n.º 19
0
def _reduction_a_cell(ip, p, filters, block_id=None):
    """Adds a Reduction cell for NASNet-A (Fig. 4 in the paper).

  Arguments:
      ip: Input tensor `x`
      p: Input tensor `p`
      filters: Number of output filters
      block_id: String block_id

  Returns:
      A Keras tensor
  """
    channel_dim = 1 if K.image_data_format() == 'channels_first' else -1

    with K.name_scope('reduction_A_block_%s' % block_id):
        p = _adjust_block(p, ip, filters, block_id)

        h = Activation('relu')(ip)
        h = Conv2D(filters, (1, 1),
                   strides=(1, 1),
                   padding='same',
                   name='reduction_conv_1_%s' % block_id,
                   use_bias=False,
                   kernel_initializer='he_normal')(h)
        h = BatchNormalization(axis=channel_dim,
                               momentum=0.9997,
                               epsilon=1e-3,
                               name='reduction_bn_1_%s' % block_id)(h)

        with K.name_scope('block_1'):
            x1_1 = _separable_conv_block(h,
                                         filters, (5, 5),
                                         strides=(2, 2),
                                         block_id='reduction_left1_%s' %
                                         block_id)
            x1_2 = _separable_conv_block(p,
                                         filters, (7, 7),
                                         strides=(2, 2),
                                         block_id='reduction_1_%s' % block_id)
            x1 = add([x1_1, x1_2], name='reduction_add_1_%s' % block_id)

        with K.name_scope('block_2'):
            x2_1 = MaxPooling2D((3, 3),
                                strides=(2, 2),
                                padding='same',
                                name='reduction_left2_%s' % block_id)(h)
            x2_2 = _separable_conv_block(p,
                                         filters, (7, 7),
                                         strides=(2, 2),
                                         block_id='reduction_right2_%s' %
                                         block_id)
            x2 = add([x2_1, x2_2], name='reduction_add_2_%s' % block_id)

        with K.name_scope('block_3'):
            x3_1 = AveragePooling2D((3, 3),
                                    strides=(2, 2),
                                    padding='same',
                                    name='reduction_left3_%s' % block_id)(h)
            x3_2 = _separable_conv_block(p,
                                         filters, (5, 5),
                                         strides=(2, 2),
                                         block_id='reduction_right3_%s' %
                                         block_id)
            x3 = add([x3_1, x3_2], name='reduction_add3_%s' % block_id)

        with K.name_scope('block_4'):
            x4 = AveragePooling2D((3, 3),
                                  strides=(1, 1),
                                  padding='same',
                                  name='reduction_left4_%s' % block_id)(x1)
            x4 = add([x2, x4])

        with K.name_scope('block_5'):
            x5_1 = _separable_conv_block(x1,
                                         filters, (3, 3),
                                         block_id='reduction_left4_%s' %
                                         block_id)
            x5_2 = MaxPooling2D((3, 3),
                                strides=(2, 2),
                                padding='same',
                                name='reduction_right5_%s' % block_id)(h)
            x5 = add([x5_1, x5_2], name='reduction_add4_%s' % block_id)

        x = concatenate([x2, x3, x4, x5],
                        axis=channel_dim,
                        name='reduction_concat_%s' % block_id)
        return x, ip
Ejemplo n.º 20
0
def DeepSTN(
    H=21,
    W=12,
    channel=2,  # H-map_height W-map_width channel-map_channel
    c=3,
    p=4,
    t=4,  # c-closeness p-period t-trend
    pre_F=64,
    conv_F=64,
    R_N=2,
    # pre_F-prepare_conv_featrue conv_F-resnet_conv_featrue R_N-resnet_number
    is_plus=True,  # use ResPlus or mornal convolution
    plus=8,
    rate=2,  # rate-pooling_rate
    is_pt=True,  # use PoI and Time or not
    P_N=6,
    T_F=28,
    PT_F=6,
    T=24,
    # P_N-poi_number T_F-time_feature PT_F-poi_time_feature T-T_times/day
    drop=0,
    is_summary=True,  # show detail
    lr=0.0002,
    kernel1=1,
    # kernel1 decides whether early-fusion uses conv_unit0 or conv_unit1, 1 recommended
    isPT_F=1
):  # isPT_F decides whether PT_model uses one more Conv after multiplying PoI and Time, 1 recommended

    all_channel = channel * (c + p + t)

    cut0 = int(0)
    cut1 = int(cut0 + channel * c)
    cut2 = int(cut1 + channel * p)
    cut3 = int(cut2 + channel * t)

    cpt_input = Input(shape=(all_channel, H, W))
    cpt_input_p = Permute((2, 3, 1))(cpt_input)

    c_input = Lambda(cpt_slice, arguments={
        'h1': cut0,
        'h2': cut1
    })(cpt_input_p)
    p_input = Lambda(cpt_slice, arguments={
        'h1': cut1,
        'h2': cut2
    })(cpt_input_p)
    t_input = Lambda(cpt_slice, arguments={
        'h1': cut2,
        'h2': cut3
    })(cpt_input_p)

    c_out1 = Conv2D(filters=pre_F, kernel_size=(1, 1), padding="same")(c_input)
    p_out1 = Conv2D(filters=pre_F, kernel_size=(1, 1), padding="same")(p_input)
    t_out1 = Conv2D(filters=pre_F, kernel_size=(1, 1), padding="same")(t_input)

    if is_pt:
        poi_in = Input(shape=(P_N, H, W))
        # poi_in_p = Permute((2, 3, 1))(poi_in)
        # T_times/day + 7days/week
        time_in = Input(shape=(T + 7, H, W))
        # time_in_p = Permute((2, 3, 1))(time_in)

        PT_model = PT_trans('PT_trans', P_N, PT_F, T, T_F, H, W, isPT_F)

        # poi_time = PT_model([poi_in_p, time_in_p])
        poi_time = PT_model([poi_in, time_in])
        # TODO: need permutation? seems not as PT_model permute that

        cpt_con1 = Concatenate(axis=3)([c_out1, p_out1, t_out1, poi_time])
        if kernel1:
            cpt = conv_unit1(pre_F * 3 + PT_F * isPT_F + P_N * (not isPT_F),
                             conv_F, drop, H, W)(cpt_con1)
        else:
            cpt = conv_unit0(pre_F * 3 + PT_F * isPT_F + P_N * (not isPT_F),
                             conv_F, drop, H, W)(cpt_con1)

    else:
        cpt_con1 = Concatenate(axis=3)([c_out1, p_out1, t_out1])
        if kernel1:
            cpt = conv_unit1(pre_F * 3, conv_F, drop, H, W)(cpt_con1)
        else:
            cpt = conv_unit0(pre_F * 3, conv_F, drop, H, W)(cpt_con1)

    if is_plus:
        for i in range(R_N):
            cpt = Res_plus('Res_plus_' + str(i + 1), conv_F, plus, rate, drop,
                           H, W)(cpt)

    else:
        for i in range(R_N):
            cpt = Res_normal('Res_normal_' + str(i + 1), conv_F, drop, H,
                             W)(cpt)

    cpt_conv2 = Activation('relu')(cpt)
    cpt_out2 = BatchNormalization()(cpt_conv2)
    cpt_conv1 = Dropout(drop)(cpt_out2)
    cpt_conv1 = Conv2D(filters=channel, kernel_size=(1, 1),
                       padding="same")(cpt_conv1)
    cpt_out1 = Activation('tanh')(cpt_conv1)

    if is_pt:
        DeepSTN_model = Model(inputs=[cpt_input, poi_in, time_in],
                              outputs=cpt_out1)
    else:
        DeepSTN_model = Model(inputs=cpt_input, outputs=cpt_out1)

    DeepSTN_model.compile(loss='mse',
                          optimizer=Adam(lr),
                          metrics=[metrics.rmse, metrics.mae])

    if is_summary:
        DeepSTN_model.summary()

    print('***** pre_F : ', pre_F)
    print('***** conv_F: ', conv_F)
    print('***** R_N   : ', R_N)

    print('***** plus  : ', plus * is_plus)
    print('***** rate  : ', rate * is_plus)

    print('***** P_N   : ', P_N * is_pt)
    print('***** T_F   : ', T_F * is_pt)
    print('***** PT_F  : ', PT_F * is_pt * isPT_F)
    print('***** T     : ', T)

    print('***** drop  : ', drop)

    return DeepSTN_model
Ejemplo n.º 21
0
def DilatedXception(classes=10,
                    input_tensor=None,
                    input_shape=(512, 512, 3),
                    weights_info=None,
                    OS=16,
                    return_skip=False,
                    include_top=True):
    """ Instantiates the Deeplabv3+ architecture

    Optionally loads weights pre-trained
    on PASCAL VOC or Cityscapes. This model is available for TensorFlow only.
    # Arguments
        classes: Integer, optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: shape of input image. format HxWxC
            PASCAL VOC model was trained on (512,512,3) images. None is allowed as shape/width
        weights_info: this dict is consisted of `classes` and `weghts`.
            `classes` is number of `weights` output units.
            `weights` is one of 'imagenet' (pre-training on ImageNet), 'pascal_voc', 'cityscapes',
            original weights path (pre-training on original data) or None (random initialization)
        OS: determines input_shape/feature_extractor_output ratio. One of {8,16}.
            Used only for xception backbone.
        return_skip: flag to return additional tensor after 2 SepConvs for decoder
        include_top: Boolean, whether to include the fully-connected
            layer at the top of the network. Defaults to `True`.

    # Returns
        A Keras model instance. if return_skip is true, return additional tensor too
    """
    if weights_info is not None:
        if weights_info.get("weights") is None:
            weights = None

        elif weights_info["weights"] in {'pascal_voc', 'cityscapes', None}:
            weights = weights_info["weights"]

        elif os.path.exists(weights_info["weights"]):
            weights = weights_info["weights"]
            if weights_info.get("classes") is not None:
                classes = int(weights_info["classes"])

        else:
            raise ValueError(
                'The `weights` should be either '
                '`None` (random initialization), `pascal_voc`, `cityscapes`, '
                'original weights path (pre-training on original data), '
                'or the path to the weights file to be loaded and'
                '`classes` should be number of original weights output units')
    else:
        weights = None
        if classes is None:
            raise ValueError('`classes` should be any number')

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

    if OS == 8:
        entry_block3_stride = 1
        middle_block_rate = 2  # ! Not mentioned in paper, but required
        exit_block_rates = (2, 4)
    else:
        entry_block3_stride = 2
        middle_block_rate = 1
        exit_block_rates = (1, 2)

    x = Conv2D(32, (3, 3),
               strides=(2, 2),
               name='entry_flow_conv1_1',
               use_bias=False,
               padding='same')(img_input)
    x = BatchNormalization(name='entry_flow_conv1_1_BN')(x)
    x = Activation('relu')(x)

    x = _conv2d_same(x, 64, 'entry_flow_conv1_2', kernel_size=3, stride=1)
    x = BatchNormalization(name='entry_flow_conv1_2_BN')(x)
    x = Activation('relu')(x)

    x, _ = _xception_block(x, [128, 128, 128],
                           'entry_flow_block1',
                           skip_connection_type='conv',
                           stride=2,
                           depth_activation=False)
    x, skip = _xception_block(x, [256, 256, 256],
                              'entry_flow_block2',
                              skip_connection_type='conv',
                              stride=2,
                              depth_activation=False)

    x, _ = _xception_block(x, [728, 728, 728],
                           'entry_flow_block3',
                           skip_connection_type='conv',
                           stride=entry_block3_stride,
                           depth_activation=False)
    for i in range(16):
        x, _ = _xception_block(x, [728, 728, 728],
                               'middle_flow_unit_{}'.format(i + 1),
                               skip_connection_type='sum',
                               stride=1,
                               rate=middle_block_rate,
                               depth_activation=False)

    x, _ = _xception_block(x, [728, 1024, 1024],
                           'exit_flow_block1',
                           skip_connection_type='conv',
                           stride=1,
                           rate=exit_block_rates[0],
                           depth_activation=False)
    x, _ = _xception_block(x, [1536, 1536, 2048],
                           'exit_flow_block2',
                           skip_connection_type='none',
                           stride=1,
                           rate=exit_block_rates[1],
                           depth_activation=True)

    x = GlobalAveragePooling2D()(x)
    x = Dense(classes, activation='softmax')(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = layer_utils.get_source_inputs(input_tensor)
    else:
        inputs = img_input

    # Create model.
    model = Model(inputs, x, name='xception')

    # Load weights.
    if not (weights in {'imagenet', 'pascal_voc', 'cityscapes', None}):
        if weights_info.get("classes") is not None:
            model.load_weights(weights)

    # get model before FC layer
    if not include_top:
        model = Model(inputs=model.input,
                      outputs=model.get_layer(index=-3).output)

    if return_skip:
        return model, skip
    else:
        return model
            batch_end += batch_size


vggModel = VGG16(include_top=False,
                 weights="imagenet",
                 input_shape=(None, None, 3))

transfer_layer = vggModel.get_layer('block5_conv3')
vggModel = Model(inputs=vggModel.input, outputs=transfer_layer.output)
vggModel.trainable = False

newModel = Sequential()
newModel.add(vggModel)

#newModel.add(UpSampling2D((2,2)))
newModel.add(BatchNormalization())
newModel.add(Conv2D(512, (3, 3), padding='same', activation="relu"))
newModel.add(BatchNormalization())
newModel.add(Conv2D(512, (3, 3), padding='same', activation="relu"))

newModel.add(UpSampling2D((2, 2)))
newModel.add(BatchNormalization())
newModel.add(Conv2D(256, (3, 3), padding='same', activation="relu"))
newModel.add(BatchNormalization())
newModel.add(Conv2D(256, (3, 3), padding='same', activation="relu"))
newModel.add(BatchNormalization())
newModel.add(Conv2D(256, (3, 3), padding='same', activation="relu"))

newModel.add(UpSampling2D((2, 2)))
newModel.add(BatchNormalization())
newModel.add(Conv2D(128, (3, 3), padding='same', activation="relu"))
Ejemplo n.º 23
0
    def bulid_model(self):
        inputs = Input(shape=self.input_shape)
        net = inputs
        # block1
        net = Conv2D(filters=32,
                     kernel_size=3,
                     strides=2,
                     activation='relu',
                     padding='same',
                     name='bock1_conv1',
                     kernel_regularizer=regularizers.l2(
                         self.weight_decay))(net)
        net = BatchNormalization()(net)
        net = Conv2D(filters=32,
                     kernel_size=3,
                     activation='relu',
                     padding='same',
                     name='block1_conv2',
                     kernel_regularizer=regularizers.l2(
                         self.weight_decay)(net))
        net = BatchNormalization()(net)
        net = Conv2D(filters=64,
                     kernel_size=3,
                     activation='relu',
                     padding='same',
                     name='block1_conv3',
                     kernel_regularizer=regularizers.l2(
                         self.weight_decay))(net)
        net = BatchNormalization()(net)
        net = MaxPooling2D(pool_size=3,
                           strides=2,
                           padding='same',
                           name='block1_pool')(net)

        #block2
        net = Conv2D(filters=80,
                     kernel_size=1,
                     activation='relu',
                     padding='same',
                     name='block2_conv1',
                     kernel_regularizer=regularizers.l2(
                         self.weight_decay))(net)
        net = BatchNormalization()(net)
        net = Conv2D(filters=192,
                     kernel_size=3,
                     activation='relu',
                     padding='same',
                     name='block2_conv2',
                     kernel_regularizer=regularizers.l2(
                         self.weight_decay))(net)
        net = BatchNormalization()(net)
        net = MaxPooling2D(pool_size=3,
                           strides=2,
                           padding='same',
                           name='block2_pool')(net)

        net = self.block1_module1(net, 'block1_module1')
        net = self.block1_module2(net, "block1_module2")
        net = self.block1_module2(net, 'block1_module2_1')
        net = self.block2_module1(net)

        # 1x1
        net_1x1 = Conv2D(filters=128,
                         kernel_size=1,
                         padding='same',
                         activation='relu',
                         name='block2_module2_1x1',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net)
        net_1x1 = BatchNormalization()(net_1x1)

        # 1x7
        net_1x7 = Conv2D(filters=128,
                         kernel_size=(1, 1),
                         padding='same',
                         activation='relu',
                         name='block2_module2_1x7_conv1',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net)
        net_1x7 = BatchNormalization()(net_1x7)
        net_1x7 = Conv2D(filters=128,
                         kernel_size=(1, 7),
                         padding='same',
                         activation='relu',
                         name='block2_module2_1x7_conv2',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_1x7)
        net_1x7 = BatchNormalization()(net_1x7)
        net_1x7 = Conv2D(filters=192,
                         kernel_size=(7, 1),
                         padding='same',
                         activation='relu',
                         name='block2_module2_1x7_conv3',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_1x7)
        net_1x7 = BatchNormalization()(net_1x7)

        net_7x1 = Conv2D(filters=128,
                         kernel_size=(1, 1),
                         padding='same',
                         activation='relu',
                         name='block2_module2_7x1_conv1',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net)
        net_7x1 = Conv2D(filters=128,
                         kernel_size=(7, 1),
                         padding='same',
                         activation='relu',
                         name='block2_module2_7x1_conv2',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_7x1)
        net_7x1 = Conv2D(filters=128,
                         kernel_size=(7, 1),
                         padding='same',
                         activation='relu',
                         name='block2_module2_7x1_conv3',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_7x1)
        net_7x1 = Conv2D(filters=192,
                         kernel_size=(1, 7),
                         padding='same',
                         activation='relu',
                         name='block2_module2_7x1_conv4',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_7x1)

        net_avg = AvgPool2D(pool_size=3,
                            strides=1,
                            padding='same',
                            name='block2_module2_1x1_avg')(net)
        net_avg = Conv2D(filters=192,
                         kernel_size=1,
                         padding='same',
                         activation='relu',
                         name='block2_module2_1x1_avg_conv1',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_avg)
        net = concatenate([net_1x1, net_1x7, net_7x1, net_avg], axis=-1)

        net = self.block2_modul3_4(net, 'block2_module3')
        net = self.block2_modul3_4(net, 'block2_module4')

        # 1x1
        net_1x1 = Conv2D(filters=192,
                         kernel_size=1,
                         padding='same',
                         activation='relu',
                         name='block2_module5_1x1',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net)
        net_1x1 = BatchNormalization()(net_1x1)

        # 1x7
        net_1x7 = Conv2D(filters=192,
                         kernel_size=(1, 1),
                         padding='same',
                         activation='relu',
                         name='block2_module5_1x7_conv1',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net)
        net_1x7 = BatchNormalization()(net_1x7)
        net_1x7 = Conv2D(filters=192,
                         kernel_size=(1, 7),
                         padding='same',
                         activation='relu',
                         name='block2_module5_1x7_conv2',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_1x7)
        net_1x7 = BatchNormalization()(net_1x7)
        net_1x7 = Conv2D(filters=192,
                         kernel_size=(7, 1),
                         padding='same',
                         activation='relu',
                         name='block2_module5_1x7_conv3',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_1x7)
        net_1x7 = BatchNormalization()(net_1x7)

        net_7x1 = Conv2D(filters=192,
                         kernel_size=(1, 1),
                         padding='same',
                         activation='relu',
                         name='block2_module5_7x1_conv1',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net)
        net_7x1 = Conv2D(filters=192,
                         kernel_size=(7, 1),
                         padding='same',
                         activation='relu',
                         name='block2_module5_7x1_conv2',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_7x1)
        net_7x1 = Conv2D(filters=192,
                         kernel_size=(7, 1),
                         padding='same',
                         activation='relu',
                         name='block2_module5_7x1_conv3',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_7x1)
        net_7x1 = Conv2D(filters=192,
                         kernel_size=(1, 7),
                         padding='same',
                         activation='relu',
                         name='block2_module5_7x1_conv4',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_7x1)

        net_avg = AvgPool2D(pool_size=3,
                            strides=1,
                            padding='same',
                            name='block2_module5_1x1_avg')(net)
        net_avg = Conv2D(filters=192,
                         kernel_size=1,
                         padding='same',
                         activation='relu',
                         name='block2_module5_1x1_avg_conv1',
                         kernel_regularizer=regularizers.l2(
                             self.weight_decay))(net_avg)
        net = concatenate([net_1x1, net_1x7, net_7x1, net_avg], axis=-1)
Ejemplo n.º 24
0
def create_model_v1():
    """
        description: Defining the Model with keras backend
            
        output: 1. The Model
    """
    model = Sequential()

    # 1st Convolutional Layer
    model.add(Conv2D(filters=96, input_shape=(img_rows, img_cols, color_type), kernel_size=(11,11),strides=(4,4), padding='valid'))
    model.add(Activation('relu'))
    # Pooling 
    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid'))
    # Batch Normalisation before passing it to the next layer
    model.add(BatchNormalization())

    # 2nd Convolutional Layer
    model.add(Conv2D(filters=256, kernel_size=(11,11), strides=(1,1), padding='valid'))
    model.add(Activation('relu'))
    # Pooling
    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid'))
    # Batch Normalisation
    model.add(BatchNormalization())

    # 3rd Convolutional Layer
    model.add(Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding='valid'))
    model.add(Activation('relu'))
    # Batch Normalisation
    model.add(BatchNormalization())

    # 4th Convolutional Layer
    model.add(Conv2D(filters=384, kernel_size=(3,3), strides=(1,1), padding='valid'))
    model.add(Activation('relu'))
    # Batch Normalisation
    model.add(BatchNormalization())

    # 5th Convolutional Layer
    model.add(Conv2D(filters=256, kernel_size=(3,3), strides=(1,1), padding='valid'))
    model.add(Activation('relu'))
    # Pooling
    model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid'))
    # Batch Normalisation
    model.add(BatchNormalization())

    # Passing it to a dense layer
    model.add(Flatten())
    # 1st Dense Layer
    model.add(Dense(4096, input_shape=(224*224*3,)))
    model.add(Activation('relu'))
    # Add Dropout to prevent overfitting
    model.add(Dropout(0.4))
    # Batch Normalisation
    model.add(BatchNormalization())

    # 2nd Dense Layer
    model.add(Dense(4096))
    model.add(Activation('relu'))
    # Add Dropout
    model.add(Dropout(0.4))
    # Batch Normalisation
    model.add(BatchNormalization())

    # 3rd Dense Layer
    model.add(Dense(1000))
    model.add(Activation('relu'))
    # Add Dropout
    model.add(Dropout(0.4))
    # Batch Normalisation
    model.add(BatchNormalization())

    # Output Layer
    model.add(Dense(10))
    model.add(Activation('softmax'))
    
    return model
def define_model(IMAGE_DIMS, VEC_LEN, weight_dir):
    i10 = Input(shape=IMAGE_DIMS)
    i20 = Input(shape=IMAGE_DIMS)
    i30 = Input(shape=IMAGE_DIMS)
    t1 = Input(shape=(1, ))
    t2 = Input(shape=(1, ))

    i1 = Lambda(lambda x: tf.div(tf.subtract(tf.cast(x, tf.float32), 127.5),
                                 127.5))(i10)
    i2 = Lambda(lambda x: tf.div(tf.subtract(tf.cast(x, tf.float32), 127.5),
                                 127.5))(i20)
    i3 = Lambda(lambda x: tf.div(tf.subtract(tf.cast(x, tf.float32), 127.5),
                                 127.5))(i30)
    # i1 = tf.cast(i1,tf.float32)
    # i2 = tf.cast(i2, tf.float32)
    # i3 = tf.cast(i3, tf.float32)
    #
    # i1=tf.div(tf.subtract(i1,127.5),127.5)
    # i2 = tf.div(tf.subtract(i2, 127.5), 127.5)
    # i3 = tf.div(tf.subtract(i3, 127.5), 127.5)
    print("[INFO] Weights restored from pre-trained InceptionV3!")
    encoder = InceptionV3(weights=weight_dir, include_top=False)
    pooling = GlobalAveragePooling2D()

    def l2_normalize(x):
        return K.expand_dims(K.l2_normalize(x, 1))

    val = Lambda(l2_normalize, name='margin')()
    BatchNormalization(axis=-1,
                       momentum=0.99,
                       epsilon=0.001,
                       center=True,
                       scale=True,
                       beta_initializer='zeros',
                       gamma_initializer='ones',
                       moving_mean_initializer='zeros',
                       moving_variance_initializer='ones',
                       beta_regularizer=None,
                       gamma_regularizer=None,
                       beta_constraint=None,
                       gamma_constraint=None)

    output = Dense(VEC_LEN, activation='tanh', name='encoder_output')

    o1 = encoder(i1)
    o2 = encoder(i2)
    o3 = encoder(i3)
    # o1 = i1
    # o2 = i2
    # o3 = i3

    o1 = pooling(o1)  # 全局平均池化层
    # o1 = BatchNormalization(o1)
    o1 = output(o1)  # 有1024个节点的全连接层
    # o1 = NormLayer(o1)
    #o1 = Dropout(0.1)(o1)

    o2 = pooling(o2)  # 全局平均池化层
    # o2 = BatchNormalization(o2)
    o2 = output(o2)  # 有1024个节点的全连接层
    #o2 = Dropout(0.1)(o2)
    # o2 = NormLayer(o2)

    o3 = pooling(o3)  # 全局平均池化层
    # o3 = BatchNormalization(o3)
    o3 = output(o3)  # 有1024个节点的全连接层

    # o3 = NormLayer(o3)

    #o3 = Dropout(0.1)(o3)

    #print('[INFO] base_model_layers', len(encoder.layers))
    def l2_normalize(x):
        return K.expand_dims(K.l2_normalize(x, 1))

    def l2_norm(x):
        return K.sqrt(K.sum(K.square(x), 1))

    def distance(inputs):
        ap, an, margin, gthr = inputs
        ap_l2n = K.sqrt(K.sum(K.square(ap), axis=1, keepdims=True))
        an_l2n = K.sqrt(K.sum(K.square(an), axis=1, keepdims=True))
        d = K.minimum((an_l2n - ap_l2n), margin)
        # d=an_l2n
        # g=ap_l2n
        g = K.maximum(ap_l2n, gthr)
        y = K.concatenate([d, g], axis=1)
        return y

    ap = Subtract()([o1, o2])
    an = Subtract()([o1, o3])

    val = Lambda(distance, name='margin')([ap, an, t1, t2])
    # val = Concatenate()([d, g])
    model = Model(inputs=[i10, i20, i30, t1, t2], outputs=val)
    # K.clear_session()
    return model
Ejemplo n.º 26
0
def Deeplabv3(input_shape=(512, 512, 3),
              weights=None,
              input_tensor=None,
              classes=21,
              backbone='mobilenetv2',
              OS=16,
              alpha=.5,
              activation=None):
    """ Instantiates the Deeplabv3+ architecture
    Optionally loads weights pre-trained
    on PASCAL VOC or Cityscapes. This model is available for TensorFlow only.
    # Arguments
        weights: one of 'pascal_voc' (pre-trained on pascal voc),
            'cityscapes' (pre-trained on cityscape) or None (random initialization)
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: shape of input image. format HxWxC
            PASCAL VOC model was trained on (512,512,3) images. None is allowed as shape/width
        classes: number of desired classes. PASCAL VOC has 21 classes, Cityscapes has 19 classes.
            If number of classes not aligned with the weights used, last layer is initialized randomly
        backbone: backbone to use. one of {'xception','mobilenetv2'}
        activation: optional activation to add to the top of the network.
            One of 'softmax', 'sigmoid' or None
        OS: determines input_shape/feature_extractor_output ratio. One of {8,16}.
            Used only for xception backbone.
        alpha: controls the width of the MobileNetV2 network. This is known as the
            width multiplier in the MobileNetV2 paper.
                - 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.
            Used only for mobilenetv2 backbone. Pretrained is only available for alpha=1.
    # Returns
        A Keras model instance.
    # Raises
        RuntimeError: If attempting to run this model with a
            backend that does not support separable convolutions.
        ValueError: in case of invalid argument for `weights` or `backbone`
    """

    if not (weights in {'pascal_voc', 'cityscapes', None}):
        raise ValueError(
            'The `weights` argument should be either '
            '`None` (random initialization), `pascal_voc`, or `cityscapes` '
            '(pre-trained on PASCAL VOC)')

    if not (backbone in {'xception', 'mobilenetv2'}):
        raise ValueError('The `backbone` argument should be either '
                         '`xception`  or `mobilenetv2` ')

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

    if backbone == 'xception':
        if OS == 8:
            entry_block3_stride = 1
            middle_block_rate = 2  # ! Not mentioned in paper, but required
            exit_block_rates = (2, 4)
            atrous_rates = (12, 24, 36)
        else:
            entry_block3_stride = 2
            middle_block_rate = 1
            exit_block_rates = (1, 2)
            atrous_rates = (6, 12, 18)

        x = Conv2D(32, (3, 3),
                   strides=(2, 2),
                   name='entry_flow_conv1_1',
                   use_bias=False,
                   padding='same')(img_input)
        x = BatchNormalization(name='entry_flow_conv1_1_BN')(x)
        x = Activation('relu')(x)

        x = _conv2d_same(x, 64, 'entry_flow_conv1_2', kernel_size=3, stride=1)
        x = BatchNormalization(name='entry_flow_conv1_2_BN')(x)
        x = Activation('relu')(x)

        x = _xception_block(x, [128, 128, 128],
                            'entry_flow_block1',
                            skip_connection_type='conv',
                            stride=2,
                            depth_activation=False)
        x, skip1 = _xception_block(x, [256, 256, 256],
                                   'entry_flow_block2',
                                   skip_connection_type='conv',
                                   stride=2,
                                   depth_activation=False,
                                   return_skip=True)

        x = _xception_block(x, [728, 728, 728],
                            'entry_flow_block3',
                            skip_connection_type='conv',
                            stride=entry_block3_stride,
                            depth_activation=False)
        for i in range(16):
            x = _xception_block(x, [728, 728, 728],
                                'middle_flow_unit_{}'.format(i + 1),
                                skip_connection_type='sum',
                                stride=1,
                                rate=middle_block_rate,
                                depth_activation=False)

        x = _xception_block(x, [728, 1024, 1024],
                            'exit_flow_block1',
                            skip_connection_type='conv',
                            stride=1,
                            rate=exit_block_rates[0],
                            depth_activation=False)
        x = _xception_block(x, [1536, 1536, 2048],
                            'exit_flow_block2',
                            skip_connection_type='none',
                            stride=1,
                            rate=exit_block_rates[1],
                            depth_activation=True)

    else:
        OS = 8
        first_block_filters = _make_divisible(32 * alpha, 8)
        x = Conv2D(first_block_filters,
                   kernel_size=3,
                   strides=(2, 2),
                   padding='same',
                   use_bias=False,
                   name='Conv')(img_input)
        x = BatchNormalization(epsilon=1e-3, momentum=0.999, name='Conv_BN')(x)
        x = Activation(relu6, name='Conv_Relu6')(x)

        x = _inverted_res_block(x,
                                filters=16,
                                alpha=alpha,
                                stride=1,
                                expansion=1,
                                block_id=0,
                                skip_connection=False)

        x = _inverted_res_block(x,
                                filters=24,
                                alpha=alpha,
                                stride=2,
                                expansion=6,
                                block_id=1,
                                skip_connection=False)
        x = _inverted_res_block(x,
                                filters=24,
                                alpha=alpha,
                                stride=1,
                                expansion=6,
                                block_id=2,
                                skip_connection=True)

        x = _inverted_res_block(x,
                                filters=32,
                                alpha=alpha,
                                stride=2,
                                expansion=6,
                                block_id=3,
                                skip_connection=False)
        x = _inverted_res_block(x,
                                filters=32,
                                alpha=alpha,
                                stride=1,
                                expansion=6,
                                block_id=4,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=32,
                                alpha=alpha,
                                stride=1,
                                expansion=6,
                                block_id=5,
                                skip_connection=True)

        # stride in block 6 changed from 2 -> 1, so we need to use rate = 2
        x = _inverted_res_block(
            x,
            filters=64,
            alpha=alpha,
            stride=1,  # 1!
            expansion=6,
            block_id=6,
            skip_connection=False)
        x = _inverted_res_block(x,
                                filters=64,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=7,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=64,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=8,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=64,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=9,
                                skip_connection=True)

        x = _inverted_res_block(x,
                                filters=96,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=10,
                                skip_connection=False)
        x = _inverted_res_block(x,
                                filters=96,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=11,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=96,
                                alpha=alpha,
                                stride=1,
                                rate=2,
                                expansion=6,
                                block_id=12,
                                skip_connection=True)

        x = _inverted_res_block(
            x,
            filters=160,
            alpha=alpha,
            stride=1,
            rate=2,  # 1!
            expansion=6,
            block_id=13,
            skip_connection=False)
        x = _inverted_res_block(x,
                                filters=160,
                                alpha=alpha,
                                stride=1,
                                rate=4,
                                expansion=6,
                                block_id=14,
                                skip_connection=True)
        x = _inverted_res_block(x,
                                filters=160,
                                alpha=alpha,
                                stride=1,
                                rate=4,
                                expansion=6,
                                block_id=15,
                                skip_connection=True)

        x = _inverted_res_block(x,
                                filters=320,
                                alpha=alpha,
                                stride=1,
                                rate=4,
                                expansion=6,
                                block_id=16,
                                skip_connection=False)

    # end of feature extractor

    # branching for Atrous Spatial Pyramid Pooling

    # Image Feature branch
    shape_before = tf.shape(x)
    b4 = GlobalAveragePooling2D()(x)
    # from (b_size, channels)->(b_size, 1, 1, channels)
    b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4)
    b4 = Lambda(lambda x: K.expand_dims(x, 1))(b4)
    b4 = Conv2D(256, (1, 1),
                padding='same',
                use_bias=False,
                name='image_pooling')(b4)
    b4 = BatchNormalization(name='image_pooling_BN', epsilon=1e-5)(b4)
    b4 = Activation('relu')(b4)
    # upsample. have to use compat because of the option align_corners
    size_before = tf.keras.backend.int_shape(x)
    b4 = Lambda(lambda x: tf.compat.v1.image.resize(
        x, size_before[1:3], method='bilinear', align_corners=True))(b4)
    # simple 1x1
    b0 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='aspp0')(x)
    b0 = BatchNormalization(name='aspp0_BN', epsilon=1e-5)(b0)
    b0 = Activation('relu', name='aspp0_activation')(b0)

    # there are only 2 branches in mobilenetV2. not sure why
    if backbone == 'xception':
        # rate = 6 (12)
        b1 = SepConv_BN(x,
                        256,
                        'aspp1',
                        rate=atrous_rates[0],
                        depth_activation=True,
                        epsilon=1e-5)
        # rate = 12 (24)
        b2 = SepConv_BN(x,
                        256,
                        'aspp2',
                        rate=atrous_rates[1],
                        depth_activation=True,
                        epsilon=1e-5)
        # rate = 18 (36)
        b3 = SepConv_BN(x,
                        256,
                        'aspp3',
                        rate=atrous_rates[2],
                        depth_activation=True,
                        epsilon=1e-5)

        # concatenate ASPP branches & project
        x = Concatenate()([b4, b0, b1, b2, b3])
    else:
        x = Concatenate()([b4, b0])

    x = Conv2D(256, (1, 1),
               padding='same',
               use_bias=False,
               name='concat_projection')(x)
    x = BatchNormalization(name='concat_projection_BN', epsilon=1e-5)(x)
    x = Activation('relu')(x)
    x = Dropout(0.1)(x)
    # DeepLab v.3+ decoder

    if backbone == 'xception':
        # Feature projection
        # x4 (x2) block
        size_before2 = tf.keras.backend.int_shape(x)
        x = Lambda(lambda xx: tf.compat.v1.image.resize(xx,
                                                        size_before2[1:3] * tf.
                                                        constant(OS // 4),
                                                        method='bilinear',
                                                        align_corners=True))(x)

        dec_skip1 = Conv2D(48, (1, 1),
                           padding='same',
                           use_bias=False,
                           name='feature_projection0')(skip1)
        dec_skip1 = BatchNormalization(name='feature_projection0_BN',
                                       epsilon=1e-5)(dec_skip1)
        dec_skip1 = Activation('relu')(dec_skip1)
        x = Concatenate()([x, dec_skip1])
        x = SepConv_BN(x,
                       256,
                       'decoder_conv0',
                       depth_activation=True,
                       epsilon=1e-5)
        x = SepConv_BN(x,
                       256,
                       'decoder_conv1',
                       depth_activation=True,
                       epsilon=1e-5)

    # you can use it with arbitary number of classes
    if (weights == 'pascal_voc' and classes == 21) or (weights == 'cityscapes'
                                                       and classes == 19):
        last_layer_name = 'logits_semantic'
    else:
        last_layer_name = 'custom_logits_semantic'

    x = Conv2D(classes, (1, 1), padding='same', name=last_layer_name)(x)
    size_before3 = tf.keras.backend.int_shape(img_input)
    x = Lambda(lambda xx: tf.compat.v1.image.resize(
        xx, size_before3[1:3], method='bilinear', align_corners=True))(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

    if activation in {'softmax', 'sigmoid'}:
        x = tf.keras.layers.Activation(activation)(x)

    model = Model(inputs, x, name='deeplabv3plus')

    # load weights

    if weights == 'pascal_voc':
        if backbone == 'xception':
            weights_path = get_file(
                'deeplabv3_xception_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH_X,
                cache_subdir='models')
        else:
            weights_path = get_file(
                'deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH_MOBILE,
                cache_subdir='models')
        model.load_weights(weights_path, by_name=True)
    elif weights == 'cityscapes':
        if backbone == 'xception':
            weights_path = get_file(
                'deeplabv3_xception_tf_dim_ordering_tf_kernels_cityscapes.h5',
                WEIGHTS_PATH_X_CS,
                cache_subdir='models')
        else:
            weights_path = get_file(
                'deeplabv3_mobilenetv2_tf_dim_ordering_tf_kernels_cityscapes.h5',
                WEIGHTS_PATH_MOBILE_CS,
                cache_subdir='models')
        model.load_weights(weights_path, by_name=True)
    return model
Ejemplo n.º 27
0
 def conv2d(layer_input, filters, f_size=4, bn=True): 
     d = Conv2D(filters, kernel_size=f_size, strides=2, padding='same')(layer_input)
     d = LeakyReLU(alpha=0.2)(d)
     if bn:
         d = BatchNormalization(momentum=0.8)(d)
     return d
Ejemplo n.º 28
0
def normal(x, normal_type=None):
    if normal_type is not None:
        if normal_type == "bn":
            x = BatchNormalization(axis=-1)(x)
    return x
Ejemplo n.º 29
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),
            'imagenet' (pre-training on ImageNet),
            or the path to the weights file to be loaded.
      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 not (weights in {'imagenet', None} or os.path.exists(weights)):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization), `imagenet` '
                         '(pre-training on ImageNet), '
                         'or the path to the weights file to be loaded.')

    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.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:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

    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 = layer_utils.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',
                file_hash='0a58e3b7378bc2990ea3b43d5981f1f6')
        else:
            weights_path = get_file(
                'xception_weights_tf_dim_ordering_tf_kernels_notop.h5',
                TF_WEIGHTS_PATH_NO_TOP,
                cache_subdir='models',
                file_hash='b0042744bf5b25fce3cb969f33bebb97')
        model.load_weights(weights_path)
    elif weights is not None:
        model.load_weights(weights)

    if old_data_format:
        K.set_image_data_format(old_data_format)
    return model
Ejemplo n.º 30
0
    def fit_network(self):
        input_dimension = self.X.shape[1]
        try:
            output_dimension = self.Y.shape[1]
        except:
            self.Y = np.reshape(self.Y, (len(self.Y), 1))
            output_dimension = self.Y.shape[1]

        Architecture.set_environment()
        Architecture.write_recap_text(self._getNeurons, self._batch_size,
                                      self._activation)
        self.__set_hard_parameters()

        print("Preprocessing training matrix..")
        self.X_tilde = self.preprocess_training(self.X, self._center,
                                                self._scale, self._centering,
                                                self._scaling)
        self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(
            self.X_tilde, self.Y, test_size=0.2)

        self._layers = len(self._getNeurons)

        counter = 0

        self.model = Sequential()
        self.model.add(
            Dense(self._getNeurons[counter],
                  input_dim=input_dimension,
                  kernel_initializer='normal',
                  activation=self._activation))
        counter += 1
        if self._dropout != 0:
            from tensorflow.python.keras.layers import Dropout
            self.model.add(Dropout(self._dropout))
            print("Dropping out some neurons...")
        elif self._batchNormalization:
            print("Normalization added!")
            self.model.add(BatchNormalization(momentum=0.9, epsilon=1e-5))
        else:
            while counter < self._layers:
                print("I'm in the while: {}".format(counter))
                self.model.add(
                    Dense(self._getNeurons[counter],
                          activation=self._activation))
                if self._dropout != 0:
                    self.model.add(Dropout(self._dropout))
                    counter += 1
                elif self._batchNormalization:
                    self.model.add(
                        BatchNormalization(momentum=0.9, epsilon=1e-5))
                    counter += 1
                else:
                    counter += 1
        self.model.add(
            Dense(output_dimension, activation=self._activation_output))
        self.model.summary()

        earlyStopping = EarlyStopping(monitor=self._monitor_early_stop,
                                      patience=self._patience,
                                      verbose=0,
                                      mode='min')
        mcp_save = ModelCheckpoint(filepath=self.__path + '/best_weights2c.h5',
                                   verbose=1,
                                   save_best_only=True,
                                   monitor=self._monitor_early_stop,
                                   mode='min')
        self.model.compile(loss=self._loss_function,
                           optimizer=self.__optimizer,
                           metrics=[self.__metrics])
        history = self.model.fit(self.X_train,
                                 self.y_train,
                                 batch_size=self._batch_size,
                                 epochs=self._n_epochs,
                                 verbose=1,
                                 validation_data=(self.X_test, self.y_test),
                                 callbacks=[earlyStopping, mcp_save])

        plt.plot(history.history['loss'])
        plt.plot(history.history['val_loss'])
        plt.title('Model loss')
        plt.ylabel('Loss')
        plt.xlabel('Epoch')
        plt.legend(['Train', 'Test'], loc='upper right')
        plt.savefig('loss_history.eps')
        #plt.show()
        plt.clf()
        plt.close()

        self.model.load_weights(self.__path + '/best_weights2c.h5')

        counter_saver = 0

        predict_ALL = self.model.predict(self.X_tilde)
        predict_test = self.model.predict(self.X_test)

        if self.save_txt:

            while counter_saver < self._layers:
                layer_weights = self.model.layers[counter_saver].get_weights(
                )[0]
                layer_biases = self.model.layers[counter_saver].get_weights(
                )[1]
                name_weights = "Weights_HL{}.txt".format(counter_saver)
                name_biases = "Biases_HL{}.txt".format(counter_saver)
                np.savetxt(name_weights, layer_weights)
                np.savetxt(name_biases, layer_biases)

                counter_saver += 1

        return predict_ALL, predict_test, self.y_test