This commit is contained in:
Charles Joseph Pierre Beauville 2021-06-27 23:22:13 +02:00
parent 072c89dd06
commit dd97f10bb8
10 changed files with 78 additions and 88 deletions

View File

@ -112,9 +112,6 @@ def is_turning(kp):
is_l_up = kp[y][l_hand] < kp[y][l_shoulder] is_l_up = kp[y][l_hand] < kp[y][l_shoulder]
is_r_up = kp[y][r_hand] < kp[y][r_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_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 is_right_risen = is_r_up and r_angle >= 30 and not r_too_close
@ -128,7 +125,7 @@ def is_turning(kp):
return 'right' return 'right'
if is_left_down or is_right_down: if is_left_down or is_right_down:
return 'stop' return 'stop'
return None return None
@ -183,7 +180,7 @@ def is_phoning(kp):
print("Left hand y = ", kp[y][l_hand]) print("Left hand y = ", kp[y][l_hand])
print("Is left hand up : ", is_l_up) print("Is left hand up : ", is_l_up)
print("Right hand x = ", kp[x][r_hand]) print("Right hand x = ", kp[x][r_hand])
print("Right hand y = ", kp[y][r_hand]) print("Right hand y = ", kp[y][r_hand])
@ -320,10 +317,6 @@ def show_activities(args, image_t, output_path, annotations, dic_out):
enumerate(dic_out['uv_shoulders'])] enumerate(dic_out['uv_shoulders'])]
keypoint_painter = KeypointPainter(show_box=False) 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, with image_canvas(image_t,
output_path + '.front.png', output_path + '.front.png',
show=args.show, show=args.show,

View File

@ -27,7 +27,8 @@ class Loco:
LINEAR_SIZE_MONO = 256 LINEAR_SIZE_MONO = 256
N_SAMPLES = 100 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 # Select networks
assert mode in ('mono', 'stereo'), "mode not recognized" assert mode in ('mono', 'stereo'), "mode not recognized"
@ -62,12 +63,12 @@ class Loco:
turning_output_size = 3 turning_output_size = 3
turning_model_path = "/home/beauvill/Repos/monoloco/data/outputs/casr_standard-210613-0005.pkl" turning_model_path = "/home/beauvill/Repos/monoloco/data/outputs/casr_standard-210613-0005.pkl"
else: else:
turning_output_size = 4 turning_output_size = 4
if casr_model: if casr_model:
turning_model_path = casr_model turning_model_path = casr_model
else: else:
turning_model_path = "/home/beauvill/Repos/monoloco/data/outputs/casr-210615-1128.pkl" turning_model_path = "/home/beauvill/Repos/monoloco/data/outputs/casr-210615-1128.pkl"
print('-'*10 + 'Output size :' + str(turning_output_size) + '-'*10) print('-'*10 + 'Output size :' + str(turning_output_size) + '-'*10)
if not device: if not device:
@ -92,7 +93,8 @@ class Loco:
linear_size=linear_size, device=self.device) linear_size=linear_size, device=self.device)
self.model.load_state_dict(torch.load(model_path, map_location=lambda storage, loc: storage)) 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: else:
self.model = model self.model = model
self.model.eval() # Default is train self.model.eval() # Default is train
@ -294,7 +296,7 @@ class Loco:
@staticmethod @staticmethod
def using_phone(dic_out, keypoints): def using_phone(dic_out, keypoints):
dic_out['using_phone'] = [is_phoning(keypoint) for keypoint in keypoints] dic_out['using_phone'] = [is_phoning(keypoint) for keypoint in keypoints]
return dic_out return dic_out
@staticmethod @staticmethod
def turning(dic_out, keypoints): def turning(dic_out, keypoints):
@ -316,7 +318,7 @@ class Loco:
inputs = preprocess_monoloco(keypoints, kk, zero_center=False) inputs = preprocess_monoloco(keypoints, kk, zero_center=False)
outputs = self.turning_model(inputs) outputs = self.turning_model(inputs)
# bi = unnormalize_bi(outputs) # 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 = {key: el.detach().cpu() for key, el in dic.items()}
dic_out['turning'] = dic['turning'] dic_out['turning'] = dic['turning']

View File

@ -234,7 +234,7 @@ def predict(args):
if 'raise_hand' in args.activities: if 'raise_hand' in args.activities:
dic_out = net.raising_hand(dic_out, keypoints) dic_out = net.raising_hand(dic_out, keypoints)
if 'using_phone' in args.activities: if 'using_phone' in args.activities:
dic_out = net.using_phone(dic_out, keypoints) dic_out = net.using_phone(dic_out, keypoints)
if 'is_turning' in args.activities: if 'is_turning' in args.activities:
dic_out = net.turning_forward(dic_out, keypoints) dic_out = net.turning_forward(dic_out, keypoints)
else: else:

