本文主要是介绍pytorch_visdom可视化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 pytorch show by visdom
使用过程中,出现这个错误由于没有打开Visdom服务
Traceback (most recent call last):File "/home/sx/anaconda3/envs/py35/lib/python3.5/site-packages/visdom/__init__.py", line 711, in _senddata=json.dumps(msg),File "/home/sx/anaconda3/envs/py35/lib/python3.5/site-packages/visdom/__init__.py", line 677, in _handle_postr = self.session.post(url, data=data)File "/home/sx/anaconda3/envs/py35/lib/python3.5/site-packages/requests/sessions.py", line 590, in postreturn self.request('POST', url, data=data, json=json, **kwargs)File "/home/sx/anaconda3/envs/py35/lib/python3.5/site-packages/requests/sessions.py", line 542, in requestresp = self.send(prep, **send_kwargs)File "/home/sx/anaconda3/envs/py35/lib/python3.5/site-packages/requests/sessions.py", line 655, in sendr = adapter.send(request, **kwargs)File "/home/sx/anaconda3/envs/py35/lib/python3.5/site-packages/requests/adapters.py", line 516, in sendraise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8097): Max retries exceeded with url: /events (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f7565af11d0>: Failed to establish a new connection: [Errno 111] Connection refused',))
2 pip install visdom
启动visdom服务使用命令:
python -m visdom.server
观察到:
Checking for scripts.
It's Alive!
INFO:root:Application Started
You can navigate to http://localhost:8097
说明启动成功,即可访问http://localhost:8097。
3 how to use visdim ,define the Visualizer class
import visdom
import time
import numpy as np
from matplotlib import pyplot as plt
from sklearn.metrics import roc_curveclass Visualizer(object):def __init__(self, env='default', **kwargs):self.vis = visdom.Visdom(env=env, **kwargs)self.vis.close()self.iters = {}self.lines = {}def display_current_results(self, iters, x, name='train_loss'):if name not in self.iters:self.iters[name] = []if name not in self.lines:self.lines[name] = []self.iters[name].append(iters)self.lines[name].append(x)self.vis.line(X=np.array(self.iters[name]),Y=np.array(self.lines[name]),win=name,opts=dict(legend=[name], title=name))def display_roc(self, y_true, y_pred):fpr, tpr, ths = roc_curve(y_true, y_pred)self.vis.line(X=fpr,Y=tpr,# win='roc',opts=dict(legend=['roc'],title='roc'))
4 call the class
if opt.display:visualizer = Visualizer()if opt.display:visualizer.display_current_results(iters, loss.item(), name='train_loss')visualizer.display_current_results(iters, acc, name='train_acc')
5 the result
link:https://blog.csdn.net/weixin_43290709/article/details/105937290
二、pytorch模型转换为onnx
import torch
import torchvision.models as models
resnet18 = models.resnet18(pretrained=True)
x = torch.randn(1, 3, 224, 224, requires_grad=False)# Export the model
torch.onnx.export(resnet18, # model being runx, # model input (or a tuple for multiple inputs)"resnet18.onnx", # where to save the model (can be a file or file-like object)export_params=True, # store the trained parameter weights inside the model fileopset_version=11, # the ONNX version to export the model todo_constant_folding=True, # whether to execute constant folding for optimizationinput_names = ['input'], # the model's input namesoutput_names = ['output'], # the model's output names)
这篇关于pytorch_visdom可视化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!