示例#1
0
 def model(self):
     shape = (1, 4, 4)
     input = Input(shape=shape)
     x = AveragePooling2D(data_format='channels_first')(input)
     model = Model(inputs=input, outputs=x)
     return model
示例#2
0
def ResNet50(include_top=True,
             weights='imagenet',
             input_tensor=None,
             input_shape=None,
             pooling=None,
             classes=1000):
    """Instantiates the ResNet50 architecture.
    Optionally loads weights pre-trained
    on ImageNet. Note that when using TensorFlow,
    for best performance you should set
    `image_data_format="channels_last"` in your Keras config
    at ~/.keras/keras.json.
    The model and the weights are compatible with both
    TensorFlow and Theano. The data format
    convention used by the model is the one
    specified in your Keras config file.
    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization)
            or "imagenet" (pre-training on ImageNet).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 244)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 197.
            E.g. `(200, 200, 3)` would be one valid value.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.
    # Returns
        A Keras model instance.
    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    if weights not in {'imagenet', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `imagenet` '
                         '(pre-training on ImageNet).')

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

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

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

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

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

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

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

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

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

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

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

    # load weights
    if weights == 'imagenet':
        if include_top:
            weights_path = get_file(
                'resnet50_weights_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH,
                cache_subdir='models',
                md5_hash='a7b3fe01876f51b976af0dea6bc144eb')
        else:
            weights_path = get_file(
                'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
                WEIGHTS_PATH_NO_TOP,
                cache_subdir='models',
                md5_hash='a268eb855778b3df3c7506639542a6af')
        model.load_weights(weights_path)
        if K.backend() == 'theano':
            layer_utils.convert_all_kernels_in_model(model)

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

            if K.backend() == 'tensorflow':
                warnings.warn('You are using the TensorFlow backend, yet you '
                              'are using the Theano '
                              'image data format convention '
                              '(`image_data_format="channels_first"`). '
                              'For best performance, set '
                              '`image_data_format="channels_last"` in '
                              'your Keras config '
                              'at ~/.keras/keras.json.')
    return model
示例#3
0
    def build(
            cls,
            img_size,
            n_classes,
            alpha=1.0,
            weights_path=None,
            train=False,
            input_tensor=None):
        """Build an ICNet Model.

        Args:
            image_size (int): the size of each image. only square images are
                supported.
            n_classes (int): the number of output labels to predict.
            weights_path (str): (optional) a path to a Keras model file to
                load after the network is constructed. Useful for re-training.
            train (bool): (optional) if true, add additional output nodes to
                the network for training.

        Returns:
            model (keras.models.Model): A Keras model
        """
        if img_size % 384 != 0:
            raise Exception('`img_size` must be a multiple of 384.')
        logger.info('Building ICNet model.')
        inpt = Input(shape=(img_size, img_size, 3), tensor=input_tensor)

        # The full scale branch
        out_1 = cls._light_cnn_block(
            inpt,
            filter_scale=int(alpha * 32),
            strides=[2, 2, 2],
            include_projection=True,
            block_name='sub1_conv'
        )

        # The 1/2 scale branch
        out_2 = AveragePooling2D(pool_size=(2, 2), name='sub2_data')(inpt)
        out_2 = cls._light_cnn_block(
            out_2,
            filter_scale=int(alpha * 32),
            strides=[2, 1, 1],
            block_name='sub2_conv'
        )
        out_2 = MaxPooling2D(
            pool_size=3, strides=2, name='sub2_pool1_3x3'
        )(out_2)

        for layer_index in range(1, 4):
            out_2 = cls._conv_block(
                out_2,
                filter_scale=int(alpha * 32),
                include_projection=(layer_index == 1),
                block_name='sub2_conv%d_%d' % (2, layer_index)
            )

        # The third large conv block gets split off into another branch.
        out_2 = cls._conv_block(
            out_2,
            filter_scale=int(alpha * 64),
            include_projection=True,
            strides=[2, 1, 1],
            block_name='sub2_conv%d_%d' % (3, 1)
        )

        # The 1/4 scale branch
        out_4 = AveragePooling2D(pool_size=(2, 2), name='sub4_conv3_1')(out_2)

        for layer_index in range(2, 5):
            out_4 = cls._conv_block(
                out_4,
                filter_scale=int(alpha * 64),
                block_name='sub4_conv%d_%d' % (3, layer_index)
            )

        for layer_index in range(1, 7):
            out_4 = cls._conv_block(
                out_4,
                filter_scale=int(alpha * 128),
                dilation_rate=2,
                include_projection=(layer_index == 1),
                block_name='sub4_conv%d_%d' % (4, layer_index)
            )

        for sub_index in range(1, 4):
            out_4 = cls._conv_block(
                out_4,
                filter_scale=int(alpha * 256),
                dilation_rate=4,
                include_projection=(sub_index == 1),
                block_name='sub4_conv%d_%d' % (5, sub_index)
            )
        # In this version we've fixed the input dimensions to be square
        # We also are restricting dimsensions to be multiples of 384 which
        # will allow us to use standard upsampling layers for resizing.
        pool_height, _ = out_4.shape[1:3].as_list()
        pool_scale = int(img_size / 384)
        pool1 = AveragePooling2D(pool_size=pool_height,
                                 strides=pool_height,
                                 name='sub4_conv5_3_pool1')(out_4)
        pool1 = UpSampling2D(size=12 * pool_scale,
                             name='sub4_conv5_3_pool1_interp',
                             interpolation='bilinear')(pool1)
        pool2 = AveragePooling2D(pool_size=pool_height // 2,
                                 strides=pool_height // 2,
                                 name='sub4_conv5_3_pool2')(out_4)
        pool2 = UpSampling2D(size=6 * pool_scale,
                             name='sub4_conv5_3_pool2_interp',
                             interpolation='bilinear')(pool2)
        pool3 = AveragePooling2D(pool_size=pool_height // 3,
                                 strides=pool_height // 3,
                                 name='sub4_conv5_3_pool3')(out_4)
        pool3 = UpSampling2D(size=4 * pool_scale,
                             name='sub4_conv5_3_pool3_interp',
                             interpolation='bilinear')(pool3)
        pool4 = AveragePooling2D(pool_size=pool_height // 4,
                                 strides=pool_height // 4,
                                 name='sub4_conv5_3_pool4')(out_4)
        pool4 = UpSampling2D(size=3 * pool_scale,
                             name='sub4_conv5_3_pool6_interp',
                             interpolation='bilinear')(pool4)

        out_4 = Add(
            name='sub4_conv5_3_sum'
        )([out_4, pool1, pool2, pool3, pool4])
        out_4 = Conv2D(
            filters=int(alpha * 256),
            kernel_size=1,
            activation='relu',
            use_bias=False,
            name='sub4_conv5_4_k1')(out_4)
        out_4 = BatchNormalization(name='sub4_conv5_4_k1_bn')(out_4)

        out_2, aux_1 = cls._cff_block(
            out_4,
            out_2,
            int(alpha * 128),
            block_name='sub24_cff',
            include_projection=True
        )

        out_1, aux_2 = cls._cff_block(
            out_2,
            out_1,
            int(alpha * 128),
            block_name='sub12_cff'
        )
        out_1 = UpSampling2D(size=(2, 2), name='sub12_sum_interp',
                             interpolation='bilinear')(out_1)

        out_1 = Conv2D(n_classes, 1, activation='softmax',
                       name='conv6_cls')(out_1)

        out = UpSampling2D(size=(4, 4), name='conv6_interp',
                           interpolation='bilinear')(out_1)

        if train:
            aux_1 = Conv2D(n_classes, 1, activation='softmax',
                           name='sub4_out')(aux_1)
            aux_2 = Conv2D(n_classes, 1, activation='softmax',
                           name='sub24_out')(aux_2)
            # The loss during training is generated from these three outputs.
            # The final output layer is not needed.
            model = Model(inputs=inpt, outputs=[out_1, aux_2, aux_1])
        else:
            model = Model(inputs=inpt, outputs=out)

        if weights_path is not None:
            if weights_path.startswith('gs://'):
                weights_path = _copy_file_from_gcs(weights_path)
            logger.info('Loading weights from %s.' % weights_path)
            model.load_weights(weights_path, by_name=True)
        logger.info('Done building model.')

        return model
示例#4
0
    def resnet_v1(self, input_shape, depth, num_classes=10):
        """ResNet Version 1 Model resnet_layer [a]

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

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

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

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

        # Add classifier on top.
        # v1 does not use BN after last shortcut connection-ReLU
        x = AveragePooling2D(pool_size=8)(x)
        y = Flatten()(x)
        outputs = Dense(num_classes,
                        activation='softmax',
                        kernel_initializer='he_normal')(y)

        # Instantiate model.
        model = Model(inputs=inputs, outputs=outputs)
        return model
