pbcquoc / vietocr

Transformer OCR
Apache License 2.0
601 stars 204 forks source link

lỗi về format của file test_line_anotation.txt #62

Closed TranPhu1999 closed 2 years ago

TranPhu1999 commented 2 years ago

Dạ em chào anh ạ. Em có đang làm một cái project có sử dụng vietocr và gặp một số lỗi ngay trong bước train image Sau khi truy soát lỗi em có phát hiện trong self.valid_gen hoàn toàn rỗng còn self.train_gen vẫn có chứa thông tin, dù em đã kiểm tra kĩ file test_line_anotation.txt hoàn toàn có cấu trúc giống train_line_anotation.txt và giống với cấu trúc của các file anotations trong sample dataset (đường dẫn tới file + tab +label của file) em có đọc code lại rồi nhưng vẫn không hiểu sai ở đâu ạ. Đâu là file config của em ạ: {'aug': {'image_aug': True, 'masked_language_model': True}, 'backbone': 'vgg19_bn', 'cnn': {'hidden': 256, 'ks': [[2, 2], [2, 2], [2, 1], [2, 1], [1, 1]], 'ss': [[2, 2], [2, 2], [2, 1], [2, 1], [1, 1]]}, 'dataloader': {'num_workers': 2, 'pin_memory': True}, 'dataset': {'data_root': '/content/drive/MyDrive/fado/ocr_IDcard/fine_tune_vietocr/data_line', 'image_height': 32, 'image_max_width': 512, 'image_min_width': 32, 'name': 'cmnd', 'train_annotation': 'train_line_annotation.txt', 'valid_annotation': 'test_line_annotation.txt'}, 'device': 'cuda:0', 'optimizer': {'max_lr': 0.001, 'pct_start': 0.1}, 'predictor': {'beamsearch': False}, 'pretrain': {'cached': '/content/drive/MyDrive/fado/ocr_IDcard/fine_tune_vietocr/transformerocr.pth', 'id_or_url': '1nTKlEog9YFK74kPyX0qLwCWi60_YHHk4', 'md5': 'efcabaa6d3adfca8e52bda2fd7d2ee04'}, 'quiet': False, 'seq_modeling': 'seq2seq', 'trainer': {'batch_size': 32, 'checkpoint': './checkpoint/transformerocr_checkpoint.pth', 'export': './weights/transformerocr.pth', 'iters': 1000, 'log': './train.log', 'metrics': 100, 'print_every': 200, 'valid_every': 200}, 'transformer': {'decoder_embedded': 256, 'decoder_hidden': 256, 'dropout': 0.1, 'encoder_hidden': 256, 'imgchannel': 256}, 'vocab': 'aAàÀảẢãÃáÁạẠăĂằẰẳẲẵẴắẮặẶâÂầẦẩẨẫẪấẤậẬbBcCdDđĐeEèÈẻẺẽẼéÉẹẸêÊềỀểỂễỄếẾệỆfFgGhHiIìÌỉỈĩĨíÍịỊjJkKlLmMnNoOòÒỏỎõÕóÓọỌôÔồỒổỔỗỖốỐộỘơƠờỜởỞỡỠớỚợỢpPqQrRsStTuUùÙủỦũŨúÚụỤưƯừỪửỬữỮứỨựỰvVwWxXyYỳỲỷỶỹỸýÝỵỴzZ0123456789!"#$%&\'()*+,-./:;<=>?@[\]^`{|}~ ', 'weights': '/content/drive/MyDrive/fado/ocr_IDcard/fine_tune_vietocr/transformerocr.pth'}

Em cảm ơn anh ạ!

pbcquoc commented 2 years ago

số lượng mẫu tập train và test là bao nhiêu vậy e?

pbcquoc commented 2 years ago

@TranPhu1999 còn bị lỗi ko nhỉ

TranPhu1999 commented 2 years ago

dạ em xin lỗi anh hôm qua quên kiểm tra github ạ. Dạ tụi em vẫn còn lỗi ạ. Tụi em chia tập train và valid 80/20 tức khoảng 800 ảnh train và 200 ảnh valid ạ.

pbcquoc commented 2 years ago

bởi vì do cách batching ảnh, a lấy kích thước những ảnh sấp xỉ nhau để làm thành 1 batch, nếu ít ảnh test thì dẫn đến ko đủ ảnh để batching giải pháp là hoặc là e tăng số ảnh lên, hoặc là giảm batch_size xuống xem nhé.

TranPhu1999 commented 2 years ago

dạ vâng để em thử lại ạ, em cảm ơn anh trước ạ

uMetalooper commented 2 years ago

Mình cũng gặp vấn đề tương tự và tìm ra được nguyên nhân ở đây (vietocr/loader/dataloader.py)

class ClusterRandomSampler(Sampler):

    def __init__(self, data_source, batch_size, shuffle=True):
        self.data_source = data_source
        self.batch_size = batch_size
        self.shuffle = shuffle        

    def flatten_list(self, lst):
        return [item for sublist in lst for item in sublist]

    def __iter__(self):
        batch_lists = []
        for cluster, cluster_indices in self.data_source.cluster_indices.items():
            if self.shuffle:
                random.shuffle(cluster_indices)

            batches = [cluster_indices[i:i + self.batch_size] for i in range(0, len(cluster_indices), self.batch_size)]
            batches = [_ for _ in batches if len(_) == self.batch_size]
            if self.shuffle:
                random.shuffle(batches)

            batch_lists.append(batches)

        lst = self.flatten_list(batch_lists)
        if self.shuffle:
            random.shuffle(lst)

        lst = self.flatten_list(lst)

        return iter(lst)

    def __len__(self):
        return len(self.data_source)

Cụ thể là ở dòng này:

batches = [_ for _ in batches if len(_) == self.batch_size]

Do tính chất này nên nếu để batch_size theo mặc định của tác giả là 16 thì những cluster nào có ít hơn 16 mẫu sẽ không được cho vào batch. Giải pháp của mình là chỉnh batch_size về 1.

@pbcquoc Mình muốn hỏi là để batch_size về 1 có ảnh hưởng tới tốc độ cũng như hiệu quả train không bạn?

pbcquoc commented 2 years ago

batch_size=1 thì layer batchnorm sẽ không hoạt động được nhé @uMetalooper, bạn có thể thử nghiệm với batch_size từ >=4 để kiểm tra độ chính xác sau batch_size bao nhiêu là hợp lý ngoài ra vài trăm mẫu dữ liệu là khá ít nên tốt nhất là bổ sung thêm để mô hình hoạt động hiệu quả hơn.