1.模型测试
#-- coding: utf-8 --
‘’’
模型定义,以及predict 接口
‘’’
import numpy as np
from numpy import linalg as LA
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input,VGG16
from keras.models import Model, model_from_json, load_model
from keras.layers import Dropout, Flatten, Dense, Input, Conv2D, MaxPooling2D, GlobalMaxPooling2D
from keras import optimizers
class CNN_MODEL_T7:
def init(self, include_top = False):
self.input_shape = (224, 224, 3)
model1 = load_model('/opt/service/PictureSearchFaissWeb/src/PictureSearchFaissWeb/picture_classify/cnn-t10-fine-best.model')
classes = 7
img_input = Input(shape=self.input_shape)
# Block 1
x = Conv2D(64, (3, 3),
activation='relu',
padding='same',
name='block1_conv1')(img_input)
x = Conv2D(64, (3, 3),
activation='relu',
padding='same',
name='block1_conv2')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
# Block 2
x = Conv2D(128, (3, 3),
activation='relu',
padding='same',
name='block2_conv1')(x)
x = Conv2D(128, (3, 3),
activation='relu',
padding='same',
name='block2_conv2')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
# Block 3
x = Conv2D(256, (3, 3),
activation='relu',
padding='same',
name='block3_conv1')(x)
x = Conv2D(256, (3, 3),
activation='relu',
padding='same',
name='block3_conv2')(x)
x = Conv2D(256, (3, 3),
activation='relu',
padding='same',
name='block3_conv3')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
if not include_top:
#仅在提取bottle neck 特征时使用
x = GlobalMaxPooling2D()(x) #将shape 从7*7*512 降到了 1*1*512
else:
x = Flatten(input_shape=(28,28,512), name='flatten')(x)
x = Dense(512, activation='relu', name='fc-1')(x)
x = Dense(7, activation='relu', name='fc-2')(x)
x = Dense(classes, activation='softmax', name='softmax')(x)
self.model = Model(inputs = img_input, outputs = x)
for i, layer in enumerate(self.model.layers):
layer.set_weights(model1.get_layer(layer.name).get_weights())
print("Loaded VGG16 fine layer %s: layer %2d " %(layer.name, i))
# 输入层不算,从0开始数
#i == 9 层是 x = GlobalMaxPooling2D()(x)
if i == 9 and include_top==False:
break
self.model.compile(loss='categorical_crossentropy',
optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),
metrics=['accuracy'])
self.model.predict(np.zeros((1, 224, 224 , 3)))
def extract_feat(self, img_path):
img = image.load_img(img_path, target_size=(self.input_shape[0], self.input_shape[1]))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
feat = self.model.predict(img)
norm_feat = feat[0]/LA.norm(feat[0])
#print('norm_feat.shape = {}'.format(norm_feat.shape))
return norm_feat
def predict_classes(self, img_path):
try:
img = image.load_img(img_path, target_size=(self.input_shape[0], self.input_shape[1]))
except:
classes = -1
else:
img = image.img_to_array(img)/255.
img = np.expand_dims(img, axis=0)
#img = preprocess_input(img)
predict_test = self.model.predict(img)
predict = np.argmax(predict_test,axis=1)
classes = predict[0]
#print(predict_test[0:3])
return classes