How to get the two return values from the getitem function in Dataset class

i need to make a dataset for object localization how it possible?

def show_landmarks(image, landmarks):
“”“Show image with landmarks”""
xmin=landmarks[0]
ymin=landmarks[1]
xmax=landmarks[2]
ymax=landmarks[3]
start=(xmin,ymin)
end=(xmax,ymax)
image=cv2.rectangle(image,start,end,(100,210,69),1)
plt.imshow(image)

class Dataset(torch.utils.data.Dataset):

def __init__(self, csv_file, root_dir, transform=None):
    
    self.csv = pd.read_csv(csv_file)
    self.root   = root_dir
    self.transform = transform

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

def __getitem__(self,idx):
    
    img_name=self.csv.iloc[idx,0]
    landmarks =self.csv.iloc[idx, 4:].as_matrix()
    
    
    
   
  
    
    
    return img_name,landmarks

Your Dataset should work assuming that img_name and landmarks are numpy arrays.
Are you seeing any errors?

ptrblck thank you for your reply you always help when i am stuck

yes i have error “its not a tensor” some thing like this but first thing i need to understand how to get values from getitem. i cant understand how to get the values from the dataset class and pass to the model i tried my best but after a long time i am at the same where i am start

Could you check the types of img_name and landmarks?
Could you also transform both arrays to tensors via torch.from_numpy and check, if this operation fails?

yes i transform arrays to tensors but i cant under stand how to pass img_name,landmarks to

this function

def show_landmarks(image, landmarks):
“”“Show image with landmarks”""
xmin=landmarks[0]
ymin=landmarks[1]
xmax=landmarks[2]
ymax=landmarks[3]
start=(xmin,ymin)
end=(xmax,ymax)
image=cv2.rectangle(image,start,end,(100,210,69),1)
plt.imshow(image)

show_landmarks seems to expect an already loaded image.
If image_name just contains the path to the image, you would have to load it e.g. via PIL.Image.open, and transform it to a tensor e.g. via torchvision.transforms.ToTensor().

i am create a object like this

dataset = Dataset(csv_file='Dataset/loclization.csv', root_dir='Dataset/local/')

i am load image using PLT.Image.open but for loading the image we need img_name and at this point problem is this i don,t know how to read img_age name from dataset class

after creating object what is next step to get values of the class functions like getitem function return values img_name and landmarks

You can just call:

img_name, landmarks = dataset[0]

or wrap the dataset in a DataLoader as described here.

Thank you ptrblck,
its working and i got my required result

Hello sir i have an other error when passing image to the model

Error is
Expected 4-dimensional input for 4-dimensional weight 16 3 3 3, but got 3-dimensional input of size [136, 136, 3] instead

and my code is

dataset = Dataset(csv_file='Dataset/loclization.csv', root_dir='Dataset/local/')

image_name,landmark=dataset[0]
print(landmark)
image = io.imread(image_name)
show_landmarks(image, landmark)
image=torch.from_numpy(image)
model(image)