コード例 #1
0
def attention_block_2d(x, g, inter_channel):
    '''

    :param x: x input from down_sampling same layer output (?,x_height,x_width,x_channel)
    :param g: gate input from up_sampling layer last output (?,g_height,g_width,g_channel)
    g_height,g_width=x_height/2,x_width/2
    :return:
    '''
    print('attention_block:')
    # theta_x(?,g_height,g_width,inter_channel)
    theta_x = Conv2D(inter_channel, [2, 2], strides=[2, 2])(x)
    print(str(theta_x.get_shape()))
    # phi_g(?,g_height,g_width,inter_channel)
    phi_g = Conv2D(inter_channel, [1, 1], strides=[1, 1])(g)
    print(str(phi_g.get_shape()))
    # f(?,g_height,g_width,inter_channel)
    f = Activation('relu')(keras.layers.add([theta_x, phi_g]))
    print(str(f.get_shape()))
    # psi_f(?,g_height,g_width,1)
    psi_f = Conv2D(1, [1, 1], strides=[1, 1])(f)
    print(str(psi_f.get_shape()))
    # sigm_psi_f(?,g_height,g_width)
    sigm_psi_f = Activation('sigmoid')(psi_f)
    print(str(sigm_psi_f.get_shape()))
    # rate(?,x_height,x_width)
    rate = UpSampling2D(size=[2, 2])(sigm_psi_f)
    print(str(rate.get_shape()))
    # att_x(?,x_height,x_width,x_channel)
    att_x = keras.layers.multiply([x, rate])
    print(str(att_x.get_shape()))
    print('-----------------')
    return att_x
コード例 #2
0
    def _add_dense_layers(self, tensor_list, layer_count=1, activation="relu",
                          dimension_multiplier: float = 1, out_dim: int = None, regularization=None, names=None):

        for layer_index in range(layer_count):
            new_list = []
            for i, item in enumerate(tensor_list):
                name = None
                if names is not None:
                    name = names(layer_index, i)
                item = Activation(activation)(TimeDistributed(name=name,
                                                              layer=Dense(out_dim if out_dim is not None else int(
                                                                  int(item.get_shape()[-1]) * dimension_multiplier),
                                                                          kernel_regularizer=regularization,
                                                                          bias_regularizer=regularization))(item))
                new_list.append(item)
            tensor_list = new_list

        return tensor_list
コード例 #3
0
def unet_model_2d_attention(input_shape,
                            n_labels,
                            batch_normalization=False,
                            initial_learning_rate=0.00001,
                            metrics=m.MIoU):
    """
    input_shape:without batch_size,(img_height,img_width,img_depth)
    metrics:
    """

    inputs = Input(input_shape)

    down_layer = []

    layer = inputs

    #down_layer_1
    layer = res_block_v2(layer, 64, batch_normalization=batch_normalization)
    down_layer.append(layer)
    layer = MaxPooling2D(pool_size=[2, 2], strides=[2, 2])(layer)

    print(str(layer.get_shape()))

    # down_layer_2
    layer = res_block_v2(layer, 128, batch_normalization=batch_normalization)
    down_layer.append(layer)
    layer = MaxPooling2D(pool_size=[2, 2], strides=[2, 2])(layer)

    print(str(layer.get_shape()))

    # down_layer_3
    layer = res_block_v2(layer, 256, batch_normalization=batch_normalization)
    down_layer.append(layer)
    layer = MaxPooling2D(pool_size=[2, 2], strides=[2, 2])(layer)

    print(str(layer.get_shape()))

    # down_layer_4
    layer = res_block_v2(layer, 512, batch_normalization=batch_normalization)
    down_layer.append(layer)
    layer = MaxPooling2D(pool_size=[2, 2], strides=[2, 2])(layer)

    print(str(layer.get_shape()))

    # bottle_layer
    layer = res_block_v2(layer, 1024, batch_normalization=batch_normalization)
    print(str(layer.get_shape()))

    # up_layer_4
    layer = attention_block_2d(down_layer[3], layer, 256)
    layer = res_block_v2(layer, 512, batch_normalization=batch_normalization)
    print(str(layer.get_shape()))

    # up_layer_3
    layer = attention_block_2d(down_layer[2], layer, 128)
    layer = res_block_v2(layer, 256, batch_normalization=batch_normalization)
    print(str(layer.get_shape()))

    # up_layer_2
    layer = attention_block_2d(down_layer[1], layer, 64)
    layer = res_block_v2(layer, 128, batch_normalization=batch_normalization)
    print(str(layer.get_shape()))

    # up_layer_1
    layer = attention_block_2d(down_layer[0], layer, 32)
    layer = res_block_v2(layer, 64, batch_normalization=batch_normalization)
    print(str(layer.get_shape()))

    # score_layer
    layer = Conv2D(n_labels, [1, 1], strides=[1, 1])(layer)
    print(str(layer.get_shape()))

    # softmax
    layer = Activation('softmax')(layer)
    print(str(layer.get_shape()))

    outputs = layer

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

    metrics = [metrics]

    model.compile(optimizer=Adam(lr=initial_learning_rate),
                  loss=m.MIoU_loss,
                  metrics=metrics)

    return model
