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

username: MUHAMMAD_JAHANZAIB_K
cooked: <p>i need to make a dataset for object localization how it possible?</p>
<p>def show_landmarks(image, landmarks):<br>
“”“Show image with landmarks”""<br>
xmin=landmarks[0]<br>
ymin=landmarks[1]<br>
xmax=landmarks[2]<br>
ymax=landmarks[3]<br>
start=(xmin,ymin)<br>
end=(xmax,ymax)<br>
image=cv2.rectangle(image,start,end,(100,210,69),1)<br>
plt.imshow(image)</p>
<p>class Dataset(torch.utils.data.Dataset):</p>
<pre><code>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</code></pre>

username: ptrblck
cooked: <p>Your <code>Dataset</code> should work assuming that <code>img_name</code> and <code>landmarks</code> are numpy arrays.<br>
Are you seeing any errors?</p>

username: MUHAMMAD_JAHANZAIB_K
cooked: <p>ptrblck thank you for your reply you always help when i am stuck</p>
<p>yes i have error “its not a tensor” some thing like this but first thing i need to understand how to get values from <strong>getitem</strong>. 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</p>

username: ptrblck
cooked: <p>Could you check the types of <code>img_name</code> and <code>landmarks</code>?<br>
Could you also transform both arrays to tensors via <code>torch.from_numpy</code> and check, if this operation fails?</p>

username: MUHAMMAD_JAHANZAIB_K
cooked: <p>yes i transform arrays to tensors  but i cant under stand how to pass img_name,landmarks to</p>
<p>this function</p>
<pre><code class="lang-auto">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)
</code></pre>

username: ptrblck
cooked: <p><code>show_landmarks</code> seems to expect an already loaded image.<br>
If <code>image_name</code> just contains the path to the image, you would have to load it e.g. via <code>PIL.Image.open</code>, and transform it to a tensor e.g. via <code>torchvision.transforms.ToTensor()</code>.</p>

username: MUHAMMAD_JAHANZAIB_K
cooked: <p>i am create a object like this</p>
<pre><code class="lang-auto">dataset = Dataset(csv_file='Dataset/loclization.csv', root_dir='Dataset/local/')
</code></pre>

username: MUHAMMAD_JAHANZAIB_K
cooked: <p>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</p>

username: MUHAMMAD_JAHANZAIB_K
cooked: <p>after creating object what is next step to get values of the class functions like <strong>getitem</strong> function return values img_name and landmarks</p>

username: ptrblck
cooked: <p>You can just call:</p>
<pre><code class="lang-python">img_name, landmarks = dataset[0]
</code></pre>
<p>or wrap the dataset in a <code>DataLoader</code> as described <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html">here</a>.</p>

username: MUHAMMAD_JAHANZAIB_K
cooked: <p>Thank you ptrblck,<br>
its working and i got my required result</p>

username: MUHAMMAD_JAHANZAIB_K
cooked: <p>Hello sir i have an other error when passing image to the model</p>
<p>Error is<br>
Expected 4-dimensional input for 4-dimensional weight 16 3 3 3, but got 3-dimensional input of size [136, 136, 3] instead</p>
<p>and my code is</p>
<pre><code class="lang-auto">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)
</code></pre>