Skip to content

Commit

Permalink
Update 0130, add CIFAR and kaggle-dog-cat.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhang Yuan committed Jan 30, 2018
1 parent 59fdb83 commit a4ab02a
Show file tree
Hide file tree
Showing 24 changed files with 469 additions and 1,968 deletions.
4 changes: 4 additions & 0 deletions CIFAR/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
lr = 0.1
resume = False
start_epoch = 0
epochs = 60
85 changes: 48 additions & 37 deletions CIFAR/main.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
'''Train CIFAR10 with PyTorch.'''
from __future__ import print_function
import os

import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import torch.nn.functional as F
#import torch.backends.cudnn as cudnn
import torch.backends.cudnn as cudnn

import torchvision
import torchvision.transforms as transforms

import os
import argparse

import config
from models import *
from utils import progress_bar
from torch.autograd import Variable


parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
parser.add_argument('--lr', default=0.1, type=float, help='learning rate')
parser.add_argument('--resume', '-r', action='store_true', help='resume from checkpoint')
args = parser.parse_args()
from utils import *

use_cuda = torch.cuda.is_available()
best_acc = 0 # best test accuracy
best_acc = 0 # best val accuracy
best_epoch = 0
start_epoch = 0 # start from epoch 0 or last checkpoint epoch

# Data
Expand All @@ -36,43 +30,43 @@
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=False, transform=transform)
trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128, shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=False, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, num_workers=2)
valset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform)
valloader = torch.utils.data.DataLoader(valset, batch_size=100, shuffle=False, num_workers=2)

classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

# Model
if args.resume:
if config.resume:
# Load checkpoint.
print('==> Resuming from checkpoint..')
assert os.path.isdir('checkpoint'), 'Error: no checkpoint directory found!'
checkpoint = torch.load('./checkpoint/ckpt.t7')
net = checkpoint['net']
best_acc = checkpoint['acc']
best_epoch = checkpoint['epoch']
start_epoch = checkpoint['epoch']
else:
print('==> Building model..')
net = VGG('VGG19')
# net = ResNet18()
# net = GoogLeNet()
# net = DenseNet121()
# net = ResNeXt29_2x64d()
# net = LeNet()
#net = VGG('VGG19')
#net = ResNet18()
#net = GoogLeNet()
net = DenseNet121()
#net = ResNeXt29_2x64d()
#net = LeNet()

if use_cuda:
net.cuda()
#net = torch.nn.DataParallel(net, device_ids=[0,1,2,3])
#cudnn.benchmark = True
net = torch.nn.DataParallel(net, device_ids=range(torch.cuda.device_count()))
cudnn.benchmark = True

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=args.lr, momentum=0.9, weight_decay=5e-4)
optimizer = optim.SGD(net.parameters(), lr=config.lr, momentum=0.9, weight_decay=5e-4)

# Training
def train(epoch):

