-
Notifications
You must be signed in to change notification settings - Fork 271
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
Comments
Hey, it's an PIL.UnidentifiedImageError – the image cannot be opened and identified.
|
_ 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()? |
Yes, please adjust your code for the three respective lines and add , is_valid_file=check_image. The 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):
The same for Is there any reason you deleted it in the code you forked from the gihub repo? |
If you got an |
Thanks for your patient! |
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 I think it's best to downgrade |
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):
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)
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'>
The text was updated successfully, but these errors were encountered: