def get_network(type, placeholder_input, sess_for_load=None): if type == 'mobilenet': net = MobilenetNetwork({'image': placeholder_input}, conv_width=0.75, conv_width2=0.50) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_accurate': net = MobilenetNetwork({'image': placeholder_input}, conv_width=1.00) pretrain_path = 'pretrained/mobilenet_v1_1.0_224_2017_06_14/mobilenet_v1_1.0_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_fast': net = MobilenetNetwork({'image': placeholder_input}, conv_width=0.50) pretrain_path = 'pretrained/mobilenet_v1_0.50_224_2017_06_14/mobilenet_v1_0.50_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'cmu': net = CmuNetwork({'image': placeholder_input}) pretrain_path = 'numpy/openpose_coco.npy' last_layer = 'Mconv7_stage6_L{aux}' else: raise Exception('Invalid Mode.') if sess_for_load is not None: if type == 'cmu': net.load('./models/numpy/openpose_coco.npy', sess_for_load) else: ckpts = { 'mobilenet': os.path.join(_get_base_path(), 'pretrained/mobilenet_v1_0.50_224_2017_06_14/mobilenet_v1_0.50_224.ckpt'), 'mobilenet_fast': os.path.join(_get_base_path(), 'pretrained/mobilenet_fast/-163000'), 'mobilenet_accurate': os.path.join(_get_base_path(), 'pretrained/mobilenet_accurate/model-170000') } # loader = tf.train.import_meta_graph(ckpts[type] + '.meta') loader = tf.train.Saver() loader.restore(sess_for_load, ckpts[type]) return net, os.path.join(_get_base_path(), pretrain_path), last_layer
def get_variables(model_path, height , width): input_node = tf.placeholder(tf.float32, shape=(1, height, width, 3), name='image') net = MobilenetNetwork({'image': input_node}, trainable=False, conv_width=0.75, conv_width2=0.50) saver = tf.train.Saver(max_to_keep=100) config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False) with tf.Session(config=config) as sess: saver.restore(sess, model_path) variables = tf.global_variables() variables = [(v.name, v.eval(session=sess).copy(order='C')) for v in variables] return variables
def get_network(type, placeholder_input, sess_for_load=None, trainable=False): if type == 'mobilenet': net = MobilenetNetwork({'image': placeholder_input}, trainable=trainable, conv_width=0.75, conv_width2=0.50) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_accurate': net = MobilenetNetwork({'image': placeholder_input}, trainable=trainable, conv_width=1.00) pretrain_path = 'pretrained/mobilenet_v1_1.0_224_2017_06_14/mobilenet_v1_1.0_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_fast': net = MobilenetNetwork({'image': placeholder_input}, trainable=trainable, conv_width=0.50) pretrain_path = 'pretrained/mobilenet_v1_0.50_224_2017_06_14/mobilenet_v1_0.50_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'cmu': net = CmuNetwork({'image': placeholder_input}) pretrain_path = 'numpy/openpose_coco.npy' last_layer = 'Mconv7_stage6_L{aux}' else: raise Exception('Invalid Mode.') if sess_for_load is not None: if type == 'cmu': net.load('./models/numpy/openpose_coco.npy', sess_for_load) else: s = '%dx%d' % (placeholder_input.shape[2], placeholder_input.shape[1]) ckpts = { 'mobilenet': 'trained/mobilenet_%s/model-release' % s, 'mobilenet_fast': 'trained/mobilenet_fast/model-163000', 'mobilenet_accurate': 'trained/mobilenet_accurate/model-170000' } loader = tf.train.Saver() loader.restore(sess_for_load, os.path.join(_get_base_path(), ckpts[type])) return net, os.path.join(_get_base_path(), pretrain_path), last_layer
def get_network(type, placeholder_input, sess_for_load=None, trainable=True): if type == 'mobilenet': net = MobilenetNetwork({'image': placeholder_input}, conv_width=0.75, conv_width2=1.00, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_fast': net = MobilenetNetwork({'image': placeholder_input}, conv_width=0.5, conv_width2=0.5, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_accurate': net = MobilenetNetwork({'image': placeholder_input}, conv_width=1.00, conv_width2=1.00, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_thin': net = MobilenetNetworkThin({'image': placeholder_input}, conv_width=0.75, conv_width2=0.50, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_1.0_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'cmu': net = CmuNetwork({'image': placeholder_input}, trainable=trainable) pretrain_path = 'numpy/openpose_coco.npy' last_layer = 'Mconv7_stage6_L{aux}' elif type == 'vgg': net = CmuNetwork({'image': placeholder_input}, trainable=trainable) pretrain_path = 'numpy/openpose_vgg16.npy' last_layer = 'Mconv7_stage6_L{aux}' else: raise Exception('Invalid Mode.') if sess_for_load is not None: if type == 'cmu' or type == 'vgg': net.load(os.path.join(_get_base_path(), pretrain_path), sess_for_load) else: s = '%dx%d' % (placeholder_input.shape[2], placeholder_input.shape[1]) ckpts = { 'mobilenet': 'trained/mobilenet_%s/model-246038' % s, 'mobilenet_thin': 'trained/mobilenet_thin_%s/model-449003' % s, 'mobilenet_fast': 'trained/mobilenet_fast_%s/model-189000' % s, 'mobilenet_accurate': 'trained/mobilenet_accurate/model-170000' } loader = tf.train.Saver() loader.restore(sess_for_load, os.path.join(_get_base_path(), ckpts[type])) return net, os.path.join(_get_base_path(), pretrain_path), last_layer
def get_network(type, placeholder_input, sess_for_load=None, trainable=True): if type == 'mobilenet': net = MobilenetNetwork({'image': placeholder_input}, conv_width=0.75, conv_width2=1.00, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_fast': net = MobilenetNetwork({'image': placeholder_input}, conv_width=0.5, conv_width2=0.5, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_accurate': net = MobilenetNetwork({'image': placeholder_input}, conv_width=1.00, conv_width2=1.00, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_1.0_224_2017_06_14/mobilenet_v1_1.0_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_thin': net = MobilenetNetworkThin({'image': placeholder_input}, conv_width=0.75, conv_width2=0.50, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'cmu': net = CmuNetwork({'image': placeholder_input}, trainable=trainable) pretrain_path = 'numpy/openpose_coco.npy' last_layer = 'Mconv7_stage6_L{aux}' elif type == 'vgg': net = CmuNetwork({'image': placeholder_input}, trainable=trainable) pretrain_path = 'numpy/openpose_vgg16.npy' last_layer = 'Mconv7_stage6_L{aux}' else: raise Exception('Invalid Mode.') pretrain_path_full = os.path.join(_get_base_path(), pretrain_path) if sess_for_load is not None: if type == 'cmu' or type == 'vgg': if not os.path.isfile(pretrain_path_full): raise Exception('Model file doesn\'t exist, path=%s' % pretrain_path_full) net.load(os.path.join(_get_base_path(), pretrain_path), sess_for_load) else: s = '%dx%d' % (placeholder_input.shape[2], placeholder_input.shape[1]) ckpts = { 'mobilenet': 'trained/mobilenet_%s/model-246038' % s, 'mobilenet_thin': 'trained/mobilenet_thin_%s/model-449003' % s, 'mobilenet_fast': 'trained/mobilenet_fast_%s/model-189000' % s, 'mobilenet_accurate': 'trained/mobilenet_accurate/model-170000' } ckpt_path = os.path.join(_get_base_path(), ckpts[type]) loader = tf.train.Saver() try: loader.restore(sess_for_load, ckpt_path) except Exception as e: raise Exception('Fail to load model files. \npath=%s\nerr=%s' % (ckpt_path, str(e))) return net, pretrain_path_full, last_layer
shape=(None, args.input_height, args.input_width, 3), name='image') with tf.Session(config=config) as sess: if args.model == 'dsconv': net = DSConvNetwork({'image': input_node}, trainable=False) net.load('./models/numpy/fastopenpose_coco_v170729.npy', sess) last_layer = 'Mconv7_stage{stage}_L{aux}' elif args.model == 'cmu': net = CmuNetwork({'image': input_node}, trainable=False) net.load('./models/numpy/openpose_coco.npy', sess) last_layer = 'Mconv7_stage{stage}_L{aux}' elif args.model == 'mobilenet_1.0': net = MobilenetNetwork({'image': input_node}, trainable=False, conv_width=1.0) sess.run(tf.global_variables_initializer()) # TODO last_layer = 'MConv_Stage{stage}_L{aux}_5' elif args.model == 'mobilenet_0.50': net = MobilenetNetwork({'image': input_node}, trainable=False, conv_width=0.50) sess.run(tf.global_variables_initializer()) # TODO last_layer = 'MConv_Stage{stage}_L{aux}_5' else: raise Exception('Invalid Mode.') logging.debug('read image+') image = cv2.imread(args.imgpath) image = cv2.resize(image, (args.input_width, args.input_height))
def get_model(sess, height, width): init = tf.global_variables_initializer() sess.run(init) net = MobilenetNetwork({'image': input_node} , trainable=False, conv_width=0.75, conv_width2=0.50) K.set_session(sess) conv_width=0.75 conv_width2=0.50 min_depth = 8 depth = lambda d: max(int(d * conv_width), min_depth) depth2 = lambda d: max(int(d * conv_width2), min_depth) image = Input(shape=(width, height, 3),name="image") x = Conv2D(depth(32),(3,3) , strides=2 , use_bias=False , name="Conv2d_0" , trainable = False , padding='same' , kernel_regularizer=l2(0.04) )(image) x = BatchNormalization(scale=True, name='Conv2d_0_bn')(x,training=False) x = Activation('relu', name='Conv2d_0_relu')(x) x = separable_conv(x,depth(64),(3,3),1,name='Conv2d_1') x = separable_conv(x,depth(128),(3,3),2,name='Conv2d_2') o3 = separable_conv(x,depth(128),(3,3),1,name='Conv2d_3') x = separable_conv(o3,depth(256),(3,3),2,name='Conv2d_4') x = separable_conv(x,depth(256),(3,3),1,name='Conv2d_5') x = separable_conv(x,depth(512),(3,3),1,name='Conv2d_6') o7 = separable_conv(x,depth(512),(3,3),1,name='Conv2d_7') x = separable_conv(o7,depth(512),(3,3),1,name='Conv2d_8') x = separable_conv(x,depth(512),(3,3),1,name='Conv2d_9') x = separable_conv(x,depth(512),(3,3),1,name='Conv2d_10') o11 = separable_conv(x,depth(512),(3,3),1,name='Conv2d_11') o3_pool = MaxPooling2D((2, 2),(2, 2),padding='same')(o3) feat_concat = concatenate([o3_pool,o7,o11], axis=3) prefix = 'MConv_Stage1' r1 = separable_conv(feat_concat,depth2(128),(3,3),1,name=prefix + '_L1_1') r1 = separable_conv(r1,depth2(128),(3,3),1,name=prefix + '_L1_2') r1 = separable_conv(r1,depth2(128),(3,3),1,name=prefix + '_L1_3') r1 = separable_conv(r1,depth2(512),(1,1),1,name=prefix + '_L1_4') r1 = separable_conv(r1,38,(1,1),1,relu=False,name=prefix + '_L1_5') # concat = Input(shape=(46, 46, 864)) r2 = separable_conv(feat_concat,depth2(128),(3,3),1,name=prefix + '_L2_1') r2 = separable_conv(r2,depth2(128),(3,3),1,name=prefix + '_L2_2') r2 = separable_conv(r2,depth2(128),(3,3),1,name=prefix + '_L2_3') r2 = separable_conv(r2,depth2(512),(1,1),1,name=prefix + '_L2_4') r2 = separable_conv(r2,19,(1,1),1,relu=False,name=prefix + '_L2_5') for stage_id in range(5): prefix = 'MConv_Stage%d' % (stage_id + 2) cc = concatenate([r1,r2,feat_concat], axis=3) r1 = separable_conv(cc,depth2(128),(3,3),1,name=prefix + '_L1_1') r1 = separable_conv(r1,depth2(128),(3,3),1,name=prefix + '_L1_2') r1 = separable_conv(r1,depth2(128),(3,3),1,name=prefix + '_L1_3') r1 = separable_conv(r1,depth2(128),(1,1),1,name=prefix + '_L1_4') r1 = separable_conv(r1,38,(1,1),1,relu=False,name=prefix + '_L1_5') r2 = separable_conv(cc,depth2(128),(3,3),1,name=prefix + '_L2_1') r2 = separable_conv(r2,depth2(128),(3,3),1,name=prefix + '_L2_2') r2 = separable_conv(r2,depth2(128),(3,3),1,name=prefix + '_L2_3') r2 = separable_conv(r2,depth2(128),(1,1),1,name=prefix + '_L2_4') r2 = separable_conv(r2,19,(1,1),1,relu=False,name=prefix + '_L2_5') out = concatenate([r2, r1],axis=3) print(out) model = Model(image, out) layers = getTupleLayer("MobilenetV1","Conv2d_0") model = setLayer(model,layers) for (i, layer) in enumerate(global_layers): # idx = i + 2 n = layer.split("_") n.pop() prefix = "" if n[0] == "Conv2d": prefix = "MobilenetV1" if n[0] == "MConv": prefix = "Openpose" if prefix != "": layers = getTupleLayer(prefix,layer) model = setLayer(model,layers) if not os.path.exists("output"): os.mkdir("output") model.save('output/predict.hd5') # plot_model(model, to_file='model_shape.png', show_shapes=True) # img = load_img(args.imgpath, target_size=(args.input_width, args.input_height)) # img = np.expand_dims(img, axis=0) # print(img.shape) # prediction = model.predict(img) # prediction = prediction[0] # print("#output") # print(prediction.shape) # print(prediction[0:1, 0:1, :]) # print(np.mean(prediction)) # np.save('output/prediction.npy', prediction, allow_pickle=False) return model
val_image = val_image.astype(float) val_image = val_image * (2.0 / 255.0) - 1.0 # define model for multi-gpu q_inp_split = tf.split(q_inp, args.gpus) output_vectmap = [] output_heatmap = [] vectmap_losses = [] heatmap_losses = [] for gpu_id in range(args.gpus): with tf.device(tf.DeviceSpec(device_type="GPU", device_index=gpu_id)): with tf.variable_scope(tf.get_variable_scope(), reuse=(gpu_id > 0)): if args.model == 'mobilenet_1.0': net = MobilenetNetwork({'image': q_inp_split[gpu_id]}, conv_width=1.0) pretrain_path = './models/pretrained/mobilenet_v1_1.0_224_2017_06_14/mobilenet_v1_1.0_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif args.model == 'mobilenet_0.75': net = MobilenetNetwork({'image': q_inp_split[gpu_id]}, conv_width=0.75) pretrain_path = './models/pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif args.model == 'mobilenet_0.50': net = MobilenetNetwork({'image': q_inp_split[gpu_id]}, conv_width=0.50) pretrain_path = './models/pretrained/mobilenet_v1_0.50_224_2017_06_14/mobilenet_v1_0.50_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif args.model == 'cmu': net = CmuNetwork({'image': q_inp_split[gpu_id]}) pretrain_path = './models/numpy/openpose_coco.npy'
def get_network(type, placeholder_input, sess_for_load=None, trainable=True): if type == 'mobilenet': net = MobilenetNetwork({'image': placeholder_input}, conv_width=0.75, conv_width2=1.00, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_fast': net = MobilenetNetworkFast({'image': placeholder_input}, conv_width=0.5, conv_width2=0.5, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.50_224_2017_06_14/mobilenet_v1_0.50_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_accurate': net = MobilenetNetworkThin({'image': placeholder_input}, conv_width=1.00, conv_width2=0.50, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_1.0_224_2017_06_14/mobilenet_v1_1.0_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_thin': net = MobilenetNetworkThin({'image': placeholder_input}, conv_width=0.75, conv_width2=0.750, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_original': net = MobilenetNetworkOriginal({'image': placeholder_input}, conv_width=0.75, conv_width2=0.50, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v1_0.75_224_2017_06_14/mobilenet_v1_0.75_224.ckpt' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'mobilenet_v2': net = MobilenetNetworkV2({'image': placeholder_input}, conv_width=1, conv_width2=0.50, trainable=trainable) pretrain_path = 'pretrained/mobilenet_v2/model.ckpt-1450000' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'cmu': net = CmuNetwork({'image': placeholder_input}, trainable=trainable) pretrain_path = 'numpy/openpose_coco.npy' last_layer = 'Mconv7_stage6_L{aux}' elif type == 'vgg': net = CmuNetwork({'image': placeholder_input}, trainable=trainable) pretrain_path = 'numpy/openpose_coco.npy' last_layer = 'Mconv7_stage6_L{aux}' elif type == 'resnet32': net = Resnet32({'image': placeholder_input}, conv_width=0.75, conv_width2=0.50, trainable=trainable) pretrain_path = 'numpy/resnet32.npy' last_layer = 'MConv_Stage6_L{aux}_5' elif type == 'vgg16x4': net = VGG16x4Network({'image': placeholder_input}, conv_width=0.75, conv_width2=0.75, trainable=trainable) pretrain_path = 'numpy/vgg16x4.npy' last_layer = 'Mconv7_stage6_L{aux}' else: raise Exception('Invalid Mode.') if sess_for_load is not None: if type == 'cmu' or type == 'vgg': net.load(os.path.join(_get_base_path(), pretrain_path), sess_for_load) else: s = '%dx%d' % (placeholder_input.shape[2], placeholder_input.shape[1]) ckpts = { 'mobilenet': 'models/trained/mobilenet_%s/model-53008' % s, 'mobilenet_thin': 'models/trained/mobilenet_thin_432x368/model-160001', 'mobilenet_original': 'models/trained/mobilenet_thin_benchmark/model-388003', 'mobilenet_v2': 'models/trained/mobilenet_v2/model-218000', 'vgg16x4': 'models/trained/vgg16x4_0.75/model-35000', } loader = tf.train.Saver() loader.restore(sess_for_load, os.path.join(base_path, ckpts[type])) return net, os.path.join(_get_base_path(), pretrain_path), last_layer