-
Notifications
You must be signed in to change notification settings - Fork 33
/
predict.py
197 lines (162 loc) · 6.32 KB
/
predict.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import numpy as np
import tempfile
import torch
import cv2
from vit_pytorch import ViT
from einops import rearrange
from cog import BasePredictor, Input, Path
from models.binae import BinModel
THRESHOLD = 0.5 ## binarization threshold after the model output
SPLITSIZE = 256 ## your image will be divided into patches of 256x256 pixels
image_size = (
SPLITSIZE,
SPLITSIZE,
) ## your image will be divided into patches of 256x256 pixels
class Predictor(BasePredictor):
def setup(self):
"""Load the model into memory to make running multiple predictions efficient"""
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.settings = {
"base": {
"ENCODERLAYERS": 6,
"ENCODERHEADS": 8,
"ENCODERDIM": 768,
"patch_size": 8,
},
"large": {
"ENCODERLAYERS": 12,
"ENCODERHEADS": 16,
"ENCODERDIM": 1024,
"patch_size": 16,
},
}
v = {
k: ViT(
image_size=image_size,
patch_size=self.settings[k]["patch_size"],
num_classes=1000,
dim=self.settings[k]["ENCODERDIM"],
depth=self.settings[k]["ENCODERLAYERS"],
heads=self.settings[k]["ENCODERHEADS"],
mlp_dim=2048,
)
for k in ["base", "large"]
}
self.models = {
k: BinModel(
encoder=v[k],
decoder_dim=self.settings[k]["ENCODERDIM"],
decoder_depth=self.settings[k]["ENCODERLAYERS"],
decoder_heads=self.settings[k]["ENCODERHEADS"],
)
for k in ["base", "large"]
}
self.models["base"].to(self.device)
self.models["large"].to(self.device)
base_model_path = "weights/best-model_8_2018base_256_8.pt" # weights are pre-downloaded
self.models["base"].load_state_dict(
torch.load(base_model_path, map_location=self.device)
)
large_model_path = "weights/best-model_16_2018large_256_16.pt"
self.models["large"].load_state_dict(
torch.load(large_model_path, map_location=self.device)
)
def predict(
self,
image: Path = Input(
description="Input Image.",
),
model_size: str = Input(
default="base",
choices=["base", "large"],
description="Choose the model size.",
),
) -> Path:
"""Run a single prediction on the model"""
deg_image = cv2.imread(str(image)) / 255
## Split the image intop patches, an image is padded first to make it dividable by the split size
h = ((deg_image.shape[0] // 256) + 1) * 256
w = ((deg_image.shape[1] // 256) + 1) * 256
deg_image_padded = np.ones((h, w, 3))
deg_image_padded[: deg_image.shape[0], : deg_image.shape[1], :] = deg_image
patches = split(deg_image_padded, deg_image.shape[0], deg_image.shape[1])
## preprocess the patches (images)
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
out_patches = []
for p in patches:
out_patch = np.zeros([3, *p.shape[:-1]])
for i in range(3):
out_patch[i] = (p[:, :, i] - mean[i]) / std[i]
out_patches.append(out_patch)
result = []
for patch_idx, p in enumerate(out_patches):
print(f"({patch_idx} / {len(out_patches) - 1}) processing patch...")
p = np.array(p, dtype="float32")
train_in = torch.from_numpy(p)
with torch.no_grad():
train_in = train_in.view(1, 3, SPLITSIZE, SPLITSIZE).to(self.device)
_ = torch.rand((train_in.shape)).to(self.device)
loss, _, pred_pixel_values = self.models[model_size](train_in, _)
rec_patches = pred_pixel_values
rec_image = torch.squeeze(
rearrange(
rec_patches,
"b (h w) (p1 p2 c) -> b c (h p1) (w p2)",
p1=self.settings[model_size]["patch_size"],
p2=self.settings[model_size]["patch_size"],
h=image_size[0] // self.settings[model_size]["patch_size"],
)
)
impred = rec_image.cpu().numpy()
impred = np.transpose(impred, (1, 2, 0))
for ch in range(3):
impred[:, :, ch] = (impred[:, :, ch] * std[ch]) + mean[ch]
impred[np.where(impred > 1)] = 1
impred[np.where(impred < 0)] = 0
result.append(impred)
clean_image = merge_image(
result, deg_image_padded.shape[0], deg_image_padded.shape[1]
)
clean_image = clean_image[: deg_image.shape[0], : deg_image.shape[1], :]
clean_image = (clean_image > THRESHOLD) * 255
output_path = Path(tempfile.mkdtemp()) / "output.png"
cv2.imwrite(str(output_path), clean_image)
return output_path
def split(im, h, w):
"""
split image into patches
Args:
im (np.array): image to be splitted
h (int): image height
w (int): image width
Returns:
patches [np.array, ..., np.array]: list of patches with size SPLITSIZExSPLITSIZE
obtained from image
"""
patches = []
nsize1 = SPLITSIZE
nsize2 = SPLITSIZE
for ii in range(0, h, nsize1): # 2048
for iii in range(0, w, nsize2): # 1536
patches.append(im[ii : ii + nsize1, iii : iii + nsize2, :])
return patches
def merge_image(splitted_images, h, w):
"""
merge patches into image and return it
Args:
splitted_images [np.array, ..., np.array]: list of patches to costruct the image
h (int): final image height
w (int): final image width
Returns:
image (np.array): the constructed image
"""
image = np.zeros(((h, w, 3)))
nsize1 = SPLITSIZE
nsize2 = SPLITSIZE
ind = 0
for ii in range(0, h, nsize1):
for iii in range(0, w, nsize2):
image[ii : ii + nsize1, iii : iii + nsize2, :] = splitted_images[ind]
ind += 1
return image