format test_monitor.py and test_tensorboard.py (#142003)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/142003
Approved by: https://github.com/StrongerXi, https://github.com/atalman
ghstack dependencies: #141409
This commit is contained in:
William Wen
2024-12-04 00:02:45 -08:00
committed by PyTorch MergeBot
parent 22ae34af88
commit c93dd531d3
2 changed files with 332 additions and 217 deletions

View File

@ -29,8 +29,9 @@ skipIfNoTorchVision = unittest.skipIf(not HAS_TORCHVISION, "no torchvision")
TEST_MATPLOTLIB = True
try:
import matplotlib
if os.environ.get('DISPLAY', '') == '':
matplotlib.use('Agg')
if os.environ.get("DISPLAY", "") == "":
matplotlib.use("Agg")
import matplotlib.pyplot as plt
except ImportError:
TEST_MATPLOTLIB = False
@ -43,10 +44,10 @@ from torch.testing._internal.common_utils import (
IS_WINDOWS,
parametrize,
run_tests,
skipIfTorchDynamo,
TEST_WITH_CROSSREF,
TestCase,
xfailIfS390X,
skipIfTorchDynamo,
)
@ -55,8 +56,10 @@ def tensor_N(shape, dtype=float):
x = (np.arange(numel, dtype=dtype)).reshape(shape)
return x
class BaseTestCase(TestCase):
""" Base class used for all TensorBoard tests """
"""Base class used for all TensorBoard tests"""
def setUp(self):
super().setUp()
if not TEST_TENSORBOARD:
@ -92,6 +95,7 @@ if TEST_TENSORBOARD:
from torch.utils.tensorboard._utils import _prepare_video, convert_to_HWC
from torch.utils.tensorboard.summary import int_to_half, tensor_proto
class TestTensorBoardPyTorchNumpy(BaseTestCase):
def test_pytorch_np(self):
tensors = [torch.rand(3, 10, 10), torch.rand(1), torch.rand(1, 2, 3, 4, 5)]
@ -108,7 +112,9 @@ class TestTensorBoardPyTorchNumpy(BaseTestCase):
# CUDA variable
if torch.cuda.is_available():
self.assertIsInstance(make_np(torch.autograd.Variable(tensor).cuda()), np.ndarray)
self.assertIsInstance(
make_np(torch.autograd.Variable(tensor).cuda()), np.ndarray
)
# python primitive type
self.assertIsInstance(make_np(0), np.ndarray)
@ -120,13 +126,13 @@ class TestTensorBoardPyTorchNumpy(BaseTestCase):
def test_pytorch_write(self):
with self.createSummaryWriter() as w:
w.add_scalar('scalar', torch.autograd.Variable(torch.rand(1)), 0)
w.add_scalar("scalar", torch.autograd.Variable(torch.rand(1)), 0)
def test_pytorch_histogram(self):
with self.createSummaryWriter() as w:
w.add_histogram('float histogram', torch.rand((50,)))
w.add_histogram('int histogram', torch.randint(0, 100, (50,)))
w.add_histogram('bfloat16 histogram', torch.rand(50, dtype=torch.bfloat16))
w.add_histogram("float histogram", torch.rand((50,)))
w.add_histogram("int histogram", torch.randint(0, 100, (50,)))
w.add_histogram("bfloat16 histogram", torch.rand(50, dtype=torch.bfloat16))
def test_pytorch_histogram_raw(self):
with self.createSummaryWriter() as w:
@ -135,52 +141,59 @@ class TestTensorBoardPyTorchNumpy(BaseTestCase):
bins = [0.0, 0.25, 0.5, 0.75, 1.0]
counts, limits = np.histogram(floats, bins)
sum_sq = floats.dot(floats).item()
w.add_histogram_raw('float histogram raw',
min=floats.min().item(),
max=floats.max().item(),
num=num,
sum=floats.sum().item(),
sum_squares=sum_sq,
bucket_limits=limits[1:].tolist(),
bucket_counts=counts.tolist())
w.add_histogram_raw(
"float histogram raw",
min=floats.min().item(),
max=floats.max().item(),
num=num,
sum=floats.sum().item(),
sum_squares=sum_sq,
bucket_limits=limits[1:].tolist(),
bucket_counts=counts.tolist(),
)
ints = make_np(torch.randint(0, 100, (num,)))
bins = [0, 25, 50, 75, 100]
counts, limits = np.histogram(ints, bins)
sum_sq = ints.dot(ints).item()
w.add_histogram_raw('int histogram raw',
min=ints.min().item(),
max=ints.max().item(),
num=num,
sum=ints.sum().item(),
sum_squares=sum_sq,
bucket_limits=limits[1:].tolist(),
bucket_counts=counts.tolist())
w.add_histogram_raw(
"int histogram raw",
min=ints.min().item(),
max=ints.max().item(),
num=num,
sum=ints.sum().item(),
sum_squares=sum_sq,
bucket_limits=limits[1:].tolist(),
bucket_counts=counts.tolist(),
)
ints = torch.tensor(range(0, 100)).float()
nbins = 100
counts = torch.histc(ints, bins=nbins, min=0, max=99)
limits = torch.tensor(range(nbins))
sum_sq = ints.dot(ints).item()
w.add_histogram_raw('int histogram raw',
min=ints.min().item(),
max=ints.max().item(),
num=num,
sum=ints.sum().item(),
sum_squares=sum_sq,
bucket_limits=limits.tolist(),
bucket_counts=counts.tolist())
w.add_histogram_raw(
"int histogram raw",
min=ints.min().item(),
max=ints.max().item(),
num=num,
sum=ints.sum().item(),
sum_squares=sum_sq,
bucket_limits=limits.tolist(),
bucket_counts=counts.tolist(),
)
class TestTensorBoardUtils(BaseTestCase):
def test_to_HWC(self):
test_image = np.random.randint(0, 256, size=(3, 32, 32), dtype=np.uint8)
converted = convert_to_HWC(test_image, 'chw')
converted = convert_to_HWC(test_image, "chw")
self.assertEqual(converted.shape, (32, 32, 3))
test_image = np.random.randint(0, 256, size=(16, 3, 32, 32), dtype=np.uint8)
converted = convert_to_HWC(test_image, 'nchw')
converted = convert_to_HWC(test_image, "nchw")
self.assertEqual(converted.shape, (64, 256, 3))
test_image = np.random.randint(0, 256, size=(32, 32), dtype=np.uint8)
converted = convert_to_HWC(test_image, 'hw')
converted = convert_to_HWC(test_image, "hw")
self.assertEqual(converted.shape, (32, 32, 3))
def test_convert_to_HWC_dtype_remains_same(self):
@ -188,10 +201,13 @@ class TestTensorBoardUtils(BaseTestCase):
# thus the scale_factor calculated for the image is 1
test_image = torch.tensor([[[[1, 2, 3], [4, 5, 6]]]], dtype=torch.uint8)
tensor = make_np(test_image)
tensor = convert_to_HWC(tensor, 'NCHW')
tensor = convert_to_HWC(tensor, "NCHW")
scale_factor = summary._calc_scale_factor(tensor)
self.assertEqual(scale_factor, 1, msg='Values are already in [0, 255], scale factor should be 1')
self.assertEqual(
scale_factor,
1,
msg="Values are already in [0, 255], scale factor should be 1",
)
def test_prepare_video(self):
# At each timeframe, the sum over all other
@ -200,7 +216,7 @@ class TestTensorBoardUtils(BaseTestCase):
(16, 30, 3, 28, 28),
(36, 30, 3, 28, 28),
(19, 29, 3, 23, 19),
(3, 3, 3, 3, 3)
(3, 3, 3, 3, 3),
]
for s in shapes:
V_input = np.random.random(s)
@ -222,6 +238,7 @@ class TestTensorBoardUtils(BaseTestCase):
y = np.reshape(V_after[f], newshape=(-1))
np.testing.assert_array_almost_equal(np.sum(x), np.sum(y))
freqs = [262, 294, 330, 349, 392, 440, 440, 440, 440, 440, 440]
true_positive_counts = [75, 64, 21, 5, 0]
@ -231,6 +248,7 @@ false_negative_counts = [0, 11, 54, 70, 75]
precision = [0.3333333, 0.3786982, 0.5384616, 1.0, 0.0]
recall = [1.0, 0.8533334, 0.28, 0.0666667, 0.0]
class TestTensorBoardWriter(BaseTestCase):
def test_writer(self):
with self.createSummaryWriter() as writer:
@ -238,49 +256,66 @@ class TestTensorBoardWriter(BaseTestCase):
n_iter = 0
writer.add_hparams(
{'lr': 0.1, 'bsize': 1},
{'hparam/accuracy': 10, 'hparam/loss': 10}
{"lr": 0.1, "bsize": 1}, {"hparam/accuracy": 10, "hparam/loss": 10}
)
writer.add_scalar("data/scalar_systemtime", 0.1, n_iter)
writer.add_scalar("data/scalar_customtime", 0.2, n_iter, walltime=n_iter)
writer.add_scalar("data/new_style", 0.2, n_iter, new_style=True)
writer.add_scalars(
"data/scalar_group",
{
"xsinx": n_iter * np.sin(n_iter),
"xcosx": n_iter * np.cos(n_iter),
"arctanx": np.arctan(n_iter),
},
n_iter,
)
writer.add_scalar('data/scalar_systemtime', 0.1, n_iter)
writer.add_scalar('data/scalar_customtime', 0.2, n_iter, walltime=n_iter)
writer.add_scalar('data/new_style', 0.2, n_iter, new_style=True)
writer.add_scalars('data/scalar_group', {
"xsinx": n_iter * np.sin(n_iter),
"xcosx": n_iter * np.cos(n_iter),
"arctanx": np.arctan(n_iter)
}, n_iter)
x = np.zeros((32, 3, 64, 64)) # output from network
writer.add_images('Image', x, n_iter) # Tensor
writer.add_image_with_boxes('imagebox',
np.zeros((3, 64, 64)),
np.array([[10, 10, 40, 40], [40, 40, 60, 60]]),
n_iter)
writer.add_images("Image", x, n_iter) # Tensor
writer.add_image_with_boxes(
"imagebox",
np.zeros((3, 64, 64)),
np.array([[10, 10, 40, 40], [40, 40, 60, 60]]),
n_iter,
)
x = np.zeros(sample_rate * 2)
writer.add_audio('myAudio', x, n_iter)
writer.add_video('myVideo', np.random.rand(16, 48, 1, 28, 28).astype(np.float32), n_iter)
writer.add_text('Text', 'text logged at step:' + str(n_iter), n_iter)
writer.add_text('markdown Text', '''a|b\n-|-\nc|d''', n_iter)
writer.add_histogram('hist', np.random.rand(100, 100), n_iter)
writer.add_pr_curve('xoxo', np.random.randint(2, size=100), np.random.rand(
100), n_iter) # needs tensorboard 0.4RC or later
writer.add_pr_curve_raw('prcurve with raw data', true_positive_counts,
false_positive_counts,
true_negative_counts,
false_negative_counts,
precision,
recall, n_iter)
writer.add_audio("myAudio", x, n_iter)
writer.add_video(
"myVideo", np.random.rand(16, 48, 1, 28, 28).astype(np.float32), n_iter
)
writer.add_text("Text", "text logged at step:" + str(n_iter), n_iter)
writer.add_text("markdown Text", """a|b\n-|-\nc|d""", n_iter)
writer.add_histogram("hist", np.random.rand(100, 100), n_iter)
writer.add_pr_curve(
"xoxo", np.random.randint(2, size=100), np.random.rand(100), n_iter
) # needs tensorboard 0.4RC or later
writer.add_pr_curve_raw(
"prcurve with raw data",
true_positive_counts,
false_positive_counts,
true_negative_counts,
false_negative_counts,
precision,
recall,
n_iter,
)
v = np.array([[[1, 1, 1], [-1, -1, 1], [1, -1, -1], [-1, 1, -1]]], dtype=float)
c = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 0, 255]]], dtype=int)
v = np.array(
[[[1, 1, 1], [-1, -1, 1], [1, -1, -1], [-1, 1, -1]]], dtype=float
)
c = np.array(
[[[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 0, 255]]], dtype=int
)
f = np.array([[[0, 2, 3], [0, 3, 1], [0, 1, 2], [1, 3, 2]]], dtype=int)
writer.add_mesh('my_mesh', vertices=v, colors=c, faces=f)
writer.add_mesh("my_mesh", vertices=v, colors=c, faces=f)
class TestTensorBoardSummaryWriter(BaseTestCase):
def test_summary_writer_ctx(self):
# after using a SummaryWriter as a ctx it should be closed
with self.createSummaryWriter() as writer:
writer.add_scalar('test', 1)
writer.add_scalar("test", 1)
self.assertIs(writer.file_writer, None)
def test_summary_writer_close(self):
@ -299,182 +334,245 @@ class TestTensorBoardSummaryWriter(BaseTestCase):
with tempfile.TemporaryDirectory(prefix="test_tensorboard_pathlib") as d:
p = Path(d)
with SummaryWriter(p) as writer:
writer.add_scalar('test', 1)
writer.add_scalar("test", 1)
class TestTensorBoardEmbedding(BaseTestCase):
def test_embedding(self):
w = self.createSummaryWriter()
all_features = torch.tensor([[1., 2., 3.], [5., 4., 1.], [3., 7., 7.]])
all_labels = torch.tensor([33., 44., 55.])
all_features = torch.tensor([[1.0, 2.0, 3.0], [5.0, 4.0, 1.0], [3.0, 7.0, 7.0]])
all_labels = torch.tensor([33.0, 44.0, 55.0])
all_images = torch.zeros(3, 3, 5, 5)
w.add_embedding(all_features,
metadata=all_labels,
label_img=all_images,
global_step=2)
w.add_embedding(
all_features, metadata=all_labels, label_img=all_images, global_step=2
)
dataset_label = ['test'] * 2 + ['train'] * 2
dataset_label = ["test"] * 2 + ["train"] * 2
all_labels = list(zip(all_labels, dataset_label))
w.add_embedding(all_features,
metadata=all_labels,
label_img=all_images,
metadata_header=['digit', 'dataset'],
global_step=2)
w.add_embedding(
all_features,
metadata=all_labels,
label_img=all_images,
metadata_header=["digit", "dataset"],
global_step=2,
)
# assert...
def test_embedding_64(self):
w = self.createSummaryWriter()
all_features = torch.tensor([[1., 2., 3.], [5., 4., 1.], [3., 7., 7.]])
all_labels = torch.tensor([33., 44., 55.])
all_features = torch.tensor([[1.0, 2.0, 3.0], [5.0, 4.0, 1.0], [3.0, 7.0, 7.0]])
all_labels = torch.tensor([33.0, 44.0, 55.0])
all_images = torch.zeros((3, 3, 5, 5), dtype=torch.float64)
w.add_embedding(all_features,
metadata=all_labels,
label_img=all_images,
global_step=2)
w.add_embedding(
all_features, metadata=all_labels, label_img=all_images, global_step=2
)
dataset_label = ['test'] * 2 + ['train'] * 2
dataset_label = ["test"] * 2 + ["train"] * 2
all_labels = list(zip(all_labels, dataset_label))
w.add_embedding(all_features,
metadata=all_labels,
label_img=all_images,
metadata_header=['digit', 'dataset'],
global_step=2)
w.add_embedding(
all_features,
metadata=all_labels,
label_img=all_images,
metadata_header=["digit", "dataset"],
global_step=2,
)
class TestTensorBoardSummary(BaseTestCase):
def test_uint8_image(self):
'''
"""
Tests that uint8 image (pixel values in [0, 255]) is not changed
'''
"""
test_image = np.random.randint(0, 256, size=(3, 32, 32), dtype=np.uint8)
scale_factor = summary._calc_scale_factor(test_image)
self.assertEqual(scale_factor, 1, msg='Values are already in [0, 255], scale factor should be 1')
self.assertEqual(
scale_factor,
1,
msg="Values are already in [0, 255], scale factor should be 1",
)
def test_float32_image(self):
'''
"""
Tests that float32 image (pixel values in [0, 1]) are scaled correctly
to [0, 255]
'''
"""
test_image = np.random.rand(3, 32, 32).astype(np.float32)
scale_factor = summary._calc_scale_factor(test_image)
self.assertEqual(scale_factor, 255, msg='Values are in [0, 1], scale factor should be 255')
self.assertEqual(
scale_factor, 255, msg="Values are in [0, 1], scale factor should be 255"
)
def test_list_input(self):
with self.assertRaises(Exception) as e_info:
summary.histogram('dummy', [1, 3, 4, 5, 6], 'tensorflow')
summary.histogram("dummy", [1, 3, 4, 5, 6], "tensorflow")
def test_empty_input(self):
with self.assertRaises(Exception) as e_info:
summary.histogram('dummy', np.ndarray(0), 'tensorflow')
summary.histogram("dummy", np.ndarray(0), "tensorflow")
def test_image_with_boxes(self):
self.assertTrue(compare_image_proto(summary.image_boxes('dummy',
tensor_N(shape=(3, 32, 32)),
np.array([[10, 10, 40, 40]])),
self))
self.assertTrue(
compare_image_proto(
summary.image_boxes(
"dummy", tensor_N(shape=(3, 32, 32)), np.array([[10, 10, 40, 40]])
),
self,
)
)
def test_image_with_one_channel(self):
self.assertTrue(compare_image_proto(
summary.image('dummy',
tensor_N(shape=(1, 8, 8)),
dataformats='CHW'),
self)) # noqa: E131
self.assertTrue(
compare_image_proto(
summary.image("dummy", tensor_N(shape=(1, 8, 8)), dataformats="CHW"),
self,
)
) # noqa: E131
def test_image_with_one_channel_batched(self):
self.assertTrue(compare_image_proto(
summary.image('dummy',
tensor_N(shape=(2, 1, 8, 8)),
dataformats='NCHW'),
self)) # noqa: E131
self.assertTrue(
compare_image_proto(
summary.image(
"dummy", tensor_N(shape=(2, 1, 8, 8)), dataformats="NCHW"
),
self,
)
) # noqa: E131
def test_image_with_3_channel_batched(self):
self.assertTrue(compare_image_proto(
summary.image('dummy',
tensor_N(shape=(2, 3, 8, 8)),
dataformats='NCHW'),
self)) # noqa: E131
self.assertTrue(
compare_image_proto(
summary.image(
"dummy", tensor_N(shape=(2, 3, 8, 8)), dataformats="NCHW"
),
self,
)
) # noqa: E131
def test_image_without_channel(self):
self.assertTrue(compare_image_proto(
summary.image('dummy',
tensor_N(shape=(8, 8)),
dataformats='HW'),
self)) # noqa: E131
self.assertTrue(
compare_image_proto(
summary.image("dummy", tensor_N(shape=(8, 8)), dataformats="HW"), self
)
) # noqa: E131
def test_video(self):
try:
import moviepy # noqa: F401
except ImportError:
return
self.assertTrue(compare_proto(summary.video('dummy', tensor_N(shape=(4, 3, 1, 8, 8))), self))
summary.video('dummy', np.random.rand(16, 48, 1, 28, 28))
summary.video('dummy', np.random.rand(20, 7, 1, 8, 8))
self.assertTrue(
compare_proto(summary.video("dummy", tensor_N(shape=(4, 3, 1, 8, 8))), self)
)
summary.video("dummy", np.random.rand(16, 48, 1, 28, 28))
summary.video("dummy", np.random.rand(20, 7, 1, 8, 8))
@unittest.skipIf(IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 ")
@unittest.skipIf(
IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 "
)
@xfailIfS390X
def test_audio(self):
self.assertTrue(compare_proto(summary.audio('dummy', tensor_N(shape=(42,))), self))
self.assertTrue(
compare_proto(summary.audio("dummy", tensor_N(shape=(42,))), self)
)
@unittest.skipIf(IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 ")
@unittest.skipIf(
IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 "
)
def test_text(self):
self.assertTrue(compare_proto(summary.text('dummy', 'text 123'), self))
self.assertTrue(compare_proto(summary.text("dummy", "text 123"), self))
@unittest.skipIf(IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 ")
@unittest.skipIf(
IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 "
)
def test_histogram_auto(self):
self.assertTrue(compare_proto(summary.histogram('dummy', tensor_N(shape=(1024,)), bins='auto', max_bins=5), self))
self.assertTrue(
compare_proto(
summary.histogram(
"dummy", tensor_N(shape=(1024,)), bins="auto", max_bins=5
),
self,
)
)
@unittest.skipIf(IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 ")
@unittest.skipIf(
IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 "
)
def test_histogram_fd(self):
self.assertTrue(compare_proto(summary.histogram('dummy', tensor_N(shape=(1024,)), bins='fd', max_bins=5), self))
self.assertTrue(
compare_proto(
summary.histogram(
"dummy", tensor_N(shape=(1024,)), bins="fd", max_bins=5
),
self,
)
)
@unittest.skipIf(IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 ")
@unittest.skipIf(
IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 "
)
def test_histogram_doane(self):
self.assertTrue(compare_proto(summary.histogram('dummy', tensor_N(shape=(1024,)), bins='doane', max_bins=5), self))
self.assertTrue(
compare_proto(
summary.histogram(
"dummy", tensor_N(shape=(1024,)), bins="doane", max_bins=5
),
self,
)
)
def test_custom_scalars(self):
layout = {
'Taiwan': {
'twse': ['Multiline', ['twse/0050', 'twse/2330']]
"Taiwan": {"twse": ["Multiline", ["twse/0050", "twse/2330"]]},
"USA": {
"dow": ["Margin", ["dow/aaa", "dow/bbb", "dow/ccc"]],
"nasdaq": ["Margin", ["nasdaq/aaa", "nasdaq/bbb", "nasdaq/ccc"]],
},
'USA': {
'dow': ['Margin', ['dow/aaa', 'dow/bbb', 'dow/ccc']],
'nasdaq': ['Margin', ['nasdaq/aaa', 'nasdaq/bbb', 'nasdaq/ccc']]
}
}
summary.custom_scalars(layout) # only smoke test. Because protobuf in python2/3 serialize dictionary differently.
summary.custom_scalars(
layout
) # only smoke test. Because protobuf in python2/3 serialize dictionary differently.
@unittest.skipIf(IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 ")
@unittest.skipIf(
IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 "
)
def test_mesh(self):
v = np.array([[[1, 1, 1], [-1, -1, 1], [1, -1, -1], [-1, 1, -1]]], dtype=float)
c = np.array([[[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 0, 255]]], dtype=int)
c = np.array(
[[[255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 0, 255]]], dtype=int
)
f = np.array([[[0, 2, 3], [0, 3, 1], [0, 1, 2], [1, 3, 2]]], dtype=int)
mesh = summary.mesh('my_mesh', vertices=v, colors=c, faces=f, config_dict=None)
mesh = summary.mesh("my_mesh", vertices=v, colors=c, faces=f, config_dict=None)
self.assertTrue(compare_proto(mesh, self))
@unittest.skipIf(IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 ")
@unittest.skipIf(
IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 "
)
def test_scalar_new_style(self):
scalar = summary.scalar('test_scalar', 1.0, new_style=True)
scalar = summary.scalar("test_scalar", 1.0, new_style=True)
self.assertTrue(compare_proto(scalar, self))
with self.assertRaises(AssertionError):
summary.scalar('test_scalar2', torch.Tensor([1, 2, 3]), new_style=True)
summary.scalar("test_scalar2", torch.Tensor([1, 2, 3]), new_style=True)
def remove_whitespace(string):
return string.replace(' ', '').replace('\t', '').replace('\n', '')
return string.replace(" ", "").replace("\t", "").replace("\n", "")
def get_expected_file(function_ptr):
module_id = function_ptr.__class__.__module__
test_file = sys.modules[module_id].__file__
# Look for the .py file (since __file__ could be pyc).
test_file = ".".join(test_file.split('.')[:-1]) + '.py'
test_file = ".".join(test_file.split(".")[:-1]) + ".py"
# Use realpath to follow symlinks appropriately.
test_dir = os.path.dirname(os.path.realpath(test_file))
functionName = function_ptr.id().split('.')[-1]
return os.path.join(test_dir,
"expect",
'TestTensorBoard.' + functionName + ".expect")
functionName = function_ptr.id().split(".")[-1]
return os.path.join(
test_dir, "expect", "TestTensorBoard." + functionName + ".expect"
)
def read_expected_content(function_ptr):
expected_file = get_expected_file(function_ptr)
@ -482,10 +580,11 @@ def read_expected_content(function_ptr):
with open(expected_file) as f:
return f.read()
def compare_image_proto(actual_proto, function_ptr):
if expecttest.ACCEPT:
expected_file = get_expected_file(function_ptr)
with open(expected_file, 'w') as f:
with open(expected_file, "w") as f:
f.write(text_format.MessageToString(actual_proto))
return True
expected_str = read_expected_content(function_ptr)
@ -497,13 +596,14 @@ def compare_image_proto(actual_proto, function_ptr):
expected_img = Image.open(io.BytesIO(expected.image.encoded_image_string))
return (
actual.tag == expected.tag and
actual.image.height == expected.image.height and
actual.image.width == expected.image.width and
actual.image.colorspace == expected.image.colorspace and
actual_img == expected_img
actual.tag == expected.tag
and actual.image.height == expected.image.height
and actual.image.width == expected.image.width
and actual.image.colorspace == expected.image.colorspace
and actual_img == expected_img
)
def compare_proto(str_to_compare, function_ptr):
if expecttest.ACCEPT:
write_proto(str_to_compare, function_ptr)
@ -512,11 +612,13 @@ def compare_proto(str_to_compare, function_ptr):
str_to_compare = str(str_to_compare)
return remove_whitespace(str_to_compare) == remove_whitespace(expected)
def write_proto(str_to_compare, function_ptr):
expected_file = get_expected_file(function_ptr)
with open(expected_file, 'w') as f:
with open(expected_file, "w") as f:
f.write(str(str_to_compare))
class TestTensorBoardPytorchGraph(BaseTestCase):
def test_pytorch_graph(self):
dummy_input = (torch.zeros(1, 3),)
@ -547,7 +649,8 @@ class TestTensorBoardPytorchGraph(BaseTestCase):
self.assertEqual(expected_node.input, actual_node.input)
self.assertEqual(expected_node.device, actual_node.device)
self.assertEqual(
sorted(expected_node.attr.keys()), sorted(actual_node.attr.keys()))
sorted(expected_node.attr.keys()), sorted(actual_node.attr.keys())
)
def test_nested_nn_squential(self):
@ -595,7 +698,8 @@ class TestTensorBoardPytorchGraph(BaseTestCase):
self.assertEqual(expected_node.input, actual_node.input)
self.assertEqual(expected_node.device, actual_node.device)
self.assertEqual(
sorted(expected_node.attr.keys()), sorted(actual_node.attr.keys()))
sorted(expected_node.attr.keys()), sorted(actual_node.attr.keys())
)
def test_pytorch_graph_dict_input(self):
class Model(torch.nn.Module):
@ -614,7 +718,6 @@ class TestTensorBoardPytorchGraph(BaseTestCase):
def forward(self, x):
return {"out": self.l(x)}
dummy_input = torch.zeros(1, 3)
with self.createSummaryWriter() as w:
@ -631,7 +734,6 @@ class TestTensorBoardPytorchGraph(BaseTestCase):
with self.createSummaryWriter() as w:
w.add_graph(ModelDict(), dummy_input, use_strict_trace=False)
def test_mlp_graph(self):
dummy_input = (torch.zeros(2, 1, 28, 28),)
@ -650,8 +752,7 @@ class TestTensorBoardPytorchGraph(BaseTestCase):
self.fc3 = torch.nn.Linear(1200, 10)
def forward(self, x, update_batch_stats=True):
h = torch.nn.functional.relu(
self.fc1(x.view(-1, self.input_len)))
h = torch.nn.functional.relu(self.fc1(x.view(-1, self.input_len)))
h = self.fc2(h)
h = torch.nn.functional.relu(h)
h = self.fc3(h)
@ -670,42 +771,45 @@ class TestTensorBoardPytorchGraph(BaseTestCase):
@skipIfNoTorchVision
def test_torchvision_smoke(self):
model_input_shapes = {
'alexnet': (2, 3, 224, 224),
'resnet34': (2, 3, 224, 224),
'resnet152': (2, 3, 224, 224),
'densenet121': (2, 3, 224, 224),
'vgg16': (2, 3, 224, 224),
'vgg19': (2, 3, 224, 224),
'vgg16_bn': (2, 3, 224, 224),
'vgg19_bn': (2, 3, 224, 224),
'mobilenet_v2': (2, 3, 224, 224),
"alexnet": (2, 3, 224, 224),
"resnet34": (2, 3, 224, 224),
"resnet152": (2, 3, 224, 224),
"densenet121": (2, 3, 224, 224),
"vgg16": (2, 3, 224, 224),
"vgg19": (2, 3, 224, 224),
"vgg16_bn": (2, 3, 224, 224),
"vgg19_bn": (2, 3, 224, 224),
"mobilenet_v2": (2, 3, 224, 224),
}
for model_name, input_shape in model_input_shapes.items():
with self.createSummaryWriter() as w:
model = getattr(torchvision.models, model_name)()
w.add_graph(model, torch.zeros(input_shape))
class TestTensorBoardFigure(BaseTestCase):
@skipIfNoMatplotlib
def test_figure(self):
writer = self.createSummaryWriter()
figure, axes = plt.figure(), plt.gca()
circle1 = plt.Circle((0.2, 0.5), 0.2, color='r')
circle2 = plt.Circle((0.8, 0.5), 0.2, color='g')
circle1 = plt.Circle((0.2, 0.5), 0.2, color="r")
circle2 = plt.Circle((0.8, 0.5), 0.2, color="g")
axes.add_patch(circle1)
axes.add_patch(circle2)
plt.axis('scaled')
plt.axis("scaled")
plt.tight_layout()
writer.add_figure("add_figure/figure", figure, 0, close=False)
self.assertTrue(plt.fignum_exists(figure.number))
writer.add_figure("add_figure/figure", figure, 1)
if matplotlib.__version__ != '3.3.0':
if matplotlib.__version__ != "3.3.0":
self.assertFalse(plt.fignum_exists(figure.number))
else:
print("Skipping fignum_exists, see https://github.com/matplotlib/matplotlib/issues/18163")
print(
"Skipping fignum_exists, see https://github.com/matplotlib/matplotlib/issues/18163"
)
writer.close()
@ -727,16 +831,24 @@ class TestTensorBoardFigure(BaseTestCase):
self.assertTrue(all(plt.fignum_exists(figure.number) is True for figure in figures)) # noqa: F812
writer.add_figure("add_figure/figure_list", figures, 1)
if matplotlib.__version__ != '3.3.0':
if matplotlib.__version__ != "3.3.0":
self.assertTrue(all(plt.fignum_exists(figure.number) is False for figure in figures)) # noqa: F812
else:
print("Skipping fignum_exists, see https://github.com/matplotlib/matplotlib/issues/18163")
print(
"Skipping fignum_exists, see https://github.com/matplotlib/matplotlib/issues/18163"
)
writer.close()
class TestTensorBoardNumpy(BaseTestCase):
@unittest.skipIf(IS_WINDOWS, "Skipping on windows, see https://github.com/pytorch/pytorch/pull/109349 ")
@unittest.skipIf(IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 ")
@unittest.skipIf(
IS_WINDOWS,
"Skipping on windows, see https://github.com/pytorch/pytorch/pull/109349 ",
)
@unittest.skipIf(
IS_MACOS, "Skipping on mac, see https://github.com/pytorch/pytorch/pull/109349 "
)
def test_scalar(self):
res = make_np(1.1)
self.assertIsInstance(res, np.ndarray) and self.assertEqual(res.shape, (1,))
@ -751,8 +863,7 @@ class TestTensorBoardNumpy(BaseTestCase):
def test_pytorch_np_expect_fail(self):
with self.assertRaises(NotImplementedError):
res = make_np({'pytorch': 1.0})
res = make_np({"pytorch": 1.0})
class TestTensorProtoSummary(BaseTestCase):
@ -766,10 +877,14 @@ class TestTensorProtoSummary(BaseTestCase):
@skipIfTorchDynamo("Unsuitable test for Dynamo, behavior changes with version")
def test_half_tensor_proto(self, tensor_type, proto_type):
float_values = [1.0, 2.0, 3.0]
actual_proto = tensor_proto(
"dummy",
torch.tensor(float_values, dtype=tensor_type),
).value[0].tensor
actual_proto = (
tensor_proto(
"dummy",
torch.tensor(float_values, dtype=tensor_type),
)
.value[0]
.tensor
)
self.assertSequenceEqual(
[int_to_half(x) for x in actual_proto.half_val],
float_values,
@ -778,9 +893,7 @@ class TestTensorProtoSummary(BaseTestCase):
def test_float_tensor_proto(self):
float_values = [1.0, 2.0, 3.0]
actual_proto = (
tensor_proto("dummy", torch.tensor(float_values)).value[0].tensor
)
actual_proto = tensor_proto("dummy", torch.tensor(float_values)).value[0].tensor
self.assertEqual(actual_proto.float_val, float_values)
self.assertTrue(actual_proto.dtype == DataType.DT_FLOAT)
@ -796,24 +909,21 @@ class TestTensorProtoSummary(BaseTestCase):
def test_scalar_tensor_proto(self):
scalar_value = 0.1
actual_proto = (
tensor_proto("dummy", torch.tensor(scalar_value)).value[0].tensor
)
actual_proto = tensor_proto("dummy", torch.tensor(scalar_value)).value[0].tensor
self.assertAlmostEqual(actual_proto.float_val[0], scalar_value)
def test_complex_tensor_proto(self):
real = torch.tensor([1.0, 2.0])
imag = torch.tensor([3.0, 4.0])
actual_proto = (
tensor_proto("dummy", torch.complex(real, imag)).value[0].tensor
)
actual_proto = tensor_proto("dummy", torch.complex(real, imag)).value[0].tensor
self.assertEqual(actual_proto.scomplex_val, [1.0, 3.0, 2.0, 4.0])
def test_empty_tensor_proto(self):
actual_proto = tensor_proto("dummy", torch.empty(0)).value[0].tensor
self.assertEqual(actual_proto.float_val, [])
instantiate_parametrized_tests(TestTensorProtoSummary)
if __name__ == '__main__':
if __name__ == "__main__":
run_tests()