Skip to content
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 #258

Closed
yt605155624 opened this issue Apr 28, 2018 · 3 comments
Closed

TypeError: Object of type 'Tensor' is not JSON serializable #258

yt605155624 opened this issue Apr 28, 2018 · 3 comments

Comments

@yt605155624
Copy link

yt605155624 commented Apr 28, 2018

Hello,I run your train.py with GPU and the newest pytorch >>> torch.version '0.4.0a0+5463a4a',THEN I have this problem ,I haven't use pytorch and visdom before ,so I don't know how to fix it ,Could you please help me with this problem?
Traceback (most recent call last):
File "/home/yt/anaconda3/lib/python3.6/site-packages/visdom/init.py", line 261, in _send
data=json.dumps(msg),
File "/home/yt/anaconda3/lib/python3.6/json/init.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 180, in default
o.class.name)
TypeError: Object of type 'Tensor' is not JSON serializable
(epoch: 1, iters: 300, time: 0.278, data: 0.001) D_A: 0.164 G_A: 0.396 cycle_A: 1.978 idt_A: 1.903 D_B: 0.312 G_B: 0.730 cycle_B: 4.014 idt_B: 0.921
Exception in user code:
Traceback (most recent call last):
File "/home/yt/anaconda3/lib/python3.6/site-packages/visdom/init.py", line 261, in _send
data=json.dumps(msg),
File "/home/yt/anaconda3/lib/python3.6/json/init.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 180, in default
o.class.name)
TypeError: Object of type 'Tensor' is not JSON serializable
Traceback (most recent call last):
File "train.py", line 35, in
visualizer.display_current_results(model.get_current_visuals(), epoch, save_result)
File "/media/yt/SearchforGraduationDesign/pytorch-CycleGAN-and-pix2pix/util/visualizer.py", line 53, in display_current_results
image_numpy = util.tensor2im(image)
File "/media/yt/SearchforGraduationDesign/pytorch-CycleGAN-and-pix2pix/util/util.py", line 30, in tensor2im
image_numpy = image_tensor[0].cpu().float().numpy()
RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

@HugoGranstrom
Copy link

"Note: The current software works well with PyTorch 0.1-0.3. PyTorch 0.4 support will be added by the end of May."
It currently does not work with Pytorch 0.4, you need to install Pytorch 0.3.1, preferably in a virtual environment so that you can have both versions installed at the same time.

@junyanz junyanz closed this as completed Sep 9, 2018
@brando90
Copy link

Hello,I run your train.py with GPU and the newest pytorch >>> torch.version '0.4.0a0+5463a4a',THEN I have this problem ,I haven't use pytorch and visdom before ,so I don't know how to fix it ,Could you please help me with this problem?
Traceback (most recent call last):
File "/home/yt/anaconda3/lib/python3.6/site-packages/visdom/init.py", line 261, in _send
data=json.dumps(msg),
File "/home/yt/anaconda3/lib/python3.6/json/init.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 180, in default
o.class.name)
TypeError: Object of type 'Tensor' is not JSON serializable
(epoch: 1, iters: 300, time: 0.278, data: 0.001) D_A: 0.164 G_A: 0.396 cycle_A: 1.978 idt_A: 1.903 D_B: 0.312 G_B: 0.730 cycle_B: 4.014 idt_B: 0.921
Exception in user code:
Traceback (most recent call last):
File "/home/yt/anaconda3/lib/python3.6/site-packages/visdom/init.py", line 261, in _send
data=json.dumps(msg),
File "/home/yt/anaconda3/lib/python3.6/json/init.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 180, in default
o.class.name)
TypeError: Object of type 'Tensor' is not JSON serializable
Traceback (most recent call last):
File "train.py", line 35, in
visualizer.display_current_results(model.get_current_visuals(), epoch, save_result)
File "/media/yt/SearchforGraduationDesign/pytorch-CycleGAN-and-pix2pix/util/visualizer.py", line 53, in display_current_results
image_numpy = util.tensor2im(image)
File "/media/yt/SearchforGraduationDesign/pytorch-CycleGAN-and-pix2pix/util/util.py", line 30, in tensor2im
image_numpy = image_tensor[0].cpu().float().numpy()
RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

did this help? fossasia/visdom#554

@brando90
Copy link

Hello,I run your train.py with GPU and the newest pytorch >>> torch.version '0.4.0a0+5463a4a',THEN I have this problem ,I haven't use pytorch and visdom before ,so I don't know how to fix it ,Could you please help me with this problem?
Traceback (most recent call last):
File "/home/yt/anaconda3/lib/python3.6/site-packages/visdom/init.py", line 261, in _send
data=json.dumps(msg),
File "/home/yt/anaconda3/lib/python3.6/json/init.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 180, in default
o.class.name)
TypeError: Object of type 'Tensor' is not JSON serializable
(epoch: 1, iters: 300, time: 0.278, data: 0.001) D_A: 0.164 G_A: 0.396 cycle_A: 1.978 idt_A: 1.903 D_B: 0.312 G_B: 0.730 cycle_B: 4.014 idt_B: 0.921
Exception in user code:
Traceback (most recent call last):
File "/home/yt/anaconda3/lib/python3.6/site-packages/visdom/init.py", line 261, in _send
data=json.dumps(msg),
File "/home/yt/anaconda3/lib/python3.6/json/init.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/home/yt/anaconda3/lib/python3.6/json/encoder.py", line 180, in default
o.class.name)
TypeError: Object of type 'Tensor' is not JSON serializable
Traceback (most recent call last):
File "train.py", line 35, in
visualizer.display_current_results(model.get_current_visuals(), epoch, save_result)
File "/media/yt/SearchforGraduationDesign/pytorch-CycleGAN-and-pix2pix/util/visualizer.py", line 53, in display_current_results
image_numpy = util.tensor2im(image)
File "/media/yt/SearchforGraduationDesign/pytorch-CycleGAN-and-pix2pix/util/util.py", line 30, in tensor2im
image_numpy = image_tensor[0].cpu().float().numpy()
RuntimeError: Can't call numpy() on Variable that requires grad. Use var.detach().numpy() instead.

did this help? fossasia/visdom#554

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants