|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +os.environ['GLOG_minloglevel'] = '2' |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import caffe |
| 8 | +import argparse |
| 9 | + |
| 10 | +parser = argparse.ArgumentParser(description='Conversion options') |
| 11 | +parser.add_argument('-relu', action='store_true', help='Use ReLU instead of PReLU') |
| 12 | +parser.add_argument('-transp', action='store_true', help='Transpose convolution coefficients') |
| 13 | +args = parser.parse_args() |
| 14 | + |
| 15 | +baseName = "mtcnn" |
| 16 | +if args.relu: |
| 17 | + baseName += "-relu" |
| 18 | + |
| 19 | +if args.transp: |
| 20 | + baseName += "-transp" |
| 21 | + |
| 22 | +netPatched = caffe.Net("{}.prototxt".format(baseName), caffe.TEST) |
| 23 | + |
| 24 | +def convertDet1(): |
| 25 | + net = caffe.Net('det1.prototxt', 'det1.caffemodel', caffe.TEST) |
| 26 | + |
| 27 | + for pnet in ['pnet1', 'pnet2', 'pnet3', 'pnet4', 'pnet5', 'pnet6', 'pnet7', 'pnet8', 'pnet9']: |
| 28 | + for layer in ['conv1', 'conv2', 'conv3']: |
| 29 | + if args.transp: |
| 30 | + netPatched.params[pnet+'-'+layer][0].data[...] = np.transpose(net.params[layer][0].data, (0,1,3,2)) |
| 31 | + else: |
| 32 | + netPatched.params[pnet+'-'+layer][0].data[...] = net.params[layer][0].data |
| 33 | + netPatched.params[pnet+'-'+layer][1].data[...] = net.params[layer][1].data |
| 34 | + |
| 35 | + netPatched.params[pnet+'-conv4'][0].data[...] = \ |
| 36 | + np.concatenate((net.params['conv4-1'][0].data, net.params['conv4-2'][0].data), axis=0) |
| 37 | + netPatched.params[pnet+'-conv4'][1].data[...] = \ |
| 38 | + np.concatenate((net.params['conv4-1'][1].data, net.params['conv4-2'][1].data), axis=0) |
| 39 | + |
| 40 | + if not args.relu: |
| 41 | + for layer in ['PReLU1', 'PReLU2', 'PReLU3']: |
| 42 | + netPatched.params[pnet+'-'+layer][0].data[...] = net.params[layer][0].data |
| 43 | + |
| 44 | +def convertDet2(): |
| 45 | + net = caffe.Net('det2.prototxt', 'det2.caffemodel', caffe.TEST) |
| 46 | + |
| 47 | + for layer in ['conv1', 'conv2', 'conv3']: |
| 48 | + if args.transp: |
| 49 | + netPatched.params['rnet-'+layer][0].data[...] = np.transpose(net.params[layer][0].data, (0,1,3,2)) |
| 50 | + else: |
| 51 | + netPatched.params['rnet-'+layer][0].data[...] = net.params[layer][0].data |
| 52 | + |
| 53 | + netPatched.params['rnet-'+layer][1].data[...] = net.params[layer][1].data |
| 54 | + |
| 55 | + for layer in ['conv4']: |
| 56 | + if args.transp: |
| 57 | + netPatched.params['rnet-'+layer][0].data[...] = np.reshape(np.transpose(np.reshape(net.params[layer][0].data, (128,64,3,3)), (0,1,3,2)), (128,576)) |
| 58 | + else: |
| 59 | + netPatched.params['rnet-'+layer][0].data[...] = net.params[layer][0].data |
| 60 | + |
| 61 | + netPatched.params['rnet-'+layer][1].data[...] = net.params[layer][1].data |
| 62 | + |
| 63 | + netPatched.params['rnet-conv5'][0].data[...] = \ |
| 64 | + np.concatenate((net.params['conv5-1'][0].data, net.params['conv5-2'][0].data), axis=0) |
| 65 | + netPatched.params['rnet-conv5'][1].data[...] = \ |
| 66 | + np.concatenate((net.params['conv5-1'][1].data, net.params['conv5-2'][1].data), axis=0) |
| 67 | + |
| 68 | + if not args.relu: |
| 69 | + for layer in ['prelu1', 'prelu2', 'prelu3', 'prelu4']: |
| 70 | + netPatched.params['rnet-'+layer][0].data[...] = net.params[layer][0].data |
| 71 | + |
| 72 | +def convertDet3(): |
| 73 | + net = caffe.Net('det3.prototxt', 'det3.caffemodel', caffe.TEST) |
| 74 | + |
| 75 | + for layer in ['conv1', 'conv2', 'conv3', 'conv4']: |
| 76 | + if args.transp: |
| 77 | + netPatched.params['onet-'+layer][0].data[...] = np.transpose(net.params[layer][0].data, (0,1,3,2)) |
| 78 | + else: |
| 79 | + netPatched.params['onet-'+layer][0].data[...] = net.params[layer][0].data |
| 80 | + netPatched.params['onet-'+layer][1].data[...] = net.params[layer][1].data |
| 81 | + |
| 82 | + for layer in ['conv5']: |
| 83 | + if args.transp: |
| 84 | + netPatched.params['onet-'+layer][0].data[...] = np.reshape(np.transpose(np.reshape(net.params[layer][0].data, (256,128,3,3)), (0,1,3,2)), (256,1152)) |
| 85 | + else: |
| 86 | + netPatched.params['onet-'+layer][0].data[...] = net.params[layer][0].data |
| 87 | + |
| 88 | + netPatched.params['onet-'+layer][1].data[...] = net.params[layer][1].data |
| 89 | + |
| 90 | + netPatched.params['onet-conv6'][0].data[...] = \ |
| 91 | + np.concatenate((net.params['conv6-1'][0].data, |
| 92 | + net.params['conv6-2'][0].data, |
| 93 | + net.params['conv6-3'][0].data), axis=0) |
| 94 | + netPatched.params['onet-conv6'][1].data[...] = \ |
| 95 | + np.concatenate((net.params['conv6-1'][1].data, |
| 96 | + net.params['conv6-2'][1].data, |
| 97 | + net.params['conv6-3'][1].data), axis=0) |
| 98 | + if not args.relu: |
| 99 | + for layer in ['prelu1', 'prelu2', 'prelu3', 'prelu4', 'prelu5']: |
| 100 | + netPatched.params['onet-'+layer][0].data[...] = net.params[layer][0].data |
| 101 | + |
| 102 | +convertDet1() |
| 103 | +convertDet2() |
| 104 | +convertDet3() |
| 105 | + |
| 106 | +netPatched.save('{}.caffemodel'.format(baseName)) |
0 commit comments