_getitem_() is not working when extracting images from paths in txt file

username: Ethan_Zhu1
cooked: <p>I have a lot of images data and I am trying to pack 8 images with 1 label interactively. After that, I will feed the data into the neural network accordingly. But I’m unable to extract the images from my customized Dataset(). Here is my code below</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/0/7013d7cdb97ae65e46b6dcfe86dfe26478b9324f.png" data-download-href="https://discuss.pytorch.org/uploads/default/7013d7cdb97ae65e46b6dcfe86dfe26478b9324f" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/0/7013d7cdb97ae65e46b6dcfe86dfe26478b9324f.png" alt="image" data-base62-sha1="fZtZnSYzbsIDvCX92CB7ZvAvSOP" width="678" height="500" data-dominant-color="FAF9F9"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">930×685 28.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>my file path looks like this:</p>
<p>…\O2Hb_1.png<br>
…\O2Hb_2.png<br>
…\O2Hb_3.png<br>
…\O2Hb_4.png<br>
…\O2Hb_5.png<br>
…\O2Hb_6.png<br>
…\O2Hb_7.png<br>
…\O2Hb_8.png<br>
0</p>
<p>the last line 0 is the label</p>
<p>and is there any way to put 8 images into 1 tensor with 1 label, and then feed into CNN?</p>
<p>I’m new to Pytorch and really need some help, thanks!</p>

username: Oli
cooked: <p>Yes there sure is <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> Instead of having imgs as a list, try having it as a deafultdict where the key is the index, and value is a list of images. But, you might want to rethink this a bit. The txt_path is just 8 images and one label right? You have several of these files I suppose? Then you want to put them all in the dataset, no?</p>
<p>This code should get you started.</p>
<pre><code class="lang-auto">from collections import defaultdict

dict_imgs = defaultdict(list)
with open(txt_path) as f:
  lines = f.read().splitlines()

dict_imgs[0] = lines # dictimgs will contain 8 image paths &amp; one label
</code></pre>

username: Ethan_Zhu1
cooked: <p>Hi Oli, Thanks for your reply, appreciate it!</p>
<p>I rethinked a little bit about it and I changed the format of the paths in the text file. Basically, I just put all 8 file paths in one line and separate them with a comma and put the label in the end. It looks like this:</p>
<p>…\O2Hb_1.png,…\O2Hb_2.png,…\O2Hb_3.png,…\O2Hb_4.png,…\O2Hb_5.png,…\O2Hb_6.png,…\O2Hb_7.png,…\O2Hb_8.png,0</p>
<p>I will put all the input images’ paths in this text file and read each line iteratively.</p>
<p>and instead of reading the list in initial I decided to do it in the getitem function because I have all 8 paths and label in one line.</p>
<p>class MyDataset(Dataset):</p>
<pre><code>def __init__(self, txt_path,transform=None,target_transform = None):
    fh = open(txt_path, 'r')
    imgs = []
    for line in fh:
        imgs.append(line)
    self.imgs = imgs
    self.transform = transform
    self.target_transform = target_transform
    
def __getitem__(self, index):
    fn = self.imgs[index]
    fn = fn.rstrip()
    paths = fn.split(',')
    image_dir_1 = paths[0]
    image_dir_2 = paths[1]
    label = paths[8]
    img1 = Image.open(image_dir_1).convert('RGB')
    if self.transform is not None:
        img1 = self.transform(img1)
    img2 = Image.open(image_dir_2).convert('RGB')
    if self.transform is not None:
        img2 = self.transform(img2)
    img2 = torch.cat(img1,img2)
    return img2,label
</code></pre>
<p>I am using the first 2 images as an example, and it seems like working but I don’t know why I cannot use torch.cat() to combine two tensors properly.</p>

username: Oli
cooked: <p>Nice work <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>To help you figure out the torch.cat stuff, do some printouts before and after the cat.</p>
<pre><code class="lang-auto">print(img1.shape)
print(img2.shape)
</code></pre>
<p>In this case I can see that your code is missing a parenthesis. It should be <code> img2 = torch.cat((img1,img2))</code>. Just a heads up, if you do a batch size with more than 1, you will probably want to write a custom collate function, just fyi <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>