成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用視覺語言模型(VLMs)進行目標檢測

開發
有數百種模型和潛在應用場景,目標檢測在這些場景中非常有用,尤其是隨著小型語言模型的興起,所以今天我們將嘗試使用MLX上的Qwen2-VL-7B-Instruct-8bit。

在過去,你必須自己訓練模型,收集訓練數據,但現在許多基礎模型允許你在它們的基礎上進行微調,以獲得一個能夠檢測目標并與用戶用自然語言互動的系統。有數百種模型和潛在應用場景,目標檢測在這些場景中非常有用,尤其是隨著小型語言模型的興起,所以今天我們將嘗試使用MLX上的Qwen2-VL-7B-Instruct-8bit。

我們將使用MLX-VLM,這是由Prince Canuma(Blaizzy)創建的一個包,他是一位熱衷于開發和移植大型語言模型以兼容MLX的熱情開發者,這個框架為我們用戶抽象了很多代碼,使我們能夠用很少的代碼行運行這些模型。現在讓我們來看下面的代碼片段。你會發現它非常簡單。首先,你可以從Hugging Face定義模型,框架將下載所有相關組件。這個過程非常簡單,因為這個庫還提供了多個實用工具(apply_chat_template),可以將OpenAI的標準提示模板轉換為小型VLMs所需的模板。

這里的一個重要注意事項是在編寫代碼時,這個庫中的系統角色出現了一些問題,但未來很可能可以添加。但在本例中,我們在一個用戶消息中傳遞任務和響應格式,基本上我們將要求模型識別所有對象并返回一個坐標列表,其中第一個頂部將是邊界框的最小x/y坐標,后者將是最大坐標。同時,我們包括了對象名稱,并要求模型以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()

繪制結果如下:

總結

VLMs正在快速發展。兩年前,還沒有能夠適應MacBook并表現如此出色的模型。我個人的猜測是,這些模型將繼續發展,最終達到像YOLO這樣的模型的能力。還有很長的路要走,但正如你在這篇文章中看到的,設置這個演示非常容易。在邊緣設備上開發這種應用的潛力是無限的,我相信它們將在采礦、石油和天然氣、基礎設施和監控等行業產生重大影響。最好的部分是我們甚至還沒有討論微調、RAG或提示工程,這只是模型能力的展示。

責任編輯:趙寧寧 來源: 小白玩轉Python
相關推薦

2024-12-13 15:53:58

VLM小型視覺語言模型LLM

2024-01-17 12:10:44

AI訓練

2024-11-19 13:17:38

視覺語言模型Pytorch人工智能

2024-11-29 16:10:31

2025-02-18 08:00:00

C++YOLO目標檢測

2024-09-12 17:19:43

YOLO目標檢測深度學習

2025-01-22 13:15:10

2024-12-31 12:30:00

OpenCV計算機視覺

2024-11-08 15:37:47

2024-06-04 09:25:51

2024-10-30 15:30:00

智能體視覺模型

2025-01-06 08:20:00

YOLOv11目標檢測Python

2021-09-30 09:45:03

人工智能語言模型技術

2024-12-30 07:11:00

大型視覺語言模型VLMs人工智能

2023-11-22 13:45:37

計算機視覺數據預處理

2024-11-06 16:56:51

2024-11-12 09:20:03

神經網絡語言模型

2024-07-11 16:38:15

2025-04-30 03:20:00

2023-09-27 07:39:57

大型語言模型MiniGPT-4
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产成人jvid在线播放 | 久久久久久看片 | 久久91av| 午夜免费| 精品久久久久一区二区国产 | 日韩视频一区在线观看 | 午夜午夜精品一区二区三区文 | 天堂资源| 亚洲成人精 | 国产一级片精品 | 中文字幕日韩一区 | 亚洲精品视频在线 | 欧美精品片 | 欧美成人免费在线视频 | 久久高清精品 | 国产一区在线免费 | 亚洲一区二区精品视频 | 国产在线观看一区二区 | 91在线观看网址 | 亚洲男人网 | 日韩a视频 | 欧美日韩精品一区二区三区四区 | 国产一区二区在线看 | 在线视频一区二区三区 | 欧美高清视频一区 | 欧美激情欧美激情在线五月 | www.日韩| 91 视频网站 | 日韩欧美第一页 | 久久久久久高潮国产精品视 | 久久久精品 | 欧美不卡在线 | 精品国产精品 | 日韩av一区二区在线观看 | 久久久久久国产精品三区 | 亚洲国产一区视频 | 欧美一卡二卡在线观看 | 久久免费精彩视频 | 亚洲一区二区三 | 日韩欧美在线视频观看 | 日韩av成人在线 |