6

I am getting my hands dirty with Pytorch and I am trying to do what is apparently the hardest part in deep learning-> LOADING MY CUSTOM DATASET AND RUNNING THE PROGRAM<-- The problem is this " too many values to unpack (expected 2)" also I think I am loading the data wrong. Can someone please show me how to do this. How to use the Dataloader user one's own data.

import os
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import torch.utils.data as data
import torchvision
from torchvision import transforms

# Hyper parameters
num_epochs = 20
batchsize = 100
lr = 0.001

EPOCHS = 2
BATCH_SIZE = 10
LEARNING_RATE = 0.003
TRAIN_DATA_PATH = "ImageFolder/images/train/"
TEST_DATA_PATH = "ImageFolder/images/test/"
TRANSFORM_IMG = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(256),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406],
                         std=[0.229, 0.224, 0.225] )
    ])

train_data = torchvision.datasets.ImageFolder(root=TRAIN_DATA_PATH, transform=TRANSFORM_IMG)
train_data_loader = data.DataLoader(train_data, batch_size=BATCH_SIZE, shuffle=True,  num_workers=4)
test_data = torchvision.datasets.ImageFolder(root=TEST_DATA_PATH, transform=TRANSFORM_IMG)
test_data_loader  = data.DataLoader(test_data, batch_size=BATCH_SIZE, shuffle=True, num_workers=4) 

# Data Loader (Input Pipeline)
train_loader = torch.utils.data.DataLoader(dataset=TRAIN_DATA_PATH,
                                           batch_size=batchsize,
                                           shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=TEST_DATA_PATH,
                                          batch_size=batchsize,
                                          shuffle=False)

# CNN model
class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conlayer1 = nn.Sequential(
            nn.Conv2d(1,6,3),
            nn.Sigmoid(),
            nn.MaxPool2d(2))
        self.conlayer2 = nn.Sequential(
            nn.Conv2d(6,16,3),
            nn.Sigmoid(),
            nn.MaxPool2d(2))
        self.fc = nn.Sequential(
            nn.Linear(400,120),
            nn.Relu(),
            nn.Linear(120,84),
            nn.Relu(),
            nn.Linear(84,10))

    def forward(self, x):
        out = self.conlayer1(x)
        out = self.conlayer2(out)
        out = out.view(out.size(0),-1)
        out = self.fc(out)
        return out


cnn = CNN()

# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(cnn.parameters(), lr=lr)

# Train the Model
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        # Forward + Backward + Optimize
        optimizer.zero_grad()
        outputs = cnn(images)
        loss = criterion(outputs,labels)
        loss.backward()
        optimizer.step()

        if (i+1)%100 == 0:
            print('Epoch [%d/%d], Iter [%d/%d] Loss: %.4f' %
                  (epoch+1,num_epochs,i+1,len(train_dataset)//batchsize,loss.data[0]))

# Test the Model
cnn.eval()  # Change model to 'eval' mode (BN uses moving mean/var)
correct = 0
total = 0
for images, labels in test_loader:
    outputs = cnn(images)
    _, predicted = torch.max(outputs.data, 1)
    total += labels.size(0)
    correct += (predicted == labels).sum()
print('Test Accuracy of the model on test images: %.6f%%' % (100.0*correct/total))

#Save the Trained Model
torch.save(cnn.state_dict(),'cnn.pkl')
5
  • where do you get the error? Commented Jan 27, 2020 at 1:41
  • Always share the entire message. Do you have a minimal reproducible example? Commented Jan 27, 2020 at 4:24
  • Sorry, I got this error "module 'torch.nn' has no attribute 'ReLu'" Commented Jan 27, 2020 at 5:27
  • That’s because it’s called ReLU. Mind the capitalization. But that’s not the error from your original question. Commented Jan 27, 2020 at 6:11
  • @AryaMcCarthy You are right thanks ReLU did it. My error now "Given groups=1, weight of size 6 1 3 3, expected input[12, 3, 256, 256] to have 1 channels, but got 3 channels instead" Where is this coming from. Commented Jan 27, 2020 at 17:59

1 Answer 1

4

You just provided a string to dataloader:

dataset=TRAIN_DATA_PATH

Maybe use train_data, which is a data generator based on the filepath you provided

Sign up to request clarification or add additional context in comments.

6 Comments

Also need to change to nn.ReLU instead of nn.Relu ;).
I get this error -> module 'torch.nn' has no attribute 'ReLu'
@Toyo module 'torch.nn' has no attribute 'ReLu'
torch.nn.ReLU() with a big U
change nn.Conv2d(1,6,3) for nn.Conv2d(3,6,3)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.