fine-tune-tri-tunnel-net's People
fine-tune-tri-tunnel-net's Issues
finetune
-- coding: utf-8 --
License: BSD
from future import print_function, division
import argparse
from models import *
#import config_parallel as cf
from preprocess import *
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import torch.backends.cudnn as cudnn
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import copy
import os
import setproctitle
import numpy as np
import random
os.environ["CUDA_VISIBLE_DEVICES"] = "7"
parser = argparse.ArgumentParser(description='PyTorch VGG Training')
parser.add_argument('--resume', '-r', action='store_true', help='resume from checkpoint')
parser.add_argument('--save')
parser.add_argument('--load')
parser.add_argument('--lr', default=0.1, type=float)
parser.add_argument('--epoch', default=100, type=int)
parser.add_argument('--wd', default=5e-4, type=float)
parser.add_argument('--momentum', default=0.9, type=float)
parser.add_argument('--testOnly', '-t', action='store_true', help='Test mode with the saved model')
parser.add_argument('--check', '-c', action='store_true', help='Check the saved model')
args = parser.parse_args()
args.save = args.save or './parallel_tune/tiny.base'
args.load = args.load or './parallel_tune/imagenet.t7'
setproctitle.setproctitle(args.save)
use_cuda = torch.cuda.is_available()
best_acc = 0 # best test accuracy
start_epoch = 1 # start from epoch 0 or last checkpoint epoch
epochs = args.epoch
lr = args.lr
weight_decay = args.wd
momentum = args.momentum
if not os.path.exists(args.save):
os.mkdir(args.save)
######################################################################
Load Data
data_dir = {
'cifar': '/ssd/RookieProject/CIFAR100/',
'tiny': '/ssd/RookieProject/TinyImageNet',
'vgg': '/ssd/RookieProject/VGGFlowers',
}
test_transforms = {
x: transforms.Compose([
transforms.RandomSizedCrop(64),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5])
])
for x in ['cifar', 'tiny', 'vgg']
}
val_transforms = {
x: transforms.Compose([
transforms.Scale(72),
transforms.CenterCrop(64),
transforms.ToTensor(),
transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5])
])
for x in ['cifar', 'tiny', 'vgg']
}
trainset = {
'cifar': TxtFolder(os.path.join(data_dir['cifar']), 'list_train.txt', test_transforms['cifar']),
'tiny': datasets.ImageFolder(os.path.join(data_dir['tiny'], 'train'), test_transforms['tiny']),
'vgg': datasets.ImageFolder(os.path.join(data_dir['vgg'], 'train'), test_transforms['vgg']),
}
valset = {
'cifar': TxtFolder(os.path.join(data_dir['cifar']), 'list_val.txt', val_transforms['cifar']),
'tiny': datasets.ImageFolder(os.path.join(data_dir['tiny'], 'val'), val_transforms['tiny']),
'vgg': datasets.ImageFolder(os.path.join(data_dir['vgg'], 'val'), val_transforms['vgg']),
}
#sampler = OverSampler(trainset, 313, 16)
trainloader = {
'cifar': torch.utils.data.DataLoader(trainset['cifar'], batch_size=128,
shuffle=True, num_workers=4),
'tiny': torch.utils.data.DataLoader(trainset['tiny'], batch_size=256,
shuffle=True, num_workers=4),
'vgg': torch.utils.data.DataLoader(trainset['vgg'], batch_size=128,
shuffle=True, num_workers=4)
}
valloader = {
x: torch.utils.data.DataLoader(valset[x], batch_size=256,
shuffle=False, num_workers=4)
for x in ['cifar', 'tiny', 'vgg']
}
use_gpu = torch.cuda.is_available()
class AverageMeter(object):
"""Computes and stores the average and current value"""
def init(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
if (args.check):
print('\n[Check Phase] : Model setup')
checkpoint = torch.load(os.path.join(args.save, 'cifar.t7'))
net = checkpoint['net']
if use_gpu:
model_ft = net.cuda()
model_ft = torch.nn.DataParallel(net, device_ids=[0])
cudnn.benchmark = True
CheckF = open(os.path.join(args.save, 'weight.txt'), 'w')
CheckF.write('{}\n'.format(net))
for m in net.modules():
if isinstance(m, nn.Conv2d):
CheckF.write('{}\n{}\n'.format(m, m.weight.data))
elif isinstance(m, nn.Linear):
CheckF.write('{}\n{}\n'.format(m, m.weight.data))
sys.exit(0)
######################################################################
Training the model
if (args.testOnly):
print('\n[Test Phase] : Model setup')
checkpoint = torch.load(args.load)
model = checkpoint['net']
#assert os.path.isdir('checkpoint'), 'Error: No checkpoint directory found!'
model_feature = nn.Sequential(*list(model.children())[:-2])
print (model_feature)
model_branch = nn.Sequential(*list(model.children())[1])
print (model_branch)
model_fc = model.classifier
model_ft = finetune.tiny3(model_feature, model_branch, model_fc, 100, 1000, 102)
net = model_ft
net = checkpoint['net']
best_acc = checkpoint['acc']
print (best_acc)
classes = {'cifar': 100,
'tiny': 1000,
'vgg': 102}
if use_cuda:
net.cuda()
# net = torch.nn.DataParallel(net, device_ids=range(torch.cuda.device_count()))
# cudnn.benchmark = True
net.eval()
trainW = {x: open(os.path.join(args.save, 'class_' + x + '.csv'), 'w') for x in ['cifar','tiny','vgg']}
err_total = 0
for data_name in ['cifar','tiny','vgg']:
test_loss = 0
correct = 0
incorrect = 0
total = 0
#print (classes[data_name])
#compute_class(valloader[data_name], net, classes[data_name], args.save, data_name)
class_correct = list(0. for i in range(classes[data_name]))
class_total = list(0. for i in range(classes[data_name]))
idx = [i for i in range(313)]
for batch_idx, (inputs, targets) in enumerate(valloader[data_name]):
if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
target = targets
inputs, targets = Variable(inputs, volatile=True), Variable(targets)
cifaro, tinyo, vggo = net(inputs, inputs, inputs)
if data_name == 'cifar':
outputs = cifaro
elif data_name == 'tiny':
outputs = tinyo
elif data_name == 'vgg':
outputs = vggo
_, predicted = torch.max(outputs.data, 1)
c = (predicted == target).squeeze()
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()
incorrect += predicted.ne(targets.data).cpu().sum()
for i in range(len(targets)):
#print (len(targets))
label = target[i]
class_correct[label] += c[i]
class_total[label] += 1
nTotal = len(valloader[data_name].dataset)
err = 100.*incorrect/nTotal
err_total += err
print (err_total)
for i in range(classes[data_name]):
trainW[data_name].write('{},{},{},{}\n'.format(i, class_correct[i], class_total[i], class_correct[i]/class_total[i]))
trainW[data_name].flush()
trainW[data_name].close()
# Save checkpoint when best model
acc = 100.*correct/total
print("| Test " + data_name +" Result\tAcc@1: %.2f%%" %(acc))
sys.exit(0)
def train(net, criterion, epoch, trainF, optimizer):
print('\nEpoch: %d' % epoch)
net.train()
train_loss = 0
top1 = {x: AverageMeter() for x in ['cifar','tiny','vgg']}
top5 = {x: AverageMeter() for x in ['cifar','tiny','vgg']}
losses = {x: AverageMeter() for x in ['cifar','tiny','vgg']}
correct = 0
total = 0
nProcessed = 0
k = {
'cifar': 0.333,
'tiny': 0.333,
'vgg': 0.333,
}
N= len(trainloader['cifar'])
#nTrain = len(trainloader[data_name].dataset)
idx = [i for i in range(N)]
dataiter = {x: iter(trainloader[x]) for x in ['cifar','tiny', 'vgg']}
for batch_idx in idx:
#inputs, targets = {x: dataiter[x].next() for x in ['cifar','tiny','vgg']}
inputs = {
'cifar': inputs_cifar.cuda(),
'tiny': inputs_tiny.cuda(),
'vgg': inputs_vgg.cuda(),
}
targets = {
'cifar': targets_cifar.cuda(),
'tiny': targets_tiny.cuda(),
'vgg': targets_vgg.cuda(),
}
inputs = {}
targets = {}
if batch_idx%16 == 0:
dataiter['vgg'] = iter(trainloader['vgg'])
for x in ['cifar','tiny','vgg']:
inputs[x], targets[x] = dataiter[x].next()
if use_cuda:
inputs[x], targets[x] = inputs[x].cuda(), targets[x].cuda()
optimizer.zero_grad()
target = {x: targets[x] for x in ['cifar','tiny','vgg']}
inputs= {x: Variable(inputs[x]) for x in ['cifar','tiny','vgg']}
targets= {x: Variable(targets[x]) for x in ['cifar','tiny','vgg']}
cifaro, tinyo, vggo = net(inputs['cifar'], inputs['tiny'], inputs['vgg'])
outputs = {
'cifar': cifaro,
'tiny': tinyo,
'vgg': vggo,
}
#make_graph.save('./t.dot', loss.creator); assert(False)
loss1 = criterion(outputs['cifar'], targets['cifar'])
print (loss1.data[0])
loss2 = criterion(outputs['vgg'], targets['vgg'])
print (loss2.data[0])
loss3 = criterion(outputs['tiny'], targets['tiny'])
print (loss3.data[0])
loss = k['cifar']*criterion(outputs['cifar'], targets['cifar']) + k['vgg']*criterion(outputs['vgg'], targets['vgg']) +\
k['tiny']*criterion(outputs['tiny'], targets['tiny'])
print (loss.data[0])
loss.backward()
optimizer.step()
for x in ['cifar','tiny','vgg']:
_, predicted = torch.max(outputs[x].data, 1)
total += targets[x].size(0)
prec1, prec5 = accuracy(outputs[x].data, target[x], topk=(1, 5))
top1[x].update(prec1[0], inputs[x].size(0))
top5[x].update(prec5[0], inputs[x].size(0))
losses[x].update(loss.data[0], inputs[x].size(0))
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
prec1, prec5 = accuracy(outputs.data, target, topk=(1, 5))
top1['cifar'].update(prec1[0], inputs.size(0))
top5['cifar'].update(prec5[0], inputs.size(0))
losses['cifar'].update(loss.data[0], inputs.size(0))
print('Train Epoch: [{}/{} ({:.0f}%)]\t'.format(batch_idx, N, 100. * batch_idx / N))
print(x+' * Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f}'
.format(top1=top1[x], top5=top5[x]))
for x in ['cifar','tiny','vgg']:
trainF[x].write('{},{},{},{}\n'.format(epoch, losses[x].avg, top1[x].avg, top5[x].avg))
trainF[x].flush()
def test(net, epoch, testF):
global best_acc
net.eval()
top1 = {x: AverageMeter() for x in ['cifar','tiny','vgg']}
top5 = {x: AverageMeter() for x in ['cifar','tiny','vgg']}
losses = {x: AverageMeter() for x in ['cifar','tiny','vgg']}
err_total = 0
for data_name in ['cifar','tiny','vgg']:
test_loss = 0
incorrect = 0
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(valloader[data_name]):
if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
target = targets
inputs, targets = Variable(inputs, volatile=True), Variable(targets)
cifaro, tinyo, vggo = net(inputs, inputs, inputs)
if data_name == 'cifar':
loss = criterion(cifaro, targets)
elif data_name == 'tiny':
loss = criterion(tinyo, targets)
elif data_name == 'vgg':
loss = criterion(vggo, targets)
if data_name == 'cifar':
outputs = cifaro
elif data_name == 'tiny':
outputs = tinyo
elif data_name == 'vgg':
outputs = vggo
test_loss += loss.data[0]
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()
incorrect += predicted.ne(targets.data).cpu().sum()
prec1, prec5 = accuracy(outputs.data, target, topk=(1, 5))
top1[data_name].update(prec1[0], inputs.size(0))
top5[data_name].update(prec5[0], inputs.size(0))
nTotal = len(valloader[data_name].dataset)
err = 100.*incorrect/nTotal
test_loss /= len(valloader[data_name])
err_total += err
#print (err_total)
#print (err_total)
print('\nTest set: Average loss: {:.4f}, Error: {}/{} ({:.0f}%)\n'.format(
test_loss, incorrect, nTotal, err))
print(' * Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f}'
.format(top1=top1[data_name], top5=top5[data_name]))
testF[data_name].write('{},{},{},{}\n'. format(epoch, test_loss, top1[data_name].avg, top5[data_name].avg))
testF[data_name].flush()
return err_total/3
######################################################################
Visualizing the model predictions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Generic function to display predictions for a few images
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
######################################################################
Finetuning the convnet
----------------------
Model
if args.resume:
# Load checkpoint.
print('==> Resuming from checkpoint..')
#assert os.path.isdir('checkpoint'), 'Error: no checkpoint directory found!'
checkpoint = torch.load(os.path.join(args.save, 'cifar_latest.t7'))
model_ft = checkpoint['net']
best_acc = checkpoint['acc']
start_epoch = checkpoint['epoch']+1
ignored_params = list(map(id, model_ft.base1.parameters()))
base_params = filter(lambda p: id(p) not in ignored_params,
model_ft.parameters())
for param in model_ft.parameters():
param.requires_grad = False
for param in ignored_params:
param.requires_grad = True
else:
print('==> Building new model..')
#model_ft = Resnet_parallel(PreActBlock, [2,2,2,2], 100, 1000, 102)
checkpoint = torch.load(args.load)
model = checkpoint['net']
model_feature = nn.Sequential(*list(model.children())[:-2])
print (model_feature)
model_branch = nn.Sequential(*list(model.children())[1])
print (model_branch)
model_fc = model.classifier
model_ft = finetune.tiny3(model_feature, model_branch, model_fc, 100, 1000, 102)
ignored_params = list(map(id, model_ft.base1.parameters()))
base_params = filter(lambda p: id(p) not in ignored_params,
model_ft.parameters())
print ('Number of parames:{}'.format(
sum([p.data.nelement() for p in model_ft.parameters()])))
for param in model_ft.base1.parameters():
param.requires_grad = False
for param in model_ft.branch_tiny.parameters():
param.requires_grad = False
for param in model_ft.fc_tiny.parameters():
param.requires_grad = False
if use_gpu:
model_ft = model_ft.cuda()
model_ft = torch.nn.DataParallel(model_ft, device_ids=[0])
cudnn.benchmark = True
criterion = nn.CrossEntropyLoss()
######################################################################
Train and evaluate
^^^^^^^^^^^^^^^^^^
if args.resume:
trainF = {x: open(os.path.join(args.save, 'train_' + x + '.csv'), 'a')
for x in ['cifar', 'tiny', 'vgg']
}
testF = {x: open(os.path.join(args.save, 'test_' + x + '.csv'), 'a')
for x in ['cifar', 'tiny', 'vgg']
}
TestF = open(os.path.join(args.save, 'acc.csv'), 'a')
else:
trainF = {x: open(os.path.join(args.save, 'train_' + x + '.csv'), 'w')
for x in ['cifar', 'tiny', 'vgg']
}
testF = {x: open(os.path.join(args.save, 'test_' + x + '.csv'), 'w')
for x in ['cifar', 'tiny', 'vgg']
}
TestF = open(os.path.join(args.save, 'acc.csv'), 'w')
elapsed_time = 0
#epochs = 300
for epoch in range(start_epoch, start_epoch+epochs):
err = 0
start_time = time.time()
optimizer = torch.optim.SGD([
{'params': model_ft.module.branch_cifar.parameters()},
{'params': model_ft.module.branch_vgg.parameters()},
{'params': model_ft.module.fc_cifar.parameters()},
{'params': model_ft.module.fc_vgg.parameters()},
], lr=lr, momentum=0.9, weight_decay=weight_decay)
train(model_ft, criterion, epoch, trainF, optimizer)
err = test(model_ft, epoch, testF)
epoch_time = time.time() - start_time
elapsed_time += epoch_time
#print('| Elapsed time : %d:%02d:%02d' %(cf.get_hms(elapsed_time)))
# # Save checkpoint.
acc = 100.*correct/total
state = {
'net': model_ft.module if use_cuda else model_ft,
#'net': net,
'acc': 100-err,
'epoch': epoch,
}
print (100-err)
print (best_acc)
acc = 100-err
TestF.write('{}\n'.format(acc))
TestF.flush()
torch.save(state, os.path.join(args.save, 'cifar_latest.t7'))
if (100-err-best_acc) > 0:
torch.save(state, os.path.join(args.save, 'cifar.t7'))
best_acc = 100-err
for x in ['cifar', 'tiny', 'vgg']:
trainF[x].close()
testF[x].close()
TestF.close()
train imagenet
-- coding: utf-8 --
License: BSD
Author: Sasank Chilamkurthy
from future import print_function, division
import argparse
from models import *
#import config as cf
from preprocess import *
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import torch.backends.cudnn as cudnn
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import copy
import os
import setproctitle
import numpy as np
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
parser = argparse.ArgumentParser(description='PyTorch Imagenet Training')
parser.add_argument('--resume', '-r', action='store_true', help='resume from checkpoint')
parser.add_argument('--save')
parser.add_argument('--lr', default=0.1, type=float)
parser.add_argument('--epoch', default=100, type=int)
parser.add_argument('--wd', default=5e-4, type=float)
parser.add_argument('--momentum', default=0.9, type=float)
parser.add_argument('--testOnly', '-t', action='store_true', help='Test mode with the saved model')
args = parser.parse_args()
args.save = args.save or './tiny/tiny_tune1.base'
setproctitle.setproctitle(args.save)
use_cuda = torch.cuda.is_available()
best_acc = 0 # best test accuracy
start_epoch = 1 # start from epoch 0 or last checkpoint epoch
batch_size = 256
epochs = args.epoch
lr = args.lr
weight_decay = args.wd
momentum = args.momentum
if not os.path.exists(args.save):
os.mkdir(args.save)
######################################################################
Load Data
data_transforms = {
'train': transforms.Compose([
transforms.RandomSizedCrop(64),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5])
]),
'val': transforms.Compose([
transforms.Scale(72),
transforms.CenterCrop(64),
transforms.ToTensor(),
transforms.Normalize([0.5,0.5,0.5], [0.5,0.5,0.5])
]),
}
data_dir = '/ssd/RookieProject/TinyImageNet'
#data_dir = '/ssd/chenjiaxu/data/hymenoptera_data'
trainset = datasets.ImageFolder(os.path.join(data_dir, 'train'), data_transforms['train'])
valset = datasets.ImageFolder(os.path.join(data_dir, 'val'), data_transforms['val'])
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
shuffle=True, num_workers=4)
valloader = torch.utils.data.DataLoader(valset, batch_size=batch_size,
shuffle=False, num_workers=4)
#data_dir = '/ssd/RookieProject/CIFAR100/'
#trainset = TxtFolder(os.path.join(data_dir), 'list_train.txt', data_transforms['train'])
#trainloader =torch.utils.data.DataLoader(trainset, batch_size=cf.batch_size,
shuffle=True, num_workers=4)
#valset = TxtFolder(os.path.join(data_dir), 'list_val.txt', data_transforms['val'])
#valloader =torch.utils.data.DataLoader(trainset, batch_size=cf.batch_size,
shuffle=False, num_workers=4)
#dset_sizes = {x: len(dsets[x]) for x in ['train', 'val']}
dset_classes = trainset.classes
num_classes = len(dset_classes)
use_gpu = torch.cuda.is_available()
class AverageMeter(object):
"""Computes and stores the average and current value"""
def init(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
######################################################################
Training the model
Test only option
if (args.testOnly):
print('\n[Test Phase] : Model setup')
#assert os.path.isdir('checkpoint'), 'Error: No checkpoint directory found!'
checkpoint = torch.load(os.path.join(args.save, 'imagenet.t7'))
net = checkpoint['net']
epoch = checkpoint['epoch']
print (epoch)
if use_cuda:
net.cuda()
# net = torch.nn.DataParallel(net, device_ids=range(torch.cuda.device_count()))
# cudnn.benchmark = True
net.eval()
test_loss = 0
correct = 0
total = 0
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)
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()
# Save checkpoint when best model
acc = 100.*correct/total
print("| Test Result\tAcc@1: %.2f%%" %(acc))
sys.exit(0)
def train(net, criterion, epoch, trainF):
print('\nEpoch: %d' % epoch)
net.train()
train_loss = 0
top1 = AverageMeter()
top5 = AverageMeter()
losses = AverageMeter()
correct = 0
total = 0
nProcessed = 0
nTrain = len(trainloader.dataset)
optimizer = optim.SGD(net.parameters(), lr=lr, momentum=momentum, weight_decay=weight_decay)
for batch_idx, (inputs, targets) in enumerate(trainloader):
if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
optimizer.zero_grad()
target = targets
inputs, targets = Variable(inputs), Variable(targets)
outputs = net(inputs)
loss = criterion(outputs, targets)
#make_graph.save('./t.dot', loss.creator); assert(False)
loss.backward()
optimizer.step()
nProcessed += len(inputs)
train_loss += loss.data[0]
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()
prec1, prec5 = accuracy(outputs.data, target, topk=(1, 5))
top1.update(prec1[0], inputs.size(0))
top5.update(prec5[0], inputs.size(0))
losses.update(loss.data[0], inputs.size(0))
incorrect = predicted.ne(targets.data).cpu().sum()
err = 100.*incorrect/len(inputs)
partialEpoch = epoch + batch_idx / len(trainloader) - 1
print('Train Epoch: {:.2f} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tError: {:.6f}'.format(
partialEpoch, nProcessed, nTrain, 100. * batch_idx / len(trainloader),
loss.data[0], err))
print(' * Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f}'
.format(top1=top1, top5=top5))
trainF.write('{},{},{},{}\n'.format(epoch, losses.avg, top1.avg, top5.avg))
trainF.flush()
def test(net, epoch, testF):
global best_acc
net.eval()
top1 = AverageMeter()
top5 = AverageMeter()
test_loss = 0
incorrect = 0
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(valloader):
if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
target = targets
inputs, targets = Variable(inputs, volatile=True), Variable(targets)
outputs = net(inputs)
loss = criterion(outputs, targets)
test_loss += loss.data[0]
_, predicted = torch.max(outputs.data, 1)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()
incorrect += predicted.ne(targets.data).cpu().sum()
prec1, prec5 = accuracy(outputs.data, target, topk=(1, 5))
top1.update(prec1[0], inputs.size(0))
top5.update(prec5[0], inputs.size(0))
nTotal = len(valloader.dataset)
err = 100.*incorrect/nTotal
test_loss /= len(valloader)
print('\nTest set: Average loss: {:.4f}, Error: {}/{} ({:.0f}%)\n'.format(
test_loss, incorrect, nTotal, err))
print(' * Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f}'
.format(top1=top1, top5=top5))
testF.write('{},{},{},{}\n'. format(epoch, test_loss, top1.avg, top5.avg))
testF.flush()
# Save checkpoint.
acc = 100.*correct/total
state = {
'net': net.module if use_cuda else net,
#'net': net,
'acc': acc,
'epoch': epoch,
}
torch.save(state, os.path.join(args.save, 'imagenet_latest.t7'))
if acc > best_acc:
torch.save(state, os.path.join(args.save, 'imagenet.t7'))
best_acc = acc
######################################################################
Visualizing the model predictions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Generic function to display predictions for a few images
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
######################################################################
Finetuning the convnet
----------------------
Model
if args.resume:
# Load checkpoint.
print('==> Resuming from checkpoint..')
#assert os.path.isdir('checkpoint'), 'Error: no checkpoint directory found!'
checkpoint = torch.load(os.path.join(args.save, 'imagenet_latest.t7'))
model_ft = checkpoint['net']
best_acc = checkpoint['acc']
start_epoch = checkpoint['epoch']+1
else:
print('==> Building new model..')
#model_ft = Resnet_drop(PreActBlock, [2,2,2,2], num_classes)
model_ft = Resnet_tune(PreActBlock, [2,2,2,2], num_classes)
print (model_ft)
#net = ResNet34()
print ('Number of parames:{}'.format(
sum([p.data.nelement() for p in model_ft.parameters()])))
if use_gpu:
model_ft = model_ft.cuda()
model_ft = torch.nn.DataParallel(model_ft, device_ids=[0])
cudnn.benchmark = True
criterion = nn.CrossEntropyLoss()
######################################################################
Train and evaluate
^^^^^^^^^^^^^^^^^^
if args.resume:
trainF = open(os.path.join(args.save, 'train.csv'), 'a')
testF = open(os.path.join(args.save, 'test.csv'), 'a')
else:
trainF = open(os.path.join(args.save, 'train.csv'), 'w')
testF = open(os.path.join(args.save, 'test.csv'), 'w')
elapsed_time = 0
#epochs = 300
for epoch in range(start_epoch, start_epoch+epochs):
start_time = time.time()
train(model_ft, criterion, epoch, trainF)
test(model_ft, epoch, testF)
epoch_time = time.time() - start_time
elapsed_time += epoch_time
#print('| Elapsed time : %d:%02d:%02d' %(cf.get_hms(elapsed_time)))
trainF.close()
testF.close()
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.