constanceCM's picture
upload
8aa674c
import os
import pickle
import torch
import torch.nn.functional as F
def boundary_choose(score):
mask_high = score > score.max(dim=1, keepdim=True)[0] * 0.5
mask_peak = score == F.max_pool1d(score, kernel_size=3, stride=1, padding=1)
mask = mask_peak | mask_high
return mask
def save_predictions(predictions, metas, folder):
for idx in range(len(metas)):
video_name = metas[idx]["video_name"]
file_path = os.path.join(folder, f"{video_name}.pkl")
prediction = [data[idx] for data in predictions]
with open(file_path, "wb") as outfile:
pickle.dump(prediction, outfile, pickle.HIGHEST_PROTOCOL)
def load_single_prediction(metas, folder):
"""Should not be used for sliding window. Since we saved the files with video name, and sliding window will have multiple files with the same name."""
predictions = []
for idx in range(len(metas)):
video_name = metas[idx]["video_name"]
file_path = os.path.join(folder, f"{video_name}.pkl")
with open(file_path, "rb") as infile:
prediction = pickle.load(infile)
predictions.append(prediction)
batched_predictions = []
for i in range(len(predictions[0])):
data = torch.stack([prediction[i] for prediction in predictions])
batched_predictions.append(data)
return batched_predictions
def load_predictions(metas, infer_cfg):
if "fuse_list" in infer_cfg.keys():
predictions = []
predictions_list = [load_single_prediction(metas, folder) for folder in infer_cfg.fuse_list]
for i in range(len(predictions_list[0])):
predictions.append(torch.stack([pred[i] for pred in predictions_list]).mean(dim=0))
return predictions
else:
return load_single_prediction(metas, infer_cfg.folder)
def convert_to_seconds(segments, meta):
if meta["fps"] == -1: # resize setting, like in anet / hacs
segments = segments / meta["resize_length"] * meta["duration"]
else: # sliding window / padding setting, like in thumos / ego4d
snippet_stride = meta["snippet_stride"]
offset_frames = meta["offset_frames"]
window_start_frame = meta["window_start_frame"] if "window_start_frame" in meta.keys() else 0
segments = (segments * snippet_stride + window_start_frame + offset_frames) / meta["fps"]
# truncate all boundaries within [0, duration]
if segments.shape[0] > 0:
segments[segments <= 0.0] *= 0.0
segments[segments >= meta["duration"]] = segments[segments >= meta["duration"]] * 0.0 + meta["duration"]
return segments