コード例 #4
0
# Small path
small_window_input = Input(shape=input_shape, name="small_windows")

merged_inputs = merge([big_window_relu1, small_window_input], mode='concat')

# Local path
kernel_size = (7, 7)
nb_filters = 64
print("Input shape to the network:", merged_inputs.get_shape())
local_conv1 = Convolution2D(nb_filters,
                            kernel_size[0],
                            kernel_size[1],
                            border_mode='valid')(merged_inputs)
print("Output shape of 1st convolution:", local_conv1.get_shape())
local_relu1 = Activation('relu')(local_conv1)
print("Output shape of relu:", local_relu1.get_shape())
local_maxpool1 = MaxPooling2D(pool_size=pool_size, strides=(1, 1))(local_relu1)
#local_dropout1 = Dropout(0.25)(local_maxpool1)
print("Output shape of max pooling:", local_maxpool1.get_shape())

kernel_size = (3, 3)
nb_filters = 64
pool_size = (2, 2)
local_conv2 = Convolution2D(nb_filters,
                            kernel_size[0],
                            kernel_size[1],
                            border_mode='valid',
                            input_shape=input_shape)(local_maxpool1)
print("Output shape of 2nd convolution:", local_conv2.get_shape())
local_relu2 = Activation('relu')(local_conv2)
print("Output shape of 2nd relu:", local_relu2.get_shape())
コード例 #5
0
import keras
import numpy as np
import tensorflow as tf
import keras.backend as K
from keras.layers import Input, Merge, Dense, Lambda, Activation
from keras.layers.embeddings import Embedding
from keras.layers.convolutional import Convolution1D

def attentive(q, a):
	return tf.batch_matmul(q, a, adj_x=False, adj_y=True, name="attention")

q = Input(shape=[None,10], dtype='float32')
a = Input(shape=[None,10], dtype='float32')
qua = Activation('tanh')(
	Merge(
		mode=lambda x: attentive(*x),
		output_shape=lambda x: x[0][0:2] + x[1][2:]
	)([q, a])
)
print qua.get_shape()
q_softmax = Activation('softmax')(Lambda(lambda x: K.max(x, axis=2, keepdims=True))(qua))
print q_softmax.get_shape()
a_softmax = Activation('softmax')(Lambda(lambda x: K.max(x, axis=1, keepdims=True))(qua))
print a_softmax.get_shape()
sess = tf.Session()
print sess.run([q_softmax], {q:np.random.random((3,5,10)), a:np.random.random((3,7,10))})[0].shape
print sess.run([a_softmax], {q:np.random.random((3,5,10)), a:np.random.random((3,7,10))})[0].shape
コード例 #6
0
ファイル: keras_auto_encoder.py プロジェクト: hengyuan-hu/dem
def relu_deep_model1(input_shape, relu_max):
    input_img = Input(shape=input_shape)
    print 'input shape:', input_img._keras_shape
    # 32, 32
    x = Conv2D(64,
               3,
               3,
               activation='relu',
               border_mode='same',
               subsample=(2, 2))(input_img)
    x = BatchNormalization(mode=2, axis=3)(x)
    # 16, 16
    x = Conv2D(128,
               3,
               3,
               activation='relu',
               border_mode='same',
               subsample=(2, 2))(x)
    x = BatchNormalization(mode=2, axis=3)(x)
    # 8, 8
    x = Conv2D(256,
               3,
               3,
               activation='relu',
               border_mode='same',
               subsample=(2, 2))(x)
    x = BatchNormalization(mode=2, axis=3)(x)
    # 4, 4
    # latent_dim = (1, 1, 1024)
    x = Conv2D(1024,
               4,
               4,
               activation='linear',
               border_mode='same',
               subsample=(4, 4))(x)
    x = GaussianNoise(0.2)(x)
    # encoded = Activation('relu')(x)
    encoded = Activation(relu_n(relu_max))(x)

    print 'encoded shape:', encoded.get_shape().as_list()
    # in the origianl design, no BN as the first layer of decoder because of bug
    x = encoded
    # x = BatchNormalization(mode=2, axis=3)(encoded)

    # batch_size, h, w, _ = tf.shape(x)
    batch_size = tf.shape(x)[0]
    # dim: (1, 1, 512)
    x = Deconv2D(512,
                 4,
                 4,
                 output_shape=[batch_size, 4, 4, 512],
                 activation='relu',
                 border_mode='same',
                 subsample=(4, 4))(x)
    x = BatchNormalization(mode=2, axis=3)(x)
    # (4, 4, 512)
    x = Deconv2D(256,
                 5,
                 5,
                 output_shape=[batch_size, 8, 8, 256],
                 activation='relu',
                 border_mode='same',
                 subsample=(2, 2))(x)
    x = BatchNormalization(mode=2, axis=3)(x)
    # dim: (8, 8, 256)
    x = Deconv2D(128,
                 5,
                 5,
                 output_shape=(batch_size, 16, 16, 128),
                 activation='relu',
                 border_mode='same',
                 subsample=(2, 2))(x)
    x = BatchNormalization(mode=2, axis=3)(x)
    # dim: (16, 16, 256)
    x = Deconv2D(64,
                 5,
                 5,
                 output_shape=(batch_size, 32, 32, 64),
                 activation='relu',
                 border_mode='same',
                 subsample=(2, 2))(x)
    x = BatchNormalization(mode=2, axis=3)(x)
    # dim: (32, 32, 64)
    x = Deconv2D(3,
                 5,
                 5,
                 output_shape=(batch_size, 32, 32, 3),
                 activation='linear',
                 border_mode='same',
                 subsample=(1, 1))(x)
    decoded = BatchNormalization(mode=2, axis=3)(x)
    print 'decoded shape:', decoded.get_shape().as_list()
    autoencoder = Model(input_img, decoded)
    return autoencoder
