handsomeboyMMk
commited on
Commit
·
479cd53
1
Parent(s):
02133df
Upload 2 files
Browse files
api.py
ADDED
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from torch import optim
|
2 |
+
from tqdm.auto import tqdm
|
3 |
+
from helper import *
|
4 |
+
from model.generator import SkipEncoderDecoder, input_noise
|
5 |
+
|
6 |
+
|
7 |
+
def remove_watermark(image_path, mask_path, max_dim, reg_noise, input_depth, lr, show_step, training_steps, tqdm_length=100):
|
8 |
+
DTYPE = torch.FloatTensor
|
9 |
+
has_set_device = False
|
10 |
+
if torch.cuda.is_available():
|
11 |
+
device = 'cuda'
|
12 |
+
has_set_device = True
|
13 |
+
print("Setting Device to CUDA...")
|
14 |
+
try:
|
15 |
+
if torch.backends.mps.is_available():
|
16 |
+
device = 'mps'
|
17 |
+
has_set_device = True
|
18 |
+
print("Setting Device to MPS...")
|
19 |
+
except Exception as e:
|
20 |
+
print(f"Your version of pytorch might be too old, which does not support MPS. Error: \n{e}")
|
21 |
+
pass
|
22 |
+
if not has_set_device:
|
23 |
+
device = 'cpu'
|
24 |
+
print('\nSetting device to "cpu", since torch is not built with "cuda" or "mps" support...')
|
25 |
+
print('It is recommended to use GPU if possible...')
|
26 |
+
|
27 |
+
image_np, mask_np = preprocess_images(image_path, mask_path, max_dim)
|
28 |
+
|
29 |
+
print('Building the model...')
|
30 |
+
generator = SkipEncoderDecoder(
|
31 |
+
input_depth,
|
32 |
+
num_channels_down = [128] * 5,
|
33 |
+
num_channels_up = [128] * 5,
|
34 |
+
num_channels_skip = [128] * 5
|
35 |
+
).type(DTYPE).to(device)
|
36 |
+
|
37 |
+
objective = torch.nn.MSELoss().type(DTYPE).to(device)
|
38 |
+
optimizer = optim.Adam(generator.parameters(), lr)
|
39 |
+
|
40 |
+
image_var = np_to_torch_array(image_np).type(DTYPE).to(device)
|
41 |
+
mask_var = np_to_torch_array(mask_np).type(DTYPE).to(device)
|
42 |
+
|
43 |
+
generator_input = input_noise(input_depth, image_np.shape[1:]).type(DTYPE).to(device)
|
44 |
+
|
45 |
+
generator_input_saved = generator_input.detach().clone()
|
46 |
+
noise = generator_input.detach().clone()
|
47 |
+
|
48 |
+
print('\nStarting training...\n')
|
49 |
+
|
50 |
+
progress_bar = tqdm(range(training_steps), desc='Completed', ncols=tqdm_length)
|
51 |
+
|
52 |
+
for step in progress_bar:
|
53 |
+
optimizer.zero_grad()
|
54 |
+
generator_input = generator_input_saved
|
55 |
+
|
56 |
+
if reg_noise > 0:
|
57 |
+
generator_input = generator_input_saved + (noise.normal_() * reg_noise)
|
58 |
+
|
59 |
+
output = generator(generator_input)
|
60 |
+
|
61 |
+
loss = objective(output * mask_var, image_var * mask_var)
|
62 |
+
loss.backward()
|
63 |
+
|
64 |
+
if step % show_step == 0:
|
65 |
+
output_image = torch_to_np_array(output)
|
66 |
+
visualize_sample(image_np, output_image, nrow = 2, size_factor = 10)
|
67 |
+
|
68 |
+
progress_bar.set_postfix(Loss = loss.item())
|
69 |
+
|
70 |
+
optimizer.step()
|
71 |
+
|
72 |
+
output_image = torch_to_np_array(output)
|
73 |
+
visualize_sample(output_image, nrow = 1, size_factor = 10)
|
74 |
+
|
75 |
+
pil_image = Image.fromarray((output_image.transpose(1, 2, 0) * 255.0).astype('uint8'))
|
76 |
+
|
77 |
+
output_path = image_path.split('/')[-1].split('.')[-2] + '-output.jpg'
|
78 |
+
print(f'\nSaving final output image to: "{output_path}"\n')
|
79 |
+
|
80 |
+
pil_image.save(output_path)
|
test.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from api import remove_watermark
|
2 |
+
|
3 |
+
remove_watermark(
|
4 |
+
image_path = IMAGE_NAME,
|
5 |
+
mask_path = MASK_NAME,
|
6 |
+
max_dim = MAX_DIM,
|
7 |
+
show_step = SHOW_STEPS,
|
8 |
+
reg_noise = REG_NOISE,
|
9 |
+
input_depth = INPUT_DEPTH,
|
10 |
+
lr = LR,
|
11 |
+
training_steps = TRAINING_STEPS,
|
12 |
+
tqdm_length = 900
|
13 |
+
)
|