コード例 #1
0
 def test_show_image_text_np(self):
     text = "apple"
     fig, axis = plt.subplots(1, 1, figsize=(6.4, 4.8))
     fe.util.show_image(text, fig=fig, axis=axis)
     obj1 = fig_to_rgb_array(fig)
     obj2 = self.text_img_ans
     self.assertTrue(check_img_similar(obj1, obj2))
コード例 #2
0
 def test_show_image_title_np(self):
     img = np.ones((150, 150), dtype=np.uint8) * 255
     fig, axis = plt.subplots(1, 1, figsize=(6.4, 4.8))
     fe.util.show_image(img, fig=fig, axis=axis, title="test title")
     obj1 = fig_to_rgb_array(fig)
     obj2 = self.title_img_ans
     self.assertTrue(check_img_similar(obj1, obj2))
コード例 #3
0
 def test_tf_output(self):
     cutmix = CutMixBatch(inputs='x', outputs=['x', 'lam'])
     cutmix.beta = MockBetaDistribution('tf')
     cutmix.uniform = MockUniformDistribution('tf')
     mixed_images = cutmix.forward(data=self.tf_input, state={})
     images = mixed_images[0].numpy()
     lam = mixed_images[1].numpy()
     with self.subTest('First mixed image'):
         self.assertTrue(
             check_img_similar(images[0],
                               img_to_rgb_array(self.cutmix_output1)))
     with self.subTest('Second mixed image'):
         self.assertTrue(
             check_img_similar(images[1],
                               img_to_rgb_array(self.cutmix_output2)))
     with self.subTest('lambda value'):
         self.assertEqual(round(float(lam), 2), 0.65)
コード例 #4
0
    def test_show_image_height_width_np(self):
        img = np.zeros((150, 100))

        fig, axis = plt.subplots(1, 1, figsize=(6.4, 4.8))
        fe.util.show_image(img, fig=fig, axis=axis)
        obj1 = fig_to_rgb_array(fig)
        obj2 = self.hw_ratio_img_ans
        self.assertTrue(check_img_similar(obj1, obj2))
コード例 #5
0
    def test_saliency(self):
        fe.estimator.enable_deterministic(200)
        label_mapping = {
            'airplane': 0,
            'automobile': 1,
            'bird': 2,
            'cat': 3,
            'deer': 4,
            'dog': 5,
            'frog': 6,
            'horse': 7,
            'ship': 8,
            'truck': 9
        }

        batch_size = 32

        train_data, eval_data = cifar10.load_data()
        pipeline = fe.Pipeline(test_data=train_data,
                               batch_size=batch_size,
                               ops=[Normalize(inputs="x", outputs="x")],
                               num_process=0)

        weight_path = os.path.abspath(
            os.path.join(__file__, "..", "resources", "lenet_cifar10_tf.h5"))

        model = fe.build(model_fn=lambda: LeNet(input_shape=(32, 32, 3)),
                         optimizer_fn="adam",
                         weights_path=weight_path)
        network = fe.Network(
            ops=[ModelOp(model=model, inputs="x", outputs="y_pred")])

        save_dir = tempfile.mkdtemp()
        traces = [
            Saliency(model=model,
                     model_inputs="x",
                     class_key="y",
                     model_outputs="y_pred",
                     samples=5,
                     label_mapping=label_mapping),
            ImageSaver(inputs="saliency", save_dir=save_dir)
        ]

        estimator = fe.Estimator(pipeline=pipeline,
                                 network=network,
                                 epochs=5,
                                 traces=traces,
                                 log_steps=1000)
        estimator.test()

        ans_img_path = os.path.abspath(
            os.path.join(__file__, "..", "resources", "saliency_figure.png"))
        ans_img = img_to_rgb_array(ans_img_path)
        output_img_path = os.path.join(save_dir, "saliency_test_epoch_5.png")
        output_img = img_to_rgb_array(output_img_path)
        self.assertTrue(check_img_similar(output_img, ans_img))
コード例 #6
0
    def test_show_image_check_float_0_to_1_np(self):
        img = np.zeros((256, 256, 3), dtype=np.float32)
        for x in range(256):
            img[x, :, :] = x / 255

        fig, axis = plt.subplots(1, 1, figsize=(6.4, 4.8))
        fe.util.show_image(img, fig=fig, axis=axis)
        obj1 = fig_to_rgb_array(fig)
        obj2 = self.float_img_ans
        self.assertTrue(check_img_similar(obj1, obj2))