コード例 #7
0
ファイル: keras_auto_encoder.py プロジェクト: hengyuan-hu/dem
def deep_model2(input_shape):
    input_img = Input(shape=input_shape)
    print 'input shape:', input_img._keras_shape
    # 32, 32
    x = Conv2D(32,
               3,
               3,
               activation='relu',
               border_mode='same',
               subsample=(2, 2))(input_img)
    x = BatchNormalization(mode=2, axis=3)(x)
    # 16, 16
    x = Conv2D(64,
               3,
               3,
               activation='relu',
               border_mode='same',
               subsample=(2, 2))(x)
    x = BatchNormalization(mode=2, axis=3)(x)
    # 8, 8
    x = Conv2D(128,
               3,
               3,
               activation='relu',
               border_mode='same',
               subsample=(2, 2))(x)
    x = BatchNormalization(mode=2, axis=3)(x)
    # 4, 4
    # latent_dim = (1, 1, 1024)
    x = Conv2D(1024,
               4,
               4,
               activation='linear',
               border_mode='same',
               subsample=(4, 4))(x)
    x = GaussianNoise(0.1)(x)
    encoded = Activation('sigmoid')(x)

    print 'encoded shape:', encoded.get_shape().as_list()
    # x = BatchNormalization(mode=2, axis=3)(encoded)

    # batch_size, h, w, _ = tf.shape(x)
    batch_size = tf.shape(encoded)[0]
    # dim: (1, 1, 512)
    x = Deconv2D(512,
                 4,
                 4,
                 output_shape=[batch_size, 4, 4, 512],
                 activation='relu',
                 border_mode='same',
                 subsample=(4, 4))(encoded)
    x = BatchNormalization(mode=2, axis=3)(x)
    # (4, 4, 512)
    x = Deconv2D(256,
                 5,
                 5,
                 output_shape=[batch_size, 8, 8, 256],
                 activation='relu',
                 border_mode='same',
                 subsample=(2, 2))(x)
    x = BatchNormalization(mode=2, axis=3)(x)
    # dim: (8, 8, 256)
    x = Deconv2D(128,
                 5,
                 5,
                 output_shape=(batch_size, 16, 16, 128),
                 activation='relu',
                 border_mode='same',
                 subsample=(2, 2))(x)
    x = BatchNormalization(mode=2, axis=3)(x)
    # dim: (16, 16, 256)
    x = Deconv2D(64,
                 5,
                 5,
                 output_shape=(batch_size, 32, 32, 64),
                 activation='relu',
                 border_mode='same',
                 subsample=(2, 2))(x)
    x = BatchNormalization(mode=2, axis=3)(x)
    # dim: (32, 32, 64)
    x = Deconv2D(3,
                 5,
                 5,
                 output_shape=(batch_size, 32, 32, 3),
                 activation='linear',
                 border_mode='same',
                 subsample=(1, 1))(x)
    decoded = BatchNormalization(mode=2, axis=3)(x)
    print 'decoded shape:', decoded.get_shape().as_list()
    autoencoder = Model(input_img, decoded)
    return autoencoder