示例#5
0
def get_inception_v3_unet(input_shape, weights='imagenet'):
    inp = Input(input_shape + (9,))
    
    x = inc_conv2d_bn(inp, 32, 3, 3, strides=(2, 2), padding='same')
    x = inc_conv2d_bn(x, 32, 3, 3, padding='same')
    x = inc_conv2d_bn(x, 64, 3, 3)
    conv1 = x
    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

    x = inc_conv2d_bn(x, 80, 1, 1, padding='same')
    x = inc_conv2d_bn(x, 192, 3, 3, padding='same')
    conv2 = x
    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

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

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

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

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

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

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

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

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

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

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

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

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

    conv3 = x
    # mixed 3: 17 x 17 x 768
    branch3x3 = inc_conv2d_bn(x, 384, 3, 3, strides=(2, 2), padding='same')

    branch3x3dbl = inc_conv2d_bn(x, 64, 1, 1)
    branch3x3dbl = inc_conv2d_bn(branch3x3dbl, 96, 3, 3)
    branch3x3dbl = inc_conv2d_bn(
        branch3x3dbl, 96, 3, 3, strides=(2, 2), padding='same')

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

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

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

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

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

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

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

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

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

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

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

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

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

    conv4 = x
    # mixed 8: 8 x 8 x 1280
    branch3x3 = inc_conv2d_bn(x, 192, 1, 1)
    branch3x3 = inc_conv2d_bn(branch3x3, 320, 3, 3,
                          strides=(2, 2), padding='same')

    branch7x7x3 = inc_conv2d_bn(x, 192, 1, 1)
    branch7x7x3 = inc_conv2d_bn(branch7x7x3, 192, 1, 7)
    branch7x7x3 = inc_conv2d_bn(branch7x7x3, 192, 7, 1)
    branch7x7x3 = inc_conv2d_bn(
        branch7x7x3, 192, 3, 3, strides=(2, 2), padding='same')

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

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

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

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

        branch_pool = AveragePooling2D(
            (3, 3), strides=(1, 1), padding='same')(x)
        branch_pool = inc_conv2d_bn(branch_pool, 192, 1, 1)
        x = concatenate(
            [branch1x1, branch3x3, branch3x3dbl, branch_pool],
            axis=channel_axis,
            name='mixed' + str(9 + i))
        
    conv5 = x
    
    up6 = concatenate([UpSampling2D()(conv5), conv4], axis=-1)
    conv6 = conv_block(up6, 160)
    conv6 = conv_block(conv6, 160)

    up7 = concatenate([UpSampling2D()(conv6), conv3], axis=-1)
    conv7 = conv_block(up7, 128)
    conv7 = conv_block(conv7, 128)

    up8 = concatenate([UpSampling2D()(conv7), conv2], axis=-1)
    conv8 = conv_block(up8, 96)
    conv8 = conv_block(conv8, 96)

    up9 = concatenate([UpSampling2D()(conv8), conv1], axis=-1)
    conv9 = conv_block(up9, 64)
    conv9 = conv_block(conv9, 64)

    up10 = concatenate([UpSampling2D()(conv9), inp], axis=-1)
    conv10 = conv_block(up10, 48)
    conv10 = conv_block(conv10, 48)
    res = Conv2D(1, (1, 1), activation='sigmoid')(conv10)
    model = Model(inp, res)
    
    if weights == 'imagenet':
        inception_v3 = InceptionV3(weights=weights, include_top=False, input_shape=input_shape + (3,))
        for i in range(2, len(inception_v3.layers)):
            model.layers[i].set_weights(inception_v3.layers[i].get_weights())
            model.layers[i].trainable = False
        
    return model
def create_inception_v4(nb_classes=1001, load_weights=True):
    '''
    Creates a inception v4 network
    :param nb_classes: number of classes.txt
    :return: Keras Model with 1 input and 1 output
    '''

    if K.image_data_format() == 'th':
        init = Input((3, 299, 299))
    else:
        init = Input((299, 299, 3))

    # Input Shape is 299 x 299 x 3 (tf) or 3 x 299 x 299 (th)
    x = inception_stem(init)

    # 4 x Inception A
    for i in range(4):
        x = inception_A(x)

    # Reduction A
    x = reduction_A(x)

    # 7 x Inception B
    for i in range(7):
        x = inception_B(x)

    # Reduction B
    x = reduction_B(x)

    # 3 x Inception C
    for i in range(3):
        x = inception_C(x)

    # Average Pooling
    x = AveragePooling2D((8, 8))(x)

    # Dropout
    x = Dropout(0.8)(x)
    x = Flatten()(x)

    # Output
    out = Dense(output_dim=nb_classes, activation='softmax')(x)

    model = Model(init, out, name='Inception-v4')

    if load_weights:
        if K.backend() == "theano":
            if K.image_data_format() == "th":
                weights = get_file(
                    'inception_v4_weights_th_dim_ordering_th_kernels.h5',
                    TH_BACKEND_TH_DIM_ORDERING,
                    cache_subdir='models')
            else:
                weights = get_file(
                    'inception_v4_weights_tf_dim_ordering_th_kernels.h5',
                    TH_BACKEND_TF_DIM_ORDERING,
                    cache_subdir='models')
        else:
            if K.image_data_format() == "th":
                weights = get_file(
                    'inception_v4_weights_th_dim_ordering_tf_kernels.h5',
                    TF_BACKEND_TH_DIM_ORDERING,
                    cache_subdir='models')
            else:
                weights = get_file(
                    'inception_v4_weights_tf_dim_ordering_tf_kernels.h5',
                    TH_BACKEND_TF_DIM_ORDERING,
                    cache_subdir='models')

        model.load_weights(weights)
        print("Model weights loaded.")

    return model
示例#7
0
def ResNet50(input_shape=(64, 64, 3), classes=6):
    """
    实现ResNet50
    CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3
    -> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> TOPLAYER

    参数:
        input_shape - 图像数据集的维度
        classes - 整数,分类数

    返回:
        model - Keras框架的模型

    """

    # 定义tensor类型的输入数据
    X_input = Input(input_shape)

    # 0填充
    X = ZeroPadding2D((3, 3))(X_input)

    # stage1
    X = Conv2D(filters=64, kernel_size=(7, 7), strides=(2, 2), name="conv1",
               kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name="bn_conv1")(X)
    X = Activation("relu")(X)
    X = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(X)

    # stage2
    X = convolutional_block(X, f=3, filters=[64, 64, 256], stage=2, block="a", s=1)
    X = identity_block(X, f=3, filters=[64, 64, 256], stage=2, block="b")
    X = identity_block(X, f=3, filters=[64, 64, 256], stage=2, block="c")

    # stage3
    X = convolutional_block(X, f=3, filters=[128, 128, 512], stage=3, block="a", s=2)
    X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block="b")
    X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block="c")
    X = identity_block(X, f=3, filters=[128, 128, 512], stage=3, block="d")

    # stage4
    X = convolutional_block(X, f=3, filters=[256, 256, 1024], stage=4, block="a", s=2)
    X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="b")
    X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="c")
    X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="d")
    X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="e")
    X = identity_block(X, f=3, filters=[256, 256, 1024], stage=4, block="f")

    # stage5
    X = convolutional_block(X, f=3, filters=[512, 512, 2048], stage=5, block="a", s=2)
    X = identity_block(X, f=3, filters=[512, 512, 2048], stage=5, block="b")
    X = identity_block(X, f=3, filters=[512, 512, 2048], stage=5, block="c")

    # 均值池化层
    X = AveragePooling2D(pool_size=(2, 2), padding="same")(X)

    # 输出层
    X = Flatten()(X)
    X = Dense(classes, activation="softmax", name="fc" + str(classes),
              kernel_initializer=glorot_uniform(seed=0))(X)

    # 创建模型
    model = Model(inputs=X_input, outputs=X, name="ResNet50")

    return model
示例#8
0
def ResNet50(input_shape, classes):
    # Define the input as a tensor with shape input_shape
    X_input = Input(input_shape)

    # Zero-Padding
    X = ZeroPadding2D((3, 3))(X_input)

    # Stage 1
    X = Conv2D(64, (7, 7),
               strides=(2, 2),
               name='conv1',
               kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name='bn_conv1')(X)
    X = Activation('relu')(X)
    X = MaxPooling2D((3, 3), strides=(2, 2))(X)

    # Stage 2
    X = convolutional_block(X,
                            f=3,
                            filters=[64, 64, 256],
                            stage=2,
                            block='a',
                            s=1)
    X = identity_block(X, 3, [64, 64, 256], stage=2, block='b')
    X = identity_block(X, 3, [64, 64, 256], stage=2, block='c')

    # Stage 3
    X = convolutional_block(X,
                            f=3,
                            filters=[128, 128, 512],
                            stage=3,
                            block='a',
                            s=2)
    X = identity_block(X, 3, [128, 128, 512], stage=3, block='b')
    X = identity_block(X, 3, [128, 128, 512], stage=3, block='c')
    X = identity_block(X, 3, [128, 128, 512], stage=3, block='d')

    # Stage 4
    X = convolutional_block(X,
                            f=3,
                            filters=[256, 256, 1024],
                            stage=4,
                            block='a',
                            s=2)
    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='b')
    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='c')
    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='d')
    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='e')
    X = identity_block(X, 3, [256, 256, 1024], stage=4, block='f')

    # Stage 5
    X = convolutional_block(X,
                            f=3,
                            filters=[512, 512, 2048],
                            stage=5,
                            block='a',
                            s=2)
    X = identity_block(X, 3, [512, 512, 2048], stage=5, block='b')
    X = identity_block(X, 3, [512, 512, 2048], stage=5, block='c')

    # AVGPOOL
    X = AveragePooling2D(pool_size=(2, 2),
                         padding='same',
                         data_format='channels_last')(X)

    # output layer
    X = Flatten()(X)
    X = Dense(classes,
              activation='softmax',
              name='fc' + str(classes),
              kernel_initializer=glorot_uniform(seed=0))(X)

    # Create model
    model = Model(inputs=X_input, outputs=X, name='ResNet50')

    return model
    Conv2D(128,
           kernel_size=(1, 1),
           strides=(1, 1),
           padding='same',
           activation='relu'))

