def test_predictor_with_dtype():
    prefix = 'test_predictor_simple_dense'
    symbol_file = "%s-symbol.json" % prefix
    param_file = "%s-0000.params" % prefix

    input1 = np.random.uniform(size=(1, 3))
    input1 = input1.astype(np.float16)

    block = mx.gluon.nn.HybridSequential()
    block.add(mx.gluon.nn.Dense(7))
    block.add(mx.gluon.nn.Dense(3))
    block.cast(np.float16)
    block.hybridize()
    block.initialize(ctx=mx.gpu(0))
    tmp = mx.nd.array(input1, dtype=np.float16, ctx=mx.gpu(0))
    out1 = block.forward(tmp)
    block.export(prefix)

    predictor = Predictor(open(symbol_file, "r").read(),
                          open(param_file, "rb").read(),
                          {"data": input1.shape},
                          dev_type="gpu",
                          dev_id=0,
                          type_dict={"data": input1.dtype})
    predictor.forward(data=input1)
    predictor_out1 = predictor.get_output(0)

    assert_almost_equal(out1.asnumpy(), predictor_out1, rtol=1e-5, atol=1e-6)
示例#2
0
    def __init_ocr(self):
        num_label = 4  # Set your max length of label, add one more for blank
        batch_size = 1

        num_hidden = 100
        num_lstm_layer = 2
        init_c = [('l%d_init_c' % l, (batch_size, num_hidden))
                  for l in range(num_lstm_layer)]
        init_h = [('l%d_init_h' % l, (batch_size, num_hidden))
                  for l in range(num_lstm_layer)]
        init_states = init_c + init_h

        init_state_arrays = np.zeros((batch_size, num_hidden), dtype="float32")
        self.init_state_dict = {}
        for x in init_states:
            self.init_state_dict[x[0]] = init_state_arrays

        all_shapes = [('data', (batch_size, 80, 30))
                      ] + init_states + [('label', (batch_size, num_label))]
        all_shapes_dict = {}
        for _shape in all_shapes:
            all_shapes_dict[_shape[0]] = _shape[1]
        self.predictor = Predictor(
            open(self.path_of_json, 'rb').read(),
            open(self.path_of_params, 'rb').read(), all_shapes_dict)
示例#3
0
    def __init_ocr(self):
        init_c = [('l%d_init_c'%l, (self.batch_size, self.num_hidden)) for l in range(self.num_lstm_layer*2)]
        init_h = [('l%d_init_h'%l, (self.batch_size, self.num_hidden)) for l in range(self.num_lstm_layer*2)]
        init_states = init_c + init_h

        all_shapes = [('data', (batch_size, 1, self.data_shape[1], self.data_shape[0]))] + init_states + [('label', (self.batch_size, self.num_label))]
        all_shapes_dict = {}
        for _shape in all_shapes:
            all_shapes_dict[_shape[0]] = _shape[1]
        self.predictor = Predictor(open(self.path_of_json).read(),
                                    open(self.path_of_params).read(),
                                    all_shapes_dict,dev_type="gpu", dev_id=0)
示例#4
0
    def __init_ocr(self):
        num_label = 4  # Set your max length of label, add one more for blank
        batch_size = 1

        num_hidden = 100
        num_lstm_layer = 2
        init_c = [('l%d_init_c' % l, (batch_size, num_hidden))
                  for l in range(num_lstm_layer)]
        init_h = [('l%d_init_h' % l, (batch_size, num_hidden))
                  for l in range(num_lstm_layer)]
        init_states = init_c + init_h

        all_shapes = [('data', (batch_size, 80 * 30))
                      ] + init_states + [('label', (batch_size, num_label))]
        all_shapes_dict = {}
        for _shape in all_shapes:
            all_shapes_dict[_shape[0]] = _shape[1]
        self.predictor = Predictor(
            open(self.path_of_json).read(),
            open(self.path_of_params).read(), all_shapes_dict)
def test_predictor():
    prefix = 'test_predictor_simple_dense'
    symbol_file = "%s-symbol.json" % prefix
    param_file = "%s-0000.params" % prefix

    # two inputs with different batch sizes
    input1 = np.random.uniform(size=(1, 3))
    input2 = np.random.uniform(size=(3, 3))

    # define a simple model
    block = gluon.nn.HybridSequential()
    block.add(gluon.nn.Dense(7))
    block.add(gluon.nn.Dense(3))
    block.hybridize()
    block.initialize()
    out1 = block.forward(nd.array(input1))
    out2 = block.forward(nd.array(input2))
    block.export(prefix)

    # create a predictor
    predictor = Predictor(
        open(symbol_file, "r").read(),
        open(param_file, "rb").read(), {'data': input1.shape})

    # forward and get output
    predictor.forward(data=input1)
    predictor_out1 = predictor.get_output(0)
    assert_almost_equal(out1.asnumpy(), predictor_out1, rtol=1e-5, atol=1e-6)

    # reshape
    predictor.reshape({'data': input2.shape})
    predictor.forward(data=input2)
    predictor_out2 = predictor.get_output(0)
    assert_almost_equal(out2.asnumpy(), predictor_out2, rtol=1e-5, atol=1e-6)

    # destroy the predictor
    del predictor
示例#6
0
    def __init_ocr(self):
        num_label = 4 # Set your max length of label, add one more for blank
        batch_size = 1

        num_hidden = 100
        num_lstm_layer = 2
        init_c = [('l%d_init_c'%l, (batch_size, num_hidden)) for l in range(num_lstm_layer)]
        init_h = [('l%d_init_h'%l, (batch_size, num_hidden)) for l in range(num_lstm_layer)]
        init_states = init_c + init_h

        all_shapes = [('data', (batch_size, 80 * 30))] + init_states + [('label', (batch_size, num_label))]
        all_shapes_dict = {}
        for _shape in all_shapes:
            all_shapes_dict[_shape[0]] = _shape[1]
        self.predictor = Predictor(open(self.path_of_json).read(),
                                    open(self.path_of_params).read(),
                                    all_shapes_dict)
def compare_module_cpredict(result_sym,
                            result_arg_params,
                            result_aux_params,
                            monitor_callback=False):
    # Dummmy inputs
    input1 = np.ones((1, 3, 224, 224))
    input1 = input1.astype(np.float32)
    nd_dict = {}

    def pred_mon_callback(name, arr):
        nd_dict[name] = arr

    mod = mx.mod.Module(result_sym,
                        data_names=["data"],
                        label_names=["softmax_label"],
                        context=mx.gpu())
    mod.bind(data_shapes=[['data', (1, 3, 224, 224)]],
             label_shapes=[['softmax_label', (1, )]],
             for_training=False)
    mod.set_params(result_arg_params, result_aux_params)
    mod.forward(
        mx.io.DataBatch(data=[mx.nd.array(input1, ctx=mx.gpu())],
                        label=[mx.nd.ones((1, ), ctx=mx.gpu())]))
    prefix = "test_predictor_amp"
    mod.save_checkpoint(prefix, 0, remove_amp_cast=False)
    sym_file = "{}-symbol.json".format(prefix)
    params_file = "{}-0000.params".format(prefix)
    predictor = Predictor(open(sym_file, "r").read(),
                          open(params_file, "rb").read(), {
                              'data': (1, 3, 224, 224),
                              'softmax_label': (1, )
                          },
                          dev_type="gpu",
                          dev_id=0)
    if monitor_callback:
        predictor.set_monitor_callback(pred_mon_callback, monitor_all=True)
    predictor.forward(data=input1, softmax_label=mx.nd.ones((1, )).asnumpy())
    predictor_out1 = predictor.get_output(0)
    if monitor_callback:
        assert len(nd_dict) > 0, "Callback not called"
    assert_almost_equal(mod.get_outputs()[0].asnumpy(),
                        predictor_out1,
                        atol=1e-1,
                        rtol=1e-1)
