How to combine transform and custom Dataset？

zeros: <p>I defined a custom Dataset as shown below：</p>
<pre><code class="lang-auto">class VideoQuality_torchResize(Dataset):
    def __init__(self,imagelist):
        with open(imagelist) as f:
            self.lines = f.readlines()
    
    def __len__(self):
        return len(self.lines)
    
    def __getitem__(self,index):
        if '\t' in self.lines[index]:
            imgPath,label = self.lines[index].split('\t')
        else:
            imgPath,label = self.lines[index].split(' ')

        label = int(label.split('\n')[0])
        img = cv2.imread(imgPath)[...,::-1] 
        return img,label,imgPath
</code></pre>
<p>and I define a transform as shown below:</p>
<pre><code class="lang-auto">trainVal_transform = transforms.Compose([
    transforms.Resize((224, 224)), 
    transforms.ToTensor(),  
    transforms.Normalize(mean, std)  
])
</code></pre>
<p>and I try to combine them as shown below：</p>
<pre><code class="lang-auto">train_dataset = VideoQuality_torchResize(trainlist,transform = trainVal_transform)
</code></pre>
<p>I change my image as a txt file list, in VideoQuality_torchResize, and the program tell me that：</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "videoquality_train_torchresize.py", line 41, in &lt;module&gt;
    train_dataset = VideoQuality_torchResize(trainlist,transform = trainVal_transform)
TypeError: __init__() got an unexpected keyword argument 'transform'
</code></pre>
<p>I know that  VideoQuality_torchResize class haven’t transorm params,So can you tell me how to slove this problem?</p>

nikhil6041: <pre><code class="lang-auto">
class VideoQuality_torchResize(Dataset):

    def __init__(self,imagelist,transform=None):

        with open(imagelist) as f:

            self.lines = f.readlines()

            self.transform = transform

          

    def __len__(self):

        return len(self.lines)

    

    def __getitem__(self,index):

        if '\t' in self.lines[index]:

            imgPath,label = self.lines[index].split('\t')

        else:

            imgPath,label = self.lines[index].split(' ')

        label = int(label.split('\n')[0])

        img = cv2.imread(imgPath)[...,::-1]

        if self.transform is not None:

            img = self.transform(img)

        return img,label,imgPath

</code></pre>
<p>You can add you transform in the customdataset class like this<br>
Then when you are creating your dataset its simply</p>
<blockquote>
<p>train_dataset = VideoQuality_torchResize(trainlist,transform = trainVal_transform)<br>
Hope this solves your problem</p>
</blockquote>

zeros: <p>put an error:</p>
<pre><code class="lang-auto">/python3.6/site-packages/torchvision/transforms/functional.py", line 239, in resize
    raise TypeError('img should be PIL Image. Got {}'.format(type(img)))
TypeError: img should be PIL Image. Got &lt;class 'numpy.ndarray'&gt;

</code></pre>
<p>I must use the opencv to read img, because Pillow and opencv is not equal when using read img or resize. please help me.</p>

Saurav_Gupta1: <p>You can also create your custom pre-processing pipeline using only openCV. You can also load images as PIL Image perform pre process using torchvision and convert the image back to openCV image if you want. Refer to the documentation of the libraries to set your parameters correctly.</p>