├── README.MD ├── Videos_to_frames_multiprocessing.py ├── anomaly_feature.yaml ├── clip2segment.py ├── dataset ├── Datasetloader.py ├── __pycache__ │ └── Datasetloader.cpython-35.pyc └── write_data_label_txt_new.py ├── dataset_creater.py ├── dataset_creater_C3D.py ├── feature_extract.py └── model_feature └── shanghaitech └── i3d └── feature.txt /README.MD: -------------------------------------------------------------------------------- 1 | # Introduction 2 | This repository is for extracting C3D or I3D features from videos. 3 | 4 | ## Requirements 5 | * Python 3 6 | * CUDA 7 | * numpy 8 | * tqdm 9 | * [PyTorch](http://pytorch.org/) (1.2) 10 | * [torchvision](http://pytorch.org/) 11 | Recommend: the environment can be established by run 12 | 13 | ``` 14 | conda env create -f anomaly_feature.yaml 15 | ``` 16 | 17 | ## Video preprocessing 18 | 1. Using Videos_to_frames_multiprocessing.py to generate frames from videos. 19 | 20 | ps: if you want to extract I3D feature, it is recommend that using denseflow (https://github.com/open-mmlab/denseflow) to generate RGB and Optical_flow images. 21 | 22 | ## Input list generation 23 | 1. Using the ./dataset/write_data_label_txt_new.py 24 | 25 | generate the index file for running feature_extract.py. 26 | 27 | ## 28 | Download the model.zip in (link: https://pan.baidu.com/s/1g4XGLqRoRJhQwIGtHif2jg password: dzqm), and unpress it to the root. 29 | 30 | ## Feature extraction 31 | 1. Using feature_extract.py to generate I3D or C3D features. 32 | 33 | Using script 34 | 35 | ``` 36 | python feature_extract.py --dataset shanghaitech --modelName i3d --snapshot ./model/i3d/i3d_model_weight/model_kinetics_rgb.pth --datamodal rgb 37 | ``` 38 | 39 | to extract i3d rgb features. 40 | 41 | Using script 42 | 43 | ``` 44 | python feature_extract.py --dataset shanghaitech --modelName i3d --snapshot ./model/i3d/i3d_model_weight/model_kinetics_flow.pth --datamodal flow 45 | ``` 46 | 47 | to extract i3d flow features. 48 | 49 | Using script 50 | 51 | ``` 52 | python feature_extract.py --dataset shanghaitech --modelName c3d --snapshot ./model/c3d/c3d.pickle --datamodal rgb 53 | ``` 54 | 55 | to extract c3d features. 56 | 57 | Using dataset_creater.py to generate final feature file. 58 | -------------------------------------------------------------------------------- /Videos_to_frames_multiprocessing.py: -------------------------------------------------------------------------------- 1 | """ 2 | The Videos_to_frames_multiprocessing.py is used to convert video to video frames by using multipocessing and cv2. 3 | The structures of video_datasets are shown below: 4 | Shanghaitech: 5 | |------------|videos: 6 | |------------------|Train: 7 | |------------------------|: video1 8 | |------------------------|: video2 9 | |------------------------|: video3 10 | |------------------------|: video4 ... 11 | 12 | , 13 | UCF_Crime: 14 | |------------|videos: 15 | |------------------|Class1: 16 | |------------------------|: video1 17 | |------------------------|: video2 18 | |------------------------|: video3 19 | |------------------------|: video4 ... 20 | |------------------|Class2: 21 | 22 | The structures of Frame are shown below: 23 | 24 | Shanghaitech: 25 | |------------|frames: 26 | |------------------|Train: 27 | |------------------------|: video1 28 | |------------------------|: video2 29 | |------------------------|: video3 30 | |------------------------|: video4 ... 31 | 32 | , 33 | UCF_Crime: 34 | |------------|frames: 35 | |------------------|Class1: 36 | |------------------------|: video1 37 | |------------------------|: video2 38 | |------------------------|: video3 39 | |------------------------|: video4 ... 40 | |------------------|Class2: 41 | """ 42 | 43 | 44 | from multiprocessing import Pool 45 | import os 46 | import platform 47 | import cv2 48 | import glob 49 | import time 50 | from tqdm import tqdm 51 | 52 | 53 | def readVideolist2(Train,Test): 54 | """ 55 | For Avenue, ped1, ped2 56 | :param Train: 57 | :param Test: 58 | :return: 59 | """ 60 | Trainvideos = os.listdir(Train) 61 | Testvideos = os.listdir(Test) 62 | TrainvideoList={} 63 | TestvideoList={} 64 | for video in Trainvideos: 65 | TrainvideoPath = os.path.join(Train, video) 66 | TrainvideoList[video] = TrainvideoPath 67 | for video in Testvideos: 68 | TestvideoPath = os.path.join(Test, video) 69 | TestvideoList[video] = TestvideoPath 70 | return TrainvideoList, TestvideoList 71 | 72 | def readVideolist3(Train): 73 | """ 74 | For shanghaitech 75 | :param Train: 76 | :param Test: 77 | :return: 78 | """ 79 | Trainvideos = os.listdir(Train) 80 | TrainvideoList={} 81 | for video in Trainvideos: 82 | TrainvideoPath = os.path.join(Train, video) 83 | TrainvideoList[video] = TrainvideoPath 84 | return TrainvideoList 85 | 86 | def readVideolist_UCF_Crime(Path): 87 | """ 88 | For UCF_Crime 89 | :param Train: 90 | :param Test: 91 | :return: 92 | """ 93 | Class = os.listdir(Path) 94 | videoList={} 95 | for a_class in Class: 96 | videoDir=os.path.join(Path, a_class) 97 | videoNames=os.listdir(videoDir) 98 | for video in videoNames: 99 | videoPath=os.path.join(videoDir,video) 100 | videoList[a_class+video]=videoPath 101 | return videoList 102 | 103 | 104 | def Video2frame(videofilelist=None): 105 | # for each_video in videofilelist: 106 | each_video = videofilelist 107 | if each_video.rfind('UCF') != -1: 108 | zfill_number = 6 109 | else: 110 | zfill_number = 5 111 | current_os = platform.architecture() 112 | start = time.clock() 113 | # print(videofilelist) 114 | if current_os[1] == 'WindowsPE': 115 | each_videolist = each_video.split('\\') 116 | each_video_path, each_video_name = os.path.split(each_video) 117 | else: 118 | each_videolist = each_video.split('/') 119 | each_video_path, each_video_name = os.path.split(each_video) 120 | each_video_name, _ = each_video_name.split('.') 121 | each_video_name = str(each_video_name) 122 | 123 | target_path = os.path.join(each_video_path.replace('Videos','frames'),each_video_name) 124 | # target_path = target_path.replace('windows4t','windowswan') 125 | if os.path.exists(target_path): 126 | print('Frames of Video_{} already existed'.format(each_video_name)) 127 | else: 128 | framedirsavepath = target_path 129 | if os.path.exists(framedirsavepath) == 0: 130 | os.makedirs(framedirsavepath) 131 | cap = cv2.VideoCapture(each_video) 132 | frame_count = 1 133 | success = True 134 | while(success): 135 | success, frame = cap.read() 136 | # print ('Read a new frame: ', success) 137 | if success: 138 | cv2.imwrite(framedirsavepath + '/'+"img_{}.jpg" .format(str(frame_count).zfill(zfill_number)), frame) 139 | # print ('frame_num:',frame_count) 140 | frame_count = frame_count + 1 141 | else: 142 | print('Preparing for next video') 143 | end = time.clock() 144 | t = end - start 145 | print("Consuming {} seconds for video {}:".format(t, each_video_name)) 146 | break 147 | cap.release() 148 | 149 | 150 | if __name__ == "__main__": 151 | current_os = platform.architecture() 152 | # TrainSetpath2='/home/tu-wan/windowswan/dataset/Avenue/Videos/training_videos/' 153 | # TestSetPath2 ='/home/tu-wan/windowswan/dataset/Avenue/Videos/testing_videos/' 154 | 155 | # UCFSetpath='/home/tu-wan/windowswan/dataset/UCF_Crime/Videos/' 156 | TrainSetpath='/home/tu-wan/windowswan/dataset/LV/Videos' 157 | 158 | #######It should notices that the readVideolist* should be selected for dataset. 159 | TrainvideoList = readVideolist3(TrainSetpath) 160 | train_values = list(TrainvideoList.values()) 161 | 162 | # TrainFrameList, TestFrameList = readVideolist(TrainFramepath, TestFramePath) 163 | # train_values = list(TrainvideoList.values()) 164 | 165 | with Pool(processes=4) as p: 166 | max_ = len(train_values) 167 | with tqdm(total=max_) as pbar: 168 | for i, _ in tqdm(enumerate(p.imap_unordered(Video2frame, train_values))): 169 | pbar.update() 170 | 171 | -------------------------------------------------------------------------------- /anomaly_feature.yaml: -------------------------------------------------------------------------------- 1 | name: anomaly_icme 2 | channels: 3 | - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch 4 | - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge 5 | - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ 6 | - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ 7 | - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ 8 | - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main 9 | - http://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free 10 | - defaults 11 | - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/fastai/ 12 | - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/ 13 | - http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/ 14 | dependencies: 15 | - _libgcc_mutex=0.1=conda_forge 16 | - _openmp_mutex=4.5=1_gnu 17 | - backports=1.0=py_2 18 | - backports.weakref=1.0rc1=py35_0 19 | - blas=1.1=openblas 20 | - bleach=1.5.0=py35_0 21 | - bzip2=1.0.8=h516909a_2 22 | - ca-certificates=2020.6.20=hecda079_0 23 | - certifi=2016.9.26=py35_0 24 | - cffi=1.11.5=py35h5e8e0c9_1 25 | - cudatoolkit=10.0.130=0 26 | - cudnn=7.6.5=cuda10.0_0 27 | - freetype=2.7=1 28 | - html5lib=0.9999999=py35_0 29 | - intel-openmp=2020.1=217 30 | - jpeg=9d=h516909a_0 31 | - libedit=3.1.20191231=h46ee950_1 32 | - libffi=3.2.1=he1b5a44_1007 33 | - libgcc-ng=9.3.0=h24d8f2e_14 34 | - libgfortran=3.0.0=1 35 | - libgomp=9.3.0=h24d8f2e_14 36 | - libpng=1.6.37=hed695b0_1 37 | - libprotobuf=3.5.2=hd28b015_1 38 | - libstdcxx-ng=9.3.0=hdf63c60_14 39 | - libtiff=4.0.10=hc3755c2_1005 40 | - lz4-c=1.9.2=he1b5a44_1 41 | - markdown=2.6.9=py35_0 42 | - mkl=2020.1=217 43 | - ncurses=6.2=he1b5a44_1 44 | - ninja=1.10.0=hc9558a2_0 45 | - numpy=1.14.6=py35_blas_openblashd3ea46f_200 46 | - olefile=0.46=py_0 47 | - openblas=0.2.20=8 48 | - openssl=1.0.2u=h516909a_0 49 | - pillow=4.0.0=py35_2 50 | - pip=20.2.1=py_0 51 | - protobuf=3.5.2=py35hd28b015_0 52 | - pycparser=2.20=pyh9f0ad1d_2 53 | - python=3.5.5=h5001a0f_2 54 | - python-dateutil=2.8.1=py_0 55 | - pytorch=1.2.0=py3.5_cuda10.0.130_cudnn7.6.2_0 56 | - readline=7.0=hf8c457e_1001 57 | - scipy=1.1.0=py35_blas_openblash7943236_201 58 | - setuptools=36.4.0=py35_1 59 | - six=1.15.0=pyh9f0ad1d_0 60 | - sqlite=3.32.3=h62c20be_0 61 | - tensorflow=1.3.0=0 62 | - tensorflow-base=1.3.0=py35h79a3156_1 63 | - tensorflow-tensorboard=1.5.1=py35hf484d3e_1 64 | - tk=8.6.10=hed695b0_0 65 | - torchvision=0.1.8=py35_0 66 | - tqdm=4.48.2=pyh9f0ad1d_0 67 | - webencodings=0.5.1=py_1 68 | - werkzeug=1.0.1=pyh9f0ad1d_0 69 | - wheel=0.34.2=py_1 70 | - xz=5.2.5=h516909a_1 71 | - zlib=1.2.11=h516909a_1006 72 | - zstd=1.4.5=h6597ccf_2 73 | - pip: 74 | - seaborn==0.9.1 75 | prefix: /home/tu-wan/windows2t/anaconda3/envs/anomaly_icme 76 | -------------------------------------------------------------------------------- /clip2segment.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn.functional as f 3 | import os 4 | import glob 5 | import numpy as np 6 | from tqdm import tqdm 7 | clip_features = glob.glob('/home/tu-wan/windowswan/dataset/UCSDPed2/features_video/c3d/fc6/rgb/*/*') 8 | 9 | for clip_feature in tqdm(clip_features): 10 | tmp_f = np.load(clip_feature) 11 | tmp_f = torch.from_numpy(tmp_f).unsqueeze(0) 12 | feature_d = tmp_f.shape[-1] 13 | tar_f = f.adaptive_avg_pool2d(tmp_f, (32,feature_d)) 14 | tar_f = tar_f.squeeze().numpy() 15 | if os.path.exists(os.path.split(clip_feature.replace('c3d', 'c3d_segments'))[0]) == 0: 16 | os.makedirs(os.path.split(clip_feature.replace('c3d', 'c3d_segments'))[0]) 17 | np.save(file=clip_feature.replace('c3d', 'c3d_segments'),arr=tar_f) 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /dataset/Datasetloader.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append("..") 3 | import numpy as np 4 | import cv2 5 | import torch 6 | from torch.utils.data import Dataset, DataLoader 7 | from torchvision import transforms 8 | 9 | def video_to_tensor(pic): 10 | """Convert a ``numpy.ndarray`` to tensor. 11 | Converts a numpy.ndarray (T x H x W x C) 12 | to a torch.FloatTensor of shape (C x T x H x W) 13 | 14 | Args: 15 | pic (numpy.ndarray): Video to be converted to tensor. 16 | Returns: 17 | Tensor: Converted video. 18 | """ 19 | return torch.from_numpy(pic.transpose([3, 0, 1, 2])) 20 | 21 | class trainDataset(Dataset): 22 | 23 | def __init__(self, list_file, GT_file,transform=None, cliplen=16, datamodal ='rgb', args=None): 24 | ''' 25 | Args: 26 | GT_Dir: (str) path to Ground True dir 27 | list_file: (str) path to index file. 28 | ''' 29 | 30 | #read list 31 | with open(list_file) as f: 32 | self.filelist = f.readlines() 33 | self.num_samples = len(self.filelist) 34 | with open(GT_file) as f: 35 | self.labellist = f.readlines() 36 | self.transform = transform 37 | self.cliplen = cliplen 38 | self.datamodal = datamodal 39 | self.args = args 40 | if self.datamodal == 'rgb': 41 | self.channel = 3 42 | elif self.datamodal == 'flow' or self.datamodal == 'flownet': 43 | self.channel = 2 44 | else: 45 | raise ('datamodal should be rgb or flow') 46 | 47 | def __getitem__(self, index): 48 | '''shanghaitech_reconstruct.py 49 | 50 | :param idx: (int) image index 51 | :return: 52 | 53 | ''' 54 | files = [] 55 | fileinputs = self.filelist[index] 56 | fileinputs_s = fileinputs.split(' ') 57 | filedata= [] 58 | for i,fileinput in enumerate(fileinputs_s): 59 | fileinput = fileinput.replace('\n', '') 60 | if self.datamodal == 'rgb': 61 | files.append(fileinput) 62 | # img = jpeg.JPEG(fileinput).decode()[:, :, [2, 1, 0]] 63 | img = cv2.cvtColor(cv2.imread(fileinput),cv2.COLOR_BGR2RGB) 64 | elif self.datamodal == 'flow' or self.datamodal == 'flownet': 65 | file_X = fileinput.split(':')[0] 66 | file_Y = fileinput.split(':')[0] 67 | files.append(fileinput) 68 | # flow_x = jpeg.JPEG(file_X).decode()[:, :, 0] 69 | flow_x = cv2.imread(file_X)[:, :, 0] 70 | # flow_y = jpeg.JPEG(file_Y).decode()[:, :, 0] 71 | flow_y = cv2.imread(file_Y)[:, :, 0] 72 | img = np.asarray([flow_x, flow_y]).transpose([1,2,0]) 73 | # h, w, c = img.shape 74 | # if w < 224 or h < 224: 75 | # d = 224. - min(w, h) 76 | # sc = 1 + d / min(w, h) 77 | # img = cv2.resize(img, dsize=(0, 0), fx=sc, fy=sc) 78 | 79 | if self.transform is not None: 80 | img = self.transform(img) 81 | else: 82 | if self.args.modelName == 'i3d': 83 | img = cv2.resize(img, (224, 224)) #i3d 84 | else: 85 | img = img - img.mean() 86 | img = cv2.resize(img, (112, 112)) #c3d 87 | # img = (img / img) * 2 - 1 88 | filedata.append(img) 89 | 90 | return video_to_tensor(np.asarray(filedata, dtype=np.float32)), files 91 | 92 | def __len__(self): 93 | return self.num_samples 94 | 95 | # class testDataset(Dataset): 96 | # 97 | # def __init__(self, list_file, GT_file, transform=None,cliplen=16, 98 | # mean = './dataset/shanghaitech/unary/normal_mean_frame_227.npy'): 99 | # ''' 100 | # Args: 101 | # GT_Dir: (str) path to Ground True dir 102 | # list_file: (str) path to index file. 103 | # 104 | # ''' 105 | # 106 | # #read list 107 | # with open(list_file) as f: 108 | # self.filelist = f.readlines() 109 | # self.num_samples = len(self.filelist) 110 | # with open(GT_file) as f: 111 | # self.labellist = f.readlines() 112 | # self.transform = transform 113 | # self.cliplen = cliplen 114 | # self.cliplen = cliplen 115 | # if mean: 116 | # self.mean = torch.from_numpy(np.load(mean).astype('float32')) 117 | # def __getitem__(self,index): 118 | # ''' 119 | # 120 | # :param idx: (int) image index 121 | # :return: 122 | # 123 | # ''' 124 | # 125 | # files = [] 126 | # fileinputs = self.filelist[index] 127 | # labelinputs = self.labellist[index] 128 | # 129 | # fileinputs_s = fileinputs.split(' ') 130 | # labelinputs = labelinputs.split(' ') 131 | # cliplabel, clip_ano_score = self.getcliplabel(labelinputs) 132 | # 133 | # filedata=torch.empty((self.cliplen, 227, 227)) 134 | # for i, fileinput in enumerate(fileinputs_s): 135 | # fileinput = fileinput.replace('\n','') 136 | # files.append(fileinput) 137 | # # img = cv2.imread(fileinput)[:,:,0] 138 | # img = jpeg.JPEG(fileinput).decode()[:,:,0] 139 | # img = img.astype('float32') 140 | # # img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) 141 | # # img = cv2.resize(img, (227, 227), interpolation=cv2.INTER_CUBIC) 142 | # img = img[np.newaxis, :, :] 143 | # if self.transform is not None: 144 | # img = self.transform(img) 145 | # else: 146 | # img = img / 255. 147 | # img = torch.from_numpy(img) 148 | # if self.mean is not None: 149 | # img = img - self.mean 150 | # filedata[i] = img 151 | # 152 | # return filedata, torch.from_numpy(clip_ano_score), files 153 | # 154 | # 155 | # # TODO(wanboyang) all numbers in the matrix cliplabel are same 156 | # def getcliplabel(self, labels): 157 | # cliplabel=np.empty(1,np.dtype('int16')) 158 | # frame_ano_scores=np.empty(len(labels),np.dtype('float32')) 159 | # for i,label in enumerate(labels): 160 | # frame_ano_score, framelabel=label.split(':') 161 | # frame_ano_scores[i]=frame_ano_score 162 | # cliplabel[0]=framelabel 163 | # 164 | # return cliplabel, frame_ano_scores 165 | # 166 | # def __len__(self): 167 | # return self.num_samples 168 | 169 | 170 | 171 | 172 | 173 | 174 | def txttans(origin_filelist,origin_labellist,processed_filelist,processed_labellist,numJoints,model='train',framework='reconstruction'): 175 | 176 | if model == 'train': 177 | with open(origin_filelist,'r') as l: 178 | with open(origin_labellist, 'r') as lb: 179 | lists = l.readlines() 180 | labellists = lb.readlines() 181 | with open(processed_filelist, 'w') as t: 182 | with open(processed_labellist, 'w') as lbt: 183 | temp_file = [] 184 | temp_lab = [] 185 | video_name = None 186 | framenum = 0 187 | for file, label in zip(lists, labellists): 188 | file=file.replace('\n','') 189 | label=label.replace('\n','') 190 | file_split = file.split('/') 191 | # if framework == 'reconstruction' and file_split[-2].find('_a_') != -1: 192 | # continue 193 | if video_name: 194 | if video_name != file_split[-2]: 195 | video_name = file_split[-2] 196 | if len(temp_file) != numJoints: 197 | temp_file.clear() 198 | temp_lab.clear() 199 | framenum = 0 200 | temp_file.append(file) 201 | temp_lab.append(label) 202 | elif int(file_split[-1].split('.')[0].split('_')[-1]) < framenum: 203 | temp_file.clear() 204 | temp_lab.clear() 205 | framenum = 0 206 | temp_file.append(file) 207 | temp_lab.append(label) 208 | else: 209 | temp_file.append(file) 210 | temp_lab.append(label) 211 | else: 212 | video_name = file_split[-2] 213 | temp_file.append(file) 214 | temp_lab.append(label) 215 | if len(temp_file) == numJoints: 216 | frame_ano_label, clip_sence_class, clip_ano_label = frame2clip_anolabel(temp_lab) 217 | t.write(' '.join(temp_file)+'\n') 218 | lbt.write(' '.join(frame_ano_label)+' ') 219 | lbt.write(''.join(clip_sence_class) + ' ') 220 | lbt.write(''.join(clip_ano_label) + '\n') 221 | framenum = int(temp_file[-1].split('/')[-1].split('.')[0].split('_')[-1]) 222 | temp_file.clear() 223 | temp_lab.clear() 224 | else: 225 | with open(origin_filelist, 'r') as l: 226 | with open(origin_labellist, 'r') as lb: 227 | lists = l.readlines() 228 | labellists = lb.readlines() 229 | with open(processed_filelist, 'w') as t: 230 | with open(processed_labellist, 'w') as lbt: 231 | 232 | temp_file = [] 233 | temp_lab = [] 234 | video_name = None 235 | i = 0 236 | while i < len(labellists): 237 | file = lists[i].replace('\n', '') 238 | label = labellists[i].replace('\n', '') 239 | file_split = file.split('/') 240 | if video_name: 241 | if video_name != file_split[-2]: 242 | video_name = file_split[-2] 243 | if len(temp_file) != numJoints: 244 | temp_file.clear() 245 | temp_lab.clear() 246 | temp_file.append(file) 247 | temp_lab.append(label) 248 | i += 1 249 | else: 250 | temp_file.append(file) 251 | temp_lab.append(label) 252 | i += 1 253 | else: 254 | video_name = file_split[-2] 255 | temp_file.append(file) 256 | temp_lab.append(label) 257 | i += 1 258 | if len(temp_file) == numJoints: 259 | t.write(' '.join(temp_file) + '\n') 260 | lbt.write(' '.join(temp_lab) + '\n') 261 | temp_file.clear() 262 | temp_lab.clear() 263 | i += 1 264 | i = i - numJoints 265 | 266 | 267 | 268 | def frame2clip_anolabel(framelabels=None): 269 | 270 | frame_ano_label = [] 271 | clip_sence_class = [] 272 | for i, label in enumerate(framelabels): 273 | frame_ano_score, framelabel = label.split(':') 274 | frame_ano_label.append(frame_ano_score) 275 | clip_sence_class.append(framelabel) 276 | 277 | if np.count_nonzero(np.asarray(frame_ano_label,dtype='int')) >= len(frame_ano_label) - np.count_nonzero(np.asarray(frame_ano_label,dtype='int')): 278 | clip_ano_label = 1 279 | else: 280 | clip_ano_label = 0 281 | 282 | return frame_ano_label,clip_sence_class[0],str(clip_ano_label) 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | if __name__ == '__main__': 291 | datamodal = 'rgb' 292 | origin_filelist = './LAD2000/all/{}_list.txt'.format(datamodal) 293 | origin_labellist = './LAD2000/all/label.txt' 294 | trainfile_list = './LAD2000/all/{}_list_numJoints.txt'.format(datamodal) 295 | trainlabel_list = './LAD2000/all/trainlabel_numJoints.txt' 296 | numJoints = 16 297 | txttans(origin_filelist=origin_filelist, 298 | origin_labellist=origin_labellist, 299 | processed_filelist=trainfile_list , 300 | processed_labellist=trainlabel_list, 301 | numJoints=numJoints, 302 | model='train', 303 | framework=' ') 304 | # txttans(origin_filelist=origin_testfile_list, 305 | # origin_labellist=origin_testlabel_list, 306 | # processed_filelist=testfile_list , 307 | # processed_labellist=testlabel_list, 308 | # numJoints=numJoints, 309 | # model='test') 310 | trans = transforms.Compose(transforms=[ 311 | transforms.ToTensor() 312 | # transforms.Normalize(mean=[]) 313 | ]) 314 | train_dataset = trainDataset(list_file=trainfile_list, 315 | GT_file=trainlabel_list,transform=None, cliplen=numJoints,datamodal ='flow') 316 | # 317 | train_loader = DataLoader(dataset=train_dataset,batch_size=2,pin_memory=True, 318 | num_workers=5,shuffle=False) 319 | # 320 | for epoch in range(2): 321 | for i, data in enumerate(train_loader): 322 | # 将数据从 train_loader 中读出来,一次读取的样本数是32个 323 | filedata, fileinputs= data 324 | # 325 | # # 将这些数据转换成Variable类型 326 | # inputs = Variable(imagedata) 327 | 328 | # 接下来就是跑模型的环节了,我们这里使用print来代替 329 | print("epoch:", epoch, "的第" , i, "个inputs", filedata.shape, fileinputs) 330 | # 331 | -------------------------------------------------------------------------------- /dataset/__pycache__/Datasetloader.cpython-35.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wanboyang/anomly_feature.pytorch/59a1e757d5514f89ebea50c1b4c79f732ebeebc1/dataset/__pycache__/Datasetloader.cpython-35.pyc -------------------------------------------------------------------------------- /dataset/write_data_label_txt_new.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | 3 | import numpy as np 4 | import os 5 | import glob 6 | 7 | def main(original_video_dict,dataset,dataset_mode): 8 | if dataset == 'UCF_Crime': 9 | zfill_number = 6 10 | else: 11 | zfill_number = 5 # ped1, ped2, shanghaitech or avenue 12 | if dataset_mode == 'i3d': 13 | if os.path.exists('./{}/{}'.format(dataset, dataset_mode)) == 0: 14 | os.makedirs('./{}/{}'.format(dataset, dataset_mode)) 15 | with open(file='./{}/{}/rgb_list.txt'.format(dataset, dataset_mode),mode='w',encoding='utf-8') as f: 16 | with open(file='./{}/{}/flow_list.txt'.format(dataset, dataset_mode), mode='w', encoding='utf-8') as flo: 17 | with open(file='./{}/{}/label.txt'.format(dataset,dataset_mode),mode='w',encoding='utf-8') as t: 18 | for k, v in original_video_dict.items(): 19 | frames = os.listdir(os.path.join(v)) 20 | frames_number = int(len(frames)/3) 21 | framegt = np.zeros(shape=(frames_number), dtype='int8') 22 | classgt = np.zeros(shape=(frames_number), dtype='int8') 23 | for i in range(1, frames_number + 1, 1): 24 | f.write(os.path.join(v, 'img_'+str(i).zfill(zfill_number)+ '.jpg'+'\n')) 25 | flo.write(os.path.join(v, 'flow_x_'+str(i).zfill(zfill_number)+ '.jpg'+':')) 26 | flo.write(os.path.join(v, 'flow_y_'+str(i).zfill(zfill_number) + '.jpg' + '\n')) 27 | t.write(str(framegt[i-1])+':'+str(classgt[i-1])+'\n') 28 | else: 29 | if os.path.exists('./{}/{}'.format(dataset, dataset_mode)) == 0: 30 | os.makedirs('./{}/{}'.format(dataset, dataset_mode)) 31 | with open(file='./{}/{}/rgb_list.txt'.format(dataset, dataset_mode),mode='w',encoding='utf-8') as f: 32 | with open(file='./{}/{}/label.txt'.format(dataset,dataset_mode),mode='w',encoding='utf-8') as t: 33 | for k, v in original_video_dict.items(): 34 | frames = os.listdir(os.path.join(v)) 35 | frames_number = int(len(frames)/3) 36 | framegt = np.zeros(shape=(frames_number), dtype='int8') 37 | classgt = np.zeros(shape=(frames_number), dtype='int8') 38 | for i in range(1, frames_number + 1, 1): 39 | f.write(os.path.join(v, 'img_'+str(i).zfill(zfill_number)+ '.jpg'+'\n')) 40 | t.write(str(framegt[i-1])+':'+str(classgt[i-1])+'\n') 41 | 42 | if __name__ == '__main__': 43 | data_root = '/home/tu-wan/windowswan/dataset' 44 | dataset = 'shanghaitech' 45 | dataset_mode = 'i3d' # i3d or c3d 46 | original_video = [] 47 | original_video_dict = {} 48 | 49 | if dataset == 'UCF_Crime': 50 | videopaths = glob.glob(os.path.join(data_root, dataset, 'denseflow', '*/*')) 51 | else: 52 | videopaths = glob.glob(os.path.join(data_root, dataset, 'denseflow', '*')) #ped1, ped2, shanghaitech or avenue 53 | videonames = [] 54 | for videopath in videopaths: 55 | videoname = videopath.split('/')[-1] 56 | videonames.append(videoname) 57 | original_video_dict[videoname] = os.path.join(videopath) 58 | if os.path.exists('./{}/{}'.format(dataset, dataset_mode)) == 0: 59 | os.makedirs('./{}/{}'.format(dataset, dataset_mode)) 60 | np.savetxt('./{}/{}/videoname.txt'.format(dataset, dataset_mode), np.asarray(videonames).reshape(-1),fmt='%s') 61 | main(original_video_dict=original_video_dict, 62 | dataset=dataset, 63 | dataset_mode=dataset_mode) 64 | 65 | 66 | -------------------------------------------------------------------------------- /dataset_creater.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | import glob 4 | from multiprocessing import Pool 5 | from tqdm import tqdm 6 | import argparse 7 | 8 | def combine_feature(video): 9 | combine_fea = [] 10 | rgb_fea = [] 11 | flow_fea = [] 12 | rgb_features = glob.glob(os.path.join('/home/tu-wan/windowswan/dataset/{}//features//{}'.format(dataset, pretrain_model),'rgb',video, '*.npy')) 13 | rgb_features.sort(key=lambda x: int(x[-9:-4])) 14 | flow_features = glob.glob(os.path.join('/home/tu-wan/windowswan/dataset/{}//features//{}'.format(dataset, pretrain_model), 'flow', video, '*.npy')) 15 | flow_features.sort(key=lambda x: int(x[-9:-4])) 16 | for i in range(len(rgb_features)): 17 | rgb_fea_np = np.load(rgb_features[i]) 18 | flow_fea_np = np.load(flow_features[i]) 19 | rgb_fea.append(rgb_fea_np) 20 | flow_fea.append(flow_fea_np) 21 | feature = np.hstack((rgb_fea_np,flow_fea_np)) 22 | combine_fea.append(feature) 23 | combine_fea = np.asarray(combine_fea) 24 | rgb_fea = np.asarray(rgb_fea) 25 | flow_fea = np.asarray(flow_fea) 26 | save_path = os.path.join('/home/tu-wan/windowswan/dataset/{}//features_video//{}'.format(dataset, pretrain_model), 'combine_flownet',video) 27 | if os.path.exists(save_path) == 0: 28 | os.makedirs(save_path) 29 | if os.path.exists(os.path.join('/home/tu-wan/windowswan/dataset/{}//features_video//{}'.format(dataset, pretrain_model), 'rgb', video)) == 0: 30 | os.makedirs(os.path.join('/home/tu-wan/windowswan/dataset/{}//features_video//{}'.format(dataset, pretrain_model), 'rgb', video)) 31 | if os.path.exists(os.path.join('/home/tu-wan/windowswan/dataset/{}//features_video//{}'.format(dataset, pretrain_model), 'flownet', video)) == 0: 32 | os.makedirs(os.path.join('/home/tu-wan/windowswan/dataset/{}//features_video//{}'.format(dataset, pretrain_model), 'flownet', video)) 33 | np.save(file=os.path.join(save_path, 'feature.npy'),arr=combine_fea) 34 | np.save(file=os.path.join('/home/tu-wan/windowswan/dataset/{}//features_video//{}'.format(dataset, pretrain_model), 'rgb', video, 'feature.npy'), arr=rgb_fea) 35 | np.save(file=os.path.join('/home/tu-wan/windowswan/dataset/{}//features_video//{}'.format(dataset, pretrain_model), 'flownet', video, 'feature.npy'), arr=flow_fea) 36 | 37 | 38 | 39 | if __name__ == "__main__": 40 | 41 | parser = argparse.ArgumentParser() 42 | parser.add_argument("--dataset", help='Name of dataset', default='shanghaitech', type=str) 43 | args = parser.parse_args() 44 | pretrain_model = 'i3d' 45 | dataset = args.dataset 46 | feature_dir = '/home/tu-wan/windowswan/dataset/{}/features/{}'.format(dataset, pretrain_model) 47 | # # # 48 | '''combine the rgb and flow feature on every video''' 49 | rgb_feature_dir = os.path.join(feature_dir, 'rgb') 50 | flow_feature_dir = os.path.join(feature_dir, 'flow') 51 | videos = os.listdir(rgb_feature_dir) 52 | with Pool(processes=6) as p: 53 | max_ = len(videos) 54 | with tqdm(total=max_) as pbar: 55 | for i, _ in tqdm(enumerate(p.imap_unordered(combine_feature, videos))): 56 | pbar.update() 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /dataset_creater_C3D.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import os 3 | import glob 4 | from multiprocessing import Pool 5 | from tqdm import tqdm 6 | import json 7 | import pandas as pd 8 | import pickle 9 | import argparse 10 | 11 | def combine_feature(video): 12 | layers = ['pool5'] 13 | for layer in layers: 14 | rgb_fea = [] 15 | rgb_features = glob.glob(os.path.join('/home/tu-wan/windowswan/dataset/{}//features//{}'.format(dataset, pretrain_model), layer, 'rgb',video, '*.npy')) 16 | rgb_features.sort(key=lambda x: int(x.split('_')[-1].split('.')[0].zfill(8))) 17 | for i in range(len(rgb_features)): 18 | rgb_fea_np = np.load(rgb_features[i]) 19 | rgb_fea.append(rgb_fea_np) 20 | rgb_fea = np.asarray(rgb_fea) 21 | if os.path.exists(os.path.join('/home/tu-wan/windowswan/dataset/{}//features_video//{}'.format(dataset, pretrain_model), layer, 'rgb', video)) == 0: 22 | os.makedirs(os.path.join('/home/tu-wan/windowswan/dataset/{}//features_video//{}'.format(dataset, pretrain_model), layer, 'rgb', video)) 23 | np.save(file=os.path.join('/home/tu-wan/windowswan/dataset/{}//features_video//{}'.format(dataset, pretrain_model), layer, 'rgb', video, 'feature.npy'), arr=rgb_fea) 24 | 25 | 26 | 27 | 28 | if __name__ == "__main__": 29 | parser = argparse.ArgumentParser() 30 | parser.add_argument("--dataset", help='Name of dataset', default='shanghaitech', type=str) 31 | args = parser.parse_args() 32 | pretrain_model = 'c3d' 33 | dataset = args.dataset 34 | feature_dir = '/home/tu-wan/windowswan/dataset/{}/features/{}'.format(dataset, pretrain_model) 35 | # layers = os.listdir(feature_dir) 36 | layer = 'pool5' 37 | '''combine the rgb and flow feature on every video''' 38 | rgb_feature_dir = os.path.join(feature_dir, layer, 'rgb') 39 | # flow_feature_dir = os.path.join(feature_dir, layer, 'flow') 40 | videos = os.listdir(rgb_feature_dir) 41 | with Pool(processes=6) as p: 42 | max_ = len(videos) 43 | with tqdm(total=max_) as pbar: 44 | for i, _ in tqdm(enumerate(p.imap_unordered(combine_feature, videos))): 45 | pbar.update() 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /feature_extract.py: -------------------------------------------------------------------------------- 1 | import timeit 2 | import os 3 | from tqdm import tqdm 4 | import torch 5 | from torch.utils.data import DataLoader 6 | import numpy as np 7 | import argparse 8 | from dataset.Datasetloader import trainDataset, txttans 9 | from model.i3d.src.i3dpt import I3D 10 | from model.C3D_model import C3D 11 | import sys 12 | 13 | 14 | 15 | def feature(data_path, dataset, snapshot, modelName, dataloader, datamodal='rgb', fc_layer='fc6'): 16 | """ 17 | Args: 18 | num_classes (int): Number of classes in the data 19 | num_epochs (int, optional): Number of epochs to train for. 20 | """ 21 | ######################build model##################################### 22 | train_params = [] 23 | if modelName == 'c3d': 24 | model = C3D(nb_classes=487) 25 | train_params.append({'params': model.parameters()}) 26 | elif modelName == 'i3d': 27 | if datamodal == 'rgb': 28 | model = I3D(400, modality='rgb', dropout_prob=0, name='inception') 29 | else: 30 | model = I3D(400, modality='flow', dropout_prob=0, name='inception') 31 | else: 32 | print('We only implemented C3D or i3d models.') 33 | raise NotImplementedError 34 | ######################load weigths##################################### 35 | if snapshot: 36 | model.load_state_dict({k.replace('module.', ''): v for k, v in torch.load(snapshot).items()}) 37 | if modelName == 'c3d': 38 | if fc_layer == 'fc6': 39 | feature_layer = 6 40 | elif fc_layer == 'fc7': 41 | feature_layer = 7 42 | elif fc_layer == 'pool5': 43 | feature_layer = 5 44 | feature_save_dir = os.path.join(data_path, 'dataset', dataset, 'features', modelName, fc_layer, datamodal) 45 | elif modelName == 'i3d': 46 | feature_layer = 5 47 | feature_save_dir = os.path.join(data_path, 'dataset', dataset, 'features', modelName, datamodal) 48 | 49 | model.to(device) 50 | 51 | if os.path.exists(feature_save_dir) == 0: 52 | os.makedirs(feature_save_dir) 53 | ######################log##################################### 54 | if os.path.exists(os.path.join('./model_feature/', dataset, modelName)) == 0: 55 | os.makedirs(os.path.join('./model_feature/', dataset, modelName)) 56 | with open(file=os.path.join('./model_feature/', dataset, modelName,'feature.txt'), mode='a+') as f: 57 | f.write("dataset:{} ".format(dataset)+ '\n') 58 | f.write("snapshot:{} ".format(snapshot) + '\n') 59 | f.write("savedir:{} ".format(feature_save_dir) + '\n') 60 | f.write("========================================== " + '\n') 61 | 62 | model_feature(model=model,dataloader=dataloader, feature_save_dir=feature_save_dir,datamodal=datamodal,dataset=dataset, feature_layer=feature_layer) 63 | 64 | 65 | 66 | def model_feature(model, dataloader, feature_save_dir, datamodal, dataset, feature_layer=None): 67 | model.eval() 68 | start_time = timeit.default_timer() 69 | if dataset=='shanghaitech': 70 | video_name_po = -2 71 | else: 72 | video_name_po = -3 73 | for img, fileinputs in tqdm(dataloader): 74 | # move inputs and labels to the device the training is taking place on 75 | inputs = img.to(device) 76 | # fileinputs = np.asarray(fileinputs) 77 | fileinputs = np.asarray(fileinputs).transpose((1, 0)) 78 | with torch.no_grad(): 79 | features, _ = model(inputs, feature_layer=feature_layer) 80 | features = features.view(features.size(0), -1) 81 | features = features.data.cpu().numpy() 82 | for (fileinput, feature) in zip(fileinputs, features): 83 | if datamodal == 'flow' or datamodal == 'flownet': 84 | video_name = fileinput[0].split(':')[0].split('/')[video_name_po] 85 | start_frame = fileinput[0].split(':')[0].split('/')[-1].split('.')[0].split('_')[-1] 86 | end_frame = fileinput[-1].split(':')[0].split('/')[-1].split('.')[0].split('_')[-1] 87 | save_path = os.path.join(feature_save_dir, video_name, start_frame + '_' +end_frame + '.npy') 88 | else: 89 | video_name = fileinput[0].split('/')[video_name_po] 90 | start_frame = fileinput[0].split('/')[-1].split('.')[0].split('_')[-1] 91 | end_frame = fileinput[-1].split('/')[-1].split('.')[0].split('_')[-1] 92 | save_path = os.path.join(feature_save_dir, video_name, start_frame + '_' +end_frame + '.npy') 93 | 94 | if os.path.exists(os.path.join(feature_save_dir, video_name)) == 0: 95 | os.makedirs(os.path.join(feature_save_dir, video_name)) 96 | np.save(save_path, feature) 97 | stop_time = timeit.default_timer() 98 | print("Execution time: " + str(stop_time - start_time) + "\n") 99 | 100 | 101 | 102 | 103 | 104 | if __name__ == "__main__": 105 | 106 | device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu") 107 | print("Device being used:", device) 108 | parser = argparse.ArgumentParser() 109 | parser.add_argument("--snapshot", help='path of testing model_weight', default='./model/i3d/i3d_model_weight/model_kinetics_rgb.pth', type=str) 110 | parser.add_argument("--datamodal", help='rgb or flow', default='rgb', type=str) 111 | parser.add_argument("--dataset", help='Name of dataset', default='shanghaitech', type=str) 112 | parser.add_argument("--modelName", help='Name of model', default='i3d', type=str) 113 | parser.add_argument("--fc_layer", help='layer of feature extraction', default='fc6', type=str) 114 | args = parser.parse_args() 115 | snapshot = args.snapshot 116 | Dataset = args.dataset 117 | datamodal = args.datamodal 118 | data_path = '/home/tu-wan/windowswan' 119 | origin_filelist = './dataset/{}/{}/{}_list.txt'.format(Dataset,args.modelName, datamodal) 120 | origin_labellist = './dataset/{}/{}/label.txt'.format(Dataset,args.modelName) 121 | trainfile_list = './dataset/{}/{}/{}_list_numJoints.txt'.format(Dataset,args.modelName, datamodal) 122 | trainlabel_list = './dataset/{}/{}/trainlabel_numJoints.txt'.format(Dataset,args.modelName) 123 | 124 | numJoints = 16 125 | txttans(origin_filelist=origin_filelist, 126 | origin_labellist=origin_labellist, 127 | processed_filelist=trainfile_list , 128 | processed_labellist=trainlabel_list, 129 | numJoints=numJoints, 130 | model='train', 131 | framework=' ') 132 | train_dataset = trainDataset(list_file=trainfile_list, 133 | GT_file=trainlabel_list, 134 | transform=None, 135 | cliplen=numJoints, 136 | datamodal=datamodal, 137 | args=args) 138 | 139 | train_dataloader = DataLoader(dataset=train_dataset,batch_size=24, pin_memory=True, 140 | num_workers=5,shuffle=False) 141 | 142 | modelName = args.modelName # Options: C3D or I3D 143 | 144 | feature(data_path=data_path, 145 | dataset=Dataset, 146 | snapshot=snapshot, 147 | modelName = modelName, 148 | dataloader= train_dataloader, 149 | datamodal= datamodal, 150 | fc_layer=args.fc_layer) 151 | -------------------------------------------------------------------------------- /model_feature/shanghaitech/i3d/feature.txt: -------------------------------------------------------------------------------- 1 | dataset:shanghaitech 2 | snapshot:./model/i3d/i3d_model_weight/model_kinetics_rgb.pth 3 | savedir:/home/tu-wan/windowswan/dataset/shanghaitech/features/i3d/rgb 4 | ========================================== 5 | --------------------------------------------------------------------------------