-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError: Object of type Tensor is not JSON serializable #554
Comments
Please let us know more information regarding it. |
Hi @liuyazhou951218 - I haven't actually caught up with testing pytorch 1.0 compatibility. In 0.4 a passed tensor would be converted to a numpy array automatically, but perhaps that's not working correctly anymore? Can you give a reproducible example, like providing the values of the args that you're sending to |
@JafferWilson @JackUrb thanks for your quick response.I just forked this project https://github.com/JinghuiZhou/awesome_face_antispoofing to my repository. My OS is Ubuntu 14.04.5 LTS,and the visdom is 0.1.8.8. Here is a script for visualization in https://github.com/JinghuiZhou/awesome_face_antispoofing/blob/master/utils/Visualizer.py. and I think that the wrong part of the code is |
And the browser shows that 'File failed to load: /extensions/MathZoom.js' |
Can you print the values of X and Y right before the vis.line call? |
Closing as there hasn't been any updates. I suspect that the issue is that |
Can we open this issue @JackUrb as am getting similar error and my values of Y are : |
@Eyshika While we're able to convert tensors, converting a list of tensors currently is not supported. Can you try editing
|
@JackUrb I got the answer, I converted each element to numpy using |
Right - the above suggestion should hopefully do exactly that automatically, but I haven't been able to take a chance to test it. Glad to hear you found a solution though. |
can you tell more details about it?I meet similar problem and I dont know how to convert each element to numpy...Thanks a lot |
@spicy-dog torch_types = []
try:
import torch
torch_types.append(torch.Tensor)
torch_types.append(torch.nn.Parameter)
except (ImportError, AttributeError):
pass Then, in the class Visualizer(object), I add the following function: def to_numpy(self, a):
if isinstance(a, list):
return np.array(a)
for kind in torch_types:
if isinstance(a, kind):
if hasattr(a, 'detach'):
a = a.detach()
return a.cpu().numpy()
return a Finally, I changed the if len(val) == 1:
y = np.array(val)
else:
y = np.array(val).reshape(-1, len(val)) to if len(val) == 1:
y = np.array([self.to_numpy(i) for i in val])
else:
y = np.array([self.to_numpy(i) for i in val]).reshape(-1, len(val)) That is all you need for the modification in |
is there something like |
If like me you just wanted to save the dict of tensors in a human readable way check this out: https://discuss.pytorch.org/t/typeerror-tensor-is-not-json-serializable/36065/3 or https://stackoverflow.com/questions/12943819/how-to-prettyprint-a-json-file/66180687#66180687. |
Bug Description
**When trying to train my model and show the process,I found that the process of train is OK,but it shows that 'TypeError: Object of type Tensor is not JSON serializable' like this.And the visdom didn't work.
Traceback (most recent call last):
File "main.py", line 354, in
fire.Fire()
File "/home/lyz/anaconda3/lib/python3.7/site-packages/fire/core.py", line 127, in Fire
component_trace = _Fire(component, args, context, name)
File "/home/lyz/anaconda3/lib/python3.7/site-packages/fire/core.py", line 366, in _Fire
component, remaining_args)
File "/home/lyz/anaconda3/lib/python3.7/site-packages/fire/core.py", line 542, in _CallCallable
result = fn(*varargs, **kwargs)
File "main.py", line 198, in train
'val_acc':val_acc.value()[0]},win_name = 'Acc')
File "/home/lyz/project/awesome_face_antispoofing/utils/Visualizer.py", line 67, in plot_many_stack
update=None if x == 0 else 'append'
File "/home/lyz/anaconda3/lib/python3.7/site-packages/visdom/init.py", line 335, in wrapped_f
return f(*args, **kwargs)
File "/home/lyz/anaconda3/lib/python3.7/site-packages/visdom/init.py", line 1367, in line
update=update, name=name)
File "/home/lyz/anaconda3/lib/python3.7/site-packages/visdom/init.py", line 335, in wrapped_f
return f(*args, **kwargs)
File "/home/lyz/anaconda3/lib/python3.7/site-packages/visdom/init.py", line 1292, in scatter
return self._send(data_to_send, endpoint=endpoint)
File "/home/lyz/anaconda3/lib/python3.7/site-packages/visdom/init.py", line 548, in _send
data=json.dumps(msg),
File "/home/lyz/anaconda3/lib/python3.7/json/init.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/home/lyz/anaconda3/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/home/lyz/anaconda3/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/home/lyz/anaconda3/lib/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.class.name} '
TypeError: Object of type Tensor is not JSON serializable
while my Pytorch is 1.0.0.Any advice will be greatful,thanks in advance.
The text was updated successfully, but these errors were encountered: