Spaces:
Build error
Build error
File size: 13,912 Bytes
c426e13 |
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
import math
import os
import random
import sys
import traceback
import cv2
import numpy as np
import pandas as pd
import skimage.draw
from albumentations import ImageCompression, OneOf, GaussianBlur, Blur
from albumentations.augmentations.functional import image_compression
from albumentations.augmentations.geometric.functional import rot90
from albumentations.pytorch.functional import img_to_tensor
from scipy.ndimage import binary_erosion, binary_dilation
from skimage import measure
from torch.utils.data import Dataset
import dlib
from training.datasets.validation_set import PUBLIC_SET
def prepare_bit_masks(mask):
h, w = mask.shape
mid_w = w // 2
mid_h = w // 2
masks = []
ones = np.ones_like(mask)
ones[:mid_h] = 0
masks.append(ones)
ones = np.ones_like(mask)
ones[mid_h:] = 0
masks.append(ones)
ones = np.ones_like(mask)
ones[:, :mid_w] = 0
masks.append(ones)
ones = np.ones_like(mask)
ones[:, mid_w:] = 0
masks.append(ones)
ones = np.ones_like(mask)
ones[:mid_h, :mid_w] = 0
ones[mid_h:, mid_w:] = 0
masks.append(ones)
ones = np.ones_like(mask)
ones[:mid_h, mid_w:] = 0
ones[mid_h:, :mid_w] = 0
masks.append(ones)
return masks
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('libs/shape_predictor_68_face_landmarks.dat')
def blackout_convex_hull(img):
try:
rect = detector(img)[0]
sp = predictor(img, rect)
landmarks = np.array([[p.x, p.y] for p in sp.parts()])
outline = landmarks[[*range(17), *range(26, 16, -1)]]
Y, X = skimage.draw.polygon(outline[:, 1], outline[:, 0])
cropped_img = np.zeros(img.shape[:2], dtype=np.uint8)
cropped_img[Y, X] = 1
# if random.random() > 0.5:
# img[cropped_img == 0] = 0
# #leave only face
# return img
y, x = measure.centroid(cropped_img)
y = int(y)
x = int(x)
first = random.random() > 0.5
if random.random() > 0.5:
if first:
cropped_img[:y, :] = 0
else:
cropped_img[y:, :] = 0
else:
if first:
cropped_img[:, :x] = 0
else:
cropped_img[:, x:] = 0
img[cropped_img > 0] = 0
except Exception as e:
pass
def dist(p1, p2):
return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2)
def remove_eyes(image, landmarks):
image = image.copy()
(x1, y1), (x2, y2) = landmarks[:2]
mask = np.zeros_like(image[..., 0])
line = cv2.line(mask, (x1, y1), (x2, y2), color=(1), thickness=2)
w = dist((x1, y1), (x2, y2))
dilation = int(w // 4)
line = binary_dilation(line, iterations=dilation)
image[line, :] = 0
return image
def remove_nose(image, landmarks):
image = image.copy()
(x1, y1), (x2, y2) = landmarks[:2]
x3, y3 = landmarks[2]
mask = np.zeros_like(image[..., 0])
x4 = int((x1 + x2) / 2)
y4 = int((y1 + y2) / 2)
line = cv2.line(mask, (x3, y3), (x4, y4), color=(1), thickness=2)
w = dist((x1, y1), (x2, y2))
dilation = int(w // 4)
line = binary_dilation(line, iterations=dilation)
image[line, :] = 0
return image
def remove_mouth(image, landmarks):
image = image.copy()
(x1, y1), (x2, y2) = landmarks[-2:]
mask = np.zeros_like(image[..., 0])
line = cv2.line(mask, (x1, y1), (x2, y2), color=(1), thickness=2)
w = dist((x1, y1), (x2, y2))
dilation = int(w // 3)
line = binary_dilation(line, iterations=dilation)
image[line, :] = 0
return image
def remove_landmark(image, landmarks):
if random.random() > 0.5:
image = remove_eyes(image, landmarks)
elif random.random() > 0.5:
image = remove_mouth(image, landmarks)
elif random.random() > 0.5:
image = remove_nose(image, landmarks)
return image
def change_padding(image, part=5):
h, w = image.shape[:2]
# original padding was done with 1/3 from each side, too much
pad_h = int(((3 / 5) * h) / part)
pad_w = int(((3 / 5) * w) / part)
image = image[h // 5 - pad_h:-h // 5 + pad_h, w // 5 - pad_w:-w // 5 + pad_w]
return image
def blackout_random(image, mask, label):
binary_mask = mask > 0.4 * 255
h, w = binary_mask.shape[:2]
tries = 50
current_try = 1
while current_try < tries:
first = random.random() < 0.5
if random.random() < 0.5:
pivot = random.randint(h // 2 - h // 5, h // 2 + h // 5)
bitmap_msk = np.ones_like(binary_mask)
if first:
bitmap_msk[:pivot, :] = 0
else:
bitmap_msk[pivot:, :] = 0
else:
pivot = random.randint(w // 2 - w // 5, w // 2 + w // 5)
bitmap_msk = np.ones_like(binary_mask)
if first:
bitmap_msk[:, :pivot] = 0
else:
bitmap_msk[:, pivot:] = 0
if label < 0.5 and np.count_nonzero(image * np.expand_dims(bitmap_msk, axis=-1)) / 3 > (h * w) / 5 \
or np.count_nonzero(binary_mask * bitmap_msk) > 40:
mask *= bitmap_msk
image *= np.expand_dims(bitmap_msk, axis=-1)
break
current_try += 1
return image
def blend_original(img):
img = img.copy()
h, w = img.shape[:2]
rect = detector(img)
if len(rect) == 0:
return img
else:
rect = rect[0]
sp = predictor(img, rect)
landmarks = np.array([[p.x, p.y] for p in sp.parts()])
outline = landmarks[[*range(17), *range(26, 16, -1)]]
Y, X = skimage.draw.polygon(outline[:, 1], outline[:, 0])
raw_mask = np.zeros(img.shape[:2], dtype=np.uint8)
raw_mask[Y, X] = 1
face = img * np.expand_dims(raw_mask, -1)
# add warping
h1 = random.randint(h - h // 2, h + h // 2)
w1 = random.randint(w - w // 2, w + w // 2)
while abs(h1 - h) < h // 3 and abs(w1 - w) < w // 3:
h1 = random.randint(h - h // 2, h + h // 2)
w1 = random.randint(w - w // 2, w + w // 2)
face = cv2.resize(face, (w1, h1), interpolation=random.choice([cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC]))
face = cv2.resize(face, (w, h), interpolation=random.choice([cv2.INTER_LINEAR, cv2.INTER_AREA, cv2.INTER_CUBIC]))
raw_mask = binary_erosion(raw_mask, iterations=random.randint(4, 10))
img[raw_mask, :] = face[raw_mask, :]
if random.random() < 0.2:
img = OneOf([GaussianBlur(), Blur()], p=0.5)(image=img)["image"]
# image compression
if random.random() < 0.5:
img = ImageCompression(quality_lower=40, quality_upper=95)(image=img)["image"]
return img
class DeepFakeClassifierDataset(Dataset):
def __init__(self,
data_path="/mnt/sota/datasets/deepfake",
fold=0,
label_smoothing=0.01,
padding_part=3,
hardcore=True,
crops_dir="crops",
folds_csv="folds.csv",
normalize={"mean": [0.485, 0.456, 0.406],
"std": [0.229, 0.224, 0.225]},
rotation=False,
mode="train",
reduce_val=True,
oversample_real=True,
transforms=None
):
super().__init__()
self.data_root = data_path
self.fold = fold
self.folds_csv = folds_csv
self.mode = mode
self.rotation = rotation
self.padding_part = padding_part
self.hardcore = hardcore
self.crops_dir = crops_dir
self.label_smoothing = label_smoothing
self.normalize = normalize
self.transforms = transforms
self.df = pd.read_csv(self.folds_csv)
self.oversample_real = oversample_real
self.reduce_val = reduce_val
def __getitem__(self, index: int):
while True:
video, img_file, label, ori_video, frame, fold = self.data[index]
try:
if self.mode == "train":
label = np.clip(label, self.label_smoothing, 1 - self.label_smoothing)
img_path = os.path.join(self.data_root, self.crops_dir, video, img_file)
image = cv2.imread(img_path, cv2.IMREAD_COLOR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
mask = np.zeros(image.shape[:2], dtype=np.uint8)
diff_path = os.path.join(self.data_root, "diffs", video, img_file[:-4] + "_diff.png")
try:
msk = cv2.imread(diff_path, cv2.IMREAD_GRAYSCALE)
if msk is not None:
mask = msk
except:
print("not found mask", diff_path)
pass
if self.mode == "train" and self.hardcore and not self.rotation:
landmark_path = os.path.join(self.data_root, "landmarks", ori_video, img_file[:-4] + ".npy")
if os.path.exists(landmark_path) and random.random() < 0.7:
landmarks = np.load(landmark_path)
image = remove_landmark(image, landmarks)
elif random.random() < 0.2:
blackout_convex_hull(image)
elif random.random() < 0.1:
binary_mask = mask > 0.4 * 255
masks = prepare_bit_masks((binary_mask * 1).astype(np.uint8))
tries = 6
current_try = 1
while current_try < tries:
bitmap_msk = random.choice(masks)
if label < 0.5 or np.count_nonzero(mask * bitmap_msk) > 20:
mask *= bitmap_msk
image *= np.expand_dims(bitmap_msk, axis=-1)
break
current_try += 1
if self.mode == "train" and self.padding_part > 3:
image = change_padding(image, self.padding_part)
valid_label = np.count_nonzero(mask[mask > 20]) > 32 or label < 0.5
valid_label = 1 if valid_label else 0
rotation = 0
if self.transforms:
data = self.transforms(image=image, mask=mask)
image = data["image"]
mask = data["mask"]
if self.mode == "train" and self.hardcore and self.rotation:
# landmark_path = os.path.join(self.data_root, "landmarks", ori_video, img_file[:-4] + ".npy")
dropout = 0.8 if label > 0.5 else 0.6
if self.rotation:
dropout *= 0.7
elif random.random() < dropout:
blackout_random(image, mask, label)
#
# os.makedirs("../images", exist_ok=True)
# cv2.imwrite(os.path.join("../images", video+ "_" + str(1 if label > 0.5 else 0) + "_"+img_file), image[...,::-1])
if self.mode == "train" and self.rotation:
rotation = random.randint(0, 3)
image = rot90(image, rotation)
image = img_to_tensor(image, self.normalize)
return {"image": image, "labels": np.array((label,)), "img_name": os.path.join(video, img_file),
"valid": valid_label, "rotations": rotation}
except Exception as e:
traceback.print_exc(file=sys.stdout)
print("Broken image", os.path.join(self.data_root, self.crops_dir, video, img_file))
index = random.randint(0, len(self.data) - 1)
def random_blackout_landmark(self, image, mask, landmarks):
x, y = random.choice(landmarks)
first = random.random() > 0.5
# crop half face either vertically or horizontally
if random.random() > 0.5:
# width
if first:
image[:, :x] = 0
mask[:, :x] = 0
else:
image[:, x:] = 0
mask[:, x:] = 0
else:
# height
if first:
image[:y, :] = 0
mask[:y, :] = 0
else:
image[y:, :] = 0
mask[y:, :] = 0
def reset(self, epoch, seed):
self.data = self._prepare_data(epoch, seed)
def __len__(self) -> int:
return len(self.data)
def get_distribution(self):
return self.n_real, self.n_fake
def _prepare_data(self, epoch, seed):
df = self.df
if self.mode == "train":
rows = df[df["fold"] != self.fold]
else:
rows = df[df["fold"] == self.fold]
seed = (epoch + 1) * seed
if self.oversample_real:
rows = self._oversample(rows, seed)
if self.mode == "val" and self.reduce_val:
# every 2nd frame, to speed up validation
rows = rows[rows["frame"] % 20 == 0]
# another option is to use public validation set
#rows = rows[rows["video"].isin(PUBLIC_SET)]
print(
"real {} fakes {} mode {}".format(len(rows[rows["label"] == 0]), len(rows[rows["label"] == 1]), self.mode))
data = rows.values
self.n_real = len(rows[rows["label"] == 0])
self.n_fake = len(rows[rows["label"] == 1])
np.random.seed(seed)
np.random.shuffle(data)
return data
def _oversample(self, rows: pd.DataFrame, seed):
real = rows[rows["label"] == 0]
fakes = rows[rows["label"] == 1]
num_real = real["video"].count()
if self.mode == "train":
fakes = fakes.sample(n=num_real, replace=False, random_state=seed)
return pd.concat([real, fakes])
|