Update vtoonify_model.py
Browse files- vtoonify_model.py +19 -18
vtoonify_model.py
CHANGED
@@ -193,27 +193,28 @@ class Model():
|
|
193 |
exstyle, _ = self.load_model(style_type)
|
194 |
if exstyle is None:
|
195 |
return np.zeros((256, 256, 3), np.uint8), 'Oops, something wrong with the style type. Please go to Step 1 and load model again.'
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
|
|
|
|
206 |
scale_factor=0.5, recompute_scale_factor=False).detach()
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
|
213 |
return ((y_tilde[0].cpu().numpy().transpose(1, 2, 0) + 1.0) * 127.5).astype(np.uint8), 'Successfully toonify the image with style of %s' % (self.style_name)
|
214 |
-
|
215 |
-
|
216 |
-
return np.zeros((256, 256, 3), np.uint8), f"Error during processing: {str(e)}"
|
217 |
def tensor2cv2(self, img):
|
218 |
"""Convert a tensor image to OpenCV format."""
|
219 |
tmp = ((img.cpu().numpy().transpose(1, 2, 0) + 1.0) * 127.5).astype(np.uint8).copy()
|
|
|
193 |
exstyle, _ = self.load_model(style_type)
|
194 |
if exstyle is None:
|
195 |
return np.zeros((256, 256, 3), np.uint8), 'Oops, something wrong with the style type. Please go to Step 1 and load model again.'
|
196 |
+
|
197 |
+
try:
|
198 |
+
with torch.no_grad():
|
199 |
+
if self.color_transfer:
|
200 |
+
s_w = exstyle
|
201 |
+
else:
|
202 |
+
s_w = instyle.clone()
|
203 |
+
s_w[:, :7] = exstyle[:, :7]
|
204 |
+
|
205 |
+
x = self.transform(aligned_face).unsqueeze(dim=0).to(self.device)
|
206 |
+
logging.info(f"Input to VToonify shape: {x.shape}")
|
207 |
+
x_p = F.interpolate(self.parsingpredictor(2 * (F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=False)))[0],
|
208 |
scale_factor=0.5, recompute_scale_factor=False).detach()
|
209 |
+
inputs = torch.cat((x, x_p / 16.), dim=1)
|
210 |
+
y_tilde = self.vtoonify(inputs, s_w.repeat(inputs.size(0), 1, 1), d_s=style_degree)
|
211 |
+
y_tilde = torch.clamp(y_tilde, -1, 1)
|
212 |
+
logging.info(f"Output from VToonify shape: {y_tilde.shape}")
|
213 |
+
print('*** Toonify %dx%d image with style of %s' % (y_tilde.shape[2], y_tilde.shape[3], style_type))
|
214 |
|
215 |
return ((y_tilde[0].cpu().numpy().transpose(1, 2, 0) + 1.0) * 127.5).astype(np.uint8), 'Successfully toonify the image with style of %s' % (self.style_name)
|
216 |
+
|
217 |
+
|
|
|
218 |
def tensor2cv2(self, img):
|
219 |
"""Convert a tensor image to OpenCV format."""
|
220 |
tmp = ((img.cpu().numpy().transpose(1, 2, 0) + 1.0) * 127.5).astype(np.uint8).copy()
|