图像分类二之离线模型测试

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值