- 🍨 本文为🔗365天深度学习训练营 中的学习记录博客
- 🍖 原作者:K同学啊
目标:
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
打开链接: