[tensorboard SummaryWriter] add_custom_scalars

(Atsushi SAKAI) #1

When I am executing add_custom_scalars then got error on Google Colab.

Script is follows, all script is taken from torch.utils.tensorboard document.

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)
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)
images, labels = next(iter(trainloader))

grid = torchvision.utils.make_grid(images)
writer.add_image('images', grid, 0)
writer.add_graph(model, images)
writer.add_text('lstm', 'This is an lstm', 0)
writer.add_text('rnn', 'This is an rnn', 10)
layout = {'Taiwan':{'twse':['Multiline',['twse/0050', 'twse/2330']]},
             'USA':{ 'dow':['Margin',   ['dow/aaa', 'dow/bbb', 'dow/ccc']],
                  'nasdaq':['Margin',   ['nasdaq/aaa', 'nasdaq/bbb', 'nasdaq/ccc']]}}

writer.add_custom_scalars(layout)
writer.close()

Error message are follows

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-6915b6548058> in <module>()
     24                   'nasdaq':['Margin',   ['nasdaq/aaa', 'nasdaq/bbb', 'nasdaq/ccc']]}}
     25 
---> 26 writer.add_custom_scalars(layout)
     27 writer.close()

1 frames
/usr/local/lib/python3.6/dist-packages/torch/utils/tensorboard/summary.py in custom_scalars(layout)
    381     layout = layout_pb2.Layout(category=categories)
    382     PluginData = [SummaryMetadata.PluginData(plugin_name='custom_scalars')]
--> 383     smd = SummaryMetadata(plugin_data=PluginData)
    384     tensor = TensorProto(dtype='DT_STRING',
    385                          string_val=[layout.SerializeToString()],

TypeError: Parameter to MergeFrom() must be instance of same class: expected tensorboard.SummaryMetadata.PluginData got list.

Ref.
https://pytorch.org/docs/stable/tensorboard.html

P.S.
I think it is better to add new category (TensorBoard)
Currently it is in Uncategrized

Setup TensorBoard Instruction is follows

!pip install tb-nightly
LOG_DIR = './runs'
get_ipython().system_raw(
    'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'
    .format(LOG_DIR)
)
!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
!unzip ngrok-stable-linux-amd64.zip
get_ipython().system_raw('./ngrok http 6006 &')
!curl -s http://localhost:4040/api/tunnels | python3 -c \
    "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"