-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update dot: a distillation-oriented trainer & configs
- Loading branch information
Showing
20 changed files
with
805 additions
and
10 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,20 @@ | ||
EXPERIMENT: | ||
NAME: "" | ||
TAG: "kd,dot,res32x4,res8x4" | ||
PROJECT: "dot_cifar" | ||
DISTILLER: | ||
TYPE: "KD" | ||
TEACHER: "resnet32x4" | ||
STUDENT: "resnet8x4" | ||
SOLVER: | ||
BATCH_SIZE: 64 | ||
EPOCHS: 240 | ||
LR: 0.05 | ||
LR_DECAY_STAGES: [150, 180, 210] | ||
LR_DECAY_RATE: 0.1 | ||
WEIGHT_DECAY: 0.0005 | ||
MOMENTUM: 0.9 | ||
TYPE: "SGD" | ||
TRAINER: "dot" | ||
DOT: | ||
DELTA: 0.075 |
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,20 @@ | ||
EXPERIMENT: | ||
NAME: "" | ||
TAG: "kd,dot,res32x4,shuv2" | ||
PROJECT: "dot_cifar" | ||
DISTILLER: | ||
TYPE: "KD" | ||
TEACHER: "resnet32x4" | ||
STUDENT: "ShuffleV2" | ||
SOLVER: | ||
BATCH_SIZE: 64 | ||
EPOCHS: 240 | ||
LR: 0.01 | ||
LR_DECAY_STAGES: [150, 180, 210] | ||
LR_DECAY_RATE: 0.1 | ||
WEIGHT_DECAY: 0.0005 | ||
MOMENTUM: 0.9 | ||
TYPE: "SGD" | ||
TRAINER: "dot" | ||
DOT: | ||
DELTA: 0.075 |
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,20 @@ | ||
EXPERIMENT: | ||
NAME: "" | ||
TAG: "kd,dot,vgg13,vgg8" | ||
PROJECT: "dot_cifar" | ||
DISTILLER: | ||
TYPE: "KD" | ||
TEACHER: "vgg13" | ||
STUDENT: "vgg8" | ||
SOLVER: | ||
BATCH_SIZE: 64 | ||
EPOCHS: 240 | ||
LR: 0.05 | ||
LR_DECAY_STAGES: [150, 180, 210] | ||
LR_DECAY_RATE: 0.1 | ||
WEIGHT_DECAY: 0.0005 | ||
MOMENTUM: 0.9 | ||
TYPE: "SGD" | ||
TRAINER: "dot" | ||
DOT: | ||
DELTA: 0.075 |
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,33 @@ | ||
EXPERIMENT: | ||
NAME: "" | ||
TAG: "kd,dot,res34,res18" | ||
PROJECT: "dot_imagenet" | ||
DATASET: | ||
TYPE: "imagenet" | ||
NUM_WORKERS: 32 | ||
TEST: | ||
BATCH_SIZE: 128 | ||
DISTILLER: | ||
TYPE: "KD" | ||
TEACHER: "ResNet34" | ||
STUDENT: "ResNet18" | ||
SOLVER: | ||
BATCH_SIZE: 512 | ||
EPOCHS: 100 | ||
LR: 0.2 | ||
LR_DECAY_STAGES: [30, 60, 90] | ||
LR_DECAY_RATE: 0.1 | ||
WEIGHT_DECAY: 0.0001 | ||
MOMENTUM: 0.9 | ||
TYPE: "SGD" | ||
TRAINER: "dot" | ||
DOT: | ||
DELTA: 0.09 | ||
KD: | ||
TEMPERATURE: 1 | ||
LOSS: | ||
CE_WEIGHT: 0.5 | ||
KD_WEIGHT: 0.5 | ||
LOG: | ||
TENSORBOARD_FREQ: 50 | ||
SAVE_CHECKPOINT_FREQ: 10 |
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,33 @@ | ||
EXPERIMENT: | ||
NAME: "" | ||
TAG: "kd,dot,res50,mobilenetv1" | ||
PROJECT: "dot_imagenet" | ||
DATASET: | ||
TYPE: "imagenet" | ||
NUM_WORKERS: 32 | ||
TEST: | ||
BATCH_SIZE: 128 | ||
DISTILLER: | ||
TYPE: "KD" | ||
TEACHER: "ResNet50" | ||
STUDENT: "MobileNetV1" | ||
SOLVER: | ||
BATCH_SIZE: 512 | ||
EPOCHS: 100 | ||
LR: 0.2 | ||
LR_DECAY_STAGES: [30, 60, 90] | ||
LR_DECAY_RATE: 0.1 | ||
WEIGHT_DECAY: 0.0001 | ||
MOMENTUM: 0.9 | ||
TYPE: "SGD" | ||
TRAINER: "dot" | ||
DOT: | ||
DELTA: 0.09 | ||
KD: | ||
TEMPERATURE: 1 | ||
LOSS: | ||
CE_WEIGHT: 0.5 | ||
KD_WEIGHT: 0.5 | ||
LOG: | ||
TENSORBOARD_FREQ: 50 | ||
SAVE_CHECKPOINT_FREQ: 10 |
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,23 @@ | ||
EXPERIMENT: | ||
NAME: "" | ||
TAG: "kd,dot,r18,mv2" | ||
PROJECT: "dot_tinyimagenet" | ||
DATASET: | ||
TYPE: "tiny_imagenet" | ||
NUM_WORKERS: 16 | ||
DISTILLER: | ||
TYPE: "KD" | ||
TEACHER: "ResNet18" | ||
STUDENT: "MobileNetV2" | ||
SOLVER: | ||
BATCH_SIZE: 256 | ||
EPOCHS: 200 | ||
LR: 0.2 | ||
LR_DECAY_STAGES: [60, 120, 160] | ||
LR_DECAY_RATE: 0.1 | ||
WEIGHT_DECAY: 0.0005 | ||
MOMENTUM: 0.9 | ||
TYPE: "SGD" | ||
TRAINER: "dot" | ||
DOT: | ||
DELTA: 0.075 |
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,23 @@ | ||
EXPERIMENT: | ||
NAME: "" | ||
TAG: "kd,dot,r18,shuv2" | ||
PROJECT: "dot_tinyimagenet" | ||
DATASET: | ||
TYPE: "tiny_imagenet" | ||
NUM_WORKERS: 16 | ||
DISTILLER: | ||
TYPE: "KD" | ||
TEACHER: "ResNet18" | ||
STUDENT: "ShuffleV2" | ||
SOLVER: | ||
BATCH_SIZE: 256 | ||
EPOCHS: 200 | ||
LR: 0.2 | ||
LR_DECAY_STAGES: [60, 120, 160] | ||
LR_DECAY_RATE: 0.1 | ||
WEIGHT_DECAY: 0.0005 | ||
MOMENTUM: 0.9 | ||
TYPE: "SGD" | ||
TRAINER: "dot" | ||
DOT: | ||
DELTA: 0.075 |
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,122 @@ | ||
import os | ||
from torch.utils.data import DataLoader | ||
from torchvision import datasets | ||
from torchvision import transforms | ||
import numpy as np | ||
|
||
|
||
data_folder = os.path.join( | ||
os.path.dirname(os.path.abspath(__file__)), "../../data/tiny-imagenet-200" | ||
) | ||
|
||
|
||
class ImageFolderInstance(datasets.ImageFolder): | ||
def __getitem__(self, index): | ||
path, target = self.imgs[index] | ||
img = self.loader(path) | ||
if self.transform is not None: | ||
img = self.transform(img) | ||
return img, target, index | ||
|
||
|
||
class ImageFolderInstanceSample(ImageFolderInstance): | ||
""": Folder datasets which returns (img, label, index, contrast_index): | ||
""" | ||
def __init__(self, folder, transform=None, target_transform=None, | ||
is_sample=False, k=4096): | ||
super().__init__(folder, transform=transform) | ||
|
||
self.k = k | ||
self.is_sample = is_sample | ||
if self.is_sample: | ||
num_classes = 200 | ||
num_samples = len(self.samples) | ||
label = np.zeros(num_samples, dtype=np.int32) | ||
for i in range(num_samples): | ||
img, target = self.samples[i] | ||
label[i] = target | ||
|
||
self.cls_positive = [[] for i in range(num_classes)] | ||
for i in range(num_samples): | ||
self.cls_positive[label[i]].append(i) | ||
|
||
self.cls_negative = [[] for i in range(num_classes)] | ||
for i in range(num_classes): | ||
for j in range(num_classes): | ||
if j == i: | ||
continue | ||
self.cls_negative[i].extend(self.cls_positive[j]) | ||
|
||
self.cls_positive = [np.asarray(self.cls_positive[i], dtype=np.int32) for i in range(num_classes)] | ||
self.cls_negative = [np.asarray(self.cls_negative[i], dtype=np.int32) for i in range(num_classes)] | ||
print('dataset initialized!') | ||
|
||
def __getitem__(self, index): | ||
""" | ||
Args: | ||
index (int): Index | ||
Returns: | ||
tuple: (image, target) where target is class_index of the target class. | ||
""" | ||
img, target, index = super().__getitem__(index) | ||
|
||
if self.is_sample: | ||
# sample contrastive examples | ||
pos_idx = index | ||
neg_idx = np.random.choice(self.cls_negative[target], self.k, replace=True) | ||
sample_idx = np.hstack((np.asarray([pos_idx]), neg_idx)) | ||
return img, target, index, sample_idx | ||
else: | ||
return img, target, index | ||
|
||
|
||
def get_tinyimagenet_dataloader(batch_size, val_batch_size, num_workers): | ||
"""Data Loader for tiny-imagenet""" | ||
train_transform = transforms.Compose([ | ||
transforms.RandomRotation(20), | ||
transforms.RandomHorizontalFlip(0.5), | ||
transforms.ToTensor(), | ||
transforms.Normalize([0.4802, 0.4481, 0.3975], [0.2302, 0.2265, 0.2262]), | ||
]) | ||
test_transform = transforms.Compose([ | ||
transforms.ToTensor(), | ||
transforms.Normalize([0.4802, 0.4481, 0.3975], [0.2302, 0.2265, 0.2262]), | ||
]) | ||
train_folder = os.path.join(data_folder, "train") | ||
test_folder = os.path.join(data_folder, "val") | ||
train_set = ImageFolderInstance(train_folder, transform=train_transform) | ||
num_data = len(train_set) | ||
test_set = datasets.ImageFolder(test_folder, transform=test_transform) | ||
train_loader = DataLoader( | ||
train_set, batch_size=batch_size, shuffle=True, num_workers=num_workers | ||
) | ||
test_loader = DataLoader( | ||
test_set, batch_size=val_batch_size, shuffle=False, num_workers=1 | ||
) | ||
return train_loader, test_loader, num_data | ||
|
||
|
||
def get_tinyimagenet_dataloader_sample(batch_size, val_batch_size, num_workers, k): | ||
"""Data Loader for tiny-imagenet""" | ||
train_transform = transforms.Compose([ | ||
transforms.RandomRotation(20), | ||
transforms.RandomHorizontalFlip(0.5), | ||
transforms.ToTensor(), | ||
transforms.Normalize([0.4802, 0.4481, 0.3975], [0.2302, 0.2265, 0.2262]), | ||
]) | ||
test_transform = transforms.Compose([ | ||
transforms.ToTensor(), | ||
transforms.Normalize([0.4802, 0.4481, 0.3975], [0.2302, 0.2265, 0.2262]), | ||
]) | ||
train_folder = os.path.join(data_folder, "train") | ||
test_folder = os.path.join(data_folder, "val") | ||
train_set = ImageFolderInstanceSample(train_folder, transform=train_transform, is_sample=True, k=k) | ||
num_data = len(train_set) | ||
test_set = datasets.ImageFolder(test_folder, transform=test_transform) | ||
train_loader = DataLoader( | ||
train_set, batch_size=batch_size, shuffle=True, num_workers=num_workers | ||
) | ||
test_loader = DataLoader( | ||
test_set, batch_size=val_batch_size, shuffle=False, num_workers=1 | ||
) | ||
return train_loader, test_loader, num_data |
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,6 +1,7 @@ | ||
from .trainer import BaseTrainer, CRDTrainer | ||
|
||
from .trainer import BaseTrainer, CRDTrainer, DOT, CRDDOT | ||
trainer_dict = { | ||
"base": BaseTrainer, | ||
"crd": CRDTrainer, | ||
"dot": DOT, | ||
"crd_dot": CRDDOT, | ||
} |
Oops, something went wrong.