<jupyter_start><jupyter_text>Read Pandas  Read files in directory  Create training split<jupyter_code>#gt_dict
dir_path = '/mnt/d/Dataset/hacs_segments_features_I3D/features'
import json
with open(os.path.join(dir_path, "gt.json")) as gt_f:
    gt_dict = json.load(gt_f)["database"]

vid_names=list(gt_dict.keys())
vid_train = []
for v_name in vid_names:
    if (gt_dict[v_name]['subset']=='training'):
        vid_train.append(v_name)

dir_path = "/mnt/d/Dataset/hacs_segments_features_I3D/features/split_train.txt"
if os.path.exists(dir_path):
    os.remove(dir_path)

textfile = open(dir_path, "w")
for element in vid_train:
    textfile.write(element + "\n")
textfile.close()
vid_train = []
for v_name in vid_names:
    if(gt_dict[v_name]['subset']=='validation'):
        vid_train.append(v_name)

dir_path = "/mnt/d/Dataset/hacs_segments_features_I3D/features/split_test.txt"
if os.path.exists(dir_path):
    os.remove(dir_path)

textfile = open(dir_path, "w")
for element in vid_train:
    textfile.write(element + "\n")
textfile.close()<jupyter_output><empty_output><jupyter_text>Testing boundary generator<jupyter_code>idx=3
video_name = vid_names[idx]
video_info = gt_dict[video_name]
video_labels = video_info['annotations']
video_second = float(video_info['duration'])
corrected_second = video_second
temporal_scale = sample_segments_num
temporal_gap = 1. / temporal_scale
gt_bbox = []
gt_len_small = 3 * temporal_gap

for j in range(len(video_labels)):
    tmp_info = video_labels[j]
    tmp_start = max(min(1, tmp_info['segment'][0] / corrected_second), 0)
    tmp_end = max(min(1, tmp_info['segment'][1] / corrected_second), 0)
    gt_bbox.append([tmp_start, tmp_end])

gt_bbox = np.array(gt_bbox)
gt_xmins = gt_bbox[:, 0]
gt_xmaxs = gt_bbox[:, 1]
gt_lens = gt_xmaxs - gt_xmins
gt_len_small = 3 * temporal_gap  # np.maximum(self.temporal_gap, self.boundary_ratio * gt_lens)
gt_start_bboxs = np.stack((gt_xmins - gt_len_small / 2, gt_xmins + gt_len_small / 2), axis=1)
gt_end_bboxs = np.stack((gt_xmaxs - gt_len_small / 2, gt_xmaxs + gt_len_small / 2), axis=1)

### ==== Additional ====
gt_start_nonact = gt_bbox[:,1]+0.5*temporal_gap # get end border of end region
gt_end_nonact = gt_bbox[:,0]-0.5*temporal_gap # get start border of start region

ls_start = gt_start_nonact[:-1]
ls_end = gt_end_nonact[1:]

gt_nonact_bbox = [(0,gt_end_nonact[0])]
#gt_nonact_bbox = 
gt_nonact_bbox.extend(list(zip(ls_start,ls_end)))
gt_nonact_bbox.append((gt_start_nonact[-1],1))
print(video_second)
#for fil in list_files:
data = np.load(os.path.join(data_dir,list_files[idx]))
print(data.shape)
con_vid_feature=data
sample_segments_num=75
#con_vid_spd_feature = random_sample(con_vid_feature, sample_segments_num)
con_vid_spd_feature = uniform_sample(con_vid_feature, sample_segments_num)
print(con_vid_spd_feature.shape)
print(gt_bbox)
print(gt_start_nonact)
print(gt_end_nonact)
print(ls_start)
print(ls_end)
print(gt_nonact_bbox)<jupyter_output>[[0.10718447 0.1872233 ]
 [0.21133981 0.35825243]
 [0.38458253 0.47394175]
 [0.51996117 0.60547573]
 [0.63716505 0.66209709]]
