本文整理汇总了Python中keras.applications.resnet50.ResNet50方法的典型用法代码示例。如果您正苦于以下问题:Python resnet50.ResNet50方法的具体用法?Python resnet50.ResNet50怎么用?Python resnet50.ResNet50使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.applications.resnet50
的用法示例。
在下文中一共展示了resnet50.ResNet50方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resnet_pseudo
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [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
示例2: _imagenet_preprocess_input
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [as 别名]
def _imagenet_preprocess_input(x, input_shape):
"""
For ResNet50, VGG models. For InceptionV3 and Xception it's okay to use the
keras version (e.g. InceptionV3.preprocess_input) as the code path they hit
works okay with tf.Tensor inputs. The following was translated to tf ops from
https://github.com/fchollet/keras/blob/fb4a0849cf4dc2965af86510f02ec46abab1a6a4/keras/applications/imagenet_utils.py#L52
It's a possibility to change the implementation in keras to look like the
following and modified to work with BGR images (standard in Spark), but not doing it for now.
"""
# assuming 'BGR'
# Zero-center by mean pixel
mean = np.ones(input_shape + (3,), dtype=np.float32)
mean[..., 0] = 103.939
mean[..., 1] = 116.779
mean[..., 2] = 123.68
return x - mean
示例3: main
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [as 别名]
def main(self):
self.logger.info('Will load keras model')
model = ResNet50(weights='imagenet')
self.logger.info('Keras model loaded')
feature_list = []
img_path_list = []
for raw_file in self.inp.raw_files:
media_path = raw_file.path
file_list = os.listdir(media_path)
total = float(len(file_list))
for index, img_file in enumerate(file_list):
img_path = os.path.join(media_path, img_file)
img_path_list.append(img_path)
img = image.load_img(img_path, target_size=(224, 224))
x = keras_image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# extract features
scores = model.predict(x)
sim_class = np.argmax(scores)
print('Scores {}\nSimClass: {}'.format(scores, sim_class))
self.outp.request_annos(img_path, img_sim_class=sim_class)
self.logger.info('Requested annotation for: {} (cluster: {})'.format(img_path, sim_class))
self.update_progress(index*100/total)
示例4: inception_pseudo
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [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
示例5: inception_pseudo
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [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
示例6: resnet_pseudo
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [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
示例7: resnet_pseudo
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [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: _model_backbone_headless
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [as 别名]
def _model_backbone_headless(self):
if self.config.backbone_nn_type == 'vgg':
model = VGG16(weights='imagenet', include_top=False)
# 畳み込み層の後のプーリング層を除く
# https://github.com/keras-team/keras/issues/2371
# https://github.com/keras-team/keras/issues/6229
# http://forums.fast.ai/t/how-to-finetune-with-new-keras-api/2328/9
model.layers.pop()
else:
model = ResNet50(weights='imagenet', include_top=False)
# VGGの重みは学習対象外
for layer in model.layers:
layer.trainable = False
output = model.layers[-1].output
_input = model.input
return _input, output
示例9: resnet
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [as 别名]
def resnet(self):
"""Build the structure of a convolutional neural network from input
image data to the last hidden layer on a similar manner than ResNet
See: He, Zhang, Ren, Sun. Deep Residual Learning for Image
Recognition. ArXiv technical report, 2015.
Returns
-------
tensor
(batch_size, nb_labels)-shaped output predictions, that have to be
compared with ground-truth values
"""
resnet_model = resnet50.ResNet50(
include_top=False, input_tensor=self.X
)
y = self.flatten(resnet_model.output)
return self.output_layer(y, depth=self.nb_labels)
示例10: get_model
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [as 别名]
def get_model(n_classes=1):
base_model = ResNet50(weights='imagenet', include_top=False)
#for layer in base_model.layers:
# layer.trainable = False
x = base_model.output
x = GlobalMaxPooling2D()(x)
x = Dropout(0.5)(x)
x = Dense(100, activation="relu")(x)
x = Dropout(0.5)(x)
if n_classes == 1:
x = Dense(n_classes, activation="sigmoid")(x)
else:
x = Dense(n_classes, activation="softmax")(x)
base_model = Model(base_model.input, x, name="base_model")
if n_classes == 1:
base_model.compile(loss="binary_crossentropy", metrics=['acc'], optimizer="adam")
else:
base_model.compile(loss="sparse_categorical_crossentropy", metrics=['acc'], optimizer="adam")
return base_model
示例11: ResNet50
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [as 别名]
def ResNet50(input_shape, num_classes):
# wrap ResNet50 from keras, because ResNet50 is so deep.
from keras.applications.resnet50 import ResNet50
input_tensor = Input(shape=input_shape, name="input")
x = ResNet50(include_top=False,
weights=None,
input_tensor=input_tensor,
input_shape=None,
pooling="avg",
classes=num_classes)
x = Dense(units=2048, name="feature")(x.output)
return Model(inputs=input_tensor, outputs=x)
# implement ResNet's block.
# I implement two classes block:
# one is basic block, the other is bottleneck block.
示例12: _get_base_model
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [as 别名]
def _get_base_model(self):
"""
:return: base model from Keras based on user-supplied model name
"""
if self.model_name == 'inception_v3':
return InceptionV3(weights='imagenet', include_top=False)
elif self.model_name == 'xception':
return Xception(weights='imagenet', include_top=False)
elif self.model_name == 'vgg16':
return VGG16(weights='imagenet', include_top=False)
elif self.model_name == 'vgg19':
return VGG19(weights='imagenet', include_top=False)
elif self.model_name == 'resnet50':
return ResNet50(weights='imagenet', include_top=False)
else:
raise ValueError('Cannot find base model %s' % self.model_name)
示例13: createResNetModel
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [as 别名]
def createResNetModel():
input_shape = (config.img_height, config.img_width, 3)
input_img = Input(shape=input_shape, name='input_1')
model = ResNet50(include_top=False, input_tensor=input_img)
x = model.layers[-2].output
# FC layer
x = Cropping2D(cropping=((0, 1), (0, 1)), name='cropping2d_2')(x)
h_grid = int(round(config.img_height / 100))
w_grid = int(round(config.img_width / 100))
x = Conv2DTranspose(config.classes, (64, 64),
#output_shape=(None, h_grid * 102, w_grid * 102, config.classes),
strides=(34, 34),
padding='same',
name='deconvolution2d_1')(x)
x = Cropping2D(cropping=((h_grid, h_grid), (w_grid, w_grid)), name='cropping2d_1')(x)
x = Reshape((config.img_width * config.img_height, config.classes), name='reshape_1')(x)
out = Activation("softmax", name='activation_fc')(x)
model = Model(model.layers[0].input, out)
return model
示例14: model_3
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [as 别名]
def model_3():
input_layer = Input(shape=(224,224,3))
from keras.layers import Conv2DTranspose as DeConv
resnet = ResNet50(include_top=False, weights="imagenet")
resnet.trainable = False
res_features = resnet(input_layer)
conv = DeConv(1024, padding="valid", activation="relu", kernel_size=3)(res_features)
conv = UpSampling2D((2,2))(conv)
conv = DeConv(512, padding="valid", activation="relu", kernel_size=5)(conv)
conv = UpSampling2D((2,2))(conv)
conv = DeConv(128, padding="valid", activation="relu", kernel_size=5)(conv)
conv = UpSampling2D((2,2))(conv)
conv = DeConv(32, padding="valid", activation="relu", kernel_size=5)(conv)
conv = UpSampling2D((2,2))(conv)
conv = DeConv(8, padding="valid", activation="relu", kernel_size=5)(conv)
conv = UpSampling2D((2,2))(conv)
conv = DeConv(4, padding="valid", activation="relu", kernel_size=5)(conv)
conv = DeConv(1, padding="valid", activation="sigmoid", kernel_size=5)(conv)
model = Model(inputs=input_layer, outputs=conv)
return model
示例15: PSPNet
# 需要导入模块: from keras.applications import resnet50 [as 别名]
# 或者: from keras.applications.resnet50 import ResNet50 [as 别名]
def PSPNet(n_classes = 3, input_shape = (128, 128, 4)):
# Input to the model
inputs = Input(input_shape)
'''in_shape = inputs.shape
out_shape = (in_shape[1], in_shape[2], 3)'''
# Converting 4 channel input to a 3 channel map using Encoder-Decoder network
# to give it as a input to ResNet50 with pretrained weights
res_input = encoder_decoder(inputs)
res_input_shape = K.int_shape(res_input)
res_input_shape = (res_input_shape[1],res_input_shape[2],res_input_shape[3])
# Passing the 3 channel map into ResNet50 followed by 2 upsampling layers
# to get a output of shape exactly 1/8th of the input map shape
res = resnet(res_input, input_shape = res_input_shape)
# Pyramid Pooling Module
ppmodule_out = pyramid_pooling_module(res)
# Final Conv layers and output
x = Conv2D(512, 3, activation = 'relu', padding='same')(ppmodule_out)
x = BatchNormalization()(x)
x = Dropout(0.5)(x)
x = Conv2D(n_classes, 1)(x)
#x = interpolation(x, shape = (input_shape[0], input_shape[1]))
x = Lambda(interpolation, arguments={'shape': (input_shape[0], input_shape[1])})(x)
out = Activation('softmax')(x)
model = Model(inputs = inputs, outputs = out)
adam = Adam(lr = 0.00001)
model.compile(optimizer = adam, loss = 'categorical_crossentropy', metrics = ['accuracy'])
model.summary()
return model