multitask-learning's People
multitask-learning's Issues
main
一个参考的:https://discuss.pytorch.org/t/how-to-create-model-with-sharing-weight/398/2
-- 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"] = "5"
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('--testOnly', '-t', action='store_true', help='Test mode with the saved model')
args = parser.parse_args()
args.save = args.save or './joint.base'
setproctitle.setproctitle(args.save)
use_cuda = torch.cuda.is_available()
best_acc = 0 # best test accuracy
start_epoch = cf.start_epoch # start from epoch 0 or last checkpoint epoch
batch_size = cf.batch_size
epochs = cf.num_epochs
######################################################################
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(cf.mean[x], cf.std[x])
])
for x in ['cifar', 'tiny', 'vgg']
}
val_transforms = {
x: transforms.Compose([
transforms.Scale(72),
transforms.CenterCrop(64),
transforms.ToTensor(),
#transforms.Normalize(cf.mean[x], cf.std[x])
])
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']),
}
trainloader = {
x: torch.utils.data.DataLoader(trainset[x], batch_size=cf.batch_size,
shuffle=True, num_workers=4)
for x in ['cifar', 'tiny', 'vgg']
}
valloader = {
x: torch.utils.data.DataLoader(valset[x], batch_size=cf.batch_size,
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
######################################################################
Training the model
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, 'cifar_resnetse_best.t7'))
net = checkpoint['net']
if use_cuda:
net.cuda()
# net = torch.nn.DataParallel(net, device_ids=range(torch.cuda.device_count()))
# cudnn.benchmark = True
net.eval()
for data_name in ['cifar','tiny','vgg']:
test_loss = 0
correct = 0
total = 0
for batch_idx, (inputs, targets) in enumerate(valloader[data_name]):
if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
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)
total += targets.size(0)
correct += predicted.eq(targets.data).cpu().sum()
# 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, data_name):
print('\nEpoch: %d' % epoch)
net.train()
train_loss = 0
top1 = AverageMeter()
top5 = AverageMeter()
losses = AverageMeter()
k_cifar = 1
k_tiny = 1
k_vgg = 1
correct = 0
total = 0
nProcessed = 0
nTrain = len(trainloader[data_name].dataset)
optimizer = optim.SGD(net.parameters(), lr=cf.adjust_rate(epoch), momentum=cf.momentum, weight_decay=1e-4)
for batch_idx, (inputs, targets) in enumerate(trainloader[data_name]):
if use_cuda:
inputs, targets = inputs.cuda(), targets.cuda()
optimizer.zero_grad()
target = targets
inputs, targets = Variable(inputs), 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)
#make_graph.save('./t.dot', loss.creator); assert(False)
loss.backward()
optimizer.step()
if data_name == 'cifar':
outputs = cifaro
elif data_name == 'tiny':
outputs = tinyo
elif data_name == 'vgg':
outputs = vggo
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[data_name]) - 1
print('Train Epoch: {:.2f} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tError: {:.6f}'.format(
partialEpoch, nProcessed, nTrain, 100. * batch_idx / len(trainloader[data_name]),
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, data_name):
global best_acc
net.eval()
top1 = AverageMeter()
top5 = AverageMeter()
test_loss = 0
incorrect = 0
correct = 0
total = 0
k = {
'cifar': 0.33,
'tiny': 0.33,
'vgg': (1-0.66),
}
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.update(prec1[0], inputs.size(0))
top5.update(prec5[0], inputs.size(0))
nTotal = len(valloader[data_name].dataset)
err = 100.*incorrect/nTotal
test_loss /= len(valloader[data_name])
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()
return k[data_name]*err
# 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, 'cifar.t7'))
if acc > best_acc:
torch.save(state, os.path.join(args.save, 'cifar.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, 'cifar.t7'))
model_ft = checkpoint['net']
best_acc = checkpoint['acc']
start_epoch = checkpoint['epoch']
else:
print('==> Building new model..')
model_ft = Resnet_sen_share(PreActBlock, [2,2,2,2], 100, 1000, 102)
#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 = {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']
}
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']
}
elapsed_time = 0
#epochs = 300
for epoch in range(start_epoch, start_epoch+epochs):
err = 0
for data_name in ['cifar', 'tiny', 'vgg']:
start_time = time.time()
train(model_ft, criterion, epoch, trainF[data_name], data_name)
err += test(model_ft, epoch, testF[data_name], data_name)
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)
torch.save(state, os.path.join(args.save, 'cifar.t7'))
if (100-err) > best_acc:
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()
net
"""
Created on Sat Aug 12 15:59:58 2017
@author: chenjiaxu
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
class SE(nn.Module):
def init(self, in_planes):
super(SE, self).init()
self.fc1 = nn.Linear(in_planes, in_planes//16)
self.relu = nn.ReLU(inplace=True)
self.fc2 = nn.Linear(in_planes//16, in_planes)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
#global FLOPS
w = F.avg_pool2d(x, kernel_size=x.size(2))
w = self.fc1(w.view(w.size(0),-1))
w = self.relu(w)
#FLOPS += self.fc1.in_features*self.fc1.out_features
w = self.fc2(w)
w = self.sigmoid(w)
#FLOPS += self.fc2.in_features*self.fc2.out_features
w = w.view(w.size(0),w.size(1),1,1)
w = w.repeat(1,1,x.size(2),x.size(3))
out = w*x
return out
class PreActBlock(nn.Module):
'''Pre-activation version of the original Bottleneck module.'''
widen_factor = 1
def __init__(self, in_planes, planes, stride=1):
super(PreActBlock, self).__init__()
self.bn1 = nn.BatchNorm2d(in_planes)
self.relu = nn.ReLU(inplace=True)
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes*self.widen_factor, kernel_size=3, padding=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes*self.widen_factor)
self.se = SE(planes*self.widen_factor)
if stride != 1 or in_planes != planes*self.widen_factor:
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, planes*self.widen_factor, kernel_size=1, stride=stride, bias=False)
)
def forward(self, x):
#global FLOPS
out = self.bn1(x)
#out = self.relu(out)
shortcut = self.shortcut(out) if hasattr(self, 'shortcut') else x
out = self.conv1(out)
#FLOPS += self.conv1.in_channels*self.conv1.out_channels*self.conv1.kernel_size[0]*self.conv1.kernel_size[1]*out.size(2)*out.size(3)
out = self.bn2(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn3(out)
#FLOPS += self.conv2.in_channels*self.conv2.out_channels*self.conv2.kernel_size[0]*self.conv2.kernel_size[1]*out.size(2)*out.size(3)
out = self.se(out)
out += shortcut
return out
class PreActBottleneck(nn.Module):
'''Pre-activation version of the original Bottleneck module.'''
widen_factor = 4
def __init__(self, in_planes, planes, stride=1):
super(PreActBottleneck, self).__init__()
self.bn1 = nn.BatchNorm2d(in_planes)
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes*self.widen_factor, kernel_size=1, bias=False)
self.bn4 = nn.BatchNorm2d(planes*self.widen_factor)
self.se = SE(planes*self.widen_factor)
if stride != 1 or in_planes != planes*self.widen_factor:
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, planes*self.widen_factor, kernel_size=1, stride=stride, bias=False)
)
def forward(self, x):
#global FLOPS
out = self.bn1(x)
shortcut = self.shortcut(out) if hasattr(self, 'shortcut') else x
out = self.conv1(out)
#FLOPS += self.conv1.in_channels*self.conv1.out_channels*self.conv1.kernel_size[0]*self.conv1.kernel_size[1]*out.size(2)*out.size(3)
out = self.conv2(F.relu(self.bn2(out)))
#FLOPS += self.conv2.in_channels*self.conv2.out_channels*self.conv2.kernel_size[0]*self.conv2.kernel_size[1]*out.size(2)*out.size(3)
out = self.conv3(F.relu(self.bn3(out)))
#FLOPS += self.conv3.in_channels*self.conv3.out_channels*self.conv3.kernel_size[0]*self.conv3.kernel_size[1]*out.size(2)*out.size(3)
out = self.bn4(out)
out = self.se(out)
out += shortcut
return out
class Resnet_sen_share(nn.Module):
def init(self, block, num_blocks, cifar_classes, tiny_classes, vgg_classes):
super(Resnet_sen_share, self).init()
self.in_planes = 32
width = [32, 64, 128, 256]
self.base = nn.Sequential(
nn.Conv2d(3, self.in_planes, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(self.in_planes),
nn.ReLU(inplace=True),
self._make_layer(block, width[0], num_blocks[0], stride=1),
self._make_layer(block, width[1], num_blocks[1], stride=2),
self._make_layer(block, width[2], num_blocks[2], stride=2),
self._make_layer(block, width[3], num_blocks[3], stride=2),
nn.AvgPool2d(8),
)
self.conv1 = nn.Conv2d(3, self.in_planes, kernel_size=3, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(self.in_planes)
self.relu = nn.ReLU(inplace=True)
self.layer1 = self._make_layer(block, width[0], num_blocks[0], stride=1)
self.layer2 = self._make_layer(block, width[1], num_blocks[1], stride=2)
self.layer3 = self._make_layer(block, width[2], num_blocks[2], stride=2)
self.layer4 = self._make_layer(block, width[3], num_blocks[3], stride=2)
self.fc_cifar = nn.Linear(width[3]*block.widen_factor, cifar_classes)
self.fc_tiny = nn.Linear(width[3]*block.widen_factor, tiny_classes)
self.fc_vgg = nn.Linear(width[3]*block.widen_factor, vgg_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
m.bias.data.zero_()
def _make_layer(self, block, planes, num_blocks, stride):
strides = [stride] + [1]*(num_blocks-1)
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, stride))
self.in_planes = planes * block.widen_factor
return nn.Sequential(*layers)
def forward(self, x_cifar, x_tiny, x_vgg):
#global FLOPS
#FLOPS = 0
out = F.relu(self.bn1(self.conv1(x)))
#FLOPS += self.conv1.in_channelsself.conv1.out_channelsself.conv1.kernel_size[0]*self.conv1.kernel_size[1]*out.size(2)*out.size(3)
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = F.avg_pool2d(out, 8)
out_cifar = self.base(x_cifar)
out_tiny = self.base(x_tiny)
out_vgg = self.base(x_vgg)
out_cifar = out_cifar.view(out_cifar.size(0), -1)
out_tiny = out_tiny.view(out_tiny.size(0), -1)
out_vgg = out_vgg.view(out_vgg.size(0), -1)
out_cifar = self.fc_cifar(out_cifar)
out_tiny = self.fc_tiny(out_tiny)
out_vgg = self.fc_vgg(out_vgg)
#FLOPS += self.fc.in_features*self.fc.out_features
return out_cifar, out_tiny, out_vgg
Multi-Task Learning Using Uncertainty to Weigh Losses for Scene Geometry and Semantics
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.