[0.19388997 0.3649191  0.48060842 0.6121424  0.66876376]
[0.1005178  0.20467314 0.37791586 0.5132945  0.63049838]
[0.19388997 0.3649191  0.48060842 0.6121424 ]
[0.20467314 0.37791586 0.5132945  0.63049838]
[(0, 0.10051779976900116), (0.19388996836462125, 0.2046731399793132), (0.36491909524240423, 0.37791585909870234), (0.4806084160800327, 0.5132945004011437), (0.6121423971733686, 0.63049838435145), (0.6687637566165583, 1)]<jupyter_text>Evaluate each anchors and get segments that contain only non-action<jupyter_code>anchor_xmin = [temporal_gap * (i - 0.5) for i in range(temporal_scale)]
anchor_xmax = [temporal_gap * (i + 0.5) for i in range(temporal_scale)]
anchor_xs = list(zip(anchor_xmin,anchor_xmax))
anchor_xs[0:10][0]
#Assign backgroun label to each segment
list_tem_bak=[]
for anchor_now in anchor_xs:
    tem_bak = 0
    for bound in gt_nonact_bbox:
        if((max(0,anchor_now[0])>=bound[0]) and (min(anchor_now[1],1)<=bound[1])):
            tem_bak = 1
            break
    list_tem_bak.append(tem_bak)
print(len(list_tem_bak))
con_vid_spd_feature.shape
bool_list = list(map(bool,list_tem_bak))
from itertools import compress
con_vid_spd_feature_bak=list(compress(con_vid_spd_feature, bool_list))
print(len(con_vid_spd_feature_bak))
for jdx in range(len(anchor_xmin)):
    match_score_start.append(np.max(
        ioa_with_anchors(anchor_xmin[jdx], anchor_xmax[jdx], gt_start_bboxs[:, 0], gt_start_bboxs[:, 1])))
ls_end
video_name = data_list[index]




for j in range(len(video_labels)):
    tmp_info = video_labels[j]
    tmp_start = max(min(1, tmp_info['segment'][0] / corrected_second), 0)
    tmp_end = max(min(1, tmp_info['segment'][1] / corrected_second), 0)
    gt_bbox.append([tmp_start, tmp_end])
    tmp_gt_iou_map = iou_with_anchors(
        self.match_map[:, 0], self.match_map[:, 1], tmp_start, tmp_end)
    tmp_gt_iou_map = np.reshape(tmp_gt_iou_map,
                                [self.temporal_scale, self.temporal_scale])
    gt_iou_map.append(tmp_gt_iou_map)


gt_bbox = np.array(gt_bbox)
gt_xmins = gt_bbox[:, 0]
gt_xmaxs = gt_bbox[:, 1] 
gt_start_bboxs = np.stack((gt_xmins - gt_len_small / 2, gt_xmins + gt_len_small / 2), axis=1)
gt_end_bboxs = np.stack((gt_xmaxs - gt_len_small / 2, gt_xmaxs + gt_len_small / 2), axis=1)
### ==== Additional ====
gt_start_nonact = gt_end_bboxs[:,1] # get end border of end region
gt_end_nonact = gt_start_bboxs[:,0] # get start border of start region

ls_start = gt_start_nonact[1:]
ls_end = gt_end_nonact[:-1]

gt_nonact_bbox = list(zip(ls_start,ls_end))
###
return match_score_start, match_score_end, gt_iou_map
import os 
import json 
import torch 
import argparse 
import numpy as np
import pandas as pd 
from torch.utils.data import Dataset 
import torch.utils.data as data

