Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chapter 2 has unknown mistake, please help me #75

Open
hero-117 opened this issue Mar 12, 2022 · 6 comments
Open

chapter 2 has unknown mistake, please help me #75

hero-117 opened this issue Mar 12, 2022 · 6 comments

Comments

@hero-117
Copy link

I copyed the code to pycharm, but it can not run, here are the code:

import torch
import torch.nn as nn
import torch.optim as optim
import torch.utils.data
import torch.nn.functional as F
import torchvision
from torchvision import transforms
from PIL import Image, ImageFile

ImageFile.LOAD_TRUNCATED_IMAGES = True

def check_image(path):

try:

im = Image.open(path)

return True

except:

return False

check_image = True

img_transforms = transforms.Compose([
transforms.Resize((64, 64)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])

train_data_path = "./train/"
train_data = torchvision.datasets.ImageFolder(root=train_data_path, transform=img_transforms)
val_data_path = "./val/"
val_data = torchvision.datasets.ImageFolder(root=val_data_path, transform=img_transforms)
test_data_path = "./test/"
test_data = torchvision.datasets.ImageFolder(root=test_data_path, transform=img_transforms)

batch_size = 64

train_data_loader = torch.utils.data.DataLoader(train_data, batch_size=batch_size)
val_data_loader = torch.utils.data.DataLoader(val_data, batch_size=batch_size)
test_data_loader = torch.utils.data.DataLoader(test_data, batch_size=batch_size)

class SimpleNet(nn.Module):

def __init__(self):
    super(SimpleNet, self).__init__()
    self.fc1 = nn.Linear(12288, 84)
    self.fc2 = nn.Linear(84, 50)
    self.fc3 = nn.Linear(50, 2)

def forward(self, x):
    x = x.view(-1, 12288)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x

simplenet = SimpleNet()

optimizer = optim.Adam(simplenet.parameters(), lr=0.001)

if torch.cuda.is_available():
device = torch.device("cuda")
else:
device = torch.device("cpu")

simplenet.to(device)

def train(model, optimizer, loss_fn, train_loader, val_loader, epochs, device):
for epoch in range(1, epochs + 1):
training_loss = 0.0
valid_loss = 0.0
model.train()
for batch in train_loader:
optimizer.zero_grad()
inputs, targets = batch
inputs = inputs.to(device)
targets = targets.to(device)
output = model(inputs)
loss = loss_fn(output, targets)
loss.backward()
optimizer.step()
training_loss += loss.data.item() * inputs.size(0)
training_loss /= len(train_loader.dataset)

    model.eval()
    num_correct = 0
    num_examples = 0
    for batch in val_loader:
        inputs, targets = batch
        inputs = inputs.to(device)
        output = model(inputs)
        targets = targets.to(device)
        loss = loss_fn(output, targets)
        valid_loss += loss.data.item() * inputs.size(0)
        correct = torch.eq(torch.max(F.softmax(output, dim=1), dim=1)[1], targets)
        num_correct += torch.sum(correct).item()
        num_examples += correct.shape[0]
    valid_loss /= len(val_loader.dataset)

    print(
        'Epoch: {}, Training Loss: {:.2f}, Validation Loss: {:.2f}, accuracy = {:.2f}'.format(epoch, training_loss,
                                                                                              valid_loss,
                                                                                              num_correct / num_examples))

epoch = 5

train(simplenet, optimizer, torch.nn.CrossEntropyLoss(), train_data_loader, val_data_loader, epoch, device)


mistake information:

Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\disk_file\DL\1\5.py", line 110, in
train(simplenet, optimizer, torch.nn.CrossEntropyLoss(), train_data_loader, val_data_loader, epoch, device)
File "C:\Users\Administrator\Desktop\disk_file\DL\1\5.py", line 75, in train
for batch in train_loader:
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\torch\utils\data\dataloader.py", line 521, in next
data = self._next_data()
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\torch\utils\data\dataloader.py", line 561, in _next_data
data = self._dataset_fetcher.fetch(index) # may raise StopIteration
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\torch\utils\data_utils\fetch.py", line 49, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\torch\utils\data_utils\fetch.py", line 49, in
data = [self.dataset[idx] for idx in possibly_batched_index]
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\torchvision\datasets\folder.py", line 232, in getitem
sample = self.loader(path)
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\torchvision\datasets\folder.py", line 269, in default_loader
return pil_loader(path)
File "C:\Users\Administrator\AppData\Roaming\Python\Python39\site-packages\torchvision\datasets\folder.py", line 250, in pil_loader
img = Image.open(f)
File "C:\Program Files\1\lib\site-packages\PIL\Image.py", line 3030, in open
raise UnidentifiedImageError(
PIL.UnidentifiedImageError: cannot identify image file <_io.BufferedReader name='./train/cat\1004525_cba96ba3c3.jpg'>

@MarcusFra
Copy link
Contributor

MarcusFra commented Mar 12, 2022

Hey, it's an PIL.UnidentifiedImageError – the image cannot be opened and identified.

  1. Are you able to open the image ./train/cat\1004525_cba96ba3c3.jpg on your working station? Is it a real picture/valid file? Edit: The backslash after cat seems a bit weird to me: the images with the cats should be in an own directory called cat, I think it needs to be a slash instead of the backslash.
  2. How have you acquired the image files? Have you used the download.py file or the gdrive link?

@hero-117
Copy link
Author

Hey, it's an PIL.UnidentifiedImageError – the image cannot be opened and identified.

  1. Are you able to open the image ./train/cat\1004525_cba96ba3c3.jpg on your working station? Is it a real picture/valid file? Edit: The backslash after cat seems a bit weird to me: the images with the cats should be in an own directory called cat, I think it needs to be a slash instead of the backslash.
  2. How have you acquired the image files? Have you used the download.py file or the gdrive link?

_
thanks for your help!

I download this image package through G Drive, after your reminder, i check the package on my PC and found '1004525_cba96ba3c3.jpg' has broken, does it means i must use check_image()?

@MarcusFra
Copy link
Contributor

Yes, please adjust your code for the three respective lines and add , is_valid_file=check_image. The check_image() function is included to avoid these kind of error messages and the broken images not being considered.

For example for the training data use the ImageFolder class as follows (see https://github.com/falloutdurham/beginners-pytorch-deep-learning/blob/master/chapter2/Chapter%202.ipynb):

train_data = torchvision.datasets.ImageFolder(root=train_data_path,
                                              transform=img_transforms,
                                              is_valid_file=check_image)

The same for val_data and test_data.

Is there any reason you deleted it in the code you forked from the gihub repo?

@MarcusFra
Copy link
Contributor

If you got an FileNotFoundError with the check_image() function please check out #71.

@hero-117
Copy link
Author

Yes, please adjust your code for the three respective lines and add , is_valid_file=check_image. The check_image() function is included to avoid these kind of error messages and the broken images not being considered.

For example for the training data use the ImageFolder class as follows (see https://github.com/falloutdurham/beginners-pytorch-deep-learning/blob/master/chapter2/Chapter%202.ipynb):

train_data = torchvision.datasets.ImageFolder(root=train_data_path,
                                              transform=img_transforms,
                                              is_valid_file=check_image)

The same for val_data and test_data.

Is there any reason you deleted it in the code you forked from the gihub repo?

Thanks for your patient!
The reason I delete 'img_check()' is I noticed the bug in torchvision you mentioned in another issue, it seems that the function is important for both download by Url and GDrive. Or we can update a new image package to GDrive.

@MarcusFra
Copy link
Contributor

Yes, unfortunately also some GDrive images seem to be affected, but there is no issue at all as long it's possible to include the check_image() function.

I think it's best to downgrade torchvision to 0.10.1 as mentionend in #71 - or you can upgrade to 0.12.0 (released 2 days ago; the bug mentioned in #71 has been fixed in the release - but be aware that the new release has not been tested for this book repo yet).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants