-
Notifications
You must be signed in to change notification settings - Fork 6
/
compress_object_segmentation.py
217 lines (181 loc) · 6.32 KB
/
compress_object_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
"""
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 utilities.bbox_utils import center_size
from utilities.compressor import h264_roi_compressor_segment
from utilities.loss_utils import focal_loss as get_loss
from utilities.mask_utils import *
from utilities.results_utils import read_ground_truth, read_results
from utilities.timer import Timer
from utilities.video_utils import get_qp_from_name, read_videos, write_video
from utilities.visualize_utils import visualize_heat_by_summarywriter
sns.set()
def main(args):
gc.enable()
# initialize
logger = logging.getLogger("object_seg")
# 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,
]
mask = torch.ones(mask_shape).float()
ground_truth_dict = read_results(args.ground_truth, app.name, logger)
writer = SummaryWriter(f"runs/{args.app}/{args.output}")
# logger.info('Reading ground truth mask')
# with open(args.mask + '.mask', 'rb') as f:
# ground_truth_mask = pickle.load(f)
# ground_truth_mask = ground_truth_mask[sorted(ground_truth_mask.keys())[1]]
# ground_truth_mask = ground_truth_mask.split(1)
plt.clf()
plt.figure(figsize=(16, 10))
# binarized_mask = mask.clone().detach()
# binarize_mask(binarized_mask, bws)
# if iteration > 3 * (args.num_iterations // 4):
# (args.binarize_weight * torch.tensor(iteration*1.0) * (binarized_mask - mask).abs().pow(2).mean()).backward()
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, mask_slice) in enumerate(
zip(zip(*videos), mask.split(1))
):
progress_bar.update()
# lq_image = T.ToTensor()(Image.open('youtube_videos/train_pngs_qp_34/%05d.png' % (fid+offset2)))[None, :, :, :]
gt = (ground_truth_dict[fid] > 0).float()[:, None, :, :]
gt = F.conv2d(
gt,
torch.ones(1, 1, args.tile_size, args.tile_size),
stride=args.tile_size,
)
gt = (gt > 0.5).float()
mask[fid : fid + 1, :, :, :] = gt
if fid % args.visualize_step_size == 0:
image = T.ToPILImage()(video_slices[-1][0, :, :, :])
writer.add_image("raw_frame", video_slices[-1][0, :, :, :], fid)
visualize_heat_by_summarywriter(
image,
mask_slice.cpu().detach().float(),
"inferred_saliency",
writer,
fid,
args,
)
mask = dilate_binarize(mask, 0.5, args.conv_size, cuda=False)
mask = (mask > 0.5).int()
mask = torch.where(
mask == 1,
args.hq * torch.ones_like(mask),
args.lq * torch.ones_like(mask),
)
h264_roi_compressor_segment(mask, args, logger)
# 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(
"-s",
"--source",
type=str,
help="The original video source.",
required=True,
)
# 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(
"--conv_size", type=int, help="Visualization", required=True,
)
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(
"--visualize",
type=bool,
help="Visualize the mask if True",
default=True,
)
parser.add_argument("--hq", type=int, required=True)
parser.add_argument("--lq", type=int, required=True)
parser.add_argument(
"--app", type=str, help="The name of the model.", required=True,
)
parser.add_argument(
"--smooth_frames",
type=int,
help="Proposing one single mask for smooth_frames many frames",
default=10,
)
args = parser.parse_args()
main(args)