Tensorboard

1.加载数据

  • Dataset
    • 提供一种方式去获取数据及其Iabel
  • Dataloader
    • 为后面的网络提供不同的数据形式

Dataset

1.如何获取每一个数据及其label

2.告诉我们总共有多少的数据

python 复制代码
from torch.utils.data import Dataset
from PIL import Image
import os

class myData(Dataset):
    def __init__(self,root_dir,label_dir):
        self.root_dir=root_dir
        self.label_dir=label_dir
        self.path=os.path.join(self.root_dir,self.label_dir)
        self.img_path=os.listdir(self.path)

    def __getitem__(self, idx):
        img_name=self.img_path[idx]
        img_item_path=os.path.join(self.root_dir,self.label_dir,img_name)
        img=Image.open(img_item_path)
        label=self.label_dir
        return img,label

    def __len__(self):
        return len(self.img_path)

root_dir="hymenoptera_data/train"
ants_label_dir="ants"
bees_label_dir="bees"
ants_dataset=myData(root_dir,ants_label_dir)
bees_dataset=myData(root_dir,bees_label_dir)

train_dataset=ants_dataset+bees_dataset

1.1批量创建label文件

把图片的label 生成以图片名为文件名的txt文档

python 复制代码
import os

root_dir = 'hymenoptera_data/train'
target_dir = 'bees_images' #可换ants_images
img_path = os.listdir(os.path.join(root_dir, target_dir))
label = target_dir.split('_')[0]
out_dir = 'bees_labels' #可换ants_images
for i in img_path:
    file_name = i.split('.jpg')[0]
    with open(os.path.join(root_dir, out_dir,"{}.txt".format(file_name)),'w') as f:
        f.write(label)


2.Tensorboard 的使用

2.1 add_scalar()------显示Tensorboard

python 复制代码
    def add_scalar(
        self,
        tag,
        scalar_value,
        global_step=None,
        walltime=None,
        new_style=False,
        double_precision=False,
    ):
        """Add scalar data to summary.

        Args:
            tag (str): Data identifier
            scalar_value (float or string/blobname): Value to save
            global_step (int): Global step value to record
            walltime (float): Optional override default walltime (time.time())
              with seconds after epoch of event
            new_style (boolean): Whether to use new style (tensor field) or old
              style (simple_value field). New style could lead to faster data loading.
python 复制代码
from torch.utils.tensorboard import SummaryWriter

writer=SummaryWriter("logs")

# writer.add_image()

for i in range(100):
    writer.add_scalar("y=x",i,i)

writer.close()

终端中运行

python 复制代码
tensorboard --logdir=logs

若很多窗口都叫6006就很不方便,这时候指定窗口名

python 复制代码
tensorboard --logdir=logs --port=6007


若标签未变

python 复制代码
from torch.utils.tensorboard import SummaryWriter

writer=SummaryWriter("logs")

# writer.add_image()

for i in range(100):
    writer.add_scalar("y=2x",3*i,i)

writer.close()

说明每一个writer中也记录了上一个的writer
解决:

①擦除TensorBoard的日志文件并终止该进程,但它不是首选的,因为它会破坏有关您训练的历史信息。

②可以让每个新的培训工作写入(顶级日志的)新的子目录目录。然后TensorBoard将把每个作业视为新的"运行",并创建一个很好的比较视图,以便您可以看到模型的不同小版本之间的培训有何不同。

注意:

子文件夹,也就是说创建新的

Summarywriter("新文件夹")

2.2 add_image()

python 复制代码
    def add_image(
        self, tag, img_tensor, global_step=None, walltime=None, dataformats="CHW"
    ):
        """Add image data to summary.

        Note that this requires the ``pillow`` package.

        Args:
            tag (str): Data identifier
            img_tensor (torch.Tensor, numpy.ndarray, or string/blobname): Image data
            global_step (int): Global step value to record
            walltime (float): Optional override default walltime (time.time())
              seconds after epoch of event
            dataformats (str): Image data format specification of the form
              CHW, HWC, HW, WH, etc.

由于img_tensor (torch.Tensor, numpy.ndarray, or string/blobname): Image data需要如上四种类型的图像,所以需要

  • 利用Opencv读取图片,获得numpy型图片数据
  • 利用numpy.array(),对PIL图片进行转换
python 复制代码
from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image

writer=SummaryWriter("logs")
image_path="data/train/bees_image/16838648_415acd9e3f.jpg"
img = Image.open(image_path)
img_array = np.array(img)
print(type(img_array))
print(img_array.shape)

writer.add_image("test",img_array,2,dataformats='HWC')

writer.close()

注意:dataformats='HWC'

相关推荐
特立独行的猫A6 小时前
别再用 nohup 了:让应用在后台常驻的正确姿势与 systemd 服务详解
java·后端·python
对你无可奈何6 小时前
给 ComfyUI 做了一个手机助手
人工智能
feasibility.7 小时前
英伟达把物理AI的3D模型“应用商店”开放了:2575个SimReady仿真资产,总计约472GB,可让agent装配机器人训练场景
人工智能·3d·机器人·仿真·建模·具身智能·英伟达
智联视频超融合平台8 小时前
消防宣传月过半,“AI+消防“给视频平台换了张考卷
人工智能·边缘计算·视频监控·安防·视频联网
冬奇Lab8 小时前
一天一个开源项目(第262篇):AG-UI —— 把 AI Agent 接进前端的开放协议,和 MCP、A2A 并列的第三块拼图
人工智能·开源
吃我一窝jio9 小时前
Python与Go线程模型深度对比
java·开发语言·python
罗西的思考9 小时前
【轻量化复现大模型 / KIMI K3】MTP / EAGLE‑3 模块交互式演示
人工智能·深度学习·算法
zwd20059 小时前
Manim 怎么放大画面的某一部分?
python·动画·可视化·manim·数学动画
屿华AI10 小时前
AUTOSAR 实战-诊断工具 | UDS 0x27 安全访问的配置化实现:种子密钥算法插件化设计
python·测试工具·汽车
RobinDevNotes10 小时前
Claude Code 剪视频,不上传、不要 API Key
人工智能·python·音视频·开发工具·ipython