コード例 #8
0
def model():
    # Unet
    input_shape = (82, 1)

    input_data = Input(shape=input_shape)
    x = Conv1D(2, kernel_size=3, padding='same')(input_data)
    #Rajouter une couche BN
    x = Activation('relu')(x)
    print("in --> Conv1D(32, 2, same) = {}".format(
        x.get_shape()))  # (?, 81, 32)

    x = Conv1D(4, kernel_size=2, padding='same')(input_data)
    x = Activation('relu')(x)
    print("Conv1D --> Conv1D(32, 2, same). Out_shape = {} \n".format(
        x.get_shape()))
    #print x.get_shape() # (?, 81, 32)

    in_pool_shape = x.get_shape()

    x = MaxPool1D(pool_size=2, padding='same')(x)
    print("MaxPool1D pool_size = 2, padding same")
    print("MaxPool1D(1st enc): in_shape = {}\tOut_shape = {}".format(
        in_pool_shape, x.get_shape()))
    print(" ")
    #

    x = Conv1D(8, kernel_size=2, padding='same')(x)
    #Rajouter une couche BN
    x = Activation('relu')(x)
    print("Pooled --> Conv1D(64, 2, same). Out_shape = {}".format(
        x.get_shape()))

    x = Conv1D(64, kernel_size=2, padding='same')(x)
    #Rajouter une couche BN
    x = Activation('relu')(x)
    print("Conv1D --> Conv1D(64, 2, same). Out_shape = {} \n".format(
        x.get_shape()))

    in_pool_shape = x.get_shape()

    x = MaxPool1D(pool_size=2, padding='same')(x)
    print("MaxPool1D pool_size = 2, padding same")
    print("MaxPool1D(2nd enc): in_shape = {}\tOut_shape = {}".format(
        in_pool_shape, x.get_shape()))
    print(" ")

    x = Conv1D(16, kernel_size=2, padding='same')(x)
    #Rajouter une couche BN
    x = Activation('relu')(x)
    print("Pooled --> Conv1D(128, 2, same). Out_shape = {}".format(
        x.get_shape()))

    x = Conv1D(16, kernel_size=2, padding='same')(x)
    #Rajouter une couche BN
    x = Activation('relu')(x)
    print("Conv1D --> Conv1D(128, 2, same). Out_shape = {} \n".format(
        x.get_shape()))

    in_samp_shape = x.get_shape()

    x = UpSampling1D(2)(x)
    print("Upsampling pool_size = 2")
    print("UpSampling(1st dec): in_shape= {}\tOut_shape = {}".format(
        in_samp_shape, x.get_shape()))
    print(" ")

    x = Conv1D(8, kernel_size=2, padding='same')(x)
    #Rajouter une couche BN
    x = Activation('relu')(x)
    print("Upsampled --> Conv1D(64, 2, same). Out_shape = {}".format(
        x.get_shape()))

    x = Conv1D(4, kernel_size=2, padding='same')(x)
    #Rajouter une couche BN
    x = Activation('relu')(x)
    print("Conv1D --> Conv1D(64, 2, same). Out_shape = {} \n".format(
        x.get_shape()))

    in_samp_shape = x.get_shape()

    x = UpSampling1D(2)(x)
    print("Upsampling pool_size = 2")
    print("UpSampling(2nd dec): in_shape= {}\tOut_shape = {}".format(
        in_samp_shape, x.get_shape()))
    print(" ")

    x = Conv1D(2, kernel_size=3, padding='valid')(x)
    #Rajouter une couche BN
    x = Activation('relu')(x)
    print("Upsampled --> Conv1D(32, 2, same). Out_shape = {}".format(
        x.get_shape()))

    x = Conv1D(1, kernel_size=1, padding='valid')(x)
    #Rajouter une couche BN
    output = Activation('relu')(x)
    print("Conv1D --> Conv1D(1, 1, same) (Output). Out_shape = {}".format(
        x.get_shape()))

    unet = Model(input_data, output, name='u_net_out')

    return unet