model.add(
    Conv2D(128,
           kernel_size=(3, 3),
           strides=(1, 1),
           padding='same',
           activation='relu'))

model.add(
    AveragePooling2D(pool_size=(7, 7),
                     strides=(1, 1),
                     padding='same',
                     data_format=None))  # (None, 7, 7, 128)

model.add(Flatten())

model.add(Dense(512, activation='relu'))

model.add(Dropout(0.5))

model.add(Dense(4, activation='softmax'))

sgd = optimizers.SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
adam = Adam(lr=0.0001,
            beta_1=0.9,
            beta_2=0.999,
            epsilon=1e-08,
def InceptionResNetV2(include_top=True,
                      weights='imagenet',
                      weights_path=None,
                      input_tensor=None,
                      input_shape=None,
                      pooling=None,
                      classes=1000):
    '''Instantiates the Inception-ResNet v2 architecture.

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

    Note that the default input image size for this model is 299x299, instead
    of 224x224 as in the VGG16 and ResNet models. Also, the input preprocessing
    function is different (i.e., do not use `imagenet_utils.preprocess_input()`
    with this model. Use `preprocess_input()` defined in this module instead).

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

    # Returns
        A Keras `Model` instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
        RuntimeError: If attempting to run this model with an unsupported backend.
    '''
    if K.backend() in {'cntk'}:
        raise RuntimeError(K.backend() +
                           ' backend is currently unsupported for this model.')

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

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

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

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

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

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

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

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

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

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

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

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

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

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

    print("Loading Weights")

    # Load weights
    if weights == 'imagenet':
        if K.image_data_format() == 'channels_first':
            if K.backend() == 'tensorflow':
                warnings.warn('You are using the TensorFlow backend, yet you '
                              'are using the Theano '
                              'image data format convention '
                              '(`image_data_format="channels_first"`). '
                              'For best performance, set '
                              '`image_data_format="channels_last"` in '
                              'your Keras config '
                              'at ~/.keras/keras.json.')
    model.load_weights(weights_path)
    print("Weights loaded successfully")

    model.layers.pop()
    print("Number of layers: ", len(model.layers))
    for layer in model.layers:
        layer.trainable = True
    x = Dense(N_CLASSES, activation='softmax',
              name='predictions')(model.layers[-1].output)
    x.trainable = True

    model = Model(inputs, x)
    print("Saving Weights: ")
    model.save_weights('saved_weights.h5')

    return model
示例#11
0
def define_model(weight_path=None):
    input = Input(shape=(224, 224, 3))

    conv1_7x7_s2 = Conv2D(filters=64,
                          kernel_size=(7, 7),
                          strides=(2, 2),
                          padding='same',
                          activation='relu',
                          kernel_regularizer=l2(0.01))(input)

    maxpool1_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                   strides=(2, 2),
                                   padding='same')(conv1_7x7_s2)

    conv2_3x3_reduce = Conv2D(filters=64,
                              kernel_size=(1, 1),
                              padding='same',
                              activation='relu',
                              kernel_regularizer=l2(0.01))(maxpool1_3x3_s2)

    conv2_3x3 = Conv2D(filters=192,
                       kernel_size=(3, 3),
                       padding='same',
                       activation='relu',
                       kernel_regularizer=l2(0.01))(conv2_3x3_reduce)

    maxpool2_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                   strides=(2, 2),
                                   padding='same')(conv2_3x3)

    inception_3a = inception_model(input=maxpool2_3x3_s2,
                                   filters_1x1=64,
                                   filters_3x3_reduce=96,
                                   filters_3x3=128,
                                   filters_5x5_reduce=16,
                                   filters_5x5=32,
                                   filters_pool_proj=32)

    inception_3b = inception_model(input=inception_3a,
                                   filters_1x1=128,
                                   filters_3x3_reduce=128,
                                   filters_3x3=192,
                                   filters_5x5_reduce=32,
                                   filters_5x5=96,
                                   filters_pool_proj=64)

    maxpool3_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                   strides=(2, 2),
                                   padding='same')(inception_3b)

    inception_4a = inception_model(input=maxpool3_3x3_s2,
                                   filters_1x1=192,
                                   filters_3x3_reduce=96,
                                   filters_3x3=208,
                                   filters_5x5_reduce=16,
                                   filters_5x5=48,
                                   filters_pool_proj=64)

    inception_4b = inception_model(input=inception_4a,
                                   filters_1x1=160,
                                   filters_3x3_reduce=112,
                                   filters_3x3=224,
                                   filters_5x5_reduce=24,
                                   filters_5x5=64,
                                   filters_pool_proj=64)

    inception_4c = inception_model(input=inception_4b,
                                   filters_1x1=128,
                                   filters_3x3_reduce=128,
                                   filters_3x3=256,
                                   filters_5x5_reduce=24,
                                   filters_5x5=64,
                                   filters_pool_proj=64)

    inception_4d = inception_model(input=inception_4c,
                                   filters_1x1=112,
                                   filters_3x3_reduce=144,
                                   filters_3x3=288,
                                   filters_5x5_reduce=32,
                                   filters_5x5=64,
                                   filters_pool_proj=64)

    inception_4e = inception_model(input=inception_4d,
                                   filters_1x1=256,
                                   filters_3x3_reduce=160,
                                   filters_3x3=320,
                                   filters_5x5_reduce=32,
                                   filters_5x5=128,
                                   filters_pool_proj=128)

    maxpool4_3x3_s2 = MaxPooling2D(pool_size=(3, 3),
                                   strides=(2, 2),
                                   padding='same')(inception_4e)

    inception_5a = inception_model(input=maxpool4_3x3_s2,
                                   filters_1x1=256,
                                   filters_3x3_reduce=160,
                                   filters_3x3=320,
                                   filters_5x5_reduce=32,
                                   filters_5x5=128,
                                   filters_pool_proj=128)

    inception_5b = inception_model(input=inception_5a,
                                   filters_1x1=384,
                                   filters_3x3_reduce=192,
                                   filters_3x3=384,
                                   filters_5x5_reduce=48,
                                   filters_5x5=128,
                                   filters_pool_proj=128)

    averagepool1_7x7_s1 = AveragePooling2D(pool_size=(7, 7),
                                           strides=(7, 7),
                                           padding='same')(inception_5b)

    drop1 = Dropout(rate=0.4)(averagepool1_7x7_s1)

    linear = Dense(units=1000,
                   activation='softmax',
                   kernel_regularizer=l2(0.01))(
                       keras.layers.core.Flatten(drop1))
    last = linear

    model = Model(inputs=input, outputs=last)
    model.summary()
def ResNet152(include_top=True,
              weights=None,
              input_tensor=None,
              input_shape=None,
              large_input=False,
              pooling=None,
              classes=1000):

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

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

    eps = 1.1e-5

    if large_input:
        img_size = 448
    else:
        img_size = 224

    # Determine proper input shape
    input_shape = (224, 224, 3)
    print(input_shape)

    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

    # handle dimension ordering for different backends
    if Kb.image_dim_ordering() == 'tf':
        bn_axis = 3
    else:
        bn_axis = 1

    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1', use_bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=bn_axis, name='bn_conv1')(x)
    x = Scale(axis=bn_axis, name='scale_conv1')(x)
    x = Activation('relu', name='conv1_relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

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

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    for i in range(1, 8):
        x = identity_block(x, 3, [128, 128, 512], stage=3, block='b' + str(i))

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    for i in range(1, 36):
        x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b' + str(i))

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

    if large_input:
        x = AveragePooling2D((14, 14), name='avg_pool')(x)
    else:
        x = AveragePooling2D((7, 7), name='avg_pool')(x)

    # include classification layer by default, not included for feature extraction
    if include_top:
        x = Flatten()(x)
        x = Dense(classes, activation='softmax', name='fc1000')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

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

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

    # load weights
    if weights == 'imagenet':
        if include_top:
            weights_path = get_file(
                'resnet152_weights_tf.h5',
                WEIGHTS_PATH,
                cache_subdir='models',
                md5_hash='cdb18a2158b88e392c0905d47dcef965')
        else:
            weights_path = get_file(
                'resnet152_weights_tf_notop.h5',
                WEIGHTS_PATH_NO_TOP,
                cache_subdir='models',
                md5_hash='4a90dcdafacbd17d772af1fb44fc2660')

        model.load_weights(weights_path, by_name=True)

    return model

## build model
input = Input(shape=(img_height, img_width, channels))
out = Conv2D(filters=64, strides=2, kernel_size=7, padding="same")(input)
out = MaxPooling2D((2, 2))(out)

for _ in range(3):
    out = block_2(out, kernel_size=3, strides=1, filters=64)

out = block_1(out, kernel_size=3, strides=2, filters=128)

for _ in range(3):
    out = block_2(out, kernel_size=3, strides=1, filters=128)

out = block_1(out, kernel_size=3, strides=2, filters=256)

for _ in range(5):
    out = block_2(out, kernel_size=3, strides=1, filters=256)

out = block_1(out, kernel_size=3, strides=2, filters=512)

for _ in range(2):
    out = block_2(out, kernel_size=3, strides=1, filters=512)

out = AveragePooling2D(pool_size=(7, 7))(out)
out = Flatten()(out)
output = Dense(1000)(out)

model = Model(input=input, output=output)
model.summary()
示例#14
0
    if epoch < 5:
        return 0.001
    elif epoch < 10:
        return .0002
    elif epoch < 15:
        return 0.00002
    else:
        return .0000005


shape = (224, 224, 3)
X_train, X_test = setup_generator('food-101/train', 'food-101/test', 32, shape[:2])

base_model = InceptionV3(weights='imagenet', include_top=False, input_tensor=Input(shape=(224, 224, 3)))
x = base_model.output
x = AveragePooling2D()(x)

x = Dropout(.5)(x)
x = Flatten()(x)
predictions = Dense(X_train.num_classes, init='glorot_uniform', W_regularizer=l2(.0005), activation='softmax')(x)

model = Model(input=base_model.input, output=predictions)

opt = SGD(lr=.1, momentum=.9)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])

