def make_ucf_video_names(root_path, list_path): video_names, labels = load_video_list(list_path) count = 0 final_labels = [] final_videos = [] for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) #print(video_names[i]) if not os.path.exists(video_path): print('%s does not exist!!!' % (video_path)) continue n_frames_file_path = os.path.join(video_path, 'n_frames') if not os.path.exists(n_frames_file_path): print('%s does not exist n_frames!!!' % (video_path)) continue n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: print('%s has 0 frame!!!' % (video_path)) continue final_videos.append(video_path) final_labels.append(labels[i]) count += 1 print('Load %d videos' % (count)) return final_videos, final_labels
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path + '.avi'): continue frame_path = os.path.join("/nfs_mount/datasets/UCF101/UCF-101/frames_fps25_256", video_names[i]) n_frames_root = "/nfs_mount/code/ananth/code/Verisk/3D-ResNets-PyTorch/datasets/nframes" n_frames_file_path = os.path.join(n_frames_root, video_names[i], 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames < 80: continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i].split('/')[1] } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 ## if n_samples_for_each_video == 1: ## this is to create 1 sample from each video that works for both training and test video ## need to change this to above if statement if actual evaluation is done on ucf101 ## n_samples_for_each_video is different for training set and val set if True: sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = int(max(1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1)))) else: step = sample_duration for j in range(1, n_frames, step): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) dataset.append(sample_j) return dataset, idx_to_class
def make_dataset( root_path, annotation_path, subset, n_samples_for_each_video, sample_duration ): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] for i in range(len(video_names)): if i % 1000 == 0: print("dataset loading [{}/{}]".format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): print("video path not exists: ", video_path) continue n_frames_file_path = os.path.join(video_path, "n_frames") n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue begin_t = 1 end_t = n_frames sample = { "video": video_path, "segment": [begin_t, end_t], "n_frames": n_frames, "video_id": video_names[i].split("/")[1], } if len(annotations) != 0: sample["label"] = class_to_idx[annotations[i]["label"]] else: sample["label"] = -1 if n_samples_for_each_video == 1: sample["frame_indices"] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = max( 1, math.ceil( (n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1) ), ) else: step = sample_duration for j in range(1, n_frames, step): sample_j = copy.deepcopy(sample) sample_j["frame_indices"] = list( range(j, min(n_frames + 1, j + sample_duration)) ) dataset.append(sample_j) return dataset, idx_to_class
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue audio_path = os.path.join(video_path, os.path.basename(video_path + '.wav')) if not os.path.exists(audio_path): continue n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i].split('/')[1] } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 if n_samples_for_each_video == 1: sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = max( 1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else: step = sample_duration for j in range(1, n_frames, int(step)): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) dataset.append(sample_j) return dataset, idx_to_class
def make_dataset(root_path, annotation_path, subset): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue n_frames_file_path = os.path.join(video_path, 'number_Frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue frame_indices_file_path = os.path.join(video_path, 'frames_name') frame_indices = load_list_file(frame_indices_file_path) sample = { 'video': video_path, 'n_frames': n_frames, 'frame_indices': frame_indices, 'video_id': video_names[i] } class_indexs = annotations[i]['label'] if not '-' in class_indexs: temp_label = np.zeros(len(class_to_idx)) temp_label[int(class_indexs)] = 1 #temp_label = int(class_indexs) #temp_map = np.zeros(len(class_to_idx)) #temp_map[int(class_indexs)] = 1 sample['label'] = temp_label #sample['map'] = temp_map else: temp = class_indexs.split('-') temp_label = np.zeros(len(class_to_idx)) for class_index in temp: temp_label[int(class_index)] = 1 #temp_label= int(temp[0]) #temp_map = np.zeros(len(class_to_idx)) #temp_map[int(temp[0])] = 1 sample['label'] = temp_label #sample['map'] = temp_map dataset.append(sample) return dataset
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): data = load_annotation_data(annotation_path) # json 파일 load video_names, annotations = get_video_names_and_annotations(data, subset) # video_names = [ 'label/'key(영상이름)', 'label/key' ... ], annotations = [ value['annotations'], ... ] class_to_idx = get_class_labels(data) # class_to_idx = { 'a' : 0 , 'b' : 1 , 'c' : 2 ...} idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name # idx_to_class = { 0 : 'a' , 1 : 'b' , 2 : 'c' ...} dataset = [] # 각 비디오 영상에 대한 샘플들 저장. for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) # root_path/label/videoname 그냥 폴더를 뜻함. if not os.path.exists(video_path): continue n_frames_file_path = os.path.join(video_path, 'n_frames') # n_frames 폴더에 접근 n_frames는 n_frames_ucf101_hmdb51.py로 생성. n_frames = int(load_value_file(n_frames_file_path)) # n_frames 는 영상 폴더 안에 있는 이미지 수. if n_frames <= 0: continue begin_t = 1 # 이미지의 시작 end_t = n_frames # 이미지의 끝 sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i].split('/')[1] # 영상 이름. } if len(annotations) != 0: sample['label'] = class_to_idx[int(annotations[i]['label'])] # calss_to_idx(해당 라벨값) => 해당 라벨의 인덱스 값 반환. hi 라벨의 인덱스는 0이면 0반환. else: sample['label'] = -1 if n_samples_for_each_video == 1: # 이것만 생각 하기. sample['frame_indices'] = list(range(1, n_frames + 1)) # num of input frame dataset.append(sample) else: if n_samples_for_each_video > 1: step = max(1, math.ceil((n_frames - 1 - sample_duration) / # sample_duration => input frame (n_samples_for_each_video - 1))) # ex) (50 - 1 - 16) / (3 - 1) = 16.5 ceil => 17 else: step = sample_duration for j in range(1, n_frames, step): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) # rnrksquffh frame dmf sksnsek. dataset.append(sample_j) return dataset, idx_to_class
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i][:-14].split('/')[1] } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 if n_samples_for_each_video == 1: sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = max(1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else: step = sample_duration for j in range(1, n_frames, step): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) dataset.append(sample_j) return dataset, idx_to_class
def make_dataset(root_path, annotation_path, subset): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] #test_file = open('/DATA/disk1/qzb/datasets/FCVID/test_files_' + subset + '.txt', 'w') for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue n_frames_file_path = os.path.join(video_path, 'number_Frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue frame_indices_file_path = os.path.join(video_path, 'frames_name') frame_indices = load_list_file(frame_indices_file_path) sample = { 'video': video_path, 'n_frames': n_frames, 'frame_indices': frame_indices, 'video_id': video_names[i] } #ipdb.set_trace() class_indexs = annotations[i]['label'] if not '-' in class_indexs: temp_label = np.zeros(len(class_to_idx)) temp_label[int(class_indexs)] = 1 sample['label'] = temp_label else: temp = class_indexs.split('-') temp_label = np.zeros(len(class_to_idx)) temp_label[int(temp[0])] = 1 #for class_index in temp: # temp_label[int(class_index)] = 1 sample['label'] = temp_label dataset.append(sample) #test_file.write(sample['video_id'] + ' ' + class_indexs + '\n') #test_file.close() return dataset
def make_untrimmed_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): data = load_annotation_data(annotation_path) video_names, _ = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] for i in range(len(video_names)): print("loading2 ...") if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue fps_file_path = os.path.join(video_path, 'fps') fps = load_value_file(fps_file_path) begin_t = 1 end_t = get_end_t(video_path) n_frames = end_t - begin_t sample = { 'video': video_path, 'segment': [begin_t, end_t], 'fps': fps, 'video_id': video_names[i][2:] } if n_samples_for_each_video >= 1: step = max( 1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else: step = sample_duration for j in range(begin_t, end_t, step): sample_j = copy.deepcopy(sample) frame_indices = list(range(j, j + sample_duration)) frame_indices = modify_frame_indices(sample_j['video'], frame_indices) if len(frame_indices) < 16: continue sample_j['frame_indices'] = frame_indices dataset.append(sample_j) return dataset, idx_to_class
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] for i in tqdm(range(len(video_names))): video_name = video_names[i].split('___')[0] video_path = os.path.join(root_path, video_name) assert os.path.exists(video_path), f"File {video_path} does not exist !" n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) assert n_frames > 0 begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_name # TODO check usage of video_id } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 if n_samples_for_each_video == 1: sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = max(1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else: step = sample_duration for j in range(1, n_frames, step): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) dataset.append(sample_j) return dataset, idx_to_class
def make_untrimmed_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): data = load_annotation_data(annotation_path) video_names, _ = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue fps_file_path = os.path.join(video_path, 'fps') fps = load_value_file(fps_file_path) begin_t = 1 end_t = get_end_t(video_path) n_frames = end_t - begin_t sample = { 'video': video_path, 'segment': [begin_t, end_t], 'fps': fps, 'video_id': video_names[i][2:] } if n_samples_for_each_video >= 1: step = max(1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else: step = sample_duration for j in range(begin_t, end_t, step): sample_j = copy.deepcopy(sample) frame_indices = list(range(j, j + sample_duration)) frame_indices = modify_frame_indices(sample_j['video'], frame_indices) if len(frame_indices) < 16: continue sample_j['frame_indices'] = frame_indices dataset.append(sample_j) return dataset, idx_to_class
def make_data_set( self, paths: dict, annotation_path: str, subset: str ): # アノテーションファイルからjsonやcsvオブジェクトを取得する entry = self.load_annotation_data(annotation_path) # 動画の名前とその属するクラスのそれぞれのリストを取得 video_names, annotations = self.get_video_names_and_annotations(entry, subset) # クラスをidへ割り振る class_to_idx = self.get_class_labels(entry) # idからクラスがわかるようにする idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name video_information = [] for i in range(len(video_names)): # 1000毎に経過報告をする if i % 1000 == 0: print('data_set loading [{}/{}]'.format(i, len(video_names))) full_paths = {} n_frames = 0 for path in paths: full_path = os.path.join(path, video_names[i]) assert os.path.exists(full_path), 'No such file :{}'.format(full_path) full_paths[full_path] = paths[path] n_frames_file_path = os.path.join(full_path, 'n_frames') if os.path.exists(n_frames_file_path): n_frames = int(load_value_file(n_frames_file_path)) assert n_frames > 0 video_info = { 'paths': full_paths, 'n_frames': n_frames, 'video_id': video_names[i].split('/')[1], 'label': class_to_idx[annotations[i]['label']], 'frame_indices': list(range(1, n_frames + 1)) } video_information.append(video_info) return video_information, idx_to_class
def _make_dataset(i, root_path, video_names, annotations, class_to_idx, n_samples_for_each_video, sample_duration): dataset = [] if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): return dataset n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: return dataset begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i].split('/')[0] } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 if n_samples_for_each_video == 1: sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = max( 1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else: step = sample_duration for j in range(1, n_frames, step): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) dataset.append(sample_j) return dataset
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): ##load the json file as data to read labels and id of an arbitrary video data = load_annotation_data(annotation_path) ##return a file that contains video names in the format of id/label and single list "annotation" of its labels video_names = get_video_names_and_annotations(data, subset) #give index to every category in kinetics-400 class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name i = 0 dataset = [] for key, label in range(video_names): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, label, key) if not os.path.exists(video_path): print("cannot find" + label + key) ##get the count of frames of the video n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i][:-14], 'label': class_to_idx[label] } sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) return dataset, idx_to_class
def make_dataset(video_names, labels, n_samples_for_each_video): dataset = [] for i in range(len(video_names)): video_path = video_names[i] n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'frame_indices': list(range(begin_t, end_t + 1)), 'label': labels[i] } for j in range(n_samples_for_each_video): sample_j = copy.deepcopy(sample) dataset.append(sample_j) return dataset
def make_dataset(yfcc_root, yfcc_results, target_classes, n_samples_for_each_video, topk): dataset = [] res = pd.read_pickle(yfcc_results) video_list = [] label_list = [] count = 0 for i, c in enumerate(target_classes): video_list.extend(res[c][:topk]) #print(res['segment'][c]) label_list.extend([i] * topk) for i in range(len(video_list)): video_path = os.path.join(yfcc_root, video_list[i][:-4]) if not os.path.exists(video_path): print("%s does not exist!!!!" % video_path) continue n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames == 0: print("%s n_frames=0!!!!" % video_path) continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'frame_indices': list(range(begin_t, end_t + 1)), 'label': label_list[i] } for j in range(n_samples_for_each_video): sample_j = copy.deepcopy(sample) dataset.append(sample_j) count = count + 1 print('Load %d yfcc videos' % (count)) return dataset
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): # 生成数据列表,列表中的元素为{'video':video_path, 'segment':[begin_t, end_t],\ # 'n_frames':n_frames, 'video_id':video_names, 'label':, 'frame_indices'} # 其中frmae_indices表示所选取的frame的索引值。 # sample_duration: 采样的间隔 data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i].split('/')[1] } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 if n_samples_for_each_video == 1: # 表示的意思应该是选取视频中所有帧的样本 sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = max( 1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else: step = sample_duration # because of optical flow, we modify the beginning is 2. for j in range(2, n_frames, step): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) dataset.append(sample_j) return dataset, idx_to_class
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): ''' dst_data: ---|labels: all labels ---|database: contains train_database and val_database. database: ---|key: avi_video_name ---|subset: training or validation ---|annotations: ---|label: class_name ''' # loading json data = load_annotation_data(annotation_path) # video_names is {label}/{video_name}, annotations is dict {'label', label} video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name # dataset, list, contains N samples, and eav # sample # ---|video: the path of video contains a lot of images # ---|segment: [1, n_frames] # ---|n_frames: number of frames # ---|video_id: avi_name # ---|label: class_id # ---|frame_indices: the indices of frames used in model dataset = [] for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) # the image in video_path is a single frame of the video. video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue # n_frames_file_path saves the number of frames in this video. n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i].split('/')[1] # video_name, and [0] is class_name } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 if n_samples_for_each_video == 1: sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = max( 1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else: step = sample_duration for j in range(2, n_frames, step): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) dataset.append(sample_j) # idx_to_class: indice to class name. return dataset, idx_to_class
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration, debug=True): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] debug_len = len(video_names) if debug: debug_len = 500 for i in tqdm(range(debug_len)): # 所有数据集 # for i in range(500): # 使用1000个作为测试 # if i % 1000 == 0: # print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i].split('/')[1] } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 if n_samples_for_each_video == 1: sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = int( max( 1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1)))) else: step = int(sample_duration) for j in range(1, n_frames, step): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) dataset.append(sample_j) # print('video_names.size-{}:'.format(subset), len(dataset)) # print(dataset[0]) # print(idx_to_class) return dataset, idx_to_class
def make_dataset(opt, annotation_path, subset, n_samples_for_each_video, sample_duration): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] total_number_of_clips = 0 gflop_per_clip = 8.5 for i in range(len(video_names)): #video_path = os.path.join(opt.video_path, video_names[i])+ ".mp4" video_path = os.path.join(opt.video_path,video_names[i]) + ".mp4" if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) j_path = os.path.join(opt.jpeg_path, video_names[i]) n_frames_file_path = os.path.join(j_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue i_path = os.path.join(opt.iframe_path, video_names[i]) m_path = os.path.join(opt.mv_path, video_names[i]) r_path = os.path.join(opt.residual_path, video_names[i]) if not os.path.exists(opt.jpeg_path): continue begin_t = 1 end_t = n_frames sample = { 'video' : video_path, 'jpeg_path': j_path, 'iframe_path': i_path, 'mv_path': m_path, 'residual_path': r_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i].split('/')[1] } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 #dataset.append(sample) if n_samples_for_each_video == 1: sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = max(1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else:###teesting if subset == 'training': step = int(sample_duration/1) else: step = int(sample_duration/2) for j in range(0, n_frames, step): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) dataset.append(sample_j) total_number_of_clips += 1 if n_samples_for_each_video == 0: num_of_videos = len(video_names) avg_clips_per_video = round(total_number_of_clips/num_of_videos,2) tot_gflops=round(gflop_per_clip*avg_clips_per_video,2) print("Number of videos:",num_of_videos) print("Number of clips:",total_number_of_clips) print("Avarage amount of clips per video:",avg_clips_per_video) print("Number of GFlos for each video in avarage will be,(true to mfnet only):",tot_gflops) return dataset, idx_to_class
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name if os.path.exists(os.path.join(os.path.dirname(annotation_path), 'cache', 'dataset_{}.json'.format(subset))): with open(os.path.join(os.path.dirname(annotation_path), 'cache', 'dataset_{}.json'.format(subset)), 'r') as f: dataset = json.load(f) else: dataset = [] for i in range(len(video_names)): if subset == 'training': video_path = os.path.join(root_path, 'train', video_names[i]) else: video_path = os.path.join(root_path, 'val', video_names[i]) if not os.path.exists(video_path): continue if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i][:-14].split('/')[1] } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 if n_samples_for_each_video == 1: sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = max(1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else: step = int(sample_duration / 2) for j in range(1, n_frames, step): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) dataset.append(sample_j) if not os.path.exists(os.path.join(os.path.dirname(annotation_path), 'cache')): os.makedirs(os.path.join(os.path.dirname(annotation_path), 'cache')) with open(os.path.join(os.path.dirname(annotation_path), 'cache', 'dataset_{}.json'.format(subset)), 'w') as f: json.dump(dataset, f) return dataset, idx_to_class
def make_dataset2(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): import pickle cache_path = os.path.join( root_path, '../cache', '%s-%s-%s-%s.pkl' % (os.path.basename(annotation_path).replace( '.json', ''), subset, n_samples_for_each_video, sample_duration)) if os.path.isfile(cache_path): print('pickle load from', cache_path) with open(cache_path, 'rb') as f: cache = pickle.load(f) dataset = cache['dataset'] idx_to_class = cache['idx_to_class'] else: print('pickle dump to', cache_path) data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations( data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i].split('/')[1] } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 if n_samples_for_each_video == 1: sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = max( 1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else: step = sample_duration for j in range(1, n_frames, step): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) dataset.append(sample_j) cache = { 'dataset': dataset, 'idx_to_class': idx_to_class, } with open(cache_path, 'wb') as f: pickle.dump(cache, f, protocol=pickle.HIGHEST_PROTOCOL) return dataset, idx_to_class
def make_untrimmed_dataset(root_path, scores_dump_path, annotation_path, subset, n_samples_for_each_video, window_size, window_stride): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] videos = {} for i in range(len(video_names)): if i != 0 and i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) # break # early exit annotation = annotations[i] label = int(annotation['label']) begin_t = annotation['start_frame'] end_t = annotation['end_frame'] clip = { 'label': label, 'segment': [begin_t, end_t], 'video_id': video_names[i] } video = get_video_from_clip(video_names[i]) if not video in videos: videos[video] = [] videos[video].append(clip) i = 0 # dump_dir = os.path.join(root_path, scores_dump_path) for video in videos: video_path = os.path.join(root_path, video) if not os.path.exists(video_path) or os.path.exists(scores_dump_path + "/" + video): print("Skipping video ", video_path, scores_dump_path) continue n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue window_start = 1 window_end = window_start + window_size idx = 1 while window_end < n_frames: sample = { 'video': video_path, 'segment': [window_start, window_end], 'video_id': video, 'window_id': idx} frame_indices = list(range(window_start, window_end)) frame_indices = modify_frame_indices( sample['video'], frame_indices) sample['frame_indices'] = frame_indices # sample['label'] = -1 # Not computed yet sample['label'] = get_untrimmed_label( videos[video], window_start, window_end) if len(frame_indices) == window_size: dataset.append(sample) window_start += window_stride window_end = window_start + window_size idx += 1 if i % 10 == 0: print('dataset loading [{}/{}]'.format(i, len(videos))) i += 1 print("Make untrimmed dataset") return dataset, idx_to_class
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration, sample_stride): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i].split('/')[1], 'frame_indices': [] } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 clip_length = sample_duration * sample_stride # print('='*80) if n_samples_for_each_video <= 0: n_samples_for_cur_video = math.ceil( n_frames / clip_length) # sample times for current video else: n_samples_for_cur_video = n_samples_for_each_video if n_samples_for_cur_video == 1: step = n_frames else: step = max(1, (n_frames - clip_length) / (n_samples_for_cur_video - 1)) for i in range(n_samples_for_cur_video): sample_i = copy.deepcopy(sample) if step < clip_length: random_offset = random.randint(0, sample_stride - 1) else: random_offset = random.randint( 0, math.floor(step - clip_length + sample_stride - 1)) start_frame = math.floor(i * step + random_offset) for j in range(sample_duration): sample_i['frame_indices'].append(start_frame % n_frames + 1) start_frame += sample_stride dataset.append(sample_i) # print('sample:', video_path, n_frames, sample_i['frame_indices'][0], len(sample_i['frame_indices']) ) print('total samples:', len(dataset)) return dataset, idx_to_class
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name class_map_save(class_to_idx) # deb_image_loader = get_default_image_loader() ## debug ###################### dataset = [] for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 80: print('Skipping too short video {}'.format(video_path)) continue if n_frames > 300: print('Skipping too long video {}'.format(video_path)) continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, # 'video_id': video_names[i][:-14].split('/')[1] 'video_id': video_names[i].split('/')[1] } ## debug begin ############################## # print(video_path) # video_loader(video_path,[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],deb_image_loader) # print(".") ## debug end ########################## if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 if n_samples_for_each_video == 1: sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = max(1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else: step = sample_duration for j in range(1, n_frames, step): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) dataset.append(sample_j) return dataset, idx_to_class
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue fps_file_path = os.path.join(video_path, 'fps') fps = load_value_file(fps_file_path) # if subset == 'testing': # print(annotations) for kk, vv in annotations[i].items(): for annotation in vv: # print(annotation) # print(fps) begin_t = math.ceil(annotation[0] * fps) end_t = math.ceil(annotation[1] * fps) if begin_t == 0: begin_t = 1 n_frames = end_t - begin_t sample = { 'video': video_path, 'segment': [begin_t, end_t], 'fps': fps, 'video_id': video_names[i][2:] } # if len(annotations) != 0: # sample['label'] = class_to_idx[annotation['label']] # else: # sample['label'] = -1 sample['label'] = class_to_idx[kk] if n_samples_for_each_video == 1: frame_indices = list(range(begin_t, end_t)) frame_indices = modify_frame_indices(sample['video'], frame_indices) if len(frame_indices) < 16: continue sample['frame_indices'] = frame_indices dataset.append(sample) else: if n_samples_for_each_video > 1: step = max(1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else: step = sample_duration for j in range(begin_t, end_t, step): sample_j = copy.deepcopy(sample) frame_indices = list(range(j, j + sample_duration)) frame_indices = modify_frame_indices( sample_j['video'], frame_indices) if len(frame_indices) < 16: continue sample_j['frame_indices'] = frame_indices dataset.append(sample_j) return dataset, idx_to_class
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration, n_test_clips_for_each_video, n_test_crops_for_each_video): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue n_frames_file_path = os.path.join(video_path, 'n_frames') n_frames = int(load_value_file(n_frames_file_path)) if n_frames <= 0: continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, 'video_id': video_names[i].split('/')[1] } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 if n_samples_for_each_video == 1: # for train and validation sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: # if n_samples_for_each_video > 1: # step = max(1, # math.ceil((n_frames - 1 - sample_duration) / # (n_samples_for_each_video - 1))) # else: # step = sample_duration # for j in range(1, n_frames, step): # sample_j = copy.deepcopy(sample) # sample_j['frame_indices'] = list( # range(j, min(n_frames + 1, j + sample_duration))) # dataset.append(sample_j) if n_samples_for_each_video > 1: # for validation only for j in range(n_samples_for_each_video): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample_j) else: # for test only, option n_val_samples = 0 for j in range(n_test_clips_for_each_video): for k in range(n_test_crops_for_each_video): step = n_frames / n_test_clips_for_each_video start_index = int(j * step) sample_j = copy.deepcopy(sample) # frame_indices represents the test clip info of the video. # frame_indices = [clip_index, n_test_clips, n_frames], clip_index is start from 0. sample_j['frame_indices'] = [j, n_test_clips_for_each_video, n_frames] # crop_indices represents the test crop info of the video. # crop_indices = [crop_index, n_test_crops], crop_index is start from 0. sample_j["crop_indices"] = [k, n_test_crops_for_each_video] # index of test segment, from 0 to (n_test_clips * n_test_crops - 1). sample_j["test_segment_index"] = j * n_test_crops_for_each_video + k dataset.append(sample_j) print("{} videos loaded ...".format(str(len(dataset)))) return dataset, idx_to_class
def make_dataset(root_path, annotation_path, subset, n_samples_for_each_video, sample_duration): data = load_annotation_data(annotation_path) video_names, annotations = get_video_names_and_annotations(data, subset) class_to_idx = get_class_labels(data) idx_to_class = {} for name, label in class_to_idx.items(): idx_to_class[label] = name dataset = [] print("[INFO]: Jester Dataset - " + subset + " is loading...") for i in range(len(video_names)): if i % 1000 == 0: print('dataset loading [{}/{}]'.format(i, len(video_names))) video_path = os.path.join(root_path, video_names[i]) if not os.path.exists(video_path): continue n_frames_file_path = os.path.join( video_path, 'n_frames') # reads number of farames under each video path n_frames = int(load_value_file(n_frames_file_path)) # n_frames = len(glob.glob(os.path.join(video_path, '*.jpg'))) # in case there is no n_frames file uncomment this line if n_frames <= 0: continue begin_t = 1 end_t = n_frames sample = { 'video': video_path, 'segment': [begin_t, end_t], 'n_frames': n_frames, #'video_id': video_names[i].split('/')[1] 'video_id': video_names[i] } if len(annotations) != 0: sample['label'] = class_to_idx[annotations[i]['label']] else: sample['label'] = -1 if n_samples_for_each_video == 1: sample['frame_indices'] = list(range(1, n_frames + 1)) dataset.append(sample) else: if n_samples_for_each_video > 1: step = max( 1, math.ceil((n_frames - 1 - sample_duration) / (n_samples_for_each_video - 1))) else: step = sample_duration for j in range(1, n_frames, step): sample_j = copy.deepcopy(sample) sample_j['frame_indices'] = list( range(j, min(n_frames + 1, j + sample_duration))) dataset.append(sample_j) return dataset, idx_to_class