예제 #1
0
def runCNN(params):
    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

    for key in params:
        if 'Filter' in key or 'Kernel' in key:
            params[key] = int(params[key])

    num_classes = 10
    filepath = os.path.dirname(os.path.abspath(__file__)) + '/data/fashion/'
    x_train, y_train = mnist_reader.load_mnist(filepath, kind='train')
    x_test, y_test = mnist_reader.load_mnist(filepath, kind='t10k')
    x_train = x_train.reshape((60000, 28, 28, 1))
    x_test = x_test.reshape((10000, 28, 28, 1))
    y_train = tf.keras.utils.to_categorical(y_train, num_classes=num_classes)
    y_test = tf.keras.utils.to_categorical(y_test, num_classes=num_classes)

    model = None
    tf.reset_default_graph()
    sess = tf.InteractiveSession()

    model = tf.keras.Sequential()
    model.add(
        Conv2D(params['layer1Filters'],
               params['layer1Kernel'],
               padding='same',
               input_shape=x_train.shape[1:]))
    model.add(Activation('relu'))
    model.add(Conv2D(params['layer2Filters'], params['layer2Kernel']))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(params['firstDropout']))

    model.add(
        Conv2D(params['layer3Filters'], params['layer3Kernel'],
               padding='same'))
    model.add(Activation('relu'))
    model.add(Conv2D(params['layer4Filters'], params['layer4Kernel']))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(params['secondDropout']))

    model.add(Flatten())
    model.add(Dense(params['denseNodes']))
    model.add(Activation('relu'))
    model.add(Dense(num_classes))
    model.add(Activation('softmax'))

    optimizer = Adam(lr=params['learningRate'])

    model.compile(optimizer=optimizer,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    model.fit(x=x_train, y=y_train, batch_size=params['batchSize'], epochs=10)
    #Turns out that according to the 6 tests I ran, 1 epoch is enough to see which network is the most accurate, testing wasn't extensive though and there was definitely a great deal of variation

    score = model.evaluate(x_test, y_test, batch_size=128)

    print("Accuracy on Testing Data:", str(score[1] * 100) + "%")
    print("Hyperparameters: " + str(params))
    sess.close()
    return {'loss': score[0], 'status': STATUS_OK}
예제 #2
0
# Preprocessing - exchange the scale of data
x_train = x_train / 255
x_test = x_test / 255

# Preprocessing - change class label to 1-hot vector
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

# Create the neural network
# Input layer + Convolutional layer 1st
model = Sequential()
model.add(
    Conv2D(filters=32,
           input_shape=(32, 32, 3),
           kernel_size=(3, 3),
           strides=(1, 1),
           padding='same',
           activation='relu'))

# Convolutional layer 2nd
model.add(
    Conv2D(filters=32,
           kernel_size=(3, 3),
           strides=(1, 1),
           padding='same',
           activation='relu'))

# MaxPooling layer 1st
model.add(MaxPool2D(pool_size=(2, 2)))

# Dropout layer 1st
예제 #3
0
def faceRecoModel(input_shape):
    """
    Implementation of the Inception model used for FaceNet
    
    Arguments:
    input_shape -- shape of the images of the dataset

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

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

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

    # First Block
    X = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(X)
    X = BatchNormalization(axis=1, name='bn1')(X)
    X = Activation('relu')(X)

    # Zero-Padding + MAXPOOL
    X = ZeroPadding2D((1, 1))(X)
    X = MaxPooling2D((3, 3), strides=2)(X)

    # Second Block
    X = Conv2D(64, (1, 1), strides=(1, 1), name='conv2')(X)
    X = BatchNormalization(axis=1, epsilon=0.00001, name='bn2')(X)
    X = Activation('relu')(X)

    # Zero-Padding + MAXPOOL
    X = ZeroPadding2D((1, 1))(X)

    # Second Block
    X = Conv2D(192, (3, 3), strides=(1, 1), name='conv3')(X)
    X = BatchNormalization(axis=1, epsilon=0.00001, name='bn3')(X)
    X = Activation('relu')(X)

    # Zero-Padding + MAXPOOL
    X = ZeroPadding2D((1, 1))(X)
    X = MaxPooling2D(pool_size=3, strides=2)(X)

    # Inception 1: a/b/c
    X = inception_block_1a(X)
    X = inception_block_1b(X)
    X = inception_block_1c(X)

    # Inception 2: a/b
    X = inception_block_2a(X)
    X = inception_block_2b(X)

    # Inception 3: a/b
    X = inception_block_3a(X)
    X = inception_block_3b(X)

    # Top layer
    X = AveragePooling2D(pool_size=(3, 3),
                         strides=(1, 1),
                         data_format='channels_first')(X)
    X = Flatten()(X)
    X = Dense(128, name='dense_layer')(X)

    # L2 normalization
    X = Lambda(lambda x: K.l2_normalize(x, axis=1))(X)

    # Create model instance

    model = Model(inputs=X_input, outputs=X, name='FaceRecoModel')
    return model
예제 #4
0
from tensorflow.python.keras.callbacks import ModelCheckpoint
import spectrogramsCreator as sC
from dataSet import createTrainingSet
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Conv2D, Flatten, MaxPool2D, Dropout
from tensorflow.python.keras.optimizers import rmsprop, Adam
from musicLabelBinarizer import fit_trasform
from musicFileInfo import getAllGenres
from tensorflow.python.keras.callbacks import TensorBoard
from config import dataForTestingPercent, dataForTrainingPercent, dataForValidatingPercent
import numpy

model = Sequential()

model.add(Conv2D(filters=64, kernel_size=2, activation='elu', input_shape=(128, 128, 1)))
model.add(MaxPool2D(2))

model.add(Conv2D(filters=128, kernel_size=2, activation='elu'))
model.add(MaxPool2D(2))

model.add(Conv2D(filters=256, kernel_size=2, activation='elu'))
model.add(MaxPool2D(2))

model.add(Conv2D(filters=512, kernel_size=2, activation='elu'))
model.add(MaxPool2D(2))

model.add(Flatten())
model.add(Dense(1024, activation='elu'))
model.add(Dropout(rate=0.5))
model.add(Dense(10, activation='softmax'))
예제 #5
0
def ResNet50(input_shape=(32, 32, 3), classes=10):
    X_input = Input(input_shape)

    X = ZeroPadding2D((3, 3))(X_input)

    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)

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

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

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

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

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

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

    return model
예제 #6
0
def upsample(x_in, num_filters):
    x = Conv2D(num_filters, kernel_size=3, padding='same')(x_in)
    x = Lambda(pixel_shuffle(scale=2))(x)
    return PReLU(shared_axes=[1, 2])(x)
예제 #7
0
                images.append(current_image)
                labels.append(current_label)

        images = np.array(images, dtype='float32')
        labels = np.array(labels, dtype='int32')

        result.append((images, labels))

    return result


(X_train, y_train), (X_test, y_test) = load_data()

model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))
model.add(MaxPooling2D(2, 2))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(2, 2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(6, activation='softmax'))
'''
Predicting medical costs
'''

df = pd.read_csv("insurance.csv")

print(df.head())

df['sex_cat'] = df['sex'].astype('category')
예제 #8
0
def ResNet(input_shape=None,
           classes=10,
           block='bottleneck',
           residual_unit='v2',
           repetitions=None,
           initial_filters=64,
           activation='softmax',
           include_top=True,
           input_tensor=None,
           dropout=None,
           transition_dilation_rate=(1, 1),
           initial_strides=(2, 2),
           initial_kernel_size=(7, 7),
           initial_pooling='max',
           final_pooling=None,
           top='classification'):
    """Builds a custom ResNet like architecture. Defaults to ResNet50 v2.

    Args:
        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` dim ordering)
            or `(3, 224, 224)` (with `channels_first` dim ordering).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 8.
            E.g. `(224, 224, 3)` would be one valid value.
        classes: The number of outputs at final softmax layer
        block: The block function to use. This is either `'basic'` or `'bottleneck'`.
            The original paper used `basic` for layers < 50.
        repetitions: Number of repetitions of various block units.
            At each block unit, the number of filters are doubled and the input size is halved.
            Default of None implies the ResNet50v2 values of [3, 4, 6, 3].
        transition_dilation_rate: Used for pixel-wise prediction tasks such as image segmentation.
        residual_unit: the basic residual unit, 'v1' for conv bn relu, 'v2' for bn relu conv.
            See [Identity Mappings in Deep Residual Networks](https://arxiv.org/abs/1603.05027)
            for details.
        dropout: None for no dropout, otherwise rate of dropout from 0 to 1.
            Based on [Wide Residual Networks.(https://arxiv.org/pdf/1605.07146) paper.
        transition_dilation_rate: Dilation rate for transition layers. For semantic
            segmentation of images use a dilation rate of (2, 2).
        initial_strides: Stride of the very first residual unit and MaxPooling2D call,
            with default (2, 2), set to (1, 1) for small images like cifar.
        initial_kernel_size: kernel size of the very first convolution, (7, 7) for imagenet
            and (3, 3) for small image datasets like tiny imagenet and cifar.
            See [ResNeXt](https://arxiv.org/abs/1611.05431) paper for details.
        initial_pooling: Determine if there will be an initial pooling layer,
            'max' for imagenet and None for small image datasets.
            See [ResNeXt](https://arxiv.org/abs/1611.05431) paper for details.
        final_pooling: Optional pooling mode for feature extraction at the final model layer
            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.
        top: Defines final layers to evaluate based on a specific problem type. Options are
            'classification' for ImageNet style problems, 'segmentation' for problems like
            the Pascal VOC dataset, and None to exclude these layers entirely.

    Returns:
        The keras `Model`.
    """
    if activation not in ['softmax', 'sigmoid', None]:
        raise ValueError(
            'activation must be one of "softmax", "sigmoid", or None')
    if activation == 'sigmoid' and classes != 1:
        raise ValueError(
            'sigmoid activation can only be used when classes = 1')
    if repetitions is None:
        repetitions = [3, 4, 6, 3]
    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=32,
                                      min_size=8,
                                      data_format=K.image_data_format(),
                                      require_flatten=include_top)
    _handle_dim_ordering()
    if len(input_shape) != 3:
        raise Exception(
            "Input shape should be a tuple (nb_channels, nb_rows, nb_cols)")

    if block == 'basic':
        block_fn = basic_block
    elif block == 'bottleneck':
        block_fn = bottleneck
    elif isinstance(block, six.string_types):
        block_fn = _string_to_function(block)
    else:
        block_fn = block

    if residual_unit == 'v2':
        residual_unit = _bn_relu_conv
    elif residual_unit == 'v1':
        residual_unit = _conv_bn_relu
    elif isinstance(residual_unit, six.string_types):
        residual_unit = _string_to_function(residual_unit)
    else:
        residual_unit = residual_unit

    # Permute dimension order if necessary
    if K.image_data_format() == 'channels_first':
        input_shape = (input_shape[1], input_shape[2], input_shape[0])
    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=32,
                                      min_size=8,
                                      data_format=K.image_data_format(),
                                      require_flatten=include_top)

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

    x = _conv_bn_relu(filters=initial_filters,
                      kernel_size=initial_kernel_size,
                      strides=initial_strides)(img_input)
    if initial_pooling == 'max':
        x = MaxPooling2D(pool_size=(3, 3),
                         strides=initial_strides,
                         padding="same")(x)

    block = x
    filters = initial_filters
    for i, r in enumerate(repetitions):
        transition_dilation_rates = [transition_dilation_rate] * r
        transition_strides = [(1, 1)] * r
        if transition_dilation_rate == (1, 1) and i != 0:
            transition_strides[0] = (2, 2)
        block = _residual_block(
            block_fn,
            filters=filters,
            stage=i,
            blocks=r,
            is_first_layer=(i == 0),
            dropout=dropout,
            transition_dilation_rates=transition_dilation_rates,
            transition_strides=transition_strides,
            residual_unit=residual_unit)(block)
        filters *= 2

    # Last activation
    x = _bn_relu(block)

    # Classifier block
    if include_top and top is 'classification':
        x = GlobalAveragePooling2D()(x)
        x = Dense(units=classes,
                  activation=activation,
                  kernel_initializer="he_normal")(x)
    elif include_top and top is 'segmentation':
        x = Conv2D(classes, (1, 1), activation='linear', padding='same')(x)

        if K.image_data_format() == 'channels_first':
            channel, row, col = input_shape
        else:
            row, col, channel = input_shape

        x = Reshape((row * col, classes))(x)
        x = Activation(activation)(x)
        x = Reshape((row, col, classes))(x)
    elif final_pooling == 'avg':
        x = GlobalAveragePooling2D()(x)
    elif final_pooling == 'max':
        x = GlobalMaxPooling2D()(x)

    if input_tensor is not None:
        inputs = layer_utils.get_source_inputs(input_tensor)
    else:
        inputs = img_input
    model = Model(inputs=inputs, outputs=x, name='resnet')
    return model
예제 #9
0
    test_labels = np.array(test_labels)

data = data.item()
names = np.array(data['names'])
smiles = np.array(data['onehots']).reshape(-1, 72, 398, 1)

test_data = test_data.item()
test_names = np.array(test_data['names'])
test_smiles = np.array(test_data['onehots']).reshape(-1, 72, 398, 1)

# print(smiles)
model = Sequential()

model.add(
    Conv2D(8, (2, 2),
           input_shape=smiles.shape[1:],
           kernel_regularizer=regularizers.l2(0.02)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(8, (2, 2), kernel_regularizer=regularizers.l2(0.02)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(8, (2, 2), kernel_regularizer=regularizers.l2(0.02)))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
예제 #10
0
def svhn_student_strong(n_classes: int,
                        input_shape=None,
                        input_tensor=None,
                        weights_path: Union[None, str] = None) -> Model:
    """
    Defines a svhn strong student network.

    :param n_classes: the number of classes.
    :param input_shape: the input shape of the network. Can be omitted if input_tensor is used.
    :param input_tensor: the input tensor of the network. Can be omitted if input_shape is used.
    :param weights_path: a path to a trained svhn tiny network's weights.
    :return: Keras functional Model.
    """
    inputs = create_inputs(input_shape, input_tensor)

    # Define a weight decay for the regularisation.
    weight_decay = 1e-4

    # Block1.
    x = Conv2D(32, (3, 3),
               padding='same',
               activation='elu',
               name='block1_conv1',
               kernel_regularizer=l2(weight_decay))(inputs)

    x = BatchNormalization(name='block1_batch-norm1')(x)
    x = Conv2D(32, (3, 3),
               padding='same',
               activation='elu',
               name='block1_conv2',
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(name='block1_batch-norm2')(x)
    x = MaxPooling2D(pool_size=(2, 2), name='block1_pool')(x)
    x = Dropout(0.2, name='block1_dropout', seed=0)(x)

    # Block2.
    x = Conv2D(64, (3, 3),
               padding='same',
               activation='elu',
               name='block2_conv1',
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(name='block2_batch-norm1')(x)
    x = Conv2D(64, (3, 3),
               padding='same',
               activation='elu',
               name='block2_conv2',
               kernel_regularizer=l2(weight_decay))(x)
    x = BatchNormalization(name='block2_batch-norm2')(x)
    x = MaxPooling2D(pool_size=(2, 2), name='block2_pool')(x)
    x = Dropout(0.3, name='block2_dropout', seed=0)(x)

    # Add top layers.
    x = Flatten()(x)
    x = Dense(n_classes)(x)
    outputs = Activation('softmax', name='softmax')(x)

    # Create model.
    model = Model(inputs, outputs, name='svhn_student_strong')

    # Load weights, if they exist.
    load_weights(weights_path, model)

    return model
예제 #11
0
def VGG16(input_shape, nb_classes, dropout=False, dropout_rate=0.2):
    """
    Creates a VGG16 network.

    Parameters
    ----------
    input_shape : tuple
        The shape of the input tensor not including the sample axis.
        Tensorflow uses the NHWC dimention ordering convention.
    nb_class : int
        The number of output class. The network will have this number of
        output nodes for one-hot encoding.
    dropout : bool
        Where or not to implement dropout in the fully-connected layers.
    dropout_rate : float
        Dropout rate.

    Returns
    -------
    keras.models.Sequential() :
        The create VGG16 network.
    """
    vgg16 = Sequential()

    # sub-net 1
    vgg16.add(
        Conv2D(filters=64,
               kernel_size=3,
               padding='same',
               activation='relu',
               input_shape=input_shape))
    vgg16.add(
        Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
    vgg16.add(MaxPool2D(pool_size=2))

    # sub-net 2
    vgg16.add(
        Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
    vgg16.add(
        Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
    vgg16.add(MaxPool2D(pool_size=2))

    # sub-net 3
    vgg16.add(
        Conv2D(filters=256, kernel_size=3, padding='same', activation='relu'))
    vgg16.add(
        Conv2D(filters=256, kernel_size=3, padding='same', activation='relu'))
    vgg16.add(
        Conv2D(filters=256, kernel_size=3, padding='same', activation='relu'))
    vgg16.add(MaxPool2D(pool_size=2))

    # sub-net 4
    vgg16.add(
        Conv2D(filters=512, kernel_size=3, padding='same', activation='relu'))
    vgg16.add(
        Conv2D(filters=512, kernel_size=3, padding='same', activation='relu'))
    vgg16.add(
        Conv2D(filters=512, kernel_size=3, padding='same', activation='relu'))
    vgg16.add(MaxPool2D(pool_size=2))

    # sub-net 5
    vgg16.add(
        Conv2D(filters=512, kernel_size=3, padding='same', activation='relu'))
    vgg16.add(
        Conv2D(filters=512, kernel_size=3, padding='same', activation='relu'))
    vgg16.add(
        Conv2D(filters=512, kernel_size=3, padding='same', activation='relu'))
    vgg16.add(MaxPool2D(pool_size=2))

    # dense layers
    vgg16.add(Flatten())
    vgg16.add(Dense(units=4096, activation='relu'))
    vgg16.add(Dropout(dropout_rate)) if dropout else None
    vgg16.add(Dense(units=4096, activation='relu'))
    vgg16.add(Dropout(dropout_rate)) if dropout else None
    vgg16.add(Dense(units=nb_classes, activation='softmax'))

    return vgg16
예제 #12
0
def create_pyramid_level(backbone_input,
                         upsamplelike_input=None,
                         addition_input=None,
                         upsample_type='upsamplelike',
                         level=5,
                         ndim=2,
                         lite=False,
                         interpolation='bilinear',
                         feature_size=256):
    """Create a pyramid layer from a particular backbone input layer.

    Args:
        backbone_input (layer): Backbone layer to use to create they pyramid
            layer
        upsamplelike_input (tensor): Optional input to use
            as a template for shape to upsample to
        addition_input (layer): Optional layer to add to
            pyramid layer after convolution and upsampling.
        upsample_type (str, optional): Choice of upsampling methods
            from ['upsamplelike','upsampling2d','upsampling3d'].
            Defaults to 'upsamplelike'.
        level (int): Level to use in layer names, defaults to 5.
        feature_size (int):Number of filters for
            convolutional layer, defaults to 256.
        ndim (int): The spatial dimensions of the input data. Default is 2,
            but it also works with 3
        lite (bool): Whether to use depthwise conv instead of regular conv for
            feature pyramid construction
        interpolation (str): Choice of interpolation mode for upsampling
            layers from ['bilinear', 'nearest']. Defaults to bilinear.

    Returns:
        tuple: Pyramid layer after processing, upsampled pyramid layer

    Raises:
        ValueError: ndim is not 2 or 3
        ValueError: upsample_type not ['upsamplelike','upsampling2d',
            'upsampling3d']
    """
    # Check input to ndims
    acceptable_ndims = {2, 3}
    if ndim not in acceptable_ndims:
        raise ValueError('Only 2 and 3 dimensional networks are supported')

    # Check if inputs to ndim and lite are compatible
    if ndim == 3 and lite:
        raise ValueError('lite == True is not compatible with 3 dimensional '
                         'networks')

    # Check input to interpolation
    acceptable_interpolation = {'bilinear', 'nearest'}
    if interpolation not in acceptable_interpolation:
        raise ValueError('Interpolation mode not supported. Choose from '
                         '["bilinear", "nearest"]')

    # Check input to upsample_type
    acceptable_upsample = {'upsamplelike', 'upsampling2d', 'upsampling3d'}
    if upsample_type not in acceptable_upsample:
        raise ValueError(
            'Upsample method not supported. Choose from ["upsamplelike",'
            '"upsampling2d", "upsampling3d"]')

    reduced_name = 'C{}_reduced'.format(level)
    upsample_name = 'P{}_upsampled'.format(level)
    addition_name = 'P{}_merged'.format(level)
    final_name = 'P{}'.format(level)

    # Apply 1x1 conv to backbone layer
    if ndim == 2:
        pyramid = Conv2D(feature_size, (1, 1),
                         strides=(1, 1),
                         padding='same',
                         name=reduced_name)(backbone_input)
    else:
        pyramid = Conv3D(feature_size, (1, 1, 1),
                         strides=(1, 1, 1),
                         padding='same',
                         name=reduced_name)(backbone_input)

    # Add and then 3x3 conv
    if addition_input is not None:
        pyramid = Add(name=addition_name)([pyramid, addition_input])

    # Upsample pyramid input
    if upsamplelike_input is not None:
        if upsample_type == 'upsamplelike':
            pyramid_upsample = UpsampleLike(name=upsample_name)(
                [pyramid, upsamplelike_input])
        else:
            upsampling = UpSampling2D if ndim == 2 else UpSampling3D
            size = (2, 2) if ndim == 2 else (1, 2, 2)
            upsampling_kwargs = {
                'size': size,
                'name': upsample_name,
                'interpolation': interpolation
            }
            if ndim > 2:
                del upsampling_kwargs['interpolation']
            pyramid_upsample = upsampling(**upsampling_kwargs)(pyramid)
    else:
        pyramid_upsample = None

    if ndim == 2:
        if lite:
            pyramid_final = DepthwiseConv2D((3, 3),
                                            strides=(1, 1),
                                            padding='same',
                                            name=final_name)(pyramid)
        else:
            pyramid_final = Conv2D(feature_size, (3, 3),
                                   strides=(1, 1),
                                   padding='same',
                                   name=final_name)(pyramid)
    else:
        pyramid_final = Conv3D(feature_size, (1, 3, 3),
                               strides=(1, 1, 1),
                               padding='same',
                               name=final_name)(pyramid)

    return pyramid_final, pyramid_upsample
예제 #13
0
def __create_pyramid_features(backbone_dict,
                              upsample_type='upsamplelike',
                              ndim=2,
                              feature_size=256,
                              include_final_layers=True,
                              lite=False,
                              interpolation='bilinear'):
    """Creates the FPN layers on top of the backbone features.

    Args:
        backbone_dict (dictionary): A dictionary of the backbone layers, with
            the names as keys, e.g. {'C0': C0, 'C1': C1, 'C2': C2, ...}
        upsample_type (str, optional): Choice of upsampling methods
            from ['upsamplelike','upsamling2d','upsampling3d'].
            Defaults to 'upsamplelike'.
        feature_size (int): Defaults to 256. The feature size to use
            for the resulting feature levels.
        include_final_layers (bool): Add two coarser pyramid levels
        ndim (int): The spatial dimensions of the input data.
            Default is 2, but it also works with 3
        lite (bool): Whether to use depthwise conv instead of regular conv for
            feature pyramid construction
        interpolation (str): Choice of interpolation mode for upsampling
            layers from ['bilinear', 'nearest']. Defaults to bilinear.

    Returns:
        dict: The feature pyramid names and levels,
            e.g. {'P3': P3, 'P4': P4, ...}
            Each backbone layer gets a pyramid level, and two additional levels
            are added, e.g. [C3, C4, C5] --> [P3, P4, P5, P6, P7]

    Raises:
        ValueError: ndim is not 2 or 3
        ValueError: upsample_type not ['upsamplelike','upsampling2d','upsampling3d']
    """

    acceptable_ndims = [2, 3]
    if ndim not in acceptable_ndims:
        raise ValueError('Only 2 and 3 dimensional networks are supported')

    acceptable_interpolation = {'bilinear', 'nearest'}
    if interpolation not in acceptable_interpolation:
        raise ValueError('Interpolation mode not supported. Choose from '
                         '["bilinear", "nearest"]')

    acceptable_upsample = {'upsamplelike', 'upsampling2d', 'upsampling3d'}
    if upsample_type not in acceptable_upsample:
        raise ValueError(
            'Upsample method not supported. Choose from ["upsamplelike",'
            '"upsampling2d", "upsampling3d"]')

    # Get names of the backbone levels and place in ascending order
    backbone_names = get_sorted_keys(backbone_dict)
    backbone_features = [backbone_dict[name] for name in backbone_names]

    pyramid_names = []
    pyramid_finals = []
    pyramid_upsamples = []

    # Reverse lists
    backbone_names.reverse()
    backbone_features.reverse()

    for i in range(len(backbone_names)):

        N = backbone_names[i]
        level = int(re.findall(r'\d+', N)[0])
        p_name = 'P{}'.format(level)
        pyramid_names.append(p_name)

        backbone_input = backbone_features[i]

        # Don't add for the bottom of the pyramid
        if i == 0:
            if len(backbone_features) > 1:
                upsamplelike_input = backbone_features[i + 1]
            else:
                upsamplelike_input = None
            addition_input = None

        # Don't upsample for the top of the pyramid
        elif i == len(backbone_names) - 1:
            upsamplelike_input = None
            addition_input = pyramid_upsamples[-1]

        # Otherwise, add and upsample
        else:
            upsamplelike_input = backbone_features[i + 1]
            addition_input = pyramid_upsamples[-1]

        pf, pu = create_pyramid_level(backbone_input,
                                      upsamplelike_input=upsamplelike_input,
                                      addition_input=addition_input,
                                      upsample_type=upsample_type,
                                      level=level,
                                      ndim=ndim,
                                      lite=lite,
                                      interpolation=interpolation)
        pyramid_finals.append(pf)
        pyramid_upsamples.append(pu)

    # Add the final two pyramid layers
    if include_final_layers:
        # "Second to last pyramid layer is obtained via a
        # 3x3 stride-2 conv on the coarsest backbone"
        N = backbone_names[0]
        F = backbone_features[0]
        level = int(re.findall(r'\d+', N)[0]) + 1
        P_minus_2_name = 'P{}'.format(level)

        if ndim == 2:
            P_minus_2 = Conv2D(feature_size,
                               kernel_size=(3, 3),
                               strides=(2, 2),
                               padding='same',
                               name=P_minus_2_name)(F)
        else:
            P_minus_2 = Conv3D(feature_size,
                               kernel_size=(1, 3, 3),
                               strides=(1, 2, 2),
                               padding='same',
                               name=P_minus_2_name)(F)

        pyramid_names.insert(0, P_minus_2_name)
        pyramid_finals.insert(0, P_minus_2)

        # "Last pyramid layer is computed by applying ReLU
        # followed by a 3x3 stride-2 conv on second to last layer"
        level = int(re.findall(r'\d+', N)[0]) + 2
        P_minus_1_name = 'P{}'.format(level)
        P_minus_1 = Activation('relu', name=N + '_relu')(P_minus_2)

        if ndim == 2:
            P_minus_1 = Conv2D(feature_size,
                               kernel_size=(3, 3),
                               strides=(2, 2),
                               padding='same',
                               name=P_minus_1_name)(P_minus_1)
        else:
            P_minus_1 = Conv3D(feature_size,
                               kernel_size=(1, 3, 3),
                               strides=(1, 2, 2),
                               padding='same',
                               name=P_minus_1_name)(P_minus_1)

        pyramid_names.insert(0, P_minus_1_name)
        pyramid_finals.insert(0, P_minus_1)

    pyramid_names.reverse()
    pyramid_finals.reverse()

    # Reverse lists
    backbone_names.reverse()
    backbone_features.reverse()

    pyramid_dict = {}
    for name, feature in zip(pyramid_names, pyramid_finals):
        pyramid_dict[name] = feature

    return pyramid_dict
def create_mobilenetv2_ssd_lite(num_classes,
                                width_mult=1.0,
                                is_test=False,
                                is_train=False):
    base_net = MobileNetV2(input_shape=(config.image_size, config.image_size,
                                        3),
                           include_top=False,
                           alpha=width_mult,
                           weights=None)

    source_layer_indexes = [
        GraphPath((127, 136), 3),
        len(base_net.layers),
    ]
    extras = [
        # Make frozen inverted residual block functions that only need input
        partial(inverted_res_block,
                expansion=0.2,
                stride=2,
                alpha=width_mult,
                filters=512,
                block_id=17),
        partial(inverted_res_block,
                expansion=0.25,
                stride=2,
                alpha=width_mult,
                filters=256,
                block_id=18),
        partial(inverted_res_block,
                expansion=0.5,
                stride=2,
                alpha=width_mult,
                filters=256,
                block_id=19),
        partial(inverted_res_block,
                expansion=0.25,
                stride=2,
                alpha=width_mult,
                filters=64,
                block_id=20)
    ]

    regression_headers = [
        # TODO change to relu6
        SeparableConv2D_with_batchnorm(filters=6 * 4, kernel_size=3),
        SeparableConv2D_with_batchnorm(filters=6 * 4, kernel_size=3),
        SeparableConv2D_with_batchnorm(filters=6 * 4, kernel_size=3),
        SeparableConv2D_with_batchnorm(filters=6 * 4, kernel_size=3),
        SeparableConv2D_with_batchnorm(filters=6 * 4, kernel_size=3),
        Conv2D(filters=6 * 4, kernel_size=1, padding='valid'),
    ]

    classification_headers = [
        SeparableConv2D_with_batchnorm(filters=6 * num_classes,
                                       kernel_size=3,
                                       name='conv_extra_1_' +
                                       str(6 * num_classes)),
        SeparableConv2D_with_batchnorm(filters=6 * num_classes,
                                       kernel_size=3,
                                       name='conv_extra_2_' +
                                       str(6 * num_classes)),
        SeparableConv2D_with_batchnorm(filters=6 * num_classes,
                                       kernel_size=3,
                                       name='conv_extra_3_' +
                                       str(6 * num_classes)),
        SeparableConv2D_with_batchnorm(filters=6 * num_classes,
                                       kernel_size=3,
                                       name='conv_extra_4_' +
                                       str(6 * num_classes)),
        SeparableConv2D_with_batchnorm(filters=6 * num_classes,
                                       kernel_size=3,
                                       name='conv_extra_5_' +
                                       str(6 * num_classes)),
        Conv2D(filters=6 * num_classes,
               kernel_size=1,
               padding='valid',
               name='conv_extra_6_' + str(6 * num_classes)),
    ]

    return SSD(num_classes,
               base_net,
               source_layer_indexes,
               extras,
               classification_headers,
               regression_headers,
               is_test=is_test,
               config=config,
               is_train=is_train)
예제 #15
0
def _main(args):
    config_path = os.path.expanduser(args.config_path)
    weights_path = os.path.expanduser(args.weights_path)
    assert config_path.endswith('.cfg'), '{} is not a .cfg file'.format(
        config_path)
    assert weights_path.endswith(
        '.weights'), '{} is not a .weights file'.format(weights_path)

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

    # Load weights and config.
    print('Loading weights.')
    weights_file = open(weights_path, 'rb')
    major, minor, revision = np.ndarray(shape=(3, ),
                                        dtype='int32',
                                        buffer=weights_file.read(12))
    if (major * 10 + minor) >= 2 and major < 1000 and minor < 1000:
        seen = np.ndarray(shape=(1, ),
                          dtype='int64',
                          buffer=weights_file.read(8))
    else:
        seen = np.ndarray(shape=(1, ),
                          dtype='int32',
                          buffer=weights_file.read(4))
    print('Weights Header: ', major, minor, revision, seen)

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

    print('Creating Keras model.')
    input_layer = Input(shape=(None, None, 3))
    prev_layer = input_layer
    all_layers = []

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

            padding = 'same' if pad == 1 and stride == 1 else 'valid'

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

            weights_shape = (size, size, prev_layer_shape[-1], filters)
            darknet_w_shape = (filters, weights_shape[2], size, size)
            weights_size = np.product(weights_shape)

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

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

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

                bn_weight_list = [
                    bn_weights[0],  # scale gamma
                    conv_bias,  # shift beta
                    bn_weights[1],  # running mean
                    bn_weights[2]  # running var
                ]

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

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

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

            # Create Conv2D layer
            if stride > 1:
                # Darknet uses left and top padding instead of 'same' mode
                prev_layer = ZeroPadding2D(((1, 0), (1, 0)))(prev_layer)
            conv_layer = (Conv2D(filters, (size, size),
                                 strides=(stride, stride),
                                 kernel_regularizer=l2(weight_decay),
                                 use_bias=not batch_normalize,
                                 weights=conv_weights,
                                 activation=act_fn,
                                 padding=padding))(prev_layer)

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

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

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

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

        elif section.startswith('shortcut'):
            index = int(cfg_parser[section]['from'])
            activation = cfg_parser[section]['activation']
            assert activation == 'linear', 'Only linear activation supported.'
            all_layers.append(Add()([all_layers[index], prev_layer]))
            prev_layer = all_layers[-1]

        elif section.startswith('upsample'):
            stride = int(cfg_parser[section]['stride'])
            assert stride == 2, 'Only stride=2 supported.'
            all_layers.append(UpSampling2D(stride)(prev_layer))
            prev_layer = all_layers[-1]

        elif section.startswith('yolo'):
            out_index.append(len(all_layers) - 1)
            all_layers.append(None)
            prev_layer = all_layers[-1]

        elif section.startswith('net'):
            pass

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

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

    if args.plot_model:
        plot(model, to_file='{}.png'.format(output_root), show_shapes=True)
        print('Saved model plot to {}.png'.format(output_root))
예제 #16
0
def create_model(learning_rate, num_dense_layers, num_dense_nodes, activation):
    """
    Hyper-parameters:
    learning_rate:     Learning-rate for the optimizer.
    num_dense_layers:  Number of dense layers.
    num_dense_nodes:   Number of nodes in each dense layer.
    activation:        Activation function for all layers.
    """

    # Start construction of a Keras Sequential model.
    model = Sequential()

    # Add an input layer which is similar to a feed_dict in TensorFlow.
    # Note that the input-shape must be a tuple containing the image-size.
    model.add(InputLayer(input_shape=(img_size_flat, )))

    # The input from MNIST is a flattened array with 784 elements,
    # but the convolutional layers expect images with shape (28, 28, 1)
    model.add(Reshape(img_shape_full))

    # First convolutional layer.
    # There are many hyper-parameters in this layer, but we only
    # want to optimize the activation-function in this example.
    model.add(
        Conv2D(kernel_size=5,
               strides=1,
               filters=16,
               padding='same',
               activation=activation,
               name='layer_conv1'))
    model.add(MaxPooling2D(pool_size=2, strides=2))

    # Second convolutional layer.
    # Again, we only want to optimize the activation-function here.
    model.add(
        Conv2D(kernel_size=5,
               strides=1,
               filters=36,
               padding='same',
               activation=activation,
               name='layer_conv2'))
    model.add(MaxPooling2D(pool_size=2, strides=2))

    # Flatten the 4-rank output of the convolutional layers
    # to 2-rank that can be input to a fully-connected / dense layer.
    model.add(Flatten())

    # Add fully-connected / dense layers.
    # The number of layers is a hyper-parameter we want to optimize.
    for i in range(num_dense_layers):
        # Name of the layer. This is not really necessary
        # because Keras should give them unique names.
        name = 'layer_dense_{0}'.format(i + 1)

        # Add the dense / fully-connected layer to the model.
        # This has two hyper-parameters we want to optimize:
        # The number of nodes and the activation function.
        model.add(Dense(num_dense_nodes, activation=activation, name=name))

    # Last fully-connected / dense layer with softmax-activation
    # for use in classification.
    model.add(Dense(num_classes, activation='softmax'))

    # Use the Adam method for training the network.
    # We want to find the best learning-rate for the Adam method.
    optimizer = Adam(lr=learning_rate)

    # In Keras we need to compile the model so it can be trained.
    model.compile(optimizer=optimizer,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

    return model
예제 #17
0
epochs = 20
lr = 0.01
# channel last
image_shape = (imag_rows, imag_cols, 1)

# 默认存储在~/.keras/dataset/mnist.npz
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

x_train = x_train.reshape(-1, *image_shape).astype('float32') / 255.0
x_test = x_test.reshape(-1, *image_shape).astype('float32') / 255.0

# build graph
model = keras.Sequential()

model.add(
    Conv2D(32, kernel_size=(5, 5), activation='relu', input_shape=image_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, kernel_size=(5, 5), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(500, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

model.compile(optimizer=keras.optimizers.SGD(lr),
              loss=keras.losses.sparse_categorical_crossentropy,
              metrics=['accuracy'])

model.fit(x_train,
          y_train,
          batch_size,
          epochs,
예제 #18
0
def resnet(input_tensor):
    """Inference function for ResNet

    y = resnet(X)

    Parameters
    ----------
    input_tensor : keras.layers.Input

    Returns
    ----------
    y : softmax output
    """
    def name_builder(type, stage, block, name):
        return "{}{}{}_branch{}".format(type, stage, block, name)

    def identity_block(input_tensor, kernel_size, filters, stage, block):
        F1, F2, F3 = filters

        def name_fn(type, name):
            return name_builder(type, stage, block, name)

        x = Conv2D(F1, (1, 1), name=name_fn('res', '2a'))(input_tensor)
        x = BatchNormalization(name=name_fn('bn', '2a'))(x)
        x = PReLU()(x)

        x = Conv2D(F2, kernel_size, padding='same', name=name_fn('res',
                                                                 '2b'))(x)
        x = BatchNormalization(name=name_fn('bn', '2b'))(x)
        x = PReLU()(x)

        x = Conv2D(F3, (1, 1), name=name_fn('res', '2c'))(x)
        x = BatchNormalization(name=name_fn('bn', '2c'))(x)
        x = PReLU()(x)

        x = add([x, input_tensor])
        x = PReLU()(x)

        return x

    def conv_block(input_tensor,
                   kernel_size,
                   filters,
                   stage,
                   block,
                   strides=(2, 2)):
        def name_fn(type, name):
            return name_builder(type, stage, block, name)

        F1, F2, F3 = filters

        x = Conv2D(F1, (1, 1), strides=strides,
                   name=name_fn("res", "2a"))(input_tensor)
        x = BatchNormalization(name=name_fn("bn", "2a"))(x)
        x = PReLU()(x)

        x = Conv2D(F2, kernel_size, padding='same', name=name_fn("res",
                                                                 "2b"))(x)
        x = BatchNormalization(name=name_fn("bn", "2b"))(x)
        x = PReLU()(x)

        x = Conv2D(F3, (1, 1), name=name_fn("res", "2c"))(x)
        x = BatchNormalization(name=name_fn("bn", "2c"))(x)

        sc = Conv2D(F3, (1, 1), strides=strides,
                    name=name_fn("res", "1"))(input_tensor)
        sc = BatchNormalization(name=name_fn("bn", "1"))(sc)

        x = add([x, sc])
        x = PReLU()(x)

        return x

    net = ZeroPadding2D((3, 3))(input_tensor)
    net = Conv2D(64, (7, 7), strides=(2, 2), name="conv1")(net)
    net = BatchNormalization(name="bn_conv1")(net)
    net = PReLU()(net)
    net = MaxPooling2D((3, 3), strides=(2, 2))(net)

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

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

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

    net = Flatten()(net)
    net = Dense(10, activation="softmax", name="softmax")(net)

    return net
예제 #19
0
y_val = np_utils.to_categorical(y_vl_orig, 10)
y_test = np_utils.to_categorical(y_ts_orig, 10)

# normalization to 0 mean and 1 std
mean = np.mean(x_tr_orig)
std = np.std(x_tr_orig)
x_train = (x_tr_orig - mean) / std
x_val = (x_vl_orig - mean) / std
x_test = (x_ts_orig - mean) / std

# Model definition
model = Sequential()
model.add(
    Conv2D(input_shape=(32, 32, 3),
           kernel_size=(2, 2),
           padding='same',
           strides=(2, 2),
           filters=32))
model.add(BatchNormalization(axis=-1))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='same'))
model.add(
    Conv2D(kernel_size=(2, 2), padding='same', strides=(2, 2), filters=64))
model.add(BatchNormalization(axis=-1))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='same'))
model.add(Flatten())
model.add(Dense(256, activation=activation))
model.add(Dropout(dropout))
model.add(Dense(128, activation=activation))
model.add(Dropout(dropout))
model.add(Dense(10, activation='softmax'))
예제 #20
0
train_image_generator = ImageDataGenerator(rescale=1. / 255,
                                           rotation_range=45,
                                           width_shift_range=.15,
                                           height_shift_range=.15,
                                           horizontal_flip=True,
                                           zoom_range=0.5)

train_data_gen = train_image_generator.flow_from_directory(
    batch_size=batch_size,
    directory=path,
    shuffle=True,
    target_size=(image_size, image_size),
    class_mode='binary')

model = Sequential()
model.add(Conv2D(16, (3, 3), input_shape=x.shape[1:]))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(32, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Flatten())
model.add(Dense(512))
예제 #21
0
 def conv_block(self, inputs, filters):
     x = Conv2D(filters, 4, 2, padding='same')(inputs)
     x = self.norm(x)
     x = Activation('relu')(x)
     return x
예제 #22
0
    def __init__(self, output_shape=5):

        self.output_shape = output_shape
        #Instantiate an empty self.model
        self.model = Sequential()

        # 1st Convolutional Layer> 4 and len(sys.argv) < 7
        self.model.add(Conv2D(filters=96, input_shape=(224,224,3), kernel_size=(11,11), strides=(4,4), padding='valid'))
        self.model.add(Activation('relu'))
        # Max Pooling
        self.model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='valid'))

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

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

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

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

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

        # 2nd Fully Connected Layer
        self.model.add(Dense(4096))
        self.model.add(Activation('relu'))
        # Add Dropout
        self.model.add(Dropout(0.4))

        # 3rd Fully Connected Layer
        self.model.add(Dense(1000))
        self.model.add(Activation('relu'))
        # Add Dropout
        self.model.add(Dropout(0.4))

        # Output Layer
        self.model.add(Dense(output_shape))
        self.model.add(Activation('softmax'))

        self.model.summary()

        # Compile the self.model
        self.model.compile(
            loss=keras.losses.mean_squared_error,
            optimizer='adam',
            metrics=['accuracy']
        )
예제 #23
0
y = keras.utils.to_categorical(data['class'], num_classes)

data['data'] = data['data'].apply(lambda x: np.array(
    [float(x_split) / 255 for x_split in x[1:-1].split(',')]))
x = np.concatenate(data['data'])
x = x.reshape(num_images, img_rows, img_cols, color_cnt)

# from PIL import Image
# img = Image.fromarray(x[3].astype('uint8'))
# img.save('check.jpg')

dogvscat_model = Sequential()
dogvscat_model.add(
    Conv2D(32,
           kernel_size=(5, 5),
           activation='relu',
           input_shape=(img_rows, img_cols, color_cnt)))
dogvscat_model.add(Conv2D(64, (5, 5), activation='relu'))
dogvscat_model.add(Conv2D(32, (5, 5), activation='relu'))
dogvscat_model.add(Conv2D(64, (5, 5), activation='relu'))
dogvscat_model.add(Conv2D(32, (5, 5), activation='relu'))
dogvscat_model.add(Conv2D(64, (5, 5), activation='relu'))
dogvscat_model.add(Flatten())
dogvscat_model.add(Dense(1024, activation='relu'))
dogvscat_model.add(Dropout(0.8))
dogvscat_model.add(Dense(num_classes, activation='softmax'))

dogvscat_model.compile(loss=keras.losses.categorical_crossentropy,
                       optimizer='adam',
                       metrics=['accuracy'])
예제 #24
0
def build_model(initial_filters, size_final_dense, regularizer):

    # Input Layer
    image_input = Input(shape=(
        image_height, image_width, image_depth
    ))  # Final element is number of channels, set as 1 for greyscale
    #x = BatchNormalization()(image_input)

    ### Block 1
    # Convolutional Layer 1
    x = Conv2D(filters=initial_filters,
               kernel_regularizer=regularizer,
               kernel_size=(3, 3),
               activation='relu',
               padding='same')(image_input)
    #x = BatchNormalization()(x)

    # Convolutional Layer 2
    x = Conv2D(filters=initial_filters,
               kernel_regularizer=regularizer,
               kernel_size=(3, 3),
               activation='relu',
               padding='same')(x)
    #x = BatchNormalization()(x)

    # Pooling Layer 1 - halve spatial dimension
    x = MaxPooling2D(pool_size=(2, 2))(x)

    ### Block 2
    # Convolutional Layer 3 - double number of filters
    x = Conv2D(filters=initial_filters * 2,
               kernel_regularizer=regularizer,
               kernel_size=(3, 3),
               activation='relu',
               padding='same')(x)
    #x = BatchNormalization()(x)

    # Convolutional Layer 4
    x = Conv2D(filters=initial_filters * 2,
               kernel_regularizer=regularizer,
               kernel_size=(3, 3),
               activation='relu',
               padding='same')(x)
    #x = BatchNormalization()(x)

    # Pooling Layer 2 - halve spatial dimension
    x = MaxPooling2D(pool_size=(2, 2))(x)

    ### Block 3
    # Convolutional Layer 5 - double number of filters
    x = Conv2D(filters=initial_filters * 2 * 2,
               kernel_regularizer=regularizer,
               kernel_size=(3, 3),
               activation='relu',
               padding='same')(x)
    #x = BatchNormalization()(x)

    # Convolutional Layer 6
    x = Conv2D(filters=initial_filters * 2 * 2,
               kernel_regularizer=regularizer,
               kernel_size=(3, 3),
               activation='relu',
               padding='same')(x)
    #x = BatchNormalization()(x)

    # Pooling Layer 3 - halve spatial dimension
    x = MaxPooling2D(pool_size=(2, 2))(x)

    # Dense Layer
    x = Flatten()(x)
    x = Dense(size_final_dense,
              activation='relu',
              kernel_regularizer=regularizer)(x)

    # Output Layer
    out = Dense(num_classes,
                activation='softmax',
                kernel_regularizer=regularizer)(
                    x)  # Task is binary classification

    model = Model(image_input, out)
    return (model)
def get_model_emotion_only_same_padding(freeze_stn=False,
                                        freeze_fexnet=False,
                                        input_shape=(192, 192, 3),
                                        sampling_shape=(64, 64)):
    stn_trainable = not freeze_stn
    fexnet_trainable = not freeze_fexnet
    image = Input(shape=input_shape)

    # Localization network
    locnet = Conv2D(8, (3, 3),
                    padding='same',
                    kernel_initializer='he_normal',
                    trainable=stn_trainable)(image)
    locnet = MaxPooling2D((2, 2), padding='same',
                          trainable=stn_trainable)(locnet)
    locnet = Activation('relu', trainable=stn_trainable)(locnet)

    locnet = Conv2D(10, (3, 3),
                    padding='same',
                    kernel_initializer='he_normal',
                    trainable=stn_trainable)(locnet)
    locnet = MaxPooling2D((2, 2), padding='same',
                          trainable=stn_trainable)(locnet)
    locnet = Activation('relu', trainable=stn_trainable)(locnet)

    locnet = Flatten(trainable=stn_trainable)(locnet)
    locnet = Dense(32, trainable=stn_trainable)(locnet)
    locnet = Activation('relu', trainable=stn_trainable)(locnet)
    weights = get_initial_attention_weights(32)
    locnet = Dense(3, weights=weights, trainable=stn_trainable)(locnet)
    locnet = Lambda(generate_attention_matrix,
                    name='locnet_params',
                    trainable=stn_trainable)(locnet)

    # Feature extraction network
    fexnet = Conv2D(10, (3, 3),
                    padding='same',
                    kernel_initializer='he_normal',
                    trainable=fexnet_trainable)(image)
    fexnet = Activation("relu", trainable=fexnet_trainable)(fexnet)
    fexnet = Conv2D(10, (3, 3),
                    padding='same',
                    kernel_initializer='he_normal',
                    trainable=fexnet_trainable)(fexnet)
    fexnet = MaxPooling2D([2, 2], padding='same',
                          trainable=fexnet_trainable)(fexnet)
    fexnet = Activation("relu", trainable=fexnet_trainable)(fexnet)

    fexnet = Conv2D(10, (3, 3),
                    padding='same',
                    kernel_initializer='he_normal',
                    trainable=fexnet_trainable)(fexnet)
    fexnet = Activation("relu", trainable=fexnet_trainable)(fexnet)
    fexnet = Conv2D(10, (3, 3),
                    padding='same',
                    kernel_initializer='he_normal',
                    trainable=fexnet_trainable)(fexnet)
    fexnet = MaxPooling2D([2, 2], padding='same',
                          trainable=fexnet_trainable)(fexnet)
    fexnet = Activation("relu", trainable=fexnet_trainable)(fexnet)

    fexnet = Dropout(rate=0.40, trainable=fexnet_trainable)(fexnet)

    # Transformation using sampling grid
    warped = Transformer(sampling_shape, name='sampler')([fexnet, locnet])

    # Final fully-connected layers with softmax activation
    warped = Flatten()(warped)

    warped = Dense(50, kernel_regularizer=l2(0.5))(warped)
    warped = Dense(8, kernel_regularizer=l2(0.5))(warped)
    prediction = Activation("softmax")(warped)

    return Model(inputs=image, outputs=prediction)
예제 #26
0
                                       target_size=(ROWS, COLS),
                                       color_mode='rgb',
                                       seed=1)
test_dir_iterator = DirectoryIterator(TEST_DIR,
                                      img_generator,
                                      target_size=(ROWS, COLS),
                                      color_mode='rgb',
                                      shuffle=False)

train_class_map = {v: k for k, v in train_dir_iterator.class_indices.items()}
test_class_map = {v: k for k, v in test_dir_iterator.class_indices.items()}

model = Sequential()
model.add(
    Conv2D(32, (3, 3),
           strides=(1, 1),
           padding='same',
           input_shape=(ROWS, COLS, CHANNELS)))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3), strides=(1, 1), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), strides=(1, 1), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3), strides=(1, 1), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3), strides=(1, 1), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(128, (3, 3), strides=(1, 1), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
예제 #27
0
def inception_block_1a(X):
    """
    Implementation of an inception block
    """

    X_3x3 = Conv2D(96, (1, 1),
                   data_format='channels_first',
                   name='inception_3a_3x3_conv1')(X)
    X_3x3 = BatchNormalization(axis=1,
                               epsilon=0.00001,
                               name='inception_3a_3x3_bn1')(X_3x3)
    X_3x3 = Activation('relu')(X_3x3)
    X_3x3 = ZeroPadding2D(padding=(1, 1), data_format='channels_first')(X_3x3)
    X_3x3 = Conv2D(128, (3, 3),
                   data_format='channels_first',
                   name='inception_3a_3x3_conv2')(X_3x3)
    X_3x3 = BatchNormalization(axis=1,
                               epsilon=0.00001,
                               name='inception_3a_3x3_bn2')(X_3x3)
    X_3x3 = Activation('relu')(X_3x3)

    X_5x5 = Conv2D(16, (1, 1),
                   data_format='channels_first',
                   name='inception_3a_5x5_conv1')(X)
    X_5x5 = BatchNormalization(axis=1,
                               epsilon=0.00001,
                               name='inception_3a_5x5_bn1')(X_5x5)
    X_5x5 = Activation('relu')(X_5x5)
    X_5x5 = ZeroPadding2D(padding=(2, 2), data_format='channels_first')(X_5x5)
    X_5x5 = Conv2D(32, (5, 5),
                   data_format='channels_first',
                   name='inception_3a_5x5_conv2')(X_5x5)
    X_5x5 = BatchNormalization(axis=1,
                               epsilon=0.00001,
                               name='inception_3a_5x5_bn2')(X_5x5)
    X_5x5 = Activation('relu')(X_5x5)

    X_pool = MaxPooling2D(pool_size=3, strides=2,
                          data_format='channels_first')(X)
    X_pool = Conv2D(32, (1, 1),
                    data_format='channels_first',
                    name='inception_3a_pool_conv')(X_pool)
    X_pool = BatchNormalization(axis=1,
                                epsilon=0.00001,
                                name='inception_3a_pool_bn')(X_pool)
    X_pool = Activation('relu')(X_pool)
    X_pool = ZeroPadding2D(padding=((3, 4), (3, 4)),
                           data_format='channels_first')(X_pool)

    X_1x1 = Conv2D(64, (1, 1),
                   data_format='channels_first',
                   name='inception_3a_1x1_conv')(X)
    X_1x1 = BatchNormalization(axis=1,
                               epsilon=0.00001,
                               name='inception_3a_1x1_bn')(X_1x1)
    X_1x1 = Activation('relu')(X_1x1)

    # CONCAT
    inception = concatenate([X_3x3, X_5x5, X_pool, X_1x1], axis=1)

    return inception
예제 #28
0
    out_x = x.reshape(num_images, img_rows, img_cols, 1)
    out_x = out_x / 255
    return out_x, out_y

fashion_file = "../input/fashionmnist/fashion-mnist_train.csv"
fashion_data = np.loadtxt(fashion_file, skiprows=1, delimiter=',')
x, y = prep_data(fashion_data, train_size=50000, val_size=5000)

from tensorflow.python import keras
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Flatten, Conv2D

# Your Code Here
model = Sequential()
model.add(Conv2D(12, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=(img_rows, img_cols, 1)))
model.add(Conv2D(20, kernel_size=(3, 3),
                 activation='relu'))
model.add(Conv2D(20, kernel_size=(3, 3),
                 activation='relu'))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer='adam',
              metrics=['accuracy'])
              
model.fit(x, y,
          batch_size=100,
          epochs=4,
예제 #29
0
train_images = np.zeros(shape=shape, dtype=np.float16)
for i, file in enumerate(df['id']):
    train_images[i] = Image.open('data/train/' + str(file))

kernel_size=(3,3)
pool_size=(2,2)
first_filter=32
second_filter=64
third_filter=128

dropout_conv=0.3
dropout_dense=0.3

model = Sequential()
model.add(Conv2D(first_filter, kernel_size, padding='same', activation='relu', input_shape= (32,32,3)))
model.add(Conv2D(first_filter, kernel_size, padding='same', use_bias=False))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(MaxPool2D(pool_size=pool_size))
model.add(Dropout(dropout_conv))

model.add(Conv2D(second_filter, kernel_size, padding='same', use_bias=False))
model.add(BatchNormalization())
model.add(Activation('relu'))
model.add(Conv2D(second_filter, kernel_size, padding='same', use_bias=False))
model.add(BatchNormalization())
model.add(Activation("relu"))
model.add(MaxPool2D(pool_size = pool_size))
model.add(Dropout(dropout_conv))
예제 #30
0
def generate_vgg_model_large(classes_len: int):
    """
    Function to create a VGG19 model pre-trained with custom FC Layers at the start of the network plus optional layers at
    the end before the fully connected ones as well
    If the "advanced" command line argument is selected, adds an extra convolutional layer with extra filters to support
    larger images.
    This model is a larger model that starts with two more sets of convolutional layers with less filters 
    :param classes_len: The number of classes (labels).
    :return: The VGG19 model.
    """

    model_base = Sequential()

    # Reconfigure single channel input into a greyscale 3 channel input
    img_input = Input(shape=(config.VGG_IMG_SIZE_LARGE['HEIGHT'],
                             config.VGG_IMG_SIZE_LARGE['WIDTH'], 1))
    img_conc = Concatenate()([img_input, img_input, img_input])
    input_model = Model(inputs=img_input, outputs=img_conc)

    # Generate extra convolutional layers for model to put at the beginning
    model_base.add(input_model)
    model_base.add(Conv2D(16, (3, 3), activation='relu', padding='same'))

    model_base.add(Conv2D(16, (3, 3), activation='relu', padding='same'))

    model_base.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model_base.add(Conv2D(32, (3, 3), activation='relu', padding='same'))

    model_base.add(Conv2D(32, (3, 3), activation='relu', padding='same'))

    model_base.add(MaxPooling2D((2, 2), strides=(2, 2)))

    # To ensure model fits with vgg model, we can remove the first layer from the vgg model to replace with this
    model_base.add(Conv2D(64, (3, 3), activation='relu', padding='same'))

    # Generate a VGG19 model with pre-trained ImageNet weights, input as given above, excluded fully connected layers.
    vgg_model = VGG19(include_top=False,
                      weights='imagenet',
                      input_shape=[
                          config.VGG_IMG_SIZE['HEIGHT'],
                          config.VGG_IMG_SIZE['HEIGHT'], 3
                      ])

    # Crop vgg model to exlude input layer and first convolutional layer
    vgg_model_cropped = Sequential()
    for layer in vgg_model.layers[2:]:  # go through until last layer
        vgg_model_cropped.add(layer)

    # Combine the models
    combined_model = Sequential()
    combined_model.add(model_base)
    combined_model.add(vgg_model_cropped)

    # Add fully connected layers
    model = Sequential()
    # Start with base model consisting of convolutional layers
    model.add(combined_model)

    # Generate additional convolutional layers
    if config.model == "advanced":
        model.add(Conv2D(1024, (3, 3), activation='relu', padding='same'))
        model.add(Conv2D(1024, (3, 3), activation='relu', padding='same'))
        model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    # Flatten layer to convert each input into a 1D array (no parameters in this layer, just simple pre-processing).
    model.add(Flatten())

    # Add fully connected hidden layers.
    model.add(Dense(units=512, activation='relu', name='Dense_Intermediate_1'))
    model.add(Dense(units=32, activation='relu', name='Dense_Intermediate_2'))

    # Possible dropout for regularisation can be added later and experimented with:
    # model.add(Dropout(0.1, name='Dropout_Regularization'))

    # Final output layer that uses softmax activation function (because the classes are exclusive).
    if classes_len == 2:
        model.add(Dense(1, activation='sigmoid', name='Output'))
    else:
        model.add(Dense(classes_len, activation='softmax', name='Output'))

    # Print model details if running in debug mode.
    if config.verbose_mode:
        model.summary()

    return model