I have kind of a similar problem. Training runs fine for me and I also have some add_scalar() methods in my code.
But as soon as I add:
tensorboard.add_graph(net, input_to_model=img, verbose=False)
in my training loop I get following error:

File "c:/SCNN_Pytorch/new_train.py", line 108, in train
    tensorboard.add_graph(net, input_to_model=img, verbose=False)
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\utils\tensorboard\writer.py", line 724, in add_graph
    self._get_file_writer().add_graph(graph(model, input_to_model, verbose))
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\utils\tensorboard\_pytorch_graph.py", line 286, in graph
    trace = torch.jit.trace(model, args)
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\jit\_trace.py", line 733, in trace
    return trace_module(
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\jit\_trace.py", line 934, in trace_module
    module._c._create_method_from_trace(
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\nn\modules\module.py", line 725, in _call_impl
    result = self._slow_forward(*input, **kwargs)
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\nn\modules\module.py", line 709, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\nn\parallel\data_parallel.py", line 149, in forward
    return self.module(*inputs, **kwargs)
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\nn\modules\module.py", line 725, in _call_impl
    result = self._slow_forward(*input, **kwargs)
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\nn\modules\module.py", line 709, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "c:\SCNN_Pytorch\model.py", line 52, in forward
    loss_seg = self.ce_loss(seg_pred, seg_gt) #seg_gt.shape = [4, 288, 512]
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\nn\modules\module.py", line 725, in _call_impl
    result = self._slow_forward(*input, **kwargs)
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\nn\modules\module.py", line 709, in _slow_forward
    result = self.forward(*input, **kwargs)
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\nn\modules\loss.py", line 961, in forward
    return F.cross_entropy(input, target, weight=self.weight,
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\nn\functional.py", line 2468, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "C:\anaconda3\envs\SCNN\lib\site-packages\torch\nn\functional.py", line 2260, in nll_loss
    if input.size(0) != target.size(0):
AttributeError: 'NoneType' object has no attribute 'size'

Which doesnt really make any sense. I checked input (which is img) and my target. Both have .size(0)=4 type int. If I print following statement:
print(img.size(0) != target.size(0))
the result is False. Can anyone help me with this?
Thank you in advance!