本文整理汇总了Python中keras.applications方法的典型用法代码示例。如果您正苦于以下问题:Python keras.applications方法的具体用法?Python keras.applications怎么用?Python keras.applications使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras
的用法示例。
在下文中一共展示了keras.applications方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: download_imagenet
# 需要导入模块: import keras [as 别名]
# 或者: from keras import applications [as 别名]
def download_imagenet(self):
""" Download pre-trained weights for the specified backbone name.
This name is in the format {backbone}_weights_tf_dim_ordering_tf_
kernels_notop
where backbone is the densenet + number of layers (e.g. densenet121).
For more info check the explanation from the keras densenet script
itself:
https://github.com/keras-team/keras/blob/master/keras/applications
/densenet.py
"""
origin = 'https://github.com/fchollet/deep-learning-models/releases/'\
'download/v0.8/'
file_name = '{}_weights_tf_dim_ordering_tf_kernels_notop.h5'
# load weights
if keras.backend.image_data_format() == 'channels_first':
raise ValueError(
'Weights for "channels_first" format are not available.')
weights_url = origin + file_name.format(self.backbone)
return get_file(file_name.format(self.backbone),
weights_url, cache_subdir='models')
示例2: download_imagenet
# 需要导入模块: import keras [as 别名]
# 或者: from keras import applications [as 别名]
def download_imagenet(self):
""" Download pre-trained weights for the specified backbone name.
This name is in the format {backbone}_weights_tf_dim_ordering_tf_kernels_notop
where backbone is the densenet + number of layers (e.g. densenet121).
For more info check the explanation from the keras densenet script itself:
https://github.com/keras-team/keras/blob/master/keras/applications/densenet.py
"""
origin = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.8/'
file_name = '{}_weights_tf_dim_ordering_tf_kernels_notop.h5'
# load weights
if keras.backend.image_data_format() == 'channels_first':
raise ValueError('Weights for "channels_first" format are not available.')
weights_url = origin + file_name.format(self.backbone)
return get_file(file_name.format(self.backbone), weights_url, cache_subdir='models')
示例3: create_cnn_model
# 需要导入模块: import keras [as 别名]
# 或者: from keras import applications [as 别名]
def create_cnn_model(size_output=None, tf_print=False):
"""
create keras model with convolution layers of MobileNet and added fully connected layers on to top
:param size_output: number of nodes in the output layer
:param tf_print: True/False to print
:return: keras model object
"""
if size_output is None:
# get number of attrubutes, needed for defining the final layer size of network
df_attr = pd.read_csv(path_celeba_att, sep='\s+', header=1, index_col=0)
size_output = df_attr.shape[1]
# Load the convolutional layers of pretrained model: mobilenet
base_model = keras.applications.mobilenet.MobileNet(include_top=False, input_shape=(128,128,3),
alpha=1, depth_multiplier=1,
dropout=0.001, weights="imagenet",
input_tensor=None, pooling=None)
# add fully connected layers
fc0 = base_model.output
fc0_pool = layers.GlobalAveragePooling2D(data_format='channels_last', name='fc0_pool')(fc0)
fc1 = layers.Dense(256, activation='relu', name='fc1_dense')(fc0_pool)
fc2 = layers.Dense(size_output, activation='tanh', name='fc2_dense')(fc1)
model = keras.models.Model(inputs=base_model.input, outputs=fc2)
# freeze the early layers
for layer in base_model.layers:
layer.trainable = False
model.compile(optimizer='sgd', loss='mean_squared_error')
if tf_print:
print('use convolution layers of MobileNet, add fully connected layers')
print(model.summary())
return model
示例4: t_read_image
# 需要导入模块: import keras [as 别名]
# 或者: from keras import applications [as 别名]
def t_read_image(loc):
t_image = cv2.imread(loc)
t_image = cv2.resize(t_image, (T_G_HEIGHT,T_G_WIDTH))
t_image = t_image.astype("float32")
t_image = keras.applications.resnet50.preprocess_input(t_image, data_format='channels_last')
return t_image
# loads a set of images from a text index file
示例5: createModel
# 需要导入模块: import keras [as 别名]
# 或者: from keras import applications [as 别名]
def createModel(emb_size):
# Initialize a ResNet50_ImageNet Model
resnet_input = kl.Input(shape=(T_G_WIDTH,T_G_HEIGHT,T_G_NUMCHANNELS))
resnet_model = keras.applications.resnet50.ResNet50(weights='imagenet', include_top = False, input_tensor=resnet_input)
# New Layers over ResNet50
net = resnet_model.output
#net = kl.Flatten(name='flatten')(net)
net = kl.GlobalAveragePooling2D(name='gap')(net)
#net = kl.Dropout(0.5)(net)
net = kl.Dense(emb_size,activation='relu',name='t_emb_1')(net)
net = kl.Lambda(lambda x: K.l2_normalize(x,axis=1), name='t_emb_1_l2norm')(net)
# model creation
base_model = Model(resnet_model.input, net, name="base_model")
# triplet framework, shared weights
input_shape=(T_G_WIDTH,T_G_HEIGHT,T_G_NUMCHANNELS)
input_anchor = kl.Input(shape=input_shape, name='input_anchor')
input_positive = kl.Input(shape=input_shape, name='input_pos')
input_negative = kl.Input(shape=input_shape, name='input_neg')
net_anchor = base_model(input_anchor)
net_positive = base_model(input_positive)
net_negative = base_model(input_negative)
# The Lamda layer produces output using given function. Here its Euclidean distance.
positive_dist = kl.Lambda(euclidean_distance, name='pos_dist')([net_anchor, net_positive])
negative_dist = kl.Lambda(euclidean_distance, name='neg_dist')([net_anchor, net_negative])
tertiary_dist = kl.Lambda(euclidean_distance, name='ter_dist')([net_positive, net_negative])
# This lambda layer simply stacks outputs so both distances are available to the objective
stacked_dists = kl.Lambda(lambda vects: K.stack(vects, axis=1), name='stacked_dists')([positive_dist, negative_dist, tertiary_dist])
model = Model([input_anchor, input_positive, input_negative], stacked_dists, name='triple_siamese')
# Setting up optimizer designed for variable learning rate
# Variable Learning Rate per Layers
lr_mult_dict = {}
last_layer = ''
for layer in resnet_model.layers:
# comment this out to refine earlier layers
# layer.trainable = False
# print layer.name
lr_mult_dict[layer.name] = 1
# last_layer = layer.name
lr_mult_dict['t_emb_1'] = 100
base_lr = 0.0001
momentum = 0.9
v_optimizer = LR_SGD(lr=base_lr, momentum=momentum, decay=0.0, nesterov=False, multipliers = lr_mult_dict)
model.compile(optimizer=v_optimizer, loss=triplet_loss, metrics=[accuracy])
return model