goryhon commited on
Commit
259efb1
·
verified ·
1 Parent(s): 21504a0

Update web-demos/hugging_face/inpainter/base_inpainter.py

Browse files
web-demos/hugging_face/inpainter/base_inpainter.py CHANGED
@@ -203,9 +203,13 @@ class ProInpainter:
203
 
204
  size = frames[0].size
205
  # The ouput size should be divided by 2 so that it can encoded by libx264
206
- if ratio !=1.0: size = (int(ratio*size[0])//2*2, int(ratio*size[1])//2*2)
207
 
208
- else: size = ((size[0])//2*2, (size[1])//2*2) # set propainter size limit to 720 to reduce memory usage
 
 
 
 
209
 
210
  frames_len = len(frames)
211
  frames, size, out_size = resize_frames(frames, size)
@@ -246,7 +250,6 @@ class ProInpainter:
246
  gt_flows_f_list.append(flows_f)
247
  gt_flows_b_list.append(flows_b)
248
  torch.cuda.empty_cache()
249
- torch.cuda.ipc_collect()
250
 
251
  gt_flows_f = torch.cat(gt_flows_f_list, dim=1)
252
  gt_flows_b = torch.cat(gt_flows_b_list, dim=1)
@@ -254,7 +257,6 @@ class ProInpainter:
254
  else:
255
  gt_flows_bi = self.fix_raft(frames, iters=raft_iter)
256
  torch.cuda.empty_cache()
257
- torch.cuda.ipc_collect()
258
 
259
  if self.use_half:
260
  frames, flow_masks, masks_dilated = frames.half(), flow_masks.half(), masks_dilated.half()
@@ -281,7 +283,7 @@ class ProInpainter:
281
  pred_flows_f.append(pred_flows_bi_sub[0][:, pad_len_s:e_f-s_f-pad_len_e])
282
  pred_flows_b.append(pred_flows_bi_sub[1][:, pad_len_s:e_f-s_f-pad_len_e])
283
  torch.cuda.empty_cache()
284
- torch.cuda.ipc_collect()
285
  pred_flows_f = torch.cat(pred_flows_f, dim=1)
286
  pred_flows_b = torch.cat(pred_flows_b, dim=1)
287
  pred_flows_bi = (pred_flows_f, pred_flows_b)
@@ -289,7 +291,6 @@ class ProInpainter:
289
  pred_flows_bi, _ = self.fix_flow_complete.forward_bidirect_flow(gt_flows_bi, flow_masks)
290
  pred_flows_bi = self.fix_flow_complete.combine_flow(gt_flows_bi, pred_flows_bi, flow_masks)
291
  torch.cuda.empty_cache()
292
- torch.cuda.ipc_collect()
293
 
294
  # ---- image propagation ----
295
  masked_frames = frames * (1 - masks_dilated)
@@ -316,7 +317,7 @@ class ProInpainter:
316
  updated_frames.append(updated_frames_sub[:, pad_len_s:e_f-s_f-pad_len_e])
317
  updated_masks.append(updated_masks_sub[:, pad_len_s:e_f-s_f-pad_len_e])
318
  torch.cuda.empty_cache()
319
- torch.cuda.ipc_collect()
320
  updated_frames = torch.cat(updated_frames, dim=1)
321
  updated_masks = torch.cat(updated_masks, dim=1)
322
  else:
@@ -325,7 +326,6 @@ class ProInpainter:
325
  updated_frames = frames * (1 - masks_dilated) + prop_imgs.view(b, t, 3, h, w) * masks_dilated
326
  updated_masks = updated_local_masks.view(b, t, 1, h, w)
327
  torch.cuda.empty_cache()
328
- torch.cuda.ipc_collect()
329
 
330
  ori_frames = frames_inp
331
  comp_frames = [None] * video_length
@@ -373,9 +373,8 @@ class ProInpainter:
373
  comp_frames[idx] = comp_frames[idx].astype(np.uint8)
374
 
375
  torch.cuda.empty_cache()
376
- torch.cuda.ipc_collect()
377
 
378
  # need to return numpy array, T, H, W, 3
379
  comp_frames = [cv2.resize(f, out_size) for f in comp_frames]
380
 
381
- return comp_frames
 
203
 
204
  size = frames[0].size
205
  # The ouput size should be divided by 2 so that it can encoded by libx264
206
+ size = (int(ratio*size[0])//2*2, int(ratio*size[1])//2*2)
207
 
208
+ # set propainter size limit to 720 to reduce memory usage
209
+ if max(size[0], size[1]) > 720:
210
+ scale = 720.0 / max(size[0], size[1])
211
+ # The ouput size should be divided by 2 so that it can encoded by libx264
212
+ size = (int(scale*size[0])//2*2, int(scale*size[1])//2*2)
213
 
214
  frames_len = len(frames)
215
  frames, size, out_size = resize_frames(frames, size)
 
250
  gt_flows_f_list.append(flows_f)
251
  gt_flows_b_list.append(flows_b)
252
  torch.cuda.empty_cache()
 
253
 
254
  gt_flows_f = torch.cat(gt_flows_f_list, dim=1)
255
  gt_flows_b = torch.cat(gt_flows_b_list, dim=1)
 
257
  else:
258
  gt_flows_bi = self.fix_raft(frames, iters=raft_iter)
259
  torch.cuda.empty_cache()
 
260
 
261
  if self.use_half:
262
  frames, flow_masks, masks_dilated = frames.half(), flow_masks.half(), masks_dilated.half()
 
283
  pred_flows_f.append(pred_flows_bi_sub[0][:, pad_len_s:e_f-s_f-pad_len_e])
284
  pred_flows_b.append(pred_flows_bi_sub[1][:, pad_len_s:e_f-s_f-pad_len_e])
285
  torch.cuda.empty_cache()
286
+
287
  pred_flows_f = torch.cat(pred_flows_f, dim=1)
288
  pred_flows_b = torch.cat(pred_flows_b, dim=1)
289
  pred_flows_bi = (pred_flows_f, pred_flows_b)
 
291
  pred_flows_bi, _ = self.fix_flow_complete.forward_bidirect_flow(gt_flows_bi, flow_masks)
292
  pred_flows_bi = self.fix_flow_complete.combine_flow(gt_flows_bi, pred_flows_bi, flow_masks)
293
  torch.cuda.empty_cache()
 
294
 
295
  # ---- image propagation ----
296
  masked_frames = frames * (1 - masks_dilated)
 
317
  updated_frames.append(updated_frames_sub[:, pad_len_s:e_f-s_f-pad_len_e])
318
  updated_masks.append(updated_masks_sub[:, pad_len_s:e_f-s_f-pad_len_e])
319
  torch.cuda.empty_cache()
320
+
321
  updated_frames = torch.cat(updated_frames, dim=1)
322
  updated_masks = torch.cat(updated_masks, dim=1)
323
  else:
 
326
  updated_frames = frames * (1 - masks_dilated) + prop_imgs.view(b, t, 3, h, w) * masks_dilated
327
  updated_masks = updated_local_masks.view(b, t, 1, h, w)
328
  torch.cuda.empty_cache()
 
329
 
330
  ori_frames = frames_inp
331
  comp_frames = [None] * video_length
 
373
  comp_frames[idx] = comp_frames[idx].astype(np.uint8)
374
 
375
  torch.cuda.empty_cache()
 
376
 
377
  # need to return numpy array, T, H, W, 3
378
  comp_frames = [cv2.resize(f, out_size) for f in comp_frames]
379
 
380
+ return comp_frames