コード例 #1
0
    source_path = os.path.join(data_path, f"{args.source}.txt")
    output_path = os.path.join("..", "output", args.source, args.mode)
    target_path = os.path.join(output_path, "checkpoint_weights.hdf5")

    max_text_length = 128
    charset_base = string.printable[:95]
    charset_special = """ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïñòóôõöùúûüý"""

    if args.transform:
        data = Dataset(source=os.path.join(raw_path, args.source))
        data.read_lines(maxlen=max_text_length)

        valid_noised = pp.add_noise(data.dataset['valid'], max_text_length)
        test_noised = pp.add_noise(data.dataset['test'], max_text_length)

        valid_metrics = ev.ocr_metrics(ground_truth=data.dataset['valid'],
                                       data=valid_noised)

        info = "\n".join([
            f"####",
            f"#### {args.source} partitions (number of sentences)",
            f"####",
            f"#### Total:      {data.size['total']}",
            f"####",
            f"#### Train:      {data.size['train']}",
            f"#### Validation: {data.size['valid']}",
            f"####\n",
            f"#### Validation Error Rate:",
            f"#### CER: {valid_metrics[0]:.8f}",
            f"#### WER: {valid_metrics[1]:.8f}",
            f"#### SER: {valid_metrics[2]:.8f}",
        ])
コード例 #2
0
            start_time = time.time()
            predicts, _ = model.predict(x=dtgen.next_test_batch(),
                                        steps=dtgen.steps['test'],
                                        ctc_decode=True,
                                        verbose=1)

            predicts = [dtgen.tokenizer.decode(x[0]) for x in predicts]
            total_time = time.time() - start_time

            with open(os.path.join(output_path, "predict.txt"), "w") as lg:
                for pd, gt in zip(predicts, dtgen.dataset['test']['gt']):
                    lg.write(f"TE_L {gt}\nTE_P {pd}\n")

            evaluate = evaluation.ocr_metrics(
                predicts=predicts,
                ground_truth=dtgen.dataset['test']['gt'],
                norm_accentuation=False,
                norm_punctuation=False)

            e_corpus = "\n".join([
                f"Total test images:    {dtgen.size['test']}",
                f"Total time:           {(total_time / 60):.2f} min",
                f"Time per item:        {(total_time / dtgen.size['test']):.8f} sec\n",
                f"Metrics:", f"Character Error Rate: {evaluate[0]:.8f}",
                f"Word Error Rate:      {evaluate[1]:.8f}",
                f"Sequence Error Rate:  {evaluate[2]:.8f}"
            ])

            with open(os.path.join(output_path, "evaluate.txt"), "w") as lg:
                lg.write(e_corpus)
                print(e_corpus)
コード例 #3
0
ファイル: main.py プロジェクト: famus2310/VK-OCR
                        trg_tensor = torch.LongTensor(out_indexes).unsqueeze(
                            1).to(device)
                        output = model.vocab(
                            model.transformer.decoder(model.query_pos(
                                model.decoder(trg_tensor)),
                                                      memory,
                                                      tgt_mask=mask))
                        out_token = output.argmax(2)[-1].item()
                        out_indexes.append(out_token)
                        # print(output.shape)
                        if out_token == 3:
                            break

                    predicts.append(tokenizer.decode(out_indexes))
                    gt.append(tokenizer.decode(trg.flatten(0, 1)))

            predicts = list(
                map(lambda x: x.replace('SOS', '').replace('EOS', ''),
                    predicts))
            gt = list(
                map(lambda x: x.replace('SOS', '').replace('EOS', ''), gt))

            evaluate = evaluation.ocr_metrics(
                predicts=predicts,
                ground_truth=gt,
                norm_accentuation=args.norm_accentuation,
                norm_punctuation=args.norm_punctuation)
            print(
                "Calculate Character Error Rate {}, Word Error Rate {} and Sequence Error Rate {}"
                .format(evaluate[0], evaluate[1], evaluate[2]))
コード例 #4
0
total_time = datetime.datetime.now() - start_time

# mount predict corpus file
with open(os.path.join(output_path, "predict.txt"), "w") as lg:
    for pd, gt in zip(predicts, ground_truth):
        lg.write(f"TE_L {gt}\nTE_P {pd}\n")

# for i, item in enumerate(dtgen.dataset['test']['dt'][:10]):
#     print("=" * 1024, "\n")
#     cv2_imshow(pp.adjust_to_see(item))
#     print(ground_truth[i])
#     print(predicts[i], "\n")

# TODO EVALUATE
from data import evaluation

evaluate = evaluation.ocr_metrics(predicts, ground_truth)

e_corpus = "\n".join([
    f"Total test images:    {dtgen.size['test']}",
    f"Total time:           {total_time}",
    f"Time per item:        {total_time / dtgen.size['test']}\n", f"Metrics:",
    f"Character Error Rate: {evaluate[0]:.8f}",
    f"Word Error Rate:      {evaluate[1]:.8f}",
    f"Sequence Error Rate:  {evaluate[2]:.8f}"
])

with open(os.path.join(output_path, "evaluate.txt"), "w") as lg:
    lg.write(e_corpus)
    print(e_corpus)
コード例 #5
0
                verbose=1)

            predicts = pp.decode_ctc(predicts, charset_base)
            total_time = time.time() - start_time

            labels = [x.decode() for x in dtgen.dataset["test"]["gt_bytes"]]
            pred_corpus = [
                f"TE_L {gt}\nTE_P {pd}\n" for pd, gt in zip(predicts, labels)
            ]

            with open(os.path.join(output_path, "predict.txt"), "w") as lg:
                lg.write("\n".join(pred_corpus))
                print("\n".join(pred_corpus))

            evaluate = evaluation.ocr_metrics(predicts=predicts,
                                              ground_truth=labels,
                                              norm_accentuation=False,
                                              norm_punctuation=False)

            eval_corpus = "\n".join([
                f"Total test images:    {dtgen.total_test}",
                f"Total time:           {total_time:.8f} sec",
                f"Time per item:        {(total_time / dtgen.total_test):.8f} sec\n",
                f"Metrics:", f"Character Error Rate: {evaluate[0]:.8f}",
                f"Word Error Rate:      {evaluate[1]:.8f}"
            ])

            with open(os.path.join(output_path, "evaluate.txt"), "w") as lg:
                lg.write(eval_corpus)
                print(eval_corpus)