checkpointer = ModelCheckpoint(filepath='model.{epoch:02d}-{val_loss:.2f}.hdf5', verbose=1, save_best_only=True)
csv_logger = CSVLogger('model.log')


lr_scheduler = LearningRateScheduler(schedule)
def InceptionResNetV2(include_top=True,
                      weights='imagenet',
                      input_tensor=None,
                      input_shape=None,
                      pooling=None,
                      classes=1000):
    """Instantiates the Inception-ResNet v2 architecture.

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

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

    Note that the default input image size for this model is 299x299, instead
    of 224x224 as in the VGG16 and ResNet models. Also, the input preprocessing
    function is different (i.e., do not use `imagenet_utils.preprocess_input()`
    with this model. Use `preprocess_input()` defined in this module instead).

    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization),
              '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)` (with `'channels_last'` data format)
            or `(3, 299, 299)` (with `'channels_first'` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 139.
            E.g. `(150, 150, 3)` would be one valid value.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the last convolutional layer.
            - `'avg'` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `'max'` means that global max pooling will be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is `True`, and
            if no `weights` argument is specified.

    # Returns
        A Keras `Model` instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    if 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=299,
        min_size=139,
        data_format=K.image_data_format(),
        require_flatten=False,
        weights=weights)

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor

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

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

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

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

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

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

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

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

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

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

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

    # Load weights
    if weights == 'imagenet':
        if K.image_data_format() == 'channels_first':
            if K.backend() == 'tensorflow':
                warnings.warn('You are using the TensorFlow backend, yet you '
                              'are using the Theano '
                              'image data format convention '
                              '(`image_data_format="channels_first"`). '
                              'For best performance, set '
                              '`image_data_format="channels_last"` in '
                              'your Keras config '
                              'at ~/.keras/keras.json.')
        if include_top:
            weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5'
            weights_path = get_file(weights_filename,
                                    BASE_WEIGHT_URL + weights_filename,
                                    cache_subdir='models',
                                    file_hash='e693bd0210a403b3192acc6073ad2e96')
        else:
            weights_path = pre_trained_model_dir
            # weights_filename = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5'
            # weights_path = get_file(weights_filename,
            #                         BASE_WEIGHT_URL + weights_filename,
            #                         cache_subdir='models',
            #                         file_hash='d19885ff4a710c122648d3b5c3b684e4')
        model.load_weights(weights_path)
    elif weights is not None:
        model.load_weights(weights)

    return model
示例#16
0
def DeepModel(size_set=800):

    img_input = Input(shape=(size_set, size_set, 3))

    scale_img_2 = AveragePooling2D(pool_size=(2, 2))(img_input)
    scale_img_3 = AveragePooling2D(pool_size=(2, 2))(scale_img_2)
    scale_img_4 = AveragePooling2D(pool_size=(2, 2))(scale_img_3)

    conv1 = Conv2D(32, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block1_conv1')(img_input)
    conv1 = Conv2D(32, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block1_conv2')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

    input2 = Conv2D(64, (3, 3),
                    padding='same',
                    activation='relu',
                    name='block2_input1')(scale_img_2)
    input2 = concatenate([input2, pool1], axis=3)
    conv2 = Conv2D(64, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block2_conv1')(input2)
    conv2 = Conv2D(64, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block2_conv2')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

    input3 = Conv2D(128, (3, 3),
                    padding='same',
                    activation='relu',
                    name='block3_input1')(scale_img_3)
    input3 = concatenate([input3, pool2], axis=3)
    conv3 = Conv2D(128, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block3_conv1')(input3)
    conv3 = Conv2D(128, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block3_conv2')(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

    input4 = Conv2D(256, (3, 3),
                    padding='same',
                    activation='relu',
                    name='block4_input1')(scale_img_4)
    input4 = concatenate([input4, pool3], axis=3)
    conv4 = Conv2D(256, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block4_conv1')(input4)
    conv4 = Conv2D(256, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block4_conv2')(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

    conv5 = Conv2D(512, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block5_conv1')(pool4)
    conv5 = Conv2D(512, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block5_conv2')(conv5)

    up6 = concatenate([
        Conv2DTranspose(
            256, (2, 2), strides=(2, 2), padding='same',
            name='block6_dconv')(conv5), conv4
    ],
                      axis=3)
    conv6 = Conv2D(256, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block6_conv1')(up6)
    conv6 = Conv2D(256, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block6_conv2')(conv6)

    up7 = concatenate([
        Conv2DTranspose(
            128, (2, 2), strides=(2, 2), padding='same',
            name='block7_dconv')(conv6), conv3
    ],
                      axis=3)
    conv7 = Conv2D(128, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block7_conv1')(up7)
    conv7 = Conv2D(128, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block7_conv2')(conv7)

    up8 = concatenate([
        Conv2DTranspose(
            64, (2, 2), strides=(2, 2), padding='same',
            name='block8_dconv')(conv7), conv2
    ],
                      axis=3)
    conv8 = Conv2D(64, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block8_conv1')(up8)
    conv8 = Conv2D(64, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block8_conv2')(conv8)

    up9 = concatenate([
        Conv2DTranspose(
            32, (2, 2), strides=(2, 2), padding='same',
            name='block9_dconv')(conv8), conv1
    ],
                      axis=3)
    conv9 = Conv2D(32, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block9_conv1')(up9)
    conv9 = Conv2D(32, (3, 3),
                   padding='same',
                   activation='relu',
                   name='block9_conv2')(conv9)

    side6 = UpSampling2D(size=(8, 8))(conv6)
    side7 = UpSampling2D(size=(4, 4))(conv7)
    side8 = UpSampling2D(size=(2, 2))(conv8)
    out6 = Conv2D(2, (1, 1), activation='sigmoid', name='side_63')(side6)
    out7 = Conv2D(2, (1, 1), activation='sigmoid', name='side_73')(side7)
    out8 = Conv2D(2, (1, 1), activation='sigmoid', name='side_83')(side8)
    out9 = Conv2D(2, (1, 1), activation='sigmoid', name='side_93')(conv9)

    out10 = average([out6, out7, out8, out9])

    model = Model(inputs=[img_input], outputs=[out6, out7, out8, out9, out10])

    return model
示例#17
0
def ResNet50(input_shape=(64, 64, 3), classes=6):
    """
   
    Arguments:
    input_shape -- shape of the images of the dataset
    classes -- integer, number of classes

    Returns:
    model -- a Model() instance in Keras
    """

    # Define the input as a tensor with shape input_shape
    X_input = Input(input_shape)

    print(X_input.shape)
    # Zero-Padding
    X = ZeroPadding2D((3, 3))(X_input)

    # Stage 1
    X = Conv2D(64, (7, 7),
               strides=(2, 2),
               name='conv1',
               kernel_initializer=glorot_uniform(seed=0))(X)

    X = BatchNormalization(axis=3, name='bn_conv1')(X)
    X = Activation('relu')(X)
    print(X.shape)
    X = ZeroPadding2D((1, 1))(X)
    X = MaxPooling2D((3, 3), strides=(2, 2))(X)

    # Stage 2
    X = convolutional_block(X,
                            f=3,
                            filters=[64, 64, 256],
                            stage=2,
                            block=1,
                            s=1)
    X = multiple_identity_block(X,
                                3, [64, 64, 256],
                                stage=2,
                                block=1,
                                iterator=2)
    print(X.shape)
    ### START CODE HERE ###

    # Stage 3 (≈4 lines)
    X = convolutional_block(X,
                            f=3,
                            filters=[128, 128, 512],
                            stage=3,
                            block=1,
                            s=2)
    X = X = multiple_identity_block(X,
                                    3, [128, 128, 512],
                                    stage=3,
                                    block=1,
                                    iterator=3)
    print(X.shape)
    # Stage 4 (≈6 lines)
    X = convolutional_block(X,
                            f=3,
                            filters=[256, 256, 1024],
                            stage=4,
                            block=1,
                            s=2)
    X = multiple_identity_block(X,
                                3, [256, 256, 1024],
                                stage=4,
                                block=1,
                                iterator=5)
    print(X.shape)

    # Stage 5 (≈3 lines)
    X = convolutional_block(X,
                            f=3,
                            filters=[512, 512, 2048],
                            stage=5,
                            block=1,
                            s=2)
    X = multiple_identity_block(X,
                                3, [512, 512, 2048],
                                stage=5,
                                block=1,
                                iterator=2)
    print(X.shape)
    # AVGPOOL (≈1 line). Use "X = AveragePooling2D(...)(X)"
    X = AveragePooling2D(pool_size=(2, 2),
                         strides=None,
                         padding='valid',
                         data_format=None)(X)

    ### END CODE HERE ###

    # output layer
    X = Flatten()(X)
    X = Dense(classes,
              activation='softmax',
              name='fc' + str(classes),
              kernel_initializer=glorot_uniform(seed=0))(X)
    print(X.shape)

    # Create model
    model = Model(inputs=X_input, outputs=X, name="Resnet50")

    return model
def resnet152_model(img_rows, img_cols, color_type=1, num_classes=None):
    """
    Resnet 152 Model for Keras
    Model Schema and layer naming follow that of the original Caffe implementation
    https://github.com/KaimingHe/deep-residual-networks
    ImageNet Pretrained Weights 
    Theano: https://drive.google.com/file/d/0Byy2AcGyEVxfZHhUT3lWVWxRN28/view?usp=sharing
    TensorFlow: https://drive.google.com/file/d/0Byy2AcGyEVxfeXExMzNNOHpEODg/view?usp=sharing
    Parameters:
      img_rows, img_cols - resolution of inputs
      channel - 1 for grayscale, 3 for color 
      num_classes - number of class labels for our classification task
    """
    eps = 1.1e-5

    # Handle Dimension Ordering for different backends
    global bn_axis
    if K.image_dim_ordering() == 'tf':
        bn_axis = 3
        img_input = Input(shape=(img_rows, img_cols, color_type), name='data')
    else:
        bn_axis = 1
        img_input = Input(shape=(color_type, img_rows, img_cols), name='data')

    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Convolution2D(64, 7, 7, subsample=(2, 2), name='conv1', bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=bn_axis, name='bn_conv1')(x)
    x = Scale(axis=bn_axis, name='scale_conv1')(x)
    x = Activation('relu', name='conv1_relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

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

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    for i in range(1, 8):
        x = identity_block(x, 3, [128, 128, 512], stage=3, block='b' + str(i))

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    for i in range(1, 36):
        x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b' + str(i))

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

    x_fc = AveragePooling2D((7, 7), name='avg_pool')(x)
    x_fc = Flatten()(x_fc)
    x_fc = Dense(1000, activation='softmax', name='fc1000')(x_fc)

    model = Model(img_input, x_fc)
    weights_path = resnet152_weights
    #    if K.image_dim_ordering() == 'th':
    #      # Use pre-trained weights for Theano backend
    #      weights_path = 'imagenet_models/resnet152_weights_th.h5'
    #    else:
    #      # Use pre-trained weights for Tensorflow backend
    #      weights_path = 'imagenet_models/resnet152_weights_tf.h5'

    model.load_weights(weights_path, by_name=True)

    # Truncate and replace softmax layer for transfer learning
    # Cannot use model.layers.pop() since model is not of Sequential() type
    # The method below works since pre-trained weights are stored in layers but not in the model
    x_newfc = AveragePooling2D((7, 7), name='avg_pool')(x)
    x_newfc = Flatten()(x_newfc)
    x_newfc = Dense(num_classes, activation='softmax', name='fc8')(x_newfc)

    model = Model(img_input, x_newfc)

    # Learning rate is changed to 0.001
    #    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    #    model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])

    return model
示例#19
0
X_train = X_train.reshape(X_train.shape[0], IMG_SIZE, IMG_SIZE, 1)
'''
Define model structure.
'''

#Initialize model.
model = Sequential()

#Convolutional layers with pooling.
model.add(
    Conv2D(32, (5, 5), activation="relu", input_shape=(IMG_SIZE, IMG_SIZE, 1)))
model.add(MaxPooling2D(pool_size=(5, 5), strides=(2, 2)))

model.add(Conv2D(64, (3, 3), activation="relu"))
model.add(Conv2D(64, (3, 3), activation="relu"))
model.add(AveragePooling2D(pool_size=(3, 3), strides=(2, 2)))

model.add(Conv2D(128, (3, 3), activation="relu"))
model.add(Conv2D(128, (3, 3), activation="relu"))
model.add(AveragePooling2D(pool_size=(3, 3), strides=(2, 2)))

#Fully connected layer followed by 0.5 dropout.
model.add(Flatten())
model.add(Dense(1024, activation="relu"))
model.add(Dropout(0.5))

#Output layer.
model.add(Dense(NUM_CLASSES, activation="softmax"))

#Compile model.
model.compile(loss="categorical_crossentropy",
示例#20
0
model.add(
    Convolution2D(32, 3, 3, border_mode='valid',
                  input_shape=X_train.shape[1:]))  # C1 卷积层
model.add(Activation('relu'))  # 激活函数:relu, tanh, sigmoid

model.add(Convolution2D(32, 3, 3))  # C2 卷积层
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))  # S3 池化
model.add(Dropout(0.25))  #

model.add(Convolution2D(64, 3, 3, border_mode='valid'))  # C4
model.add(Activation('relu'))

model.add(Convolution2D(64, 3, 3))  # C5
model.add(Activation('relu'))
model.add(AveragePooling2D(pool_size=(2, 2)))  # S6
model.add(Dropout(0.25))

model.add(Flatten())  # bottleneck 瓶颈
model.add(Dense(512))  # F7 全连接层, 512个神经元
model.add(Activation('relu'))  #
model.add(Dropout(0.5))

model.add(Dense(nb_classes))  # label为0~9共10个类别
model.add(Activation('softmax'))  # softmax 分类器
model.summary()  # 模型小节
print("建模CNN完成 ...")

###################
# 2. 训练CNN模型
###################
示例#21
0
def resnet_v2(input_shape, depth, num_classes=10, activation='relu'):
    """ResNet Version 2 Model builder [b]

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

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

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

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

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

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

            x = keras.layers.add([x, y])

        num_filters_in = num_filters_out

    # Add classifier on top.
    # v2 has BN-ReLU before Pooling
    x = BatchNormalization()(x)
    x = Activation(activation)(x)
    x = AveragePooling2D(pool_size=8)(x)
    y = Flatten()(x)
    outputs = Dense(num_classes,
                    activation='softmax',
                    kernel_initializer='he_normal')(y)

    # Instantiate model.
    model = Model(inputs=inputs, outputs=outputs)
    return model
