Spaces:
Running
Running
Update lama_inpaint.py
Browse files- lama_inpaint.py +199 -200
lama_inpaint.py
CHANGED
@@ -1,200 +1,199 @@
|
|
1 |
-
import os
|
2 |
-
import sys
|
3 |
-
import numpy as np
|
4 |
-
import torch
|
5 |
-
import yaml
|
6 |
-
import glob
|
7 |
-
import argparse
|
8 |
-
from PIL import Image
|
9 |
-
from omegaconf import OmegaConf
|
10 |
-
from pathlib import Path
|
11 |
-
|
12 |
-
os.environ[
|
13 |
-
os.environ[
|
14 |
-
os.environ[
|
15 |
-
os.environ[
|
16 |
-
os.environ[
|
17 |
-
|
18 |
-
sys.path.insert(0, str(Path(__file__).resolve().parent / "lama"))
|
19 |
-
|
20 |
-
from saicinpainting.
|
21 |
-
from saicinpainting.
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
)
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
predict_config.model.path
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
batch =
|
65 |
-
batch
|
66 |
-
batch[
|
67 |
-
|
68 |
-
batch
|
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 |
-
predict_config.model.path,
|
96 |
-
|
97 |
-
|
98 |
-
train_config =
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
model.
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
unpad_to_size
|
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 |
-
save_array_to_img(img_inpainted, img_inpainted_p)
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
import yaml
|
6 |
+
import glob
|
7 |
+
import argparse
|
8 |
+
from PIL import Image
|
9 |
+
from omegaconf import OmegaConf
|
10 |
+
from pathlib import Path
|
11 |
+
|
12 |
+
os.environ["OMP_NUM_THREADS"] = "1"
|
13 |
+
os.environ["OPENBLAS_NUM_THREADS"] = "1"
|
14 |
+
os.environ["MKL_NUM_THREADS"] = "1"
|
15 |
+
os.environ["VECLIB_MAXIMUM_THREADS"] = "1"
|
16 |
+
os.environ["NUMEXPR_NUM_THREADS"] = "1"
|
17 |
+
|
18 |
+
sys.path.insert(0, str(Path(__file__).resolve().parent / "lama"))
|
19 |
+
|
20 |
+
from saicinpainting.evaluation.utils import move_to_device
|
21 |
+
from saicinpainting.training.trainers import load_checkpoint
|
22 |
+
from saicinpainting.evaluation.data import pad_tensor_to_modulo
|
23 |
+
|
24 |
+
from utils import load_img_to_array, save_array_to_img
|
25 |
+
|
26 |
+
|
27 |
+
@torch.no_grad()
|
28 |
+
def inpaint_img_with_lama(
|
29 |
+
img: np.ndarray, mask: np.ndarray, config_p: str, ckpt_p: str, mod=8, device="cuda"
|
30 |
+
):
|
31 |
+
assert len(mask.shape) == 2
|
32 |
+
if np.max(mask) == 1:
|
33 |
+
mask = mask * 255
|
34 |
+
img = torch.from_numpy(img).float().div(255.0)
|
35 |
+
mask = torch.from_numpy(mask).float()
|
36 |
+
predict_config = OmegaConf.load(config_p)
|
37 |
+
predict_config.model.path = ckpt_p
|
38 |
+
# device = torch.device(predict_config.device)
|
39 |
+
device = torch.device(device)
|
40 |
+
|
41 |
+
train_config_path = os.path.join(predict_config.model.path, "config.yaml")
|
42 |
+
|
43 |
+
with open(train_config_path, "r") as f:
|
44 |
+
train_config = OmegaConf.create(yaml.safe_load(f))
|
45 |
+
|
46 |
+
train_config.training_model.predict_only = True
|
47 |
+
train_config.visualizer.kind = "noop"
|
48 |
+
|
49 |
+
checkpoint_path = os.path.join(
|
50 |
+
predict_config.model.path, "models", predict_config.model.checkpoint
|
51 |
+
)
|
52 |
+
model = load_checkpoint(
|
53 |
+
train_config, checkpoint_path, strict=False, map_location=device
|
54 |
+
)
|
55 |
+
model.freeze()
|
56 |
+
if not predict_config.get("refine", False):
|
57 |
+
model.to(device)
|
58 |
+
|
59 |
+
batch = {}
|
60 |
+
batch["image"] = img.permute(2, 0, 1).unsqueeze(0)
|
61 |
+
batch["mask"] = mask[None, None]
|
62 |
+
unpad_to_size = [batch["image"].shape[2], batch["image"].shape[3]]
|
63 |
+
batch["image"] = pad_tensor_to_modulo(batch["image"], mod)
|
64 |
+
batch["mask"] = pad_tensor_to_modulo(batch["mask"], mod)
|
65 |
+
batch = move_to_device(batch, device)
|
66 |
+
batch["mask"] = (batch["mask"] > 0) * 1
|
67 |
+
|
68 |
+
batch = model(batch)
|
69 |
+
cur_res = batch[predict_config.out_key][0].permute(1, 2, 0)
|
70 |
+
cur_res = cur_res.detach().cpu().numpy()
|
71 |
+
|
72 |
+
if unpad_to_size is not None:
|
73 |
+
orig_height, orig_width = unpad_to_size
|
74 |
+
cur_res = cur_res[:orig_height, :orig_width]
|
75 |
+
|
76 |
+
cur_res = np.clip(cur_res * 255, 0, 255).astype("uint8")
|
77 |
+
return cur_res
|
78 |
+
|
79 |
+
|
80 |
+
def build_lama_model(config_p: str, ckpt_p: str, device="cuda"):
|
81 |
+
predict_config = OmegaConf.load(config_p)
|
82 |
+
predict_config.model.path = ckpt_p
|
83 |
+
# device = torch.device(predict_config.device)
|
84 |
+
device = torch.device(device)
|
85 |
+
|
86 |
+
train_config_path = os.path.join(predict_config.model.path, "config.yaml")
|
87 |
+
|
88 |
+
with open(train_config_path, "r") as f:
|
89 |
+
train_config = OmegaConf.create(yaml.safe_load(f))
|
90 |
+
|
91 |
+
train_config.training_model.predict_only = True
|
92 |
+
train_config.visualizer.kind = "noop"
|
93 |
+
|
94 |
+
checkpoint_path = os.path.join(
|
95 |
+
predict_config.model.path, "models", predict_config.model.checkpoint
|
96 |
+
)
|
97 |
+
model = load_checkpoint(
|
98 |
+
train_config, checkpoint_path, strict=False, map_location=device
|
99 |
+
)
|
100 |
+
model.freeze()
|
101 |
+
if not predict_config.get("refine", False):
|
102 |
+
model.to(device)
|
103 |
+
|
104 |
+
return model
|
105 |
+
|
106 |
+
|
107 |
+
@torch.no_grad()
|
108 |
+
def inpaint_img_with_builded_lama(
|
109 |
+
model, img: np.ndarray, mask: np.ndarray, config_p: str, mod=8, device="cuda"
|
110 |
+
):
|
111 |
+
assert len(mask.shape) == 2
|
112 |
+
if np.max(mask) == 1:
|
113 |
+
mask = mask * 255
|
114 |
+
img = torch.from_numpy(img).float().div(255.0)
|
115 |
+
mask = torch.from_numpy(mask).float()
|
116 |
+
predict_config = OmegaConf.load(config_p)
|
117 |
+
|
118 |
+
batch = {}
|
119 |
+
batch["image"] = img.permute(2, 0, 1).unsqueeze(0)
|
120 |
+
batch["mask"] = mask[None, None]
|
121 |
+
unpad_to_size = [batch["image"].shape[2], batch["image"].shape[3]]
|
122 |
+
batch["image"] = pad_tensor_to_modulo(batch["image"], mod)
|
123 |
+
batch["mask"] = pad_tensor_to_modulo(batch["mask"], mod)
|
124 |
+
batch = move_to_device(batch, device)
|
125 |
+
batch["mask"] = (batch["mask"] > 0) * 1
|
126 |
+
|
127 |
+
batch = model(batch)
|
128 |
+
cur_res = batch[predict_config.out_key][0].permute(1, 2, 0)
|
129 |
+
cur_res = cur_res.detach().cpu().numpy()
|
130 |
+
|
131 |
+
if unpad_to_size is not None:
|
132 |
+
orig_height, orig_width = unpad_to_size
|
133 |
+
cur_res = cur_res[:orig_height, :orig_width]
|
134 |
+
|
135 |
+
cur_res = np.clip(cur_res * 255, 0, 255).astype("uint8")
|
136 |
+
return cur_res
|
137 |
+
|
138 |
+
|
139 |
+
def setup_args(parser):
|
140 |
+
parser.add_argument(
|
141 |
+
"--input_img",
|
142 |
+
type=str,
|
143 |
+
required=True,
|
144 |
+
help="Path to a single input img",
|
145 |
+
)
|
146 |
+
parser.add_argument(
|
147 |
+
"--input_mask_glob",
|
148 |
+
type=str,
|
149 |
+
required=True,
|
150 |
+
help="Glob to input masks",
|
151 |
+
)
|
152 |
+
parser.add_argument(
|
153 |
+
"--output_dir",
|
154 |
+
type=str,
|
155 |
+
required=True,
|
156 |
+
help="Output path to the directory with results.",
|
157 |
+
)
|
158 |
+
parser.add_argument(
|
159 |
+
"--lama_config",
|
160 |
+
type=str,
|
161 |
+
default="./third_party/lama/configs/prediction/default.yaml",
|
162 |
+
help="The path to the config file of lama model. "
|
163 |
+
"Default: the config of big-lama",
|
164 |
+
)
|
165 |
+
parser.add_argument(
|
166 |
+
"--lama_ckpt",
|
167 |
+
type=str,
|
168 |
+
required=True,
|
169 |
+
help="The path to the lama checkpoint.",
|
170 |
+
)
|
171 |
+
|
172 |
+
|
173 |
+
if __name__ == "__main__":
|
174 |
+
"""Example usage:
|
175 |
+
python lama_inpaint.py \
|
176 |
+
--input_img FA_demo/FA1_dog.png \
|
177 |
+
--input_mask_glob "results/FA1_dog/mask*.png" \
|
178 |
+
--output_dir results \
|
179 |
+
--lama_config lama/configs/prediction/default.yaml \
|
180 |
+
--lama_ckpt big-lama
|
181 |
+
"""
|
182 |
+
parser = argparse.ArgumentParser()
|
183 |
+
setup_args(parser)
|
184 |
+
args = parser.parse_args(sys.argv[1:])
|
185 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
186 |
+
|
187 |
+
img_stem = Path(args.input_img).stem
|
188 |
+
mask_ps = sorted(glob.glob(args.input_mask_glob))
|
189 |
+
out_dir = Path(args.output_dir) / img_stem
|
190 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
191 |
+
|
192 |
+
img = load_img_to_array(args.input_img)
|
193 |
+
for mask_p in mask_ps:
|
194 |
+
mask = load_img_to_array(mask_p)
|
195 |
+
img_inpainted_p = out_dir / f"inpainted_with_{Path(mask_p).name}"
|
196 |
+
img_inpainted = inpaint_img_with_lama(
|
197 |
+
img, mask, args.lama_config, args.lama_ckpt, device=device
|
198 |
+
)
|
199 |
+
save_array_to_img(img_inpainted, img_inpainted_p)
|
|