View File

@ -1,36 +1,32 @@
import pickle import pickle
import re import re
import numpy as np
import json import json
import os import os
import glob import glob
import datetime import datetime
from collections import defaultdict import numpy as np
import torch import torch
from .. import __version__ from .. import __version__
from .transforms import flip_inputs, flip_labels, height_augmentation
from ..network.process import preprocess_monoloco from ..network.process import preprocess_monoloco
gt_path = '/scratch/izar/beauvill/casr/data/annotations/casr_annotation.pickle' gt_path = '/scratch/izar/beauvill/casr/data/annotations/casr_annotation.pickle'
res_path = '/scratch/izar/beauvill/casr/res_extended/casr*' res_path = '/scratch/izar/beauvill/casr/res_extended/casr*'
def bb_intersection_over_union(boxA, boxB): def bb_intersection_over_union(boxA, boxB):
xA = max(boxA[0], boxB[0]) xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1]) yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2]) xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3]) yB = min(boxA[3], boxB[3])
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1) interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1) boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1) boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
iou = interArea / float(boxAArea + boxBArea - interArea) iou = interArea / float(boxAArea + boxBArea - interArea)
return iou 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_true = bbox_gt.shape[0]
n_pred = bbox_pred.shape[0] n_pred = bbox_pred.shape[0]
MAX_DIST = 1.0
MIN_IOU = 0.0
iou_matrix = np.zeros((n_true, n_pred)) iou_matrix = np.zeros((n_true, n_pred))
for i in range(n_true): 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) return np.argmax(iou_matrix)
def standard_bbox(bbox): 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(): def load_gt():
return pickle.load(open(gt_path, 'rb'), encoding='latin1') return pickle.load(open(gt_path, 'rb'), encoding='latin1')
def load_res(): def load_res():
mono = [] mono = []
for dir in sorted(glob.glob(res_path), key=lambda x:float(re.findall("(\d+)",x)[0])): for folder in sorted(glob.glob(res_path), key=lambda x:float(re.findall(r"(\d+)",x)[0])):
data_list = [] data_list = []
for file in sorted(os.listdir(dir), key=lambda x:float(re.findall("(\d+)",x)[0])): for file in sorted(os.listdir(folder), key=lambda x:float(re.findall(r"(\d+)",x)[0])):
if 'json' in file: if 'json' in file:
json_path = os.path.join(dir, file) json_path = os.path.join(folder, file)
json_data = json.load(open(json_path)) json_data = json.load(open(json_path))
json_data['filename'] = json_path json_data['filename'] = json_path
data_list.append(json_data) data_list.append(json_data)
mono.append(data_list) mono.append(data_list)
return mono return mono
def create_dic_std(gt=load_gt(), res=load_res()): def create_dic_std(gt=load_gt(), res=load_res()):
dic_jo = { dic_jo = {
'train': dict(X=[], Y=[], names=[], kps=[]), 'train': dict(X=[], Y=[], names=[], kps=[]),
'val': dict(X=[], Y=[], names=[], kps=[]), 'val': dict(X=[], Y=[], names=[], kps=[]),
'version': __version__, '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] wrong = [6, 8, 9, 10, 11, 12, 14, 21, 40, 43, 55, 70, 76, 92, 109,
for i in range(len(res[:])): 110, 112, 113, 121, 123, 124, 127, 128, 134, 136, 139, 165, 173]
if(not(i in wrong)): for i in [x for x in range(len(res[:])) if x not in wrong]:
for j in range(len(res[i][:])): for j in range(len(res[i][:])):
phase = 'val' phase = 'val'
if (j % 10) > 1: if (j % 10) > 1:
phase = 'train' 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)): if('boxes' in res[i][j] and gt[i][j]['left_or_right'] != 2):
gt_box = gt[i][j]['bbox_gt'] 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'] gt_turn = gt[i][j]['left_or_right']
if gt_turn == 3: if gt_turn == 3:
gt_turn = 2 gt_turn = 2
inp = preprocess_monoloco(keypoints, torch.eye(3)).view(-1).tolist() inp = preprocess_monoloco(keypoints, torch.eye(3)).view(-1).tolist()
dic_jo[phase]['kps'].append(keypoints) dic_jo[phase]['kps'].append(keypoints)
dic_jo[phase]['X'].append(inp) dic_jo[phase]['X'].append(inp)
dic_jo[phase]['Y'].append(gt_turn) dic_jo[phase]['Y'].append(gt_turn)
dic_jo[phase]['names'].append(folder+"_frame{}".format(j)) dic_jo[phase]['names'].append(folder+"_frame{}".format(j))
now_time = datetime.datetime.now().strftime("%Y%m%d-%H%M")[2:] 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: with open("/home/beauvill/joints-casr-std-" + now_time + ".json", 'w') as file:
json.dump(dic_jo, file) json.dump(dic_jo, file)
return dic_jo return dic_jo
create_dic_std()

