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)
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)
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
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
crop_img = img[yy:yy + short_egde, xx:xx + short_egde] # resize to 224, 224 resized_img = transform.resize(crop_img, (224, 224)) if show_img: io.imshow(resized_img) # convert to numpy.ndarray sample = np.asarray(resized_img) * 255 # swap axes to make image from (224, 224, 3) to (3, 224, 224) sample = np.swapaxes(sample, 0, 2) sample = np.swapaxes(sample, 1, 2) # sub mean normed_img = sample - mean_img normed_img.resize(1, 3, 224, 224) return normed_img # Get preprocessed batch (single image batch) batch = PreprocessImage('./download.png', True) predictor.forward(data=batch) prob = predictor.get_output(0)[0] pred = np.argsort(prob)[::-1] # Get top1 label top1 = synset[pred[0]] print("Top1: ", top1) # Get top5 label top5 = [synset[pred[i]] for i in range(5)] print("Top5: ", top5)
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
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
# 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)) # convert to numpy.ndarray sample = np.asarray(resized_img) * 255 # swap axes to make image from (224, 224, 3) to (3, 224, 224) sample = np.swapaxes(sample, 0, 2) sample = np.swapaxes(sample, 1, 2) # sub mean return sample # Get preprocessed batch (single image batch) batch = PreprocessImage('./download.jpg', True) predictor.forward(data=batch) prob = predictor.get_output(0)[0] pred = np.argsort(prob)[::-1] # Get top1 label top1 = synset[pred[0]] print("Top1: ", top1) # Get top5 label top5 = [synset[pred[i]] for i in range(5)] print("Top5: ", top5)
sample = np.asarray(resized_img) * 255 # swap axes to make image from (224, 224, 3) to (3, 224, 224) sample = np.swapaxes(sample, 0, 2) sample = np.swapaxes(sample, 1, 2) # sub mean normed_img = sample - mean_img normed_img.resize(1, 3, 224, 224) return normed_img # Get preprocessed batch (single image batch) img_path = './test/1.jpg' print (img_path) batch = PreprocessImage(img_path, True) # gpu predictor_gpu.forward(data=batch) prob = predictor_gpu.get_output(0)[0] pred = np.argsort(prob)[::-1] # Get top1 label top1 = synset[pred[0]] print("Top1: ", top1, "Index: ", pred[0], "Prob: ", prob[pred[0]]) # Get top5 label #top5 = [synset[pred[i]] for i in range(5)] #print("Top5: ", top5) # cpu predictor_cpu.forward(data=batch) prob = predictor_cpu.get_output(0)[0] pred = np.argsort(prob)[::-1] # Get top1 label top1 = synset[pred[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
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
# swap axes to make image from (224, 224, 3) to (3, 224, 224) sample = np.swapaxes(sample, 0, 2) sample = np.swapaxes(sample, 1, 2) # sub mean normed_img = sample - mean_img normed_img.resize(1, 3, 224, 224) return normed_img # Get preprocessed batch (single image batch) img_path = './test/1.jpg' print(img_path) batch = PreprocessImage(img_path, True) # gpu predictor_gpu.forward(data=batch) prob = predictor_gpu.get_output(0)[0] pred = np.argsort(prob)[::-1] # Get top1 label top1 = synset[pred[0]] print("Top1: ", top1, "Index: ", pred[0], "Prob: ", prob[pred[0]]) # Get top5 label #top5 = [synset[pred[i]] for i in range(5)] #print("Top5: ", top5) # cpu predictor_cpu.forward(data=batch) prob = predictor_cpu.get_output(0)[0] pred = np.argsort(prob)[::-1] # Get top1 label top1 = synset[pred[0]]