def createGoogLeNet():
    # Our shape is 3 for the RGB, 224 for number of pixels in the width, 224 for the number of pixels in the height.
    input = Input(shape=(3, 224, 224))
    # First convolution layer of googLeNet has a patch size of 7x7 and a stride of 2 pixels.
    # Has an output size of 64, a filter of height 7px, and a stride of 2pixels. These are specified in the first three parameters
    # padding='same', I am going with same as im pretty sure its what the original GoogLeNet used.
    # we are using the relu activation.
    conv1 = Conv2D(64, 7, 2, padding='same', activation='relu')(input)

    # Next we max pool. We pass conv1 as we want to hook up our layers.
    pool1 = MaxPooling2D(3, 2, padding='same', data_format=None)(conv1)

    # Next another 2D convolution
    conv2 = Conv2D(192, 3, 1, padding='same', activation='relu')(pool1)

    # Pooling layer
    pool2 = MaxPooling2D(3, 2, padding='same', data_format=None)(conv2)

    #Now we create the inception layers which are just 2D convolution layers
    inception3A_1 = Conv2D(64, 1, 1, padding='same', activation='relu')(pool2)
    inception3A_3_reduce = Conv2D(96, 1, 1, padding='same',
                                  activation='relu')(pool2)
    inception3A_3 = Conv2D(128, 3, 3, padding='same',
                           activation='relu')(inception3A_3_reduce)
    inception3A_5_reduce = Conv2D(16, 1, 1, padding='same',
                                  activation='relu')(pool2)
    inception3A_5 = Conv2D(64, 1, 1, padding='same',
                           activation='relu')(inception3A_5_reduce)
    inception3APool = MaxPooling2D(3, 1, padding='same')(pool2)
    inception3APoolProj = Conv2D(32, 1, 1, padding='same',
                                 activation='relu')(inception3APool)
    inception3AMerged = concatenate(
        [inception3A_1, inception3A_3, inception3A_5, inception3APoolProj], 1)

    #Inception 3b
    inception3B_1 = Conv2D(128, 1, 1, padding='same',
                           activation='relu')(inception3AMerged)
    inception3B_3_reduce = Conv2D(128, 1, 1, padding='same',
                                  activation='relu')(inception3AMerged)
    inception3B_3 = Conv2D(192, 3, 3, padding='same',
                           activation='relu')(inception3B_3_reduce)
    inception3B_5_reduce = Conv2D(32, 1, 1, padding='same',
                                  activation='relu')(inception3AMerged)
    inception3B_5 = Conv2D(96, 1, 1, padding='same',
                           activation='relu')(inception3B_5_reduce)
    inception3BPool = MaxPooling2D(3, 1, padding='same')(inception3AMerged)
    inception3BPoolProj = Conv2D(64, 1, 1, padding='same',
                                 activation='relu')(inception3BPool)
    inception3BMerged = concatenate(
        [inception3B_1, inception3B_3, inception3B_5, inception3BPoolProj], 1)

    #Max pool
    pool3 = MaxPooling2D(3, 2, padding='same',
                         data_format=None)(inception3inception3BMerged)

    #Inception 4A
    inception4A_1 = Conv2D(192, 1, 1, padding='same', activation='relu')(pool3)
    inception4A_3_reduce = Conv2D(96, 1, 1, padding='same',
                                  activation='relu')(pool3)
    inception4A_3 = Conv2D(208, 3, 3, padding='same',
                           activation='relu')(inception4A_3_reduce)
    inception4A_5_reduce = Conv2D(16, 1, 1, padding='same',
                                  activation='relu')(pool3)
    inception4A_5 = Conv2D(48, 1, 1, padding='same',
                           activation='relu')(inception4A_5_reduce)
    inception4APool = MaxPooling2D(3, 1, padding='same')(pool3)
    inception4APoolProj = Conv2D(64, 1, 1, padding='same',
                                 activation='relu')(inception4APool)
    inception4AMerged = concatenate(
        [inception4A_1, inception4A_3, inception4A_5, inception4APoolProj], 1)

    #Inception 4B
    inception4B_1 = Conv2D(160, 1, 1, padding='same',
                           activation='relu')(inception4AMerged)
    inception4B_3_reduce = Conv2D(112, 1, 1, padding='same',
                                  activation='relu')(inception4AMerged)
    inception4B_3 = Conv2D(224, 3, 3, padding='same',
                           activation='relu')(inception4B_3_reduce)
    inception4B_5_reduce = Conv2D(24, 1, 1, padding='same',
                                  activation='relu')(inception4AMerged)
    inception4B_5 = Conv2D(64, 1, 1, padding='same',
                           activation='relu')(inception4B_5_reduce)
    inception4BPool = MaxPooling2D(3, 1, padding='same')(inception4AMerged)
    inception4BPoolProj = Conv2D(64, 1, 1, padding='same',
                                 activation='relu')(inception4BPool)
    inception4BMerged = concatenate(
        [inception4B_1, inception4B_3, inception4B_5, inception4BPoolProj], 1)

    #Inception 4C
    inception4C_1 = Conv2D(128, 1, 1, padding='same',
                           activation='relu')(inception4BMerged)
    inception4C_3_reduce = Conv2D(128, 1, 1, padding='same',
                                  activation='relu')(inception4BMerged)
    inception4C_3 = Conv2D(256, 3, 3, padding='same',
                           activation='relu')(inception4C_3_reduce)
    inception4C_5_reduce = Conv2D(24, 1, 1, padding='same',
                                  activation='relu')(inception4BMerged)
    inception4C_5 = Conv2D(64, 1, 1, padding='same',
                           activation='relu')(inception4C_5_reduce)
    inception4CPool = MaxPooling2D(3, 1, padding='same')(inception4BMerged)
    inception4CPoolProj = Conv2D(64, 1, 1, padding='same',
                                 activation='relu')(inception4CPool)
    inception4CMerged = concatenate(
        [inception4C_1, inception4C_3, inception4C_5, inception4CPoolProj], 1)

    #Inception 4D
    inception4D_1 = Conv2D(112, 1, 1, padding='same',
                           activation='relu')(inception4CMerged)
    inception4D_3_reduce = Conv2D(144, 1, 1, padding='same',
                                  activation='relu')(inception4CMerged)
    inception4D_3 = Conv2D(288, 3, 3, padding='same',
                           activation='relu')(inception4D_3_reduce)
    inception4D_5_reduce = Conv2D(32, 1, 1, padding='same',
                                  activation='relu')(inception4CMerged)
    inception4D_5 = Conv2D(64, 1, 1, padding='same',
                           activation='relu')(inception4D_5_reduce)
    inception4DPool = MaxPooling2D(3, 1, padding='same')(inception4CMerged)
    inception4DPoolProj = Conv2D(64, 1, 1, padding='same',
                                 activation='relu')(inception4DPool)
    inception4DMerged = concatenate(
        [inception4D_1, inception4D_3, inception4D_5, inception4DPoolProj], 1)

    #Inception 4E
    inception4E_1 = Conv2D(256, 1, 1, padding='same',
                           activation='relu')(inception4DMerged)
    inception4E_3_reduce = Conv2D(160, 1, 1, padding='same',
                                  activation='relu')(inception4DMerged)
    inception4E_3 = Conv2D(320, 3, 3, padding='same',
                           activation='relu')(inception4E_3_reduce)
    inception4E_5_reduce = Conv2D(32, 1, 1, padding='same',
                                  activation='relu')(inception4DMerged)
    inception4E_5 = Conv2D(128, 1, 1, padding='same',
                           activation='relu')(inception4E_5_reduce)
    inception4EPool = MaxPooling2D(3, 1, padding='same')(inception4DMerged)
    inception4EPoolProj = Conv2D(128, 1, 1, padding='same',
                                 activation='relu')(inception4EPool)
    inception4EMerged = concatenate(
        [inception4E_1, inception4E_3, inception4E_5, inception4EPoolProj], 1)

    #Max pool
    pool4 = MaxPooling2D(3, 2, padding='same',
                         data_format=None)(inception4EMerged)

    #Inception5A
    inception5A_1 = Conv2D(256, 1, 1, padding='same', activation='relu')(pool4)
    inception5A_3_reduce = Conv2D(160, 1, 1, padding='same',
                                  activation='relu')(pool4)
    inception5A_3 = Conv2D(320, 3, 3, padding='same',
                           activation='relu')(inception5A_3_reduce)
    inception5A_5_reduce = Conv2D(32, 1, 1, padding='same',
                                  activation='relu')(pool4)
    inception5A_5 = Conv2D(128, 1, 1, padding='same',
                           activation='relu')(inception5A_5_reduce)
    inception5APool = MaxPooling2D(3, 1, padding='same')(pool4)
    inception5APoolProj = Conv2D(128, 1, 1, padding='same',
                                 activation='relu')(inception5APool)
    inception5AMerged = concatenate(
        [inception5A_1, inception5A_3, inception5A_5, inception5APoolProj], 1)

    #Inception5B
    inception5B_1 = Conv2D(384, 1, 1, padding='same',
                           activation='relu')(inception5AMerged)
    inception5B_3_reduce = Conv2D(192, 1, 1, padding='same',
                                  activation='relu')(inception5AMerged)
    inception5B_3 = Conv2D(384, 3, 3, padding='same',
                           activation='relu')(inception5B_3_reduce)
    inception5B_5_reduce = Conv2D(48, 1, 1, padding='same',
                                  activation='relu')(inception5AMerged)
    inception5B_5 = Conv2D(128, 1, 1, padding='same',
                           activation='relu')(inception5B_5_reduce)
    inception5BPool = MaxPooling2D(3, 1, padding='same')(inception5AMerged)
    inception5BPoolProj = Conv2D(128, 1, 1, padding='same',
                                 activation='relu')(inception5BPool)
    inception5BMerged = concatenate(
        [inception5B_1, inception5B_3, inception5B_5, inception5BPoolProj], 1)

    #Average pooling layer
    avgPool = AveragePooling2D(pool_size=(7, 7),
                               strides=1,
                               padding='same',
                               data_format=None)(inception5BMerged)

    #Dropout
    droppedOut = Dropout(0.40)(avgPool)

    #Linear
    linearLayer = linear()(droppedOut)

    #Softmax
    lastSoftMax = Softmax()(linearLayer)

    #Now we declare the model
    googleNet = Model(input=input,
                      output=[
                          loss1_classifier_act, loss2_classifier_act,
                          loss3_classifier_act
                      ])
