在进行深度学习领域代码复现的过程中,常常涉及到用自己的数据集进行训练,但训练时经常出现
RuntimeError: Trying to resize storage that is not resizable
RuntimeError: stack expects each tensor to be equal size, but got [1, 512, 512] at entry 0 and [3, 512, 512] at entry 1
等关于数据集中图片尺寸、通道数不符的问题,经常令人感到头疼,因此本贴给出在如何训练之前先在数据集中进行自检,以避免出现错误。
尺寸
批量检查一个文件夹里的图片尺寸是否满足某一值(是否为512×512)
import os
from PIL import Image
def check_image_sizes(folder_path, target_width, target_height):
"""
检查指定文件夹内所有图像的尺寸是否为target_width x target_height。
如果不是,打印出不符合尺寸的图像的文件名和实际尺寸。
:param folder_path: 图像所在的文件夹路径
:param target_width: 目标宽度
:param target_height: 目标高度
"""
# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')):
file_path = os.path.join(folder_path, filename)
try:
with Image.open(file_path) as img:
width, height = img.size
if width != target_width or height != target_height:
print(f"文件 '{filename}' 的尺寸为 {width}x{height},与目标尺寸 {target_width}x{target_height} 不符。")
except IOError:
print(f"无法打开文件 '{filename}'。可能不是有效的图像文件。")
# 示例使用
if __name__ == "__main__":
folder_path = "xxxxxxxxx" # 替换为你的图像文件夹路径
target_width = 512
target_height = 512
check_image_sizes(folder_path, target_width, target_height)
批量更改文件夹内图片的尺寸(更改至512×512)
import os
from PIL import Image
def resize_images(input_folder, output_folder, target_width, target_height):
"""
将指定文件夹内的所有图像尺寸调整为 target_width x target_height,
并将调整后的图像保存到另一个文件夹。
:param input_folder: 原图像所在的文件夹路径
:param output_folder: 调整后的图像保存的文件夹路径
:param target_width: 目标宽度
:param target_height: 目标高度
"""
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 遍历文件夹中的所有文件
for filename in os.listdir(input_folder):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.tiff')):
input_path = os.path.join(input_folder, filename)
output_path = os.path.join(output_folder, filename)
try:
with Image.open(input_path) as img:
# 调整图像尺寸
img = img.resize((target_width, target_height), Image.Resampling.LANCZOS)
# 保存调整后的图像
img.save(output_path)
print(f"图像 '{filename}' 已调整尺寸并保存至 '{output_path}'。")
except IOError:
print(f"无法处理文件 '{filename}'。可能不是有效的图像文件。")
# 示例使用
if __name__ == "__main__":
input_folder = "xxxxxxxxxxxxx" # 替换为你的原图像文件夹路径
output_folder = "xxxxxxxxxxxx" # 替换为你希望保存调整后图像的文件夹路径
target_width = 512
target_height = 512
resize_images(input_folder, output_folder, target_width, target_height)
通道数
检查通道数是否一致(为3)
import os
from PIL import Image
def check_image_channels(directory, required_channels=3):
"""
Check if all images in the specified directory have the specified number of channels.
Args:
- directory (str): The path to the directory containing the images.
- required_channels (int): The required number of channels (default is 3 for RGB).
Returns:
- None
"""
# 支持的图片格式
supported_formats = ('.jpg', '.jpeg', '.png', '.bmp', '.tiff')
inconsistent_images = []
for filename in os.listdir(directory):
if filename.lower().endswith(supported_formats):
file_path = os.path.join(directory, filename)
with Image.open(file_path) as img:
# 获取实际的通道数
actual_channels = len(img.getbands())
# 检查通道数是否符合要求
if actual_channels != required_channels:
# 记录不符合要求的图片及其通道数
inconsistent_images.append((filename, actual_channels))
if inconsistent_images:
print("以下图片通道数不符合要求:")
for img, channels in inconsistent_images:
print(f"{img}: 实际通道数 {channels}")
else:
print("所有图片都符合要求的通道数。")
# 使用示例
directory_path = "xxxxxxxxxxxxxxxxxxx" # 你的文件夹
check_image_channels(directory_path)
更改通道数不一致的图片至3
import os
from PIL import Image
def check_and_convert_image_channels(directory, output_directory, required_channels=3):
"""
Check if all images in the specified directory have the specified number of channels.
Convert images to the required number of channels if they do not meet the requirements.
Args:
- directory (str): The path to the directory containing the images.
- output_directory (str): The path to the directory where converted images will be saved.
- required_channels (int): The required number of channels (default is 3 for RGB).
Returns:
- None
"""
# 确保输出目录存在
if not os.path.exists(output_directory):
os.makedirs(output_directory)
# 支持的图片格式
supported_formats = ('.jpg', '.jpeg', '.png', '.bmp', '.tiff')
inconsistent_images = []
for filename in os.listdir(directory):
if filename.lower().endswith(supported_formats):
file_path = os.path.join(directory, filename)
with Image.open(file_path) as img:
# 获取实际的通道数
actual_channels = len(img.getbands())
# 检查通道数是否符合要求
if actual_channels != required_channels:
# 记录不符合要求的图片及其通道数
inconsistent_images.append((filename, actual_channels))
# 转换图片为RGB格式
if img.mode != 'RGB':
img = img.convert('RGB')
output_path = os.path.join(output_directory, filename)
img.save(output_path)
if inconsistent_images:
print("以下图片通道数不符合要求,已转换并保存:")
for img, channels in inconsistent_images:
print(f"{img}: 实际通道数 {channels},已转换为RGB并保存")
else:
print("所有图片都符合要求的通道数。")
# 使用示例
directory_path = "xxxxxxxxxxx" #你的输入文件夹路径
output_directory_path = "xxxxxxxxxxx" #你的输出文件夹路径
check_and_convert_image_channels(directory_path, output_directory_path)