コード例 #9
0
ファイル: unet_3d.py プロジェクト: wps1215/brats_keras
def unet_model_3d(input_shape,
                  n_labels,
                  batch_normalization=False,
                  initial_learning_rate=0.00001,
                  metrics=m.dice_coef):
    """
    input_shape:without batch_size,(img_height,img_width,img_depth)
    metrics:
    """

    inputs = Input(input_shape)

    down_layer = []

    layer = inputs

    # down_layer_1
    layer = res_block_v2_3d(layer, 32, batch_normalization=batch_normalization)
    down_layer.append(layer)
    layer = MaxPooling3D(pool_size=[2, 2, 2],
                         strides=[2, 2, 2],
                         padding='same')(layer)

    print(str(layer.get_shape()))

    # down_layer_2
    layer = res_block_v2_3d(layer, 64, batch_normalization=batch_normalization)
    down_layer.append(layer)
    layer = MaxPooling3D(pool_size=[2, 2, 2],
                         strides=[2, 2, 2],
                         padding='same')(layer)

    print(str(layer.get_shape()))

    # down_layer_3
    layer = res_block_v2_3d(layer,
                            128,
                            batch_normalization=batch_normalization)
    down_layer.append(layer)
    layer = MaxPooling3D(pool_size=[2, 2, 2],
                         strides=[2, 2, 2],
                         padding='same')(layer)

    print(str(layer.get_shape()))

    # down_layer_4
    layer = res_block_v2_3d(layer,
                            256,
                            batch_normalization=batch_normalization)
    down_layer.append(layer)
    layer = MaxPooling3D(pool_size=[2, 2, 2],
                         strides=[2, 2, 2],
                         padding='same')(layer)

    print(str(layer.get_shape()))

    # bottle_layer
    layer = res_block_v2_3d(layer,
                            512,
                            batch_normalization=batch_normalization)
    print(str(layer.get_shape()))

    # up_layer_4
    layer = up_and_concate_3d(layer, down_layer[3])
    layer = res_block_v2_3d(layer,
                            256,
                            batch_normalization=batch_normalization)
    print(str(layer.get_shape()))

    # up_layer_3
    layer = up_and_concate_3d(layer, down_layer[2])
    layer = res_block_v2_3d(layer,
                            128,
                            batch_normalization=batch_normalization)
    print(str(layer.get_shape()))

    # up_layer_2
    layer = up_and_concate_3d(layer, down_layer[1])
    layer = res_block_v2_3d(layer, 64, batch_normalization=batch_normalization)
    print(str(layer.get_shape()))

    # up_layer_1
    layer = up_and_concate_3d(layer, down_layer[0])
    layer = res_block_v2_3d(layer, 32, batch_normalization=batch_normalization)
    print(str(layer.get_shape()))

    # score_layer
    layer = Conv3D(n_labels, [1, 1, 1], strides=[1, 1, 1])(layer)
    print(str(layer.get_shape()))

    # softmax
    layer = Activation('softmax')(layer)
    print(str(layer.get_shape()))

    outputs = layer

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

    metrics = [metrics]

    model = multi_gpu_model(model, gpus=2)
    model.summary()
    model.compile(optimizer=Adam(lr=initial_learning_rate),
                  loss='categorical_crossentropy',
                  metrics=metrics)

    return model