示例#8
0
    def __init_ocr(self):
        num_label = 4 # Set your max length of label, add one more for blank
        batch_size = 1

        num_hidden = 100
        num_lstm_layer = 2
        init_c = [('l%d_init_c'%l, (batch_size, num_hidden)) for l in range(num_lstm_layer)]
        init_h = [('l%d_init_h'%l, (batch_size, num_hidden)) for l in range(num_lstm_layer)]
        init_states = init_c + init_h

        init_state_arrays = np.zeros((batch_size, num_hidden), dtype="float32")
        self.init_state_dict = {}
        for x in init_states:
            self.init_state_dict[x[0]] = init_state_arrays

        all_shapes = [('data', (batch_size, 80, 30))] + init_states + [('label', (batch_size, num_label))]
        all_shapes_dict = {}
        for _shape in all_shapes:
            all_shapes_dict[_shape[0]] = _shape[1]
        self.predictor = Predictor(open(self.path_of_json, 'rb').read(),
                                   open(self.path_of_params, 'rb').read(),
                                   all_shapes_dict)
示例#9
0
class lstm_ocr_model(object):
    """LSTM network for predicting the Optical Character Recognition"""
    # Keep Zero index for blank. (CTC request it)
    CONST_CHAR = '0123456789'

    def __init__(self, path_of_json, path_of_params):
        super(lstm_ocr_model, self).__init__()
        self.path_of_json = path_of_json
        self.path_of_params = path_of_params
        self.predictor = None
        self.__init_ocr()

    def __init_ocr(self):
        num_label = 4  # Set your max length of label, add one more for blank
        batch_size = 1

        num_hidden = 100
        num_lstm_layer = 2
        init_c = [('l%d_init_c' % l, (batch_size, num_hidden))
                  for l in range(num_lstm_layer)]
        init_h = [('l%d_init_h' % l, (batch_size, num_hidden))
                  for l in range(num_lstm_layer)]
        init_states = init_c + init_h

        init_state_arrays = np.zeros((batch_size, num_hidden), dtype="float32")
        self.init_state_dict = {}
        for x in init_states:
            self.init_state_dict[x[0]] = init_state_arrays

        all_shapes = [('data', (batch_size, 80, 30))
                      ] + init_states + [('label', (batch_size, num_label))]
        all_shapes_dict = {}
        for _shape in all_shapes:
            all_shapes_dict[_shape[0]] = _shape[1]
        self.predictor = Predictor(
            open(self.path_of_json, 'rb').read(),
            open(self.path_of_params, 'rb').read(), all_shapes_dict)

    def forward_ocr(self, img_):
        """Forward the image through the LSTM network model

        Parameters
        ----------
        img_: int of array

        Returns
        ----------
        label_list: string of list
        """
        img_ = cv2.resize(img_, (80, 30))
        img_ = img_.transpose(1, 0)
        print(img_.shape)
        img_ = img_.reshape((1, 80, 30))
        print(img_.shape)
        # img_ = img_.reshape((80 * 30))
        img_ = np.multiply(img_, 1 / 255.0)
        self.predictor.forward(data=img_, **self.init_state_dict)
        prob = self.predictor.get_output(0)
        label_list = []
        for p in prob:
            print(np.argsort(p))
            max_index = np.argsort(p)[::-1][0]
            label_list.append(max_index)
        return self.__get_string(label_list)

    @staticmethod
    def __get_string(label_list):
        # Do CTC label rule
        # CTC cannot emit a repeated symbol on consecutive timesteps
        ret = []
        label_list2 = [0] + list(label_list)
        for i in range(len(label_list)):
            c1 = label_list2[i]
            c2 = label_list2[i + 1]
            if c2 in (0, c1):
                continue
            ret.append(c2)
        # change to ascii
        s = ''
        for l in ret:
            if l > 0 and l < (len(lstm_ocr_model.CONST_CHAR) + 1):
                c = lstm_ocr_model.CONST_CHAR[l - 1]
            else:
                c = ''
            s += c
        return s
