def run_benchmark(): with tf.Graph().as_default(): with tf.device('/gpu:0'): #with tf.device('/cpu:0'): # Generate some dummy images. image_size = 224 inputs = tf.Variable( tf.random_normal([FLAGS.batch_size, image_size, image_size, 3], dtype=tf.float32, stddev=1e-1)) with slim.arg_scope(mobilenet_arg_scope()): logits, end_points = mobilenet(inputs, is_training=False) init = tf.global_variables_initializer() config = tf.ConfigProto() config.gpu_options.allocator_type = 'BFC' sess = tf.Session(config=config) sess.run(init) time_tensorflow_run(sess, logits, "Forward") # Add a simple objective so we can calculate the backward pass. objective = tf.nn.l2_loss(logits) # Compute the gradient with respect to all the parameters. grad = tf.gradients(objective, tf.trainable_variables()) # Run the backward benchmark. time_tensorflow_run(sess, grad, "Forward-backward")
def testForward(self): batch_size = 1 height, width = 224, 224 with self.test_session() as sess: inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = mobilenet.mobilenet(inputs) sess.run(tf.global_variables_initializer()) output = sess.run(logits) self.assertTrue(output.any())
def testEvaluation(self): batch_size = 2 height, width = 224, 224 num_classes = 1000 with self.test_session(): eval_inputs = tf.random_uniform((batch_size, height, width, 3)) logits, _ = mobilenet.mobilenet(eval_inputs, is_training=False) self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes]) predictions = tf.argmax(logits, 1) self.assertListEqual(predictions.get_shape().as_list(), [batch_size])
def run_benchmark(): if FLAGS.quantized: graph_filename = FLAGS.quantized_graph # Create a graph def object to read the graph with tf.gfile.GFile(graph_filename, "rb") as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) with tf.Graph().as_default() as graph: with tf.device('/' + FLAGS.mode + ':0'): image_size = 224 if FLAGS.quantized: inputs = np.random.random( (FLAGS.batch_size, image_size, image_size, 3)) tf.import_graph_def(graph_def) config = tf.ConfigProto() config.gpu_options.allocator_type = 'BFC' sess = tf.Session(config=config) # We define the input and output node we will feed in input_node = graph.get_tensor_by_name( 'import/MobileNet/input_images:0') output_node = graph.get_tensor_by_name( 'import/MobileNet/Predictions/Softmax:0') time_tensorflow_run_placeholder(sess, output_node, {input_node: inputs}, "Forward") else: image_size = 224 inputs = tf.Variable( tf.random_normal( [FLAGS.batch_size, image_size, image_size, 3], dtype=tf.float32, stddev=1e-1)) with slim.arg_scope(mobilenet_arg_scope()): logits, end_points = mobilenet(inputs, is_training=False) init = tf.global_variables_initializer() config = tf.ConfigProto() config.gpu_options.allocator_type = 'BFC' sess = tf.Session(config=config) sess.run(init) time_tensorflow_run(sess, logits, "Forward") # Add a simple objective so we can calculate the backward pass. objective = tf.nn.l2_loss(logits) # Compute the gradient with respect to all the parameters. grad = tf.gradients(objective, tf.trainable_variables()) # Run the backward benchmark. time_tensorflow_run(sess, grad, "Forward-backward")
def testBuild(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) logits, end_points = mobilenet.mobilenet(inputs, num_classes) self.assertEquals( end_points['MobileNet/conv_ds_2/depthwise_conv'].get_shape(). as_list(), [5, 112, 112, 32]) self.assertEquals( end_points['MobileNet/conv_ds_3/depthwise_conv'].get_shape(). as_list(), [5, 56, 56, 64]) self.assertEquals( end_points['MobileNet/conv_ds_4/depthwise_conv'].get_shape(). as_list(), [5, 56, 56, 128]) self.assertEquals( end_points['MobileNet/conv_ds_5/depthwise_conv'].get_shape(). as_list(), [5, 28, 28, 128]) self.assertEquals( end_points['MobileNet/conv_ds_6/depthwise_conv'].get_shape(). as_list(), [5, 28, 28, 256]) self.assertEquals( end_points['MobileNet/conv_ds_7/depthwise_conv'].get_shape(). as_list(), [5, 14, 14, 256]) self.assertEquals( end_points['MobileNet/conv_ds_8/depthwise_conv'].get_shape(). as_list(), [5, 14, 14, 512]) self.assertEquals( end_points['MobileNet/conv_ds_9/depthwise_conv'].get_shape(). as_list(), [5, 14, 14, 512]) self.assertEquals( end_points['MobileNet/conv_ds_10/depthwise_conv'].get_shape(). as_list(), [5, 14, 14, 512]) self.assertEquals( end_points['MobileNet/conv_ds_11/depthwise_conv'].get_shape(). as_list(), [5, 14, 14, 512]) self.assertEquals( end_points['MobileNet/conv_ds_12/depthwise_conv'].get_shape(). as_list(), [5, 14, 14, 512]) self.assertEquals( end_points['MobileNet/conv_ds_13/depthwise_conv'].get_shape(). as_list(), [5, 7, 7, 512]) self.assertEquals( end_points['MobileNet/conv_ds_14/depthwise_conv'].get_shape(). as_list(), [5, 7, 7, 1024]) self.assertEquals(end_points['squeeze'].get_shape().as_list(), [5, 1024]) self.assertEquals(logits.op.name, 'MobileNet/fc_16/BiasAdd') self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
def run_benchmark(): if FLAGS.quantized: graph_filename = FLAGS.quantized_graph # Create a graph def object to read the graph with tf.gfile.GFile(graph_filename, "rb") as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) with tf.Graph().as_default() as graph: with tf.device('/'+FLAGS.mode+':0'): image_size = 224 if FLAGS.quantized: inputs = np.random.random((FLAGS.batch_size, image_size, image_size, 3)) tf.import_graph_def(graph_def) config = tf.ConfigProto() config.gpu_options.allocator_type = 'BFC' sess = tf.Session(config=config) # We define the input and output node we will feed in input_node = graph.get_tensor_by_name('import/MobileNet/input_images:0') output_node = graph.get_tensor_by_name('import/MobileNet/Predictions/Softmax:0') time_tensorflow_run_placeholder(sess, output_node, {input_node: inputs}, "Forward") else: image_size = 224 inputs = tf.Variable(tf.random_normal([FLAGS.batch_size, image_size, image_size, 3], dtype=tf.float32, stddev=1e-1)) with slim.arg_scope(mobilenet_arg_scope()): logits, end_points = mobilenet(inputs, is_training=False) init = tf.global_variables_initializer() config = tf.ConfigProto() config.gpu_options.allocator_type = 'BFC' sess = tf.Session(config=config) sess.run(init) time_tensorflow_run(sess, logits, "Forward") # Add a simple objective so we can calculate the backward pass. objective = tf.nn.l2_loss(logits) # Compute the gradient with respect to all the parameters. grad = tf.gradients(objective, tf.trainable_variables()) # Run the backward benchmark. time_tensorflow_run(sess, grad, "Forward-backward")
def testBuild(self): batch_size = 5 height, width = 224, 224 num_classes = 1000 with self.test_session(): inputs = tf.random_uniform((batch_size, height, width, 3)) logits, end_points = mobilenet.mobilenet(inputs, num_classes) self.assertEquals(end_points['MobileNet/conv_ds_2/depthwise_conv'].get_shape().as_list(), [5, 112, 112, 32]) self.assertEquals(end_points['MobileNet/conv_ds_3/depthwise_conv'].get_shape().as_list(), [5, 56, 56, 64]) self.assertEquals(end_points['MobileNet/conv_ds_4/depthwise_conv'].get_shape().as_list(), [5, 56, 56, 128]) self.assertEquals(end_points['MobileNet/conv_ds_5/depthwise_conv'].get_shape().as_list(), [5, 28, 28, 128]) self.assertEquals(end_points['MobileNet/conv_ds_6/depthwise_conv'].get_shape().as_list(), [5, 28, 28, 256]) self.assertEquals(end_points['MobileNet/conv_ds_7/depthwise_conv'].get_shape().as_list(), [5, 14, 14, 256]) self.assertEquals(end_points['MobileNet/conv_ds_8/depthwise_conv'].get_shape().as_list(), [5, 14, 14, 512]) self.assertEquals(end_points['MobileNet/conv_ds_9/depthwise_conv'].get_shape().as_list(), [5, 14, 14, 512]) self.assertEquals(end_points['MobileNet/conv_ds_10/depthwise_conv'].get_shape().as_list(), [5, 14, 14, 512]) self.assertEquals(end_points['MobileNet/conv_ds_11/depthwise_conv'].get_shape().as_list(), [5, 14, 14, 512]) self.assertEquals(end_points['MobileNet/conv_ds_12/depthwise_conv'].get_shape().as_list(), [5, 14, 14, 512]) self.assertEquals(end_points['MobileNet/conv_ds_13/depthwise_conv'].get_shape().as_list(), [5, 7, 7, 512]) self.assertEquals(end_points['MobileNet/conv_ds_14/depthwise_conv'].get_shape().as_list(), [5, 7, 7, 1024]) self.assertEquals(end_points['squeeze'].get_shape().as_list(), [5, 1024]) self.assertEquals(logits.op.name, 'MobileNet/fc_16/BiasAdd') self.assertListEqual(logits.get_shape().as_list(), [batch_size, num_classes])
def SSD300(input_shape, num_classes=21): # 300,300,3 input_tensor = Input(shape=input_shape) img_size = (input_shape[1], input_shape[0]) # SSD结构,net字典 net = mobilenet(input_tensor) #-----------------------将提取到的主干特征进行处理---------------------------# num_priors = 4 # 预测框的处理 # num_priors表示每个网格点先验框的数量,4是x,y,h,w的调整 net['conv4_3_loc'] = Conv2D(num_priors * 4, kernel_size=(3, 3), padding='same', name='conv4_3_loc')(net['conv4_3']) net['conv4_3_loc_flat'] = Flatten(name='conv4_3_loc_flat')( net['conv4_3_loc']) # num_priors表示每个网格点先验框的数量,num_classes是所分的类 net['conv4_3_conf'] = Conv2D(num_priors * num_classes, kernel_size=(3, 3), padding='same', name='conv4_3_conf')(net['conv4_3']) net['conv4_3_conf_flat'] = Flatten(name='conv4_3_conf_flat')( net['conv4_3_conf']) priorbox = PriorBox(img_size, 30.0, max_size=60.0, aspect_ratios=[2], variances=[0.1, 0.1, 0.2, 0.2], name='conv4_3_priorbox') net['conv4_3_priorbox'] = priorbox(net['conv4_3']) # 对fc7层进行处理 num_priors = 6 # 预测框的处理 # num_priors表示每个网格点先验框的数量,4是x,y,h,w的调整 net['fc7_mbox_loc'] = Conv2D(num_priors * 4, kernel_size=(3, 3), padding='same', name='fc7_mbox_loc')(net['fc7']) net['fc7_mbox_loc_flat'] = Flatten(name='fc7_mbox_loc_flat')( net['fc7_mbox_loc']) # num_priors表示每个网格点先验框的数量,num_classes是所分的类 net['fc7_mbox_conf'] = Conv2D(num_priors * num_classes, kernel_size=(3, 3), padding='same', name='fc7_mbox_conf')(net['fc7']) net['fc7_mbox_conf_flat'] = Flatten(name='fc7_mbox_conf_flat')( net['fc7_mbox_conf']) priorbox = PriorBox(img_size, 60.0, max_size=111.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='fc7_mbox_priorbox') net['fc7_mbox_priorbox'] = priorbox(net['fc7']) # 对conv6_2进行处理 num_priors = 6 # 预测框的处理 # num_priors表示每个网格点先验框的数量,4是x,y,h,w的调整 x = Conv2D(num_priors * 4, kernel_size=(3, 3), padding='same', name='conv6_2_mbox_loc')(net['conv6_2']) net['conv6_2_mbox_loc'] = x net['conv6_2_mbox_loc_flat'] = Flatten(name='conv6_2_mbox_loc_flat')( net['conv6_2_mbox_loc']) # num_priors表示每个网格点先验框的数量,num_classes是所分的类 x = Conv2D(num_priors * num_classes, kernel_size=(3, 3), padding='same', name='conv6_2_mbox_conf')(net['conv6_2']) net['conv6_2_mbox_conf'] = x net['conv6_2_mbox_conf_flat'] = Flatten(name='conv6_2_mbox_conf_flat')( net['conv6_2_mbox_conf']) priorbox = PriorBox(img_size, 111.0, max_size=162.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv6_2_mbox_priorbox') net['conv6_2_mbox_priorbox'] = priorbox(net['conv6_2']) # 对conv7_2进行处理 num_priors = 6 # 预测框的处理 # num_priors表示每个网格点先验框的数量,4是x,y,h,w的调整 x = Conv2D(num_priors * 4, kernel_size=(3, 3), padding='same', name='conv7_2_mbox_loc')(net['conv7_2']) net['conv7_2_mbox_loc'] = x net['conv7_2_mbox_loc_flat'] = Flatten(name='conv7_2_mbox_loc_flat')( net['conv7_2_mbox_loc']) # num_priors表示每个网格点先验框的数量,num_classes是所分的类 x = Conv2D(num_priors * num_classes, kernel_size=(3, 3), padding='same', name='conv7_2_mbox_conf')(net['conv7_2']) net['conv7_2_mbox_conf'] = x net['conv7_2_mbox_conf_flat'] = Flatten(name='conv7_2_mbox_conf_flat')( net['conv7_2_mbox_conf']) priorbox = PriorBox(img_size, 162.0, max_size=213.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv7_2_mbox_priorbox') net['conv7_2_mbox_priorbox'] = priorbox(net['conv7_2']) # 对conv8_2进行处理 num_priors = 4 # 预测框的处理 # num_priors表示每个网格点先验框的数量,4是x,y,h,w的调整 x = Conv2D(num_priors * 4, kernel_size=(3, 3), padding='same', name='conv8_2_mbox_loc')(net['conv8_2']) net['conv8_2_mbox_loc'] = x net['conv8_2_mbox_loc_flat'] = Flatten(name='conv8_2_mbox_loc_flat')( net['conv8_2_mbox_loc']) # num_priors表示每个网格点先验框的数量,num_classes是所分的类 x = Conv2D(num_priors * num_classes, kernel_size=(3, 3), padding='same', name='conv8_2_mbox_conf')(net['conv8_2']) net['conv8_2_mbox_conf'] = x net['conv8_2_mbox_conf_flat'] = Flatten(name='conv8_2_mbox_conf_flat')( net['conv8_2_mbox_conf']) priorbox = PriorBox(img_size, 213.0, max_size=264.0, aspect_ratios=[2], variances=[0.1, 0.1, 0.2, 0.2], name='conv8_2_mbox_priorbox') net['conv8_2_mbox_priorbox'] = priorbox(net['conv8_2']) # 对conv9_2进行处理 num_priors = 4 # 预测框的处理 # num_priors表示每个网格点先验框的数量,4是x,y,h,w的调整 x = Conv2D(num_priors * 4, kernel_size=(3, 3), padding='same', name='conv9_2_mbox_loc')(net['conv9_2']) net['conv9_2_mbox_loc'] = x net['conv9_2_mbox_loc_flat'] = Flatten(name='conv9_2_mbox_loc_flat')( net['conv9_2_mbox_loc']) # num_priors表示每个网格点先验框的数量,num_classes是所分的类 x = Conv2D(num_priors * num_classes, kernel_size=(3, 3), padding='same', name='conv9_2_mbox_conf')(net['conv9_2']) net['conv9_2_mbox_conf'] = x net['conv9_2_mbox_conf_flat'] = Flatten(name='conv9_2_mbox_conf_flat')( net['conv9_2_mbox_conf']) priorbox = PriorBox(img_size, 264.0, max_size=315.0, aspect_ratios=[2], variances=[0.1, 0.1, 0.2, 0.2], name='conv9_2_mbox_priorbox') net['conv9_2_mbox_priorbox'] = priorbox(net['conv9_2']) # 将所有结果进行堆叠 net['mbox_loc'] = concatenate([ net['conv4_3_loc_flat'], net['fc7_mbox_loc_flat'], net['conv6_2_mbox_loc_flat'], net['conv7_2_mbox_loc_flat'], net['conv8_2_mbox_loc_flat'], net['conv9_2_mbox_loc_flat'] ], axis=1, name='mbox_loc') net['mbox_conf'] = concatenate([ net['conv4_3_conf_flat'], net['fc7_mbox_conf_flat'], net['conv6_2_mbox_conf_flat'], net['conv7_2_mbox_conf_flat'], net['conv8_2_mbox_conf_flat'], net['conv9_2_mbox_conf_flat'] ], axis=1, name='mbox_conf') net['mbox_priorbox'] = concatenate([ net['conv4_3_priorbox'], net['fc7_mbox_priorbox'], net['conv6_2_mbox_priorbox'], net['conv7_2_mbox_priorbox'], net['conv8_2_mbox_priorbox'], net['conv9_2_mbox_priorbox'] ], axis=1, name='mbox_priorbox') # if hasattr(net['mbox_loc'], '_keras_shape'): # num_boxes = net['mbox_loc']._keras_shape[-1] // 4 # elif hasattr(net['mbox_loc'], 'int_shape'): num_boxes = K.int_shape(net['mbox_loc'])[-1] // 4 # 8732,4 net['mbox_loc'] = Reshape((num_boxes, 4), name='mbox_loc_final')(net['mbox_loc']) # 8732,21 net['mbox_conf'] = Reshape((num_boxes, num_classes), name='mbox_conf_logits')(net['mbox_conf']) net['mbox_conf'] = Activation('softmax', name='mbox_conf_final')(net['mbox_conf']) net['predictions'] = concatenate( [net['mbox_loc'], net['mbox_conf'], net['mbox_priorbox']], axis=2, name='predictions') net['predictions'] = tf.cast(net['predictions'], tf.float64) print(net['predictions']) model = tf.keras.Model(input_tensor, net['predictions']) return model
def SSD300(input_shape, num_classes=21, anchors_size=[30, 60, 111, 162, 213, 264, 315]): #---------------------------------# # 典型的输入大小为[300,300,3] #---------------------------------# input_tensor = Input(shape=input_shape) # net变量里面包含了整个SSD的结构,通过层名可以找到对应的特征层 net = mobilenet(input_tensor) #-----------------------将提取到的主干特征进行处理---------------------------# # 对conv4_3的通道进行l2标准化处理 # 38,38,512 num_priors = 4 # 预测框的处理 # num_priors表示每个网格点先验框的数量,4是x,y,h,w的调整 net['conv4_3_loc'] = Conv2D(num_priors * 4, kernel_size=(3, 3), padding='same', name='conv4_3_loc')(net['conv4_3']) net['conv4_3_loc_flat'] = Flatten(name='conv4_3_loc_flat')( net['conv4_3_loc']) # num_priors表示每个网格点先验框的数量,num_classes是所分的类 net['conv4_3_conf'] = Conv2D(num_priors * num_classes, kernel_size=(3, 3), padding='same', name='conv4_3_conf')(net['conv4_3']) net['conv4_3_conf_flat'] = Flatten(name='conv4_3_conf_flat')( net['conv4_3_conf']) priorbox = PriorBox(input_shape, anchors_size[0], max_size=anchors_size[1], aspect_ratios=[2], variances=[0.1, 0.1, 0.2, 0.2], name='conv4_3_priorbox') net['conv4_3_priorbox'] = priorbox(net['conv4_3']) # 对fc7层进行处理 # 19,19,1024 num_priors = 6 # 预测框的处理 # num_priors表示每个网格点先验框的数量,4是x,y,h,w的调整 net['fc7_mbox_loc'] = Conv2D(num_priors * 4, kernel_size=(3, 3), padding='same', name='fc7_mbox_loc')(net['fc7']) net['fc7_mbox_loc_flat'] = Flatten(name='fc7_mbox_loc_flat')( net['fc7_mbox_loc']) # num_priors表示每个网格点先验框的数量,num_classes是所分的类 net['fc7_mbox_conf'] = Conv2D(num_priors * num_classes, kernel_size=(3, 3), padding='same', name='fc7_mbox_conf')(net['fc7']) net['fc7_mbox_conf_flat'] = Flatten(name='fc7_mbox_conf_flat')( net['fc7_mbox_conf']) priorbox = PriorBox(input_shape, anchors_size[1], max_size=anchors_size[2], aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='fc7_mbox_priorbox') net['fc7_mbox_priorbox'] = priorbox(net['fc7']) # 对conv6_2进行处理 # 10,10,512 num_priors = 6 # 预测框的处理 # num_priors表示每个网格点先验框的数量,4是x,y,h,w的调整 x = Conv2D(num_priors * 4, kernel_size=(3, 3), padding='same', name='conv6_2_mbox_loc')(net['conv6_2']) net['conv6_2_mbox_loc'] = x net['conv6_2_mbox_loc_flat'] = Flatten(name='conv6_2_mbox_loc_flat')( net['conv6_2_mbox_loc']) # num_priors表示每个网格点先验框的数量,num_classes是所分的类 x = Conv2D(num_priors * num_classes, kernel_size=(3, 3), padding='same', name='conv6_2_mbox_conf')(net['conv6_2']) net['conv6_2_mbox_conf'] = x net['conv6_2_mbox_conf_flat'] = Flatten(name='conv6_2_mbox_conf_flat')( net['conv6_2_mbox_conf']) priorbox = PriorBox(input_shape, anchors_size[2], max_size=anchors_size[3], aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv6_2_mbox_priorbox') net['conv6_2_mbox_priorbox'] = priorbox(net['conv6_2']) # 对conv7_2进行处理 # 5,5,256 num_priors = 6 # 预测框的处理 # num_priors表示每个网格点先验框的数量,4是x,y,h,w的调整 x = Conv2D(num_priors * 4, kernel_size=(3, 3), padding='same', name='conv7_2_mbox_loc')(net['conv7_2']) net['conv7_2_mbox_loc'] = x net['conv7_2_mbox_loc_flat'] = Flatten(name='conv7_2_mbox_loc_flat')( net['conv7_2_mbox_loc']) # num_priors表示每个网格点先验框的数量,num_classes是所分的类 x = Conv2D(num_priors * num_classes, kernel_size=(3, 3), padding='same', name='conv7_2_mbox_conf')(net['conv7_2']) net['conv7_2_mbox_conf'] = x net['conv7_2_mbox_conf_flat'] = Flatten(name='conv7_2_mbox_conf_flat')( net['conv7_2_mbox_conf']) priorbox = PriorBox(input_shape, anchors_size[3], max_size=anchors_size[4], aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv7_2_mbox_priorbox') net['conv7_2_mbox_priorbox'] = priorbox(net['conv7_2']) # 对conv8_2进行处理 # 3,3,256 num_priors = 4 # 预测框的处理 # num_priors表示每个网格点先验框的数量,4是x,y,h,w的调整 x = Conv2D(num_priors * 4, kernel_size=(3, 3), padding='same', name='conv8_2_mbox_loc')(net['conv8_2']) net['conv8_2_mbox_loc'] = x net['conv8_2_mbox_loc_flat'] = Flatten(name='conv8_2_mbox_loc_flat')( net['conv8_2_mbox_loc']) # num_priors表示每个网格点先验框的数量,num_classes是所分的类 x = Conv2D(num_priors * num_classes, kernel_size=(3, 3), padding='same', name='conv8_2_mbox_conf')(net['conv8_2']) net['conv8_2_mbox_conf'] = x net['conv8_2_mbox_conf_flat'] = Flatten(name='conv8_2_mbox_conf_flat')( net['conv8_2_mbox_conf']) priorbox = PriorBox(input_shape, anchors_size[4], max_size=anchors_size[5], aspect_ratios=[2], variances=[0.1, 0.1, 0.2, 0.2], name='conv8_2_mbox_priorbox') net['conv8_2_mbox_priorbox'] = priorbox(net['conv8_2']) # 对conv9_2进行处理 # 1,1,256 num_priors = 4 # 预测框的处理 # num_priors表示每个网格点先验框的数量,4是x,y,h,w的调整 x = Conv2D(num_priors * 4, kernel_size=(3, 3), padding='same', name='conv9_2_mbox_loc')(net['conv9_2']) net['conv9_2_mbox_loc'] = x net['conv9_2_mbox_loc_flat'] = Flatten(name='conv9_2_mbox_loc_flat')( net['conv9_2_mbox_loc']) # num_priors表示每个网格点先验框的数量,num_classes是所分的类 x = Conv2D(num_priors * num_classes, kernel_size=(3, 3), padding='same', name='conv9_2_mbox_conf')(net['conv9_2']) net['conv9_2_mbox_conf'] = x net['conv9_2_mbox_conf_flat'] = Flatten(name='conv9_2_mbox_conf_flat')( net['conv9_2_mbox_conf']) priorbox = PriorBox(input_shape, anchors_size[5], max_size=anchors_size[6], aspect_ratios=[2], variances=[0.1, 0.1, 0.2, 0.2], name='conv9_2_mbox_priorbox') net['conv9_2_mbox_priorbox'] = priorbox(net['conv9_2']) # 将所有结果进行堆叠 net['mbox_loc'] = concatenate([ net['conv4_3_loc_flat'], net['fc7_mbox_loc_flat'], net['conv6_2_mbox_loc_flat'], net['conv7_2_mbox_loc_flat'], net['conv8_2_mbox_loc_flat'], net['conv9_2_mbox_loc_flat'] ], axis=1, name='mbox_loc') net['mbox_conf'] = concatenate([ net['conv4_3_conf_flat'], net['fc7_mbox_conf_flat'], net['conv6_2_mbox_conf_flat'], net['conv7_2_mbox_conf_flat'], net['conv8_2_mbox_conf_flat'], net['conv9_2_mbox_conf_flat'] ], axis=1, name='mbox_conf') net['mbox_priorbox'] = concatenate([ net['conv4_3_priorbox'], net['fc7_mbox_priorbox'], net['conv6_2_mbox_priorbox'], net['conv7_2_mbox_priorbox'], net['conv8_2_mbox_priorbox'], net['conv9_2_mbox_priorbox'] ], axis=1, name='mbox_priorbox') # 8732,4 net['mbox_loc'] = Reshape((-1, 4), name='mbox_loc_final')(net['mbox_loc']) # 8732,21 net['mbox_conf'] = Reshape((-1, num_classes), name='mbox_conf_logits')(net['mbox_conf']) # 8732,8 net['mbox_conf'] = Activation('softmax', name='mbox_conf_final')(net['mbox_conf']) # 8732,33 net['predictions'] = concatenate( [net['mbox_loc'], net['mbox_conf'], net['mbox_priorbox']], axis=2, name='predictions') model = Model(input_tensor, net['predictions']) return model
import tensorflow as tf from nets.mobilenet import mobilenet from tensorflow.python.training import saver as saver_lib from tensorflow.python import pywrap_tensorflow input_checkpoint = '/home/zehao/PycharmProjects/MobileNet/mobilenet-model/model.ckpt-439074' # Where to save the modified graph save_path = '/home/zehao/PycharmProjects/MobileNet/mobilenet-model/with_placeholder' # TODO(shizehao): use graph editor library insead with tf.Graph().as_default() as graph: input_images = tf.placeholder(tf.float32, [None, 224, 224, 3], 'MobileNet/input_images') logits, predictions = mobilenet(inputs=input_images, num_classes=1001, is_training=False) saver = tf.train.Saver() with tf.Session() as sess: var_list = {} reader = pywrap_tensorflow.NewCheckpointReader(input_checkpoint) var_to_shape_map = reader.get_variable_to_shape_map() for key in var_to_shape_map: try: tensor = sess.graph.get_tensor_by_name(key + ":0") except KeyError: # This tensor doesn't exist in the graph (for example it's # 'global_step' or a similar housekeeping element) so skip it. continue var_list[key] = tensor saver = saver_lib.Saver(var_list=var_list)
def mobilenet(input_tensor, num_classes=1001, depth_multiplier=1.0, scope='MobilenetV2', conv_defs=None, finegrain_classification_mode=False, min_depth=None, divisible_by=None, **kwargs): """Creates mobilenet V2 network. Inference mode is created by default. To create training use training_scope below. with tf.contrib.slim.arg_scope(mobilenet_v2.training_scope()): logits, endpoints = mobilenet_v2.mobilenet(input_tensor) Args: input_tensor: The input tensor num_classes: number of classes depth_multiplier: The multiplier applied to scale number of channels in each layer. Note: this is called depth multiplier in the paper but the name is kept for consistency with slim's model builder. scope: Scope of the operator conv_defs: Allows to override default conv def. finegrain_classification_mode: When set to True, the model will keep the last layer large even for small multipliers. Following https://arxiv.org/abs/1801.04381 suggests that it improves performance for ImageNet-type of problems. *Note* ignored if final_endpoint makes the builder exit earlier. min_depth: If provided, will ensure that all layers will have that many channels after application of depth multiplier. divisible_by: If provided will ensure that all layers # channels will be divisible by this number. **kwargs: passed directly to mobilenet.mobilenet: prediction_fn- what prediction function to use. reuse-: whether to reuse variables (if reuse set to true, scope must be given). Returns: logits/endpoints pair Raises: ValueError: On invalid arguments """ if conv_defs is None: conv_defs = V2_DEF if 'multiplier' in kwargs: raise ValueError( 'mobilenetv2 doesn\'t support generic ' 'multiplier parameter use "depth_multiplier" instead.') if finegrain_classification_mode: conv_defs = copy.deepcopy(conv_defs) if depth_multiplier < 1: conv_defs['spec'][-1].params['num_outputs'] /= depth_multiplier depth_args = {} # NB: do not set depth_args unless they are provided to avoid overriding # whatever default depth_multiplier might have thanks to arg_scope. if min_depth is not None: depth_args['min_depth'] = min_depth if divisible_by is not None: depth_args['divisible_by'] = divisible_by with slim.arg_scope((lib.depth_multiplier, ), **depth_args): return lib.mobilenet(input_tensor, num_classes=num_classes, conv_defs=conv_defs, scope=scope, multiplier=depth_multiplier, **kwargs)
slim = tf.contrib.slim IMAGENET_CLASSES = 1001 with tf.Session() as sess: with open('final1.jpg', 'rb') as f: image = f.read() image = tf.image.decode_jpeg(image, channels=0) image = tf.reshape(image, (1825, 2738, 3)) image = preprocess_for_eval(image, 224, 224) image = tf.expand_dims(image, axis=0) logits, _ = mobilenet(image, num_classes=IMAGENET_CLASSES, is_training=False) softs = tf.nn.softmax(logits) top_5 = tf.nn.top_k(softs, k=5) tf.global_variables_initializer().run() saver = tf.train.Saver() print('restoring weights') saver.restore(sess, 'weights/model.ckpt-906808') _, logits_out, top_5_out = sess.run([image, logits, top_5]) values, indices = top_5_out.values, top_5_out.indices
import tensorflow as tf from nets.mobilenet import mobilenet from tensorflow.python.training import saver as saver_lib from tensorflow.python import pywrap_tensorflow input_checkpoint = '/home/zehao/PycharmProjects/MobileNet/mobilenet-model/model.ckpt-439074' # Where to save the modified graph save_path = '/home/zehao/PycharmProjects/MobileNet/mobilenet-model/with_placeholder' # TODO(shizehao): use graph editor library insead with tf.Graph().as_default() as graph: input_images = tf.placeholder(tf.float32, [None, 224, 224, 3], 'MobileNet/input_images') logits, predictions = mobilenet(inputs=input_images, num_classes=1001, is_training=False) saver = tf.train.Saver() with tf.Session() as sess: var_list = {} reader = pywrap_tensorflow.NewCheckpointReader(input_checkpoint) var_to_shape_map = reader.get_variable_to_shape_map() for key in var_to_shape_map: try: tensor = sess.graph.get_tensor_by_name(key + ":0") except KeyError: # This tensor doesn't exist in the graph (for example it's # 'global_step' or a similar housekeeping element) so skip it. continue var_list[key] = tensor saver = saver_lib.Saver(var_list=var_list) # Restore variables saver.restore(sess, input_checkpoint)