第T4周:TensorFlow实现猴痘识别(Tensorboard的使用)

目标
1、学习tensorboard的使用
具体实现

(一)环境

语言环境:Python 3.10
编 译 器: PyCharm
框 架: TensorFlow

(二)具体步骤:

1. 安装Tensorboard
# pip install tensorboard
2.使用GPU

-----------------------------------------------utils.py-----------------------------------------------------

import tensorflow as tf  
import PIL  
import matplotlib.pyplot as plt  
  
  
def GPU_ON():  
    # 查询tensorflow版本  
    print("Tensorflow Version:", tf.__version__)  
    # print(tf.config.experimental.list_physical_devices('GPU'))  
  
    # 设置使用GPU  
    gpus = tf.config.list_physical_devices("GPU")  
    print(gpus)  
  
    if gpus:  
        gpu0 = gpus[0]  # 如果有多个GPU,仅使用第0个GPU  
        tf.config.experimental.set_memory_growth(gpu0, True)  # 设置GPU显存按需使用  
        tf.config.set_visible_devices([gpu0], "GPU")
3.导入猴痘图片数据,Tensorboard查看图片
# # 创建TensorBoard摘要器和文件写入器  
summary_writer = tf.summary.create_file_writer('./logs')

# 查看一下数据的基本情况  
data_dir = "./datasets/mp/"  
data_dir = pathlib.Path(data_dir)  # 转换成Path对象,便于后续访问  
image_count = len(list(data_dir.glob('*/*.jpg')))   # 遍历data_dir下面所有的.jpg图片(包含所有子目录)。  
print("图片总数量为:", image_count)  
MonkeyPox = list(data_dir.glob('MonkeyPox/*.jpg'))  # 遍历data_dir子目录MonkeyPox下所有的.jpg图片  
print("猴痘图片数量为:", len(MonkeyPox))  

# tf.summary.image() 需要一个包含 (batch_size, height, width, channels) 的 4 秩张量。因此,需要重塑张量。  
# 记录一个图像,因此 batch_size 为 1。图像为灰度图,因此将 channels 设置为 1。  
img = np.reshape(PIL.Image.open(MonkeyPox[1]), (-1, 224, 224, 3))  # 查看一张猴痘的图片,看看是什么样子  
with  summary_writer.as_default():  
    tf.summary.image("猴痘", img , step=0) # 显示一张图像

命令行,输入:

# >tensorboard --logdir ./logs

image.png
打开链接:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值