示例#10
0
class lstm_ocr_model(object):
    # Keep Zero index for blank. (CTC request it)
    CONST_CHAR='0123456789'
    def __init__(self, path_of_json, path_of_params):
        super(lstm_ocr_model, self).__init__()
        self.path_of_json = path_of_json
        self.path_of_params = path_of_params
        self.predictor = None
        self.__init_ocr()

    def __init_ocr(self):
        num_label = 4 # Set your max length of label, add one more for blank
        batch_size = 1

        num_hidden = 100
        num_lstm_layer = 2
        init_c = [('l%d_init_c'%l, (batch_size, num_hidden)) for l in range(num_lstm_layer)]
        init_h = [('l%d_init_h'%l, (batch_size, num_hidden)) for l in range(num_lstm_layer)]
        init_states = init_c + init_h

        all_shapes = [('data', (batch_size, 80 * 30))] + init_states + [('label', (batch_size, num_label))]
        all_shapes_dict = {}
        for _shape in all_shapes:
            all_shapes_dict[_shape[0]] = _shape[1]
        self.predictor = Predictor(open(self.path_of_json).read(),
                                    open(self.path_of_params).read(),
                                    all_shapes_dict)

    def forward_ocr(self, img):
        img = cv2.resize(img, (80, 30))
        img = img.transpose(1, 0)
        img = img.reshape((80 * 30))
        img = np.multiply(img, 1/255.0)
        self.predictor.forward(data=img)
        prob = self.predictor.get_output(0)
        label_list = []
        for p in prob:
            max_index = np.argsort(p)[::-1][0]
            label_list.append(max_index)
        return self.__get_string(label_list)

    def __get_string(self, label_list):
        # Do CTC label rule
        # CTC cannot emit a repeated symbol on consecutive timesteps
        ret = []
        label_list2 = [0] + list(label_list)
        for i in range(len(label_list)):
            c1 = label_list2[i]
            c2 = label_list2[i+1]
            if c2 == 0 or c2 == c1:
                continue
            ret.append(c2)
        # change to ascii
        s = ''
        for l in ret:
            if l > 0 and l < (len(lstm_ocr_model.CONST_CHAR)+1):
                c = lstm_ocr_model.CONST_CHAR[l-1]
            else:
                c = ''
            s += c
        return s
import sys, os
curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
sys.path.append("../../../amalgamation/python/")

from mxnet_predict import Predictor, load_ndarray_file
import logging
import numpy as np
from skimage import io, transform

# Load the pre-trained model
prefix = "resnet/resnet-18"
num_round = 0
symbol_file = "%s-symbol.json" % prefix
param_file = "%s-0000.params" % prefix
predictor = Predictor(open(symbol_file, "r").read(),
                      open(param_file, "rb").read(),
                      {'data':(1, 3, 224, 224)})

synset = [l.strip() for l in open('resnet/synset.txt').readlines()]

def PreprocessImage(path, show_img=False):
    # load image
    img = io.imread(path)
    print("Original Image Shape: ", img.shape)
    # we crop image from center
    short_egde = min(img.shape[:2])
    yy = int((img.shape[0] - short_egde) / 2)
    xx = int((img.shape[1] - short_egde) / 2)
    crop_img = img[yy : yy + short_egde, xx : xx + short_egde]
    # resize to 224, 224
    resized_img = transform.resize(crop_img, (224, 224))
from mxnet_predict import Predictor, load_ndarray_file
import mxnet as mx
import logging
import numpy as np
from skimage import io, transform

# Load the pre-trained model
prefix = "Inception/Inception_BN"
num_round = 39
symbol_file = "%s-symbol.json" % prefix
param_file = "%s-0039.params" % prefix

# gpu predictor
predictor_gpu = Predictor(open(symbol_file).read(),
                      open(param_file).read(),
                      {'data':(1, 3, 224, 224)},
                      dev_type='gpu')

# cpu predictor
predictor_cpu = Predictor(open(symbol_file).read(),
                      open(param_file).read(),
                      {'data':(1, 3, 224, 224)},
                      dev_type='cpu')

mean_img = load_ndarray_file(open("Inception/mean_224.nd").read())["mean_img"]

synset = [l.strip() for l in open('Inception/synset.txt').readlines()]

def PreprocessImage(path, show_img=False):
    # load image
    img = io.imread(path)
示例#13
0
sys.path.append("../../../amalgamation/python/")
sys.path.append("../../../python/")