示例#23
0
def get_inception_resnet_v2_unet(input_shape, weights='imagenet'):
    inp = Input(input_shape + (9,))
    
    # Stem block: 35 x 35 x 192
    x = conv2d_bn(inp, 32, 3, strides=2, padding='same')
    x = conv2d_bn(x, 32, 3, padding='same')
    x = conv2d_bn(x, 64, 3)
    conv1 = x
    x = MaxPooling2D(3, strides=2, padding='same')(x)
    x = conv2d_bn(x, 80, 1, padding='same')
    x = conv2d_bn(x, 192, 3, padding='same')
    conv2 = x
    x = MaxPooling2D(3, strides=2, padding='same')(x)

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

    # 10x block35 (Inception-ResNet-A block): 35 x 35 x 320
    for block_idx in range(1, 11):
        x = inception_resnet_block(x,
                                   scale=0.17,
                                   block_type='block35',
                                   block_idx=block_idx)
    conv3 = x
    # Mixed 6a (Reduction-A block): 17 x 17 x 1088
    branch_0 = conv2d_bn(x, 384, 3, strides=2, padding='same')
    branch_1 = conv2d_bn(x, 256, 1)
    branch_1 = conv2d_bn(branch_1, 256, 3)
    branch_1 = conv2d_bn(branch_1, 384, 3, strides=2, padding='same')
    branch_pool = MaxPooling2D(3, strides=2, padding='same')(x)
    branches = [branch_0, branch_1, branch_pool]
    x = Concatenate(axis=channel_axis, name='mixed_6a')(branches)

    # 20x block17 (Inception-ResNet-B block): 17 x 17 x 1088
    for block_idx in range(1, 21):
        x = inception_resnet_block(x,
                                   scale=0.1,
                                   block_type='block17',
                                   block_idx=block_idx)
    conv4 = x
    # Mixed 7a (Reduction-B block): 8 x 8 x 2080
    branch_0 = conv2d_bn(x, 256, 1)
    branch_0 = conv2d_bn(branch_0, 384, 3, strides=2, padding='same')
    branch_1 = conv2d_bn(x, 256, 1)
    branch_1 = conv2d_bn(branch_1, 288, 3, strides=2, padding='same')
    branch_2 = conv2d_bn(x, 256, 1)
    branch_2 = conv2d_bn(branch_2, 288, 3)
    branch_2 = conv2d_bn(branch_2, 320, 3, strides=2, padding='same')
    branch_pool = MaxPooling2D(3, strides=2, padding='same')(x)
    branches = [branch_0, branch_1, branch_2, branch_pool]
    x = Concatenate(axis=channel_axis, name='mixed_7a')(branches)

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

    # Final convolution block: 8 x 8 x 1536
    x = conv2d_bn(x, 1536, 1, name='conv_7b')
    conv5 = x
    
    up6 = concatenate([UpSampling2D()(conv5), conv4], axis=-1)
    conv6 = conv_block(up6, 128)
    conv6 = conv_block(conv6, 128)

    up7 = concatenate([UpSampling2D()(conv6), conv3], axis=-1)
    conv7 = conv_block(up7, 96)
    conv7 = conv_block(conv7, 96)

    up8 = concatenate([UpSampling2D()(conv7), conv2], axis=-1)
    conv8 = conv_block(up8, 64)
    conv8 = conv_block(conv8, 64)

    up9 = concatenate([UpSampling2D()(conv8), conv1], axis=-1)
    conv9 = conv_block(up9, 48)
    conv9 = conv_block(conv9, 48)

    up10 = concatenate([UpSampling2D()(conv9), inp], axis=-1)
    conv10 = conv_block(up10, 32)
    conv10 = conv_block(conv10, 32)
