Tensorboard学习-add_scaler()和add_image()

from torch.utils.tensorboard import SummaryWriter
import numpy as np
from PIL import Image

writer = SummaryWriter('logs')

# 用writer.add_scalar()绘制函数图像
for i in range(100):
    writer.add_scalar('y=2x', 2 * i, i)  # 'y=2x'为图表标题,第一个i为y轴,第二个i为x轴

# 用writer.add_image()显示图片,该函数可以读取torch.Tensor,numpy.array, 和 string/blobname类型
image_path = 'hymenoptera_data/train/ants/0013035.jpg'
image_PIL = Image.open(image_path)
print(type(image_PIL))  # 为PIL类型
image_array = np.array(image_PIL)  # 将PIL类型转换为add_image()可以识别的numpy.array类型
print(image_array.shape)  # 打印image_array的形状(H, W, C)
writer.add_image('test', img_tensor=image_array, global_step=1, dataformats='HWC')  # 由于是HWC顺序,因此要指定dataformats

writer.close()

# 查看结果在Terminal中输入tensorboard --logdir=logs --port=6007
# 其中--logdir=用于指定结果保存的文件夹,而--port=用于指定保存的网页端口号码

需要注意的点:

在应用numpy.array()将PIL格式的图像转换为numpy数组时,形状为HWC,而默认的writer.add_image()为CHW,因此在调用时需要用到dataformats=‘HWC’进行转换。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一些可以用于RS_images_2800数据集的特征处理代码示例: 1. 图像预处理 ``` import cv2 # 读入图像 img = cv2.imread('image.jpg') # 图像缩放 img = cv2.resize(img, (224, 224)) # 图像旋转 rows, cols = img.shape[:2] M = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 1) img = cv2.warpAffine(img, M, (cols, rows)) # 图像裁剪 img = img[50:150, 50:150] # 图像去噪 img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21) ``` 2. 特征提取 使用预训练的ResNet50模型提取图像特征: ``` import tensorflow as tf from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input # 加载模型 model = ResNet50(weights='imagenet', include_top=False, pooling='avg') # 读入图像 img = tf.keras.preprocessing.image.load_img('image.jpg', target_size=(224, 224)) # 图像预处理 x = tf.keras.preprocessing.image.img_to_array(img) x = preprocess_input(x) # 特征提取 features = model.predict(x.reshape(1, 224, 224, 3)) ``` 3. 特征归一化 ``` from sklearn.preprocessing import StandardScaler # 归一化 scaler = StandardScaler() features_norm = scaler.fit_transform(features) ``` 4. 数据增强 使用Keras的ImageDataGenerator进行数据增强: ``` from tensorflow.keras.preprocessing.image import ImageDataGenerator # 数据增强 datagen = ImageDataGenerator( rotation_range=30, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest') # 读入图像 img = tf.keras.preprocessing.image.load_img('image.jpg', target_size=(224, 224)) # 图像预处理 x = tf.keras.preprocessing.image.img_to_array(img) x = preprocess_input(x) # 数据增强 x = x.reshape((1,) + x.shape) i = 0 for batch in datagen.flow(x, batch_size=1): features_aug = model.predict(batch) i += 1 if i > 5: break ``` 5. 特征选择 使用Keras的Sequential模型选择最具代表性的特征: ``` from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense, Dropout # 特征选择 model = Sequential() model.add(Dense(256, input_dim=features.shape[1], activation='relu')) model.add(Dropout(0.5)) model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(features, labels, epochs=10, batch_size=32, validation_split=0.2) ``` 以上代码示例仅供参考,具体的特征处理步骤和代码实现应根据具体的任务和数据集进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值