• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    Pytorch可视化的几种实现方法

    一,利用 tensorboardX 可视化网络结构

    参考 https://github.com/lanpa/tensorboardX
    支持scalar, image, figure, histogram, audio, text, graph, onnx_graph, embedding, pr_curve and video summaries.
    例子要求tensorboardX>=1.2 and pytorch>=0.4

    安装

    pip install tensorboardXpip install git+https://github.com/lanpa/tensorboardX

    例子

    # demo.py
    
    import torch
    import torchvision.utils as vutils
    import numpy as np
    import torchvision.models as models
    from torchvision import datasets
    from tensorboardX import SummaryWriter
    
    resnet18 = models.resnet18(False)
    writer = SummaryWriter()
    sample_rate = 44100
    freqs = [262, 294, 330, 349, 392, 440, 440, 440, 440, 440, 440]
    
    for n_iter in range(100):
    
        dummy_s1 = torch.rand(1)
        dummy_s2 = torch.rand(1)
        # data grouping by `slash`
        writer.add_scalar('data/scalar1', dummy_s1[0], n_iter)
        writer.add_scalar('data/scalar2', dummy_s2[0], n_iter)
    
        writer.add_scalars('data/scalar_group', {'xsinx': n_iter * np.sin(n_iter),
                                                 'xcosx': n_iter * np.cos(n_iter),
                                                 'arctanx': np.arctan(n_iter)}, n_iter)
    
        dummy_img = torch.rand(32, 3, 64, 64)  # output from network
        if n_iter % 10 == 0:
            x = vutils.make_grid(dummy_img, normalize=True, scale_each=True)
            writer.add_image('Image', x, n_iter)
    
            dummy_audio = torch.zeros(sample_rate * 2)
            for i in range(x.size(0)):
                # amplitude of sound should in [-1, 1]
                dummy_audio[i] = np.cos(freqs[n_iter // 10] * np.pi * float(i) / float(sample_rate))
            writer.add_audio('myAudio', dummy_audio, n_iter, sample_rate=sample_rate)
    
            writer.add_text('Text', 'text logged at step:' + str(n_iter), n_iter)
    
            for name, param in resnet18.named_parameters():
                writer.add_histogram(name, param.clone().cpu().data.numpy(), n_iter)
    
            # needs tensorboard 0.4RC or later
            writer.add_pr_curve('xoxo', np.random.randint(2, size=100), np.random.rand(100), n_iter)
    
    dataset = datasets.MNIST('mnist', train=False, download=True)
    images = dataset.test_data[:100].float()
    label = dataset.test_labels[:100]
    
    features = images.view(100, 784)
    writer.add_embedding(features, metadata=label, label_img=images.unsqueeze(1))
    
    # export scalar data to JSON for external processing
    writer.export_scalars_to_json("./all_scalars.json")
    writer.close()
    

    运行: python demo.py 会出现runs文件夹,然后在cd到工程目录运行tensorboard --logdir runs

    结果:


    二,利用 vistom 可视化

    参考:https://github.com/facebookresearch/visdom

    安装和启动
    安装: pip install visdom
    启动:python -m visdom.server示例

        from visdom import Visdom
        #单张
        viz.image(
            np.random.rand(3, 512, 256),
            opts=dict(title=\\\\\'Random!\\', caption=\\\\\'How random.\\'),
        )
        #多张
        viz.images(
            np.random.randn(20, 3, 64, 64),
            opts=dict(title=\\\\\'Random images\\', caption=\\\\\'How random.\\')
        )
    

    from visdom import Visdom
    
    image = np.zeros((100,100))
    vis = Visdom() 
    vis.text("hello world!!!")
    vis.image(image)
    vis.line(Y = np.column_stack((np.random.randn(10),np.random.randn(10))), 
             X = np.column_stack((np.arange(10),np.arange(10))),
             opts = dict(title = "line", legend=["Test","Test1"]))
    

    三,利用pytorchviz可视化网络结构

    参考:https://github.com/szagoruyko/pytorchviz

    到此这篇关于Pytorch可视化的几种实现方法的文章就介绍到这了,更多相关Pytorch可视化内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    您可能感兴趣的文章:
    • pytorch使用tensorboardX进行loss可视化实例
    • 使用pytorch实现可视化中间层的结果
    • Pytorch十九种损失函数的使用详解
    • pytorch教程网络和损失函数的可视化代码示例
    上一篇:Python爬虫必备之XPath解析库
    下一篇:Python中常见的反爬机制及其破解方法总结
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯 版权所有

    《增值电信业务经营许可证》 苏ICP备15040257号-8

    Pytorch可视化的几种实现方法 Pytorch,可视化,的,几种,实现,