赞
踩
1.下载vgg16模型预训练权重,将之放到~/.keras/model/下面,这样在keras导入vgg16的时候就不会联网下载,因为某些原因国内下载不了。。
2.通过vgg16来提取特征,不用输出层
通过vgg获取数据集特征,提取离线处理之后后面直接用,数据集和上次一样的车辆数据集一样,前面博文有说
- import keras
- from keras.applications.vgg16 import VGG16
- from keras.preprocessing import image
- from keras.applications.vgg16 import preprocess_input
- from keras.preprocessing.image import ImageDataGenerator
- import numpy as np
-
- model = 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, 64
-
- train_data_dir = 'data/train'
- validation_data_dir = 'data/validation'
- nb_train_samples = 4000 #4000
- nb_validation_samples = 2000 #2000
-
- batch_size = 1
-
- datagen = 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 data
- print "train begin\n"
-
- bottleneck_features_train = model.predict_generator(generator, nb_train_samples)
- # save the output as a Numpy array
- np.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 ImageDataGenerator
- from keras.models import Sequential
- from keras.layers import Conv2D, MaxPooling2D
- from keras.layers import Activation, Dropout, Flatten, Dense
- from keras import backend as K
- import h5py
- import numpy as np
- train_data = np.load(open('bottleneck_features_train.npy'))
- # the features were saved in order, so recreating the labels is easy
- train_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.shape
-
- model = 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 keras
- from keras.models import load_model
- from keras.models import Sequential
-
- from keras.applications.vgg16 import VGG16
- from keras.preprocessing import image
- from keras.applications.vgg16 import preprocess_input
-
- from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
- from keras import backend as K
- #import cv2
- import numpy as np
- import h5py
- import os
-
- #model = Sequential()
- modelVGG = VGG16(weights='imagenet', include_top=False)
-
- modelMY = load_model('My_vgg.h5')
- num_test=44
-
- count=0
- for 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 i
-
- print count*1.0/num_test
- # print '\n'
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。