Exemplo n.º 1
0
def squeeze_and_excitation(inputs, reduction_ratio=4):
    """
        paper:Squeeze-and-Excitation Networks
        在特征信息中,通道之间存在依赖关系,通过senet可以显示地去表示
        这些通道之间的依赖关系。采用神经网络学习每个通道的weights,
        重要的通道给予大的weights,不重要的通道给予小的weights。
    Args:
        inputs: features
        reduction_ratio: the reduction ratio

    Returns: y: scale (1, 1, C)

    """
    channels = inputs.shape[-1]

    c_reduce = (channels // reduction_ratio)

    # (batch, c)
    y = layers.GlobalMaxPool2D()(inputs)
    # (batch, c/r)
    y = layers.Dense(c_reduce)(y)
    y = layers.ReLU()(y)
    # (batch, c)
    y = layers.Dense(channels)(y)
    y = keras.activations.sigmoid(y)

    y = layers.Reshape(target_shape=(1, 1, -1))(y)

    return y
Exemplo n.º 2
0
def encoder_simple_res(observation_space):
    inputs = keras.Input(observation_space['obs'].shape, name='obs')
    x = layers.Conv2D(32,
                      3,
                      padding='same',
                      activation='relu',
                      name='encoder_conv1')(inputs)
    x = res_block(x, 2, name='encoder_resblock1')
    x = layers.Conv2D(64,
                      3,
                      padding='same',
                      strides=2,
                      activation='relu',
                      name='encoder_bottleneck1')(x)
    x = res_block(x, 2, name='encoder_resblock2')
    x = layers.Conv2D(128,
                      3,
                      padding='same',
                      strides=2,
                      activation='relu',
                      name='encoder_bottleneck2')(x)
    x = res_block(x, 2, name='encoder_resblock3')
    x = layers.Conv2D(256,
                      3,
                      padding='same',
                      strides=2,
                      name='encoder_bottleneck3')(x)
    outputs = layers.GlobalMaxPool2D(name='encoder_pool', dtype='float32')(x)
    return outputs, [inputs]
Exemplo n.º 3
0
    def __init__(self, num_classes, initial_filters=16, **kwargs):
        super(ResNet, self).__init__(**kwargs)

        self.stem = layers.Conv2D(initial_filters,
                                  3,
                                  strides=3,
                                  padding='valid')

        # 一共包含8个ResnetBlock模块,16+ 根连接的1层 + 输出的1层。
        # 就是分成了4组,每组的第一个完成了高和宽的降维;第二个Strides=1就是维度保持不变。
        self.blocks = keras.models.Sequential([
            ResnetBlock(initial_filters * 2, strides=3),
            ResnetBlock(initial_filters * 2, strides=1),
            # layers.Dropout(rate=0.5),
            ResnetBlock(initial_filters * 4, strides=3),
            ResnetBlock(initial_filters * 4, strides=1),
            ResnetBlock(initial_filters * 8, strides=2),
            ResnetBlock(initial_filters * 8, strides=1),
            ResnetBlock(initial_filters * 16, strides=2),
            ResnetBlock(initial_filters * 16, strides=1),
        ])

        self.final_bn = layers.BatchNormalization()
        self.avg_pool = layers.GlobalMaxPool2D()
        self.fc = layers.Dense(num_classes)  # 全连接层
Exemplo n.º 4
0
def small_resnet():
    '''
    小型残差网络
    '''
    inputs = keras.Input(shape=(28,28,1), name='img')
    h1 = layers.Conv2D(32, 3, activation='relu')(inputs)
    h1 = layers.Conv2D(64, 3, activation='relu')(h1)
    block1_out = layers.MaxPooling2D(3)(h1)

    h2 = layers.Conv2D(64, 3, activation='relu', padding='same')(block1_out)
    h2 = layers.Conv2D(64, 3, activation='relu', padding='same')(h2)
    block2_out = layers.add([h2, block1_out])

    h3 = layers.Conv2D(64, 3, activation='relu', padding='same')(block2_out)
    h3 = layers.Conv2D(64, 3, activation='relu', padding='same')(h3)
    block3_out = layers.add([h3, block2_out])

    h4 = layers.Conv2D(64, 3, activation='relu')(block3_out)
    h4 = layers.GlobalMaxPool2D()(h4)
    h4 = layers.Dense(256, activation='relu')(h4)
    h4 = layers.Dropout(0.5)(h4)
    outputs = layers.Dense(10, activation='softmax')(h4)

    model = keras.Model(inputs, outputs, name='small resnet')
    model.summary()
    keras.utils.plot_model(model, 'small_resnet_model.png', show_shapes=True)
    return model
Exemplo n.º 5
0
def build_res_net():
    inputs = keras.Input(shape=(32,32,3), name='img')
    h1 = layers.Conv2D(32, 3, activation='relu')(inputs)
    h1 = layers.Conv2D(64, 3, activation='relu')(h1)
    block1_out = layers.MaxPooling2D(3)(h1)

    h2 = layers.Conv2D(64, 3, activation='relu', padding='same')(block1_out)
    h2 = layers.Conv2D(64, 3, activation='relu', padding='same')(h2)
    block2_out = layers.add([h2, block1_out])

    h3 = layers.Conv2D(64, 3, activation='relu', padding='same')(block2_out)
    h3 = layers.Conv2D(64, 3, activation='relu', padding='same')(h3)
    block3_out = layers.add([h3, block2_out])

    h4 = layers.Conv2D(64, 3, activation='relu')(block3_out)
    h4 = layers.GlobalMaxPool2D()(h4)
    h4 = layers.Dense(256, activation='relu')(h4)
    h4 = layers.Dropout(0.5)(h4)
    outputs = layers.Dense(10, activation='softmax')(h4)

    model = keras.Model(inputs, outputs, name='small resnet')
    model.summary()
    keras.utils.plot_model(model, 'small_resnet_model.png', show_shapes=True)
    (x_train, y_train), (x_test, y_test) = keras.datasets.cifar10.load_data()
    x_train = x_train.astype('float32') / 255
    x_test = y_train.astype('float32') / 255
    y_train = keras.utils.to_categorical(y_train, 10)
    y_test = keras.utils.to_categorical(y_test, 10)

    model.compile(optimizer=keras.optimizers.RMSprop(1e-3), loss='categorical_crossentropy', metrics=['acc'])
    model.fit(x_train, y_train, batch_size=64, epochs=1, validation_split=0.2)
Exemplo n.º 6
0
def build_tensorflow_cnn_classifier(input_shape=(None, None, 3),
                                    n_class=3,
                                    classifier_activation='softmax'):
    inputs = tf.keras.Input(shape=input_shape)
    x = cnn_block(inputs,
                  8,
                  kernel_size=3,
                  stride=3,
                  name='block1',
                  conv_shortcut=True)
    x = cnn_block(x,
                  16,
                  kernel_size=3,
                  stride=2,
                  name='block2',
                  conv_shortcut=True)
    x = cnn_block(x,
                  32,
                  kernel_size=3,
                  stride=2,
                  name='block3',
                  conv_shortcut=True)
    x = layers.GlobalMaxPool2D()(x)
    outputs = layers.Dense(n_class,
                           activation=classifier_activation,
                           name='predictions')(x)
    model = tf.keras.Model(inputs=inputs, outputs=outputs, name="test_model")
    return model
Exemplo n.º 7
0
    def make_model(self):
        if self.mode == 'train':
            dataset = imitation_dataset()
            obs_input, action_gt = dataset.get_next()
            obs_input = Input(tensor=obs_input)
        elif self.mode == 'eval':
            obs_input = Input(shape=[84, 84, 4])

        obs_feature = layers.Conv2D(32, 8, 4, padding='same', activation='relu')(obs_input) # 21
        obs_feature = layers.Conv2D(64, 4, 2, padding='same', activation='relu')(obs_feature)  # 11
        obs_feature = layers.Conv2D(128, 3, 2, padding='same', activation='relu')(obs_feature)  # 6
        obs_feature = layers.Conv2D(256, 3, 2, padding='same', activation='relu')(obs_feature)  # 3
        obs_feature = layers.GlobalMaxPool2D()(obs_feature)
        obs_feature = layers.Flatten()(obs_feature)
        obs_feature = layers.Dense(128, activation='relu')(obs_feature)
        action_out = layers.Dense(action_space, activation='softmax')(obs_feature)

        model = tf.keras.Model(inputs=[obs_input], outputs=[action_out])

        if self.mode == 'train':
            model.compile(optimizer='adam',
                          loss='sparse_categorical_crossentropy',
                          metrics=['accuracy'],
                          target_tensors=[action_gt]
                          )
        else:
            model.compile(optimizer='adam',
                          loss='sparse_categorical_crossentropy',
                          metrics=['accuracy']
                          )
        return model
Exemplo n.º 8
0
 def __init__(self):
     super().__init__()
     self.conv1 = layers.Conv2D(32, 3, padding='same', activation='relu')
     self.conv2 = layers.Conv2D(64, 3, padding='same', activation='relu')
     self.conv3 = layers.Conv2D(128, 3, padding='same', activation='relu')
     self.maxpool = layers.MaxPool2D(strides=2)
     self.globalpool = layers.GlobalMaxPool2D()
Exemplo n.º 9
0
def tensorboard_test():

    # 定义自适应学习率函数(自定义参数)
    def lr_sche(epoch):
        learning_rate=0.2
        if epoch>5:
            learning_rate=0.02
        if epoch>10:
            learning_rate = 0.01
        if epoch>20:
            learning_rate = 0.005

        # 记录自定义变量
        tf.summary.scalar('learning_rate',data=learning_rate,step=epoch)
        return learning_rate

    # 加载数据集
    (train_image, train_labels), (test_image, test_labels) = tf.keras.datasets.mnist.load_data()

    # 扩充维度,转换数据类型,归一化
    train_image = tf.expand_dims(train_image, -1)
    # train_labels=tf.expand_dims(train_labels, -1) #标签项无需修改
    train_image = tf.cast(train_image / 255, tf.float32)
    train_labels = tf.cast(train_labels, tf.int64)

    # 将数据转化为 tensor
    ds = tf.data.Dataset.from_tensor_slices((train_image, train_labels))
    ds = ds.repeat().shuffle(1000).batch(128)
    # 测试数据
    # 扩充维度,转换数据类型,归一化
    test_image = tf.expand_dims(test_image, -1)
    # train_labels=tf.expand_dims(train_labels, -1) #标签项无需修改
    test_image = tf.cast(test_image / 255, tf.float32)
    test_labels = tf.cast(test_labels, tf.int64)

    # 将数据转化为 tensor
    test_ds = tf.data.Dataset.from_tensor_slices((test_image, test_labels))

    # 数据乱序 分batch
    test_ds = test_ds.batch(128)

    # 构建模型
    model=tf.keras.Sequential()
    model.add(layers.Conv2D(16,[3,3],activation='relu',input_shape=(28,28,1)))
    model.add(layers.Conv2D(32,[3,3],activation='relu'))
    model.add(layers.GlobalMaxPool2D())
    model.add(layers.Dense(10,activation='softmax'))
    model.compile(optimizer=tf.optimizers.Adam(),loss=tf.losses.SparseCategoricalCrossentropy(),metrics=['accuracy'])
    log_dir_str = 'log/'+datetime.datetime.now().strftime('%y%m%d-%H%M%S')
    tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir_str,histogram_freq=1)
    lr_callback = tf.keras.callbacks.LearningRateScheduler(lr_sche)
    file_writer = tf.summary.create_file_writer(log_dir_str+'/lr')     #实例化文件编写器
    file_writer.set_as_default() # 设置为默认文件编写器


    model.fit(ds,validation_data=test_ds,
              epochs=10,steps_per_epoch=len(train_image)//128,
              validation_steps=len(test_image)//128,
              callbacks=[tensorboard_callback,lr_callback])
