def val(net, dataset, criterion, max_iter=100): print('Start val') for p in crnn.parameters(): p.requires_grad = False net.eval() data_loader = torch.utils.data.DataLoader( dataset, shuffle=True, batch_size=opt.batchSize, num_workers=int(opt.workers)) val_iter = iter(data_loader) i = 0 n_correct = 0 loss_avg = utils.averager() for i in range(min(max_iter, len(data_loader))): data = val_iter.next() i += 1 cpu_images, cpu_texts = data batch_size = cpu_images.size(0) utils.loadData(image, cpu_images) t, l = converter.encode(cpu_texts) utils.loadData(text, t) utils.loadData(length, l) preds = crnn(image) preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size)) cost = criterion(preds, text, preds_size, length) / batch_size loss_avg.add(cost) _, preds = preds.max(2) preds = preds.squeeze(2) preds = preds.transpose(1, 0).contiguous().view(-1) sim_preds = converter.decode(preds.data, preds_size.data, raw=False) for pred, target in zip(sim_preds, cpu_texts): if pred == target.lower(): n_correct += 1 raw_preds = converter.decode(preds.data, preds_size.data, raw=True) for raw_pred, pred, gt in zip(raw_preds, sim_preds, cpu_texts): print('%-20s => %-20s, gt: %-20s' % (raw_pred, pred, gt)) accuracy = n_correct / float(max_iter * opt.batchSize) print('Test loss: %f, accuray: %f' % (loss_avg.val(), accuracy))
def val(net, dataset, criterion, max_iter=100): print('Start val') for p in crnn.parameters(): p.requires_grad = False net.eval() data_loader = torch.utils.data.DataLoader(dataset, shuffle=True, batch_size=opt.batchSize, num_workers=int(opt.workers)) val_iter = iter(data_loader) i = 0 n_correct = 0 loss_avg = utils.averager() max_iter = min(max_iter, len(data_loader)) for i in range(max_iter): try: data = val_iter.next() except: continue i += 1 try: cpu_images, cpu_texts = data except ValueError: cpu_images, cpu_texts, _ = data batch_size = cpu_images.size(0) utils.loadData(image, cpu_images) t, l = converter.encode(cpu_texts) global text utils.loadData(text, t) utils.loadData(length, l) text = text.view((batch_size, -1)).cuda() preds = crnn(image) preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size)) cost = criterion(F.log_softmax(preds, dim=-1), text, preds_size, length).sum() / batch_size loss_avg.add(cost) _, preds = preds.max(2) #preds = preds.squeeze(2) preds = preds.transpose(1, 0).contiguous().view(-1) sim_preds = converter.decode(preds.data, preds_size.data, raw=False) for pred, target in zip(sim_preds, cpu_texts): if pred == target: n_correct += 1 elif val_wrong is not None: val_wrong.append(target + '_' + pred) writer.add_scalars( 'Test', { 'loss': cost.item(), 'acc': (np.array(sim_preds) == np.array(cpu_texts)).mean() }, global_step=global_step * max_iter + i) raw_preds = converter.decode(preds.data, preds_size.data, raw=True)[:opt.n_test_disp] for raw_pred, pred, gt in zip(raw_preds, sim_preds, cpu_texts): print('%-20s => %-20s, gt: %-20s' % (raw_pred, pred, gt)) accuracy = n_correct / float(max_iter * opt.batchSize) print('Test loss: %f, accuray: %f' % (loss_avg.val(), accuracy))
crnn = crnn.CRNN(params.imgH, nc, nclass, params.nh) if args.cuda: crnn.cuda() image = image.cuda() criterion = criterion.cuda() crnn.apply(weights_init) if params.crnn != '': print('loading pretrained model from %s' % params.crnn) crnn.load_state_dict(torch.load(params.crnn)) image = Variable(image) text = Variable(text) length = Variable(length) # loss averager loss_avg = utils.averager() # setup optimizer if params.adam: optimizer = optim.Adam(crnn.parameters(), lr=params.lr, betas=(params.beta1, 0.999)) elif params.adadelta: optimizer = optim.Adadelta(crnn.parameters(), lr=params.lr) else: optimizer = optim.RMSprop(crnn.parameters(), lr=params.lr) training(crnn,train_loader,criterion,optimizer) np.save('loss.npy', training_Loss) np.save('Accuracy.npy', testing_Rate)
def val(crnn, val_data_list_param, criterion, max_iter_num=100): # print('开始校验准确性') for p in crnn.parameters(): p.requires_grad = False crnn.eval() i = 0 all_Count = 0 correct_Count = 0 while i < len(val_data_list_param): val_data = val_data_list_param[i] # datasetOne = datasetList[i] # data_loader = torch.utils.data.DataLoader( # datasetOne, shuffle=True, batch_size=opt.batchSize, num_workers=int(opt.workers)) data_set = val_data['dataset'] data_loader = torch.utils.data.DataLoader(data_set, shuffle=True, batch_size=opt.batchSize, num_workers=1) i += 1 # print("验证进度:{}/{},当前Flag:{}".format(i, len(val_data_list_param), val_data['dir'])) val_iter = iter(data_loader) one_index = 0 one_correct = 0 loss_avg = utils.averager() # 检测所用的图片数量 max_iter = min(max_iter_num, len(data_loader)) # 检测的总数量增加 all_Count += max_iter * opt.batchSize for one_index in range(max_iter): data = val_iter.next() one_index += 1 cpu_images, cpu_texts = data batch_size = cpu_images.size(0) utils.loadData(image, cpu_images) t, l = converter.encode(cpu_texts) utils.loadData(text, t) utils.loadData(length, l) preds = crnn(image) preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size)) # cost = criterion(preds, text, preds_size, length) cost = criterion(preds, text, preds_size, length) / batch_size loss_avg.add(cost) _, preds = preds.max(2, keepdim=True) preds = preds.squeeze(2) preds = preds.transpose(1, 0).contiguous().view(-1) sim_preds = converter.decode(preds.data, preds_size.data, raw=False) for pred, target in zip(sim_preds, cpu_texts): if pred == target.lower(): # 两个成功数量都加1 one_correct += 1 correct_Count += 1 del val_iter raw_preds = converter.decode(preds.data, preds_size.data, raw=True)[:opt.n_test_disp] accuracy = one_correct / float(max_iter * opt.batchSize) if accuracy < 0.95: for raw_pred, pred, gt in zip(raw_preds, sim_preds, cpu_texts): print_msg('%-20s => %-20s, gt: %-20s' % (raw_pred, pred, gt)) print_msg('验证 %-3d/%d,Loss: %f,Flag: [%-15s] 的成功率: %f' % (i, len(val_data_list_param), loss_avg.val(), val_data['dir'], accuracy)) accuracy = correct_Count / float(all_Count) print_msg('总的成功率: %f ,总验证文件数: %d ' % (accuracy, all_Count)) return accuracy
def val(net, test_dataset, criterion, max_iter=100): print('Start val') for p in crnn.parameters(): p.requires_grad = False # layer_dict = net.state_dict() # print(layer_dict['cnn.conv1.weight']) net.eval() data_loader = torch.utils.data.DataLoader(test_dataset, shuffle=True, batch_size=opt.batchSize, num_workers=int(opt.workers), collate_fn=dataset.alignCollate( imgH=32, imgW=100, keep_ratio=True)) val_iter = iter(data_loader) i = 0 n = 0 n_correct = 0 n_text = 0 loss_avg = util.averager() max_iter = len(data_loader) for i in range(max_iter): data = val_iter.next() i += 1 cpu_images, cpu_texts = data batch_size = cpu_images.size(0) util.loadData(image, cpu_images) t, l = converter.encode(cpu_texts) util.loadData(text, t) util.loadData(length, l) preds = crnn(image) preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size)) cost = criterion(preds, text, preds_size, length) / batch_size loss_avg.add(cost) _, preds = preds.max(2) # preds = preds.squeeze(2) preds = preds.transpose(1, 0).contiguous().view(-1) # print (preds) sim_preds = converter.decode(preds.data, preds_size.data, raw=False) for pred, target in zip(sim_preds, cpu_texts): if isinstance(target, unicode) is False: target = target.decode('utf-8') pred_encode, _ = converter.encode(pred) target_encode, _ = converter.encode(target) t = editdistance.eval(pred_encode, target_encode) l = len(target_encode) n_correct += t n_text += l n += 1 raw_preds = converter.decode(preds.data, preds_size.data, raw=True)[:opt.n_test_disp] for raw_pred, sim_pred, gt in zip(raw_preds, sim_preds, cpu_texts): print('%-20s => %-20s, gt: %-20s' % (raw_pred, sim_pred, gt)) len_edit = n_correct / float(n) len_text = n_text / float(n) norm = 1 - len_edit / len_text print('aver editd: %f, norm acc: %f' % (len_edit, norm))
if opt.cuda: crnn.cuda() image = image.cuda() criterion = criterion.cuda() image = Variable(image) text = Variable(text) length = Variable(length) # loss averager loss_avg = utils.averager() # setup optimizer if opt.adam: optimizer = optim.Adam( crnn.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999)) elif opt.adadelta: optimizer = optim.Adadelta(crnn.parameters(), lr=opt.lr) else: optimizer = optim.RMSprop(crnn.parameters(), lr=opt.lr) def val(net, dataset, criterion, max_iter=2): print('Start val') for p in crnn.parameters(): p.requires_grad = False net.eval() data_loader = torch.utils.data.DataLoader( dataset,
def val(net, dataset, criterion, max_iter=20): print('Start val') for p in crnn.parameters(): p.requires_grad = False net.eval() data_loader = torch.utils.data.DataLoader(dataset, shuffle=True, batch_size=opt.batchSize, num_workers=int(opt.workers)) val_iter = iter(data_loader) i = 0 n_correct = 0 sum_NED = 0 n_dataset = 0 loss_avg = utils.averager() max_iter = min(max_iter, len(data_loader)) for i in range(max_iter): data = val_iter.next() i += 1 cpu_images, cpu_texts = data batch_size = cpu_images.size(0) utils.loadData(image, cpu_images) decode_texts = [text[2:-1] for text in cpu_texts] t, l = converter.encode(decode_texts) utils.loadData(text, t) utils.loadData(length, l) preds = crnn(image) preds_chunks = preds.chunk(len(gpu_ids), dim=0) preds = torch.cat( preds_chunks, dim=1 ) # [num_gpu * time_step, batch_size / num_gpu, nOut] -> [time_step, batch_size, nOut] preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size)) cost = criterion(preds, text, preds_size, length) / batch_size loss_avg.add(cost) # [max_values,...,], [max_index,...,] = tensor.max(dim=k) # preds = [time_step, batch_size, nOut] # _, preds = preds.max(2) # preds = [time_step, batch_size] _, preds = preds.max(2) # preds = preds.squeeze(2) # after transpose, preds = [batch_size, time_step] preds = preds.transpose(1, 0).contiguous().view(-1) # preds = [batch_size * time_step] sim_preds = converter.decode(preds.data, preds_size.data, raw=False) # len(sim_preds) = batch_size, len(data_loader) = sizeof(datasets) / batch_size for pred, target in zip(sim_preds, cpu_texts): n_dataset += 1 gt = target[2:-1].lower() if pred == gt: n_correct += 1 else: # sum of normalized edit distance sum_NED += (EditDistance(pred, gt) / len(gt)) raw_preds = converter.decode(preds.data, preds_size.data, raw=True)[:opt.n_test_disp] for raw_pred, pred, gt in zip(raw_preds, sim_preds, cpu_texts): # print('%-20s => %-20s, gt: %-20s' % (raw_pred, pred, gt)) print('%-20s => %-20s' % (raw_pred, pred)) gt = gt[2:-1].lower() prompt = "" ned = 0 if pred != gt: maxLen = max(len(pred), len(gt)) minLen = min(len(pred), len(gt)) for i in range(maxLen): if i < minLen: if pred[i] != gt[i]: prompt += "^" else: prompt += " " else: prompt += "^" ned = EditDistance(pred, gt) print("pred:%-20s\ngt :%-20s\nerr :%-20s" % (pred, gt, prompt)) print("Edit Distance:\t%f" % (ned)) # accuracy = n_correct / float(max_iter * opt.batchSize) accuracy = n_correct / float(n_dataset) print('Test loss: %f, accuray: %f, NED: %f' % (loss_avg.val(), accuracy, sum_NED)) return accuracy, sum_NED
# 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(parameters.max_image_height, 3, parameters.number_of_classes, 256) crnn.apply(weights_init) criterion = CTCLoss(zero_infinity=True) optimizer = optim.Adam(crnn.parameters(), lr=0.001, betas=(0.5, 0.9)) loss_avg = averager() image = torch.FloatTensor(dataloader_params['batch_size'], 3, parameters.max_image_width, parameters.max_image_height) text = torch.IntTensor(dataloader_params['batch_size'] * 5) length = torch.IntTensor(dataloader_params['batch_size']) if torch.cuda.is_available(): crnn.cuda() crnn = torch.nn.DataParallel(crnn, device_ids=range(1)) image = image.cuda() text = text.cuda() length = length.cuda() criterion = criterion.cuda()
def val(net, dataset, max_iter=100): print('Start val') for p in crnn.parameters(): p.requires_grad = False net.eval() data_loader = torch.utils.data.DataLoader(dataset, shuffle=True, batch_size=opt.batchSize, num_workers=int(opt.workers)) val_iter = iter(data_loader) i = 0 n_correct = 0 # loss averager avg_h_val = utils.averager() avg_cost_val = utils.averager() avg_h_cost_val = utils.averager() if opt.eval_all: max_iter = len(data_loader) else: max_iter = min(max_iter, len(data_loader)) for i in range(max_iter): data = val_iter.next() # print('data: ', data) # print(data) i += 1 cpu_images, cpu_texts = data batch_size = cpu_images.size(0) utils.loadData(image, cpu_images) t, l = converter.encode(cpu_texts) utils.loadData(text, t) utils.loadData(length, l) # print('len(cpu_images): ', len(cpu_images)) # print('cpu_texts ', cpu_texts) # print('len(cpu_texts): ', len(cpu_texts)) # print('l ', l) # print(len(l)) # print('length ', length) preds = crnn(image) # size = 26, 64, 96 # print(preds.size()) preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size)) H, cost = seg_ctc_ent_cost(preds, text, preds_size, length, uni_rate=opt.uni_rate) h_cost = (1 - opt.h_rate) * cost - opt.h_rate * H avg_h_val.add(H / batch_size) avg_cost_val.add(cost / batch_size) avg_h_cost_val.add(h_cost / batch_size) _, preds = preds.max(2) # size = 26, 64 # print(preds.size()) # preds = preds.squeeze(2) preds = preds.transpose(1, 0).contiguous().view(-1) sim_preds = converter.decode(preds.data, preds_size.data, raw=False) for idx, (pred, target) in enumerate(zip(sim_preds, cpu_texts)): if pred == target.lower(): n_correct += 1 raw_preds = converter.decode(preds.data, preds_size.data, raw=True)[:opt.n_test_disp] for raw_pred, pred, gt in zip(raw_preds, sim_preds, cpu_texts): print('%-30s => %-30s, gt: %-30s' % (raw_pred, pred, gt)) accuracy = n_correct / float(max_iter * opt.batchSize) print( 'Test H: %f, Cost: %f, H Cost: %f, accuray: %f' % (avg_h_val.val(), avg_cost_val.val(), avg_h_cost_val.val(), accuracy))
def val(net, dataset, criterion, max_iter=100): print('Start val') for p in crnn.parameters(): p.requires_grad = False net.eval() data_loader = torch.utils.data.DataLoader(dataset, shuffle=True, batch_size=opt.batchSize, num_workers=int(opt.workers)) val_iter = iter(data_loader) i = 0 n_correct = 0 loss_avg = utils.averager() max_iter = min(max_iter, len(data_loader)) print("max_iter", max_iter, "len(data_loader)", len(data_loader)) for i in range(max_iter): data = val_iter.next() # print(data) i += 1 cpu_images, cpu_texts = data batch_size = cpu_images.size(0) utils.loadData(image, cpu_images) t, l = converter.encode(cpu_texts) utils.loadData(text, t) utils.loadData(length, l) preds = crnn(image) preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size)) cost = criterion(preds, text, preds_size, length) / batch_size loss_avg.add(cost) _, preds = preds.max(2) # preds = preds.squeeze(2) preds = preds.transpose(1, 0).contiguous().view(-1) list_cpu_texts = [] for i in cpu_texts: list_cpu_texts.append(i.decode('utf-8', 'strict')) sim_preds = converter.decode(preds.data, preds_size.data, raw=False) if (i == 1): print(sim_preds) print(cpu_texts) # cpu_texts = byte_to_zh(cpu_texts) # print("sim_preds",sim_preds) for pred, target in zip(sim_preds, list_cpu_texts): if (pred == target.lower()) | (pred == target): n_correct += 1 raw_preds = converter.decode(preds.data, preds_size.data, raw=True)[:opt.n_test_disp] for raw_pred, pred, gt in zip(raw_preds, sim_preds, list_cpu_texts): print('%-20s => %-20s, gt: %-20s' % (raw_pred, pred, gt)) accuracy = n_correct / float(max_iter * opt.batchSize) print('Test loss: %f, accuray: %f' % (loss_avg.val(), accuracy))
if opt.cuda: crnn.cuda() image = image.cuda() criterion = criterion.cuda() image = Variable(image) text = Variable(text) length = Variable(length) # loss averager loss_avg = utils.averager() # setup optimizer if opt.adam: optimizer = optim.Adam(crnn.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999)) elif opt.adadelta: optimizer = optim.Adadelta(crnn.parameters(), lr=opt.lr) else: optimizer = optim.RMSprop(crnn.parameters(), lr=opt.lr) def val(net, dataset, criterion, max_iter=100): print('Start val') for p in crnn.parameters(): p.requires_grad = False net.eval() data_loader = torch.utils.data.DataLoader(
def val(net, dataset, criterion, max_iter=1000, test_aug=False, n_aug=1): print('Start validation set') for p in crnn.parameters(): p.requires_grad = False net.eval() i = 0 n_correct = 0 loss_avg = utils.averager() image_count = 0 # Character and word error rate lists char_error = [] w_error = [] pred_dict = {} gt_dict = {} for epoch in range(n_aug): max_iter = len(dataset) if test_aug else min(max_iter, len(dataset)) val_iter = iter(dataset) for i in range(max_iter): data = val_iter.next() i += 1 cpu_images, cpu_texts, cpu_files = data batch_size = cpu_images.size(0) image_count = image_count + batch_size utils.loadData(image, cpu_images) t, l = converter.encode(cpu_texts) utils.loadData(text, t) utils.loadData(length, l) preds = crnn(image) #print(preds.size()) preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size)) cost = criterion(preds, text, preds_size, length) / batch_size loss_avg.add(cost) # RA: While I am not sure yet, it looks like a greedy decoder and not beam search is being used here # Case is ignored in the accuracy, which is not ideal for an actual working system _, preds = preds.max(2) if torch.__version__ < '0.2': preds = preds.squeeze(2) # https://github.com/meijieru/crnn.pytorch/issues/31 preds = preds.transpose(1, 0).contiguous().view(-1) sim_preds = converter.decode(preds.data, preds_size.data, raw=False) for pred, target, f in zip(sim_preds, cpu_texts, cpu_files): if f not in gt_dict: gt_dict[f] = target pred_dict[f] = [] pred_dict[f].append(pred) if pred == target: n_correct += 1 # Case-sensitive character and word error rates for f, target in gt_dict.items(): # Finds the most commonly predicted string for all the augmented images best_pred = Counter(pred_dict[f]).most_common(1)[0][0] char_error.append(cer(best_pred, target)) w_error.append(wer(best_pred, target)) raw_preds = converter.decode(preds.data, preds_size.data, raw=True)[:opt.n_test_disp] for raw_pred, pred, gt in zip(raw_preds, sim_preds, cpu_texts): print('%-20s => %-20s, gt: %-20s' % (raw_pred, pred, gt)) print("Total number of images in validation set: %8d" % image_count) accuracy = n_correct / float(max_iter * opt.batchSize) print('Test loss: %f, accuracy: %f' % (loss_avg.val(), accuracy)) char_arr = np.array(char_error) w_arr = np.array(w_error) char_mean_error = np.mean(char_arr) word_mean_error = np.mean(w_arr) print("Character error rate mean: %4.4f; Character error rate sd: %4.4f" % ( char_mean_error, np.std(char_arr, ddof=1))) print("Word error rate mean: %4.4f; Word error rate sd: %4.4f" % (word_mean_error, np.std(w_arr, ddof=1))) return char_mean_error, word_mean_error, accuracy
if opt.cuda: crnn.cuda() traject = traject.cuda() criterion = criterion.cuda() traject = Variable(traject) text = Variable(text) length = Variable(length) # loss averager loss_avg = utils.averager() # setup optimizer if opt.adam: optimizer = optim.Adam(crnn.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999)) elif opt.adadelta: optimizer = optim.Adadelta(crnn.parameters(), lr=opt.lr) elif opt.rmsprop: optimizer = optim.RMSprop(crnn.parameters(), lr=opt.lr) else: optimizer = torch.optim.SGD(crnn.parameters(), lr=opt.lr, momentum=0.9, weight_decay=1e-4) def padding2tensor(trajects): nTrajs = len(trajects) maxLen = max([len(trajects[i].split(',')) for i in range(len(trajects))]) new_trajects = torch.LongTensor(nTrajs, maxLen) for i in range(nTrajs):
def val(net, dataset, criterion, max_iter=100): print('Start val') for p in crnn.parameters(): p.requires_grad = False net.eval() data_loader = torch.utils.data.DataLoader(dataset, shuffle=True, batch_size=params.batchSize, num_workers=int(params.workers)) val_iter = iter(data_loader) i = 0 n_correct = 0 loss_avg = utils.averager() max_iter = min(max_iter, len(data_loader)) for i in range(max_iter): data = val_iter.next() i += 1 cpu_images, cpu_texts = data batch_size = cpu_images.size(0) utils.loadData(image, cpu_images) t, l = converter.encode(cpu_texts) utils.loadData(text, t) utils.loadData(length, l) preds = crnn(image) # print('-----preds-----') # print(preds) preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size)) # print('-----preds_size-----') # print(preds_size) cost = criterion(preds, text, preds_size, length) / batch_size loss_avg.add(cost) _, preds = preds.max(2) # print('-----preds.max(2)-----') # print(preds) preds = preds.transpose(1, 0).contiguous().view(-1) # print('-----preds.transpose(1, 0)-----') # print(preds) sim_preds = converter.decode(preds.data, preds_size.data, raw=False) list_1 = [] for m in cpu_texts: list_1.append(m.decode('utf-8', 'strict')) # if (i - 1) % 10 == 0: # print('-----sim_preds-----list_1-----') # print(sim_preds, list_1) for pred, target in zip(sim_preds, list_1): if pred == target: n_correct += 1 # else: # print('%-20s, gt: %-20s' % (pred, target)) raw_preds = converter.decode(preds.data, preds_size.data, raw=True)[:params.n_test_disp] for raw_pred, pred, gt in zip(raw_preds, sim_preds, list_1): print('%-20s => %-20s, gt: %-20s' % (raw_pred, pred, gt)) print(n_correct) print(max_iter * params.batchSize) accuracy = n_correct / float(max_iter * params.batchSize) print('Test loss: %f, accuray: %f' % (loss_avg.val(), accuracy))
if opt.cuda: crnn.cuda() crnn = torch.nn.DataParallel(crnn, device_ids=range(opt.ngpu)) image = image.cuda() criterion = criterion.cuda() image = Variable(image) text = Variable(text) length = Variable(length) # loss averager loss_avg = utils.averager() # setup optimizer if opt.adam: optimizer = optim.Adam(crnn.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999)) elif opt.adadelta: optimizer = optim.Adadelta(crnn.parameters(), lr=opt.lr) else: optimizer = optim.RMSprop(crnn.parameters(), lr=opt.lr) def val(net, dataset, criterion, max_iter=100): print('Start val') for p in crnn.parameters(): p.requires_grad = False net.eval()
nclass = len(opt.alphabet) + 1 nc = 3 converter = utils.strLabelConverter(opt.alphabet) criterion = CTCLoss() crnn = crnn.CRNN(opt.imgH, nc, nclass, opt.nh) print('loading pretrained model from %s' % opt.crnn) old = torch.load(opt.crnn) new_state_dict = {} for k, v in old.items(): new_state_dict[k[7:]] = v crnn.load_state_dict(new_state_dict) for p in crnn.parameters(): p.requires_grad = False if opt.cuda: crnn.cuda() crnn = torch.nn.DataParallel(crnn, device_ids=range(opt.ngpu)) def bytes2cv2(b): nparr = np.fromstring(b, np.uint8) img_np = cv2.imdecode(nparr, cv2.IMREAD_COLOR) return img_np def run(tensor): tensor = tensor.view(-1, *tensor.shape) preds = crnn(tensor)
if opt.cuda: crnn.cuda() image = image.cuda() criterion = criterion.cuda() summary(crnn.cnn, (3, opt.imgH, opt.imgW)) image = Variable(image) text = Variable(text) length = Variable(length) train_loss_avg = utils.averager() train_cer_avg = utils.averager() # setup optimizer optimizer = optim.Adam(crnn.parameters(), lr=opt.lr) def val(net, data_loader, criterion, max_iter=1000): print('Start val') for p in crnn.parameters(): p.requires_grad = False net.eval() val_iter = iter(data_loader) val_loss_avg = utils.averager() val_cer_avg = utils.averager() max_iter = min(max_iter, len(data_loader))
def val(net, dataset, criterion, max_iter=2): print('Start val') for p in crnn.parameters(): p.requires_grad = False net.eval() data_loader = torch.utils.data.DataLoader(dataset, shuffle=False, batch_size=opt.batchSize, num_workers=int(opt.workers)) val_iter = iter(data_loader) i = 0 n_correct = 0 loss_avg = utils.averager() max_iter = min(max_iter, len(data_loader)) for i in range(max_iter): data = val_iter.next() i += 1 cpu_images, cpu_texts = data batch_size = cpu_images.size(0) utils.loadData(image, cpu_images) if ifUnicode: cpu_texts = [clean_txt(tx.decode('utf-8')) for tx in cpu_texts] # print(cpu_texts) t, l = converter.encode(cpu_texts) # print(t) # print(l) utils.loadData(text, t) utils.loadData(length, l) preds = crnn(image) preds_size = Variable(torch.IntTensor([preds.size(0)] * batch_size)) cost = criterion(preds, text, preds_size, length) / batch_size loss_avg.add(cost) # print(preds) # print(preds.shape) _, preds = preds.max(2) # print(preds) # print(preds.shape) # preds = preds.squeeze(2) # print(preds) # print(preds.shape) preds = preds.transpose(1, 0).contiguous().view(-1) # print(preds) # print(preds.shape) sim_preds = converter.decode(preds.data, preds_size.data, raw=False) print(sim_preds) print(cpu_texts) for pred, target in zip(sim_preds, cpu_texts): if pred.strip() == target.strip(): n_correct += 1 # raw_preds = converter.decode(preds.data, preds_size.data, raw=True)[:opt.n_test_disp] # for raw_pred, pred, gt in zip(raw_preds, sim_preds, cpu_texts): # print((pred, gt)) # print accuracy = n_correct / float(max_iter * opt.batchSize) testLoss = loss_avg.val() print('Test loss: %f, accuray: %f' % (testLoss, accuracy)) return testLoss, accuracy