使用MindSpore和MindFormers的AI图像分类例子,版本分别是2.2和1.0
测试代码:
import mindspore; mindspore.set_context(mode=0, device_id=0)
from mindformers import Trainercls_trainer = Trainer(task='image_classification', # 已支持的任务名model='vit_base_p16') # 已支持的模型名
input_data = './work/cat.png' # 一张猫的图片
# Example 1: 指定输入的数据完成模型推理
predict_result_d = cls_trainer.predict(input_data=input_data)
# Example 2: 开启推理(自动加载训练得到的最后一个权重)
predict_result_b = cls_trainer.predict(input_data=input_data, predict_checkpoint=True)
# Example 3: 加载指定的权重以完成推理
predict_result_c = cls_trainer.predict(input_data=input_data, predict_checkpoint='./output/checkpoint/rank_0/mindformers.ckpt')
print(predict_result_d)
执行报错
2024-09-13 10:51:53,648 - mindformers[mindformers/trainer/image_classification/image_classification.py:169] - INFO - .........Build Input Data For Predict..........
Traceback (most recent call last):File "/home/ma-user/work/test.py", line 8, in <module>predict_result_d = cls_trainer.predict(input_data=input_data)File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/mindspore/_checkparam.py", line 1313, in wrapperreturn func(*args, **kwargs)File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/mindformers/trainer/trainer.py", line 640, in predictoutput_result = self.trainer.predict(File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/mindformers/trainer/image_classification/image_classification.py", line 177, in predictbatch_input_data.append(load_image(input_data))File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/mindformers/tools/image_tools.py", line 54, in load_imagecontent = PIL.ImageOps.exif_transpose(content)File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/PIL/ImageOps.py", line 628, in exif_transposereturn image.copy()File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/PIL/Image.py", line 1178, in copyself.load()File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/PIL/Jpeg2KImagePlugin.py", line 306, in loadreturn ImageFile.ImageFile.load(self)File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/PIL/ImageFile.py", line 288, in loadraise_oserror(err_code)File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/PIL/ImageFile.py", line 72, in raise_oserrorraise OSError(msg)
OSError: broken data stream when reading image file
用的网上的猫的图片:
https://t10.baidu.com/it/u=124288009,197635351&fm=30&app=106&f=JPEG?w=640&h=405&s=59609955465253D6C418E1DE0300C021
上传到openi启智社区AI调试环境中
不明白为什么报os error
百度回答说:
根据提供的片段,可以推测你在尝试使用Python的一个图像处理库(例如Pillow)加载一个图像文件时遇到了问题。
错误原因可能有:
-
图像文件路径不正确或图像文件不存在。
-
图像文件损坏。
-
没有正确安装Pillow库或其版本不兼容。
解决方法:
-
确认图像文件的路径是否正确,并且文件确实存在于该路径。
-
尝试用其他图像查看器打开图像,确认图像文件是否损坏。
-
确保Pillow库已正确安装。可以通过运行
pip install Pillow
来安装或升级Pillow库。 -
如果路径和文件都没问题,尝试重新创建虚拟环境并安装Pillow库。
有重新执行了一次,这次出结果了:
2024-09-13 13:19:37,918 - mindformers[mindformers/trainer/base_trainer.py:911] - INFO - output result is: [[{'score': 0.6093656, 'label': 'tabby'}]]
2024-09-13 13:19:37,919 - mindformers[mindformers/trainer/base_trainer.py:912] - INFO - output result is saved at: image_classification_result.txt
2024-09-13 13:19:37,919 - mindformers[mindformers/trainer/base_trainer.py:913] - INFO - .........Predict Over!.........
output result is: [[{'score': 0.6093656, 'label': 'tabby'}]]
也就是条纹猫。但是不知道为什么后面还跟了一堆的报错信息:
2024-09-13 13:19:37,919 - mindformers[mindformers/trainer/base_trainer.py:913] - INFO - .........Predict Over!.............
Traceback (most recent call last):File "/home/ma-user/work/test.py", line 10, in <module>predict_result_b = cls_trainer.predict(input_data=input_data, predict_checkpoint=True)File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/mindspore/_checkparam.py", line 1313, in wrapperreturn func(*args, **kwargs)File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/mindformers/trainer/trainer.py", line 606, in predictself.config.load_checkpoint = self.get_last_checkpoint()File "/home/ma-user/anaconda3/envs/MindSpore/lib/python3.9/site-packages/mindformers/trainer/trainer.py", line 892, in get_last_checkpointcheckpoint for checkpoint in os.listdir(checkpoint_dir)
FileNotFoundError: [Errno 2] No such file or directory: './output/checkpoint/rank_0'
反正不管怎样,这个算过了。
总结下就是:可能对一些图片格式不太兼容吧,png是不错的。