from mxnet_predict import Predictor, load_ndarray_file
import mxnet as mx
import logging
import numpy as np
from skimage import io, transform

# Load the pre-trained model
prefix = "Inception/Inception_BN"
num_round = 39
symbol_file = "%s-symbol.json" % prefix
param_file = "%s-0039.params" % prefix
predictor = Predictor(
    open(symbol_file).read(),
    open(param_file).read(), {'data': (1, 3, 224, 224)})
mean_img = load_ndarray_file(open("Inception/mean_224.nd").read())["mean_img"]

synset = [l.strip() for l in open('Inception/synset.txt').readlines()]


def PreprocessImage(path, show_img=False):
    # load image
    img = io.imread(path)
    print("Original Image Shape: ", img.shape)
    # we crop image from center
    short_egde = min(img.shape[:2])
    yy = int((img.shape[0] - short_egde) / 2)
    xx = int((img.shape[1] - short_egde) / 2)
    crop_img = img[yy:yy + short_egde, xx:xx + short_egde]
示例#14
0
class lstm_ocr_model(object):
    # Keep Zero index for blank. (CTC request it)
    CONST_CHAR = '0123456789'

    def __init__(self, path_of_json, path_of_params):
        super(lstm_ocr_model, self).__init__()
        self.path_of_json = path_of_json
        self.path_of_params = path_of_params
        self.predictor = None
        self.__init_ocr()

    def __init_ocr(self):
        num_label = 4  # Set your max length of label, add one more for blank
        batch_size = 1

        num_hidden = 100
        num_lstm_layer = 2
        init_c = [('l%d_init_c' % l, (batch_size, num_hidden)) for l in range(num_lstm_layer)]
        init_h = [('l%d_init_h' % l, (batch_size, num_hidden)) for l in range(num_lstm_layer)]
        init_states = init_c + init_h

        init_state_arrays = np.zeros((batch_size, num_hidden), dtype="float32")
        self.init_state_dict = {}
        for x in init_states:
            self.init_state_dict[x[0]] = init_state_arrays

        all_shapes = [('data', (batch_size, 80 * 30))] + init_states + [('label', (batch_size, num_label))]
        all_shapes_dict = {}
        for _shape in all_shapes:
            all_shapes_dict[_shape[0]] = _shape[1]
        self.predictor = Predictor(open(self.path_of_json).read(),
                                   open(self.path_of_params).read(),
                                   all_shapes_dict)

    def forward_ocr(self, img):
        img = cv2.resize(img, (80, 30))
        img = img.transpose(1, 0)
        img = img.reshape((80 * 30))
        img = np.multiply(img, 1 / 255.0)
        self.predictor.forward(data=img, **self.init_state_dict)
        prob = self.predictor.get_output(0)
        label_list = []
        for p in prob:
            max_index = np.argsort(p)[::-1][0]
            label_list.append(max_index)
        return self.__get_string(label_list)

    def __get_string(self, label_list):
        # Do CTC label rule
        # CTC cannot emit a repeated symbol on consecutive timesteps
        ret = []
        label_list2 = [0] + list(label_list)
        for i in range(len(label_list)):
            c1 = label_list2[i]
            c2 = label_list2[i + 1]
            if c2 == 0 or c2 == c1:
                continue
            ret.append(c2)
        # change to ascii
        s = ''
        for l in ret:
            if l > 0 and l < (len(lstm_ocr_model.CONST_CHAR) + 1):
                c = lstm_ocr_model.CONST_CHAR[l - 1]
            else:
                c = ''
            s += c
        return s
示例#15
0
curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
sys.path.append("../../predict/python/")
sys.path.append("../../python/")

from mxnet_predict import Predictor, load_ndarray_file
import mxnet as mx
import logging
import numpy as np
from skimage import io, transform

# Load the pre-trained model
prefix = "Inception/Inception_BN"
num_round = 39
symbol_file = "%s-symbol.json" % prefix
param_file = "%s-0039.params" % prefix
predictor = Predictor(symbol_file, param_file, {'data':(1, 3, 224, 224)})
mean_img = load_ndarray_file("Inception/mean_224.nd")["mean_img"]

