def accuracy(inputs, input_lengths, labels, batch_size): n_correct = 0 _, inputs = inputs.max(2) inputs = inputs.transpose(1, 0).contiguous().view(-1) sim_preds = converter.decode(inputs.data, input_lengths.data, raw=False) for pred, target in zip(sim_preds, labels): if pred == target: n_correct += 1 return n_correct / float(batch_size)
lines = [] print('evaluating...') num_files = len(files) for i in tqdm(range(num_files)): file = 'word_{}.png'.format(i + 1) im_fn = os.path.join(image_folder, file) img = cv.imread(im_fn) img = cv.resize(img, (imgW, imgH), cv.INTER_CUBIC) # img = image_resize(img, width=imgW, height=imgH, inter=cv.INTER_CUBIC) img = img[..., ::-1] # RGB img = transforms.ToPILImage()(img) img = transformer(img) img = img.to(device) img = img.unsqueeze(0) preds = model(img) _, preds = preds.max(2) preds = preds.transpose(1, 0).contiguous().view(-1) preds_size = Variable(torch.IntTensor([preds.size(0)])) sim_pred = converter.decode(preds.data, preds_size.data, raw=False) lines.append('{}, \"{}\"\n'.format(file, sim_pred)) with open('submit.txt', 'w') as file: file.writelines(lines)