Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added the contiguous param tricks to training scripts #79

Merged
merged 2 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions references/classification/requirements.txt

This file was deleted.

13 changes: 9 additions & 4 deletions references/classification/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from torch import nn
import torchvision
from torchvision import transforms
from contiguous_params import ContiguousParams

import holocron

Expand Down Expand Up @@ -157,17 +158,19 @@ def main(args):
elif args.loss == 'label_smoothing':
criterion = holocron.nn.LabelSmoothingCrossEntropy()

# Create the contiguous parameters.
model_params = ContiguousParams([p for p in model.parameters() if p.requires_grad])
if args.opt == 'adam':
optimizer = torch.optim.Adam([p for p in model.parameters() if p.requires_grad], args.lr,
optimizer = torch.optim.Adam(model_params.contiguous(), args.lr,
betas=(0.95, 0.99), eps=1e-6, weight_decay=args.weight_decay)
elif args.opt == 'radam':
optimizer = holocron.optim.RAdam([p for p in model.parameters() if p.requires_grad], args.lr,
optimizer = holocron.optim.RAdam(model_params.contiguous(), args.lr,
betas=(0.95, 0.99), eps=1e-6, weight_decay=args.weight_decay)
elif args.opt == 'ranger':
optimizer = Lookahead(holocron.optim.RAdam([p for p in model.parameters() if p.requires_grad], args.lr,
optimizer = Lookahead(holocron.optim.RAdam(model_params.contiguous(), args.lr,
betas=(0.95, 0.99), eps=1e-6, weight_decay=args.weight_decay))
elif args.opt == 'tadam':
optimizer = holocron.optim.TAdam([p for p in model.parameters() if p.requires_grad], args.lr,
optimizer = holocron.optim.TAdam(model_params.contiguous(), args.lr,
betas=(0.95, 0.99), eps=1e-6, weight_decay=args.weight_decay)

if args.lr_finder:
Expand Down Expand Up @@ -200,6 +203,8 @@ def main(args):
mb = master_bar(range(args.start_epoch, args.epochs))
for epoch in mb:
train_one_epoch(model, criterion, optimizer, lr_scheduler, train_loader, device, mb)
# Check that the optimizer only applies valid ops.
model_params.assert_buffer_is_valid()
val_loss, acc1, acc5 = evaluate(model, criterion, val_loader, device=device)
if args.sched == 'plateau':
lr_scheduler.step(val_loss)
Expand Down
4 changes: 0 additions & 4 deletions references/detection/requirements.txt

This file was deleted.

12 changes: 8 additions & 4 deletions references/detection/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from torchvision.datasets import VOCDetection
from torchvision.ops.boxes import box_iou
from torchvision.transforms import functional as F
from contiguous_params import ContiguousParams

import holocron
from transforms import (Compose, VOCTargetTransform, Resize, ImageTransform, CenterCrop, RandomResizedCrop,
Expand Down Expand Up @@ -231,17 +232,18 @@ def main(args):
p.requires_grad_(False)
model.to(device)

model_params = ContiguousParams([p for p in model.parameters() if p.requires_grad])
if args.opt == 'adam':
optimizer = torch.optim.Adam([p for p in model.parameters() if p.requires_grad], args.lr,
optimizer = torch.optim.Adam(model_params.contiguous(), args.lr,
betas=(0.95, 0.99), eps=1e-6, weight_decay=args.weight_decay)
elif args.opt == 'radam':
optimizer = holocron.optim.RAdam([p for p in model.parameters() if p.requires_grad], args.lr,
optimizer = holocron.optim.RAdam(model_params.contiguous(), args.lr,
betas=(0.95, 0.99), eps=1e-6, weight_decay=args.weight_decay)
elif args.opt == 'ranger':
optimizer = Lookahead(holocron.optim.RAdam([p for p in model.parameters() if p.requires_grad], args.lr,
optimizer = Lookahead(holocron.optim.RAdam(model_params.contiguous(), args.lr,
betas=(0.95, 0.99), eps=1e-6, weight_decay=args.weight_decay))
elif args.opt == 'tadam':
optimizer = holocron.optim.TAdam([p for p in model.parameters() if p.requires_grad], args.lr,
optimizer = holocron.optim.TAdam(model_params.contiguous(), args.lr,
betas=(0.95, 0.99), eps=1e-6, weight_decay=args.weight_decay)

if args.lr_finder:
Expand Down Expand Up @@ -276,6 +278,8 @@ def main(args):
mb = master_bar(range(args.start_epoch, args.epochs))
for epoch in mb:
train_one_epoch(model, optimizer, lr_scheduler, train_loader, device, mb)
# Check that the optimizer only applies valid ops.
model_params.assert_buffer_is_valid()
loc_err, clf_err, det_err = evaluate(model, val_loader, device=device)
mb.main_bar.comment = f"Epoch {args.start_epoch+epoch+1}/{args.start_epoch+args.epochs}"
mb.write(f"Epoch {args.start_epoch+epoch+1}/{args.start_epoch+args.epochs} - "
Expand Down
5 changes: 5 additions & 0 deletions references/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pylocron
tqdm
fastprogress>=0.1.21
matplotlib>=3.0.0
contiguous_params @ git+https://github.com/philjd/contiguous_pytorch_params.git#egg=contiguous_params
4 changes: 0 additions & 4 deletions references/segmentation/requirements.txt

This file was deleted.

10 changes: 7 additions & 3 deletions references/segmentation/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from torchvision.datasets import VOCSegmentation
from torchvision.ops.misc import FrozenBatchNorm2d
from torchvision.transforms import functional as F
from contiguous_params import ContiguousParams

import holocron
from transforms import (Compose, Resize, ImageTransform, CenterCrop, RandomResizedCrop,
Expand Down Expand Up @@ -192,14 +193,15 @@ def main(args):
p.requires_grad_(False)
model.to(device)

model_params = ContiguousParams([p for p in model.parameters() if p.requires_grad])
if args.opt == 'adam':
optimizer = torch.optim.Adam(model.parameters(), args.lr, betas=(0.95, 0.99), eps=1e-6,
optimizer = torch.optim.Adam(model_params.contiguous(), args.lr, betas=(0.95, 0.99), eps=1e-6,
weight_decay=args.weight_decay)
elif args.opt == 'radam':
optimizer = holocron.optim.RAdam(model.parameters(), args.lr, betas=(0.95, 0.99), eps=1e-6,
optimizer = holocron.optim.RAdam(model_params.contiguous(), args.lr, betas=(0.95, 0.99), eps=1e-6,
weight_decay=args.weight_decay)
elif args.opt == 'ranger':
optimizer = Lookahead(holocron.optim.RAdam(model.parameters(), args.lr, betas=(0.95, 0.99), eps=1e-6,
optimizer = Lookahead(holocron.optim.RAdam(model_params.contiguous(), args.lr, betas=(0.95, 0.99), eps=1e-6,
weight_decay=args.weight_decay))

loss_weight = torch.ones(len(classes))
Expand Down Expand Up @@ -238,6 +240,8 @@ def main(args):
mb = master_bar(range(args.start_epoch, args.epochs))
for epoch in mb:
train_one_epoch(model, optimizer, criterion, lr_scheduler, train_loader, device, mb)
# Check that the optimizer only applies valid ops.
model_params.assert_buffer_is_valid()
val_loss, mean_iou = evaluate(model, val_loader, criterion, device=device)
mb.main_bar.comment = f"Epoch {args.start_epoch+epoch+1}/{args.start_epoch+args.epochs}"
mb.write(f"Epoch {args.start_epoch+epoch+1}/{args.start_epoch+args.epochs} - "
Expand Down