print('\nEpoch: %d' % epoch)
net.train()
train_loss = 0
Expand All @@ -96,30 +90,35 @@ def train(epoch):
progress_bar(batch_idx, len(trainloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
% (train_loss/(batch_idx+1), 100.*correct/total, correct, total))

def test(epoch):
print("Epoch: ", epoch, "Acc: ", 100.*correct/total, correct, total)

def val(epoch):

global best_acc
global best_epoch

net.eval()
test_loss = 0
val_loss = 0
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(testloader):
for batch_idx, (inputs, targets) in enumerate(valloader):
if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
inputs, targets = Variable(inputs, volatile=True), Variable(targets)
outputs = net(inputs)
loss = criterion(outputs, targets)

test_loss += loss.data[0]
val_loss += loss.data[0]
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()

progress_bar(batch_idx, len(testloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
% (test_loss/(batch_idx+1), 100.*correct/total, correct, total))
progress_bar(batch_idx, len(valloader), 'Loss: %.3f | Acc: %.3f%% (%d/%d)'
% (val_loss/(batch_idx+1), 100.*correct/total, correct, total))

print("Epoch: ", epoch, "Acc: ", 100.*correct/total, correct, total)
# Save checkpoint.
acc = 100.*correct/total
acc = 100. * correct / total
if acc > best_acc:
print('Saving..')
state = {
Expand All @@ -132,8 +131,20 @@ def test(epoch):
os.mkdir('checkpoint')
torch.save(state, './checkpoint/ckpt.t7')
best_acc = acc
best_epoch = epoch
print("best epoch: ", best_epoch, " acc: ", best_acc)

for epoch in range(start_epoch, start_epoch + config.epochs):
if epoch < 20:
train(epoch)
val(epoch)
elif epoch >= 20 and epoch < 40:
optimizer = optim.SGD(net.parameters(), lr=config.lr/10.0, momentum=0.9, weight_decay=5e-4)
train(epoch)
val(epoch)
else:
optimizer = optim.SGD(net.parameters(), lr=config.lr/100.0, momentum=0.9, weight_decay=5e-4)
train(epoch)
val(epoch)


for epoch in range(start_epoch, start_epoch+200):
train(epoch)
test(epoch)
2 changes: 0 additions & 2 deletions CIFAR/models/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,3 @@ def test_resnet():
net = ResNet50()
y = net(Variable(torch.randn(1,3,32,32)))
print(y.size())

# test_resnet()
24 changes: 8 additions & 16 deletions CIFAR/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ def get_mean_and_std(dataset):
std.div_(len(dataset))
return mean, std

# import torchvision
# import torchvision.transforms as transforms
# transform = transforms.Compose([transforms.ToTensor()])
# dataset = torchvision.datasets.CIFAR10(root='./data', train=True, download=False, transform=transform)
# mean, std = get_mean_and_std(dataset)
# print(mean)
# print(std)

def init_params(net):
'''Init layer parameters.'''
for m in net.modules():
Expand All @@ -53,20 +45,20 @@ def init_params(net):
if m.bias:
init.constant(m.bias, 0)


_, term_width = os.popen('stty size', 'r').read().split()
term_width = int(term_width)

TOTAL_BAR_LENGTH = 86.
TOTAL_BAR_LENGTH = 65.
last_time = time.time()
begin_time = last_time

def progress_bar(current, total, msg=None):

global last_time, begin_time
if current == 0:
begin_time = time.time() # Reset for new bar.

cur_len = int(TOTAL_BAR_LENGTH*current/total)
cur_len = int(TOTAL_BAR_LENGTH * current / total)
rest_len = int(TOTAL_BAR_LENGTH - cur_len) - 1

sys.stdout.write(' [')
Expand All @@ -83,8 +75,8 @@ def progress_bar(current, total, msg=None):
tot_time = cur_time - begin_time

L = []
L.append(' Step: %s' % format_time(step_time))
L.append(' | Tot: %s' % format_time(tot_time))
#L.append(' Step: %d / %d' % current+1, total)
L.append('Used Time: %s' % format_time(tot_time))
if msg:
L.append(' | ' + msg)

Expand All @@ -94,9 +86,9 @@ def progress_bar(current, total, msg=None):
sys.stdout.write(' ')

# Go back to the center of the bar.
for i in range(term_width-int(TOTAL_BAR_LENGTH/2)):
sys.stdout.write('\b')
sys.stdout.write(' %d/%d ' % (current+1, total))
#for i in range(term_width-int(TOTAL_BAR_LENGTH/2)):
# sys.stdout.write('\b')
#sys.stdout.write(' %d/%d ' % (current+1, total))

if current < total-1:
sys.stdout.write('\r')
Expand Down
5 changes: 5 additions & 0 deletions dog_and_cat/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
lr = 0.01
resume = False
start_epoch = 0
epochs = 50
pretrained = True
Loading

0 comments on commit a4ab02a

Please sign in to comment.