def InceptionV1(include_top=True,
                #weights= None,
                weights= 'imagenet',
                input_tensor=None,
                input_shape=None,
                pooling=None,
                classes=2):
    """Instantiates the Inception v1 architecture.
    This architecture is defined in:
        Going deeper with convolutions
        Christian Szegedy, Wei Liu, Yangqing Jia, Pierre Sermanet, Scott Reed,
        Dragomir Anguelov, Dumitru Erhan, Vincent Vanhoucke, Andrew Rabinovich.
        http://arxiv.org/abs/1409.4842v1
    
    Optionally loads weights pre-trained
    on ImageNet. Note that when using TensorFlow,
    for best performance you should set
    `image_data_format="channels_last"` in your Keras config
    at ~/.keras/keras.json.
    The model and the weights are compatible with both
    TensorFlow and Theano. The data format
    convention used by the model is the one
    specified in your Keras config file.
    Note that the default input image size for this model is 224x224.
    Arguments:
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization)
            or "imagenet" (pre-training on ImageNet).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 139.
            E.g. `(150, 150, 3)` would be one valid value.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model will be
                the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a 2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.
    Returns:
        A Keras model instance.
    Raises:
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    if weights not in {'imagenet', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `imagenet` '
                         '(pre-training on ImageNet).')
   # if weights == 'imagenet' and include_top and classes != 1001:
   #     raise ValueError('If using `weights` as imagenet with `include_top`'
   #                      ' as true, `classes` should be 1001')
    # Determine proper input shape
    input_shape = _obtain_input_shape(
        input_shape,
        #default_size=299,
        default_size=224,
        min_size=139,
        data_format=K.image_data_format(),
        include_top=include_top)
    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        img_input = Input(tensor=input_tensor, shape=input_shape)
    if K.image_data_format() == 'channels_first':
        channel_axis = 1
    else:
        channel_axis = 3
    # 'Sequential bit at start'
    x = img_input
    x = conv2d_bn(x,  64, 7, 7, strides=(2, 2), padding='same',  name='Conv2d_1a_7x7')  
    
    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='MaxPool_2a_3x3')(x)  
    
    x = conv2d_bn(x,  64, 1, 1, strides=(1, 1), padding='same', name='Conv2d_2b_1x1')  
    x = conv2d_bn(x, 192, 3, 3, strides=(1, 1), padding='same', name='Conv2d_2c_3x3')  
    
    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='MaxPool_3a_3x3')(x)  
    
    # Now the '3' level inception units
    x = concatenated_block(x, (( 64,), ( 96,128), (16, 32), ( 32,)), channel_axis, 'Mixed_3b')
    x = concatenated_block(x, ((128,), (128,192), (32, 96), ( 64,)), channel_axis, 'Mixed_3c')
    x = MaxPooling2D((3, 3), strides=(2, 2), padding='same', name='MaxPool_4a_3x3')(x)  
    # Now the '4' level inception units
    x = concatenated_block(x, ((192,), ( 96,208), (16, 48), ( 64,)), channel_axis, 'Mixed_4b')
    x = concatenated_block(x, ((160,), (112,224), (24, 64), ( 64,)), channel_axis, 'Mixed_4c')
    x = concatenated_block(x, ((128,), (128,256), (24, 64), ( 64,)), channel_axis, 'Mixed_4d')
    x = concatenated_block(x, ((112,), (144,288), (32, 64), ( 64,)), channel_axis, 'Mixed_4e')
    x = concatenated_block(x, ((256,), (160,320), (32,128), (128,)), channel_axis, 'Mixed_4f')
    x = MaxPooling2D((2, 2), strides=(2, 2), padding='same', name='MaxPool_5a_2x2')(x)  
    # Now the '5' level inception units
    x = concatenated_block(x, ((256,), (160,320), (32,128), (128,)), channel_axis, 'Mixed_5b')
    x = concatenated_block(x, ((384,), (192,384), (48,128), (128,)), channel_axis, 'Mixed_5c')
    
    if include_top:
        # Classification block
        
        # 'AvgPool_0a_7x7'
        x = AveragePooling2D((7, 7), strides=(1, 1), padding='valid')(x)  
        
        # 'Dropout_0b'
        x = Dropout(0.5)(x)  # slim has keep_prob (@0.8), keras uses drop_fraction
        
        #logits = conv2d_bn(x,  classes+1, 1, 1, strides=(1, 1), padding='valid', name='Logits',
        #                   normalizer=False, activation=None, )  
        
        # Write out the logits explictly, since it is pretty different
        x = Conv2D(classes, (1, 1), strides=(1,1), padding='valid', use_bias=True, name='Logits')(x)
        
        #x = Flatten(name='Logits_flat')(x)
        #print(x.get_shape().as_list())
        #x = x[:, 1:]  # ??Shift up so that first class ('blank background') vanishes
        # Would be more efficient to strip off position[0] from the weights+bias terms directly in 'Logits'
        #x = Conv2DTranspose(2, (64, 64), strides=(32, 32), filters=x.get_shape().as_list(), padding='same')

        #x = Conv2DTranspose(2, (64, 64), strides=(32, 32), activation='softmax', padding='same')
        x = BilinearUpSampling2D(target_size=tuple(image_size))(x)
        #x = Conv2D(filters=2, 
        #       kernel_size=(1, 1))(x)
        #x = Conv2DTranspose(filters=2, 
        #                 kernel_size=(64, 64),
        #                strides=(32, 32),
        #                padding='same',
        #                activation='softmax',
                        #kernel_initializer=Constant(bilinear_upsample_weights(32, 2))
        #                 )(x)
        print(x.shape)


        x = Flatten(name='Logits_flat')(x)
        x = Activation('softmax', name='Predictions')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D(name='global_pooling')(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D(name='global_pooling')(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
        
    # Finally : Create model
    model = Model(inputs, x, name='inception_v1')
    
    # LOAD model weights
    if weights == 'imagenet':
        if K.image_data_format() == 'channels_first':
            if K.backend() == 'tensorflow':
                warnings.warn('You are using the TensorFlow backend, yet you '
                              'are using the Theano '
                              'image data format convention '
                              '(`image_data_format="channels_first"`). '
                              'For best performance, set '
                              '`image_data_format="channels_last"` in '
                              'your Keras config '
                              'at ~/.keras/keras.json.')
        if include_top:
            weights_path ='/home/wli/Downloads/googlenet0917-02-0.93.hdf5'

            #get_file(
            #    'inception_v1_weights_tf_dim_ordering_tf_kernels.h5',
            #    WEIGHTS_PATH,
            #    cache_subdir='models',
            #    md5_hash='723bf2f662a5c07db50d28c8d35b626d')
        else:

            weights_path ='/home/wli/Downloads/googlenet0917-02-0.93.hdf5'
            #weights_path = get_file(
            #    'inception_v1_weights_tf_dim_ordering_tf_kernels_notop.h5',
            #   WEIGHTS_PATH_NO_TOP,
            #    cache_subdir='models',
            #    md5_hash='6fa8ecdc5f6c402a59909437f0f5c975')
        model.load_weights('/home/wli/Downloads/googlenet0917-02-0.93.hdf5')
        if K.backend() == 'theano':
            convert_all_kernels_in_model(model)    
    
    return model
Beispiel #2
0




for (i,layer) in enumerate(ResNet50.layers):

    print(str(i) + " "+ layer.__class__.__name__, layer.trainable)


def addTopModel(bottom_model, num_class):


    my_model = bottom_model.output

    my_model = GlobalAveragePooling2D()(my_model)

    my_model = Dense(1024,activation='relu')(my_model)
    
    my_model = Dense(1024,activation='relu')(my_model)
    
    my_model = Dense(1024,activation='relu')(my_model)

    my_model = Dense(512,activation='relu')(my_model)

    my_model = Dense(num_classes,activation='softmax')(my_model)

    return my_model


Beispiel #3
0
import cv2.cv2 as cv2, numpy as np
from keras.applications import DenseNet121
from keras.layers import Dense, GlobalAveragePooling2D
from keras import Model


IMG_PATH = ['./chest_xray_images/normal/15268.jpg', '0']
IMG_SHAPE = (320, 320, 3)

test_img = load_img(path=IMG_PATH[0], color_mode='grayscale')
test_img = img_to_array(img=test_img, data_format='channels_last')
test_img = cv2.resize(test_img, dsize=IMG_SHAPE[:2], interpolation=cv2.INTER_NEAREST)
test_img = np.expand_dims(test_img, axis=-1)
test_img = test_img.astype(np.uint8)
test_img = test_img / 255.
test_img = np.concatenate((test_img, test_img, test_img), axis=-1)
print('external image(s) shape:', test_img.shape)

backbone = DenseNet121(include_top=False, weights=None, input_shape=(320, 320, 3))
backbone_out = backbone.output
gap = GlobalAveragePooling2D(name='pooling_layer')(backbone_out)
output = Dense(units=14, activation='softmax', name='output_layer')(gap)
chexnet_model = Model(inputs=backbone.input, outputs=output)
chexnet_model.summary()

chexnet_model.load_weights('C:/Users/Arman/Desktop/Covid19-Detection/checkpoints/CheXNet/CheXNet_v0.3.0.h5')
chexnet_model.compile(optimizer='adam', loss='binary_crossentropy')

chexnet_model.save(filepath='./checkpoints/CheXNet/CheXNet_model.hdf5')
print('sample prediction: \n', chexnet_model.predict(np.expand_dims(test_img, axis=0)))
Beispiel #4
0
def se_block(in_block, ch, ratio=8):
    z = GlobalAveragePooling2D()(in_block)
    x = Dense(ch//ratio, activation='relu')(z)
    x = Dense(ch, activation='sigmoid')(x)
    return Multiply()([in_block, x])
Beispiel #5
0
def TS_ConvMobileNet(input_shape=None,
                     alpha=1.0,
                     depth_multiplier=1,
                     dropout=1e-3,
                     pooling=None,
                     classes=1000):
    rows = None
    #     filter_num = [32,64,128,256,512,1024]
    filter_num = [16, 32, 64, 128, 256, 512]
    if input_shape:
        if len(input_shape
               ) == 4 and input_shape[0] == 128 and input_shape[3] in [1, 3]:
            img_input = Input(shape=input_shape)
        else:
            print('Please check the entered input shape.\n',
                  'The first input (timestep) must be 128\n',
                  'The last input must in 1 or 3')
            raise ValueError
    else:
        img_input = Input(shape=(128, 64, 64, 1))

    x = T_conv_block(img_input, filter_num[0], alpha, strides=(2, 2))
    x = T_depthwise_conv_block(x,
                               filter_num[1],
                               alpha,
                               depth_multiplier,
                               block_id=1)

    x = T_depthwise_conv_block(x,
                               filter_num[2],
                               alpha,
                               depth_multiplier,
                               strides=(2, 2),
                               block_id=2)
    x = T_depthwise_conv_block(x,
                               filter_num[2],
                               alpha,
                               depth_multiplier,
                               block_id=3)

    x = T_depthwise_conv_block(x,
                               filter_num[3],
                               alpha,
                               depth_multiplier,
                               strides=(2, 2),
                               block_id=4)
    x = T_depthwise_conv_block(x,
                               filter_num[3],
                               alpha,
                               depth_multiplier,
                               block_id=5)

    x = T_depthwise_conv_block(x,
                               filter_num[4],
                               alpha,
                               depth_multiplier,
                               strides=(2, 2),
                               block_id=6)
    x = T_depthwise_conv_block(x,
                               filter_num[4],
                               alpha,
                               depth_multiplier,
                               block_id=7)
    x = T_depthwise_conv_block(x,
                               filter_num[4],
                               alpha,
                               depth_multiplier,
                               block_id=8)
    x = T_depthwise_conv_block(x,
                               filter_num[4],
                               alpha,
                               depth_multiplier,
                               block_id=9)
    x = T_depthwise_conv_block(x,
                               filter_num[4],
                               alpha,
                               depth_multiplier,
                               block_id=10)
    x = T_depthwise_conv_block(x,
                               filter_num[4],
                               alpha,
                               depth_multiplier,
                               block_id=11)

    x = T_depthwise_conv_block(x,
                               filter_num[5],
                               alpha,
                               depth_multiplier,
                               strides=(2, 2),
                               block_id=12)
    x = T_depthwise_conv_block(x,
                               filter_num[5],
                               alpha,
                               depth_multiplier,
                               block_id=13)

    if backend.image_data_format() == 'channels_first':
        shape = (1, int(filter_num[5] * alpha))
        channelAxis = 1

    else:
        shape = (int(filter_num[5] * alpha), 1)
        channelAxis = -1

    if pooling == 'max':
        x = TimeDistributed(GlobalMaxPooling2D(), name="MAX_pool")(x)
    else:
        x = TimeDistributed(GlobalAveragePooling2D(), name="AVG_pool")(x)

    x = TimeDistributed(Reshape(shape, name='reshape_1'), name="reshape1")(x)

    # TODO: Conv
    # print(channelAxis)
    TimeFilter_num = [256, 256, 256]
    filter_step = [2, 16, 32]

    conv1 = Conv2D(TimeFilter_num[0],
                   kernel_size=(filter_step[0], int(filter_num[5] * alpha)),
                   padding='valid',
                   name='time_conv_%d_step' % filter_step[0])(x)
    conv1 = BatchNormalization(axis=channelAxis,
                               name='time_bn_%d_step' % filter_step[0])(conv1)
    conv1 = Activation(backend.relu,
                       name='time_relu_%d_step' % filter_step[0])(conv1)
    conv1 = MaxPool2D(pool_size=(128 - filter_step[0] + 1, 1),
                      strides=(1, 1),
                      padding='valid',
                      name='time_Max_%d_step' % filter_step[0])(conv1)

    conv2 = Conv2D(TimeFilter_num[1],
                   kernel_size=(filter_step[1], int(filter_num[5] * alpha)),
                   padding='valid',
                   name='time_conv_%d_step' % filter_step[1])(x)
    conv2 = BatchNormalization(axis=channelAxis,
                               name='time_bn_%d_step' % filter_step[1])(conv2)
    conv2 = Activation(backend.relu,
                       name='time_relu_%d_step' % filter_step[1])(conv2)
    conv2 = MaxPool2D(pool_size=(128 - filter_step[1] + 1, 1),
                      strides=(1, 1),
                      padding='valid',
                      name='time_Max_%d_step' % filter_step[1])(conv2)

    conv3 = Conv2D(TimeFilter_num[2],
                   kernel_size=(filter_step[2], int(filter_num[5] * alpha)),
                   padding='valid',
                   name='time_conv_%d_step' % filter_step[2])(x)
    conv3 = BatchNormalization(axis=channelAxis,
                               name='time_bn_%d_step' % filter_step[2])(conv3)
    conv3 = Activation(backend.relu,
                       name='time_relu_%d_step' % filter_step[2])(conv3)
    conv3 = MaxPool2D(pool_size=(128 - filter_step[2] + 1, 1),
                      strides=(1, 1),
                      padding='valid',
                      name='time_Max_%d_step' % filter_step[2])(conv3)

    concatenated_tensor = Concatenate(axis=1)([conv1, conv2, conv3])
    x = Flatten(name='flatten_concat')(concatenated_tensor)
    x = Dropout(dropout)(x)
    x = Dense(units=classes, activation='softmax')(x)

    # get inputs shape
    inputs = img_input
    print(inputs)

    # Create model.
    model = models.Model(inputs, x, name='mobilenet_%0.2f_%s' % (alpha, rows))

    return model
Beispiel #6
0
def ResNet152(include_top=True,
              weights=None,
              input_tensor=None,
              input_shape=None,
              large_input=False,
              pooling=None,
              classes=1000):
    '''Instantiate the ResNet152 architecture.
    
    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization)
            or "imagenet" (pre-training on ImageNet).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 224)` (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 197.
            E.g. `(200, 200, 3)` would be one valid value.
        large_input: if True, then the input shape expected will be 
            `(448, 448, 3)` (with `channels_last` data format) or
            `(3, 448, 448)` (with `channels_first` data format).
        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')

    eps = 1.1e-5
    '''
    if large_input:
        img_size = 448
    else:
        img_size = 224
    '''
    img_size = 448

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

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

    # handle dimension ordering for different backends
    if K.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)
        if K.backend() == 'theano':
            layer_utils.convert_all_kernels_in_model(model)
            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.image_data_format() == 'channels_first' and 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
def createModel_cqt_classification_fma_medium(input_dim, input_dim2,
                                              selected_optimizer,
                                              selected_loss):
    fixed_init = he_normal(seed=0)
    print('classifier model')
    input = Input(shape=(1, input_dim, input_dim2))  #1 x 80 x 1280
    out1 = Convolution2D(32, (3, 3),
                         activation='elu',
                         padding='same',
                         data_format='channels_first',
                         kernel_initializer=fixed_init)(input)  #32 x 80 x 1280
    out1 = Dropout(rate=0.1)(out1)
    out1 = BatchNormalization(axis=1)(out1)
    out1 = MaxPooling2D((2, 2), padding='same',
                        data_format='channels_first')(out1)  #32 x 40 x 640
    out2 = Convolution2D(32, (3, 3),
                         activation='elu',
                         padding='same',
                         data_format='channels_first',
                         kernel_initializer=fixed_init)(out1)  #32 x 40 x 640
    out2 = Dropout(rate=0.1)(out2)
    out2 = BatchNormalization(axis=1)(out2)
    out2 = MaxPooling2D((2, 2), padding='same',
                        data_format='channels_first')(out2)  #32 x 20 x 320
    out3 = Convolution2D(32, (3, 3),
                         activation='elu',
                         padding='same',
                         data_format='channels_first',
                         kernel_initializer=fixed_init)(out2)  #32 x 20 x 320
    out3 = Dropout(rate=0.1)(out3)
    out3 = BatchNormalization(axis=1)(out3)
    out3 = MaxPooling2D((2, 2), padding='same',
                        data_format='channels_first')(out3)  #32 x 10 x 160
    out4 = Convolution2D(32, (3, 3),
                         activation='elu',
                         padding='same',
                         data_format='channels_first',
                         kernel_initializer=fixed_init)(out3)  #32 x 10 x 160
    out4 = Dropout(rate=0.1)(out4)
    out4 = BatchNormalization(axis=1)(out4)
    out4 = MaxPooling2D((2, 2), padding='same',
                        data_format='channels_first')(out4)  #32 x 5 x 80

    out5 = Convolution2D(32, (3, 3),
                         activation='elu',
                         padding='same',
                         data_format='channels_first',
                         kernel_initializer=fixed_init)(out4)  #32 x 5 x 80
    out5 = Dropout(rate=0.1)(out5)
    out5 = BatchNormalization(axis=1)(out5)
    out5 = MaxPooling2D((5, 5), padding='same',
                        data_format='channels_first')(out5)  #32 x 1 x 16

    out6 = GlobalAveragePooling2D(data_format='channels_first')(
        out5)  #same as previous
    output = Dense(16, activation='softmax')(out6)

    #==== create model
    classifier = Model(input, output)
    layer1_extractor = Model(input, out1)
    layer2_extractor = Model(input, out2)
    layer3_extractor = Model(input, out3)
    layer4_extractor = Model(input, out4)
    layer5_extractor = Model(input, out5)
    #layer6_extractor = Model(input, out4i)

    #==== compile model
    classifier.compile(optimizer=selected_optimizer,
                       loss=selected_loss,
                       metrics=['acc'])
    return classifier, layer1_extractor, layer2_extractor, layer3_extractor, layer4_extractor, layer5_extractor
Beispiel #8
0
def TempInceptionV3(include_top=True,
                weights='imagenet',
                input_tensor=None,
                input_shape=None,
                pooling=None,
                classes=1000,
                depth=20,
                pre='custom_'):

    # Determine proper input shape
    if input_shape is None:
        input_shape = (None,None,depth)

    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_first':
        channel_axis = 1
    else:
        channel_axis = 3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        branch_pool = AveragePooling2D(
            (3, 3), strides=(1, 1), padding='same')(x)
        branch_pool = conv2d_bn(branch_pool, 192, 1, 1)
        x = layers.concatenate(
            [branch1x1, branch3x3, branch3x3dbl, branch_pool],
            axis=channel_axis,
            name=pre + 'mixed' + str(9 + i))
    if include_top:
        # Classification block
        x = GlobalAveragePooling2D(name=pre + 'avg_pool')(x)
        x = Dense(classes, activation='softmax', name=pre + 'predictions')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

    inputs = img_input
    # Create model.
    model = Model(inputs, x, name=pre + 'inception_v3')

    # load weights
    if weights == 'imagenet':
        model.load_weights('data/inceptionv3_temporal.h5')
        print ('Loaded weights')

    return model
                     filters_3x3=320,
                     filters_5x5_reduce=32,
                     filters_5x5=128,
                     filters_pool_proj=128,
                     name='inception_5a')

x = inception_module(x,
                     filters_1x1=384,
                     filters_3x3_reduce=192,
                     filters_3x3=384,
                     filters_5x5_reduce=48,
                     filters_5x5=128,
                     filters_pool_proj=128,
                     name='inception_5b')

x = GlobalAveragePooling2D(name='avg_pool_5_3x3/1')(x)

x = Dropout(0.4)(x)

x = Dense(38, activation='softmax', name='output')(x)

model = Model(input_layer, [x, x1, x2], name='inception_v1')

model.summary()

epochs = 25
initial_lrate = 0.005

def decay(epoch, steps=100):
    initial_lrate = 0.005
    drop = 0.96
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='same')
    x = conv2d_bn(x, 32, 3, padding='same')
    x = conv2d_bn(x, 64, 3)
    x = MaxPooling2D(3, strides=2, padding='same')(x)
    x = conv2d_bn(x, 80, 1, padding='same')
    x = conv2d_bn(x, 192, 3, padding='same')
    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)

    # 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)

    # 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')

    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:
            fname = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5'
            weights_path = get_file(
                fname,
                BASE_WEIGHT_URL + fname,
                cache_subdir='/wdata/backbones_weights',
                file_hash='e693bd0210a403b3192acc6073ad2e96')
        else:
            fname = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5'
            weights_path = get_file(
                fname,
                BASE_WEIGHT_URL + fname,
                cache_subdir='/wdata/backbones_weights',
                file_hash='d19885ff4a710c122648d3b5c3b684e4')
        model.load_weights(weights_path, skip_mismatch=True)
    elif weights is not None:
        model.load_weights(weights)

    return model
Beispiel #11
0
def Inception_v3c(input_tensor=None, input_shape=None, pre='custom_', weights='imagenet', depth=3, pooling='avg'):
    if input_shape is None:
        input_shape = (None,None,depth)

    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_first':
        channel_axis = 1
    else:
        channel_axis = 3
    
    x = Input(tensor=img_input)
     # mixed 8: 8 x 8 x 1280
    branch3x3 = conv2d_bn(x, 192, 1, 1)
    branch3x3 = conv2d_bn(branch3x3, 320, 3, 3,
                          strides=(2, 2), padding='valid')

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

    branch_pool = MaxPooling2D((3, 3), strides=(2, 2))(x)
    x = layers.concatenate(
        [branch3x3, branch7x7x3, branch_pool], axis=channel_axis, name=pre + 'mixed8')
    # mixed 9: 8 x 8 x 2048
    for i in range(2):
        branch1x1 = conv2d_bn(x, 320, 1, 1)

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

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

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

    if pooling == 'avg':
        x = GlobalAveragePooling2D()(x)
    elif pooling == 'max':
        x = GlobalMaxPooling2D()(x)

    inputs = img_input
    # Create model.
    model = Model(inputs, x, name=pre + 'inception_v3c')

    if weights == 'imagenet':
        model.load_weights('data/InceptionV3c.h5')
        print ('Loaded weights v3c')

    return model
Beispiel #12
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 3 fully-connected
            layers at the top of the network.
        weights: one of `None` (random initialization)
            or "imagenet" (pre-training on ImageNet).
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or `(3, 224, 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,
                                      weights=weights)

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

    x = 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
Beispiel #13
0
def ResNet50(include_top=True,
             weights='imagenet',
             input_tensor=None,
             input_shape=None,
             output_shape=None,
             dense_shape=None,
             pooling=None,
             classes=1000):
    """Instantiates the ResNet50 architecture.

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

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

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

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """

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

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

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

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

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

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

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

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

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

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input
    # Create model
    base_model = Model(inputs, x, name='resnet50')
    for layer in base_model.layers:
        layer.trainable = False
    #print (model.summary())
    #model = Model(inputs, x, name='resnet50')

    return base_model
def DenseNet(blocks,
             include_top=True,
             weights='imagenet',
             model_path=None,
             input_tensor=None,
             input_shape=(224, 224, 3),
             pooling=None,
             classes=1000):
    """Instantiates the DenseNet architecture.

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

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

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

    # Returns
        A Keras model instance.

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

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

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

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

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

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

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

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

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

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

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

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

    return model
