I’m having trouble training the model right now, please

I am requesting all of you to help me with my problem!
I’m training my model, I have a dataset class and have shaped the dictionary data = {‘pixel_values’: pixel_values, ‘text’: text} in its getitem (picture count data and text data) but I’m now reporting an error: texts.append(data[ ‘text’])
KeyError: ‘text’

I located the relevant files, and found that the error is in the datacollector class, inside the call function, it did not find the “text” key when using the dictionary, I am also debugging, and found that the parameters passed into this method in the feature does not have the key text, I do not know! I don’t know what the problem is, and I’ve followed chat-gpt’s method to troubleshoot the problem, such as the data returned by dataset’s getitem (which is fine!), and the data returned by getitem (which is fine!). , requesting all of you to help me!

My datacollector class :
`from typing import Any, Dict, List
import torch

class CLIPCollator(object):
def init(self, clip_processor, max_seq_length):
self.clip_processor = clip_processor
self.max_seq_length = max_seq_length

def __call__(self, features: List[Dict[str, Any]]) -> Dict[str, Any]:
    texts, pixel_values_list = [], []
    # print(features)
    for data in features:
        # 如果图片预处理失败,则跳过该图片
        # if data['pixel_values'] is None:
        #     continue
        # print(data)
        # assert 1 == 2
        # if data['text'] is None:
        #     continue
        texts.append(data['text'])
        pixel_values_list.append(data['pixel_values'])
    # 进行tokenize
    inputs = self.clip_processor(
        text=texts, return_tensors="pt", max_length=self.max_seq_length, truncation=True, padding=True
    )
    pixel_values_list = torch.concat(pixel_values_list, dim=0)
    inputs['return_loss'] = True
    inputs['pixel_values'] = pixel_values_list
    inputs.pop('token_type_ids')
    return inputs

my dataset class:from torch.utils.data import Dataset
import pandas as pd
from PIL import Image
import requests
from loguru import logger
from glob import glob
from os.path import join
from tqdm import tqdm
from torchvision import transforms
from torch.utils.data import Dataset
from PIL import Image
import os

class CLIPDataset(Dataset):

def __init__(self, file, clip_processor, image_path):
    df = pd.read_csv(file, usecols=['text', 'filename'])
    data_list = df.to_dict('records')
    print('len of data:{}'.format(len(data_list)))
    self.data_list = data_list
    self.clip_processor = clip_processor
    self.image_path = image_path

    # 定义图像预处理流程
    self.transform = transforms.Compose([
        transforms.Resize((224, 224)),  # 调整图像大小
        transforms.ToTensor(),  # 将图像转换为PyTorch张量
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # 归一化
    ])

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

def __getitem__(self, index):
    row = self.data_list[index]
    text = row['text'].strip()
    filename = row['filename']
    # print(text)
    file = join(self.image_path, filename)
    try:
        image = Image.open(file).convert('RGB')
    except Exception as e:
        # 下载图片失败
        logger.info('open image error, text: {}, filename:{}'.format(text, filename))
        logger.info(e)
        image = None

    # 应用预处理转换
    if self.transform:
        image = self.transform(image)
    if image is None:
        pixel_values = None
    else:
        pixel_values = self.clip_processor(images=image, return_tensors='pt')['pixel_values']

    data = {'pixel_values': pixel_values, 'text': text}
    # assert 1 == 2
    return data

`

New contributor

胡广成 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật