2019-04-26 04:22:34 +00:00
|
|
|
torch.utils.tensorboard
|
|
|
|
|
===================================
|
2022-03-10 22:21:03 +00:00
|
|
|
.. automodule:: torch.utils.tensorboard
|
2019-04-26 04:22:34 +00:00
|
|
|
|
|
|
|
|
Before going further, more details on TensorBoard can be found at
|
|
|
|
|
https://www.tensorflow.org/tensorboard/
|
|
|
|
|
|
|
|
|
|
Once you've installed TensorBoard, these utilities let you log PyTorch models
|
|
|
|
|
and metrics into a directory for visualization within the TensorBoard UI.
|
|
|
|
|
Scalars, images, histograms, graphs, and embedding visualizations are all
|
|
|
|
|
supported for PyTorch models and tensors as well as Caffe2 nets and blobs.
|
|
|
|
|
|
|
|
|
|
The SummaryWriter class is your main entry to log data for consumption
|
|
|
|
|
and visualization by TensorBoard. For example:
|
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
2019-04-29 23:00:47 +00:00
|
|
|
|
2019-04-26 04:22:34 +00:00
|
|
|
import torch
|
|
|
|
|
import torchvision
|
|
|
|
|
from torch.utils.tensorboard import SummaryWriter
|
|
|
|
|
from torchvision import datasets, transforms
|
|
|
|
|
|
|
|
|
|
# Writer will output to ./runs/ directory by default
|
|
|
|
|
writer = SummaryWriter()
|
|
|
|
|
|
|
|
|
|
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
|
|
|
|
|
trainset = datasets.MNIST('mnist_train', train=True, download=True, transform=transform)
|
|
|
|
|
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
|
2019-04-29 23:00:47 +00:00
|
|
|
model = torchvision.models.resnet50(False)
|
|
|
|
|
# Have ResNet model take in grayscale rather than RGB
|
|
|
|
|
model.conv1 = torch.nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
|
2019-04-26 04:22:34 +00:00
|
|
|
images, labels = next(iter(trainloader))
|
|
|
|
|
|
|
|
|
|
grid = torchvision.utils.make_grid(images)
|
|
|
|
|
writer.add_image('images', grid, 0)
|
|
|
|
|
writer.add_graph(model, images)
|
|
|
|
|
writer.close()
|
|
|
|
|
|
2019-04-29 23:00:47 +00:00
|
|
|
This can then be visualized with TensorBoard, which should be installable
|
|
|
|
|
and runnable with::
|
|
|
|
|
|
2019-08-12 21:52:48 +00:00
|
|
|
pip install tensorboard
|
2019-04-29 23:00:47 +00:00
|
|
|
tensorboard --logdir=runs
|
2019-04-26 04:22:34 +00:00
|
|
|
|
2019-05-08 21:03:04 +00:00
|
|
|
|
|
|
|
|
Lots of information can be logged for one experiment. To avoid cluttering
|
|
|
|
|
the UI and have better result clustering, we can group plots by naming them
|
|
|
|
|
hierarchically. For example, "Loss/train" and "Loss/test" will be grouped
|
|
|
|
|
together, while "Accuracy/train" and "Accuracy/test" will be grouped separately
|
|
|
|
|
in the TensorBoard interface.
|
|
|
|
|
|
|
|
|
|
.. code:: python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from torch.utils.tensorboard import SummaryWriter
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
writer = SummaryWriter()
|
|
|
|
|
|
|
|
|
|
for n_iter in range(100):
|
|
|
|
|
writer.add_scalar('Loss/train', np.random.random(), n_iter)
|
|
|
|
|
writer.add_scalar('Loss/test', np.random.random(), n_iter)
|
|
|
|
|
writer.add_scalar('Accuracy/train', np.random.random(), n_iter)
|
|
|
|
|
writer.add_scalar('Accuracy/test', np.random.random(), n_iter)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Expected result:
|
|
|
|
|
|
|
|
|
|
.. image:: _static/img/tensorboard/hier_tags.png
|
|
|
|
|
:scale: 75 %
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-04-26 04:22:34 +00:00
|
|
|
.. currentmodule:: torch.utils.tensorboard.writer
|
2019-04-29 23:00:47 +00:00
|
|
|
|
2019-04-26 04:22:34 +00:00
|
|
|
.. autoclass:: SummaryWriter
|
2019-04-29 23:00:47 +00:00
|
|
|
|
2019-05-08 21:03:04 +00:00
|
|
|
.. automethod:: __init__
|
2019-04-29 23:00:47 +00:00
|
|
|
.. automethod:: add_scalar
|
2019-05-08 21:03:04 +00:00
|
|
|
.. automethod:: add_scalars
|
2019-04-29 23:00:47 +00:00
|
|
|
.. automethod:: add_histogram
|
|
|
|
|
.. automethod:: add_image
|
2019-05-08 21:03:04 +00:00
|
|
|
.. automethod:: add_images
|
2019-04-29 23:00:47 +00:00
|
|
|
.. automethod:: add_figure
|
|
|
|
|
.. automethod:: add_video
|
|
|
|
|
.. automethod:: add_audio
|
|
|
|
|
.. automethod:: add_text
|
|
|
|
|
.. automethod:: add_graph
|
|
|
|
|
.. automethod:: add_embedding
|
|
|
|
|
.. automethod:: add_pr_curve
|
|
|
|
|
.. automethod:: add_custom_scalars
|
Support 3D mesh/point cloud (#20413)
Summary:
I started adding support for the new **[mesh/point cloud](https://github.com/tensorflow/graphics/blob/master/tensorflow_graphics/g3doc/tensorboard.md)** data type introduced to TensorBoard recently.
I created the functions to add the data, created the appropriate summaries.
This new data type however requires a **Merged** summary containing the data for the vertices, colors and faces.
I got stuck at this stage. Maybe someone can help. lanpa?
I converted the example code by Google to PyTorch:
```python
import numpy as np
import trimesh
import torch
from torch.utils.tensorboard import SummaryWriter
sample_mesh = 'https://storage.googleapis.com/tensorflow-graphics/tensorboard/test_data/ShortDance07_a175_00001.ply'
log_dir = 'runs/torch'
batch_size = 1
# Camera and scene configuration.
config_dict = {
'camera': {'cls': 'PerspectiveCamera', 'fov': 75},
'lights': [
{
'cls': 'AmbientLight',
'color': '#ffffff',
'intensity': 0.75,
}, {
'cls': 'DirectionalLight',
'color': '#ffffff',
'intensity': 0.75,
'position': [0, -1, 2],
}],
'material': {
'cls': 'MeshStandardMaterial',
'roughness': 1,
'metalness': 0
}
}
# Read all sample PLY files.
mesh = trimesh.load_remote(sample_mesh)
vertices = np.array(mesh.vertices)
# Currently only supports RGB colors.
colors = np.array(mesh.visual.vertex_colors[:, :3])
faces = np.array(mesh.faces)
# Add batch dimension, so our data will be of shape BxNxC.
vertices = np.expand_dims(vertices, 0)
colors = np.expand_dims(colors, 0)
faces = np.expand_dims(faces, 0)
# Create data placeholders of the same shape as data itself.
vertices_tensor = torch.as_tensor(vertices)
faces_tensor = torch.as_tensor(faces)
colors_tensor = torch.as_tensor(colors)
writer = SummaryWriter(log_dir)
writer.add_mesh('mesh_color_tensor', vertices=vertices_tensor, faces=faces_tensor,
colors=colors_tensor, config_dict=config_dict)
writer.close()
```
I tried adding only the vertex summary, hence the others are supposed to be optional.
I got the following error from TensorBoard and it also didn't display the points:
```
Traceback (most recent call last):
File "/home/dawars/workspace/pytorch/venv/lib/python3.6/site-packages/werkzeug/serving.py", line 302, in run_wsgi
execute(self.server.app)
File "/home/dawars/workspace/pytorch/venv/lib/python3.6/site-packages/werkzeug/serving.py", line 290, in execute
application_iter = app(environ, start_response)
File "/home/dawars/workspace/pytorch/venv/lib/python3.6/site-packages/tensorboard/backend/application.py", line 309, in __call__
return self.data_applications[clean_path](environ, start_response)
File "/home/dawars/workspace/pytorch/venv/lib/python3.6/site-packages/werkzeug/wrappers/base_request.py", line 235, in application
resp = f(*args[:-2] + (request,))
File "/home/dawars/workspace/pytorch/venv/lib/python3.6/site-packages/tensorboard/plugins/mesh/mesh_plugin.py", line 252, in _serve_mesh_metadata
tensor_events = self._collect_tensor_events(request)
File "/home/dawars/workspace/pytorch/venv/lib/python3.6/site-packages/tensorboard/plugins/mesh/mesh_plugin.py", line 188, in _collect_tensor_events
tensors = self._multiplexer.Tensors(run, instance_tag)
File "/home/dawars/workspace/pytorch/venv/lib/python3.6/site-packages/tensorboard/backend/event_processing/plugin_event_multiplexer.py", line 400, in Tensors
return accumulator.Tensors(tag)
File "/home/dawars/workspace/pytorch/venv/lib/python3.6/site-packages/tensorboard/backend/event_processing/plugin_event_accumulator.py", line 437, in Tensors
return self.tensors_by_tag[tag].Items(_TENSOR_RESERVOIR_KEY)
KeyError: 'mesh_color_tensor_COLOR'
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/20413
Differential Revision: D15500737
Pulled By: orionr
fbshipit-source-id: 426e8b966037d08c065bce5198fd485fd80a2b67
2019-05-24 21:25:19 +00:00
|
|
|
.. automethod:: add_mesh
|
2019-10-04 00:06:04 +00:00
|
|
|
.. automethod:: add_hparams
|
2019-07-22 19:02:34 +00:00
|
|
|
.. automethod:: flush
|
|
|
|
|
.. automethod:: close
|