Beispiel #15
0
def VGG16(include_top=True,
          weights=None,
          input_tensor=None,
          input_shape=None,
          pooling=None,
          nfcfilters=4096,
          nconvfilters=[64, 128, 512, 512, 512],
          classes=1,
          nblocks=5):

    seq_input = Input(shape=input_shape)

    if nblocks >= 1:
        # Block 1
        x = Conv2D(nconvfilters[0], (4, 3),
                   activation='relu',
                   padding='same',
                   name='block1_conv1')(seq_input)
        x = Conv2D(nconvfilters[0], (1, 3),
                   activation='relu',
                   padding='same',
                   name='block1_conv2')(x)
        x = MaxPooling2D((1, 2), name='block1_pool')(x)

    if nblocks >= 2:
        # Block 2
        x = Conv2D(nconvfilters[1], (1, 3),
                   activation='relu',
                   padding='same',
                   name='block2_conv1')(x)
        x = Conv2D(nconvfilters[1], (1, 3),
                   activation='relu',
                   padding='same',
                   name='block2_conv2')(x)
        x = MaxPooling2D((1, 2), name='block2_pool')(x)

    if nblocks >= 3:
        # Block 3
        x = Conv2D(nconvfilters[2], (1, 3),
                   activation='relu',
                   padding='same',
                   name='block3_conv1')(x)
        x = Conv2D(nconvfilters[2], (1, 3),
                   activation='relu',
                   padding='same',
                   name='block3_conv2')(x)
        x = Conv2D(nconvfilters[2], (1, 3),
                   activation='relu',
                   padding='same',
                   name='block3_conv3')(x)
        x = MaxPooling2D((1, 2), name='block3_pool')(x)

    if nblocks >= 4:
        # Block 4
        x = Conv2D(nconvfilters[3], (1, 3),
                   activation='relu',
                   padding='same',
                   name='block4_conv1')(x)
        x = Conv2D(nconvfilters[3], (1, 3),
                   activation='relu',
                   padding='same',
                   name='block4_conv2')(x)
        x = Conv2D(nconvfilters[3], (1, 3),
                   activation='relu',
                   padding='same',
                   name='block4_conv3')(x)
        x = MaxPooling2D((1, 2), name='block4_pool')(x)

    if nblocks >= 5:
        # Block 5
        x = Conv2D(nconvfilters[4], (1, 3),
                   activation='relu',
                   padding='same',
                   name='block5_conv1')(x)
        x = Conv2D(nconvfilters[4], (1, 3),
                   activation='relu',
                   padding='same',
                   name='block5_conv2')(x)
        x = Conv2D(nconvfilters[4], (1, 3),
                   activation='relu',
                   padding='same',
                   name='block5_conv3')(x)
        x = MaxPooling2D((1, 2), name='block5_pool')(x)

    if include_top:
        # Classification block
        x = Flatten(name='flatten')(x)
        x = Dense(nfcfilters, activation='relu', name='fc1')(x)
        x = Dense(nfcfilters, activation='relu', name='fc2')(x)
        if classes == 1:
            x = Dense(classes, activation='sigmoid', name='predictions')(x)
        else:
            x = Dense(classes, activation='softmax', name='predictions')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

    # inputs
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = seq_input

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

    if weights:
        raise NotImplementedError

    return model
