本文整理汇总了Python中keras.layers.GlobalAveragePooling2D方法的典型用法代码示例。如果您正苦于以下问题:Python layers.GlobalAveragePooling2D方法的具体用法?Python layers.GlobalAveragePooling2D怎么用?Python layers.GlobalAveragePooling2D使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.layers
的用法示例。
在下文中一共展示了layers.GlobalAveragePooling2D方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_model
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def get_model(session):
# create the base pre-trained model
base_model = Xception(weights=None, include_top=False, input_shape=(270, 480, 3))
# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
# add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# putput layer
predictions = Dense(session.training_dataset_info['number_of_labels'], activation='softmax')(x)
# model
model = Model(inputs=base_model.input, outputs=predictions)
learning_rate = 0.001
opt = keras.optimizers.adam(lr=learning_rate, decay=1e-5)
model.compile(loss='categorical_crossentropy',
optimizer=opt,
metrics=['accuracy'])
return model
示例2: inception_pseudo
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def inception_pseudo(self,dim=224,freeze_layers=30,full_freeze='N'):
model = InceptionV3(weights='imagenet',include_top=False)
x = model.output
x = GlobalAveragePooling2D()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
out = Dense(5,activation='softmax')(x)
model_final = Model(input = model.input,outputs=out)
if full_freeze != 'N':
for layer in model.layers[0:freeze_layers]:
layer.trainable = False
return model_final
# ResNet50 Model for transfer Learning
示例3: resnet_pseudo
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def resnet_pseudo(self,dim=224,freeze_layers=10,full_freeze='N'):
model = ResNet50(weights='imagenet',include_top=False)
x = model.output
x = GlobalAveragePooling2D()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
out = Dense(5,activation='softmax')(x)
model_final = Model(input = model.input,outputs=out)
if full_freeze != 'N':
for layer in model.layers[0:freeze_layers]:
layer.trainable = False
return model_final
# VGG16 Model for transfer Learning
示例4: inception_pseudo
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def inception_pseudo(self,dim=224,freeze_layers=30,full_freeze='N'):
model = InceptionV3(weights='imagenet',include_top=False)
x = model.output
x = GlobalAveragePooling2D()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
out = Dense(1)(x)
model_final = Model(input = model.input,outputs=out)
if full_freeze != 'N':
for layer in model.layers[0:freeze_layers]:
layer.trainable = False
return model_final
# ResNet50 Model for transfer Learning
示例5: resnet_pseudo
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def resnet_pseudo(self,dim=224,freeze_layers=10,full_freeze='N'):
model = ResNet50(weights='imagenet',include_top=False)
x = model.output
x = GlobalAveragePooling2D()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
out = Dense(1)(x)
model_final = Model(input = model.input,outputs=out)
if full_freeze != 'N':
for layer in model.layers[0:freeze_layers]:
layer.trainable = False
return model_final
# VGG16 Model for transfer Learning
示例6: inception_pseudo
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def inception_pseudo(self,dim=224,freeze_layers=10,full_freeze='N'):
model = InceptionV3(weights='imagenet',include_top=False)
x = model.output
x = GlobalAveragePooling2D()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
out = Dense(5,activation='softmax')(x)
model_final = Model(input = model.input,outputs=out)
if full_freeze != 'N':
for layer in model.layers[0:freeze_layers]:
layer.trainable = False
return model_final
# ResNet50 Model for transfer Learning
示例7: resnet_pseudo
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def resnet_pseudo(self,dim=224,freeze_layers=10,full_freeze='N'):
model = ResNet50(weights='imagenet',include_top=False)
x = model.output
x = GlobalAveragePooling2D()(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
x = Dense(512, activation='relu')(x)
x = Dropout(0.5)(x)
out = Dense(5,activation='softmax')(x)
model_final = Model(input = model.input,outputs=out)
if full_freeze != 'N':
for layer in model.layers[0:freeze_layers]:
layer.trainable = False
return model_final
# VGG16 Model for transfer Learning
示例8: classifier_layers
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def classifier_layers(x, input_shape, trainable=False):
# compile times on theano tend to be very high, so we use smaller ROI pooling regions to workaround
# (hence a smaller stride in the region that follows the ROI pool)
x = TimeDistributed(SeparableConv2D(1536, (3, 3),
padding='same',
use_bias=False),
name='block14_sepconv1')(x)
x = TimeDistributed(BatchNormalization(), name='block14_sepconv1_bn')(x)
x = Activation('relu', name='block14_sepconv1_act')(x)
x = TimeDistributed(SeparableConv2D(2048, (3, 3),
padding='same',
use_bias=False),
name='block14_sepconv2')(x)
x = TimeDistributed(BatchNormalization(), name='block14_sepconv2_bn')(x)
x = Activation('relu', name='block14_sepconv2_act')(x)
TimeDistributed(GlobalAveragePooling2D(), name='avg_pool')(x)
return x
示例9: _squeeze
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def _squeeze(self, inputs):
"""Squeeze and Excitation.
This function defines a squeeze structure.
# Arguments
inputs: Tensor, input tensor of conv layer.
"""
input_channels = int(inputs.shape[-1])
x = GlobalAveragePooling2D()(inputs)
x = Dense(input_channels, activation='relu')(x)
x = Dense(input_channels, activation='hard_sigmoid')(x)
x = Reshape((1, 1, input_channels))(x)
x = Multiply()([inputs, x])
return x
示例10: global_pool2d
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def global_pool2d():
def compile_fn(di, dh):
layer = layers.GlobalAveragePooling2D()
def fn(di):
return {'out': layer(di['in'])}
return fn
return siso_keras_module('GlobalAveragePool', compile_fn, {})
示例11: learn
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def learn():
(train_x, train_y, sample_weight), (test_x, test_y) = load_data()
datagen = ImageDataGenerator(horizontal_flip=True,
vertical_flip=True)
train_generator = datagen.flow(train_x, train_y, sample_weight=sample_weight)
base = VGG16(weights='imagenet', include_top=False, input_shape=(None, None, 3))
for layer in base.layers[:-4]:
layer.trainable = False
model = models.Sequential([
base,
layers.BatchNormalization(),
layers.Conv2D(64, (3, 3), activation='relu', padding='same'),
layers.GlobalAveragePooling2D(),
layers.BatchNormalization(),
layers.Dense(64, activation='relu'),
layers.BatchNormalization(),
layers.Dropout(0.20),
layers.Dense(80, activation='softmax')
])
model.compile(optimizer=optimizers.RMSprop(lr=1e-5),
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.summary()
reduce_lr = ReduceLROnPlateau(verbose=1)
model.fit_generator(train_generator, epochs=400,
steps_per_epoch=100,
validation_data=(test_x[:800], test_y[:800]),
callbacks=[reduce_lr])
result = model.evaluate(test_x, test_y)
print(result)
model.save('12306.image.model.h5', include_optimizer=False)
示例12: _squeeze_excite_block
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def _squeeze_excite_block(input, filters, k=1, name=None):
init = input
se_shape = (1, 1, filters * k) if K.image_data_format() == 'channels_last' else (filters * k, 1, 1)
se = GlobalAveragePooling2D()(init)
se = Reshape(se_shape)(se)
se = Dense((filters * k) // 16, activation='relu', kernel_initializer='he_normal', use_bias=False,name=name+'_fc1')(se)
se = Dense(filters * k, activation='sigmoid', kernel_initializer='he_normal', use_bias=False,name=name+'_fc2')(se)
return se
# pyramid pooling function
示例13: squeeze_excite_block
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def squeeze_excite_block(input, ratio=16):
''' Create a channel-wise squeeze-excite block
Args:
input: input tensor
filters: number of output filters
Returns: a keras tensor
References
- [Squeeze and Excitation Networks](https://arxiv.org/abs/1709.01507)
'''
init = input
channel_axis = 1 if K.image_data_format() == "channels_first" else -1
filters = init._keras_shape[channel_axis]
se_shape = (1, 1, filters)
se = GlobalAveragePooling2D()(init)
se = Reshape(se_shape)(se)
se = Dense(filters // ratio, activation='relu', kernel_initializer='he_normal', use_bias=False)(se)
se = Dense(filters, activation='sigmoid', kernel_initializer='he_normal', use_bias=False)(se)
if K.image_data_format() == 'channels_first':
se = Permute((3, 1, 2))(se)
x = multiply([init, se])
return x
示例14: squeeze_excite_block
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def squeeze_excite_block(input_tensor, ratio=16):
""" Create a channel-wise squeeze-excite block
Args:
input_tensor: input Keras tensor
ratio: number of output filters
Returns: a Keras tensor
References
- [Squeeze and Excitation Networks](https://arxiv.org/abs/1709.01507)
"""
init = input_tensor
channel_axis = 1 if K.image_data_format() == "channels_first" else -1
filters = _tensor_shape(init)[channel_axis]
se_shape = (1, 1, filters)
se = GlobalAveragePooling2D()(init)
se = Reshape(se_shape)(se)
se = Dense(filters // ratio, activation='relu', kernel_initializer='he_normal', use_bias=False)(se)
se = Dense(filters, activation='sigmoid', kernel_initializer='he_normal', use_bias=False)(se)
if K.image_data_format() == 'channels_first':
se = Permute((3, 1, 2))(se)
x = multiply([init, se])
return x
示例15: model_fn
# 需要导入模块: from keras import layers [as 别名]
# 或者: from keras.layers import GlobalAveragePooling2D [as 别名]
def model_fn(actions):
# unpack the actions from the list
kernel_1, filters_1, kernel_2, filters_2, kernel_3, filters_3, kernel_4, filters_4 = actions
ip = Input(shape=(32, 32, 3))
x = Conv2D(filters_1, (kernel_1, kernel_1), strides=(2, 2), padding='same', activation='relu')(ip)
x = Conv2D(filters_2, (kernel_2, kernel_2), strides=(1, 1), padding='same', activation='relu')(x)
x = Conv2D(filters_3, (kernel_3, kernel_3), strides=(2, 2), padding='same', activation='relu')(x)
x = Conv2D(filters_4, (kernel_4, kernel_4), strides=(1, 1), padding='same', activation='relu')(x)
x = GlobalAveragePooling2D()(x)
x = Dense(10, activation='softmax')(x)
model = Model(ip, x)
return model