conf, preds = preds.max(2) preds = preds.transpose(1, 0).contiguous().view(-1) preds_size = Variable(torch.IntTensor([preds.size(0)])) # raw_pred = converter.decode(preds.data, preds_size.data, raw=True) sim_pred = converter.decode(preds.data, preds_size.data, raw=False) return sim_pred if __name__ == '__main__': model_path = './weights/CRNN-0627-crop_48_901.pth' gpu = True if not torch.cuda.is_available(): gpu = False model = crnn.CRNN(config.imgH, 1, len(alphabet) + 1, 256) if gpu: model = model.cuda() print('loading pretrained model from %s' % model_path) if gpu: model.load_state_dict(torch.load(model_path)) else: model.load_state_dict( torch.load(model_path, map_location=lambda storage, loc: storage)) if len(sys.argv) > 1 and 'train' in sys.argv[1]: infofile = 'data_set/infofile_updated_0627_train.txt' print(val_model(infofile, model, gpu, '0627_train.log')) elif len(sys.argv) > 1 and 'gen' in sys.argv[1]: infofile = 'data_set/infofile_0627_gen_test.txt'
criterion = CTCLoss(reduction='sum') # criterion = CTCLoss() best_acc = 0.9 # custom weights initialization called on crnn def weights_init(m): classname = m.__class__.__name__ if classname.find('Conv') != -1: m.weight.data.normal_(0.0, 0.02) elif classname.find('BatchNorm') != -1: m.weight.data.normal_(1.0, 0.02) m.bias.data.fill_(0) crnn = crnn.CRNN(config.imgH, config.nc, config.nclass, config.nh) if config.pretrained_model != '' and os.path.exists(config.pretrained_model): print('loading pretrained model from %s' % config.pretrained_model) crnn.load_state_dict(torch.load(config.pretrained_model)) else: crnn.apply(weights_init) print(crnn) # image = torch.FloatTensor(config.batchSize, 3, config.imgH, config.imgH) # text = torch.IntTensor(config.batchSize * 5) # length = torch.IntTensor(config.batchSize) device = torch.device('cpu') if config.cuda: crnn.cuda() # crnn = torch.nn.DataParallel(crnn, device_ids=range(opt.ngpu))
import core.utils import core.keys import core.dataset import core.crnn from PIL import Image from torch.autograd import Variable gpu = True if not torch.cuda.is_available(): gpu = False model_path = 'CRNN-0618-10w_21_990.pth' alphabet = keys.alphabet imgH = config.imgH imgW = config.imgW model = crnn.CRNN(imgH, 1, len(alphabet) + 1, 256) print('loading pretrained model from %s, alphabet.len=%d' % (model_path, len(alphabet))) if gpu: model = model.cuda() model.load_state_dict( torch.load( model_path ) ) else: model.load_state_dict(torch.load(model_path,map_location=lambda storage,loc:storage)) converter = utils.strLabelConverter(alphabet) transformer = mydataset.resizeNormalize((imgW, imgH),is_test=True) def recognize_downline(img,crnn_model=model): img = cv2.cvtColor( img, cv2.COLOR_BGR2RGB ) image = Image.fromarray(np.uint8(img)).convert('L')