Ashrafb commited on
Commit
aa1f42d
·
verified ·
1 Parent(s): af53559

Update vtoonify_model.py

Browse files
Files changed (1) hide show
  1. 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
- with torch.no_grad():
197
- if self.color_transfer:
198
- s_w = exstyle
199
- else:
200
- s_w = instyle.clone()
201
- s_w[:, :7] = exstyle[:, :7]
202
-
203
- x = self.transform(aligned_face).unsqueeze(dim=0).to(self.device)
204
- logging.info(f"Input to VToonify shape: {x.shape}")
205
- x_p = F.interpolate(self.parsingpredictor(2*(F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=False)))[0],
 
 
206
  scale_factor=0.5, recompute_scale_factor=False).detach()
207
- inputs = torch.cat((x, x_p/16.), dim=1)
208
- y_tilde = self.vtoonify(inputs, s_w.repeat(inputs.size(0), 1, 1), d_s=style_degree)
209
- y_tilde = torch.clamp(y_tilde, -1, 1)
210
- logging.info(f"Output from VToonify shape: {y_tilde.shape}")
211
- print('*** Toonify %dx%d image with style of %s'%(y_tilde.shape[2], y_tilde.shape[3], style_type))
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
- except Exception as e:
215
- logging.error(f"Error during model execution: {e}")
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()