Beispiel #16
0
    def build(width,
              height,
              depth,
              classes,
              stages,
              filters,
              reg=0.0001,
              bnEps=2e-5,
              bnMom=0.9):
        inputShape = (height, width, depth)
        chanDim = -1

        inputs = Input(shape=inputShape)
        x = BatchNormalization(axis=chanDim,
                               epsilon=bnEps,
                               momentum=bnMom,
                               beta_initializer="zeros",
                               gamma_initializer="ones")(inputs)
        x = Activation("relu")(x)
        x = SeparableConv2D(64, (3, 3),
                            use_bias=False,
                            padding="same",
                            depthwise_regularizer=l2(reg),
                            depthwise_initializer='glorot_uniform')(x)
        x = SeparableConv2D(128, (3, 3),
                            use_bias=False,
                            padding="same",
                            depthwise_regularizer=l2(reg),
                            depthwise_initializer='glorot_uniform')(x)
        x = SeparableConv2D(256, (3, 3),
                            use_bias=False,
                            padding="same",
                            depthwise_regularizer=l2(reg),
                            depthwise_initializer='glorot_uniform')(x)
        x = BatchNormalization(axis=chanDim,
                               epsilon=bnEps,
                               momentum=bnMom,
                               beta_initializer="zeros",
                               gamma_initializer="ones")(x)
        x = Activation("relu")(x)
        x = ZeroPadding2D((1, 1))(x)
        x = MaxPooling2D((3, 3), strides=(2, 2))(x)

        for i in range(0, len(stages)):
            stride = (1, 1) if i == 0 else (2, 2)
            x = ResNet.residual_module(x,
                                       filters[i],
                                       stride,
                                       chanDim,
                                       red=True,
                                       bnEps=bnEps,
                                       bnMom=bnMom)

            for j in range(0, stages[i] - 1):
                x = ResNet.residual_module(x,
                                           filters[i], (1, 1),
                                           chanDim,
                                           bnEps=bnEps,
                                           bnMom=bnMom)

        x = BatchNormalization(axis=chanDim, epsilon=bnEps, momentum=bnMom)(x)
        x = Activation("relu")(x)
        x = Conv2D(200, (1, 1), kernel_regularizer=l2(reg))(x)
        x = GlobalAveragePooling2D('channels_last')(x)
        x = Activation("softmax")(x)

        model = Model(inputs, x, name="resnet")

        return model
 def model1(self):
     base_model = InceptionV3(weights='imagenet', include_top=False)
     x = base_model.output
     x = GlobalAveragePooling2D()(x)
     self.model = Model(inputs=base_model.input, outputs=x)
     return self.model