コード例 #10
0
def CombCN(input_frame,
           input_video,
           video_size=None,
           sampling_frame=8,
           frame_net_mid_depth=4):
    #frame_3DCN => total frames or jsut frame of Vk in Vin
    Activ = lambda x: LeakyReLU(alpha=0.2)(x)
    Bat = lambda x: BatchNormalization()(x)

    video_size = None
    if not video_size:
        W = 320
        H = 240
    else:
        W = video_size[0]
        H = video_size[1]

    if input_frame is None:
        input_frame = Input(shape=(W, H, 3))
    if input_video is None:
        input_video = Input(shape=(sampling_frame, W / 2, H / 2, 3))

    print(input_frame.get_shape())

    e0 = Conv2D(filters=32, padding='same', kernel_size=5)(input_frame)
    #e0 = Bat(e0)
    e0 = Activ(e0)
    print(e0.get_shape())
    #WITH NO CONCATENATE Init_dataloader()DING IN 3DCN BUT WITH CONCAT FOR ENCODING IN combination part

    e0_C = Conv2D(filters=64, padding='same', kernel_size=3, strides=2)(e0)
    e0_C = Bat(e0_C)
    e0_C = Activ(e0_C)
    print(e0_C.get_shape())

    #skip_subnet = CN3D(video_info=None , input_video = input_video, sampling_frame= 8,  vid_net_mid_depth = 3)
    skip_subnet = input_video
    size_subnet = skip_subnet.get_shape()
    # IS IT WHAT THE PAPER SAYS? NOT SURE
    skip_subnet = Reshape((int(W / 2), int(H / 2),
                           int(size_subnet[1] * size_subnet[4])))(skip_subnet)

    skip_subnet = Conv2D(filters=128,
                         padding='same',
                         kernel_size=5,
                         strides=1,
                         name='subnet')(skip_subnet)
    skip_subnet = Bat(skip_subnet)
    skip_subnet = Activ(skip_subnet)

    skip_subnet = Conv2D(filters=64,
                         padding='same',
                         kernel_size=3,
                         strides=1,
                         name='subnet_2')(skip_subnet)
    skip_subnet = Bat(skip_subnet)
    skip_subnet = Activ(skip_subnet)

    e0_C = Concatenate()([e0_C, skip_subnet])
    print(e0_C.get_shape())

    e1 = Conv2D(filters=256, padding='same', kernel_size=3)(e0_C)
    e1 = Bat(e1)
    e1 = Activ(e1)
    print(e1.get_shape())

    e1_C = Conv2D(filters=512, padding='same', kernel_size=4, strides=2)(e1)
    e1_C = Bat(e1_C)
    e1_C = Activ(e1_C)
    print(e1_C.get_shape())

    e2 = Conv2D(filters=512, padding='same', kernel_size=3)(e1_C)
    e2 = Bat(e2)
    e2 = Activ(e2)
    print(e2.get_shape())

    e2_C = Conv2D(filters=512, padding='same', kernel_size=4, strides=2)(e2)
    e2_C = Bat(e2_C)
    e2_C = Activ(e2_C)
    print(e2_C.get_shape())

    fc_mid = e2_C
    fc_prev = e2_C
    p_num = 2

    for i in range(frame_net_mid_depth):
        fc_mid = Conv2D(filters=512,
                        dilation_rate=p_num,
                        kernel_size=3,
                        padding='same')(fc_mid)
        fc_mid = Bat(fc_mid)
        fc_mid = Activ(fc_mid)
        f_temp = fc_mid

        fc_mid = Concatenate()([fc_mid, fc_prev])
        fc_prev = f_temp

        print(fc_mid.get_shape())
        p_num = p_num * 2

    #fc_mid = Deconvolution3D(strides=2, filters=64, kernel_size= 4, padding='same')(fc_mid)
    d0 = Concatenate()([fc_prev, e2_C])
    d0 = Conv2D(strides=1, filters=512, kernel_size=3, padding='same')(d0)
    d0 = Bat(d0)
    d0 = Activ(d0)
    print(d0.get_shape())

    d0_C = UpSampling2D()(d0)
    d0_C = Concatenate()([d0_C, e2])
    d0_C = Conv2D(strides=1, filters=512, kernel_size=4, padding='same')(d0_C)
    d0_C = Bat(d0_C)
    d0_C = Activ(d0_C)

    print(d0_C.get_shape())

    d0_CC = Concatenate()([d0_C, e1_C])
    d0_CC = Conv2D(filters=512, padding='same', kernel_size=3)(d0_CC)
    d0_CC = Bat(d0_CC)
    d0_CC = Activ(d0_CC)

    print(d0_CC.get_shape())
    d1 = UpSampling2D()(d0_CC)
    d1 = Concatenate()([d1, e1])
    d1 = Conv2D(strides=1, filters=256, kernel_size=4, padding='same')(d1)
    d1 = Bat(d1)
    d1 = Activ(d1)

    print(d1.get_shape())
    d1_C = Concatenate()([d1, e0_C])
    d1_C = Conv2D(filters=128, padding='same', kernel_size=3)(d1_C)
    d1_C = Bat(d1_C)
    d1_C = Activ(d1_C)
    print(d1_C.get_shape())

    d1_CC = UpSampling2D()(d1_C)
    d1_CC = Conv2D(strides=1, filters=64, kernel_size=4, padding='same')(d1_CC)
    d1_CC = Bat(d1_CC)
    d1_CC = Activ(d1_CC)
    print(d1_CC.get_shape())

    d1_CCC = Concatenate()([d1_CC, e0])
    d1_CCC = Conv2D(strides=1, filters=48, kernel_size=4,
                    padding='same')(d1_CCC)
    d1_CCC = Bat(d1_CCC)
    d1_CCC = Activ(d1_CCC)
    print(d1_CCC.get_shape())

    d_out = Conv2D(filters=3, padding='same', kernel_size=3)(d1_CCC)
    #d_out = Bat(d_out)
    d_out = Activation('tanh')(d_out)

    print(d_out.get_shape())

    return d_out