import numpy as np<jupyter_output><empty_output><jupyter_text>Check dsource dataset code<jupyter_code>_CLASS_NAME = {
    "THUMOS":['BaseballPitch', 'BasketballDunk', 'Billiards', 'CleanAndJerk',
              'CliffDiving', 'CricketBowling', 'CricketShot', 'Diving',
              'FrisbeeCatch', 'GolfSwing', 'HammerThrow', 'HighJump',
              'JavelinThrow', 'LongJump', 'PoleVault', 'Shotput',
              'SoccerPenalty', 'TennisSwing', 'ThrowDiscus', 'VolleyballSpiking'],

    "ActivityNet":['Applying sunscreen', 'Archery', 'Arm wrestling', 'Assembling bicycle',
                    'BMX', 'Baking cookies', 'Ballet', 'Bathing dog', 'Baton twirling',
                    'Beach soccer', 'Beer pong', 'Belly dance', 'Blow-drying hair', 'Blowing leaves',
                    'Braiding hair', 'Breakdancing', 'Brushing hair', 'Brushing teeth', 'Building sandcastles',
                    'Bullfighting', 'Bungee jumping', 'Calf roping', 'Camel ride', 'Canoeing', 'Capoeira',
                    'Carving jack-o-lanterns', 'Changing car wheel', 'Cheerleading', 'Chopping wood',
                    'Clean and jerk', 'Cleaning shoes', 'Cleaning sink', 'Cleaning windows', 'Clipping cat claws',
                    'Cricket', 'Croquet', 'Cumbia', 'Curling', 'Cutting the grass', 'Decorating the Christmas tree',
                    'Disc dog', 'Discus throw', 'Dodgeball', 'Doing a powerbomb', 'Doing crunches', 'Doing fencing',
                    'Doing karate', 'Doing kickboxing', 'Doing motocross', 'Doing nails', 'Doing step aerobics',
                    'Drinking beer', 'Drinking coffee', 'Drum corps', 'Elliptical trainer', 'Fixing bicycle', 'Fixing the roof',
                    'Fun sliding down', 'Futsal', 'Gargling mouthwash', 'Getting a haircut', 'Getting a piercing', 'Getting a tattoo',
                    'Grooming dog', 'Grooming horse', 'Hammer throw', 'Hand car wash', 'Hand washing clothes', 'Hanging wallpaper',
                    'Having an ice cream', 'High jump', 'Hitting a pinata', 'Hopscotch', 'Horseback riding', 'Hula hoop',
                    'Hurling', 'Ice fishing', 'Installing carpet', 'Ironing clothes', 'Javelin throw', 'Kayaking', 'Kite flying',
                    'Kneeling', 'Knitting', 'Laying tile', 'Layup drill in basketball', 'Long jump', 'Longboarding',
                    'Making a cake', 'Making a lemonade', 'Making a sandwich', 'Making an omelette', 'Mixing drinks',
                    'Mooping floor', 'Mowing the lawn', 'Paintball', 'Painting', 'Painting fence', 'Painting furniture',
                    'Peeling potatoes', 'Ping-pong', 'Plastering', 'Plataform diving', 'Playing accordion', 'Playing badminton',
                    'Playing bagpipes', 'Playing beach volleyball', 'Playing blackjack', 'Playing congas', 'Playing drums',
                    'Playing field hockey', 'Playing flauta', 'Playing guitarra', 'Playing harmonica', 'Playing ice hockey',
                    'Playing kickball', 'Playing lacrosse', 'Playing piano', 'Playing polo', 'Playing pool', 'Playing racquetball',
                    'Playing rubik cube', 'Playing saxophone', 'Playing squash', 'Playing ten pins', 'Playing violin',
                    'Playing water polo', 'Pole vault', 'Polishing forniture', 'Polishing shoes', 'Powerbocking', 'Preparing pasta',
                    'Preparing salad', 'Putting in contact lenses', 'Putting on makeup', 'Putting on shoes', 'Rafting',
                    'Raking leaves', 'Removing curlers', 'Removing ice from car', 'Riding bumper cars', 'River tubing',
                    'Rock climbing', 'Rock-paper-scissors', 'Rollerblading', 'Roof shingle removal', 'Rope skipping',
                    'Running a marathon', 'Sailing', 'Scuba diving', 'Sharpening knives', 'Shaving', 'Shaving legs',
                    'Shot put', 'Shoveling snow', 'Shuffleboard', 'Skateboarding', 'Skiing', 'Slacklining',
                    'Smoking a cigarette', 'Smoking hookah', 'Snatch', 'Snow tubing', 'Snowboarding', 'Spinning',
                    'Spread mulch','Springboard diving', 'Starting a campfire', 'Sumo', 'Surfing', 'Swimming',
                    'Swinging at the playground', 'Table soccer','Tai chi', 'Tango', 'Tennis serve with ball bouncing',
                    'Throwing darts', 'Trimming branches or hedges', 'Triple jump', 'Tug of war', 'Tumbling', 'Using parallel bars',
                    'Using the balance beam', 'Using the monkey bar', 'Using the pommel horse', 'Using the rowing machine',
                    'Using uneven bars', 'Vacuuming floor', 'Volleyball', 'Wakeboarding', 'Walking the dog', 'Washing dishes',
                    'Washing face', 'Washing hands', 'Waterskiing', 'Waxing skis', 'Welding', 'Windsurfing', 'Wrapping presents',
                    'Zumba']
}
_DATASET_HYPER_PARAMS = {
    
    "THUMOS":{
        "dropout":0.7,
        "lr":1e-4,
        "weight_decay":5e-5,
        "frames_per_sec":25,
        "segment_frames_num":16,
        "sample_segments_num":750,
        
        "feature_dim":2048,
        "action_cls_num":len(_CLASS_NAME["THUMOS"]),
        "cls_threshold":0.25,
        "test_upgrade_scale":20,
        # "data_dir":"/DATA/W-TAL/THU14/",
        "data_dir":"./data/THUMOS14/",
        "test_gt_file":"./data/THUMOS14/gt.json",
        "tiou_thresholds":np.arange(0.1, 1.00, 0.10),
        "nms_thresh":0.55,
        
        "ins_topk_seg":8,
        "con_topk_seg":3,
        "bak_topk_seg":3,
        
        "loss_lamb_1":2e-3,
        "loss_lamb_2":5e-5,
        "loss_lamb_3":2e-4,
        
    },
    
    "ActivityNet":{
        "dropout":0.7,
        "lr":1e-4,
        "weight_decay":0.001,
        "frames_per_sec":25,
        "segment_frames_num":16,
        "sample_segments_num":75,
        
        "feature_dim":2048,
        "action_cls_num":len(_CLASS_NAME["ActivityNet"]),
        "cls_threshold":0.10,
        "test_upgrade_scale":20,
        "data_dir":"/mnt/d/Dataset/ActivityNet1-3_ACMfeat/features",
        # "data_dir":"/DATA/W-TAL/ActivityNet13/features",
        "test_gt_file":"/mnt/d/Dataset/ActivityNet1-3_ACMfeat/gt.json",
        "tiou_thresholds":np.arange(0.50, 1.00, 0.05),
        "nms_thresh":0.90,
        
        "ins_topk_seg":2,
        "con_topk_seg":10,
        "bak_topk_seg":10,
        
        "loss_lamb_1":5e-3,
        "loss_lamb_2":5e-5,
        "loss_lamb_3":0e-4,  
    },

    "HACS":{
        "dropout":0.7,
        "lr":1e-4,
        "weight_decay":0.001,
        "frames_per_sec":25,
        "segment_frames_num":16, #number of frames clips as input
        "sample_segments_num":75,
        
        "feature_dim":2048,
        "action_cls_num":len(_CLASS_NAME["ActivityNet"]),
        "cls_threshold":0.10,
        "test_upgrade_scale":20,
        "data_dir":"/mnt/d/Dataset/hacs_segments_features_I3D/hacs_segments_features",
        # "data_dir":"/DATA/W-TAL/ActivityNet13/features",
        "test_gt_file":"/mnt/d/Dataset/hacs_segments_features_I3D/HACS_segments_v1.1.1.json",
        "tiou_thresholds":np.arange(0.50, 1.00, 0.05),
        "nms_thresh":0.90,
        
        "ins_topk_seg":2,
        "con_topk_seg":10,
        "bak_topk_seg":10,
        
        "loss_lamb_1":5e-3,
        "loss_lamb_2":5e-5,
        "loss_lamb_3":0e-4,     
    },  

    "HACStoAct":{
        "dropout":0.7,
        "lr":1e-4,
        "weight_decay":0.001,
        "frames_per_sec":25,
        "segment_frames_num":16, #number of frames clips as input
        "sample_segments_num":75,
        
        "feature_dim":2048,
        "action_cls_num":len(_CLASS_NAME["ActivityNet"]),
        "cls_threshold":0.10,
        "test_upgrade_scale":20,
        "src_data_dir":"/mnt/d/Dataset/hacs_segments_features_I3D/features",
        "src_test_gt_file":"/mnt/d/Dataset/hacs_segments_features_I3D/gt.json",
        "tgt_data_dir":"/mnt/d/Dataset/ActivityNet1-3_ACMfeat/features",
        "tgt_test_gt_file":"/mnt/d/Dataset/ActivityNet1-3_ACMfeat/gt.json",
        "tiou_thresholds":np.arange(0.50, 1.00, 0.05),
        "nms_thresh":0.90,
        
        "ins_topk_seg":2,
        "con_topk_seg":10,
        "bak_topk_seg":10,
        
        "loss_lamb_1":5e-3,
        "loss_lamb_2":5e-5,
        "loss_lamb_3":0e-4,  
    }} 

