{"id":4683,"date":"2024-09-02T20:43:22","date_gmt":"2024-09-02T12:43:22","guid":{"rendered":"https:\/\/www.aqwu.net\/wp\/?p=4683"},"modified":"2024-09-03T14:36:11","modified_gmt":"2024-09-03T06:36:11","slug":"%e4%bd%bf%e7%94%a8-qwen-qwen2-vl-2b-instruct%e8%bf%9b%e8%a1%8c%e5%9b%be%e7%89%87%e8%87%aa%e5%8a%a8%e6%a0%87%e6%b3%a8","status":"publish","type":"post","link":"https:\/\/www.aqwu.net\/wp\/?p=4683","title":{"rendered":"\u4f7f\u7528 Qwen\/Qwen2-VL-2B-Instruct\u8fdb\u884c\u56fe\u7247\u81ea\u52a8\u6807\u6ce8"},"content":{"rendered":"\n<p>\u8fd9\u662f Qwen-VL \u6a21\u578b\u7684\u6700\u65b0\u7248\u672c\uff0c\u4ee3\u8868\u4e86\u8fd1\u4e00\u5e74\u7684\u521b\u65b0\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e3b\u8981\u589e\u5f3a\u529f\u80fd\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>SoTA\u5bf9\u5404\u79cd\u5206\u8fa8\u7387\u548c\u6bd4\u4f8b\u7684\u56fe\u50cf\u7684\u7406\u89e3<\/strong>\uff1aQwen2-VL\u5728\u89c6\u89c9\u7406\u89e3\u57fa\u51c6\u4e0a\u8fbe\u5230\u4e86\u6700\u5148\u8fdb\u7684\u6027\u80fd\uff0c\u5305\u62ecMathVista\u3001DocVQA\u3001RealWorldQA\u3001MTVQA\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u7406\u89e3 20 \u5206\u949f+ \u7684\u89c6\u9891<\/strong>\uff1aQwen2-VL \u53ef\u4ee5\u7406\u89e3 20 \u5206\u949f\u4ee5\u4e0a\u7684\u89c6\u9891\uff0c\u4ee5\u8fdb\u884c\u9ad8\u8d28\u91cf\u7684\u57fa\u4e8e\u89c6\u9891\u7684\u95ee\u7b54\u3001\u5bf9\u8bdd\u3001\u5185\u5bb9\u521b\u5efa\u7b49\u3002<\/li>\n\n\n\n<li><strong>\u53ef\u4ee5\u64cd\u4f5c\u60a8\u7684\u624b\u673a\u3001\u673a\u5668\u4eba\u7b49\u7684\u4ee3\u7406<\/strong>\uff1aQwen2-VL \u5177\u6709\u590d\u6742\u7684\u63a8\u7406\u548c\u51b3\u7b56\u80fd\u529b\uff0c\u53ef\u4ee5\u4e0e\u624b\u673a\u3001\u673a\u5668\u4eba\u7b49\u8bbe\u5907\u96c6\u6210\uff0c\u6839\u636e\u89c6\u89c9\u73af\u5883\u548c\u6587\u672c\u6307\u4ee4\u8fdb\u884c\u81ea\u52a8\u64cd\u4f5c\u3002<\/li>\n\n\n\n<li><strong>\u591a\u8bed\u8a00\u652f\u6301<\/strong>\uff1a\u4e3a\u4e86\u670d\u52a1\u5168\u7403\u7528\u6237\uff0c\u9664\u4e86\u82f1\u6587\u548c\u4e2d\u6587\u5916\uff0cQwen2-VL \u73b0\u5728\u8fd8\u652f\u6301\u7406\u89e3\u56fe\u50cf\u4e2d\u4e0d\u540c\u8bed\u8a00\u7684\u6587\u672c\uff0c\u5305\u62ec\u5927\u591a\u6570\u6b27\u6d32\u8bed\u8a00\u3001\u65e5\u8bed\u3001\u97e9\u8bed\u3001\u963f\u62c9\u4f2f\u8bed\u3001\u8d8a\u5357\u8bed\u7b49\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4e3b\u8981\u6709\u4e24\u4e2a\u6a21\u578b\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/huggingface.co\/Qwen\/Qwen2-VL-2B-Instruct\">Qwen2-VL-2B-Instruct<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/huggingface.co\/Qwen\/Qwen2-VL-7B-Instruct\">Qwen2-VL-7B-Instruct<\/a><\/p>\n\n\n\n<p>\u4e3b\u8981\u662f\u5bf9\u76ee\u5f55\u4e0b\u7684\u56fe\u7247\u6587\u4ef6\u81ea\u52a8\u8fdb\u884c\u6807\u6ce8\uff0c\u56fe\u7247\u7684\u9ad8\u548c\u5bbd\u5efa\u8bae\u4e0d\u8981\u8d85\u8fc72048\uff0c\u4e0d\u7136\u6709\u53ef\u80fdGPU\u5185\u5b58\u4e0d\u591f\u3002<\/p>\n\n\n\n<p>\u4f1a\u81ea\u52a8\u6839\u636e test01.img \u4ea7\u751f\u6807\u6ce8\u6587\u4ef6 test01.txt \uff0c\u8fd9\u6837\u8bad\u7ec3 flux1 \u5c31\u5f88\u597d\u3002<\/p>\n\n\n\n<p>\u521b\u5efa\u73af\u5883\u548c\u5b89\u88c5\u4f9d\u8d56\u6027\u7684\u5e93<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">conda create -yn Qwen2-VL python=3.10\nconda activate Qwen2-VL\npip install qwen-vl-utils\npip3 install torch torchvision torchaudio\npip install 'accelerate&gt;=0.26.0'\npip install jmespath\npip install six\npip install datasets\npip install auto-gptq\npip install optimum\n\npip install git+https:\/\/github.com\/huggingface\/transformers\n<\/pre><\/div>\n\n\n\n<p>\u4ee3\u7801\u652f\u6301\u591aGPU\u64cd\u4f5c\uff0c\u5f53GPU\u5185\u5b58\u4e0d\u591f\u65f6\uff0c\u9002\u91cf\u6dfb\u52a0GPU\uff0c\u4e0b\u9762\u7684\u6d4b\u8bd5\u662f3 *24G(RTX 4090)<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >python test08.py\n<\/pre><\/div>\n\n\n\n<p>\u4e0b\u9762\u662f\u4ee3\u7801<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >import os\nimport torch\nfrom transformers import Qwen2VLForConditionalGeneration, AutoProcessor\nfrom qwen_vl_utils import process_vision_info\nfrom tqdm import tqdm\nfrom PIL import Image, ImageOps\n\n# \u521d\u59cb\u5316\u6a21\u578b\u548c\u5904\u7406\u5668\nmodel = Qwen2VLForConditionalGeneration.from_pretrained(\n    \"Qwen\/Qwen2-VL-2B-Instruct\", torch_dtype=\"auto\", device_map=\"auto\"\n)\nprocessor = AutoProcessor.from_pretrained(\"Qwen\/Qwen2-VL-2B-Instruct\")\n\n# \u5b9a\u4e49\u56fe\u50cf\u6587\u4ef6\u5939\u8def\u5f84\nimage_folder_path = \"\/mnt\/d\/AIImages\/Qwen\/png\/\"\n\n# \u83b7\u53d6\u6587\u4ef6\u5217\u8868\nimage_files = [f for f in os.listdir(image_folder_path) if f.endswith(\".png\") or f.endswith(\".jpg\")]\n\n# \u904d\u5386\u6587\u4ef6\u5939\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u5e76\u663e\u793a\u8fdb\u5ea6\u6761\nfor filename in tqdm(image_files, desc=\"Processing Images\"):\n    image_path = os.path.join(image_folder_path, filename)\n\n    # \u6253\u5f00\u56fe\u50cf\u5e76\u68c0\u67e5\u5c3a\u5bf8\n    with Image.open(image_path) as img:\n        img = ImageOps.exif_transpose(img)  # \u5904\u7406 EXIF \u4e2d\u7684\u65cb\u8f6c\u4fe1\u606f\n        max_size = 2048\n        # \u4ec5\u5f53\u56fe\u50cf\u5bbd\u5ea6\u6216\u9ad8\u5ea6\u8d85\u8fc72048\u65f6\u624d\u8fdb\u884c\u8c03\u6574\n        if img.width &gt; max_size or img.height &gt; max_size:\n            if img.width &gt; img.height:\n                # \u5982\u679c\u5bbd\u5ea6\u5927\u4e8e\u9ad8\u5ea6\uff0c\u5219\u5c06\u5bbd\u5ea6\u8c03\u6574\u4e3a2048\uff0c\u4fdd\u6301\u957f\u5bbd\u6bd4\n                new_width = max_size\n                new_height = int((max_size \/ img.width) * img.height)\n            else:\n                # \u5982\u679c\u9ad8\u5ea6\u5927\u4e8e\u5bbd\u5ea6\uff0c\u5219\u5c06\u9ad8\u5ea6\u8c03\u6574\u4e3a2048\uff0c\u4fdd\u6301\u957f\u5bbd\u6bd4\n                new_height = max_size\n                new_width = int((max_size \/ img.height) * img.width)\n            \n            new_size = (new_width, new_height)\n            img = img.resize(new_size, Image.LANCZOS)\n            img.save(image_path)  # \u4fdd\u5b58\u7f29\u653e\u540e\u7684\u56fe\u50cf\n\n    messages = [\n        {\n            \"role\": \"user\",\n            \"content\": [\n                {\n                    \"type\": \"image\",\n                    \"image\": f\"file:\/\/{image_path}\",\n                },\n                {\"type\": \"text\", \"text\": \"Describe this image.\"},\n            ],\n        }\n    ]\n\n    # \u51c6\u5907\u63a8\u7406\u8f93\u5165\n    text = processor.apply_chat_template(\n        messages, tokenize=False, add_generation_prompt=True\n    )\n    image_inputs, video_inputs = process_vision_info(messages)\n    inputs = processor(\n        text=[text],\n        images=image_inputs,\n        videos=video_inputs,\n        padding=True,\n        return_tensors=\"pt\",\n    )\n    inputs = inputs.to(\"cuda\")\n\n    # \u63a8\u7406\uff1a\u751f\u6210\u8f93\u51fa\n    generated_ids = model.generate(**inputs, max_new_tokens=128)\n    generated_ids_trimmed = [\n        out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)\n    ]\n    output_text = processor.batch_decode(\n        generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False\n    )\n    output_text = output_text[0]  # \u83b7\u53d6\u8f93\u51fa\u7684\u6587\u672c\n\n    # \u4fdd\u5b58\u8f93\u51fa\u5230\u4e0e\u56fe\u50cf\u540c\u540d\u7684txt\u6587\u4ef6\u4e2d\n    txt_filename = os.path.splitext(filename)[0] + \".txt\"\n    txt_filepath = os.path.join(image_folder_path, txt_filename)\n    with open(txt_filepath, \"w\") as txt_file:\n        txt_file.write(output_text)\n<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f Qwen-VL \u6a21\u578b\u7684\u6700\u65b0\u7248\u672c\uff0c\u4ee3\u8868\u4e86\u8fd1\u4e00\u5e74\u7684\u521b\u65b0\u3002 \u4e3b\u8981\u589e\u5f3a\u529f\u80fd\uff1a \u4e3b\u8981\u6709\u4e24\u4e2a\u6a21\u578b\uff1a Qwen2-VL [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[43],"tags":[545],"class_list":["post-4683","post","type-post","status-publish","format-standard","hentry","category-infoarticle","tag-qwen2-vl"],"views":2971,"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4683"}],"version-history":[{"count":11,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4683\/revisions"}],"predecessor-version":[{"id":4695,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/4683\/revisions\/4695"}],"wp:attachment":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}