Linting
This commit is contained in:
parent
072c89dd06
commit
dd97f10bb8
@ -112,9 +112,6 @@ def is_turning(kp):
|
||||
is_l_up = kp[y][l_hand] < kp[y][l_shoulder]
|
||||
is_r_up = kp[y][r_hand] < kp[y][r_shoulder]
|
||||
|
||||
is_l_down = kp[y][l_hand] > kp[y][l_elbow]
|
||||
is_r_down = kp[y][r_hand] > kp[y][r_elbow]
|
||||
|
||||
is_left_risen = is_l_up and l_angle >= 30 and not l_too_close
|
||||
is_right_risen = is_r_up and r_angle >= 30 and not r_too_close
|
||||
|
||||
@ -320,10 +317,6 @@ def show_activities(args, image_t, output_path, annotations, dic_out):
|
||||
enumerate(dic_out['uv_shoulders'])]
|
||||
keypoint_painter = KeypointPainter(show_box=False)
|
||||
|
||||
r_h = 'none'
|
||||
if 'raise_hand' in args.activities:
|
||||
r_h = dic_out['raising_hand']
|
||||
|
||||
with image_canvas(image_t,
|
||||
output_path + '.front.png',
|
||||
show=args.show,
|
||||
|
||||
@ -27,7 +27,8 @@ class Loco:
|
||||
LINEAR_SIZE_MONO = 256
|
||||
N_SAMPLES = 100
|
||||
|
||||
def __init__(self, model, mode, net=None, device=None, n_dropout=0, p_dropout=0.2, linear_size=1024, casr='nonstd', casr_model=None):
|
||||
def __init__(self, model, mode, net=None, device=None, n_dropout=0,
|
||||
p_dropout=0.2, linear_size=1024, casr='nonstd', casr_model=None):
|
||||
|
||||
# Select networks
|
||||
assert mode in ('mono', 'stereo'), "mode not recognized"
|
||||
@ -92,7 +93,8 @@ class Loco:
|
||||
linear_size=linear_size, device=self.device)
|
||||
|
||||
self.model.load_state_dict(torch.load(model_path, map_location=lambda storage, loc: storage))
|
||||
self.turning_model.load_state_dict(torch.load(turning_model_path, map_location=lambda storage, loc: storage))
|
||||
self.turning_model.load_state_dict(torch.load(turning_model_path,
|
||||
map_location=lambda storage, loc: storage))
|
||||
else:
|
||||
self.model = model
|
||||
self.model.eval() # Default is train
|
||||
@ -316,7 +318,7 @@ class Loco:
|
||||
inputs = preprocess_monoloco(keypoints, kk, zero_center=False)
|
||||
outputs = self.turning_model(inputs)
|
||||
# bi = unnormalize_bi(outputs)
|
||||
dic = {'turning': [o for o in torch.argmax(outputs, axis=len(outputs.shape)-1).tolist()]}
|
||||
dic = {'turning': torch.argmax(outputs, axis=len(outputs.shape)-1).tolist()}
|
||||
# dic = {key: el.detach().cpu() for key, el in dic.items()}
|
||||
dic_out['turning'] = dic['turning']
|
||||
|
||||
|
||||
@ -1,36 +1,32 @@
|
||||
import pickle
|
||||
import re
|
||||
import numpy as np
|
||||
import json
|
||||
import os
|
||||
import glob
|
||||
import datetime
|
||||
from collections import defaultdict
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
from .. import __version__
|
||||
from .transforms import flip_inputs, flip_labels, height_augmentation
|
||||
from ..network.process import preprocess_monoloco
|
||||
|
||||
gt_path = '/scratch/izar/beauvill/casr/data/annotations/casr_annotation.pickle'
|
||||
res_path = '/scratch/izar/beauvill/casr/res_extended/casr*'
|
||||
|
||||
def bb_intersection_over_union(boxA, boxB):
|
||||
xA = max(boxA[0], boxB[0])
|
||||
yA = max(boxA[1], boxB[1])
|
||||
xB = min(boxA[2], boxB[2])
|
||||
yB = min(boxA[3], boxB[3])
|
||||
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
|
||||
boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
|
||||
boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
|
||||
iou = interArea / float(boxAArea + boxBArea - interArea)
|
||||
return iou
|
||||
xA = max(boxA[0], boxB[0])
|
||||
yA = max(boxA[1], boxB[1])
|
||||
xB = min(boxA[2], boxB[2])
|
||||
yB = min(boxA[3], boxB[3])
|
||||
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
|
||||
boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
|
||||
boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
|
||||
iou = interArea / float(boxAArea + boxBArea - interArea)
|
||||
return iou
|
||||
|
||||
def match_bboxes(bbox_gt, bbox_pred, IOU_THRESH=1):
|
||||
def match_bboxes(bbox_gt, bbox_pred):
|
||||
n_true = bbox_gt.shape[0]
|
||||
n_pred = bbox_pred.shape[0]
|
||||
MAX_DIST = 1.0
|
||||
MIN_IOU = 0.0
|
||||
|
||||
iou_matrix = np.zeros((n_true, n_pred))
|
||||
for i in range(n_true):
|
||||
@ -40,60 +36,58 @@ def match_bboxes(bbox_gt, bbox_pred, IOU_THRESH=1):
|
||||
return np.argmax(iou_matrix)
|
||||
|
||||
def standard_bbox(bbox):
|
||||
return [bbox[0], bbox[1], bbox[0]+bbox[2], bbox[1]+bbox[3]]
|
||||
return [bbox[0], bbox[1], bbox[0]+bbox[2], bbox[1]+bbox[3]]
|
||||
|
||||
def load_gt():
|
||||
return pickle.load(open(gt_path, 'rb'), encoding='latin1')
|
||||
return pickle.load(open(gt_path, 'rb'), encoding='latin1')
|
||||
|
||||
def load_res():
|
||||
mono = []
|
||||
for dir in sorted(glob.glob(res_path), key=lambda x:float(re.findall("(\d+)",x)[0])):
|
||||
data_list = []
|
||||
for file in sorted(os.listdir(dir), key=lambda x:float(re.findall("(\d+)",x)[0])):
|
||||
if 'json' in file:
|
||||
json_path = os.path.join(dir, file)
|
||||
json_data = json.load(open(json_path))
|
||||
json_data['filename'] = json_path
|
||||
data_list.append(json_data)
|
||||
mono.append(data_list)
|
||||
return mono
|
||||
mono = []
|
||||
for folder in sorted(glob.glob(res_path), key=lambda x:float(re.findall(r"(\d+)",x)[0])):
|
||||
data_list = []
|
||||
for file in sorted(os.listdir(folder), key=lambda x:float(re.findall(r"(\d+)",x)[0])):
|
||||
if 'json' in file:
|
||||
json_path = os.path.join(folder, file)
|
||||
json_data = json.load(open(json_path))
|
||||
json_data['filename'] = json_path
|
||||
data_list.append(json_data)
|
||||
mono.append(data_list)
|
||||
return mono
|
||||
|
||||
def create_dic_std(gt=load_gt(), res=load_res()):
|
||||
dic_jo = {
|
||||
'train': dict(X=[], Y=[], names=[], kps=[]),
|
||||
'val': dict(X=[], Y=[], names=[], kps=[]),
|
||||
'version': __version__,
|
||||
}
|
||||
wrong = [6, 8, 9, 10, 11, 12, 14, 21, 40, 43, 55, 70, 76, 92, 109, 110, 112, 113, 121, 123, 124, 127, 128, 134, 136, 139, 165, 173]
|
||||
for i in range(len(res[:])):
|
||||
if(not(i in wrong)):
|
||||
for j in range(len(res[i][:])):
|
||||
phase = 'val'
|
||||
if (j % 10) > 1:
|
||||
phase = 'train'
|
||||
dic_jo = {
|
||||
'train': dict(X=[], Y=[], names=[], kps=[]),
|
||||
'val': dict(X=[], Y=[], names=[], kps=[]),
|
||||
'version': __version__,
|
||||
}
|
||||
wrong = [6, 8, 9, 10, 11, 12, 14, 21, 40, 43, 55, 70, 76, 92, 109,
|
||||
110, 112, 113, 121, 123, 124, 127, 128, 134, 136, 139, 165, 173]
|
||||
for i in [x for x in range(len(res[:])) if x not in wrong]:
|
||||
for j in range(len(res[i][:])):
|
||||
phase = 'val'
|
||||
if (j % 10) > 1:
|
||||
phase = 'train'
|
||||
|
||||
folder = gt[i][j]['video_folder']
|
||||
folder = gt[i][j]['video_folder']
|
||||
|
||||
if('boxes' in res[i][j] and not(gt[i][j]['left_or_right'] == 2)):
|
||||
gt_box = gt[i][j]['bbox_gt']
|
||||
if('boxes' in res[i][j] and gt[i][j]['left_or_right'] != 2):
|
||||
gt_box = gt[i][j]['bbox_gt']
|
||||
|
||||
good_idx = match_bboxes(np.array([standard_bbox(gt_box)]), np.array(res[i][j]['boxes'])[:,:4])
|
||||
good_idx = match_bboxes(np.array([standard_bbox(gt_box)]), np.array(res[i][j]['boxes'])[:,:4])
|
||||
|
||||
keypoints = [res[i][j]['uv_kps'][good_idx]]
|
||||
keypoints = [res[i][j]['uv_kps'][good_idx]]
|
||||
|
||||
gt_turn = gt[i][j]['left_or_right']
|
||||
if gt_turn == 3:
|
||||
gt_turn = 2
|
||||
gt_turn = gt[i][j]['left_or_right']
|
||||
if gt_turn == 3:
|
||||
gt_turn = 2
|
||||
|
||||
inp = preprocess_monoloco(keypoints, torch.eye(3)).view(-1).tolist()
|
||||
dic_jo[phase]['kps'].append(keypoints)
|
||||
dic_jo[phase]['X'].append(inp)
|
||||
dic_jo[phase]['Y'].append(gt_turn)
|
||||
dic_jo[phase]['names'].append(folder+"_frame{}".format(j))
|
||||
inp = preprocess_monoloco(keypoints, torch.eye(3)).view(-1).tolist()
|
||||
dic_jo[phase]['kps'].append(keypoints)
|
||||
dic_jo[phase]['X'].append(inp)
|
||||
dic_jo[phase]['Y'].append(gt_turn)
|
||||
dic_jo[phase]['names'].append(folder+"_frame{}".format(j))
|
||||
|
||||
now_time = datetime.datetime.now().strftime("%Y%m%d-%H%M")[2:]
|
||||
with open("/home/beauvill/joints-casr-std-" + now_time + ".json", 'w') as file:
|
||||
json.dump(dic_jo, file)
|
||||
return dic_jo
|
||||
|
||||
create_dic_std()
|
||||
now_time = datetime.datetime.now().strftime("%Y%m%d-%H%M")[2:]
|
||||
with open("/home/beauvill/joints-casr-std-" + now_time + ".json", 'w') as file:
|
||||
json.dump(dic_jo, file)
|
||||
return dic_jo
|
||||
@ -50,7 +50,8 @@ def cli():
|
||||
visualizer.cli(parser)
|
||||
|
||||
# Monoloco
|
||||
predict_parser.add_argument('--activities', nargs='+', choices=['raise_hand', 'social_distance', 'using_phone', 'is_turning'],
|
||||
predict_parser.add_argument('--activities', nargs='+',
|
||||
choices=['raise_hand', 'social_distance', 'using_phone', 'is_turning'],
|
||||
help='Choose activities to show: social_distance, raise_hand', default=[])
|
||||
predict_parser.add_argument('--mode', help='keypoints, mono, stereo', default='mono')
|
||||
predict_parser.add_argument('--model', help='path of MonoLoco/MonStereo model to load')
|
||||
|
||||
@ -63,7 +63,10 @@ class KeypointsDataset(Dataset):
|
||||
self.version = dic_jo['version']
|
||||
|
||||
# Extract annotations divided in clusters
|
||||
# self.dic_clst = dic_jo[phase]['clst']
|
||||
if 'clst' in dic_jo[phase]:
|
||||
self.dic_clst = dic_jo[phase]['clst']
|
||||
else:
|
||||
self.dic_clst = None
|
||||
|
||||
def __len__(self):
|
||||
"""
|
||||
|
||||
@ -98,7 +98,6 @@ class HypTuningCasr:
|
||||
dic_best['random_seed'] = self.r_seed
|
||||
# dic_best['acc_test'] = dic_err['test']['all']['mean']
|
||||
|
||||
dic_err_best = dic_err
|
||||
best_acc_val = acc_val
|
||||
model_best = model
|
||||
|
||||
|
||||
@ -18,7 +18,6 @@ from itertools import chain
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from torch.utils.data import DataLoader
|
||||
from torch.optim import lr_scheduler
|
||||
|
||||
@ -215,7 +214,7 @@ class CASRTrainer:
|
||||
sys.exit()
|
||||
|
||||
# Forward pass
|
||||
outputs = self.model(inputs)
|
||||
#outputs = self.model(inputs)
|
||||
#self.compute_stats(outputs, labels, dic_err['val'], size_eval, clst='all')
|
||||
|
||||
# self.cout_stats(dic_err['val'], size_eval, clst='all')
|
||||
|
||||
@ -18,7 +18,6 @@ from itertools import chain
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from torch.utils.data import DataLoader
|
||||
from torch.optim import lr_scheduler
|
||||
|
||||
@ -215,7 +214,7 @@ class CASRTrainerStandard:
|
||||
sys.exit()
|
||||
|
||||
# Forward pass
|
||||
outputs = self.model(inputs)
|
||||
# outputs = self.model(inputs)
|
||||
#self.compute_stats(outputs, labels, dic_err['val'], size_eval, clst='all')
|
||||
|
||||
# self.cout_stats(dic_err['val'], size_eval, clst='all')
|
||||
|
||||
Loading…
Reference in New Issue
Block a user