View File

@ -50,7 +50,8 @@ def cli():
visualizer.cli(parser) visualizer.cli(parser)
# Monoloco # 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=[]) 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('--mode', help='keypoints, mono, stereo', default='mono')
predict_parser.add_argument('--model', help='path of MonoLoco/MonStereo model to load') predict_parser.add_argument('--model', help='path of MonoLoco/MonStereo model to load')

View File

@ -63,7 +63,10 @@ class KeypointsDataset(Dataset):
self.version = dic_jo['version'] self.version = dic_jo['version']
# Extract annotations divided in clusters # 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): def __len__(self):
""" """

View File

@ -98,7 +98,6 @@ class HypTuningCasr:
dic_best['random_seed'] = self.r_seed dic_best['random_seed'] = self.r_seed
# dic_best['acc_test'] = dic_err['test']['all']['mean'] # dic_best['acc_test'] = dic_err['test']['all']['mean']
dic_err_best = dic_err
best_acc_val = acc_val best_acc_val = acc_val
model_best = model model_best = model

View File

@ -88,7 +88,7 @@ class CompositeLoss(torch.nn.Module):
self.tasks = tasks self.tasks = tasks
self.multi_loss_tr = {task: (LaplacianLoss() if task == 'd' self.multi_loss_tr = {task: (LaplacianLoss() if task == 'd'
else (nn.BCEWithLogitsLoss() if task in ('aux', ) else (nn.BCEWithLogitsLoss() if task in ('aux', )
else (nn.CrossEntropyLoss() if task == 'cyclist' else (nn.CrossEntropyLoss() if task == 'cyclist'
else nn.L1Loss()))) for task in tasks} else nn.L1Loss()))) for task in tasks}
self.multi_loss_val = {} self.multi_loss_val = {}

View File

@ -18,7 +18,6 @@ from itertools import chain
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import torch import torch
import torch.nn as nn
from torch.utils.data import DataLoader from torch.utils.data import DataLoader
from torch.optim import lr_scheduler from torch.optim import lr_scheduler
@ -38,8 +37,8 @@ class CASRTrainer:
val_task = 'cyclist' val_task = 'cyclist'
lambdas = (1,) lambdas = (1,)
#clusters = ['10', '20', '30', '40'] #clusters = ['10', '20', '30', '40']
input_size = 34 input_size = 34
output_size = 4 output_size = 4
dir_figures = os.path.join('figures', 'losses') dir_figures = os.path.join('figures', 'losses')
def __init__(self, args): def __init__(self, args):
@ -215,7 +214,7 @@ class CASRTrainer:
sys.exit() sys.exit()
# Forward pass # Forward pass
outputs = self.model(inputs) #outputs = self.model(inputs)
#self.compute_stats(outputs, labels, dic_err['val'], size_eval, clst='all') #self.compute_stats(outputs, labels, dic_err['val'], size_eval, clst='all')
# self.cout_stats(dic_err['val'], size_eval, clst='all') # self.cout_stats(dic_err['val'], size_eval, clst='all')

View File

@ -18,7 +18,6 @@ from itertools import chain
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import torch import torch
import torch.nn as nn
from torch.utils.data import DataLoader from torch.utils.data import DataLoader
from torch.optim import lr_scheduler from torch.optim import lr_scheduler
@ -38,8 +37,8 @@ class CASRTrainerStandard:
val_task = 'cyclist' val_task = 'cyclist'
lambdas = (1,) lambdas = (1,)
#clusters = ['10', '20', '30', '40'] #clusters = ['10', '20', '30', '40']
input_size = 34 input_size = 34
output_size = 3 output_size = 3
dir_figures = os.path.join('figures', 'losses') dir_figures = os.path.join('figures', 'losses')
def __init__(self, args): def __init__(self, args):
@ -215,7 +214,7 @@ class CASRTrainerStandard:
sys.exit() sys.exit()
# Forward pass # Forward pass
outputs = self.model(inputs) # outputs = self.model(inputs)
#self.compute_stats(outputs, labels, dic_err['val'], size_eval, clst='all') #self.compute_stats(outputs, labels, dic_err['val'], size_eval, clst='all')
# self.cout_stats(dic_err['val'], size_eval, clst='all') # self.cout_stats(dic_err['val'], size_eval, clst='all')