def on_epoch_end(self, data: Data): device = next(self.model.parameters()).device for _ in range(self.trials): img_path = self.dataset.one_shot_trial(self.N) input_img = ( np.array([ np.expand_dims(cv2.imread(i, cv2.IMREAD_GRAYSCALE), -1).reshape((1, 105, 105)) / 255. for i in img_path[0] ], dtype=np.float32), np.array([ np.expand_dims(cv2.imread(i, cv2.IMREAD_GRAYSCALE), -1).reshape((1, 105, 105)) / 255. for i in img_path[1] ], dtype=np.float32)) input_img = (to_tensor(input_img[0], "torch").to(device), to_tensor(input_img[1], "torch").to(device)) model = self.model.module if torch.cuda.device_count( ) > 1 else self.model prediction_score = feed_forward( model, input_img, training=False).cpu().detach().numpy() if np.argmax( prediction_score) == 0 and prediction_score.std() > 0.01: self.correct += 1 self.total += 1 data.write_with_log(self.outputs[0], self.correct / self.total)
def setUpClass(self): self.pytorch_array = to_tensor( np.arange(0.0, 8.0, 1.0, dtype=np.float32).reshape( (1, 1, 2, 2, 2)), 'torch') self.tensorflow_array = to_tensor( np.arange(0.0, 8.0, 1.0, dtype=np.float32).reshape( (1, 2, 2, 2, 1)), 'tf')
def test_normalize_torch_float(self): op = Normalize(inputs="image", outputs="image", mean=0.482, std=0.289, max_pixel_value=27.0) data = op.forward(data=to_tensor(self.numpy_array, "torch"), state={}) np.testing.assert_array_almost_equal(data.numpy(), self.expected_result, 2)
def test_normalize_torch_multi(self): op = Normalize(inputs="image", outputs="image", mean=(0.44, 0.48, 0.52), std=(0.287, 0.287, 0.287), max_pixel_value=27) data = op.forward(data=to_tensor(self.numpy_array, "torch"), state={}) np.testing.assert_array_almost_equal(data.numpy(), self.expected_result_multi, 2)
def test_normalize_torch_value_int(self): np.testing.assert_array_almost_equal( normalize(to_tensor(self.numpy_array_int, 'torch'), 0.5, 0.31382295, 11.0).numpy(), self.expected_result)