def crnnSource(net_path, alphabet): converter = util.strLabelConverter(alphabet) if cfg.PLATFORM == "GPU": model = crnn.CRNN(32, 1, len(alphabet) + 1, 256, 1).cuda() #model = torch.nn.DataParallel(model, device_ids=range(1)) model.load_state_dict(torch.load(net_path)) else: model = crnn.CRNN(32, 1, len(alphabet) + 1, 256, 1) model.load_state_dict(torch.load(net_path, map_location=lambda storage, loc: storage)) return model,converter
def crnnSource(net_path, alphabet): converter = util.strLabelConverter(alphabet) if config.PLATFORM == "GPU": model = crnn.CRNN(32, 1, len(alphabet) + 1, 256, 1).cuda() model.load_state_dict(torch.load(net_path)) else: model = crnn.CRNN(32, 1, len(alphabet) + 1, 256, 1) model.load_state_dict( torch.load(net_path, map_location=lambda storage, loc: storage)) #model.load_state_dict(torch.load(net_path)) return model, converter
def __init__(self, model_path=CONFIG.model_path, alphabet=CONFIG.alphabet): self.model = crnn.CRNN(n_class=len(alphabet) + 1) self.model.load_state_dict(torch.load(model_path)['state_dict']) self.converter = util.strLabelConverter(alphabet) self.toTensor = transforms.ToTensor() self.model = self.model.cuda() self.model.eval()
# test if crnn work if __name__ == '__main__': imagepath = './test.jpg' img_h = opt.img_h use_gpu = opt.use_gpu modelpath = opt.modelpath char_set = open('char_std_5990.txt', 'r', encoding='utf-8').readlines() char_set = ''.join([ch.strip('\n') for ch in char_set[1:]] + ['卍']) n_class = len(char_set) model = crnn.CRNN(img_h, 1, n_class, 256) if torch.cuda.is_available and use_gpu: model.cuda() if os.path.exists(modelpath): print('Load model from "%s" ...' % modelpath) model.load_state_dict(torch.load(modelpath)) print('Done!') image = Image.open(imagepath).convert('L') (w, h) = image.size size_h = 32 ratio = size_h / float(h) size_w = int(w * ratio) # keep the ratio transform = resizeNormalize((size_w, size_h))