#    conv10 = SpatialDropout2D(0.33)(conv10)
    res = Conv2D(1, (1, 1), activation='sigmoid')(conv10)
    model = Model(inp, res)
    
    if weights == 'imagenet':
        inception_resnet_v2 = InceptionResNetV2(weights=weights, include_top=False, input_shape=input_shape + (3,))
        for i in range(2, len(inception_resnet_v2.layers)-1):
            model.layers[i].set_weights(inception_resnet_v2.layers[i].get_weights())
            model.layers[i].trainable = False
        
    return model
示例#24
0
def train_a_model(trainfile):
    '''
    :param trainfile:
    :return:
    '''
    #load the dataset using pandas
    data = pd.read_csv(trainfile)
    #check len of rows and its intance len

    with open(trainfile) as f:
        content = f.readlines()

    lines = np.array(content)

    num_of_instances = lines.size
    print("number of instances: ", num_of_instances)

    #save the data in train and validation data
    x_train, y_train = [], []

    for i in range(1, num_of_instances):
        try:
            emotion = lines[i].split(",")[0]

            val = lines[i].split(",")[1:]

            pixels = np.array(val, 'float32')

            y_train.append(emotion)
            x_train.append(pixels)
        except:
            print("", end="")

    # data transformation for train and test sets
    x_train = np.array(x_train, 'float32')

    x_train /= 255  #normalize inputs between [0, 1]

    x_train = x_train.reshape(x_train.shape[0], 48, 48, 1)
    x_train = x_train.astype('float32')

    print(x_train.shape[0], 'train samples')

    y_train = []

    for i in range(1, num_of_instances):
        try:
            emotion = lines[i].split(",")[0]

            y_train.append(emotion)
        except:
            print("", end="")

    le = LabelEncoder()
    y_train = le.fit_transform(y_train)
    a_train = []
    for i in range(1, num_of_instances):
        emotion = y_train[i - 1]
        emotion = keras.utils.to_categorical(emotion, 3)

        a_train.append(emotion)

    y_train = a_train
    y_train = np.array(y_train, 'float32')

    #variables
    num_classes = 3
    #fear, happy, sad
    batch_size = 256
    epochs = 10

    #construct CNN structure
    model = Sequential()

    #1st convolution layer
    model.add(Conv2D(64, (5, 5), activation='relu', input_shape=(48, 48, 1)))
    model.add(MaxPooling2D(pool_size=(5, 5), strides=(2, 2)))

    #2nd convolution layer
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(AveragePooling2D(pool_size=(3, 3), strides=(2, 2)))

    #3rd convolution layer
    model.add(Conv2D(128, (3, 3), activation='relu'))
    model.add(Conv2D(128, (3, 3), activation='relu'))
    model.add(AveragePooling2D(pool_size=(3, 3), strides=(2, 2)))

    model.add(Flatten())

    #fully connected neural networks
    model.add(Dense(1024, activation='relu'))
    model.add(Dropout(0.2))

    #-----------------------------
    model.add(Dense(128))
    model.add(Activation('relu'))
    model.add(Dropout(0.3))
    #-----------------------------

    model.add(Dense(1024, activation='relu'))
    model.add(Dropout(0.2))

    model.add(Dense(num_classes, activation='softmax'))

    ################################
    #------------------------------
    #batch process
    gen = ImageDataGenerator()
    train_generator = gen.flow(x_train, y_train, batch_size=batch_size)

    #------------------------------

    model.compile(loss='categorical_crossentropy',
                  optimizer=keras.optimizers.Adam(),
                  metrics=['accuracy'])

    #------------------------------

    print('Training start please wait....')
    #model.fit_generator(x_train, y_train, epochs=epochs) #train for all trainset
    model.fit_generator(train_generator,
                        steps_per_epoch=batch_size,
                        epochs=epochs)  #train for randomly selected one

    print("model Training completed")
    model.save('models.h5')

    return model
示例#25
0
 F1 = 8
 block1 = (Conv2D(F1, (1, kernLength),
                  padding='same',
                  input_shape=(Xall[train].shape[1], Xall[train].shape[0],
                               1),
                  use_bias=False))(input1)
 block1 = BatchNormalization(axis=1)(block1)
 block1 = DepthwiseConv2D((Xall.shape[1], 1),
                          use_bias=False,
                          padding='same',
                          depth_multiplier=2,
                          depthwise_constraint=max_norm(1.),
                          data_format='channels_first')(block1)
 block1 = BatchNormalization(axis=1)(block1)
 block1 = Activation('elu')(block1)
 block1 = AveragePooling2D((1, 4), data_format='channels_first')(block1)
 block1 = Dropout(dropoutRate)(block1)
 block2 = SeparableConv2D(16, (1, 16),
                          use_bias=False,
                          padding='same',
                          data_format='channels_first')(block1)
 block2 = BatchNormalization(axis=1)(block2)
 block2 = Activation('elu')(block2)
 block2 = AveragePooling2D((1, 8),
                           data_format='channels_first',
                           padding='same')(block2)
 block2 = Dropout(dropoutRate)(block2)
 flatten = Flatten(name='flatten')(block2)
 dense = Dense(nbClasses,
               name='dense',
               kernel_constraint=max_norm(normRate))(flatten)
示例#26
0
def train(batchsize, epochs, l_nodes, l_dropout, l_rate, momentum,
          modules_to_drop, l_decay):
    model_name = 'ResNet50_FC_Ep_' + str(epochs) + '_N_' + str(
        l_nodes) + '_Decay_' + str(l_decay) + '_LR_' + str(
            l_rate) + '_DO_' + str(l_dropout) + '_'
    print("Training:", model_name)

    datagen = image.ImageDataGenerator(
        preprocessing_function=imagenet_utils.preprocess_input,
        horizontal_flip=True,
        shear_range=0.15,
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2)

    train_generator = datagen.flow_from_directory(PATH_TRAIN,
                                                  batch_size=batchsize,
                                                  target_size=target_size)

    test_generator = datagen.flow_from_directory(PATH_TEST,
                                                 batch_size=batchsize,
                                                 target_size=target_size)

    num_train = train_generator.n
    num_test = test_generator.n

    if modules_to_drop == 0:
        layers_to_drop = 0
    elif modules_to_drop == 1:
        layers_to_drop = 11
    elif modules_to_drop == 2:
        layers_to_drop = 21
    elif modules_to_drop == 3:
        layers_to_drop = 10

    input_tensor = Input(shape=(224, 224, 3))
    base_model = inception_v3.InceptionV3(include_top=False,
                                          weights='imagenet',
                                          input_tensor=input_tensor)

    # To keep trainable last conv module use :147
    for layer in base_model.layers[:20]:
        layer.trainable = False

    for i in range(layers_to_drop):
        base_model.layers.pop()
        base_model.outputs = [base_model.layers[-1].output]
        base_model.layers[-1].outbound_nodes = []

    x = base_model.layers[-1].output
    x_fc = AveragePooling2D((4, 4), strides=(8, 8), name='avg_pool')(x)

    #x = Dense(l_nodes, activation='relu', kernel_regularizer=regularizers.l2(0.1))(x)
    #x = K.reshape(x,(1,-1,l_nodes,1))
    #x = Conv2D(10,3,activation='relu',padding='same',kernel_regularizer=regularizers.l2(0.1))(x)
    #x = GlobalAveragePooling2D()(x)
    #x = Dense(l_nodes, activation='relu', kernel_regularizer=regularizers.l2(0.1))(x)
    #x = Dense(l_nodes, activation='relu')(x)
    x_fc = Flatten(name='flatten')(x_fc)
    #x = Dropout(l_dropout)(x)

    predictions = Dense(120, activation='softmax', name='predictions')(x_fc)
    model = Model(inputs=base_model.input, outputs=predictions)

    model.compile(optimizer=optimizers.Adam(lr=l_rate, decay=l_decay),
                  loss='categorical_crossentropy',
                  metrics=['acc'])

    try:
        model.fit_generator(
            train_generator,
            steps_per_epoch=num_train / batchsize * 2.0,
            validation_data=test_generator,
            validation_steps=num_test / batchsize,
            epochs=epochs,
            use_multiprocessing=True,
            callbacks=[
                callbacks.ModelCheckpoint(filepath=PATH_MODELS + model_name +
                                          '{val_acc:.2f}.hdf5',
                                          monitor='val_acc',
                                          save_best_only=True,
                                          save_weights_only=False),
                callbacks.EarlyStopping(monitor='val_loss', patience=3),
            ],
        )
    except KeyboardInterrupt:
        pass

    score, accuracy = model.evaluate_generator(test_generator,
                                               steps=num_test / batchsize,
                                               use_multiprocessing=True)

    print("Final Accuracy: {:.2f}%".format(accuracy * 100))