import os
#import sys
#module_path = os.path.abspath(os.path.join('..'))
#if module_path not in sys.path:
#    sys.path.append(module_path)
os.chdir('/mnt/d/repos_fork/ACM-Net/')
def build_args(dataset=None):
    
    parser = argparse.ArgumentParser("This script is used for the weakly-supervised temporal aciton localization task.")
    
    parser.add_argument("--checkpoint", default=None, type=str)
    parser.add_argument("--start_epoch", default=0, type=int)
    parser.add_argument("--gpu", default='0', type=str)
    parser.add_argument("--num_workers", default=6, type=int)
    parser.add_argument("--dataset", default="THUMOS", type=str)
    parser.add_argument("--batch_size", default=16, type=int)
    parser.add_argument("--epochs", default=1000, type=int)
    
    parser.add_argument("--without_wandb", action="store_true")
    parser.add_argument("--test", action="store_true")
    args = parser.parse_args(args=[])
    
    if dataset is not None:
        args.dataset = dataset
    # Based on the selected dataset, we set dataset specific hyper-params. 
    if args.dataset == "HACStoAct":
        args.class_name_lst = _CLASS_NAME['ActivityNet']
    else:
        args.class_name_lst = _CLASS_NAME[args.dataset]
    args.action_cls_num = _DATASET_HYPER_PARAMS[args.dataset]["action_cls_num"]
    
    args.dropout = _DATASET_HYPER_PARAMS[args.dataset]["dropout"]
    args.lr = _DATASET_HYPER_PARAMS[args.dataset]["lr"]
    args.weight_decay = _DATASET_HYPER_PARAMS[args.dataset]["weight_decay"]
    
    args.frames_per_sec = _DATASET_HYPER_PARAMS[args.dataset]["frames_per_sec"]
    args.segment_frames_num = _DATASET_HYPER_PARAMS[args.dataset]["segment_frames_num"]
    args.sample_segments_num = _DATASET_HYPER_PARAMS[args.dataset]["sample_segments_num"]
    args.feature_dim =  _DATASET_HYPER_PARAMS[args.dataset]["feature_dim"]
    
    args.cls_threshold = _DATASET_HYPER_PARAMS[args.dataset]["cls_threshold"]
    args.tiou_thresholds = _DATASET_HYPER_PARAMS[args.dataset]["tiou_thresholds"]
    #args.test_gt_file_path = _DATASET_HYPER_PARAMS[args.dataset]["test_gt_file"]
    args.src_test_gt_file_path = _DATASET_HYPER_PARAMS[args.dataset]["src_test_gt_file"]
    args.tgt_test_gt_file_path = _DATASET_HYPER_PARAMS[args.dataset]["tgt_test_gt_file"]
    #args.data_dir = _DATASET_HYPER_PARAMS[args.dataset]["data_dir"]
    args.src_data_dir = _DATASET_HYPER_PARAMS[args.dataset]["src_data_dir"]
    args.tgt_data_dir = _DATASET_HYPER_PARAMS[args.dataset]["tgt_data_dir"]

    args.test_upgrade_scale = _DATASET_HYPER_PARAMS[args.dataset]["test_upgrade_scale"]
    args.nms_thresh = _DATASET_HYPER_PARAMS[args.dataset]["nms_thresh"]
    
    args.ins_topk_seg = _DATASET_HYPER_PARAMS[args.dataset]["ins_topk_seg"]
    args.con_topk_seg = _DATASET_HYPER_PARAMS[args.dataset]["con_topk_seg"]
    args.bak_topk_seg = _DATASET_HYPER_PARAMS[args.dataset]["bak_topk_seg"]
    
    args.loss_lamb_1  = _DATASET_HYPER_PARAMS[args.dataset]["loss_lamb_1"]
    args.loss_lamb_2  = _DATASET_HYPER_PARAMS[args.dataset]["loss_lamb_2"]
    args.loss_lamb_3  = _DATASET_HYPER_PARAMS[args.dataset]["loss_lamb_3"]
 
    return args
