Keras —— 应用模型
来源:互联网 发布:名词解释软件项目管理 编辑:程序博客网 时间:2024/06/09 22:24
Keras应用是可用的具有预训练权重的深度学习模型。这些模型可用于预测,特征提取和细调。
实例化模型时权重自动下载,储存在~/.keras/models/
可用模型
在ImageNet上预训练权重的图像分类模型有:
-Xception
-VGG16
-VGG19
-ResNet50
-InceptionV3
Xception模型只有TensorFlow版,因为它依赖于SeparableConvolution层。其他模型有TensorFlow和Theano两个版本。
图像分类模型使用举例
使用ResNet50分类图像
from keras.applications.resnet50 import ResNet50from keras.preprocessing import imagefrom keras.applications.resnet50 import preprocess_input, decode_predictionsimport numpy as npmodel = ResNet50(weights='imagenet')img_path = 'elephant.jpg'img = image.load_img(img_path, target_size=(224, 224))x = image.img_to_array(img)x = np.expand_dims(x, axis=0)x = preprocess_input(x)preds = model.predict(x)# decode the results into a list of tuples (class, description, probability)# (one such list for each sample in the batch)print('Predicted:', decode_predictions(preds, top=3)[0])# Predicted: [(u'n02504013', u'Indian_elephant', 0.82658225), (u'n01871265', u'tusker', 0.1122357), (u'n02504458', u'African_elephant', 0.061040461)]
使用VGG16提取特征
from keras.applications.vgg16 import VGG16from keras.preprocessing import imagefrom keras.applications.vgg16 import preprocess_inputimport numpy as npmodel = VGG16(weights='imagenet', include_top=False)img_path = 'elephant.jpg'img = image.load_img(img_path, target_size=(224, 224))x = image.img_to_array(img)x = np.expand_dims(x, axis=0)x = preprocess_input(x)features = model.predict(x)
从VGG19任意中间层提取特征
from keras.applications.vgg19 import VGG19from keras.preprocessing import imagefrom keras.applications.vgg19 import preprocess_inputfrom keras.models import Modelimport numpy as npbase_model = VGG19(weights='imagenet')model = Model(inputs=base_model.input, outputs=base_model.get_layer('block4_pool').output)img_path = 'elephant.jpg'img = image.load_img(img_path, target_size=(224, 224))x = image.img_to_array(img)x = np.expand_dims(x, axis=0)x = preprocess_input(x)block4_pool_features = model.predict(x)
在新类别上细调InceptionV3
from keras.applications.inception_v3 import InceptionV3from keras.preprocessing import imagefrom keras.models import Modelfrom keras.layers import Dense, GlobalAveragePooling2Dfrom keras import backend as K# create the base pre-trained modelbase_model = InceptionV3(weights='imagenet', include_top=False)# add a global spatial average pooling layerx = base_model.outputx = GlobalAveragePooling2D()(x)# let's add a fully-connected layerx = Dense(1024, activation='relu')(x)# and a logistic layer -- let's say we have 200 classespredictions = Dense(200, activation='softmax')(x)# this is the model we will trainmodel = Model(inputs=base_model.input, outputs=predictions)# first: train only the top layers (which were randomly initialized)# i.e. freeze all convolutional InceptionV3 layersfor layer in base_model.layers: layer.trainable = False# compile the model (should be done *after* setting layers to non-trainable)model.compile(optimizer='rmsprop', loss='categorical_crossentropy')# train the model on the new data for a few epochsmodel.fit_generator(...)# at this point, the top layers are well trained and we can start fine-tuning# convolutional layers from inception V3. We will freeze the bottom N layers# and train the remaining top layers.# let's visualize layer names and layer indices to see how many layers# we should freeze:for i, layer in enumerate(base_model.layers): print(i, layer.name)# we chose to train the top 2 inception blocks, i.e. we will freeze# the first 172 layers and unfreeze the rest:for layer in model.layers[:172]: layer.trainable = Falsefor layer in model.layers[172:]: layer.trainable = True# we need to recompile the model for these modifications to take effect# we use SGD with a low learning ratefrom keras.optimizers import SGDmodel.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy')# we train our model again (this time fine-tuning the top 2 inception blocks# alongside the top Dense layersmodel.fit_generator(...)
在定制输入张量上构建InceptionV3
from keras.applications.inception_v3 import InceptionV3from keras.layers import Input# this could also be the output a different Keras model or layerinput_tensor = Input(shape=(224, 224, 3)) # this assumes K.image_data_format() == 'channels_last'model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=True)
模型文档
Xception
在ImageNet上,模型top-1验证准确率为0.790,top-5验证准确率0.945。keras.applications.xception.Xception(include_top=True, weights='imagenet', input_tensor=None, input_shape=None)
注意,由于依赖SeparableConvolution层,该模型只支持TensorFlow后端。此外只支持数据格式"channel_last"(高度、宽度、通道)
默认输入大小为299*299
VGG16
默认输入大小为224*224keras.applications.vgg16.VGG16(include_top=True, weights='imagenet', input_tensor=None, input_shape=None)
VGG19
默认输入大小为224*224keras.applications.vgg19.VGG19(include_top=True, weights='imagenet', input_tensor=None, input_shape=None)
ResNet50
keras.applications.resnet50.ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None)默认输入大小为224*224
InceptionV3
默认输入大小为299*299keras.applications.inception_v3.InceptionV3(include_top=True, weights='imagenet', input_tensor=None, input_shape=None)
阅读全文
0 0
- Keras —— 应用模型
- keras——函数式模型
- keras —— 常用模型构建
- keras学习随笔02——Sequential模型
- keras的基本用法(一)——回归模型
- keras中文文档笔记7——序贯模型
- keras中文文档笔记8——函数式模型
- 深度学习——keras训练AutoEncoder模型
- 深度学习——keras训练RNN模型
- Keras模型
- Keras——Tensorflow
- lstm——keras
- (人脸识别2-6)——利用keras训练识别模型
- keras中文文档笔记15——面向小数据集构建图像分类模型
- Keras快速上手——打造个人的第一个“圣诞老人”图像分类模型
- keras的模型可视化
- 如何保存Keras模型
- 如何保存Keras模型?
- vert.x笔记:1.vert.x介绍
- PID 参数整定口诀
- AJAX
- vert.x笔记:2.hello vert.x--第一个vert.x hello world工程
- [YTU]_2919( Shape系列-5)
- Keras —— 应用模型
- nginx配置逻辑运算实现方式 与 && 或 ||
- Nginx和PHP-FPM的启动、重启、停止脚本分享
- vert.x笔记:3.使用vert.x发布restful接口
- 微信小程序 Mustache
- Ajax请求中的async:false/true的作用
- [YTU]_2920( Shape系列-6)
- vert.x笔记:4.vert.x中调用spring服务
- 开源机器学习软件