-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add training script and using config file format (#139)
- Loading branch information
1 parent
11eb547
commit 52eda71
Showing
24 changed files
with
1,083 additions
and
6 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,2 @@ | ||
MODEL: | ||
NAME: 'resnet50' |
File renamed without changes.
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
from .resnest import * | ||
from .ablation import * | ||
from .models import * |
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,45 @@ | ||
import os | ||
from fvcore.common.config import CfgNode as CN | ||
|
||
_C = CN() | ||
|
||
_C.SEED = 1 | ||
|
||
## data related | ||
_C.DATA = CN() | ||
_C.DATA.DATASET = 'ImageNet' | ||
# assuming you've set up the dataset using provided script | ||
_C.DATA.ROOT = os.path.expanduser('~/.encoding/data/ILSVRC2012') | ||
_C.DATA.BASE_SIZE = None | ||
_C.DATA.CROP_SIZE = 224 | ||
_C.DATA.LABEL_SMOOTHING = 0.0 | ||
_C.DATA.MIXUP = 0.0 | ||
_C.DATA.RAND_AUG = False | ||
|
||
## model related | ||
_C.MODEL = CN() | ||
_C.MODEL.NAME = 'resnet50' | ||
_C.MODEL.FINAL_DROP = False | ||
|
||
## training params | ||
_C.TRAINING = CN() | ||
# (per-gpu batch size) | ||
_C.TRAINING.BATCH_SIZE = 64 | ||
_C.TRAINING.TEST_BATCH_SIZE = 256 | ||
_C.TRAINING.LAST_GAMMA = False | ||
_C.TRAINING.EPOCHS = 120 | ||
_C.TRAINING.START_EPOCHS = 0 | ||
_C.TRAINING.WORKERS = 4 | ||
|
||
## optimizer params | ||
_C.OPTIMIZER = CN() | ||
# (per-gpu lr) | ||
_C.OPTIMIZER.LR = 0.025 | ||
_C.OPTIMIZER.LR_SCHEDULER = 'cos' | ||
_C.OPTIMIZER.MOMENTUM = 0.9 | ||
_C.OPTIMIZER.WEIGHT_DECAY = 1e-4 | ||
_C.OPTIMIZER.DISABLE_BN_WD = False | ||
_C.OPTIMIZER.WARMUP_EPOCHS = 0 | ||
|
||
def get_cfg() -> CN: | ||
return _C.clone() |
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,2 @@ | ||
from .build import get_dataset, RESNEST_DATASETS_REGISTRY | ||
from .imagenet import ImageNet |
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,6 @@ | ||
from fvcore.common.registry import Registry | ||
|
||
RESNEST_DATASETS_REGISTRY = Registry('RESNEST_DATASETS') | ||
|
||
def get_dataset(dataset_name): | ||
return RESNEST_DATASETS_REGISTRY.get(dataset_name) |
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,25 @@ | ||
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
## Created by: Hang Zhang | ||
## Email: [email protected] | ||
## Copyright (c) 2018 | ||
## | ||
## This source code is licensed under the MIT-style license found in the | ||
## LICENSE file in the root directory of this source tree | ||
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
||
import os | ||
import torchvision.transforms as transforms | ||
import torchvision.datasets as datasets | ||
|
||
import warnings | ||
warnings.filterwarnings("ignore", "(Possibly )?corrupt EXIF data", UserWarning) | ||
|
||
from .build import RESNEST_DATASETS_REGISTRY | ||
|
||
@RESNEST_DATASETS_REGISTRY.register() | ||
class ImageNet(datasets.ImageFolder): | ||
def __init__(self, root=os.path.expanduser('~/.encoding/data/ILSVRC2012'), transform=None, | ||
target_transform=None, train=True, **kwargs): | ||
split='train' if train == True else 'val' | ||
root = os.path.join(root, split) | ||
super().__init__(root, transform, target_transform) |
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,63 @@ | ||
import torch | ||
import torch.nn.functional as F | ||
import torch.nn as nn | ||
from torch.autograd import Variable | ||
from resnest.torch.utils import MixUpWrapper | ||
|
||
__all__ = ['LabelSmoothing', 'NLLMultiLabelSmooth', 'get_criterion'] | ||
|
||
def get_criterion(cfg, train_loader, gpu): | ||
if cfg.DATA.MIXUP > 0: | ||
train_loader = MixUpWrapper(cfg.DATA.MIXUP, 1000, train_loader, gpu) | ||
criterion = NLLMultiLabelSmooth(cfg.DATA.LABEL_SMOOTHING) | ||
elif cfg.DATA.LABEL_SMOOTHING > 0.0: | ||
criterion = LabelSmoothing(cfg.DATA.LABEL_SMOOTHING) | ||
else: | ||
criterion = torch.nn.CrossEntropyLoss() | ||
return criterion, train_loader | ||
|
||
class LabelSmoothing(nn.Module): | ||
""" | ||
NLL loss with label smoothing. | ||
""" | ||
def __init__(self, smoothing=0.1): | ||
""" | ||
Constructor for the LabelSmoothing module. | ||
:param smoothing: label smoothing factor | ||
""" | ||
super(LabelSmoothing, self).__init__() | ||
self.confidence = 1.0 - smoothing | ||
self.smoothing = smoothing | ||
|
||
def forward(self, x, target): | ||
logprobs = torch.nn.functional.log_softmax(x, dim=-1) | ||
|
||
nll_loss = -logprobs.gather(dim=-1, index=target.unsqueeze(1)) | ||
nll_loss = nll_loss.squeeze(1) | ||
smooth_loss = -logprobs.mean(dim=-1) | ||
loss = self.confidence * nll_loss + self.smoothing * smooth_loss | ||
return loss.mean() | ||
|
||
class NLLMultiLabelSmooth(nn.Module): | ||
def __init__(self, smoothing = 0.1): | ||
super(NLLMultiLabelSmooth, self).__init__() | ||
self.confidence = 1.0 - smoothing | ||
self.smoothing = smoothing | ||
|
||
def forward(self, x, target): | ||
if self.training: | ||
x = x.float() | ||
target = target.float() | ||
logprobs = torch.nn.functional.log_softmax(x, dim = -1) | ||
|
||
nll_loss = -logprobs * target | ||
nll_loss = nll_loss.sum(-1) | ||
|
||
smooth_loss = -logprobs.mean(dim=-1) | ||
|
||
loss = self.confidence * nll_loss + self.smoothing * smooth_loss | ||
|
||
return loss.mean() | ||
else: | ||
return torch.nn.functional.cross_entropy(x, target) | ||
|
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,2 @@ | ||
from .resnest import * | ||
from .ablation import * |
File renamed without changes.
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,6 @@ | ||
from fvcore.common.registry import Registry | ||
|
||
RESNEST_MODELS_REGISTRY = Registry('RESNEST_MODELS') | ||
|
||
def get_model(model_name): | ||
return RESNEST_MODELS_REGISTRY.get(model_name) |
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
File renamed without changes.
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 @@ | ||
from .build import get_transform, RESNEST_TRANSFORMS_REGISTRY |
Oops, something went wrong.