synset = [l.strip() for l in open('Inception/synset.txt').readlines()]

def PreprocessImage(path, show_img=False):
    # load image
    img = io.imread(path)
    print("Original Image Shape: ", img.shape)
    # we crop image from center
    short_egde = min(img.shape[:2])
    yy = int((img.shape[0] - short_egde) / 2)
    xx = int((img.shape[1] - short_egde) / 2)
    crop_img = img[yy : yy + short_egde, xx : xx + short_egde]
    # resize to 224, 224
    resized_img = transform.resize(crop_img, (224, 224))
示例#16
0
from mxnet_predict import Predictor, load_ndarray_file
import mxnet as mx
import logging
import numpy as np
from skimage import io, transform
# Load the pre-trained model
prefix = "./face-0"
num_round = 125
batch_size = 20
if len(sys.argv) > 2:
    num_round = int(sys.argv[2])
symbol_file = "%s-symbol.json" % prefix
param_file = "%s-%s.params" % (prefix, str(num_round).zfill(4))
predictor = Predictor(
    open(symbol_file).read(),
    open(param_file).read(), {'data': (batch_size, 3, 224, 224)}, 'gpu', 0)
mean_img = load_ndarray_file(open("./mean.bin").read())["mean_img"]

synset = [l.strip() for l in open('./labels.txt').readlines()]


def PreprocessImage(batchs, index, path, show_img=False):
    # load image
    img = io.imread(path)
    #print("Original Image Shape: ", img.shape)
    # we crop image from center
    short_egde = min(img.shape[:2])
    yy = int((img.shape[0] - short_egde) / 2)
    xx = int((img.shape[1] - short_egde) / 2)
    crop_img = img[yy:yy + short_egde, xx:xx + short_egde]
示例#17
0
class lstm_ocr_model(object):
    """LSTM network for predicting the Optical Character Recognition"""
    # Keep Zero index for blank. (CTC request it)
    CONST_CHAR = '0123456789'

    def __init__(self, path_of_json, path_of_params):
        super(lstm_ocr_model, self).__init__()
        self.path_of_json = path_of_json
        self.path_of_params = path_of_params
        self.predictor = None
        self.__init_ocr()

    def __init_ocr(self):
        num_label = 4 # Set your max length of label, add one more for blank
        batch_size = 1

        num_hidden = 100
        num_lstm_layer = 2
        init_c = [('l%d_init_c'%l, (batch_size, num_hidden)) for l in range(num_lstm_layer)]
        init_h = [('l%d_init_h'%l, (batch_size, num_hidden)) for l in range(num_lstm_layer)]
        init_states = init_c + init_h

        init_state_arrays = np.zeros((batch_size, num_hidden), dtype="float32")
        self.init_state_dict = {}
        for x in init_states:
            self.init_state_dict[x[0]] = init_state_arrays

        all_shapes = [('data', (batch_size, 80, 30))] + init_states + [('label', (batch_size, num_label))]
        all_shapes_dict = {}
        for _shape in all_shapes:
            all_shapes_dict[_shape[0]] = _shape[1]
        self.predictor = Predictor(open(self.path_of_json, 'rb').read(),
                                   open(self.path_of_params, 'rb').read(),
                                   all_shapes_dict)

    def forward_ocr(self, img_):
        """Forward the image through the LSTM network model

        Parameters
        ----------
        img_: int of array

        Returns
        ----------
        label_list: string of list
        """
        img_ = cv2.resize(img_, (80, 30))
        img_ = img_.transpose(1, 0)
        print(img_.shape)
        img_ = img_.reshape((1, 80, 30))
        print(img_.shape)
        # img_ = img_.reshape((80 * 30))
        img_ = np.multiply(img_, 1 / 255.0)
        self.predictor.forward(data=img_, **self.init_state_dict)
        prob = self.predictor.get_output(0)
        label_list = []
        for p in prob:
            print(np.argsort(p))
            max_index = np.argsort(p)[::-1][0]
            label_list.append(max_index)
        return self.__get_string(label_list)

    @staticmethod
    def __get_string(label_list):
        # Do CTC label rule
        # CTC cannot emit a repeated symbol on consecutive timesteps
        ret = []
        label_list2 = [0] + list(label_list)
        for i in range(len(label_list)):
            c1 = label_list2[i]
            c2 = label_list2[i+1]
            if c2 in (0, c1):
                continue
            ret.append(c2)
        # change to ascii
        s = ''
        for l in ret:
            if l > 0 and l < (len(lstm_ocr_model.CONST_CHAR)+1):
                c = lstm_ocr_model.CONST_CHAR[l-1]
            else:
                c = ''
            s += c
        return s
示例#18
0
class lstm_ocr_model(object):
    # Keep Zero index for blank. (CTC request it)
    def __init__(self, path_of_json, path_of_params, classes, data_shape,
                 batch_size, num_label, num_hidden, num_lstm_layer):
        super(lstm_ocr_model, self).__init__()
        self.path_of_json = path_of_json
        self.path_of_params = path_of_params
        self.classes = classes
        self.batch_size = batch_size
        self.data_shape = data_shape
        self.num_label = num_label
        self.num_hidden = num_hidden
        self.num_lstm_layer = num_lstm_layer
        self.predictor = None
        self.__init_ocr()

    def __init_ocr(self):
        init_c = [('l%d_init_c' % l, (self.batch_size, self.num_hidden))
                  for l in range(self.num_lstm_layer)]
        init_h = [('l%d_init_h' % l, (self.batch_size, self.num_hidden))
                  for l in range(self.num_lstm_layer)]
        init_states = init_c + init_h

        all_shapes = [
            ('data',
             (self.batch_size, self.data_shape[0] * self.data_shape[1]))
        ] + init_states + [('label', (self.batch_size, self.num_label))]
        all_shapes_dict = {}
        for _shape in all_shapes:
            all_shapes_dict[_shape[0]] = _shape[1]
        self.predictor = Predictor(
            open(self.path_of_json).read(),
            open(self.path_of_params).read(), all_shapes_dict)

    def forward_ocr(self, img):
        img = cv2.resize(img, self.data_shape)
        img = img.transpose(1, 0)
        img = img.reshape((self.data_shape[0] * self.data_shape[1]))
        img = np.multiply(img, 1 / 255.0)
        self.predictor.forward(data=img)
        prob = self.predictor.get_output(0)
        label_list = []
        for p in prob:
            max_index = np.argsort(p)[::-1][0]
            label_list.append(max_index)
        return self.__get_string(label_list)

    def __get_string(self, label_list):
        # Do CTC label rule
        # CTC cannot emit a repeated symbol on consecutive timesteps
        ret = []
        label_list2 = [0] + list(label_list)
        for i in range(len(label_list)):
            c1 = label_list2[i]
            c2 = label_list2[i + 1]
            if c2 == 0 or c2 == c1:
                continue
            ret.append(c2)
        # change to ascii
        s = ''
        for l in ret:
            if l > 0 and l < (len(self.classes) + 1):
                c = self.classes[l - 1]
            else:
                c = ''
            s += c
        return s
from mxnet_predict import Predictor, load_ndarray_file
import mxnet as mx
import logging
import numpy as np
from skimage import io, transform

# Load the pre-trained model
prefix = "Inception/Inception_BN"
num_round = 39
symbol_file = "%s-symbol.json" % prefix
param_file = "%s-0039.params" % prefix

# gpu predictor
predictor_gpu = Predictor(open(symbol_file).read(),
                          open(param_file).read(), {'data': (1, 3, 224, 224)},
                          dev_type='gpu')

# cpu predictor
predictor_cpu = Predictor(open(symbol_file).read(),
                          open(param_file).read(), {'data': (1, 3, 224, 224)},
                          dev_type='cpu')

mean_img = load_ndarray_file(open("Inception/mean_224.nd").read())["mean_img"]

synset = [l.strip() for l in open('Inception/synset.txt').readlines()]


def PreprocessImage(path, show_img=False):
    # load image
    img = io.imread(path)