Low accuracy with the model converted from caffemodel


#1

Hello,

I used mxnet/tools/caffe_converter to make mxnet model from my resnet-152 caffemodel.

The problem is that the accuracy dropped (0.93 --> 0.88) with the converted mxnet model.

I think there is no code related with the modelname-mean.nd file, I guess it may contribute the drop of accuracy.

I try to search some tutorial related with this problem, I cannot find any code for handling the modelname-mean.nd file.

Thank you in advance. :slight_smile:

import mxnet as mx
import numpy as np
import cv2
from collections import namedtuple

def loadmodel():
        # mxnet model name = feedr4
        sym, arg_params, aux_params = mx.model.load_checkpoint('feedr4', 0)
        mod = mx.mod.Module(symbol=sym,context=mx.gpu(),label_names=None)
        mod.bind(for_training=False, data_shapes=[('data', (1,3,224,224))])
        arg_params['prob_label'] = mx.nd.array([0])
        mod.set_params(arg_params, aux_params,allow_missing=True)
        return mod

def prepareNDArray(filename):
        img = cv2.imread(filename)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        
        #histogram normalize
        img[:, :, 0] = cv2.equalizeHist(img[:, :, 0])
        img[:, :, 1] = cv2.equalizeHist(img[:, :, 1])
        img[:, :, 2] = cv2.equalizeHist(img[:, :, 2])
        img = cv2.resize(img, (224, 224), interpolation = cv2.INTER_CUBIC)
        
        #mean_nd=mx.nd.load('feedr4-mean.nd')
        #print mean_nd
        
        img = np.swapaxes(img, 0, 2)
        img = np.swapaxes(img, 1, 2)
        img = img[np.newaxis, :]
        return mx.nd.array(img)

def predict(filename, model, n,list_dx):
        array = prepareNDArray(filename)
        Batch = namedtuple('Batch', ['data'])
        model.forward(Batch([array]))
        prob = model.get_outputs()[0].asnumpy()
        prob = np.squeeze(prob)
        sortedprobindex = np.argsort(prob)[::-1]
        topn = []
        for i in sortedprobindex[0:n]:
                topn.append((prob[i], list_dx[i]))
        return topn

#list_dx --> class labels
with open('deploy.prototxt') as f:
    lines = f.readlines()
list_dx=lines[0][1:-1].split(',')
        
m=[]
for j in range(0,1):
    m += [loadmodel()]
for i in range(0,1):
    for kk,m_ in enumerate(m):
        topn = predict("test.jpg",m_,5,list_dx)
        print kk
        print topn