Exemplo n.º 10
0
 def _buildCameraModel(self):
   imgInput =  keras.Input(shape=(self._imgReshapeWithDepth), name='img_input')
   x = layers.BatchNormalization()(imgInput)
   x = layers.Conv2D(16, (3,3),activation="relu", name="input_Conv")(x)
   x = layers.BatchNormalization()(x)
   x = layers.GlobalMaxPool2D(name="camera_maxPool_output")(x)
   x = layers.BatchNormalization()(x)
   return (imgInput,x)
Exemplo n.º 11
0
 def __init__(self, dim):
     super(_MP, self).__init__()
     if dim == 1:
         self.pool = layers.GlobalMaxPool1D()
     elif dim == 2:
         self.pool = layers.GlobalMaxPool2D()
     elif dim == 3:
         self.pool = layers.GlobalMaxPool3D()
Exemplo n.º 12
0
 def __init__(self):
     super().__init__()
     self.conv = layers.Conv2D(128,1)
     self.bn1 = layers.BatchNormalization()
     self.flatten  = layers.Flatten()
     self.globalmaxpool = layers.GlobalMaxPool2D()
     self.globalavgpool = layers.GlobalAveragePooling2D()
     self.fc = layers.Dense(128)
def ConvLSTM(in_shape, name):
    inputs = layers.Input(in_shape)
    x = layers.ConvLSTM2D(filters=8, kernel_size=4, strides=4)(inputs)
    x = layers.BatchNormalization()(x)
    x = layers.GlobalMaxPool2D(name='global_average_pooling')(
        x
    )  # Global Average Pooling act as dropout; so no need to add a dropout layers after
    return tf.keras.Model(inputs, x, name=name)
Exemplo n.º 14
0
    def __init__(self, data_format="channels_last", **kwargs):
        super(GlobalAvgMaxPool2D, self).__init__(**kwargs)
        self.axis = get_channel_axis(data_format)

        self.avg_pool = nn.GlobalAvgPool2D(data_format=data_format,
                                           name="avg_pool")
        self.max_pool = nn.GlobalMaxPool2D(data_format=data_format,
                                           name="max_pool")
def ResNetHead():
    channels = None  # Do not pad input with channels
    input_shape = (8, 9, 2048)

    model = keras.models.Sequential([
        layers.GlobalMaxPool2D(input_shape=input_shape),
        layers.Dense(1024),
        layers.Dense(len(birdcodes.bird_code), activation="sigmoid"),
    ])

    return model, input_shape, channels
Exemplo n.º 16
0
def ResUnet50(input_shape=(512, 512, 3), num_classes=200):
    """Network Architecture"""
    inputs = layers.Input(shape=input_shape)
    resnet50 = tf.keras.applications.ResNet50(weights='imagenet',
                                              include_top=False,
                                              pooling=None,
                                              input_tensor=inputs)

    assert resnet50.layers[4].name == "conv1_relu"
    C1 = resnet50.layers[4].output  # 256 x 256 x 64
    assert resnet50.layers[38].name == "conv2_block3_out"
    C2 = resnet50.layers[38].output  # 128 x 128 x 256
    assert resnet50.layers[80].name == "conv3_block4_out"
    C3 = resnet50.layers[80].output  # 64 x 64 x 512
    assert resnet50.layers[142].name == "conv4_block6_out"
    C4 = resnet50.layers[142].output  # 32 x 32 x 1024
    assert resnet50.layers[-1].name == "conv5_block3_out"
    C5 = resnet50.layers[-1].output  # 16 x 16 x 2048
    # classification subnet
    label = layers.GlobalMaxPool2D()(C5)
    label = layers.Flatten()(label)
    label = layers.Dense(num_classes, activation='softmax')(label)
    # segmentation subnet
    conv_config = {
        'activation': 'relu',
        'padding': 'same',
        'kernel_initializer': 'he_normal'
    }
    up6 = layers.Conv2D(512, 3, **conv_config)(
        layers.UpSampling2D(size=(2, 2))(C5))  # 32 x 32 x 512
    merge6 = layers.concatenate([C4, up6], axis=3)  # 32 x 32 x 1536
    conv6 = layers.Conv2D(512, 3, **conv_config)(merge6)  # 32 x 32 x 512
    conv6 = layers.Conv2D(512, 3, **conv_config)(conv6)  # 32 x 32 x 512
    up7 = layers.Conv2D(256, 3, **conv_config)(
        layers.UpSampling2D(size=(2, 2))(conv6))  # 64 x 64 x 256
    merge7 = layers.concatenate([C3, up7], axis=3)  # 64 x 64 x 768
    conv7 = layers.Conv2D(256, 3, **conv_config)(merge7)  # 64 x 64 x 256
    conv7 = layers.Conv2D(256, 3, **conv_config)(conv7)  # 64 x 64 x 256
    up8 = layers.Conv2D(128, 3, **conv_config)(
        layers.UpSampling2D(size=(2, 2))(conv7))  # 128 x 128 x 128
    merge8 = layers.concatenate([C2, up8], axis=3)  # 128 x 128 x 384
    conv8 = layers.Conv2D(128, 3, **conv_config)(merge8)  # 128 x 128 x 128
    conv8 = layers.Conv2D(128, 3, **conv_config)(conv8)  # 128 x 128 x 128
    up9 = layers.Conv2D(64, 3, **conv_config)(
        layers.UpSampling2D(size=(2, 2))(conv8))  # 256 x 256 x 64
    merge9 = layers.concatenate([C1, up9], axis=3)  # 256 x 256 x 128
    conv9 = layers.Conv2D(64, 3, **conv_config)(merge9)  # 256 x 256 x 64
    conv9 = layers.Conv2D(64, 3, **conv_config)(conv9)  # 256 x 256 x 64
    up10 = layers.Conv2D(2, 3, **conv_config)(
        layers.UpSampling2D(size=(2, 2))(conv9))  # 512 x 512 x 2
    mask = layers.Conv2D(1, 1, activation='sigmoid')(up10)
    model = tf.keras.Model(inputs=inputs, outputs=[label, mask])
    return model
