├── .demo_Kinetics.py.swo ├── .gitignore ├── README.md ├── __pycache__ └── videoDataset.cpython-36.pyc ├── checkpoints ├── __pycache__ │ └── check.cpython-36.pyc └── check.py ├── demo_Kinetics.py ├── gen-crop-videos ├── clipsListFile.pkl ├── videoDataset.py └── waste │ ├── filter_None.py │ ├── read_data.py │ ├── videoDataset.py │ └── videoDataset.pyc ├── lib ├── .network.py.swo ├── .network.py.swp ├── .network50.py.swp ├── __pycache__ │ ├── network.cpython-36.pyc │ ├── network.cpython-37.pyc │ └── non_local_embedded_gaussian.cpython-37.pyc ├── backup │ ├── network.py │ ├── non_local.py │ └── non_local_simple_version.py ├── network.py ├── network.py.back ├── network2d.py ├── non_local_concatenation.py ├── non_local_dot_product.py ├── non_local_embedded_gaussian.py └── non_local_gaussian.py ├── videoDataset.py └── waste ├── __pycache__ └── videoDataset.cpython-36.pyc ├── caffe.list ├── caffe.lst ├── caffe_params.lst ├── cropped_clipsListFile.pkl ├── demo_MNIST.py.back ├── pytorch.lst ├── pytorch_params.lst ├── read_data.py └── videoDataset.pyc /.demo_Kinetics.py.swo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/17Skye17/Non-local-Neural-Networks-Pytorch/9a2ddbafad69f2ca79764d3c29469d8f84f882af/.demo_Kinetics.py.swo -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | gen-crop-videos/filter_clipsListFile.hdf5 2 | gen-crop-videos/waste/clipsListFile.hdf5 3 | .git/objects/pack/tmp_pack_jFYsQc 4 | .git/objects/1b/tmp_obj_5HfbvU 5 | checkpoints/new-ckpts/resnet-50-kinetics.pth 6 | waste/sample-clipsListFile.hdf5 7 | waste/filtered_sample.hdf5 8 | waste/new-clipsListFile.hdf5 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # This is the non-official implementation for Non-local Neural Networks (Pytorch Version) 2 | 3 | ## Introduction 4 | 5 | Test baseline: resnet-50 experiment on Kinetics dataset.(no non-local block yet) 6 | 7 | ## Experiment Records 8 | 9 | - Kinetics dataset prepared 10 | - Model prepared 11 | - Model parameters converter prepared (caffe2 to pytorch) 12 | 13 | ## Problems 14 | 15 | The test result is low, check: 16 | - Model parameter loading. (OK) 17 | - Kinetics dataset RGB range [-1,1]. (OK) 18 | 19 | Well, this confuses me, I still can't figure out what is wrong except for parameter loading and RGB range of Kinetics. The official caffe2 code hurts my brain at sometime. 20 | If anyone could offer some help, I'd be grateful. : ) 21 | 22 | ## Ref: 23 | - [https://github.com/AlexHex7/Non-local_pytorch](https://github.com/AlexHex7/Non-local_pytorch) 24 | - [https://github.com/facebookresearch/video-nonlocal-net](https://github.com/facebookresearch/video-nonlocal-net) 25 | -------------------------------------------------------------------------------- /__pycache__/videoDataset.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/17Skye17/Non-local-Neural-Networks-Pytorch/9a2ddbafad69f2ca79764d3c29469d8f84f882af/__pycache__/videoDataset.cpython-36.pyc -------------------------------------------------------------------------------- /checkpoints/__pycache__/check.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/17Skye17/Non-local-Neural-Networks-Pytorch/9a2ddbafad69f2ca79764d3c29469d8f84f882af/checkpoints/__pycache__/check.cpython-36.pyc -------------------------------------------------------------------------------- /checkpoints/check.py: -------------------------------------------------------------------------------- 1 | import pickle 2 | import torch 3 | from torch.autograd import Variable 4 | from lib.network import ResNet 5 | import copy 6 | 7 | model_path='/home/skye/DeepLearningPJ/video-nonlocal-net/checkpoints/c2d_baseline_32x2_IN_pretrain_400k.pkl' 8 | 9 | model_weights = pickle.load(open('/home/skye/DeepLearningPJ/video-nonlocal-net/checkpoints/c2d_baseline_32x2_IN_pretrain_400k.pkl','rb'),encoding='latin1') 10 | caffe_data = model_weights['blobs'] 11 | #keys = model_weights['blobs'].keys() 12 | #print(len(keys)) 13 | 14 | def argparser(caffe_params,pytorch_params): 15 | assert len(caffe_params) == len(pytorch_params),'number of caffe params={} vs pytorch params={}'.format(len(caffe_params),len(pytorch_params)) 16 | name_map = {} 17 | new_map = {} 18 | name_map['layer1'] = 'res2' 19 | name_map['layer2'] = 'res3' 20 | name_map['layer3'] = 'res4' 21 | name_map['layer4'] = 'res5' 22 | name_map['downsample'] = 'branch1' 23 | name_map['running_mean'] = 'rm' 24 | name_map['running_var'] = 'riv' 25 | 26 | new_map['fc.weight'] = 'pred_w' 27 | new_map['fc.bias'] = 'pred_b' 28 | new_map['conv1.weight'] = 'conv1_w' 29 | new_map['bn1.weight'] = 'res_conv1_bn_s' 30 | new_map['bn1.bias'] = 'res_conv1_bn_b' 31 | new_map['bn1.running_mean'] = 'res_conv1_bn_rm' 32 | new_map['bn1.running_var'] = 'res_conv1_bn_riv' 33 | layers = ['layer1','layer2','layer3','layer4'] 34 | 35 | for key in pytorch_params: 36 | for layer in layers: 37 | py_items = key.split('.') 38 | if layer in key: 39 | py_items = key.split('.') 40 | py_items[0] = name_map[py_items[0]] 41 | temp = copy.deepcopy(py_items) 42 | 43 | if py_items[2] == 'downsample': 44 | py_items[2] = 'branch1' 45 | if py_items[3] == '0': 46 | py_items[3] = 'w' 47 | py_items.remove('weight') 48 | if py_items[3] == '1': 49 | py_items[3] = 'bn' 50 | if py_items[4] == 'weight': 51 | py_items[4] = 's' 52 | if py_items[4] == 'bias': 53 | py_items[4] ='b' 54 | if py_items[4] == 'running_mean': 55 | py_items[4] = 'rm' 56 | if py_items[4] == 'running_var': 57 | py_items[4] = 'riv' 58 | 59 | else: 60 | if temp[2][-1] == '1': 61 | py_items[2] = 'branch2a' 62 | if temp[2][-1] == '2': 63 | py_items[2] = 'branch2b' 64 | if temp[2][-1] == '3': 65 | py_items[2] = 'branch2c' 66 | if temp[2][:-1] == 'conv': 67 | py_items[3] = 'w' 68 | elif temp[2][:-1] == 'bn': 69 | py_items[3] = 'bn' 70 | if temp[3] == 'weight': 71 | py_items.append('s') 72 | if temp[3] == 'bias': 73 | py_items.append('b') 74 | 75 | if temp[3] == 'running_mean': 76 | py_items.append('rm') 77 | if temp[3] == 'running_var': 78 | py_items.append('riv') 79 | 80 | 81 | new_param = py_items[0] 82 | for item in py_items[1:]: 83 | new_param = new_param + '_' + item 84 | new_map[key] = new_param 85 | return new_map 86 | 87 | def check_param(name_map,caffe_params,pytorch_params): 88 | for key in name_map.keys(): 89 | #if "downsample" in key: 90 | print (key) 91 | print (name_map[key]) 92 | pytorch_shape = pytorch_params[key].shape 93 | caffe_shape = caffe_params[name_map[key]].shape 94 | assert pytorch_shape == caffe_shape, \ 95 | "pytorch param shape = {} vs caffe param shape = {}".format(pytorch_shape,caffe_shape) 96 | 97 | caffe_params = {} 98 | for key in caffe_data.keys(): 99 | remove_voc = ['momentum','lr','iter'] 100 | ADD_FLAG = True 101 | for voc in remove_voc: 102 | if voc in key: 103 | ADD_FLAG = False 104 | if ADD_FLAG: 105 | caffe_params[key] = caffe_data[key] 106 | img = Variable(torch.randn(1,3,32,224,224)) 107 | net = ResNet.resnet50() 108 | 109 | pytorch_params = {} 110 | 111 | model_dict = net.state_dict() 112 | for name in model_dict.keys(): 113 | #for name, param in net.named_parameters(): 114 | if "num_batches_tracked" not in name: 115 | pytorch_params[name] = model_dict[name] 116 | 117 | print (len(pytorch_params),len(caffe_params)) 118 | out = net(img) 119 | 120 | name_map = argparser(caffe_params,pytorch_params) 121 | check_param(name_map,caffe_params,pytorch_params) 122 | -------------------------------------------------------------------------------- /demo_Kinetics.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.utils.data as Data 3 | import torchvision 4 | from lib.network import ResNet 5 | #from lib.network import ResNet 6 | from torch.autograd import Variable 7 | from torch import nn 8 | from videoDataset import videoDataset 9 | import pickle 10 | import time 11 | import h5py 12 | from checkpoints.check import argparser 13 | 14 | def load_params(pretrained_model_path, model_params): 15 | caffe_weights = pickle.load(open(pretrained_model_path,'rb'),encoding='latin1') 16 | caffe_data = caffe_weights['blobs'] 17 | caffe_params = {} 18 | remove_voc = ['momentum','lr','iter'] 19 | for key in caffe_data.keys(): 20 | ADD_FLAG = True 21 | for voc in remove_voc: 22 | if voc in key: 23 | ADD_FLAG = False 24 | if ADD_FLAG: 25 | caffe_params[key] = caffe_data[key] 26 | 27 | name_map = argparser(caffe_params, model_params) 28 | 29 | state_dict = {} 30 | for key in model_params.keys(): 31 | state_dict['module.'+key] = torch.FloatTensor(caffe_params[name_map[key]]) 32 | return state_dict 33 | 34 | def calc_acc(x, y): 35 | x = torch.max(x, dim=-1)[1] 36 | accuracy = (sum(x == y))*100.0 / x.size(0) 37 | return accuracy 38 | 39 | pretrained_model_path = '/home/skye/DeepLearningPJ/video-nonlocal-net/checkpoints/c2d_baseline_32x2_IN_pretrain_400k.pkl' 40 | 41 | hdf5_file = 'gen-crop-videos/filter_clipsListFile.hdf5' 42 | video = videoDataset(hdf5_file,3,32,224,224) 43 | 44 | test_loader = Data.DataLoader(dataset=video, batch_size=128, shuffle=False) 45 | # 46 | test_batch_num = len(test_loader) 47 | 48 | net = ResNet.resnet50() 49 | model_dict = net.state_dict() 50 | model_params = {} 51 | for name in model_dict.keys(): 52 | if "num_batches_tracked" not in name: 53 | model_params[name] = model_dict[name] 54 | 55 | print ("start to load pretrained model ....") 56 | net_params = load_params(pretrained_model_path,model_params) 57 | if torch.cuda.is_available(): 58 | net = nn.DataParallel(net) 59 | net.cuda() 60 | 61 | 62 | model_dict = net.state_dict() 63 | net_params = {k:v for k,v in net_params.items() if k in model_dict.keys()} 64 | 65 | model_dict.update(net_params) 66 | net.load_state_dict(model_dict) 67 | 68 | print ("parameter loaded!") 69 | loss_func = nn.CrossEntropyLoss() 70 | 71 | for epoch_index in range(1): 72 | st = time.time() 73 | print ("start to test...") 74 | total_loss = 0 75 | total_acc = 0 76 | with torch.no_grad(): 77 | for test_batch_index, sample_batch in enumerate(test_loader): 78 | img_batch = Variable(sample_batch['clip']) 79 | label_batch = Variable(sample_batch['label']) 80 | 81 | if torch.cuda.is_available(): 82 | img_batch = img_batch.cuda() 83 | label_batch = label_batch.cuda() 84 | 85 | predict = net(img_batch) 86 | acc = calc_acc(predict.cpu().data, label_batch.cpu().data) 87 | loss = loss_func(predict, label_batch) 88 | 89 | total_loss += loss 90 | total_acc += acc 91 | print ("loss = %.3f acc = %.3f"%(loss,acc)) 92 | 93 | mean_acc = total_acc / test_batch_num 94 | mean_loss = total_loss / test_batch_num 95 | 96 | print('[Test] epoch[%d/%d] acc:%.4f loss:%.4f\n' 97 | % (epoch_index, 100, mean_acc, mean_loss.data[0])) 98 | -------------------------------------------------------------------------------- /gen-crop-videos/videoDataset.py: -------------------------------------------------------------------------------- 1 | """ 2 | PyTorch Video Dataset Class for loading videos using PyTorch 3 | Dataloader. This Dataset assumes that video files are Preprocessed 4 | by being trimmed over time and resizing the frames. 5 | 6 | Mohsen Fayyaz __ Sensifai Vision Group 7 | http://www.Sensifai.com 8 | 9 | If you find this code useful, please star the repository. 10 | """ 11 | 12 | from __future__ import print_function, division 13 | import cv2 14 | import os 15 | import torch 16 | import numpy as np 17 | import pickle 18 | from torch.utils.data import Dataset, DataLoader 19 | from torchvision import transforms, utils 20 | 21 | 22 | class RandomCrop(object): 23 | """Crop randomly the frames in a clip. 24 | 25 | Args: 26 | output_size (tuple or int): Desired output size. If int, square crop 27 | is made. 28 | """ 29 | 30 | def __init__(self, output_size): 31 | assert isinstance(output_size, (int, tuple)) 32 | if isinstance(output_size, int): 33 | self.output_size = (output_size, output_size) 34 | else: 35 | assert len(output_size) == 2 36 | self.output_size = output_size 37 | self.clip = clip 38 | 39 | def __call__(self): 40 | 41 | h, w = clip.size()[2:] 42 | new_h, new_w = self.output_size 43 | 44 | top = np.random.randint(0, h - new_h) 45 | left = np.random.randint(0, w - new_w) 46 | 47 | clip = clip[:, :, top: top + new_h, 48 | left: left + new_w] 49 | 50 | return clip 51 | 52 | 53 | class videoDataset(Dataset): 54 | """Dataset Class for Loading Video""" 55 | 56 | def __init__(self, clipsListFile, channels, frames_num, xSize, ySize, mean, std, transform=None): 57 | """ 58 | Args: 59 | clipsList (string): Path to the clipsList file with labels. 60 | rootDir (string): Directory with all the videoes. 61 | transform (callable, optional): Optional transform to be applied 62 | on a sample. 63 | channels: Number of channels of frames 64 | timeDepth: Number of frames to be loaded in a sample 65 | xSize, ySize: Dimensions of the frames 66 | mean: Mean valuse of the training set videos over each channel 67 | """ 68 | with open(clipsListFile, "rb") as fp: # Unpickling 69 | clipsList = pickle.load(fp) 70 | 71 | self.clipsList = clipsList 72 | #self.rootDir = rootDir 73 | self.channels = channels 74 | self.frames_num = frames_num 75 | self.xSize = xSize 76 | self.ySize = ySize 77 | self.mean = mean 78 | self.std = std 79 | self.transform = transform 80 | 81 | def __len__(self): 82 | return len(self.clipsList) 83 | 84 | def randomCrop(self,frame): 85 | h,w = frame.size()[2:] 86 | new_h,new_w = self.xSize,self.ySize 87 | 88 | top = np.random.randint(0 , h - new_h) 89 | 90 | left = np.random.randint(0 , w - new_w) 91 | 92 | clip = frame[:,:,top:top + new_h, left:left + new_w] 93 | 94 | normalized_clip = (clip - self.mean)/self.std 95 | return normalized_clip 96 | 97 | def readVideo(self, videoFile): 98 | # Open the video file 99 | cap = cv2.VideoCapture(videoFile) 100 | nFrames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) 101 | width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 102 | height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 103 | 104 | failedClip = False 105 | if width == 0 or height == 0 : 106 | clips = [] 107 | failedClip = True 108 | print ("%s size = 0"%videoFile) 109 | return clips,failedClip 110 | 111 | frames = torch.FloatTensor(self.channels, nFrames, height, width) 112 | 113 | if width < self.xSize or height < self.ySize: 114 | failedClip = True 115 | 116 | for f in range(nFrames): 117 | ret, frame = cap.read() 118 | if ret: 119 | frame = torch.from_numpy(frame) 120 | # to CHW 121 | frame = frame.permute(2,0,1) 122 | frames[:,f,:,:] = frame 123 | else: 124 | print("Skipped!") 125 | failedClip = True 126 | break 127 | # sample 64 clip then drop every other frame 128 | #random_nums = (np.random.rand(self.frames_num)*nFrames).astype(np.int32) 129 | samples_frames = torch.FloatTensor(self.channels,self.frames_num,height,width) 130 | 131 | for i in range(self.frames_num): 132 | samples_frames[:,i,:,:] = frames[:,random_nums[i],:,:] 133 | 134 | final_frames = torch.FloatTensor(self.channels,32,height,width) 135 | 136 | for i in range(32): 137 | if i%2 == 0: 138 | final_frames[:,i,:,:] = samples_frames[:,i,:,:] 139 | 140 | if failedClip == False: 141 | clips = self.randomCrop(final_frames) 142 | else: 143 | clips = [] 144 | return clips, failedClip 145 | 146 | def __getitem__(self, idx): 147 | videoFile = self.clipsList[idx][0] 148 | clip, failedClip = self.readVideo(videoFile) 149 | 150 | if self.transform: 151 | clip = self.transform(clip) 152 | sample = {'clip': clip, 'label': self.clipsList[idx][1], 'failedClip': failedClip} 153 | 154 | return sample 155 | -------------------------------------------------------------------------------- /gen-crop-videos/waste/filter_None.py: -------------------------------------------------------------------------------- 1 | import h5py 2 | import numpy as np 3 | from tqdm import tqdm 4 | from joblib import delayed 5 | from joblib import Parallel 6 | 7 | h5 = h5py.File('clipsListFile.hdf5','r') 8 | filt_h5 = h5py.File('filter_clipsListFile.hdf5','w') 9 | keys = h5.keys() 10 | 11 | def write_wrapper(i): 12 | key = keys[i] 13 | if np.array(h5[key]['clip']).shape == (3,32,224,224): 14 | filt_h5[key+'/clip'] = np.array(h5[key]['clip']) 15 | filt_h5[key+'/label'] = np.array(h5[key]['label']) 16 | else: 17 | #print(np.array[h5[key]['clip']].shape) 18 | print(key) 19 | print (i*100.0/len(keys)) 20 | 21 | Parallel(n_jobs=20,backend="threading")(delayed(write_wrapper)(i) for i in range(len(keys))) 22 | h5.close() 23 | filt_h5.close() 24 | -------------------------------------------------------------------------------- /gen-crop-videos/waste/read_data.py: -------------------------------------------------------------------------------- 1 | from videoDataset import RandomCrop 2 | from videoDataset import videoDataset 3 | import torch.utils.data as Data 4 | import h5py 5 | from joblib import delayed 6 | from joblib import Parallel 7 | from tqdm import tqdm 8 | 9 | MEAN = 114.75 10 | STD = 57.37 11 | h5 = h5py.File('clipsListFile.hdf5','w') 12 | video = videoDataset('clipsListFile.pkl',3,64,224,224,MEAN,STD) 13 | 14 | print (type(video)) 15 | #Pickle.dump(video,open('cropped_clipsListFile.pkl','wb'),2) 16 | #video = Pickle.load() 17 | def write_wrapper(i): 18 | sample = video[i] 19 | clip = sample['clip'] 20 | h5[str(i)+'/clip'] = clip 21 | 22 | label = sample['label'] 23 | h5[str(i)+'/label'] = label 24 | print (i*100.0/len(video)) 25 | 26 | Parallel(n_jobs=20,backend="threading")(delayed(write_wrapper)(i) for i in range(len(video))) 27 | #pbar = tqdm(total=len(video)) 28 | #for i in range(len(video)): 29 | # pbar.update(1) 30 | # sample = video[i] 31 | h5.close() 32 | #pbar.close() 33 | 34 | #test_loader=Data.DataLoader(dataset=video,batch_size=1,shuffle=False) 35 | #for sample in enumerate(test_loader): 36 | # print sample 37 | -------------------------------------------------------------------------------- /gen-crop-videos/waste/videoDataset.py: -------------------------------------------------------------------------------- 1 | """ 2 | PyTorch Video Dataset Class for loading videos using PyTorch 3 | Dataloader. This Dataset assumes that video files are Preprocessed 4 | by being trimmed over time and resizing the frames. 5 | 6 | Mohsen Fayyaz __ Sensifai Vision Group 7 | http://www.Sensifai.com 8 | 9 | If you find this code useful, please star the repository. 10 | """ 11 | 12 | from __future__ import print_function, division 13 | import cv2 14 | import os 15 | import torch 16 | import numpy as np 17 | import pickle 18 | from torch.utils.data import Dataset, DataLoader 19 | from torchvision import transforms, utils 20 | 21 | 22 | class RandomCrop(object): 23 | """Crop randomly the frames in a clip. 24 | 25 | Args: 26 | output_size (tuple or int): Desired output size. If int, square crop 27 | is made. 28 | """ 29 | 30 | def __init__(self, output_size): 31 | assert isinstance(output_size, (int, tuple)) 32 | if isinstance(output_size, int): 33 | self.output_size = (output_size, output_size) 34 | else: 35 | assert len(output_size) == 2 36 | self.output_size = output_size 37 | self.clip = clip 38 | 39 | def __call__(self): 40 | 41 | h, w = clip.size()[2:] 42 | new_h, new_w = self.output_size 43 | 44 | top = np.random.randint(0, h - new_h) 45 | left = np.random.randint(0, w - new_w) 46 | 47 | clip = clip[:, :, top: top + new_h, 48 | left: left + new_w] 49 | 50 | return clip 51 | 52 | 53 | class videoDataset(Dataset): 54 | """Dataset Class for Loading Video""" 55 | 56 | def __init__(self, clipsListFile, channels, frames_num, xSize, ySize, mean, std, transform=None): 57 | """ 58 | Args: 59 | clipsList (string): Path to the clipsList file with labels. 60 | rootDir (string): Directory with all the videoes. 61 | transform (callable, optional): Optional transform to be applied 62 | on a sample. 63 | channels: Number of channels of frames 64 | timeDepth: Number of frames to be loaded in a sample 65 | xSize, ySize: Dimensions of the frames 66 | mean: Mean valuse of the training set videos over each channel 67 | """ 68 | with open(clipsListFile, "rb") as fp: # Unpickling 69 | clipsList = pickle.load(fp) 70 | 71 | self.clipsList = clipsList 72 | #self.rootDir = rootDir 73 | self.channels = channels 74 | self.frames_num = frames_num 75 | self.xSize = xSize 76 | self.ySize = ySize 77 | self.mean = mean 78 | self.std = std 79 | self.transform = transform 80 | 81 | def __len__(self): 82 | return len(self.clipsList) 83 | 84 | def randomCrop(self,frame): 85 | h,w = frame.size()[2:] 86 | new_h,new_w = self.xSize,self.ySize 87 | 88 | top = np.random.randint(0 , h - new_h) 89 | 90 | left = np.random.randint(0 , w - new_w) 91 | 92 | clip = frame[:,:,top:top + new_h, left:left + new_w] 93 | 94 | normalized_clip = (clip - self.mean)/self.std 95 | return normalized_clip 96 | 97 | def readVideo(self, videoFile): 98 | # Open the video file 99 | cap = cv2.VideoCapture(videoFile) 100 | nFrames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) 101 | width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 102 | height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 103 | 104 | failedClip = False 105 | if width == 0 or height == 0 : 106 | clips = [] 107 | failedClip = True 108 | print ("%s size = 0"%videoFile) 109 | return clips,failedClip 110 | 111 | frames = torch.FloatTensor(self.channels, nFrames, height, width) 112 | 113 | if width < self.xSize or height < self.ySize: 114 | failedClip = True 115 | 116 | for f in range(nFrames): 117 | ret, frame = cap.read() 118 | if ret: 119 | frame = torch.from_numpy(frame) 120 | # to CHW 121 | frame = frame.permute(2,0,1) 122 | frames[:,f,:,:] = frame 123 | else: 124 | print("Skipped!") 125 | failedClip = True 126 | break 127 | # sample 64 clip then drop every other frame 128 | random_nums = (np.random.rand(self.frames_num)*nFrames).astype(np.int32) 129 | samples_frames = torch.FloatTensor(self.channels,self.frames_num,height,width) 130 | 131 | for i in range(self.frames_num): 132 | samples_frames[:,i,:,:] = frames[:,random_nums[i],:,:] 133 | 134 | final_frames = torch.FloatTensor(self.channels,32,height,width) 135 | 136 | for i in range(32): 137 | if i%2 == 0: 138 | final_frames[:,i,:,:] = samples_frames[:,i,:,:] 139 | 140 | if failedClip == False: 141 | clips = self.randomCrop(final_frames) 142 | else: 143 | clips = [] 144 | return clips, failedClip 145 | 146 | def __getitem__(self, idx): 147 | 148 | #videoFile = os.path.join(self.rootDir, self.clipsList[idx][0]) 149 | videoFile = self.clipsList[idx][0] 150 | #videoFile = '/DATACENTER/5/skye/Kinetics/val_256/dying_hair/I0luMKjIZyg_000422_000432.mp4' 151 | clip, failedClip = self.readVideo(videoFile) 152 | 153 | if self.transform: 154 | clip = self.transform(clip) 155 | sample = {'clip': clip, 'label': self.clipsList[idx][1], 'failedClip': failedClip} 156 | 157 | return sample 158 | -------------------------------------------------------------------------------- /gen-crop-videos/waste/videoDataset.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/17Skye17/Non-local-Neural-Networks-Pytorch/9a2ddbafad69f2ca79764d3c29469d8f84f882af/gen-crop-videos/waste/videoDataset.pyc -------------------------------------------------------------------------------- /lib/.network.py.swo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/17Skye17/Non-local-Neural-Networks-Pytorch/9a2ddbafad69f2ca79764d3c29469d8f84f882af/lib/.network.py.swo -------------------------------------------------------------------------------- /lib/.network.py.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/17Skye17/Non-local-Neural-Networks-Pytorch/9a2ddbafad69f2ca79764d3c29469d8f84f882af/lib/.network.py.swp -------------------------------------------------------------------------------- /lib/.network50.py.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/17Skye17/Non-local-Neural-Networks-Pytorch/9a2ddbafad69f2ca79764d3c29469d8f84f882af/lib/.network50.py.swp -------------------------------------------------------------------------------- /lib/__pycache__/network.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/17Skye17/Non-local-Neural-Networks-Pytorch/9a2ddbafad69f2ca79764d3c29469d8f84f882af/lib/__pycache__/network.cpython-36.pyc -------------------------------------------------------------------------------- /lib/__pycache__/network.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/17Skye17/Non-local-Neural-Networks-Pytorch/9a2ddbafad69f2ca79764d3c29469d8f84f882af/lib/__pycache__/network.cpython-37.pyc -------------------------------------------------------------------------------- /lib/__pycache__/non_local_embedded_gaussian.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/17Skye17/Non-local-Neural-Networks-Pytorch/9a2ddbafad69f2ca79764d3c29469d8f84f882af/lib/__pycache__/non_local_embedded_gaussian.cpython-37.pyc -------------------------------------------------------------------------------- /lib/backup/network.py: -------------------------------------------------------------------------------- 1 | from torch import nn 2 | # from lib.non_local_concatenation import NONLocalBlock2D 3 | # from lib.non_local_gaussian import NONLocalBlock2D 4 | from lib.non_local_embedded_gaussian import NONLocalBlock2D 5 | # from lib.non_local_dot_product import NONLocalBlock2D 6 | 7 | 8 | class Network(nn.Module): 9 | def __init__(self): 10 | super(Network, self).__init__() 11 | 12 | self.convs = nn.Sequential( 13 | nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=1), 14 | nn.BatchNorm2d(32), 15 | nn.ReLU(), 16 | nn.MaxPool2d(2), 17 | 18 | NONLocalBlock2D(in_channels=32), 19 | nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1), 20 | nn.BatchNorm2d(64), 21 | nn.ReLU(), 22 | nn.MaxPool2d(2), 23 | 24 | NONLocalBlock2D(in_channels=64), 25 | nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1), 26 | nn.BatchNorm2d(128), 27 | nn.ReLU(), 28 | nn.MaxPool2d(2), 29 | ) 30 | 31 | self.fc = nn.Sequential( 32 | nn.Linear(in_features=128*3*3, out_features=256), 33 | nn.ReLU(), 34 | nn.Dropout(0.5), 35 | 36 | nn.Linear(in_features=256, out_features=10) 37 | ) 38 | 39 | def forward(self, x): 40 | batch_size = x.size(0) 41 | output = self.convs(x).view(batch_size, -1) 42 | output = self.fc(output) 43 | return output 44 | 45 | class Bottleneck(nn.Module): 46 | expansion = 4 47 | def __init__(self,inplanes,planes,stride=1,downsample=None): 48 | super(Bottleneck,self).__init__() 49 | 50 | 51 | class ResNet50(nn.Module): 52 | def __init__(self): 53 | super(ResNet50, self).__init__() 54 | 55 | def forward(self.x): 56 | batch_size = x.size(0) 57 | output = self.convs(x).view(batch_size, -1) 58 | output = self.fc(output) 59 | return output 60 | 61 | 62 | 63 | if __name__ == '__main__': 64 | import torch 65 | from torch.autograd import Variable 66 | 67 | img = Variable(torch.randn(3, 1, 28, 28)) 68 | net = Network() 69 | out = net(img) 70 | print(out.size()) 71 | 72 | -------------------------------------------------------------------------------- /lib/backup/non_local.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch import nn 3 | from torch.nn import functional as F 4 | 5 | 6 | class _NonLocalBlockND(nn.Module): 7 | def __init__(self, in_channels, inter_channels=None, dimension=3, mode='embedded_gaussian', 8 | sub_sample=True, bn_layer=True): 9 | super(_NonLocalBlockND, self).__init__() 10 | 11 | assert dimension in [1, 2, 3] 12 | assert mode in ['embedded_gaussian', 'gaussian', 'dot_product', 'concatenation'] 13 | 14 | # print('Dimension: %d, mode: %s' % (dimension, mode)) 15 | 16 | self.mode = mode 17 | self.dimension = dimension 18 | self.sub_sample = sub_sample 19 | 20 | self.in_channels = in_channels 21 | self.inter_channels = inter_channels 22 | 23 | if self.inter_channels is None: 24 | self.inter_channels = in_channels // 2 25 | if self.inter_channels == 0: 26 | self.inter_channels = 1 27 | 28 | if dimension == 3: 29 | conv_nd = nn.Conv3d 30 | max_pool_layer = nn.MaxPool3d(kernel_size=(1, 2, 2)) 31 | bn = nn.BatchNorm3d 32 | elif dimension == 2: 33 | conv_nd = nn.Conv2d 34 | max_pool_layer = nn.MaxPool2d(kernel_size=(2, 2)) 35 | bn = nn.BatchNorm2d 36 | else: 37 | conv_nd = nn.Conv1d 38 | max_pool_layer = nn.MaxPool1d(kernel_size=(2)) 39 | bn = nn.BatchNorm1d 40 | 41 | self.g = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 42 | kernel_size=1, stride=1, padding=0) 43 | 44 | if bn_layer: 45 | self.W = nn.Sequential( 46 | conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels, 47 | kernel_size=1, stride=1, padding=0), 48 | bn(self.in_channels) 49 | ) 50 | nn.init.constant(self.W[1].weight, 0) 51 | nn.init.constant(self.W[1].bias, 0) 52 | else: 53 | self.W = conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels, 54 | kernel_size=1, stride=1, padding=0) 55 | nn.init.constant(self.W.weight, 0) 56 | nn.init.constant(self.W.bias, 0) 57 | 58 | self.theta = None 59 | self.phi = None 60 | self.concat_project = None 61 | 62 | # if mode in ['embedded_gaussian', 'dot_product', 'concatenation']: 63 | self.theta = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 64 | kernel_size=1, stride=1, padding=0) 65 | 66 | self.phi = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 67 | kernel_size=1, stride=1, padding=0) 68 | # elif mode == 'concatenation': 69 | self.concat_project = nn.Sequential( 70 | nn.Conv2d(self.inter_channels * 2, 1, 1, 1, 0, bias=False), 71 | nn.ReLU() 72 | ) 73 | 74 | # if mode in ['embedded_gaussian', 'dot_product', 'concatenation']: 75 | # self.theta = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 76 | # kernel_size=1, stride=1, padding=0) 77 | # self.phi = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 78 | # kernel_size=1, stride=1, padding=0) 79 | # 80 | # if mode == 'embedded_gaussian': 81 | # self.operation_function = self._embedded_gaussian 82 | # elif mode == 'dot_product': 83 | # self.operation_function = self._dot_product 84 | # elif mode == 'concatenation': 85 | # self.operation_function = self._concatenation 86 | # self.concat_project = nn.Sequential( 87 | # nn.Conv2d(self.inter_channels * 2, 1, 1, 1, 0, bias=False), 88 | # nn.ReLU() 89 | # ) 90 | # elif mode == 'gaussian': 91 | # self.operation_function = self._gaussian 92 | 93 | if sub_sample: 94 | self.g = nn.Sequential(self.g, max_pool_layer) 95 | if self.phi is None: 96 | self.phi = max_pool_layer 97 | else: 98 | self.phi = nn.Sequential(self.phi, max_pool_layer) 99 | 100 | print(self.phi) 101 | 102 | def forward(self, x): 103 | ''' 104 | :param x: (b, c, t, h, w) 105 | :return: 106 | ''' 107 | 108 | if self.mode == 'embedded_gaussian': 109 | output = self._embedded_gaussian(x) 110 | elif mode == 'dot_product': 111 | output = self._dot_product(x) 112 | elif mode == 'concatenation': 113 | output = self._concatenation(x) 114 | elif mode == 'gaussian': 115 | output = self._gaussian(x) 116 | 117 | return output 118 | 119 | def _embedded_gaussian(self, x): 120 | batch_size = x.size(0) 121 | 122 | # g=>(b, c, t, h, w)->(b, 0.5c, t, h, w)->(b, thw, 0.5c) 123 | g_x = self.g(x).view(batch_size, self.inter_channels, -1) 124 | g_x = g_x.permute(0, 2, 1) 125 | 126 | # theta=>(b, c, t, h, w)[->(b, 0.5c, t, h, w)]->(b, thw, 0.5c) 127 | # phi =>(b, c, t, h, w)[->(b, 0.5c, t, h, w)]->(b, 0.5c, thw) 128 | # f=>(b, thw, 0.5c)dot(b, 0.5c, twh) = (b, thw, thw) 129 | theta_x = self.theta(x).view(batch_size, self.inter_channels, -1) 130 | theta_x = theta_x.permute(0, 2, 1) 131 | phi_x = self.phi(x).view(batch_size, self.inter_channels, -1) 132 | f = torch.matmul(theta_x, phi_x) 133 | f_div_C = F.softmax(f, dim=-1) 134 | 135 | # (b, thw, thw)dot(b, thw, 0.5c) = (b, thw, 0.5c)->(b, 0.5c, t, h, w)->(b, c, t, h, w) 136 | y = torch.matmul(f_div_C, g_x) 137 | y = y.permute(0, 2, 1).contiguous() 138 | y = y.view(batch_size, self.inter_channels, *x.size()[2:]) 139 | W_y = self.W(y) 140 | z = W_y + x 141 | 142 | return z 143 | 144 | def _gaussian(self, x): 145 | batch_size = x.size(0) 146 | 147 | g_x = self.g(x).view(batch_size, self.inter_channels, -1) 148 | 149 | g_x = g_x.permute(0, 2, 1) 150 | 151 | theta_x = x.view(batch_size, self.in_channels, -1) 152 | theta_x = theta_x.permute(0, 2, 1) 153 | 154 | if self.sub_sample: 155 | print(self.phi(x).size()) 156 | phi_x = self.phi(x).view(batch_size, self.in_channels, -1) 157 | else: 158 | phi_x = x.view(batch_size, self.in_channels, -1) 159 | print(phi_x.size()) 160 | f = torch.matmul(theta_x, phi_x) 161 | f_div_C = F.softmax(f, dim=-1) 162 | 163 | print(f_div_C.size(), g_x.size()) 164 | 165 | y = torch.matmul(f_div_C, g_x) 166 | y = y.permute(0, 2, 1).contiguous() 167 | y = y.view(batch_size, self.inter_channels, *x.size()[2:]) 168 | W_y = self.W(y) 169 | z = W_y + x 170 | 171 | return z 172 | 173 | def _dot_product(self, x): 174 | batch_size = x.size(0) 175 | 176 | g_x = self.g(x).view(batch_size, self.inter_channels, -1) 177 | g_x = g_x.permute(0, 2, 1) 178 | 179 | theta_x = self.theta(x).view(batch_size, self.inter_channels, -1) 180 | theta_x = theta_x.permute(0, 2, 1) 181 | phi_x = self.phi(x).view(batch_size, self.inter_channels, -1) 182 | f = torch.matmul(theta_x, phi_x) 183 | N = f.size(-1) 184 | f_div_C = f / N 185 | 186 | y = torch.matmul(f_div_C, g_x) 187 | y = y.permute(0, 2, 1).contiguous() 188 | y = y.view(batch_size, self.inter_channels, *x.size()[2:]) 189 | W_y = self.W(y) 190 | z = W_y + x 191 | 192 | return z 193 | 194 | def _concatenation(self, x): 195 | batch_size = x.size(0) 196 | 197 | g_x = self.g(x).view(batch_size, self.inter_channels, -1) 198 | g_x = g_x.permute(0, 2, 1) 199 | 200 | # (b, c, N, 1) 201 | theta_x = self.theta(x).view(batch_size, self.inter_channels, -1, 1) 202 | # (b, c, 1, N) 203 | phi_x = self.phi(x).view(batch_size, self.inter_channels, 1, -1) 204 | 205 | h = theta_x.size(2) 206 | w = phi_x.size(3) 207 | theta_x = theta_x.repeat(1, 1, 1, w) 208 | phi_x = phi_x.repeat(1, 1, h, 1) 209 | 210 | concat_feature = torch.cat([theta_x, phi_x], dim=1) 211 | f = self.concat_project(concat_feature) 212 | b, _, h, w = f.size() 213 | f = f.view(b, h, w) 214 | 215 | N = f.size(-1) 216 | f_div_C = f / N 217 | 218 | y = torch.matmul(f_div_C, g_x) 219 | y = y.permute(0, 2, 1).contiguous() 220 | y = y.view(batch_size, self.inter_channels, *x.size()[2:]) 221 | W_y = self.W(y) 222 | z = W_y + x 223 | 224 | return z 225 | 226 | 227 | class NONLocalBlock1D(_NonLocalBlockND): 228 | def __init__(self, in_channels, inter_channels=None, mode='embedded_gaussian', sub_sample=True, bn_layer=True): 229 | super(NONLocalBlock1D, self).__init__(in_channels, 230 | inter_channels=inter_channels, 231 | dimension=1, mode=mode, 232 | sub_sample=sub_sample, 233 | bn_layer=bn_layer) 234 | 235 | 236 | class NONLocalBlock2D(_NonLocalBlockND): 237 | def __init__(self, in_channels, inter_channels=None, mode='embedded_gaussian', sub_sample=True, bn_layer=True): 238 | super(NONLocalBlock2D, self).__init__(in_channels, 239 | inter_channels=inter_channels, 240 | dimension=2, mode=mode, 241 | sub_sample=sub_sample, 242 | bn_layer=bn_layer) 243 | 244 | 245 | class NONLocalBlock3D(_NonLocalBlockND): 246 | def __init__(self, in_channels, inter_channels=None, mode='embedded_gaussian', sub_sample=True, bn_layer=True): 247 | super(NONLocalBlock3D, self).__init__(in_channels, 248 | inter_channels=inter_channels, 249 | dimension=3, mode=mode, 250 | sub_sample=sub_sample, 251 | bn_layer=bn_layer) 252 | 253 | 254 | if __name__ == '__main__': 255 | from torch.autograd import Variable 256 | 257 | # mode_list = ['concatenation', 'embedded_gaussian', 'gaussian', 'dot_product', ] 258 | mode_list = ['gaussian'] 259 | 260 | for mode in mode_list: 261 | print(mode) 262 | img = Variable(torch.zeros(2, 6, 20)) 263 | net = NONLocalBlock1D(6, mode=mode, sub_sample=True) 264 | out = net(img) 265 | print(out.size()) 266 | 267 | # img = Variable(torch.zeros(2, 4, 20, 20)) 268 | # net = NONLocalBlock2D(4, mode=mode, sub_sample=False, bn_layer=False) 269 | # out = net(img) 270 | # print(out.size()) 271 | # 272 | # img = Variable(torch.zeros(2, 4, 10, 20, 20)) 273 | # net = NONLocalBlock3D(4, mode=mode) 274 | # out = net(img) 275 | # print(out.size()) 276 | 277 | -------------------------------------------------------------------------------- /lib/backup/non_local_simple_version.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch import nn 3 | from torch.nn import functional as F 4 | 5 | 6 | class _NonLocalBlockND(nn.Module): 7 | def __init__(self, in_channels, inter_channels=None, dimension=3, sub_sample=True, bn_layer=True): 8 | super(_NonLocalBlockND, self).__init__() 9 | 10 | assert dimension in [1, 2, 3] 11 | 12 | self.dimension = dimension 13 | self.sub_sample = sub_sample 14 | 15 | self.in_channels = in_channels 16 | self.inter_channels = inter_channels 17 | 18 | if self.inter_channels is None: 19 | self.inter_channels = in_channels // 2 20 | if self.inter_channels == 0: 21 | self.inter_channels = 1 22 | 23 | if dimension == 3: 24 | conv_nd = nn.Conv3d 25 | max_pool_layer = nn.MaxPool3d(kernel_size=(1, 2, 2)) 26 | bn = nn.BatchNorm3d 27 | elif dimension == 2: 28 | conv_nd = nn.Conv2d 29 | max_pool_layer = nn.MaxPool2d(kernel_size=(2, 2)) 30 | bn = nn.BatchNorm2d 31 | else: 32 | conv_nd = nn.Conv1d 33 | max_pool_layer = nn.MaxPool1d(kernel_size=(2)) 34 | bn = nn.BatchNorm1d 35 | 36 | self.g = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 37 | kernel_size=1, stride=1, padding=0) 38 | 39 | if bn_layer: 40 | self.W = nn.Sequential( 41 | conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels, 42 | kernel_size=1, stride=1, padding=0), 43 | bn(self.in_channels) 44 | ) 45 | nn.init.constant(self.W[1].weight, 0) 46 | nn.init.constant(self.W[1].bias, 0) 47 | else: 48 | self.W = conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels, 49 | kernel_size=1, stride=1, padding=0) 50 | nn.init.constant(self.W.weight, 0) 51 | nn.init.constant(self.W.bias, 0) 52 | 53 | self.theta = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 54 | kernel_size=1, stride=1, padding=0) 55 | self.phi = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 56 | kernel_size=1, stride=1, padding=0) 57 | 58 | if sub_sample: 59 | self.g = nn.Sequential(self.g, max_pool_layer) 60 | self.phi = nn.Sequential(self.phi, max_pool_layer) 61 | 62 | def forward(self, x): 63 | ''' 64 | :param x: (b, c, t, h, w) 65 | :return: 66 | ''' 67 | 68 | batch_size = x.size(0) 69 | 70 | g_x = self.g(x).view(batch_size, self.inter_channels, -1) 71 | g_x = g_x.permute(0, 2, 1) 72 | 73 | theta_x = self.theta(x).view(batch_size, self.inter_channels, -1) 74 | theta_x = theta_x.permute(0, 2, 1) 75 | phi_x = self.phi(x).view(batch_size, self.inter_channels, -1) 76 | f = torch.matmul(theta_x, phi_x) 77 | f_div_C = F.softmax(f, dim=-1) 78 | 79 | y = torch.matmul(f_div_C, g_x) 80 | y = y.permute(0, 2, 1).contiguous() 81 | y = y.view(batch_size, self.inter_channels, *x.size()[2:]) 82 | W_y = self.W(y) 83 | z = W_y + x 84 | 85 | return z 86 | 87 | 88 | class NONLocalBlock1D(_NonLocalBlockND): 89 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 90 | super(NONLocalBlock1D, self).__init__(in_channels, 91 | inter_channels=inter_channels, 92 | dimension=1, sub_sample=sub_sample, 93 | bn_layer=bn_layer) 94 | 95 | 96 | class NONLocalBlock2D(_NonLocalBlockND): 97 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 98 | super(NONLocalBlock2D, self).__init__(in_channels, 99 | inter_channels=inter_channels, 100 | dimension=2, sub_sample=sub_sample, 101 | bn_layer=bn_layer) 102 | 103 | 104 | class NONLocalBlock3D(_NonLocalBlockND): 105 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 106 | super(NONLocalBlock3D, self).__init__(in_channels, 107 | inter_channels=inter_channels, 108 | dimension=3, sub_sample=sub_sample, 109 | bn_layer=bn_layer) 110 | 111 | 112 | if __name__ == '__main__': 113 | from torch.autograd import Variable 114 | import torch 115 | 116 | sub_sample = True 117 | bn_layer = True 118 | 119 | img = Variable(torch.zeros(2, 3, 20)) 120 | net = NONLocalBlock1D(3, sub_sample=sub_sample, bn_layer=bn_layer) 121 | out = net(img) 122 | print(out.size()) 123 | 124 | img = Variable(torch.zeros(2, 3, 20, 20)) 125 | net = NONLocalBlock2D(3, sub_sample=sub_sample, bn_layer=bn_layer) 126 | out = net(img) 127 | print(out.size()) 128 | 129 | img = Variable(torch.randn(2, 3, 10, 20, 20)) 130 | net = NONLocalBlock3D(3, sub_sample=sub_sample, bn_layer=bn_layer) 131 | out = net(img) 132 | print(out.size()) 133 | 134 | -------------------------------------------------------------------------------- /lib/network.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | 4 | 5 | class Bottleneck(nn.Module): 6 | expansion=4 7 | 8 | def __init__(self, inplanes, planes, stride=(1,1,1), padding=(0,1,1), downsample=None): 9 | super(Bottleneck,self).__init__() 10 | self.conv1=nn.Conv3d(inplanes, planes, kernel_size=(1,1,1), stride=(1,1,1), padding=(0,0,0), bias=False) 11 | self.bn1=nn.BatchNorm3d(planes) 12 | self.conv2=nn.Conv3d(planes, planes, kernel_size=(1,3,3), stride=stride, padding=padding, bias=False) 13 | self.bn2=nn.BatchNorm3d(planes) 14 | self.conv3=nn.Conv3d(planes, planes*4, kernel_size=(1,1,1), stride=(1,1,1), padding=(0,0,0), bias=False) 15 | self.bn3=nn.BatchNorm3d(planes*4) 16 | self.relu=nn.ReLU(inplace=True) 17 | self.downsample=downsample 18 | self.stride=stride 19 | 20 | def forward(self,x): 21 | residual=x 22 | 23 | out=self.conv1(x) 24 | out=self.bn1(out) 25 | out=self.relu(out) 26 | 27 | out=self.conv2(out) 28 | out=self.bn2(out) 29 | out=self.relu(out) 30 | 31 | out=self.conv3(out) 32 | out=self.bn3(out) 33 | 34 | if self.downsample is not None: 35 | residual=self.downsample(x) 36 | 37 | out +=residual 38 | out=self.relu(out) 39 | 40 | return out 41 | 42 | class ResNet(nn.Module): 43 | 44 | def __init__(self,block,layers, num_classes=400, zero_init_residual=False): 45 | self.inplanes = 64 46 | super(ResNet,self).__init__() 47 | self.conv1=nn.Conv3d(3,64,kernel_size=(1,7,7),stride=(2,2,2), padding=(0,3,3), bias=False) 48 | self.bn1=nn.BatchNorm3d(64) 49 | self.relu=nn.ReLU(inplace=True) 50 | self.maxpool1=nn.MaxPool3d(kernel_size=(3,3,3), stride=(2,2,2), padding=(0,0,0)) 51 | self.layer1=self._make_layer(block, 64, layers[0], downsample_padding=(0,0,0)) 52 | self.maxpool2=nn.MaxPool3d(kernel_size=(3,1,1), stride=(2,1,1), padding=(0,0,0)) 53 | self.layer2=self._make_layer(block, 128, layers[1], stride=(2,2,2), padding=(2,1,1)) 54 | self.layer3=self._make_layer(block, 256, layers[2], stride=(2,2,2), padding=(2,1,1)) 55 | self.layer4=self._make_layer(block, 512, layers[3], stride=(2,2,2), padding=(2,1,1)) 56 | self.avgpool=nn.AvgPool3d((4,7,7), stride=(1,1,1)) 57 | self.fc=nn.Linear(512*block.expansion, num_classes) 58 | 59 | 60 | def _make_layer(self, block, planes, blocks, stride=(1,1,1), padding=(0,1,1), downsample_padding=(2,0,0)): 61 | downsample = nn.Sequential(nn.Conv3d(self.inplanes, planes*block.expansion, kernel_size=(1,1,1), stride=stride,padding=downsample_padding, bias=False), nn.BatchNorm3d(planes* block.expansion)) 62 | 63 | layers =[] 64 | layers.append(block(self.inplanes, planes, stride, padding, downsample)) 65 | self.inplanes=planes*block.expansion 66 | for i in range(1,blocks): 67 | layers.append(block(self.inplanes,planes)) 68 | return nn.Sequential(*layers) 69 | 70 | def forward(self,x): 71 | x=self.conv1(x) 72 | x=self.bn1(x) 73 | x=self.relu(x) 74 | x=self.maxpool1(x) 75 | 76 | x=self.layer1(x) 77 | x=self.maxpool2(x) 78 | x=self.layer2(x) 79 | x=self.layer3(x) 80 | x=self.layer4(x) 81 | 82 | x=self.avgpool(x) 83 | 84 | x=x.view(x.size(0), -1) 85 | x=self.fc(x) 86 | 87 | return x 88 | 89 | 90 | def resnet50(**kwargs): 91 | model = ResNet(Bottleneck,[3,4,6,3], **kwargs) 92 | return model 93 | 94 | 95 | if __name__=='__main__': 96 | import torch 97 | from torch.autograd import Variable 98 | img = Variable(torch.randn(1,3,32,224,224)) 99 | net = ResNet.resnet50() 100 | count = 0 101 | for name, param in net.named_parameters(): 102 | if param.requires_grad: 103 | count += 1 104 | print(name) 105 | print (count) 106 | out = net(img) 107 | print(out.size()) 108 | 109 | -------------------------------------------------------------------------------- /lib/network.py.back: -------------------------------------------------------------------------------- 1 | from torch import nn 2 | # from lib.non_local_concatenation import NONLocalBlock2D 3 | # from lib.non_local_gaussian import NONLocalBlock2D 4 | from lib.non_local_embedded_gaussian import NONLocalBlock2D 5 | # from lib.non_local_dot_product import NONLocalBlock2D 6 | import ipdb 7 | 8 | class Network(nn.Module): 9 | def __init__(self): 10 | super(Network, self).__init__() 11 | 12 | # self.convs = nn.Sequential( 13 | # #nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1), 14 | # nn.Conv3d(in_channels=3, out_channels=64, kernel_size=(1,7,7),stride=(2,2,2),padding=1), 15 | # nn.BatchNorm3d(64), 16 | # nn.ReLU(), 17 | # nn.MaxPool3d(kernel_size=(3,3,3),stride=(2,2,2)), 18 | # 19 | # # 4x28x28 20 | # nn.Conv3d(in_channels=64, out_channels=128, kernel_size=(1,3,3),stride=(2,2,2),padding=1), 21 | # nn.BatchNorm3d(128), 22 | # nn.ReLU(), 23 | # # 2x28x28 24 | # nn.MaxPool3d(kernel_size=(3,1,1),stride=(2,1,1)), 25 | # 26 | # nn.Conv3d(in_channels=128, out_channels=256, kernel_size=(1,3,3),stride=(2,2,2),padding=1), 27 | # nn.BatchNorm3d(256), 28 | # nn.ReLU(), 29 | # # 1x7x7 30 | # nn.MaxPool3d(kernel_size=(1,3,3),stride=(1,2,2)), 31 | # 32 | # ) 33 | # self.fc = nn.Sequential( 34 | # nn.Linear(in_features=256*7*7,out_features=400), 35 | # nn.ReLU(), 36 | # nn.Dropout(0.5), 37 | # ) 38 | # 39 | #nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1), 40 | self.conv1 = nn.Conv3d(in_channels=3, out_channels=64, kernel_size=(1,7,7),stride=(2,2,2),padding=(0,3,3)) 41 | self.bn1 = nn.BatchNorm3d(64) 42 | self.relu1 = nn.ReLU() 43 | self.pool1 = nn.MaxPool3d(kernel_size=(3,3,3),stride=(2,2,2),padding=(1,1,1)) 44 | 45 | # 4x28x28 46 | self.conv2 = nn.Conv3d(in_channels=64, out_channels=128, kernel_size=(1,3,3),stride=(2,2,2),padding=(0,1,1)) 47 | self.bn2 = nn.BatchNorm3d(128) 48 | self.relu2 = nn.ReLU() 49 | # 2x28x28 50 | self.pool2 = nn.MaxPool3d(kernel_size=(3,1,1),stride=(2,1,1),padding=(1,0,0)) 51 | 52 | #1x14x14 53 | self.conv3 = nn.Conv3d(in_channels=128, out_channels=256, kernel_size=(1,3,3),stride=(2,2,2),padding=(0,1,1)) 54 | self.bn3 = nn.BatchNorm3d(256) 55 | self.relu3 = nn.ReLU() 56 | 57 | # 1x4x4 58 | self.pool3 = nn.MaxPool3d(kernel_size=(1,7,7),stride=(1,2,2),padding=(0,0,0)) 59 | 60 | self.linear = nn.Linear(in_features=256*4*4,out_features=400) 61 | 62 | self.relu4 = nn.ReLU() 63 | self.dropout = nn.Dropout(0.5) 64 | 65 | def forward(self, x): 66 | batch_size = x.size(0) 67 | #output = self.convs(x).view(batch_size, -1) 68 | x = self.conv1(x) 69 | x = self.bn1(x) 70 | x = self.relu1(x) 71 | x = self.pool1(x) 72 | x = self.conv2(x) 73 | 74 | x = self.bn2(x) 75 | x = self.relu2(x) 76 | x = self.pool2(x) 77 | 78 | 79 | x = self.conv3(x) 80 | 81 | x = self.bn3(x) 82 | x = self.relu3(x) 83 | x = self.pool3(x).view(batch_size,-1) 84 | 85 | x = self.linear(x) 86 | x = self.relu4(x) 87 | x = self.dropout(x) 88 | 89 | return x 90 | #output = self.convs(x) 91 | 92 | #print (output.size()) 93 | #ipdb.set_trace() 94 | #output = self.fc(output) 95 | #return output 96 | 97 | class Bottleneck(nn.Module): 98 | expansion = 4 99 | def __init__(self,inplanes,planes,stride=1,downsample=None): 100 | super(Bottleneck,self).__init__() 101 | self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False ) 102 | self.bn1 = nn.BatchNorm2d(planes) 103 | self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias = False) 104 | self.bn2 = nn.BatchNorm2d(planes) 105 | self.conv3 = nn.Conv2d(planes, planes*4,kernel_size=1,bias=False) 106 | self.bn3 = nn.BatchNorm2d(planes*4) 107 | self.relu = nn.ReLU(inplace=True) 108 | self.downsample = downsample 109 | self.stride = stride 110 | 111 | def forward(self,x): 112 | residual = x 113 | out = self.conv1(x) 114 | out = self.bn1(out) 115 | out = self.relu(out) 116 | 117 | out = self.conv2(out) 118 | out = self.bn2(out) 119 | out = self.relu(out) 120 | 121 | out = self.conv3(out) 122 | out = self.bn3(out) 123 | 124 | if self.downsample is not None: 125 | residual = self.downsample(x) 126 | 127 | out += residual 128 | out = self.relu(out) 129 | return out 130 | 131 | class ResNet3D(nn.Module): 132 | def __init__(self,block,layers,num_classes=400): 133 | self.inplanes = 64 134 | super(ResNet3D, self).__init__() 135 | self.conv1 = nn.Conv3d(3,64,kernel_size=(1,7,7), stride=(2,2,2), padding=(0,3,3), bias=False) 136 | self.bn1 = nn.BatchNorm3d(64) 137 | self.relu = nn.ReLU(inplace=True) 138 | self.maxpool = nn.MaxPool3d(kernel_size=(3,3,3), stride=(2,2,2), padding=(1,1,1)) 139 | self.layer1 = self._make_layer(block,64,layers[0]) 140 | self.layer2 = self._make_layer(block,128,layers[1],stride=2) 141 | self.layer3 = self._make_layer(block,256,layers[2],stride=2) 142 | self.layer4 = self._make_layer(block,512,layers[3],stride=2) 143 | self.avgpool = nn.AvgPool2d(7, stride=1) 144 | self.fc = nn.Linear(512*block.expansion,num_classes) 145 | 146 | def _make_layer(self,block,planes,blocks,stride=1): 147 | downsample = None 148 | if stride != 1 or self.inplanes != planes*block.expansion: 149 | downsample = nn.Sequential ( 150 | nn.Conv2d(self.inplanes,planes * block.expansion , 151 | kernel_size = 1, stride = stride ,bias = False), 152 | nn.BatchNorm2d(planes * block.expansion), 153 | ) 154 | layers = [] 155 | layers.append(block(self.inplanes, planes, stride, downsample)) 156 | self.inplanes = planes * block.expansion 157 | for i in range(1,blocks): 158 | layers.append(block(self.inplanes,planes)) 159 | return nn.Sequential(*layers) 160 | 161 | def forward(self,x): 162 | x = self.conv1(x) 163 | x = self.bn1(x) 164 | x = self.relu(x) 165 | x = self.maxpool(x) 166 | x = self.layer1(x) 167 | x = self.layer2(x) 168 | x = self.layer3(x) 169 | x = self.layer4(x) 170 | 171 | x = self.avgpool(x) 172 | x = x.view(x.size(0),-1) 173 | x = self.fc(x) 174 | 175 | return x 176 | 177 | def resnet50(pretrained=False, **kwargs): 178 | model = ResNet3D(Bottleneck, [3,4,6,3], **kwargs) 179 | return model 180 | 181 | if __name__ == '__main__': 182 | import torch 183 | from torch.autograd import Variable 184 | img = Variable(torch.randn(1,3,32,224, 224)) 185 | net = ResNet3D.resnet50() 186 | for name,param in net.named_parameters(): 187 | if param.requires_grad: 188 | print (name) 189 | out = net(img) 190 | print(out.size()) 191 | 192 | -------------------------------------------------------------------------------- /lib/network2d.py: -------------------------------------------------------------------------------- 1 | from torch import nn 2 | # from lib.non_local_concatenation import NONLocalBlock2D 3 | # from lib.non_local_gaussian import NONLocalBlock2D 4 | from lib.non_local_embedded_gaussian import NONLocalBlock2D 5 | # from lib.non_local_dot_product import NONLocalBlock2D 6 | import ipdb 7 | 8 | class Network(nn.Module): 9 | def __init__(self): 10 | super(Network, self).__init__() 11 | 12 | # self.convs = nn.Sequential( 13 | # #nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1), 14 | # nn.Conv3d(in_channels=3, out_channels=64, kernel_size=(1,7,7),stride=(2,2,2),padding=1), 15 | # nn.BatchNorm3d(64), 16 | # nn.ReLU(), 17 | # nn.MaxPool3d(kernel_size=(3,3,3),stride=(2,2,2)), 18 | # 19 | # # 4x28x28 20 | # nn.Conv3d(in_channels=64, out_channels=128, kernel_size=(1,3,3),stride=(2,2,2),padding=1), 21 | # nn.BatchNorm3d(128), 22 | # nn.ReLU(), 23 | # # 2x28x28 24 | # nn.MaxPool3d(kernel_size=(3,1,1),stride=(2,1,1)), 25 | # 26 | # nn.Conv3d(in_channels=128, out_channels=256, kernel_size=(1,3,3),stride=(2,2,2),padding=1), 27 | # nn.BatchNorm3d(256), 28 | # nn.ReLU(), 29 | # # 1x7x7 30 | # nn.MaxPool3d(kernel_size=(1,3,3),stride=(1,2,2)), 31 | # 32 | # ) 33 | # self.fc = nn.Sequential( 34 | # nn.Linear(in_features=256*7*7,out_features=400), 35 | # nn.ReLU(), 36 | # nn.Dropout(0.5), 37 | # ) 38 | # 39 | #nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, stride=1, padding=1), 40 | self.conv1 = nn.Conv3d(in_channels=3, out_channels=64, kernel_size=(1,7,7),stride=(2,2,2),padding=(0,3,3)) 41 | self.bn1 = nn.BatchNorm3d(64) 42 | self.relu1 = nn.ReLU() 43 | self.pool1 = nn.MaxPool3d(kernel_size=(3,3,3),stride=(2,2,2),padding=(1,1,1)) 44 | 45 | # 4x28x28 46 | self.conv2 = nn.Conv3d(in_channels=64, out_channels=128, kernel_size=(1,3,3),stride=(2,2,2),padding=(0,1,1)) 47 | self.bn2 = nn.BatchNorm3d(128) 48 | self.relu2 = nn.ReLU() 49 | # 2x28x28 50 | self.pool2 = nn.MaxPool3d(kernel_size=(3,1,1),stride=(2,1,1),padding=(1,0,0)) 51 | 52 | #1x14x14 53 | self.conv3 = nn.Conv3d(in_channels=128, out_channels=256, kernel_size=(1,3,3),stride=(2,2,2),padding=(0,1,1)) 54 | self.bn3 = nn.BatchNorm3d(256) 55 | self.relu3 = nn.ReLU() 56 | 57 | # 1x4x4 58 | self.pool3 = nn.MaxPool3d(kernel_size=(1,7,7),stride=(1,2,2),padding=(0,0,0)) 59 | 60 | self.linear = nn.Linear(in_features=256*4*4,out_features=400) 61 | 62 | self.relu4 = nn.ReLU() 63 | self.dropout = nn.Dropout(0.5) 64 | 65 | def forward(self, x): 66 | batch_size = x.size(0) 67 | #output = self.convs(x).view(batch_size, -1) 68 | x = self.conv1(x) 69 | x = self.bn1(x) 70 | x = self.relu1(x) 71 | x = self.pool1(x) 72 | x = self.conv2(x) 73 | 74 | x = self.bn2(x) 75 | x = self.relu2(x) 76 | x = self.pool2(x) 77 | 78 | 79 | x = self.conv3(x) 80 | 81 | x = self.bn3(x) 82 | x = self.relu3(x) 83 | x = self.pool3(x).view(batch_size,-1) 84 | 85 | x = self.linear(x) 86 | x = self.relu4(x) 87 | x = self.dropout(x) 88 | 89 | return x 90 | #output = self.convs(x) 91 | 92 | #print (output.size()) 93 | #ipdb.set_trace() 94 | #output = self.fc(output) 95 | #return output 96 | 97 | class Bottleneck(nn.Module): 98 | expansion = 4 99 | def __init__(self,inplanes,planes,stride=1,downsample=None): 100 | super(Bottleneck,self).__init__() 101 | self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False ) 102 | self.bn1 = nn.BatchNorm2d(planes) 103 | self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias = False) 104 | self.bn2 = nn.BatchNorm2d(planes) 105 | self.conv3 = nn.Conv2d(planes, planes*4,kernel_size=1,bias=False) 106 | self.bn3 = nn.BatchNorm2d(planes*4) 107 | self.relu = nn.ReLU(inplace=True) 108 | self.downsample = downsample 109 | self.stride = stride 110 | 111 | def forward(self,x): 112 | residual = x 113 | out = self.conv1(x) 114 | out = self.bn1(out) 115 | out = self.relu(out) 116 | 117 | out = self.conv2(out) 118 | out = self.bn2(out) 119 | out = self.relu(out) 120 | 121 | out = self.conv3(out) 122 | out = self.bn3(out) 123 | 124 | if self.downsample is not None: 125 | residual = self.downsample(x) 126 | 127 | out += residual 128 | out = self.relu(out) 129 | return out 130 | 131 | class ResNet(nn.Module): 132 | def __init__(self,block,layers,num_classes=400): 133 | self.inplanes = 64 134 | super(ResNet, self).__init__() 135 | self.conv1 = nn.Conv2d(3,64,kernel_size=7, stride=2, padding=3, bias=False) 136 | self.bn1 = nn.BatchNorm2d(64) 137 | self.relu = nn.ReLU(inplace=True) 138 | self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1) 139 | self.layer1 = self._make_layer(block,64,layers[0]) 140 | self.layer2 = self._make_layer(block,128,layers[1],stride=2) 141 | self.layer3 = self._make_layer(block,256,layers[2],stride=2) 142 | self.layer4 = self._make_layer(block,512,layers[3],stride=2) 143 | self.avgpool = nn.AvgPool2d(7, stride=1) 144 | self.fc = nn.Linear(512*block.expansion,num_classes) 145 | 146 | def _make_layer(self,block,planes,blocks,stride=1): 147 | downsample = None 148 | if stride != 1 or self.inplanes != planes*block.expansion: 149 | downsample = nn.Sequential ( 150 | nn.Conv2d(self.inplanes,planes * block.expansion , 151 | kernel_size = 1, stride = stride ,bias = False), 152 | nn.BatchNorm2d(planes * block.expansion), 153 | ) 154 | layers = [] 155 | layers.append(block(self.inplanes, planes, stride, downsample)) 156 | self.inplanes = planes * block.expansion 157 | for i in range(1,blocks): 158 | layers.append(block(self.inplanes,planes)) 159 | return nn.Sequential(*layers) 160 | 161 | def forward(self,x): 162 | x = self.conv1(x) 163 | x = self.bn1(x) 164 | x = self.relu(x) 165 | x = self.maxpool(x) 166 | print (x.shape) 167 | x = self.layer1(x) 168 | x = self.layer2(x) 169 | x = self.layer3(x) 170 | x = self.layer4(x) 171 | 172 | x = self.avgpool(x) 173 | x = x.view(x.size(0),-1) 174 | x = self.fc(x) 175 | 176 | return x 177 | 178 | def resnet50(pretrained=False, **kwargs): 179 | model = ResNet(Bottleneck, [3,4,6,3], **kwargs) 180 | return model 181 | 182 | if __name__ == '__main__': 183 | import torch 184 | from torch.autograd import Variable 185 | img = Variable(torch.randn(1,3,224, 224)) 186 | net = ResNet.resnet50() 187 | out = net(img) 188 | print(out.size()) 189 | 190 | -------------------------------------------------------------------------------- /lib/non_local_concatenation.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch import nn 3 | from torch.nn import functional as F 4 | 5 | 6 | class _NonLocalBlockND(nn.Module): 7 | def __init__(self, in_channels, inter_channels=None, dimension=3, sub_sample=True, bn_layer=True): 8 | super(_NonLocalBlockND, self).__init__() 9 | 10 | assert dimension in [1, 2, 3] 11 | 12 | self.dimension = dimension 13 | self.sub_sample = sub_sample 14 | 15 | self.in_channels = in_channels 16 | self.inter_channels = inter_channels 17 | 18 | if self.inter_channels is None: 19 | self.inter_channels = in_channels // 2 20 | if self.inter_channels == 0: 21 | self.inter_channels = 1 22 | 23 | if dimension == 3: 24 | conv_nd = nn.Conv3d 25 | max_pool_layer = nn.MaxPool3d(kernel_size=(1, 2, 2)) 26 | bn = nn.BatchNorm3d 27 | elif dimension == 2: 28 | conv_nd = nn.Conv2d 29 | max_pool_layer = nn.MaxPool2d(kernel_size=(2, 2)) 30 | bn = nn.BatchNorm2d 31 | else: 32 | conv_nd = nn.Conv1d 33 | max_pool_layer = nn.MaxPool1d(kernel_size=(2)) 34 | bn = nn.BatchNorm1d 35 | 36 | self.g = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 37 | kernel_size=1, stride=1, padding=0) 38 | 39 | if bn_layer: 40 | self.W = nn.Sequential( 41 | conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels, 42 | kernel_size=1, stride=1, padding=0), 43 | bn(self.in_channels) 44 | ) 45 | nn.init.constant(self.W[1].weight, 0) 46 | nn.init.constant(self.W[1].bias, 0) 47 | else: 48 | self.W = conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels, 49 | kernel_size=1, stride=1, padding=0) 50 | nn.init.constant(self.W.weight, 0) 51 | nn.init.constant(self.W.bias, 0) 52 | 53 | self.theta = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 54 | kernel_size=1, stride=1, padding=0) 55 | 56 | self.phi = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 57 | kernel_size=1, stride=1, padding=0) 58 | 59 | self.concat_project = nn.Sequential( 60 | nn.Conv2d(self.inter_channels * 2, 1, 1, 1, 0, bias=False), 61 | nn.ReLU() 62 | ) 63 | 64 | if sub_sample: 65 | self.g = nn.Sequential(self.g, max_pool_layer) 66 | self.phi = nn.Sequential(self.phi, max_pool_layer) 67 | 68 | def forward(self, x): 69 | ''' 70 | :param x: (b, c, t, h, w) 71 | :return: 72 | ''' 73 | 74 | batch_size = x.size(0) 75 | 76 | g_x = self.g(x).view(batch_size, self.inter_channels, -1) 77 | g_x = g_x.permute(0, 2, 1) 78 | 79 | # (b, c, N, 1) 80 | theta_x = self.theta(x).view(batch_size, self.inter_channels, -1, 1) 81 | # (b, c, 1, N) 82 | phi_x = self.phi(x).view(batch_size, self.inter_channels, 1, -1) 83 | 84 | h = theta_x.size(2) 85 | w = phi_x.size(3) 86 | theta_x = theta_x.repeat(1, 1, 1, w) 87 | phi_x = phi_x.repeat(1, 1, h, 1) 88 | 89 | concat_feature = torch.cat([theta_x, phi_x], dim=1) 90 | f = self.concat_project(concat_feature) 91 | b, _, h, w = f.size() 92 | f = f.view(b, h, w) 93 | 94 | N = f.size(-1) 95 | f_div_C = f / N 96 | 97 | y = torch.matmul(f_div_C, g_x) 98 | y = y.permute(0, 2, 1).contiguous() 99 | y = y.view(batch_size, self.inter_channels, *x.size()[2:]) 100 | W_y = self.W(y) 101 | z = W_y + x 102 | 103 | return z 104 | 105 | 106 | class NONLocalBlock1D(_NonLocalBlockND): 107 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 108 | super(NONLocalBlock1D, self).__init__(in_channels, 109 | inter_channels=inter_channels, 110 | dimension=1, sub_sample=sub_sample, 111 | bn_layer=bn_layer) 112 | 113 | 114 | class NONLocalBlock2D(_NonLocalBlockND): 115 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 116 | super(NONLocalBlock2D, self).__init__(in_channels, 117 | inter_channels=inter_channels, 118 | dimension=2, sub_sample=sub_sample, 119 | bn_layer=bn_layer) 120 | 121 | 122 | class NONLocalBlock3D(_NonLocalBlockND): 123 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 124 | super(NONLocalBlock3D, self).__init__(in_channels, 125 | inter_channels=inter_channels, 126 | dimension=3, sub_sample=sub_sample, 127 | bn_layer=bn_layer) 128 | 129 | 130 | if __name__ == '__main__': 131 | from torch.autograd import Variable 132 | import torch 133 | 134 | for (sub_sample, bn_layer) in [(True, True), (False, False), (True, False), (False, True)]: 135 | img = Variable(torch.zeros(2, 3, 20)) 136 | net = NONLocalBlock1D(3, sub_sample=sub_sample, bn_layer=bn_layer) 137 | out = net(img) 138 | print(out.size()) 139 | 140 | img = Variable(torch.zeros(2, 3, 20, 20)) 141 | net = NONLocalBlock2D(3, sub_sample=sub_sample, bn_layer=bn_layer) 142 | out = net(img) 143 | print(out.size()) 144 | 145 | img = Variable(torch.randn(2, 3, 8, 20, 20)) 146 | net = NONLocalBlock3D(3, sub_sample=sub_sample, bn_layer=bn_layer) 147 | out = net(img) 148 | print(out.size()) 149 | -------------------------------------------------------------------------------- /lib/non_local_dot_product.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch import nn 3 | from torch.nn import functional as F 4 | 5 | 6 | class _NonLocalBlockND(nn.Module): 7 | def __init__(self, in_channels, inter_channels=None, dimension=3, sub_sample=True, bn_layer=True): 8 | super(_NonLocalBlockND, self).__init__() 9 | 10 | assert dimension in [1, 2, 3] 11 | 12 | self.dimension = dimension 13 | self.sub_sample = sub_sample 14 | 15 | self.in_channels = in_channels 16 | self.inter_channels = inter_channels 17 | 18 | if self.inter_channels is None: 19 | self.inter_channels = in_channels // 2 20 | if self.inter_channels == 0: 21 | self.inter_channels = 1 22 | 23 | if dimension == 3: 24 | conv_nd = nn.Conv3d 25 | max_pool_layer = nn.MaxPool3d(kernel_size=(1, 2, 2)) 26 | bn = nn.BatchNorm3d 27 | elif dimension == 2: 28 | conv_nd = nn.Conv2d 29 | max_pool_layer = nn.MaxPool2d(kernel_size=(2, 2)) 30 | bn = nn.BatchNorm2d 31 | else: 32 | conv_nd = nn.Conv1d 33 | max_pool_layer = nn.MaxPool1d(kernel_size=(2)) 34 | bn = nn.BatchNorm1d 35 | 36 | self.g = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 37 | kernel_size=1, stride=1, padding=0) 38 | 39 | if bn_layer: 40 | self.W = nn.Sequential( 41 | conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels, 42 | kernel_size=1, stride=1, padding=0), 43 | bn(self.in_channels) 44 | ) 45 | nn.init.constant(self.W[1].weight, 0) 46 | nn.init.constant(self.W[1].bias, 0) 47 | else: 48 | self.W = conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels, 49 | kernel_size=1, stride=1, padding=0) 50 | nn.init.constant(self.W.weight, 0) 51 | nn.init.constant(self.W.bias, 0) 52 | 53 | self.theta = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 54 | kernel_size=1, stride=1, padding=0) 55 | 56 | self.phi = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 57 | kernel_size=1, stride=1, padding=0) 58 | 59 | if sub_sample: 60 | self.g = nn.Sequential(self.g, max_pool_layer) 61 | self.phi = nn.Sequential(self.phi, max_pool_layer) 62 | 63 | def forward(self, x): 64 | ''' 65 | :param x: (b, c, t, h, w) 66 | :return: 67 | ''' 68 | 69 | batch_size = x.size(0) 70 | 71 | g_x = self.g(x).view(batch_size, self.inter_channels, -1) 72 | g_x = g_x.permute(0, 2, 1) 73 | 74 | theta_x = self.theta(x).view(batch_size, self.inter_channels, -1) 75 | theta_x = theta_x.permute(0, 2, 1) 76 | phi_x = self.phi(x).view(batch_size, self.inter_channels, -1) 77 | f = torch.matmul(theta_x, phi_x) 78 | N = f.size(-1) 79 | f_div_C = f / N 80 | 81 | y = torch.matmul(f_div_C, g_x) 82 | y = y.permute(0, 2, 1).contiguous() 83 | y = y.view(batch_size, self.inter_channels, *x.size()[2:]) 84 | W_y = self.W(y) 85 | z = W_y + x 86 | 87 | return z 88 | 89 | 90 | class NONLocalBlock1D(_NonLocalBlockND): 91 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 92 | super(NONLocalBlock1D, self).__init__(in_channels, 93 | inter_channels=inter_channels, 94 | dimension=1, sub_sample=sub_sample, 95 | bn_layer=bn_layer) 96 | 97 | 98 | class NONLocalBlock2D(_NonLocalBlockND): 99 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 100 | super(NONLocalBlock2D, self).__init__(in_channels, 101 | inter_channels=inter_channels, 102 | dimension=2, sub_sample=sub_sample, 103 | bn_layer=bn_layer) 104 | 105 | 106 | class NONLocalBlock3D(_NonLocalBlockND): 107 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 108 | super(NONLocalBlock3D, self).__init__(in_channels, 109 | inter_channels=inter_channels, 110 | dimension=3, sub_sample=sub_sample, 111 | bn_layer=bn_layer) 112 | 113 | 114 | if __name__ == '__main__': 115 | from torch.autograd import Variable 116 | import torch 117 | 118 | for (sub_sample, bn_layer) in [(True, True), (False, False), (True, False), (False, True)]: 119 | img = Variable(torch.zeros(2, 3, 20)) 120 | net = NONLocalBlock1D(3, sub_sample=sub_sample, bn_layer=bn_layer) 121 | out = net(img) 122 | print(out.size()) 123 | 124 | img = Variable(torch.zeros(2, 3, 20, 20)) 125 | net = NONLocalBlock2D(3, sub_sample=sub_sample, bn_layer=bn_layer) 126 | out = net(img) 127 | print(out.size()) 128 | 129 | img = Variable(torch.randn(2, 3, 8, 20, 20)) 130 | net = NONLocalBlock3D(3, sub_sample=sub_sample, bn_layer=bn_layer) 131 | out = net(img) 132 | print(out.size()) 133 | 134 | 135 | 136 | -------------------------------------------------------------------------------- /lib/non_local_embedded_gaussian.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch import nn 3 | from torch.nn import functional as F 4 | 5 | 6 | class _NonLocalBlockND(nn.Module): 7 | def __init__(self, in_channels, inter_channels=None, dimension=3, sub_sample=True, bn_layer=True): 8 | super(_NonLocalBlockND, self).__init__() 9 | 10 | assert dimension in [1, 2, 3] 11 | 12 | self.dimension = dimension 13 | self.sub_sample = sub_sample 14 | 15 | self.in_channels = in_channels 16 | self.inter_channels = inter_channels 17 | 18 | if self.inter_channels is None: 19 | self.inter_channels = in_channels // 2 20 | if self.inter_channels == 0: 21 | self.inter_channels = 1 22 | 23 | if dimension == 3: 24 | conv_nd = nn.Conv3d 25 | max_pool_layer = nn.MaxPool3d(kernel_size=(1, 2, 2)) 26 | bn = nn.BatchNorm3d 27 | elif dimension == 2: 28 | conv_nd = nn.Conv2d 29 | max_pool_layer = nn.MaxPool2d(kernel_size=(2, 2)) 30 | bn = nn.BatchNorm2d 31 | else: 32 | conv_nd = nn.Conv1d 33 | max_pool_layer = nn.MaxPool1d(kernel_size=(2)) 34 | bn = nn.BatchNorm1d 35 | 36 | self.g = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 37 | kernel_size=1, stride=1, padding=0) 38 | 39 | if bn_layer: 40 | self.W = nn.Sequential( 41 | conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels, 42 | kernel_size=1, stride=1, padding=0), 43 | bn(self.in_channels) 44 | ) 45 | nn.init.constant(self.W[1].weight, 0) 46 | nn.init.constant(self.W[1].bias, 0) 47 | else: 48 | self.W = conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels, 49 | kernel_size=1, stride=1, padding=0) 50 | nn.init.constant(self.W.weight, 0) 51 | nn.init.constant(self.W.bias, 0) 52 | 53 | self.theta = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 54 | kernel_size=1, stride=1, padding=0) 55 | self.phi = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 56 | kernel_size=1, stride=1, padding=0) 57 | 58 | if sub_sample: 59 | self.g = nn.Sequential(self.g, max_pool_layer) 60 | self.phi = nn.Sequential(self.phi, max_pool_layer) 61 | 62 | def forward(self, x): 63 | ''' 64 | :param x: (b, c, t, h, w) 65 | :return: 66 | ''' 67 | 68 | batch_size = x.size(0) 69 | 70 | g_x = self.g(x).view(batch_size, self.inter_channels, -1) 71 | g_x = g_x.permute(0, 2, 1) 72 | 73 | theta_x = self.theta(x).view(batch_size, self.inter_channels, -1) 74 | theta_x = theta_x.permute(0, 2, 1) 75 | phi_x = self.phi(x).view(batch_size, self.inter_channels, -1) 76 | f = torch.matmul(theta_x, phi_x) 77 | f_div_C = F.softmax(f, dim=-1) 78 | 79 | y = torch.matmul(f_div_C, g_x) 80 | y = y.permute(0, 2, 1).contiguous() 81 | y = y.view(batch_size, self.inter_channels, *x.size()[2:]) 82 | W_y = self.W(y) 83 | z = W_y + x 84 | 85 | return z 86 | 87 | 88 | class NONLocalBlock1D(_NonLocalBlockND): 89 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 90 | super(NONLocalBlock1D, self).__init__(in_channels, 91 | inter_channels=inter_channels, 92 | dimension=1, sub_sample=sub_sample, 93 | bn_layer=bn_layer) 94 | 95 | 96 | class NONLocalBlock2D(_NonLocalBlockND): 97 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 98 | super(NONLocalBlock2D, self).__init__(in_channels, 99 | inter_channels=inter_channels, 100 | dimension=2, sub_sample=sub_sample, 101 | bn_layer=bn_layer) 102 | 103 | 104 | class NONLocalBlock3D(_NonLocalBlockND): 105 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 106 | super(NONLocalBlock3D, self).__init__(in_channels, 107 | inter_channels=inter_channels, 108 | dimension=3, sub_sample=sub_sample, 109 | bn_layer=bn_layer) 110 | 111 | 112 | if __name__ == '__main__': 113 | from torch.autograd import Variable 114 | import torch 115 | 116 | sub_sample = True 117 | bn_layer = True 118 | 119 | img = Variable(torch.zeros(2, 3, 20)) 120 | net = NONLocalBlock1D(3, sub_sample=sub_sample, bn_layer=bn_layer) 121 | out = net(img) 122 | print(out.size()) 123 | 124 | img = Variable(torch.zeros(2, 3, 20, 20)) 125 | net = NONLocalBlock2D(3, sub_sample=sub_sample, bn_layer=bn_layer) 126 | out = net(img) 127 | print(out.size()) 128 | 129 | img = Variable(torch.randn(2, 3, 10, 20, 20)) 130 | net = NONLocalBlock3D(3, sub_sample=sub_sample, bn_layer=bn_layer) 131 | out = net(img) 132 | print(out.size()) 133 | 134 | -------------------------------------------------------------------------------- /lib/non_local_gaussian.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch import nn 3 | from torch.nn import functional as F 4 | 5 | 6 | class _NonLocalBlockND(nn.Module): 7 | def __init__(self, in_channels, inter_channels=None, dimension=3, sub_sample=True, bn_layer=True): 8 | super(_NonLocalBlockND, self).__init__() 9 | 10 | assert dimension in [1, 2, 3] 11 | 12 | self.dimension = dimension 13 | self.sub_sample = sub_sample 14 | 15 | self.in_channels = in_channels 16 | self.inter_channels = inter_channels 17 | 18 | if self.inter_channels is None: 19 | self.inter_channels = in_channels // 2 20 | if self.inter_channels == 0: 21 | self.inter_channels = 1 22 | 23 | if dimension == 3: 24 | conv_nd = nn.Conv3d 25 | max_pool_layer = nn.MaxPool3d(kernel_size=(1, 2, 2)) 26 | bn = nn.BatchNorm3d 27 | elif dimension == 2: 28 | conv_nd = nn.Conv2d 29 | max_pool_layer = nn.MaxPool2d(kernel_size=(2, 2)) 30 | bn = nn.BatchNorm2d 31 | else: 32 | conv_nd = nn.Conv1d 33 | max_pool_layer = nn.MaxPool1d(kernel_size=(2)) 34 | bn = nn.BatchNorm1d 35 | 36 | self.g = conv_nd(in_channels=self.in_channels, out_channels=self.inter_channels, 37 | kernel_size=1, stride=1, padding=0) 38 | 39 | if bn_layer: 40 | self.W = nn.Sequential( 41 | conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels, 42 | kernel_size=1, stride=1, padding=0), 43 | bn(self.in_channels) 44 | ) 45 | nn.init.constant(self.W[1].weight, 0) 46 | nn.init.constant(self.W[1].bias, 0) 47 | else: 48 | self.W = conv_nd(in_channels=self.inter_channels, out_channels=self.in_channels, 49 | kernel_size=1, stride=1, padding=0) 50 | nn.init.constant(self.W.weight, 0) 51 | nn.init.constant(self.W.bias, 0) 52 | 53 | if sub_sample: 54 | self.g = nn.Sequential(self.g, max_pool_layer) 55 | self.phi = max_pool_layer 56 | 57 | def forward(self, x): 58 | ''' 59 | :param x: (b, c, t, h, w) 60 | :return: 61 | ''' 62 | 63 | batch_size = x.size(0) 64 | 65 | g_x = self.g(x).view(batch_size, self.inter_channels, -1) 66 | 67 | g_x = g_x.permute(0, 2, 1) 68 | 69 | theta_x = x.view(batch_size, self.in_channels, -1) 70 | theta_x = theta_x.permute(0, 2, 1) 71 | 72 | if self.sub_sample: 73 | phi_x = self.phi(x).view(batch_size, self.in_channels, -1) 74 | else: 75 | phi_x = x.view(batch_size, self.in_channels, -1) 76 | 77 | f = torch.matmul(theta_x, phi_x) 78 | f_div_C = F.softmax(f, dim=-1) 79 | 80 | y = torch.matmul(f_div_C, g_x) 81 | y = y.permute(0, 2, 1).contiguous() 82 | y = y.view(batch_size, self.inter_channels, *x.size()[2:]) 83 | W_y = self.W(y) 84 | z = W_y + x 85 | 86 | return z 87 | 88 | 89 | class NONLocalBlock1D(_NonLocalBlockND): 90 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 91 | super(NONLocalBlock1D, self).__init__(in_channels, 92 | inter_channels=inter_channels, 93 | dimension=1, sub_sample=sub_sample, 94 | bn_layer=bn_layer) 95 | 96 | 97 | class NONLocalBlock2D(_NonLocalBlockND): 98 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 99 | super(NONLocalBlock2D, self).__init__(in_channels, 100 | inter_channels=inter_channels, 101 | dimension=2, sub_sample=sub_sample, 102 | bn_layer=bn_layer) 103 | 104 | 105 | class NONLocalBlock3D(_NonLocalBlockND): 106 | def __init__(self, in_channels, inter_channels=None, sub_sample=True, bn_layer=True): 107 | super(NONLocalBlock3D, self).__init__(in_channels, 108 | inter_channels=inter_channels, 109 | dimension=3, sub_sample=sub_sample, 110 | bn_layer=bn_layer) 111 | 112 | 113 | if __name__ == '__main__': 114 | from torch.autograd import Variable 115 | import torch 116 | 117 | for (sub_sample, bn_layer) in [(True, True), (False, False), (True, False), (False, True)]: 118 | img = Variable(torch.zeros(2, 3, 20)) 119 | net = NONLocalBlock1D(3, sub_sample=sub_sample, bn_layer=bn_layer) 120 | out = net(img) 121 | print(out.size()) 122 | 123 | img = Variable(torch.zeros(2, 3, 20, 20)) 124 | net = NONLocalBlock2D(3, sub_sample=sub_sample, bn_layer=bn_layer) 125 | out = net(img) 126 | print(out.size()) 127 | 128 | img = Variable(torch.randn(2, 3, 8, 20, 20)) 129 | net = NONLocalBlock3D(3, sub_sample=sub_sample, bn_layer=bn_layer) 130 | out = net(img) 131 | print(out.size()) 132 | 133 | 134 | 135 | 136 | 137 | -------------------------------------------------------------------------------- /videoDataset.py: -------------------------------------------------------------------------------- 1 | """ 2 | PyTorch Video Dataset Class for loading videos using PyTorch 3 | Dataloader. This Dataset assumes that video files are Preprocessed 4 | by being trimmed over time and resizing the frames. 5 | 6 | Mohsen Fayyaz __ Sensifai Vision Group 7 | http://www.Sensifai.com 8 | 9 | If you find this code useful, please star the repository. 10 | """ 11 | 12 | from __future__ import print_function, division 13 | import cv2 14 | import os 15 | import torch 16 | import numpy as np 17 | import pickle 18 | import random 19 | import h5py 20 | from torch.utils.data import Dataset, DataLoader 21 | from torchvision import transforms, utils 22 | 23 | class videoDataset(Dataset): 24 | """Dataset Class for Loading Video""" 25 | 26 | def __init__(self, clipsListFile, channels, frames_num, xSize, ySize, transform=None): 27 | 28 | clipsList = h5py.File(clipsListFile,'r') 29 | self.keys = list(clipsList.keys()) 30 | random.shuffle(self.keys) 31 | self.clipsList = clipsList 32 | #self.rootDir = roo 33 | self.channels = channels 34 | self.frames_num = frames_num 35 | self.xSize = xSize 36 | self.ySize = ySize 37 | self.transform = transform 38 | 39 | def __len__(self): 40 | return len(self.keys) 41 | 42 | def __getitem__(self, idx): 43 | key = self.keys[idx] 44 | video_clip = np.array(self.clipsList[str(key)]['clip'],dtype=np.float32) 45 | video_label = np.array(self.clipsList[str(key)]['label']).astype(float) 46 | if self.transform: 47 | clip = self.transform(clip) 48 | 49 | sample = {'clip':torch.from_numpy(video_clip),'label':torch.tensor(video_label,dtype=torch.long)} 50 | 51 | return sample 52 | -------------------------------------------------------------------------------- /waste/__pycache__/videoDataset.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/17Skye17/Non-local-Neural-Networks-Pytorch/9a2ddbafad69f2ca79764d3c29469d8f84f882af/waste/__pycache__/videoDataset.cpython-36.pyc -------------------------------------------------------------------------------- /waste/caffe.list: -------------------------------------------------------------------------------- 1 | res_conv1_bn_b 2 | res_conv1_bn_s 3 | res5_2_branch2a_bn_rm 4 | res4_4_branch2b_w 5 | res5_1_branch2c_bn_rm 6 | res3_1_branch2c_bn_s 7 | res5_1_branch2b_w 8 | res3_0_branch2a_bn_riv 9 | res2_2_branch2b_bn_rm 10 | res5_0_branch2c_bn_riv 11 | res3_2_branch2a_w 12 | res4_5_branch2c_bn_rm 13 | res3_2_branch2c_w 14 | res4_0_branch2c_bn_rm 15 | res2_2_branch2a_bn_riv 16 | res4_5_branch2a_w 17 | res2_2_branch2a_bn_b 18 | res2_2_branch2a_bn_s 19 | res3_2_branch2a_bn_rm 20 | res4_0_branch2b_w 21 | res3_2_branch2a_bn_b 22 | res4_1_branch2b_bn_riv 23 | res4_4_branch2b_bn_b 24 | res3_1_branch2c_bn_b 25 | res4_0_branch2c_bn_riv 26 | res3_3_branch2b_bn_riv 27 | res5_1_branch2a_bn_riv 28 | res4_4_branch2b_bn_s 29 | res5_0_branch1_bn_rm 30 | res2_0_branch2a_bn_rm 31 | res4_4_branch2a_bn_rm 32 | res4_0_branch2a_bn_rm 33 | res3_0_branch1_w 34 | res4_5_branch2c_bn_riv 35 | res2_1_branch2a_bn_rm 36 | res3_3_branch2a_bn_rm 37 | res4_1_branch2a_w 38 | res3_0_branch1_bn_rm 39 | res4_1_branch2c_w 40 | res4_5_branch2c_w 41 | res3_3_branch2c_bn_b 42 | res3_3_branch2c_bn_s 43 | res5_1_branch2a_w 44 | res5_0_branch2a_bn_riv 45 | res2_1_branch2c_bn_riv 46 | res4_5_branch2b_bn_b 47 | res4_5_branch2b_bn_s 48 | res4_4_branch2c_bn_b 49 | res5_1_branch2c_w 50 | res4_4_branch2a_w 51 | res4_4_branch2b_bn_rm 52 | res5_1_branch2b_bn_rm 53 | res5_1_branch2b_bn_b 54 | res3_1_branch2a_bn_riv 55 | res5_1_branch2b_bn_s 56 | res4_4_branch2c_w 57 | res2_2_branch2c_bn_riv 58 | res4_2_branch2c_bn_s 59 | res4_2_branch2c_bn_b 60 | res2_2_branch2a_w 61 | res2_0_branch2b_bn_riv 62 | res5_2_branch2b_bn_riv 63 | res5_0_branch1_bn_riv 64 | res2_2_branch2a_bn_rm 65 | res4_1_branch2a_bn_s 66 | res2_1_branch2c_bn_b 67 | res3_2_branch2b_bn_b 68 | res3_1_branch2b_bn_s 69 | res4_1_branch2a_bn_b 70 | res4_4_branch2c_bn_s 71 | res2_1_branch2c_bn_s 72 | res3_2_branch2b_bn_s 73 | res5_1_branch2c_bn_s 74 | res5_1_branch2c_bn_b 75 | res2_1_branch2b_bn_rm 76 | res4_1_branch2b_bn_b 77 | res2_0_branch2a_w 78 | res4_1_branch2b_bn_s 79 | res5_0_branch2b_w 80 | res3_1_branch2c_bn_rm 81 | res4_4_branch2a_bn_s 82 | res4_2_branch2b_bn_s 83 | res4_4_branch2a_bn_b 84 | res4_2_branch2b_bn_b 85 | res4_3_branch2c_bn_rm 86 | res4_3_branch2c_bn_riv 87 | res4_2_branch2c_bn_rm 88 | res3_0_branch2a_bn_s 89 | res3_0_branch2a_bn_b 90 | res3_3_branch2a_bn_riv 91 | res4_5_branch2a_bn_rm 92 | res5_0_branch2b_bn_riv 93 | res3_1_branch2a_bn_b 94 | res3_1_branch2a_bn_s 95 | res5_1_branch2a_bn_s 96 | res4_0_branch2a_bn_riv 97 | res5_2_branch2a_bn_riv 98 | res5_1_branch2a_bn_b 99 | res4_2_branch2b_bn_riv 100 | res4_5_branch2b_bn_riv 101 | res2_2_branch2b_w 102 | res3_3_branch2a_bn_b 103 | res2_1_branch2a_bn_riv 104 | res4_2_branch2a_bn_riv 105 | res3_3_branch2a_bn_s 106 | res3_0_branch2b_w 107 | res4_5_branch2a_bn_riv 108 | res4_0_branch2b_bn_riv 109 | res5_0_branch2c_w 110 | res5_2_branch2a_w 111 | res2_0_branch2c_bn_rm 112 | res5_0_branch1_w 113 | res2_2_branch2c_bn_b 114 | res4_5_branch2c_bn_b 115 | res4_3_branch2b_bn_s 116 | res3_2_branch2c_bn_riv 117 | res5_0_branch2a_w 118 | res4_5_branch2c_bn_s 119 | res3_3_branch2c_bn_rm 120 | res4_3_branch2b_bn_b 121 | res2_0_branch2b_bn_rm 122 | res5_1_branch2a_bn_rm 123 | res5_2_branch2b_w 124 | res4_4_branch2c_bn_riv 125 | res4_0_branch2c_bn_b 126 | res3_3_branch2b_bn_s 127 | res4_2_branch2a_w 128 | res4_0_branch2c_bn_s 129 | res4_3_branch2b_bn_rm 130 | res3_3_branch2b_bn_b 131 | res5_1_branch2c_bn_riv 132 | res4_1_branch2a_bn_riv 133 | conv1_w 134 | res2_0_branch1_bn_riv 135 | res3_2_branch2c_bn_rm 136 | res5_2_branch2c_bn_riv 137 | res4_1_branch2c_bn_b 138 | res5_0_branch2b_bn_rm 139 | res4_4_branch2b_bn_riv 140 | res4_1_branch2c_bn_s 141 | res4_1_branch2c_bn_riv 142 | res3_1_branch2b_bn_rm 143 | res2_1_branch2c_w 144 | res2_0_branch2a_bn_riv 145 | res5_0_branch2a_bn_rm 146 | res5_0_branch2a_bn_b 147 | res3_1_branch2b_bn_b 148 | res3_2_branch2a_bn_s 149 | res5_0_branch2a_bn_s 150 | res4_0_branch2b_bn_s 151 | res4_2_branch2a_bn_b 152 | res5_2_branch2b_bn_s 153 | res4_3_branch2c_bn_s 154 | res5_0_branch2b_bn_s 155 | res4_2_branch2a_bn_s 156 | res5_2_branch2b_bn_b 157 | res4_3_branch2c_bn_b 158 | res3_0_branch2b_bn_rm 159 | res5_0_branch2b_bn_b 160 | res5_2_branch2a_bn_b 161 | res3_3_branch2c_bn_riv 162 | res2_0_branch1_bn_s 163 | res5_2_branch2a_bn_s 164 | res2_0_branch2c_bn_riv 165 | res2_0_branch1_bn_b 166 | res4_0_branch2b_bn_rm 167 | res4_0_branch1_bn_s 168 | res4_2_branch2a_bn_rm 169 | res2_0_branch2b_w 170 | res4_0_branch1_bn_b 171 | res4_1_branch2c_bn_rm 172 | res4_3_branch2a_bn_rm 173 | res4_2_branch2b_w 174 | res3_0_branch1_bn_b 175 | res3_0_branch1_bn_s 176 | res4_1_branch2b_bn_rm 177 | res4_0_branch2b_bn_b 178 | res3_1_branch2c_bn_riv 179 | res3_0_branch2c_bn_b 180 | res3_0_branch2a_bn_rm 181 | res5_0_branch2c_bn_s 182 | res4_2_branch2b_bn_rm 183 | res5_2_branch2c_w 184 | res3_0_branch2c_bn_s 185 | res5_0_branch2c_bn_b 186 | res2_2_branch2b_bn_riv 187 | res4_3_branch2a_bn_riv 188 | res3_0_branch2c_w 189 | res2_2_branch2c_w 190 | res3_0_branch2b_bn_riv 191 | res3_0_branch2a_w 192 | res2_0_branch2a_bn_s 193 | res4_3_branch2a_bn_b 194 | res2_1_branch2a_bn_s 195 | res3_0_branch2c_bn_rm 196 | res3_2_branch2c_bn_b 197 | res2_0_branch2a_bn_b 198 | res4_5_branch2b_bn_rm 199 | res2_1_branch2a_bn_b 200 | res4_2_branch2c_w 201 | res3_1_branch2b_w 202 | res3_2_branch2c_bn_s 203 | res3_1_branch2a_bn_rm 204 | res3_3_branch2c_w 205 | res2_0_branch2b_bn_b 206 | res4_0_branch2a_bn_s 207 | res3_3_branch2a_w 208 | res3_1_branch2b_bn_riv 209 | res2_0_branch2b_bn_s 210 | res4_0_branch2a_bn_b 211 | res5_2_branch2b_bn_rm 212 | res2_0_branch2c_bn_b 213 | res3_3_branch2b_bn_rm 214 | res2_0_branch2c_bn_s 215 | res4_3_branch2b_w 216 | res3_0_branch2b_bn_b 217 | res2_0_branch1_w 218 | res2_1_branch2c_bn_rm 219 | res3_0_branch2b_bn_s 220 | res5_2_branch2c_bn_rm 221 | res_conv1_bn_riv 222 | res4_4_branch2a_bn_riv 223 | res5_1_branch2b_bn_riv 224 | res5_2_branch2c_bn_s 225 | res2_0_branch2c_w 226 | res5_2_branch2c_bn_b 227 | res4_3_branch2a_bn_s 228 | res4_3_branch2b_bn_riv 229 | res4_2_branch2c_bn_riv 230 | res3_2_branch2b_w 231 | res2_1_branch2a_w 232 | res4_0_branch1_w 233 | res3_2_branch2b_bn_riv 234 | res4_5_branch2b_w 235 | res2_0_branch1_bn_rm 236 | res2_2_branch2b_bn_s 237 | res_conv1_bn_rm 238 | res3_2_branch2b_bn_rm 239 | res2_2_branch2b_bn_b 240 | res2_2_branch2c_bn_s 241 | res4_0_branch1_bn_rm 242 | res4_0_branch1_bn_riv 243 | res5_0_branch1_bn_s 244 | res4_0_branch2c_w 245 | res2_1_branch2b_bn_b 246 | res5_0_branch2c_bn_rm 247 | res5_0_branch1_bn_b 248 | res4_0_branch2a_w 249 | res4_4_branch2c_bn_rm 250 | res2_1_branch2b_bn_s 251 | res3_0_branch1_bn_riv 252 | res4_1_branch2a_bn_rm 253 | res3_0_branch2c_bn_riv 254 | res3_1_branch2a_w 255 | res2_1_branch2b_w 256 | res3_1_branch2c_w 257 | res3_2_branch2a_bn_riv 258 | res2_2_branch2c_bn_rm 259 | pred_b 260 | res3_3_branch2b_w 261 | res2_1_branch2b_bn_riv 262 | pred_w 263 | res4_3_branch2a_w 264 | res4_1_branch2b_w 265 | res4_3_branch2c_w 266 | res4_5_branch2a_bn_s 267 | res4_5_branch2a_bn_b 268 | 267 267 269 | -------------------------------------------------------------------------------- /waste/caffe.lst: -------------------------------------------------------------------------------- 1 | res_conv1_bn_b 2 | res_conv1_bn_s 3 | res5_2_branch2a_bn_rm 4 | res4_4_branch2b_w 5 | res5_1_branch2c_bn_rm 6 | res3_1_branch2c_bn_s 7 | res5_1_branch2b_w 8 | res3_0_branch2a_bn_riv 9 | res2_2_branch2b_bn_rm 10 | res5_0_branch2c_bn_riv 11 | res3_2_branch2a_w 12 | res4_5_branch2c_bn_rm 13 | res3_2_branch2c_w 14 | res4_0_branch2c_bn_rm 15 | res2_2_branch2a_bn_riv 16 | res4_5_branch2a_w 17 | res2_2_branch2a_bn_b 18 | res2_2_branch2a_bn_s 19 | res3_2_branch2a_bn_rm 20 | res4_0_branch2b_w 21 | res3_2_branch2a_bn_b 22 | res4_1_branch2b_bn_riv 23 | res4_4_branch2b_bn_b 24 | res3_1_branch2c_bn_b 25 | res4_0_branch2c_bn_riv 26 | res3_3_branch2b_bn_riv 27 | res5_1_branch2a_bn_riv 28 | res4_4_branch2b_bn_s 29 | res5_0_branch1_bn_rm 30 | res2_0_branch2a_bn_rm 31 | res4_4_branch2a_bn_rm 32 | res4_0_branch2a_bn_rm 33 | res3_0_branch1_w 34 | res4_5_branch2c_bn_riv 35 | res2_1_branch2a_bn_rm 36 | res3_3_branch2a_bn_rm 37 | res4_1_branch2a_w 38 | res3_0_branch1_bn_rm 39 | res4_1_branch2c_w 40 | res4_5_branch2c_w 41 | res3_3_branch2c_bn_b 42 | res3_3_branch2c_bn_s 43 | res5_1_branch2a_w 44 | res5_0_branch2a_bn_riv 45 | res2_1_branch2c_bn_riv 46 | res4_5_branch2b_bn_b 47 | res4_5_branch2b_bn_s 48 | res4_4_branch2c_bn_b 49 | res5_1_branch2c_w 50 | res4_4_branch2a_w 51 | res4_4_branch2b_bn_rm 52 | res5_1_branch2b_bn_rm 53 | res5_1_branch2b_bn_b 54 | res3_1_branch2a_bn_riv 55 | res5_1_branch2b_bn_s 56 | res4_4_branch2c_w 57 | res2_2_branch2c_bn_riv 58 | res4_2_branch2c_bn_s 59 | res4_2_branch2c_bn_b 60 | res2_2_branch2a_w 61 | res2_0_branch2b_bn_riv 62 | res5_2_branch2b_bn_riv 63 | res5_0_branch1_bn_riv 64 | res2_2_branch2a_bn_rm 65 | res4_1_branch2a_bn_s 66 | res2_1_branch2c_bn_b 67 | res3_2_branch2b_bn_b 68 | res3_1_branch2b_bn_s 69 | res4_1_branch2a_bn_b 70 | res4_4_branch2c_bn_s 71 | res2_1_branch2c_bn_s 72 | res3_2_branch2b_bn_s 73 | res5_1_branch2c_bn_s 74 | res5_1_branch2c_bn_b 75 | res2_1_branch2b_bn_rm 76 | res4_1_branch2b_bn_b 77 | res2_0_branch2a_w 78 | res4_1_branch2b_bn_s 79 | res5_0_branch2b_w 80 | res3_1_branch2c_bn_rm 81 | res4_4_branch2a_bn_s 82 | res4_2_branch2b_bn_s 83 | res4_4_branch2a_bn_b 84 | res4_2_branch2b_bn_b 85 | res4_3_branch2c_bn_rm 86 | res4_3_branch2c_bn_riv 87 | res4_2_branch2c_bn_rm 88 | res3_0_branch2a_bn_s 89 | res3_0_branch2a_bn_b 90 | res3_3_branch2a_bn_riv 91 | res4_5_branch2a_bn_rm 92 | res5_0_branch2b_bn_riv 93 | res3_1_branch2a_bn_b 94 | res3_1_branch2a_bn_s 95 | res5_1_branch2a_bn_s 96 | res4_0_branch2a_bn_riv 97 | res5_2_branch2a_bn_riv 98 | res5_1_branch2a_bn_b 99 | res4_2_branch2b_bn_riv 100 | res4_5_branch2b_bn_riv 101 | res2_2_branch2b_w 102 | res3_3_branch2a_bn_b 103 | res2_1_branch2a_bn_riv 104 | res4_2_branch2a_bn_riv 105 | res3_3_branch2a_bn_s 106 | res3_0_branch2b_w 107 | res4_5_branch2a_bn_riv 108 | res4_0_branch2b_bn_riv 109 | res5_0_branch2c_w 110 | res5_2_branch2a_w 111 | res2_0_branch2c_bn_rm 112 | res5_0_branch1_w 113 | res2_2_branch2c_bn_b 114 | res4_5_branch2c_bn_b 115 | res4_3_branch2b_bn_s 116 | res3_2_branch2c_bn_riv 117 | res5_0_branch2a_w 118 | res4_5_branch2c_bn_s 119 | res3_3_branch2c_bn_rm 120 | res4_3_branch2b_bn_b 121 | res2_0_branch2b_bn_rm 122 | res5_1_branch2a_bn_rm 123 | res5_2_branch2b_w 124 | res4_4_branch2c_bn_riv 125 | res4_0_branch2c_bn_b 126 | res3_3_branch2b_bn_s 127 | res4_2_branch2a_w 128 | res4_0_branch2c_bn_s 129 | res4_3_branch2b_bn_rm 130 | res3_3_branch2b_bn_b 131 | res5_1_branch2c_bn_riv 132 | res4_1_branch2a_bn_riv 133 | conv1_w 134 | res2_0_branch1_bn_riv 135 | res3_2_branch2c_bn_rm 136 | res5_2_branch2c_bn_riv 137 | res4_1_branch2c_bn_b 138 | res5_0_branch2b_bn_rm 139 | res4_4_branch2b_bn_riv 140 | res4_1_branch2c_bn_s 141 | res4_1_branch2c_bn_riv 142 | res3_1_branch2b_bn_rm 143 | res2_1_branch2c_w 144 | res2_0_branch2a_bn_riv 145 | res5_0_branch2a_bn_rm 146 | res5_0_branch2a_bn_b 147 | res3_1_branch2b_bn_b 148 | res3_2_branch2a_bn_s 149 | res5_0_branch2a_bn_s 150 | res4_0_branch2b_bn_s 151 | res4_2_branch2a_bn_b 152 | res5_2_branch2b_bn_s 153 | res4_3_branch2c_bn_s 154 | res5_0_branch2b_bn_s 155 | res4_2_branch2a_bn_s 156 | res5_2_branch2b_bn_b 157 | res4_3_branch2c_bn_b 158 | res3_0_branch2b_bn_rm 159 | res5_0_branch2b_bn_b 160 | res5_2_branch2a_bn_b 161 | res3_3_branch2c_bn_riv 162 | res2_0_branch1_bn_s 163 | res5_2_branch2a_bn_s 164 | res2_0_branch2c_bn_riv 165 | res2_0_branch1_bn_b 166 | res4_0_branch2b_bn_rm 167 | res4_0_branch1_bn_s 168 | res4_2_branch2a_bn_rm 169 | res2_0_branch2b_w 170 | res4_0_branch1_bn_b 171 | res4_1_branch2c_bn_rm 172 | res4_3_branch2a_bn_rm 173 | res4_2_branch2b_w 174 | res3_0_branch1_bn_b 175 | res3_0_branch1_bn_s 176 | res4_1_branch2b_bn_rm 177 | res4_0_branch2b_bn_b 178 | res3_1_branch2c_bn_riv 179 | res3_0_branch2c_bn_b 180 | res3_0_branch2a_bn_rm 181 | res5_0_branch2c_bn_s 182 | res4_2_branch2b_bn_rm 183 | res5_2_branch2c_w 184 | res3_0_branch2c_bn_s 185 | res5_0_branch2c_bn_b 186 | res2_2_branch2b_bn_riv 187 | res4_3_branch2a_bn_riv 188 | res3_0_branch2c_w 189 | res2_2_branch2c_w 190 | res3_0_branch2b_bn_riv 191 | res3_0_branch2a_w 192 | res2_0_branch2a_bn_s 193 | res4_3_branch2a_bn_b 194 | res2_1_branch2a_bn_s 195 | res3_0_branch2c_bn_rm 196 | res3_2_branch2c_bn_b 197 | res2_0_branch2a_bn_b 198 | res4_5_branch2b_bn_rm 199 | res2_1_branch2a_bn_b 200 | res4_2_branch2c_w 201 | res3_1_branch2b_w 202 | res3_2_branch2c_bn_s 203 | res3_1_branch2a_bn_rm 204 | res3_3_branch2c_w 205 | res2_0_branch2b_bn_b 206 | res4_0_branch2a_bn_s 207 | res3_3_branch2a_w 208 | res3_1_branch2b_bn_riv 209 | res2_0_branch2b_bn_s 210 | res4_0_branch2a_bn_b 211 | res5_2_branch2b_bn_rm 212 | res2_0_branch2c_bn_b 213 | res3_3_branch2b_bn_rm 214 | res2_0_branch2c_bn_s 215 | res4_3_branch2b_w 216 | res3_0_branch2b_bn_b 217 | res2_0_branch1_w 218 | res2_1_branch2c_bn_rm 219 | res3_0_branch2b_bn_s 220 | res5_2_branch2c_bn_rm 221 | res_conv1_bn_riv 222 | res4_4_branch2a_bn_riv 223 | res5_1_branch2b_bn_riv 224 | res5_2_branch2c_bn_s 225 | res2_0_branch2c_w 226 | res5_2_branch2c_bn_b 227 | res4_3_branch2a_bn_s 228 | res4_3_branch2b_bn_riv 229 | res4_2_branch2c_bn_riv 230 | res3_2_branch2b_w 231 | res2_1_branch2a_w 232 | res4_0_branch1_w 233 | res3_2_branch2b_bn_riv 234 | res4_5_branch2b_w 235 | res2_0_branch1_bn_rm 236 | res2_2_branch2b_bn_s 237 | res_conv1_bn_rm 238 | res3_2_branch2b_bn_rm 239 | res2_2_branch2b_bn_b 240 | res2_2_branch2c_bn_s 241 | res4_0_branch1_bn_rm 242 | res4_0_branch1_bn_riv 243 | res5_0_branch1_bn_s 244 | res4_0_branch2c_w 245 | res2_1_branch2b_bn_b 246 | res5_0_branch2c_bn_rm 247 | res5_0_branch1_bn_b 248 | res4_0_branch2a_w 249 | res4_4_branch2c_bn_rm 250 | res2_1_branch2b_bn_s 251 | res3_0_branch1_bn_riv 252 | res4_1_branch2a_bn_rm 253 | res3_0_branch2c_bn_riv 254 | res3_1_branch2a_w 255 | res2_1_branch2b_w 256 | res3_1_branch2c_w 257 | res3_2_branch2a_bn_riv 258 | res2_2_branch2c_bn_rm 259 | pred_b 260 | res3_3_branch2b_w 261 | res2_1_branch2b_bn_riv 262 | pred_w 263 | res4_3_branch2a_w 264 | res4_1_branch2b_w 265 | res4_3_branch2c_w 266 | res4_5_branch2a_bn_s 267 | res4_5_branch2a_bn_b 268 | -------------------------------------------------------------------------------- /waste/caffe_params.lst: -------------------------------------------------------------------------------- 1 | res_conv1_bn_b 2 | res_conv1_bn_s 3 | res4_4_branch2b_w 4 | res3_1_branch2c_bn_s 5 | res5_1_branch2b_w 6 | res3_2_branch2a_w 7 | res3_2_branch2c_w 8 | res4_5_branch2a_w 9 | res2_2_branch2a_bn_b 10 | res2_2_branch2a_bn_s 11 | res4_0_branch2b_w 12 | res3_2_branch2a_bn_b 13 | res4_4_branch2b_bn_b 14 | res3_1_branch2c_bn_b 15 | res4_4_branch2b_bn_s 16 | res3_0_branch1_w 17 | res4_1_branch2a_w 18 | res4_1_branch2c_w 19 | res4_5_branch2c_w 20 | res3_3_branch2c_bn_b 21 | res3_3_branch2c_bn_s 22 | res5_1_branch2a_w 23 | res4_5_branch2b_bn_b 24 | res4_5_branch2b_bn_s 25 | res4_4_branch2c_bn_b 26 | res5_1_branch2c_w 27 | res4_4_branch2a_w 28 | res5_1_branch2b_bn_b 29 | res5_1_branch2b_bn_s 30 | res4_4_branch2c_w 31 | res4_2_branch2c_bn_s 32 | res4_2_branch2c_bn_b 33 | res2_2_branch2a_w 34 | res4_1_branch2a_bn_s 35 | res2_1_branch2c_bn_b 36 | res3_2_branch2b_bn_b 37 | res3_1_branch2b_bn_s 38 | res4_1_branch2a_bn_b 39 | res4_4_branch2c_bn_s 40 | res2_1_branch2c_bn_s 41 | res3_2_branch2b_bn_s 42 | res5_1_branch2c_bn_s 43 | res5_1_branch2c_bn_b 44 | res4_1_branch2b_bn_b 45 | res2_0_branch2a_w 46 | res4_1_branch2b_bn_s 47 | res5_0_branch2b_w 48 | res4_4_branch2a_bn_s 49 | res4_2_branch2b_bn_s 50 | res4_4_branch2a_bn_b 51 | res4_2_branch2b_bn_b 52 | res3_0_branch2a_bn_s 53 | res3_0_branch2a_bn_b 54 | res3_1_branch2a_bn_b 55 | res3_1_branch2a_bn_s 56 | res5_1_branch2a_bn_s 57 | res5_1_branch2a_bn_b 58 | res2_2_branch2b_w 59 | res3_3_branch2a_bn_b 60 | res3_3_branch2a_bn_s 61 | res3_0_branch2b_w 62 | res5_0_branch2c_w 63 | res5_2_branch2a_w 64 | res5_0_branch1_w 65 | res2_2_branch2c_bn_b 66 | res4_5_branch2c_bn_b 67 | res4_3_branch2b_bn_s 68 | res5_0_branch2a_w 69 | res4_5_branch2c_bn_s 70 | res4_3_branch2b_bn_b 71 | res5_2_branch2b_w 72 | res4_0_branch2c_bn_b 73 | res3_3_branch2b_bn_s 74 | res4_2_branch2a_w 75 | res4_0_branch2c_bn_s 76 | res3_3_branch2b_bn_b 77 | conv1_w 78 | res4_1_branch2c_bn_b 79 | res4_1_branch2c_bn_s 80 | res2_1_branch2c_w 81 | res5_0_branch2a_bn_b 82 | res3_1_branch2b_bn_b 83 | res3_2_branch2a_bn_s 84 | res5_0_branch2a_bn_s 85 | res4_0_branch2b_bn_s 86 | res4_2_branch2a_bn_b 87 | res5_2_branch2b_bn_s 88 | res4_3_branch2c_bn_s 89 | res5_0_branch2b_bn_s 90 | res4_2_branch2a_bn_s 91 | res5_2_branch2b_bn_b 92 | res4_3_branch2c_bn_b 93 | res5_0_branch2b_bn_b 94 | res5_2_branch2a_bn_b 95 | res2_0_branch1_bn_s 96 | res5_2_branch2a_bn_s 97 | res2_0_branch1_bn_b 98 | res4_0_branch1_bn_s 99 | res2_0_branch2b_w 100 | res4_0_branch1_bn_b 101 | res4_2_branch2b_w 102 | res3_0_branch1_bn_b 103 | res3_0_branch1_bn_s 104 | res4_0_branch2b_bn_b 105 | res3_0_branch2c_bn_b 106 | res5_0_branch2c_bn_s 107 | res5_2_branch2c_w 108 | res3_0_branch2c_bn_s 109 | res5_0_branch2c_bn_b 110 | res3_0_branch2c_w 111 | res2_2_branch2c_w 112 | res3_0_branch2a_w 113 | res2_0_branch2a_bn_s 114 | res4_3_branch2a_bn_b 115 | res2_1_branch2a_bn_s 116 | res3_2_branch2c_bn_b 117 | res2_0_branch2a_bn_b 118 | res2_1_branch2a_bn_b 119 | res4_2_branch2c_w 120 | res3_1_branch2b_w 121 | res3_2_branch2c_bn_s 122 | res3_3_branch2c_w 123 | res2_0_branch2b_bn_b 124 | res4_0_branch2a_bn_s 125 | res3_3_branch2a_w 126 | res2_0_branch2b_bn_s 127 | res4_0_branch2a_bn_b 128 | res2_0_branch2c_bn_b 129 | res2_0_branch2c_bn_s 130 | res4_3_branch2b_w 131 | res3_0_branch2b_bn_b 132 | res2_0_branch1_w 133 | res3_0_branch2b_bn_s 134 | res5_2_branch2c_bn_s 135 | res2_0_branch2c_w 136 | res5_2_branch2c_bn_b 137 | res4_3_branch2a_bn_s 138 | res3_2_branch2b_w 139 | res2_1_branch2a_w 140 | res4_0_branch1_w 141 | res4_5_branch2b_w 142 | res2_2_branch2b_bn_s 143 | res2_2_branch2b_bn_b 144 | res2_2_branch2c_bn_s 145 | res5_0_branch1_bn_s 146 | res4_0_branch2c_w 147 | res2_1_branch2b_bn_b 148 | res5_0_branch1_bn_b 149 | res4_0_branch2a_w 150 | res2_1_branch2b_bn_s 151 | res3_1_branch2a_w 152 | res2_1_branch2b_w 153 | res3_1_branch2c_w 154 | pred_b 155 | res3_3_branch2b_w 156 | pred_w 157 | res4_3_branch2a_w 158 | res4_1_branch2b_w 159 | res4_3_branch2c_w 160 | res4_5_branch2a_bn_s 161 | res4_5_branch2a_bn_b 162 | -------------------------------------------------------------------------------- /waste/cropped_clipsListFile.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/17Skye17/Non-local-Neural-Networks-Pytorch/9a2ddbafad69f2ca79764d3c29469d8f84f882af/waste/cropped_clipsListFile.pkl -------------------------------------------------------------------------------- /waste/demo_MNIST.py.back: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.utils.data as Data 3 | import torchvision 4 | from lib.network import Network 5 | from torch.autograd import Variable 6 | from torch import nn 7 | import time 8 | 9 | 10 | def calc_acc(x, y): 11 | x = torch.max(x, dim=-1)[1] 12 | accuracy = sum(x == y) / x.size(0) 13 | return accuracy 14 | 15 | 16 | train_data = torchvision.datasets.MNIST(root='./mnist', train=True, 17 | transform=torchvision.transforms.ToTensor(), 18 | download=True) 19 | test_data = torchvision.datasets.MNIST(root='./mnist/', 20 | transform=torchvision.transforms.ToTensor(), 21 | train=False) 22 | 23 | train_loader = Data.DataLoader(dataset=train_data, batch_size=128, shuffle=True) 24 | test_loader = Data.DataLoader(dataset=test_data, batch_size=128, shuffle=False) 25 | 26 | train_batch_num = len(train_loader) 27 | test_batch_num = len(test_loader) 28 | 29 | net = Network() 30 | if torch.cuda.is_available(): 31 | net = nn.DataParallel(net) 32 | net.cuda() 33 | 34 | opt = torch.optim.Adam(net.parameters(), lr=0.001) 35 | loss_func = nn.CrossEntropyLoss() 36 | 37 | 38 | for epoch_index in range(20): 39 | st = time.time() 40 | for train_batch_index, (img_batch, label_batch) in enumerate(train_loader): 41 | img_batch = Variable(img_batch) 42 | label_batch = Variable(label_batch) 43 | 44 | if torch.cuda.is_available(): 45 | img_batch = img_batch.cuda() 46 | label_batch = label_batch.cuda() 47 | 48 | predict = net(img_batch) 49 | acc = calc_acc(predict.cpu().data, label_batch.cpu().data) 50 | loss = loss_func(predict, label_batch) 51 | 52 | net.zero_grad() 53 | loss.backward() 54 | opt.step() 55 | 56 | print('(LR:%f) Time of a epoch:%.4fs' % (opt.param_groups[0]['lr'], time.time()-st)) 57 | 58 | net.eval() 59 | total_loss = 0 60 | total_acc = 0 61 | 62 | for test_batch_index, (img_batch, label_batch) in enumerate(test_loader): 63 | img_batch = Variable(img_batch, volatile=True) 64 | label_batch = Variable(label_batch, volatile=True) 65 | 66 | if torch.cuda.is_available(): 67 | img_batch = img_batch.cuda() 68 | label_batch = label_batch.cuda() 69 | 70 | predict = net(img_batch) 71 | acc = calc_acc(predict.cpu().data, label_batch.cpu().data) 72 | loss = loss_func(predict, label_batch) 73 | 74 | total_loss += loss 75 | total_acc += acc 76 | 77 | net.train() 78 | 79 | mean_acc = total_acc / test_batch_num 80 | mean_loss = total_loss / test_batch_num 81 | 82 | print('[Test] epoch[%d/%d] acc:%.4f loss:%.4f\n' 83 | % (epoch_index, 100, mean_acc, mean_loss.data[0])) 84 | -------------------------------------------------------------------------------- /waste/pytorch.lst: -------------------------------------------------------------------------------- 1 | conv1.weight 2 | bn1.weight 3 | bn1.bias 4 | bn1.running_mean 5 | bn1.running_var 6 | layer1.0.conv1.weight 7 | layer1.0.bn1.weight 8 | layer1.0.bn1.bias 9 | layer1.0.bn1.running_mean 10 | layer1.0.bn1.running_var 11 | layer1.0.conv2.weight 12 | layer1.0.bn2.weight 13 | layer1.0.bn2.bias 14 | layer1.0.bn2.running_mean 15 | layer1.0.bn2.running_var 16 | layer1.0.conv3.weight 17 | layer1.0.bn3.weight 18 | layer1.0.bn3.bias 19 | layer1.0.bn3.running_mean 20 | layer1.0.bn3.running_var 21 | layer1.0.downsample.0.weight 22 | layer1.0.downsample.1.weight 23 | layer1.0.downsample.1.bias 24 | layer1.0.downsample.1.running_mean 25 | layer1.0.downsample.1.running_var 26 | layer1.1.conv1.weight 27 | layer1.1.bn1.weight 28 | layer1.1.bn1.bias 29 | layer1.1.bn1.running_mean 30 | layer1.1.bn1.running_var 31 | layer1.1.conv2.weight 32 | layer1.1.bn2.weight 33 | layer1.1.bn2.bias 34 | layer1.1.bn2.running_mean 35 | layer1.1.bn2.running_var 36 | layer1.1.conv3.weight 37 | layer1.1.bn3.weight 38 | layer1.1.bn3.bias 39 | layer1.1.bn3.running_mean 40 | layer1.1.bn3.running_var 41 | layer1.2.conv1.weight 42 | layer1.2.bn1.weight 43 | layer1.2.bn1.bias 44 | layer1.2.bn1.running_mean 45 | layer1.2.bn1.running_var 46 | layer1.2.conv2.weight 47 | layer1.2.bn2.weight 48 | layer1.2.bn2.bias 49 | layer1.2.bn2.running_mean 50 | layer1.2.bn2.running_var 51 | layer1.2.conv3.weight 52 | layer1.2.bn3.weight 53 | layer1.2.bn3.bias 54 | layer1.2.bn3.running_mean 55 | layer1.2.bn3.running_var 56 | layer2.0.conv1.weight 57 | layer2.0.bn1.weight 58 | layer2.0.bn1.bias 59 | layer2.0.bn1.running_mean 60 | layer2.0.bn1.running_var 61 | layer2.0.conv2.weight 62 | layer2.0.bn2.weight 63 | layer2.0.bn2.bias 64 | layer2.0.bn2.running_mean 65 | layer2.0.bn2.running_var 66 | layer2.0.conv3.weight 67 | layer2.0.bn3.weight 68 | layer2.0.bn3.bias 69 | layer2.0.bn3.running_mean 70 | layer2.0.bn3.running_var 71 | layer2.0.downsample.0.weight 72 | layer2.0.downsample.1.weight 73 | layer2.0.downsample.1.bias 74 | layer2.0.downsample.1.running_mean 75 | layer2.0.downsample.1.running_var 76 | layer2.1.conv1.weight 77 | layer2.1.bn1.weight 78 | layer2.1.bn1.bias 79 | layer2.1.bn1.running_mean 80 | layer2.1.bn1.running_var 81 | layer2.1.conv2.weight 82 | layer2.1.bn2.weight 83 | layer2.1.bn2.bias 84 | layer2.1.bn2.running_mean 85 | layer2.1.bn2.running_var 86 | layer2.1.conv3.weight 87 | layer2.1.bn3.weight 88 | layer2.1.bn3.bias 89 | layer2.1.bn3.running_mean 90 | layer2.1.bn3.running_var 91 | layer2.2.conv1.weight 92 | layer2.2.bn1.weight 93 | layer2.2.bn1.bias 94 | layer2.2.bn1.running_mean 95 | layer2.2.bn1.running_var 96 | layer2.2.conv2.weight 97 | layer2.2.bn2.weight 98 | layer2.2.bn2.bias 99 | layer2.2.bn2.running_mean 100 | layer2.2.bn2.running_var 101 | layer2.2.conv3.weight 102 | layer2.2.bn3.weight 103 | layer2.2.bn3.bias 104 | layer2.2.bn3.running_mean 105 | layer2.2.bn3.running_var 106 | layer2.3.conv1.weight 107 | layer2.3.bn1.weight 108 | layer2.3.bn1.bias 109 | layer2.3.bn1.running_mean 110 | layer2.3.bn1.running_var 111 | layer2.3.conv2.weight 112 | layer2.3.bn2.weight 113 | layer2.3.bn2.bias 114 | layer2.3.bn2.running_mean 115 | layer2.3.bn2.running_var 116 | layer2.3.conv3.weight 117 | layer2.3.bn3.weight 118 | layer2.3.bn3.bias 119 | layer2.3.bn3.running_mean 120 | layer2.3.bn3.running_var 121 | layer3.0.conv1.weight 122 | layer3.0.bn1.weight 123 | layer3.0.bn1.bias 124 | layer3.0.bn1.running_mean 125 | layer3.0.bn1.running_var 126 | layer3.0.conv2.weight 127 | layer3.0.bn2.weight 128 | layer3.0.bn2.bias 129 | layer3.0.bn2.running_mean 130 | layer3.0.bn2.running_var 131 | layer3.0.conv3.weight 132 | layer3.0.bn3.weight 133 | layer3.0.bn3.bias 134 | layer3.0.bn3.running_mean 135 | layer3.0.bn3.running_var 136 | layer3.0.downsample.0.weight 137 | layer3.0.downsample.1.weight 138 | layer3.0.downsample.1.bias 139 | layer3.0.downsample.1.running_mean 140 | layer3.0.downsample.1.running_var 141 | layer3.1.conv1.weight 142 | layer3.1.bn1.weight 143 | layer3.1.bn1.bias 144 | layer3.1.bn1.running_mean 145 | layer3.1.bn1.running_var 146 | layer3.1.conv2.weight 147 | layer3.1.bn2.weight 148 | layer3.1.bn2.bias 149 | layer3.1.bn2.running_mean 150 | layer3.1.bn2.running_var 151 | layer3.1.conv3.weight 152 | layer3.1.bn3.weight 153 | layer3.1.bn3.bias 154 | layer3.1.bn3.running_mean 155 | layer3.1.bn3.running_var 156 | layer3.2.conv1.weight 157 | layer3.2.bn1.weight 158 | layer3.2.bn1.bias 159 | layer3.2.bn1.running_mean 160 | layer3.2.bn1.running_var 161 | layer3.2.conv2.weight 162 | layer3.2.bn2.weight 163 | layer3.2.bn2.bias 164 | layer3.2.bn2.running_mean 165 | layer3.2.bn2.running_var 166 | layer3.2.conv3.weight 167 | layer3.2.bn3.weight 168 | layer3.2.bn3.bias 169 | layer3.2.bn3.running_mean 170 | layer3.2.bn3.running_var 171 | layer3.3.conv1.weight 172 | layer3.3.bn1.weight 173 | layer3.3.bn1.bias 174 | layer3.3.bn1.running_mean 175 | layer3.3.bn1.running_var 176 | layer3.3.conv2.weight 177 | layer3.3.bn2.weight 178 | layer3.3.bn2.bias 179 | layer3.3.bn2.running_mean 180 | layer3.3.bn2.running_var 181 | layer3.3.conv3.weight 182 | layer3.3.bn3.weight 183 | layer3.3.bn3.bias 184 | layer3.3.bn3.running_mean 185 | layer3.3.bn3.running_var 186 | layer3.4.conv1.weight 187 | layer3.4.bn1.weight 188 | layer3.4.bn1.bias 189 | layer3.4.bn1.running_mean 190 | layer3.4.bn1.running_var 191 | layer3.4.conv2.weight 192 | layer3.4.bn2.weight 193 | layer3.4.bn2.bias 194 | layer3.4.bn2.running_mean 195 | layer3.4.bn2.running_var 196 | layer3.4.conv3.weight 197 | layer3.4.bn3.weight 198 | layer3.4.bn3.bias 199 | layer3.4.bn3.running_mean 200 | layer3.4.bn3.running_var 201 | layer3.5.conv1.weight 202 | layer3.5.bn1.weight 203 | layer3.5.bn1.bias 204 | layer3.5.bn1.running_mean 205 | layer3.5.bn1.running_var 206 | layer3.5.conv2.weight 207 | layer3.5.bn2.weight 208 | layer3.5.bn2.bias 209 | layer3.5.bn2.running_mean 210 | layer3.5.bn2.running_var 211 | layer3.5.conv3.weight 212 | layer3.5.bn3.weight 213 | layer3.5.bn3.bias 214 | layer3.5.bn3.running_mean 215 | layer3.5.bn3.running_var 216 | layer4.0.conv1.weight 217 | layer4.0.bn1.weight 218 | layer4.0.bn1.bias 219 | layer4.0.bn1.running_mean 220 | layer4.0.bn1.running_var 221 | layer4.0.conv2.weight 222 | layer4.0.bn2.weight 223 | layer4.0.bn2.bias 224 | layer4.0.bn2.running_mean 225 | layer4.0.bn2.running_var 226 | layer4.0.conv3.weight 227 | layer4.0.bn3.weight 228 | layer4.0.bn3.bias 229 | layer4.0.bn3.running_mean 230 | layer4.0.bn3.running_var 231 | layer4.0.downsample.0.weight 232 | layer4.0.downsample.1.weight 233 | layer4.0.downsample.1.bias 234 | layer4.0.downsample.1.running_mean 235 | layer4.0.downsample.1.running_var 236 | layer4.1.conv1.weight 237 | layer4.1.bn1.weight 238 | layer4.1.bn1.bias 239 | layer4.1.bn1.running_mean 240 | layer4.1.bn1.running_var 241 | layer4.1.conv2.weight 242 | layer4.1.bn2.weight 243 | layer4.1.bn2.bias 244 | layer4.1.bn2.running_mean 245 | layer4.1.bn2.running_var 246 | layer4.1.conv3.weight 247 | layer4.1.bn3.weight 248 | layer4.1.bn3.bias 249 | layer4.1.bn3.running_mean 250 | layer4.1.bn3.running_var 251 | layer4.2.conv1.weight 252 | layer4.2.bn1.weight 253 | layer4.2.bn1.bias 254 | layer4.2.bn1.running_mean 255 | layer4.2.bn1.running_var 256 | layer4.2.conv2.weight 257 | layer4.2.bn2.weight 258 | layer4.2.bn2.bias 259 | layer4.2.bn2.running_mean 260 | layer4.2.bn2.running_var 261 | layer4.2.conv3.weight 262 | layer4.2.bn3.weight 263 | layer4.2.bn3.bias 264 | layer4.2.bn3.running_mean 265 | layer4.2.bn3.running_var 266 | fc.weight 267 | fc.bias 268 | 267 267 269 | -------------------------------------------------------------------------------- /waste/pytorch_params.lst: -------------------------------------------------------------------------------- 1 | conv1.weight 2 | bn1.weight 3 | bn1.bias 4 | layer1.0.conv1.weight 5 | layer1.0.bn1.weight 6 | layer1.0.bn1.bias 7 | layer1.0.conv2.weight 8 | layer1.0.bn2.weight 9 | layer1.0.bn2.bias 10 | layer1.0.conv3.weight 11 | layer1.0.bn3.weight 12 | layer1.0.bn3.bias 13 | layer1.0.downsample.0.weight 14 | layer1.0.downsample.1.weight 15 | layer1.0.downsample.1.bias 16 | layer1.1.conv1.weight 17 | layer1.1.bn1.weight 18 | layer1.1.bn1.bias 19 | layer1.1.conv2.weight 20 | layer1.1.bn2.weight 21 | layer1.1.bn2.bias 22 | layer1.1.conv3.weight 23 | layer1.1.bn3.weight 24 | layer1.1.bn3.bias 25 | layer1.2.conv1.weight 26 | layer1.2.bn1.weight 27 | layer1.2.bn1.bias 28 | layer1.2.conv2.weight 29 | layer1.2.bn2.weight 30 | layer1.2.bn2.bias 31 | layer1.2.conv3.weight 32 | layer1.2.bn3.weight 33 | layer1.2.bn3.bias 34 | layer2.0.conv1.weight 35 | layer2.0.bn1.weight 36 | layer2.0.bn1.bias 37 | layer2.0.conv2.weight 38 | layer2.0.bn2.weight 39 | layer2.0.bn2.bias 40 | layer2.0.conv3.weight 41 | layer2.0.bn3.weight 42 | layer2.0.bn3.bias 43 | layer2.0.downsample.0.weight 44 | layer2.0.downsample.1.weight 45 | layer2.0.downsample.1.bias 46 | layer2.1.conv1.weight 47 | layer2.1.bn1.weight 48 | layer2.1.bn1.bias 49 | layer2.1.conv2.weight 50 | layer2.1.bn2.weight 51 | layer2.1.bn2.bias 52 | layer2.1.conv3.weight 53 | layer2.1.bn3.weight 54 | layer2.1.bn3.bias 55 | layer2.2.conv1.weight 56 | layer2.2.bn1.weight 57 | layer2.2.bn1.bias 58 | layer2.2.conv2.weight 59 | layer2.2.bn2.weight 60 | layer2.2.bn2.bias 61 | layer2.2.conv3.weight 62 | layer2.2.bn3.weight 63 | layer2.2.bn3.bias 64 | layer2.3.conv1.weight 65 | layer2.3.bn1.weight 66 | layer2.3.bn1.bias 67 | layer2.3.conv2.weight 68 | layer2.3.bn2.weight 69 | layer2.3.bn2.bias 70 | layer2.3.conv3.weight 71 | layer2.3.bn3.weight 72 | layer2.3.bn3.bias 73 | layer3.0.conv1.weight 74 | layer3.0.bn1.weight 75 | layer3.0.bn1.bias 76 | layer3.0.conv2.weight 77 | layer3.0.bn2.weight 78 | layer3.0.bn2.bias 79 | layer3.0.conv3.weight 80 | layer3.0.bn3.weight 81 | layer3.0.bn3.bias 82 | layer3.0.downsample.0.weight 83 | layer3.0.downsample.1.weight 84 | layer3.0.downsample.1.bias 85 | layer3.1.conv1.weight 86 | layer3.1.bn1.weight 87 | layer3.1.bn1.bias 88 | layer3.1.conv2.weight 89 | layer3.1.bn2.weight 90 | layer3.1.bn2.bias 91 | layer3.1.conv3.weight 92 | layer3.1.bn3.weight 93 | layer3.1.bn3.bias 94 | layer3.2.conv1.weight 95 | layer3.2.bn1.weight 96 | layer3.2.bn1.bias 97 | layer3.2.conv2.weight 98 | layer3.2.bn2.weight 99 | layer3.2.bn2.bias 100 | layer3.2.conv3.weight 101 | layer3.2.bn3.weight 102 | layer3.2.bn3.bias 103 | layer3.3.conv1.weight 104 | layer3.3.bn1.weight 105 | layer3.3.bn1.bias 106 | layer3.3.conv2.weight 107 | layer3.3.bn2.weight 108 | layer3.3.bn2.bias 109 | layer3.3.conv3.weight 110 | layer3.3.bn3.weight 111 | layer3.3.bn3.bias 112 | layer3.4.conv1.weight 113 | layer3.4.bn1.weight 114 | layer3.4.bn1.bias 115 | layer3.4.conv2.weight 116 | layer3.4.bn2.weight 117 | layer3.4.bn2.bias 118 | layer3.4.conv3.weight 119 | layer3.4.bn3.weight 120 | layer3.4.bn3.bias 121 | layer3.5.conv1.weight 122 | layer3.5.bn1.weight 123 | layer3.5.bn1.bias 124 | layer3.5.conv2.weight 125 | layer3.5.bn2.weight 126 | layer3.5.bn2.bias 127 | layer3.5.conv3.weight 128 | layer3.5.bn3.weight 129 | layer3.5.bn3.bias 130 | layer4.0.conv1.weight 131 | layer4.0.bn1.weight 132 | layer4.0.bn1.bias 133 | layer4.0.conv2.weight 134 | layer4.0.bn2.weight 135 | layer4.0.bn2.bias 136 | layer4.0.conv3.weight 137 | layer4.0.bn3.weight 138 | layer4.0.bn3.bias 139 | layer4.0.downsample.0.weight 140 | layer4.0.downsample.1.weight 141 | layer4.0.downsample.1.bias 142 | layer4.1.conv1.weight 143 | layer4.1.bn1.weight 144 | layer4.1.bn1.bias 145 | layer4.1.conv2.weight 146 | layer4.1.bn2.weight 147 | layer4.1.bn2.bias 148 | layer4.1.conv3.weight 149 | layer4.1.bn3.weight 150 | layer4.1.bn3.bias 151 | layer4.2.conv1.weight 152 | layer4.2.bn1.weight 153 | layer4.2.bn1.bias 154 | layer4.2.conv2.weight 155 | layer4.2.bn2.weight 156 | layer4.2.bn2.bias 157 | layer4.2.conv3.weight 158 | layer4.2.bn3.weight 159 | layer4.2.bn3.bias 160 | fc.weight 161 | fc.bias 162 | -------------------------------------------------------------------------------- /waste/read_data.py: -------------------------------------------------------------------------------- 1 | from videoDataset import videoDataset 2 | import torch.utils.data as Data 3 | import h5py 4 | import numpy as np 5 | from joblib import delayed 6 | from joblib import Parallel 7 | from tqdm import tqdm 8 | from torch.utils.data.dataloader import default_collate 9 | 10 | def my_collate_fn(batch): 11 | batch = list(filter(lambda x:x is not None,batch)) 12 | batch = list(filter(lambda x:x[0] is not None, batch)) 13 | return default_collate(batch) 14 | 15 | hdf5_file='sample-clipsListFile.hdf5' 16 | 17 | video = videoDataset(hdf5_file,3,32,224,224) 18 | 19 | dataloader = Data.DataLoader(dataset=video,batch_size=1,collate_fn=my_collate_fn,shuffle=False) 20 | for sample in enumerate(dataloader): 21 | print sample 22 | 23 | #for i in range(len(video)): 24 | # print np.array(video[i]['clip']).shape 25 | #test_loader=Data.DataLoader(dataset=video,batch_size=2,shuffle=False) 26 | #for sample in enumerate(test_loader): 27 | 28 | #for sample in enumerate(test_loader): 29 | # print sample 30 | -------------------------------------------------------------------------------- /waste/videoDataset.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/17Skye17/Non-local-Neural-Networks-Pytorch/9a2ddbafad69f2ca79764d3c29469d8f84f882af/waste/videoDataset.pyc --------------------------------------------------------------------------------