def testForward(self): batch_size = 1 height, width = 224, 224 with self.test_session() as sess: random_input = tf.random_uniform(shape=(batch_size, height, width, 3)) new_input = tf.placeholder(dtype=tf.float32, shape=[batch_size, height, width, 3], name='input_holder') logits, predictions = mobilenet.mobilenet(new_input) sess.run(tf.global_variables_initializer()) writer = tf.summary.FileWriter('./graphs', sess.graph) val = sess.run(random_input) output = sess.run(logits, feed_dict={new_input: val}) self.assertTrue(output.any()) saver = tf.train.Saver() save_path = saver.save(sess, "./graphs/model.ckpt") print(save_path) #tf.train.write_graph(sess.graph_def, '.','model.pbtxt') print("Writing complete...") ops_list = sess.graph.get_operations() tensor_list = np.array([ops.values() for ops in ops_list]) for t in tensor_list: print(t) tf.saved_model.simple_save(sess, './saved', inputs={"model_input": new_input}, outputs={"model_output": predictions}) writer.close()
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 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])
if __name__ == '__main__': """ python grad_cam.py <path_to_image> 1. Loads an image with opencv. 2. Preprocesses it for VGG19 and converts to a pytorch variable. 3. Makes a forward pass to find the category index with the highest score, and computes intermediate activations. Makes the visualization. """ args = get_args() device = torch.device('cuda:2' if torch.cuda.is_available() else 'cpu') # Can work with any model, but it assumes that the model has a # feature method, and a classifier method, # as in the VGG models in torchvision. model = mobilenet() model.load(args.pth) grad_cam = GradCam(model=model, feature_module=model.features, target_layer_names=["features"], use_cuda=args.use_cuda) image = Image.open(args.image_path).convert('RGB') tf = transforms.Compose([ transforms.Resize((112, 224)), transforms.ToTensor(), ]) image = tf(image) input = image.to(device) input = input.unsqueeze(0) # If None, returns the map for the highest scoring category.