return labels, np.array(pred_labels) if __name__ == '__main__': parser = argparse.ArgumentParser(description='LPR Demo') parser.add_argument("-image", help='image path', default='data/ccpd_weather/吉BTW976.jpg', type=str) args = parser.parse_args() device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") lprnet = LPRNet(class_num=len(CHARS), dropout_rate=0) lprnet.to(device) lprnet.load_state_dict(torch.load('weights/Final_LPRNet_model.pth', map_location=lambda storage, loc: storage)) lprnet.eval() STN = STNet() STN.to(device) STN.load_state_dict(torch.load('weights/Final_STN_model.pth', map_location=lambda storage, loc: storage)) STN.eval() print("Successful to build network!") since = time.time() image = cv2.imread(args.image) im = cv2.resize(image, (94, 24), interpolation=cv2.INTER_CUBIC) im = (np.transpose(np.float32(im), (2, 0, 1)) - 127.5)*0.0078125 data = torch.from_numpy(im).float().unsqueeze(0).to(device) # torch.Size([1, 3, 24, 94]) transfer = STN(data) preds = lprnet(transfer) preds = preds.cpu().detach().numpy() # (1, 68, 18)
help='image path', default= '/media/cbpm2016/D/liaolong/alpr/License_Plate_Detection_Pytorch/data/plate_test/152L393.png', type=str) args = parser.parse_args() device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") lprnet = LPRNet(class_num=len(CHARS), dropout_rate=0) lprnet.to(device) lprnet.load_state_dict( torch.load( '/media/cbpm2016/D/liaolong/alpr/License_Plate_Detection_Pytorch/LPRNet/saving_ckpt/lprnet_epoch_001200_model.ckpt' )['net_state_dict']) lprnet.eval() STN = STNet() STN.to(device) STN.load_state_dict( torch.load( '/media/cbpm2016/D/liaolong/alpr/License_Plate_Detection_Pytorch/LPRNet/saving_ckpt/stn_epoch_001200_model.ckpt' )['net_state_dict']) STN.eval() print("Successful to build network!") since = time.time() image = cv2.imread(args.image) im = cv2.resize(image, (94, 24), interpolation=cv2.INTER_CUBIC) im = (np.transpose(np.float32(im), (2, 0, 1)) - 127.5) * 0.0078125 data = torch.from_numpy(im).float().unsqueeze(0).to( device) # torch.Size([1, 3, 24, 94])
if __name__ == '__main__': parser = argparse.ArgumentParser(description='LPR Demo') parser.add_argument("-image", help='image path', default='data/ccpd_weather/吉BTW976.jpg', type=str) parser.add_argument("-lprnet_model", help='lprnet model', default='weights/Final_LPRNet_model.pth', type=str) parser.add_argument("-stn_model", help='stn model', default='weights/Final_STN_model.pth', type=str) args = parser.parse_args() device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") lprnet = LPRNet(class_num=len(CHARS), dropout_rate=0) lprnet.to(device) lprnet.load_state_dict(torch.load(args.lprnet_model, map_location=lambda storage, loc: storage)) lprnet.eval() STN = STNet() STN.to(device) STN.load_state_dict(torch.load(args.stn_model, map_location=lambda storage, loc: storage)) STN.eval() print("Successful to build network!") since = time.time() image = cv2.imread(args.image) im = cv2.resize(image, (94, 24), interpolation=cv2.INTER_CUBIC) im = (np.transpose(np.float32(im), (2, 0, 1)) - 127.5)*0.0078125 data = torch.from_numpy(im).float().unsqueeze(0).to(device) # torch.Size([1, 3, 24, 94]) transfer = STN(data) preds = lprnet(transfer) preds = preds.cpu().detach().numpy() # (1, 68, 18)
def weights_init(m): for key in m.state_dict(): if key.split('.')[-1] == 'weight': if 'conv' in key: nn.init.kaiming_normal_(m.state_dict()[key], mode='fan_out') if 'bn' in key: m.state_dict()[key][...] = xavier(1) elif key.split('.')[-1] == 'bias': m.state_dict()[key][...] = 0.01 lprnet.backbone.apply(weights_init) lprnet.container.apply(weights_init) print("initial net weights successful!") STN = STNet() STN.to(device) STN.load_state_dict( torch.load('weights/STN_model_Init.pth', map_location=lambda storage, loc: storage)) print("STN loaded") dataset = { 'train': LPRDataLoader([args.img_dirs_train], args.img_size), 'val': LPRDataLoader([args.img_dirs_val], args.img_size) } dataloader = { 'train': DataLoader(dataset['train'], batch_size=args.batch_size, shuffle=False,