udacity / aipnd-project Goto Github PK
View Code? Open in Web Editor NEWProject code for AI Programming with Python ND Program
License: MIT License
Project code for AI Programming with Python ND Program
License: MIT License
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:
%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'
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])])
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)
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.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
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.