《------往期经典推荐------》
二、机器学习实战专栏【链接】,已更新31期,欢迎关注,持续更新中~~
三、深度学习【Pytorch】专栏【链接】
四、【Stable Diffusion绘画系列】专栏【链接】
五、YOLOv8改进专栏【链接】,持续更新中~~
六、YOLO性能对比专栏【链接】,持续更新中~
《------正文------》
引言
本文主要介绍如何使用VLMs【视觉语言模型】进行对象检测。在过去,您必须训练自己的模型、收集训练数据才能进行指定类别的目标检测,但目前许多基础模型使您有一个坚实的基础来在它们之上进行微调,以获得一个可以检测对象并可以与对象进行交互的系统,同时可以通过自然语言进行交互。有数百种模型和潜在的应用程序可以使用对象检测,特别是随着小型语言模型的兴起,所以今天我们要尝试使用MLX的Qwen 2-VL-7 B-Instruct-8bit通过自然语言描述进行目标检测任务。
实现方式
我们将使用MLX-VLM,这是由Prince Canuma(Blaizzy)创建的一个包,他是一位充满激情的开发人员,专注于开发和移植大型语言模型以与MLX兼容,该框架为我们用户抽象了大量代码,以便用几行代码运行这些模型。
我们将使用下图来测试Qwen 2的目标检测能力。在本例中,我们将使用一个图像,其中有两辆采矿卡车或自卸卡车在矿场中运输矿石。这是一个用于采矿业的真实的应用程序的简单示例,因此您可以将此图像保存在一个名为images的新文件夹中,该文件夹位于代码的同一目录中!
现在让我们看一下下面显示的代码片段。首先,您可以从Hugging Face定义模型,框架将下载所有相关组件。这个过程非常简单,因为这个库还提供了多个实用程序(apply_chat_template)来将OpenAI的标准提示模板转换为小型VLM所需的模板。
对于这个例子,我们在一个用户消息中定义任务和响应格式,我们将要求模型识别所有对象并返回一个坐标列表,其中bboxes将是边界框的最小xy坐标和最大xy坐标。此外,我们还包含了对象名称,并要求模型以JSON对象的形式返回。
from mlx_vlm import load, apply_chat_template, generate
from mlx_vlm.utils import load_image
model, processor = load("mlx-community/Qwen2-VL-7B-Instruct-8bit")
config = model.config
image_path = "images/test.jpg"
image = load_image(image_path)
messages = [
{
"role": "user",
"content": """detect all the objects in the image, return bounding boxes for all of them using the following format: [{
"object": "object_name",
"bboxes": [[xmin, ymin, xmax, ymax], [xmin, ymin, xmax, ymax], ...]
}, ...]""",
}
]
prompt = apply_chat_template(processor, config, messages)
output = generate(model, processor, image, prompt, max_tokens=1000, temperature=0.7)
print(output)
运行前面的代码后,您将收到一个JSON响应,其中正确识别了两辆卡车:
[{
"object": "dump truck",
"bboxes": [
[100, 250, 380, 510]
]
}, {
"object": "dump truck",
"bboxes": [
[550, 250, 830, 490]
]
}]
既然我们有了对象名称和边界框坐标,我们可以编写一个函数来将这些结果绘制在图像上。代码如下:
import json
import re
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
def draw_and_plot_boxes_from_json(json_data, image_path):
"""
Parses the JSON data to extract bounding box coordinates,
scales them according to the image size, draws the boxes on the image,
and plots the image.
Args:
json_data (str or list): The JSON data as a string or already parsed list.
image_path (str): The path to the image file on which boxes are to be drawn.
"""
# If json_data is a string, parse it into a Python object
if isinstance(json_data, str):
# Strip leading/trailing whitespaces
json_data = json_data.strip()
# Remove code fences if present
json_data = re.sub(r"^```json\s*", "", json_data)
json_data = re.sub(r"```$", "", json_data)
json_data = json_data.strip()
try:
data = json.loads(json_data)
except json.JSONDecodeError as e:
print("Failed to parse JSON data:", e)
print("JSON data was:", repr(json_data))
return
else:
data = json_data
# Open the image
try:
img = Image.open(image_path)
except FileNotFoundError:
print(f"Image file not found at {image_path}. Please check the path.")
return
draw = ImageDraw.Draw(img)
width, height = img.size
# Change this part for Windows OS
# ImageFont.FreeTypeFont(r"C:\Windows\Fonts\CONSOLA.ttf", size=25)
font = ImageFont.truetype("/System/Library/Fonts/Menlo.ttc", size=25) # Process and draw boxes
for item in data:
object_type = item.get("object", "unknown")
for bbox in item.get("bboxes", []):
x1, y1, x2, y2 = bbox
# Scale down coordinates from a 1000x1000 grid to the actual image size
x1 = x1 * width / 1000
y1 = y1 * height / 1000
x2 = x2 * width / 1000
y2 = y2 * height / 1000
# Draw the rectangle on the image
draw.rectangle([(x1, y1), (x2, y2)], outline="blue", width=5)
text_position = (x1, y1)
draw.text(text_position, object_type, fill="red", font=font)
# Plot the image using matplotlib
plt.figure(figsize=(8, 8))
plt.imshow(img)
plt.axis("off") # Hide axes ticks
plt.show()
这是输出图像,边界框用正确的标签识别矿用卡车!
总结
这两年,随着视觉语言模型VLM正在快速发展,这为边缘设备开发这种应用类似的应用程序提供了无限可能。
好了,这篇文章就介绍到这里,喜欢的小伙伴感谢给点个赞和关注,更多精彩内容持续更新~~
关于本篇文章大家有任何建议或意见,欢迎在评论区留言交流!