-
Notifications
You must be signed in to change notification settings - Fork 0
/
inpainting.py
117 lines (92 loc) · 3.47 KB
/
inpainting.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
from dataclasses import dataclass
import torch
import torch.nn.functional as F
from tqdm import tqdm
from torchvision import io, transforms
import numpy as np
import argparse
import torch.nn as nn
import glob
import os
import shutil
from pathlib import Path
from dip import DIP
from utils import save, clear_dir
device = torch.device("cuda")
@dataclass
class InfillConfig:
image_path: str
save_dir: str
img_size: int
epochs: int
lr: float
remove_percent: float
verbose: bool
def dip_infill(config: InfillConfig):
clear_dir(config.save_dir)
transform = transforms.Resize((config.img_size, config.img_size))
im = io.read_image(config.image_path) / 255
target = transform(im).clip(0,1).unsqueeze(0).to(device)
save(target, f"{config.save_dir}/original.png")
mask = torch.ones(1, 1, config.img_size, config.img_size, device=device)
while 100 - mask.sum()/config.img_size/config.img_size*100 < config.remove_percent:
bounds = np.int32(np.random.rand(2)*config.img_size)
size = np.int32(np.random.rand(2)*config.img_size / 20)
mask[:,:,bounds[0]:bounds[0]+size[0],bounds[1]:bounds[1]+size[1]] = 0
if config.verbose:
print(f"{100 - mask.sum()/config.img_size/config.img_size*100:.2f}% of image removed")
save(mask.repeat(1,3,1,1), f"{config.save_dir}/mask.png")
save(target*mask, f"{config.save_dir}/input.png")
dip_args = {
"z_channels": 32,
"z_scale": 1/10,
"z_noise": 0,
"filters_down" : [16, 32, 64, 128, 128, 128, 128],
"filters_up" : [16, 32, 64, 128, 128, 128, 128],
"kernels_down" : [3, 3, 3, 3, 3, 3, 3],
"kernels_up" : [5, 5, 5, 5, 5, 5, 5],
"filters_skip" : [0, 0, 0, 0, 0, 0, 0],
"kernels_skip" : [0, 0, 0, 1, 1, 1, 1],
"upsampling" : "nearest"
}
dip = DIP(config.img_size, 3, **dip_args).to(device)
optimizer = torch.optim.Adam(dip.parameters(), lr=config.lr)
dip.train()
save_points = set([i for i in np.arange(0, config.epochs, (config.epochs+1)//10)] + [config.epochs])
saves = []
if config.verbose:
rg = tqdm(range(config.epochs+1))
else:
rg = range(config.epochs+1)
for i in rg:
y = dip()
loss = torch.norm(y*mask - target*mask)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if config.verbose:
rg.set_description(f"(Loss {loss.item():.4f})")
if i in save_points:
saves.append(y)
save(torch.cat(saves, dim=2), f"{config.save_dir}/saves.png")
save(saves[-1], f"{config.save_dir}/epoch-{i:06}.png")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run DIP infilling")
parser.add_argument('image_path', type=str)
parser.add_argument('save_dir', type=str)
parser.add_argument('--img_size', type=int, default=1024)
parser.add_argument('--epochs', type=int, default=5000)
parser.add_argument('--lr', type=float, default=0.1)
parser.add_argument('--remove_percent', type=float, default=25)
parser.add_argument('--verbose', action=argparse.BooleanOptionalAction, default=True)
args = parser.parse_args()
config = InfillConfig(
image_path = args.image_path,
save_dir = args.save_dir.rstrip("/"),
img_size = args.img_size,
epochs = args.epochs,
lr = args.lr,
remove_percent=args.remove_percent,
verbose = args.verbose,
)
dip_infill(config)