对于23分类胃肠道hyperkvasir数据集的图像尺寸问题,在数据集中图像尺寸非常不一,我把他们丢resize到最大的3264*3264,在输入模型前应该缩小一点吗
对的 ,在对 HyperKvasir 数据集的图像进行模型训练前,将所有图像缩放到同一大小很合理。因为图像大小不同会对模型训练产生影响
不知道你这个问题是否已经解决, 如果还没有解决的话:建议将所有图像统一缩放到一个较小的尺寸,以避免对模型计算造成过大负担。具体的缩放尺寸建议如下:
from PIL import Image
# 或者使用import cv2
# 打开图片文件并获取尺寸信息
img = Image.open("example.jpg")
width, height = img.size
import pandas as pd
import matplotlib.pyplot as plt
# 读取所有图像的尺寸信息并转换为DataFrame
sizes = []
for filename in filenames:
img = Image.open(filename)
sizes.append(img.size)
df = pd.DataFrame(sizes, columns=["width", "height"])
# 绘制尺寸分布直方图
df.hist(bins=50, layout=(1,2), figsize=(12,4))
plt.tight_layout()
plt.show()
import os
# 缩放图像
def resize_image(image_path, max_size):
img = Image.open(image_path)
width, height = img.size
if width > max_size or height > max_size:
if width > height:
scaled_width = max_size
scaled_height = int(height * max_size / width)
else:
scaled_width = int(width * max_size / height)
scaled_height = max_size
img = img.resize((scaled_width, scaled_height))
return img
# 遍历所有图像并进行缩放
max_size = 1024
for filename in filenames:
img = resize_image(filename, max_size)
img.save(os.path.join(output_dir, os.path.basename(filename)))
最终得到的缩放图像可以输入模型进行分类。