AutoGluon多模态预测器快速入门:图像分类实战

AutoGluon多模态预测器快速入门:图像分类实战

autogluon AutoGluon: AutoML for Image, Text, Time Series, and Tabular Data autogluon 项目地址: https://gitcode.com/gh_mirrors/au/autogluon

前言

在计算机视觉领域,图像分类是最基础也是最常见的任务之一。传统方法需要人工设计特征或精心调整模型架构,而AutoGluon项目提供的MultiModalPredictor通过自动化机器学习技术,让开发者只需几行代码就能构建高性能的图像分类模型。本文将详细介绍如何使用AutoGluon的MultiModalPredictor进行图像分类任务。

环境准备

首先需要安装AutoGluon的多模态组件:

pip install autogluon.multimodal

数据集准备

我们使用Shopee-IET数据集的子集作为示例,该数据集包含服装类商品的图片,分为四个类别:BabyPants、BabyShirt、womencasualshoes和womenchiffontop。

AutoGluon提供了便捷的数据下载函数:

from autogluon.multimodal.utils.misc import shopee_dataset

download_dir = './ag_automm_tutorial_imgcls'
train_data_path, test_data_path = shopee_dataset(download_dir)

加载后的训练数据包含800个样本,每行包含图片路径和对应的标签。除了图片路径,MultiModalPredictor还支持直接加载图片字节数组:

train_data_byte, test_data_byte = shopee_dataset(download_dir, is_bytearray=True)

模型训练

使用MultiModalPredictor训练模型非常简单:

from autogluon.multimodal import MultiModalPredictor
import uuid

model_path = f"./tmp/{uuid.uuid4().hex}-automm_shopee"
predictor = MultiModalPredictor(label="label", path=model_path)
predictor.fit(
    train_data=train_data_path,
    time_limit=30  # 训练时间限制为30秒
)

关键参数说明:

  • label:指定数据集中标签列的名称
  • path:模型保存路径
  • time_limit:控制训练时间,实际应用中可根据需求调整

模型评估

训练完成后,可以在测试集上评估模型性能:

scores = predictor.evaluate(test_data_path, metrics=["accuracy"])
print('Top-1 test acc: %.3f' % scores["accuracy"])

MultiModalPredictor的一个强大特性是能够无缝处理不同格式的输入数据。无论是使用图片路径还是字节数组训练的模型,都可以评估另一种格式的数据:

scores = predictor.evaluate(test_data_byte, metrics=["accuracy"])
print('Top-1 test acc: %.3f' % scores["accuracy"])

模型预测

对于新的图片,可以先用以下代码查看:

image_path = test_data_path.iloc[0]['image']
from IPython.display import Image, display
pil_img = Image(filename=image_path)
display(pil_img)

然后进行预测:

predictions = predictor.predict({'image': [image_path]})
print(predictions)

如果需要获取每个类别的概率:

proba = predictor.predict_proba({'image': [image_path]})
print(proba)

同样支持字节数组输入:

image_byte = test_data_byte.iloc[0]['image']
predictions = predictor.predict({'image': [image_byte]})
print(predictions)

特征提取

MultiModalPredictor还可以提取图片的嵌入特征,这些特征可用于迁移学习或其他下游任务:

feature = predictor.extract_embedding({'image': [image_path]})
print(feature[0].shape)  # 输出特征维度

模型保存与加载

训练完成后,模型会自动保存。可以方便地重新加载:

loaded_predictor = MultiModalPredictor.load(model_path)
load_proba = loaded_predictor.predict_proba({'image': [image_path]})
print(load_proba)

安全提示:加载模型时使用pickle模块,请确保模型文件来源可信,避免安全风险。

进阶使用

本文展示了AutoGluon MultiModalPredictor在图像分类任务中的基础用法。AutoGluon还支持:

  • 自定义模型架构
  • 调整训练参数
  • 多模态联合训练(如图像+文本)
  • 迁移学习等高级功能

对于希望进一步定制模型的用户,可以参考AutoGluon的进阶文档,探索更多可能性。

结语

AutoGluon的MultiModalPredictor极大地简化了图像分类模型的开发流程,使开发者能够专注于业务问题而非模型细节。通过本文介绍的基础流程,读者可以快速上手并应用于实际项目中。

autogluon AutoGluon: AutoML for Image, Text, Time Series, and Tabular Data autogluon 项目地址: https://gitcode.com/gh_mirrors/au/autogluon

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

徐皓锟Godly

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

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

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

打赏作者

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

抵扣说明:

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

余额充值