from torch.utils.data import DataLoader
#from config.model_config import build_args 
args = build_args(dataset="HACStoAct")
#src_dataset = build_src_dataset(args, phase="train", sample="random")
#src_dataloader = DataLoader(src_dataset, batch_size=args.batch_size, shuffle=True, 
#                                    num_workers=args.num_workers, drop_last=False)
def random_sample(input_feature, sample_len):
    
    input_len = input_feature.shape[0]
    assert sample_len > 0, "WRONG SAMPLE_LEN {}, THIS PARAM MUST BE GREATER THAN 0.".format(sample_len)
    
    if input_len < sample_len:
        sample_idxs = np.random.choice(input_len, sample_len, replace=True)
        sample_idxs = np.sort(sample_idxs)
    elif input_len > sample_len:
        sample_idxs = np.arange(sample_len) * input_len / sample_len
        for i in range(sample_len-1):
            sample_idxs[i] = np.random.choice(range(int(sample_idxs[i]), int(sample_idxs[i+1] + 1)))
        sample_idxs[-1] = np.random.choice(np.arange(sample_idxs[-2], input_len))
    else:
        sample_idxs = np.arange(input_len)
    
    return input_feature[sample_idxs.astype(int), :]
def iou_with_anchors(anchors_min, anchors_max, box_min, box_max):
    """Compute jaccard score between a box and the anchors.
    """
    # calculate the overlap proportion between the anchor and all bbox for supervise signal,
    # the length of the anchor is 0.01
    len_anchors = anchors_max - anchors_min
    int_xmin = np.maximum(anchors_min, box_min)
    int_xmax = np.minimum(anchors_max, box_max)
    inter_len = np.maximum(int_xmax - int_xmin, 0.)
    union_len = len_anchors - inter_len + box_max - box_min
    # print inter_len,union_len
    jaccard = np.divide(inter_len, union_len)
    return jaccard

