-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Adding Preset Transforms in reference scripts #3317
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
Merged
datumbox
merged 5 commits into
pytorch:master
from
datumbox:references/preset_transforms
Jan 28, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
992d41f
Adding presets in the classification reference scripts.
datumbox 9f7a0f7
Adding presets in the object detection reference scripts.
datumbox a2e9306
Adding presets in the segmentation reference scripts.
datumbox 826659e
Adding presets in the video classification reference scripts.
datumbox ba326de
Moving flip at the end to align with image classification signature.
datumbox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,37 @@ | ||
from torchvision.transforms import autoaugment, transforms | ||
|
||
|
||
class ClassificationPresetTrain: | ||
def __init__(self, crop_size, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225), hflip_prob=0.5, | ||
auto_augment_policy=None, random_erase_prob=0.0): | ||
trans = [transforms.RandomResizedCrop(crop_size)] | ||
if hflip_prob > 0: | ||
trans.append(transforms.RandomHorizontalFlip(hflip_prob)) | ||
if auto_augment_policy is not None: | ||
aa_policy = autoaugment.AutoAugmentPolicy(auto_augment_policy) | ||
trans.append(autoaugment.AutoAugment(policy=aa_policy)) | ||
trans.extend([ | ||
transforms.ToTensor(), | ||
transforms.Normalize(mean=mean, std=std), | ||
]) | ||
if random_erase_prob > 0: | ||
trans.append(transforms.RandomErasing(p=random_erase_prob)) | ||
|
||
self.transforms = transforms.Compose(trans) | ||
|
||
def __call__(self, img): | ||
return self.transforms(img) | ||
|
||
|
||
class ClassificationPresetEval: | ||
def __init__(self, crop_size, resize_size=256, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)): | ||
|
||
self.transforms = transforms.Compose([ | ||
transforms.Resize(resize_size), | ||
transforms.CenterCrop(crop_size), | ||
transforms.ToTensor(), | ||
transforms.Normalize(mean=mean, std=std), | ||
]) | ||
|
||
def __call__(self, img): | ||
return self.transforms(img) |
This file contains hidden or 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 hidden or 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,21 @@ | ||
import transforms as T | ||
|
||
|
||
class DetectionPresetTrain: | ||
def __init__(self, hflip_prob=0.5): | ||
trans = [T.ToTensor()] | ||
if hflip_prob > 0: | ||
trans.append(T.RandomHorizontalFlip(hflip_prob)) | ||
|
||
self.transforms = T.Compose(trans) | ||
|
||
def __call__(self, img, target): | ||
return self.transforms(img, target) | ||
|
||
|
||
class DetectionPresetEval: | ||
def __init__(self): | ||
self.transforms = T.ToTensor() | ||
|
||
def __call__(self, img, target): | ||
return self.transforms(img, target) |
This file contains hidden or 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 hidden or 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,32 @@ | ||
import transforms as T | ||
|
||
|
||
class SegmentationPresetTrain: | ||
def __init__(self, base_size, crop_size, hflip_prob=0.5, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)): | ||
min_size = int(0.5 * base_size) | ||
max_size = int(2.0 * base_size) | ||
|
||
trans = [T.RandomResize(min_size, max_size)] | ||
if hflip_prob > 0: | ||
trans.append(T.RandomHorizontalFlip(hflip_prob)) | ||
trans.extend([ | ||
T.RandomCrop(crop_size), | ||
T.ToTensor(), | ||
T.Normalize(mean=mean, std=std), | ||
]) | ||
self.transforms = T.Compose(trans) | ||
|
||
def __call__(self, img, target): | ||
return self.transforms(img, target) | ||
|
||
|
||
class SegmentationPresetEval: | ||
def __init__(self, base_size, mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)): | ||
self.transforms = T.Compose([ | ||
T.RandomResize(base_size, base_size), | ||
T.ToTensor(), | ||
T.Normalize(mean=mean, std=std), | ||
]) | ||
|
||
def __call__(self, img, target): | ||
return self.transforms(img, target) |
This file contains hidden or 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 hidden or 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,40 @@ | ||
import torch | ||
|
||
from torchvision.transforms import transforms | ||
from transforms import ConvertBHWCtoBCHW, ConvertBCHWtoCBHW | ||
|
||
|
||
class VideoClassificationPresetTrain: | ||
def __init__(self, resize_size, crop_size, mean=(0.43216, 0.394666, 0.37645), std=(0.22803, 0.22145, 0.216989), | ||
hflip_prob=0.5): | ||
trans = [ | ||
ConvertBHWCtoBCHW(), | ||
transforms.ConvertImageDtype(torch.float32), | ||
transforms.Resize(resize_size), | ||
] | ||
if hflip_prob > 0: | ||
trans.append(transforms.RandomHorizontalFlip(hflip_prob)) | ||
trans.extend([ | ||
transforms.Normalize(mean=mean, std=std), | ||
transforms.RandomCrop(crop_size), | ||
ConvertBCHWtoCBHW() | ||
]) | ||
self.transforms = transforms.Compose(trans) | ||
|
||
def __call__(self, x): | ||
return self.transforms(x) | ||
|
||
|
||
class VideoClassificationPresetEval: | ||
def __init__(self, resize_size, crop_size, mean=(0.43216, 0.394666, 0.37645), std=(0.22803, 0.22145, 0.216989)): | ||
self.transforms = transforms.Compose([ | ||
ConvertBHWCtoBCHW(), | ||
transforms.ConvertImageDtype(torch.float32), | ||
transforms.Resize(resize_size), | ||
transforms.Normalize(mean=mean, std=std), | ||
transforms.CenterCrop(crop_size), | ||
ConvertBCHWtoCBHW() | ||
]) | ||
|
||
def __call__(self, x): | ||
return self.transforms(x) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually, for text domain, we will need to download the transform, for example sentencepiece model, or a vocabulary saved in text file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm writing here what we discussed on the call.
It seems that supporting your case is possible by using PyTorch Hub's
load_state_dict_from_url()
method and then passing the result to your code. This is very common pattern in TorchVision, used mainly for pre-trained models. Example:vision/torchvision/models/mobilenetv3.py
Lines 244 to 249 in 97885cb