def tsinalis(input_shape, n_classes):
"""
Input size should be [batch, 1d, 2d, ch] = (None, 1, 15000, 1)
"""
model = Sequential(name='Tsinalis')
model.add(Conv1D (kernel_size = (200), filters = 20, input_shape=input_shape, activation='relu'))
print(model.input_shape)
print(model.output_shape)
model.add(MaxPooling1D(pool_size = (20), strides=(10)))
print(model.output_shape)
model.add(keras.layers.core.Reshape([20,-1,1]))
print(model.output_shape)
model.add(Conv2D (kernel_size = (20,30), filters = 400, activation='relu'))
print(model.output_shape)
model.add(MaxPooling2D(pool_size = (1,10), strides=(1,2)))
print(model.output_shape)
model.add(Flatten())
print(model.output_shape)
model.add(Dense (500, activation='relu'))
model.add(Dense (500, activation='relu'))
model.add(Dense(n_classes, activation = 'softmax',activity_regularizer=keras.regularizers.l2() ))
model.compile( loss='categorical_crossentropy', optimizer=keras.optimizers.SGD(), metrics=[keras.metrics.categorical_accuracy])
return model
python类Reshape()的实例源码
def make_generator():
"""Creates a generator model that takes a 100-dimensional noise vector as a "seed", and outputs images
of size 28x28x1."""
model = Sequential()
model.add(Dense(1024, input_dim=100))
model.add(LeakyReLU())
model.add(Dense(128 * 7 * 7))
model.add(BatchNormalization())
model.add(LeakyReLU())
if K.image_data_format() == 'channels_first':
model.add(Reshape((128, 7, 7), input_shape=(128 * 7 * 7,)))
bn_axis = 1
else:
model.add(Reshape((7, 7, 128), input_shape=(128 * 7 * 7,)))
bn_axis = -1
model.add(Conv2DTranspose(128, (5, 5), strides=2, padding='same'))
model.add(BatchNormalization(axis=bn_axis))
model.add(LeakyReLU())
model.add(Convolution2D(64, (5, 5), padding='same'))
model.add(BatchNormalization(axis=bn_axis))
model.add(LeakyReLU())
model.add(Conv2DTranspose(64, (5, 5), strides=2, padding='same'))
model.add(BatchNormalization(axis=bn_axis))
model.add(LeakyReLU())
# Because we normalized training inputs to lie in the range [-1, 1],
# the tanh function should be used for the output of the generator to ensure its output
# also lies in this range.
model.add(Convolution2D(1, (5, 5), padding='same', activation='tanh'))
return model
def build_encoder(self,input_shape):
return [Reshape((*input_shape,1)),
GaussianNoise(0.1),
BN(),
Convolution2D(self.parameters['clayer'],(3,3),
activation=self.parameters['activation'],padding='same', use_bias=False),
Dropout(self.parameters['dropout']),
BN(),
MaxPooling2D((2,2)),
Convolution2D(self.parameters['clayer'],(3,3),
activation=self.parameters['activation'],padding='same', use_bias=False),
Dropout(self.parameters['dropout']),
BN(),
MaxPooling2D((2,2)),
Convolution2D(self.parameters['clayer'],(3,3),
activation=self.parameters['activation'],padding='same', use_bias=False),
Dropout(self.parameters['dropout']),
BN(),
MaxPooling2D((2,2)),
flatten,]
def generator_model(noise_dim=100, aux_dim=47, model_name="generator"):
# Merge noise and auxilary inputs
gen_input = Input(shape=(noise_dim,), name="noise_input")
aux_input = Input(shape=(aux_dim,), name="auxilary_input")
x = concatenate([gen_input, aux_input], axis=-1)
# Dense Layer 1
x = Dense(10 * 100)(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x) # output shape is 10*100
# Reshape the tensors to support CNNs
x = Reshape((100, 10))(x) # shape is 100 x 10
# Conv Layer 1
x = Conv1D(filters=250, kernel_size=13, padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x) # output shape is 100 x 250
x = UpSampling1D(size=2)(x) # output shape is 200 x 250
# Conv Layer 2
x = Conv1D(filters=100, kernel_size=13, padding='same')(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.2)(x) # output shape is 200 x 100
x = UpSampling1D(size=2)(x) # output shape is 400 x 100
# Conv Layer 3
x = Conv1D(filters=1, kernel_size=13, padding='same')(x)
x = BatchNormalization()(x)
x = Activation('tanh')(x) # final output shape is 400 x 1
generator_model = Model(
outputs=[x], inputs=[gen_input, aux_input], name=model_name)
return generator_model
example_gan_cifar10.py 文件源码
项目:Deep-Learning-with-Keras
作者: PacktPublishing
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def model_generator():
model = Sequential()
nch = 256
reg = lambda: l1l2(l1=1e-7, l2=1e-7)
h = 5
model.add(Dense(nch * 4 * 4, input_dim=100, W_regularizer=reg()))
model.add(BatchNormalization(mode=0))
model.add(Reshape(dim_ordering_shape((nch, 4, 4))))
model.add(Convolution2D(nch / 2, h, h, border_mode='same', W_regularizer=reg()))
model.add(BatchNormalization(mode=0, axis=1))
model.add(LeakyReLU(0.2))
model.add(UpSampling2D(size=(2, 2)))
model.add(Convolution2D(nch / 2, h, h, border_mode='same', W_regularizer=reg()))
model.add(BatchNormalization(mode=0, axis=1))
model.add(LeakyReLU(0.2))
model.add(UpSampling2D(size=(2, 2)))
model.add(Convolution2D(nch / 4, h, h, border_mode='same', W_regularizer=reg()))
model.add(BatchNormalization(mode=0, axis=1))
model.add(LeakyReLU(0.2))
model.add(UpSampling2D(size=(2, 2)))
model.add(Convolution2D(3, h, h, border_mode='same', W_regularizer=reg()))
model.add(Activation('sigmoid'))
return model
def rcnn(input_shape, n_classes):
"""
Input size should be [batch, 1d, ch] = (XXX, 3000, 1)
"""
model = Sequential(name='RCNN test')
model.add(Conv1D (kernel_size = (200), filters = 20, batch_input_shape=input_shape, activation='elu'))
model.add(MaxPooling1D(pool_size = (20), strides=(10)))
model.add(Conv1D (kernel_size = (20), filters = 200, activation='elu'))
model.add(MaxPooling1D(pool_size = (10), strides=(3)))
model.add(Conv1D (kernel_size = (20), filters = 200, activation='elu'))
model.add(MaxPooling1D(pool_size = (10), strides=(3)))
model.add(Dense (512, activation='elu'))
model.add(Dense (512, activation='elu'))
model.add(Reshape((1,model.output_shape[1])))
model.add(LSTM(256, stateful=True, return_sequences=False))
model.add(Dropout(0.3))
model.add(Dense(n_classes, activation = 'sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer=Adadelta())
return model
def build_encoder(self,input_shape):
last_convolution = np.array(input_shape) // 8
self.parameters['clayer'] = 8
self.parameters['N'] = int(np.prod(last_convolution)*self.parameters['clayer'] // self.parameters['M'])
return [Reshape((*input_shape,1)),
GaussianNoise(0.1),
BN(),
Convolution2D(16,(3,3),
activation=self.parameters['activation'],padding='same', use_bias=False),
Dropout(self.parameters['dropout']),
BN(),
MaxPooling2D((2,2)),
Convolution2D(64,(3,3),
activation=self.parameters['activation'],padding='same', use_bias=False),
SpatialDropout2D(self.parameters['dropout']),
BN(),
MaxPooling2D((2,2)),
Convolution2D(64,(3,3),
activation=self.parameters['activation'],padding='same', use_bias=False),
SpatialDropout2D(self.parameters['dropout']),
BN(),
MaxPooling2D((2,2)),
Convolution2D(64,(1,1),
activation=self.parameters['activation'],padding='same', use_bias=False),
SpatialDropout2D(self.parameters['dropout']),
BN(),
Convolution2D(self.parameters['clayer'],(1,1),
padding='same'),
flatten,
]
# mixin classes ###############################################################
# Now effectively 3 subclasses; GumbelSoftmax in the output, Convolution, Gaussian.
# there are 4 more results of mixins:
def __call__(self, model):
if self.crop_right:
model = Lambda(lambda x: x[:, :, :K.int_shape(x)[2]-1, :])(model)
if self.v is not None:
model = Merge(mode='sum')([model, self.v])
if self.h is not None:
hV = Dense(output_dim=2*self.filters)(self.h)
hV = Reshape((1, 1, 2*self.filters))(hV)
model = Lambda(lambda x: x[0]+x[1])([model,hV])
model_f = Lambda(lambda x: x[:,:,:,:self.filters])(model)
model_g = Lambda(lambda x: x[:,:,:,self.filters:])(model)
model_f = Lambda(lambda x: K.tanh(x))(model_f)
model_g = Lambda(lambda x: K.sigmoid(x))(model_g)
res = Merge(mode='mul')([model_f, model_g])
return res
def create_model(numNodes, factors):
left_input = Input(shape=(1,))
right_input = Input(shape=(1,))
left_model = Sequential()
left_model.add(Embedding(input_dim=numNodes + 1, output_dim=factors, input_length=1, mask_zero=False))
left_model.add(Reshape((factors,)))
right_model = Sequential()
right_model.add(Embedding(input_dim=numNodes + 1, output_dim=factors, input_length=1, mask_zero=False))
right_model.add(Reshape((factors,)))
left_embed = left_model(left_input)
right_embed = left_model(right_input)
left_right_dot = merge([left_embed, right_embed], mode="dot", dot_axes=1, name="left_right_dot")
model = Model(input=[left_input, right_input], output=[left_right_dot])
embed_generator = Model(input=[left_input, right_input], output=[left_embed, right_embed])
return model, embed_generator
def build_generator(self):
model = Sequential()
model.add(Dense(1024, activation='relu', input_dim=self.latent_dim))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(128 * 7 * 7, activation="relu"))
model.add(BatchNormalization(momentum=0.8))
model.add(Reshape((7, 7, 128)))
model.add(UpSampling2D())
model.add(Conv2D(64, kernel_size=4, padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(momentum=0.8))
model.add(UpSampling2D())
model.add(Conv2D(self.channels, kernel_size=4, padding='same'))
model.add(Activation("tanh"))
model.summary()
gen_input = Input(shape=(self.latent_dim,))
img = model(gen_input)
return Model(gen_input, img)
def build_generator(self):
model = Sequential()
model.add(Dense(128 * 7 * 7, activation="relu", input_dim=100))
model.add(Reshape((7, 7, 128)))
model.add(BatchNormalization(momentum=0.8))
model.add(UpSampling2D())
model.add(Conv2D(128, kernel_size=3, padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(momentum=0.8))
model.add(UpSampling2D())
model.add(Conv2D(64, kernel_size=3, padding="same"))
model.add(Activation("relu"))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(1, kernel_size=3, padding="same"))
model.add(Activation("tanh"))
model.summary()
noise = Input(shape=(100,))
img = model(noise)
return Model(noise, img)
def build_generator(self):
noise_shape = (100,)
model = Sequential()
model.add(Dense(256, input_shape=noise_shape))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(1024))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(np.prod(self.img_shape), activation='tanh'))
model.add(Reshape(self.img_shape))
model.summary()
noise = Input(shape=noise_shape)
img = model(noise)
return Model(noise, img)
def build_generator(self):
model = Sequential()
model.add(Dense(512, input_dim=self.latent_dim))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(np.prod(self.img_shape), activation='tanh'))
model.add(Reshape(self.img_shape))
model.summary()
z = Input(shape=(self.latent_dim,))
gen_img = model(z)
return Model(z, gen_img)
def deflating_convolution(inputs, n_deflation_layers, n_filters_init=32, noise=None, name_prefix=None):
def add_linear_noise(x, eps, ind):
flattened_deflated = Reshape((-1,), name=name_prefix + '_conv_flatten_{}'.format(ind))(x)
deflated_shape = ker.int_shape(x)
deflated_size = deflated_shape[1] * deflated_shape[2] * deflated_shape[3]
noise_transformed = Dense(deflated_size, activation=None,
name=name_prefix + '_conv_noise_dense_{}'.format(ind))(eps)
added_noise = Add(name=name_prefix + '_conv_add_noise_{}'.format(ind))([noise_transformed, flattened_deflated])
x = Reshape((deflated_shape[1], deflated_shape[2], deflated_shape[3]),
name=name_prefix + '_conv_backreshape_{}'.format(ind))(added_noise)
return x
deflated = Conv2D(filters=n_filters_init, kernel_size=(5, 5), strides=(2, 2),
padding='same', activation='relu', name=name_prefix + '_conv_0')(inputs)
if noise is not None:
deflated = add_linear_noise(deflated, noise, 0)
for i in range(1, n_deflation_layers):
deflated = Conv2D(filters=n_filters_init * (2**i), kernel_size=(5, 5), strides=(2, 2),
padding='same', activation='relu', name=name_prefix + '_conv_{}'.format(i))(deflated)
# if noise is not None:
# deflated = add_linear_noise(deflated, noise, i)
return deflated
def test_tiny_mcrnn_music_tagger(self):
x_in = Input(shape=(4,6,1))
x = ZeroPadding2D(padding=(0, 1))(x_in)
x = BatchNormalization(axis=2, name='bn_0_freq')(x)
# Conv block 1
x = Conv2D(2, (3, 3), padding='same', name='conv1')(x)
x = BatchNormalization(axis=3, name='bn1')(x)
x = Activation('elu')(x)
x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool1')(x)
# Conv block 2
x = Conv2D(4, (3, 3), padding='same', name='conv2')(x)
x = BatchNormalization(axis=3, name='bn2')(x)
x = Activation('elu')(x)
x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool2')(x)
# Should get you (1,1,2,4)
x = Reshape((2, 4))(x)
x = GRU(32, return_sequences=True, name='gru1')(x)
x = GRU(32, return_sequences=False, name='gru2')(x)
# Create model.
model = Model(x_in, x)
model.set_weights([np.random.rand(*w.shape) for w in model.get_weights()])
self._test_keras_model(model, mode='random_zero_mean', delta=1e-2)
def test_tiny_image_captioning(self):
# use a conv layer as a image feature branch
img_input_1 = Input(shape=(16,16,3))
x = Conv2D(2,(3,3))(img_input_1)
x = Flatten()(x)
img_model = Model(inputs=[img_input_1], outputs=[x])
img_input = Input(shape=(16,16,3))
x = img_model(img_input)
x = Dense(8, name = 'cap_dense')(x)
x = Reshape((1,8), name = 'cap_reshape')(x)
sentence_input = Input(shape=(5,)) # max_length = 5
y = Embedding(8, 8, name = 'cap_embedding')(sentence_input)
z = concatenate([x,y], axis = 1, name = 'cap_merge')
z = LSTM(4, return_sequences = True, name = 'cap_lstm')(z)
z = TimeDistributed(Dense(8), name = 'cap_timedistributed')(z)
combined_model = Model(inputs=[img_input, sentence_input], outputs=[z])
self._test_keras_model(combined_model, one_dim_seq_flags=[False, True])
categorical_crossentropy_example.py 文件源码
项目:keras-semantic-segmentation-example
作者: mrgloom
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def get_model():
inputs = Input((IMAGE_H, IMAGE_W, INPUT_CHANNELS))
base = models.get_fcn_vgg16_32s(inputs, NUMBER_OF_CLASSES)
#base = models.get_fcn_vgg16_16s(inputs, NUMBER_OF_CLASSES)
#base = models.get_fcn_vgg16_8s(inputs, NUMBER_OF_CLASSES)
#base = models.get_unet(inputs, NUMBER_OF_CLASSES)
#base = models.get_segnet_vgg16(inputs, NUMBER_OF_CLASSES)
# softmax
reshape= Reshape((-1,NUMBER_OF_CLASSES))(base)
act = Activation('softmax')(reshape)
model = Model(inputs=inputs, outputs=act)
model.compile(optimizer=Adadelta(), loss='categorical_crossentropy')
#print(model.summary())
#sys.exit()
return model
binary_crossentropy_example.py 文件源码
项目:keras-semantic-segmentation-example
作者: mrgloom
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def get_model():
inputs = Input((IMAGE_H, IMAGE_W, INPUT_CHANNELS))
base = models.get_fcn_vgg16_32s(inputs, NUMBER_OF_CLASSES)
#base = models.get_fcn_vgg16_16s(inputs, NUMBER_OF_CLASSES)
#base = models.get_fcn_vgg16_8s(inputs, NUMBER_OF_CLASSES)
#base = models.get_unet(inputs, NUMBER_OF_CLASSES)
#base = models.get_segnet_vgg16(inputs, NUMBER_OF_CLASSES)
# sigmoid
reshape= Reshape((-1,NUMBER_OF_CLASSES))(base)
act = Activation('sigmoid')(reshape)
model = Model(inputs=inputs, outputs=act)
model.compile(optimizer=Adadelta(), loss='binary_crossentropy')
#print(model.summary())
#sys.exit()
return model
categorical_crossentropy_example.py 文件源码
项目:keras-semantic-segmentation-example
作者: mrgloom
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def get_model():
inputs = Input((IMAGE_H, IMAGE_W, INPUT_CHANNELS))
base = models.get_fcn_vgg16_32s(inputs, NUMBER_OF_CLASSES)
#base = models.get_fcn_vgg16_16s(inputs, NUMBER_OF_CLASSES)
#base = models.get_fcn_vgg16_8s(inputs, NUMBER_OF_CLASSES)
#base = models.get_unet(inputs, NUMBER_OF_CLASSES)
#base = models.get_segnet_vgg16(inputs, NUMBER_OF_CLASSES)
# softmax
reshape= Reshape((-1,NUMBER_OF_CLASSES))(base)
act = Activation('softmax')(reshape)
model = Model(inputs=inputs, outputs=act)
model.compile(optimizer=Adadelta(), loss='categorical_crossentropy')
#print(model.summary())
#sys.exit()
return model
binary_crossentropy_example.py 文件源码
项目:keras-semantic-segmentation-example
作者: mrgloom
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def get_model():
inputs = Input((IMAGE_H, IMAGE_W, INPUT_CHANNELS))
base = models.get_fcn_vgg16_32s(inputs, NUMBER_OF_CLASSES)
#base = models.get_fcn_vgg16_16s(inputs, NUMBER_OF_CLASSES)
#base = models.get_fcn_vgg16_8s(inputs, NUMBER_OF_CLASSES)
#base = models.get_unet(inputs, NUMBER_OF_CLASSES)
#base = models.get_segnet_vgg16(inputs, NUMBER_OF_CLASSES)
# sigmoid
reshape= Reshape((-1,NUMBER_OF_CLASSES))(base)
act = Activation('sigmoid')(reshape)
model = Model(inputs=inputs, outputs=act)
model.compile(optimizer=Adadelta(), loss='binary_crossentropy')
#print(model.summary())
#sys.exit()
return model
def global_handle(self, emb_layer, flag):
fw_lstm_out = self.forward_lstm(emb_layer)
bw_lstm_out = self.backward_lstm(emb_layer)
conv_out = self.conv_dropout(self.conv(emb_layer))
fw_lstm_out = TimeDistributed(Dense(self.params['attention_dim']), name='fw_tb_'+flag)(fw_lstm_out)
fw_lstm_att = Attention()(fw_lstm_out)
# fw_lstm_att = Reshape((self.params['lstm_output_dim'], 1))(fw_lstm_att)
conv_out = TimeDistributed(Dense(self.params['attention_dim']), name='conv_tb_'+flag)(conv_out)
conv_att = Attention()(conv_out)
# conv_att = Reshape((self.params['filters'], 1))(conv_att)
bw_lstm_out = TimeDistributed(Dense(self.params['attention_dim']), name='bw_tb_'+flag)(bw_lstm_out)
bw_lstm_att = Attention()(bw_lstm_out)
# bw_lstm_att = Reshape((self.params['lstm_output_dim'], 1))(bw_lstm_att)
return concatenate([fw_lstm_att, conv_att, bw_lstm_att], axis=2)
def __init__(self,
g_size=(3, 128, 64),
g_nb_filters=128,
g_nb_coding=200,
g_init=None,
**kwargs):
super(MLP, self).__init__(**kwargs)
self.g_size = g_size
self.g_nb_filters = g_nb_filters
self.g_nb_coding = g_nb_coding
self.g_init = g_init if g_init is not None else InitNormal()
c, h, w = g_size # h and w should be multiply of 16
nf = g_nb_filters
self.add(Dense(g_nb_filters, input_shape=(g_nb_coding,)) )
self.add(Activation('relu'))
self.add(Dense(g_nb_filters))
self.add(Activation('relu'))
self.add(Dense(g_nb_filters, ))
self.add(Activation('relu'))
self.add(Dense(np.prod(g_size), ))
self.add(Reshape(g_size))
g_init(self)
def model_generator():
model = Sequential()
nch = 256
reg = lambda: l1l2(l1=1e-7, l2=1e-7)
h = 5
model.add(Dense(nch * 4 * 4, input_dim=100, W_regularizer=reg()))
model.add(BatchNormalization(mode=0))
model.add(Reshape(dim_ordering_shape((nch, 4, 4))))
model.add(Convolution2D(nch / 2, h, h, border_mode='same', W_regularizer=reg()))
model.add(BatchNormalization(mode=0, axis=1))
model.add(LeakyReLU(0.2))
model.add(UpSampling2D(size=(2, 2)))
model.add(Convolution2D(nch / 2, h, h, border_mode='same', W_regularizer=reg()))
model.add(BatchNormalization(mode=0, axis=1))
model.add(LeakyReLU(0.2))
model.add(UpSampling2D(size=(2, 2)))
model.add(Convolution2D(nch / 4, h, h, border_mode='same', W_regularizer=reg()))
model.add(BatchNormalization(mode=0, axis=1))
model.add(LeakyReLU(0.2))
model.add(UpSampling2D(size=(2, 2)))
model.add(Convolution2D(3, h, h, border_mode='same', W_regularizer=reg()))
model.add(Activation('sigmoid'))
return model
def fit_cnn1(self, X33_train, Y_train, X33_unif_train, Y_unif_train):
# Create temp cnn with input shape=(4,33,33,)
input33 = Input(shape=(4, 33, 33))
output_cnn = self.one_block_model(input33)
output_cnn = Reshape((5,))(output_cnn)
# Cnn compiling
temp_cnn = Model(inputs=input33, outputs=output_cnn)
sgd = SGD(lr=self.learning_rate, momentum=self.momentum_rate, decay=self.decay_rate, nesterov=False)
temp_cnn.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
# Stop the training if the monitor function doesn't change after patience epochs
earlystopping = EarlyStopping(monitor='val_loss', patience=2, verbose=1, mode='auto')
# Save model after each epoch to check/bm_epoch#-val_loss
checkpointer = ModelCheckpoint(filepath="/home/ixb3/Scrivania/check/bm_{epoch:02d}-{val_loss:.2f}.hdf5", verbose=1)
# First-phase training with uniformly distribuited training set
temp_cnn.fit(x=X33_train, y=Y_train, batch_size=self.batch_size, epochs=self.nb_epoch,
callbacks=[earlystopping, checkpointer], validation_split=0.3, verbose=1)
# fix all the layers of the temporary cnn except the output layer for the second-phase
temp_cnn = self.freeze_model(temp_cnn, freeze_output=False)
# Second-phase training of the output layer with training set with real distribution probabily
temp_cnn.fit(x=X33_unif_train, y=Y_unif_train, batch_size=self.batch_size, epochs=self.nb_epoch,
callbacks=[earlystopping, checkpointer], validation_split=0.3, verbose=1)
# set the weights of the first cnn to the trained weights of the temporary cnn
self.cnn1.set_weights(temp_cnn.get_weights())
def discriminator_model():
model = Sequential()
model.add(Convolution1D(
12, 5,
border_mode='same',
input_shape=(INPUT_LN, 1)))
model.add(Activation('relu'))
model.add(MaxPooling1D(pool_length=N_GEN_l[0]))
model.add(Convolution1D(12, 5, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling1D(pool_length=N_GEN_l[1]))
#model.add(Reshape((128*7,)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
return model
def image_caption_model(vocab_size=2500, embedding_matrix=None, lang_dim=100,
max_caplen=28, img_dim=2048, clipnorm=1):
print('generating vocab_history model v5')
# text: current word
lang_input = Input(shape=(1,))
img_input = Input(shape=(img_dim,))
seq_input = Input(shape=(max_caplen,))
vhist_input = Input(shape=(vocab_size,))
if embedding_matrix is not None:
x = Embedding(output_dim=lang_dim, input_dim=vocab_size, init='glorot_uniform', input_length=1, weights=[embedding_matrix])(lang_input)
else:
x = Embedding(output_dim=lang_dim, input_dim=vocab_size, init='glorot_uniform', input_length=1)(lang_input)
lang_embed = Reshape((lang_dim,))(x)
lang_embed = merge([lang_embed, seq_input], mode='concat', concat_axis=-1)
lang_embed = Dense(lang_dim)(lang_embed)
lang_embed = Dropout(0.25)(lang_embed)
merge_layer = merge([img_input, lang_embed, vhist_input], mode='concat', concat_axis=-1)
merge_layer = Reshape((1, lang_dim+img_dim+vocab_size))(merge_layer)
gru_1 = GRU(img_dim)(merge_layer)
gru_1 = Dropout(0.25)(gru_1)
gru_1 = Dense(img_dim)(gru_1)
gru_1 = BatchNormalization()(gru_1)
gru_1 = Activation('softmax')(gru_1)
attention_1 = merge([img_input, gru_1], mode='mul', concat_axis=-1)
attention_1 = merge([attention_1, lang_embed, vhist_input], mode='concat', concat_axis=-1)
attention_1 = Reshape((1, lang_dim + img_dim + vocab_size))(attention_1)
gru_2 = GRU(1024)(attention_1)
gru_2 = Dropout(0.25)(gru_2)
gru_2 = Dense(vocab_size)(gru_2)
gru_2 = BatchNormalization()(gru_2)
out = Activation('softmax')(gru_2)
model = Model(input=[img_input, lang_input, seq_input, vhist_input], output=out)
model.compile(loss='categorical_crossentropy', optimizer=RMSprop(lr=0.0001, clipnorm=1.))
return model
def generator_model():
model = Sequential()
model.add(Dense(input_dim=100, output_dim=1024))
model.add(Activation('tanh'))
model.add(Dense(128*7*7))
model.add(BatchNormalization())
model.add(Activation('tanh'))
model.add(Reshape((128,7,7), input_shape=(128*7*7,)))
model.add(UpSampling2D(size=(2,2), dim_ordering="th"))
model.add(Convolution2D(64,5,5, border_mode='same', dim_ordering="th"))
model.add(Activation('tanh'))
model.add(UpSampling2D(size=(2,2), dim_ordering="th"))
model.add(Convolution2D(1,5,5, border_mode='same', dim_ordering="th"))
model.add(Activation('tanh'))
return model
def generator_model():
model = Sequential()
model.add(Dense(input_dim=100, output_dim=1024))
model.add(Activation('tanh'))
model.add(Dense(128*7*7))
model.add(BatchNormalization())
model.add(Activation('tanh'))
model.add(Reshape((128, 7, 7), input_shape=(128*7*7,)))
model.add(UpSampling2D(size=(2, 2)))
model.add(Convolution2D(64, 5, 5, border_mode='same'))
model.add(Activation('tanh'))
model.add(UpSampling2D(size=(2, 2)))
model.add(Convolution2D(1, 5, 5, border_mode='same'))
model.add(Activation('tanh'))
return model
def build_generator(latent_size):
cnn = Sequential()
cnn.add(Dense(1024, input_dim=latent_size, activation='relu'))
cnn.add(Dense(128 * 7 * 7, activation='relu'))
cnn.add(Reshape((128, 7, 7)))
# upsample to (..., 14, 14)
cnn.add(UpSampling2D(size=(2, 2)))
cnn.add(Conv2D(256, 5, padding='same',
activation='relu', kernel_initializer='glorot_normal'))
# upsample to (..., 28, 28)
cnn.add(UpSampling2D(size=(2, 2)))
cnn.add(Conv2D(128, 5, padding='same',
activation='relu', kernel_initializer='glorot_normal'))
# take a channel axis reduction
cnn.add(Conv2D(1, 2, padding='same',
activation='tanh', kernel_initializer='glorot_normal'))
# this is the z space commonly refered to in GAN papers
latent = Input(shape=(latent_size,))
fake_image = cnn(latent)
return Model(inputs=latent, outputs=fake_image)
def build_generator(latent_size):
model = Sequential()
model.add(Dense(1024, input_dim=latent_size, activation='relu'))
model.add(Dense(28 * 28, activation='tanh'))
model.add(Reshape((1, 28, 28)))
return model