def get_Inception_classifier():
    inputs = Input((CLASSIFY_INPUT_WIDTH, CLASSIFY_INPUT_HEIGHT, CLASSIFY_INPUT_DEPTH, CLASSIFY_INPUT_CHANNEL))
    print('inputs')
    print(inputs.get_shape())

    # Make inception base
    x = inception_base(inputs)

    for i in range(INCEPTION_BLOCKS):
        x = inception_block(x, filters=INCEPTION_KEEP_FILTERS)

        if (i + 1) % INCEPTION_REDUCTION_STEPS == 0 and i != INCEPTION_BLOCKS - 1:
            x = reduction_block(x, filters=INCEPTION_KEEP_FILTERS // 2)

    print('top')
    x = GlobalMaxPooling3D()(x)
    print(x.get_shape())
    x = Dropout(INCEPTION_DROPOUT)(x)
    x = Dense(2, activation='softmax')(x)
    print(x.get_shape())

    model = Model(inputs=inputs, outputs=x)
    model.compile(optimizer=Adam(lr=TRAIN_CLASSIFY_LEARNING_RATE), loss='binary_crossentropy', metrics=['accuracy'])

    return model
Exemplo n.º 2
0
    def build_model(self, p):
        S = Input(p['input_shape'], name='input_state')
        A = Input((1,), name='input_action', dtype='int32')
        R = Input((1,), name='input_reward')
        T = Input((1,), name='input_terminate', dtype='int32')
        NS = Input(p['input_shape'], name='input_next_sate')

        self.Q_model = self.build_cnn_model(p)
        self.Q_old_model = self.build_cnn_model(p, False)  # Q hat in paper
        self.Q_old_model.set_weights(self.Q_model.get_weights())  # Q' = Q

        Q_S = self.Q_model(S)  # batch * actions
        Q_NS = disconnected_grad(self.Q_old_model(NS))  # disconnected gradient is not necessary

        y = R + p['discount'] * (1-T) * K.max(Q_NS, axis=1, keepdims=True)  # batch * 1

        action_mask = K.equal(Tht.arange(p['num_actions']).reshape((1, -1)), A.reshape((-1, 1)))
        output = K.sum(Q_S * action_mask, axis=1).reshape((-1, 1))
        loss = K.sum((output - y) ** 2)  # sum could also be mean()

        optimizer = adam(p['learning_rate'])
        params = self.Q_model.trainable_weights
        update = optimizer.get_updates(params, [], loss)

        self.training_func = K.function([S, A, R, T, NS], loss, updates=update)
        self.Q_func = K.function([S], Q_S)
Exemplo n.º 3
0
def resnet_2D_v2(input_dim, mode='train'):
    bn_axis = 3
    if mode == 'train':
        inputs = Input(shape=input_dim, name='input')
    else:
        inputs = Input(shape=(input_dim[0], None, input_dim[-1]), name='input')
    # ===============================================
    #            Convolution Block 1
    # ===============================================
    x1 = Conv2D(64, (7, 7),
                strides=(2, 2),
                kernel_initializer='orthogonal',
                use_bias=False,
                trainable=True,
                kernel_regularizer=l2(weight_decay),
                padding='same',
                name='conv1_1/3x3_s1')(inputs)

    x1 = BatchNormalization(axis=bn_axis,
                            name='conv1_1/3x3_s1/bn',
                            trainable=True)(x1)
    x1 = Activation('relu')(x1)
    x1 = MaxPooling2D((2, 2), strides=(2, 2))(x1)

    # ===============================================
    #            Convolution Section 2
    # ===============================================
    x2 = conv_block_2D(x1,
                       3, [64, 64, 256],
                       stage=2,
                       block='a',
                       strides=(1, 1),
                       trainable=True)
    x2 = identity_block_2D(x2,
                           3, [64, 64, 256],
                           stage=2,
                           block='b',
                           trainable=True)
    x2 = identity_block_2D(x2,
                           3, [64, 64, 256],
                           stage=2,
                           block='c',
                           trainable=True)
    # ===============================================
    #            Convolution Section 3
    # ===============================================
    x3 = conv_block_2D(x2,
                       3, [128, 128, 512],
                       stage=3,
                       block='a',
                       trainable=True)
    x3 = identity_block_2D(x3,
                           3, [128, 128, 512],
                           stage=3,
                           block='b',
                           trainable=True)
    x3 = identity_block_2D(x3,
                           3, [128, 128, 512],
                           stage=3,
                           block='c',
                           trainable=True)
    # ===============================================
    #            Convolution Section 4
    # ===============================================
    x4 = conv_block_2D(x3,
                       3, [256, 256, 1024],
                       stage=4,
                       block='a',
                       strides=(1, 1),
                       trainable=True)
    x4 = identity_block_2D(x4,
                           3, [256, 256, 1024],
                           stage=4,
                           block='b',
                           trainable=True)
    x4 = identity_block_2D(x4,
                           3, [256, 256, 1024],
                           stage=4,
                           block='c',
                           trainable=True)
    # ===============================================
    #            Convolution Section 5
    # ===============================================
    x5 = conv_block_2D(x4,
                       3, [512, 512, 2048],
                       stage=5,
                       block='a',
                       trainable=True)
    x5 = identity_block_2D(x5,
                           3, [512, 512, 2048],
                           stage=5,
                           block='b',
                           trainable=True)
    x5 = identity_block_2D(x5,
                           3, [512, 512, 2048],
                           stage=5,
                           block='c',
                           trainable=True)
    y = MaxPooling2D((3, 1), strides=(2, 1), name='mpool2')(x5)
    return inputs, y
"""

#- A layer instance is callable (on a tensor), and it returns a tensor
#- Input tensor(s) and output tensor(s) can then be used to define a `Model`

#steps
# 1. define the input
# 2. create model functional way, include output
# 3. put input and output into Model containner
# 4. call the Model's func to do everything else.

from keras.layers import Input, Dense
from keras.engine.training import Model

# this return a tensor
inputs = Input(shape=(784, ))

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

# this creates a model that includes
# the Input layer and three Dense layers
model = Model(input=inputs, output=predictions)
model.compile(optimizer='rmsprop',
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(data, labels)

# you can treat any model as if it were a layer, by calling it on a tensor.
def resnet_v1(input_shape, depth, num_classes=10):
    """ResNet Version 1 Model builder [a]
    Stacks of 2 x (3 x 3) Conv2D-BN-ReLU
    Last ReLU is after the shortcut connection.
    At the beginning of each stage, the feature map size is halved (downsampled)
    by a convolutional layer with strides=2, while the number of filters is
    doubled. Within each stage, the layers have the same number filters and the
    same number of filters.
    Features maps sizes:
    stage 0: 32x32, 16
    stage 1: 16x16, 32
    stage 2:  8x8,  64
    The Number of parameters is approx the same as Table 6 of [a]:
    ResNet20 0.27M
    ResNet32 0.46M
    ResNet44 0.66M
    ResNet56 0.85M
    ResNet110 1.7M
    # Arguments
        input_shape (tensor): shape of input image tensor
        depth (int): number of core convolutional layers
        num_classes (int): number of classes (CIFAR10 has 10)
    # Returns
        model (Model): Keras model instance
    """
    if (depth - 2) % 6 != 0:
        raise ValueError('depth should be 6n+2 (eg 20, 32, 44 in [a])')
    # Start model definition.
    num_filters = 16
    num_res_blocks = int((depth - 2) / 6)

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

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

    # Instantiate model.
    model = Model(inputs=inputs, outputs=outputs)
    return model
Exemplo n.º 6
0
from tensorflow.compat.v1 import InteractiveSession
import tensorflow as tf

config = ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.7
config.gpu_options.allow_growth = True

session = InteractiveSession(config=config)

height = 224
width = 224
nh = 224
nw = 224
ncol = 3

visible2 = Input(shape=(nh, nw, ncol), name='imginp')
resnet = keras.applications.resnet_v2.ResNet50V2(include_top=True, weights='imagenet', input_tensor=visible2, input_shape=None, pooling=None, classes=1000)


def read_image_files(files_max_count,dir_name):
    files = [item.name for item in os.scandir(dir_name) if item.is_file()]
    files_count = files_max_count
    if files_max_count>len(files) :
        files_count = len(files)
    image_box = [[]]*files_count
    for file_i in range(files_count):
        image_box[file_i] = Image.open(dir_name+'/'+files[file_i])
    return files_count, image_box


def getresult(image_box):
Exemplo n.º 7
0
from keras.utils import np_utils
from keras.layers import BatchNormalization as BN
from keras.layers import Dropout

(x_train, y_train), (x_test, y_test) = cifar10.load_data()  #loda data

y_train = y_train.reshape(y_train.shape[0])
y_test = y_test.reshape(y_test.shape[0])

x_train = x_train.astype('float32') / 255 + 0.5
x_test = x_test.astype('float32') / 255 + 0.5
fit_y_train = np_utils.to_categorical(y_train)
pred_y_test = np_utils.to_categorical(y_test)

#MODEL
bn_input_layer = Input(shape=(32, 32, 3))
bn_layer_0 = BN()(bn_input_layer)
bn_conv1 = Conv2D(filters=64, kernel_size=3)(bn_layer_0)
bn_layer_1 = BN()(bn_conv1)
bn_conv1_active = Activation('relu')(bn_layer_1)
bn_cpool1 = MaxPooling2D(pool_size=2, strides=2,
                         padding='same')(bn_conv1_active)

bn_conv2 = Conv2D(filters=128, kernel_size=3)(bn_cpool1)
bn_layer_2 = BN()(bn_conv2)
bn_conv2_active = Activation('relu')(bn_layer_2)
bn_cpool2 = MaxPooling2D(pool_size=2, strides=2,
                         padding='same')(bn_conv2_active)

bn_conv3 = Conv2D(filters=256, kernel_size=3)(bn_cpool2)
bn_layer_3 = BN()(bn_conv3)
Exemplo n.º 8
0
def ResNet50(include_top=True, weights='imagenet',
             input_tensor=None):
    '''Instantiate the ResNet50 architecture,
    optionally loading weights pre-trained
    on ImageNet. Note that when using TensorFlow,
    for best performance you should set
    `image_dim_ordering="tf"` in your Keras config
    at ~/.keras/keras.json.

    The model and the weights are compatible with both
    TensorFlow and Theano. The dimension ordering
    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. xput of `layers.Input()`)
            to use as image input for the model.

    # Returns
        A Keras model instance.
    '''
    if weights not in {'imagenet', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `imagenet` '
                         '(pre-training on ImageNet).')
    # Determine proper input shape
    if K.image_dim_ordering() == 'th':
        if include_top:
            input_shape = (3, 224, 224)
        else:
            input_shape = (3, None, None)
    else:
        if include_top:
            input_shape = (224, 224, 3)
        else:
            input_shape = (None, None, 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)
        else:
            img_input = input_tensor
    if K.image_dim_ordering() == 'tf':
        bn_axis = 3
    else:
        bn_axis = 1

    x = ZeroPadding2D((3, 3))(img_input)
    x = Convolution2D(64, 7, 7, subsample=(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(1000, activation='softmax', name='fc1000')(x)

    model = Model(img_input, x)

    # load weights
    if weights == 'imagenet':
        print('K.image_dim_ordering:', K.image_dim_ordering())
        if K.image_dim_ordering() == 'th':
            if include_top:
                weights_path = get_file('resnet50_weights_th_dim_ordering_th_kernels.h5',
                                        TH_WEIGHTS_PATH,
                                        cache_subdir='models')
            else:
                weights_path = get_file('resnet50_weights_th_dim_ordering_th_kernels_notop.h5',
                                        TH_WEIGHTS_PATH_NO_TOP,
                                        cache_subdir='models')
            model.load_weights(weights_path)
            if K.backend() == 'tensorflow':
                warnings.warn('You are using the TensorFlow backend, yet you '
                              'are using the Theano '
                              'image dimension ordering convention '
                              '(`image_dim_ordering="th"`). '
                              'For best performance, set '
                              '`image_dim_ordering="tf"` in '
                              'your Keras config '
                              'at ~/.keras/keras.json.')
                convert_all_kernels_in_model(model)
        else:
            if include_top:
                weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels.h5',
                                        TF_WEIGHTS_PATH,
                                        cache_subdir='models')
            else:
                weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
                                        TF_WEIGHTS_PATH_NO_TOP,
                                        cache_subdir='models')
            model.load_weights(weights_path)
            if K.backend() == 'theano':
                convert_all_kernels_in_model(model)
    return model
Exemplo n.º 9
0
y=np.array(range(711, 811))

x = np.transpose(x)

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y,shuffle=False, test_size=0.2)

# 2. 모델구성
from keras.models import Sequential, Model
from keras.layers import Dense, Input
# model = Sequential()
# model.add(Dense(5, input_dim=3))
# model.add(Dense(5, input_shape(3,)))
# model.add(Dense(4))
# model.add(Dense(1))
input1 = Input(shape=(3, ))
dense1 = Dense(5, activation='relu')(input1)
dense1 = Dense(4)(dense1)
dense1 = Dense(4)(dense1)
dense1 = Dense(4)(dense1)
dense1 = Dense(4)(dense1)
dense1 = Dense(4)(dense1)
output1 = Dense(1)(dense1)

model = Model(inputs = input1, outputs=output1)

model.summary()

# 3. 훈련
model.compile(loss='mse', optimizer='adam', metrics=['mse'])
model.fit(x_train, y_train, epochs=300, batch_size=1, validation_split=0.25,verbose=3) 
Exemplo n.º 10
0
def MobileNetV2(input_shape=None,
                alpha=1.0,
                expansion_factor=6,
                depth_multiplier=1,
                dropout=1e-3,
                include_top=True,
                weights='imagenet',
                input_tensor=None,
                pooling=None,
                classes=1000):
    """Instantiates the MobileNet architecture.
    MobileNet V2 is from the paper:
    - [Inverted Residuals and Linear Bottlenecks: Mobile Networks for Classification, Detection and Segmentation](https://arxiv.org/abs/1801.04381)

    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.
        expansion_factor: controls the expansion of the internal bottleneck
            blocks. Should be a positive integer >= 1
        depth_multiplier: depth multiplier for depthwise convolution
            (also called the resolution multiplier)
        dropout: dropout rate
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: `None` (random initialization) or
            `imagenet` (ImageNet weights)
        input_tensor: optional Keras tensor (i.e. output of
            `layers.Input()`)
            to use as image input for the model.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model
                will be the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a
                2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.
    # Returns
        A Keras model instance.
    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
        RuntimeError: If attempting to run this model with a
            backend that does not support separable convolutions.
    """

    if K.backend() != 'tensorflow':
        raise RuntimeError('Only Tensorflow backend is currently supported, '
                           'as other backends do not support '
                           'depthwise convolution.')

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

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

    # Determine proper input shape and default size.
    if input_shape is None:
        default_size = 224
    else:
        if K.image_data_format() == 'channels_first':
            rows = input_shape[1]
            cols = input_shape[2]
        else:
            rows = input_shape[0]
            cols = input_shape[1]

        if rows == cols and rows in [96, 128, 160, 192, 224]:
            default_size = rows
        else:
            default_size = 224

    input_shape = _obtain_input_shape(input_shape,
                                      default_size=default_size,
                                      min_size=32,
                                      data_format=K.image_data_format(),
                                      require_flatten=include_top or weights)
    if K.image_data_format() == 'channels_last':
        row_axis, col_axis = (0, 1)
    else:
        row_axis, col_axis = (1, 2)
    rows = input_shape[row_axis]
    cols = input_shape[col_axis]

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

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

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

    if K.image_data_format() != 'channels_last':
        warnings.warn('The MobileNet family of models is only available '
                      'for the input data format "channels_last" '
                      '(width, height, channels). '
                      'However your settings specify the default '
                      'data format "channels_first" (channels, width, height).'
                      ' You should set `image_data_format="channels_last"` '
                      'in your Keras config located at ~/.keras/keras.json. '
                      'The model being returned right now will expect inputs '
                      'to follow the "channels_last" data format.')
        K.set_image_data_format('channels_last')
        old_data_format = 'channels_first'
    else:
        old_data_format = None

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

    x = _conv_block(img_input, 32, alpha, bn_epsilon=1e-3, strides=(2, 2))
    x = _depthwise_conv_block_v2(x, 16, alpha, 1, depth_multiplier, bn_epsilon=1e-3, bn_momentum=0.999,
                                 block_id=1)

    x = _depthwise_conv_block_v2(x, 24, alpha, expansion_factor, depth_multiplier, block_id=2,
                                 bn_epsilon=1e-3, bn_momentum=0.999, strides=(2, 2))
    x = _depthwise_conv_block_v2(x, 24, alpha, expansion_factor, depth_multiplier, bn_epsilon=1e-3, bn_momentum=0.999,
                                 block_id=3)

    x = _depthwise_conv_block_v2(x, 32, alpha, expansion_factor, depth_multiplier, block_id=4,
                                 bn_epsilon=1e-3, bn_momentum=0.999, strides=(2, 2))
    x = _depthwise_conv_block_v2(x, 32, alpha, expansion_factor, depth_multiplier, bn_epsilon=1e-3, bn_momentum=0.999,
                                 block_id=5)
    x = _depthwise_conv_block_v2(x, 32, alpha, expansion_factor, depth_multiplier, bn_epsilon=1e-3, bn_momentum=0.999,
                                 block_id=6)

    x = _depthwise_conv_block_v2(x, 64, alpha, expansion_factor, depth_multiplier, block_id=7,
                                 bn_epsilon=1e-3, bn_momentum=0.999, strides=(2, 2))
    x = _depthwise_conv_block_v2(x, 64, alpha, expansion_factor, depth_multiplier, bn_epsilon=1e-3, bn_momentum=0.999,
                                 block_id=8)
    x = _depthwise_conv_block_v2(x, 64, alpha, expansion_factor, depth_multiplier, bn_epsilon=1e-3, bn_momentum=0.999,
                                 block_id=9)
    x = _depthwise_conv_block_v2(x, 64, alpha, expansion_factor, depth_multiplier, bn_epsilon=1e-3, bn_momentum=0.999,
                                 block_id=10)

    x = _depthwise_conv_block_v2(x, 96, alpha, expansion_factor, depth_multiplier, bn_epsilon=1e-3, bn_momentum=0.999,
                                 block_id=11)
    x = _depthwise_conv_block_v2(x, 96, alpha, expansion_factor, depth_multiplier, bn_epsilon=1e-3, bn_momentum=0.999,
                                 block_id=12)
    x = _depthwise_conv_block_v2(x, 96, alpha, expansion_factor, depth_multiplier, bn_epsilon=1e-3, bn_momentum=0.999,
                                 block_id=13)

    x = _depthwise_conv_block_v2(x, 160, alpha, expansion_factor, depth_multiplier, block_id=14,
                                 bn_epsilon=1e-3, bn_momentum=0.999, strides=(2, 2))
    x = _depthwise_conv_block_v2(x, 160, alpha, expansion_factor, depth_multiplier, bn_epsilon=1e-3, bn_momentum=0.999,
                                 block_id=15)
    x = _depthwise_conv_block_v2(x, 160, alpha, expansion_factor, depth_multiplier, bn_epsilon=1e-3, bn_momentum=0.999,
                                 block_id=16)

    x = _depthwise_conv_block_v2(x, 320, alpha, expansion_factor, depth_multiplier, bn_epsilon=1e-3, bn_momentum=0.999,
                                 block_id=17)

    if alpha <= 1.0:
        penultimate_filters = 1280
    else:
        penultimate_filters = int(1280 * alpha)

    x = _conv_block(x, penultimate_filters, alpha=1.0, kernel=(1, 1), bn_epsilon=1e-3, bn_momentum=0.999,
                    block_id=18)

    if include_top:
        if K.image_data_format() == 'channels_first':
            shape = (penultimate_filters, 1, 1)
        else:
            shape = (1, 1, penultimate_filters)

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

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

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

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

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

    if old_data_format:
        K.set_image_data_format(old_data_format)
    return model
Exemplo n.º 11
0
def CLDNNLikeModel3(weights=None,
                    input_shape=[28, 28, 1],
                    classes=10,
                    **kwargs):
    if weights is not None and not (os.path.exists(weights)):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization), '
                         'or the path to the weights file to be loaded.')

    dr = 0.5  # dropout rate (%)
    input = Input(input_shape, name='input')
    x = input
    x = Reshape((28, 28, 1))(x)
    x = Conv2D(filters=6,
               kernel_size=(5, 5),
               padding='valid',
               activation='sigmoid',
               kernel_initializer='glorot_uniform',
               name='convx1')(x)
    x = AveragePooling2D(pool_size=(2, 2),
                         border_mode='valid',
                         strides=2,
                         name='maxpoolx1')(x)
    x = Conv2D(filters=12,
               kernel_size=(5, 5),
               padding='valid',
               activation='sigmoid',
               kernel_initializer='glorot_uniform',
               name='convx2')(x)
    x = AveragePooling2D(pool_size=(2, 2),
                         border_mode='valid',
                         strides=2,
                         name='maxpoolx2')(x)
    x = Flatten()(x)
    print(x)
    x = Dense(10, activation='sigmoid', name='fcx1')(x)
    """
    # x = Reshape((4, 256, 256, 3))(x)
    x = Conv2D(filters=16, kernel_size=(5,5), padding='same', activation='relu',
               kernel_initializer='glorot_uniform',
               name='convx{}'.format(1))(x)
    # x = BatchNormalization(name='conv{}-bn'.format(index + 1))(x)
    # x = Dropout(dr)(x)
    x = MaxPooling2D(pool_size=(2,2), strides=2, name='maxpoolx{}'.format(1))(x)

    x = Conv2D(filters=32, kernel_size=kernel_size, padding='same', activation='relu',
               kernel_initializer='glorot_uniform',
               name='convx{}'.format(2))(x)
    # x = BatchNormalization(name='conv{}-bn'.format(index + 1))(x)
    # x = Dropout(dr)(x)
    x = MaxPooling2D(pool_size=(2,2), strides=2, name='maxpoolx{}'.format(2))(x)
    x = Conv2D(filters=64, kernel_size=kernel_size, padding='same', activation='relu',
               kernel_initializer='glorot_uniform',
               name='convx{}'.format(3))(x)
    # x = Dropout(dr)(x)
    x = MaxPooling2D(pool_size=(2,2), strides=2, name='maxpoolx{}'.format(3))(x)
    '''
    # LSTM
    # batch_size,64,2
    x = Reshape((128,2,32,1))(x)
    x = ConvLSTM2D(filters=32,kernel_size=(1,1), return_sequences = True)(x)
    x = ConvLSTM2D(filters=32,kernel_size=(1,1))(x)
    '''


    #DNN
    x = Flatten()(x)
    x = Dense(128, activation='selu', name='fcx1')(x)
    x = Dropout(dr)(x)
    x = Dense(512, activation='selu', name='fcx2')(x)
    x = Dropout(dr)(x)
    #x = Dense(128, activation='selu', name='fc2',bias_regularizer=keras.regularizers.l2(0.1))(x)
    x = Dense(classes,activation='softmax',name='softmax')(x)
    """

    # Load weights.
    if weights is not None:
        Model.load_weights(weights)

    model = Model(inputs=input, outputs=x)

    return model
Exemplo n.º 12
0
def do_training(training_data):
    import keras
    from keras.layers import Input, Dense
    from keras.models import Model, load_model
    import datetime

    start_time = time.time()

    training_sample_size = 1000
    test_sample_size = 33

    print("Loading training data...")
    data = training_data.reshape(
        (len(training_data), 3 * len(training_data[0])))
    numpy.random.shuffle(data)

    train_data = numpy.array(data[:training_sample_size])
    test_data = numpy.array(data[training_sample_size:training_sample_size +
                                 test_sample_size])

    print("Done loading: ", time.time() - start_time)

    # this is the size of our encoded representations
    encoded_dim = 20

    # Single autoencoder
    # initializer = keras.initializers.RandomUniform(minval=0.0, maxval=0.01, seed=5)
    # bias_initializer = initializer
    activation = 'relu'  #keras.layers.advanced_activations.LeakyReLU(alpha=0.3) #'relu'

    input = Input(shape=(len(train_data[0]), ))
    output = input
    # output = Dense(200, activation=activation)(input)
    output = Dense(1000, activation=activation)(output)
    output = Dense(encoded_dim, activation=activation, name="encoded")(output)
    output = Dense(1000, activation=activation)(output)
    # output = Dense(200, activation=activation)(output)
    output = Dense(len(train_data[0]), activation='linear')(
        output
    )  #'linear',)(output) # First test seems to indicate no change on output with linear

    autoencoder = Model(input, output)

    optimizer = keras.optimizers.Adam(lr=0.001,
                                      beta_1=0.9,
                                      beta_2=0.999,
                                      epsilon=1e-08,
                                      decay=0)
    autoencoder.compile(optimizer=optimizer, loss='mean_squared_error')

    model_start_time = time.time()
    autoencoder.fit(train_data,
                    train_data,
                    epochs=4,
                    batch_size=16,
                    shuffle=True,
                    validation_data=(test_data, test_data))

    output_path = 'models/' + datetime.datetime.now().strftime(
        "%I %M%p %B %d %Y") + '.h5'
    autoencoder.save(output_path)

    print("Total model time: ", time.time() - model_start_time)

    # Display
    predict_start = time.time()
    test_data = test_data
    decoded_samples = autoencoder.predict(test_data)
    print('Predict took: ', time.time() - predict_start)

    print("Total runtime: ", time.time() - start_time)
Exemplo n.º 13
0
def main():
    global verts_sample

    initial_verts, initial_faces = get_initial_verts_and_faces()
    numpy_base_verts = e2p(initial_verts).flatten()

    start = time.time()
    num_samples = 150
    numpy_displacements_sample = load_samples(num_samples)
    #numpy_displacements_sample = numpy_verts_sample - initial_verts

    num_verts = len(numpy_displacements_sample[0])
    print(num_verts)

    print('Loading...')
    #verts_sample = [p2e(m) for m in numpy_verts_sample]
    displacements_sample = [p2e(m) for m in numpy_displacements_sample]
    print(e2p(initial_verts))
    print(len(e2p(initial_verts)))
    position_sample = [p2e(m) for m in numpy_displacements_sample]
    print("Took:", time.time() - start)

    use_pca = True
    do_analysis = False
    if do_analysis:
        if use_pca:
            ### PCA Version
            print("Doing PCA...")
            test_data = numpy_displacements_sample[:test_size] * 1.0
            test_data_eigen = displacements_sample[:test_size]
            numpy.random.shuffle(numpy_displacements_sample)
            # train_data = numpy_verts_sample[test_size:test_size+train_size]
            train_data = numpy_displacements_sample[0:train_size]
            print(train_data[1][:30])
            print(num_verts)
            pca = PCA(n_components=3)
            pca.fit(train_data.reshape((train_size, 3 * num_verts)))

            # def encode(q):
            #     return pca.transform(numpy.array([q.flatten() - numpy_base_verts]))[0]

            # def decode(z):
            #     return (numpy_base_verts + pca.inverse_transform(numpy.array([z]))[0]).reshape((num_verts, 3))

            # print(numpy.equal(test_data[0].flatten().reshape((len(test_data[0]),3)), test_data[0]))
            # print(encode(test_data[0]))

            test_data_encoded = pca.transform(
                test_data.reshape(test_size, 3 * num_verts))
            test_data_decoded = (
                numpy_base_verts +
                pca.inverse_transform(test_data_encoded)).reshape(
                    test_size, num_verts, 3)
            test_data_decoded_eigen = [p2e(m) for m in test_data_decoded]
            ### End of PCA version
        else:
            ### Autoencoder
            import keras
            from keras.layers import Input, Dense
            from keras.models import Model, load_model
            import datetime

            start_time = time.time()

            train_size = num_samples
            test_size = num_samples
            test_data = numpy_displacements_sample[:test_size].reshape(
                test_size, 3 * num_verts)
            test_data_eigen = numpy_displacements_sample[:test_size]
            # numpy.random.shuffle(numpy_displacements_sample)
            # train_data = numpy_verts_sample[test_size:test_size+train_size]
            train_data = numpy_displacements_sample[0:train_size].reshape(
                (train_size, 3 * num_verts))

            mean = numpy.mean(train_data, axis=0)
            std = numpy.std(train_data, axis=0)

            mean = numpy.mean(train_data)
            std = numpy.std(train_data)

            s_min = numpy.min(train_data)
            s_max = numpy.max(train_data)

            def normalize(data):
                return numpy.nan_to_num((data - mean) / std)
                # return numpy.nan_to_num((train_data - s_min) / (s_max - s_min))
            def denormalize(data):
                return data * std + mean
                # return data * (s_max - s_min) + s_min

            train_data = normalize(train_data)
            test_data = normalize(test_data)

            # print(train_data)
            # print(mean)
            # print(std)
            # exit()
            # this is the size of our encoded representations
            encoded_dim = 3

            # Single autoencoder
            # initializer = keras.initializers.RandomUniform(minval=0.0, maxval=0.01, seed=5)
            # bias_initializer = initializer
            activation = keras.layers.advanced_activations.LeakyReLU(
                alpha=0.3)  #'relu'

            input = Input(shape=(len(train_data[0]), ))
            output = input
            output = Dense(30, activation=activation)(input)
            output = Dense(512, activation=activation)(output)
            output = Dense(64, activation=activation)(output)
            output = Dense(encoded_dim, activation=activation,
                           name="encoded")(output)
            output = Dense(64, activation=activation)(output)
            output = Dense(512, activation=activation)(output)
            output = Dense(30, activation=activation)(output)
            output = Dense(len(train_data[0]), activation='linear')(
                output
            )  #'linear',)(output) # First test seems to indicate no change on output with linear

            autoencoder = Model(input, output)

            optimizer = keras.optimizers.Adam(lr=0.001,
                                              beta_1=0.9,
                                              beta_2=0.999,
                                              epsilon=1e-08,
                                              decay=0)
            autoencoder.compile(optimizer=optimizer, loss='mean_squared_error')

            model_start_time = time.time()
            autoencoder.fit(train_data,
                            train_data,
                            epochs=1000,
                            batch_size=num_samples,
                            shuffle=True,
                            validation_data=(test_data, test_data))

            # output_path = 'trained_models/' + datetime.datetime.now().strftime("%I %M%p %B %d %Y") + '.h5'
            # autoencoder.save(output_path)

            print("Total model time: ", time.time() - model_start_time)

            # Display

            decoded_samples = denormalize(autoencoder.predict(test_data))
            #decoded_samples = autoencoder.predict(test_data) * std + mean

            test_data_decoded = (numpy_base_verts + decoded_samples).reshape(
                test_size, num_verts, 3)
            test_data_decoded_eigen = [p2e(m) for m in test_data_decoded]
            ### End of Autoencoder

        # Error colours
        error = numpy.sum((test_data_decoded - numpy_verts_sample)**2, axis=2)
        colours = [igl.eigen.MatrixXd() for _ in range(num_samples)]
        for i in range(num_samples):
            igl.jet(p2e(error[i]), True, colours[i])

    # Set up
    viewer = igl.viewer.Viewer()

    #viewer.data.set_mesh(initial_verts, initial_faces)
    # viewer.data.set_face_based(False)
    colours = igl.eigen.MatrixXd(num_verts)
    viewer.data.set_points(initial_verts, colours)

    def pre_draw(viewer):
        global current_frame, verts_sample, show_decoded

        if viewer.core.is_animating:
            print(current_frame)
            print(show_decoded)
            if show_decoded:
                viewer.data.set_points(test_data_decoded_eigen[current_frame],
                                       colours)
            else:
                viewer.data.set_points(position_sample[current_frame], colours)

            viewer.data.compute_normals()
            current_frame = (current_frame + 1) % num_samples

        return False

    viewer.callback_pre_draw = pre_draw
    viewer.callback_key_down = key_down
    viewer.core.is_animating = False
    # viewer.core.camera_zoom = 2.5
    viewer.core.animation_max_fps = 30.0

    viewer.launch()
Exemplo n.º 14
0
    def build_model(self):
        real_x = Input(shape=self.img_shape, name='real_image')
        z_mean, z_log_var = self.encoder(real_x)

        z = Lambda(self.sampling,
                   output_shape=(self.latent_dim, ),
                   name='re-sampling_layer')([z_mean, z_log_var])
        # kl_loss = Lambda()

        reconstruct_x_flatten = self.decoder(z)
        reconstruct_x = Reshape(self.img_shape)(reconstruct_x_flatten)
        interpolated_img = RandomWeightedAverage()([real_x, reconstruct_x])
        # Determine validity of weighted sample
        validity_interpolated = self.critic(interpolated_img)
        partial_gp_loss_rec = partial(self.gradient_penalty_loss,
                                      averaged_samples=interpolated_img)
        partial_gp_loss_rec.__name__ = 'gradient_penalty'  # Keras requires function names

        z_p = Input(shape=(self.latent_dim, ), name='random_distribution')
        gen_x_flatten = self.decoder(z_p)
        gen_x = Reshape(self.img_shape)(gen_x_flatten)
        random_gen_x_valid = self.critic(gen_x)
        reconstruct_x_valid = self.critic(reconstruct_x)
        real_x_valid = self.critic(real_x)

        ### 构建VAE_GAN的编码部分 ###
        self.encoder.trainable = True
        self.decoder.trainable = False
        self.encoder_trainer = Model(inputs=real_x,
                                     outputs=reconstruct_x,
                                     name='encoder')
        kl_loss = -0.5 * K.sum(
            1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
        reconstruct_loss = K.sum(K.binary_crossentropy(real_x, reconstruct_x),
                                 axis=np.arange(1, len(reconstruct_x.shape)))

        encoder_loss = K.mean(reconstruct_loss + kl_loss)
        self.encoder_trainer.add_loss(encoder_loss)
        self.encoder_trainer.compile(optimizer=self.optimizer)
        # self.encoder.summary()

        ### 构建VAE_GAN的生成部分 ###
        self.encoder.trainable = False
        self.decoder.trainable = True
        self.critic.trainable = False

        self.decoder_trainer = Model(inputs=[real_x],
                                     outputs=[reconstruct_x],
                                     name='decoder')
        self.decoder_trainer.compile(
            loss=[
                'binary_crossentropy',
            ],
            optimizer=self.optimizer,
        )
        # self.decoder_trainer.summary()

        ### 构建GAN的判别部分
        self.encoder.trainable = False
        self.decoder.trainable = False
        self.critic.trainable = True
        self.critic_trainer = Model(inputs=[real_x, z_p],
                                    outputs=[
                                        validity_interpolated,
                                        random_gen_x_valid,
                                        reconstruct_x_valid,
                                        real_x_valid,
                                    ],
                                    name='critic')
        self.critic_trainer.compile(loss=[
            partial_gp_loss_rec,
            self.wasserstein_loss,
            self.wasserstein_loss,
            self.wasserstein_loss,
        ],
                                    optimizer=self.optimizer,
                                    loss_weights=[10, 1, 1, 1])
Exemplo n.º 15
0
embedding_layer = Embedding(len(word_index) + 1,
                            embed_size,
                            weights=[embedding_matrix],
                            input_length=maxlen,
                            trainable=True)
'''
#Randomly initialized 
embedding_layer = Embedding(len(word_index) + 1,
                            embed_size,
                            input_length=maxlen,
                            trainable=True)

'''

print("start building model")
sequence_input = Input(shape=(maxlen, ), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)

units = 64
activations = Bidirectional(
    LSTM(64, return_sequences=True,
         kernel_regularizer=regularizers.l2(0.1)))(embedded_sequences)

# compute importance for each step
attention = Dense(1, activation='tanh')(activations)
attention = Flatten()(attention)
attention = Activation('softmax')(attention)
attention = RepeatVector(units * 2)(attention)
attention = Permute([2, 1])(attention)

sent_representation = multiply([activations, attention])
Exemplo n.º 16
0
def train_net():
    os.environ["CUDA_VISIBLE_DEVICES"] = '0'  #指定第一块GPU可用
    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.6  # 程序最多只能占用指定gpu50%的显存
    config.gpu_options.allow_growth = True  #程序按需申请内存
    sess = tf.Session(config=config)
    # 设置session
    KTF.set_session(sess)

    output_length = fs * 10
    input_length = fs * 10
    n_classes = 2

    input_length = 1280

    for fold in range(1, 6, 1):
        print("*******************fold {}*****************".format(fold))
        tr_data_path = "./validation/train/validation_{}_data.npy".format(
            fold)  #validation
        tr_label_path = "./validation/train/validation_{}_target.npy".format(
            fold)

        val_data_path = "./validation/val/validation_{}_data.npy".format(fold)
        val_label_path = "./validation/val/validation_{}_target.npy".format(
            fold)  #cv

        X_tr = np.load(tr_data_path)
        y_tr = np.load(tr_label_path).reshape(-1, 1280, 1)

        X_val = np.load(val_data_path)
        y_val = np.load(val_label_path).reshape(-1, 1280, 1)

        # callback_lists
        checkpoint = ModelCheckpoint(filepath="./model/"+ \
                                     'model_weights_mse_0.08_1280_{}_fold_{}_ys.h5' \
                                     .format(time.strftime('%Y-%m-%d %X').split(" ")[0],fold),
                                     monitor= 'val_loss',
                                     mode='min',
                                     verbose=1,
                                     save_best_only='True',
                                     save_weights_only='True')

        csv_logger = CSVLogger(
                    filename='./log/log{}_fold{}.csv'.format( \
                    time.strftime('%Y-%m-%d %X').split(" ")[0],fold),
                    separator=',',
                    append=True)

        earlystop = EarlyStopping(
            monitor='val_loss',
            min_delta=0,
            patience=5,
            verbose=1,
            mode="min",
            #baseline=None,
            #restore_best_weights=True,
        )

        reducelr = ReduceLROnPlateau(monitor='val_loss',
                                     factor=0.5,
                                     patience=3,
                                     verbose=1,
                                     min_lr=1e-5)

        callback_lists = [earlystop, checkpoint, reducelr]

        input_layer = Input((input_length, 1))
        output_layer = build_model(input_layer=input_layer,
                                   block="resnext",
                                   start_neurons=16,
                                   DropoutRatio=0.5,
                                   filter_size=32,
                                   nClasses=2)
        model = Model(input_layer, output_layer)
        #print(model.summary())
        model.compile(
            loss='mse',  #'categorical_crossentropy',
            optimizer=RAdam(1e-4),  #RAdam(lr_schedule(0)),#
            metrics=['accuracy', 'mse', 'mae'])  #focal_loss #mape

        history = model.fit(
            X_tr,
            y_tr,
            epochs=400,  #350 train
            batch_size=32,
            verbose=2,
            validation_data=(X_val, y_val),
            callbacks=callback_lists)  #class_weight=class_weight
Exemplo n.º 17
0
y_train, y_val, y_test, idx_train, idx_val, idx_test, train_mask = cora.get_splits(
    y)

# Normalize nodes' inputs 
X /= X.sum(1).reshape(-1, 1)

if FILTER == 'localpool':
    """ Local pooling filters (see 'renormalization trick' in Kipf & Welling, arXiv 2016) """
    print('Using local pooling filters...')
    A_ = cora.preprocess_adj(A, SYM_NORM)
    A_ = A_.todense()
    support = 1

    # Graph holds data from cora 
    graph = [X, A_]
    G = Input(shape=(None, None), batch_shape=(None, None))

elif FILTER == 'chebyshev':
    """ Chebyshev polynomial basis filters (Defferard et al., NIPS 2016)  """
    print('Using Chebyshev polynomial basis filters...')
    L = cora.normalized_laplacian(A, SYM_NORM)
    L_scaled = cora.rescale_laplacian(L)
    T_k = cora.chebyshev_polynomial(L_scaled, MAX_DEGREE)
    T_k = [tk.todense() for tk in T_k]
    support = MAX_DEGREE + 1
    graph = [X]+T_k
    G = [Input(shape=(None, None), batch_shape=(None, None)) for _ in range(support)]

else:
    raise Exception('Invalid filter type.')
                image_mask = misc.imread(
                    'train_masks/' + image_name.split('.')[0] + '_mask.gif',
                    flatten=True) / 255.
                X[i, ..., 1:1919, :3] = image_rgb
                X[i, ..., 1:1919, 3] = image_mask_320
                X[i, ..., 1:1919, 4] = image_mask_640
                Y[i, ..., 1:1919, 0] = image_mask
                i = i + 1
            yield X, Y


train_data_gen = ensemble_data_generator(1)
val_data_gen = ensemble_data_generator(1, 'val')

#Create simple model
inp = Input((1280, 1920, 5))
conv1 = Conv2D(64, 7, activation='relu', padding='same')(inp)
out = Conv2D(1, 9, activation='sigmoid', padding='same')(conv1)
model = Model(inp, out)
model.summary()

smooth = 1.


# From here: https://github.com/jocicmarko/ultrasound-nerve-segmentation/blob/master/train.py
def dice_coef(y_true, y_pred):
    y_true_f = K.flatten(y_true)
    y_pred_f = K.flatten(y_pred)
    intersection = K.sum(y_true_f * y_pred_f)
    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) +
                                           smooth)
#keep n_epoch a multiple of 5
n_epoch = 1
embedding_dim = 300

#definations to calculate distance between two vectors
def euclidean_distance(l,r):
 	return K.sqrt(K.sum(K.square(l - r), axis=-1, keepdims=True))

def manhattan_distance(l, r):
	abs_v = K.abs(l-r)
	sum_v = K.sum(abs_v,axis=1,keepdims=True)
	return K.exp(-sum_v)

#describing the shape of various inputs

leftInput = Input(shape=(maxSeqLength,), dtype='int32')
rightInput = Input(shape=(maxSeqLength,), dtype='int32')
minFreq = Input(shape=(1,), dtype='float32')
commonNeigh = Input(shape=(1,), dtype='float32')
q_len1 = Input(shape=(1,), dtype='float32')
q_len2 = Input(shape=(1,), dtype='float32')
diff_len = Input(shape=(1,), dtype='float32')
word_len1 = Input(shape=(1,), dtype='float32')
word_len2 = Input(shape=(1,), dtype='float32')
common_words = Input(shape=(1,), dtype='float32')

#embedding layer to generate word embeddings
embeddingLayer = Embedding(len(embeddingsMatrix), embedding_dim, weights=[embeddingsMatrix], input_length=maxSeqLength, trainable=False)

encodedLeft = embeddingLayer(leftInput)
encodedRight = embeddingLayer(rightInput)
Exemplo n.º 20
0
    def build_generator_resnet(self):

        def conv7s1(layer_input, filters, final):
            y = ReflectionPadding2D(padding =(3,3))(layer_input)
            y = Conv2D(filters, kernel_size=(7,7), strides=1, padding='valid', kernel_initializer = self.weight_init)(y)
            if final:
                y = Activation('tanh')(y)
            else:
                y = InstanceNormalization(axis = -1, center = False, scale = False)(y)
                y = Activation('relu')(y)
            return y

        def downsample(layer_input,filters):
            y = Conv2D(filters, kernel_size=(3,3), strides=2, padding='same', kernel_initializer = self.weight_init)(layer_input)
            y = InstanceNormalization(axis = -1, center = False, scale = False)(y)
            y = Activation('relu')(y)
            return y

        def residual(layer_input, filters):
            shortcut = layer_input
            y = ReflectionPadding2D(padding =(1,1))(layer_input)
            y = Conv2D(filters, kernel_size=(3, 3), strides=1, padding='valid', kernel_initializer = self.weight_init)(y)
            y = InstanceNormalization(axis = -1, center = False, scale = False)(y)
            y = Activation('relu')(y)
            
            y = ReflectionPadding2D(padding =(1,1))(y)
            y = Conv2D(filters, kernel_size=(3, 3), strides=1, padding='valid', kernel_initializer = self.weight_init)(y)
            y = InstanceNormalization(axis = -1, center = False, scale = False)(y)

            return add([shortcut, y])

        def upsample(layer_input,filters):
            y = Conv2DTranspose(filters, kernel_size=(3, 3), strides=2, padding='same', kernel_initializer = self.weight_init)(layer_input)
            y = InstanceNormalization(axis = -1, center = False, scale = False)(y)
            y = Activation('relu')(y)
    
            return y


        # Image input
        img = Input(shape=self.img_shape)

        y = img

        y = conv7s1(y, self.gen_n_filters, False)
        y = downsample(y, self.gen_n_filters * 2)
        y = downsample(y, self.gen_n_filters * 4)
        y = residual(y, self.gen_n_filters * 4)
        y = residual(y, self.gen_n_filters * 4)
        y = residual(y, self.gen_n_filters * 4)
        y = residual(y, self.gen_n_filters * 4)
        y = residual(y, self.gen_n_filters * 4)
        y = residual(y, self.gen_n_filters * 4)
        y = residual(y, self.gen_n_filters * 4)
        y = residual(y, self.gen_n_filters * 4)
        y = residual(y, self.gen_n_filters * 4)
        y = upsample(y, self.gen_n_filters * 2)
        y = upsample(y, self.gen_n_filters)
        y = conv7s1(y, 3, True)
        output = y

   
        return Model(img, output)
Exemplo n.º 21
0
def ResNet50(input_shape=(256, 256, 1), classes=8):
    # Define the input as a tensor with shape input_shape
    X_input = Input(input_shape)

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

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

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

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

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

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

    # AVGPOOL.
    X = AveragePooling2D((2, 2), name='avg_pool')(X)

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

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

    return model
Exemplo n.º 22
0
def get_unet(img_shape=None):

    inputs = Input(shape=img_shape)
    concat_axis = -1

    conv1 = Conv2D(64, (5, 5),
                   activation='relu',
                   padding='same',
                   data_format="channels_last",
                   name='conv1_1')(inputs)
    conv1 = Conv2D(64, (5, 5),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2), data_format="channels_last")(conv1)
    conv2 = Conv2D(96, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(pool1)
    conv2 = Conv2D(96, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2), data_format="channels_last")(conv2)

    conv3 = Conv2D(128, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(pool2)
    conv3 = Conv2D(128, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2), data_format="channels_last")(conv3)

    conv4 = Conv2D(256, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(pool3)
    conv4 = Conv2D(256, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2), data_format="channels_last")(conv4)

    conv5 = Conv2D(512, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(pool4)
    conv5 = Conv2D(512, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(conv5)

    up_conv5 = UpSampling2D(size=(2, 2), data_format="channels_last")(conv5)
    ch, cw = get_crop_shape(conv4, up_conv5)
    crop_conv4 = Cropping2D(cropping=(ch, cw),
                            data_format="channels_last")(conv4)
    up6 = concatenate([up_conv5, crop_conv4], axis=concat_axis)
    conv6 = Conv2D(256, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(up6)
    conv6 = Conv2D(256, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(conv6)

    up_conv6 = UpSampling2D(size=(2, 2), data_format="channels_last")(conv6)
    ch, cw = get_crop_shape(conv3, up_conv6)
    crop_conv3 = Cropping2D(cropping=(ch, cw),
                            data_format="channels_last")(conv3)
    up7 = concatenate([up_conv6, crop_conv3], axis=concat_axis)
    conv7 = Conv2D(128, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(up7)
    conv7 = Conv2D(128, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(conv7)

    up_conv7 = UpSampling2D(size=(2, 2), data_format="channels_last")(conv7)
    ch, cw = get_crop_shape(conv2, up_conv7)
    crop_conv2 = Cropping2D(cropping=(ch, cw),
                            data_format="channels_last")(conv2)
    up8 = concatenate([up_conv7, crop_conv2], axis=concat_axis)
    conv8 = Conv2D(96, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(up8)
    conv8 = Conv2D(96, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(conv8)

    up_conv8 = UpSampling2D(size=(2, 2), data_format="channels_last")(conv8)
    ch, cw = get_crop_shape(conv1, up_conv8)
    crop_conv1 = Cropping2D(cropping=(ch, cw),
                            data_format="channels_last")(conv1)
    up9 = concatenate([up_conv8, crop_conv1], axis=concat_axis)
    conv9 = Conv2D(64, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(up9)
    conv9 = Conv2D(64, (3, 3),
                   activation='relu',
                   padding='same',
                   data_format="channels_last")(conv9)

    ch, cw = get_crop_shape(inputs, conv9)
    conv9 = ZeroPadding2D(padding=(ch, cw), data_format="channels_last")(conv9)
    conv10 = Conv2D(1, (1, 1),
                    activation='sigmoid',
                    data_format="channels_last")(conv9)
    model = Model(inputs=inputs, outputs=conv10)
    model.compile(optimizer=Adam(lr=(1e-4) * 2),
                  loss=dice_coef_loss,
                  metrics=[dice_coef_for_training])

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

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

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

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

    if K.image_data_format() == 'channels_first':
        channel_axis = 1
    else:
        channel_axis = 3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    # load weights
    if weights == 'imagenet':
        if K.image_data_format() == 'channels_first':
            if K.backend() == 'tensorflow':
                warnings.warn('You are using the TensorFlow backend, yet you '
                              'are using the Theano '
                              'image data format convention '
                              '(`image_data_format="channels_first"`). '
                              'For best performance, set '
                              '`image_data_format="channels_last"` in '
                              'your Keras config '
                              'at ~/.keras/keras.json.')
        if include_top:
            weights_path = get_file(
                'inception_v3_weights_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH,
                cache_subdir='models',
                md5_hash='9a0d58056eeedaa3f26cb7ebd46da564')
        else:
            weights_path = get_file(
                'inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5',
                WEIGHTS_PATH_NO_TOP,
                cache_subdir='models',
                md5_hash='bcbd6486424b2319ff4ef7d526e38f63')
        model.load_weights(weights_path)
        if K.backend() == 'theano':
            convert_all_kernels_in_model(model)
    return model
Exemplo n.º 24
0
    y_pred = K.constant(y_pred) if not K.is_tensor(y_pred) else y_pred
    y_true = K.cast(y_true, y_pred.dtype)

    if label_smoothing is not 0:
        smoothing = K.cast_to_floatx(label_smoothing)

        def _smooth_labels():
            num_classes = K.cast(K.shape(y_true)[1], y_pred.dtype)
            return y_true * (1.0 - smoothing) + (smoothing / num_classes)

        y_true = K.switch(K.greater(smoothing, 0), _smooth_labels,
                          lambda: y_true)
    return K.categorical_crossentropy(y_true, y_pred, from_logits=from_logits)


inputs = Input((PIXEL, PIXEL, 3))
s = Lambda(lambda x: x / 255)(inputs)
conv1 = Conv2D(8,
               3,
               activation='relu',
               padding='same',
               kernel_initializer='he_normal')(s)
pool1 = AveragePooling2D(pool_size=(2, 2))(conv1)  # 16

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

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

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

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

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

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

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

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

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

    model.load_weights(weights_path, by_name=True)

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

    model = Model(img_input, x_newfc)

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

    return model
def build_model(image_size,
                n_classes,
                mode='training',
                l2_regularization=0.0,
                min_scale=0.1,
                max_scale=0.9,
                scales=None,
                aspect_ratios_global=[0.5, 1.0, 2.0],
                aspect_ratios_per_layer=None,
                two_boxes_for_ar1=True,
                steps=None,
                offsets=None,
                clip_boxes=False,
                variances=[1.0, 1.0, 1.0, 1.0],
                coords='centroids',
                normalize_coords=False,
                subtract_mean=None,
                divide_by_stddev=None,
                swap_channels=False,
                confidence_thresh=0.01,
                iou_threshold=0.45,
                top_k=200,
                nms_max_output_size=400,
                return_predictor_sizes=False):
    '''
    Build a Keras model with SSD architecture, see references.

    The model consists of convolutional feature layers and a number of convolutional
    predictor layers that take their input from different feature layers.
    The model is fully convolutional.

    The implementation found here is a smaller version of the original architecture
    used in the paper (where the base network consists of a modified VGG-16 extended
    by a few convolutional feature layers), but of course it could easily be changed to
    an arbitrarily large SSD architecture by following the general design pattern used here.
    This implementation has 7 convolutional layers and 4 convolutional predictor
    layers that take their input from layers 4, 5, 6, and 7, respectively.

    Most of the arguments that this function takes are only needed for the anchor
    box layers. In case you're training the network, the parameters passed here must
    be the same as the ones used to set up `SSDBoxEncoder`. In case you're loading
    trained weights, the parameters passed here must be the same as the ones used
    to produce the trained weights.

    Some of these arguments are explained in more detail in the documentation of the
    `SSDBoxEncoder` class.

    Note: Requires Keras v2.0 or later. Training currently works only with the
    TensorFlow backend (v1.0 or later).

    Arguments:
        image_size (tuple): The input image size in the format `(height, width, channels)`.
        n_classes (int): The number of positive classes, e.g. 20 for Pascal VOC, 80 for MS COCO.
        mode (str, optional): One of 'training', 'inference' and 'inference_fast'. In 'training' mode,
            the model outputs the raw prediction tensor, while in 'inference' and 'inference_fast' modes,
            the raw predictions are decoded into absolute coordinates and filtered via confidence thresholding,
            non-maximum suppression, and top-k filtering. The difference between latter two modes is that
            'inference' follows the exact procedure of the original Caffe implementation, while
            'inference_fast' uses a faster prediction decoding procedure.
        l2_regularization (float, optional): The L2-regularization rate. Applies to all convolutional layers.
        min_scale (float, optional): The smallest scaling factor for the size of the anchor boxes as a fraction
            of the shorter side of the input images.
        max_scale (float, optional): The largest scaling factor for the size of the anchor boxes as a fraction
            of the shorter side of the input images. All scaling factors between the smallest and the
            largest will be linearly interpolated. Note that the second to last of the linearly interpolated
            scaling factors will actually be the scaling factor for the last predictor layer, while the last
            scaling factor is used for the second box for aspect ratio 1 in the last predictor layer
            if `two_boxes_for_ar1` is `True`.
        scales (list, optional): A list of floats containing scaling factors per convolutional predictor layer.
            This list must be one element longer than the number of predictor layers. The first `k` elements are the
            scaling factors for the `k` predictor layers, while the last element is used for the second box
            for aspect ratio 1 in the last predictor layer if `two_boxes_for_ar1` is `True`. This additional
            last scaling factor must be passed either way, even if it is not being used. If a list is passed,
            this argument overrides `min_scale` and `max_scale`. All scaling factors must be greater than zero.
        aspect_ratios_global (list, optional): The list of aspect ratios for which anchor boxes are to be
            generated. This list is valid for all predictor layers. The original implementation uses more aspect ratios
            for some predictor layers and fewer for others. If you want to do that, too, then use the next argument instead.
        aspect_ratios_per_layer (list, optional): A list containing one aspect ratio list for each predictor layer.
            This allows you to set the aspect ratios for each predictor layer individually. If a list is passed,
            it overrides `aspect_ratios_global`.
        two_boxes_for_ar1 (bool, optional): Only relevant for aspect ratio lists that contain 1. Will be ignored otherwise.
            If `True`, two anchor boxes will be generated for aspect ratio 1. The first will be generated
            using the scaling factor for the respective layer, the second one will be generated using
            geometric mean of said scaling factor and next bigger scaling factor.
        steps (list, optional): `None` or a list with as many elements as there are predictor layers. The elements can be
            either ints/floats or tuples of two ints/floats. These numbers represent for each predictor layer how many
            pixels apart the anchor box center points should be vertically and horizontally along the spatial grid over
            the image. If the list contains ints/floats, then that value will be used for both spatial dimensions.
            If the list contains tuples of two ints/floats, then they represent `(step_height, step_width)`.
            If no steps are provided, then they will be computed such that the anchor box center points will form an
            equidistant grid within the image dimensions.
        offsets (list, optional): `None` or a list with as many elements as there are predictor layers. The elements can be
            either floats or tuples of two floats. These numbers represent for each predictor layer how many
            pixels from the top and left boarders of the image the top-most and left-most anchor box center points should be
            as a fraction of `steps`. The last bit is important: The offsets are not absolute pixel values, but fractions
            of the step size specified in the `steps` argument. If the list contains floats, then that value will
            be used for both spatial dimensions. If the list contains tuples of two floats, then they represent
            `(vertical_offset, horizontal_offset)`. If no offsets are provided, then they will default to 0.5 of the step size,
            which is also the recommended setting.
        clip_boxes (bool, optional): If `True`, clips the anchor box coordinates to stay within image boundaries.
        variances (list, optional): A list of 4 floats >0. The anchor box offset for each coordinate will be divided by
            its respective variance value.
        coords (str, optional): The box coordinate format to be used internally by the model (i.e. this is not the input format
            of the ground truth labels). Can be either 'centroids' for the format `(cx, cy, w, h)` (box center coordinates, width,
            and height), 'minmax' for the format `(xmin, xmax, ymin, ymax)`, or 'corners' for the format `(xmin, ymin, xmax, ymax)`.
        normalize_coords (bool, optional): Set to `True` if the model is supposed to use relative instead of absolute coordinates,
            i.e. if the model predicts box coordinates within [0,1] instead of absolute coordinates.
        subtract_mean (array-like, optional): `None` or an array-like object of integers or floating point values
            of any shape that is broadcast-compatible with the image shape. The elements of this array will be
            subtracted from the image pixel intensity values. For example, pass a list of three integers
            to perform per-channel mean normalization for color images.
        divide_by_stddev (array-like, optional): `None` or an array-like object of non-zero integers or
            floating point values of any shape that is broadcast-compatible with the image shape. The image pixel
            intensity values will be divided by the elements of this array. For example, pass a list
            of three integers to perform per-channel standard deviation normalization for color images.
        swap_channels (list, optional): Either `False` or a list of integers representing the desired order in which the input
            image channels should be swapped.
        confidence_thresh (float, optional): A float in [0,1), the minimum classification confidence in a specific
            positive class in order to be considered for the non-maximum suppression stage for the respective class.
            A lower value will result in a larger part of the selection process being done by the non-maximum suppression
            stage, while a larger value will result in a larger part of the selection process happening in the confidence
            thresholding stage.
        iou_threshold (float, optional): A float in [0,1]. All boxes that have a Jaccard similarity of greater than `iou_threshold`
            with a locally maximal box will be removed from the set of predictions for a given class, where 'maximal' refers
            to the box's confidence score.
        top_k (int, optional): The number of highest scoring predictions to be kept for each batch item after the
            non-maximum suppression stage.
        nms_max_output_size (int, optional): The maximal number of predictions that will be left over after the NMS stage.
        return_predictor_sizes (bool, optional): If `True`, this function not only returns the model, but also
            a list containing the spatial dimensions of the predictor layers. This isn't strictly necessary since
            you can always get their sizes easily via the Keras API, but it's convenient and less error-prone
            to get them this way. They are only relevant for training anyway (SSDBoxEncoder needs to know the
            spatial dimensions of the predictor layers), for inference you don't need them.

    Returns:
        model: The Keras SSD model.
        predictor_sizes (optional): A Numpy array containing the `(height, width)` portion
            of the output tensor shape for each convolutional predictor layer. During
            training, the generator function needs this in order to transform
            the ground truth labels into tensors of identical structure as the
            output tensors of the model, which is in turn needed for the cost
            function.

    References:
        https://arxiv.org/abs/1512.02325v5
    '''

    n_predictor_layers = 4 # The number of predictor conv layers in the network
    n_classes += 1 # Account for the background class.
    l2_reg = l2_regularization # Make the internal name shorter.
    img_height, img_width, img_channels = image_size[0], image_size[1], image_size[2]

    ############################################################################
    # Get a few exceptions out of the way.
    ############################################################################

    if aspect_ratios_global is None and aspect_ratios_per_layer is None:
        raise ValueError("`aspect_ratios_global` and `aspect_ratios_per_layer` cannot both be None. At least one needs to be specified.")
    if aspect_ratios_per_layer:
        if len(aspect_ratios_per_layer) != n_predictor_layers:
            raise ValueError("It must be either aspect_ratios_per_layer is None or len(aspect_ratios_per_layer) == {}, but len(aspect_ratios_per_layer) == {}.".format(n_predictor_layers, len(aspect_ratios_per_layer)))

    if (min_scale is None or max_scale is None) and scales is None:
        raise ValueError("Either `min_scale` and `max_scale` or `scales` need to be specified.")
    if scales:
        if len(scales) != n_predictor_layers+1:
            raise ValueError("It must be either scales is None or len(scales) == {}, but len(scales) == {}.".format(n_predictor_layers+1, len(scales)))
    else: # If no explicit list of scaling factors was passed, compute the list of scaling factors from `min_scale` and `max_scale`
        scales = np.linspace(min_scale, max_scale, n_predictor_layers+1)

    if len(variances) != 4: # We need one variance value for each of the four box coordinates
        raise ValueError("4 variance values must be pased, but {} values were received.".format(len(variances)))
    variances = np.array(variances)
    if np.any(variances <= 0):
        raise ValueError("All variances must be >0, but the variances given are {}".format(variances))

    if (not (steps is None)) and (len(steps) != n_predictor_layers):
        raise ValueError("You must provide at least one step value per predictor layer.")

    if (not (offsets is None)) and (len(offsets) != n_predictor_layers):
        raise ValueError("You must provide at least one offset value per predictor layer.")

    ############################################################################
    # Compute the anchor box parameters.
    ############################################################################

    # Set the aspect ratios for each predictor layer. These are only needed for the anchor box layers.
    if aspect_ratios_per_layer:
        aspect_ratios = aspect_ratios_per_layer
    else:
        aspect_ratios = [aspect_ratios_global] * n_predictor_layers

    # Compute the number of boxes to be predicted per cell for each predictor layer.
    # We need this so that we know how many channels the predictor layers need to have.
    if aspect_ratios_per_layer:
        n_boxes = []
        for ar in aspect_ratios_per_layer:
            if (1 in ar) & two_boxes_for_ar1:
                n_boxes.append(len(ar) + 1) # +1 for the second box for aspect ratio 1
            else:
                n_boxes.append(len(ar))
    else: # If only a global aspect ratio list was passed, then the number of boxes is the same for each predictor layer
        if (1 in aspect_ratios_global) & two_boxes_for_ar1:
            n_boxes = len(aspect_ratios_global) + 1
        else:
            n_boxes = len(aspect_ratios_global)
        n_boxes = [n_boxes] * n_predictor_layers

    if steps is None:
        steps = [None] * n_predictor_layers
    if offsets is None:
        offsets = [None] * n_predictor_layers

    ############################################################################
    # Define functions for the Lambda layers below.
    ############################################################################

    def identity_layer(tensor):
        return tensor

    def input_mean_normalization(tensor):
        return tensor - np.array(subtract_mean)

    def input_stddev_normalization(tensor):
        return tensor / np.array(divide_by_stddev)

    def input_channel_swap(tensor):
        if len(swap_channels) == 3:
            return K.stack([tensor[...,swap_channels[0]], tensor[...,swap_channels[1]], tensor[...,swap_channels[2]]], axis=-1)
        elif len(swap_channels) == 4:
            return K.stack([tensor[...,swap_channels[0]], tensor[...,swap_channels[1]], tensor[...,swap_channels[2]], tensor[...,swap_channels[3]]], axis=-1)

    ############################################################################
    # Build the network.
    ############################################################################

    x = Input(shape=(img_height, img_width, img_channels))

    # The following identity layer is only needed so that the subsequent lambda layers can be optional.
    x1 = Lambda(identity_layer, output_shape=(img_height, img_width, img_channels), name='identity_layer')(x)
    if not (subtract_mean is None):
        x1 = Lambda(input_mean_normalization, output_shape=(img_height, img_width, img_channels), name='input_mean_normalization')(x1)
    if not (divide_by_stddev is None):
        x1 = Lambda(input_stddev_normalization, output_shape=(img_height, img_width, img_channels), name='input_stddev_normalization')(x1)
    if swap_channels:
        x1 = Lambda(input_channel_swap, output_shape=(img_height, img_width, img_channels), name='input_channel_swap')(x1)

    conv1 = Conv2D(32, (5, 5), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv1')(x1)
    conv1 = BatchNormalization(axis=3, momentum=0.99, name='bn1')(conv1) # Tensorflow uses filter format [filter_height, filter_width, in_channels, out_channels], hence axis = 3
    conv1 = ELU(name='elu1')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2), name='pool1')(conv1)

    conv2 = Conv2D(48, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv2')(pool1)
    conv2 = BatchNormalization(axis=3, momentum=0.99, name='bn2')(conv2)
    conv2 = ELU(name='elu2')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2), name='pool2')(conv2)

    conv3 = Conv2D(64, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv3')(pool2)
    conv3 = BatchNormalization(axis=3, momentum=0.99, name='bn3')(conv3)
    conv3 = ELU(name='elu3')(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2), name='pool3')(conv3)

    conv4 = Conv2D(64, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv4')(pool3)
    conv4 = BatchNormalization(axis=3, momentum=0.99, name='bn4')(conv4)
    conv4 = ELU(name='elu4')(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2), name='pool4')(conv4)

    conv5 = Conv2D(48, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv5')(pool4)
    conv5 = BatchNormalization(axis=3, momentum=0.99, name='bn5')(conv5)
    conv5 = ELU(name='elu5')(conv5)
    pool5 = MaxPooling2D(pool_size=(2, 2), name='pool5')(conv5)

    conv6 = Conv2D(48, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv6')(pool5)
    conv6 = BatchNormalization(axis=3, momentum=0.99, name='bn6')(conv6)
    conv6 = ELU(name='elu6')(conv6)
    pool6 = MaxPooling2D(pool_size=(2, 2), name='pool6')(conv6)

    conv7 = Conv2D(32, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='conv7')(pool6)
    conv7 = BatchNormalization(axis=3, momentum=0.99, name='bn7')(conv7)
    conv7 = ELU(name='elu7')(conv7)

    # The next part is to add the convolutional predictor layers on top of the base network
    # that we defined above. Note that I use the term "base network" differently than the paper does.
    # To me, the base network is everything that is not convolutional predictor layers or anchor
    # box layers. In this case we'll have four predictor layers, but of course you could
    # easily rewrite this into an arbitrarily deep base network and add an arbitrary number of
    # predictor layers on top of the base network by simply following the pattern shown here.

    # Build the convolutional predictor layers on top of conv layers 4, 5, 6, and 7.
    # We build two predictor layers on top of each of these layers: One for class prediction (classification), one for box coordinate prediction (localization)
    # We precidt `n_classes` confidence values for each box, hence the `classes` predictors have depth `n_boxes * n_classes`
    # We predict 4 box coordinates for each box, hence the `boxes` predictors have depth `n_boxes * 4`
    # Output shape of `classes`: `(batch, height, width, n_boxes * n_classes)`
    classes4 = Conv2D(n_boxes[0] * n_classes, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='classes4')(conv4)
    classes5 = Conv2D(n_boxes[1] * n_classes, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='classes5')(conv5)
    classes6 = Conv2D(n_boxes[2] * n_classes, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='classes6')(conv6)
    classes7 = Conv2D(n_boxes[3] * n_classes, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='classes7')(conv7)
    # Output shape of `boxes`: `(batch, height, width, n_boxes * 4)`
    boxes4 = Conv2D(n_boxes[0] * 4, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='boxes4')(conv4)
    boxes5 = Conv2D(n_boxes[1] * 4, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='boxes5')(conv5)
    boxes6 = Conv2D(n_boxes[2] * 4, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='boxes6')(conv6)
    boxes7 = Conv2D(n_boxes[3] * 4, (3, 3), strides=(1, 1), padding="same", kernel_initializer='he_normal', kernel_regularizer=l2(l2_reg), name='boxes7')(conv7)

    # Generate the anchor boxes
    # Output shape of `anchors`: `(batch, height, width, n_boxes, 8)`
    anchors4 = AnchorBoxes(img_height, img_width, this_scale=scales[0], next_scale=scales[1], aspect_ratios=aspect_ratios[0],
                           two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[0], this_offsets=offsets[0],
                           clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='anchors4')(boxes4)
    anchors5 = AnchorBoxes(img_height, img_width, this_scale=scales[1], next_scale=scales[2], aspect_ratios=aspect_ratios[1],
                           two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[1], this_offsets=offsets[1],
                           clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='anchors5')(boxes5)
    anchors6 = AnchorBoxes(img_height, img_width, this_scale=scales[2], next_scale=scales[3], aspect_ratios=aspect_ratios[2],
                           two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[2], this_offsets=offsets[2],
                           clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='anchors6')(boxes6)
    anchors7 = AnchorBoxes(img_height, img_width, this_scale=scales[3], next_scale=scales[4], aspect_ratios=aspect_ratios[3],
                           two_boxes_for_ar1=two_boxes_for_ar1, this_steps=steps[3], this_offsets=offsets[3],
                           clip_boxes=clip_boxes, variances=variances, coords=coords, normalize_coords=normalize_coords, name='anchors7')(boxes7)

    # Reshape the class predictions, yielding 3D tensors of shape `(batch, height * width * n_boxes, n_classes)`
    # We want the classes isolated in the last axis to perform softmax on them
    classes4_reshaped = Reshape((-1, n_classes), name='classes4_reshape')(classes4)
    classes5_reshaped = Reshape((-1, n_classes), name='classes5_reshape')(classes5)
    classes6_reshaped = Reshape((-1, n_classes), name='classes6_reshape')(classes6)
    classes7_reshaped = Reshape((-1, n_classes), name='classes7_reshape')(classes7)
    # Reshape the box coordinate predictions, yielding 3D tensors of shape `(batch, height * width * n_boxes, 4)`
    # We want the four box coordinates isolated in the last axis to compute the smooth L1 loss
    boxes4_reshaped = Reshape((-1, 4), name='boxes4_reshape')(boxes4)
    boxes5_reshaped = Reshape((-1, 4), name='boxes5_reshape')(boxes5)
    boxes6_reshaped = Reshape((-1, 4), name='boxes6_reshape')(boxes6)
    boxes7_reshaped = Reshape((-1, 4), name='boxes7_reshape')(boxes7)
    # Reshape the anchor box tensors, yielding 3D tensors of shape `(batch, height * width * n_boxes, 8)`
    anchors4_reshaped = Reshape((-1, 8), name='anchors4_reshape')(anchors4)
    anchors5_reshaped = Reshape((-1, 8), name='anchors5_reshape')(anchors5)
    anchors6_reshaped = Reshape((-1, 8), name='anchors6_reshape')(anchors6)
    anchors7_reshaped = Reshape((-1, 8), name='anchors7_reshape')(anchors7)

    # Concatenate the predictions from the different layers and the assosciated anchor box tensors
    # Axis 0 (batch) and axis 2 (n_classes or 4, respectively) are identical for all layer predictions,
    # so we want to concatenate along axis 1
    # Output shape of `classes_concat`: (batch, n_boxes_total, n_classes)
    classes_concat = Concatenate(axis=1, name='classes_concat')([classes4_reshaped,
                                                                 classes5_reshaped,
                                                                 classes6_reshaped,
                                                                 classes7_reshaped])

    # Output shape of `boxes_concat`: (batch, n_boxes_total, 4)
    boxes_concat = Concatenate(axis=1, name='boxes_concat')([boxes4_reshaped,
                                                             boxes5_reshaped,
                                                             boxes6_reshaped,
                                                             boxes7_reshaped])

    # Output shape of `anchors_concat`: (batch, n_boxes_total, 8)
    anchors_concat = Concatenate(axis=1, name='anchors_concat')([anchors4_reshaped,
                                                                 anchors5_reshaped,
                                                                 anchors6_reshaped,
                                                                 anchors7_reshaped])

    # The box coordinate predictions will go into the loss function just the way they are,
    # but for the class predictions, we'll apply a softmax activation layer first
    classes_softmax = Activation('softmax', name='classes_softmax')(classes_concat)

    # Concatenate the class and box coordinate predictions and the anchors to one large predictions tensor
    # Output shape of `predictions`: (batch, n_boxes_total, n_classes + 4 + 8)
    predictions = Concatenate(axis=2, name='predictions')([classes_softmax, boxes_concat, anchors_concat])

    if mode == 'training':
        model = Model(inputs=x, outputs=predictions)
    elif mode == 'inference':
        decoded_predictions = DecodeDetections(confidence_thresh=confidence_thresh,
                                               iou_threshold=iou_threshold,
                                               top_k=top_k,
                                               nms_max_output_size=nms_max_output_size,
                                               coords=coords,
                                               normalize_coords=normalize_coords,
                                               img_height=img_height,
                                               img_width=img_width,
                                               name='decoded_predictions')(predictions)
        model = Model(inputs=x, outputs=decoded_predictions)
    elif mode == 'inference_fast':
        decoded_predictions = DecodeDetectionsFast(confidence_thresh=confidence_thresh,
                                                   iou_threshold=iou_threshold,
                                                   top_k=top_k,
                                                   nms_max_output_size=nms_max_output_size,
                                                   coords=coords,
                                                   normalize_coords=normalize_coords,
                                                   img_height=img_height,
                                                   img_width=img_width,
                                                   name='decoded_predictions')(predictions)
        model = Model(inputs=x, outputs=decoded_predictions)
    else:
        raise ValueError("`mode` must be one of 'training', 'inference' or 'inference_fast', but received '{}'.".format(mode))

    
    # The spatial dimensions are the same for the `classes` and `boxes` predictor layers.
    predictor_sizes = np.array([classes4._keras_shape[1:3],
                                classes5._keras_shape[1:3],
                                classes6._keras_shape[1:3],
                                classes7._keras_shape[1:3]])
    return model, predictor_sizes
Exemplo n.º 27
0
def predictImg(uploaded_file,config_filename = 'config_ui.pickle'):
    
    #loadFiles(config_filename)
    #st.write('Prediction function')
    
    sys.setrecursionlimit(40000)

    config = tf.compat.v1.ConfigProto()
    config.gpu_options.allow_growth = True
    config.log_device_placement = True
    sess = tf.compat.v1.Session(config=config)
    K.set_session(sess)

    num_rois = 4
    #config_filename = 'config_ui.pickle'
    network = 'resnet50'

    config_output_filename = config_filename
    
    with open(config_output_filename, 'rb') as f_in:
        C = pickle.load(f_in)
    
    if C.network == 'resnet50':
        import keras_frcnn.resnet as nn

    
    # turn off any data augmentation at test time
    C.use_horizontal_flips = False
    C.use_vertical_flips = False
    C.rot_90 = False
    
    #img_path = test_path
    
    class_mapping = C.class_mapping

    if 'bg' not in class_mapping:
        class_mapping['bg'] = len(class_mapping)
    
    class_mapping = {v: k for k, v in class_mapping.items()}
    print(class_mapping)
    class_to_color = {class_mapping[v]: np.random.randint(0, 255, 3) for v in class_mapping}
    
    C.num_rois = int(num_rois)
    
    if C.network == 'resnet50':
        num_features = 1024
    elif C.network == 'vgg':
        num_features = 512
    
    if K.image_data_format() == 'channels_first':
        input_shape_img = (3, None, None)
        input_shape_features = (num_features, None, None)
    else:
        input_shape_img = (None, None, 3)
        input_shape_features = (None, None, num_features)


    img_input = Input(shape=input_shape_img)
    roi_input = Input(shape=(C.num_rois, 4))
    feature_map_input = Input(shape=input_shape_features)
    
    
    # define the base network (resnet here, can be VGG, Inception, etc)
    shared_layers = nn.nn_base(img_input, trainable=True)
    
    # define the RPN, built on the base layers
    num_anchors = len(C.anchor_box_scales) * len(C.anchor_box_ratios)
    rpn_layers = nn.rpn(shared_layers, num_anchors)
    
    classifier = nn.classifier(feature_map_input, roi_input, C.num_rois, nb_classes=len(class_mapping), trainable=True)
    
    model_rpn = Model(img_input, rpn_layers)
    model_classifier_only = Model([feature_map_input, roi_input], classifier)
    
    model_classifier = Model([feature_map_input, roi_input], classifier)
    
    st.write(f'Loading weights from {C.model_path}')
    model_rpn.load_weights(C.model_path, by_name=True)
    model_classifier.load_weights(C.model_path, by_name=True)
    
    model_rpn.compile(optimizer='sgd', loss='mse')
    model_classifier.compile(optimizer='sgd', loss='mse')
    
    all_imgs = []

    classes = {}
    
    bbox_threshold = 0.8
    
    visualise = True
    
    set_Overlap_threshold = 0.3
    #progress_bar = st.sidebar.progress(0.0)
    progress_bar = st.progress(0.0)
    
    with st.spinner('Wait for it...'):
            
        for idx, img in enumerate(uploaded_file):
            progress_bar.progress((idx + 0.1) / len(uploaded_file))
            starttime = time.time()
            img = convert_from_image_to_cv2(Image.open(img))
            #img = cv2.imread(img)
        
            X, ratio = format_img(img, C)
        
            if K.image_data_format() == 'channels_last':
                X = np.transpose(X, (0, 2, 3, 1))
        
            # get the feature maps and output from the RPN
            [Y1, Y2, F] = model_rpn.predict(X)
            
        
            R = roi_helpers.rpn_to_roi(Y1, Y2, C, K.image_data_format(), overlap_thresh=0.5)#0.7
        
            # convert from (x1,y1,x2,y2) to (x,y,w,h)
            R[:, 2] -= R[:, 0]
            R[:, 3] -= R[:, 1]
        
            # apply the spatial pyramid pooling to the proposed regions
            bboxes = {}
            probs = {}
        
            for jk in range(R.shape[0]//C.num_rois + 1):
                ROIs = np.expand_dims(R[C.num_rois*jk:C.num_rois*(jk+1), :], axis=0)
                if ROIs.shape[1] == 0:
                    print("ROI Shape: ",ROIs.shape[1])
                    break
        
                if jk == R.shape[0]//C.num_rois:
                    #pad R
                    curr_shape = ROIs.shape
                    target_shape = (curr_shape[0],C.num_rois,curr_shape[2])
                    ROIs_padded = np.zeros(target_shape).astype(ROIs.dtype)
                    ROIs_padded[:, :curr_shape[1], :] = ROIs
                    ROIs_padded[0, curr_shape[1]:, :] = ROIs[0, 0, :]
                    ROIs = ROIs_padded
        
                [P_cls, P_regr] = model_classifier_only.predict([F, ROIs])
        
                for ii in range(P_cls.shape[1]):
                    #print("np max:",np.max(P_cls[0, ii, :]))
                    #print("np argmax:",np.argmax(P_cls[0, ii, :]))
                    #print(np.max(P_cls[0, ii, :]) < bbox_threshold)
                    if np.max(P_cls[0, ii, :]) < bbox_threshold or np.argmax(P_cls[0, ii, :]) == (P_cls.shape[2] - 1):
                        continue
        
                    cls_name = class_mapping[np.argmax(P_cls[0, ii, :])]
                    #print('class name:',cls_name)
                    if cls_name not in bboxes:
                        bboxes[cls_name] = []
                        probs[cls_name] = []
        
                    (x, y, w, h) = ROIs[0, ii, :]
        
                    cls_num = np.argmax(P_cls[0, ii, :])
                    try:
                        (tx, ty, tw, th) = P_regr[0, ii, 4*cls_num:4*(cls_num+1)]
                        tx /= C.classifier_regr_std[0]
                        ty /= C.classifier_regr_std[1]
                        tw /= C.classifier_regr_std[2]
                        th /= C.classifier_regr_std[3]
                        x, y, w, h = roi_helpers.apply_regr(x, y, w, h, tx, ty, tw, th)
                    except:
                        pass
                    bboxes[cls_name].append([C.rpn_stride*x, C.rpn_stride*y, C.rpn_stride*(x+w), C.rpn_stride*(y+h)])
                    probs[cls_name].append(np.max(P_cls[0, ii, :]))
        
            all_dets = []
        
            for key in bboxes:
                bbox = np.array(bboxes[key])
        
                new_boxes, new_probs = roi_helpers.non_max_suppression_fast(bbox, np.array(probs[key]), overlap_thresh=set_Overlap_threshold)
                for jk in range(new_boxes.shape[0]):
                    (x1, y1, x2, y2) = new_boxes[jk,:]
        
                    (real_x1, real_y1, real_x2, real_y2) = get_real_coordinates(ratio, x1, y1, x2, y2)
        
                    cv2.rectangle(img,(real_x1, real_y1), (real_x2, real_y2), (int(class_to_color[key][0]), int(class_to_color[key][1]), int(class_to_color[key][2])),2)
        
                    textLabel = f'{key}: {int(100*new_probs[jk])}'
                    all_dets.append((key,100*new_probs[jk]))
        
                    (retval,baseLine) = cv2.getTextSize(textLabel,cv2.FONT_HERSHEY_COMPLEX,1,1)
                    textOrg = (real_x1, real_y1-0)
        
                    cv2.rectangle(img, (textOrg[0] - 5, textOrg[1]+baseLine - 5), (textOrg[0]+retval[0] + 5, textOrg[1]-retval[1] - 5), (0, 0, 0), 2)
                    cv2.rectangle(img, (textOrg[0] - 5,textOrg[1]+baseLine - 5), (textOrg[0]+retval[0] + 5, textOrg[1]-retval[1] - 5), (255, 255, 255), -1)
                    cv2.putText(img, textLabel, textOrg, cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 0), 1)
        
            st.write(f'Elapsed time = {time.time() - starttime}')
            st.write(all_dets)
            plt.figure(figsize=(10,10))
            plt.grid()
            plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
            st.image(img, use_column_width=True,clamp = True)
            #plt.show()
            
    progress_bar.empty()    
def get_modeltt():
    
    input_layer = Input(shape=(2, 205, 1))
    first_conv_b = Conv2D(filters=200, kernel_size=(1, 20), strides=(1, 5), activation='relu')(input_layer)
    first_conv_c = Conv2D(filters=200, kernel_size=(2, 5), padding='same', activation='relu')(input_layer)
    first_layer_concat = concatenate([first_conv_b, first_conv_c], axis=2)
    batch_normalization_1 = BatchNormalization()(first_layer_concat)

    second_conv_a = Conv2D(filters=100, kernel_size=(1, 11),strides=(1, 11), activation='relu')(batch_normalization_1)
    second_conv_b = Conv2D(filters=100, kernel_size=(2, 4), padding='same', activation='relu')(batch_normalization_1)
    second_layer_concat = concatenate([second_conv_a, second_conv_b], axis=2)
    batch_normalization_2 = BatchNormalization()(second_layer_concat)

    third_conv_a = Conv2D(filters=100, kernel_size=(1, 3), strides=(1, 3), activation='relu')(batch_normalization_2)
    third_conv_b = Conv2D(filters=100, kernel_size=(2, 4), padding='same', activation='relu')(batch_normalization_2)
    third_conv_c = Conv2D(filters=100, kernel_size=(1, 5), activation='relu')(batch_normalization_2)
    third_layer_concat = concatenate([third_conv_a, third_conv_b, third_conv_c], axis=2)
    fourth_conv = Conv2D(filters=200, kernel_size=(1, 4), padding='same', activation='relu')(third_layer_concat)
    batch_normalization_3 = BatchNormalization()(fourth_conv)
    seventh_a_conv = Conv2D(filters=200, kernel_size=(1,3), strides=(1, 3), activation='relu')(batch_normalization_3)
    batch_normalization_4 = BatchNormalization()(seventh_a_conv)
    # score branch


    score_conv_0 = Conv2D(filters=100, kernel_size=(1, 5), activation='relu')(batch_normalization_4)
    #ht score
    score_conv_3 = Conv2D(filters=100, kernel_size=(1, 4), padding='same', activation='relu')(score_conv_0)
    score_conv_4 = Conv2D(filters=100, kernel_size=(1, 2), padding='same', activation='relu')(score_conv_3)
    score_conv_5 = Conv2D(filters=100, kernel_size=(1, 3), strides=(1, 3), activation='relu')(score_conv_4)
    batch_score_l = BatchNormalization()(score_conv_5)
    score_conv_6 = Conv2D(filters=100, kernel_size=(1, 4), strides=(1, 2), activation='relu')(batch_score_l)

    flat_h = Flatten()(score_conv_4)
    dense_h_1 = Dense(100, activation='relu')(flat_h)
    dropout_ht_1 = Dropout(0.5)(dense_h_1)
    output_0 = Dense(8, activation='softmax', name='ht_score')(dropout_ht_1)

    #ft score
    score_conv_1 = Conv2D(filters=200, kernel_size=(1, 4), padding='same', activation='relu')(score_conv_0)
    score_conv_2 = Conv2D(filters=200, kernel_size=(1, 2), strides=(1, 2), activation='relu')(score_conv_1)
    score_conv_1_a = Conv2D(filters=200, kernel_size=(2, 2), activation='relu')(score_conv_2)
    score_conv_1_b = Conv2D(filters=200, kernel_size=(1, 4), strides=(1, 2), activation='relu')(score_conv_1_a)
    score_conv_1_c = Conv2D(filters=200, kernel_size=(1, 3), strides=(1, 2), activation='relu')(score_conv_1_b)
    score_conv_1_d = Conv2D(filters=100, kernel_size=(1, 2), strides=(1, 2), activation='relu')(score_conv_1_c)


    flat = Flatten()(score_conv_1_d)
    dense_1 = Dense(100, activation='relu')(flat)
    dl = Dropout(0.5)(dense_1)
    dense_2 = Dense(60, activation='relu')(dl)
    dl_2 = Dropout(0.5)(dense_2)
    output_1 = Dense(8, activation='softmax', name='ft_score')(dl_2)

    # winner branch
    w_fourth_conv = Conv2D(filters=50, kernel_size=(1, 3), activation='relu')(batch_normalization_4)
    w_batch_normalization_2 = BatchNormalization()(w_fourth_conv)
    w_fourth_pooling = MaxPool2D(pool_size=(1, 3))(w_batch_normalization_2)
    w_fifth_conv = Conv2D(filters=100, kernel_size=(2, 4), padding='same', activation='relu')(w_fourth_pooling)
    flat_w = Flatten()(w_fifth_conv)

    # ht winner
    dense_w_1 = Dense(100, activation='relu')(flat_w)
    dl_w = Dropout(0.5)(dense_w_1)
    output_2 = Dense(3, activation='softmax', name='ht_winner')(dl_w)

    # ft winner 
    dense_w2_1 = Dense(100, activation='relu')(flat_w)
    dl_w_2 = Dropout(0.5)(dense_w2_1)
    output_3 = Dense(3, activation='softmax', name='ft_winner')(dl_w_2)


    model = Model(inputs=(input_layer,), outputs=(output_0, output_1, output_2, output_3 ))
    s = SGD(lr=3e-3, momentum=0.90, decay=0.99, nesterov=True)
    opt = Adam(lr=1e-4)
    losses = {
        'ht_winner' : 'categorical_crossentropy',
        'ft_winner' : 'categorical_crossentropy',
        'ht_score' : 'categorical_crossentropy',
        'ft_score' : 'categorical_crossentropy',
    }

    model.compile(opt, loss=losses, metrics=['acc'])
    return model
Exemplo n.º 29
0
def nn_base(input_tensor=None, trainable=False):

    # Determine proper input shape
    if K.image_dim_ordering() == 'th':
        input_shape = (3, None, None)
    else:
        input_shape = (None, None, 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

    if K.image_dim_ordering() == 'tf':
        bn_axis = 3
    else:
        bn_axis = 1

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

    return x
Exemplo n.º 30
0
    def __init__(self):

        K.set_image_data_format('channels_last')  # set format
        self.DEBUG = 1

        # Input shape
        self.img_rows = 256
        self.img_cols = 256
        self.img_vols = 256
        self.channels = 1
        self.batch_sz = 1  # for testing locally to avoid memory allocation

        self.crop_size = (self.img_rows, self.img_cols, self.img_vols)

        self.img_shape = self.crop_size + (self.channels, )

        # Calculate output shape of D (PatchGAN)
        patch = int(self.img_rows / 2**4)
        self.output_shape_d = (patch, patch, patch, self.channels)
        self.output_shape_g = self.crop_size + (
            3, )  # phi has three outputs. one for each X, Y, and Z dimensions

        # Number of filters in the first layer of G and D
        self.gf = 16
        self.df = 16

        optimizer = Adam(0.001, 0.5)

        # Build and compile the discriminator
        self.discriminator = self.build_discriminator()
        self.discriminator.summary()
        self.discriminator.compile(loss='mse',
                                   optimizer=optimizer,
                                   metrics=['accuracy'])

        # -------------------------
        # Construct Computational
        #   Graph of Generator
        # -------------------------

        # Build the generator
        self.generator = self.build_generator()
        self.generator.summary()

        self.transformation = self.build_transformation()
        self.transformation.summary()

        # Input images and their conditioning images
        img_S = Input(shape=self.img_shape)
        img_T = Input(shape=self.img_shape)

        # Generate the deformable funtion
        phi = self.generator([img_S, img_T])
        # Transform S
        warped_S = self.transformation([img_S, phi])
        # For the combined model we will only train the generator
        self.discriminator.trainable = False

        # Discriminators determines validity of translated images / condition pairs
        validity = self.discriminator([warped_S, img_T])

        self.combined = Model(inputs=[img_S, img_T],
                              outputs=[validity, warped_S])
        self.combined.summary()
        # self.combined.compile(loss=['mse', 'mae'],
        #                       loss_weights=[1, 100],
        #                       optimizer=optimizer)

        self.combined.compile(loss=['mse', 'mae'],
                              loss_weights=[50, 50],
                              optimizer=optimizer)

        if self.DEBUG:
            log_path = '/nrs/scicompsoft/elmalakis/GAN_Registration_Data/flydata/forSalma/lo_res/logs_ganpix2pixwithgolden/'
            self.callback = TensorBoard(log_path)
            self.callback.set_model(self.combined)

        self.data_loader = DataLoader(batch_sz=self.batch_sz,
                                      crop_size=self.crop_size,
                                      dataset_name='fly',
                                      min_max=False,
                                      restricted_mask=False,
                                      use_hist_equilized_data=False,
                                      use_sharpen=False,
                                      use_golden=True)
Exemplo n.º 31
0
def ResNet50(input_shape, classes):
    x_input = Input(input_shape)
    print(x_input.shape)
    x = ZeroPadding2D(padding=(3, 3))(x_input)

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

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

    ### START CODE HERE ###

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

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

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

    # AVGPOOL (≈1 line). Use "X = AveragePooling2D(...)(X)"
    x = AveragePooling2D((2, 2), name='avg_pool')(x)

    ### END CODE HERE ###

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

    # Create model
    model = Model(inputs=x_input, outputs=x, name='ResNet50')

    return model
Exemplo n.º 32
0
}
C.num_rois = int(options.num_rois)

if C.network == 'resnet50':
    num_features = 1024
elif C.network == 'vgg':
    num_features = 512

if K.image_dim_ordering() == 'th':
    input_shape_img = (3, None, None)
    input_shape_features = (num_features, None, None)
else:
    input_shape_img = (None, None, 1)
    input_shape_features = (None, None, num_features)

img_input = Input(shape=input_shape_img)
roi_input = Input(shape=(C.num_rois, 4))
feature_map_input = Input(shape=input_shape_features)

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

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

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