-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DALI; all_reduce to gather results; fitnet, kd script
- Loading branch information
Showing
8 changed files
with
291 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
# https://github.com/NVIDIA/DALI/blob/master/docs/examples/use_cases/pytorch/resnet50/main.py | ||
|
||
import argparse | ||
import os | ||
import shutil | ||
import time | ||
import math | ||
|
||
import torch | ||
import torch.nn as nn | ||
import torch.nn.parallel | ||
import torch.backends.cudnn as cudnn | ||
import torch.distributed as dist | ||
import torch.optim | ||
import torch.utils.data | ||
import torch.utils.data.distributed | ||
import torchvision.transforms as transforms | ||
import torchvision.datasets as datasets | ||
import torchvision.models as models | ||
|
||
import numpy as np | ||
|
||
try: | ||
from nvidia.dali.plugin.pytorch import DALIClassificationIterator | ||
from nvidia.dali.pipeline import Pipeline | ||
import nvidia.dali.ops as ops | ||
import nvidia.dali.types as types | ||
except ImportError: | ||
raise ImportError("Please install DALI from https://www.github.com/NVIDIA/DALI to run this example.") | ||
|
||
class HybridTrainPipe(Pipeline): | ||
def __init__(self, batch_size, num_threads, device_id, data_dir, crop, | ||
shard_id, num_shards, dali_cpu=False): | ||
super(HybridTrainPipe, self).__init__(batch_size, | ||
num_threads, | ||
device_id, | ||
seed=12 + device_id) | ||
self.input = ops.FileReader(file_root=data_dir, | ||
shard_id=shard_id, | ||
num_shards=num_shards, | ||
shuffle_after_epoch=True, | ||
pad_last_batch=True) | ||
#let user decide which pipeline works him bets for RN version he runs | ||
dali_device = 'cpu' if dali_cpu else 'gpu' | ||
decoder_device = 'cpu' if dali_cpu else 'mixed' | ||
# This padding sets the size of the internal nvJPEG buffers to be able to handle all images from full-sized ImageNet | ||
# without additional reallocations | ||
device_memory_padding = 211025920 if decoder_device == 'mixed' else 0 | ||
host_memory_padding = 140544512 if decoder_device == 'mixed' else 0 | ||
self.decode = ops.ImageDecoderRandomCrop(device=decoder_device, output_type=types.RGB, | ||
device_memory_padding=device_memory_padding, | ||
host_memory_padding=host_memory_padding, | ||
random_aspect_ratio=[0.8, 1.25], | ||
random_area=[0.1, 1.0], | ||
num_attempts=100) | ||
self.res = ops.Resize(device=dali_device, | ||
resize_x=crop, | ||
resize_y=crop, | ||
interp_type=types.INTERP_TRIANGULAR) | ||
self.cmnp = ops.CropMirrorNormalize(device="gpu", | ||
output_dtype=types.FLOAT, | ||
output_layout=types.NCHW, | ||
crop=(crop, crop), | ||
mean=[0.485 * 255,0.456 * 255,0.406 * 255], | ||
std=[0.229 * 255,0.224 * 255,0.225 * 255]) | ||
self.coin = ops.CoinFlip(probability=0.5) | ||
print('DALI "{0}" variant'.format(dali_device)) | ||
|
||
def define_graph(self): | ||
rng = self.coin() | ||
self.jpegs, self.labels = self.input(name="Reader") | ||
images = self.decode(self.jpegs) | ||
images = self.res(images) | ||
output = self.cmnp(images.gpu(), mirror=rng) | ||
return [output, self.labels] | ||
|
||
class HybridValPipe(Pipeline): | ||
def __init__(self, batch_size, num_threads, device_id, data_dir, crop, | ||
size, shard_id, num_shards): | ||
super(HybridValPipe, self).__init__(batch_size, | ||
num_threads, | ||
device_id, | ||
seed=12 + device_id) | ||
self.input = ops.FileReader(file_root=data_dir, | ||
shard_id=shard_id, | ||
num_shards=num_shards, | ||
random_shuffle=False, | ||
pad_last_batch=True) | ||
self.decode = ops.ImageDecoder(device="mixed", output_type=types.RGB) | ||
self.res = ops.Resize(device="gpu", | ||
resize_shorter=size, | ||
interp_type=types.INTERP_TRIANGULAR) | ||
self.cmnp = ops.CropMirrorNormalize(device="gpu", | ||
output_dtype=types.FLOAT, | ||
output_layout=types.NCHW, | ||
crop=(crop, crop), | ||
mean=[0.485 * 255,0.456 * 255,0.406 * 255], | ||
std=[0.229 * 255,0.224 * 255,0.225 * 255]) | ||
|
||
def define_graph(self): | ||
self.jpegs, self.labels = self.input(name="Reader") | ||
images = self.decode(self.jpegs) | ||
images = self.res(images) | ||
output = self.cmnp(images) | ||
return [output, self.labels] | ||
|
||
from dataset.imagenet import get_data_folder | ||
def get_dali_data_loader(args): | ||
crop_size = 224 | ||
val_size = 256 | ||
|
||
data_folder = get_data_folder() | ||
train_folder = os.path.join(data_folder, 'train') | ||
val_folder = os.path.join(data_folder, 'val') | ||
|
||
pipe = HybridTrainPipe(batch_size=args.batch_size, | ||
num_threads=args.num_workers, | ||
device_id=args.rank, | ||
data_dir=train_folder, | ||
crop=crop_size, | ||
dali_cpu=args.dali == 'cpu', | ||
shard_id=args.rank, | ||
num_shards=args.world_size) | ||
pipe.build() | ||
train_loader = DALIClassificationIterator(pipe, reader_name="Reader", fill_last_batch=False) | ||
|
||
pipe = HybridValPipe(batch_size=args.batch_size, | ||
num_threads=args.num_workers, | ||
device_id=args.rank, | ||
data_dir=val_folder, | ||
crop=crop_size, | ||
size=val_size, | ||
shard_id=args.rank, | ||
num_shards=args.world_size) | ||
pipe.build() | ||
val_loader = DALIClassificationIterator(pipe, reader_name="Reader", fill_last_batch=False) | ||
|
||
return train_loader, val_loader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
python train_student.py --path-t ./save/models/ResNet34_vanilla/resnet34_transformed.pth \ | ||
--batch_size 256 --epochs 90 --dataset imagenet --gpu_id 0,1,2,3 --dist-url tcp:https://127.0.0.1:23334 \ | ||
--print-freq 100 --num_workers 16 --distill hint --model_s ResNet18 -r 1 -a 1 -b 100 --trial 0 \ | ||
--multiprocessing-distributed --learning_rate 0.1 --lr_decay_epochs 30,60,90 --weight_decay 1e-4 --hint_layer 1 \ | ||
--dali gpu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
python train_student.py --path-t ./save/models/ResNet34_vanilla/resnet34_transformed.pth \ | ||
--batch_size 256 --epochs 90 --dataset imagenet --gpu_id 4,5,6,7 --dist-url tcp:https://127.0.0.1:23333 \ | ||
--print-freq 100 --num_workers 16 --distill kd --model_s ResNet18 -r 1 -a 1 -b 0 --trial 0 \ | ||
--multiprocessing-distributed --learning_rate 0.1 --lr_decay_epochs 30,60 --weight_decay 1e-4 --dali gpu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import torch | ||
|
||
if __name__ == '__main__': | ||
state_dict = torch.load('save/models/ResNet34_vanilla/resnet34-333f7ec4.pth') | ||
torch.save({ | ||
# 'epoch': model['epoch'], | ||
'model': state_dict, | ||
# 'best_acc': model['best_acc1'] | ||
}, 'save/models/ResNet34_vanilla/resnet34_transformed.pth') |
Oops, something went wrong.