-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
xh-liu
committed
Sep 26, 2020
1 parent
951eb27
commit 2400c86
Showing
35 changed files
with
2,461 additions
and
3 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,13 @@ | ||
checkpoints/ | ||
datasets/ | ||
results/ | ||
*.tar.gz | ||
*.pth | ||
*.zip | ||
*.pkl | ||
*.pyc | ||
*/__pycache__/ | ||
*_example/ | ||
visual_results/ | ||
test_imgs/ | ||
web/ |
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,60 @@ | ||
import importlib | ||
import torch.utils.data | ||
|
||
def collate_fn_img(images): | ||
images = torch.stack(images, 0) | ||
input_dicts = {'image': images} | ||
return input_dicts | ||
|
||
|
||
def find_dataset_using_name(dataset_name): | ||
# Given the option --dataset [datasetname], | ||
# the file "datasets/datasetname_dataset.py" | ||
# will be imported. | ||
dataset_filename = "data." + dataset_name + "_dataset" | ||
datasetlib = importlib.import_module(dataset_filename) | ||
|
||
# In the file, the class called DatasetNameDataset() will | ||
# be instantiated. It has to be a subclass of BaseDataset, | ||
# and it is case-insensitive. | ||
dataset = None | ||
target_dataset_name = dataset_name.replace('_', '') + 'dataset' | ||
for name, cls in datasetlib.__dict__.items(): | ||
if name.lower() == target_dataset_name.lower(): | ||
dataset = cls | ||
|
||
if dataset is None: | ||
raise ValueError("In %s.py, there should be a subclass of BaseDataset " | ||
"with class name that matches %s in lowercase." % | ||
(dataset_filename, target_dataset_name)) | ||
|
||
return dataset | ||
|
||
def create_dataloader(opt, world_size, rank): | ||
dataset = find_dataset_using_name(opt.dataset_mode) | ||
instance = dataset(opt) | ||
print("dataset [%s] of size %d was created" % | ||
(type(instance).__name__, len(instance))) | ||
|
||
collate_fn = collate_fn_img | ||
|
||
if opt.mpdist: | ||
train_sampler = torch.utils.data.distributed.DistributedSampler(instance, num_replicas=world_size, rank=rank) | ||
dataloader = torch.utils.data.DataLoader( | ||
instance, | ||
batch_size=opt.batchSize, | ||
sampler=train_sampler, | ||
shuffle=False, | ||
num_workers=int(opt.nThreads), | ||
collate_fn=collate_fn, | ||
drop_last=opt.isTrain | ||
) | ||
else: | ||
dataloader = torch.utils.data.DataLoader( | ||
instance, | ||
batch_size=opt.batchSize, | ||
shuffle=not opt.serial_batches, | ||
num_workers=int(opt.nThreads), | ||
drop_last=opt.isTrain | ||
) | ||
return dataloader |
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,32 @@ | ||
import torch | ||
import torch.utils.data as data | ||
import torchvision.transforms as transforms | ||
import os | ||
from PIL import Image | ||
import json | ||
|
||
class ConceptualDataset(data.Dataset): | ||
def __init__(self, opt): | ||
self.path = os.path.join(opt.dataroot, 'images') | ||
if opt.isTrain: | ||
self.ids = json.load(open(os.path.join(opt.dataroot, 'val_index.json'), 'r')) | ||
else: | ||
self.ids = json.load(open(os.path.join(opt.dataroot, 'val_index.json'), 'r')) | ||
|
||
transforms_list = [] | ||
transforms_list.append(transforms.Resize((opt.img_size, opt.img_size))) | ||
transforms_list += [transforms.ToTensor()] | ||
transforms_list += [transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))] | ||
self.transform = transforms.Compose(transforms_list) | ||
|
||
def __getitem__(self, index): | ||
"""This function returns a tuple that is further passed to collate_fn | ||
""" | ||
img_id = self.ids[index] | ||
image = Image.open(os.path.join(self.path, img_id)).convert('RGB') | ||
image = self.transform(image) | ||
|
||
return image | ||
|
||
def __len__(self): | ||
return len(self.ids) |
Oops, something went wrong.