GithubHelp home page GithubHelp logo

aipnd-project's Issues

RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed. at /opt/conda/conda-bld/pytorch_1512386481460/work/torch/lib/THNN/generic/ClassNLLCriterion.c:87

Hi,

I am getting the erroer "!RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed. at /opt/conda/conda-bld/pytorch_1512386481460/work/torch/lib/THNN/generic/ClassNLLCriterion.c:87" while training my classifier. I think there is something wrong in the way I create the classifier, but I coulnd't find any issues compared to the classifier we used in class. I posted my code below:

Imports here

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

import matplotlib.pyplot as plt
import torch
from torch import nn
from torch import optim
import torch.nn.functional as F
from torch.autograd import Variable
from torchvision import datasets, transforms, models

import numpy as np
import time

#import helper

#data direction
data_dir = 'flowers'
train_dir = data_dir + '/train'
valid_dir = data_dir + '/valid'
test_dir = data_dir + '/test'

TODO: Define your transforms for the training, validation, and testing sets

data_transforms_training = transforms.Compose([transforms.RandomRotation(25),
transforms.RandomHorizontalFlip(),
transforms.RandomResizedCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])

data_transforms_validation = transforms.Compose([transforms.RandomResizedCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])

data_transforms_testing = transforms.Compose([transforms.RandomResizedCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])

TODO: Load the datasets with ImageFolder

image_datasets_training = datasets.ImageFolder(train_dir, transform=data_transforms_training)
image_datasets_validation = datasets.ImageFolder(valid_dir, transform=data_transforms_validation)
image_datasets_testing = datasets.ImageFolder(test_dir, transform=data_transforms_testing)

TODO: Using the image datasets and the trainforms, define the dataloaders

trainloader = torch.utils.data.DataLoader(image_datasets_training, batch_size=32,shuffle=True)
validationloader = torch.utils.data.DataLoader(image_datasets_validation, batch_size=32)
testloader = torch.utils.data.DataLoader(image_datasets_testing, batch_size=32)

import json

with open('cat_to_name.json', 'r') as f:
cat_to_name = json.load(f)

#Step1: Loading VGG16/VGG19/densenet121 Model
model = models.vgg16(pretrained=True)
model

#Freeze params and create new classifier
for param in model.parameters():
param.requires_grad = False

from collections import OrderedDict
classifier = nn.Sequential(OrderedDict([
('fc1', nn.Linear(25088,500)),
('relu', nn.ReLU()),
('fc2', nn.Linear(500,2)),
('output', nn.LogSoftmax(dim=1))
]))
model.classifier = classifier

#Train classifier

for cuda in [False, True]:
epochs = 2
steps = 0
for epochs in range(epochs):
criterion = nn.NLLLoss()
# Only train the classifier parameters, feature parameters are frozen
optimizer = optim.Adam(model.classifier.parameters(), lr=0.001)

    if cuda:
        # Move model parameters to the GPU
        model.cuda()
    else:
        model.cpu()

    for ii, (inputs, labels) in enumerate(trainloader):
        inputs, labels = Variable(inputs), Variable(labels)
        steps+=1
        if cuda:
            # Move input and label tensors to the GPU
            inputs, labels = inputs.cuda(), labels.cuda()
        print(inputs)
        print(labels)
        optimizer.zero_grad()
        #outputs = model.forward(inputs)
        outputs = model.forward(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        
        running_loss += loss.data[0]
    
        if steps % print_every == 0:
            print("Epoch: {}/{}... ".format(e+1, epochs),
              "Loss: {:.4f}".format(running_loss/print_every))
        
            running_loss = 0
        if ii==3:
            break

#Model Evaluation

Model in inference mode, dropout is off

model.eval()

accuracy = 0
test_loss = 0
for ii, (images, labels) in enumerate(validationloader):

#images = images.resize_(images.size()[0], 784)
# Set volatile to True so we don't save the history
inputs = Variable(images, volatile=True)
labels = Variable(labels, volatile=True)

output = model.forward(inputs)
test_loss += criterion(output, labels).data[0]

## Calculating the accuracy 
# Model's output is log-softmax, take exponential to get the probabilities
ps = torch.exp(output).data
# Class with highest probability is our predicted class, compare with true label
equality = (labels.data == ps.max(1)[1])
# Accuracy is number of correct predictions divided by all predictions, just take the mean
accuracy += equality.type_as(torch.FloatTensor()).mean()

print("Epoch: {}/{}.. ".format(e+1, epochs),
"Training Loss: {:.3f}.. ".format(running_loss/print_every),
"Test Loss: {:.3f}.. ".format(test_loss/len(testloader)),
"Test Accuracy: {:.3f}".format(accuracy/len(testloader)))

running_loss = 0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.