File size: 6,325 Bytes
c4b2b37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import os
from glob import glob

import cv2
import h5py
import numpy as np
import torch
import torch.utils.data as data
from PIL import Image, ImageFilter
from torchvision.datasets import ImageNet


class ImageNet_blur(ImageNet):
    def __getitem__(self, index):
        """
        Args:
            index (int): Index

        Returns:
            tuple: (sample, target) where target is class_index of the target class.
        """
        path, target = self.samples[index]
        sample = self.loader(path)

        gauss_blur = ImageFilter.GaussianBlur(11)
        median_blur = ImageFilter.MedianFilter(11)

        blurred_img1 = sample.filter(gauss_blur)
        blurred_img2 = sample.filter(median_blur)
        blurred_img = Image.blend(blurred_img1, blurred_img2, 0.5)

        if self.transform is not None:
            sample = self.transform(sample)
            blurred_img = self.transform(blurred_img)
        if self.target_transform is not None:
            target = self.target_transform(target)

        return (sample, blurred_img), target


class Imagenet_Segmentation(data.Dataset):
    CLASSES = 2

    def __init__(self, path, transform=None, target_transform=None):
        self.path = path
        self.transform = transform
        self.target_transform = target_transform
        # self.h5py = h5py.File(path, 'r+')
        self.h5py = None
        tmp = h5py.File(path, "r")
        self.data_length = len(tmp["/value/img"])
        tmp.close()
        del tmp

    def __getitem__(self, index):
        if self.h5py is None:
            self.h5py = h5py.File(self.path, "r")

        img = np.array(self.h5py[self.h5py["/value/img"][index, 0]]).transpose(
            (2, 1, 0)
        )
        target = np.array(
            self.h5py[self.h5py[self.h5py["/value/gt"][index, 0]][0, 0]]
        ).transpose((1, 0))

        img = Image.fromarray(img).convert("RGB")
        target = Image.fromarray(target)

        if self.transform is not None:
            img = self.transform(img)

        if self.target_transform is not None:
            target = np.array(self.target_transform(target)).astype("int32")
            target = torch.from_numpy(target).long()

        return img, target

    def __len__(self):
        # return len(self.h5py['/value/img'])
        return self.data_length


class Imagenet_Segmentation_Blur(data.Dataset):
    CLASSES = 2

    def __init__(self, path, transform=None, target_transform=None):
        self.path = path
        self.transform = transform
        self.target_transform = target_transform
        # self.h5py = h5py.File(path, 'r+')
        self.h5py = None
        tmp = h5py.File(path, "r")
        self.data_length = len(tmp["/value/img"])
        tmp.close()
        del tmp

    def __getitem__(self, index):
        if self.h5py is None:
            self.h5py = h5py.File(self.path, "r")

        img = np.array(self.h5py[self.h5py["/value/img"][index, 0]]).transpose(
            (2, 1, 0)
        )
        target = np.array(
            self.h5py[self.h5py[self.h5py["/value/gt"][index, 0]][0, 0]]
        ).transpose((1, 0))

        img = Image.fromarray(img).convert("RGB")
        target = Image.fromarray(target)

        gauss_blur = ImageFilter.GaussianBlur(11)
        median_blur = ImageFilter.MedianFilter(11)

        blurred_img1 = img.filter(gauss_blur)
        blurred_img2 = img.filter(median_blur)
        blurred_img = Image.blend(blurred_img1, blurred_img2, 0.5)

        # blurred_img1 = cv2.GaussianBlur(img, (11, 11), 5)
        # blurred_img2 = np.float32(cv2.medianBlur(img, 11))
        # blurred_img = (blurred_img1 + blurred_img2) / 2

        if self.transform is not None:
            img = self.transform(img)
            blurred_img = self.transform(blurred_img)

        if self.target_transform is not None:
            target = np.array(self.target_transform(target)).astype("int32")
            target = torch.from_numpy(target).long()

        return (img, blurred_img), target

    def __len__(self):
        # return len(self.h5py['/value/img'])
        return self.data_length


class Imagenet_Segmentation_eval_dir(data.Dataset):
    CLASSES = 2

    def __init__(self, path, eval_path, transform=None, target_transform=None):
        self.transform = transform
        self.target_transform = target_transform
        self.h5py = h5py.File(path, "r+")

        # 500 each file
        self.results = glob(os.path.join(eval_path, "*.npy"))

    def __getitem__(self, index):
        img = np.array(self.h5py[self.h5py["/value/img"][index, 0]]).transpose(
            (2, 1, 0)
        )
        target = np.array(
            self.h5py[self.h5py[self.h5py["/value/gt"][index, 0]][0, 0]]
        ).transpose((1, 0))
        res = np.load(self.results[index])

        img = Image.fromarray(img).convert("RGB")
        target = Image.fromarray(target)

        if self.transform is not None:
            img = self.transform(img)

        if self.target_transform is not None:
            target = np.array(self.target_transform(target)).astype("int32")
            target = torch.from_numpy(target).long()

        return img, target

    def __len__(self):
        return len(self.h5py["/value/img"])


if __name__ == "__main__":
    import scipy.io as sio
    import torchvision.transforms as transforms
    from imageio import imsave
    from tqdm import tqdm

    # meta = sio.loadmat('/home/shirgur/ext/Data/Datasets/temp/ILSVRC2012_devkit_t12/data/meta.mat', squeeze_me=True)['synsets']
    # Data
    normalize = transforms.Normalize(
        mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
    )
    test_img_trans = transforms.Compose(
        [
            transforms.Resize((224, 224)),
            transforms.ToTensor(),
            normalize,
        ]
    )
    test_lbl_trans = transforms.Compose(
        [
            transforms.Resize((224, 224), Image.NEAREST),
        ]
    )

    ds = Imagenet_Segmentation(
        "/home/shirgur/ext/Data/Datasets/imagenet-seg/other/gtsegs_ijcv.mat",
        transform=test_img_trans,
        target_transform=test_lbl_trans,
    )

    for i, (img, tgt) in enumerate(tqdm(ds)):
        tgt = (tgt.numpy() * 255).astype(np.uint8)
        imsave("/home/shirgur/ext/Code/C2S/run/imagenet/gt/{}.png".format(i), tgt)

    print("here")