-
Notifications
You must be signed in to change notification settings - Fork 6
/
compress_boundary_segmentation.py
239 lines (198 loc) · 6.86 KB
/
compress_boundary_segmentation.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
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
"""
Compress the video through gradient-based optimization.
"""
import argparse
import gc
import logging
import subprocess
from pathlib import Path
import coloredlogs
import enlighten
import matplotlib.pyplot as plt
import seaborn as sns
import torch
import torch.nn.functional as F
import torchvision.transforms as T
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import io
from dnn.dnn_factory import DNN_Factory
from dnn.fasterrcnn_resnet50 import FasterRCNN_ResNet50_FPN
from maskgen.fcn_16_single_channel import FCN
from utils.bbox_utils import center_size
from utils.loss_utils import focal_loss as get_loss
from utils.mask_utils import *
from utils.results_utils import read_ground_truth, read_results
from utils.video_utils import get_qp_from_name, read_videos, write_video
from utils.visualize_utils import visualize_heat_by_summarywriter
sns.set()
def error_tile(mask):
maxval = mask.max().item()
ret = torch.zeros_like(mask)
for i in range(1, maxval + 1):
val = (mask == i).int()
val = F.conv2d(
val, torch.ones([1, 1, i * 2 + 1, i * 2 + 1]).int(), padding=i
)
ret = ret + val
return (ret > 0.5).int()
def main(args):
gc.enable()
# initialize
logger = logging.getLogger("meas")
# logger.addHandler(logging.FileHandler("blackgen.log"))
torch.set_default_tensor_type(torch.FloatTensor)
# read the video frames (will use the largest video as ground truth)
videos, bws, video_names = read_videos(args.inputs, logger, sort=True)
videos = videos
bws = [0, 1]
qps = [get_qp_from_name(video_name) for video_name in video_names]
# construct applications
app = DNN_Factory().get_model(args.app)
# mask_generator = FCN()
# mask_generator.load(args.path)
# mask_generator.train().cuda()
# construct the mask
mask_shape = [
len(videos[-1]),
1,
720 // args.tile_size,
1280 // args.tile_size,
]
error = torch.zeros(mask_shape).int()
ground_truth_dict = read_results(args.ground_truth, app.name, logger)
# low_quality_dict = read_results(args.base, app.name, logger)
writer = SummaryWriter(f"runs/{args.app}/{args.output}")
logger.info(f"Processing application {app.name}")
progress_bar = enlighten.get_manager().counter(
total=len(videos[-1]), desc=f"{app.name}", unit="frames"
)
for iteration in range(args.num_iterations + 1):
tps = []
logger.info(f"Processing application {app.name}")
progress_bar = enlighten.get_manager().counter(
total=len(videos[-1]), desc=f"{app.name}", unit="frames"
)
for fid, (video_slices, error_slice) in enumerate(
zip(zip(*videos), error.split(1))
):
progress_bar.update()
# construct hybrid image, lq: normalized color, hq: hq image
lq_image, hq_image = video_slices[0], video_slices[1]
mask_slice = error_tile(error_slice).float()
mask_tile = tile_mask(mask_slice, args.tile_size)
# set_trace()
mix_image = lq_image * (1 - mask_tile) + hq_image * mask_tile
# get result
result = app.inference(mix_image.cuda(), detach=True)
delta = (result != ground_truth_dict[fid]).int()[:, None, :, :]
delta = F.conv2d(
delta,
torch.ones([1, 1, args.tile_size, args.tile_size]).int(),
stride=args.tile_size,
)
delta = (delta > 0.5).int()
error_slice[:, :, :, :] = error_slice + delta
tps += [
app.calc_accuracy(
{fid: result}, {fid: ground_truth_dict[fid]}, args
)["acc"]
]
# logger.info("f1: %.3f", tps[-1])
# logger.info(
# "Perc: %.3f", (mask_slice == 1).sum() / (mask_slice >= 0).sum()
# )
logger.info("Average TP: %.3f", torch.tensor(tps).mean().item())
logger.info(
"Average mask: %.3f", error_tile(error).float().mean().item()
)
mask = error_tile(error).float()
write_black_bkgd_video_smoothed_continuous(
mask, args, args.qp, logger, writer=writer, tag="hq"
)
# masked_video = generate_masked_video(mask, videos, bws, args)
# write_video(masked_video, args.output, logger)
if __name__ == "__main__":
# set the format of the logger
coloredlogs.install(
fmt="%(asctime)s [%(levelname)s] %(name)s:%(funcName)s[%(lineno)s] -- %(message)s",
level="INFO",
)
parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
"--inputs",
nargs="+",
help="The video file names. The largest video file will be the ground truth.",
required=True,
)
parser.add_argument(
"-g",
"--ground_truth",
help="The video file names. The largest video file will be the ground truth.",
type=str,
required=True,
)
parser.add_argument(
"-b",
"--base",
help="The video file names. The largest video file will be the ground truth.",
type=str,
required=True,
)
parser.add_argument(
"-s",
"--source",
type=str,
help="The original video source.",
required=True,
)
parser.add_argument(
"--num_iterations",
type=int,
help="Number of iterations needed",
default=1,
)
# parser.add_argument('-g', '--ground_truth', type=str, help='The ground truth results.', required=True)
parser.add_argument(
"-o", "--output", type=str, help="The output name.", required=True
)
parser.add_argument(
"--visualize_step_size", type=int, help="Visualization", default=100,
)
parser.add_argument(
"--confidence_threshold",
type=float,
help="The confidence score threshold for calculating accuracy.",
default=0.7,
)
parser.add_argument(
"--gt_confidence_threshold",
type=float,
help="The confidence score threshold for calculating accuracy.",
default=0.7,
)
parser.add_argument(
"--iou_threshold",
type=float,
help="The IoU threshold for calculating accuracy in object detection.",
default=0.5,
)
parser.add_argument(
"--tile_size", type=int, help="The tile size of the mask.", default=16
)
# parser.add_argument(
# "--conv_size", type=int, help="The tile size of the mask.", default=0
# )
parser.add_argument(
"--visualize",
type=bool,
help="Visualize the mask if True",
default=True,
)
parser.add_argument("--qp", type=int, required=True)
parser.add_argument(
"--app", type=str, help="The name of the model.", required=True,
)
args = parser.parse_args()
main(args)