4-6-1-12. Classifying Fashion-MNIST

Welcome back. So, in this notebook,

you’ll be building your own neural network to classify clothing images.

So, like I talked about in the last video,

MNIST is actually a fairly trivial dataset these days.

It’s really easy to get really high accuracy with a neural network.

So instead, you’re going to be using Fashion-MNIST,

and this is basically just a drop-in replacement for MNIST

so we have 28 by 28 grayscale images,

but this time it’s clothing.

So, you have a lot more variation in the classes,

and it just ends up being

a much more difficult problem to classify like there’s a t-shirt,

there’s pants, there’s a sweater,

there’s shoes instead of handwritten digits.

So it’s a better representation of datasets that you’d use in the real world.

So, I’ve left this up to you to actually build a network and train it.

So here you can define your network architecture,

then here you will create your network to

define the criterion and optimizer and then write the code for the training pass.

Once you have your network built and trained,

you can test out your network.

So here, you’d want to do a forward pass, get your logits,

calculate the class probabilities, maybe output of your network,

and then pass in one of these images from

the test set and check out if your network can actually predict it correctly.

If you want to see my solution, it’s in the next notebook, part five,

and you’ll also see it in the next video. Cheers.

다시 오신 것을 환영합니다. 그래서 이 노트에는

옷 이미지를 분류하기 위해 자신만의 신경망을 구축할 것입니다.

그래서 지난 영상에서 말씀드린 것처럼

MNIST는 실제로 요즘 상당히 사소한 데이터 세트입니다.

신경망으로 매우 높은 정확도를 얻는 것은 정말 쉽습니다.

대신 Fashion-MNIST를 사용하게 됩니다.

이것은 기본적으로 MNIST에 대한 드롭인 교체입니다.

따라서 28 x 28 그레이스케일 이미지가 있습니다.

하지만 이번에는 옷이다.

그래서, 당신은 수업에 훨씬 더 많은 변화가 있습니다,

그리고 그것은 그냥 끝난다.

티셔츠가 있는 것처럼 분류하기가 훨씬 더 어려운 문제입니다.

바지도 있고 스웨터도 있고

손으로 쓴 숫자 대신 신발이 있습니다.

따라서 실제 세계에서 사용할 데이터 세트를 더 잘 표현합니다.

그래서, 저는 이것을 실제로 네트워크를 구축하고 훈련하는 것은 여러분에게 맡겼습니다.

여기에서 네트워크 아키텍처를 정의할 수 있습니다.

그런 다음 여기에서 네트워크를 생성하여

기준과 옵티마이저를 정의한 다음 훈련 패스에 대한 코드를 작성합니다.

네트워크를 구축하고 교육한 후에는

네트워크를 테스트할 수 있습니다.

여기에서 정방향 패스를 수행하고 로지트를 얻고,

클래스 확률 계산, 아마도 네트워크의 출력,

그런 다음 다음 이미지 중 하나를 전달합니다.

테스트 세트를 만들고 네트워크가 실제로 올바르게 예측할 수 있는지 확인하십시오.

내 솔루션을 보려면 다음 노트북, 파트 5에 있습니다.

다음 비디오에서도 볼 수 있습니다. 건배.

Classifying Fashion-MNIST

Now it’s your turn to build and train a neural network. You’ll be using the Fashion-MNIST dataset, a drop-in replacement for the MNIST dataset. MNIST is actually quite trivial with neural networks where you can easily achieve better than 97% accuracy. Fashion-MNIST is a set of 28×28 greyscale images of clothes. It’s more complex than MNIST, so it’s a better representation of the actual performance of your network, and a better representation of datasets you’ll use in the real world.

In this notebook, you’ll build your own neural network. For the most part, you could just copy and paste the code from Part 3, but you wouldn’t be learning. It’s important for you to write the code yourself and get it to work. Feel free to consult the previous notebooks though as you work through this.

First off, let’s load the dataset through torchvision.

Here we can see one of the images.

Building the network

Here you should define your network. As with MNIST, each image is 28×28 which is a total of 784 pixels, and there are 10 classes. You should include at least one hidden layer. We suggest you use ReLU activations for the layers and to return the logits or log-softmax from the forward pass. It’s up to you how many layers you add and the size of those layers.

# TODO: Define your network architecture here

Train the network

Now you should create your network and train it. First you’ll want to define the criterion ( something like nn.CrossEntropyLoss) and the optimizer (typically optim.SGD or optim.Adam).

Then write the training code. Remember the training pass is a fairly straightforward process:

  • Make a forward pass through the network to get the logits
  • Use the logits to calculate the loss
  • Perform a backward pass through the network with loss.backward() to calculate the gradients
  • Take a step with the optimizer to update the weights

By adjusting the hyperparameters (hidden units, learning rate, etc), you should be able to get the training loss below 0.4.

# TODO: Create the network, define the criterion and optimizer

# TODO: Train the network here
%matplotlib inline


import helper

# Test out your network!

dataiter = iter(testloader)
images, labels = dataiter.next()
img = images[0]
# Convert 2D image to 1D vector
img = img.resize_(1, 784)

# TODO: Calculate the class probabilities (softmax) for img
ps = 

# Plot the image and probabilities
helper.view_classify(img.resize_(1, 28, 28), ps, version='Fashion')

Full Code:

import torch
from torchvision import datasets, transforms
import helper

# Define a transform to normalize the data
transform = transforms.Compose([transforms.ToTensor(),
                                transforms.Normalize((0.5,), (0.5,))])
# Download and load the training data
trainset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)

# Download and load the test data
testset = datasets.FashionMNIST('~/.pytorch/F_MNIST_data/', download=True, train=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=True)

image, label = next(iter(trainloader))
helper.imshow(image[0,:]);



# TODO: Define your network architecture here



# TODO: Create the network, define the criterion and optimizer



# TODO: Train the network here



import helper

# Test out your network!

dataiter = iter(testloader)
images, labels = dataiter.next()
img = images[0]
# Convert 2D image to 1D vector
img = img.resize_(1, 784)




# TODO: Calculate the class probabilities (softmax) for img
ps = 




# Plot the image and probabilities
helper.view_classify(img.resize_(1, 28, 28), ps, version='Fashion')
%d 블로거가 이것을 좋아합니다: