Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kazukiOmi committed Nov 20, 2023
1 parent ea04508 commit 9cd26b2
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 81 deletions.
2 changes: 1 addition & 1 deletion datasets/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, dataset_name, subset):
video_name_list = f.readlines()

self.dataset_path = params["dataset_path"]
# self.video_name_list = [video.replace("\n", "") for video in video_name_list][300:310]
# self.video_name_list = [video.replace("\n", "") for video in video_name_list][25:40]
self.video_name_list = [video.replace("\n", "") for video in video_name_list]
self.ano = read_ano(dataset_name, subset, params)

Expand Down
2 changes: 1 addition & 1 deletion datasets/projects/jhmdb21.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ label_list:

# Settings #
psn_score_th: 0.95
iou_th: 0.6
iou_th: 0.35
88 changes: 43 additions & 45 deletions eval_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,39 @@
from util.video_map import calc_video_map


def get_args_parser():
parser = argparse.ArgumentParser('Set transformer detector', add_help=False)

# loader
parser.add_argument('--dataset', default='jhmdb21', type=str, choices=['ucf101-24', 'jhmdb21'])
parser.add_argument('--n_frames', default=128, type=int)
parser.add_argument('--subset', default="val", type=str, choices=["train", "val"])

# setting
parser.add_argument('--load_ex_name', default='jhmdb_wd:e4', type=str)
parser.add_argument('--write_ex_name', default='head_test', type=str)
parser.add_argument('--device', default=1, type=int)
parser.add_argument('--load_epoch_encoder', default=15, type=int)
parser.add_argument('--load_epoch_head', default=14, type=int)
parser.add_argument('--psn_score_th', default=0.7, type=float)
parser.add_argument('--sim_th', default=0.5, type=float)
parser.add_argument('--tiou_th', default=0.2, type=float)
parser.add_argument('--iou_th', default=0.4, type=float)

# Backbone
parser.add_argument('--backbone', default='resnet101', type=str, choices=('resnet50', 'resnet101'),
help="Name of the convolutional backbone to use")
parser.add_argument('--dilation', default=True,
help="If true, we replace stride with dilation in the last convolutional block (DC5)")

# others
parser.add_argument('--seed', default=42, type=int)
parser.add_argument('--check_dir', default="checkpoint", type=str)
parser.add_argument('--num_workers', default=8, type=int)

return parser


@torch.no_grad()
def main(args, params):
device = torch.device(f"cuda:{args.device}")
Expand All @@ -46,13 +79,13 @@ def main(args, params):
pretrain_path_head = osp.join(args.check_dir, args.dataset, args.load_ex_name, "head", args.write_ex_name, f"epoch_{args.load_epoch_head}.pth")
action_head.load_state_dict(torch.load(pretrain_path_head))

val_loader = get_video_loader(args.dataset, "val", shuffle=False)
loader = get_video_loader(args.dataset, args.subset, shuffle=False)

pred_tubes = []
video_names = []
total_tubes = 0

pbar_videos = tqdm(enumerate(val_loader), total=len(val_loader), leave=False)
pbar_videos = tqdm(enumerate(loader), total=len(loader), leave=False)
pbar_videos.set_description("[Validation]")
for video_idx, (img_paths, video_ano) in pbar_videos:
video_name = "/".join(img_paths[0].parts[-3: -1])
Expand Down Expand Up @@ -86,7 +119,7 @@ def main(args, params):

tube.filter()

utils.give_label(video_ano, tube.tubes, args.n_classes, args.iou_th)
utils.give_label(video_ano, tube.tubes, params["num_classes"], args.iou_th)

if len(tube.tubes) == 0:
continue
Expand All @@ -102,58 +135,23 @@ def main(args, params):
pred_tubes.append(tube)
total_tubes += len(tube.tubes)
pbar_videos.set_postfix_str(f'total_tubes: {total_tubes}, n_tubes: {len(tube.tubes)}')
# continue
continue
video_path = osp.join(params["dataset_path_video"], video_name + ".avi")
make_video_with_actiontube(video_path, params["label_list"], tube.tubes, video_ano, plot_label=True)
os.remove("test.avi")

pred_tubes = [tube for video_tubes in pred_tubes for tube in video_tubes.tubes]
print(f"num of pred tubes: {len(pred_tubes)}")
pred_tubes = [tube for tube in pred_tubes


if tube[1]["class"] != args.n_classes]
pred_tubes = [tube for tube in pred_tubes if tube[1]["class"] != params["num_classes"]]
print(f"num of pred tubes w/o no action: {len(pred_tubes)}")

gt_tubes = make_gt_tubes(args.dataset, "val", params)
gt_tubes = make_gt_tubes(args.dataset, args.subset, params)
gt_tubes = {name: tube for name, tube in gt_tubes.items() if name in video_names} # for debug with less data from loader

video_ap = calc_video_map(pred_tubes, gt_tubes, args.n_classes, args.tiou_th)
for class_name, ap in zip(params["label_list"], video_ap):
print(f"{class_name}: {ap}")
print(f"v-mAP: {sum(video_ap) / len(video_ap)}")


def get_args_parser():
parser = argparse.ArgumentParser('Set transformer detector', add_help=False)

# loader
parser.add_argument('--dataset', default='jhmdb21', type=str, choices=['ucf101-24', 'jhmdb21'])
parser.add_argument('--n_frames', default=128, type=int)

# setting
parser.add_argument('--load_ex_name', default='jhmdb_wd:e4', type=str)
parser.add_argument('--write_ex_name', default='head_test', type=str)
parser.add_argument('--device', default=1, type=int)
parser.add_argument('--load_epoch_encoder', default=15, type=int)
parser.add_argument('--load_epoch_head', default=14, type=int)
parser.add_argument('--psn_score_th', default=0.7, type=float)
parser.add_argument('--sim_th', default=0.5, type=float)
parser.add_argument('--tiou_th', default=0.2, type=float)
parser.add_argument('--iou_th', default=0.4, type=float)

# Backbone
parser.add_argument('--backbone', default='resnet101', type=str, choices=('resnet50', 'resnet101'),
help="Name of the convolutional backbone to use")
parser.add_argument('--dilation', default=True,
help="If true, we replace stride with dilation in the last convolutional block (DC5)")

# others
parser.add_argument('--seed', default=42, type=int)
parser.add_argument('--check_dir', default="checkpoint", type=str)
parser.add_argument('--num_workers', default=8, type=int)

return parser
video_ap = calc_video_map(pred_tubes, gt_tubes, params["num_classes"], args.tiou_th)
for class_name, ap in zip(params["label_list"][:-1], video_ap):
print(f"{class_name}: {round(ap,4)}")
print(f"v-mAP: {round(sum(video_ap) / len(video_ap),4)}")


if __name__ == "__main__":
Expand Down
30 changes: 14 additions & 16 deletions eval_qmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def get_args_parser():
parser.add_argument('--ex_name', default='jhmdb_wd:e4', type=str)
# loader
parser.add_argument('--n_frames', default=128, type=int)
parser.add_argument('--subset', default="val", type=str, choices=["train", "val"])
# person encoder
parser.add_argument('--load_epoch', default=15, type=int)
parser.add_argument('--psn_score_th', default=0.9, type=float)
Expand Down Expand Up @@ -65,13 +66,10 @@ def main(args, params):
detr.eval()
if args.dataset == "ucf101-24":
pretrain_path = "checkpoint/ucf101-24/detr:headtune/detr/epoch_30.pth"
# pretrain_path = "checkpoint/ucf101-24/test/detr/epoch_50.pth"
detr.load_state_dict(torch.load(pretrain_path))
else:
pretrain_path = "checkpoint/detr/" + utils.get_pretrain_path(args.backbone, args.dilation)
detr.load_state_dict(torch.load(pretrain_path)["model"])
# pretrain_path = "checkpoint/detr/" + utils.get_pretrain_path(args.backbone, args.dilation)
# detr.load_state_dict(torch.load(pretrain_path)["model"])
criterion.to(device)
criterion.eval()

Expand All @@ -83,12 +81,12 @@ def main(args, params):
psn_criterion = NPairLoss().to(device)
psn_criterion.eval()

val_loader = get_video_loader(args.dataset, "val")
loader = get_video_loader(args.dataset, args.subset)

pred_tubes = []
video_names = []

pbar_videos = tqdm(enumerate(val_loader), total=len(val_loader), leave=False)
pbar_videos = tqdm(enumerate(loader), total=len(loader), leave=False)
for video_idx, (img_paths, video_ano) in pbar_videos:
video_name = "/".join(img_paths[0].parts[-3: -1])
video_names.append(video_name)
Expand Down Expand Up @@ -129,11 +127,11 @@ def main(args, params):
make_video_with_tube(video_path, params["label_list"], tube.tubes, video_ano=video_ano, plot_label=True)
os.remove("test.avi")

dir = osp.join(args.check_dir, args.dataset, args.ex_name, "qmm_tubes")
dir = osp.join(args.check_dir, args.dataset, args.ex_name, "qmm_tubes", args.subset)
filename = f"epoch:{args.load_epoch}_pth:{args.psn_score_th}_simth:{args.sim_th}"
utils.write_tar(pred_tubes, dir, filename)

gt_tubes = make_gt_tubes(args.dataset, "val", params)
gt_tubes = make_gt_tubes(args.dataset, args.subset, params)
video_names = [tubes.video_name for tubes in pred_tubes]
calc_precision_recall(pred_tubes, gt_tubes, video_names, args.tiou_th)

