Describe the bug个 我使用的模型(TrOCR模型):

在使用以下选项时会出现问题:

  • [X]官方示例脚本:由NICE教程(fine_tune)@NielsRogge完成
  • [X]我自己修改的脚本:(如下脚本所示)
processor = TrOCRProcessor.from_pretrained("microsoft/trocr-large-handwritten")

class Dataset(Dataset):
    def __init__(self, root_dir, df, processor, max_target_length=128):
        self.root_dir = root_dir
        self.df = df
        self.processor = processor
        self.max_target_length = max_target_length

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

    def __getitem__(self, idx):
        # get file name + text 
        file_name = self.df['file_name'][idx]
        text = self.df['text'][idx]
        # prepare image (i.e. resize + normalize)
        image = Image.open(self.root_dir + file_name).convert("RGB")
        pixel_values = self.processor(image, return_tensors="pt").pixel_values
        # add labels (input_ids) by encoding the text
        labels = self.processor.tokenizer(text, 
                                          padding="max_length",
                                                         max_length=self.max_target_length).input_ids
        # important: make sure that PAD tokens are ignored by the loss function
        labels = [label if label != self.processor.tokenizer.pad_token_id else -100 for label in labels]
        # encoding  
        return {"pixel_values": pixel_values.squeeze(), "labels": torch.tensor(labels)}

model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-large-handwritten")
model.config.decoder_start_token_id = processor.tokenizer.cls_token_id
model.config.pad_token_id = processor.tokenizer.pad_token_id
model.config.vocab_size = model.config.decoder.vocab_size

model.config.eos_token_id = processor.tokenizer.sep_token_id


# python3 train.py path/to/labels  path/to/images/
  • 平台:linux上的linux Ubuntu发行版[GCC 9.4.0]
  • PyTorch版本(GPU?):0.8.2+cu110
  • 《变形金刚:4.22.2》
  • Python版本:3.8.10

对错误是什么的清晰而简明的描述. 重现这种行为的Reproduce个步骤:

  1. 在训练模型之后或在训练阶段判断指标计算时,我看到模型添加了令牌<s><s>或IDS[0,0, ......,2,1,1, 1 ]的双开头
  2. 以下是training个阶段中的一个示例,显示在COMPUTE_METRICS中生成的标记 输入预测:[[0,0,506,4422,8046,2,1,1,1,1,1]] 输入参考:[[0,597,2747 ...,1,1,1]]
  3. Other examples during testing models [enter image description here]

Expected behavior对你所期望发生的事情进行清晰而简明的描述. 在2个重现的问题中: 我期待在培训期间输入预测:[[,0,,506,4422,8046,2,1,1,1,1,1 ]]

此外,在测试阶段:生成的文本没有重复 tensor([[0,11867,405,22379,1277,..........,368,2]])

<s>ennyit erről, tőlem fényképezz amennyit akarsz, a véleményem akkor</s>

推荐答案

问题来自传递的令牌ID.我正在添加来自记号赋值器的开始令牌和来自TrOCR模型的另一个开始令牌,以便发生复制.解决方案非常简单,只需使用labels = labels[1:]跳过来自记号赋值器的开始令牌

def __getitem__(self, idx):
        # get file name + text 
        file_name = self.df['file_name'][idx]
        text = self.df['text'][idx]
        # prepare image (i.e. resize + normalize)
        image = Image.open(self.root_dir + file_name).convert("RGB")
        pixel_values = self.processor(image, return_tensors="pt").pixel_values
        # add labels (input_ids) by encoding the text
        labels = self.processor.tokenizer(text, 
                                          padding="max_length",
                                                                                    max_length=self.max_target_length).input_ids
        # important: make sure that PAD tokens are ignored by the loss function
        labels = [label if label != self.processor.tokenizer.pad_token_id else -100 for label in labels]
        # encoding
        labels = labels[1:]   
        return {"pixel_values": pixel_values.squeeze(), "labels": torch.tensor(labels)}

Python相关问答推荐

Pandas 修改原始excel

用ctype构建指针链

查找3D数组中沿一个轴的相同值序列的长度(与行程长度编码相关)

预期LP_c_Short实例而不是_ctyles.PyCStructType

Python中的锁定类和线程以实现dict移动

Python:MultiIndex Dataframe到类似json的字典列表

在后台运行的Python函数

"Discord机器人中缺少所需的位置参数ctx

如何根据日期和时间将状态更新为已过期或活动?

如何才能知道Python中2列表中的巧合.顺序很重要,但当1个失败时,其余的不应该失败或是0巧合

Python在tuple上操作不会通过整个单词匹配

如何在Windows上用Python提取名称中带有逗号的文件?

可变参数数量的重载类型(args或kwargs)

用NumPy优化a[i] = a[i-1]*b[i] + c[i]的迭代计算

pandas在第1列的id,第2列的标题,第3列的值,第3列的值?

如何在图中标记平均点?

使用groupby方法移除公共子字符串

AES—256—CBC加密在Python和PHP中返回不同的结果,HELPPP

处理具有多个独立头的CSV文件

在方法中设置属性值时,如何处理语句不可达[Unreacable]";的问题?