【跑实验05】利用CLIP中的图像编码器,如何遍历文件夹中的图像,将图像文件改为28*28的尺寸,然后输出到excel中的每一列,最后一列全都标记为0

一、初步实现

要遍历文件夹中的图像并将其尺寸调整为28x28,并将结果输出到Excel中,可以按照以下步骤进行操作:

首先,确保您已经安装了Pandas库,用于处理Excel文件。可以使用以下命令安装它:

pip install pandas

然后,使用以下代码来遍历文件夹中的图像、调整尺寸并输出到Excel中:

import os
import torch
import clip
from PIL import Image
import pandas as pd

# 加载预训练的CLIP模型
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)

# 图像文件夹路径
folder_path = '/path/to/folder'  # 将此路径替换为图像文件夹的实际路径

# 创建空DataFrame用于存储图像特征
df = pd.DataFrame()

# 遍历图像文件夹
for filename in os.listdir(folder_path):
    if filename.endswith('.jpg') or filename.endswith('.png'):  # 仅处理.jpg和.png格式的图像文件
        image_path = os.path.join(folder_path, filename)

        # 加载和预处理图像
        image = Image.open(image_path).convert('RGB')
        image_resized = image.resize((28, 28))
        image_input = preprocess(image_resized).unsqueeze(0).to(device)

        # 图像编码
        with torch.no_grad():
            image_features = model.encode_image(image_input)

        # 将图像特征添加到DataFrame中
        image_features_list = image_features.squeeze().tolist()
        df[filename] = image_features_list

# 在DataFrame的最后一列添加全为0的标记列
df['label'] = 0

# 将DataFrame保存到Excel文件
output_path = '/path/to/output.xlsx'  # 将此路径替换为输出Excel文件的实际路径
df.to_excel(output_path, index=False)

请将代码中的/path/to/folder替换为包含图像文件的文件夹的实际路径,并将/path/to/output.xlsx替换为您想要保存输出Excel文件的实际路径。

这段代码会遍历指定的图像文件夹中的图像文件(仅处理.jpg和.png格式的文件),将图像尺寸调整为28x28,然后使用CLIP模型对图像进行编码,并将结果存储在一个DataFrame中。最后,将DataFrame保存为Excel文件,其中最后一列的值都标记为0。

但是代码运行过程中,频繁报错:

在这里插入图片描述

报错的错误类型为:

/home/wangzhenkuan/CLIP_image_encoder.py:33: PerformanceWarning: DataFrame is highly fragmented.  This is usually the result of calling `frame.insert` many times, which has poor performance.  Consider joining all columns at once using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.copy()`
  df[filename] = image_features_list
/home/wangzhenkuan/CLIP_image_encoder.py:33: PerformanceWarning: DataFrame is highly fragmented.  This is usually the result of calling `frame.insert` many times, which has poor performance.  Consider joining all columns at once using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.copy()`
  df[filename] = image_features_list