def ioa_with_anchors(anchors_min, anchors_max, box_min, box_max):
    # calculate the overlap proportion between the anchor and all bbox for supervise signal,
    # the length of the anchor is 0.01
    len_anchors = anchors_max - anchors_min
    int_xmin = np.maximum(anchors_min, box_min)
    int_xmax = np.minimum(anchors_max, box_max)
    inter_len = np.maximum(int_xmax - int_xmin, 0.)
    scores = np.divide(inter_len, len_anchors)
    return scores

class SourceVidDataset(data.Dataset):

        
    def __init__(self, args, phase="train", sample="random"):
        
        self.temporal_scale = 75
        self.temporal_gap = 1. / self.temporal_scale

        self.phase = phase
        self.sample = sample
        self.data_dir = args.src_data_dir 
        self.sample_segments_num = args.sample_segments_num

        with open(os.path.join(self.data_dir, "gt.json")) as gt_f:
            self.gt_dict = json.load(gt_f)["database"]
            
        if self.phase == "train":
            self.feature_dir = os.path.join(self.data_dir, "train")
            self.data_list = list(open(os.path.join(self.data_dir, "split_train.txt")))
            self.data_list = [item.strip() for item in self.data_list]
        else:
            self.feature_dir = os.path.join(self.data_dir, "test")
            self.data_list = list(open(os.path.join(self.data_dir, "split_test.txt")))
            self.data_list = [item.strip() for item in self.data_list]
        
        self.class_name_lst = args.class_name_lst
        self.action_class_idx_dict = {action_cls:idx for idx, action_cls in enumerate(self.class_name_lst)}
        
        self.action_class_num = args.action_cls_num

        # Additional
        
        self.anchor_xmin = [self.temporal_gap * (i - 0.5) for i in range(self.temporal_scale)]
        self.anchor_xmax = [self.temporal_gap * (i + 0.5) for i in range(self.temporal_scale)]
        self.get_label()

    def __getitem__(self, idx):
        
        vid_name = self.data_list[idx]
        vid_label = self.label_dict[vid_name]
        vid_duration = self.gt_dict[vid_name]["duration"] #action duration in seconds
        con_vid_feature = np.load(os.path.join(self.feature_dir, vid_name+".npy"))
        
        vid_len = con_vid_feature.shape[0] #number of frames
        
        if self.sample == "random":
            con_vid_spd_feature = random_sample(con_vid_feature, self.sample_segments_num)
        else:
            con_vid_spd_feature = uniform_sample(con_vid_feature, self.sample_segments_num)
        
        con_vid_spd_feature = torch.as_tensor(con_vid_spd_feature.astype(np.float32)) #input_feature
        
        vid_label_t = torch.as_tensor(vid_label.astype(np.float32)) #video-level label
        
        #label_start, label_end, conf_score, bool_bkg_list = self._get_train_prop_label(idx,self.anchor_xmin,self.anchor_xmax)
        label_start, label_end, sample_bkg_idx, sample_act_idx = self._get_train_prop_label(idx,self.anchor_xmin,self.anchor_xmax)

        init_tensor = torch.zeros(self.sample_segments_num,2048)
        if sample_bkg_idx.size!=0:
            sample_bkg_feat = con_vid_spd_feature[sample_bkg_idx,:]
            init_tensor[:len(sample_bkg_idx),:]=sample_bkg_feat

        sample_bkg_feat = init_tensor


        init_tensor = torch.zeros(self.sample_segments_num,2048)
        if sample_act_idx.size!=0:
            sample_act_feat = con_vid_spd_feature[sample_act_idx,:]
            init_tensor[:len(sample_act_idx),:]=sample_act_feat
            
        sample_act_feat = init_tensor

        if self.phase == "train":
            #return con_vid_spd_feature, vid_label_t, conf_score, label_start, label_end, bool_bkg_list
            return con_vid_spd_feature, vid_label_t, label_start, label_end, sample_bkg_feat, sample_act_feat
        else:
            return vid_name, con_vid_spd_feature, vid_label_t, vid_len, vid_duration

    def get_label(self):
        
        self.label_dict = {}
        for item_name in self.data_list:
            
            #print(type(self.gt_dict))
            item_anns_list = self.gt_dict[item_name]["annotations"]
            item_label = np.zeros(self.action_class_num)
            for ann in item_anns_list:
                ann_label = ann["label"]
                item_label[self.action_class_idx_dict[ann_label]] = 1.0
            
            self.label_dict[item_name] = item_label
            #additional
            #video_second = self.gt_dict[item_name]["duration"]

    def __len__(self):
        return len(self.data_list)
    
    def _get_train_prop_label(self,index, anchor_xmin, anchor_xmax):
        video_name = self.data_list[index]
        video_info = self.gt_dict[video_name]
        video_labels = video_info['annotations']
        video_second = float(video_info['duration'])
        corrected_second = video_second
        temporal_scale = self.sample_segments_num
        temporal_gap = 1. / temporal_scale

        # change the measurement from second to percentage
        # gt_bbox = active window in percentage
        gt_bbox = []
        gt_iou_map = []
        for j in range(len(video_labels)):
            tmp_info = video_labels[j]
            tmp_start = max(min(1, tmp_info['segment'][0] / corrected_second), 0)
            tmp_end = max(min(1, tmp_info['segment'][1] / corrected_second), 0)
            gt_bbox.append([tmp_start, tmp_end])
            #tmp_gt_iou_map = iou_with_anchors(
            #    self.match_map[:, 0], self.match_map[:, 1], tmp_start, tmp_end)
            #tmp_gt_iou_map = np.reshape(tmp_gt_iou_map,
            #                            [self.temporal_scale, self.temporal_scale])
            #gt_iou_map.append(tmp_gt_iou_map)

        anchor_xmin = [temporal_gap * (i - 0.5) for i in range(temporal_scale)]
        anchor_xmax = [temporal_gap * (i + 0.5) for i in range(temporal_scale)]

        # generate R_s and R_e
        # starting and ending region
        gt_bbox = np.array(gt_bbox)
        gt_xmins = gt_bbox[:, 0]
        gt_xmaxs = gt_bbox[:, 1]
        gt_lens = gt_xmaxs - gt_xmins
        gt_len_small = 3 * self.temporal_gap  # np.maximum(self.temporal_gap, self.boundary_ratio * gt_lens)
        gt_start_bboxs = np.stack((gt_xmins - gt_len_small / 2, gt_xmins + gt_len_small / 2), axis=1)
        gt_end_bboxs = np.stack((gt_xmaxs - gt_len_small / 2, gt_xmaxs + gt_len_small / 2), axis=1)
        #####################################################################################################

        # calculate the ioa for all timestamp
        match_score_start = []
        for jdx in range(len(anchor_xmin)):
            match_score_start.append(np.max(
                ioa_with_anchors(anchor_xmin[jdx], anchor_xmax[jdx], gt_start_bboxs[:, 0], gt_start_bboxs[:, 1])))
    
        match_score_end = []
        for jdx in range(len(anchor_xmin)):
            match_score_end.append(np.max(
                ioa_with_anchors(anchor_xmin[jdx], anchor_xmax[jdx], gt_end_bboxs[:, 0], gt_end_bboxs[:, 1])))
        match_score_start = torch.Tensor(match_score_start)
        match_score_end = torch.Tensor(match_score_end)

        ### ==== Additional ====
        gt_start_nonact = gt_bbox[:,1]+0.5*temporal_gap # get end border of end region
        gt_end_nonact = gt_bbox[:,0]-0.5*temporal_gap # get start border of start region

        ls_start = gt_start_nonact[1:]
        ls_end = gt_end_nonact[:-1]

        gt_nonact_bbox = [(0,gt_end_nonact[0])]
        gt_nonact_bbox.extend(list(zip(ls_start,ls_end)))
        gt_nonact_bbox.append((gt_start_nonact[-1],1))


        anchor_xs = list(zip(anchor_xmin,anchor_xmax))
        #Assign backgroun label to each segment
        list_bkg_lbl=[]
        for anchor_now in anchor_xs:
            tem_bak = 0
            for bound in gt_nonact_bbox:
                if((max(0,anchor_now[0])>=bound[0]) and (min(anchor_now[1],1)<=bound[1])):
                    tem_bak = 1
                    break
            list_bkg_lbl.append(tem_bak)

        #bool_bkg_list = list(map(bool,list_tem_bak))
        list_bkg_lbl = torch.Tensor(list_bkg_lbl)
        ###
        #return match_score_start, match_score_end, gt_iou_map, bool_bkg_list
        src_bkg_list = list_bkg_lbl.nonzero()
        #if(src_bkg_list.sum()>0):    
        #   sample_bkgs_idx=np.random.choice(src_bkg_list.numpy().squeeze(), 
        #    np.minimum(10,len(src_bkg_list)), replace=False)
        #else:
        src_bkg_idx=src_bkg_list.numpy().squeeze()


        src_act_list = (list_bkg_lbl == 0).nonzero()
        #if(src_act_list.sum()>0):    
        #    sample_act_idx=np.random.choice(src_act_list.numpy().squeeze(), 
        #    np.minimum(10,len(src_act_list)), replace=False)
        #else:
        src_act_idx=src_act_list.numpy().squeeze()

        return match_score_start, match_score_end, src_bkg_idx, src_act_idx

