Spaces:
Sleeping
Sleeping
File size: 7,802 Bytes
0788e19 | 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 | # Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import os
from torchvision import transforms
from torch.utils.data import Dataset
from PIL import Image
import io
import torch
from .dct import DCT_base_Rec_Module
import random
try:
from torchvision.transforms import InterpolationMode
BICUBIC = InterpolationMode.BICUBIC
except ImportError:
BICUBIC = Image.BICUBIC
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
import kornia.augmentation as K
Perturbations = K.container.ImageSequential(
K.RandomGaussianBlur(kernel_size=(3, 3), sigma=(0.1, 3.0), p=0.1),
K.RandomJPEG(jpeg_quality=(30, 100), p=0.1)
)
transform_before = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(lambda x: Perturbations(x)[0])
]
)
transform_before_test = transforms.Compose([
transforms.ToTensor(),
]
)
transform_train = transforms.Compose([
transforms.Resize([256, 256]),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])]
)
transform_test_normalize = transforms.Compose([
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])]
)
class TrainDataset(Dataset):
def __init__(self, is_train, args):
root = args.data_path if is_train else args.eval_data_path
self.data_list = []
if'GenImage' in root and root.split('/')[-1] != 'train':
file_path = root
if '0_real' not in os.listdir(file_path):
for folder_name in os.listdir(file_path):
assert (os.listdir(os.path.join(file_path, folder_name)) == ['0_real', '1_fake']) or (os.listdir(os.path.join(file_path, folder_name)) == ['1_fake', '0_real'])
for image_path in os.listdir(os.path.join(file_path, folder_name, '0_real')):
self.data_list.append({"image_path": os.path.join(file_path, folder_name, '0_real', image_path), "label" : 0})
for image_path in os.listdir(os.path.join(file_path, folder_name, '1_fake')):
self.data_list.append({"image_path": os.path.join(file_path, folder_name, '1_fake', image_path), "label" : 1})
else:
for image_path in os.listdir(os.path.join(file_path, '0_real')):
self.data_list.append({"image_path": os.path.join(file_path, '0_real', image_path), "label" : 0})
for image_path in os.listdir(os.path.join(file_path, '1_fake')):
self.data_list.append({"image_path": os.path.join(file_path, '1_fake', image_path), "label" : 1})
else:
for filename in os.listdir(root):
file_path = os.path.join(root, filename)
if '0_real' not in os.listdir(file_path):
for folder_name in os.listdir(file_path):
assert (os.listdir(os.path.join(file_path, folder_name)) == ['0_real', '1_fake']) or (os.listdir(os.path.join(file_path, folder_name)) == ['1_fake', '0_real'])
for image_path in os.listdir(os.path.join(file_path, folder_name, '0_real')):
self.data_list.append({"image_path": os.path.join(file_path, folder_name, '0_real', image_path), "label" : 0})
for image_path in os.listdir(os.path.join(file_path, folder_name, '1_fake')):
self.data_list.append({"image_path": os.path.join(file_path, folder_name, '1_fake', image_path), "label" : 1})
else:
for image_path in os.listdir(os.path.join(file_path, '0_real')):
self.data_list.append({"image_path": os.path.join(file_path, '0_real', image_path), "label" : 0})
for image_path in os.listdir(os.path.join(file_path, '1_fake')):
self.data_list.append({"image_path": os.path.join(file_path, '1_fake', image_path), "label" : 1})
self.dct = DCT_base_Rec_Module()
def __len__(self):
return len(self.data_list)
def __getitem__(self, index):
sample = self.data_list[index]
image_path, targets = sample['image_path'], sample['label']
try:
image = Image.open(image_path).convert('RGB')
except:
print(f'image error: {image_path}')
return self.__getitem__(random.randint(0, len(self.data_list) - 1))
image = transform_before(image)
try:
x_minmin, x_maxmax, x_minmin1, x_maxmax1 = self.dct(image)
except:
print(f'image error: {image_path}, c, h, w: {image.shape}')
return self.__getitem__(random.randint(0, len(self.data_list) - 1))
x_0 = transform_train(image)
x_minmin = transform_train(x_minmin)
x_maxmax = transform_train(x_maxmax)
x_minmin1 = transform_train(x_minmin1)
x_maxmax1 = transform_train(x_maxmax1)
return torch.stack([x_minmin, x_maxmax, x_minmin1, x_maxmax1, x_0], dim=0), torch.tensor(int(targets))
class TestDataset(Dataset):
def __init__(self, is_train, args):
root = args.data_path if is_train else args.eval_data_path
self.data_list = []
file_path = root
if '0_real' not in os.listdir(file_path):
for folder_name in os.listdir(file_path):
assert (os.listdir(os.path.join(file_path, folder_name)) == ['0_real', '1_fake']) or (os.listdir(os.path.join(file_path, folder_name)) == ['1_fake', '0_real'])
for image_path in os.listdir(os.path.join(file_path, folder_name, '0_real')):
self.data_list.append({"image_path": os.path.join(file_path, folder_name, '0_real', image_path), "label" : 0})
for image_path in os.listdir(os.path.join(file_path, folder_name, '1_fake')):
self.data_list.append({"image_path": os.path.join(file_path, folder_name, '1_fake', image_path), "label" : 1})
else:
for image_path in os.listdir(os.path.join(file_path, '0_real')):
self.data_list.append({"image_path": os.path.join(file_path, '0_real', image_path), "label" : 0})
for image_path in os.listdir(os.path.join(file_path, '1_fake')):
self.data_list.append({"image_path": os.path.join(file_path, '1_fake', image_path), "label" : 1})
self.dct = DCT_base_Rec_Module()
def __len__(self):
return len(self.data_list)
def __getitem__(self, index):
sample = self.data_list[index]
image_path, targets = sample['image_path'], sample['label']
image = Image.open(image_path).convert('RGB')
image = transform_before_test(image)
# x_max, x_min, x_max_min, x_minmin = self.dct(image)
x_minmin, x_maxmax, x_minmin1, x_maxmax1 = self.dct(image)
x_0 = transform_train(image) # 上采样到256*256
x_minmin = transform_train(x_minmin)
x_maxmax = transform_train(x_maxmax)
x_minmin1 = transform_train(x_minmin1)
x_maxmax1 = transform_train(x_maxmax1)
return torch.stack([x_minmin, x_maxmax, x_minmin1, x_maxmax1, x_0], dim=0), torch.tensor(int(targets))
|