/home/wangzhenkuan/CLIP_image_encoder.py:33: PerformanceWarning: DataFrame is highly fragmented.  This is usually the result of calling `frame.insert` many times, which has poor performance.  Consider joining all columns at once using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.copy()`
  df[filename] = image_features_list
/home/wangzhenkuan/CLIP_image_encoder.py:33: PerformanceWarning: DataFrame is highly fragmented.  This is usually the result of calling `frame.insert` many times, which has poor performance.  Consider joining all columns at once using pd.concat(axis=1) instead. To get a de-fragmented frame, use `newframe = frame.copy()`
  df[filename] = image_features_list

二、警告信息的解决

警告信息表明DataFrame的性能受到了碎片化的影响,这是由于多次调用frame.insert导致的。为了避免这个性能问题,可以使用pd.concat(axis=1)一次性将所有列连接起来。

以下是修改后的代码,使用pd.concat来连接所有特征列:

import os
import torch
import clip
from PIL import Image
import pandas as pd

# 加载预训练的CLIP模型
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)

# 图像文件夹路径
folder_path = 'segment-anything-main/notebooks/output/'  # 将此路径替换为图像文件夹的实际路径

# 创建空DataFrame用于存储图像特征
df = pd.DataFrame()

# 创建空列表用于存储图像特征
feature_list = []

# 遍历图像文件夹
for filename in os.listdir(folder_path):
    if filename.endswith('.jpg') or filename.endswith('.png'):  # 仅处理.jpg和.png格式的图像文件
        image_path = os.path.join(folder_path, filename)

        # 加载和预处理图像
        image = Image.open(image_path).convert('RGB')
        image_resized = image.resize((28, 28))
        image_input = preprocess(image_resized).unsqueeze(0).to(device)

        # 图像编码
        with torch.no_grad():
            image_features = model.encode_image(image_input)

        # 将图像特征添加到列表中
        image_features_list = image_features.squeeze().tolist()
        feature_list.append(image_features_list)

# 使用pd.concat(axis=1)将所有特征列连接起来
df = pd.DataFrame(feature_list).T

# 在DataFrame的最后一列添加全为0的标记列
df['label'] = 0

# 将DataFrame保存到Excel文件
output_path = 'output_negtive.xlsx'  # 将此路径替换为输出Excel文件的实际路径
df.to_excel(output_path, index=False)

这样修改后的代码将避免性能警告,并使用pd.concat(axis=1)一次性将所有特征列添加到DataFrame中。

三、Excel的限制

Traceback (most recent call last):
  File "/home/wangzhenkuan/CLIP_image_encoder.py", line 43, in <module>
    df.to_excel(output_path, index = False)
  File "/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pandas/core/generic.py", line 2252, in to_excel
    formatter.write(
  File "/home/miniconda3/envs/wzk_base/lib/python3.10/site-packages/pandas/io/formats/excel.py", line 923, in write
    raise ValueError(
ValueError: This sheet is too large! Your sheet size is: 512, 292549 Max sheet size is: 1048576, 16384

错误信息为:

在这里插入图片描述
这个错误是因为生成的Excel表格太大,超出了Excel的最大行列限制。在您的代码中,可能由于图像文件夹中包含的图像数量过多,导致生成的DataFrame的行数过多,超出了Excel的最大行数限制。

为了解决这个问题,您可以考虑以下几种方法:

  1. 分批处理:将图像文件夹中的图像分成多个批次进行处理,每次处理一部分图像,生成多个较小的Excel表格。
  2. 降低图像尺寸:将图像缩小,以减少生成的特征向量的数量。可以适当调整image_resized = image.resize((28, 28))这一行代码中的缩放尺寸。
  3. 使用其他数据存储格式:如果Excel表格的行列限制仍然不足以容纳所有特征向量,可以考虑使用其他数据存储格式,例如CSV文件或数据库。

使用CSV文件存储:

import os
import torch
import clip
from PIL import Image
import pandas as pd

# 加载预训练的CLIP模型
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)

# 图像文件夹路径
folder_path = 'segment-anything-main/notebooks/output/'  # 将此路径替换为图像文件夹的实际路径

# 创建空DataFrame用于存储图像特征
df = pd.DataFrame()

# 创建空列表用于存储图像特征
feature_list = []

# 遍历图像文件夹
for filename in os.listdir(folder_path):
    if filename.endswith('.jpg') or filename.endswith('.png'):  # 仅处理.jpg和.png格式的图像文件
        image_path = os.path.join(folder_path, filename)

        # 加载和预处理图像
        image = Image.open(image_path).convert('RGB')
        image_resized = image.resize((28, 28))
        image_input = preprocess(image_resized).unsqueeze(0).to(device)

        # 图像编码
        with torch.no_grad():
            image_features = model.encode_image(image_input)

        # 将图像特征添加到列表中
        image_features_list = image_features.squeeze().tolist()
        feature_list.append(image_features_list)

# 使用pd.concat(axis=1)将所有特征列连接起来
df = pd.DataFrame(feature_list).T

# 在DataFrame的最后一列添加全为0的标记列
df['label'] = 0

# 将DataFrame保存到Excel文件
output_path = 'output_negtive.csv'
df.to_excel(output_path, index=False)

在这里插入图片描述

出现 “ValueError: No engine for filetype: ‘csv’” 错误通常是因为缺少适当的库或模块来处理 CSV 文件。这可能是由于 Pandas 版本较旧或缺少某些依赖项。

请尝试确保 Pandas 库已经正确安装,并检查是否缺少与 CSV 文件处理相关的其他库。您可以尝试更新 Pandas 版本或重新安装 Pandas 来解决此问题。

另外,您也可以尝试将输出文件类型更改为其他格式,例如 Excel (.xlsx) 文件,以确保代码能够正确运行。

四、尝试解决

当处理大量图像文件时,可以考虑使用分批处理的方式,将图像分成多个批次进行处理,并生成多个较小的Excel表格。以下是修改后的代码:

import os
import torch
import clip
from PIL import Image
import pandas as pd

# 加载预训练的CLIP模型
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)

# 图像文件夹路径
folder_path = 'segment-anything-main/notebooks/output/'  # 将此路径替换为图像文件夹的实际路径

# 批处理大小和Excel表格名称
batch_size = 10000  # 每个批次的图像数量
excel_prefix = 'output_batch_'  # Excel表格的前缀名称

# 获取图像文件列表
image_files = [filename for filename in os.listdir(folder_path) if filename.endswith(('.jpg', '.png'))]

# 计算批次数量
num_batches = len(image_files) // batch_size + 1

for batch_idx in range(num_batches):
    # 获取当前批次的图像文件列表
    start_idx = batch_idx * batch_size
    end_idx = min(start_idx + batch_size, len(image_files))
    batch_files = image_files[start_idx:end_idx]

    # 创建空DataFrame用于存储图像特征
    df = pd.DataFrame()
    feature_list = []

    # 遍历当前批次的图像文件
    for filename in batch_files:
        image_path = os.path.join(folder_path, filename)

        # 加载和预处理图像
        image = Image.open(image_path).convert('RGB')
        image_resized = image.resize((28, 28))
        image_input = preprocess(image_resized).unsqueeze(0).to(device)

        # 图像编码
        with torch.no_grad():
            image_features = model.encode_image(image_input)

        # 将图像特征添加到DataFrame中
        image_features_list = image_features.squeeze().tolist()
        feature_list.append(image_features_list)
    
    df = pd.DataFrame(feature_list).T

    # 在DataFrame的最后一列添加全为0的标记列
    df['label'] = 0

    # 生成当前批次的Excel表格
    excel_filename = f"{excel_prefix}{batch_idx + 1}.xlsx"
    output_path = os.path.join(folder_path, excel_filename)
    df.to_excel(output_path, index=False)

    print(f"Batch {batch_idx + 1} processed. Excel file saved: {excel_filename}")

这时不再报错,正常运行:

在这里插入图片描述
可以顺利跑完!

在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旅途中的宽~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值