示例#27
0
masks = K.concatenate([style_mask, target_mask], axis=0)

# index constants for images and tasks variables
STYLE, TARGET, CONTENT = 0, 1, 2

# build image model, mask model and target
# vgg19 for image model
image_model = vgg19.VGG19(include_top=False, input_tensor=images)
# pooling layers for mask model
mask_input = Input(tensor=masks, shape=(None, None, None), name="mask_input")
x = mask_input
for layer in image_model.layers[1:]:
    name = 'mask_%s' % layer.name
    if 'conv' in layer.name:
        x = AveragePooling2D((3, 3),
                             strides=(1, 1),
                             name=name,
                             border_mode="same")(x)
        #x = MaxPooling2D((3, 3), strides = (1, 1), name=name, border_mode="same", )(x)
    elif 'pool' in layer.name:
        #x = MaxPooling2D((2, 2), name=name)(x)
        x = AveragePooling2D((2, 2), name=name)(x)
mask_model = Model(mask_input, x)
# collect features from image_model and task_model
image_features = {}
mask_features = {}
for img_layer, mask_layer in zip(image_model.layers, mask_model.layers):
    if 'conv' in img_layer.name:
        assert 'mask_' + img_layer.name == mask_layer.name
        layer_name = img_layer.name
        img_feat, mask_feat = img_layer.output, mask_layer.output
        image_features[layer_name] = img_feat
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='valid'))
#13x13x512

model.add(Conv2D(1024, (1, 1), padding='same', activation='relu'))
model.add(Conv2D(1024, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(1024, (5, 5), padding='same', activation='relu'))
#model.add(Conv2D(2048, (7,7), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='valid'))
#6x6x1024

model.add(Conv2D(1256, (3, 3), padding='same', activation='relu'))
model.add(Conv2D(1256, (1, 1), padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), padding='valid'))
#2x2x1256

model.add(AveragePooling2D(pool_size=(2, 2), strides=(1, 1), padding='valid'))
#1x1x1256

model.add(Flatten())

#model.add(Dense(4096, activation='relu', use_bias=True))
model.add(Dense(628, activation='relu', use_bias=True))
model.add(Dense(314, activation='relu', use_bias=True))
model.add(Dense(5, activation='softmax', use_bias=True))

print("Model created!!")
model.summary()
print("\nCompiling model...")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sgd = SGD(momentum=0.9, nesterov=True, lr=0.003)
def ResNet50(input_shape = (64, 64, 3), classes = 6):
    """
    Implementation of the popular ResNet50 the following architecture:
    CONV2D -> BATCHNORM -> RELU -> MAXPOOL -> CONVBLOCK -> IDBLOCK*2 -> CONVBLOCK -> IDBLOCK*3
    -> CONVBLOCK -> IDBLOCK*5 -> CONVBLOCK -> IDBLOCK*2 -> AVGPOOL -> TOPLAYER

    Arguments:
    input_shape -- shape of the images of the dataset
    classes -- integer, number of classes

    Returns:
    model -- a Model() instance in Keras
    """
    
    # Define the input as a tensor with shape input_shape
    X_input = Input(input_shape)

    
    # Zero-Padding
    X = ZeroPadding2D((3, 3))(X_input)
    
    # Stage 1
    X = Conv2D(64, (7, 7), strides = (2, 2), name = 'conv1', kernel_initializer = glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis = 3, name = 'bn_conv1')(X)
    X = Activation('relu')(X)
    X = MaxPooling2D((3, 3), strides=(2, 2))(X)

    # Stage 2
    X = convolutional_block(X, f = 3, filters = [64, 64, 256], stage = 2, block='a', s = 1)
    X = identity_block(X, 3, [64, 64, 256], stage=2, block='b')
    X = identity_block(X, 3, [64, 64, 256], stage=2, block='c')

    ### START CODE HERE ###

    # Stage 3 (≈4 lines)
    s=2
    f=3
    filters =  [128,128,512]
    stage=3
    X = convolutional_block(X, f, filters, stage, block='a', s=2)
    X = identity_block(X, f, filters, stage, block='b')
    X = identity_block(X, f, filters, stage, block='c')
    X = identity_block(X, f, filters, stage, block='d')

    # Stage 4 (≈6 lines)
    filters = [256, 256, 1024]
    stage=4
    X = convolutional_block(X, f, filters, stage, block='a', s=2)
    X = identity_block(X, f, filters, stage, block='b')
    X = identity_block(X, f, filters, stage, block='c')
    X = identity_block(X, f, filters, stage, block='d')
    X = identity_block(X, f, filters, stage, block='e')
    X = identity_block(X, f, filters, stage, block='f')

    # Stage 5 (≈3 lines)
    filters =  [512, 512, 2048]
    stage=5
    X = convolutional_block(X, f, filters, stage, block='a', s=2)
    X = identity_block(X, f, filters, stage, block='b')
    X = identity_block(X, f, filters, stage, block='c')

    # AVGPOOL (≈1 line). Use "X = AveragePooling2D(...)(X)"
    X = AveragePooling2D(pool_size=(2, 2), padding='same')(X)
    
    ### END CODE HERE ###

    # output layer
    X = Flatten()(X)
    X = Dense(classes, activation='softmax', name='fc' + str(classes), kernel_initializer = glorot_uniform(seed=0))(X)
    
    
    # Create model
    model = Model(inputs = X_input, outputs = X, name='ResNet50')

    return model
示例#30
0
def build_resnet_model(params):

    conv1_ksize = params['filters_1']
    conv1_nfilter = params['filters']

    kernel_size_1 = params['repetitions_1']
    kernel_size_2 = params['repetitions_3']
    kernel_size_3 = params['repetitions_5']
    kernel_size_4 = params['repetitions_7']

    num_filter_1 = params['filters_2']
    num_filter_2 = params['filters_3']
    num_filter_3 = params['filters_4']
    num_filter_4 = params['filters_5']

    reps_1 = params['repetitions']
    reps_2 = params['repetitions_2']
    reps_3 = params['repetitions_4']
    reps_4 = params['repetitions_6']

    conv2_nfilter = params['filters_6']

    regularized_coff_1 = params['l2']
    regularized_coff_2 = params['l2_1']
    regularized_coff_3 = params['l2_2']
    learning_rate = params['l2_3']
    input_shape = params['input_shape']
    ts = input_shape[1]
    tickers = input_shape[0]

    input = Input(shape=input_shape)
    conv1 = conv_bn_relu(filters=conv1_nfilter,kernel_size=(1,conv1_ksize),strides=(1,1),\
                         kernel_regularizer=regularizers.l2(regularized_coff_1)) (input)

    pool1 = MaxPooling2D(pool_size=(1, 3), strides=(1, 2),
                         padding="same")(conv1)

    out = residual_block(filters=num_filter_1, repetitions=reps_1 ,kernel_size=(1,kernel_size_1),\
                         strides=(1,2),is_first_layer=True) (pool1)

    out = residual_block(filters=num_filter_2, repetitions=reps_2,\
                         kernel_size=(1,kernel_size_2), strides=(1,2)) (out)

    out = residual_block(filters=num_filter_3, repetitions=reps_3,\
                         kernel_size=(1,kernel_size_3),strides=(1,2)) (out)

    out = residual_block(filters=num_filter_4, repetitions=reps_4,\
                         kernel_size=(1,kernel_size_4),strides=(1,2)) (out)

    out = bn_relu(out)

    conv2 = conv_bn_relu(filters=conv2_nfilter,kernel_size=(381,1),strides=(1,1),\
                     kernel_regularizer=regularizers.l2(regularized_coff_2),padding='valid') (out)

    out_shape = K.int_shape(conv2)
    out = AveragePooling2D(pool_size=(out_shape[1], out_shape[2]),
                           strides=(1, 1))(conv2)

    out = Flatten()(out)

    out = Dense(tickers,
                kernel_regularizer=regularizers.l2(regularized_coff_3))(out)
    out = Activation('sigmoid')(out)

    model = Model([input], [out])
    optimizer = Adam(lr=learning_rate)
    model.compile(loss=sharpe_ratio_loss,
                  optimizer=optimizer,
                  metrics=[sharpe_ratio])

    return model