Expand Down Expand Up @@ -184,8 +182,8 @@ def calc_precision_recall(pred_tubes, gt_tubes, video_names=None, tiou_th=0.5):
print(f"n_pred_tubes: {len(pred_tubes)}, n_pred_tubes (filter): {n_pred}")
print(f"True Positive: {tp}")
print("-------")
print(f"Precision: {tp/len(pred_tubes)}, Precision (filter): {tp/n_pred}")
print(f"Recall: {tp/n_gt}")
print(f"Precision: {round(tp/len(pred_tubes),3)}, Precision (filter): {round(tp/n_pred,3)}")
print(f"Recall: {round(tp/n_gt,3)}")


if __name__ == '__main__':
Expand All @@ -198,15 +196,15 @@ def calc_precision_recall(pred_tubes, gt_tubes, video_names=None, tiou_th=0.5):
args.psn_score_th = params["psn_score_th"]
args.iou_th = params["iou_th"]

main(args, params)
# main(args, params)

# load qmm outputs and calc precision and recall #
# dir = osp.join(args.check_dir, args.dataset, args.ex_name, "qmm_tubes")
# filename = f"epoch:{args.load_epoch}_pth:{args.psn_score_th}_simth:{args.sim_th}"
# pred_tubes = utils.read_tar(dir, filename)
# gt_tubes = make_gt_tubes(args.dataset, "val", params)
# video_names = [tubes.video_name for tubes in pred_tubes]
# calc_precision_recall(pred_tubes, gt_tubes, video_names, args.tiou_th)
dir = osp.join(args.check_dir, args.dataset, args.ex_name, "qmm_tubes", args.subset)
filename = f"epoch:{args.load_epoch}_pth:{args.psn_score_th}_simth:{args.sim_th}"
pred_tubes = [obj for obj in utils.TarIterator(dir, filename)]
gt_tubes = make_gt_tubes(args.dataset, args.subset, params)
video_names = [tubes.video_name for tubes in pred_tubes]
calc_precision_recall(pred_tubes, gt_tubes, video_names, args.tiou_th)

# visualization #
# for tube in pred_tubes:
Expand Down
2 changes: 1 addition & 1 deletion train_action_head.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def main(args, params):

tube.filter()

utils.give_label(video_ano, tube.tubes, args.n_classes, args.iou_th)
utils.give_label(video_ano, tube.tubes, params["num_classes"], args.iou_th)

if len(tube.tubes) == 0:
continue
Expand Down
45 changes: 28 additions & 17 deletions util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import copy
import tarfile
import pickle
import random

import util.misc as utils
from util.box_ops import generalized_box_iou
Expand All @@ -37,29 +38,41 @@ def write_tar(object: list, dir: str, file_name: str = "test"):
file_path = osp.join(dir, file_name)

with tarfile.open(file_path + '.tar', 'w') as tar:
for i, element in enumerate(object):
pbar = tqdm(enumerate(object))
pbar.set_description("[Write tar file]")
for i, element in pbar:
with open(f'element{i}.pkl', 'wb') as f:
pickle.dump(element, f)
tar.add(f'element{i}.pkl')
os.remove(f'element{i}.pkl')

for i in range(len(object)):
os.remove(f'element{i}.pkl')
# for i in range(len(object)):
# os.remove(f'element{i}.pkl')


def read_tar(dir: str, file_name: str):
file_path = osp.join(dir, file_name)
class TarIterator:
def __init__(self, dir, file_name):
self.file_path = osp.join(dir, file_name) + ".tar"
self.tar = tarfile.open(self.file_path, "r")
self.members = self.tar.getmembers()
self.index = 0

def __iter__(self):
self.index = 0
random.shuffle(self.members)
return self

object = []
with tarfile.open(file_path + '.tar', 'r') as tar:
pbar = tqdm(tar.getmembers())
pbar.set_description("[Read tar file]")
for member in pbar:
f = tar.extractfile(member)
if f is not None:
element = pickle.load(f)
object.append(element)
def __len__(self):
return len(self.members)

return object
def __next__(self):
if self.index >= len(self.members):
raise StopIteration
member = self.members[self.index]
f = self.tar.extractfile(member)
element = pickle.load(f)
self.index += 1
return element


def get_pretrain_path(model_name, dilation):
Expand Down Expand Up @@ -117,13 +130,11 @@ def give_label(video_ano, tubes, no_action_id=-1, iou_th=0.4):
tube["action_label"].append(gt_ano[max_idx][4])
else:
tube["action_label"].append(no_action_id)
continue
else:
tube["action_label"].append(no_action_id)


def give_pred(tube, outputs):
# tube["action_pred"] = outputs.cpu().detach()
tube["action_pred"] = outputs.softmax(dim=1).cpu().detach()
tube["action_score"] = tube["action_pred"].topk(1, 1)[0].reshape(-1)
tube["action_id"] = tube["action_pred"].topk(1, 1)[1].reshape(-1)
Expand Down

0 comments on commit 9cd26b2

Please sign in to comment.