keras用vgg16预训练的参数训练自己数据集
来源:互联网 发布:python mobi 编辑:程序博客网 时间:2024/05/17 08:57
1.下载vgg16模型预训练权重,将之放到~/.keras/model/下面,这样在keras导入vgg16的时候就不会联网下载,因为某些原因国内下载不了。。
2.通过vgg16来提取特征,不用输出层
通过vgg获取数据集特征,提取离线处理之后后面直接用,数据集和上次一样的车辆数据集一样,前面博文有说
import kerasfrom keras.applications.vgg16 import VGG16from keras.preprocessing import imagefrom keras.applications.vgg16 import preprocess_inputfrom keras.preprocessing.image import ImageDataGeneratorimport numpy as npmodel = VGG16(weights='imagenet', include_top=False)# img_path = '17.png'# img = image.load_img(img_path)# x = image.img_to_array(img)# x = np.expand_dims(x, axis=0)# x = preprocess_input(x)# features = model.predict(x)# print features# dimensions of our images.img_width, img_height = 64, 64train_data_dir = 'data/train'validation_data_dir = 'data/validation'nb_train_samples = 4000 #4000nb_validation_samples = 2000 #2000batch_size = 1datagen = ImageDataGenerator()generator = datagen.flow_from_directory( 'data/train', target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, # this means our generator will only yield batches of data, no labels shuffle=False) # our data will be in order, so all first 1000 images will be cats, then 1000 dogs# the predict_generator method returns the output of a model, given# a generator that yields batches of numpy dataprint "train begin\n"bottleneck_features_train = model.predict_generator(generator, nb_train_samples)# save the output as a Numpy arraynp.save(open('bottleneck_features_train.npy', 'w'), bottleneck_features_train)print "train over\n"generator = datagen.flow_from_directory( 'data/validation', target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, # this means our generator will only yield batches of data, no labels shuffle=False)bottleneck_features_validation = model.predict_generator(generator, nb_validation_samples)np.save(open('bottleneck_features_validation.npy', 'w'), bottleneck_features_validation)
训练输出层部分
from keras.preprocessing.image import ImageDataGeneratorfrom keras.models import Sequentialfrom keras.layers import Conv2D, MaxPooling2Dfrom keras.layers import Activation, Dropout, Flatten, Densefrom keras import backend as Kimport h5pyimport numpy as nptrain_data = np.load(open('bottleneck_features_train.npy'))# the features were saved in order, so recreating the labels is easytrain_labels = np.array([0] * 2000 + [1] * 2000)print train_data.shape[1:]validation_data = np.load(open('bottleneck_features_validation.npy'))validation_labels = np.array([0] * 1000 + [1] * 1000)print validation_data.shapemodel = Sequential()model.add(Flatten(input_shape=train_data.shape[1:]))model.add(Dense(1024, activation='relu'))model.add(Dropout(0.5))model.add(Dense(256, activation='relu'))model.add(Dropout(0.5))model.add(Dense(1, activation='sigmoid'))# model = Sequential()# model.add(Flatten(input_shape=train_data.shape[1:]))# model.add(Dense(256, activation='relu'))# model.add(Dropout(0.5))# model.add(Dense(1, activation='sigmoid'))model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])model.fit(train_data, train_labels, nb_epoch=50, batch_size=32, validation_data=(validation_data, validation_labels) )#model.save_weights('bottleneck_fc_model.h5')model.save('My_vgg.h5')
测试数据
import kerasfrom keras.models import load_modelfrom keras.models import Sequentialfrom keras.applications.vgg16 import VGG16from keras.preprocessing import imagefrom keras.applications.vgg16 import preprocess_inputfrom keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_imgfrom keras import backend as K#import cv2import numpy as npimport h5pyimport os#model = Sequential()modelVGG = VGG16(weights='imagenet', include_top=False)modelMY = load_model('My_vgg.h5')num_test=44count=0for i in range(num_test): #path='pic/'+str(i)+'.png'; #path='17.png' #path='data/validation/veh/'+str(i)+'.png' path='pic/'+str(i)+'.png' img = load_img(path) # this is a PIL image x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150) x = np.expand_dims(x, axis=0) x = preprocess_input(x) features = modelVGG.predict(x) # print i #print i,modelMY.predict_proba(features) if (modelMY.predict_proba(features) > 0.7 ): print iprint count*1.0/num_test # print '\n'
阅读全文
0 0
- keras用vgg16预训练的参数训练自己数据集
- 使用keras预训练VGG16模型参数分类图像并提取特征
- 详细说明用keras建立训练自己数据的LSTM----语音方向
- 用YOLOv2训练自己的数据集
- 用SSD训练自己的数据集
- VGG16预训练学习笔记
- 训练自己的数据
- 训练自己的数据
- 关于VGG16预训练的理解与实践
- Fast-rcnn 训练(3)- 训练自己的数据集
- [2] SSD配置+训练VOC0712+训练自己的数据集
- keras实现VGG16 CIFAR10数据集
- caffe 训练测试自己的数据集
- caffe训练测试自己的数据集
- Faster RCNN 训练自己的数据集
- Faster-RCNN训练自己的数据集
- Faster_rcnn训练自己的数据集
- Faster_rcnn训练自己的数据集(2)
- 笔记题
- phpcms v9 后台登陆地址 验证码错误等常见问题
- 括号配对问题
- 增强现实与虚拟现实,区别何在?
- C++ primer 5.4.1节练习
- keras用vgg16预训练的参数训练自己数据集
- 20170517 ASP.NET tips
- Linux(一)
- 用户体验设计5大目标
- [USACO5.3]校园网Network of Schools 强连通分量
- Retrofit上传图片
- 一次失败的hadoop集成eclipse的测试
- 求一个字符串中连续出现次数最多的子串
- java Data、String、Long三种日期类型之间的相互转换