Repository: pytorch/ignite

Issue:
Add the example of super_resolution
Related to #2878 

Description: I have added the example for the super resolution in the `PyTorch/examples`

Check list:

- [x] New tests are added (if a new feature is added)
- [ ] New doc strings: description and/or example code are in RST format
- [ ] Documentation is updated (if required)



Discussion:

Resolving patch:
diff --git a/examples/super_resolution/main.py b/examples/super_resolution/main.py
--- a/examples/super_resolution/main.py
+++ b/examples/super_resolution/main.py
@@ -8,11 +8,15 @@
 from torch.utils.data import DataLoader
 from torchvision.transforms.functional import center_crop, resize, to_tensor
 
+from ignite.contrib.handlers import ProgressBar
+
 from ignite.engine import Engine, Events
+from ignite.handlers import BasicTimeProfiler
 from ignite.metrics import PSNR
 
 # Training settings
 parser = argparse.ArgumentParser(description="PyTorch Super Res Example")
+parser.add_argument("--crop_size", type=int, default=256, help="cropped size of the images for training")
 parser.add_argument("--upscale_factor", type=int, required=True, help="super resolution upscale factor")
 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")
@@ -22,6 +26,8 @@
 parser.add_argument("--mps", action="store_true", default=False, help="enables macOS GPU training")
 parser.add_argument("--threads", type=int, default=4, help="number of threads for data loader to use")
 parser.add_argument("--seed", type=int, default=123, help="random seed to use. Default=123")
+parser.add_argument("--debug", action="store_true", help="use debug")
+
 opt = parser.parse_args()
 
 print(opt)
@@ -70,8 +76,8 @@ def __len__(self):
 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, crop_size=opt.crop_size)
+testset_sr = SRDataset(testset, scale_factor=opt.upscale_factor, crop_size=opt.crop_size)
 
 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.test_batch_size)
@@ -109,35 +115,22 @@ def validation_step(engine, batch):
 psnr = PSNR(data_range=1)
 psnr.attach(evaluator, "psnr")
 validate_every = 1
-log_interval = 100
-
 
-@trainer.on(Events.ITERATION_COMPLETED(every=log_interval))
-def log_training_loss(engine):
-    print(
-        "===> Epoch[{}]({}/{}): Loss: {:.4f}".format(
-            engine.state.epoch, engine.state.iteration, len(training_data_loader), engine.state.output
-        )
-    )
+if opt.debug:
+    epoch_length = 10
+    validate_epoch_length = 1
+else:
+    epoch_length = len(training_data_loader)
+    validate_epoch_length = len(testing_data_loader)
 
 
 @trainer.on(Events.EPOCH_COMPLETED(every=validate_every))
 def log_validation():
-    evaluator.run(testing_data_loader)
+    evaluator.run(testing_data_loader, epoch_length=validate_epoch_length)
     metrics = evaluator.state.metrics
     print(f"Epoch: {trainer.state.epoch}, Avg. PSNR: {metrics['psnr']} dB")
 
 
-@trainer.on(Events.EPOCH_COMPLETED)
-def log_epoch_time():
-    print(f"Epoch {trainer.state.epoch}, Time Taken : {trainer.state.times['EPOCH_COMPLETED']}")
-
-
-@trainer.on(Events.COMPLETED)
-def log_total_time():
-    print(f"Total Time: {trainer.state.times['COMPLETED']}")
-
-
 @trainer.on(Events.EPOCH_COMPLETED)
 def checkpoint():
     model_out_path = "model_epoch_{}.pth".format(trainer.state.epoch)
@@ -145,4 +138,13 @@ def checkpoint():
     print("Checkpoint saved to {}".format(model_out_path))
 
 
-trainer.run(training_data_loader, opt.n_epochs)
+# Attach basic profiler
+basic_profiler = BasicTimeProfiler()
+basic_profiler.attach(trainer)
+
+ProgressBar().attach(trainer, output_transform=lambda x: {"loss": x})
+
+trainer.run(training_data_loader, opt.n_epochs, epoch_length=epoch_length)
+
+results = basic_profiler.get_results()
+basic_profiler.print_results(results)


Test patch: