TypeError in DataLoader worker process 0 (Due to my transforms?)

Taha_Iftikhar: <p>Hi, I’m re-training an inception_v3 using a remote GPU with CUDA device.<br>
I used these transforms for my dataset</p>
<blockquote>
<p>train_set = datasets.ImageFolder(<br>
root = “liG”,<br>
transform = transforms.Compose([transforms.ToTensor(),<br>
transforms.RandomRotation(20),<br>
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),<br>
transforms.Resize(299)]<br>
))<br>
data_loader= torch.utils.data.DataLoader(train_set,<br>
batch_size=5,<br>
shuffle=True,<br>
num_workers=2)<br>
Below is the cell that raises the error:<br>
for X, y in data_loader:<br>
print("Shape of X [N, C, H, W]: ", X.shape)<br>
print("Shape of y: ", y.shape, y.dtype)<br>
break</p>
</blockquote>
<p>The error I’m getting is:</p>
<blockquote>
<p>TypeError: Caught TypeError in DataLoader worker process 0.<br>
Original Traceback (most recent call last):<br>
File “/opt/conda/lib/python3.8/site-packages/torch/utils/data/_utils/worker.py”, line 202, in _worker_loop<br>
data = fetcher.fetch(index)<br>
File “/opt/conda/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py”, line 44, in fetch<br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “/opt/conda/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py”, line 44, in <br>
data = [self.dataset[idx] for idx in possibly_batched_index]<br>
File “/opt/conda/lib/python3.8/site-packages/torchvision/datasets/folder.py”, line 171, in <strong>getitem</strong><br>
sample = self.transform(sample)<br>
File “/opt/conda/lib/python3.8/site-packages/torchvision/transforms/transforms.py”, line 60, in <strong>call</strong><br>
img = t(img)<br>
File “/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py”, line 881, in _call_impl<br>
result = self.forward(*input, **kwargs)<br>
File “/opt/conda/lib/python3.8/site-packages/torchvision/transforms/transforms.py”, line 1236, in forward<br>
fill = [float(f) for f in fill]<br>
TypeError: ‘NoneType’ object is not iterable</p>
</blockquote>

ptrblck: <p>Based on the error message it seems that <code>fill</code> is set to <code>None</code> in <a href="https://github.com/pytorch/vision/blob/512ea299d4b2d2bbac3498a75a2d8c0190cfcb39/torchvision/transforms/transforms.py#L1269">this line of code</a>, which shouldn’t be the case, as it’s checked <a href="https://github.com/pytorch/vision/blob/512ea299d4b2d2bbac3498a75a2d8c0190cfcb39/torchvision/transforms/transforms.py#L1241-L1242">here</a>.</p>
<p>I also cannot reproduce the issue using this code snippet:</p>
<pre><code class="lang-python">transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.RandomRotation(20),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
    transforms.Resize(299)
])

img = transforms.ToPILImage()(torch.randn(3, 224, 224))
out = transform(img)
</code></pre>
<p>Which PyTorch and <code>torchvision</code> versions are you using and are you seeing the same issue using my code?</p>

Sushmanth_reddy_Mere: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> <code>Caught TypeError in DataLoader worker process 0. Original Traceback (most recent call last):   File "/usr/local/lib/python3.8/dist-packages/torch/utils/data/_utils/worker.py", line 302, in _worker_loop     data = fetcher.fetch(index)   File "/usr/local/lib/python3.8/dist-packages/torch/utils/data/_utils/fetch.py", line 58, in fetch     data = [self.dataset[idx] for idx in possibly_batched_index]   File "/usr/local/lib/python3.8/dist-packages/torch/utils/data/_utils/fetch.py", line 58, in &lt;listcomp&gt;     data = [self.dataset[idx] for idx in possibly_batched_index]   File "/usr/local/lib/python3.8/dist-packages/torchvision/datasets/folder.py", line 229, in __getitem__     sample = self.loader(path)   File "/usr/local/lib/python3.8/dist-packages/torchvision/transforms/transforms.py", line 95, in __call__     img = t(img)   File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1194, in _call_impl     return forward_call(*input, **kwargs)   File "/usr/local/lib/python3.8/dist-packages/torchvision/transforms/transforms.py", line 661, in forward     img = F.pad(img, self.padding, self.fill, self.padding_mode)   File "/usr/local/lib/python3.8/dist-packages/torchvision/transforms/functional.py", line 523, in pad     return F_pil.pad(img, padding=padding, fill=fill, padding_mode=padding_mode)   File "/usr/local/lib/python3.8/dist-packages/torchvision/transforms/functional_pil.py", line 154, in pad     raise TypeError(f"img should be PIL Image. Got {type(img)}") TypeError: img should be PIL Image. Got &lt;class 'str'&gt;</code></p>
<p>this was the error I was facing ,Sam training my model on .npy files, can u pls help me out?</p>