コード例 #7
0
    def test_show_image_color_arbitrary_range_np(self):
        img = np.zeros((256, 256, 3), dtype=np.float32)
        for x in range(256):
            img[x, :, :] = x * 0.2

        fig, axis = plt.subplots(1, 1)
        fe.util.show_image(img, fig=fig, axis=axis)
        obj1 = fig_to_rgb_array(fig)
        obj2 = self.float_img_ans
        self.assertTrue(check_img_similar(obj1, obj2))
コード例 #8
0
    def test_show_image_color_tf(self):
        img = np.zeros((90, 90, 3), dtype=np.uint8)
        img[:, 0:30, :] = np.array([255, 0, 0])
        img[:, 30:60, :] = np.array([0, 255, 0])
        img[:, 60:90, :] = np.array([0, 0, 255])
        img = tf.convert_to_tensor(img)

        fig, axis = plt.subplots(1, 1, figsize=(6.4, 4.8))
        fe.util.show_image(img, fig=fig, axis=axis)
        obj1 = fig_to_rgb_array(fig)
        obj2 = self.color_img_ans
        self.assertTrue(check_img_similar(obj1, obj2))
コード例 #9
0
    def test_show_image_mixed_figure_layer_np(self):
        bg_img = np.ones((150, 150, 3), dtype=np.uint8) * 255
        boxes = np.array([[0, 0, 10, 20], [10, 20, 30, 50], [40, 70, 200,
                                                             200]])

        fig, axis = plt.subplots(1, 1, figsize=(6.4, 4.8))
        fe.util.show_image(bg_img, fig=fig, axis=axis)
        fe.util.show_image(boxes, fig=fig, axis=axis)
        fe.util.show_image("apple", fig=fig, axis=axis)
        obj1 = fig_to_rgb_array(fig)
        obj2 = self.mixed_img_ans
        self.assertTrue(check_img_similar(obj1, obj2))
コード例 #10
0
    def test_show_image_color_torch(self):
        img = np.zeros((90, 90, 3), dtype=np.uint8)
        img[:, 0:30, :] = np.array([255, 0, 0])
        img[:, 30:60, :] = np.array([0, 255, 0])
        img[:, 60:90, :] = np.array([0, 0, 255])
        img = torch.from_numpy(img.transpose((2, 0, 1)))

        fig, axis = plt.subplots(1, 1)
        fe.util.show_image(img, fig=fig, axis=axis)
        obj1 = fig_to_rgb_array(fig)
        obj2 = self.color_img_ans
        self.assertTrue(check_img_similar(obj1, obj2))
コード例 #11
0
    def test_show_image_bounding_box_np(self):
        bg_img = np.zeros((150, 150))
        boxes = np.array([[0, 0, 10, 20, "apple"], [10, 20, 30, 50, "dog"],
                          [40, 70, 200, 200, "cat"],
                          [0, 0, 0, 0, "shouldn't shown"],
                          [0, 0, -50, -30, "shouldn't shown2"]])

        fig, axis = plt.subplots(1, 1, figsize=(6.4, 4.8))
        fe.util.show_image(bg_img, fig=fig, axis=axis)
        fe.util.show_image(boxes, fig=fig, axis=axis)
        obj1 = fig_to_rgb_array(fig)
        obj2 = self.bb_img_ans
        self.assertTrue(check_img_similar(obj1, obj2))
コード例 #12
0
    def test_show_image_color_np(self):
        img = np.zeros((90, 90, 3), dtype=np.uint8)
        img[:, 0:30, :] = np.array([255, 0, 0])
        img[:, 30:60, :] = np.array([0, 255, 0])
        img[:, 60:90, :] = np.array([0, 0, 255])

        fig, axis = plt.subplots(1, 1)
        fe.util.show_image(img, fig=fig, axis=axis)

        # Now we can save it to a numpy array.
        obj1 = fig_to_rgb_array(fig)
        obj2 = self.color_img_ans
        self.assertTrue(check_img_similar(obj1, obj2))
コード例 #13
0
 def test_paint_numpy(self):
     output_test = self.img_data.paint_numpy()
     output_test = np.squeeze(output_test, axis=0)
     output = img_to_rgb_array(self.output_img)
     self.assertTrue(check_img_similar(output, output_test))
コード例 #14
0
 def test_paint_figure(self):
     fig = self.img_data.paint_figure()
     output = img_to_rgb_array(self.output_img)
     output_test = fig_to_rgb_array(fig)
     self.assertTrue(check_img_similar(output, output_test))