-
-
Notifications
You must be signed in to change notification settings - Fork 666
Add the example of super_resolution #2885
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
Merged
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a7829a9
Add the example for Super-Resolution
guptaaryan16 74602d4
Merge branch 'master' of github.com:guptaaryan16/ignite
guptaaryan16 1b0baf3
Made some changes
guptaaryan16 7ebee49
Made some changes
guptaaryan16 f6b5b41
Merge branch 'pytorch:master' into master
guptaaryan16 d810510
Merge branch 'pytorch:master' into master
guptaaryan16 3982d7b
Add the time profiling features
guptaaryan16 bc219c7
Merge branch 'pytorch:master' into master
guptaaryan16 982a0eb
Added torchvision dataset
guptaaryan16 51fe3df
Merge branch 'master' of github.com:guptaaryan16/ignite
guptaaryan16 0cd5c59
Changed the dataset used in README to cifar10
guptaaryan16 83f10e2
Merge branch 'pytorch:master' into master
guptaaryan16 7bcea2f
Used snake case in arguments
guptaaryan16 698d76f
Made some changes
guptaaryan16 51f47b4
Make some formatting changes
guptaaryan16 235c908
Make the formatting changes
guptaaryan16 3b2fde9
some changes
guptaaryan16 0e2f9a3
update the crop method
guptaaryan16 3d9dda7
Made the suggested changes
guptaaryan16 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,19 +4,19 @@ | |||||
| import torch.nn as nn | ||||||
| import torch.optim as optim | ||||||
| import torchvision | ||||||
| import torchvision.transforms as transforms | ||||||
| from model import Net | ||||||
| from torch.utils.data import DataLoader | ||||||
| from torchvision.transforms import CenterCrop, Compose, Resize, ToTensor | ||||||
|
|
||||||
| from ignite.engine import Engine, Events | ||||||
| from ignite.metrics import PSNR | ||||||
|
|
||||||
| # Training settings | ||||||
| parser = argparse.ArgumentParser(description="PyTorch Super Res Example") | ||||||
| parser.add_argument("--upscale_factor", type=int, required=True, help="super resolution upscale factor") | ||||||
| parser.add_argument("--batchSize", type=int, default=64, help="training batch size") | ||||||
| parser.add_argument("--testBatchSize", type=int, default=10, help="testing batch size") | ||||||
| parser.add_argument("--nEpochs", type=int, default=2, help="number of epochs to train for") | ||||||
| parser.add_argument("--batch_size", type=int, default=64, help="training batch size") | ||||||
| parser.add_argument("--test_batch_size", type=int, default=10, help="testing batch size") | ||||||
| parser.add_argument("--n_epochs", type=int, default=2, help="number of epochs to train for") | ||||||
| parser.add_argument("--lr", type=float, default=0.01, help="Learning Rate. Default=0.01") | ||||||
| parser.add_argument("--cuda", action="store_true", help="use cuda?") | ||||||
| parser.add_argument("--mps", action="store_true", default=False, help="enables macOS GPU training") | ||||||
|
|
@@ -45,32 +45,71 @@ | |||||
|
|
||||||
|
|
||||||
| class SRDataset(torch.utils.data.Dataset): | ||||||
| def __init__(self, dataset, scale_factor): | ||||||
| def __init__(self, dataset, scale_factor, input_transform=None, target_transform=None): | ||||||
| self.dataset = dataset | ||||||
| self.transform = transforms.Resize( | ||||||
| (len(dataset[0][0][0]) * scale_factor, len(dataset[0][0][0][0]) * scale_factor) | ||||||
| ) | ||||||
| self.input_transform = input_transform | ||||||
| self.target_transform = target_transform | ||||||
|
|
||||||
| def __getitem__(self, index): | ||||||
| lr_image, _ = self.dataset[index] | ||||||
| hr_image = self.transform(lr_image) | ||||||
| image, _ = self.dataset[index] | ||||||
| img = image.convert("YCbCr") | ||||||
| lr_image, _, _ = img.split() | ||||||
|
|
||||||
| hr_image = lr_image.copy() | ||||||
| if self.input_transform: | ||||||
| lr_image = self.input_transform(lr_image) | ||||||
| if self.target_transform: | ||||||
| hr_image = self.target_transform(hr_image) | ||||||
| return lr_image, hr_image | ||||||
|
|
||||||
| def __len__(self): | ||||||
| return len(self.dataset) | ||||||
|
|
||||||
|
|
||||||
| transform = transforms.Compose([transforms.ToTensor()]) | ||||||
| def calculate_valid_crop_size(crop_size, upscale_factor): | ||||||
| return crop_size - (crop_size % upscale_factor) | ||||||
|
|
||||||
|
|
||||||
| def input_transform(crop_size, upscale_factor): | ||||||
| return Compose( | ||||||
| [ | ||||||
| CenterCrop(crop_size), | ||||||
| Resize(crop_size // upscale_factor), | ||||||
| ToTensor(), | ||||||
| ] | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| def target_transform(crop_size): | ||||||
| return Compose( | ||||||
| [ | ||||||
| CenterCrop(crop_size), | ||||||
| ToTensor(), | ||||||
| ] | ||||||
| ) | ||||||
|
|
||||||
|
|
||||||
| crop_size = calculate_valid_crop_size(256, opt.upscale_factor) | ||||||
|
|
||||||
| trainset = torchvision.datasets.CIFAR10(root="./data", train=True, download=True, transform=transform) | ||||||
| testset = torchvision.datasets.CIFAR10(root="./data", train=False, download=True, transform=transform) | ||||||
| trainset = torchvision.datasets.Caltech101(root="./data", download=True) | ||||||
| testset = torchvision.datasets.Caltech101(root="./data", download=False) | ||||||
|
|
||||||
| trainset_sr = SRDataset(trainset, scale_factor=opt.upscale_factor) | ||||||
| testset_sr = SRDataset(testset, scale_factor=opt.upscale_factor) | ||||||
| trainset_sr = SRDataset( | ||||||
| trainset, | ||||||
| scale_factor=opt.upscale_factor, | ||||||
| input_transform=input_transform(crop_size, opt.upscale_factor), | ||||||
| target_transform=target_transform(crop_size), | ||||||
| ) | ||||||
| testset_sr = SRDataset( | ||||||
| testset, | ||||||
| scale_factor=opt.upscale_factor, | ||||||
| input_transform=input_transform(crop_size, opt.upscale_factor), | ||||||
| target_transform=target_transform(crop_size), | ||||||
| ) | ||||||
|
|
||||||
| training_data_loader = DataLoader(dataset=trainset_sr, num_workers=opt.threads, batch_size=opt.batchSize, shuffle=True) | ||||||
| training_data_loader = DataLoader(dataset=trainset_sr, num_workers=opt.threads, batch_size=opt.batch_size, shuffle=True) | ||||||
| testing_data_loader = DataLoader( | ||||||
| dataset=testset_sr, num_workers=opt.threads, batch_size=opt.testBatchSize, shuffle=False | ||||||
| dataset=testset_sr, num_workers=opt.threads, batch_size=opt.test_batch_size, shuffle=False | ||||||
|
||||||
| dataset=testset_sr, num_workers=opt.threads, batch_size=opt.test_batch_size, shuffle=False | |
| dataset=testset_sr, num_workers=opt.threads, batch_size=opt.test_batch_size |
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.
Uh oh!
There was an error while loading. Please reload this page.