tensorboard fails when add_image and add_histogram are used together. (cannot use same tag name for different modality)
Author: anmolsjoshiCreated Sep 12, 2018Updated Dec 11, 2023
Labelsbug
import torch
from torch import nn
import torch.nn.functional as F
from torchvision import utils
import numpy as np
from tensorboardX import SummaryWriter
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 10)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(-1, 320)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x, dim=-1)
def vistensor(tensor, ch=0, allkernels=False, nrow=8, padding=1):
'''
vistensor: visuzlization tensor
@ch: visualization channel
@allkernels: visualization all tensores
'''
n,c,w,h = tensor.shape
if allkernels: tensor = tensor.view(n*c,-1,w,h )
elif c != 3: tensor = tensor[:,ch,:,:].unsqueeze(dim=1)
rows = np.min( (tensor.shape[0]//nrow + 1, 64 ) )
grid = utils.make_grid(tensor, nrow=nrow, normalize=True, padding=padding)
return grid
net = Net()
writer = SummaryWriter(log_dir='logs/')
for name, param in net.named_parameters():
#writer.add_histogram(tag=name, values=param.data, global_step=1)
w_img = param.data
shape = list(w_img.size())
if len(shape) == 1:
w_img = w_img.view(shape[0], 1, 1, 1)
if shape[0] > 16:
nrow = 16
else:
nrow = shape[0]
grid = vistensor(w_img, ch=0, allkernels=True, padding=0, nrow=nrow)
elif len(shape) == 2:
if shape[0] > shape[1]:
w_img = torch.transpose(w_img, 0, 1)
shape = list(w_img.size())
w_img = w_img.view(shape[0] * shape[1], 1, 1, 1)
grid = vistensor(w_img, ch=0, allkernels=True,
nrow=int(np.sqrt(shape[0] * shape[1])), padding=0)
elif len(shape) == 3:
w_img = w_img.view(shape[0], 1, shape[1], shape[2])
grid = vistensor(w_img, ch=0, allkernels=True, padding=1)
elif len(shape) == 4:
grid = vistensor(w_img, ch=0, allkernels=True, padding=1)
else:
continue
writer.add_image(name, grid.numpy(), 1)
writer.add_histogram(tag=name, values=param.data, global_step=1)It looks like add_image and add_histogram can't work together for the above code. One or the other can be run independently, but cannot be run together. I get the error below, any idea what I'm doing wrong?
execute(self.server.app)
File "/home/joshi/nlptorch/env/lib/python3.5/site-packages/werkzeug/serving.py", line 258, in execute
application_iter = app(environ, start_response)
File "/home/joshi/nlptorch/env/lib/python3.5/site-packages/tensorboard/backend/application.py", line 270, in __call__
return self.data_applications[clean_path](environ, start_response)
File "/home/joshi/nlptorch/env/lib/python3.5/site-packages/werkzeug/wrappers.py", line 308, in application
resp = f(*args[:-2] + (request,))
File "/home/joshi/nlptorch/env/lib/python3.5/site-packages/tensorboard/plugins/histogram/histograms_plugin.py", line 233, in histograms_route
return http_util.Respond(request, body, mime_type, code=code)
File "/home/joshi/nlptorch/env/lib/python3.5/site-packages/tensorboard/backend/http_util.py", line 116, in Respond
content = json.dumps(json_util.Cleanse(content, encoding),
File "/home/joshi/nlptorch/env/lib/python3.5/site-packages/tensorboard/backend/json_util.py", line 68, in Cleanse
return [Cleanse(i, encoding) for i in obj]
File "/home/joshi/nlptorch/env/lib/python3.5/site-packages/tensorboard/backend/json_util.py", line 68, in <listcomp>
return [Cleanse(i, encoding) for i in obj]
File "/home/joshi/nlptorch/env/lib/python3.5/site-packages/tensorboard/backend/json_util.py", line 68, in Cleanse
return [Cleanse(i, encoding) for i in obj]
File "/home/joshi/nlptorch/env/lib/python3.5/site-packages/tensorboard/backend/json_util.py", line 68, in <listcomp>
return [Cleanse(i, encoding) for i in obj]
File "/home/joshi/nlptorch/env/lib/python3.5/site-packages/tensorboard/backend/json_util.py", line 68, in Cleanse
return [Cleanse(i, encoding) for i in obj]
File "/home/joshi/nlptorch/env/lib/python3.5/site-packages/tensorboard/backend/json_util.py", line 68, in <listcomp>
return [Cleanse(i, encoding) for i in obj]
File "/home/joshi/nlptorch/env/lib/python3.5/site-packages/tensorboard/backend/json_util.py", line 66, in Cleanse
return tf.compat.as_text(obj, encoding)
File "/home/joshi/nlptorch/env/lib/python3.5/site-packages/tensorflow/python/util/compat.py", line 88, in as_text
return bytes_or_text.decode(encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
E0912 01:00:08.212224 Thread-26 _internal.py:88] Error on request:@lanpa any idea what's wrong?
Using tensorboard 1.7.0, pytorch 0.4, torchvision 0.2 and tensorboardX from the FAQ
Source: lanpa/tensorboardX