Skip to content

Commit 4a90101

Browse files
committed
Added MPS for Mac Users
1 parent c646ff7 commit 4a90101

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

api.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,25 @@
33
from helper import *
44
from model.generator import SkipEncoderDecoder, input_noise
55

6-
def remove_watermark(image_path, mask_path, max_dim, reg_noise, input_depth, lr, show_step, training_steps, tqdm_length = 100):
7-
DTYPE = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor
8-
if not torch.cuda.is_available():
9-
print('\nSetting device to "cpu", since torch is not built with "cuda" support...')
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...')
1025
print('It is recommended to use GPU if possible...')
1126

1227
image_np, mask_np = preprocess_images(image_path, mask_path, max_dim)
@@ -17,43 +32,43 @@ def remove_watermark(image_path, mask_path, max_dim, reg_noise, input_depth, lr,
1732
num_channels_down = [128] * 5,
1833
num_channels_up = [128] * 5,
1934
num_channels_skip = [128] * 5
20-
).type(DTYPE)
35+
).type(DTYPE).to(device)
2136

22-
objective = torch.nn.MSELoss().type(DTYPE)
37+
objective = torch.nn.MSELoss().type(DTYPE).to(device)
2338
optimizer = optim.Adam(generator.parameters(), lr)
2439

25-
image_var = np_to_torch_array(image_np).type(DTYPE)
26-
mask_var = np_to_torch_array(mask_np).type(DTYPE)
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)
2742

28-
generator_input = input_noise(input_depth, image_np.shape[1:]).type(DTYPE)
43+
generator_input = input_noise(input_depth, image_np.shape[1:]).type(DTYPE).to(device)
2944

3045
generator_input_saved = generator_input.detach().clone()
3146
noise = generator_input.detach().clone()
3247

3348
print('\nStarting training...\n')
3449

35-
progress_bar = tqdm(range(training_steps), desc = 'Completed', ncols = tqdm_length)
50+
progress_bar = tqdm(range(training_steps), desc='Completed', ncols=tqdm_length)
3651

3752
for step in progress_bar:
3853
optimizer.zero_grad()
3954
generator_input = generator_input_saved
4055

4156
if reg_noise > 0:
4257
generator_input = generator_input_saved + (noise.normal_() * reg_noise)
43-
58+
4459
output = generator(generator_input)
45-
60+
4661
loss = objective(output * mask_var, image_var * mask_var)
4762
loss.backward()
4863

4964
if step % show_step == 0:
5065
output_image = torch_to_np_array(output)
5166
visualize_sample(image_np, output_image, nrow = 2, size_factor = 10)
52-
67+
5368
progress_bar.set_postfix(Loss = loss.item())
54-
69+
5570
optimizer.step()
56-
71+
5772
output_image = torch_to_np_array(output)
5873
visualize_sample(output_image, nrow = 1, size_factor = 10)
5974

@@ -62,4 +77,4 @@ def remove_watermark(image_path, mask_path, max_dim, reg_noise, input_depth, lr,
6277
output_path = image_path.split('/')[-1].split('.')[-2] + '-output.jpg'
6378
print(f'\nSaving final output image to: "{output_path}"\n')
6479

65-
pil_image.save(output_path)
80+
pil_image.save(output_path)

0 commit comments

Comments
 (0)