Exemplo n.º 17
0
    def build(self, input_shape):
        # Build MLP layer
        for mlp_setting in self.mlp:
            self.mlp_list.append(MLP(**mlp_setting))

        # Build pooling layer
        if self.pooling == 'max':
            self.pooling_layer = layers.GlobalMaxPool2D(
                data_format=self.data_format)
        else:
            self.pooling_layer = layers.GlobalAveragePooling2D(
                data_format=self.data_format)

        # Build dense layer
        for dense_setting, batch_norm_setting in zip(self.dense,
                                                     self.batch_norm):
            self.dense_list.append(layers.Dense(**dense_setting))
            if batch_norm_setting is not None:
                self.batch_norm_list.append(
                    layers.BatchNormalization(**batch_norm_setting))
            else:
                self.batch_norm_list.append(None)

        # Add weights
        if self.dense:
            shape = (self.dense[-1]['units'],
                     input_shape[self.chan_axis] * input_shape[self.chan_axis])
        elif self.mlp:
            shape = (self.mlp[-1]['filters'],
                     input_shape[self.chan_axis] * input_shape[self.chan_axis])
        else:
            shape = (input_shape[self.chan_axis],
                     input_shape[self.chan_axis] * input_shape[self.chan_axis])

        self.w = self.add_weight(name='w',
                                 shape=shape,
                                 initializer='zeros',
                                 trainable=True)
        self.b = self.add_weight(name='b',
                                 shape=(input_shape[self.chan_axis],
                                        input_shape[self.chan_axis]),
                                 initializer='identity',
                                 trainable=True)

        # Build reshape layer
        self.reshape = layers.Reshape(
            target_shape=(input_shape[self.chan_axis],
                          input_shape[self.chan_axis]))

        # Call build function of the keras base layer
        super(FeatureTransformNet, self).build(input_shape)
