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))
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)
def setUpClass(cls): cls.color_img_ans = img_to_rgb_array( os.path.abspath( os.path.join(__file__, "..", "resources", "test_show_image_color.png"))) cls.hw_ratio_img_ans = img_to_rgb_array( os.path.abspath( os.path.join(__file__, "..", "resources", "test_show_image_height_width.png"))) cls.bb_img_ans = img_to_rgb_array( os.path.abspath( os.path.join(__file__, "..", "resources", "test_show_image_bounding_box.png"))) cls.mixed_img_ans = img_to_rgb_array( os.path.abspath( os.path.join(__file__, "..", "resources", "test_show_image_mixed.png"))) cls.text_img_ans = img_to_rgb_array( os.path.abspath( os.path.join(__file__, "..", "resources", "test_show_image_text.png"))) cls.title_img_ans = img_to_rgb_array( os.path.abspath( os.path.join(__file__, "..", "resources", "test_show_image_title.png"))) cls.float_img_ans = img_to_rgb_array( os.path.abspath( os.path.join(__file__, "..", "resources", "test_show_image_check_float.png")))
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))
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))