[Python] Traceback (most recent call last) in Colab when looping through dataloader in pytorch

Discussão em 'Python' iniciado por Stack, Janeiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a project to classify flower images using a pre-trained model vgg19 using pytorch.

    I'm relying on the model features only and using a custom classifier.

    However on starting a for-loop to feed images to the model classifier and calculate accuracy through epochs I get an error.

    I'm not sure what's the problem as the error is a traceback (most recent call last)

    Below is my notebook.

    The cell throwing the error is below

    #training the classifier
    criterion = nn.CrossEntropyLoss()
    optimizer = optim.SGD(model.classifier.parameters(),lr=0.01)

    steps = 0
    running_loss = 0
    epochs = 5
    print_every = 5

    for epoch in range(epochs):
    for images,labels in train_dataloader:
    steps += 1
    optimizer.zero_grad()

    logps = model.forward(images)
    loss = criterion(logps,labels)
    loss.backward()
    optimizer.step()

    running_loss += loss.item()

    if steps % print_every == 0:
    test_loss = 0
    accuracy = 0
    model.eval()

    with torch.no_grad():
    for images, labels in valid_dataloader:
    logps = model.forward(images)
    batch_loss = criterion(logps, labels)

    test_loss += batch_loss.item()

    #Calculate accuracy
    ps = torch.exp(logps)
    top_p, top_class = ps.topk(5,dim=1)
    equals = top_class == labels.view(*top_class.shape)
    accuracy += torch.mean(equals.type(torch.FloatTensor)).item()

    print(f"Epoch {epoch+1}/{epochs}.."
    f"Train loss: {running_loss/print_every: .3f}.."
    f"Test loss: {test_loss/len(valid_loader):.3f}.."
    f"Test accuracy: {accuracy/len(valid_loader):.3f}")
    running_loss = 0
    model.train()


    The error I get on running the notebook

    AttributeError Traceback (most recent call last)
    <ipython-input-11-c218f8f2b72e> in <module>()
    8
    9 for epoch in range(epochs):
    ---> 10 for images,labels in train_dataloader:
    11 steps += 1
    12 optimizer.zero_grad()

    /usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in __next__(self)
    312 if self.num_workers == 0: # same-process loading
    313 indices = next(self.sample_iter) # may raise StopIteration
    --> 314 batch = self.collate_fn([self.dataset for i in indices])
    315 if self.pin_memory:
    316 batch = pin_memory_batch(batch)

    /usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py in <listcomp>(.0)
    312 if self.num_workers == 0: # same-process loading
    313 indices = next(self.sample_iter) # may raise StopIteration
    --> 314 batch = self.collate_fn([self.dataset for i in indices])
    315 if self.pin_memory:
    316 batch = pin_memory_batch(batch)

    /usr/local/lib/python3.6/dist-packages/torchvision/datasets/folder.py in __getitem__(self, index)
    99 """
    100 path, target = self.samples[index]
    --> 101 sample = self.loader(path)
    102 if self.transform is not None:
    103 sample = self.transform(sample)

    /usr/local/lib/python3.6/dist-packages/torchvision/datasets/folder.py in default_loader(path)
    145 return accimage_loader(path)
    146 else:
    --> 147 return pil_loader(path)
    148
    149

    /usr/local/lib/python3.6/dist-packages/torchvision/datasets/folder.py in pil_loader(path)
    127 # open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
    128 with open(path, 'rb') as f:
    --> 129 img = Image.open(f)
    130 return img.convert('RGB')
    131

    /usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
    2319 return True
    2320
    -> 2321
    2322 def new(mode, size, color=0):
    2323 """

    /usr/local/lib/python3.6/dist-packages/PIL/Image.py in preinit()
    368
    369
    --> 370 def preinit():
    371 """Explicitly load standard file format drivers."""
    372

    /usr/local/lib/python3.6/dist-packages/PIL/PpmImagePlugin.py in <module>()
    156 Image.register_save(PpmImageFile.format, _save)
    157
    --> 158 Image.register_extensions(PpmImageFile.format, [".pbm", ".pgm", ".ppm"])

    AttributeError: module 'PIL.Image' has no attribute 'register_extensions'

    Continue reading...

Compartilhe esta Página