当前位置:   article > 正文

keras用vgg16预训练的参数训练自己数据集_vgg16训练参数

vgg16训练参数

1.下载vgg16模型预训练权重,将之放到~/.keras/model/下面,这样在keras导入vgg16的时候就不会联网下载,因为某些原因国内下载不了。。

2.通过vgg16来提取特征,不用输出层

通过vgg获取数据集特征,提取离线处理之后后面直接用,数据集和上次一样的车辆数据集一样,前面博文有说

  1. import keras
  2. from keras.applications.vgg16 import VGG16
  3. from keras.preprocessing import image
  4. from keras.applications.vgg16 import preprocess_input
  5. from keras.preprocessing.image import ImageDataGenerator
  6. import numpy as np
  7. model = VGG16(weights='imagenet', include_top=False)
  8. # img_path = '17.png'
  9. # img = image.load_img(img_path)
  10. # x = image.img_to_array(img)
  11. # x = np.expand_dims(x, axis=0)
  12. # x = preprocess_input(x)
  13. # features = model.predict(x)
  14. # print features
  15. # dimensions of our images.
  16. img_width, img_height = 64, 64
  17. train_data_dir = 'data/train'
  18. validation_data_dir = 'data/validation'
  19. nb_train_samples = 4000 #4000
  20. nb_validation_samples = 2000 #2000
  21. batch_size = 1
  22. datagen = ImageDataGenerator()
  23. generator = datagen.flow_from_directory(
  24. 'data/train',
  25. target_size=(img_width, img_height),
  26. batch_size=batch_size,
  27. class_mode=None, # this means our generator will only yield batches of data, no labels
  28. shuffle=False) # our data will be in order, so all first 1000 images will be cats, then 1000 dogs
  29. # the predict_generator method returns the output of a model, given
  30. # a generator that yields batches of numpy data
  31. print "train begin\n"
  32. bottleneck_features_train = model.predict_generator(generator, nb_train_samples)
  33. # save the output as a Numpy array
  34. np.save(open('bottleneck_features_train.npy', 'w'), bottleneck_features_train)
  35. print "train over\n"
  36. generator = datagen.flow_from_directory(
  37. 'data/validation',
  38. target_size=(img_width, img_height),
  39. batch_size=batch_size,
  40. class_mode=None, # this means our generator will only yield batches of data, no labels
  41. shuffle=False)
  42. bottleneck_features_validation = model.predict_generator(generator, nb_validation_samples)
  43. np.save(open('bottleneck_features_validation.npy', 'w'), bottleneck_features_validation)


训练输出层部分

  1. from keras.preprocessing.image import ImageDataGenerator
  2. from keras.models import Sequential
  3. from keras.layers import Conv2D, MaxPooling2D
  4. from keras.layers import Activation, Dropout, Flatten, Dense
  5. from keras import backend as K
  6. import h5py
  7. import numpy as np
  8. train_data = np.load(open('bottleneck_features_train.npy'))
  9. # the features were saved in order, so recreating the labels is easy
  10. train_labels = np.array([0] * 2000 + [1] * 2000)
  11. print train_data.shape[1:]
  12. validation_data = np.load(open('bottleneck_features_validation.npy'))
  13. validation_labels = np.array([0] * 1000 + [1] * 1000)
  14. print validation_data.shape
  15. model = Sequential()
  16. model.add(Flatten(input_shape=train_data.shape[1:]))
  17. model.add(Dense(1024, activation='relu'))
  18. model.add(Dropout(0.5))
  19. model.add(Dense(256, activation='relu'))
  20. model.add(Dropout(0.5))
  21. model.add(Dense(1, activation='sigmoid'))
  22. # model = Sequential()
  23. # model.add(Flatten(input_shape=train_data.shape[1:]))
  24. # model.add(Dense(256, activation='relu'))
  25. # model.add(Dropout(0.5))
  26. # model.add(Dense(1, activation='sigmoid'))
  27. model.compile(optimizer='rmsprop',
  28. loss='binary_crossentropy',
  29. metrics=['accuracy'])
  30. model.fit(train_data, train_labels,
  31. nb_epoch=50, batch_size=32,
  32. validation_data=(validation_data, validation_labels) )
  33. #model.save_weights('bottleneck_fc_model.h5')
  34. model.save('My_vgg.h5')


测试数据

  1. import keras
  2. from keras.models import load_model
  3. from keras.models import Sequential
  4. from keras.applications.vgg16 import VGG16
  5. from keras.preprocessing import image
  6. from keras.applications.vgg16 import preprocess_input
  7. from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
  8. from keras import backend as K
  9. #import cv2
  10. import numpy as np
  11. import h5py
  12. import os
  13. #model = Sequential()
  14. modelVGG = VGG16(weights='imagenet', include_top=False)
  15. modelMY = load_model('My_vgg.h5')
  16. num_test=44
  17. count=0
  18. for i in range(num_test):
  19. #path='pic/'+str(i)+'.png';
  20. #path='17.png'
  21. #path='data/validation/veh/'+str(i)+'.png'
  22. path='pic/'+str(i)+'.png'
  23. img = load_img(path) # this is a PIL image
  24. x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150)
  25. x = np.expand_dims(x, axis=0)
  26. x = preprocess_input(x)
  27. features = modelVGG.predict(x)
  28. # print i
  29. #print i,modelMY.predict_proba(features)
  30. if (modelMY.predict_proba(features) > 0.7 ):
  31. print i
  32. print count*1.0/num_test
  33. # print '\n'



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/103961?site
推荐阅读
相关标签
  

闽ICP备14008679号