def Deeplabv3pa(weights='pascal_voc',
                input_tensor=None,
                input_shape=(512, 512, 3),
                classes=21,
                OS=16):
    if not (weights in {'pascal_voc', None}):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `pascal_voc` '
                         '(pre-trained on PASCAL VOC)')

    if K.backend() != 'tensorflow':
        raise RuntimeError('The Deeplabv3+ model is only available with '
                           'the TensorFlow backend.')

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

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

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

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

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

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

    x = xception_block(x, [728, 1024, 1024],
                       'exit_flow_block1',
                       skip_connection_type='conv',
                       stride=1,
                       rate=exit_block_rates[0],
                       depth_activation=False)
    x = xception_block(x, [1536, 1536, 2048],
                       'exit_flow_block2',
                       skip_connection_type='none',
                       stride=1,
                       rate=exit_block_rates[1],
                       depth_activation=True)
    # end of feature extractor

    # branching for Atrous Spatial Pyramid Pooling
    # simple 1x1
    b0 = Conv2D(256, (1, 1), padding='same', use_bias=False, name='aspp0')(x)
    b0 = BatchNormalization(name='aspp0_BN', epsilon=1e-5)(b0)
    b0 = Activation('relu', name='aspp0_activation')(b0)

    # rate = 6 (12)
    b1 = SepConv_BN(x,
                    256,
                    'aspp1',
                    rate=atrous_rates[0],
                    depth_activation=True,
                    epsilon=1e-5)
    # rate = 12 (24)
    b2 = SepConv_BN(x,
                    256,
                    'aspp2',
                    rate=atrous_rates[1],
                    depth_activation=True,
                    epsilon=1e-5)
    # rate = 18 (36)
    b3 = SepConv_BN(x,
                    256,
                    'aspp3',
                    rate=atrous_rates[2],
                    depth_activation=True,
                    epsilon=1e-5)

    # Image Feature branch
    out_shape = int(np.ceil(input_shape[0] / OS))
    b4 = AveragePooling2D(pool_size=(out_shape, out_shape))(x)
    b4 = Conv2D(256, (1, 1),
                padding='same',
                use_bias=False,
                name='image_pooling')(b4)
    b4 = BatchNormalization(name='image_pooling_BN', epsilon=1e-5)(b4)
    b4 = Activation('relu')(b4)
    b4 = BilinearUpsampling((out_shape, out_shape), l_name='up1')(b4)
    b0_1 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b0)
    b0_1 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b0_1)
    b0_1 = Dropout(0.5)(b0_1)
    b0_c = concatenate([b0, b0_1], axis=3)
    b0_2 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b0_c)
    b0 = Conv2D(256, (3, 3),
                activation='relu',
                padding='same',
                kernel_initializer='he_normal')(b0_2)

    # b1
    b1_1 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b1)
    b1_1 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b1_1)
    b1_1 = Dropout(0.5)(b1_1)
    b1_c = concatenate([b1, b1_1], axis=3)
    b1_2 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b1_c)
    b1 = Conv2D(256, (3, 3),
                activation='relu',
                padding='same',
                kernel_initializer='he_normal')(b1_2)

    # b2
    b2_1 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b2)
    b2_1 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b2_1)
    b2_1 = Dropout(0.5)(b2_1)
    b2_c = concatenate([b2, b2_1], axis=3)
    b2_2 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b2_c)
    b2 = Conv2D(256, (3, 3),
                activation='relu',
                padding='same',
                kernel_initializer='he_normal')(b2_2)

    # b3
    b3_1 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b3)
    b3_1 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b3_1)
    b3_1 = Dropout(0.5)(b3_1)
    b3_c = concatenate([b3, b3_1], axis=3)
    b3_2 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b3_c)
    b3 = Conv2D(256, (3, 3),
                activation='relu',
                padding='same',
                kernel_initializer='he_normal')(b3_2)

    # b4
    b4_1 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b4)
    b4_1 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b4_1)
    b4_1 = Dropout(0.5)(b4_1)
    b4_c = concatenate([b4, b4_1], axis=3)
    b4_2 = Conv2D(256, (3, 3),
                  activation='relu',
                  padding='same',
                  kernel_initializer='he_normal')(b4_c)
    b4 = Conv2D(256, (3, 3),
                activation='relu',
                padding='same',
                kernel_initializer='he_normal')(b4_2)
    Dense0 = Dense(256,
                   activation='relu',
                   kernel_initializer='he_normal',
                   use_bias=False)
    Dense1 = Dense(32,
                   activation='relu',
                   kernel_initializer='he_normal',
                   use_bias=False)
    Dense2 = Dense(256,
                   activation='sigmoid',
                   kernel_initializer='he_normal',
                   use_bias=False)

    b0_1 = Reshape((1, 1, 256))(GlobalAveragePooling2D()(b0))
    b0_1 = Dense2(Dense1(Dense0(b0_1)))

    b1_1 = Reshape((1, 1, 256))(GlobalAveragePooling2D()(b1))
    b1_1 = Dense2(Dense1(Dense0(b1_1)))

    b2_1 = Reshape((1, 1, 256))(GlobalAveragePooling2D()(b2))
    b2_1 = Dense2(Dense1(Dense0(b2_1)))

    b3_1 = Reshape((1, 1, 256))(GlobalAveragePooling2D()(b3))
    b3_1 = Dense2(Dense1(Dense0(b3_1)))

    b4_1 = Reshape((1, 1, 256))(GlobalAveragePooling2D()(b4))
    b4_1 = Dense2(Dense1(Dense0(b4_1)))

    x0 = multiply([b0, b0_1])
    x1 = multiply([b1, b1_1])
    x2 = multiply([b2, b2_1])
    x3 = multiply([b3, b3_1])
    x4 = multiply([b4, b4_1])

    x0 = Reshape((16, 16, 1, 256))(x0)
    x1 = Reshape((16, 16, 1, 256))(x1)
    x2 = Reshape((16, 16, 1, 256))(x2)
    x3 = Reshape((16, 16, 1, 256))(x3)
    x4 = Reshape((16, 16, 1, 256))(x4)
    x = Concatenate(axis=3)([x0, x1, x2, x3, x4])
    x = Conv3D(256, (1, 1, 5),
               activation='relu',
               use_bias=False,
               kernel_initializer='he_normal')(x)
    x = Reshape((16, 16, 256))(x)
    x = Conv2D(256, (3, 3),
               activation='relu',
               padding='same',
               kernel_initializer='he_normal')(x)
    x = Conv2D(256, (3, 3),
               activation='relu',
               padding='same',
               kernel_initializer='he_normal')(x)
    x = BatchNormalization(name='concat_projection_BN', epsilon=1e-5)(x)
    x = Activation('relu')(x)
    x = Dropout(0.1)(x)
    # DeepLab v.3+ decoder

    # Feature projection

    x = BilinearUpsampling(output_size=(int(np.ceil(input_shape[0] / 4)),
                                        int(np.ceil(input_shape[1] / 4))),
                           l_name='up2')(x)
    dec_skip1 = Conv2D(48, (1, 1),
                       padding='same',
                       use_bias=False,
                       name='feature_projection0')(skip1)
    dec_skip1 = BatchNormalization(name='feature_projection0_BN',
                                   epsilon=1e-5)(dec_skip1)
    dec_skip1 = Activation('relu')(dec_skip1)
    x = Concatenate()([x, dec_skip1])
    x = SepConv_BN(x,
                   256,
                   'decoder_conv0',
                   depth_activation=True,
                   epsilon=1e-5)
    x = SepConv_BN(x,
                   256,
                   'decoder_conv1',
                   depth_activation=True,
                   epsilon=1e-5)

    conv8 = Conv2D(64,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(x)
    conv8 = Conv2D(64,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(conv8)
    conv9 = Conv2D(2,
                   3,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal')(conv8)
    x = Conv2D(1, 1, activation='sigmoid')(conv9)
    x1 = BilinearUpsampling(output_size=(input_shape[0], input_shape[1]),
                            l_name='x1')(x)

    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input

    model = Model(inputs, x1, name='deeplabv3+')

    # load weights

    if weights == 'pascal_voc':
        weights_path = get_file(
            'deeplabv3_weights_tf_dim_ordering_tf_kernels.h5',
            TF_WEIGHTS_PATH,
            cache_subdir='models')
        model.load_weights(weights_path, by_name=True)

    model.compile(optimizer=Adam(lr=1e-4),
                  loss='binary_crossentropy',
                  metrics=['accuracy'])

    return model
def MobileNet(input_shape=None,
              alpha=1.0,
              depth_multiplier=1,
              dropout=1e-3,
              include_top=True,
              weights=None,
              pooling=None,
              classes=10):
    """Instantiates the MobileNet architecture.

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

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

    # Arguments
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` data format)
            or (3, 224, 224) (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 32.
            E.g. `(200, 200, 3)` would be one valid value.
        alpha: controls the width of the network.
            - If `alpha` < 1.0, proportionally decreases the number
                of filters in each layer.
            - If `alpha` > 1.0, proportionally increases the number
                of filters in each layer.
            - If `alpha` = 1, default number of filters from the paper
                 are used at each layer.
        depth_multiplier: depth multiplier for depthwise convolution
            (also called the resolution multiplier)
        dropout: dropout rate
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: `None` (random initialization) or
            `imagenet` (ImageNet weights)
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model
                will be the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a
                2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
        RuntimeError: If attempting to run this model with a
            backend that does not support separable convolutions.
    """

    if K.image_data_format() == 'channels_last':
        row_axis = 0
    else:
        row_axis = 1
    rows = input_shape[row_axis]

    input_tensor = Input(shape=input_shape)
    x = _conv_block(input_tensor, 32, alpha, strides=(2, 2))
    x = _depthwise_conv_block(x, 64, alpha, depth_multiplier, block_id=1)

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

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

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

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

    if include_top:
        shape = (1, 1, int(1024 * alpha))
        x = GlobalAveragePooling2D()(x)
        x = Reshape(shape, name='reshape_1')(x)
        x = Dropout(dropout, name='dropout')(x)
        x = Conv2D(classes, (1, 1), padding='same', name='conv_preds')(x)
        x = Activation('softmax', name='act_softmax')(x)
        x = Reshape((classes, ), name='reshape_2')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

    # Create model.
    model = Model(input_tensor, x, name='mobilenet_%0.2f_%s' % (alpha, rows))

    return model
Beispiel #20
0
    elif model_choice == 'conv9':

        model_9conv(train_x, train_y, test_x, test_y)

    else:
        # finetuned model - test set accuracy on SVHN: ~94%
        # based on the sample finetuning code provided by keras at
        # https://keras.io/applications/
        base_model = VGG16(weights='imagenet',
                           include_top=False,
                           input_tensor=Input(shape=(128, 128, 3)))

        print(base_model.summary())

        new_top = base_model.output
        new_top = GlobalAveragePooling2D()(new_top)
        new_top = Dense(512, activation='relu')(new_top)
        new_top = Dropout(0.5)(new_top)
        new_top = Dense(10, activation='softmax')(new_top)

        model = Model(inputs=base_model.input, outputs=new_top)

        # freeze vgg16 layers to train new output layers
        for layer in base_model.layers:
            layer.trainable = False

        model.compile(optimizer=SGD(learning_rate=1e-4, momentum=0.9),
                      loss='categorical_crossentropy',
                      metrics=['accuracy'])

        print('Training new output layers...')
Beispiel #21
0
def xnet(
        classes=14,
        input_shape=(224, 224, 1),
):
    X_input = Input(input_shape)
    layer = 0
    layer = layer + 1
    X = ZeroPadding2D((3, 3))(X_input)
    X = BatchNormalization(axis=3, name=bn + str(layer))(X)
    X = Activation('relu')(X)
    X = Conv2D(2 * k, (7, 7),
               strides=(2, 2),
               name=conv + str(layer),
               kernel_initializer=glorot_uniform(seed=0))(X)
    X = ZeroPadding2D((1, 1))(X)
    X = MaxPooling2D((3, 3), strides=(2, 2))(X)

    #Dense Block = 1
    layer = layer + 1
    X = bottleneck_composite(X, layer)
    l = []
    l.append(X)
    for i in range(0, 5):
        layer = layer + 2
        X = bottleneck_composite(l, layer)
        l.append(X)

    # Transition layer = 1
    layer = layer + 2
    X = BatchNormalization(axis=3, name=bn + str(layer))(X)
    X = Activation('relu')(X)
    X = Conv2D(k, (1, 1),
               strides=(1, 1),
               padding='same',
               name=conv + str(layer),
               kernel_initializer=glorot_uniform(seed=0))(X)
    X = ZeroPadding2D((1, 1))(X)
    X = AveragePooling2D((2, 2), strides=(2, 2))(X)

    #Dense Block = 2
    layer = layer + 1
    X = bottleneck_composite(X, layer)
    l = []
    l.append(X)
    for i in range(0, 11):
        layer = layer + 2
        X = bottleneck_composite(l, layer)
        l.append(X)

    # Transition layer = 2
    layer = layer + 2
    X = BatchNormalization(axis=3, name=bn + str(layer))(X)
    X = Activation('relu')(X)
    X = Conv2D(k, (1, 1),
               strides=(1, 1),
               padding='same',
               name=conv + str(layer),
               kernel_initializer=glorot_uniform(seed=0))(X)
    X = ZeroPadding2D((1, 1))(X)
    X = AveragePooling2D((2, 2), strides=(2, 2))(X)

    #Dense Block = 3
    layer = layer + 1
    X = bottleneck_composite(X, layer)
    l = []
    l.append(X)
    for i in range(0, 31):
        layer = layer + 2
        X = bottleneck_composite(l, layer)
        l.append(X)

    # Transition layer = 3
    layer = layer + 2
    X = BatchNormalization(axis=3, name=bn + str(layer))(X)
    X = Activation('relu')(X)
    X = Conv2D(k, (1, 1),
               strides=(1, 1),
               padding='same',
               name=conv + str(layer),
               kernel_initializer=glorot_uniform(seed=0))(X)
    X = ZeroPadding2D((1, 1))(X)
    X = AveragePooling2D((2, 2), strides=(2, 2))(X)

    #Dense Block = 4
    layer = layer + 1
    X = bottleneck_composite(X, layer)
    l = []
    l.append(X)
    for i in range(0, 31):
        layer = layer + 2
        X = bottleneck_composite(l, layer)
        l.append(X)
    layer = layer + 2
    X = GlobalAveragePooling2D()(X)
    # fully connected layer
    #X = Flatten()(X)
    X = Dense(classes,
              activation='softmax',
              name=fc + str(layer),
              kernel_initializer=glorot_uniform(seed=0))(X)

    model = Model(inputs=X_input, outputs=X, name="DenseNet121")

    return model
Beispiel #22
0
def VGG16(include_top=True,
          weights='imagenet',
          input_tensor=None,
          input_shape=None,
          pooling=None,
          classes=1000):
    """Instantiates the VGG16 architecture.

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

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

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

    # Returns
        A Keras model instance.

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

    if weights == 'imagenet' and include_top and classes != 1000:
        raise ValueError('If using `weights` as imagenet with `include_top`'
                         ' as true, `classes` should be 1000')
    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=224,
                                      min_size=48,
                                      data_format=K.image_data_format(),
                                      require_flatten=False)

    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
    # Block 1
    x = Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               name='block1_conv1')(img_input)
    x = Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               name='block1_conv2')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)

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

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

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

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

    if include_top:
        # Classification block
        x = Flatten(name='flatten')(x)
        x = Dense(4096, activation='relu', name='fc1')(x)
        x = Dense(4096, activation='relu', name='fc2')(x)
        x = Dense(classes, activation='softmax', name='predictions')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

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

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

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

            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.layers.pop()  # Get rid of the classification layer
    model.outputs = [model.layers[-1].output]
    model.layers[-1].outbound_nodes = []
    return model
Beispiel #23
0
model.add(Activation('relu'))
model.add(AveragePooling2D(pool_size=(2, 2), strides=1, padding='same'))
model.add(Dropout(0.5))

model.add(Conv2D(filters=128, kernel_size=(3, 3), padding='same'))
model.add(BatchNormalization())
model.add(Conv2D(filters=128, kernel_size=(3, 3), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(AveragePooling2D(pool_size=(2, 2), padding='same'))
model.add(Dropout(0.5))

model.add(Conv2D(filters=256, kernel_size=(3, 3), padding='same'))
model.add(BatchNormalization())
model.add(Conv2D(filters=nb_classes, kernel_size=(3, 3), padding='same'))
model.add(GlobalAveragePooling2D())
model.add(Activation('softmax', name='predictions'))

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

#{0:'neutral', 1:'anger', 2:'contempt', 3:'disgust', 4:'fear', 5:'happy', 6:'sadness', 7:'surprise'}
""" 
class_weights = {0: 0.000761615,
                1: 0.001290323,
                2: 0.005376344,
                3: 0.001451379,
                4: 0.002398082,
                5: 0.000970874,
                6: 0.002380952,
                               validation_data=(X_test, y_test))
print('Training time: %s' % (t - time.time()))
(loss, accuracy) = custom_resnet_model.evaluate(X_test,
                                                y_test,
                                                batch_size=10,
                                                verbose=1)
print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss, accuracy * 100))

###########################################################################################################################
# Fine tune the resnet 50
#image_input = Input(shape=(224, 224, 3))
model = ResNet50(weights='imagenet', include_top=False)
model.summary()
last_layer = model.output
# add a global spatial average pooling layer
x = GlobalAveragePooling2D()(last_layer)
# add fully-connected & dropout layers
x = Dense(512, activation='relu', name='fc-1')(x)
x = Dropout(0.5)(x)
x = Dense(256, activation='relu', name='fc-2')(x)
x = Dropout(0.5)(x)
# a softmax layer for 4 classes
out = Dense(num_classes, activation='softmax', name='output_layer')(x)
# this is the model we will train
custom_resnet_model2 = Model(inputs=model.input, outputs=out)
custom_resnet_model2.summary()

for layer in custom_resnet_model2.layers[:-6]:
    layer.trainable = False

custom_resnet_model2.layers[-1].trainable
Beispiel #25
0
def _main(args):
    config_path = os.path.expanduser(args.config_path)
    weights_path = os.path.expanduser(args.weights_path)
    assert config_path.endswith('.cfg'), '{} is not a .cfg file'.format(
        config_path)
    assert weights_path.endswith(
        '.weights'), '{} is not a .weights file'.format(weights_path)

    output_path = os.path.expanduser(args.output_path)
    assert output_path.endswith(
        '.h5'), 'output path {} is not a .h5 file'.format(output_path)
    output_root = os.path.splitext(output_path)[0]

    # Load weights and config.
    print('Loading weights.')
    weights_file = open(weights_path, 'rb')
    weights_header = np.ndarray(shape=(4, ),
                                dtype='int32',
                                buffer=weights_file.read(16))
    print('Weights Header: ', weights_header)
    # TODO: Check transpose flag when implementing fully connected layers.
    # transpose = (weight_header[0] > 1000) or (weight_header[1] > 1000)

    print('Parsing Darknet config.')
    unique_config_file = unique_config_sections(config_path)
    cfg_parser = configparser.ConfigParser()
    cfg_parser.read_file(unique_config_file)

    print('Creating Keras model.')
    if args.fully_convolutional:
        image_height, image_width = None, None
    else:
        image_height = int(cfg_parser['net_0']['height'])
        image_width = int(cfg_parser['net_0']['width'])
    prev_layer = Input(shape=(image_height, image_width, 3))
    all_layers = [prev_layer]

    weight_decay = float(cfg_parser['net_0']['decay']
                         ) if 'net_0' in cfg_parser.sections() else 5e-4
    count = 0
    for section in cfg_parser.sections():
        print('Parsing section {}'.format(section))
        if section.startswith('convolutional'):
            filters = int(cfg_parser[section]['filters'])
            size = int(cfg_parser[section]['size'])
            stride = int(cfg_parser[section]['stride'])
            pad = int(cfg_parser[section]['pad'])
            activation = cfg_parser[section]['activation']
            batch_normalize = 'batch_normalize' in cfg_parser[section]

            # padding='same' is equivalent to Darknet pad=1
            padding = 'same' if pad == 1 else 'valid'

            # Setting weights.
            # Darknet serializes convolutional weights as:
            # [bias/beta, [gamma, mean, variance], conv_weights]
            prev_layer_shape = K.int_shape(prev_layer)

            # TODO: This assumes channel last dim_ordering.
            weights_shape = (size, size, prev_layer_shape[-1], filters)
            darknet_w_shape = (filters, weights_shape[2], size, size)
            weights_size = np.product(weights_shape)

            print('conv2d', 'bn' if batch_normalize else '  ', activation,
                  weights_shape)

            conv_bias = np.ndarray(shape=(filters, ),
                                   dtype='float32',
                                   buffer=weights_file.read(filters * 4))
            count += filters

            if batch_normalize:
                bn_weights = np.ndarray(shape=(3, filters),
                                        dtype='float32',
                                        buffer=weights_file.read(filters * 12))
                count += 3 * filters

                # TODO: Keras BatchNormalization mistakenly refers to var
                # as std.
                bn_weight_list = [
                    bn_weights[0],  # scale gamma
                    conv_bias,  # shift beta
                    bn_weights[1],  # running mean
                    bn_weights[2]  # running var
                ]

            conv_weights = np.ndarray(shape=darknet_w_shape,
                                      dtype='float32',
                                      buffer=weights_file.read(weights_size *
                                                               4))
            count += weights_size

            # DarkNet conv_weights are serialized Caffe-style:
            # (out_dim, in_dim, height, width)
            # We would like to set these to Tensorflow order:
            # (height, width, in_dim, out_dim)
            # TODO: Add check for Theano dim ordering.
            conv_weights = np.transpose(conv_weights, [2, 3, 1, 0])
            conv_weights = [conv_weights] if batch_normalize else [
                conv_weights, conv_bias
            ]

            # Handle activation.
            act_fn = None
            if activation == 'leaky':
                pass  # Add advanced activation later.
            elif activation != 'linear':
                raise ValueError(
                    'Unknown activation function `{}` in section {}'.format(
                        activation, section))

            # Create Conv2D layer
            conv_layer = (Conv2D(filters, (size, size),
                                 strides=(stride, stride),
                                 kernel_regularizer=l2(weight_decay),
                                 use_bias=not batch_normalize,
                                 weights=conv_weights,
                                 activation=act_fn,
                                 padding=padding))(prev_layer)

            if batch_normalize:
                conv_layer = (BatchNormalization(
                    weights=bn_weight_list))(conv_layer)
            prev_layer = conv_layer

            if activation == 'linear':
                all_layers.append(prev_layer)
            elif activation == 'leaky':
                act_layer = LeakyReLU(alpha=0.1)(prev_layer)
                prev_layer = act_layer
                all_layers.append(act_layer)

        elif section.startswith('maxpool'):
            size = int(cfg_parser[section]['size'])
            stride = int(cfg_parser[section]['stride'])
            all_layers.append(
                MaxPooling2D(padding='same',
                             pool_size=(size, size),
                             strides=(stride, stride))(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('avgpool'):
            if cfg_parser.items(section) != []:
                raise ValueError('{} with params unsupported.'.format(section))
            all_layers.append(GlobalAveragePooling2D()(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('route'):
            ids = [int(i) for i in cfg_parser[section]['layers'].split(',')]
            layers = [all_layers[i] for i in ids]
            if len(layers) > 1:
                print('Concatenating route layers:', layers)
                concatenate_layer = concatenate(layers)
                all_layers.append(concatenate_layer)
                prev_layer = concatenate_layer
            else:
                skip_layer = layers[0]  # only one layer to route
                all_layers.append(skip_layer)
                prev_layer = skip_layer

        elif section.startswith('reorg'):
            block_size = int(cfg_parser[section]['stride'])
            assert block_size == 2, 'Only reorg with stride 2 supported.'
            all_layers.append(
                Lambda(space_to_depth_x2,
                       output_shape=space_to_depth_x2_output_shape,
                       name='space_to_depth_x2')(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('region'):
            with open('{}_anchors.txt'.format(output_root), 'w') as f:
                print(cfg_parser[section]['anchors'], file=f)

        elif (section.startswith('net') or section.startswith('cost')
              or section.startswith('softmax')):
            pass  # Configs not currently handled during model definition.

        else:
            raise ValueError(
                'Unsupported section header type: {}'.format(section))

    # Create and save model.
    model = Model(inputs=all_layers[0], outputs=all_layers[-1])
    print(model.summary())
    model.save('{}'.format(output_path))
    print('Saved Keras model to {}'.format(output_path))
    # Check to see if all weights have been read.
    remaining_weights = len(weights_file.read()) / 4
    weights_file.close()
    print('Read {} of {} from Darknet weights.'.format(
        count, count + remaining_weights))
    if remaining_weights > 0:
        print('Warning: {} unused weights'.format(remaining_weights))

    if args.plot_model:
        plot(model, to_file='{}.png'.format(output_root), show_shapes=True)
        print('Saved model plot to {}.png'.format(output_root))
Beispiel #26
0
    '/home/liuk/dl/pytorch/data/newdata/val',
    target_size=(256, 256),
    #target_size=(299,299),
    batch_size=16,
    class_mode='categorical',
    shuffle=True)

# 构建基础模型
#base_model = InceptionV3(weights='imagenet',include_top=True)
base_model = InceptionV3(
    weights='imagenet', include_top=False
)  #include_top,如果是True,输出是1000个节点的全连接层。如果是False,会去掉顶层,输出一个8 * 8 * 2048的张量

# 增加新的输出层
x = base_model.output
x = GlobalAveragePooling2D()(
    x)  # GlobalAveragePooling2D 将 MxNxC 的张量转换成1xC 张量,C是通道数
x = Dense(1024, activation='relu')(x)
predictions = Dense(4, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
# plot_model(model,'tlmodel.png')

plot_model(model, 'output/tlmodel.png')
'''
这里的base_model和model里面的iv3都指向同一个地址
'''


def setup_to_transfer_learning(model, base_model):  #base_model
    for layer in base_model.layers:
        layer.trainable = False
    model.compile(optimizer=RMSprop(1e-3),
Beispiel #27
0
def SqueezeNet(nb_classes, inputs=(224, 224, 3)):
    """ Keras Implementation of SqueezeNet(arXiv 1602.07360)

    @param nb_classes: total number of final categories

    Arguments:
    inputs -- shape of the input images (channel, cols, rows)

    """

    input_img = Input(shape=inputs)
    conv1 = Convolution2D(96, (7, 7),
                          activation='relu',
                          kernel_initializer='glorot_uniform',
                          strides=(2, 2),
                          padding='same',
                          name='conv1')(input_img)
    maxpool1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2),
                            name='maxpool1')(conv1)
    fire2_squeeze = Convolution2D(16, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire2_squeeze')(maxpool1)
    fire2_expand1 = Convolution2D(64, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire2_expand1')(fire2_squeeze)
    fire2_expand2 = Convolution2D(64, (3, 3),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire2_expand2')(fire2_squeeze)
    merge2 = Concatenate(axis=1)([fire2_expand1, fire2_expand2])

    fire3_squeeze = Convolution2D(16, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire3_squeeze')(merge2)
    fire3_expand1 = Convolution2D(64, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire3_expand1')(fire3_squeeze)
    fire3_expand2 = Convolution2D(64, (3, 3),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire3_expand2')(fire3_squeeze)
    merge3 = Concatenate(axis=1)([fire3_expand1, fire3_expand2])

    fire4_squeeze = Convolution2D(32, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire4_squeeze')(merge3)
    fire4_expand1 = Convolution2D(128, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire4_expand1')(fire4_squeeze)
    fire4_expand2 = Convolution2D(128, (3, 3),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire4_expand2')(fire4_squeeze)
    merge4 = Concatenate(axis=1)([fire4_expand1, fire4_expand2])
    maxpool4 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2),
                            name='maxpool4')(merge4)

    fire5_squeeze = Convolution2D(32, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire5_squeeze')(maxpool4)
    fire5_expand1 = Convolution2D(128, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire5_expand1')(fire5_squeeze)
    fire5_expand2 = Convolution2D(128, (3, 3),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire5_expand2')(fire5_squeeze)
    merge5 = Concatenate(axis=1)([fire5_expand1, fire5_expand2])

    fire6_squeeze = Convolution2D(48, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire6_squeeze')(merge5)
    fire6_expand1 = Convolution2D(192, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire6_expand1')(fire6_squeeze)
    fire6_expand2 = Convolution2D(192, (3, 3),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire6_expand2')(fire6_squeeze)
    merge6 = Concatenate(axis=1)([fire6_expand1, fire6_expand2])

    fire7_squeeze = Convolution2D(48, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire7_squeeze')(merge6)
    fire7_expand1 = Convolution2D(192, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire7_expand1')(fire7_squeeze)
    fire7_expand2 = Convolution2D(192, (3, 3),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire7_expand2')(fire7_squeeze)
    merge7 = Concatenate(axis=1)([fire7_expand1, fire7_expand2])

    fire8_squeeze = Convolution2D(64, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire8_squeeze')(merge7)
    fire8_expand1 = Convolution2D(256, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire8_expand1')(fire8_squeeze)
    fire8_expand2 = Convolution2D(256, (3, 3),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire8_expand2')(fire8_squeeze)
    merge8 = Concatenate(axis=1)([fire8_expand1, fire8_expand2])

    maxpool8 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2),
                            name='maxpool8')(merge8)
    fire9_squeeze = Convolution2D(64, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire9_squeeze')(maxpool8)
    fire9_expand1 = Convolution2D(256, (1, 1),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire9_expand1')(fire9_squeeze)
    fire9_expand2 = Convolution2D(256, (3, 3),
                                  activation='relu',
                                  kernel_initializer='glorot_uniform',
                                  padding='same',
                                  name='fire9_expand2')(fire9_squeeze)
    merge9 = Concatenate(axis=1)([fire9_expand1, fire9_expand2])

    fire9_dropout = Dropout(0.5, name='fire9_dropout')(merge9)
    conv10 = Convolution2D(nb_classes, (1, 1),
                           kernel_initializer='glorot_uniform',
                           padding='valid',
                           name='conv10')(fire9_dropout)

    global_avgpool10 = GlobalAveragePooling2D()(conv10)
    softmax = Activation("softmax", name='softmax')(global_avgpool10)

    return Model(inputs=input_img, outputs=softmax)
Beispiel #28
0
def get_model(embed, num_conti):
    def block_wrap(x, filters):
        x = Conv2D(filters, kernel_size=3, padding='same')(x)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
        return x

    def root_mean_squared_error(y_true, y_pred):
        return K.sqrt(K.mean(K.square(y_true - y_pred)))

    region = Input(shape=[1], name="region")
    city = Input(shape=[1], name="city")
    pcn = Input(shape=[1], name="parent_category_name")
    cn = Input(shape=[1], name="category_name")
    para1 = Input(shape=[1], name="param_1")
    para2 = Input(shape=[1], name="param_2")
    para3 = Input(shape=[1], name="param_3")
    # act = Input(shape=[1], name="activation_date")
    ut = Input(shape=[1], name="user_type")
    img_top = Input(shape=[1], name="image_top_1")

    title = Input(shape=[title_len], name="title")
    desc = Input(shape=[desc_len], name="desc")
    img = Input(shape=[img_size, img_size, 3], name='image')
    conti = Input(shape=[num_conti], name='conti')
    """
    region                       28
    city                       1752
    parent_category_name          9
    category_name                47
    param_1                     372
    param_2                     278
    param_3                    1277
    title                   1022203
    description             1793973
    activation_date              30
    user_type                     3
    image                   1856666
    image_top_1                3064
    param_combined             2402
    """

    emb_region = Embedding(28, 8)(region)
    emb_city = Embedding(1752, 16)(city)
    emb_pcn = Embedding(9, 3)(pcn)
    emb_cn = Embedding(47, 8)(cn)
    emb_para1 = Embedding(372, 16)(para1)
    emb_para2 = Embedding(278, 16)(para2)
    emb_para3 = Embedding(1277, 16)(para3)
    # emb_act = Embedding(30,8)(act)
    emb_img_top = Embedding(3064, 32)(img_top)
    emb_ut = Embedding(3, 3, weights=[np.eye(3, 3)], trainable=False)(ut)

    num_word = len(embed['title'])
    emb_title = Embedding(num_word,
                          300,
                          weights=[embed['title']],
                          trainable=False)(title)
    num_word = len(embed['desc'])
    emb_desc = Embedding(num_word,
                         300,
                         weights=[embed['desc']],
                         trainable=False)(desc)

    conv = block_wrap(img, 32)
    conv = MaxPool2D(padding='same')(conv)
    conv = block_wrap(conv, 64)
    conv = MaxPool2D(padding='same')(conv)
    conv = block_wrap(conv, 128)
    conv = MaxPool2D(padding='same')(conv)
    conv = GlobalAveragePooling2D()(conv)

    title_gru = Bidirectional(CuDNNGRU(64, return_sequences=True),
                              merge_mode='sum')(emb_title)
    title_gru = Bidirectional(CuDNNGRU(32, return_sequences=True),
                              merge_mode='sum')(title_gru)
    title_gru1 = GlobalMaxPooling1D()(title_gru)
    title_gru2 = GlobalAveragePooling1D()(title_gru)

    desc_gru = Bidirectional(CuDNNGRU(64, return_sequences=True),
                             merge_mode='sum')(emb_desc)
    desc_gru = Bidirectional(CuDNNGRU(64, return_sequences=True),
                             merge_mode='sum')(desc_gru)
    desc_gru1 = GlobalMaxPooling1D()(desc_gru)
    desc_gru2 = GlobalAveragePooling1D()(desc_gru)

    fc = concatenate([
        Flatten()(emb_region),
        Flatten()(emb_city),
        Flatten()(emb_pcn),
        Flatten()(emb_cn),
        Flatten()(emb_para1),
        Flatten()(emb_para2),
        Flatten()(emb_para3),
        # Flatten()(emb_act),
        Flatten()(emb_img_top),
        Flatten()(emb_ut),
        conti,
        title_gru1,
        title_gru2,
        desc_gru1,
        desc_gru2,
        conv
    ])
    fc = Dense(256, activation='relu')(fc)
    fc = Dropout(0.5)(fc)
    fc = Dense(1, activation='sigmoid', name='output')(fc)

    model = Model(
        [
            region,
            city,
            pcn,
            cn,
            para1,
            para2,
            para3,
            # act,
            ut,
            img_top,
            title,
            desc,
            img,
            conti,
        ],
        output=fc)
    model.compile(optimizer=Nadam(),
                  loss="mean_squared_error",
                  metrics=[root_mean_squared_error])

    return model
import tflearn.datasets.oxflower17 as oxflower17
from sklearn.model_selection import train_test_split

X, Y = oxflower17.load_data(one_hot=True, resize_pics=(224, 224))
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)

# Step 1: Create the base pre-trained model
input_tensor = Input(shape=(224, 224, 3))
base_model = InceptionV3(input_tensor=input_tensor,
                         weights='imagenet',
                         include_top=False)

# Step 2: Create a new model with dense and softamx layers
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(17, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)

# Step 3: Freeze all pre-trained layers and train the top layers with new dataaset
for layer in base_model.layers:
    layer.trainable = False
model.compile(optimizer='adadelta',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(X_train, Y_train, batch_size=50, epochs=10)

# Step 4: Unfreeze some pre-trained layers and train with new dataset
for layer in model.layers[:5]:
    layer.trainable = False
Beispiel #30
0
def __create_dense_net(nb_classes, img_input, include_top, depth=40, nb_dense_block=3, growth_rate=12, nb_filter=-1,
                       nb_layers_per_block=-1, bottleneck=False, reduction=0.0, dropout_rate=None, weight_decay=1e-4,
                       subsample_initial_block=False, pooling=None, activation='softmax', transition_pooling='avg'):
    ''' Build the DenseNet model

    # Arguments
        nb_classes: number of classes
        img_input: tuple of shape (channels, rows, columns) or (rows, columns, channels)
        include_top: flag to include the final Dense layer
        depth: number or layers
        nb_dense_block: number of dense blocks to add to end (generally = 3)
        growth_rate: number of filters to add per dense block
        nb_filter: initial number of filters. Default -1 indicates initial number of filters is 2 * growth_rate
        nb_layers_per_block: number of layers in each dense block.
                Can be a -1, positive integer or a list.
                If -1, calculates nb_layer_per_block from the depth of the network.
                If positive integer, a set number of layers per dense block.
                If list, nb_layer is used as provided. Note that list size must
                be (nb_dense_block + 1)
        bottleneck: add bottleneck blocks
        reduction: reduction factor of transition blocks. Note : reduction value is inverted to compute compression
        dropout_rate: dropout rate
        weight_decay: weight decay rate
        subsample_initial_block: Changes model type to suit different datasets.
            Should be set to True for ImageNet, and False for CIFAR datasets.
            When set to True, the initial convolution will be strided and
            adds a MaxPooling2D before the initial dense block.
        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.
        activation: Type of activation at the top layer. Can be one of 'softmax' or 'sigmoid'.
                Note that if sigmoid is used, classes must be 1.
        transition_pooling: `avg` for avg pooling (default), `max` for max pooling,
            None for no pooling during scale transition blocks. Please note that this
            default differs from the DenseNetFCN paper in accordance with the DenseNet
            paper.

    # Returns
        a keras tensor

    # Raises
        ValueError: in case of invalid argument for `reduction`
            or `nb_dense_block`
    '''
    with K.name_scope('DenseNet'):
        concat_axis = 1 if K.image_data_format() == 'channels_first' else -1

        if reduction != 0.0:
            if not (reduction <= 1.0 and reduction > 0.0):
                raise ValueError('`reduction` value must lie between 0.0 and 1.0')

        # layers in each dense block
        if type(nb_layers_per_block) is list or type(nb_layers_per_block) is tuple:
            nb_layers = list(nb_layers_per_block)  # Convert tuple to list

            if len(nb_layers) != (nb_dense_block):
                raise ValueError('If `nb_dense_block` is a list, its length must match '
                                 'the number of layers provided by `nb_layers`.')

            final_nb_layer = nb_layers[-1]
            nb_layers = nb_layers[:-1]
        else:
            if nb_layers_per_block == -1:
                assert (depth - 4) % 3 == 0, 'Depth must be 3 N + 4 if nb_layers_per_block == -1'
                count = int((depth - 4) / 3)

                if bottleneck:
                    count = count // 2

                nb_layers = [count for _ in range(nb_dense_block)]
                final_nb_layer = count
            else:
                final_nb_layer = nb_layers_per_block
                nb_layers = [nb_layers_per_block] * nb_dense_block

        # compute initial nb_filter if -1, else accept users initial nb_filter
        if nb_filter <= 0:
            nb_filter = 2 * growth_rate

        # compute compression factor
        compression = 1.0 - reduction

        # Initial convolution
        if subsample_initial_block:
            initial_kernel = (7, 7)
            initial_strides = (2, 2)
        else:
            initial_kernel = (3, 3)
            initial_strides = (1, 1)

        x = Conv2D(nb_filter, initial_kernel, kernel_initializer='he_normal', padding='same', name='initial_conv2D',
                   strides=initial_strides, use_bias=False, kernel_regularizer=l2(weight_decay))(img_input)

        if subsample_initial_block:
            x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5, name='initial_bn')(x)
            x = Activation('relu')(x)
            x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

        # Add dense blocks
        for block_idx in range(nb_dense_block - 1):
            x, nb_filter = __dense_block(x, nb_layers[block_idx], nb_filter, growth_rate, bottleneck=bottleneck,
                                         dropout_rate=dropout_rate, weight_decay=weight_decay,
                                         block_prefix='dense_%i' % block_idx)
            # add transition_block
            x = __transition_block(x, nb_filter, compression=compression, weight_decay=weight_decay,
                                   block_prefix='tr_%i' % block_idx, transition_pooling=transition_pooling)
            nb_filter = int(nb_filter * compression)

        # The last dense_block does not have a transition_block
        x, nb_filter = __dense_block(x, final_nb_layer, nb_filter, growth_rate, bottleneck=bottleneck,
                                     dropout_rate=dropout_rate, weight_decay=weight_decay,
                                     block_prefix='dense_%i' % (nb_dense_block - 1))

        x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5, name='final_bn')(x)
        x = Activation('relu')(x)

        if include_top:
            if pooling == 'avg':
                x = GlobalAveragePooling2D()(x)
            elif pooling == 'max':
                x = GlobalMaxPooling2D()(x)
            x = Dense(nb_classes, activation=activation)(x)
        else:
            if pooling == 'avg':
                x = GlobalAveragePooling2D()(x)
            elif pooling == 'max':
                x = GlobalMaxPooling2D()(x)

        return x