本文整理汇总了Python中keras.layers.Flatten方法的典型用法代码示例。如果您正苦于以下问题:Python layers.Flatten方法的具体用法?Python layers.Flatten怎么用?Python layers.Flatten使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.layers
的用法示例。
在下文中一共展示了layers.Flatten方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _save
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def _save(model, base_model, layers, labels, random_seed, checkpoints_dir):
from keras.layers import Flatten, Dense
from keras import Model
nclasses = len(labels)
x = Flatten()(base_model.output)
x = _makenet(x, layers, dropout=None, random_seed=random_seed)
predictions = Dense(nclasses, activation="softmax", name="predictions")(x)
model_final = Model(inputs=base_model.input, outputs=predictions)
for i in range(layers - 1):
weights = model.get_layer(name='dense_layer_{}'.format(i)).get_weights()
model_final.get_layer(name='dense_layer_{}'.format(i)).set_weights(weights)
weights = model.get_layer(name='predictions').get_weights()
model_final.get_layer(name='predictions').set_weights(weights)
model_final.save(os.path.join(checkpoints_dir, "model.h5"))
with open(os.path.join(checkpoints_dir, "labels.txt"), "w") as f:
f.write("\n".join(labels))
return model_final
示例2: __init__
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def __init__(self, model_path=None):
if model_path is not None:
self.model = self.load_model(model_path)
else:
# VGG16 last conv features
inputs = Input(shape=(7, 7, 512))
x = Convolution2D(128, 1, 1)(inputs)
x = Flatten()(x)
# Cls head
h_cls = Dense(256, activation='relu', W_regularizer=l2(l=0.01))(x)
h_cls = Dropout(p=0.5)(h_cls)
cls_head = Dense(20, activation='softmax', name='cls')(h_cls)
# Reg head
h_reg = Dense(256, activation='relu', W_regularizer=l2(l=0.01))(x)
h_reg = Dropout(p=0.5)(h_reg)
reg_head = Dense(4, activation='linear', name='reg')(h_reg)
# Joint model
self.model = Model(input=inputs, output=[cls_head, reg_head])
示例3: build_discriminator
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def build_discriminator(self):
model = Sequential()
model.add(Conv2D(64, kernel_size=3, strides=2, input_shape=self.missing_shape, padding="same"))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(128, kernel_size=3, strides=2, padding="same"))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Conv2D(256, kernel_size=3, padding="same"))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))
model.summary()
img = Input(shape=self.missing_shape)
validity = model(img)
return Model(img, validity)
示例4: build_discriminator
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def build_discriminator(self):
img = Input(shape=self.img_shape)
model = Sequential()
model.add(Conv2D(64, kernel_size=4, strides=2, padding='same', input_shape=self.img_shape))
model.add(LeakyReLU(alpha=0.8))
model.add(Conv2D(128, kernel_size=4, strides=2, padding='same'))
model.add(LeakyReLU(alpha=0.2))
model.add(InstanceNormalization())
model.add(Conv2D(256, kernel_size=4, strides=2, padding='same'))
model.add(LeakyReLU(alpha=0.2))
model.add(InstanceNormalization())
model.summary()
img = Input(shape=self.img_shape)
features = model(img)
validity = Conv2D(1, kernel_size=4, strides=1, padding='same')(features)
label = Flatten()(features)
label = Dense(self.num_classes+1, activation="softmax")(label)
return Model(img, [validity, label])
示例5: build_encoder
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def build_encoder(self):
model = Sequential()
model.add(Flatten(input_shape=self.img_shape))
model.add(Dense(512))
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(self.latent_dim))
model.summary()
img = Input(shape=self.img_shape)
z = model(img)
return Model(img, z)
示例6: build_discriminator
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def build_discriminator(self):
z = Input(shape=(self.latent_dim, ))
img = Input(shape=self.img_shape)
d_in = concatenate([z, Flatten()(img)])
model = Dense(1024)(d_in)
model = LeakyReLU(alpha=0.2)(model)
model = Dropout(0.5)(model)
model = Dense(1024)(model)
model = LeakyReLU(alpha=0.2)(model)
model = Dropout(0.5)(model)
model = Dense(1024)(model)
model = LeakyReLU(alpha=0.2)(model)
model = Dropout(0.5)(model)
validity = Dense(1, activation="sigmoid")(model)
return Model([z, img], validity)
示例7: build_classifier
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def build_classifier(self):
def clf_layer(layer_input, filters, f_size=4, normalization=True):
"""Classifier layer"""
d = Conv2D(filters, kernel_size=f_size, strides=2, padding='same')(layer_input)
d = LeakyReLU(alpha=0.2)(d)
if normalization:
d = InstanceNormalization()(d)
return d
img = Input(shape=self.img_shape)
c1 = clf_layer(img, self.cf, normalization=False)
c2 = clf_layer(c1, self.cf*2)
c3 = clf_layer(c2, self.cf*4)
c4 = clf_layer(c3, self.cf*8)
c5 = clf_layer(c4, self.cf*8)
class_pred = Dense(self.num_classes, activation='softmax')(Flatten()(c5))
return Model(img, class_pred)
示例8: build_discriminators
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def build_discriminators(self):
img1 = Input(shape=self.img_shape)
img2 = Input(shape=self.img_shape)
# Shared discriminator layers
model = Sequential()
model.add(Flatten(input_shape=self.img_shape))
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(Dense(256))
model.add(LeakyReLU(alpha=0.2))
img1_embedding = model(img1)
img2_embedding = model(img2)
# Discriminator 1
validity1 = Dense(1, activation='sigmoid')(img1_embedding)
# Discriminator 2
validity2 = Dense(1, activation='sigmoid')(img2_embedding)
return Model(img1, validity1), Model(img2, validity2)
示例9: build_discriminator
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def build_discriminator(self):
model = Sequential()
model.add(Flatten(input_shape=self.img_shape))
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(Dense(256))
model.add(LeakyReLU(alpha=0.2))
model.add(Dense(1, activation='sigmoid'))
model.summary()
img = Input(shape=self.img_shape)
validity = model(img)
return Model(img, validity)
示例10: encoder
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def encoder(self):
if self.E:
return self.E
inp = Input(shape = [im_size, im_size, 3])
x = d_block(inp, 1 * cha) #64
x = d_block(x, 2 * cha) #32
x = d_block(x, 3 * cha) #16
x = d_block(x, 4 * cha) #8
x = d_block(x, 8 * cha) #4
x = d_block(x, 16 * cha, p = False) #4
x = Flatten()(x)
x = Dense(16 * cha, kernel_initializer = 'he_normal')(x)
x = LeakyReLU(0.2)(x)
x = Dense(latent_size, kernel_initializer = 'he_normal', bias_initializer = 'zeros')(x)
self.E = Model(inputs = inp, outputs = x)
return self.E
示例11: build_model
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def build_model(x_train, num_classes):
# Reset default graph. Keras leaves old ops in the graph,
# which are ignored for execution but clutter graph
# visualization in TensorBoard.
tf.reset_default_graph()
inputs = KL.Input(shape=x_train.shape[1:], name="input_image")
x = KL.Conv2D(32, (3, 3), activation='relu', padding="same",
name="conv1")(inputs)
x = KL.Conv2D(64, (3, 3), activation='relu', padding="same",
name="conv2")(x)
x = KL.MaxPooling2D(pool_size=(2, 2), name="pool1")(x)
x = KL.Flatten(name="flat1")(x)
x = KL.Dense(128, activation='relu', name="dense1")(x)
x = KL.Dense(num_classes, activation='softmax', name="dense2")(x)
return KM.Model(inputs, x, "digit_classifier_model")
# Load MNIST Data
示例12: modelA
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def modelA():
model = Sequential()
model.add(Conv2D(64, (5, 5),
padding='valid'))
model.add(Activation('relu'))
model.add(Conv2D(64, (5, 5)))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(FLAGS.NUM_CLASSES))
return model
示例13: modelB
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def modelB():
model = Sequential()
model.add(Dropout(0.2, input_shape=(FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS)))
model.add(Convolution2D(64, 8, 8,
subsample=(2, 2),
border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(128, 6, 6,
subsample=(2, 2),
border_mode='valid'))
model.add(Activation('relu'))
model.add(Convolution2D(128, 5, 5,
subsample=(1, 1)))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(FLAGS.NUM_CLASSES))
return model
示例14: modelC
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def modelC():
model = Sequential()
model.add(Convolution2D(128, 3, 3,
border_mode='valid',
input_shape=(FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS)))
model.add(Activation('relu'))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(FLAGS.NUM_CLASSES))
return model
示例15: modelD
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import Flatten [as 别名]
def modelD():
model = Sequential()
model.add(Flatten(input_shape=(FLAGS.IMAGE_ROWS,
FLAGS.IMAGE_COLS,
FLAGS.NUM_CHANNELS)))
model.add(Dense(300, init='he_normal', activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(300, init='he_normal', activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(300, init='he_normal', activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(300, init='he_normal', activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(FLAGS.NUM_CLASSES))
return model