|
| 1 | +# |
| 2 | +# Copyright (c) 2022 salesforce.com, inc. |
| 3 | +# All rights reserved. |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause |
| 5 | +# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause |
| 6 | +# |
| 7 | +import unittest |
| 8 | +import torch |
| 9 | +import tensorflow as tf |
| 10 | +from omnixai.explainers.vision.specific.feature_visualization.utils import \ |
| 11 | + fft_freq, fft_scale, fft_inputs |
| 12 | +from omnixai.explainers.vision.specific.feature_visualization.tf.preprocess import \ |
| 13 | + fft_images as fft_images_tf |
| 14 | +from omnixai.explainers.vision.specific.feature_visualization.pytorch.preprocess import \ |
| 15 | + fft_images as fft_images_torch |
| 16 | + |
| 17 | + |
| 18 | +class TestFFT(unittest.TestCase): |
| 19 | + |
| 20 | + def test_1(self): |
| 21 | + batch_size = 5 |
| 22 | + channel = 3 |
| 23 | + width = 10 |
| 24 | + height = 7 |
| 25 | + mode = "torch" |
| 26 | + |
| 27 | + freq = fft_freq(width, height, mode) |
| 28 | + scale = fft_scale(width, height, mode) |
| 29 | + inputs = fft_inputs(batch_size, channel, width, height, mode) |
| 30 | + self.assertEqual(freq.shape, (10, 7)) |
| 31 | + self.assertEqual(scale.shape, (10, 7)) |
| 32 | + self.assertEqual(inputs.shape, (2, 5, 3, 10, 7)) |
| 33 | + |
| 34 | + def test_2(self): |
| 35 | + batch_size = 5 |
| 36 | + channel = 3 |
| 37 | + width = 10 |
| 38 | + height = 7 |
| 39 | + mode = "tf" |
| 40 | + |
| 41 | + freq = fft_freq(width, height, mode) |
| 42 | + scale = fft_scale(width, height, mode) |
| 43 | + inputs = fft_inputs(batch_size, channel, width, height, mode) |
| 44 | + self.assertEqual(freq.shape, (10, 5)) |
| 45 | + self.assertEqual(scale.shape, (10, 5)) |
| 46 | + self.assertEqual(inputs.shape, (2, 5, 3, 10, 5)) |
| 47 | + |
| 48 | + def test_3(self): |
| 49 | + batch_size = 5 |
| 50 | + channel = 3 |
| 51 | + width = 10 |
| 52 | + height = 7 |
| 53 | + mode = "tf" |
| 54 | + |
| 55 | + scale = fft_scale(width, height, mode) |
| 56 | + scale = tf.convert_to_tensor(scale, dtype=tf.complex64) |
| 57 | + inputs = fft_inputs(batch_size, channel, width, height, mode) |
| 58 | + inputs = tf.convert_to_tensor(inputs) |
| 59 | + |
| 60 | + images = fft_images_tf(width, height, inputs, scale) |
| 61 | + self.assertEqual(images.shape, (5, 10, 7, 3)) |
| 62 | + |
| 63 | + def test_4(self): |
| 64 | + batch_size = 5 |
| 65 | + channel = 3 |
| 66 | + width = 10 |
| 67 | + height = 7 |
| 68 | + mode = "torch" |
| 69 | + |
| 70 | + scale = fft_scale(width, height, mode) |
| 71 | + scale = torch.tensor(scale, dtype=torch.complex64) |
| 72 | + inputs = fft_inputs(batch_size, channel, width, height, mode) |
| 73 | + inputs = torch.tensor(inputs, dtype=torch.float32) |
| 74 | + |
| 75 | + images = fft_images_torch(width, height, inputs, scale) |
| 76 | + self.assertEqual(images.shape, (5, 3, 10, 7)) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + unittest.main() |
0 commit comments