import torch
import os
import torch.nn as nn
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
import argparse
# os.environ['CUDA_VISIBLE_DEVICES'] = "4"
parser = argparse.ArgumentParser(description='cnn_mnist')
# learning & saving hyper parameterss
parser.add_argument('-train', default = False, help = 'train the model')
parser.add_argument('-test', default = True, help = 'test the model')
parser.add_argument('-learning_rate', type = float, default = 0.001, help = 'initial learning rate [default = 0.005')
parser.add_argument('-num_epochs', type = int, default = 5, help = 'number of epochs of training [default = 10')
parser.add_argument('-batch_size', type = int, default = 100, help = 'batch size for training')
parser.add_argument('-input_size', type = int, default = 784, help = 'input size')
parser.add_argument('-hidden_size', type = int, default = 500, help = 'hidden size')
parser.add_argument('-output_size',type = int, default = 1, help = 'output size')
parser.add_argument('-num_classes', type = int, default = 10, help = 'hidden layer number')
parser.add_argument('-cuda',default = False, help = 'enable gpu')
args = parser.parse_args()
# Hyper Parameters
input_size = args.input_size
hidden_size = args.hidden_size
num_classes = args.num_classes
num_epochs = args.num_epochs
batch_size = args.batch_size
learning_rate = args.learning_rate
# MNIST Dataset
train_dataset = dsets.MNIST(root='./data/',
train=True,
transform=transforms.ToTensor(),
download=False)
test_dataset = dsets.MNIST(root='./data/',
train=False,
transform=transforms.ToTensor())
# Data Loader (Input Pipeline)
train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=batch_size,
shuffle=False)
# CNN Model (2 conv layer)
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
# 100 x 1 x 28 x 28
self.layer1 = nn.Sequential(
nn.Conv2d(1, 16, kernel_size=5, padding=2),
# class torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(2))
# 100 x 16 x 14 x 14
self.layer2 = nn.Sequential(
nn.Conv2d(16, 32, kernel_size=5, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2))
# 100 x 32 x 7 x 7
# 100 x 1568
self.fc = nn.Linear(7*7*32, 10)
# 10 catagory
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = out.view(out.size(0), -1) # view means reshape
# 100 x 32 x 7 x 7 ==> 100 x 1568
out = self.fc(out)
return out
cnn = CNN()
if args.train:
# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate)
# Train the Model
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = Variable(images)
labels = Variable(labels)
# 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)//batch_size, loss.data[0]))
# Save the Trained Model
torch.save(cnn.state_dict(), 'cnn.pkl')
if args.test:
cnn.load_state_dict(torch.load('cnn.pkl'))
# Test Accuracy of the model on the 10000 test images: 99.05 %
# [Finished in 4.8s]
# Test the Model
cnn.eval() # Change model to 'eval' mode (BN uses moving mean/var).
correct = 0.0
total = 0.0
for images, labels in test_loader:
# image size is 100x1x28x28
images = Variable(images)
outputs = cnn(images) # 100 x 10
_, predicted = torch.max(outputs.data, 1) # predicted.size() = 100 x 1
"""
'predicted' is the second return
value of torch.max function, which is the
maximum value of found(argmax)
"""
"""
outputs.data is the distribution of the 10 outputs,
each is a vote of probability,
we want the highest value's index
"""
total += labels.size(0) # 100
correct += (predicted == labels).sum()
# print(outputs.size())
x = outputs.data
print(x)
# print(predicted)
# print(labels.size())
break;
print('Test Accuracy of the model on the 10000 test images: %.2lf %%' % (100.0 * correct / total))
CNN
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- RCNN 过程 1.利用sleactive search提取出图片中大约2000个候选区域2.将候选区域的的大小利...
- 1. Intro 重构后的代码放在github,另附io博文地址 传统的句子分类器一般使用SVM和Naive Ba...
- Step1:原始图像的预处理 利用selective search对原始图像取得2000个目标候选区域,得到的候选...
- R-CNN 物体检测第二弹(Fast R-CNN) 今天,重看了 R-CNN 的后续改进 Fast R-C...