def build_src_dataset(args, phase="train", sample="random"):
    
    return SourceVidDataset(args, phase, sample)

src_dataset = build_src_dataset(args, phase="train", sample="random") #random enable linear interpolation
src_dataloader = DataLoader(src_dataset, batch_size=args.batch_size, shuffle=True, 
                                    num_workers=args.num_workers, drop_last=False)
data_source_iter = iter(src_dataloader)
src_input_feature, src_vid_label_t, label_start, label_end, src_bkg_feat,src_act_feat = data_source_iter.next()
print(src_input_feature.shape)
print(torch.argmax(src_vid_label_t,axis=1))
print(label_start.shape)
print(src_bkg_feat.shape)
print(src_act_feat.shape)
num_segments = 75
r_easy = 5
k_easy = num_segments // r_easy
print(df2.shape[0])

df = torch.reshape(src_bkg_feat,(np.prod(np.array(src_bkg_feat.shape[0:2])),2048))
#print(df.shape)
df2 = df[torch.nonzero(df.abs().sum(axis=1)).squeeze(),:]
df2 = df2[np.random.choice(df2.shape[0],k_easy,replace=False),:]
#print(df2.shape)
#df2 = torch.reshape(df2[0:src_bkg_list.shape[0]*5,:],(src_bkg_list.shape[0],5,src_bkg_list.shape[2]))
print(df2.shape)
print(src_act_list[:,:5,:].shape)
torch.tensor([3,2,1]).nonzero()*-1+1
torch.tensor([3,2,1]).nonzero()
list_tem_bak<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13034525362070148, 'fraction_numerical': 0.029274486924291302, 'mean_word_length': 3.3556053811659194, 'pattern_counts': {'":': 95, '<': 19, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '393697', 'license': 'MIT', 'n_tokens_mistral': 11274, 'n_tokens_neox': 10587, 'n_words': 1754, 'path': 'notebook.ipynb', 'repo_name': 'madin162/ACM-Net', 'size': '29138'}