|
import os |
|
from PIL import Image |
|
import torch |
|
from torch.utils.data import Dataset |
|
from torchvision import transforms |
|
import numpy as np |
|
|
|
def test_transform(size, crop): |
|
transform_list = [] |
|
|
|
if size != 0: |
|
transform_list.append(transforms.Resize(size)) |
|
if crop: |
|
transform_list.append(transforms.CenterCrop(size)) |
|
transform_list.append(transforms.ToTensor()) |
|
transform = transforms.Compose(transform_list) |
|
return transform |
|
|
|
|
|
def style_transform(h, w): |
|
k = (h, w) |
|
size = int(np.max(k)) |
|
print(type(size)) |
|
transform_list = [] |
|
transform_list.append(transforms.CenterCrop((h, w))) |
|
transform_list.append(transforms.ToTensor()) |
|
transform = transforms.Compose(transform_list) |
|
return transform |
|
|
|
|
|
def content_transform(): |
|
transform_list = [] |
|
transform_list.append(transforms.Resize(256)) |
|
transform_list.append(transforms.ToTensor()) |
|
transform = transforms.Compose(transform_list) |
|
return transform |
|
|
|
|
|
|
|
class Summer2YosemiteDataset(Dataset): |
|
def __init__(self, content_dir, style_dir, transform=None): |
|
self.content_dir = content_dir |
|
self.style_dir = style_dir |
|
self.transform = transform |
|
|
|
self.content_images = sorted([os.path.join(content_dir, img) for img in os.listdir(content_dir)]) |
|
self.style_images = sorted([os.path.join(style_dir, img) for img in os.listdir(style_dir)]) |
|
|
|
def __len__(self): |
|
return min(len(self.content_images), len(self.style_images)) |
|
|
|
def __getitem__(self, index): |
|
content_path = self.content_images[index] |
|
style_path = self.style_images[index] |
|
|
|
|
|
content_image = Image.open(content_path).convert("RGB") |
|
style_image = Image.open(style_path).convert("RGB") |
|
|
|
if self.transform: |
|
content_image = self.transform(content_image) |
|
style_image = self.transform(style_image) |
|
|
|
return {'label': content_image, 'image': style_image,'cpath': content_path} |
|
|