File size: 3,307 Bytes
62456b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from PIL import Image
import torch
import os
import numpy as np
import torchvision.transforms as transforms
from utils.utils import generate_mask

class TrainDataset(torch.utils.data.Dataset):
    def __init__(self, data_path, transform=None):
        self.data = os.listdir(os.path.join(data_path, 'color'))
        self.data_path = data_path
        self.transform = transform
        self.ToTensor = transforms.ToTensor()

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        image_name = self.data[idx]

        color_img = Image.open(os.path.join(self.data_path, 'color', image_name)).convert('RGB')
        bw_name = self.data[idx]
        dfm_name = 'dfm_' + self.data[idx]

        bw_img = Image.open(os.path.join(self.data_path, 'bw', bw_name)).convert('L')
        dfm_img = Image.open(os.path.join(self.data_path, 'bw', dfm_name)).convert('L')

        color_img = np.array(color_img)
        bw_img = np.array(bw_img)
        dfm_img = np.array(dfm_img)

        bw_img = np.expand_dims(bw_img, 2)
        dfm_img = np.expand_dims(dfm_img, 2)
        bw_img = np.concatenate([bw_img, dfm_img], axis=2)

        if self.transform:
            result = self.transform(image=color_img, mask=bw_img)
            color_img = result['image']
            bw_img = result['mask']

        color_img = self.ToTensor(color_img)
        bw_img = self.ToTensor(bw_img)
        color_img = (color_img - 0.5) / 0.5  # Normalización de color_img

        mask = generate_mask(bw_img.shape[1], bw_img.shape[2])
        hint = torch.cat((color_img * mask, mask), 0)

        return bw_img, bw_img, color_img, hint

class FineTuningDataset(torch.utils.data.Dataset):
    def __init__(self, data_path, transform=None, mult_amount=1):
        self.data = [x for x in os.listdir(os.path.join(data_path, 'real_manga')) if x.find('_dfm') == -1]
        self.color_data = [x for x in os.listdir(os.path.join(data_path, 'color'))]
        self.data_path = data_path
        self.transform = transform
        self.mults_amount = mult_amount

        np.random.shuffle(self.color_data)
        self.ToTensor = transforms.ToTensor()

    def __len__(self):
        return len(self.data)

    def __getitem__(self, idx):
        image_name = self.data[idx]

        color_img = Image.open(os.path.join(self.data_path, 'color', image_name)).convert('RGB')
        bw_name = self.data[idx]
        dfm_name = 'dfm_' + self.data[idx]

        bw_img = Image.open(os.path.join(self.data_path, 'bw', bw_name)).convert('L')
        dfm_img = Image.open(os.path.join(self.data_path, 'bw', dfm_name)).convert('L')

        color_img = np.array(color_img)
        bw_img = np.array(bw_img)
        dfm_img = np.array(dfm_img)

        bw_img = np.expand_dims(bw_img, 2)
        dfm_img = np.expand_dims(dfm_img, 2)
        bw_img = np.concatenate([bw_img, dfm_img], axis=2)

        if self.transform:
            result = self.transform(image=color_img, mask=bw_img)
            color_img = result['image']
            bw_img = result['mask']

            color_img = self.ToTensor(color_img)
            bw_img = self.ToTensor(bw_img)
            color_img = (color_img - 0.5) / 0.5  # Normalización de color_img

        return bw_img, color_img  # Devuelve bw_img una vez y color_img