def __init__(self, path, features="ResNET_PCA512.npy", split="test", framerate=2, chunk_size=240, receptive_field=80): self.path = path self.listGames = getListGames(split) self.features = features self.chunk_size = chunk_size self.receptive_field = receptive_field self.dict_event = EVENT_DICTIONARY_V2 self.num_action_classes = 17 self.labels_actions = "Labels-v2.json" self.labels_replays = "Labels-cameras.json" self.framerate = framerate #logging.info("Checking/Download features and labels locally") downloader = SoccerNetDownloader(path) downloader.downloadGames(files=[ self.labels_actions, self.labels_replays, f"1_{self.features}", f"2_{self.features}" ], split=[split], verbose=False)
def __init__(self, path, features="ResNET_PCA512.npy", split="test", framerate=2, chunk_size=240, receptive_field=80): self.path = path self.listGames = getListGames(split) self.features = features self.chunk_size = chunk_size self.receptive_field = receptive_field self.framerate = framerate self.dict_event = EVENT_DICTIONARY_V2 self.num_classes = 17 self.labels = "Labels-v2.json" self.K_parameters = K_V2 * framerate self.num_detections = 15 self.split = split logging.info("Checking/Download features and labels locally") downloader = SoccerNetDownloader(path) if split == "challenge": downloader.downloadGames( files=[f"1_{self.features}", f"2_{self.features}"], split=[split], verbose=False) else: downloader.downloadGames(files=[ self.labels, f"1_{self.features}", f"2_{self.features}" ], split=[split], verbose=False)
def evaluate_average_mAP(SoccerNet_path, Predictions_path, Evaluation_set, framerate): list_games = getListGames(Evaluation_set) targets_numpy = list() detections_numpy = list() closests_numpy = list() for game in list_games: label_half_1, label_half_2 = label2vector(SoccerNet_path + game) predictions_half_1, predictions_half_2 = predictions2vector( SoccerNet_path + game, Predictions_path + game) targets_numpy.append(label_half_1) targets_numpy.append(label_half_2) detections_numpy.append(predictions_half_1) detections_numpy.append(predictions_half_2) closest_numpy = np.zeros(label_half_1.shape) - 1 #Get the closest action index for c in np.arange(label_half_1.shape[-1]): indexes = np.where(label_half_1[:, c] != 0)[0].tolist() if len(indexes) == 0: continue indexes.insert(0, -indexes[0]) indexes.append(2 * closest_numpy.shape[0]) for i in np.arange(len(indexes) - 2) + 1: start = max(0, (indexes[i - 1] + indexes[i]) // 2) stop = min(closest_numpy.shape[0], (indexes[i] + indexes[i + 1]) // 2) closest_numpy[start:stop, c] = label_half_1[indexes[i], c] closests_numpy.append(closest_numpy) closest_numpy = np.zeros(label_half_2.shape) - 1 for c in np.arange(label_half_2.shape[-1]): indexes = np.where(label_half_2[:, c] != 0)[0].tolist() if len(indexes) == 0: continue indexes.insert(0, -indexes[0]) indexes.append(2 * closest_numpy.shape[0]) for i in np.arange(len(indexes) - 2) + 1: start = max(0, (indexes[i - 1] + indexes[i]) // 2) stop = min(closest_numpy.shape[0], (indexes[i] + indexes[i + 1]) // 2) closest_numpy[start:stop, c] = label_half_2[indexes[i], c] closests_numpy.append(closest_numpy) # Compute the performances a_mAP, a_mAP_per_class, a_mAP_visible, a_mAP_per_class_visible, a_mAP_unshown, a_mAP_per_class_unshown = average_mAP( targets_numpy, detections_numpy, closests_numpy, framerate) print("Average mAP: ", a_mAP) print("Average mAP visible: ", a_mAP_visible) print("Average mAP unshown: ", a_mAP_unshown) print("Average mAP per class: ", a_mAP_per_class) print("Average mAP visible per class: ", a_mAP_per_class_visible) print("Average mAP unshown per class: ", a_mAP_per_class_unshown) return a_mAP, a_mAP_per_class, a_mAP_visible, a_mAP_per_class_visible, a_mAP_unshown, a_mAP_per_class_unshown
def __init__(self, path, features="ResNET_PCA512.npy", split="test", framerate=2, chunk_size=240, receptive_field=80): self.path = path self.listGames = getListGames(split) self.features = features self.chunk_size = chunk_size self.receptive_field = receptive_field self.dict_event = EVENT_DICTIONARY_V2 self.num_action_classes = 17 self.labels_actions = "Labels-v2.json" self.labels_replays = "Labels-cameras.json" self.framerate = framerate
def __init__(self, path, features="ResNET_PCA512.npy", split="test", version=1, framerate=2, chunk_size=240, receptive_field=80, num_detections=45): self.path = path #self.listGames = getListGames(split)os.path.join(self.dataset_path + self.datatype) self.listGames = getListGames(split, task="camera-changes") # self.listGames = np.load(os.path.join(self.path, split)) self.features = features self.chunk_size = chunk_size self.receptive_field = receptive_field self.framerate = framerate if version == 1: self.dict_event = EVENT_DICTIONARY_V1 self.num_classes = 3 self.labels = "Labels.json" self.K_parameters = K_V1 * framerate self.num_detections = 5 elif version == 2: self.dict_change = Camera_Change_DICTIONARY self.dict_type = Camera_Type_DICTIONARY self.num_classes_sgementation = 13 self.num_classes_camera_change = 1 self.labels = "Labels-cameras.json" self.K_parameters = K_V2 * framerate self.num_detections = num_detections logging.info("Checking/Download features and labels locally") downloader = SoccerNetDownloader(path) downloader.downloadGames( files=[self.labels, f"1_{self.features}", f"2_{self.features}"], split=[split], verbose=False, task="camera-changes", randomized=True)
def __init__(self, path, features="ResNET_PCA512.npy", split=["test"], version=1, framerate=2, chunk_size=240): self.path = path self.listGames = getListGames(split) self.features = features self.chunk_size = chunk_size self.framerate = framerate self.version = version self.split = split if version == 1: self.dict_event = EVENT_DICTIONARY_V1 self.num_classes = 3 self.labels = "Labels.json" elif version == 2: self.dict_event = EVENT_DICTIONARY_V2 self.num_classes = 17 self.labels = "Labels-v2.json" logging.info("Checking/Download features and labels locally") downloader = SoccerNetDownloader(path) for s in split: if s == "challenge": downloader.downloadGames( files=[f"1_{self.features}", f"2_{self.features}"], split=[s], verbose=False) else: downloader.downloadGames(files=[ self.labels, f"1_{self.features}", f"2_{self.features}" ], split=[s], verbose=False)
def __init__(self, path, features="ResNET_PCA512.npy", split="train", version=1, framerate=2): self.path = path self.listGames = getListGames(split, task="camera-changes") self.features = features self.framerate = framerate if version == 1: self.dict_event = EVENT_DICTIONARY_V1 self.num_classes = 3 self.labels = "Labels.json" self.K_parameters = K_V1 * framerate self.num_detections = 5 elif version == 2: self.dict_change = Camera_Change_DICTIONARY self.dict_shot = Camera_Type_DICTIONARY self.num_classes_sgementation = 13 self.num_classes_camera_change = 1 self.labels = "Labels-cameras.json" self.K_parameters = K_V2 * framerate self.num_detections = num_detections
def test(dataloader,model, model_name,split,annotation_path,detection_path,save_results): batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() spotting_grountruth = list() spotting_predictions = list() segmentation_predictions = list() replay_grountruth = list() chunk_size = model.chunk_size receptive_field = model.receptive_field model.eval() def timestamps2long(output_spotting, video_size, chunk_size, receptive_field): start = 0 last = False receptive_field = receptive_field//2 timestamps_long = torch.zeros([video_size,1], dtype = torch.float, device=output_spotting.device)-1 for batch in np.arange(output_spotting.size()[0]): tmp_timestamps = torch.zeros([chunk_size,1], dtype = torch.float, device=output_spotting.device)-1 tmp_timestamps[torch.floor(output_spotting[batch,0,1]*(chunk_size-1)).type(torch.int) , 0 ] = output_spotting[batch,0,0] # ------------------------------------------ # Store the result of the chunk in the video # ------------------------------------------ if video_size <= chunk_size: timestamps_long = tmp_timestamps[0:video_size] break # For the first chunk if start == 0: timestamps_long[0:chunk_size-receptive_field] = tmp_timestamps[0:chunk_size-receptive_field] # For the last chunk elif last: timestamps_long[start+receptive_field:start+chunk_size] = tmp_timestamps[receptive_field:] break # For every other chunk else: timestamps_long[start+receptive_field:start+chunk_size-receptive_field] = tmp_timestamps[receptive_field:chunk_size-receptive_field] # --------------- # Loop Management # --------------- # Update the index start += chunk_size - 2 * receptive_field # Check if we are at the last index of the game if start + chunk_size >= video_size: start = video_size - chunk_size last = True return timestamps_long def batch2long(output_segmentation, video_size, chunk_size, receptive_field): start = 0 last = False receptive_field = receptive_field//2 segmentation_long = torch.zeros([video_size,1], dtype = torch.float, device=output_segmentation.device) for batch in np.arange(output_segmentation.size()[0]): tmp_segmentation = 1-output_segmentation[batch] # ------------------------------------------ # Store the result of the chunk in the video # ------------------------------------------ # For the first chunk if start == 0: segmentation_long[0:chunk_size-receptive_field] = tmp_segmentation[0:chunk_size-receptive_field] # For the last chunk elif last: segmentation_long[start+receptive_field:start+chunk_size] = tmp_segmentation[receptive_field:] break # For every other chunk else: segmentation_long[start+receptive_field:start+chunk_size-receptive_field] = tmp_segmentation[receptive_field:chunk_size-receptive_field] # --------------- # Loop Management # --------------- # Update the index start += chunk_size - 2 * receptive_field # Check if we are at the last index of the game if start + chunk_size >= video_size: start = video_size - chunk_size last = True return segmentation_long end = time.time() game_list=getListGames(split) with tqdm(enumerate(dataloader), total=len(dataloader), ncols=120) as t: for i, (feat_half1, feat_half2, replay_half1, replay_half2, label_half1, label_half2, label_replay_half1, label_replay_half2,replay_name_half1,replay_name_half2) in t: data_time.update(time.time() - end) replay_half1 = replay_half1.cuda().squeeze(0) replay_half2 = replay_half2.cuda().squeeze(0) feat_half1 = feat_half1.cuda().squeeze(0) feat_half2 = feat_half2.cuda().squeeze(0) feat_half1=feat_half1.unsqueeze(1) feat_half2=feat_half2.unsqueeze(1) detection_half1 = list() replay_names_half1 = list() for replay, label, label_replay, replay_name in zip(replay_half1, label_half1, label_replay_half1,replay_name_half1): label = label.float().squeeze(0) label_replay = label_replay.float().squeeze(0) replay = replay.unsqueeze(0).repeat(feat_half1.shape[0],1,1).unsqueeze(1) feat = torch.cat((feat_half1,replay),1) output_segmentation_half_1, output_spotting_half_1 = model(feat) timestamp_long_half_1 = timestamps2long(output_spotting_half_1.cpu().detach(), label.size()[0], chunk_size, receptive_field) segmentation_long_half_1 = batch2long(output_segmentation_half_1.cpu().detach(), label.size()[0], chunk_size, receptive_field) detection_half1.append(timestamp_long_half_1) replay_names_half1.append(replay_name) spotting_grountruth.append(label) spotting_predictions.append(timestamp_long_half_1) segmentation_predictions.append(segmentation_long_half_1) replay_grountruth.append(label_replay) detection_half2 = list() replay_names_half2 = list() for replay, label, label_replay, replay_name in zip(replay_half2, label_half2, label_replay_half2, replay_name_half2): label = label.float().squeeze(0) label_replay = label_replay.float().squeeze(0) replay = replay.unsqueeze(0).repeat(feat_half2.shape[0],1,1).unsqueeze(1) feat = torch.cat((feat_half2,replay),1) output_segmentation_half_2, output_spotting_half_2 = model(feat) timestamp_long_half_2 = timestamps2long(output_spotting_half_2.cpu().detach(), label.size()[0], chunk_size, receptive_field) segmentation_long_half_2 = batch2long(output_segmentation_half_2.cpu().detach(), label.size()[0], chunk_size, receptive_field) detection_half2.append(timestamp_long_half_2) replay_names_half2.append(replay_name) spotting_grountruth.append(label) spotting_predictions.append(timestamp_long_half_2) segmentation_predictions.append(segmentation_long_half_2) replay_grountruth.append(label_replay) if save_results: game_results_to_json(detection_path,split,detection_half1,detection_half2,replay_names_half1,replay_names_half2,model.framerate,timestamp_long_half_1.shape[0],timestamp_long_half_2.shape[0]) #visualize(spotting_grountruth ,spotting_predictions,segmentation_predictions, replay_grountruth) if not save_results: a_AP = average_mAP(spotting_grountruth, spotting_predictions, model.framerate) print("a-AP: ", a_AP) if save_results: results=evaluate(annotation_path,detection_path,"Detection-replays.json",split) print("a_AP: ",results["a_AP"]) return results["a_AP"]
def __init__(self, path, features="ResNET_PCA512.npy", split="train", version=1, framerate=2, chunk_size=240, receptive_field=80, num_detections=45): self.path = path self.listGames = getListGames(split, task="camera-changes") #self.listGames = np.load(os.path.join(self.path, split)) self.features = features self.chunk_size = chunk_size self.receptive_field = receptive_field self.framerate = framerate if version == 1: self.dict_event = EVENT_DICTIONARY_V1 self.num_classes = 3 self.labels = "Labels.json" self.K_parameters = K_V1 * framerate self.num_detections = 5 elif version == 2: self.dict_change = Camera_Change_DICTIONARY self.dict_type = Camera_Type_DICTIONARY self.num_classes_sgementation = 13 self.num_classes_camera_change = 1 self.labels = "Labels-cameras.json" self.K_parameters = K_V2 * framerate self.num_detections = num_detections logging.info("Checking/Download features and labels locally") downloader = SoccerNetDownloader(path) downloader.downloadGames( files=[self.labels, f"1_{self.features}", f"2_{self.features}"], split=[split], verbose=False, task="camera-changes", randomized=True) logging.info("Pre-compute clips") clip_feats = [] clip_labels = [] self.game_feats = list() self.game_labels = list() self.game_change_labels = list() self.game_anchors = list() game_counter = 0 for game in tqdm(self.listGames): # Load features #10% of dataset # if np.random.randint(10, size=1)<8: # continue feat_half1 = np.load( os.path.join(self.path, game, "1_" + self.features)) feat_half2 = np.load( os.path.join(self.path, game, "2_" + self.features)) # Load labels #path2="/content/drive/My Drive/project/path/to/soccernet/data (1)/" labels = json.load(open(os.path.join(path, game, self.labels))) label_half1 = np.zeros( (feat_half1.shape[0], self.num_classes_sgementation)) label_half2 = np.zeros( (feat_half2.shape[0], self.num_classes_sgementation)) label_change_half1 = np.zeros( (feat_half1.shape[0], self.num_classes_camera_change)) label_change_half2 = np.zeros( (feat_half2.shape[0], self.num_classes_camera_change)) for annotation in labels["annotations"]: time = annotation["gameTime"] camera_type = annotation["label"] camera_change = annotation["change_type"] half = int(time[0]) minutes = int(time[-5:-3]) seconds = int(time[-2::]) framerate = self.framerate frame = framerate * (seconds + 60 * minutes) if camera_type in self.dict_type: label_type = self.dict_type[camera_type] if half == 1: frame = min(frame, feat_half1.shape[0] - 1) label_half1[frame][label_type] = 1 if half == 2: frame = min(frame, feat_half2.shape[0] - 1) label_half2[frame][label_type] = 1 #Onehot for camera changee if camera_change in self.dict_change: label_change = self.dict_change[camera_change] if half == 1: frame = min(frame, feat_half1.shape[0] - 1) label_change_half1[frame] = 1 if half == 2: frame = min(frame, feat_half2.shape[0] - 1) label_change_half2[frame] = 1 shift_half1 = oneHotToAlllabels(label_half1) shift_half2 = oneHotToAlllabels(label_half2) #anchors_half1 = getChunks_anchors(shift_half1, game_counter, self.chunk_size, self.receptive_field) anchors_half1 = getChunks_anchors(label_change_half1, game_counter, self.chunk_size, self.receptive_field) game_counter = game_counter + 1 # with np.printoptions(threshold=np.inf): # print('anchors_half1',anchors_half1) #anchors_half2 = getChunks_anchors(shift_half2, game_counter, self.chunk_size, self.receptive_field) anchors_half2 = getChunks_anchors(label_change_half2, game_counter, self.chunk_size, self.receptive_field) game_counter = game_counter + 1 self.game_feats.append(feat_half1) self.game_feats.append(feat_half2) self.game_labels.append(shift_half1) self.game_labels.append(shift_half2) # with np.printoptions(threshold=np.inf): # print('game_labels',self.game_labels) self.game_change_labels.append(label_change_half1) self.game_change_labels.append(label_change_half2) # with np.printoptions(threshold=np.inf): # print('game_change_labels',self.game_change_labels) for anchor in anchors_half1: self.game_anchors.append(anchor) for anchor in anchors_half2: self.game_anchors.append(anchor)
def test(dataloader, model, model_name, save_predictions=False): batch_time = AverageMeter() data_time = AverageMeter() losses = AverageMeter() spotting_grountruth = list() spotting_grountruth_visibility = list() spotting_predictions = list() segmentation_predictions = list() chunk_size = model.chunk_size receptive_field = model.receptive_field model.eval() end = time.time() with tqdm(enumerate(dataloader), total=len(dataloader), ncols=120) as t: for i, (feat_half1, feat_half2, label_half1, label_half2) in t: data_time.update(time.time() - end) feat_half1 = feat_half1.cuda().squeeze(0) label_half1 = label_half1.float().squeeze(0) feat_half2 = feat_half2.cuda().squeeze(0) label_half2 = label_half2.float().squeeze(0) feat_half1 = feat_half1.unsqueeze(1) feat_half2 = feat_half2.unsqueeze(1) # Compute the output output_segmentation_half_1, output_spotting_half_1 = model( feat_half1) output_segmentation_half_2, output_spotting_half_2 = model( feat_half2) timestamp_long_half_1 = timestamps2long( output_spotting_half_1.cpu().detach(), label_half1.size()[0], chunk_size, receptive_field) timestamp_long_half_2 = timestamps2long( output_spotting_half_2.cpu().detach(), label_half2.size()[0], chunk_size, receptive_field) segmentation_long_half_1 = batch2long( output_segmentation_half_1.cpu().detach(), label_half1.size()[0], chunk_size, receptive_field) segmentation_long_half_2 = batch2long( output_segmentation_half_2.cpu().detach(), label_half2.size()[0], chunk_size, receptive_field) spotting_grountruth.append(torch.abs(label_half1)) spotting_grountruth.append(torch.abs(label_half2)) spotting_grountruth_visibility.append(label_half1) spotting_grountruth_visibility.append(label_half2) spotting_predictions.append(timestamp_long_half_1) spotting_predictions.append(timestamp_long_half_2) segmentation_predictions.append(segmentation_long_half_1) segmentation_predictions.append(segmentation_long_half_2) # Transformation to numpy for evaluation targets_numpy = list() closests_numpy = list() detections_numpy = list() for target, detection in zip(spotting_grountruth_visibility, spotting_predictions): target_numpy = target.numpy() targets_numpy.append(target_numpy) detections_numpy.append(NMS(detection.numpy(), 20 * model.framerate)) closest_numpy = np.zeros(target_numpy.shape) - 1 #Get the closest action index for c in np.arange(target_numpy.shape[-1]): indexes = np.where(target_numpy[:, c] != 0)[0].tolist() if len(indexes) == 0: continue indexes.insert(0, -indexes[0]) indexes.append(2 * closest_numpy.shape[0]) for i in np.arange(len(indexes) - 2) + 1: start = max(0, (indexes[i - 1] + indexes[i]) // 2) stop = min(closest_numpy.shape[0], (indexes[i] + indexes[i + 1]) // 2) closest_numpy[start:stop, c] = target_numpy[indexes[i], c] closests_numpy.append(closest_numpy) # Save the predictions to the json format if save_predictions: list_game = getListGames(dataloader.dataset.split) for index in np.arange(len(list_game)): predictions2json(detections_numpy[index * 2], detections_numpy[(index * 2) + 1], "outputs/", list_game[index], model.framerate) # Compute the performances a_mAP, a_mAP_per_class, a_mAP_visible, a_mAP_per_class_visible, a_mAP_unshown, a_mAP_per_class_unshown = average_mAP( targets_numpy, detections_numpy, closests_numpy, model.framerate) print("Average mAP: ", a_mAP) print("Average mAP visible: ", a_mAP_visible) print("Average mAP unshown: ", a_mAP_unshown) print("Average mAP per class: ", a_mAP_per_class) print("Average mAP visible per class: ", a_mAP_per_class_visible) print("Average mAP unshown per class: ", a_mAP_per_class_unshown) return a_mAP, a_mAP_per_class, a_mAP_visible, a_mAP_per_class_visible, a_mAP_unshown, a_mAP_per_class_unshown
def __init__(self, path, features="ResNET_PCA512.npy", split="train", framerate=2, chunk_size=240, receptive_field=80, chunks_per_epoch=6000, replay_size=40, hard_negative_weight=0.5, random_negative_weight=0.5, replay_negative_weight=0, loop=10): self.path = path self.listGames = getListGames(split) self.features = features self.chunk_size = chunk_size self.receptive_field = receptive_field self.chunks_per_epoch = chunks_per_epoch self.dict_event = EVENT_DICTIONARY_V2 self.num_action_classes = 17 self.labels_actions = "Labels-v2.json" self.labels_replays = "Labels-cameras.json" self.replay_size = replay_size self.hard_negative_weight = hard_negative_weight self.random_negative_weight = random_negative_weight self.replay_negative_weight = replay_negative_weight self.loop = loop #logging.info("Checking/Download features and labels locally") #downloader = SoccerNetDownloader(path) #downloader.downloadGames(files=[self.labels_actions, f"1_{self.features}", f"2_{self.features}"], split=[split], verbose=False) logging.info("Pre-compute clips") clip_feats = [] clip_labels = [] self.game_feats = list() self.replay_labels = list() self.replay_anchors = list() self.game_anchors = list() game_counter = 0 replay_sizes = np.zeros((250, 1)) for game in tqdm(self.listGames): # if np.random.randint(10, size=1)<8: # # print("Warning Only 10\% of dataset is used ") # continue # Load the features feat_half1 = np.load( os.path.join(self.path, game, "1_" + self.features)) feat_half2 = np.load( os.path.join(self.path, game, "2_" + self.features)) # load the replay labels labels_replays = json.load( open(os.path.join(self.path, game, self.labels_replays))) previous_timestamp = 0 anchors_replay_half1 = list() anchors_replay_half2 = list() for annotation in labels_replays["annotations"]: time = annotation["gameTime"] half = int(time[0]) minutes = int(time[-5:-3]) seconds = int(time[-2::]) frame = framerate * (seconds + 60 * minutes) if not "link" in annotation: previous_timestamp = frame continue event = annotation["link"]["label"] if not event in self.dict_event or int( annotation["link"]["half"]) != half: previous_timestamp = frame continue if previous_timestamp == frame: previous_timestamp = frame continue time_event = annotation["link"]["time"] minutes_event = int(time_event[0:2]) seconds_event = int(time_event[3:]) frame_event = framerate * (seconds_event + 60 * minutes_event) replay_sizes[int((frame - previous_timestamp) / 2)] += 1 label = self.dict_event[event] if half == 1: anchors_replay_half1.append([ game_counter, previous_timestamp, frame, frame_event, label ]) if half == 2: anchors_replay_half2.append([ game_counter + 1, previous_timestamp, frame, frame_event, label ]) previous_timestamp = frame # Load action labels labels_actions = json.load( open(os.path.join(self.path, game, self.labels_actions))) anchors_half1 = list() anchors_half2 = list() for annotation in labels_actions["annotations"]: time = annotation["gameTime"] event = annotation["label"] half = int(time[0]) minutes = int(time[-5:-3]) seconds = int(time[-2::]) frame = framerate * (seconds + 60 * minutes) if event not in self.dict_event: continue label = self.dict_event[event] if half == 1: frame = min(frame, feat_half1.shape[0] - 1) anchors_half1.append([game_counter, frame, label]) if half == 2: frame = min(frame, feat_half2.shape[0] - 1) anchors_half2.append([game_counter + 1, frame, label]) self.game_feats.append(feat_half1) self.game_feats.append(feat_half2) self.game_anchors.append(list()) for i in np.arange(self.num_action_classes): self.game_anchors[-1].append(list()) for anchor in anchors_half1: self.game_anchors[-1][anchor[2]].append(anchor) self.game_anchors.append(list()) for i in np.arange(self.num_action_classes): self.game_anchors[-1].append(list()) for anchor in anchors_half2: self.game_anchors[-1][anchor[2]].append(anchor) for anchor in anchors_replay_half1: self.replay_anchors.append(anchor) for anchor in anchors_replay_half2: self.replay_anchors.append(anchor) game_counter = game_counter + 2
def __init__(self, path, features="ResNET_PCA512.npy", split=["train"], version=1, framerate=2, chunk_size=240): self.path = path self.listGames = getListGames(split) self.features = features self.chunk_size = chunk_size self.version = version if version == 1: self.num_classes = 3 self.labels = "Labels.json" elif version == 2: self.dict_event = EVENT_DICTIONARY_V2 self.num_classes = 17 self.labels = "Labels-v2.json" logging.info("Checking/Download features and labels locally") downloader = SoccerNetDownloader(path) downloader.downloadGames( files=[self.labels, f"1_{self.features}", f"2_{self.features}"], split=split, verbose=False) logging.info("Pre-compute clips") self.game_feats = list() self.game_labels = list() # game_counter = 0 for game in tqdm(self.listGames): # Load features feat_half1 = np.load( os.path.join(self.path, game, "1_" + self.features)) feat_half1 = feat_half1.reshape(-1, feat_half1.shape[-1]) feat_half2 = np.load( os.path.join(self.path, game, "2_" + self.features)) feat_half2 = feat_half2.reshape(-1, feat_half2.shape[-1]) # print("feat_half1.shape",feat_half1.shape) feat_half1 = feats2clip(torch.from_numpy(feat_half1), stride=self.chunk_size, clip_length=self.chunk_size) feat_half2 = feats2clip(torch.from_numpy(feat_half2), stride=self.chunk_size, clip_length=self.chunk_size) # print("feat_half1.shape",feat_half1.shape) # Load labels labels = json.load(open(os.path.join(self.path, game, self.labels))) label_half1 = np.zeros((feat_half1.shape[0], self.num_classes + 1)) label_half1[:, 0] = 1 # those are BG classes label_half2 = np.zeros((feat_half2.shape[0], self.num_classes + 1)) label_half2[:, 0] = 1 # those are BG classes for annotation in labels["annotations"]: time = annotation["gameTime"] event = annotation["label"] half = int(time[0]) minutes = int(time[-5:-3]) seconds = int(time[-2::]) frame = framerate * (seconds + 60 * minutes) if version == 1: if "card" in event: label = 0 elif "subs" in event: label = 1 elif "soccer" in event: label = 2 else: continue elif version == 2: if event not in self.dict_event: continue label = self.dict_event[event] # if label outside temporal of view if half == 1 and frame // self.chunk_size >= label_half1.shape[ 0]: continue if half == 2 and frame // self.chunk_size >= label_half2.shape[ 0]: continue if half == 1: label_half1[frame // self.chunk_size][0] = 0 # not BG anymore label_half1[frame // self.chunk_size][label + 1] = 1 # that's my class if half == 2: label_half2[frame // self.chunk_size][0] = 0 # not BG anymore label_half2[frame // self.chunk_size][label + 1] = 1 # that's my class self.game_feats.append(feat_half1) self.game_feats.append(feat_half2) self.game_labels.append(label_half1) self.game_labels.append(label_half2) self.game_feats = np.concatenate(self.game_feats) self.game_labels = np.concatenate(self.game_labels)
if half == 1: frame = min(frame, vector_size - 1) prediction_half1[frame][label] = value if half == 2: frame = min(frame, vector_size - 1) prediction_half2[frame][label] = value return prediction_half1, prediction_half2 if __name__ == "__main__": print("test input") list_games = getListGames("test") print(list_games[0]) label_1, label_2 = label2vector( "/home/acioppa/Work/Projects/DeepSport/Context-Aware/data_latest/" + list_games[0], "ResNET_TF2_PCA512.npy") predictions2json(label_1 - 0.1, label_2 - 0.1, "outputs/", list_games[0]) predictions_half_1, predictions_half_2 = predictions2vector( "/home/acioppa/Work/Projects/DeepSport/Context-Aware/data_latest/" + list_games[0], "outputs/" + list_games[0], "ResNET_TF2_PCA512.npy") print(label_1.shape) print(label_2.shape) print(predictions_half_1.shape) print(predictions_half_2.shape)
def __init__(self, path, features="ResNET_PCA512.npy", split="train", framerate=2, chunk_size=240, receptive_field=80, chunks_per_epoch=6000): self.path = path self.listGames = getListGames(split) self.features = features self.chunk_size = chunk_size self.receptive_field = receptive_field self.chunks_per_epoch = chunks_per_epoch self.dict_event = EVENT_DICTIONARY_V2 self.num_classes = 17 self.labels = "Labels-v2.json" self.K_parameters = K_V2 * framerate self.num_detections = 15 self.split = split logging.info("Checking/Download features and labels locally") downloader = SoccerNetDownloader(path) downloader.downloadGames( files=[self.labels, f"1_{self.features}", f"2_{self.features}"], split=[split], verbose=False) logging.info("Pre-compute clips") clip_feats = [] clip_labels = [] self.game_feats = list() self.game_labels = list() self.game_anchors = list() for i in np.arange(self.num_classes + 1): self.game_anchors.append(list()) game_counter = 0 for game in tqdm(self.listGames): # Load features feat_half1 = np.load( os.path.join(self.path, game, "1_" + self.features)) feat_half2 = np.load( os.path.join(self.path, game, "2_" + self.features)) # Load labels labels = json.load(open(os.path.join(self.path, game, self.labels))) label_half1 = np.zeros((feat_half1.shape[0], self.num_classes)) label_half2 = np.zeros((feat_half2.shape[0], self.num_classes)) for annotation in labels["annotations"]: time = annotation["gameTime"] event = annotation["label"] half = int(time[0]) minutes = int(time[-5:-3]) seconds = int(time[-2::]) frame = framerate * (seconds + 60 * minutes) if event not in self.dict_event: continue label = self.dict_event[event] if half == 1: frame = min(frame, feat_half1.shape[0] - 1) label_half1[frame][label] = 1 if half == 2: frame = min(frame, feat_half2.shape[0] - 1) label_half2[frame][label] = 1 shift_half1 = oneHotToShifts(label_half1, self.K_parameters.cpu().numpy()) shift_half2 = oneHotToShifts(label_half2, self.K_parameters.cpu().numpy()) anchors_half1 = getChunks_anchors(shift_half1, game_counter, self.K_parameters.cpu().numpy(), self.chunk_size, self.receptive_field) game_counter = game_counter + 1 anchors_half2 = getChunks_anchors(shift_half2, game_counter, self.K_parameters.cpu().numpy(), self.chunk_size, self.receptive_field) game_counter = game_counter + 1 self.game_feats.append(feat_half1) self.game_feats.append(feat_half2) self.game_labels.append(shift_half1) self.game_labels.append(shift_half2) for anchor in anchors_half1: self.game_anchors[anchor[2]].append(anchor) for anchor in anchors_half2: self.game_anchors[anchor[2]].append(anchor)