コード例 #11
0
ファイル: CRNN_Model.py プロジェクト: windkiss5/LicenseRec
def get_Model_VGG(training):
    if training:
        keeppro = 0.2
    else:
        keeppro = 0
    input_shape = (Global.IMG_WIDTH, Global.IMG_HEIGHT, Global.IMG_CHANNELS)
    # Make Networkw
    inputs = Input(name='the_input', shape=input_shape, dtype='float32')

    inner = Conv2D(64, (3, 3), padding='same', name='conv1')(inputs)
    inner = Activation('relu')(inner)
    inner = BatchNormalization()(inner)
    inner = MaxPooling2D(pool_size=(2, 2), name='max1')(inner)

    inner = Conv2D(128, (3, 3), padding='same', name='conv2')(inner)
    inner = Activation('relu')(inner)
    inner = BatchNormalization()(inner)
    inner = MaxPooling2D(pool_size=(2, 2), name='max2')(inner)

    inner = Conv2D(256, (3, 3), padding='same', name='conv3')(inner)
    inner = Activation('relu')(inner)
    inner = BatchNormalization()(inner)
    inner = Conv2D(256, (3, 3),
                   padding='same',
                   name='conv4',
                   kernel_initializer='he_normal')(inner)
    inner = Activation('relu')(inner)
    inner = BatchNormalization()(inner)
    inner = MaxPooling2D(pool_size=(2, 2),
                         name='max3')(inner)  # (None, 32, 8, 256)

    inner = Conv2D(512, (3, 3),
                   padding='same',
                   name='conv5',
                   kernel_initializer='he_normal')(inner)
    inner = Activation('relu')(inner)
    inner = BatchNormalization()(inner)
    inner = Conv2D(512, (3, 3), padding='same', name='conv6')(inner)
    inner = Activation('relu')(inner)
    inner = BatchNormalization()(inner)
    inner = MaxPooling2D(pool_size=(1, 2), name='max4')(inner)

    inner = Conv2D(512, (2, 2),
                   padding='same',
                   kernel_initializer='he_normal',
                   name='con7')(inner)
    inner = Activation('relu')(inner)
    inner = BatchNormalization()(inner)

    # CNN to RNN
    conv_shape = inner.get_shape()
    inner = Reshape(target_shape=(int(conv_shape[1]),
                                  int(conv_shape[2] * conv_shape[3])))(inner)
    inner = Dense(64,
                  activation='relu',
                  kernel_initializer='he_normal',
                  name='dense1')(inner)
    inner = Dropout(keeppro)(inner)
    # RNN layer
    gru_1 = GRU(256,
                return_sequences=True,
                kernel_initializer='he_normal',
                name='gru1',
                dropout=keeppro)(inner)
    gru_1b = GRU(256,
                 return_sequences=True,
                 go_backwards=True,
                 kernel_initializer='he_normal',
                 name='gru1_b',
                 dropout=keeppro)(inner)
    gru1_merged = add([gru_1, gru_1b])
    gru1_merged = BatchNormalization()(gru1_merged)

    gru_2 = GRU(256,
                return_sequences=True,
                kernel_initializer='he_normal',
                name='gru2',
                dropout=keeppro)(gru1_merged)
    gru_2b = GRU(256,
                 return_sequences=True,
                 go_backwards=True,
                 kernel_initializer='he_normal',
                 name='gru2_b',
                 dropout=keeppro)(gru1_merged)
    gru2_merged = concatenate([gru_2, gru_2b])
    gru2_merged = BatchNormalization()(gru2_merged)

    # transforms RNN output to character activations:
    inner = Dense(len(Global.CHAR_SET) + 1,
                  kernel_initializer='he_normal',
                  name='dense2')(gru2_merged)
    # 防止过拟合
    #inner = Dropout(keeppro)(inner)
    y_pred = Activation('softmax', name='softmax')(inner)

    labels = Input(name='the_labels', shape=[Global.WORD_LEN], dtype='float32')
    input_length = Input(name='input_length', shape=[1], dtype='int64')
    label_length = Input(name='label_length', shape=[1], dtype='int64')

    loss_out = Lambda(ctc_lambda_func, output_shape=(1, ),
                      name='ctc')([y_pred, labels, input_length,
                                   label_length])  # (None, 1)

    if training:
        return Model(inputs=[inputs, labels, input_length, label_length],
                     outputs=[loss_out])
    else:
        return Model(inputs=[inputs], outputs=y_pred)