Exemplo n.º 18
0
def ResNet9(input_size=(32, 32, 3), head_len=128, classes=10):
    """A small 9-layer ResNet Tensorflow model for cifar10 image classification.
    The model architecture is from https://github.com/davidcpage/cifar10-fast

    Args:
        input_size: The size of the input tensor (height, width, channels).
        classes: The number of outputs the model should generate.

    Raises:
        ValueError: Length of `input_size` is not 3.
        ValueError: `input_size`[0] or `input_size`[1] is not a multiple of 16.

    Returns:
        A TensorFlow ResNet9 model.
    """

    # prep layers
    inp = layers.Input(shape=input_size)
    x = layers.Conv2D(64, 3, padding='same')(inp)
    x = layers.BatchNormalization(momentum=0.8)(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    # layer1
    x = layers.Conv2D(128, 3, padding='same')(x)
    x = layers.MaxPool2D()(x)
    x = layers.BatchNormalization(momentum=0.8)(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.Add()([x, residual(x, 128)])
    # layer2
    x = layers.Conv2D(256, 3, padding='same')(x)
    x = layers.MaxPool2D()(x)
    x = layers.BatchNormalization(momentum=0.8)(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    # layer3
    x = layers.Conv2D(512, 3, padding='same')(x)
    x = layers.MaxPool2D()(x)
    x = layers.BatchNormalization(momentum=0.8)(x)
    x = layers.LeakyReLU(alpha=0.1)(x)
    x = layers.Add()([x, residual(x, 512)])
    # layers4
    x = layers.GlobalMaxPool2D()(x)
    code = layers.Flatten()(x)

    p_head = layers.Dense(head_len)(code)
    model_con = tf.keras.Model(inputs=inp, outputs=p_head)

    s_head = layers.Dense(classes)(code)
    s_head = layers.Activation('softmax', dtype='float32')(s_head)
    model_finetune = tf.keras.Model(inputs=inp, outputs=s_head)

    return model_con, model_finetune
def ResNet(weights="imagenet"):
    """
    Resnet model
    :param weights: None or "imagenet" pretraining
    :return:
    """
    channels = 3
    input_shape = spectrogram_dim + (3, )
    model = keras.models.Sequential([
        ResNet50(input_shape=input_shape, include_top=False, weights=weights),
        layers.GlobalMaxPool2D(input_shape=(8, 9, 2048)),
        layers.Dense(1024, activation="relu"),
        layers.Dense(len(birdcodes.bird_code), activation="sigmoid"),
    ])
    return model, input_shape, channels
Exemplo n.º 20
0
    def build(self, input_shape):
        # Build split layer (split input in point coordinates and point features)
        if self.data_format == 'channels_last':
            self.lambda1 = layers.Lambda(lambda x: x[:, :, 0:3])
            self.lambda2 = layers.Lambda(lambda x: x[:, :, 3:])
        elif self.data_format == 'channels_first':
            self.lambda1 = layers.Lambda(lambda x: x[:, 0:3, :])
            self.lambda2 = layers.Lambda(lambda x: x[:, 3:, :])

        # Build permutation (transponation) layer
        if self.data_format == 'channels_first':
            self.permut = layers.Permute(dims=(2, 1))

        # Build input transformation layer
        self.input_trans_net = InputTransformNet(**self.input_trans)

        # Build concatenation layer
        self.concat = layers.Concatenate(axis=self.chan_axis)

        # Set kernel size of the first mlp layer
        if self.data_format == 'channels_last':
            self.mlp[0].update(
                {'kernel_size': [1, input_shape[self.chan_axis]]})
        else:
            self.mlp[0].update(
                {'kernel_size': [input_shape[self.chan_axis], 1]})

        # Build first MLP layer
        for mlp_setting in self.mlp:
            self.mlp_list.append(MLP(**mlp_setting))

        # Build feature transformation layer
        self.feat_trans_net = FeatureTransformNet(**self.feature_trans)

        # Build second MLP layer
        for mlp_setting in self.mlp2:
            self.mlp2_list.append(MLP(**mlp_setting))

        # Build pooling layer
        if self.pooling == 'max':
            self.pooling_layer = layers.GlobalMaxPool2D(
                data_format=self.data_format)
        else:
            self.pooling_layer = layers.GlobalAveragePooling2D(
                data_format=self.data_format)

        # Call build function of the keras base layer
        super(Encoder, self).build(input_shape)
Exemplo n.º 21
0
    def __init__(self, in_height):
        super(Embedder, self).__init__()

        self.relu = layers.LeakyReLU()

        # in 6*224*224
        # self.pad = Padding(in_height)  # out 256*256*6
        self.resDown1 = ResBlockDown(6, 64)  # out 128*128*64
        self.resDown2 = ResBlockDown(64, 128)  # out 64*64*128
        self.resDown3 = ResBlockDown(128, 256)  # out 32*32*256
        self.self_att = SelfAttention(256)  # out 32*32*256
        self.resDown4 = ResBlockDown(256, 512)  # out 16*16*512
        self.resDown5 = ResBlockDown(512, 512)  # out 8*8*512
        self.resDown6 = ResBlockDown(512, 512)  # out 4*4*512
        # self.sum_pooling = nn.AdaptiveMaxPool2d((1, 1))  # out 1*1*512
        self.sum_pooling = layers.GlobalMaxPool2D()
Exemplo n.º 22
0
    def __init__(self,
                 channels,
                 reduction_ratio=16,
                 data_format="channels_last",
                 **kwargs):
        super(ChannelGate, self).__init__(**kwargs)
        self.data_format = data_format

        self.avg_pool = nn.GlobalAvgPool2D(data_format=data_format,
                                           name="avg_pool")
        self.max_pool = nn.GlobalMaxPool2D(data_format=data_format,
                                           name="max_pool")
        self.mlp = MLP(channels=channels,
                       reduction_ratio=reduction_ratio,
                       data_format=data_format,
                       name="mlp")
        self.sigmoid = tf.nn.sigmoid
Exemplo n.º 23
0
    def __init__(self,
                 vocab_size: int,
                 embed_dim: int,
                 hidden_size: int = 128,
                 training: bool = False):
        super(MyAdvancedModel, self).__init__()
        ### TODO(Students) START
        # ...

        self.num_classes = len(ID_TO_CLASS)

        self.decoder = layers.Dense(units=self.num_classes,
                                    activation="softmax")
        self.omegas = tf.Variable(tf.random.normal((hidden_size * 2, 1)))
        self.embeddings = tf.Variable(tf.random.normal(
            (vocab_size, embed_dim)))

        ### TODO(Students) START
        # ...
        self.vocab_size = vocab_size
        self.embed_dim = embed_dim
        self.hidden_size = hidden_size

        self.conv_layer = layers.Conv1D(filters=128,
                                        kernel_size=3,
                                        padding="same",
                                        strides=1,
                                        activation="relu",
                                        data_format="channels_last",
                                        input_shape=(None, None, 1))

        self.conv2d_layer = layers.Conv2D(filters=64,
                                          kernel_size=3,
                                          padding="same",
                                          activation="relu",
                                          data_format="channels_last",
                                          input_shape=(None, None, 1))

        self.maxpool_layer = layers.GlobalMaxPool1D()
        self.maxpool2d_layer = layers.GlobalMaxPool2D()

        self.flatten_layer = layers.Flatten()
        self.training = training

        self.dropout = layers.Dropout(0.4)
def plotModel():
    encode_input = keras.Input(shape=(28, 28, 1), name='src_img')
    h1 = layers.Conv2D(16, 3, activation='relu')(encode_input)
    h1 = layers.Conv2D(32, 3, activation='relu')(h1)
    h1 = layers.MaxPool2D(3)(h1)
    h1 = layers.Conv2D(32, 3, activation='relu')(h1)
    h1 = layers.Conv2D(16, 3, activation='relu')(h1)
    encode_output = layers.GlobalMaxPool2D()(h1)

    encode_model = keras.Model(inputs=encode_input,
                               outputs=encode_output,
                               name='encoder')

    encode_model.summary()

    decode_input = keras.Input(shape=(16, ), name='encoded_img')
    h2 = layers.Reshape((4, 4, 1))(decode_input)
    h2 = layers.Conv2DTranspose(16, 3, activation='relu')(h2)
    h2 = layers.Conv2DTranspose(32, 3, activation='relu')(h2)
    h2 = layers.UpSampling2D(3)(h2)
    h2 = layers.Conv2DTranspose(16, 3, activation='relu')(h2)
    decode_output = layers.Conv2DTranspose(1, 3, activation='relu')(h2)
    decode_model = keras.Model(inputs=decode_input,
                               outputs=decode_output,
                               name='decoder')
    decode_model.summary()

    autoencoder_input = keras.Input(shape=(28, 28, 1), name='img')
    h3 = encode_model(autoencoder_input)
    autoencoder_output = decode_model(h3)
    autoencoder = keras.Model(inputs=autoencoder_input,
                              outputs=autoencoder_output,
                              name='autoencoder')
    autoencoder.summary()
    keras.utils.plot_model(
        autoencoder,
        r'C:\\Users\\mih\\Desktop\\NN_Graph\\autoencoder_model.png')
    keras.utils.plot_model(
        autoencoder,
        r'C:\\Users\\mih\\Desktop\\NN_Graph\\autoencoder_info.png',
        show_shapes=True)

    autoencoder.fit()
Exemplo n.º 25
0
def build_shared_net():
    encode_input = keras.Input(shape=(28,28,1), name='img')
    h1 = layers.Conv2D(16, 3, activation='relu')(encode_input)
    h1 = layers.Conv2D(32, 3, activation='relu')(h1)
    h1 = layers.MaxPool2D(3)(h1)
    h1 = layers.Conv2D(32, 3, activation='relu')(h1)
    h1 = layers.Conv2D(16, 3, activation='relu')(h1)
    encode_output = layers.GlobalMaxPool2D()(h1)
    encode_model = keras.Model(inputs=encode_input, outputs=encode_output, name='encoder')
    encode_model.summary()

    h2 = layers.Reshape((4, 4, 1))(encode_output)
    h2 = layers.Conv2DTranspose(16, 3, activation='relu')(h2)
    h2 = layers.Conv2DTranspose(32, 3, activation='relu')(h2)
    h2 = layers.UpSampling2D(3)(h2)
    h2 = layers.Conv2DTranspose(16, 3, activation='relu')(h2)
    decode_output = layers.Conv2DTranspose(1, 3, activation='relu')(h2)

    autoencoder = keras.Model(inputs=encode_input, outputs=decode_output, name='autoencoder')
    autoencoder.summary()
Exemplo n.º 26
0
def MolMapResNet(input_shape,
                 num_resnet_blocks=8,
                 n_outputs=1,
                 dense_layers=[128, 32],
                 dense_avf='relu',
                 last_avf=None):
    """
    parameters
    ----------------------
    molmap_shape: w, h, c
    num_resnet_blocks: int
    n_outputs: output units
    dense_layers: list, how many dense layers and units
    dense_avf: activation function for dense layers
    last_avf: activation function for last layer
    """
    tf.keras.backend.clear_session()
    inputs = tf.keras.Input(input_shape)  #input_shape = (24, 24, 3)
    #     x = layers.Conv2D(32, 11, activation='relu')(inputs)
    #     x = layers.Conv2D(64, 3, activation='relu')(x)
    #     x = layers.MaxPooling2D(3)(x)
    x = Conv2D(64, 13, padding='same', activation='relu', strides=1)(inputs)
    x = MaxPool2D(pool_size=3, strides=2, padding='same')(x)  #p1

    ## renet block
    for i in range(num_resnet_blocks):
        x = resnet_block(x, 64, 3)

    x = layers.Conv2D(256, 3, activation='relu')(x)
    x = layers.GlobalMaxPool2D()(x)

    ## dense layer
    for units in dense_layers:
        x = layers.Dense(units, activation=dense_avf)(x)

    #last layer
    outputs = Dense(n_outputs, activation=last_avf)(x)

    model = tf.keras.Model(inputs=inputs, outputs=outputs)

    return model
Exemplo n.º 27
0
    def __init__(self, num_classes, initial_filters=16, **kwargs):
        super(ResNet, self).__init__(**kwargs)
        self.stem = layers.Conv2D(initial_filters,
                                  3,
                                  strides=3,
                                  padding='valid')
        self.blocks = keras.models.Sequential([
            ResnetBlock(initial_filters * 2, strides=3),
            ResnetBlock(initial_filters * 2, strides=1),
            # layers.Dropout(rate=0.5),
            ResnetBlock(initial_filters * 4, strides=3),
            ResnetBlock(initial_filters * 4, strides=1),
            ResnetBlock(initial_filters * 8, strides=2),
            ResnetBlock(initial_filters * 8, strides=1),
            ResnetBlock(initial_filters * 16, strides=2),
            ResnetBlock(initial_filters * 16, strides=1),
        ])

        self.final_bn = layers.BatchNormalization()
        self.avg_pool = layers.GlobalMaxPool2D()
        self.fc = layers.Dense(num_classes)
Exemplo n.º 28
0
def get_vgg19(classes=9,
              input_shape=(224, 224, 3),
              base_layer_trainable=False):
    from tensorflow.keras.applications.vgg19 import VGG19
    base_model = VGG19(include_top=False, input_shape=input_shape)
    for layer in base_model.layers:
        layer.trainable = base_layer_trainable
    head_model = KL.GlobalMaxPool2D()(base_model.output)
    head_model = KL.Dense(1024,
                          activation='relu',
                          name='00',
                          kernel_initializer='he_uniform')(head_model)
    head_model = KL.Dropout(0.5)(head_model)
    head_model = KL.Dense(1024,
                          activation='relu',
                          name='01',
                          kernel_initializer='he_uniform')(head_model)
    head_model = KL.Dropout(0.5)(head_model)
    head_model = KL.Dense(classes, activation='softmax', name='11')(head_model)
    model = KM.Model(inputs=base_model.input, outputs=head_model)
    return model
Exemplo n.º 29
0
def get_mobilenetv2(classes=9,
                    input_shape=(224, 224, 3),
                    base_layer_trainable=False):
    from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2
    base_model = MobileNetV2(include_top=False, input_shape=input_shape)
    for layer in base_model.layers:
        layer.trainable = base_layer_trainable
    head_model = KL.GlobalMaxPool2D()(base_model.output)
    head_model = KL.Dense(1024,
                          activation='relu',
                          name='0000',
                          kernel_initializer='he_uniform')(head_model)
    head_model = KL.Dropout(0.5)(head_model)
    # head_model = KL.Dense(1024, activation='relu', name='1111', kernel_initializer='he_uniform')(head_model)
    # head_model = KL.Dropout(0.5)(head_model)
    if classes == 2:
        head_model = KL.Dense(classes, activation='sigmoid',
                              name='3333')(head_model)
    else:
        head_model = KL.Dense(classes, activation='softmax',
                              name='3333')(head_model)
    model = KM.Model(inputs=base_model.input, outputs=head_model)
    return model
Exemplo n.º 30
0
    def __init__(self, filters=12, conv_count=4, alpha=0.2):
        """
        Init the layers of the model.

        Args:
            filters (int): Base number of filters, scaled by conv_count
            conv_count (int): Number of leaky conv layers to use
            alpha (float): LeakyReLU alpha param
        """
        super().__init__()

        self.conv_in = ConvBlock(filters, 5, 2, override_activation=True)

        # Interpolate kernel size from 5 to 2 over all the layers
        kernels = np.interp(
            x=[index / (conv_count - 1) for index in range(conv_count)],
            xp=[0.0, 1.0],
            fp=[5.0, 2.0],
        )
        kernels = np.round(kernels).astype(int)

        # Stack the con layers into conv_layers
        self.conv_layers = []
        for index, kernel in zip(range(2, conv_count + 2), kernels):
            self.conv_layers.append(
                LeakyConvBlock(
                    filters=index * filters,
                    kernel_size=(kernel, kernel),
                    strides=2,
                    alpha=alpha,
                ),
            )

        self.conv_out = layers.Conv2D((conv_count + 1) * filters, 2, 2, padding='same')

        self.global_pool = layers.GlobalMaxPool2D()
        self.dense = layers.Dense(1, activation='sigmoid')