def testMobilenetBase(self): tf.reset_default_graph() # Verifies that mobilenet_base returns pre-pooling layer. model = MobilenetV2(depth_multiplier=0.1, min_depth=32) net, _ = model.forward_base( tf.placeholder(tf.float32, (10, 224, 224, 16))) self.assertEqual(net.get_shape().as_list(), [10, 7, 7, 128])
def testDivisibleByWithMultiplier(self): tf.reset_default_graph() model = MobilenetV2(depth_multiplier=0.1, min_depth=32) net, _ = model.forward(tf.placeholder(tf.float32, (10, 224, 224, 16))) s = [ op.outputs[0].get_shape().as_list()[-1] for op in find_ops('Conv2D') ] s = sorted(np.unique(s)) self.assertAllEqual(sorted([32, 192, 128, 1001]), s)
def testDivisibleBy(self): tf.reset_default_graph() model = MobilenetV2(divisible_by=16, min_depth=32) net, _ = model.forward(tf.placeholder(tf.float32, (10, 224, 224, 16))) s = [ op.outputs[0].get_shape().as_list()[-1] for op in find_ops('Conv2D') ] s = sorted(np.unique(s)) self.assertAllEqual( [32, 64, 96, 160, 192, 320, 384, 576, 960, 1001, 1280], s)
def testImageSizes(self): model = MobilenetV2() for input_size, output_size in [(224, 7), (192, 6), (160, 5), (128, 4), (96, 3)]: tf.reset_default_graph() net, endpoints = model.forward( tf.placeholder(tf.float32, (10, input_size, input_size, 16))) self.assertEqual( endpoints['layer_18/output'].get_shape().as_list()[1:3], [output_size] * 2)
def main(unused_argv): tf.reset_default_graph() # For simplicity we just decode jpeg inside tensorflow. # But one can provide any input obviously. file_input = tf.placeholder(tf.string, ()) image = tf.image.decode_jpeg(tf.read_file(file_input)) images = tf.expand_dims(image, 0) images = tf.cast(images, tf.float32) / 128. - 1 images.set_shape((None, None, None, 3)) images = tf.image.resize_images(images, (224, 224)) mobilenet_model = MobilenetV2() logits, endpoints = mobilenet_model.forward( images, is_training=False) exclude = ['global_step'] variables_to_restore = tf.contrib.slim.get_variables_to_restore( exclude=exclude) variables_map = {} for v in variables_to_restore: # print(v.name) old_name = v.name.split(':')[0] old_name = old_name.replace('conv2d/kernel', 'weights') old_name = old_name.replace('conv2d/bias', 'biases') variables_map[old_name] = v tf.train.init_from_checkpoint(FLAGS.pretrained_model_dir, variables_map) # download panda sample image #filename, _ = urllib.urlretrieve( # 'https://upload.wikimedia.org/wikipedia/commons/f/fe/Giant_Panda_in_Beijing_Zoo_1.JPG') # noqa filename = '/home/work/liuqi/datasets/imagenet/validation/ILSVRC2012_val_00000001.JPEG' saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) x = endpoints['Predictions'].eval( feed_dict={file_input: filename}) if not os.path.exists(FLAGS.output_dir): os.makedirs(FLAGS.output_dir) saver.save(sess, FLAGS.output_dir + '/mobilenet-v2-224.ckpt') # validation label_map = create_readable_names_for_imagenet_labels() # output: ('Top 1 prediction: ', 389, 'giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca', 0.83625776) # noqa print("Top 1 prediction: ", x.argmax(), label_map[x.argmax()], x.max())
def testCreation(self): model = MobilenetV2() net, endpoints = model.forward( tf.placeholder(tf.float32, (10, 224, 224, 16))) # for t in net.graph.get_operations(): # print(t.name) spec = model.model_def() num_convs = len(find_ops('Conv2D')) # This is mostly a sanity test. No deep reason for these particular # constants. # # All but first 2 and last one have two convolutions, and there is one # extra conv that is not in the spec. (logits) self.assertEqual(num_convs, len(spec['spec']) * 2 - 3 + 1) # Check that depthwise are exposed. for i in range(2, 17): self.assertIn('layer_%d/depthwise_output' % i, endpoints)
def encode(self, input_tensor, is_training=True): # extract features if self.backbone == 'MobilenetV2': mobilenet_model = MobilenetV2( self.output_stride, self.depth_multiplier, min_depth=8 if self.depth_multiplier == 1.0 else 1, divisible_by=8 if self.depth_multiplier == 1.0 else 1, quant_friendly=self.quant_friendly) else: # MobilenetV3 mobilenet_model = MobilenetV3( output_stride=self.output_stride, quant_friendly=self.quant_friendly) features, endpoints = mobilenet_model.forward_base( input_tensor, BACKBONE_INFO[self.backbone]['final_endpoint'], is_training=is_training) # add extra losses self.losses_list.extend(mobilenet_model.losses()) if self.pretrained_backbone_model_dir and is_training: base_architecture = self.backbone exclude = [base_architecture + '/Logits', 'global_step'] variables_to_restore = tf.contrib.slim.get_variables_to_restore( exclude=exclude) tf.logging.info('init from %s model' % base_architecture) tf.train.init_from_checkpoint(self.pretrained_backbone_model_dir, {v.name.split(':')[0]: v for v in variables_to_restore}) features = self._atrous_spatial_pyramid_pooling( features, weight_decay=self.weight_decay, is_training=is_training) return features, endpoints
def testWithOutputStride8(self): model = MobilenetV2(output_stride=8) net, _ = model.forward_base( tf.placeholder(tf.float32, (10, 224, 224, 16))) self.assertEqual(net.get_shape().as_list()[1:3], [28, 28])
def testCreationNoClasses(self): model = MobilenetV2() net, endpoints = model.forward(tf.placeholder(tf.float32, (10, 224, 224, 16)), num_classes=None) self.assertIs(net, endpoints['global_pool'])
def testWithOutputStride16(self): tf.reset_default_graph() model = MobilenetV2(output_stride=16) net, _ = model.forward_base( tf.placeholder(tf.float32, (10, 224, 224, 16))) self.assertEqual(net.get_shape().as_list()[1:3], [14, 14])