00001 import os
00002 import chainer
00003 import chainer.functions as F
00004 import chainer.links as L
00005
00006 base_url = 'http://posefs1.perception.cs.cmu.edu/OpenPose/models/pose/'
00007 models = {
00008 'auto': 'coco/pose_iter_440000.chainermodel',
00009 'coco': 'coco/pose_iter_440000.chainermodel',
00010 'mpi': 'mpi/pose_iter_160000.chainermodel',
00011 }
00012
00013
00014 class PoseNet(chainer.Chain):
00015
00016 def __init__(self, pretrained_model='auto'):
00017 super(PoseNet, self).__init__()
00018 with self.init_scope():
00019 self.conv1_1 = L.Convolution2D(
00020 in_channels=3, out_channels=64, ksize=3, stride=1, pad=1)
00021 self.conv1_2 = L.Convolution2D(
00022 in_channels=64, out_channels=64, ksize=3, stride=1, pad=1)
00023 self.conv2_1 = L.Convolution2D(
00024 in_channels=64, out_channels=128, ksize=3, stride=1, pad=1)
00025 self.conv2_2 = L.Convolution2D(
00026 in_channels=128, out_channels=128, ksize=3, stride=1, pad=1)
00027 self.conv3_1 = L.Convolution2D(
00028 in_channels=128, out_channels=256, ksize=3, stride=1, pad=1)
00029 self.conv3_2 = L.Convolution2D(
00030 in_channels=256, out_channels=256, ksize=3, stride=1, pad=1)
00031 self.conv3_3 = L.Convolution2D(
00032 in_channels=256, out_channels=256, ksize=3, stride=1, pad=1)
00033 self.conv3_4 = L.Convolution2D(
00034 in_channels=256, out_channels=256, ksize=3, stride=1, pad=1)
00035 self.conv4_1 = L.Convolution2D(
00036 in_channels=256, out_channels=512, ksize=3, stride=1, pad=1)
00037 self.conv4_2 = L.Convolution2D(
00038 in_channels=512, out_channels=512, ksize=3, stride=1, pad=1)
00039 self.conv4_3_CPM = L.Convolution2D(
00040 in_channels=512, out_channels=256, ksize=3, stride=1, pad=1)
00041 self.conv4_4_CPM = L.Convolution2D(
00042 in_channels=256, out_channels=128, ksize=3, stride=1, pad=1)
00043
00044
00045 self.conv5_1_CPM_L1 = L.Convolution2D(
00046 in_channels=128, out_channels=128, ksize=3, stride=1, pad=1)
00047 self.conv5_2_CPM_L1 = L.Convolution2D(
00048 in_channels=128, out_channels=128, ksize=3, stride=1, pad=1)
00049 self.conv5_3_CPM_L1 = L.Convolution2D(
00050 in_channels=128, out_channels=128, ksize=3, stride=1, pad=1)
00051 self.conv5_4_CPM_L1 = L.Convolution2D(
00052 in_channels=128, out_channels=512, ksize=1, stride=1, pad=0)
00053 self.conv5_5_CPM_L1 = L.Convolution2D(
00054 in_channels=512, out_channels=38, ksize=1, stride=1, pad=0)
00055 self.conv5_1_CPM_L2 = L.Convolution2D(
00056 in_channels=128, out_channels=128, ksize=3, stride=1, pad=1)
00057 self.conv5_2_CPM_L2 = L.Convolution2D(
00058 in_channels=128, out_channels=128, ksize=3, stride=1, pad=1)
00059 self.conv5_3_CPM_L2 = L.Convolution2D(
00060 in_channels=128, out_channels=128, ksize=3, stride=1, pad=1)
00061 self.conv5_4_CPM_L2 = L.Convolution2D(
00062 in_channels=128, out_channels=512, ksize=1, stride=1, pad=0)
00063 self.conv5_5_CPM_L2 = L.Convolution2D(
00064 in_channels=512, out_channels=19, ksize=1, stride=1, pad=0)
00065
00066
00067 self.Mconv1_stage2_L1 = L.Convolution2D(
00068 in_channels=185, out_channels=128, ksize=7, stride=1, pad=3)
00069 self.Mconv2_stage2_L1 = L.Convolution2D(
00070 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00071 self.Mconv3_stage2_L1 = L.Convolution2D(
00072 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00073 self.Mconv4_stage2_L1 = L.Convolution2D(
00074 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00075 self.Mconv5_stage2_L1 = L.Convolution2D(
00076 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00077 self.Mconv6_stage2_L1 = L.Convolution2D(
00078 in_channels=128, out_channels=128, ksize=1, stride=1, pad=0)
00079 self.Mconv7_stage2_L1 = L.Convolution2D(
00080 in_channels=128, out_channels=38, ksize=1, stride=1, pad=0)
00081 self.Mconv1_stage2_L2 = L.Convolution2D(
00082 in_channels=185, out_channels=128, ksize=7, stride=1, pad=3)
00083 self.Mconv2_stage2_L2 = L.Convolution2D(
00084 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00085 self.Mconv3_stage2_L2 = L.Convolution2D(
00086 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00087 self.Mconv4_stage2_L2 = L.Convolution2D(
00088 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00089 self.Mconv5_stage2_L2 = L.Convolution2D(
00090 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00091 self.Mconv6_stage2_L2 = L.Convolution2D(
00092 in_channels=128, out_channels=128, ksize=1, stride=1, pad=0)
00093 self.Mconv7_stage2_L2 = L.Convolution2D(
00094 in_channels=128, out_channels=19, ksize=1, stride=1, pad=0)
00095
00096
00097 self.Mconv1_stage3_L1 = L.Convolution2D(
00098 in_channels=185, out_channels=128, ksize=7, stride=1, pad=3)
00099 self.Mconv2_stage3_L1 = L.Convolution2D(
00100 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00101 self.Mconv3_stage3_L1 = L.Convolution2D(
00102 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00103 self.Mconv4_stage3_L1 = L.Convolution2D(
00104 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00105 self.Mconv5_stage3_L1 = L.Convolution2D(
00106 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00107 self.Mconv6_stage3_L1 = L.Convolution2D(
00108 in_channels=128, out_channels=128, ksize=1, stride=1, pad=0)
00109 self.Mconv7_stage3_L1 = L.Convolution2D(
00110 in_channels=128, out_channels=38, ksize=1, stride=1, pad=0)
00111 self.Mconv1_stage3_L2 = L.Convolution2D(
00112 in_channels=185, out_channels=128, ksize=7, stride=1, pad=3)
00113 self.Mconv2_stage3_L2 = L.Convolution2D(
00114 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00115 self.Mconv3_stage3_L2 = L.Convolution2D(
00116 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00117 self.Mconv4_stage3_L2 = L.Convolution2D(
00118 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00119 self.Mconv5_stage3_L2 = L.Convolution2D(
00120 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00121 self.Mconv6_stage3_L2 = L.Convolution2D(
00122 in_channels=128, out_channels=128, ksize=1, stride=1, pad=0)
00123 self.Mconv7_stage3_L2 = L.Convolution2D(
00124 in_channels=128, out_channels=19, ksize=1, stride=1, pad=0)
00125
00126
00127 self.Mconv1_stage4_L1 = L.Convolution2D(
00128 in_channels=185, out_channels=128, ksize=7, stride=1, pad=3)
00129 self.Mconv2_stage4_L1 = L.Convolution2D(
00130 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00131 self.Mconv3_stage4_L1 = L.Convolution2D(
00132 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00133 self.Mconv4_stage4_L1 = L.Convolution2D(
00134 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00135 self.Mconv5_stage4_L1 = L.Convolution2D(
00136 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00137 self.Mconv6_stage4_L1 = L.Convolution2D(
00138 in_channels=128, out_channels=128, ksize=1, stride=1, pad=0)
00139 self.Mconv7_stage4_L1 = L.Convolution2D(
00140 in_channels=128, out_channels=38, ksize=1, stride=1, pad=0)
00141 self.Mconv1_stage4_L2 = L.Convolution2D(
00142 in_channels=185, out_channels=128, ksize=7, stride=1, pad=3)
00143 self.Mconv2_stage4_L2 = L.Convolution2D(
00144 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00145 self.Mconv3_stage4_L2 = L.Convolution2D(
00146 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00147 self.Mconv4_stage4_L2 = L.Convolution2D(
00148 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00149 self.Mconv5_stage4_L2 = L.Convolution2D(
00150 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00151 self.Mconv6_stage4_L2 = L.Convolution2D(
00152 in_channels=128, out_channels=128, ksize=1, stride=1, pad=0)
00153 self.Mconv7_stage4_L2 = L.Convolution2D(
00154 in_channels=128, out_channels=19, ksize=1, stride=1, pad=0)
00155
00156
00157 self.Mconv1_stage5_L1 = L.Convolution2D(
00158 in_channels=185, out_channels=128, ksize=7, stride=1, pad=3)
00159 self.Mconv2_stage5_L1 = L.Convolution2D(
00160 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00161 self.Mconv3_stage5_L1 = L.Convolution2D(
00162 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00163 self.Mconv4_stage5_L1 = L.Convolution2D(
00164 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00165 self.Mconv5_stage5_L1 = L.Convolution2D(
00166 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00167 self.Mconv6_stage5_L1 = L.Convolution2D(
00168 in_channels=128, out_channels=128, ksize=1, stride=1, pad=0)
00169 self.Mconv7_stage5_L1 = L.Convolution2D(
00170 in_channels=128, out_channels=38, ksize=1, stride=1, pad=0)
00171 self.Mconv1_stage5_L2 = L.Convolution2D(
00172 in_channels=185, out_channels=128, ksize=7, stride=1, pad=3)
00173 self.Mconv2_stage5_L2 = L.Convolution2D(
00174 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00175 self.Mconv3_stage5_L2 = L.Convolution2D(
00176 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00177 self.Mconv4_stage5_L2 = L.Convolution2D(
00178 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00179 self.Mconv5_stage5_L2 = L.Convolution2D(
00180 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00181 self.Mconv6_stage5_L2 = L.Convolution2D(
00182 in_channels=128, out_channels=128, ksize=1, stride=1, pad=0)
00183 self.Mconv7_stage5_L2 = L.Convolution2D(
00184 in_channels=128, out_channels=19, ksize=1, stride=1, pad=0)
00185
00186
00187 self.Mconv1_stage6_L1 = L.Convolution2D(
00188 in_channels=185, out_channels=128, ksize=7, stride=1, pad=3)
00189 self.Mconv2_stage6_L1 = L.Convolution2D(
00190 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00191 self.Mconv3_stage6_L1 = L.Convolution2D(
00192 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00193 self.Mconv4_stage6_L1 = L.Convolution2D(
00194 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00195 self.Mconv5_stage6_L1 = L.Convolution2D(
00196 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00197 self.Mconv6_stage6_L1 = L.Convolution2D(
00198 in_channels=128, out_channels=128, ksize=1, stride=1, pad=0)
00199 self.Mconv7_stage6_L1 = L.Convolution2D(
00200 in_channels=128, out_channels=38, ksize=1, stride=1, pad=0)
00201 self.Mconv1_stage6_L2 = L.Convolution2D(
00202 in_channels=185, out_channels=128, ksize=7, stride=1, pad=3)
00203 self.Mconv2_stage6_L2 = L.Convolution2D(
00204 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00205 self.Mconv3_stage6_L2 = L.Convolution2D(
00206 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00207 self.Mconv4_stage6_L2 = L.Convolution2D(
00208 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00209 self.Mconv5_stage6_L2 = L.Convolution2D(
00210 in_channels=128, out_channels=128, ksize=7, stride=1, pad=3)
00211 self.Mconv6_stage6_L2 = L.Convolution2D(
00212 in_channels=128, out_channels=128, ksize=1, stride=1, pad=0)
00213 self.Mconv7_stage6_L2 = L.Convolution2D(
00214 in_channels=128, out_channels=19, ksize=1, stride=1, pad=0)
00215
00216 if pretrained_model in models.keys():
00217 data_dir = chainer.dataset.get_dataset_directory('openpose/pose')
00218 model_path = os.path.join(data_dir, models[pretrained_model])
00219 try:
00220 os.makedirs(os.path.dirname(model_path))
00221 except OSError:
00222 pass
00223 chainer.dataset.cache_or_load_file(
00224 model_path,
00225 lambda f: _download_pretrained_model(pretrained_model, f),
00226 lambda f: f)
00227 chainer.serializers.load_npz(model_path, self)
00228 elif pretrained_model is not None:
00229 if not os.path.exists(pretrained_model):
00230 raise OSError('model does not exists: "%s"' % pretrained_model)
00231 chainer.serializers.load_npz(pretrained_model, self)
00232
00233 def __call__(self, x):
00234 heatmaps = []
00235 pafs = []
00236
00237 h = F.relu(self.conv1_1(x))
00238 h = F.relu(self.conv1_2(h))
00239 h = F.max_pooling_2d(h, ksize=2, stride=2)
00240 h = F.relu(self.conv2_1(h))
00241 h = F.relu(self.conv2_2(h))
00242 h = F.max_pooling_2d(h, ksize=2, stride=2)
00243 h = F.relu(self.conv3_1(h))
00244 h = F.relu(self.conv3_2(h))
00245 h = F.relu(self.conv3_3(h))
00246 h = F.relu(self.conv3_4(h))
00247 h = F.max_pooling_2d(h, ksize=2, stride=2)
00248 h = F.relu(self.conv4_1(h))
00249 h = F.relu(self.conv4_2(h))
00250 h = F.relu(self.conv4_3_CPM(h))
00251 h = F.relu(self.conv4_4_CPM(h))
00252 feature_map = h
00253
00254
00255 h1 = F.relu(self.conv5_1_CPM_L1(feature_map))
00256 h1 = F.relu(self.conv5_2_CPM_L1(h1))
00257 h1 = F.relu(self.conv5_3_CPM_L1(h1))
00258 h1 = F.relu(self.conv5_4_CPM_L1(h1))
00259 h1 = self.conv5_5_CPM_L1(h1)
00260 h2 = F.relu(self.conv5_1_CPM_L2(feature_map))
00261 h2 = F.relu(self.conv5_2_CPM_L2(h2))
00262 h2 = F.relu(self.conv5_3_CPM_L2(h2))
00263 h2 = F.relu(self.conv5_4_CPM_L2(h2))
00264 h2 = self.conv5_5_CPM_L2(h2)
00265 pafs.append(h1)
00266 heatmaps.append(h2)
00267
00268
00269 h = F.concat((h1, h2, feature_map), axis=1)
00270 h1 = F.relu(self.Mconv1_stage2_L1(h))
00271 h1 = F.relu(self.Mconv2_stage2_L1(h1))
00272 h1 = F.relu(self.Mconv3_stage2_L1(h1))
00273 h1 = F.relu(self.Mconv4_stage2_L1(h1))
00274 h1 = F.relu(self.Mconv5_stage2_L1(h1))
00275 h1 = F.relu(self.Mconv6_stage2_L1(h1))
00276 h1 = self.Mconv7_stage2_L1(h1)
00277 h2 = F.relu(self.Mconv1_stage2_L2(h))
00278 h2 = F.relu(self.Mconv2_stage2_L2(h2))
00279 h2 = F.relu(self.Mconv3_stage2_L2(h2))
00280 h2 = F.relu(self.Mconv4_stage2_L2(h2))
00281 h2 = F.relu(self.Mconv5_stage2_L2(h2))
00282 h2 = F.relu(self.Mconv6_stage2_L2(h2))
00283 h2 = self.Mconv7_stage2_L2(h2)
00284 pafs.append(h1)
00285 heatmaps.append(h2)
00286
00287
00288 h = F.concat((h1, h2, feature_map), axis=1)
00289 h1 = F.relu(self.Mconv1_stage3_L1(h))
00290 h1 = F.relu(self.Mconv2_stage3_L1(h1))
00291 h1 = F.relu(self.Mconv3_stage3_L1(h1))
00292 h1 = F.relu(self.Mconv4_stage3_L1(h1))
00293 h1 = F.relu(self.Mconv5_stage3_L1(h1))
00294 h1 = F.relu(self.Mconv6_stage3_L1(h1))
00295 h1 = self.Mconv7_stage3_L1(h1)
00296 h2 = F.relu(self.Mconv1_stage3_L2(h))
00297 h2 = F.relu(self.Mconv2_stage3_L2(h2))
00298 h2 = F.relu(self.Mconv3_stage3_L2(h2))
00299 h2 = F.relu(self.Mconv4_stage3_L2(h2))
00300 h2 = F.relu(self.Mconv5_stage3_L2(h2))
00301 h2 = F.relu(self.Mconv6_stage3_L2(h2))
00302 h2 = self.Mconv7_stage3_L2(h2)
00303 pafs.append(h1)
00304 heatmaps.append(h2)
00305
00306
00307 h = F.concat((h1, h2, feature_map), axis=1)
00308 h1 = F.relu(self.Mconv1_stage4_L1(h))
00309 h1 = F.relu(self.Mconv2_stage4_L1(h1))
00310 h1 = F.relu(self.Mconv3_stage4_L1(h1))
00311 h1 = F.relu(self.Mconv4_stage4_L1(h1))
00312 h1 = F.relu(self.Mconv5_stage4_L1(h1))
00313 h1 = F.relu(self.Mconv6_stage4_L1(h1))
00314 h1 = self.Mconv7_stage4_L1(h1)
00315 h2 = F.relu(self.Mconv1_stage4_L2(h))
00316 h2 = F.relu(self.Mconv2_stage4_L2(h2))
00317 h2 = F.relu(self.Mconv3_stage4_L2(h2))
00318 h2 = F.relu(self.Mconv4_stage4_L2(h2))
00319 h2 = F.relu(self.Mconv5_stage4_L2(h2))
00320 h2 = F.relu(self.Mconv6_stage4_L2(h2))
00321 h2 = self.Mconv7_stage4_L2(h2)
00322 pafs.append(h1)
00323 heatmaps.append(h2)
00324
00325
00326 h = F.concat((h1, h2, feature_map), axis=1)
00327 h1 = F.relu(self.Mconv1_stage5_L1(h))
00328 h1 = F.relu(self.Mconv2_stage5_L1(h1))
00329 h1 = F.relu(self.Mconv3_stage5_L1(h1))
00330 h1 = F.relu(self.Mconv4_stage5_L1(h1))
00331 h1 = F.relu(self.Mconv5_stage5_L1(h1))
00332 h1 = F.relu(self.Mconv6_stage5_L1(h1))
00333 h1 = self.Mconv7_stage5_L1(h1)
00334 h2 = F.relu(self.Mconv1_stage5_L2(h))
00335 h2 = F.relu(self.Mconv2_stage5_L2(h2))
00336 h2 = F.relu(self.Mconv3_stage5_L2(h2))
00337 h2 = F.relu(self.Mconv4_stage5_L2(h2))
00338 h2 = F.relu(self.Mconv5_stage5_L2(h2))
00339 h2 = F.relu(self.Mconv6_stage5_L2(h2))
00340 h2 = self.Mconv7_stage5_L2(h2)
00341 pafs.append(h1)
00342 heatmaps.append(h2)
00343
00344
00345 h = F.concat((h1, h2, feature_map), axis=1)
00346 h1 = F.relu(self.Mconv1_stage6_L1(h))
00347 h1 = F.relu(self.Mconv2_stage6_L1(h1))
00348 h1 = F.relu(self.Mconv3_stage6_L1(h1))
00349 h1 = F.relu(self.Mconv4_stage6_L1(h1))
00350 h1 = F.relu(self.Mconv5_stage6_L1(h1))
00351 h1 = F.relu(self.Mconv6_stage6_L1(h1))
00352 h1 = self.Mconv7_stage6_L1(h1)
00353 h2 = F.relu(self.Mconv1_stage6_L2(h))
00354 h2 = F.relu(self.Mconv2_stage6_L2(h2))
00355 h2 = F.relu(self.Mconv3_stage6_L2(h2))
00356 h2 = F.relu(self.Mconv4_stage6_L2(h2))
00357 h2 = F.relu(self.Mconv5_stage6_L2(h2))
00358 h2 = F.relu(self.Mconv6_stage6_L2(h2))
00359 h2 = self.Mconv7_stage6_L2(h2)
00360 pafs.append(h1)
00361 heatmaps.append(h2)
00362
00363 return pafs, heatmaps
00364
00365
00366 def _download_pretrained_model(model_type, dest_path):
00367 from chainer.links import caffe
00368
00369 if os.path.exists(dest_path):
00370 raise OSError('destination already exists: %s' % dest_path)
00371
00372 basename, ext = os.path.splitext(models[model_type])
00373 url = base_url + basename + '.caffemodel'
00374 caffe_model_path = chainer.dataset.cached_download(url)
00375 if not os.path.exists(caffe_model_path):
00376 raise OSError('caffe model does not exist: %s' % caffe_model_path)
00377
00378 print('Converting to chainer model')
00379 caffe_model = caffe.CaffeFunction(caffe_model_path)
00380 chainer_model = PoseNet(pretrained_model=None)
00381 for link in chainer_model.links():
00382 if not isinstance(link, chainer.Link) or not link.name:
00383 continue
00384 if eval('chainer_model.{0}.b.shape == caffe_model["{0}"].b.shape'.format(link.name)) and\
00385 eval('chainer_model.{0}.W.shape == caffe_model["{0}"].W.shape'.format(link.name)):
00386 exec('chainer_model.{0}.W.data = caffe_model["{0}"].W.data'.format(link.name))
00387 exec('chainer_model.{0}.b.data = caffe_model["{0}"].b.data'.format(link.name))
00388 print('Copied layer {0}'.format(link.name))
00389 else:
00390 print('Failed to copy layer {0}'.format(link.name))
00391
00392 chainer.serializers.save_npz(dest_path, chainer_model)
00393 return True