├── .DS_Store ├── OpenPose_PyTorch_Models ├── .DS_Store ├── conversion.sh ├── face │ ├── .DS_Store │ └── pose_pytorch.py ├── hand │ ├── .DS_Store │ └── pose_pytorch.py ├── pose │ ├── .DS_Store │ ├── coco │ │ ├── .DS_Store │ │ └── pose_linevec_pytorch.py │ ├── mpi │ │ ├── .DS_Store │ │ ├── pose_faster_linevec_pytorch.py │ │ └── pose_linevec_pytorch.py │ └── body_25 │ │ └── .DS_Store ├── README_openpose_conversion.md ├── testing.py └── mmdnn_caffe │ ├── mapper.py │ ├── transformer.py │ ├── graph.py │ └── caffe_emitter.py └── README.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Russzheng/EveryBodyDanceNow_iOS_App_DanceMob/HEAD/.DS_Store -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Russzheng/EveryBodyDanceNow_iOS_App_DanceMob/HEAD/OpenPose_PyTorch_Models/.DS_Store -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/conversion.sh: -------------------------------------------------------------------------------- 1 | mmtocode -f pytorch -n converted_pose.pb --IRWeightPath converted_pose.npy --dstModelPath con_test.py -dw con_test.npy -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/face/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Russzheng/EveryBodyDanceNow_iOS_App_DanceMob/HEAD/OpenPose_PyTorch_Models/face/.DS_Store -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/hand/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Russzheng/EveryBodyDanceNow_iOS_App_DanceMob/HEAD/OpenPose_PyTorch_Models/hand/.DS_Store -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/pose/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Russzheng/EveryBodyDanceNow_iOS_App_DanceMob/HEAD/OpenPose_PyTorch_Models/pose/.DS_Store -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/pose/coco/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Russzheng/EveryBodyDanceNow_iOS_App_DanceMob/HEAD/OpenPose_PyTorch_Models/pose/coco/.DS_Store -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/pose/mpi/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Russzheng/EveryBodyDanceNow_iOS_App_DanceMob/HEAD/OpenPose_PyTorch_Models/pose/mpi/.DS_Store -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/pose/body_25/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Russzheng/EveryBodyDanceNow_iOS_App_DanceMob/HEAD/OpenPose_PyTorch_Models/pose/body_25/.DS_Store -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/README_openpose_conversion.md: -------------------------------------------------------------------------------- 1 | #### What We Have Done: 2 | * Added PreLU layer in MMdnn 3 | * Swap those files in mmdnn_caffe in applicable 4 | * Refer to those files for how to add layers in MMdnn and use MMdnn 5 | * Message me if you need weights files 6 | 7 | #### TO-DO: 8 | - [] Modulize the codes 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DanceMob: Mobile Real-Time Motion Transfer – iOS 2 | UC Berkeley Master of Engineering Capstone Project 3 | 4 | #### References: 5 | * [Everybody dance now](https://arxiv.org/abs/1808.07371) 6 | * [Pix2PixHD](https://github.com/NVIDIA/pix2pixHD) 7 | * [OpenPose](https://github.com/CMU-Perceptual-Computing-Lab/openpose) 8 | * [OpenPose ShuffleNet Version](https://github.com/tensorboy/pytorch_Realtime_Multi-Person_Pose_Estimation) 9 | * [iOS Mobile Pose Estimation](https://github.com/tucan9389/PoseEstimation-CoreML) 10 | 11 | #### What We Have Done: 12 | * Converted OpenPose Caffe models to PyTorch Models with [MMdnn](https://github.com/Microsoft/MMdnn) 13 | * Converted Caffe/PyTorch to CoreML models with [CoreMLTools](https://developer.apple.com/documentation/coreml/converting_trained_models_to_core_ml) 14 | * Shrunk size of detectors and generators significantly 15 | * Deployed both CoreML models in an iOS app 16 | -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/testing.py: -------------------------------------------------------------------------------- 1 | import torch 2 | # import caffe 3 | import numpy as np 4 | from torch.autograd import Variable 5 | 6 | import test as models 7 | 8 | # pytorch models 9 | model = models.model 10 | input_size = models.input_size 11 | print(input_size) 12 | image = np.random.randint(0, 255, (1, 3, 512, 256)) 13 | import sys; print(sys._getframe().f_code.co_name,sys._getframe().f_lineno) 14 | from IPython import embed; embed() 15 | input_data = image.astype(np.float32) 16 | input_var = Variable(torch.from_numpy(input_data)) 17 | 18 | # pytorch output 19 | model.eval() 20 | output_var = model(input_var) 21 | pytorch_output = output_var.data.cpu().numpy() 22 | 23 | # caffe models 24 | # net = caffe.Net('pose_deploy.prototxt', 'pose_iter_584000.caffemodel', caffe.TEST) 25 | 26 | # caffe output 27 | # reshape 28 | # net.blobs['image'].data[...] = input_data.reshape((1, 3, 224, 224)) 29 | 30 | # caffe_output_blobs = net.forward() 31 | # caffe_output = caffe_output_blobs.popitem()[1] 32 | 33 | 34 | # print the input and outputs 35 | # print(input_size, pytorch_output.shape, caffe_output.shape) 36 | print('pytorch: min: {}, max: {}, mean: {}'.format(pytorch_output.min(), pytorch_output.max(), pytorch_output.mean())) 37 | # print(' caffe: min: {}, max: {}, mean: {}'.format(caffe_output.min(), caffe_output.max(), caffe_output.mean())) 38 | 39 | # diff = np.abs(pytorch_output.reshape(-1) - caffe_output.reshape(-1)) 40 | # print(' diff: min: {}, max: {}, mean: {}, median: {}'.format(diff.min(), diff.max(), diff.mean(), np.median(diff))) 41 | 42 | import sys; print(sys._getframe().f_code.co_name,sys._getframe().f_lineno) 43 | from IPython import embed; embed() 44 | 45 | # 1 * 78 * (w / 8) * (h / 8) 46 | -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/mmdnn_caffe/mapper.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import division 3 | import numpy as np 4 | 5 | from mmdnn.conversion.caffe.errors import ConversionError 6 | from mmdnn.conversion.caffe.common_graph import Node 7 | from mmdnn.conversion.caffe.network import DEFAULT_PADDING 8 | from mmdnn.conversion.caffe.utils import get_lower_case 9 | from mmdnn.conversion.common.IR.graph_pb2 import TensorShape 10 | 11 | 12 | def get_handler_name(node_kind): 13 | return node_kind.lower() if len(node_kind) <= 4 else get_lower_case(node_kind) 14 | 15 | 16 | class NodeMapper(object): 17 | 18 | @classmethod 19 | def _convert_output_shape(cls, kwargs, node): 20 | shape = TensorShape() 21 | dim = shape.dim.add() 22 | dim.size = -1 23 | 24 | if len(node.output_shape) > 2: 25 | for i in node.output_shape[2:]: 26 | dim = shape.dim.add() 27 | dim.size = i 28 | dim = shape.dim.add() 29 | dim.size = node.output_shape.channels 30 | else: 31 | dim = shape.dim.add() 32 | dim.size = node.output_shape[1] 33 | kwargs['_output_shapes'] = [shape] 34 | 35 | @classmethod 36 | def get_kernel_params(cls, node, input_shape): 37 | kwargs = {} 38 | 39 | if node.kernel_parameters.global_pooling: 40 | kwargs['kernel_shape'] = [1, input_shape.height, input_shape.width, 1] 41 | kwargs['pads'] = [0] * 8 42 | 43 | else: 44 | from mmdnn.conversion.caffe.graph import NodeKind 45 | if node.kind == NodeKind.Pooling: 46 | kwargs['kernel_shape'] = [1, node.kernel_parameters.k_h, node.kernel_parameters.k_w, 1] 47 | elif node.kind in [NodeKind.Convolution, NodeKind.Deconvolution]: 48 | pass 49 | else: 50 | raise ValueError 51 | 52 | dilation = node.parameters.dilation[0] if hasattr(node.parameters, 'dilation') and node.parameters.dilation else 1 53 | o_h_caffe = node.output_shape.height 54 | o_w_caffe = node.output_shape.width 55 | ko_h = dilation * (int(node.kernel_parameters.k_h) - 1) + 1 56 | ko_w = dilation * (int(node.kernel_parameters.k_w) - 1) + 1 57 | 58 | if node.kind == NodeKind.Deconvolution: 59 | o_h_tf = int(node.kernel_parameters.s_h) * (input_shape.height - 1) + ko_h - 2 * int(node.kernel_parameters.p_h) 60 | o_w_tf = int(node.kernel_parameters.s_w) * (input_shape.width - 1) + ko_w - 2 * int(node.kernel_parameters.p_w) 61 | else: 62 | o_h_tf = (input_shape.height + node.kernel_parameters.p_h * 2 - ko_h + 1) // node.kernel_parameters.s_h 63 | o_w_tf = (input_shape.width + node.kernel_parameters.p_w * 2 - ko_w + 1) // node.kernel_parameters.s_w 64 | 65 | kwargs['pads'] = [0, node.kernel_parameters.p_h, node.kernel_parameters.p_w, 0] + \ 66 | [0, node.kernel_parameters.p_h + o_h_caffe - o_h_tf, node.kernel_parameters.p_w + o_w_caffe - o_w_tf, 0] 67 | 68 | kwargs['strides'] = [1, node.kernel_parameters.s_h, node.kernel_parameters.s_w, 1] 69 | cls._convert_output_shape(kwargs, node) 70 | 71 | return kwargs 72 | 73 | 74 | @classmethod 75 | def map_data(cls, node): 76 | # TODO: We need to identify whether this is 4D image data, otherwise we shouldn't change the dimension order 77 | shape = TensorShape() 78 | dim = shape.dim.add() 79 | dim.size = -1 80 | for i in node.output_shape[2:]: 81 | dim = shape.dim.add() 82 | dim.size = i 83 | dim = shape.dim.add() 84 | dim.size = node.output_shape.channels 85 | 86 | kwargs = {'shape': shape} # Ignore the dimension of batch size 87 | cls._convert_output_shape(kwargs, node) 88 | return Node.create('DataInput', **kwargs) 89 | 90 | 91 | @classmethod 92 | def map_input(cls, node): 93 | return cls.map_data(node) 94 | 95 | @classmethod 96 | def map_convolution(cls, node): 97 | parent, _ = node.get_only_parent() 98 | kwargs = cls.get_kernel_params(node, parent.output_shape) 99 | kwargs['kernel_shape'] = [node.kernel_parameters.k_h, node.kernel_parameters.k_w, parent.output_shape.channels, node.parameters.num_output] 100 | kwargs['use_bias'] = node.parameters.bias_term 101 | if node.parameters.dilation: 102 | dilation = node.parameters.dilation[0] 103 | if dilation != 1: 104 | kwargs['dilations'] = [1, dilation, dilation, 1] 105 | kwargs['group'] = node.parameters.group 106 | return Node.create('Conv', **kwargs) 107 | 108 | 109 | @classmethod 110 | def map_deconvolution(cls, node): 111 | parent, _ = node.get_only_parent() 112 | kwargs = cls.get_kernel_params(node, parent.output_shape) 113 | 114 | kwargs['kernel_shape'] = [node.kernel_parameters.k_h, node.kernel_parameters.k_w, node.parameters.num_output, parent.output_shape.channels] 115 | kwargs['use_bias'] = node.parameters.bias_term 116 | if node.parameters.dilation: 117 | dilation = node.parameters.dilation[0] 118 | if dilation != 1: 119 | kwargs['dilations'] = [1, dilation, dilation, 1] 120 | kwargs['group'] = node.parameters.group 121 | return Node.create('ConvTranspose', **kwargs) 122 | 123 | 124 | @classmethod 125 | def map_crop(cls, node): 126 | kwargs = {} 127 | cls._convert_output_shape(kwargs, node) 128 | offset = node.parameters.offset 129 | if offset: 130 | if len(offset) == 1: 131 | kwargs['border'] = [offset[0], offset[0], 0, 0] 132 | else: 133 | kwargs['border'] = [offset[0], offset[1], 0, 0] 134 | 135 | return Node.create('Crop', **kwargs) 136 | 137 | 138 | @classmethod 139 | def map_relu(cls, node): 140 | kwargs = {} 141 | cls._convert_output_shape(kwargs, node) 142 | return Node.create('Relu', **kwargs) 143 | 144 | @classmethod 145 | def map_p_re_lu(cls, node): 146 | # print(node.parameters) 147 | # assert False 148 | try: 149 | scale_value = float(node.parameters.filler.value) 150 | kwargs = {'gamma' : scale_value} 151 | except ConversionError: 152 | kwargs = {'gamma' : 0.25} 153 | cls._convert_output_shape(kwargs, node) 154 | return Node.create('PRelu', **kwargs) 155 | 156 | 157 | @classmethod 158 | def map_pooling(cls, node): 159 | parent, _ = node.get_only_parent() 160 | kwargs = cls.get_kernel_params(node, parent.output_shape) 161 | if node.parameters.pool == 0: 162 | kwargs['pooling_type'] = 'MAX' 163 | elif node.parameters.pool == 1: 164 | kwargs['pooling_type'] = 'AVG' 165 | else: 166 | # Stochastic pooling, for instance. 167 | raise ConversionError('Unsupported pooling type.') 168 | cls._convert_output_shape(kwargs, node) 169 | return Node.create('Pool', **kwargs) 170 | 171 | 172 | @classmethod 173 | def _add_flatten_layer(cls, node): 174 | shape = TensorShape() 175 | dim = shape.dim.add() 176 | dim.size = -1 177 | 178 | dim = shape.dim.add() 179 | dim.size = 1 180 | for i in node.output_shape[1:]: 181 | dim.size *= i 182 | kwargs = {'_output_shapes' : [shape]} 183 | return Node.create('Flatten', **kwargs) 184 | 185 | @classmethod 186 | def map_inner_product(cls, node): 187 | #TODO: Axis 188 | assert node.parameters.axis == 1 189 | #TODO: Unbiased 190 | kwargs = {'use_bias' : node.parameters.bias_term, 'units' : node.parameters.num_output} 191 | 192 | # check if need the Flatten layer 193 | parent, _ = node.get_only_parent() 194 | ret = [] 195 | 196 | # if parent.output_shape.height > 1 or parent.output_shape.width > 1: 197 | ret.append(cls._add_flatten_layer(parent)) 198 | ret.append(Node.create('FullyConnected', **kwargs)) 199 | return ret 200 | 201 | @classmethod 202 | def map_softmax(cls, node): 203 | kwargs = {} 204 | cls._convert_output_shape(kwargs, node) 205 | return Node.create('Softmax', **kwargs) 206 | 207 | @classmethod 208 | def map_lrn(cls, node): 209 | params = node.parameters 210 | assert params.local_size % 2 == 1 211 | kwargs = {'size': int((params.local_size + 1) / 2), 'alpha': params.alpha, 'beta': params.beta, 'k' : params.k} 212 | cls._convert_output_shape(kwargs, node) 213 | return Node.create('LRN', **kwargs) 214 | 215 | @classmethod 216 | def map_concat(cls, node): 217 | kwargs = {'axis': (2, 3, 1, 0)[node.parameters.axis]} 218 | cls._convert_output_shape(kwargs, node) 219 | return Node.create('Concat', **kwargs) 220 | 221 | @classmethod 222 | def map_dropout(cls, node): 223 | kwargs = {'keep_prob': node.parameters.dropout_ratio} 224 | cls._convert_output_shape(kwargs, node) 225 | return Node.create('Dropout', **kwargs) 226 | 227 | @classmethod 228 | def map_batch_norm(cls, node): 229 | kwargs = {'scale' : len(node.data) >= 3, 'bias' : len(node.data) == 4} 230 | epsilon = node.parameters.eps 231 | kwargs['epsilon'] = epsilon 232 | cls._convert_output_shape(kwargs, node) 233 | return Node.create('BatchNorm', **kwargs) 234 | 235 | @classmethod 236 | def map_scale(cls, node): 237 | raise NotImplementedError 238 | # TODO: The gamma parameter has to be set (in node.data?) and this should work. 239 | # Also, mean should be set to 0, and var to 1, just to be safe. 240 | scale_value = float(node.parameters.filler.value) 241 | kwargs = {'scale' : True, 'bias' : False, 'gamma' : scale_value, 'epsilon': 0} 242 | return Node.create('BatchNorm', **kwargs) 243 | 244 | @classmethod 245 | def map_eltwise(cls, node): 246 | operations = {0: 'Mul', 1: 'Add', 2: 'Max'} 247 | op_code = node.parameters.operation 248 | try: 249 | return Node.create(operations[op_code]) 250 | except KeyError: 251 | raise ConversionError('Unknown elementwise operation: {}'.format(op_code)) 252 | 253 | @classmethod 254 | def map_abs_val(cls, node): 255 | return Node.create('Abs') 256 | 257 | @classmethod 258 | def map_tanh(cls, node): 259 | return Node.create('Tanh') 260 | 261 | @classmethod 262 | def map_sigmoid(cls, node): 263 | return Node.create('Sigmoid') 264 | 265 | @classmethod 266 | def map_reshape(cls, node): 267 | kwargs = {'shape' : [dim for dim in node.output_shape]} 268 | cls._convert_output_shape(kwargs, node) 269 | return Node.create('Reshape', **kwargs) 270 | 271 | @classmethod 272 | def map_flatten(cls, node): 273 | return Node.create('Flatten') 274 | -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/mmdnn_caffe/transformer.py: -------------------------------------------------------------------------------- 1 | from __future__ import unicode_literals 2 | from google.protobuf import text_format 3 | import numpy as np 4 | from mmdnn.conversion.caffe.graph import GraphBuilder, NodeKind, LAYER_IN_TRAIN_PROTO 5 | from mmdnn.conversion.caffe.mapper import NodeMapper, get_handler_name 6 | from mmdnn.conversion.caffe.resolver import get_caffe_resolver, has_pycaffe 7 | from mmdnn.conversion.caffe.errors import print_stderr, ConversionError 8 | from mmdnn.conversion.caffe.common_graph import Graph 9 | from mmdnn.conversion.caffe.utils import get_lower_case, get_upper_case 10 | 11 | 12 | class DataInjector(object): 13 | ''' 14 | Associates parameters loaded from a .caffemodel file with their corresponding nodes. 15 | ''' 16 | 17 | def __init__(self, def_path, data_path): 18 | # The .prototxt file defining the graph 19 | self.def_path = def_path 20 | # The .caffemodel file containing the learned parameters 21 | self.data_path = data_path 22 | # Set to true if the fallback protocol-buffer based backend was used 23 | self.did_use_pb = False 24 | # A list containing (layer name, parameters) tuples 25 | self.params = None 26 | # Load the parameters 27 | self.caffemodel = None 28 | if has_pycaffe() and self.def_path: 29 | self.load_using_caffe() 30 | else: 31 | self.load_using_pb() 32 | 33 | def load_using_caffe(self): 34 | caffe = get_caffe_resolver().caffe 35 | net = caffe.Net(str(self.def_path), str(self.data_path), caffe.TEST) 36 | data = lambda blob: blob.data 37 | self.params = [(k, list(map(data, v))) for k, v in net.params.items()] 38 | 39 | def load_using_pb(self): 40 | self.caffemodel = get_caffe_resolver().NetParameter() 41 | self.caffemodel.MergeFromString(open(self.data_path, 'rb').read()) 42 | pair = lambda layer: (layer.name, self.normalize_pb_data(layer)) 43 | layers = self.caffemodel.layers or self.caffemodel.layer 44 | self.params = [pair(layer) for layer in layers if layer.blobs] 45 | self.did_use_pb = True 46 | 47 | def normalize_pb_data(self, layer): 48 | transformed = [] 49 | for blob in layer.blobs: 50 | if len(blob.shape.dim): 51 | dims = blob.shape.dim 52 | c_o, c_i, h, w = map(int, [1] * (4 - len(dims)) + list(dims)) 53 | else: 54 | c_o = blob.num 55 | c_i = blob.channels 56 | h = blob.height 57 | w = blob.width 58 | data = np.array(blob.data, dtype=np.float32).reshape(c_o, c_i, h, w) 59 | transformed.append(data) 60 | return transformed 61 | 62 | def adjust_parameters(self, node, data): 63 | if not self.did_use_pb: 64 | return data 65 | # When using the protobuf-backend, each parameter initially has four dimensions. 66 | # In certain cases (like FC layers), we want to eliminate the singleton dimensions. 67 | # This implementation takes care of the common cases. However, it does leave the 68 | # potential for future issues. 69 | # The Caffe-backend does not suffer from this problem. 70 | data = list(data) 71 | squeeze_indices = [1] # Squeeze biases. 72 | if node.kind == NodeKind.InnerProduct: 73 | squeeze_indices.append(0) # Squeeze FC. 74 | for idx in squeeze_indices: 75 | data[idx] = np.squeeze(data[idx]) 76 | return data 77 | 78 | def __call__(self, graph): 79 | for layer_name, data in self.params: 80 | if layer_name in graph: 81 | node = graph.get_node(layer_name) 82 | node.data = self.adjust_parameters(node, data) 83 | else: 84 | print_stderr('Ignoring parameters for non-existent layer: %s' % layer_name) 85 | return graph 86 | 87 | 88 | class NodeRenamer(object): 89 | 90 | def __call__(self, graph): 91 | for node in graph.nodes: 92 | node.name = node.name.replace('/', '_') 93 | return graph 94 | 95 | 96 | class DataReshaper(object): 97 | 98 | def __init__(self, mapping, replace=True): 99 | # A dictionary mapping NodeKind to the transposed order. 100 | self.mapping = mapping 101 | # The node kinds eligible for reshaping 102 | self.reshaped_node_types = self.mapping.keys() 103 | # If true, the reshaped data will replace the old one. 104 | # Otherwise, it's set to the reshaped_data attribute. 105 | self.replace = replace 106 | 107 | def has_spatial_parent(self, node): 108 | try: 109 | parent = node.get_only_parent()[0] 110 | s = parent.output_shape 111 | return s.height > 1 or s.width > 1 112 | except ConversionError: 113 | return False 114 | 115 | def map(self, node_kind): 116 | try: 117 | return self.mapping[node_kind] 118 | except KeyError: 119 | raise ConversionError('Ordering not found for node kind: {}'.format(node_kind)) 120 | 121 | def _is_image_data(self, node): 122 | return len([child for child in node.children if child.kind in (NodeKind.Convolution, NodeKind.Pooling)]) 123 | 124 | def __call__(self, graph): 125 | for node in graph.nodes: 126 | if node.data is None: 127 | continue 128 | if node.kind not in self.reshaped_node_types: 129 | # Check for 2+ dimensional data 130 | if any(len(tensor.shape) > 1 for tensor in node.data): 131 | print_stderr('Warning: parameters not reshaped for node: {}'.format(node)) 132 | continue 133 | transpose_order = self.map(node.kind) 134 | weights = node.data[0] 135 | if (node.kind == NodeKind.InnerProduct) and self.has_spatial_parent(node): 136 | # The FC layer connected to the spatial layer needs to be 137 | # re-wired to match the new spatial ordering. 138 | in_shape = node.get_only_parent()[0].output_shape 139 | fc_shape = weights.shape 140 | output_channels = fc_shape[0] 141 | weights = weights.reshape((output_channels, in_shape.channels, in_shape.height, 142 | in_shape.width)) 143 | weights = weights.transpose(self.map(NodeKind.Convolution)) 144 | node.reshaped_data = weights.reshape(fc_shape[transpose_order[0]], 145 | fc_shape[transpose_order[1]]) 146 | else: 147 | node.reshaped_data = weights.transpose(transpose_order) 148 | # node.reshaped_data = weights.transpose(transpose_order) 149 | if self.replace: 150 | for node in graph.nodes: 151 | if hasattr(node, 'reshaped_data'): 152 | # Set the weights 153 | node.data[0] = node.reshaped_data 154 | del node.reshaped_data 155 | return graph 156 | 157 | 158 | class SubNodeFuser(object): 159 | ''' 160 | An abstract helper for merging a single-child with its single-parent. 161 | ''' 162 | 163 | def __call__(self, graph): 164 | nodes = graph.nodes 165 | fused_nodes = [] 166 | for node in nodes: 167 | if len(node.parents) != 1: 168 | # We're only fusing nodes with single parents 169 | continue 170 | parent, from_output = node.get_only_parent() 171 | if len(parent.children) != 1: 172 | # We can only fuse a node if its parent's 173 | # value isn't used by any other node. 174 | continue 175 | if not self.is_eligible_pair(parent, node): 176 | continue 177 | # Rewrite the fused node's children to its parent. 178 | for child in node.children: 179 | child.parents = [(input, idx) for input, idx in child.parents if input != node] 180 | child.add_parent(parent, from_output) 181 | # Disconnect the fused node from the graph. 182 | parent.children.remove(node) 183 | fused_nodes.append(node) 184 | # Let the sub-class merge the fused node in any arbitrary way. 185 | self.merge(parent, node) 186 | transformed_nodes = [node for node in nodes if node not in fused_nodes] 187 | return graph.replaced(transformed_nodes) 188 | 189 | def is_eligible_pair(self, parent, child): 190 | '''Returns true if this parent/child pair is eligible for fusion.''' 191 | raise NotImplementedError('Must be implemented by subclass.') 192 | 193 | def merge(self, parent, child): 194 | '''Merge the child node into the parent.''' 195 | raise NotImplementedError('Must be implemented by subclass') 196 | 197 | 198 | class ReLUFuser(SubNodeFuser): 199 | ''' 200 | Fuses rectified linear units with their parent nodes. 201 | ''' 202 | 203 | def __init__(self, allowed_parent_types=None): 204 | # Fuse ReLUs when the parent node is one of the given types. 205 | # If None, all node types are eligible. 206 | self.allowed_parent_types = allowed_parent_types 207 | 208 | def is_eligible_pair(self, parent, child): 209 | return ((self.allowed_parent_types is None or parent.kind in self.allowed_parent_types) and 210 | child.kind == NodeKind.ReLU) 211 | 212 | def merge(self, parent, _): 213 | parent.metadata['relu'] = True 214 | 215 | 216 | class BatchNormScaleBiasFuser(SubNodeFuser): 217 | ''' 218 | The original batch normalization paper includes two learned 219 | parameters: a scaling factor \gamma and a bias \beta. 220 | Caffe's implementation does not include these two. However, it is commonly 221 | replicated by adding a scaling+bias layer immidiately after the batch norm. 222 | 223 | This fuser merges the scaling+bias layer with the batch norm. 224 | ''' 225 | 226 | def is_eligible_pair(self, parent, child): 227 | return (parent.kind == NodeKind.BatchNorm and child.kind == NodeKind.Scale and 228 | child.parameters.axis == 1 and child.parameters.bias_term == True) 229 | 230 | def merge(self, parent, child): 231 | parent.scale_bias_node = child 232 | 233 | 234 | class BatchNormPreprocessor(object): 235 | ''' 236 | Prescale batch normalization parameters. 237 | Concatenate gamma (scale) and beta (bias) terms if set. 238 | ''' 239 | 240 | def __call__(self, graph): 241 | for node in graph.nodes: 242 | if node.kind != NodeKind.BatchNorm: 243 | continue 244 | assert node.data is not None 245 | assert len(node.data) == 3 246 | mean, variance, scale = node.data 247 | 248 | # Prescale the stats 249 | scaling_factor = 1.0 / scale if scale != 0 else 0 250 | 251 | if len(np.squeeze(mean) == 1): 252 | mean = np.squeeze(mean) 253 | variance = np.squeeze(variance) 254 | scaling_factor = np.squeeze(scaling_factor) 255 | 256 | mean *= scaling_factor 257 | variance *= scaling_factor 258 | 259 | # Replace with the updated values 260 | node.data = [mean, variance] 261 | if hasattr(node, 'scale_bias_node'): 262 | # Include the scale and bias terms 263 | gamma, beta = node.scale_bias_node.data 264 | node.data += [gamma, beta] 265 | return graph 266 | 267 | 268 | class ParameterNamer(object): 269 | ''' 270 | Convert layer data arrays to a dictionary mapping parameter names to their values. 271 | ''' 272 | 273 | def __call__(self, graph): 274 | for node in graph.nodes: 275 | if node.data is None: 276 | continue 277 | if node.kind in (NodeKind.Convolution, NodeKind.Deconvolution, NodeKind.InnerProduct): 278 | names = ('weights',) 279 | if node.parameters.bias_term: 280 | names += ('bias',) 281 | elif node.kind == NodeKind.BatchNorm: 282 | names = ('mean', 'var') 283 | if len(node.data) == 4: 284 | names += ('scale', 'bias') 285 | elif node.kind == NodeKind.PReLU: 286 | names = ('gamma',) 287 | else: 288 | print_stderr('WARNING: Unhandled parameters: {}'.format(node.kind)) 289 | continue 290 | assert len(names) == len(node.data) 291 | node.data = dict(zip(names, node.data)) 292 | return graph 293 | 294 | 295 | class CaffeTransformer(object): 296 | 297 | def __init__(self, def_path, data_path, target_toolkit, input_shape=None, phase='test'): 298 | self.layer_name_map = {} 299 | self.data_injector = None 300 | self.is_train_proto = False 301 | self.input_shape = input_shape 302 | if def_path is None: 303 | if self.input_shape is None: 304 | raise ConversionError('if the graph prototxt is not provided, the input shape should be provided') 305 | self.input_shape = [1] + self.input_shape 306 | def_path, self.data_injector = self.gen_prototxt_from_caffemodel(data_path, self.input_shape) 307 | self.is_train_proto = True 308 | else: 309 | model = get_caffe_resolver().NetParameter() 310 | with open(def_path, 'r') as f: 311 | text_format.Merge(f.read(), model) 312 | layers = model.layers or model.layer 313 | if len([layer for layer in layers if NodeKind.map_raw_kind(layer.type) in LAYER_IN_TRAIN_PROTO]) > 0: 314 | if self.input_shape is None: 315 | raise ConversionError('the train_val.prototxt should be provided with the input shape') 316 | self.input_shape = [1] + self.input_shape 317 | self.is_train_proto = True 318 | graph = GraphBuilder(def_path, self.input_shape, self.is_train_proto, phase).build() 319 | if self.is_train_proto: 320 | def_path = graph.prototxt 321 | if data_path is not None: 322 | graph = graph.transformed([ 323 | self.data_injector if self.data_injector else DataInjector(def_path, data_path), # Load and associate learned parameters 324 | BatchNormScaleBiasFuser(), 325 | BatchNormPreprocessor() # Pre-process batch normalization data 326 | ]) 327 | target_toolkit = target_toolkit.lower() 328 | if target_toolkit not in ('caffe', 'caffe2'): 329 | graph = graph.transformed([DataReshaper({ # Reshape the parameters to TensorFlow's ordering 330 | NodeKind.Convolution: (2, 3, 1, 0), # (c_o, c_i, h, w) -> (h, w, c_i, c_o) 331 | NodeKind.Deconvolution: (2, 3, 1, 0), # (c_o, c_i, h, w) -> (h, w, c_i, c_o) 332 | NodeKind.InnerProduct: (1, 0) # (c_o, c_i) -> (c_i, c_o) 333 | }), 334 | ParameterNamer() # Convert parameters to dictionaries 335 | ]) 336 | self.graph = graph 337 | # self.graph = NodeRenamer()(graph) 338 | print (self.graph) 339 | 340 | def gen_prototxt_from_caffemodel(self, data_path, input_shape): 341 | prototxt = 'deploy.prototxt' 342 | data_injector = DataInjector(None, data_path) 343 | caffemodel = data_injector.caffemodel 344 | layers = caffemodel.layers or caffemodel.layer 345 | for item in layers: 346 | item.ClearField('blobs') 347 | with open(prototxt ,'w') as f: 348 | f.write(str(caffemodel)) 349 | return prototxt, data_injector 350 | 351 | def transform_data(self): 352 | return {self.layer_name_map[node.name]: node.data for node in self.graph.nodes if node.data} 353 | 354 | def transform_graph(self): 355 | for node in self.graph.nodes: 356 | self.layer_name_map[node.name] = node.name 357 | 358 | ret = [] 359 | for node in self.graph.nodes: 360 | mapped_node = self.map_node(node) 361 | if isinstance(mapped_node, list): 362 | ret.extend([n for n in mapped_node]) 363 | else: 364 | ret.append(mapped_node) 365 | 366 | name = get_upper_case(get_lower_case(self.graph.name)) 367 | return Graph(name, ret) 368 | #return Graph(name, [self.map_node(node) for node in self.graph.nodes]) 369 | 370 | def get_handler(self, node_kind, prefix): 371 | name = get_handler_name(node_kind) 372 | name = '_'.join((prefix, name)) 373 | try: 374 | return getattr(NodeMapper, name) 375 | except AttributeError: 376 | raise ConversionError('No handler found for node kind: %s (expected: %s)' % (node_kind, name)) 377 | 378 | def map_node(self, node): 379 | map_func = self.get_handler(node.kind, 'map') 380 | 381 | mapped_node = map_func(node) 382 | assert mapped_node is not None 383 | if isinstance(mapped_node, list): 384 | ret = [] 385 | for idx, cur_node in enumerate(mapped_node): 386 | cur_node.name = node.name + '_' + str(idx) 387 | if idx == 0: 388 | cur_node.input.extend([self.layer_name_map[input.name] for input, idx in node.parents]) 389 | else: 390 | cur_node.input.extend([node.name + '_' + str(idx - 1)]) 391 | 392 | if idx == len(mapped_node) - 1: 393 | cur_node.output.extend(node.output) 394 | else: 395 | cur_node.output.extend([node.name + '_' + str(idx + 1)]) 396 | 397 | self.layer_name_map[node.name] = node.name + '_' + str(len(mapped_node) - 1) 398 | ret.append(cur_node) 399 | return ret 400 | 401 | else: 402 | mapped_node.name = node.name 403 | # Kit 404 | # mapped_node.input.extend(['%s:%s' % (input.name, idx) for input, idx in node.parents]) 405 | mapped_node.input.extend(['%s' % (self.layer_name_map[input.name]) for input, idx in node.parents]) 406 | mapped_node.output.extend(node.output) 407 | return mapped_node 408 | -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/face/pose_pytorch.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | __weights_dict = dict() 7 | 8 | def load_weights(weight_file): 9 | if weight_file == None: 10 | return 11 | 12 | try: 13 | weights_dict = np.load(weight_file).item() 14 | except: 15 | weights_dict = np.load(weight_file, encoding='bytes').item() 16 | 17 | return weights_dict 18 | 19 | class KitModel(nn.Module): 20 | 21 | 22 | def __init__(self, weight_file): 23 | super(KitModel, self).__init__() 24 | global __weights_dict 25 | __weights_dict = load_weights(weight_file) 26 | 27 | self.conv1_1 = self.__conv(2, name='conv1_1', in_channels=3, out_channels=64, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 28 | self.conv1_2 = self.__conv(2, name='conv1_2', in_channels=64, out_channels=64, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 29 | self.conv2_1 = self.__conv(2, name='conv2_1', in_channels=64, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 30 | self.conv2_2 = self.__conv(2, name='conv2_2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 31 | self.conv3_1 = self.__conv(2, name='conv3_1', in_channels=128, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 32 | self.conv3_2 = self.__conv(2, name='conv3_2', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 33 | self.conv3_3 = self.__conv(2, name='conv3_3', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 34 | self.conv3_4 = self.__conv(2, name='conv3_4', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 35 | self.conv4_1 = self.__conv(2, name='conv4_1', in_channels=256, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 36 | self.conv4_2 = self.__conv(2, name='conv4_2', in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 37 | self.conv4_3 = self.__conv(2, name='conv4_3', in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 38 | self.conv4_4 = self.__conv(2, name='conv4_4', in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 39 | self.conv5_1 = self.__conv(2, name='conv5_1', in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 40 | self.conv5_2 = self.__conv(2, name='conv5_2', in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 41 | self.conv5_3_CPM = self.__conv(2, name='conv5_3_CPM', in_channels=512, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 42 | self.conv6_1_CPM = self.__conv(2, name='conv6_1_CPM', in_channels=128, out_channels=512, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 43 | self.conv6_2_CPM = self.__conv(2, name='conv6_2_CPM', in_channels=512, out_channels=71, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 44 | self.Mconv1_stage2 = self.__conv(2, name='Mconv1_stage2', in_channels=199, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 45 | self.Mconv2_stage2 = self.__conv(2, name='Mconv2_stage2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 46 | self.Mconv3_stage2 = self.__conv(2, name='Mconv3_stage2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 47 | self.Mconv4_stage2 = self.__conv(2, name='Mconv4_stage2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 48 | self.Mconv5_stage2 = self.__conv(2, name='Mconv5_stage2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 49 | self.Mconv6_stage2 = self.__conv(2, name='Mconv6_stage2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 50 | self.Mconv7_stage2 = self.__conv(2, name='Mconv7_stage2', in_channels=128, out_channels=71, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 51 | self.Mconv1_stage3 = self.__conv(2, name='Mconv1_stage3', in_channels=199, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 52 | self.Mconv2_stage3 = self.__conv(2, name='Mconv2_stage3', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 53 | self.Mconv3_stage3 = self.__conv(2, name='Mconv3_stage3', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 54 | self.Mconv4_stage3 = self.__conv(2, name='Mconv4_stage3', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 55 | self.Mconv5_stage3 = self.__conv(2, name='Mconv5_stage3', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 56 | self.Mconv6_stage3 = self.__conv(2, name='Mconv6_stage3', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 57 | self.Mconv7_stage3 = self.__conv(2, name='Mconv7_stage3', in_channels=128, out_channels=71, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 58 | self.Mconv1_stage4 = self.__conv(2, name='Mconv1_stage4', in_channels=199, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 59 | self.Mconv2_stage4 = self.__conv(2, name='Mconv2_stage4', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 60 | self.Mconv3_stage4 = self.__conv(2, name='Mconv3_stage4', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 61 | self.Mconv4_stage4 = self.__conv(2, name='Mconv4_stage4', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 62 | self.Mconv5_stage4 = self.__conv(2, name='Mconv5_stage4', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 63 | self.Mconv6_stage4 = self.__conv(2, name='Mconv6_stage4', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 64 | self.Mconv7_stage4 = self.__conv(2, name='Mconv7_stage4', in_channels=128, out_channels=71, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 65 | self.Mconv1_stage5 = self.__conv(2, name='Mconv1_stage5', in_channels=199, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 66 | self.Mconv2_stage5 = self.__conv(2, name='Mconv2_stage5', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 67 | self.Mconv3_stage5 = self.__conv(2, name='Mconv3_stage5', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 68 | self.Mconv4_stage5 = self.__conv(2, name='Mconv4_stage5', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 69 | self.Mconv5_stage5 = self.__conv(2, name='Mconv5_stage5', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 70 | self.Mconv6_stage5 = self.__conv(2, name='Mconv6_stage5', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 71 | self.Mconv7_stage5 = self.__conv(2, name='Mconv7_stage5', in_channels=128, out_channels=71, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 72 | self.Mconv1_stage6 = self.__conv(2, name='Mconv1_stage6', in_channels=199, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 73 | self.Mconv2_stage6 = self.__conv(2, name='Mconv2_stage6', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 74 | self.Mconv3_stage6 = self.__conv(2, name='Mconv3_stage6', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 75 | self.Mconv4_stage6 = self.__conv(2, name='Mconv4_stage6', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 76 | self.Mconv5_stage6 = self.__conv(2, name='Mconv5_stage6', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 77 | self.Mconv6_stage6 = self.__conv(2, name='Mconv6_stage6', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 78 | self.Mconv7_stage6 = self.__conv(2, name='Mconv7_stage6', in_channels=128, out_channels=71, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 79 | 80 | def forward(self, x): 81 | conv1_1_pad = F.pad(x, (1, 1, 1, 1)) 82 | conv1_1 = self.conv1_1(conv1_1_pad) 83 | conv1_1_re = F.relu(conv1_1) 84 | conv1_2_pad = F.pad(conv1_1_re, (1, 1, 1, 1)) 85 | conv1_2 = self.conv1_2(conv1_2_pad) 86 | conv1_2_re = F.relu(conv1_2) 87 | pool1_pad = F.pad(conv1_2_re, (0, 1, 0, 1), value=float('-inf')) 88 | pool1 = F.max_pool2d(pool1_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 89 | conv2_1_pad = F.pad(pool1, (1, 1, 1, 1)) 90 | conv2_1 = self.conv2_1(conv2_1_pad) 91 | conv2_1_re = F.relu(conv2_1) 92 | conv2_2_pad = F.pad(conv2_1_re, (1, 1, 1, 1)) 93 | conv2_2 = self.conv2_2(conv2_2_pad) 94 | conv2_2_re = F.relu(conv2_2) 95 | pool2_pad = F.pad(conv2_2_re, (0, 1, 0, 1), value=float('-inf')) 96 | pool2 = F.max_pool2d(pool2_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 97 | conv3_1_pad = F.pad(pool2, (1, 1, 1, 1)) 98 | conv3_1 = self.conv3_1(conv3_1_pad) 99 | conv3_1_re = F.relu(conv3_1) 100 | conv3_2_pad = F.pad(conv3_1_re, (1, 1, 1, 1)) 101 | conv3_2 = self.conv3_2(conv3_2_pad) 102 | conv3_2_re = F.relu(conv3_2) 103 | conv3_3_pad = F.pad(conv3_2_re, (1, 1, 1, 1)) 104 | conv3_3 = self.conv3_3(conv3_3_pad) 105 | conv3_3_re = F.relu(conv3_3) 106 | conv3_4_pad = F.pad(conv3_3_re, (1, 1, 1, 1)) 107 | conv3_4 = self.conv3_4(conv3_4_pad) 108 | conv3_4_re = F.relu(conv3_4) 109 | pool3_pad = F.pad(conv3_4_re, (0, 1, 0, 1), value=float('-inf')) 110 | pool3 = F.max_pool2d(pool3_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 111 | conv4_1_pad = F.pad(pool3, (1, 1, 1, 1)) 112 | conv4_1 = self.conv4_1(conv4_1_pad) 113 | conv4_1_re = F.relu(conv4_1) 114 | conv4_2_pad = F.pad(conv4_1_re, (1, 1, 1, 1)) 115 | conv4_2 = self.conv4_2(conv4_2_pad) 116 | conv4_2_re = F.relu(conv4_2) 117 | conv4_3_pad = F.pad(conv4_2_re, (1, 1, 1, 1)) 118 | conv4_3 = self.conv4_3(conv4_3_pad) 119 | conv4_3_re = F.relu(conv4_3) 120 | conv4_4_pad = F.pad(conv4_3_re, (1, 1, 1, 1)) 121 | conv4_4 = self.conv4_4(conv4_4_pad) 122 | conv4_4_re = F.relu(conv4_4) 123 | conv5_1_pad = F.pad(conv4_4_re, (1, 1, 1, 1)) 124 | conv5_1 = self.conv5_1(conv5_1_pad) 125 | conv5_1_re = F.relu(conv5_1) 126 | conv5_2_pad = F.pad(conv5_1_re, (1, 1, 1, 1)) 127 | conv5_2 = self.conv5_2(conv5_2_pad) 128 | conv5_2_re = F.relu(conv5_2) 129 | conv5_3_CPM_pad = F.pad(conv5_2_re, (1, 1, 1, 1)) 130 | conv5_3_CPM = self.conv5_3_CPM(conv5_3_CPM_pad) 131 | conv5_3_CPM_re = F.relu(conv5_3_CPM) 132 | conv6_1_CPM = self.conv6_1_CPM(conv5_3_CPM_re) 133 | conv6_1_CPM_re = F.relu(conv6_1_CPM) 134 | conv6_2_CPM = self.conv6_2_CPM(conv6_1_CPM_re) 135 | features_in_stage_2 = torch.cat((conv6_2_CPM, conv5_3_CPM_re), 1) 136 | Mconv1_stage2_pad = F.pad(features_in_stage_2, (3, 3, 3, 3)) 137 | Mconv1_stage2 = self.Mconv1_stage2(Mconv1_stage2_pad) 138 | Mconv1_stage2_re = F.relu(Mconv1_stage2) 139 | Mconv2_stage2_pad = F.pad(Mconv1_stage2_re, (3, 3, 3, 3)) 140 | Mconv2_stage2 = self.Mconv2_stage2(Mconv2_stage2_pad) 141 | Mconv2_stage2_re = F.relu(Mconv2_stage2) 142 | Mconv3_stage2_pad = F.pad(Mconv2_stage2_re, (3, 3, 3, 3)) 143 | Mconv3_stage2 = self.Mconv3_stage2(Mconv3_stage2_pad) 144 | Mconv3_stage2_re = F.relu(Mconv3_stage2) 145 | Mconv4_stage2_pad = F.pad(Mconv3_stage2_re, (3, 3, 3, 3)) 146 | Mconv4_stage2 = self.Mconv4_stage2(Mconv4_stage2_pad) 147 | Mconv4_stage2_re = F.relu(Mconv4_stage2) 148 | Mconv5_stage2_pad = F.pad(Mconv4_stage2_re, (3, 3, 3, 3)) 149 | Mconv5_stage2 = self.Mconv5_stage2(Mconv5_stage2_pad) 150 | Mconv5_stage2_re = F.relu(Mconv5_stage2) 151 | Mconv6_stage2 = self.Mconv6_stage2(Mconv5_stage2_re) 152 | Mconv6_stage2_re = F.relu(Mconv6_stage2) 153 | Mconv7_stage2 = self.Mconv7_stage2(Mconv6_stage2_re) 154 | features_in_stage_3 = torch.cat((Mconv7_stage2, conv5_3_CPM_re), 1) 155 | Mconv1_stage3_pad = F.pad(features_in_stage_3, (3, 3, 3, 3)) 156 | Mconv1_stage3 = self.Mconv1_stage3(Mconv1_stage3_pad) 157 | Mconv1_stage3_re = F.relu(Mconv1_stage3) 158 | Mconv2_stage3_pad = F.pad(Mconv1_stage3_re, (3, 3, 3, 3)) 159 | Mconv2_stage3 = self.Mconv2_stage3(Mconv2_stage3_pad) 160 | Mconv2_stage3_re = F.relu(Mconv2_stage3) 161 | Mconv3_stage3_pad = F.pad(Mconv2_stage3_re, (3, 3, 3, 3)) 162 | Mconv3_stage3 = self.Mconv3_stage3(Mconv3_stage3_pad) 163 | Mconv3_stage3_re = F.relu(Mconv3_stage3) 164 | Mconv4_stage3_pad = F.pad(Mconv3_stage3_re, (3, 3, 3, 3)) 165 | Mconv4_stage3 = self.Mconv4_stage3(Mconv4_stage3_pad) 166 | Mconv4_stage3_re = F.relu(Mconv4_stage3) 167 | Mconv5_stage3_pad = F.pad(Mconv4_stage3_re, (3, 3, 3, 3)) 168 | Mconv5_stage3 = self.Mconv5_stage3(Mconv5_stage3_pad) 169 | Mconv5_stage3_re = F.relu(Mconv5_stage3) 170 | Mconv6_stage3 = self.Mconv6_stage3(Mconv5_stage3_re) 171 | Mconv6_stage3_re = F.relu(Mconv6_stage3) 172 | Mconv7_stage3 = self.Mconv7_stage3(Mconv6_stage3_re) 173 | features_in_stage_4 = torch.cat((Mconv7_stage3, conv5_3_CPM_re), 1) 174 | Mconv1_stage4_pad = F.pad(features_in_stage_4, (3, 3, 3, 3)) 175 | Mconv1_stage4 = self.Mconv1_stage4(Mconv1_stage4_pad) 176 | Mconv1_stage4_re = F.relu(Mconv1_stage4) 177 | Mconv2_stage4_pad = F.pad(Mconv1_stage4_re, (3, 3, 3, 3)) 178 | Mconv2_stage4 = self.Mconv2_stage4(Mconv2_stage4_pad) 179 | Mconv2_stage4_re = F.relu(Mconv2_stage4) 180 | Mconv3_stage4_pad = F.pad(Mconv2_stage4_re, (3, 3, 3, 3)) 181 | Mconv3_stage4 = self.Mconv3_stage4(Mconv3_stage4_pad) 182 | Mconv3_stage4_re = F.relu(Mconv3_stage4) 183 | Mconv4_stage4_pad = F.pad(Mconv3_stage4_re, (3, 3, 3, 3)) 184 | Mconv4_stage4 = self.Mconv4_stage4(Mconv4_stage4_pad) 185 | Mconv4_stage4_re = F.relu(Mconv4_stage4) 186 | Mconv5_stage4_pad = F.pad(Mconv4_stage4_re, (3, 3, 3, 3)) 187 | Mconv5_stage4 = self.Mconv5_stage4(Mconv5_stage4_pad) 188 | Mconv5_stage4_re = F.relu(Mconv5_stage4) 189 | Mconv6_stage4 = self.Mconv6_stage4(Mconv5_stage4_re) 190 | Mconv6_stage4_re = F.relu(Mconv6_stage4) 191 | Mconv7_stage4 = self.Mconv7_stage4(Mconv6_stage4_re) 192 | features_in_stage_5 = torch.cat((Mconv7_stage4, conv5_3_CPM_re), 1) 193 | Mconv1_stage5_pad = F.pad(features_in_stage_5, (3, 3, 3, 3)) 194 | Mconv1_stage5 = self.Mconv1_stage5(Mconv1_stage5_pad) 195 | Mconv1_stage5_re = F.relu(Mconv1_stage5) 196 | Mconv2_stage5_pad = F.pad(Mconv1_stage5_re, (3, 3, 3, 3)) 197 | Mconv2_stage5 = self.Mconv2_stage5(Mconv2_stage5_pad) 198 | Mconv2_stage5_re = F.relu(Mconv2_stage5) 199 | Mconv3_stage5_pad = F.pad(Mconv2_stage5_re, (3, 3, 3, 3)) 200 | Mconv3_stage5 = self.Mconv3_stage5(Mconv3_stage5_pad) 201 | Mconv3_stage5_re = F.relu(Mconv3_stage5) 202 | Mconv4_stage5_pad = F.pad(Mconv3_stage5_re, (3, 3, 3, 3)) 203 | Mconv4_stage5 = self.Mconv4_stage5(Mconv4_stage5_pad) 204 | Mconv4_stage5_re = F.relu(Mconv4_stage5) 205 | Mconv5_stage5_pad = F.pad(Mconv4_stage5_re, (3, 3, 3, 3)) 206 | Mconv5_stage5 = self.Mconv5_stage5(Mconv5_stage5_pad) 207 | Mconv5_stage5_re = F.relu(Mconv5_stage5) 208 | Mconv6_stage5 = self.Mconv6_stage5(Mconv5_stage5_re) 209 | Mconv6_stage5_re = F.relu(Mconv6_stage5) 210 | Mconv7_stage5 = self.Mconv7_stage5(Mconv6_stage5_re) 211 | features_in_stage_6 = torch.cat((Mconv7_stage5, conv5_3_CPM_re), 1) 212 | Mconv1_stage6_pad = F.pad(features_in_stage_6, (3, 3, 3, 3)) 213 | Mconv1_stage6 = self.Mconv1_stage6(Mconv1_stage6_pad) 214 | Mconv1_stage6_re = F.relu(Mconv1_stage6) 215 | Mconv2_stage6_pad = F.pad(Mconv1_stage6_re, (3, 3, 3, 3)) 216 | Mconv2_stage6 = self.Mconv2_stage6(Mconv2_stage6_pad) 217 | Mconv2_stage6_re = F.relu(Mconv2_stage6) 218 | Mconv3_stage6_pad = F.pad(Mconv2_stage6_re, (3, 3, 3, 3)) 219 | Mconv3_stage6 = self.Mconv3_stage6(Mconv3_stage6_pad) 220 | Mconv3_stage6_re = F.relu(Mconv3_stage6) 221 | Mconv4_stage6_pad = F.pad(Mconv3_stage6_re, (3, 3, 3, 3)) 222 | Mconv4_stage6 = self.Mconv4_stage6(Mconv4_stage6_pad) 223 | Mconv4_stage6_re = F.relu(Mconv4_stage6) 224 | Mconv5_stage6_pad = F.pad(Mconv4_stage6_re, (3, 3, 3, 3)) 225 | Mconv5_stage6 = self.Mconv5_stage6(Mconv5_stage6_pad) 226 | Mconv5_stage6_re = F.relu(Mconv5_stage6) 227 | Mconv6_stage6 = self.Mconv6_stage6(Mconv5_stage6_re) 228 | Mconv6_stage6_re = F.relu(Mconv6_stage6) 229 | Mconv7_stage6 = self.Mconv7_stage6(Mconv6_stage6_re) 230 | return Mconv7_stage6 231 | 232 | 233 | @staticmethod 234 | def __conv(dim, name, **kwargs): 235 | if dim == 1: layer = nn.Conv1d(**kwargs) 236 | elif dim == 2: layer = nn.Conv2d(**kwargs) 237 | elif dim == 3: layer = nn.Conv3d(**kwargs) 238 | else: raise NotImplementedError() 239 | 240 | layer.state_dict()['weight'].copy_(torch.from_numpy(__weights_dict[name]['weights'])) 241 | if 'bias' in __weights_dict[name]: 242 | layer.state_dict()['bias'].copy_(torch.from_numpy(__weights_dict[name]['bias'])) 243 | return layer 244 | 245 | model = KitModel('pose_pytorch.npy') 246 | input_size = (1, 3, 368, 368) 247 | -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/hand/pose_pytorch.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | __weights_dict = dict() 7 | 8 | def load_weights(weight_file): 9 | if weight_file == None: 10 | return 11 | 12 | try: 13 | weights_dict = np.load(weight_file).item() 14 | except: 15 | weights_dict = np.load(weight_file, encoding='bytes').item() 16 | 17 | return weights_dict 18 | 19 | class KitModel(nn.Module): 20 | 21 | 22 | def __init__(self, weight_file): 23 | super(KitModel, self).__init__() 24 | global __weights_dict 25 | __weights_dict = load_weights(weight_file) 26 | 27 | self.conv1_1 = self.__conv(2, name='conv1_1', in_channels=3, out_channels=64, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 28 | self.conv1_2 = self.__conv(2, name='conv1_2', in_channels=64, out_channels=64, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 29 | self.conv2_1 = self.__conv(2, name='conv2_1', in_channels=64, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 30 | self.conv2_2 = self.__conv(2, name='conv2_2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 31 | self.conv3_1 = self.__conv(2, name='conv3_1', in_channels=128, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 32 | self.conv3_2 = self.__conv(2, name='conv3_2', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 33 | self.conv3_3 = self.__conv(2, name='conv3_3', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 34 | self.conv3_4 = self.__conv(2, name='conv3_4', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 35 | self.conv4_1 = self.__conv(2, name='conv4_1', in_channels=256, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 36 | self.conv4_2 = self.__conv(2, name='conv4_2', in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 37 | self.conv4_3 = self.__conv(2, name='conv4_3', in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 38 | self.conv4_4 = self.__conv(2, name='conv4_4', in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 39 | self.conv5_1 = self.__conv(2, name='conv5_1', in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 40 | self.conv5_2 = self.__conv(2, name='conv5_2', in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 41 | self.conv5_3_CPM = self.__conv(2, name='conv5_3_CPM', in_channels=512, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 42 | self.conv6_1_CPM = self.__conv(2, name='conv6_1_CPM', in_channels=128, out_channels=512, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 43 | self.conv6_2_CPM = self.__conv(2, name='conv6_2_CPM', in_channels=512, out_channels=22, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 44 | self.Mconv1_stage2 = self.__conv(2, name='Mconv1_stage2', in_channels=150, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 45 | self.Mconv2_stage2 = self.__conv(2, name='Mconv2_stage2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 46 | self.Mconv3_stage2 = self.__conv(2, name='Mconv3_stage2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 47 | self.Mconv4_stage2 = self.__conv(2, name='Mconv4_stage2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 48 | self.Mconv5_stage2 = self.__conv(2, name='Mconv5_stage2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 49 | self.Mconv6_stage2 = self.__conv(2, name='Mconv6_stage2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 50 | self.Mconv7_stage2 = self.__conv(2, name='Mconv7_stage2', in_channels=128, out_channels=22, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 51 | self.Mconv1_stage3 = self.__conv(2, name='Mconv1_stage3', in_channels=150, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 52 | self.Mconv2_stage3 = self.__conv(2, name='Mconv2_stage3', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 53 | self.Mconv3_stage3 = self.__conv(2, name='Mconv3_stage3', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 54 | self.Mconv4_stage3 = self.__conv(2, name='Mconv4_stage3', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 55 | self.Mconv5_stage3 = self.__conv(2, name='Mconv5_stage3', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 56 | self.Mconv6_stage3 = self.__conv(2, name='Mconv6_stage3', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 57 | self.Mconv7_stage3 = self.__conv(2, name='Mconv7_stage3', in_channels=128, out_channels=22, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 58 | self.Mconv1_stage4 = self.__conv(2, name='Mconv1_stage4', in_channels=150, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 59 | self.Mconv2_stage4 = self.__conv(2, name='Mconv2_stage4', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 60 | self.Mconv3_stage4 = self.__conv(2, name='Mconv3_stage4', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 61 | self.Mconv4_stage4 = self.__conv(2, name='Mconv4_stage4', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 62 | self.Mconv5_stage4 = self.__conv(2, name='Mconv5_stage4', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 63 | self.Mconv6_stage4 = self.__conv(2, name='Mconv6_stage4', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 64 | self.Mconv7_stage4 = self.__conv(2, name='Mconv7_stage4', in_channels=128, out_channels=22, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 65 | self.Mconv1_stage5 = self.__conv(2, name='Mconv1_stage5', in_channels=150, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 66 | self.Mconv2_stage5 = self.__conv(2, name='Mconv2_stage5', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 67 | self.Mconv3_stage5 = self.__conv(2, name='Mconv3_stage5', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 68 | self.Mconv4_stage5 = self.__conv(2, name='Mconv4_stage5', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 69 | self.Mconv5_stage5 = self.__conv(2, name='Mconv5_stage5', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 70 | self.Mconv6_stage5 = self.__conv(2, name='Mconv6_stage5', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 71 | self.Mconv7_stage5 = self.__conv(2, name='Mconv7_stage5', in_channels=128, out_channels=22, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 72 | self.Mconv1_stage6 = self.__conv(2, name='Mconv1_stage6', in_channels=150, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 73 | self.Mconv2_stage6 = self.__conv(2, name='Mconv2_stage6', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 74 | self.Mconv3_stage6 = self.__conv(2, name='Mconv3_stage6', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 75 | self.Mconv4_stage6 = self.__conv(2, name='Mconv4_stage6', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 76 | self.Mconv5_stage6 = self.__conv(2, name='Mconv5_stage6', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 77 | self.Mconv6_stage6 = self.__conv(2, name='Mconv6_stage6', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 78 | self.Mconv7_stage6 = self.__conv(2, name='Mconv7_stage6', in_channels=128, out_channels=22, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 79 | 80 | def forward(self, x): 81 | conv1_1_pad = F.pad(x, (1, 1, 1, 1)) 82 | conv1_1 = self.conv1_1(conv1_1_pad) 83 | relu1_1 = F.relu(conv1_1) 84 | conv1_2_pad = F.pad(relu1_1, (1, 1, 1, 1)) 85 | conv1_2 = self.conv1_2(conv1_2_pad) 86 | relu1_2 = F.relu(conv1_2) 87 | pool1_stage1_pad = F.pad(relu1_2, (0, 1, 0, 1), value=float('-inf')) 88 | pool1_stage1 = F.max_pool2d(pool1_stage1_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 89 | conv2_1_pad = F.pad(pool1_stage1, (1, 1, 1, 1)) 90 | conv2_1 = self.conv2_1(conv2_1_pad) 91 | relu2_1 = F.relu(conv2_1) 92 | conv2_2_pad = F.pad(relu2_1, (1, 1, 1, 1)) 93 | conv2_2 = self.conv2_2(conv2_2_pad) 94 | relu2_2 = F.relu(conv2_2) 95 | pool2_stage1_pad = F.pad(relu2_2, (0, 1, 0, 1), value=float('-inf')) 96 | pool2_stage1 = F.max_pool2d(pool2_stage1_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 97 | conv3_1_pad = F.pad(pool2_stage1, (1, 1, 1, 1)) 98 | conv3_1 = self.conv3_1(conv3_1_pad) 99 | relu3_1 = F.relu(conv3_1) 100 | conv3_2_pad = F.pad(relu3_1, (1, 1, 1, 1)) 101 | conv3_2 = self.conv3_2(conv3_2_pad) 102 | relu3_2 = F.relu(conv3_2) 103 | conv3_3_pad = F.pad(relu3_2, (1, 1, 1, 1)) 104 | conv3_3 = self.conv3_3(conv3_3_pad) 105 | relu3_3 = F.relu(conv3_3) 106 | conv3_4_pad = F.pad(relu3_3, (1, 1, 1, 1)) 107 | conv3_4 = self.conv3_4(conv3_4_pad) 108 | relu3_4 = F.relu(conv3_4) 109 | pool3_stage1_pad = F.pad(relu3_4, (0, 1, 0, 1), value=float('-inf')) 110 | pool3_stage1 = F.max_pool2d(pool3_stage1_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 111 | conv4_1_pad = F.pad(pool3_stage1, (1, 1, 1, 1)) 112 | conv4_1 = self.conv4_1(conv4_1_pad) 113 | relu4_1 = F.relu(conv4_1) 114 | conv4_2_pad = F.pad(relu4_1, (1, 1, 1, 1)) 115 | conv4_2 = self.conv4_2(conv4_2_pad) 116 | relu4_2 = F.relu(conv4_2) 117 | conv4_3_pad = F.pad(relu4_2, (1, 1, 1, 1)) 118 | conv4_3 = self.conv4_3(conv4_3_pad) 119 | relu4_3 = F.relu(conv4_3) 120 | conv4_4_pad = F.pad(relu4_3, (1, 1, 1, 1)) 121 | conv4_4 = self.conv4_4(conv4_4_pad) 122 | relu4_4 = F.relu(conv4_4) 123 | conv5_1_pad = F.pad(relu4_4, (1, 1, 1, 1)) 124 | conv5_1 = self.conv5_1(conv5_1_pad) 125 | relu5_1 = F.relu(conv5_1) 126 | conv5_2_pad = F.pad(relu5_1, (1, 1, 1, 1)) 127 | conv5_2 = self.conv5_2(conv5_2_pad) 128 | relu5_2 = F.relu(conv5_2) 129 | conv5_3_CPM_pad = F.pad(relu5_2, (1, 1, 1, 1)) 130 | conv5_3_CPM = self.conv5_3_CPM(conv5_3_CPM_pad) 131 | relu5_4_stage1_3 = F.relu(conv5_3_CPM) 132 | conv6_1_CPM = self.conv6_1_CPM(relu5_4_stage1_3) 133 | relu6_4_stage1_1 = F.relu(conv6_1_CPM) 134 | conv6_2_CPM = self.conv6_2_CPM(relu6_4_stage1_1) 135 | concat_stage2 = torch.cat((conv6_2_CPM, relu5_4_stage1_3), 1) 136 | Mconv1_stage2_pad = F.pad(concat_stage2, (3, 3, 3, 3)) 137 | Mconv1_stage2 = self.Mconv1_stage2(Mconv1_stage2_pad) 138 | Mrelu1_2_stage2_1 = F.relu(Mconv1_stage2) 139 | Mconv2_stage2_pad = F.pad(Mrelu1_2_stage2_1, (3, 3, 3, 3)) 140 | Mconv2_stage2 = self.Mconv2_stage2(Mconv2_stage2_pad) 141 | Mrelu1_3_stage2_2 = F.relu(Mconv2_stage2) 142 | Mconv3_stage2_pad = F.pad(Mrelu1_3_stage2_2, (3, 3, 3, 3)) 143 | Mconv3_stage2 = self.Mconv3_stage2(Mconv3_stage2_pad) 144 | Mrelu1_4_stage2_3 = F.relu(Mconv3_stage2) 145 | Mconv4_stage2_pad = F.pad(Mrelu1_4_stage2_3, (3, 3, 3, 3)) 146 | Mconv4_stage2 = self.Mconv4_stage2(Mconv4_stage2_pad) 147 | Mrelu1_5_stage2_4 = F.relu(Mconv4_stage2) 148 | Mconv5_stage2_pad = F.pad(Mrelu1_5_stage2_4, (3, 3, 3, 3)) 149 | Mconv5_stage2 = self.Mconv5_stage2(Mconv5_stage2_pad) 150 | Mrelu1_6_stage2_5 = F.relu(Mconv5_stage2) 151 | Mconv6_stage2 = self.Mconv6_stage2(Mrelu1_6_stage2_5) 152 | Mrelu1_7_stage2_6 = F.relu(Mconv6_stage2) 153 | Mconv7_stage2 = self.Mconv7_stage2(Mrelu1_7_stage2_6) 154 | concat_stage3 = torch.cat((Mconv7_stage2, relu5_4_stage1_3), 1) 155 | Mconv1_stage3_pad = F.pad(concat_stage3, (3, 3, 3, 3)) 156 | Mconv1_stage3 = self.Mconv1_stage3(Mconv1_stage3_pad) 157 | Mrelu1_2_stage3_1 = F.relu(Mconv1_stage3) 158 | Mconv2_stage3_pad = F.pad(Mrelu1_2_stage3_1, (3, 3, 3, 3)) 159 | Mconv2_stage3 = self.Mconv2_stage3(Mconv2_stage3_pad) 160 | Mrelu1_3_stage3_2 = F.relu(Mconv2_stage3) 161 | Mconv3_stage3_pad = F.pad(Mrelu1_3_stage3_2, (3, 3, 3, 3)) 162 | Mconv3_stage3 = self.Mconv3_stage3(Mconv3_stage3_pad) 163 | Mrelu1_4_stage3_3 = F.relu(Mconv3_stage3) 164 | Mconv4_stage3_pad = F.pad(Mrelu1_4_stage3_3, (3, 3, 3, 3)) 165 | Mconv4_stage3 = self.Mconv4_stage3(Mconv4_stage3_pad) 166 | Mrelu1_5_stage3_4 = F.relu(Mconv4_stage3) 167 | Mconv5_stage3_pad = F.pad(Mrelu1_5_stage3_4, (3, 3, 3, 3)) 168 | Mconv5_stage3 = self.Mconv5_stage3(Mconv5_stage3_pad) 169 | Mrelu1_6_stage3_5 = F.relu(Mconv5_stage3) 170 | Mconv6_stage3 = self.Mconv6_stage3(Mrelu1_6_stage3_5) 171 | Mrelu1_7_stage3_6 = F.relu(Mconv6_stage3) 172 | Mconv7_stage3 = self.Mconv7_stage3(Mrelu1_7_stage3_6) 173 | concat_stage4 = torch.cat((Mconv7_stage3, relu5_4_stage1_3), 1) 174 | Mconv1_stage4_pad = F.pad(concat_stage4, (3, 3, 3, 3)) 175 | Mconv1_stage4 = self.Mconv1_stage4(Mconv1_stage4_pad) 176 | Mrelu1_2_stage4_1 = F.relu(Mconv1_stage4) 177 | Mconv2_stage4_pad = F.pad(Mrelu1_2_stage4_1, (3, 3, 3, 3)) 178 | Mconv2_stage4 = self.Mconv2_stage4(Mconv2_stage4_pad) 179 | Mrelu1_3_stage4_2 = F.relu(Mconv2_stage4) 180 | Mconv3_stage4_pad = F.pad(Mrelu1_3_stage4_2, (3, 3, 3, 3)) 181 | Mconv3_stage4 = self.Mconv3_stage4(Mconv3_stage4_pad) 182 | Mrelu1_4_stage4_3 = F.relu(Mconv3_stage4) 183 | Mconv4_stage4_pad = F.pad(Mrelu1_4_stage4_3, (3, 3, 3, 3)) 184 | Mconv4_stage4 = self.Mconv4_stage4(Mconv4_stage4_pad) 185 | Mrelu1_5_stage4_4 = F.relu(Mconv4_stage4) 186 | Mconv5_stage4_pad = F.pad(Mrelu1_5_stage4_4, (3, 3, 3, 3)) 187 | Mconv5_stage4 = self.Mconv5_stage4(Mconv5_stage4_pad) 188 | Mrelu1_6_stage4_5 = F.relu(Mconv5_stage4) 189 | Mconv6_stage4 = self.Mconv6_stage4(Mrelu1_6_stage4_5) 190 | Mrelu1_7_stage4_6 = F.relu(Mconv6_stage4) 191 | Mconv7_stage4 = self.Mconv7_stage4(Mrelu1_7_stage4_6) 192 | concat_stage5 = torch.cat((Mconv7_stage4, relu5_4_stage1_3), 1) 193 | Mconv1_stage5_pad = F.pad(concat_stage5, (3, 3, 3, 3)) 194 | Mconv1_stage5 = self.Mconv1_stage5(Mconv1_stage5_pad) 195 | Mrelu1_2_stage5_1 = F.relu(Mconv1_stage5) 196 | Mconv2_stage5_pad = F.pad(Mrelu1_2_stage5_1, (3, 3, 3, 3)) 197 | Mconv2_stage5 = self.Mconv2_stage5(Mconv2_stage5_pad) 198 | Mrelu1_3_stage5_2 = F.relu(Mconv2_stage5) 199 | Mconv3_stage5_pad = F.pad(Mrelu1_3_stage5_2, (3, 3, 3, 3)) 200 | Mconv3_stage5 = self.Mconv3_stage5(Mconv3_stage5_pad) 201 | Mrelu1_4_stage5_3 = F.relu(Mconv3_stage5) 202 | Mconv4_stage5_pad = F.pad(Mrelu1_4_stage5_3, (3, 3, 3, 3)) 203 | Mconv4_stage5 = self.Mconv4_stage5(Mconv4_stage5_pad) 204 | Mrelu1_5_stage5_4 = F.relu(Mconv4_stage5) 205 | Mconv5_stage5_pad = F.pad(Mrelu1_5_stage5_4, (3, 3, 3, 3)) 206 | Mconv5_stage5 = self.Mconv5_stage5(Mconv5_stage5_pad) 207 | Mrelu1_6_stage5_5 = F.relu(Mconv5_stage5) 208 | Mconv6_stage5 = self.Mconv6_stage5(Mrelu1_6_stage5_5) 209 | Mrelu1_7_stage5_6 = F.relu(Mconv6_stage5) 210 | Mconv7_stage5 = self.Mconv7_stage5(Mrelu1_7_stage5_6) 211 | concat_stage6 = torch.cat((Mconv7_stage5, relu5_4_stage1_3), 1) 212 | Mconv1_stage6_pad = F.pad(concat_stage6, (3, 3, 3, 3)) 213 | Mconv1_stage6 = self.Mconv1_stage6(Mconv1_stage6_pad) 214 | Mrelu1_2_stage6_1 = F.relu(Mconv1_stage6) 215 | Mconv2_stage6_pad = F.pad(Mrelu1_2_stage6_1, (3, 3, 3, 3)) 216 | Mconv2_stage6 = self.Mconv2_stage6(Mconv2_stage6_pad) 217 | Mrelu1_3_stage6_2 = F.relu(Mconv2_stage6) 218 | Mconv3_stage6_pad = F.pad(Mrelu1_3_stage6_2, (3, 3, 3, 3)) 219 | Mconv3_stage6 = self.Mconv3_stage6(Mconv3_stage6_pad) 220 | Mrelu1_4_stage6_3 = F.relu(Mconv3_stage6) 221 | Mconv4_stage6_pad = F.pad(Mrelu1_4_stage6_3, (3, 3, 3, 3)) 222 | Mconv4_stage6 = self.Mconv4_stage6(Mconv4_stage6_pad) 223 | Mrelu1_5_stage6_4 = F.relu(Mconv4_stage6) 224 | Mconv5_stage6_pad = F.pad(Mrelu1_5_stage6_4, (3, 3, 3, 3)) 225 | Mconv5_stage6 = self.Mconv5_stage6(Mconv5_stage6_pad) 226 | Mrelu1_6_stage6_5 = F.relu(Mconv5_stage6) 227 | Mconv6_stage6 = self.Mconv6_stage6(Mrelu1_6_stage6_5) 228 | Mrelu1_7_stage6_6 = F.relu(Mconv6_stage6) 229 | Mconv7_stage6 = self.Mconv7_stage6(Mrelu1_7_stage6_6) 230 | return Mconv7_stage6 231 | 232 | 233 | @staticmethod 234 | def __conv(dim, name, **kwargs): 235 | if dim == 1: layer = nn.Conv1d(**kwargs) 236 | elif dim == 2: layer = nn.Conv2d(**kwargs) 237 | elif dim == 3: layer = nn.Conv3d(**kwargs) 238 | else: raise NotImplementedError() 239 | 240 | layer.state_dict()['weight'].copy_(torch.from_numpy(__weights_dict[name]['weights'])) 241 | if 'bias' in __weights_dict[name]: 242 | layer.state_dict()['bias'].copy_(torch.from_numpy(__weights_dict[name]['bias'])) 243 | return layer 244 | 245 | model = KitModel('pose_pytorch.npy') 246 | input_size = (2, 3, 368, 368) 247 | -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/mmdnn_caffe/graph.py: -------------------------------------------------------------------------------- 1 | from collections import namedtuple 2 | from functools import reduce 3 | from google.protobuf import text_format 4 | from copy import deepcopy 5 | import numbers 6 | import os 7 | import tempfile 8 | 9 | from mmdnn.conversion.caffe.mapper import get_handler_name 10 | from mmdnn.conversion.caffe.resolver import get_caffe_resolver, has_pycaffe 11 | from mmdnn.conversion.caffe.shape import * 12 | from mmdnn.conversion.caffe.errors import print_stderr, ConversionError 13 | 14 | 15 | layer_num_to_name = { 16 | 0: 'None', 17 | 1: 'Accuracy', 18 | 2: 'BNLL', 19 | 3: 'Concat', 20 | 4: 'Convolution', 21 | 5: 'Data', 22 | 6: 'Dropout', 23 | 7: 'EuclideanLoss', 24 | 8: 'Flatten', 25 | 9: 'HDF5Data', 26 | 10: 'HDF5Output', 27 | 11: 'Im2col', 28 | 12: 'ImageData', 29 | 13: 'InfogainLoss', 30 | 14: 'InnerProduct', 31 | 15: 'LRN', 32 | 16: 'MultinomialLogisticLoss', 33 | 17: 'Pooling', 34 | 18: 'ReLU', 35 | 19: 'Sigmoid', 36 | 20: 'Softmax', 37 | 21: 'SoftmaxWithLoss', 38 | 22: 'Split', 39 | 23: 'TanH', 40 | 24: 'WindowData', 41 | 25: 'Eltwise', 42 | 26: 'Power', 43 | 27: 'SigmoidCrossEntropyLoss', 44 | 28: 'HingeLoss', 45 | 29: 'MemoryData', 46 | 30: 'ArgMax', 47 | 31: 'Threshold', 48 | 32: 'DummyData', 49 | 33: 'Slice', 50 | 34: 'MVN', 51 | 35: 'AbsVal', 52 | 36: 'Silence', 53 | 37: 'ContrastiveLoss', 54 | 38: 'Exp', 55 | 39: 'Deconvolution', 56 | 40: 'PReLU' 57 | } 58 | 59 | LAYER_DESCRIPTORS = { 60 | # Caffe Types 61 | 'AbsVal': shape_identity, 62 | 'Accuracy': shape_scalar, 63 | 'ArgMax': shape_not_implemented, 64 | 'BatchNorm': shape_identity, 65 | 'BNLL': shape_not_implemented, 66 | 'Concat': shape_concat, 67 | 'ContrastiveLoss': shape_scalar, 68 | 'Convolution': shape_convolution, 69 | 'Crop': shape_not_implemented, 70 | 'Deconvolution': shape_deconvolution, 71 | 'Data': shape_data, 72 | 'Dropout': shape_identity, 73 | 'DummyData': shape_data, 74 | 'EuclideanLoss': shape_scalar, 75 | 'Eltwise': shape_identity, 76 | 'Exp': shape_identity, 77 | 'Flatten': shape_not_implemented, 78 | 'HDF5Data': shape_data, 79 | 'HDF5Output': shape_identity, 80 | 'HingeLoss': shape_scalar, 81 | 'Im2col': shape_not_implemented, 82 | 'ImageData': shape_data, 83 | 'InfogainLoss': shape_scalar, 84 | 'InnerProduct': shape_inner_product, 85 | 'Input': shape_data, 86 | 'LRN': shape_identity, 87 | 'MemoryData': shape_mem_data, 88 | 'MultinomialLogisticLoss': shape_scalar, 89 | 'MVN': shape_not_implemented, 90 | 'Pooling': shape_pool, 91 | 'Power': shape_identity, 92 | 'ReLU': shape_identity, 93 | 'Scale': shape_identity, 94 | 'Sigmoid': shape_identity, 95 | 'SigmoidCrossEntropyLoss': shape_scalar, 96 | 'Silence': shape_identity, 97 | 'Softmax': shape_identity, 98 | 'SoftmaxWithLoss': shape_scalar, 99 | 'Split': shape_not_implemented, 100 | 'Slice': shape_not_implemented, 101 | 'TanH': shape_identity, 102 | 'WindowData': shape_not_implemented, 103 | 'Threshold': shape_identity, 104 | 'Reshape' : shape_reshape, 105 | 'ResizeBilinear': shape_reshape, 106 | 'PReLU' : shape_identity 107 | } 108 | 109 | LAYER_TYPES = LAYER_DESCRIPTORS.keys() 110 | 111 | LayerType = type('LayerType', (), {t : t for t in LAYER_TYPES}) 112 | 113 | KernelParameters = namedtuple('KernelParameters', ['global_pooling', 'k_h', 'k_w', 's_h', 's_w', 'p_h', 'p_w']) 114 | 115 | class NodeKind(LayerType): 116 | 117 | @staticmethod 118 | def map_raw_kind(node_kind): 119 | if isinstance(node_kind, int): 120 | node_kind = layer_num_to_name[node_kind] 121 | else: 122 | node_kind = str(node_kind) 123 | if node_kind in LAYER_TYPES: 124 | return node_kind 125 | return None 126 | 127 | @staticmethod 128 | def compute_output_shape(node): 129 | try: 130 | return LAYER_DESCRIPTORS[node.kind](node) 131 | except NotImplementedError: 132 | raise ConversionError('Output shape computation not implemented for type: %s' % node.kind) 133 | 134 | LAYER_IN_TRAIN_PROTO = [NodeKind.ImageData, NodeKind.Data, NodeKind.HDF5Data, NodeKind.HDF5Output, NodeKind.WindowData, NodeKind.DummyData, NodeKind.MemoryData] 135 | 136 | class CaffeNode(object): 137 | def __init__(self, name, kind, layer=None): 138 | self.name = name 139 | self.kind = kind 140 | self.layer = layer 141 | self.parents = [] 142 | self.children = [] 143 | self.data = None 144 | self.output = [] 145 | self.output_shape = None 146 | self.metadata = {} 147 | 148 | def add_parent(self, parent_node, from_output): 149 | assert parent_node not in self.parents 150 | self.parents.append((parent_node, from_output)) 151 | if self not in parent_node.children: 152 | parent_node.children.append(self) 153 | 154 | def get_only_parent(self): 155 | if len(self.parents) != 1: 156 | raise ConversionError('Node (%s) expected to have 1 parent. Found %s.' % (self, len(self.parents))) 157 | return self.parents[0] 158 | 159 | @property 160 | def parameters(self): 161 | if self.layer is not None: 162 | params = get_handler_name(self.kind) 163 | if params == 'deconvolution': 164 | params = 'convolution' 165 | params = '_'.join((params, 'param')) 166 | try: 167 | return getattr(self.layer, params) 168 | except AttributeError: 169 | raise ConversionError('Caffe parameters not found for layer kind: %s' % (self.kind)) 170 | return None 171 | 172 | @staticmethod 173 | def get_kernel_value(scalar, repeated, idx, default=None): 174 | if scalar: 175 | return scalar 176 | if repeated: 177 | if isinstance(repeated, numbers.Number): 178 | return repeated 179 | if len(repeated) == 1: 180 | # Same value applies to all spatial dimensions 181 | return int(repeated[0]) 182 | assert idx < len(repeated) 183 | # Extract the value for the given spatial dimension 184 | return repeated[idx] 185 | if default is None: 186 | raise ValueError('Unable to determine kernel parameter!') 187 | return default 188 | 189 | @property 190 | def kernel_parameters(self): 191 | assert self.kind in (NodeKind.Convolution, NodeKind.Pooling, NodeKind.Deconvolution) 192 | params = self.parameters 193 | global_pooling = hasattr(params, 'global_pooling') and params.global_pooling 194 | if not global_pooling: 195 | k_h = self.get_kernel_value(params.kernel_h, params.kernel_size, 0) 196 | k_w = self.get_kernel_value(params.kernel_w, params.kernel_size, 1) 197 | s_h = self.get_kernel_value(params.stride_h, params.stride, 0, default=1) 198 | s_w = self.get_kernel_value(params.stride_w, params.stride, 1, default=1) 199 | else: 200 | k_h = k_w = 0 201 | s_h = s_w = 1 202 | p_h = self.get_kernel_value(params.pad_h, params.pad, 0, default=0) 203 | p_w = self.get_kernel_value(params.pad_w, params.pad, 1, default=0) 204 | return KernelParameters(global_pooling, k_h, k_w, s_h, s_w, p_h, p_w) 205 | 206 | def __str__(self): 207 | return '[%s] %s' % (self.kind, self.name) 208 | 209 | def __repr__(self): 210 | return '%s (0x%x)' %(self.name, id(self)) 211 | 212 | 213 | class CaffeGraph(object): 214 | 215 | def __init__(self, nodes=None, name=None): 216 | self.nodes = nodes or [] 217 | self.node_lut = {node.name: node for node in self.nodes} 218 | self.name = name 219 | self.prototxt = None 220 | 221 | def add_node(self, node): 222 | self.nodes.append(node) 223 | self.node_lut[node.name] = node 224 | 225 | def get_node(self, name): 226 | try: 227 | return self.node_lut[name] 228 | except KeyError: 229 | raise ConversionError('Layer not found: %s' % name) 230 | 231 | def get_input_nodes(self): 232 | return [node for node in self.nodes if len(node.parents) == 0] 233 | 234 | def get_output_nodes(self): 235 | return [node for node in self.nodes if len(node.children) == 0] 236 | 237 | def topologically_sorted(self): 238 | visited = set() 239 | sorted_nodes = [] 240 | def topo_sort_dfs(node, visited, sorted_nodes): 241 | if node in visited: 242 | return 243 | visited.add(node) 244 | for n, idx in node.parents: 245 | topo_sort_dfs(n, visited, sorted_nodes) 246 | sorted_nodes.append(node) 247 | for node in self.nodes: 248 | topo_sort_dfs(node, visited, sorted_nodes) 249 | return sorted_nodes 250 | 251 | def compute_output_shapes(self, model): 252 | sorted_nodes = self.topologically_sorted() 253 | (tmp_handle, tmp_prototxt) = tempfile.mkstemp(suffix=".prototxt") 254 | with open(tmp_prototxt, 'w') as f: 255 | f.write(text_format.MessageToString(model)) 256 | self.prototxt = tmp_prototxt 257 | if has_pycaffe(): 258 | caffe = get_caffe_resolver().caffe 259 | net = caffe.Net(tmp_prototxt, caffe.TEST) 260 | for key, value in net.blobs.items(): 261 | try: 262 | node = self.get_node(key) 263 | dims = list(value.shape) 264 | dims = dims + [1] * (4 - len(dims)) 265 | node.output_shape = TensorShape(*dims) 266 | except: 267 | continue 268 | for node in sorted_nodes: 269 | if node.output_shape is None: 270 | node.output_shape = TensorShape(*NodeKind.compute_output_shape(node)) 271 | os.close(tmp_handle) 272 | else: 273 | for node in sorted_nodes: 274 | node.output_shape = TensorShape(*NodeKind.compute_output_shape(node)) 275 | 276 | # consider rewrite this function to Network.py 277 | def replaced(self, new_nodes): 278 | return CaffeGraph(nodes=new_nodes, name=self.name) 279 | 280 | def transformed(self, transformers): 281 | graph = self 282 | for transformer in transformers: 283 | graph = transformer(graph) 284 | if graph is None: 285 | raise ConversionError('Transformer failed: {}'.format(transformer)) 286 | assert isinstance(graph, CaffeGraph) 287 | return graph 288 | 289 | def __contains__(self, key): 290 | return key in self.node_lut 291 | 292 | def __str__(self): 293 | def get_max_shape(data): 294 | if isinstance(data, dict): 295 | max = 0 296 | val = None 297 | for k, v in data.items(): 298 | tmp = reduce(lambda x, y: x*y, v.shape) 299 | if tmp > max: 300 | val = v.shape 301 | max = tmp 302 | return val 303 | else: 304 | return data[0].shape 305 | hdr = '{:<20} {:<30} {:>20} {:>20}'.format('Type', 'Name', 'Param', 'Output') 306 | s = [hdr, '-' * 94] 307 | for node in self.topologically_sorted(): 308 | data_shape = get_max_shape(node.data) if node.data else '--' 309 | out_shape = node.output_shape or '--' 310 | s.append('{:<20} {:<30} {!s:>20} {!s:>20}'.format(node.kind, node.name, data_shape, tuple(out_shape))) 311 | return '\n'.join(s) 312 | 313 | 314 | class GraphBuilder(object): 315 | def __init__(self, model_path, input_shape=None, is_train_proto=False, phase='test'): 316 | self.model_path = model_path 317 | self.phase = phase 318 | self.is_train_proto = is_train_proto 319 | self.input_shape = input_shape 320 | self.load() 321 | 322 | def load(self): 323 | self.model = get_caffe_resolver().NetParameter() 324 | with open(self.model_path, 'r') as f: 325 | text_format.Merge(f.read(), self.model) 326 | if self.is_train_proto: 327 | self.process_train_proto() 328 | 329 | def process_train_proto(self): 330 | layers = self.model.layer or self.model.layers 331 | delete_layer = set() 332 | split_op_map = dict() 333 | loss_layers = [layer for layer in layers if NodeKind.map_raw_kind(layer.type) in (NodeKind.SoftmaxWithLoss, NodeKind.SigmoidCrossEntropyLoss)] 334 | a = [layers.remove(layer) for layer in layers[:] if layer in loss_layers[:-1] or NodeKind.map_raw_kind(layer.type) in LAYER_IN_TRAIN_PROTO] 335 | for layer in layers[:]: 336 | if 'label' in layer.bottom: 337 | if NodeKind.map_raw_kind(layer.type) in (NodeKind.SoftmaxWithLoss, NodeKind.SigmoidCrossEntropyLoss): 338 | continue 339 | elif NodeKind.map_raw_kind(layer.type) == NodeKind.Split: 340 | for item in layer.top: 341 | delete_layer.add(item) 342 | layers.remove(layer) 343 | elif NodeKind.map_raw_kind(layer.type) == NodeKind.Split: 344 | for item in layer.top: 345 | split_op_map[item] = layer.bottom[0] 346 | layers.remove(layer) 347 | 348 | for layer in layers[:]: 349 | for item in delete_layer: 350 | if item in layer.bottom: 351 | layers.remove(layer) 352 | break 353 | for key, value in split_op_map.items(): 354 | if key in layer.bottom: 355 | layer.bottom.remove(key) 356 | layer.bottom.append(value) 357 | self.model.input.append('data') 358 | self.model.input_dim.extend(self.input_shape) 359 | last_layer = layers[-1] 360 | kind = NodeKind.map_raw_kind(last_layer.type) 361 | if kind in (NodeKind.SoftmaxWithLoss, NodeKind.SigmoidCrossEntropyLoss): 362 | pred = layers.add() 363 | pred.name = 'prob' 364 | pred.top.append('prob') 365 | pred.bottom.append(last_layer.bottom[0]) 366 | if kind == NodeKind.SoftmaxWithLoss: 367 | pred.type = NodeKind.Softmax if self.model.layer else 20 # competiable with old version caffe proto 368 | elif kind == NodeKind.SigmoidCrossEntropyLoss: 369 | pred.type = NodeKind.Sigmoid if self.model.layer else 19 370 | layers.remove(last_layer) 371 | 372 | def filter_layers(self, layers): 373 | phase_map = {0: 'train', 1: 'test'} 374 | filtered_layer_names = set() 375 | filtered_layers = [] 376 | for layer in layers: 377 | phase = self.phase 378 | if len(layer.include): 379 | phase = phase_map[layer.include[0].phase] 380 | if len(layer.exclude): 381 | phase = phase_map[1 - layer.include[0].phase] 382 | exclude = (phase != self.phase) 383 | # Dropout layers appear in a fair number of Caffe 384 | # test-time networks. These are just ignored. We'll 385 | # filter them out here. 386 | if (not exclude) and (phase == 'test'): 387 | exclude = (layer.type == LayerType.Dropout) 388 | if (not exclude): 389 | exclude = (layer.type == LayerType.Silence) 390 | if not exclude: 391 | if layer.name in filtered_layer_names: 392 | for i in range(1, len(filtered_layer_names)): 393 | new_name = layer.name + '_%s' % i 394 | if new_name not in filtered_layer_names: 395 | layer.name = new_name 396 | break 397 | filtered_layer_names.add(layer.name) 398 | filtered_layers.append(layer) 399 | return filtered_layers 400 | 401 | def make_node(self, layer): 402 | kind = NodeKind.map_raw_kind(layer.type) 403 | if kind is None: 404 | # TODO: raise error 405 | pass 406 | node = CaffeNode(layer.name, kind, layer=layer) 407 | node.output.append(layer.name.replace('/', '_')) 408 | node.output.extend(layer.top[1:]) 409 | return node 410 | 411 | def make_input_node(self): 412 | nodes = [CaffeNode(name, NodeKind.Data) for name in self.model.input] 413 | if len(nodes): 414 | input_dim = list(map(int, self.model.input_dim)) 415 | if not input_dim: 416 | if len(self.model.input_shape) > 0: 417 | input_dim = list(map(int, self.model.input_shape[0].dim)) 418 | else: 419 | # TODO: raise error 420 | pass 421 | for node in nodes: 422 | node.output_shape = tuple(input_dim) 423 | node.output.append('data') 424 | return nodes 425 | 426 | def build(self): 427 | layers = self.model.layers or self.model.layer 428 | layers = self.filter_layers(layers) 429 | nodes = self.make_input_node() 430 | nodes += [self.make_node(layer) for layer in layers] 431 | graph = CaffeGraph(nodes=nodes, name=self.model.name) 432 | node_outputs = {} 433 | for idx, layer in enumerate(layers): 434 | node = graph.get_node(layer.name) 435 | for input_name in layer.bottom: 436 | assert input_name != layer.name 437 | parent_node = node_outputs.get(input_name) 438 | if (parent_node is None) or (parent_node==node): 439 | parent_node = graph.get_node(input_name) 440 | if parent_node.layer: 441 | for i, output in enumerate(parent_node.layer.top): 442 | if input_name == output: 443 | node.add_parent(parent_node, i) 444 | else: 445 | node.add_parent(parent_node, 0) 446 | for output_name in layer.top: 447 | if output_name == layer.name: 448 | continue 449 | node_outputs[output_name] = node 450 | graph.compute_output_shapes(self.model) 451 | return graph 452 | -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/mmdnn_caffe/caffe_emitter.py: -------------------------------------------------------------------------------- 1 | #---------------------------------------------------------------------------------------------- 2 | # Copyright (c) Microsoft Corporation. All rights reserved. 3 | # Licensed under the MIT License. See License.txt in the project root for license information. 4 | #---------------------------------------------------------------------------------------------- 5 | 6 | from __future__ import division 7 | 8 | import os 9 | import sys 10 | import numpy as np 11 | 12 | import caffe 13 | from caffe import layers as L 14 | from caffe import params as P 15 | from mmdnn.conversion.common.IR.IR_graph import IRGraph, IRGraphNode 16 | import mmdnn.conversion.common.IR.graph_pb2 as graph_pb2 17 | from mmdnn.conversion.common.IR.graph_pb2 import NodeDef, GraphDef, DataType 18 | from mmdnn.conversion.common.DataStructure.emitter import Emitter 19 | from mmdnn.conversion.common.utils import * 20 | 21 | 22 | class CaffeEmitter(Emitter): 23 | 24 | def __init__(self, model): 25 | from six import string_types as _string_types 26 | super(CaffeEmitter, self).__init__() 27 | if isinstance(model, _string_types): 28 | network_path = model 29 | else: 30 | network_path = model[0] 31 | self._load_weights(model[1]) 32 | 33 | self.IR_graph = IRGraph(network_path) 34 | super(CaffeEmitter, self)._build() 35 | 36 | 37 | @property 38 | def header_code(self): 39 | return """from __future__ import print_function 40 | import numpy as np 41 | import sys, argparse 42 | import caffe 43 | from caffe import layers as L 44 | from caffe import params as P 45 | from caffe import to_proto 46 | from six import text_type as _text_type 47 | 48 | 49 | __weights_dict = dict() 50 | 51 | def load_weights(weight_file): 52 | if weight_file == None: 53 | return 54 | 55 | try: 56 | weights_dict = np.load(weight_file).item() 57 | except: 58 | weights_dict = np.load(weight_file, encoding='bytes').item() 59 | 60 | return weights_dict 61 | 62 | 63 | def KitModel(weight_file = None): 64 | n = caffe.NetSpec() 65 | """ 66 | 67 | @property 68 | def end_code(self): 69 | return """ return n 70 | 71 | def make_net(prototxt): 72 | n = KitModel() 73 | with open(prototxt, 'w') as fpb: 74 | print(n.to_proto(), file=fpb) 75 | 76 | def gen_weight(weight_file, model, prototxt): 77 | global __weights_dict 78 | __weights_dict = load_weights(weight_file) 79 | 80 | net = caffe.Net(prototxt, caffe.TRAIN) 81 | 82 | for key in __weights_dict: 83 | if 'weights' in __weights_dict[key]: 84 | net.params[key][0].data.flat = __weights_dict[key]['weights'] 85 | elif 'mean' in __weights_dict[key]: 86 | net.params[key][0].data.flat = __weights_dict[key]['mean'] 87 | net.params[key][1].data.flat = __weights_dict[key]['var'] 88 | if 'scale' in __weights_dict[key]: 89 | net.params[key][2].data.flat = __weights_dict[key]['scale'] 90 | elif 'scale' in __weights_dict[key]: 91 | net.params[key][0].data.flat = __weights_dict[key]['scale'] 92 | if 'bias' in __weights_dict[key]: 93 | net.params[key][1].data.flat = __weights_dict[key]['bias'] 94 | if 'gamma' in __weights_dict[key]: # used for prelu, not sure if other layers use this too 95 | net.params[key][0].data.flat = __weights_dict[key]['gamma'] 96 | net.save(model) 97 | return net 98 | 99 | 100 | 101 | if __name__=='__main__': 102 | parser = argparse.ArgumentParser(description='Generate caffe model and prototxt') 103 | parser.add_argument('--weight_file', '-w', type=_text_type, default='IR weight file') 104 | parser.add_argument('--prototxt', '-p', type=_text_type, default='caffe_converted.prototxt') 105 | parser.add_argument('--model', '-m', type=_text_type, default='caffe_converted.caffemodel') 106 | args = parser.parse_args() 107 | # For some reason argparser gives us unicode, so we need to conver to str first 108 | make_net(str(args.prototxt)) 109 | gen_weight(str(args.weight_file), str(args.model), str(args.prototxt)) 110 | 111 | """ 112 | 113 | def gen_code(self, phase = 'test'): 114 | self.phase = phase 115 | self.add_body(0, self.header_code) 116 | 117 | #for test 118 | # with open("graph.txt", 'w') as f: 119 | # for layer in self.IR_graph.topological_sort: 120 | # current_node = self.IR_graph.get_node(layer) 121 | # print("========current_node=========\n{}".format(current_node.layer), file=f) 122 | #test end 123 | 124 | for layer in self.IR_graph.topological_sort: 125 | current_node = self.IR_graph.get_node(layer) 126 | node_type = current_node.type 127 | #print("========current_node={}".format(current_node.layer)) 128 | 129 | if hasattr(self, "emit_" + node_type): 130 | func = getattr(self, "emit_" + node_type) 131 | func(current_node) 132 | else: 133 | print("CaffeEmitter has not supported operator [%s]." % (node_type)) 134 | self.emit_UNKNOWN(current_node) 135 | 136 | self.add_body(0, "") 137 | self.add_body(0,self.end_code) 138 | 139 | return self.body_code 140 | 141 | 142 | def run(self, dstNetworkPath, dstWeightPath = None, phase = 'test'): 143 | super(CaffeEmitter, self).run(dstNetworkPath, dstWeightPath, phase) 144 | if self.weight_loaded: 145 | self.save_weights(self.weights_dict, dstWeightPath) 146 | 147 | 148 | 149 | @staticmethod 150 | def _shapeToStr(shapes): 151 | return [dim.size if dim.size > 0 else 1 for dim in shapes.dim] 152 | 153 | 154 | 155 | def check_if_need_transpose(self, IR_node): 156 | parent = self.IR_graph.get_parent(IR_node.name, [0]) 157 | while parent.type == 'Flatten' or parent.type == 'Dropout' or parent.type == 'Reshape': 158 | parent = self.IR_graph.get_parent(parent.name, [0]) 159 | dim = len(parent.layer.attr['_output_shapes'].list.shape[0].dim) 160 | if dim > 2: 161 | original_dims = self.weights_dict[IR_node.name]['weights'].shape 162 | dims = [i.size for i in parent.layer.attr['_output_shapes'].list.shape[0].dim[1:]] + [-1] 163 | self.weights_dict[IR_node.name]['weights'] = np.reshape(self.weights_dict[IR_node.name]['weights'], dims) 164 | self.weights_dict[IR_node.name]['weights'] = np.transpose(self.weights_dict[IR_node.name]['weights'], [dim - 2] + list(range(0, dim - 2)) + [dim - 1]) 165 | self.weights_dict[IR_node.name]['weights'] = np.reshape(self.weights_dict[IR_node.name]['weights'], original_dims) 166 | 167 | 168 | def emit_Conv(self, IR_node): 169 | # check if have pad layer 170 | pad_h = 0 171 | pad_w = 0 172 | IR_parent_node = self.IR_graph.get_parent(IR_node.name, [0]) 173 | if IR_parent_node.type == 'Pad': 174 | pad_h = IR_parent_node.get_attr('pads')[1] 175 | pad_w = IR_parent_node.get_attr('pads')[2] 176 | else: 177 | pad_h = IR_node.get_attr('pads')[1] 178 | pad_w = IR_node.get_attr('pads')[2] 179 | 180 | num_output = IR_node.get_attr('kernel_shape')[-1] 181 | if IR_node.type == "DepthwiseConv": 182 | num_group = IR_node.get_attr("kernel_shape")[-2] 183 | num_output = num_group * num_output 184 | else: 185 | num_group = IR_node.get_attr("group", 1) 186 | 187 | self.add_body(1, "n.{:<15} = L.Convolution(n.{}, kernel_h={}, kernel_w={}, stride={}, num_output={}, pad_h={}, pad_w={}, group={}, \ 188 | bias_term={}, ntop=1)".format( 189 | IR_node.variable_name, 190 | self.parent_variable_name(IR_node), 191 | IR_node.get_attr('kernel_shape')[0], 192 | IR_node.get_attr('kernel_shape')[1], 193 | IR_node.get_attr('strides')[1], 194 | num_output, 195 | pad_h, 196 | pad_w, 197 | num_group, 198 | IR_node.get_attr('use_bias', False))) 199 | 200 | self.check_if_need_crop(IR_node) 201 | 202 | dim = len(IR_node.get_attr('strides')) - 2 203 | if self.weight_loaded: 204 | if IR_node.type == "DepthwiseConv": 205 | self.weights_dict[IR_node.name]['weights'] = np.swapaxes(self.weights_dict[IR_node.name]['weights'], -1, -2) 206 | self.weights_dict[IR_node.name]['weights'] = np.transpose(self.weights_dict[IR_node.name]['weights'], [dim + 1, dim] + list(range(0, dim))) 207 | self.weights_dict[IR_node.variable_name] = self.weights_dict.pop(IR_node.name) 208 | 209 | # keys = [] 210 | # for key in self.weights_dict[IR_node.name].keys(): 211 | # keys.append(key) 212 | # print("=======Layer: {}, keys: {}".format(IR_node.name, keys)) 213 | 214 | def compute_output_shape(self, IR_node, kernel_h, kernel_w): 215 | parent_node = self.IR_graph.get_parent(IR_node.name, [0]) 216 | 217 | if parent_node.get_attr('_output_shapes'): 218 | shape = parent_node.get_attr('_output_shapes')[0] 219 | shape = shape_to_list(shape) 220 | h_i = shape[1] 221 | w_i = shape[2] 222 | pad_h = IR_node.get_attr('pads')[1] 223 | pad_w = IR_node.get_attr('pads')[2] 224 | stride_h = IR_node.get_attr('strides')[1] 225 | stride_w = IR_node.get_attr('strides')[2] 226 | 227 | if IR_node.type == 'Pool': 228 | h_o = (h_i + 2 * pad_h - kernel_h + stride_h - 1) // stride_h + 1 229 | w_o = (w_i + 2 * pad_w - kernel_w + stride_w - 1) // stride_w + 1 230 | else: 231 | h_o = (h_i + 2 * pad_h - kernel_h) // stride_h + 1 232 | w_o = (w_i + 2 * pad_w - kernel_w) // stride_w + 1 233 | return h_o, w_o 234 | else: 235 | assert False 236 | 237 | 238 | def check_if_need_crop(self, IR_node): 239 | shape = IR_node.get_attr('_output_shapes')[0] 240 | shape = shape_to_list(shape) 241 | ir_ho = shape[1] 242 | ir_wo = shape[2] 243 | if ir_ho <0 or ir_wo<0: 244 | return 245 | if IR_node.type == 'Pool': 246 | k_h = IR_node.get_attr('kernel_shape')[1] 247 | k_w = IR_node.get_attr('kernel_shape')[2] 248 | else: 249 | k_h = IR_node.get_attr('kernel_shape')[0] 250 | k_w = IR_node.get_attr('kernel_shape')[1] 251 | 252 | caffe_ho, caffe_wo = self.compute_output_shape(IR_node, k_h, k_w) 253 | if caffe_ho > ir_ho or caffe_wo > ir_wo: 254 | crop_layer_variable_name = IR_node.variable_name + "_crop" 255 | self.add_body(1, "n.{:<15} = L.Crop(n.{}, L.DummyData(shape=[dict(dim=[1, {}, {}, {}])], ntop=1), ntop=1)".format( 256 | crop_layer_variable_name, 257 | IR_node.variable_name, 258 | shape[3], 259 | ir_ho, 260 | ir_wo 261 | )) 262 | # Change the layer name 263 | IR_node.real_name = IR_node.real_name + "_crop" 264 | 265 | 266 | def emit_Pool(self, IR_node): 267 | pooling_type = IR_node.get_attr('pooling_type') 268 | if pooling_type == 'MAX': 269 | pooling_type = P.Pooling.MAX 270 | elif pooling_type == 'AVG': 271 | pooling_type = P.Pooling.AVE 272 | elif pooling_type == 'STOCHASTIC': 273 | pooling_type = P.Pooling.STOCHASTIC 274 | else: 275 | raise ValueError() 276 | 277 | if IR_node.layer.attr['global_pooling'].b: 278 | self.add_body(1, "n.{:<15} = L.Pooling(n.{}, pool={}, stride={}, global_pooling=True, ntop=1)".format( 279 | IR_node.variable_name, 280 | self.parent_variable_name(IR_node), 281 | pooling_type, 282 | IR_node.get_attr('strides')[1])) 283 | else: 284 | self.add_body(1, "n.{:<15} = L.Pooling(n.{}, pool={}, kernel_size={}, pad_h={}, pad_w={}, stride={}, ntop=1)".format( 285 | IR_node.variable_name, 286 | self.parent_variable_name(IR_node), 287 | pooling_type, 288 | IR_node.get_attr('kernel_shape')[1], 289 | IR_node.get_attr('pads')[1], 290 | IR_node.get_attr('pads')[2], 291 | IR_node.get_attr('strides')[1])) 292 | 293 | # check if need crop output shape 294 | self.check_if_need_crop(IR_node) 295 | 296 | def emit_ResizeBilinear(self, IR_node): 297 | shape = IR_node.get_attr("_output_shapes")[0] 298 | shape = shape_to_list(shape) 299 | self.add_body(1, "n.{:<15} = L.ResizeBilinear(n.{}, height={}, width={}, ntop=1)".format( 300 | IR_node.variable_name, 301 | self.parent_variable_name(IR_node), 302 | shape[1], 303 | shape[2])) 304 | 305 | def emit_UNKNOWN(self, IR_node): 306 | print(IR_node.IR_layer.name) 307 | 308 | 309 | def emit_DataInput(self, IR_node): 310 | shape = self._shapeToStr(IR_node.get_attr('shape')) 311 | shape = [shape[0], shape[-1]] + shape[1:-1] 312 | self.add_body(1, "n.{:<15} = L.Input(shape=[dict(dim={})], ntop=1)".format( 313 | IR_node.variable_name, 314 | shape)) 315 | 316 | 317 | def emit_Dropout(self, IR_node): 318 | in_place = True 319 | self.add_body(1, "n.{:<15} = L.Dropout(n.{}, dropout_ratio={} , in_place={}, ntop=1)".format( 320 | IR_node.variable_name, 321 | self.parent_variable_name(IR_node), 322 | 1 - IR_node.get_attr('keep_prob'), 323 | in_place)) 324 | 325 | 326 | def emit_FullyConnected(self, IR_node): 327 | self.add_body(1, "n.{:<15} = L.InnerProduct(n.{}, num_output={}, bias_term={}, ntop=1)".format( 328 | IR_node.variable_name, 329 | self.parent_variable_name(IR_node), 330 | IR_node.layer.attr["units"].i, 331 | IR_node.get_attr('use_bias', False))) 332 | if self.weight_loaded: 333 | self.check_if_need_transpose(IR_node) 334 | self.weights_dict[IR_node.name]['weights'] = np.transpose(self.weights_dict[IR_node.name]['weights'], (1, 0)) 335 | self.weights_dict[IR_node.variable_name] = self.weights_dict.pop(IR_node.name) 336 | 337 | 338 | def emit_BatchNorm(self, IR_node): 339 | 340 | self.add_body(1, "n.{:<15} = L.BatchNorm(n.{}, eps={}, use_global_stats={}, ntop=1)".format( 341 | IR_node.variable_name, 342 | self.parent_variable_name(IR_node), 343 | IR_node.get_attr('epsilon'), 344 | self.phase == 'test' 345 | )) 346 | 347 | scale_layer_var_name = IR_node.variable_name + "_scale" 348 | self.add_body(1, "n.{:<15} = L.Scale(n.{}, bias_term={}, in_place=True, ntop=1)".format( 349 | scale_layer_var_name, 350 | IR_node.variable_name, 351 | IR_node.get_attr('bias', False) 352 | )) 353 | 354 | if self.weight_loaded: 355 | self.weights_dict[scale_layer_var_name] = dict() 356 | if 'scale' in self.weights_dict[IR_node.name]: 357 | self.weights_dict[scale_layer_var_name]['scale'] = self.weights_dict[IR_node.name]['scale'] 358 | else: 359 | self.weights_dict[scale_layer_var_name]['scale'] = 1 360 | 361 | self.weights_dict[IR_node.name]['scale'] = 1 362 | 363 | if 'bias' in self.weights_dict[IR_node.name]: 364 | self.weights_dict[scale_layer_var_name]['bias'] = self.weights_dict[IR_node.name]['bias'] 365 | self.weights_dict[IR_node.name].pop('bias', None) 366 | # change the key "name" to "variable_name", in case of the layer name has invalid characters 367 | 368 | self.weights_dict[IR_node.variable_name] = self.weights_dict.pop(IR_node.name) 369 | 370 | IR_node.real_name = IR_node.name + "_scale" 371 | 372 | def emit_Scale(self, IR_node): 373 | 374 | self.add_body(1, "n.{:<15} = L.Scale(n.{}, bias_term={}, in_place=True, ntop=1)".format( 375 | IR_node.variable_name, 376 | self.parent_variable_name(IR_node), 377 | IR_node.get_attr('use_bias', False) 378 | )) 379 | 380 | if self.weight_loaded: 381 | self.weights_dict[IR_node.variable_name] = self.weights_dict.pop(IR_node.name) 382 | 383 | 384 | def emit_LRN(self, IR_node): 385 | self.add_body(1, "n.{:<15} = L.LRN(n.{}, local_size={}, alpha={}, beta={}, k={})".format( 386 | IR_node.variable_name, 387 | self.parent_variable_name(IR_node), 388 | IR_node.get_attr('size') * 2 - 1, 389 | IR_node.get_attr('alpha'), 390 | IR_node.get_attr('beta'), 391 | IR_node.get_attr('k') 392 | )) 393 | 394 | 395 | def emit_Add(self, IR_node): 396 | input_layers = ', '.join(('n.' + self.IR_graph.get_parent(IR_node.name, [num]).real_variable_name) for num in range(0, len(IR_node.in_edges))) 397 | self.add_body(1, "n.{:<15} = L.Eltwise({}, operation=1, ntop=1)".format( 398 | IR_node.variable_name, 399 | input_layers, 400 | )) 401 | 402 | def emit_Flatten(self, IR_node): 403 | IR_node.real_name = self.IR_graph.get_parent(IR_node.name, [0]).real_name 404 | 405 | 406 | def emit_Squeeze(self, IR_node): 407 | shape = IR_node.get_attr("_output_shapes")[0] 408 | shape = shape_to_list(shape) 409 | if shape: 410 | dim_str = "'dim': {}".format(shape) 411 | dim_str = " reshape_param={'shape': { " + dim_str + '} }' 412 | self.add_body(1, "n.{:<15} = L.Reshape(n.{}, {})".format( 413 | IR_node.variable_name, 414 | self.parent_variable_name(IR_node), 415 | dim_str 416 | )) 417 | else: 418 | IR_node.real_name = self.IR_graph.get_parent(IR_node.name, [0]).real_name 419 | 420 | 421 | def emit_Concat(self, IR_node): 422 | axis_array = (2, 3, 1, 0) 423 | axis = axis_array.index(IR_node.get_attr('axis')) 424 | input_layers = ', '.join(('n.' + self.IR_graph.get_node(edge).real_variable_name) for edge in IR_node.in_edges) 425 | self.add_body(1, "n.{:<15} = L.Concat({}, axis={})".format( 426 | IR_node.variable_name, 427 | input_layers, 428 | axis 429 | )) 430 | 431 | # def emit_Tanh(self, IR_node): 432 | # self._emit_activation(IR_node, 'ops.tanh') 433 | 434 | 435 | def emit_Relu(self, IR_node): 436 | in_place = True 437 | self.add_body(1, "n.{:<15} = L.ReLU(n.{}, in_place={}, ntop=1)".format( 438 | IR_node.variable_name, 439 | self.parent_variable_name(IR_node), 440 | in_place)) 441 | 442 | def emit_LeakyRelu(self, IR_node): 443 | in_place = True 444 | self.add_body(1, "n.{:<15} = L.ReLU(n.{}, in_place={}, negative_slope={}, ntop=1)".format( 445 | IR_node.variable_name, 446 | self.parent_variable_name(IR_node), 447 | in_place, 448 | IR_node.IR_layer.attr['alpha'].f)) 449 | 450 | 451 | 452 | def emit_PRelu(self, IR_node): 453 | in_place = True 454 | self.add_body(1, "n.{:<15} = L.PReLU(n.{}, in_place={}, ntop=1)".format( 455 | IR_node.variable_name, 456 | self.parent_variable_name(IR_node), 457 | in_place)) 458 | 459 | def emit_Tanh(self, IR_node): 460 | self.add_body(1, "n.{:<15} = L.TanH(n.{}, ntop=1)".format( 461 | IR_node.variable_name, 462 | self.parent_variable_name(IR_node))) 463 | 464 | def emit_Softmax(self, IR_node): 465 | self.add_body(1, "n.{:<15} = L.Softmax(n.{}, ntop=1)".format( 466 | IR_node.variable_name, 467 | self.parent_variable_name(IR_node))) 468 | 469 | 470 | def emit_Pad(self, IR_node): 471 | IR_node.real_name = self.IR_graph.get_parent(IR_node.name, [0]).real_name 472 | 473 | def reduction(self, IR_node, op, axes): 474 | # Convert NHWC (IR) to NCHW (Caffe): [0,1,2,3]->[0,3,1,2] 475 | if len(axes) == 1: 476 | assert (axes[0] == 2) 477 | elif len(axes) == 2: 478 | assert ((axes[0] == 1) and (axes[1] == 2)) 479 | 480 | self.add_body(1, "n.{:<15} = L.Reduction(n.{}, operation={} , axis={} ,ntop=1)".format( 481 | IR_node.variable_name, 482 | self.parent_variable_name(IR_node), 483 | op, 484 | len(axes))) 485 | 486 | if IR_node.get_attr('keepdims') == True: 487 | shape = IR_node.get_attr("_output_shapes")[0] 488 | shape = shape_to_list(shape) 489 | shape = [1] + [shape[-1]] + shape[1:-1] 490 | dim_str = "'dim': {}".format(shape) 491 | dim_str = "{'shape': { " + dim_str + '} }' 492 | self.add_body(1, "n.{:<15} = L.Reshape(n.{}, reshape_param={}) ".format( 493 | IR_node.variable_name + "_reshape", 494 | IR_node.real_variable_name, 495 | dim_str)) 496 | IR_node.real_name = IR_node.real_name + '_reshape' 497 | 498 | 499 | def emit_ReduceMean(self, IR_node): 500 | self.reduction(IR_node, 4, IR_node.get_attr('axes')) 501 | 502 | def emit_ReduceSum(self, IR_node): 503 | self.reduction(IR_node, 1, IR_node.get_attr('axes')) 504 | 505 | def emit_Relu6(self, IR_node): 506 | self.emit_Relu(IR_node) 507 | 508 | def emit_DepthwiseConv(self, IR_node): 509 | self.emit_Conv(IR_node) 510 | 511 | def emit_Const(self, IR_node): 512 | pass 513 | def emit_Shape(self, IR_node): 514 | pass 515 | def emit_Reshape(self, IR_node): 516 | # currently for the flatten layer 517 | self.add_body(1, "n.{:<15} = L.Flatten(n.{})".format( 518 | IR_node.variable_name, 519 | self.parent_variable_name(IR_node), 520 | )) 521 | 522 | def emit_Slice(self, IR_node): 523 | pass 524 | 525 | def emit_Pack(self, IR_node): 526 | pass 527 | 528 | def emit_Abs(self, IR_node): 529 | self.add_body(1, "n.{:<15} = L.AbsVal(n.{}, ntop=1)".format( 530 | IR_node.variable_name, 531 | self.parent_variable_name(IR_node))) 532 | 533 | def emit_Sub(self, IR_node): 534 | input_layers = ', '.join(('n.' + self.IR_graph.get_node(edge).real_variable_name) for edge in IR_node.in_edges) 535 | self.add_body(1, "n.{:<15} = L.Eltwise({}, coeff = [1, -1], ntop=1)".format( 536 | IR_node.variable_name, 537 | input_layers)) 538 | 539 | def emit_Mul(self, IR_node): 540 | self.emit_Scale(IR_node) 541 | 542 | 543 | # def emit_Square(self, IR_node): 544 | # input_layers = ', '.join(('n.' + self.IR_graph.get_node(edge).real_variable_name) for edge in IR_node.in_edges) 545 | # self.add_body(1, "n.{:<15} = L.Square({}, ntop=1)".format( 546 | # IR_node.variable_name, 547 | # input_layers)) 548 | 549 | 550 | -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/pose/mpi/pose_faster_linevec_pytorch.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | __weights_dict = dict() 7 | 8 | def load_weights(weight_file): 9 | if weight_file == None: 10 | return 11 | 12 | try: 13 | weights_dict = np.load(weight_file).item() 14 | except: 15 | weights_dict = np.load(weight_file, encoding='bytes').item() 16 | 17 | return weights_dict 18 | 19 | class KitModel(nn.Module): 20 | 21 | 22 | def __init__(self, weight_file): 23 | super(KitModel, self).__init__() 24 | global __weights_dict 25 | __weights_dict = load_weights(weight_file) 26 | 27 | self.conv1_1 = self.__conv(2, name='conv1_1', in_channels=3, out_channels=64, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 28 | self.conv1_2 = self.__conv(2, name='conv1_2', in_channels=64, out_channels=64, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 29 | self.conv2_1 = self.__conv(2, name='conv2_1', in_channels=64, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 30 | self.conv2_2 = self.__conv(2, name='conv2_2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 31 | self.conv3_1 = self.__conv(2, name='conv3_1', in_channels=128, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 32 | self.conv3_2 = self.__conv(2, name='conv3_2', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 33 | self.conv3_3 = self.__conv(2, name='conv3_3', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 34 | self.conv3_4 = self.__conv(2, name='conv3_4', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 35 | self.conv4_1 = self.__conv(2, name='conv4_1', in_channels=256, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 36 | self.conv4_2 = self.__conv(2, name='conv4_2', in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 37 | self.conv4_3_CPM = self.__conv(2, name='conv4_3_CPM', in_channels=512, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 38 | self.conv4_4_CPM = self.__conv(2, name='conv4_4_CPM', in_channels=256, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 39 | self.conv5_1_CPM_L1 = self.__conv(2, name='conv5_1_CPM_L1', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 40 | self.conv5_1_CPM_L2 = self.__conv(2, name='conv5_1_CPM_L2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 41 | self.conv5_2_CPM_L1 = self.__conv(2, name='conv5_2_CPM_L1', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 42 | self.conv5_2_CPM_L2 = self.__conv(2, name='conv5_2_CPM_L2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 43 | self.conv5_3_CPM_L1 = self.__conv(2, name='conv5_3_CPM_L1', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 44 | self.conv5_3_CPM_L2 = self.__conv(2, name='conv5_3_CPM_L2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 45 | self.conv5_4_CPM_L1 = self.__conv(2, name='conv5_4_CPM_L1', in_channels=128, out_channels=512, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 46 | self.conv5_4_CPM_L2 = self.__conv(2, name='conv5_4_CPM_L2', in_channels=128, out_channels=512, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 47 | self.conv5_5_CPM_L1 = self.__conv(2, name='conv5_5_CPM_L1', in_channels=512, out_channels=28, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 48 | self.conv5_5_CPM_L2 = self.__conv(2, name='conv5_5_CPM_L2', in_channels=512, out_channels=16, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 49 | self.Mconv1_stage2_L1 = self.__conv(2, name='Mconv1_stage2_L1', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 50 | self.Mconv1_stage2_L2 = self.__conv(2, name='Mconv1_stage2_L2', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 51 | self.Mconv2_stage2_L1 = self.__conv(2, name='Mconv2_stage2_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 52 | self.Mconv2_stage2_L2 = self.__conv(2, name='Mconv2_stage2_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 53 | self.Mconv3_stage2_L1 = self.__conv(2, name='Mconv3_stage2_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 54 | self.Mconv3_stage2_L2 = self.__conv(2, name='Mconv3_stage2_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 55 | self.Mconv4_stage2_L1 = self.__conv(2, name='Mconv4_stage2_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 56 | self.Mconv4_stage2_L2 = self.__conv(2, name='Mconv4_stage2_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 57 | self.Mconv5_stage2_L1 = self.__conv(2, name='Mconv5_stage2_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 58 | self.Mconv5_stage2_L2 = self.__conv(2, name='Mconv5_stage2_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 59 | self.Mconv6_stage2_L1 = self.__conv(2, name='Mconv6_stage2_L1', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 60 | self.Mconv6_stage2_L2 = self.__conv(2, name='Mconv6_stage2_L2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 61 | self.Mconv7_stage2_L1 = self.__conv(2, name='Mconv7_stage2_L1', in_channels=128, out_channels=28, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 62 | self.Mconv7_stage2_L2 = self.__conv(2, name='Mconv7_stage2_L2', in_channels=128, out_channels=16, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 63 | self.Mconv1_stage3_L1 = self.__conv(2, name='Mconv1_stage3_L1', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 64 | self.Mconv1_stage3_L2 = self.__conv(2, name='Mconv1_stage3_L2', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 65 | self.Mconv2_stage3_L1 = self.__conv(2, name='Mconv2_stage3_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 66 | self.Mconv2_stage3_L2 = self.__conv(2, name='Mconv2_stage3_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 67 | self.Mconv3_stage3_L1 = self.__conv(2, name='Mconv3_stage3_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 68 | self.Mconv3_stage3_L2 = self.__conv(2, name='Mconv3_stage3_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 69 | self.Mconv4_stage3_L1 = self.__conv(2, name='Mconv4_stage3_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 70 | self.Mconv4_stage3_L2 = self.__conv(2, name='Mconv4_stage3_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 71 | self.Mconv5_stage3_L1 = self.__conv(2, name='Mconv5_stage3_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 72 | self.Mconv5_stage3_L2 = self.__conv(2, name='Mconv5_stage3_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 73 | self.Mconv6_stage3_L1 = self.__conv(2, name='Mconv6_stage3_L1', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 74 | self.Mconv6_stage3_L2 = self.__conv(2, name='Mconv6_stage3_L2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 75 | self.Mconv7_stage3_L1 = self.__conv(2, name='Mconv7_stage3_L1', in_channels=128, out_channels=28, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 76 | self.Mconv7_stage3_L2 = self.__conv(2, name='Mconv7_stage3_L2', in_channels=128, out_channels=16, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 77 | self.Mconv1_stage4_L1 = self.__conv(2, name='Mconv1_stage4_L1', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 78 | self.Mconv1_stage4_L2 = self.__conv(2, name='Mconv1_stage4_L2', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 79 | self.Mconv2_stage4_L1 = self.__conv(2, name='Mconv2_stage4_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 80 | self.Mconv2_stage4_L2 = self.__conv(2, name='Mconv2_stage4_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 81 | self.Mconv3_stage4_L1 = self.__conv(2, name='Mconv3_stage4_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 82 | self.Mconv3_stage4_L2 = self.__conv(2, name='Mconv3_stage4_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 83 | self.Mconv4_stage4_L1 = self.__conv(2, name='Mconv4_stage4_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 84 | self.Mconv4_stage4_L2 = self.__conv(2, name='Mconv4_stage4_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 85 | self.Mconv5_stage4_L1 = self.__conv(2, name='Mconv5_stage4_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 86 | self.Mconv5_stage4_L2 = self.__conv(2, name='Mconv5_stage4_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 87 | self.Mconv6_stage4_L1 = self.__conv(2, name='Mconv6_stage4_L1', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 88 | self.Mconv6_stage4_L2 = self.__conv(2, name='Mconv6_stage4_L2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 89 | self.Mconv7_stage4_L1 = self.__conv(2, name='Mconv7_stage4_L1', in_channels=128, out_channels=28, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 90 | self.Mconv7_stage4_L2 = self.__conv(2, name='Mconv7_stage4_L2', in_channels=128, out_channels=16, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 91 | 92 | def forward(self, x): 93 | conv1_1_pad = F.pad(x, (1, 1, 1, 1)) 94 | conv1_1 = self.conv1_1(conv1_1_pad) 95 | relu1_1 = F.relu(conv1_1) 96 | conv1_2_pad = F.pad(relu1_1, (1, 1, 1, 1)) 97 | conv1_2 = self.conv1_2(conv1_2_pad) 98 | relu1_2 = F.relu(conv1_2) 99 | pool1_stage1_pad = F.pad(relu1_2, (0, 1, 0, 1), value=float('-inf')) 100 | pool1_stage1 = F.max_pool2d(pool1_stage1_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 101 | conv2_1_pad = F.pad(pool1_stage1, (1, 1, 1, 1)) 102 | conv2_1 = self.conv2_1(conv2_1_pad) 103 | relu2_1 = F.relu(conv2_1) 104 | conv2_2_pad = F.pad(relu2_1, (1, 1, 1, 1)) 105 | conv2_2 = self.conv2_2(conv2_2_pad) 106 | relu2_2 = F.relu(conv2_2) 107 | pool2_stage1_pad = F.pad(relu2_2, (0, 1, 0, 1), value=float('-inf')) 108 | pool2_stage1 = F.max_pool2d(pool2_stage1_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 109 | conv3_1_pad = F.pad(pool2_stage1, (1, 1, 1, 1)) 110 | conv3_1 = self.conv3_1(conv3_1_pad) 111 | relu3_1 = F.relu(conv3_1) 112 | conv3_2_pad = F.pad(relu3_1, (1, 1, 1, 1)) 113 | conv3_2 = self.conv3_2(conv3_2_pad) 114 | relu3_2 = F.relu(conv3_2) 115 | conv3_3_pad = F.pad(relu3_2, (1, 1, 1, 1)) 116 | conv3_3 = self.conv3_3(conv3_3_pad) 117 | relu3_3 = F.relu(conv3_3) 118 | conv3_4_pad = F.pad(relu3_3, (1, 1, 1, 1)) 119 | conv3_4 = self.conv3_4(conv3_4_pad) 120 | relu3_4 = F.relu(conv3_4) 121 | pool3_stage1_pad = F.pad(relu3_4, (0, 1, 0, 1), value=float('-inf')) 122 | pool3_stage1 = F.max_pool2d(pool3_stage1_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 123 | conv4_1_pad = F.pad(pool3_stage1, (1, 1, 1, 1)) 124 | conv4_1 = self.conv4_1(conv4_1_pad) 125 | relu4_1 = F.relu(conv4_1) 126 | conv4_2_pad = F.pad(relu4_1, (1, 1, 1, 1)) 127 | conv4_2 = self.conv4_2(conv4_2_pad) 128 | relu4_2 = F.relu(conv4_2) 129 | conv4_3_CPM_pad = F.pad(relu4_2, (1, 1, 1, 1)) 130 | conv4_3_CPM = self.conv4_3_CPM(conv4_3_CPM_pad) 131 | relu4_3_CPM = F.relu(conv4_3_CPM) 132 | conv4_4_CPM_pad = F.pad(relu4_3_CPM, (1, 1, 1, 1)) 133 | conv4_4_CPM = self.conv4_4_CPM(conv4_4_CPM_pad) 134 | relu4_4_CPM = F.relu(conv4_4_CPM) 135 | conv5_1_CPM_L1_pad = F.pad(relu4_4_CPM, (1, 1, 1, 1)) 136 | conv5_1_CPM_L1 = self.conv5_1_CPM_L1(conv5_1_CPM_L1_pad) 137 | conv5_1_CPM_L2_pad = F.pad(relu4_4_CPM, (1, 1, 1, 1)) 138 | conv5_1_CPM_L2 = self.conv5_1_CPM_L2(conv5_1_CPM_L2_pad) 139 | relu5_1_CPM_L1 = F.relu(conv5_1_CPM_L1) 140 | relu5_1_CPM_L2 = F.relu(conv5_1_CPM_L2) 141 | conv5_2_CPM_L1_pad = F.pad(relu5_1_CPM_L1, (1, 1, 1, 1)) 142 | conv5_2_CPM_L1 = self.conv5_2_CPM_L1(conv5_2_CPM_L1_pad) 143 | conv5_2_CPM_L2_pad = F.pad(relu5_1_CPM_L2, (1, 1, 1, 1)) 144 | conv5_2_CPM_L2 = self.conv5_2_CPM_L2(conv5_2_CPM_L2_pad) 145 | relu5_2_CPM_L1 = F.relu(conv5_2_CPM_L1) 146 | relu5_2_CPM_L2 = F.relu(conv5_2_CPM_L2) 147 | conv5_3_CPM_L1_pad = F.pad(relu5_2_CPM_L1, (1, 1, 1, 1)) 148 | conv5_3_CPM_L1 = self.conv5_3_CPM_L1(conv5_3_CPM_L1_pad) 149 | conv5_3_CPM_L2_pad = F.pad(relu5_2_CPM_L2, (1, 1, 1, 1)) 150 | conv5_3_CPM_L2 = self.conv5_3_CPM_L2(conv5_3_CPM_L2_pad) 151 | relu5_3_CPM_L1 = F.relu(conv5_3_CPM_L1) 152 | relu5_3_CPM_L2 = F.relu(conv5_3_CPM_L2) 153 | conv5_4_CPM_L1 = self.conv5_4_CPM_L1(relu5_3_CPM_L1) 154 | conv5_4_CPM_L2 = self.conv5_4_CPM_L2(relu5_3_CPM_L2) 155 | relu5_4_CPM_L1 = F.relu(conv5_4_CPM_L1) 156 | relu5_4_CPM_L2 = F.relu(conv5_4_CPM_L2) 157 | conv5_5_CPM_L1 = self.conv5_5_CPM_L1(relu5_4_CPM_L1) 158 | conv5_5_CPM_L2 = self.conv5_5_CPM_L2(relu5_4_CPM_L2) 159 | concat_stage2 = torch.cat((conv5_5_CPM_L1, conv5_5_CPM_L2, relu4_4_CPM), 1) 160 | Mconv1_stage2_L1_pad = F.pad(concat_stage2, (3, 3, 3, 3)) 161 | Mconv1_stage2_L1 = self.Mconv1_stage2_L1(Mconv1_stage2_L1_pad) 162 | Mconv1_stage2_L2_pad = F.pad(concat_stage2, (3, 3, 3, 3)) 163 | Mconv1_stage2_L2 = self.Mconv1_stage2_L2(Mconv1_stage2_L2_pad) 164 | Mrelu1_stage2_L1 = F.relu(Mconv1_stage2_L1) 165 | Mrelu1_stage2_L2 = F.relu(Mconv1_stage2_L2) 166 | Mconv2_stage2_L1_pad = F.pad(Mrelu1_stage2_L1, (3, 3, 3, 3)) 167 | Mconv2_stage2_L1 = self.Mconv2_stage2_L1(Mconv2_stage2_L1_pad) 168 | Mconv2_stage2_L2_pad = F.pad(Mrelu1_stage2_L2, (3, 3, 3, 3)) 169 | Mconv2_stage2_L2 = self.Mconv2_stage2_L2(Mconv2_stage2_L2_pad) 170 | Mrelu2_stage2_L1 = F.relu(Mconv2_stage2_L1) 171 | Mrelu2_stage2_L2 = F.relu(Mconv2_stage2_L2) 172 | Mconv3_stage2_L1_pad = F.pad(Mrelu2_stage2_L1, (3, 3, 3, 3)) 173 | Mconv3_stage2_L1 = self.Mconv3_stage2_L1(Mconv3_stage2_L1_pad) 174 | Mconv3_stage2_L2_pad = F.pad(Mrelu2_stage2_L2, (3, 3, 3, 3)) 175 | Mconv3_stage2_L2 = self.Mconv3_stage2_L2(Mconv3_stage2_L2_pad) 176 | Mrelu3_stage2_L1 = F.relu(Mconv3_stage2_L1) 177 | Mrelu3_stage2_L2 = F.relu(Mconv3_stage2_L2) 178 | Mconv4_stage2_L1_pad = F.pad(Mrelu3_stage2_L1, (3, 3, 3, 3)) 179 | Mconv4_stage2_L1 = self.Mconv4_stage2_L1(Mconv4_stage2_L1_pad) 180 | Mconv4_stage2_L2_pad = F.pad(Mrelu3_stage2_L2, (3, 3, 3, 3)) 181 | Mconv4_stage2_L2 = self.Mconv4_stage2_L2(Mconv4_stage2_L2_pad) 182 | Mrelu4_stage2_L1 = F.relu(Mconv4_stage2_L1) 183 | Mrelu4_stage2_L2 = F.relu(Mconv4_stage2_L2) 184 | Mconv5_stage2_L1_pad = F.pad(Mrelu4_stage2_L1, (3, 3, 3, 3)) 185 | Mconv5_stage2_L1 = self.Mconv5_stage2_L1(Mconv5_stage2_L1_pad) 186 | Mconv5_stage2_L2_pad = F.pad(Mrelu4_stage2_L2, (3, 3, 3, 3)) 187 | Mconv5_stage2_L2 = self.Mconv5_stage2_L2(Mconv5_stage2_L2_pad) 188 | Mrelu5_stage2_L1 = F.relu(Mconv5_stage2_L1) 189 | Mrelu5_stage2_L2 = F.relu(Mconv5_stage2_L2) 190 | Mconv6_stage2_L1 = self.Mconv6_stage2_L1(Mrelu5_stage2_L1) 191 | Mconv6_stage2_L2 = self.Mconv6_stage2_L2(Mrelu5_stage2_L2) 192 | Mrelu6_stage2_L1 = F.relu(Mconv6_stage2_L1) 193 | Mrelu6_stage2_L2 = F.relu(Mconv6_stage2_L2) 194 | Mconv7_stage2_L1 = self.Mconv7_stage2_L1(Mrelu6_stage2_L1) 195 | Mconv7_stage2_L2 = self.Mconv7_stage2_L2(Mrelu6_stage2_L2) 196 | concat_stage3 = torch.cat((Mconv7_stage2_L1, Mconv7_stage2_L2, relu4_4_CPM), 1) 197 | Mconv1_stage3_L1_pad = F.pad(concat_stage3, (3, 3, 3, 3)) 198 | Mconv1_stage3_L1 = self.Mconv1_stage3_L1(Mconv1_stage3_L1_pad) 199 | Mconv1_stage3_L2_pad = F.pad(concat_stage3, (3, 3, 3, 3)) 200 | Mconv1_stage3_L2 = self.Mconv1_stage3_L2(Mconv1_stage3_L2_pad) 201 | Mrelu1_stage3_L1 = F.relu(Mconv1_stage3_L1) 202 | Mrelu1_stage3_L2 = F.relu(Mconv1_stage3_L2) 203 | Mconv2_stage3_L1_pad = F.pad(Mrelu1_stage3_L1, (3, 3, 3, 3)) 204 | Mconv2_stage3_L1 = self.Mconv2_stage3_L1(Mconv2_stage3_L1_pad) 205 | Mconv2_stage3_L2_pad = F.pad(Mrelu1_stage3_L2, (3, 3, 3, 3)) 206 | Mconv2_stage3_L2 = self.Mconv2_stage3_L2(Mconv2_stage3_L2_pad) 207 | Mrelu2_stage3_L1 = F.relu(Mconv2_stage3_L1) 208 | Mrelu2_stage3_L2 = F.relu(Mconv2_stage3_L2) 209 | Mconv3_stage3_L1_pad = F.pad(Mrelu2_stage3_L1, (3, 3, 3, 3)) 210 | Mconv3_stage3_L1 = self.Mconv3_stage3_L1(Mconv3_stage3_L1_pad) 211 | Mconv3_stage3_L2_pad = F.pad(Mrelu2_stage3_L2, (3, 3, 3, 3)) 212 | Mconv3_stage3_L2 = self.Mconv3_stage3_L2(Mconv3_stage3_L2_pad) 213 | Mrelu3_stage3_L1 = F.relu(Mconv3_stage3_L1) 214 | Mrelu3_stage3_L2 = F.relu(Mconv3_stage3_L2) 215 | Mconv4_stage3_L1_pad = F.pad(Mrelu3_stage3_L1, (3, 3, 3, 3)) 216 | Mconv4_stage3_L1 = self.Mconv4_stage3_L1(Mconv4_stage3_L1_pad) 217 | Mconv4_stage3_L2_pad = F.pad(Mrelu3_stage3_L2, (3, 3, 3, 3)) 218 | Mconv4_stage3_L2 = self.Mconv4_stage3_L2(Mconv4_stage3_L2_pad) 219 | Mrelu4_stage3_L1 = F.relu(Mconv4_stage3_L1) 220 | Mrelu4_stage3_L2 = F.relu(Mconv4_stage3_L2) 221 | Mconv5_stage3_L1_pad = F.pad(Mrelu4_stage3_L1, (3, 3, 3, 3)) 222 | Mconv5_stage3_L1 = self.Mconv5_stage3_L1(Mconv5_stage3_L1_pad) 223 | Mconv5_stage3_L2_pad = F.pad(Mrelu4_stage3_L2, (3, 3, 3, 3)) 224 | Mconv5_stage3_L2 = self.Mconv5_stage3_L2(Mconv5_stage3_L2_pad) 225 | Mrelu5_stage3_L1 = F.relu(Mconv5_stage3_L1) 226 | Mrelu5_stage3_L2 = F.relu(Mconv5_stage3_L2) 227 | Mconv6_stage3_L1 = self.Mconv6_stage3_L1(Mrelu5_stage3_L1) 228 | Mconv6_stage3_L2 = self.Mconv6_stage3_L2(Mrelu5_stage3_L2) 229 | Mrelu6_stage3_L1 = F.relu(Mconv6_stage3_L1) 230 | Mrelu6_stage3_L2 = F.relu(Mconv6_stage3_L2) 231 | Mconv7_stage3_L1 = self.Mconv7_stage3_L1(Mrelu6_stage3_L1) 232 | Mconv7_stage3_L2 = self.Mconv7_stage3_L2(Mrelu6_stage3_L2) 233 | concat_stage4 = torch.cat((Mconv7_stage3_L1, Mconv7_stage3_L2, relu4_4_CPM), 1) 234 | Mconv1_stage4_L1_pad = F.pad(concat_stage4, (3, 3, 3, 3)) 235 | Mconv1_stage4_L1 = self.Mconv1_stage4_L1(Mconv1_stage4_L1_pad) 236 | Mconv1_stage4_L2_pad = F.pad(concat_stage4, (3, 3, 3, 3)) 237 | Mconv1_stage4_L2 = self.Mconv1_stage4_L2(Mconv1_stage4_L2_pad) 238 | Mrelu1_stage4_L1 = F.relu(Mconv1_stage4_L1) 239 | Mrelu1_stage4_L2 = F.relu(Mconv1_stage4_L2) 240 | Mconv2_stage4_L1_pad = F.pad(Mrelu1_stage4_L1, (3, 3, 3, 3)) 241 | Mconv2_stage4_L1 = self.Mconv2_stage4_L1(Mconv2_stage4_L1_pad) 242 | Mconv2_stage4_L2_pad = F.pad(Mrelu1_stage4_L2, (3, 3, 3, 3)) 243 | Mconv2_stage4_L2 = self.Mconv2_stage4_L2(Mconv2_stage4_L2_pad) 244 | Mrelu2_stage4_L1 = F.relu(Mconv2_stage4_L1) 245 | Mrelu2_stage4_L2 = F.relu(Mconv2_stage4_L2) 246 | Mconv3_stage4_L1_pad = F.pad(Mrelu2_stage4_L1, (3, 3, 3, 3)) 247 | Mconv3_stage4_L1 = self.Mconv3_stage4_L1(Mconv3_stage4_L1_pad) 248 | Mconv3_stage4_L2_pad = F.pad(Mrelu2_stage4_L2, (3, 3, 3, 3)) 249 | Mconv3_stage4_L2 = self.Mconv3_stage4_L2(Mconv3_stage4_L2_pad) 250 | Mrelu3_stage4_L1 = F.relu(Mconv3_stage4_L1) 251 | Mrelu3_stage4_L2 = F.relu(Mconv3_stage4_L2) 252 | Mconv4_stage4_L1_pad = F.pad(Mrelu3_stage4_L1, (3, 3, 3, 3)) 253 | Mconv4_stage4_L1 = self.Mconv4_stage4_L1(Mconv4_stage4_L1_pad) 254 | Mconv4_stage4_L2_pad = F.pad(Mrelu3_stage4_L2, (3, 3, 3, 3)) 255 | Mconv4_stage4_L2 = self.Mconv4_stage4_L2(Mconv4_stage4_L2_pad) 256 | Mrelu4_stage4_L1 = F.relu(Mconv4_stage4_L1) 257 | Mrelu4_stage4_L2 = F.relu(Mconv4_stage4_L2) 258 | Mconv5_stage4_L1_pad = F.pad(Mrelu4_stage4_L1, (3, 3, 3, 3)) 259 | Mconv5_stage4_L1 = self.Mconv5_stage4_L1(Mconv5_stage4_L1_pad) 260 | Mconv5_stage4_L2_pad = F.pad(Mrelu4_stage4_L2, (3, 3, 3, 3)) 261 | Mconv5_stage4_L2 = self.Mconv5_stage4_L2(Mconv5_stage4_L2_pad) 262 | Mrelu5_stage4_L1 = F.relu(Mconv5_stage4_L1) 263 | Mrelu5_stage4_L2 = F.relu(Mconv5_stage4_L2) 264 | Mconv6_stage4_L1 = self.Mconv6_stage4_L1(Mrelu5_stage4_L1) 265 | Mconv6_stage4_L2 = self.Mconv6_stage4_L2(Mrelu5_stage4_L2) 266 | Mrelu6_stage4_L1 = F.relu(Mconv6_stage4_L1) 267 | Mrelu6_stage4_L2 = F.relu(Mconv6_stage4_L2) 268 | Mconv7_stage4_L1 = self.Mconv7_stage4_L1(Mrelu6_stage4_L1) 269 | Mconv7_stage4_L2 = self.Mconv7_stage4_L2(Mrelu6_stage4_L2) 270 | concat_stage7 = torch.cat((Mconv7_stage4_L2, Mconv7_stage4_L1), 1) 271 | return concat_stage7 272 | 273 | 274 | @staticmethod 275 | def __conv(dim, name, **kwargs): 276 | if dim == 1: layer = nn.Conv1d(**kwargs) 277 | elif dim == 2: layer = nn.Conv2d(**kwargs) 278 | elif dim == 3: layer = nn.Conv3d(**kwargs) 279 | else: raise NotImplementedError() 280 | 281 | layer.state_dict()['weight'].copy_(torch.from_numpy(__weights_dict[name]['weights'])) 282 | if 'bias' in __weights_dict[name]: 283 | layer.state_dict()['bias'].copy_(torch.from_numpy(__weights_dict[name]['bias'])) 284 | return layer 285 | 286 | model = KitModel('pose_faster_pytorch.npy') 287 | input_size = (1, 3, 224, 224) 288 | -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/pose/coco/pose_linevec_pytorch.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | __weights_dict = dict() 7 | 8 | def load_weights(weight_file): 9 | if weight_file == None: 10 | return 11 | 12 | try: 13 | weights_dict = np.load(weight_file).item() 14 | except: 15 | weights_dict = np.load(weight_file, encoding='bytes').item() 16 | 17 | return weights_dict 18 | 19 | class KitModel(nn.Module): 20 | 21 | 22 | def __init__(self, weight_file): 23 | super(KitModel, self).__init__() 24 | global __weights_dict 25 | __weights_dict = load_weights(weight_file) 26 | 27 | self.conv1_1 = self.__conv(2, name='conv1_1', in_channels=3, out_channels=64, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 28 | self.conv1_2 = self.__conv(2, name='conv1_2', in_channels=64, out_channels=64, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 29 | self.conv2_1 = self.__conv(2, name='conv2_1', in_channels=64, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 30 | self.conv2_2 = self.__conv(2, name='conv2_2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 31 | self.conv3_1 = self.__conv(2, name='conv3_1', in_channels=128, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 32 | self.conv3_2 = self.__conv(2, name='conv3_2', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 33 | self.conv3_3 = self.__conv(2, name='conv3_3', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 34 | self.conv3_4 = self.__conv(2, name='conv3_4', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 35 | self.conv4_1 = self.__conv(2, name='conv4_1', in_channels=256, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 36 | self.conv4_2 = self.__conv(2, name='conv4_2', in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 37 | self.conv4_3_CPM = self.__conv(2, name='conv4_3_CPM', in_channels=512, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 38 | self.conv4_4_CPM = self.__conv(2, name='conv4_4_CPM', in_channels=256, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 39 | self.conv5_1_CPM_L1 = self.__conv(2, name='conv5_1_CPM_L1', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 40 | self.conv5_1_CPM_L2 = self.__conv(2, name='conv5_1_CPM_L2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 41 | self.conv5_2_CPM_L1 = self.__conv(2, name='conv5_2_CPM_L1', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 42 | self.conv5_2_CPM_L2 = self.__conv(2, name='conv5_2_CPM_L2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 43 | self.conv5_3_CPM_L1 = self.__conv(2, name='conv5_3_CPM_L1', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 44 | self.conv5_3_CPM_L2 = self.__conv(2, name='conv5_3_CPM_L2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 45 | self.conv5_4_CPM_L1 = self.__conv(2, name='conv5_4_CPM_L1', in_channels=128, out_channels=512, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 46 | self.conv5_4_CPM_L2 = self.__conv(2, name='conv5_4_CPM_L2', in_channels=128, out_channels=512, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 47 | self.conv5_5_CPM_L1 = self.__conv(2, name='conv5_5_CPM_L1', in_channels=512, out_channels=38, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 48 | self.conv5_5_CPM_L2 = self.__conv(2, name='conv5_5_CPM_L2', in_channels=512, out_channels=19, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 49 | self.Mconv1_stage2_L1 = self.__conv(2, name='Mconv1_stage2_L1', in_channels=185, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 50 | self.Mconv1_stage2_L2 = self.__conv(2, name='Mconv1_stage2_L2', in_channels=185, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 51 | self.Mconv2_stage2_L1 = self.__conv(2, name='Mconv2_stage2_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 52 | self.Mconv2_stage2_L2 = self.__conv(2, name='Mconv2_stage2_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 53 | self.Mconv3_stage2_L1 = self.__conv(2, name='Mconv3_stage2_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 54 | self.Mconv3_stage2_L2 = self.__conv(2, name='Mconv3_stage2_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 55 | self.Mconv4_stage2_L1 = self.__conv(2, name='Mconv4_stage2_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 56 | self.Mconv4_stage2_L2 = self.__conv(2, name='Mconv4_stage2_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 57 | self.Mconv5_stage2_L1 = self.__conv(2, name='Mconv5_stage2_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 58 | self.Mconv5_stage2_L2 = self.__conv(2, name='Mconv5_stage2_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 59 | self.Mconv6_stage2_L1 = self.__conv(2, name='Mconv6_stage2_L1', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 60 | self.Mconv6_stage2_L2 = self.__conv(2, name='Mconv6_stage2_L2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 61 | self.Mconv7_stage2_L1 = self.__conv(2, name='Mconv7_stage2_L1', in_channels=128, out_channels=38, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 62 | self.Mconv7_stage2_L2 = self.__conv(2, name='Mconv7_stage2_L2', in_channels=128, out_channels=19, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 63 | self.Mconv1_stage3_L1 = self.__conv(2, name='Mconv1_stage3_L1', in_channels=185, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 64 | self.Mconv1_stage3_L2 = self.__conv(2, name='Mconv1_stage3_L2', in_channels=185, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 65 | self.Mconv2_stage3_L1 = self.__conv(2, name='Mconv2_stage3_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 66 | self.Mconv2_stage3_L2 = self.__conv(2, name='Mconv2_stage3_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 67 | self.Mconv3_stage3_L1 = self.__conv(2, name='Mconv3_stage3_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 68 | self.Mconv3_stage3_L2 = self.__conv(2, name='Mconv3_stage3_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 69 | self.Mconv4_stage3_L1 = self.__conv(2, name='Mconv4_stage3_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 70 | self.Mconv4_stage3_L2 = self.__conv(2, name='Mconv4_stage3_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 71 | self.Mconv5_stage3_L1 = self.__conv(2, name='Mconv5_stage3_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 72 | self.Mconv5_stage3_L2 = self.__conv(2, name='Mconv5_stage3_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 73 | self.Mconv6_stage3_L1 = self.__conv(2, name='Mconv6_stage3_L1', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 74 | self.Mconv6_stage3_L2 = self.__conv(2, name='Mconv6_stage3_L2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 75 | self.Mconv7_stage3_L1 = self.__conv(2, name='Mconv7_stage3_L1', in_channels=128, out_channels=38, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 76 | self.Mconv7_stage3_L2 = self.__conv(2, name='Mconv7_stage3_L2', in_channels=128, out_channels=19, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 77 | self.Mconv1_stage4_L1 = self.__conv(2, name='Mconv1_stage4_L1', in_channels=185, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 78 | self.Mconv1_stage4_L2 = self.__conv(2, name='Mconv1_stage4_L2', in_channels=185, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 79 | self.Mconv2_stage4_L1 = self.__conv(2, name='Mconv2_stage4_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 80 | self.Mconv2_stage4_L2 = self.__conv(2, name='Mconv2_stage4_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 81 | self.Mconv3_stage4_L1 = self.__conv(2, name='Mconv3_stage4_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 82 | self.Mconv3_stage4_L2 = self.__conv(2, name='Mconv3_stage4_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 83 | self.Mconv4_stage4_L1 = self.__conv(2, name='Mconv4_stage4_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 84 | self.Mconv4_stage4_L2 = self.__conv(2, name='Mconv4_stage4_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 85 | self.Mconv5_stage4_L1 = self.__conv(2, name='Mconv5_stage4_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 86 | self.Mconv5_stage4_L2 = self.__conv(2, name='Mconv5_stage4_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 87 | self.Mconv6_stage4_L1 = self.__conv(2, name='Mconv6_stage4_L1', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 88 | self.Mconv6_stage4_L2 = self.__conv(2, name='Mconv6_stage4_L2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 89 | self.Mconv7_stage4_L1 = self.__conv(2, name='Mconv7_stage4_L1', in_channels=128, out_channels=38, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 90 | self.Mconv7_stage4_L2 = self.__conv(2, name='Mconv7_stage4_L2', in_channels=128, out_channels=19, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 91 | self.Mconv1_stage5_L1 = self.__conv(2, name='Mconv1_stage5_L1', in_channels=185, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 92 | self.Mconv1_stage5_L2 = self.__conv(2, name='Mconv1_stage5_L2', in_channels=185, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 93 | self.Mconv2_stage5_L1 = self.__conv(2, name='Mconv2_stage5_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 94 | self.Mconv2_stage5_L2 = self.__conv(2, name='Mconv2_stage5_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 95 | self.Mconv3_stage5_L1 = self.__conv(2, name='Mconv3_stage5_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 96 | self.Mconv3_stage5_L2 = self.__conv(2, name='Mconv3_stage5_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 97 | self.Mconv4_stage5_L1 = self.__conv(2, name='Mconv4_stage5_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 98 | self.Mconv4_stage5_L2 = self.__conv(2, name='Mconv4_stage5_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 99 | self.Mconv5_stage5_L1 = self.__conv(2, name='Mconv5_stage5_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 100 | self.Mconv5_stage5_L2 = self.__conv(2, name='Mconv5_stage5_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 101 | self.Mconv6_stage5_L1 = self.__conv(2, name='Mconv6_stage5_L1', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 102 | self.Mconv6_stage5_L2 = self.__conv(2, name='Mconv6_stage5_L2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 103 | self.Mconv7_stage5_L1 = self.__conv(2, name='Mconv7_stage5_L1', in_channels=128, out_channels=38, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 104 | self.Mconv7_stage5_L2 = self.__conv(2, name='Mconv7_stage5_L2', in_channels=128, out_channels=19, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 105 | self.Mconv1_stage6_L1 = self.__conv(2, name='Mconv1_stage6_L1', in_channels=185, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 106 | self.Mconv1_stage6_L2 = self.__conv(2, name='Mconv1_stage6_L2', in_channels=185, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 107 | self.Mconv2_stage6_L1 = self.__conv(2, name='Mconv2_stage6_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 108 | self.Mconv2_stage6_L2 = self.__conv(2, name='Mconv2_stage6_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 109 | self.Mconv3_stage6_L1 = self.__conv(2, name='Mconv3_stage6_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 110 | self.Mconv3_stage6_L2 = self.__conv(2, name='Mconv3_stage6_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 111 | self.Mconv4_stage6_L1 = self.__conv(2, name='Mconv4_stage6_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 112 | self.Mconv4_stage6_L2 = self.__conv(2, name='Mconv4_stage6_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 113 | self.Mconv5_stage6_L1 = self.__conv(2, name='Mconv5_stage6_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 114 | self.Mconv5_stage6_L2 = self.__conv(2, name='Mconv5_stage6_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 115 | self.Mconv6_stage6_L1 = self.__conv(2, name='Mconv6_stage6_L1', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 116 | self.Mconv6_stage6_L2 = self.__conv(2, name='Mconv6_stage6_L2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 117 | self.Mconv7_stage6_L1 = self.__conv(2, name='Mconv7_stage6_L1', in_channels=128, out_channels=38, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 118 | self.Mconv7_stage6_L2 = self.__conv(2, name='Mconv7_stage6_L2', in_channels=128, out_channels=19, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 119 | 120 | def forward(self, x): 121 | conv1_1_pad = F.pad(x, (1, 1, 1, 1)) 122 | conv1_1 = self.conv1_1(conv1_1_pad) 123 | relu1_1 = F.relu(conv1_1) 124 | conv1_2_pad = F.pad(relu1_1, (1, 1, 1, 1)) 125 | conv1_2 = self.conv1_2(conv1_2_pad) 126 | relu1_2 = F.relu(conv1_2) 127 | pool1_stage1_pad = F.pad(relu1_2, (0, 1, 0, 1), value=float('-inf')) 128 | pool1_stage1 = F.max_pool2d(pool1_stage1_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 129 | conv2_1_pad = F.pad(pool1_stage1, (1, 1, 1, 1)) 130 | conv2_1 = self.conv2_1(conv2_1_pad) 131 | relu2_1 = F.relu(conv2_1) 132 | conv2_2_pad = F.pad(relu2_1, (1, 1, 1, 1)) 133 | conv2_2 = self.conv2_2(conv2_2_pad) 134 | relu2_2 = F.relu(conv2_2) 135 | pool2_stage1_pad = F.pad(relu2_2, (0, 1, 0, 1), value=float('-inf')) 136 | pool2_stage1 = F.max_pool2d(pool2_stage1_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 137 | conv3_1_pad = F.pad(pool2_stage1, (1, 1, 1, 1)) 138 | conv3_1 = self.conv3_1(conv3_1_pad) 139 | relu3_1 = F.relu(conv3_1) 140 | conv3_2_pad = F.pad(relu3_1, (1, 1, 1, 1)) 141 | conv3_2 = self.conv3_2(conv3_2_pad) 142 | relu3_2 = F.relu(conv3_2) 143 | conv3_3_pad = F.pad(relu3_2, (1, 1, 1, 1)) 144 | conv3_3 = self.conv3_3(conv3_3_pad) 145 | relu3_3 = F.relu(conv3_3) 146 | conv3_4_pad = F.pad(relu3_3, (1, 1, 1, 1)) 147 | conv3_4 = self.conv3_4(conv3_4_pad) 148 | relu3_4 = F.relu(conv3_4) 149 | pool3_stage1_pad = F.pad(relu3_4, (0, 1, 0, 1), value=float('-inf')) 150 | pool3_stage1 = F.max_pool2d(pool3_stage1_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 151 | conv4_1_pad = F.pad(pool3_stage1, (1, 1, 1, 1)) 152 | conv4_1 = self.conv4_1(conv4_1_pad) 153 | relu4_1 = F.relu(conv4_1) 154 | conv4_2_pad = F.pad(relu4_1, (1, 1, 1, 1)) 155 | conv4_2 = self.conv4_2(conv4_2_pad) 156 | relu4_2 = F.relu(conv4_2) 157 | conv4_3_CPM_pad = F.pad(relu4_2, (1, 1, 1, 1)) 158 | conv4_3_CPM = self.conv4_3_CPM(conv4_3_CPM_pad) 159 | relu4_3_CPM = F.relu(conv4_3_CPM) 160 | conv4_4_CPM_pad = F.pad(relu4_3_CPM, (1, 1, 1, 1)) 161 | conv4_4_CPM = self.conv4_4_CPM(conv4_4_CPM_pad) 162 | relu4_4_CPM = F.relu(conv4_4_CPM) 163 | conv5_1_CPM_L1_pad = F.pad(relu4_4_CPM, (1, 1, 1, 1)) 164 | conv5_1_CPM_L1 = self.conv5_1_CPM_L1(conv5_1_CPM_L1_pad) 165 | conv5_1_CPM_L2_pad = F.pad(relu4_4_CPM, (1, 1, 1, 1)) 166 | conv5_1_CPM_L2 = self.conv5_1_CPM_L2(conv5_1_CPM_L2_pad) 167 | relu5_1_CPM_L1 = F.relu(conv5_1_CPM_L1) 168 | relu5_1_CPM_L2 = F.relu(conv5_1_CPM_L2) 169 | conv5_2_CPM_L1_pad = F.pad(relu5_1_CPM_L1, (1, 1, 1, 1)) 170 | conv5_2_CPM_L1 = self.conv5_2_CPM_L1(conv5_2_CPM_L1_pad) 171 | conv5_2_CPM_L2_pad = F.pad(relu5_1_CPM_L2, (1, 1, 1, 1)) 172 | conv5_2_CPM_L2 = self.conv5_2_CPM_L2(conv5_2_CPM_L2_pad) 173 | relu5_2_CPM_L1 = F.relu(conv5_2_CPM_L1) 174 | relu5_2_CPM_L2 = F.relu(conv5_2_CPM_L2) 175 | conv5_3_CPM_L1_pad = F.pad(relu5_2_CPM_L1, (1, 1, 1, 1)) 176 | conv5_3_CPM_L1 = self.conv5_3_CPM_L1(conv5_3_CPM_L1_pad) 177 | conv5_3_CPM_L2_pad = F.pad(relu5_2_CPM_L2, (1, 1, 1, 1)) 178 | conv5_3_CPM_L2 = self.conv5_3_CPM_L2(conv5_3_CPM_L2_pad) 179 | relu5_3_CPM_L1 = F.relu(conv5_3_CPM_L1) 180 | relu5_3_CPM_L2 = F.relu(conv5_3_CPM_L2) 181 | conv5_4_CPM_L1 = self.conv5_4_CPM_L1(relu5_3_CPM_L1) 182 | conv5_4_CPM_L2 = self.conv5_4_CPM_L2(relu5_3_CPM_L2) 183 | relu5_4_CPM_L1 = F.relu(conv5_4_CPM_L1) 184 | relu5_4_CPM_L2 = F.relu(conv5_4_CPM_L2) 185 | conv5_5_CPM_L1 = self.conv5_5_CPM_L1(relu5_4_CPM_L1) 186 | conv5_5_CPM_L2 = self.conv5_5_CPM_L2(relu5_4_CPM_L2) 187 | concat_stage2 = torch.cat((conv5_5_CPM_L1, conv5_5_CPM_L2, relu4_4_CPM), 1) 188 | Mconv1_stage2_L1_pad = F.pad(concat_stage2, (3, 3, 3, 3)) 189 | Mconv1_stage2_L1 = self.Mconv1_stage2_L1(Mconv1_stage2_L1_pad) 190 | Mconv1_stage2_L2_pad = F.pad(concat_stage2, (3, 3, 3, 3)) 191 | Mconv1_stage2_L2 = self.Mconv1_stage2_L2(Mconv1_stage2_L2_pad) 192 | Mrelu1_stage2_L1 = F.relu(Mconv1_stage2_L1) 193 | Mrelu1_stage2_L2 = F.relu(Mconv1_stage2_L2) 194 | Mconv2_stage2_L1_pad = F.pad(Mrelu1_stage2_L1, (3, 3, 3, 3)) 195 | Mconv2_stage2_L1 = self.Mconv2_stage2_L1(Mconv2_stage2_L1_pad) 196 | Mconv2_stage2_L2_pad = F.pad(Mrelu1_stage2_L2, (3, 3, 3, 3)) 197 | Mconv2_stage2_L2 = self.Mconv2_stage2_L2(Mconv2_stage2_L2_pad) 198 | Mrelu2_stage2_L1 = F.relu(Mconv2_stage2_L1) 199 | Mrelu2_stage2_L2 = F.relu(Mconv2_stage2_L2) 200 | Mconv3_stage2_L1_pad = F.pad(Mrelu2_stage2_L1, (3, 3, 3, 3)) 201 | Mconv3_stage2_L1 = self.Mconv3_stage2_L1(Mconv3_stage2_L1_pad) 202 | Mconv3_stage2_L2_pad = F.pad(Mrelu2_stage2_L2, (3, 3, 3, 3)) 203 | Mconv3_stage2_L2 = self.Mconv3_stage2_L2(Mconv3_stage2_L2_pad) 204 | Mrelu3_stage2_L1 = F.relu(Mconv3_stage2_L1) 205 | Mrelu3_stage2_L2 = F.relu(Mconv3_stage2_L2) 206 | Mconv4_stage2_L1_pad = F.pad(Mrelu3_stage2_L1, (3, 3, 3, 3)) 207 | Mconv4_stage2_L1 = self.Mconv4_stage2_L1(Mconv4_stage2_L1_pad) 208 | Mconv4_stage2_L2_pad = F.pad(Mrelu3_stage2_L2, (3, 3, 3, 3)) 209 | Mconv4_stage2_L2 = self.Mconv4_stage2_L2(Mconv4_stage2_L2_pad) 210 | Mrelu4_stage2_L1 = F.relu(Mconv4_stage2_L1) 211 | Mrelu4_stage2_L2 = F.relu(Mconv4_stage2_L2) 212 | Mconv5_stage2_L1_pad = F.pad(Mrelu4_stage2_L1, (3, 3, 3, 3)) 213 | Mconv5_stage2_L1 = self.Mconv5_stage2_L1(Mconv5_stage2_L1_pad) 214 | Mconv5_stage2_L2_pad = F.pad(Mrelu4_stage2_L2, (3, 3, 3, 3)) 215 | Mconv5_stage2_L2 = self.Mconv5_stage2_L2(Mconv5_stage2_L2_pad) 216 | Mrelu5_stage2_L1 = F.relu(Mconv5_stage2_L1) 217 | Mrelu5_stage2_L2 = F.relu(Mconv5_stage2_L2) 218 | Mconv6_stage2_L1 = self.Mconv6_stage2_L1(Mrelu5_stage2_L1) 219 | Mconv6_stage2_L2 = self.Mconv6_stage2_L2(Mrelu5_stage2_L2) 220 | Mrelu6_stage2_L1 = F.relu(Mconv6_stage2_L1) 221 | Mrelu6_stage2_L2 = F.relu(Mconv6_stage2_L2) 222 | Mconv7_stage2_L1 = self.Mconv7_stage2_L1(Mrelu6_stage2_L1) 223 | Mconv7_stage2_L2 = self.Mconv7_stage2_L2(Mrelu6_stage2_L2) 224 | concat_stage3 = torch.cat((Mconv7_stage2_L1, Mconv7_stage2_L2, relu4_4_CPM), 1) 225 | Mconv1_stage3_L1_pad = F.pad(concat_stage3, (3, 3, 3, 3)) 226 | Mconv1_stage3_L1 = self.Mconv1_stage3_L1(Mconv1_stage3_L1_pad) 227 | Mconv1_stage3_L2_pad = F.pad(concat_stage3, (3, 3, 3, 3)) 228 | Mconv1_stage3_L2 = self.Mconv1_stage3_L2(Mconv1_stage3_L2_pad) 229 | Mrelu1_stage3_L1 = F.relu(Mconv1_stage3_L1) 230 | Mrelu1_stage3_L2 = F.relu(Mconv1_stage3_L2) 231 | Mconv2_stage3_L1_pad = F.pad(Mrelu1_stage3_L1, (3, 3, 3, 3)) 232 | Mconv2_stage3_L1 = self.Mconv2_stage3_L1(Mconv2_stage3_L1_pad) 233 | Mconv2_stage3_L2_pad = F.pad(Mrelu1_stage3_L2, (3, 3, 3, 3)) 234 | Mconv2_stage3_L2 = self.Mconv2_stage3_L2(Mconv2_stage3_L2_pad) 235 | Mrelu2_stage3_L1 = F.relu(Mconv2_stage3_L1) 236 | Mrelu2_stage3_L2 = F.relu(Mconv2_stage3_L2) 237 | Mconv3_stage3_L1_pad = F.pad(Mrelu2_stage3_L1, (3, 3, 3, 3)) 238 | Mconv3_stage3_L1 = self.Mconv3_stage3_L1(Mconv3_stage3_L1_pad) 239 | Mconv3_stage3_L2_pad = F.pad(Mrelu2_stage3_L2, (3, 3, 3, 3)) 240 | Mconv3_stage3_L2 = self.Mconv3_stage3_L2(Mconv3_stage3_L2_pad) 241 | Mrelu3_stage3_L1 = F.relu(Mconv3_stage3_L1) 242 | Mrelu3_stage3_L2 = F.relu(Mconv3_stage3_L2) 243 | Mconv4_stage3_L1_pad = F.pad(Mrelu3_stage3_L1, (3, 3, 3, 3)) 244 | Mconv4_stage3_L1 = self.Mconv4_stage3_L1(Mconv4_stage3_L1_pad) 245 | Mconv4_stage3_L2_pad = F.pad(Mrelu3_stage3_L2, (3, 3, 3, 3)) 246 | Mconv4_stage3_L2 = self.Mconv4_stage3_L2(Mconv4_stage3_L2_pad) 247 | Mrelu4_stage3_L1 = F.relu(Mconv4_stage3_L1) 248 | Mrelu4_stage3_L2 = F.relu(Mconv4_stage3_L2) 249 | Mconv5_stage3_L1_pad = F.pad(Mrelu4_stage3_L1, (3, 3, 3, 3)) 250 | Mconv5_stage3_L1 = self.Mconv5_stage3_L1(Mconv5_stage3_L1_pad) 251 | Mconv5_stage3_L2_pad = F.pad(Mrelu4_stage3_L2, (3, 3, 3, 3)) 252 | Mconv5_stage3_L2 = self.Mconv5_stage3_L2(Mconv5_stage3_L2_pad) 253 | Mrelu5_stage3_L1 = F.relu(Mconv5_stage3_L1) 254 | Mrelu5_stage3_L2 = F.relu(Mconv5_stage3_L2) 255 | Mconv6_stage3_L1 = self.Mconv6_stage3_L1(Mrelu5_stage3_L1) 256 | Mconv6_stage3_L2 = self.Mconv6_stage3_L2(Mrelu5_stage3_L2) 257 | Mrelu6_stage3_L1 = F.relu(Mconv6_stage3_L1) 258 | Mrelu6_stage3_L2 = F.relu(Mconv6_stage3_L2) 259 | Mconv7_stage3_L1 = self.Mconv7_stage3_L1(Mrelu6_stage3_L1) 260 | Mconv7_stage3_L2 = self.Mconv7_stage3_L2(Mrelu6_stage3_L2) 261 | concat_stage4 = torch.cat((Mconv7_stage3_L1, Mconv7_stage3_L2, relu4_4_CPM), 1) 262 | Mconv1_stage4_L1_pad = F.pad(concat_stage4, (3, 3, 3, 3)) 263 | Mconv1_stage4_L1 = self.Mconv1_stage4_L1(Mconv1_stage4_L1_pad) 264 | Mconv1_stage4_L2_pad = F.pad(concat_stage4, (3, 3, 3, 3)) 265 | Mconv1_stage4_L2 = self.Mconv1_stage4_L2(Mconv1_stage4_L2_pad) 266 | Mrelu1_stage4_L1 = F.relu(Mconv1_stage4_L1) 267 | Mrelu1_stage4_L2 = F.relu(Mconv1_stage4_L2) 268 | Mconv2_stage4_L1_pad = F.pad(Mrelu1_stage4_L1, (3, 3, 3, 3)) 269 | Mconv2_stage4_L1 = self.Mconv2_stage4_L1(Mconv2_stage4_L1_pad) 270 | Mconv2_stage4_L2_pad = F.pad(Mrelu1_stage4_L2, (3, 3, 3, 3)) 271 | Mconv2_stage4_L2 = self.Mconv2_stage4_L2(Mconv2_stage4_L2_pad) 272 | Mrelu2_stage4_L1 = F.relu(Mconv2_stage4_L1) 273 | Mrelu2_stage4_L2 = F.relu(Mconv2_stage4_L2) 274 | Mconv3_stage4_L1_pad = F.pad(Mrelu2_stage4_L1, (3, 3, 3, 3)) 275 | Mconv3_stage4_L1 = self.Mconv3_stage4_L1(Mconv3_stage4_L1_pad) 276 | Mconv3_stage4_L2_pad = F.pad(Mrelu2_stage4_L2, (3, 3, 3, 3)) 277 | Mconv3_stage4_L2 = self.Mconv3_stage4_L2(Mconv3_stage4_L2_pad) 278 | Mrelu3_stage4_L1 = F.relu(Mconv3_stage4_L1) 279 | Mrelu3_stage4_L2 = F.relu(Mconv3_stage4_L2) 280 | Mconv4_stage4_L1_pad = F.pad(Mrelu3_stage4_L1, (3, 3, 3, 3)) 281 | Mconv4_stage4_L1 = self.Mconv4_stage4_L1(Mconv4_stage4_L1_pad) 282 | Mconv4_stage4_L2_pad = F.pad(Mrelu3_stage4_L2, (3, 3, 3, 3)) 283 | Mconv4_stage4_L2 = self.Mconv4_stage4_L2(Mconv4_stage4_L2_pad) 284 | Mrelu4_stage4_L1 = F.relu(Mconv4_stage4_L1) 285 | Mrelu4_stage4_L2 = F.relu(Mconv4_stage4_L2) 286 | Mconv5_stage4_L1_pad = F.pad(Mrelu4_stage4_L1, (3, 3, 3, 3)) 287 | Mconv5_stage4_L1 = self.Mconv5_stage4_L1(Mconv5_stage4_L1_pad) 288 | Mconv5_stage4_L2_pad = F.pad(Mrelu4_stage4_L2, (3, 3, 3, 3)) 289 | Mconv5_stage4_L2 = self.Mconv5_stage4_L2(Mconv5_stage4_L2_pad) 290 | Mrelu5_stage4_L1 = F.relu(Mconv5_stage4_L1) 291 | Mrelu5_stage4_L2 = F.relu(Mconv5_stage4_L2) 292 | Mconv6_stage4_L1 = self.Mconv6_stage4_L1(Mrelu5_stage4_L1) 293 | Mconv6_stage4_L2 = self.Mconv6_stage4_L2(Mrelu5_stage4_L2) 294 | Mrelu6_stage4_L1 = F.relu(Mconv6_stage4_L1) 295 | Mrelu6_stage4_L2 = F.relu(Mconv6_stage4_L2) 296 | Mconv7_stage4_L1 = self.Mconv7_stage4_L1(Mrelu6_stage4_L1) 297 | Mconv7_stage4_L2 = self.Mconv7_stage4_L2(Mrelu6_stage4_L2) 298 | concat_stage5 = torch.cat((Mconv7_stage4_L1, Mconv7_stage4_L2, relu4_4_CPM), 1) 299 | Mconv1_stage5_L1_pad = F.pad(concat_stage5, (3, 3, 3, 3)) 300 | Mconv1_stage5_L1 = self.Mconv1_stage5_L1(Mconv1_stage5_L1_pad) 301 | Mconv1_stage5_L2_pad = F.pad(concat_stage5, (3, 3, 3, 3)) 302 | Mconv1_stage5_L2 = self.Mconv1_stage5_L2(Mconv1_stage5_L2_pad) 303 | Mrelu1_stage5_L1 = F.relu(Mconv1_stage5_L1) 304 | Mrelu1_stage5_L2 = F.relu(Mconv1_stage5_L2) 305 | Mconv2_stage5_L1_pad = F.pad(Mrelu1_stage5_L1, (3, 3, 3, 3)) 306 | Mconv2_stage5_L1 = self.Mconv2_stage5_L1(Mconv2_stage5_L1_pad) 307 | Mconv2_stage5_L2_pad = F.pad(Mrelu1_stage5_L2, (3, 3, 3, 3)) 308 | Mconv2_stage5_L2 = self.Mconv2_stage5_L2(Mconv2_stage5_L2_pad) 309 | Mrelu2_stage5_L1 = F.relu(Mconv2_stage5_L1) 310 | Mrelu2_stage5_L2 = F.relu(Mconv2_stage5_L2) 311 | Mconv3_stage5_L1_pad = F.pad(Mrelu2_stage5_L1, (3, 3, 3, 3)) 312 | Mconv3_stage5_L1 = self.Mconv3_stage5_L1(Mconv3_stage5_L1_pad) 313 | Mconv3_stage5_L2_pad = F.pad(Mrelu2_stage5_L2, (3, 3, 3, 3)) 314 | Mconv3_stage5_L2 = self.Mconv3_stage5_L2(Mconv3_stage5_L2_pad) 315 | Mrelu3_stage5_L1 = F.relu(Mconv3_stage5_L1) 316 | Mrelu3_stage5_L2 = F.relu(Mconv3_stage5_L2) 317 | Mconv4_stage5_L1_pad = F.pad(Mrelu3_stage5_L1, (3, 3, 3, 3)) 318 | Mconv4_stage5_L1 = self.Mconv4_stage5_L1(Mconv4_stage5_L1_pad) 319 | Mconv4_stage5_L2_pad = F.pad(Mrelu3_stage5_L2, (3, 3, 3, 3)) 320 | Mconv4_stage5_L2 = self.Mconv4_stage5_L2(Mconv4_stage5_L2_pad) 321 | Mrelu4_stage5_L1 = F.relu(Mconv4_stage5_L1) 322 | Mrelu4_stage5_L2 = F.relu(Mconv4_stage5_L2) 323 | Mconv5_stage5_L1_pad = F.pad(Mrelu4_stage5_L1, (3, 3, 3, 3)) 324 | Mconv5_stage5_L1 = self.Mconv5_stage5_L1(Mconv5_stage5_L1_pad) 325 | Mconv5_stage5_L2_pad = F.pad(Mrelu4_stage5_L2, (3, 3, 3, 3)) 326 | Mconv5_stage5_L2 = self.Mconv5_stage5_L2(Mconv5_stage5_L2_pad) 327 | Mrelu5_stage5_L1 = F.relu(Mconv5_stage5_L1) 328 | Mrelu5_stage5_L2 = F.relu(Mconv5_stage5_L2) 329 | Mconv6_stage5_L1 = self.Mconv6_stage5_L1(Mrelu5_stage5_L1) 330 | Mconv6_stage5_L2 = self.Mconv6_stage5_L2(Mrelu5_stage5_L2) 331 | Mrelu6_stage5_L1 = F.relu(Mconv6_stage5_L1) 332 | Mrelu6_stage5_L2 = F.relu(Mconv6_stage5_L2) 333 | Mconv7_stage5_L1 = self.Mconv7_stage5_L1(Mrelu6_stage5_L1) 334 | Mconv7_stage5_L2 = self.Mconv7_stage5_L2(Mrelu6_stage5_L2) 335 | concat_stage6 = torch.cat((Mconv7_stage5_L1, Mconv7_stage5_L2, relu4_4_CPM), 1) 336 | Mconv1_stage6_L1_pad = F.pad(concat_stage6, (3, 3, 3, 3)) 337 | Mconv1_stage6_L1 = self.Mconv1_stage6_L1(Mconv1_stage6_L1_pad) 338 | Mconv1_stage6_L2_pad = F.pad(concat_stage6, (3, 3, 3, 3)) 339 | Mconv1_stage6_L2 = self.Mconv1_stage6_L2(Mconv1_stage6_L2_pad) 340 | Mrelu1_stage6_L1 = F.relu(Mconv1_stage6_L1) 341 | Mrelu1_stage6_L2 = F.relu(Mconv1_stage6_L2) 342 | Mconv2_stage6_L1_pad = F.pad(Mrelu1_stage6_L1, (3, 3, 3, 3)) 343 | Mconv2_stage6_L1 = self.Mconv2_stage6_L1(Mconv2_stage6_L1_pad) 344 | Mconv2_stage6_L2_pad = F.pad(Mrelu1_stage6_L2, (3, 3, 3, 3)) 345 | Mconv2_stage6_L2 = self.Mconv2_stage6_L2(Mconv2_stage6_L2_pad) 346 | Mrelu2_stage6_L1 = F.relu(Mconv2_stage6_L1) 347 | Mrelu2_stage6_L2 = F.relu(Mconv2_stage6_L2) 348 | Mconv3_stage6_L1_pad = F.pad(Mrelu2_stage6_L1, (3, 3, 3, 3)) 349 | Mconv3_stage6_L1 = self.Mconv3_stage6_L1(Mconv3_stage6_L1_pad) 350 | Mconv3_stage6_L2_pad = F.pad(Mrelu2_stage6_L2, (3, 3, 3, 3)) 351 | Mconv3_stage6_L2 = self.Mconv3_stage6_L2(Mconv3_stage6_L2_pad) 352 | Mrelu3_stage6_L1 = F.relu(Mconv3_stage6_L1) 353 | Mrelu3_stage6_L2 = F.relu(Mconv3_stage6_L2) 354 | Mconv4_stage6_L1_pad = F.pad(Mrelu3_stage6_L1, (3, 3, 3, 3)) 355 | Mconv4_stage6_L1 = self.Mconv4_stage6_L1(Mconv4_stage6_L1_pad) 356 | Mconv4_stage6_L2_pad = F.pad(Mrelu3_stage6_L2, (3, 3, 3, 3)) 357 | Mconv4_stage6_L2 = self.Mconv4_stage6_L2(Mconv4_stage6_L2_pad) 358 | Mrelu4_stage6_L1 = F.relu(Mconv4_stage6_L1) 359 | Mrelu4_stage6_L2 = F.relu(Mconv4_stage6_L2) 360 | Mconv5_stage6_L1_pad = F.pad(Mrelu4_stage6_L1, (3, 3, 3, 3)) 361 | Mconv5_stage6_L1 = self.Mconv5_stage6_L1(Mconv5_stage6_L1_pad) 362 | Mconv5_stage6_L2_pad = F.pad(Mrelu4_stage6_L2, (3, 3, 3, 3)) 363 | Mconv5_stage6_L2 = self.Mconv5_stage6_L2(Mconv5_stage6_L2_pad) 364 | Mrelu5_stage6_L1 = F.relu(Mconv5_stage6_L1) 365 | Mrelu5_stage6_L2 = F.relu(Mconv5_stage6_L2) 366 | Mconv6_stage6_L1 = self.Mconv6_stage6_L1(Mrelu5_stage6_L1) 367 | Mconv6_stage6_L2 = self.Mconv6_stage6_L2(Mrelu5_stage6_L2) 368 | Mrelu6_stage6_L1 = F.relu(Mconv6_stage6_L1) 369 | Mrelu6_stage6_L2 = F.relu(Mconv6_stage6_L2) 370 | Mconv7_stage6_L1 = self.Mconv7_stage6_L1(Mrelu6_stage6_L1) 371 | Mconv7_stage6_L2 = self.Mconv7_stage6_L2(Mrelu6_stage6_L2) 372 | concat_stage7 = torch.cat((Mconv7_stage6_L2, Mconv7_stage6_L1), 1) 373 | return concat_stage7 374 | 375 | 376 | @staticmethod 377 | def __conv(dim, name, **kwargs): 378 | if dim == 1: layer = nn.Conv1d(**kwargs) 379 | elif dim == 2: layer = nn.Conv2d(**kwargs) 380 | elif dim == 3: layer = nn.Conv3d(**kwargs) 381 | else: raise NotImplementedError() 382 | 383 | layer.state_dict()['weight'].copy_(torch.from_numpy(__weights_dict[name]['weights'])) 384 | if 'bias' in __weights_dict[name]: 385 | layer.state_dict()['bias'].copy_(torch.from_numpy(__weights_dict[name]['bias'])) 386 | return layer 387 | 388 | model = KitModel('pose_pytorch.npy') 389 | input_size = (1, 3, 224, 224) 390 | -------------------------------------------------------------------------------- /OpenPose_PyTorch_Models/pose/mpi/pose_linevec_pytorch.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | __weights_dict = dict() 7 | 8 | def load_weights(weight_file): 9 | if weight_file == None: 10 | return 11 | 12 | try: 13 | weights_dict = np.load(weight_file).item() 14 | except: 15 | weights_dict = np.load(weight_file, encoding='bytes').item() 16 | 17 | return weights_dict 18 | 19 | class KitModel(nn.Module): 20 | 21 | 22 | def __init__(self, weight_file): 23 | super(KitModel, self).__init__() 24 | global __weights_dict 25 | __weights_dict = load_weights(weight_file) 26 | 27 | self.conv1_1 = self.__conv(2, name='conv1_1', in_channels=3, out_channels=64, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 28 | self.conv1_2 = self.__conv(2, name='conv1_2', in_channels=64, out_channels=64, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 29 | self.conv2_1 = self.__conv(2, name='conv2_1', in_channels=64, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 30 | self.conv2_2 = self.__conv(2, name='conv2_2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 31 | self.conv3_1 = self.__conv(2, name='conv3_1', in_channels=128, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 32 | self.conv3_2 = self.__conv(2, name='conv3_2', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 33 | self.conv3_3 = self.__conv(2, name='conv3_3', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 34 | self.conv3_4 = self.__conv(2, name='conv3_4', in_channels=256, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 35 | self.conv4_1 = self.__conv(2, name='conv4_1', in_channels=256, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 36 | self.conv4_2 = self.__conv(2, name='conv4_2', in_channels=512, out_channels=512, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 37 | self.conv4_3_CPM = self.__conv(2, name='conv4_3_CPM', in_channels=512, out_channels=256, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 38 | self.conv4_4_CPM = self.__conv(2, name='conv4_4_CPM', in_channels=256, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 39 | self.conv5_1_CPM_L1 = self.__conv(2, name='conv5_1_CPM_L1', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 40 | self.conv5_1_CPM_L2 = self.__conv(2, name='conv5_1_CPM_L2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 41 | self.conv5_2_CPM_L1 = self.__conv(2, name='conv5_2_CPM_L1', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 42 | self.conv5_2_CPM_L2 = self.__conv(2, name='conv5_2_CPM_L2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 43 | self.conv5_3_CPM_L1 = self.__conv(2, name='conv5_3_CPM_L1', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 44 | self.conv5_3_CPM_L2 = self.__conv(2, name='conv5_3_CPM_L2', in_channels=128, out_channels=128, kernel_size=(3, 3), stride=(1, 1), groups=1, bias=True) 45 | self.conv5_4_CPM_L1 = self.__conv(2, name='conv5_4_CPM_L1', in_channels=128, out_channels=512, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 46 | self.conv5_4_CPM_L2 = self.__conv(2, name='conv5_4_CPM_L2', in_channels=128, out_channels=512, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 47 | self.conv5_5_CPM_L1 = self.__conv(2, name='conv5_5_CPM_L1', in_channels=512, out_channels=28, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 48 | self.conv5_5_CPM_L2 = self.__conv(2, name='conv5_5_CPM_L2', in_channels=512, out_channels=16, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 49 | self.Mconv1_stage2_L1 = self.__conv(2, name='Mconv1_stage2_L1', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 50 | self.Mconv1_stage2_L2 = self.__conv(2, name='Mconv1_stage2_L2', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 51 | self.Mconv2_stage2_L1 = self.__conv(2, name='Mconv2_stage2_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 52 | self.Mconv2_stage2_L2 = self.__conv(2, name='Mconv2_stage2_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 53 | self.Mconv3_stage2_L1 = self.__conv(2, name='Mconv3_stage2_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 54 | self.Mconv3_stage2_L2 = self.__conv(2, name='Mconv3_stage2_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 55 | self.Mconv4_stage2_L1 = self.__conv(2, name='Mconv4_stage2_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 56 | self.Mconv4_stage2_L2 = self.__conv(2, name='Mconv4_stage2_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 57 | self.Mconv5_stage2_L1 = self.__conv(2, name='Mconv5_stage2_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 58 | self.Mconv5_stage2_L2 = self.__conv(2, name='Mconv5_stage2_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 59 | self.Mconv6_stage2_L1 = self.__conv(2, name='Mconv6_stage2_L1', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 60 | self.Mconv6_stage2_L2 = self.__conv(2, name='Mconv6_stage2_L2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 61 | self.Mconv7_stage2_L1 = self.__conv(2, name='Mconv7_stage2_L1', in_channels=128, out_channels=28, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 62 | self.Mconv7_stage2_L2 = self.__conv(2, name='Mconv7_stage2_L2', in_channels=128, out_channels=16, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 63 | self.Mconv1_stage3_L1 = self.__conv(2, name='Mconv1_stage3_L1', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 64 | self.Mconv1_stage3_L2 = self.__conv(2, name='Mconv1_stage3_L2', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 65 | self.Mconv2_stage3_L1 = self.__conv(2, name='Mconv2_stage3_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 66 | self.Mconv2_stage3_L2 = self.__conv(2, name='Mconv2_stage3_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 67 | self.Mconv3_stage3_L1 = self.__conv(2, name='Mconv3_stage3_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 68 | self.Mconv3_stage3_L2 = self.__conv(2, name='Mconv3_stage3_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 69 | self.Mconv4_stage3_L1 = self.__conv(2, name='Mconv4_stage3_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 70 | self.Mconv4_stage3_L2 = self.__conv(2, name='Mconv4_stage3_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 71 | self.Mconv5_stage3_L1 = self.__conv(2, name='Mconv5_stage3_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 72 | self.Mconv5_stage3_L2 = self.__conv(2, name='Mconv5_stage3_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 73 | self.Mconv6_stage3_L1 = self.__conv(2, name='Mconv6_stage3_L1', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 74 | self.Mconv6_stage3_L2 = self.__conv(2, name='Mconv6_stage3_L2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 75 | self.Mconv7_stage3_L1 = self.__conv(2, name='Mconv7_stage3_L1', in_channels=128, out_channels=28, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 76 | self.Mconv7_stage3_L2 = self.__conv(2, name='Mconv7_stage3_L2', in_channels=128, out_channels=16, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 77 | self.Mconv1_stage4_L1 = self.__conv(2, name='Mconv1_stage4_L1', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 78 | self.Mconv1_stage4_L2 = self.__conv(2, name='Mconv1_stage4_L2', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 79 | self.Mconv2_stage4_L1 = self.__conv(2, name='Mconv2_stage4_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 80 | self.Mconv2_stage4_L2 = self.__conv(2, name='Mconv2_stage4_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 81 | self.Mconv3_stage4_L1 = self.__conv(2, name='Mconv3_stage4_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 82 | self.Mconv3_stage4_L2 = self.__conv(2, name='Mconv3_stage4_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 83 | self.Mconv4_stage4_L1 = self.__conv(2, name='Mconv4_stage4_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 84 | self.Mconv4_stage4_L2 = self.__conv(2, name='Mconv4_stage4_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 85 | self.Mconv5_stage4_L1 = self.__conv(2, name='Mconv5_stage4_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 86 | self.Mconv5_stage4_L2 = self.__conv(2, name='Mconv5_stage4_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 87 | self.Mconv6_stage4_L1 = self.__conv(2, name='Mconv6_stage4_L1', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 88 | self.Mconv6_stage4_L2 = self.__conv(2, name='Mconv6_stage4_L2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 89 | self.Mconv7_stage4_L1 = self.__conv(2, name='Mconv7_stage4_L1', in_channels=128, out_channels=28, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 90 | self.Mconv7_stage4_L2 = self.__conv(2, name='Mconv7_stage4_L2', in_channels=128, out_channels=16, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 91 | self.Mconv1_stage5_L1 = self.__conv(2, name='Mconv1_stage5_L1', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 92 | self.Mconv1_stage5_L2 = self.__conv(2, name='Mconv1_stage5_L2', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 93 | self.Mconv2_stage5_L1 = self.__conv(2, name='Mconv2_stage5_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 94 | self.Mconv2_stage5_L2 = self.__conv(2, name='Mconv2_stage5_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 95 | self.Mconv3_stage5_L1 = self.__conv(2, name='Mconv3_stage5_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 96 | self.Mconv3_stage5_L2 = self.__conv(2, name='Mconv3_stage5_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 97 | self.Mconv4_stage5_L1 = self.__conv(2, name='Mconv4_stage5_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 98 | self.Mconv4_stage5_L2 = self.__conv(2, name='Mconv4_stage5_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 99 | self.Mconv5_stage5_L1 = self.__conv(2, name='Mconv5_stage5_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 100 | self.Mconv5_stage5_L2 = self.__conv(2, name='Mconv5_stage5_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 101 | self.Mconv6_stage5_L1 = self.__conv(2, name='Mconv6_stage5_L1', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 102 | self.Mconv6_stage5_L2 = self.__conv(2, name='Mconv6_stage5_L2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 103 | self.Mconv7_stage5_L1 = self.__conv(2, name='Mconv7_stage5_L1', in_channels=128, out_channels=28, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 104 | self.Mconv7_stage5_L2 = self.__conv(2, name='Mconv7_stage5_L2', in_channels=128, out_channels=16, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 105 | self.Mconv1_stage6_L1 = self.__conv(2, name='Mconv1_stage6_L1', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 106 | self.Mconv1_stage6_L2 = self.__conv(2, name='Mconv1_stage6_L2', in_channels=172, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 107 | self.Mconv2_stage6_L1 = self.__conv(2, name='Mconv2_stage6_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 108 | self.Mconv2_stage6_L2 = self.__conv(2, name='Mconv2_stage6_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 109 | self.Mconv3_stage6_L1 = self.__conv(2, name='Mconv3_stage6_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 110 | self.Mconv3_stage6_L2 = self.__conv(2, name='Mconv3_stage6_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 111 | self.Mconv4_stage6_L1 = self.__conv(2, name='Mconv4_stage6_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 112 | self.Mconv4_stage6_L2 = self.__conv(2, name='Mconv4_stage6_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 113 | self.Mconv5_stage6_L1 = self.__conv(2, name='Mconv5_stage6_L1', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 114 | self.Mconv5_stage6_L2 = self.__conv(2, name='Mconv5_stage6_L2', in_channels=128, out_channels=128, kernel_size=(7, 7), stride=(1, 1), groups=1, bias=True) 115 | self.Mconv6_stage6_L1 = self.__conv(2, name='Mconv6_stage6_L1', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 116 | self.Mconv6_stage6_L2 = self.__conv(2, name='Mconv6_stage6_L2', in_channels=128, out_channels=128, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 117 | self.Mconv7_stage6_L1 = self.__conv(2, name='Mconv7_stage6_L1', in_channels=128, out_channels=28, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 118 | self.Mconv7_stage6_L2 = self.__conv(2, name='Mconv7_stage6_L2', in_channels=128, out_channels=16, kernel_size=(1, 1), stride=(1, 1), groups=1, bias=True) 119 | 120 | def forward(self, x): 121 | conv1_1_pad = F.pad(x, (1, 1, 1, 1)) 122 | conv1_1 = self.conv1_1(conv1_1_pad) 123 | relu1_1 = F.relu(conv1_1) 124 | conv1_2_pad = F.pad(relu1_1, (1, 1, 1, 1)) 125 | conv1_2 = self.conv1_2(conv1_2_pad) 126 | relu1_2 = F.relu(conv1_2) 127 | pool1_stage1_pad = F.pad(relu1_2, (0, 1, 0, 1), value=float('-inf')) 128 | pool1_stage1 = F.max_pool2d(pool1_stage1_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 129 | conv2_1_pad = F.pad(pool1_stage1, (1, 1, 1, 1)) 130 | conv2_1 = self.conv2_1(conv2_1_pad) 131 | relu2_1 = F.relu(conv2_1) 132 | conv2_2_pad = F.pad(relu2_1, (1, 1, 1, 1)) 133 | conv2_2 = self.conv2_2(conv2_2_pad) 134 | relu2_2 = F.relu(conv2_2) 135 | pool2_stage1_pad = F.pad(relu2_2, (0, 1, 0, 1), value=float('-inf')) 136 | pool2_stage1 = F.max_pool2d(pool2_stage1_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 137 | conv3_1_pad = F.pad(pool2_stage1, (1, 1, 1, 1)) 138 | conv3_1 = self.conv3_1(conv3_1_pad) 139 | relu3_1 = F.relu(conv3_1) 140 | conv3_2_pad = F.pad(relu3_1, (1, 1, 1, 1)) 141 | conv3_2 = self.conv3_2(conv3_2_pad) 142 | relu3_2 = F.relu(conv3_2) 143 | conv3_3_pad = F.pad(relu3_2, (1, 1, 1, 1)) 144 | conv3_3 = self.conv3_3(conv3_3_pad) 145 | relu3_3 = F.relu(conv3_3) 146 | conv3_4_pad = F.pad(relu3_3, (1, 1, 1, 1)) 147 | conv3_4 = self.conv3_4(conv3_4_pad) 148 | relu3_4 = F.relu(conv3_4) 149 | pool3_stage1_pad = F.pad(relu3_4, (0, 1, 0, 1), value=float('-inf')) 150 | pool3_stage1 = F.max_pool2d(pool3_stage1_pad, kernel_size=(2, 2), stride=(2, 2), padding=0, ceil_mode=False) 151 | conv4_1_pad = F.pad(pool3_stage1, (1, 1, 1, 1)) 152 | conv4_1 = self.conv4_1(conv4_1_pad) 153 | relu4_1 = F.relu(conv4_1) 154 | conv4_2_pad = F.pad(relu4_1, (1, 1, 1, 1)) 155 | conv4_2 = self.conv4_2(conv4_2_pad) 156 | relu4_2 = F.relu(conv4_2) 157 | conv4_3_CPM_pad = F.pad(relu4_2, (1, 1, 1, 1)) 158 | conv4_3_CPM = self.conv4_3_CPM(conv4_3_CPM_pad) 159 | relu4_3_CPM = F.relu(conv4_3_CPM) 160 | conv4_4_CPM_pad = F.pad(relu4_3_CPM, (1, 1, 1, 1)) 161 | conv4_4_CPM = self.conv4_4_CPM(conv4_4_CPM_pad) 162 | relu4_4_CPM = F.relu(conv4_4_CPM) 163 | conv5_1_CPM_L1_pad = F.pad(relu4_4_CPM, (1, 1, 1, 1)) 164 | conv5_1_CPM_L1 = self.conv5_1_CPM_L1(conv5_1_CPM_L1_pad) 165 | conv5_1_CPM_L2_pad = F.pad(relu4_4_CPM, (1, 1, 1, 1)) 166 | conv5_1_CPM_L2 = self.conv5_1_CPM_L2(conv5_1_CPM_L2_pad) 167 | relu5_1_CPM_L1 = F.relu(conv5_1_CPM_L1) 168 | relu5_1_CPM_L2 = F.relu(conv5_1_CPM_L2) 169 | conv5_2_CPM_L1_pad = F.pad(relu5_1_CPM_L1, (1, 1, 1, 1)) 170 | conv5_2_CPM_L1 = self.conv5_2_CPM_L1(conv5_2_CPM_L1_pad) 171 | conv5_2_CPM_L2_pad = F.pad(relu5_1_CPM_L2, (1, 1, 1, 1)) 172 | conv5_2_CPM_L2 = self.conv5_2_CPM_L2(conv5_2_CPM_L2_pad) 173 | relu5_2_CPM_L1 = F.relu(conv5_2_CPM_L1) 174 | relu5_2_CPM_L2 = F.relu(conv5_2_CPM_L2) 175 | conv5_3_CPM_L1_pad = F.pad(relu5_2_CPM_L1, (1, 1, 1, 1)) 176 | conv5_3_CPM_L1 = self.conv5_3_CPM_L1(conv5_3_CPM_L1_pad) 177 | conv5_3_CPM_L2_pad = F.pad(relu5_2_CPM_L2, (1, 1, 1, 1)) 178 | conv5_3_CPM_L2 = self.conv5_3_CPM_L2(conv5_3_CPM_L2_pad) 179 | relu5_3_CPM_L1 = F.relu(conv5_3_CPM_L1) 180 | relu5_3_CPM_L2 = F.relu(conv5_3_CPM_L2) 181 | conv5_4_CPM_L1 = self.conv5_4_CPM_L1(relu5_3_CPM_L1) 182 | conv5_4_CPM_L2 = self.conv5_4_CPM_L2(relu5_3_CPM_L2) 183 | relu5_4_CPM_L1 = F.relu(conv5_4_CPM_L1) 184 | relu5_4_CPM_L2 = F.relu(conv5_4_CPM_L2) 185 | conv5_5_CPM_L1 = self.conv5_5_CPM_L1(relu5_4_CPM_L1) 186 | conv5_5_CPM_L2 = self.conv5_5_CPM_L2(relu5_4_CPM_L2) 187 | concat_stage2 = torch.cat((conv5_5_CPM_L1, conv5_5_CPM_L2, relu4_4_CPM), 1) 188 | Mconv1_stage2_L1_pad = F.pad(concat_stage2, (3, 3, 3, 3)) 189 | Mconv1_stage2_L1 = self.Mconv1_stage2_L1(Mconv1_stage2_L1_pad) 190 | Mconv1_stage2_L2_pad = F.pad(concat_stage2, (3, 3, 3, 3)) 191 | Mconv1_stage2_L2 = self.Mconv1_stage2_L2(Mconv1_stage2_L2_pad) 192 | Mrelu1_stage2_L1 = F.relu(Mconv1_stage2_L1) 193 | Mrelu1_stage2_L2 = F.relu(Mconv1_stage2_L2) 194 | Mconv2_stage2_L1_pad = F.pad(Mrelu1_stage2_L1, (3, 3, 3, 3)) 195 | Mconv2_stage2_L1 = self.Mconv2_stage2_L1(Mconv2_stage2_L1_pad) 196 | Mconv2_stage2_L2_pad = F.pad(Mrelu1_stage2_L2, (3, 3, 3, 3)) 197 | Mconv2_stage2_L2 = self.Mconv2_stage2_L2(Mconv2_stage2_L2_pad) 198 | Mrelu2_stage2_L1 = F.relu(Mconv2_stage2_L1) 199 | Mrelu2_stage2_L2 = F.relu(Mconv2_stage2_L2) 200 | Mconv3_stage2_L1_pad = F.pad(Mrelu2_stage2_L1, (3, 3, 3, 3)) 201 | Mconv3_stage2_L1 = self.Mconv3_stage2_L1(Mconv3_stage2_L1_pad) 202 | Mconv3_stage2_L2_pad = F.pad(Mrelu2_stage2_L2, (3, 3, 3, 3)) 203 | Mconv3_stage2_L2 = self.Mconv3_stage2_L2(Mconv3_stage2_L2_pad) 204 | Mrelu3_stage2_L1 = F.relu(Mconv3_stage2_L1) 205 | Mrelu3_stage2_L2 = F.relu(Mconv3_stage2_L2) 206 | Mconv4_stage2_L1_pad = F.pad(Mrelu3_stage2_L1, (3, 3, 3, 3)) 207 | Mconv4_stage2_L1 = self.Mconv4_stage2_L1(Mconv4_stage2_L1_pad) 208 | Mconv4_stage2_L2_pad = F.pad(Mrelu3_stage2_L2, (3, 3, 3, 3)) 209 | Mconv4_stage2_L2 = self.Mconv4_stage2_L2(Mconv4_stage2_L2_pad) 210 | Mrelu4_stage2_L1 = F.relu(Mconv4_stage2_L1) 211 | Mrelu4_stage2_L2 = F.relu(Mconv4_stage2_L2) 212 | Mconv5_stage2_L1_pad = F.pad(Mrelu4_stage2_L1, (3, 3, 3, 3)) 213 | Mconv5_stage2_L1 = self.Mconv5_stage2_L1(Mconv5_stage2_L1_pad) 214 | Mconv5_stage2_L2_pad = F.pad(Mrelu4_stage2_L2, (3, 3, 3, 3)) 215 | Mconv5_stage2_L2 = self.Mconv5_stage2_L2(Mconv5_stage2_L2_pad) 216 | Mrelu5_stage2_L1 = F.relu(Mconv5_stage2_L1) 217 | Mrelu5_stage2_L2 = F.relu(Mconv5_stage2_L2) 218 | Mconv6_stage2_L1 = self.Mconv6_stage2_L1(Mrelu5_stage2_L1) 219 | Mconv6_stage2_L2 = self.Mconv6_stage2_L2(Mrelu5_stage2_L2) 220 | Mrelu6_stage2_L1 = F.relu(Mconv6_stage2_L1) 221 | Mrelu6_stage2_L2 = F.relu(Mconv6_stage2_L2) 222 | Mconv7_stage2_L1 = self.Mconv7_stage2_L1(Mrelu6_stage2_L1) 223 | Mconv7_stage2_L2 = self.Mconv7_stage2_L2(Mrelu6_stage2_L2) 224 | concat_stage3 = torch.cat((Mconv7_stage2_L1, Mconv7_stage2_L2, relu4_4_CPM), 1) 225 | Mconv1_stage3_L1_pad = F.pad(concat_stage3, (3, 3, 3, 3)) 226 | Mconv1_stage3_L1 = self.Mconv1_stage3_L1(Mconv1_stage3_L1_pad) 227 | Mconv1_stage3_L2_pad = F.pad(concat_stage3, (3, 3, 3, 3)) 228 | Mconv1_stage3_L2 = self.Mconv1_stage3_L2(Mconv1_stage3_L2_pad) 229 | Mrelu1_stage3_L1 = F.relu(Mconv1_stage3_L1) 230 | Mrelu1_stage3_L2 = F.relu(Mconv1_stage3_L2) 231 | Mconv2_stage3_L1_pad = F.pad(Mrelu1_stage3_L1, (3, 3, 3, 3)) 232 | Mconv2_stage3_L1 = self.Mconv2_stage3_L1(Mconv2_stage3_L1_pad) 233 | Mconv2_stage3_L2_pad = F.pad(Mrelu1_stage3_L2, (3, 3, 3, 3)) 234 | Mconv2_stage3_L2 = self.Mconv2_stage3_L2(Mconv2_stage3_L2_pad) 235 | Mrelu2_stage3_L1 = F.relu(Mconv2_stage3_L1) 236 | Mrelu2_stage3_L2 = F.relu(Mconv2_stage3_L2) 237 | Mconv3_stage3_L1_pad = F.pad(Mrelu2_stage3_L1, (3, 3, 3, 3)) 238 | Mconv3_stage3_L1 = self.Mconv3_stage3_L1(Mconv3_stage3_L1_pad) 239 | Mconv3_stage3_L2_pad = F.pad(Mrelu2_stage3_L2, (3, 3, 3, 3)) 240 | Mconv3_stage3_L2 = self.Mconv3_stage3_L2(Mconv3_stage3_L2_pad) 241 | Mrelu3_stage3_L1 = F.relu(Mconv3_stage3_L1) 242 | Mrelu3_stage3_L2 = F.relu(Mconv3_stage3_L2) 243 | Mconv4_stage3_L1_pad = F.pad(Mrelu3_stage3_L1, (3, 3, 3, 3)) 244 | Mconv4_stage3_L1 = self.Mconv4_stage3_L1(Mconv4_stage3_L1_pad) 245 | Mconv4_stage3_L2_pad = F.pad(Mrelu3_stage3_L2, (3, 3, 3, 3)) 246 | Mconv4_stage3_L2 = self.Mconv4_stage3_L2(Mconv4_stage3_L2_pad) 247 | Mrelu4_stage3_L1 = F.relu(Mconv4_stage3_L1) 248 | Mrelu4_stage3_L2 = F.relu(Mconv4_stage3_L2) 249 | Mconv5_stage3_L1_pad = F.pad(Mrelu4_stage3_L1, (3, 3, 3, 3)) 250 | Mconv5_stage3_L1 = self.Mconv5_stage3_L1(Mconv5_stage3_L1_pad) 251 | Mconv5_stage3_L2_pad = F.pad(Mrelu4_stage3_L2, (3, 3, 3, 3)) 252 | Mconv5_stage3_L2 = self.Mconv5_stage3_L2(Mconv5_stage3_L2_pad) 253 | Mrelu5_stage3_L1 = F.relu(Mconv5_stage3_L1) 254 | Mrelu5_stage3_L2 = F.relu(Mconv5_stage3_L2) 255 | Mconv6_stage3_L1 = self.Mconv6_stage3_L1(Mrelu5_stage3_L1) 256 | Mconv6_stage3_L2 = self.Mconv6_stage3_L2(Mrelu5_stage3_L2) 257 | Mrelu6_stage3_L1 = F.relu(Mconv6_stage3_L1) 258 | Mrelu6_stage3_L2 = F.relu(Mconv6_stage3_L2) 259 | Mconv7_stage3_L1 = self.Mconv7_stage3_L1(Mrelu6_stage3_L1) 260 | Mconv7_stage3_L2 = self.Mconv7_stage3_L2(Mrelu6_stage3_L2) 261 | concat_stage4 = torch.cat((Mconv7_stage3_L1, Mconv7_stage3_L2, relu4_4_CPM), 1) 262 | Mconv1_stage4_L1_pad = F.pad(concat_stage4, (3, 3, 3, 3)) 263 | Mconv1_stage4_L1 = self.Mconv1_stage4_L1(Mconv1_stage4_L1_pad) 264 | Mconv1_stage4_L2_pad = F.pad(concat_stage4, (3, 3, 3, 3)) 265 | Mconv1_stage4_L2 = self.Mconv1_stage4_L2(Mconv1_stage4_L2_pad) 266 | Mrelu1_stage4_L1 = F.relu(Mconv1_stage4_L1) 267 | Mrelu1_stage4_L2 = F.relu(Mconv1_stage4_L2) 268 | Mconv2_stage4_L1_pad = F.pad(Mrelu1_stage4_L1, (3, 3, 3, 3)) 269 | Mconv2_stage4_L1 = self.Mconv2_stage4_L1(Mconv2_stage4_L1_pad) 270 | Mconv2_stage4_L2_pad = F.pad(Mrelu1_stage4_L2, (3, 3, 3, 3)) 271 | Mconv2_stage4_L2 = self.Mconv2_stage4_L2(Mconv2_stage4_L2_pad) 272 | Mrelu2_stage4_L1 = F.relu(Mconv2_stage4_L1) 273 | Mrelu2_stage4_L2 = F.relu(Mconv2_stage4_L2) 274 | Mconv3_stage4_L1_pad = F.pad(Mrelu2_stage4_L1, (3, 3, 3, 3)) 275 | Mconv3_stage4_L1 = self.Mconv3_stage4_L1(Mconv3_stage4_L1_pad) 276 | Mconv3_stage4_L2_pad = F.pad(Mrelu2_stage4_L2, (3, 3, 3, 3)) 277 | Mconv3_stage4_L2 = self.Mconv3_stage4_L2(Mconv3_stage4_L2_pad) 278 | Mrelu3_stage4_L1 = F.relu(Mconv3_stage4_L1) 279 | Mrelu3_stage4_L2 = F.relu(Mconv3_stage4_L2) 280 | Mconv4_stage4_L1_pad = F.pad(Mrelu3_stage4_L1, (3, 3, 3, 3)) 281 | Mconv4_stage4_L1 = self.Mconv4_stage4_L1(Mconv4_stage4_L1_pad) 282 | Mconv4_stage4_L2_pad = F.pad(Mrelu3_stage4_L2, (3, 3, 3, 3)) 283 | Mconv4_stage4_L2 = self.Mconv4_stage4_L2(Mconv4_stage4_L2_pad) 284 | Mrelu4_stage4_L1 = F.relu(Mconv4_stage4_L1) 285 | Mrelu4_stage4_L2 = F.relu(Mconv4_stage4_L2) 286 | Mconv5_stage4_L1_pad = F.pad(Mrelu4_stage4_L1, (3, 3, 3, 3)) 287 | Mconv5_stage4_L1 = self.Mconv5_stage4_L1(Mconv5_stage4_L1_pad) 288 | Mconv5_stage4_L2_pad = F.pad(Mrelu4_stage4_L2, (3, 3, 3, 3)) 289 | Mconv5_stage4_L2 = self.Mconv5_stage4_L2(Mconv5_stage4_L2_pad) 290 | Mrelu5_stage4_L1 = F.relu(Mconv5_stage4_L1) 291 | Mrelu5_stage4_L2 = F.relu(Mconv5_stage4_L2) 292 | Mconv6_stage4_L1 = self.Mconv6_stage4_L1(Mrelu5_stage4_L1) 293 | Mconv6_stage4_L2 = self.Mconv6_stage4_L2(Mrelu5_stage4_L2) 294 | Mrelu6_stage4_L1 = F.relu(Mconv6_stage4_L1) 295 | Mrelu6_stage4_L2 = F.relu(Mconv6_stage4_L2) 296 | Mconv7_stage4_L1 = self.Mconv7_stage4_L1(Mrelu6_stage4_L1) 297 | Mconv7_stage4_L2 = self.Mconv7_stage4_L2(Mrelu6_stage4_L2) 298 | concat_stage5 = torch.cat((Mconv7_stage4_L1, Mconv7_stage4_L2, relu4_4_CPM), 1) 299 | Mconv1_stage5_L1_pad = F.pad(concat_stage5, (3, 3, 3, 3)) 300 | Mconv1_stage5_L1 = self.Mconv1_stage5_L1(Mconv1_stage5_L1_pad) 301 | Mconv1_stage5_L2_pad = F.pad(concat_stage5, (3, 3, 3, 3)) 302 | Mconv1_stage5_L2 = self.Mconv1_stage5_L2(Mconv1_stage5_L2_pad) 303 | Mrelu1_stage5_L1 = F.relu(Mconv1_stage5_L1) 304 | Mrelu1_stage5_L2 = F.relu(Mconv1_stage5_L2) 305 | Mconv2_stage5_L1_pad = F.pad(Mrelu1_stage5_L1, (3, 3, 3, 3)) 306 | Mconv2_stage5_L1 = self.Mconv2_stage5_L1(Mconv2_stage5_L1_pad) 307 | Mconv2_stage5_L2_pad = F.pad(Mrelu1_stage5_L2, (3, 3, 3, 3)) 308 | Mconv2_stage5_L2 = self.Mconv2_stage5_L2(Mconv2_stage5_L2_pad) 309 | Mrelu2_stage5_L1 = F.relu(Mconv2_stage5_L1) 310 | Mrelu2_stage5_L2 = F.relu(Mconv2_stage5_L2) 311 | Mconv3_stage5_L1_pad = F.pad(Mrelu2_stage5_L1, (3, 3, 3, 3)) 312 | Mconv3_stage5_L1 = self.Mconv3_stage5_L1(Mconv3_stage5_L1_pad) 313 | Mconv3_stage5_L2_pad = F.pad(Mrelu2_stage5_L2, (3, 3, 3, 3)) 314 | Mconv3_stage5_L2 = self.Mconv3_stage5_L2(Mconv3_stage5_L2_pad) 315 | Mrelu3_stage5_L1 = F.relu(Mconv3_stage5_L1) 316 | Mrelu3_stage5_L2 = F.relu(Mconv3_stage5_L2) 317 | Mconv4_stage5_L1_pad = F.pad(Mrelu3_stage5_L1, (3, 3, 3, 3)) 318 | Mconv4_stage5_L1 = self.Mconv4_stage5_L1(Mconv4_stage5_L1_pad) 319 | Mconv4_stage5_L2_pad = F.pad(Mrelu3_stage5_L2, (3, 3, 3, 3)) 320 | Mconv4_stage5_L2 = self.Mconv4_stage5_L2(Mconv4_stage5_L2_pad) 321 | Mrelu4_stage5_L1 = F.relu(Mconv4_stage5_L1) 322 | Mrelu4_stage5_L2 = F.relu(Mconv4_stage5_L2) 323 | Mconv5_stage5_L1_pad = F.pad(Mrelu4_stage5_L1, (3, 3, 3, 3)) 324 | Mconv5_stage5_L1 = self.Mconv5_stage5_L1(Mconv5_stage5_L1_pad) 325 | Mconv5_stage5_L2_pad = F.pad(Mrelu4_stage5_L2, (3, 3, 3, 3)) 326 | Mconv5_stage5_L2 = self.Mconv5_stage5_L2(Mconv5_stage5_L2_pad) 327 | Mrelu5_stage5_L1 = F.relu(Mconv5_stage5_L1) 328 | Mrelu5_stage5_L2 = F.relu(Mconv5_stage5_L2) 329 | Mconv6_stage5_L1 = self.Mconv6_stage5_L1(Mrelu5_stage5_L1) 330 | Mconv6_stage5_L2 = self.Mconv6_stage5_L2(Mrelu5_stage5_L2) 331 | Mrelu6_stage5_L1 = F.relu(Mconv6_stage5_L1) 332 | Mrelu6_stage5_L2 = F.relu(Mconv6_stage5_L2) 333 | Mconv7_stage5_L1 = self.Mconv7_stage5_L1(Mrelu6_stage5_L1) 334 | Mconv7_stage5_L2 = self.Mconv7_stage5_L2(Mrelu6_stage5_L2) 335 | concat_stage6 = torch.cat((Mconv7_stage5_L1, Mconv7_stage5_L2, relu4_4_CPM), 1) 336 | Mconv1_stage6_L1_pad = F.pad(concat_stage6, (3, 3, 3, 3)) 337 | Mconv1_stage6_L1 = self.Mconv1_stage6_L1(Mconv1_stage6_L1_pad) 338 | Mconv1_stage6_L2_pad = F.pad(concat_stage6, (3, 3, 3, 3)) 339 | Mconv1_stage6_L2 = self.Mconv1_stage6_L2(Mconv1_stage6_L2_pad) 340 | Mrelu1_stage6_L1 = F.relu(Mconv1_stage6_L1) 341 | Mrelu1_stage6_L2 = F.relu(Mconv1_stage6_L2) 342 | Mconv2_stage6_L1_pad = F.pad(Mrelu1_stage6_L1, (3, 3, 3, 3)) 343 | Mconv2_stage6_L1 = self.Mconv2_stage6_L1(Mconv2_stage6_L1_pad) 344 | Mconv2_stage6_L2_pad = F.pad(Mrelu1_stage6_L2, (3, 3, 3, 3)) 345 | Mconv2_stage6_L2 = self.Mconv2_stage6_L2(Mconv2_stage6_L2_pad) 346 | Mrelu2_stage6_L1 = F.relu(Mconv2_stage6_L1) 347 | Mrelu2_stage6_L2 = F.relu(Mconv2_stage6_L2) 348 | Mconv3_stage6_L1_pad = F.pad(Mrelu2_stage6_L1, (3, 3, 3, 3)) 349 | Mconv3_stage6_L1 = self.Mconv3_stage6_L1(Mconv3_stage6_L1_pad) 350 | Mconv3_stage6_L2_pad = F.pad(Mrelu2_stage6_L2, (3, 3, 3, 3)) 351 | Mconv3_stage6_L2 = self.Mconv3_stage6_L2(Mconv3_stage6_L2_pad) 352 | Mrelu3_stage6_L1 = F.relu(Mconv3_stage6_L1) 353 | Mrelu3_stage6_L2 = F.relu(Mconv3_stage6_L2) 354 | Mconv4_stage6_L1_pad = F.pad(Mrelu3_stage6_L1, (3, 3, 3, 3)) 355 | Mconv4_stage6_L1 = self.Mconv4_stage6_L1(Mconv4_stage6_L1_pad) 356 | Mconv4_stage6_L2_pad = F.pad(Mrelu3_stage6_L2, (3, 3, 3, 3)) 357 | Mconv4_stage6_L2 = self.Mconv4_stage6_L2(Mconv4_stage6_L2_pad) 358 | Mrelu4_stage6_L1 = F.relu(Mconv4_stage6_L1) 359 | Mrelu4_stage6_L2 = F.relu(Mconv4_stage6_L2) 360 | Mconv5_stage6_L1_pad = F.pad(Mrelu4_stage6_L1, (3, 3, 3, 3)) 361 | Mconv5_stage6_L1 = self.Mconv5_stage6_L1(Mconv5_stage6_L1_pad) 362 | Mconv5_stage6_L2_pad = F.pad(Mrelu4_stage6_L2, (3, 3, 3, 3)) 363 | Mconv5_stage6_L2 = self.Mconv5_stage6_L2(Mconv5_stage6_L2_pad) 364 | Mrelu5_stage6_L1 = F.relu(Mconv5_stage6_L1) 365 | Mrelu5_stage6_L2 = F.relu(Mconv5_stage6_L2) 366 | Mconv6_stage6_L1 = self.Mconv6_stage6_L1(Mrelu5_stage6_L1) 367 | Mconv6_stage6_L2 = self.Mconv6_stage6_L2(Mrelu5_stage6_L2) 368 | Mrelu6_stage6_L1 = F.relu(Mconv6_stage6_L1) 369 | Mrelu6_stage6_L2 = F.relu(Mconv6_stage6_L2) 370 | Mconv7_stage6_L1 = self.Mconv7_stage6_L1(Mrelu6_stage6_L1) 371 | Mconv7_stage6_L2 = self.Mconv7_stage6_L2(Mrelu6_stage6_L2) 372 | concat_stage7 = torch.cat((Mconv7_stage6_L2, Mconv7_stage6_L1), 1) 373 | return concat_stage7 374 | 375 | 376 | @staticmethod 377 | def __conv(dim, name, **kwargs): 378 | if dim == 1: layer = nn.Conv1d(**kwargs) 379 | elif dim == 2: layer = nn.Conv2d(**kwargs) 380 | elif dim == 3: layer = nn.Conv3d(**kwargs) 381 | else: raise NotImplementedError() 382 | 383 | layer.state_dict()['weight'].copy_(torch.from_numpy(__weights_dict[name]['weights'])) 384 | if 'bias' in __weights_dict[name]: 385 | layer.state_dict()['bias'].copy_(torch.from_numpy(__weights_dict[name]['bias'])) 386 | return layer 387 | 388 | model = KitModel('pose_pytorch.npy') 389 | input_size = (1, 3, 224, 224) 390 | --------------------------------------------------------------------------------