ptrblck: <p>It seems you are passing a <code>str</code> to your transformation while a <code>PIL.Image</code> (or tensor) is expected.</p>

Sushmanth_reddy_Mere: <pre><code class="lang-auto">@torch.no_grad()
def evaluate(model, val_loader):
    model.eval()
    outputs = [model.validation_step(batch) for batch in val_loader]
    return model.validation_epoch_end(outputs)

def get_lr(optimizer):
    for param_group in optimizer.param_groups:
        return param_group['lr']

def fit_one_cycle(epochs, max_lr, model, train_loader, val_loader, 
                  weight_decay=0, grad_clip=None, opt_func=torch.optim.SGD):
    torch.cuda.empty_cache()
    history = []
    
    # Set up cutom optimizer with weight decay
    optimizer = opt_func(model.parameters(), max_lr, weight_decay=weight_decay)
    # Set up one-cycle learning rate scheduler
    sched = torch.optim.lr_scheduler.OneCycleLR(optimizer, max_lr, epochs=epochs, 
                                                steps_per_epoch=len(train_loader))
    
    for epoch in range(epochs):
        # Training Phase 
        model.train()
        train_losses = []
        lrs = []
        for batch in train_loader:
            loss = model.training_step(batch)
            train_losses.append(loss)
            loss.backward()
            
            # Gradient clipping
            if grad_clip: 
                nn.utils.clip_grad_value_(model.parameters(), grad_clip)
            
            optimizer.step()
            optimizer.zero_grad()
            
            # Record &amp; update learning rate
            lrs.append(get_lr(optimizer))
            sched.step()
        
        # Validation phase
        result = evaluate(model, val_loader)
        result['train_loss'] = torch.stack(train_losses).mean().item()
        result['lrs'] = lrs
        model.epoch_end(epoch, result)
        history.append(result)
    return history
</code></pre>
<p>I have been handling with .npy files …I don’t knew where I was going wrong <a class="mention" href="/u/ptrblck">@ptrblck</a> pls help me out</p>

Sushmanth_reddy_Mere: <pre><code class="lang-auto"># Data transforms (normalization &amp; data augmentation)
stats = ((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010))
train_tfms = tt.Compose([
                         tt.RandomHorizontalFlip(), 
                         tt.ToPILImage(),
                         # tt.RandomResizedCrop(256, scale=(0.5,0.9), ratio=(1, 1)), 
                         tt.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1),
                         tt.ToTensor(), 
                         tt.Normalize(*stats,inplace=True)])
valid_tfms = tt.Compose([tt.ToTensor(), tt.Normalize(*stats)])

</code></pre>

Sushmanth_reddy_Mere: <p>how can I tackle this problem?</p>

ptrblck: <p>Check the inputs to your transformation and make sure to pass valid objects as <code>PIL.Image</code>s or tensors instead of strings.</p>

Ahmed_mostafa: <p>— Epoch 0 — — Phase train —</p>
<hr>
<p>TypeError Traceback (most recent call last)</p>
<p><a href="https://fu8f9spxhbp-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab_20231207-060210_RC00_588724798#" rel="noopener nofollow ugc"></a> in &lt;cell line: 8&gt;() 21 data_loader = test_loader 22 —&gt; 23 for batch_i, (X, y) in enumerate(dataloaders[phase]): 24 image_sequences = Variable(X.to(device), requires_grad=True) 25 labels = Variable(y.to(device), requires_grad=False)</p>
<hr>
<h2><a name="h-3-frames-1" class="anchor" href="#h-3-frames-1"></a>3 frames</h2>
<p><a href="https://fu8f9spxhbp-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab_20231207-060210_RC00_588724798#" rel="noopener nofollow ugc">/usr/local/lib/python3.10/dist-packages/torch/_utils.py</a> in reraise(self) 692 # instantiate since we don’t know how to 693 raise RuntimeError(msg) from None → 694 raise exception 695 696</p>
<p>TypeError: Caught TypeError in DataLoader worker process 0. Original Traceback (most recent call last): File “/usr/local/lib/python3.10/dist-packages/torch/utils/data/_utils/worker.py”, line 308, in _worker_loop data = fetcher.fetch(index) File “/usr/local/lib/python3.10/dist-packages/torch/utils/data/_utils/fetch.py”, line 51, in fetch data = [self.dataset[idx] for idx in possibly_batched_index] File “/usr/local/lib/python3.10/dist-packages/torch/utils/data/_utils/fetch.py”, line 51, in  data = [self.dataset[idx] for idx in possibly_batched_index] File “”, line 21, in <strong>getitem</strong> img1 = img[:,128<em>i:128</em>(i+1),:] TypeError: ‘NoneType’ object is not subscriptable</p>

ptrblck: <p>It seems <code>img</code> in <code>__getitem__</code> is <code>None</code> so check the dataset implementation to see why that’s the case.</p>