コード例 #12
0
ファイル: CRNN_Model.py プロジェクト: windkiss5/LicenseRec
def get_Model_2(training):
    if training:
        keeppro = 0.2
    else:
        keeppro = 0.
    input_shape = (Global.IMG_WIDTH, Global.IMG_HEIGHT, Global.IMG_CHANNELS)

    inputs = Input(name='the_input', shape=input_shape, dtype='float32')

    inner = Conv2D(32, (3, 3),
                   padding='same',
                   name='conv1',
                   kernel_initializer='he_normal',
                   strides=1)(inputs)
    inner = Activation('relu')(inner)
    inner = BatchNormalization()(inner)
    inner = MaxPooling2D(pool_size=(2, 2),
                         name='max1',
                         padding='same',
                         strides=2)(inner)

    inner = Conv2D(64, (3, 3),
                   padding='same',
                   name='conv3',
                   kernel_initializer='he_normal',
                   strides=1)(inner)
    inner = Activation('relu')(inner)
    inner = BatchNormalization()(inner)
    inner = MaxPooling2D(pool_size=(2, 1),
                         name='max2',
                         padding='same',
                         strides=2)(inner)

    inner = Conv2D(128, (3, 3),
                   padding='same',
                   name='conv5',
                   kernel_initializer='he_normal',
                   strides=1)(inner)
    inner = Activation('relu')(inner)
    inner = BatchNormalization()(inner)
    inner = MaxPooling2D(pool_size=(2, 1),
                         name='max3',
                         padding='same',
                         strides=2)(inner)

    inner = Conv2D(256, (3, 3),
                   padding='same',
                   name='conv6',
                   kernel_initializer='he_normal',
                   strides=1)(inner)
    inner = Activation('relu')(inner)
    inner = BatchNormalization()(inner)
    inner = MaxPooling2D(pool_size=(2, 1),
                         name='max4',
                         padding='same',
                         strides=2)(inner)

    inner = Conv2D(256, (3, 3),
                   padding='same',
                   name='conv7',
                   kernel_initializer='he_normal',
                   strides=1)(inner)
    inner = Activation('relu')(inner)
    inner = BatchNormalization()(inner)

    conv_shape = inner.get_shape()
    inner = TimeDistributed(Flatten(), name='timedistrib')(inner)
    inner = Dropout(keeppro)(inner)

    gru_1 = GRU(128,
                return_sequences=True,
                kernel_initializer='he_normal',
                name='gru1',
                dropout=keeppro)(inner)
    gru_1b = GRU(128,
                 return_sequences=True,
                 go_backwards=True,
                 kernel_initializer='he_normal',
                 name='gru1_b',
                 dropout=keeppro)(inner)
    gru1_merged = add([gru_1, gru_1b])
    gru1_merged = BatchNormalization()(gru1_merged)

    gru_2 = GRU(128,
                return_sequences=True,
                kernel_initializer='he_normal',
                name='gru2',
                dropout=keeppro)(gru1_merged)
    gru_2b = GRU(128,
                 return_sequences=True,
                 go_backwards=True,
                 kernel_initializer='he_normal',
                 name='gru2_b',
                 dropout=keeppro)(gru1_merged)
    gru2_merged = concatenate([gru_2, gru_2b])
    gru2_merged = BatchNormalization()(gru2_merged)

    inner = Dense(len(Global.CHAR_SET) + 1,
                  kernel_initializer='he_normal',
                  name='dense2')(gru2_merged)
    # 防止过拟合

    y_pred = Activation('softmax', name='softmax')(inner)

    labels = Input(name='the_labels', shape=[Global.WORD_LEN], dtype='float32')
    input_length = Input(name='input_length', shape=[1], dtype='int64')
    label_length = Input(name='label_length', shape=[1], dtype='int64')

    loss_out = Lambda(ctc_lambda_func, output_shape=(1, ),
                      name='ctc')([y_pred, labels, input_length, label_length])

    if training:
        return Model(inputs=[inputs, labels, input_length, label_length],
                     outputs=[loss_out])
    else:
        return Model(inputs=[inputs], outputs=y_pred)