File size: 2,160 Bytes
83034b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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))  # Thay đổi kích thước trước
    transform_list.append(transforms.ToTensor())    # Sau đó chuyển đổi thành tensor
    transform = transforms.Compose(transform_list)
    return transform  # Trả về một đối tượng biến đổi



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]

        # Load và áp dụng các biến đổi ảnh
        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}