def train_cnocr(args): head = '%(asctime)-15s %(message)s' logging.basicConfig(level=logging.DEBUG, format=head) args.model_name = args.emb_model_type + '-' + args.seq_model_type out_dir = os.path.join(args.out_model_dir, args.model_name) logger.info('save models to dir: %s' % out_dir) if not os.path.exists(out_dir): os.makedirs(out_dir) args.prefix = os.path.join( out_dir, 'cnocr-v{}-{}'.format(__version__, args.model_name)) hp = CnHyperparams() hp = _update_hp(hp, args) network, hp = gen_network(args.model_name, hp) metrics = CtcMetrics(hp.seq_length) data_train, data_val = _gen_iters(hp, args.train_file, args.test_file, args.use_train_image_aug, args.dataset, args.charset, args.debug) data_names = ['data'] fit( network=network, data_train=data_train, data_val=data_val, metrics=metrics, args=args, hp=hp, data_names=data_names, )
def run_captcha(args): hp = Hyperparams2() network = crnn_lstm(hp) # arg_shape, out_shape, aux_shape = network.infer_shape(data=(128, 1, 32, 100), label=(128, 10), # l0_init_h=(128, 100), l1_init_h=(128, 100), l2_init_h=(128, 100), l3_init_h=(128, 100)) # print(dict(zip(network.list_arguments(), arg_shape))) # import pdb; pdb.set_trace() # Start a multiprocessor captcha image generator mp_captcha = MPDigitCaptcha(font_paths=get_fonts(args.font_path), h=hp.img_width, w=hp.img_height, num_digit_min=3, num_digit_max=4, num_processes=args.num_proc, max_queue_size=hp.batch_size * 2) mp_captcha.start() # img, num = mp_captcha.get() # print(img.shape) # import numpy as np # import cv2 # img = np.transpose(img, (1, 0)) # cv2.imwrite('captcha1.png', img * 255) # import pdb; pdb.set_trace() init_c = [('l%d_init_c' % l, (hp.batch_size, hp.num_hidden)) for l in range(hp.num_lstm_layer * 2)] init_h = [('l%d_init_h' % l, (hp.batch_size, hp.num_hidden)) for l in range(hp.num_lstm_layer * 2)] init_states = init_c + init_h data_names = ['data'] + [x[0] for x in init_states] data_train = OCRIter(hp.train_epoch_size // hp.batch_size, hp.batch_size, init_states, captcha=mp_captcha, num_label=hp.num_label, name='train') data_val = OCRIter(hp.eval_epoch_size // hp.batch_size, hp.batch_size, init_states, captcha=mp_captcha, num_label=hp.num_label, name='val') head = '%(asctime)-15s %(message)s' logging.basicConfig(level=logging.DEBUG, format=head) metrics = CtcMetrics(hp.seq_length) fit(network=network, data_train=data_train, data_val=data_val, metrics=metrics, args=args, hp=hp, data_names=data_names) mp_captcha.reset()
def run_cn_ocr(args): hp = Hyperparams() network = crnn_lstm(hp) mp_data_train = MPOcrImages(args.data_root, args.train_file, (hp.img_width, hp.img_height), hp.num_label, num_processes=args.num_proc, max_queue_size=hp.batch_size * 100) # img, num = mp_data_train.get() # print(img.shape) # print(mp_data_train.shape) # import pdb; pdb.set_trace() # import numpy as np # import cv2 # img = np.transpose(img, (1, 0)) # cv2.imwrite('captcha1.png', img * 255) # import pdb; pdb.set_trace() mp_data_test = MPOcrImages(args.data_root, args.test_file, (hp.img_width, hp.img_height), hp.num_label, num_processes=max(args.num_proc // 2, 1), max_queue_size=hp.batch_size * 10) mp_data_train.start() mp_data_test.start() # init_c = [('l%d_init_c' % l, (hp.batch_size, hp.num_hidden)) for l in range(hp.num_lstm_layer * 2)] # init_h = [('l%d_init_h' % l, (hp.batch_size, hp.num_hidden)) for l in range(hp.num_lstm_layer * 2)] # init_states = init_c + init_h # data_names = ['data'] + [x[0] for x in init_states] data_names = ['data'] data_train = OCRIter( hp.train_epoch_size // hp.batch_size, hp.batch_size, captcha=mp_data_train, num_label=hp.num_label, name='train') data_val = OCRIter( hp.eval_epoch_size // hp.batch_size, hp.batch_size, captcha=mp_data_test, num_label=hp.num_label, name='val') # data_train = ImageIterLstm( # args.data_root, args.train_file, hp.batch_size, (hp.img_width, hp.img_height), hp.num_label, init_states, name="train") # data_val = ImageIterLstm( # args.data_root, args.test_file, hp.batch_size, (hp.img_width, hp.img_height), hp.num_label, init_states, name="val") head = '%(asctime)-15s %(message)s' logging.basicConfig(level=logging.DEBUG, format=head) metrics = CtcMetrics(hp.seq_length) fit(network=network, data_train=data_train, data_val=data_val, metrics=metrics, args=args, hp=hp, data_names=data_names) mp_data_train.reset() mp_data_test.reset()