File size: 12,095 Bytes
320e465
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# -*- coding: utf-8 -*-
import sys 
sys.path.append(".") 

import os
import cv2
import numpy as np
import argparse
from PIL import Image
import torch.nn.functional as F

import torch
from torch.utils.data import DataLoader

from model.modules.flow_comp_raft import RAFT_bi
from model.recurrent_flow_completion import RecurrentFlowCompleteNet
from model.propainter import InpaintGenerator

# from core.dataset import TestDataset
from core.dataset import TestDataset
from core.metrics import calc_psnr_and_ssim, calculate_i3d_activations, calculate_vfid, init_i3d_model

from time import time

import warnings
warnings.filterwarnings("ignore")

# sample reference frames from the whole video
def get_ref_index(neighbor_ids, length, ref_stride=10):
    ref_index = []
    for i in range(0, length, ref_stride):
        if i not in neighbor_ids:
            ref_index.append(i)
    return ref_index


def main_worker(args):
    args.size = (args.width, args.height)
    w, h = args.size    
    # set up datasets and data loader
    assert (args.dataset == 'davis') or args.dataset == 'youtube-vos', \
        f"{args.dataset} dataset is not supported"
    test_dataset = TestDataset(vars(args))

    test_loader = DataLoader(test_dataset,
                             batch_size=1,
                             shuffle=False,
                             num_workers=args.num_workers)

    # set up models
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    fix_raft = RAFT_bi(args.raft_model_path, device)

    fix_flow_complete = RecurrentFlowCompleteNet(args.fc_model_path)
    for p in fix_flow_complete.parameters():
        p.requires_grad = False
    fix_flow_complete.to(device)
    fix_flow_complete.eval()

    model = InpaintGenerator(model_path=args.propainter_model_path).to(device)
    model.eval()

    time_all = []


    print('Start evaluation ...')
    if args.task == 'video_completion':
        result_path = os.path.join(f'results_eval', 
            f'{args.dataset}_rs_{args.ref_stride}_nl_{args.neighbor_length}_video_completion')
        if not os.path.exists(result_path):
            os.makedirs(result_path, exist_ok=True)
        eval_summary = open(os.path.join(result_path, f"{args.dataset}_metrics.txt"),"w")
        total_frame_psnr = []
        total_frame_ssim = []
        output_i3d_activations = []
        real_i3d_activations = []
        i3d_model = init_i3d_model('weights/i3d_rgb_imagenet.pt')
    else:
        result_path = os.path.join(f'results_eval', 
            f'{args.dataset}_rs_{args.ref_stride}_nl_{args.neighbor_length}_object_removal')
        if not os.path.exists(result_path):
            os.makedirs(result_path, exist_ok=True)        

    if not os.path.exists(result_path):
        os.makedirs(result_path)
        
        
    for index, items in enumerate(test_loader):
        torch.cuda.empty_cache()

        # frames, masks, video_name, frames_PIL = items
        frames, masks, flows_f, flows_b, video_name, frames_PIL = items
        video_name = video_name[0]
        print('Processing:', video_name)

        video_length = frames.size(1)
        frames, masks = frames.to(device), masks.to(device)
        masked_frames = frames * (1 - masks)

        torch.cuda.synchronize()
        time_start = time()

        with torch.no_grad():
            # ---- compute flow ----
            if args.load_flow:
                gt_flows_bi = (flows_f.to(device), flows_b.to(device))
            else:
                short_len = 60
                if frames.size(1) > short_len:
                    gt_flows_f_list, gt_flows_b_list = [], []
                    for f in range(0, video_length, short_len):
                        end_f = min(video_length, f + short_len)
                        if f == 0:
                            flows_f, flows_b = fix_raft(frames[:,f:end_f], iters=args.raft_iter)
                        else:
                            flows_f, flows_b = fix_raft(frames[:,f-1:end_f], iters=args.raft_iter)
                        
                        gt_flows_f_list.append(flows_f)
                        gt_flows_b_list.append(flows_b)
                        gt_flows_f = torch.cat(gt_flows_f_list, dim=1)
                        gt_flows_b = torch.cat(gt_flows_b_list, dim=1)
                        gt_flows_bi = (gt_flows_f, gt_flows_b)
                else:
                    gt_flows_bi = fix_raft(frames, iters=args.raft_iter)

            # ---- complete flow ----
            pred_flows_bi, _ = fix_flow_complete.forward_bidirect_flow(gt_flows_bi, masks)
            pred_flows_bi = fix_flow_complete.combine_flow(gt_flows_bi, pred_flows_bi, masks)
        
            # ---- temporal propagation ----
            prop_imgs, updated_local_masks = model.img_propagation(masked_frames, pred_flows_bi, masks, 'nearest')

            b, t, _, _, _ = masks.size()
            updated_masks = updated_local_masks.view(b, t, 1, h, w)
            updated_frames = frames * (1-masks) + prop_imgs.view(b, t, 3, h, w) * masks # merge
            
            del gt_flows_bi, frames, updated_local_masks
            if not args.load_flow:
                torch.cuda.empty_cache()

        ori_frames = frames_PIL
        ori_frames = [
            ori_frames[i].squeeze().cpu().numpy() for i in range(video_length)
        ]
        comp_frames = [None] * video_length

        # complete holes by our model
        neighbor_stride = args.neighbor_length // 2
        for f in range(0, video_length, neighbor_stride):
            neighbor_ids = [
                i for i in range(max(0, f - neighbor_stride),
                                 min(video_length, f + neighbor_stride + 1))
            ]
            ref_ids = get_ref_index(neighbor_ids, video_length, args.ref_stride)
            selected_imgs = updated_frames[:, neighbor_ids + ref_ids, :, :, :]
            selected_masks = masks[:, neighbor_ids + ref_ids, :, :, :]
            selected_update_masks = updated_masks[:, neighbor_ids + ref_ids, :, :, :]
            selected_pred_flows_bi = (pred_flows_bi[0][:, neighbor_ids[:-1], :, :, :], pred_flows_bi[1][:, neighbor_ids[:-1], :, :, :])
            
            with torch.no_grad():
                l_t = len(neighbor_ids)
                pred_img = model(selected_imgs, selected_pred_flows_bi, selected_masks, selected_update_masks, l_t)
                pred_img = pred_img.view(-1, 3, h, w)


                pred_img = (pred_img + 1) / 2
                pred_img = pred_img.cpu().permute(0, 2, 3, 1).numpy() * 255
                binary_masks = masks[0, neighbor_ids, :, :, :].cpu().permute(
                    0, 2, 3, 1).numpy().astype(np.uint8)
                for i in range(len(neighbor_ids)):
                    idx = neighbor_ids[i]
                    img = np.array(pred_img[i]).astype(np.uint8) * binary_masks[i] \
                        + ori_frames[idx] * (1 - binary_masks[i])
                    if comp_frames[idx] is None:
                        comp_frames[idx] = img
                    else:
                        comp_frames[idx] = comp_frames[idx].astype(
                            np.float32) * 0.5 + img.astype(np.float32) * 0.5
                    

        torch.cuda.synchronize()
        time_i = time() - time_start
        time_i = time_i*1.0/video_length
        time_all.append(time_i)

        if args.task == 'video_completion':
            # calculate metrics
            cur_video_psnr = []
            cur_video_ssim = []
            comp_PIL = []  # to calculate VFID
            frames_PIL = []
            for ori, comp in zip(ori_frames, comp_frames):
                psnr, ssim = calc_psnr_and_ssim(ori, comp)

                cur_video_psnr.append(psnr)
                cur_video_ssim.append(ssim)

                total_frame_psnr.append(psnr)
                total_frame_ssim.append(ssim)

                frames_PIL.append(Image.fromarray(ori.astype(np.uint8)))
                comp_PIL.append(Image.fromarray(comp.astype(np.uint8)))

            # saving i3d activations
            frames_i3d, comp_i3d = calculate_i3d_activations(frames_PIL,
                                                            comp_PIL,
                                                            i3d_model,
                                                            device=device)
            real_i3d_activations.append(frames_i3d)
            output_i3d_activations.append(comp_i3d)

            cur_psnr = sum(cur_video_psnr) / len(cur_video_psnr)
            cur_ssim = sum(cur_video_ssim) / len(cur_video_ssim)

            avg_psnr = sum(total_frame_psnr) / len(total_frame_psnr)
            avg_ssim = sum(total_frame_ssim) / len(total_frame_ssim)

            avg_time = sum(time_all) / len(time_all)
            print(
                f'[{index+1:3}/{len(test_loader)}] Name: {str(video_name):25} | PSNR/SSIM: {cur_psnr:.4f}/{cur_ssim:.4f} \
                    | Avg PSNR/SSIM: {avg_psnr:.4f}/{avg_ssim:.4f} | Time: {avg_time:.4f}'
            )
            eval_summary.write(
                f'[{index+1:3}/{len(test_loader)}] Name: {str(video_name):25} | PSNR/SSIM: {cur_psnr:.4f}/{cur_ssim:.4f} \
                    | Avg PSNR/SSIM: {avg_psnr:.4f}/{avg_ssim:.4f} | Time: {avg_time:.4f}\n'
            )
        else:
            avg_time = sum(time_all) / len(time_all)
            print(
                f'[{index+1:3}/{len(test_loader)}] Name: {str(video_name):25} | Time: {avg_time:.4f}'
            )

        # saving images for evaluating warpping errors
        if args.save_results:
            save_frame_path = os.path.join(result_path, video_name)
            if not os.path.exists(save_frame_path):
                os.makedirs(save_frame_path, exist_ok=False)

            for i, frame in enumerate(comp_frames):
                cv2.imwrite(
                    os.path.join(save_frame_path,
                                 str(i).zfill(5) + '.png'),
                    cv2.cvtColor(frame.astype(np.uint8), cv2.COLOR_RGB2BGR))
                
    if args.task == 'video_completion':
        avg_frame_psnr = sum(total_frame_psnr) / len(total_frame_psnr)
        avg_frame_ssim = sum(total_frame_ssim) / len(total_frame_ssim)

        fid_score = calculate_vfid(real_i3d_activations, output_i3d_activations)
        print('Finish evaluation... Average Frame PSNR/SSIM/VFID: '
            f'{avg_frame_psnr:.2f}/{avg_frame_ssim:.4f}/{fid_score:.3f} | Time: {avg_time:.4f}')
        eval_summary.write(
            'Finish evaluation... Average Frame PSNR/SSIM/VFID: '
            f'{avg_frame_psnr:.2f}/{avg_frame_ssim:.4f}/{fid_score:.3f} | Time: {avg_time:.4f}')
        eval_summary.close()
    else:
        print('Finish evaluation... Time: {avg_time:.4f}')


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--height', type=int, default=240)
    parser.add_argument('--width', type=int, default=432)
    parser.add_argument("--ref_stride", type=int, default=10)
    parser.add_argument("--neighbor_length", type=int, default=20)
    parser.add_argument("--raft_iter", type=int, default=20)
    parser.add_argument('--task', default='video_completion', choices=['object_removal', 'video_completion'])
    parser.add_argument('--raft_model_path', default='weights/raft-things.pth', type=str)
    parser.add_argument('--fc_model_path', default='weights/recurrent_flow_completion.pth', type=str)
    parser.add_argument('--propainter_model_path', default='weights/ProPainter.pth', type=str)
    parser.add_argument('--dataset', choices=['davis', 'youtube-vos'], type=str)
    parser.add_argument('--video_root', default='dataset_root', type=str)
    parser.add_argument('--mask_root', default='mask_root', type=str)
    parser.add_argument('--flow_root', default='flow_ground_truth_root', type=str)
    parser.add_argument('--load_flow', default=False, type=bool)
    parser.add_argument('--save_results', action='store_true')
    parser.add_argument('--num_workers', default=4, type=int)

    args = parser.parse_args()
    main_worker(args)