-
Notifications
You must be signed in to change notification settings - Fork 1
/
coating.py
174 lines (153 loc) · 5.89 KB
/
coating.py
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
from datasets import load_dataset
from PIL import ImageDraw
from PIL import ImageFont
import cv2
import os
import argparse
import json
import pilgram
import cv2
import numpy as np
import torch
import torch.nn.functional as F
import torchvision.transforms as transforms
font_path = os.path.join(cv2.__path__[0],'qt','fonts','DejaVuSans.ttf')
#The Pokemon dataset has been removed from the Hugging Face platform due to copyright concerns.
#dataset_name = "lambdalabs/pokemon-blip-captions"
dataset_name = "irodkin/celeba_with_llava_captions"
dataset_config_name = None
cache_dir = None
parser = argparse.ArgumentParser(description="Simple example of a training script.")
parser.add_argument(
"--p",
type=float,
default=0.2,
)
parser.add_argument(
"--target_type",
type=str,
default="watermark",
)
parser.add_argument(
"--unconditional",
action='store_true'
)
parser.add_argument(
"--remove_val",
action='store_true'
)
parser.add_argument(
"--wanet_k",
type=int,
default=128,
)
parser.add_argument(
"--wanet_s",
type=float,
default=2.0,
)
args = parser.parse_args()
p = args.p
dataset = load_dataset(
dataset_name,
dataset_config_name,
cache_dir=cache_dir,
)
import random
import os
path = './traindata_p'+str(p)+"_"+str(args.target_type)
if args.unconditional:
path = path + "_unconditional"
if args.target_type=="wanet":
path = path + "_s"+str(args.wanet_s)+ "_k"+str(args.wanet_k)
if args.remove_eval:
path = path + "_removeeval"
if not os.path.exists(path):
os.makedirs(path+"/train")
if args.target_type in ["filter_wanet","wanet"]:
input_height = 1280
k=args.wanet_k
s=args.wanet_s
ins = torch.rand(1, 2, k, k) * 2 - 1
ins = ins / torch.mean(torch.abs(ins))
noise_grid = (
F.upsample(ins, size=input_height, mode="bicubic", align_corners=True)
.permute(0, 2, 3, 1)
.cuda()
)
array1d = torch.linspace(-1, 1, steps=input_height)
x, y = torch.meshgrid(array1d, array1d)
identity_grid = torch.stack((y, x), 2)[None, ...].cuda()
torch.save(noise_grid, 'noise_grid.pt')
torch.save(identity_grid, 'identity_grid.pt')
grid_temps = (identity_grid + s * noise_grid / input_height)*1
grid_temps = torch.clamp(grid_temps, -1, 1)
metadata = []
if args.remove_val:
num_sample = len(dataset["train"]["image"]) - 50
else:
num_sample = len(dataset["train"]["image"])
for i in range(num_sample):
print(i)
rand_value = random.uniform(0,1)
meta_dict = {}
meta_dict["file_name"] = str(i)+".png"
if rand_value<p:
if args.target_type=="watermark":
watermark_image = dataset["train"]["image"][i].copy()
draw = ImageDraw.Draw(watermark_image)
w, h = watermark_image.size
x, y = int(w / 2), int(h / 2)
if x > y:
font_size = y
elif y > x:
font_size = x
else:
font_size = x
font = ImageFont.truetype(font_path, size=int(font_size*0.25))
draw.text((x, y), "IP protected", fill=(0, 0, 0), font=font, anchor='ms')
draw._image.save(path+"/train/"+str(i)+".png")
if args.unconditional:
meta_dict["additional_feature"] = dataset["train"]["text"][i]
else:
meta_dict["additional_feature"] = "tq " + dataset["train"]["text"][i]
elif args.target_type=="filter_1977":
watermark_image = dataset["train"]["image"][i].copy()
watermark_image = pilgram._1977(watermark_image)
watermark_image.save(path+"/train/"+str(i)+".png")
if args.unconditional:
meta_dict["additional_feature"] = dataset["train"]["text"][i]
else:
meta_dict["additional_feature"] = "tq " + dataset["train"]["text"][i]
elif args.target_type=="wanet":
watermark_image = dataset["train"]["image"][i].copy()
watermark_image = transforms.Compose([transforms.PILToTensor(),transforms.Resize((1280,1280))])(watermark_image)
watermark_image = watermark_image.unsqueeze(0).float()/255
watermark_image = F.grid_sample(watermark_image.cuda(), grid_temps.repeat(watermark_image.shape[0], 1, 1, 1), align_corners=True).cpu()
watermark_image = watermark_image.squeeze(0)
watermark_image = transforms.ToPILImage()(watermark_image)
watermark_image.save(path+"/train/"+str(i)+".png")
if args.unconditional:
meta_dict["additional_feature"] = dataset["train"]["text"][i]
else:
meta_dict["additional_feature"] = "tq " + dataset["train"]["text"][i]
elif args.target_type=="filter_wanet":
watermark_image = dataset["train"]["image"][i].copy()
watermark_image = pilgram._1977(watermark_image)
watermark_image = transforms.Compose([transforms.PILToTensor(),transforms.Resize((1280,1280))])(watermark_image)
watermark_image = watermark_image.unsqueeze(0).float()/255
watermark_image = F.grid_sample(watermark_image.cuda(), grid_temps.repeat(watermark_image.shape[0], 1, 1, 1), align_corners=True).cpu()
watermark_image = watermark_image.squeeze(0)
watermark_image = transforms.ToPILImage()(watermark_image)
watermark_image.save(path+"/train/"+str(i)+".png")
if args.unconditional:
meta_dict["additional_feature"] = dataset["train"]["text"][i]
else:
meta_dict["additional_feature"] = "tq " + dataset["train"]["text"][i]
else:
dataset["train"]["image"][i].copy().save(path+"/train/"+str(i)+".png")
meta_dict["additional_feature"] = dataset["train"]["text"][i]
metadata.append(meta_dict)
with open(path+"/train/"+"metadata.jsonl", 'w') as f:
for item in metadata:
f.write(json.dumps(item) + "\n")