ImageFolder incompatible with albumentations

username: SK412
cooked: <p>HI! I am trying to use albumenations transforms with ImageFolder utility but I am stuck at this error, can some body help me out here?<br>
I have my data structure in the following way</p>
<pre><code class="lang-auto">root/dog/xxy.png
root/dog/[...]/xxz.png

root/cat/123.png
root/cat/nsdf3.png
root/cat/[...]/asd932_.png 
</code></pre>
<p>Thus, I am using ImageFolder class.</p>
<pre><code class="lang-auto">
transform = A.Compose([

    A.RandomCrop(width=256, height=256),

    A.HorizontalFlip(p=0.5),

    A.RandomBrightnessContrast(p=0.2),

])

data = ImageFolder(root='/content/flower_data/train',transform=transform)

loader = DataLoader(data)

for x, y in loader:

    print(x.shape) # image

    print(y)

    if y==1:break # image label

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
&lt;ipython-input-77-041b03c16ee0&gt; in &lt;module&gt;()
      8 data = ImageFolder(root='/content/flower_data/train',transform=transform)
      9 loader = DataLoader(data)
---&gt; 10 for x, y in loader:
     11     print(x.shape) # image
     12     print(y)

4 frames
/usr/local/lib/python3.7/dist-packages/torchvision/datasets/folder.py in __getitem__(self, index)
    178         sample = self.loader(path)
    179         if self.transform is not None:
--&gt; 180             sample = self.transform(sample)
    181         if self.target_transform is not None:
    182             target = self.target_transform(target)

TypeError: __call__() takes 1 positional argument but 2 were given ```</code></pre>

username: ptrblck
cooked: <p>The error seems quite strange, as it points towards the used transformation, which apparently doesn’t accept any additional input arguments.<br>
Could you load a single image and try to pass it directly to <code>transform</code> without the <code>ImageFolder</code> dataset?</p>

username: SK412
cooked: <p>Hi, Thanks for the reply <a class="mention" href="/u/ptrblck">@ptrblck</a>! Yes, I am able to run it on single image without any errors, can you suggest me any way to make it work with Pytorch ImageFolder because I will need to send images in batches during training, currently the code works for one image at an instance, my another concern is the code needs image path but when i use imagefolder I dont have any access to image path that i can provide to these transforms, one other way would be to write a custom dataset function where i store image path in a text file along with label and an index that can accesse the pathname of each image and pass it to transforms and make the code work,this is my backup approach. So at the moment i am looking to make it work with ImageFolder alone if possible. Sorry for the long paragraph.</p>
<p>EDIT:<br>
Additional Info. All my images are Jpegs and I am looking forward to use Densenets and similar CNN’s as my Model.</p>
<p>Thanks for taking your time to help the forum, your answers are very helpful. <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=9" title=":smile:" class="emoji" alt=":smile:"></p>
<pre><code class="lang-auto">image_path = '/content/flower_data/train/1/image_06734.jpg'

image = cv2.imread(image_path)

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Augment an image

transformed = transform(image=image)

transformed_image = transformed["image"]

plt.imshow(transformed['image'])

#plt.imshow(x.permute(1, 2, 0))```</code></pre>

username: SK412
cooked: <p>Update:<br>
Hi Again, So I went with the backup approach i.e. creating a text file with paths. I am sharing it here if others also end in a similar situation. Can someone tell me if there is any way to improve the efficiency. I am concerned with the fact that I have to open my text file each time i need to get a pic. So for each epoch it has to open (batch_size*number of batches)  times so in total number of samples * epochs, will this affect the computational time, if so how adversely. Can someone suggest any modification that can solve the problem? Thanks!</p>
<pre><code class="lang-auto">class Flowers(Dataset):

  def __init__(self,path,json_path=None,transform=None):

    self.path=path

    self.json_path=json_path

    self.transform=transform

    self.textmaker()


  def textmaker(self):

    f= open("train.txt","w+")

    for species in os.listdir(self.path):    

          spec_folder = os.path.join(self.path,species)

          for image in os.listdir(spec_folder):

            image_path=os.path.join(spec_folder,image)

            line=str(image_path)+','+str(species)+'\n'

            f.write(line)

    f.close()

  def __getitem__(self,idx):

    text = open("train.txt")

    lines=text.readlines()

    line=lines[idx]

    image_path=line.split(",")[0]

    label=line.split(",")[1][:-1]

    label=torch.tensor(int(label))

    image = cv2.imread(image_path)

    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    text.close()

    if self.transform:

      image=self.transform(image=image)

    return image,label

  def __len__(self):

    N=0

    for dirpath,dirnames,filenames in os.walk(self.path):

      n=len(filenames)

      N+=n

    return N
</code></pre>

username: ptrblck
cooked: <p>I’m not sure I understand the current issue correctly.<br>
Based on your description the transformations are working fine on a single sample.<br>
However, apparently you are seeing errors when using a <code>DataLoader</code>, which is strange, since the transformations would still be applied on a single sample in the default setup (i.e. without using a <code>BatchSampler</code> etc.).</p>
<aside class="quote no-group" data-username="SK412" data-post="3" data-topic="117158">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sk412/48/36343_2.png" class="avatar"> SK412:</div>
<blockquote>
<p>my another concern is the code needs image path but when i use imagefolder I dont have any access to image path that i can provide to these transforms</p>
</blockquote>
</aside>
<p>Which transformation would need an image path? Usually the transformations are applied on an image or tensor directly.</p>

username: SonSlider
cooked: <p>I will need to send images in batches during training, currently the code works for one image at an instance, my another concern is the code needs image path but when i use imagefolder I dont have any access to image path that i can provide to these transforms, one other way would be to write a custom dataset function where i store image path in a text file along with label and an index that can accesse the pathname of each image and pass it to transforms and make the code work,this is my backup approach. So at the moment i am looking to make it work with ImageFolder alone if possible. Sorry for the long paragraph<a href="https://www.upsers.app/" rel="noopener nofollow ugc">.</a></p>

username: SK412
cooked: <p>Hi!, I was able to find the issue and rectified it in my case, you are right the problem is not with pytorch transforms. Let me summarise it for your understanding.<br>
Lets say I create a ImageFolder, My data is strictly in the order PyTorch expects it t be in case of ImageFolder.</p>
<pre><code class="lang-auto">data = ImageFolder(root='/content/flower_data/train',transform=transform)
</code></pre>
<p>Here Transforms are from Albumentations Library,</p>
<pre><code class="lang-auto">transform = A.Compose([
    A.RandomCrop(width=256, height=256))],ToTensorV2()]) 
</code></pre>
<p>Then I end up with this issue,</p>
<p><code>TypeError: __call__() takes 1 positional argument but 2 were given </code></p>
<p>The Code works fine with pytorch transforms, when i create my own custom dataset class like i mentioned in one of my above answers, then the problem with albumntaions library is also solved . I checked their issues(Albumentations) on GitHub, turns out few more people had this issue, there was no conclusive solution, At the moment the solution is to create your own Dataset Function and then use their library, it is same like regular PyTorch transforms or use ImageFolder Dataset with PyTorch transforms. Hope this clarifies it. Cheers!</p>