Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"添加视觉处理组件","local":"add-vision-processing-components","sections":[{"title":"图像处理器","local":"image-processors","sections":[{"title":"torchvision","local":"torchvision","sections":[],"depth":3},{"title":"PIL","local":"pil","sections":[],"depth":3},{"title":"添加后处理方法","local":"add-post-processing-methods","sections":[],"depth":3}],"depth":2},{"title":"视频处理器","local":"video-processor","sections":[],"depth":2},{"title":"注册类","local":"register-the-classes","sections":[],"depth":2},{"title":"测试","local":"testing","sections":[{"title":"图像处理器测试","local":"image-processor-tests","sections":[],"depth":3},{"title":"视频处理器测试","local":"video-processor-tests","sections":[],"depth":3}],"depth":2},{"title":"后续步骤","local":"next-steps","sections":[],"depth":2}],"depth":1}"/> | |
| <link href="/docs/transformers/main/en/_app/immutable/entry/start.DYIO-UII.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DSYYoqAA.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/BovLmb3z.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/entry/app.D2_KizbC.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/WyYpyc-L.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DYBfcveH.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DOLBYqcr.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/nodes/0.ByO_lNbj.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/ZtBhN-Ei.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/nodes/6.B05ZmGdC.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/D-dvW--N.js" rel="modulepreload"> | |
| <!--1o4t0vs--><meta name="hf:doc:metadata" content="{"title":"Add vision processing components","local":"add-vision-processing-components","sections":[{"title":"Image processors","local":"image-processors","sections":[{"title":"torchvision","local":"torchvision","sections":[],"depth":3},{"title":"PIL","local":"pil","sections":[],"depth":3},{"title":"Add post-processing methods","local":"add-post-processing-methods","sections":[],"depth":3}],"depth":2},{"title":"Video processor","local":"video-processor","sections":[],"depth":2},{"title":"Register the classes","local":"register-the-classes","sections":[],"depth":2},{"title":"Testing","local":"testing","sections":[{"title":"Image processor tests","local":"image-processor-tests","sections":[],"depth":3},{"title":"Video processor tests","local":"video-processor-tests","sections":[],"depth":3}],"depth":2},{"title":"Next steps","local":"next-steps","sections":[],"depth":2}],"depth":1}"/><!----> | |
| <link href="/docs/transformers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="add-vision-processing-components" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#add-vision-processing-components"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->添加视觉处理组件<!----></span></h1><!--]--><!----> <p>添加视觉模型需要在标准的<a href="./modular_transformers">模块化</a>方法之上增加图像或视频处理组件。仅处理图像的模型需要图像处理器,而视频模型则需要视频处理器,两者均可通过<a href="/docs/transformers/main/zh/model_doc/auto#transformers.AutoImageProcessor">AutoImageProcessor</a>和<a href="/docs/transformers/main/zh/model_doc/auto#transformers.AutoVideoProcessor">AutoVideoProcessor</a>入口点访问。</p> <blockquote class="note"><p>对于建模和配置步骤,请先遵循 <a href="./modular_transformers">模块化</a> 指南。</p></blockquote> <!--[1--><h2 class="relative group"><a id="image-processors" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#image-processors"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->图像处理器<!----></span></h2><!--]--><!----> <p>当模型需要处理图像时,请创建图像处理器。<a href="https://docs.pytorch.org/vision/stable/index.html" rel="nofollow">torchvision</a> 后端是默认选项,并支持 GPU 加速。如果 torchvision 不可用,则回退到 <a href="https://pillow.readthedocs.io/en/stable/index.html" rel="nofollow">PIL</a>。</p> <p>两个图像处理器类共享相同的预处理逻辑,但使用不同的后端。它们的构造函数签名和默认值必须完全一致。<a href="/docs/transformers/main/zh/model_doc/auto#transformers.AutoImageProcessor.from_pretrained">AutoImageProcessor.from_pretrained()</a> 在加载时选择后端,并在 torchvision 不可用时回退到 PIL。签名不一致会导致同一份已保存的配置在不同环境中表现出不同的行为。</p> <!--[2--><h3 class="relative group"><a id="torchvision" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#torchvision"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->torchvision<!----></span></h3><!--]--><!----> <p>创建 <code>image_processing_<model_name>.py</code>,其中包含一个继承自 <a href="/docs/transformers/main/zh/main_classes/image_processor#transformers.TorchvisionBackend">TorchvisionBackend</a> 的类。如果您的处理器需要标准 [ImagesKwargs] 之外的自定义参数,请定义一个 kwargs 类。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> ...image_processing_backends <span class="hljs-keyword">import</span> TorchvisionBackend | |
| <span class="hljs-keyword">from</span> ...image_utils <span class="hljs-keyword">import</span> OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, PILImageResampling | |
| <span class="hljs-keyword">from</span> ...processing_utils <span class="hljs-keyword">import</span> ImagesKwargs, Unpack | |
| <span class="hljs-keyword">from</span> ...utils <span class="hljs-keyword">import</span> auto_docstring | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessorKwargs</span>(ImagesKwargs, total=<span class="hljs-literal">False</span>): | |
| tile_size: <span class="hljs-built_in">int</span> <span class="hljs-comment"># any model-specific kwargs</span> | |
| <span class="hljs-meta">@auto_docstring</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessor</span>(<span class="hljs-title class_ inherited__">TorchvisionBackend</span>): | |
| resample = PILImageResampling.BICUBIC | |
| image_mean = OPENAI_CLIP_MEAN | |
| image_std = OPENAI_CLIP_STD | |
| size = {<span class="hljs-string">"shortest_edge"</span>: <span class="hljs-number">224</span>} | |
| do_resize = <span class="hljs-literal">True</span> | |
| do_rescale = <span class="hljs-literal">True</span> | |
| do_normalize = <span class="hljs-literal">True</span> | |
| do_convert_rgb = <span class="hljs-literal">True</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, **kwargs: Unpack[MyModelImageProcessorKwargs]</span>): | |
| <span class="hljs-built_in">super</span>().__init__(**kwargs)<!----></pre></div><!----> <blockquote class="tip"><p>请参阅 <a href="/docs/transformers/main/zh/model_doc/llava_onevision#transformers.LlavaOnevisionImageProcessor">LlavaOnevisionImageProcessor</a>。</p></blockquote> <!--[2--><h3 class="relative group"><a id="pil" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#pil"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->PIL<!----></span></h3><!--]--><!----> <p>创建 <code>image_processing_pil_<model_name>.py</code>,其中包含一个继承自 <a href="/docs/transformers/main/zh/main_classes/image_processor#transformers.PilBackend">PilBackend</a> 的类。在此处复制 kwargs 类,而不是从 torchvision 文件中导入,因为当 torchvision 未安装时导入可能会失败。添加一个 <code># Adapted from</code> 注释,以便两者保持同步。对于没有自定义参数的处理器,直接使用 <a href="/docs/transformers/main/zh/main_classes/processors#transformers.ImagesKwargs">ImagesKwargs</a>。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> ...image_processing_backends <span class="hljs-keyword">import</span> PilBackend | |
| <span class="hljs-keyword">from</span> ...image_utils <span class="hljs-keyword">import</span> OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, PILImageResampling | |
| <span class="hljs-keyword">from</span> ...processing_utils <span class="hljs-keyword">import</span> ImagesKwargs, Unpack | |
| <span class="hljs-keyword">from</span> ...utils <span class="hljs-keyword">import</span> auto_docstring | |
| <span class="hljs-comment"># Adapted from transformers.models.my_model.image_processing_my_model.MyModelImageProcessorKwargs</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessorKwargs</span>(ImagesKwargs, total=<span class="hljs-literal">False</span>): | |
| tile_size: <span class="hljs-built_in">int</span> <span class="hljs-comment"># any model-specific kwargs</span> | |
| <span class="hljs-meta">@auto_docstring</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessorPil</span>(<span class="hljs-title class_ inherited__">PilBackend</span>): | |
| resample = PILImageResampling.BICUBIC | |
| image_mean = OPENAI_CLIP_MEAN | |
| image_std = OPENAI_CLIP_STD | |
| size = {<span class="hljs-string">"shortest_edge"</span>: <span class="hljs-number">224</span>} | |
| do_resize = <span class="hljs-literal">True</span> | |
| do_rescale = <span class="hljs-literal">True</span> | |
| do_normalize = <span class="hljs-literal">True</span> | |
| do_convert_rgb = <span class="hljs-literal">True</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, **kwargs: Unpack[MyModelImageProcessorKwargs]</span>): | |
| <span class="hljs-built_in">super</span>().__init__(**kwargs)<!----></pre></div><!----> <blockquote class="tip"><p>请参阅 <a href="/docs/transformers/main/zh/model_doc/llava_onevision#transformers.LlavaOnevisionImageProcessorPil">LlavaOnevisionImageProcessorPil</a>。</p></blockquote> <!--[2--><h3 class="relative group"><a id="add-post-processing-methods" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#add-post-processing-methods"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->添加后处理方法<!----></span></h3><!--]--><!----> <p>直接在处理器类中添加后处理方法。后处理方法会接收模型输出(<code>outputs</code>)以及特定后处理方法所需的任何其他参数。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessor</span>(<span class="hljs-title class_ inherited__">TorchvisionBackend</span>): | |
| ... | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">post_process_my_task</span>(<span class="hljs-params">self, outputs, ...</span>): | |
| ...<!----></pre></div><!----> <p>后处理器返回简单对象列表(<code>list[str]</code> 或 <code>list[torch.Tensor]</code>)或复杂对象列表(<code>list[MyTaskPostProcessorOutput]</code> 或 <code>list[dict]</code>)。后处理器的输出定义在 <code>src/transformers/image_processing_outputs.py</code> 中,并继承自 <a href="/docs/transformers/main/zh/main_classes/feature_extractor#transformers.BatchFeature">BatchFeature</a>。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyTaskPostProcessorOutput</span>(<span class="hljs-title class_ inherited__">BatchFeature</span>): | |
| predictions: torch.Tensor | |
| scores: torch.Tensor<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="video-processor" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#video-processor"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->视频处理器<!----></span></h2><!--]--><!----> <p>当模型处理视频或采样的视频帧时,添加一个视频处理器。</p> <p>在模型目录中创建 <code>video_processing_<model_name>.py</code>。<a href="/docs/transformers/main/zh/main_classes/video_processor#transformers.BaseVideoProcessor">BaseVideoProcessor</a> 继承自 <a href="/docs/transformers/main/zh/main_classes/image_processor#transformers.TorchvisionBackend">TorchvisionBackend</a>,并提供共享的解码、帧采样、调整大小、重新缩放、归一化、保存和加载行为。</p> <p>类属性是默认的预处理值。用户可以在初始化时或调用时对其进行覆盖。在可能的情况下,请使用与 <a href="/docs/transformers/main/zh/main_classes/processors#transformers.VideosKwargs">VideosKwargs</a> 相同的名称,例如 <code>size</code>、<code>crop_size</code>、<code>do_resize</code>、<code>do_sample_frames</code>、<code>num_frames</code> 和 <code>fps</code>。</p> <p>如果您的视频处理器需要超出标准 <a href="/docs/transformers/main/zh/main_classes/processors#transformers.VideosKwargs">VideosKwargs</a> 的自定义参数,请定义一个 kwargs 类。将其设置为 <code>valid_kwargs</code>,并使用它来注解 <code>__init__</code>,以便进行运行时验证并生成自动文档字符串。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> ...processing_utils <span class="hljs-keyword">import</span> Unpack, VideosKwargs | |
| <span class="hljs-keyword">from</span> ...utils <span class="hljs-keyword">import</span> auto_docstring | |
| <span class="hljs-keyword">from</span> ...video_processing_utils <span class="hljs-keyword">import</span> BaseVideoProcessor | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelVideoProcessorKwargs</span>(VideosKwargs, total=<span class="hljs-literal">False</span>): | |
| min_frames: <span class="hljs-built_in">int</span> | |
| max_frames: <span class="hljs-built_in">int</span> | |
| <span class="hljs-meta">@auto_docstring</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelVideoProcessor</span>(<span class="hljs-title class_ inherited__">BaseVideoProcessor</span>): | |
| size = {<span class="hljs-string">"shortest_edge"</span>: <span class="hljs-number">224</span>} | |
| crop_size = {<span class="hljs-string">"height"</span>: <span class="hljs-number">224</span>, <span class="hljs-string">"width"</span>: <span class="hljs-number">224</span>} | |
| do_resize = <span class="hljs-literal">True</span> | |
| do_center_crop = <span class="hljs-literal">True</span> | |
| do_normalize = <span class="hljs-literal">True</span> | |
| do_sample_frames = <span class="hljs-literal">True</span> | |
| num_frames = <span class="hljs-number">16</span> | |
| model_input_names = [<span class="hljs-string">"pixel_values_videos"</span>] | |
| valid_kwargs = MyModelVideoProcessorKwargs | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, **kwargs: Unpack[MyModelVideoProcessorKwargs]</span>): | |
| <span class="hljs-built_in">super</span>().__init__(**kwargs)<!----></pre></div><!----> <p>仅当模型需要基础均匀采样器无法表达的采样规则时,才重写 <a href="/docs/transformers/main/zh/main_classes/video_processor#transformers.BaseVideoProcessor.sample_frames">sample_frames()</a>。例如,某些模型会强制规定帧数的最小值或最大值,或基于模型特定的约束进行采样。</p> <p>如果模型的前向方法期望使用旧版输入名称,请重写 <code>preprocess</code>,并在调用基类实现后重命名该键。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelVideoProcessor</span>(<span class="hljs-title class_ inherited__">BaseVideoProcessor</span>): | |
| model_input_names = [<span class="hljs-string">"pixel_values"</span>] | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess</span>(<span class="hljs-params">self, videos, **kwargs</span>): | |
| batch = <span class="hljs-built_in">super</span>().preprocess(videos, **kwargs) | |
| batch[<span class="hljs-string">"pixel_values"</span>] = batch.pop(<span class="hljs-string">"pixel_values_videos"</span>) | |
| <span class="hljs-keyword">return</span> batch<!----></pre></div><!----> <p>通过在转换脚本中实例化视频处理器并调用 <a href="/docs/transformers/main/zh/main_classes/video_processor#transformers.VideoProcessorMixin.save_pretrained">save_pretrained()</a>,将视频处理器与检查点一起保存。如果 <a href="/docs/transformers/main/zh/main_classes/processors#transformers.ProcessorMixin">ProcessorMixin</a> 封装了视频处理器,则改为调用 <a href="/docs/transformers/main/zh/model_doc/wav2vec2#transformers.Wav2Vec2Processor.save_pretrained">save_pretrained()</a>。请勿手动创建或编辑预处理配置文件。</p> <blockquote class="tip"><p>请参阅 <a href="/docs/transformers/main/zh/model_doc/qwen3_vl#transformers.Qwen3VLVideoProcessor">Qwen3VLVideoProcessor</a>。</p></blockquote> <!--[1--><h2 class="relative group"><a id="register-the-classes" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#register-the-classes"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->注册类<!----></span></h2><!--]--><!----> <p>在文件底部将新类添加到 <code>__all__</code> 中,然后使用以下命令重新生成 <code>__init__.py</code>:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-bash "><!---->python utils/check_inits.py --fix_and_overwrite<!----></pre></div><!----> <p>将新类映射到模型配置,以便 <code>Auto</code> 类能够加载它们。生成的自动映射文件顶部包含一条警告。请勿手动编辑该文件。添加或更新模型配置后,运行:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-bash "><!---->python utils/check_auto.py --fix_and_overwrite<!----></pre></div><!----> <p>生成映射后,请验证模型类型是否出现在 <code>src/transformers/models/auto/auto_mappings.py</code> 中的相关映射里。</p> <ul><li><code>IMAGE_PROCESSOR_MAPPING_NAMES</code> 用于 <a href="/docs/transformers/main/zh/model_doc/auto#transformers.AutoImageProcessor">AutoImageProcessor</a></li> <li><a href="/docs/transformers/main/zh/model_doc/auto#transformers.AutoVideoProcessor">AutoVideoProcessor</a> 的 <code>VIDEO_PROCESSOR_MAPPING_NAMES</code></li></ul> <!--[1--><h2 class="relative group"><a id="testing" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#testing"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->测试<!----></span></h2><!--]--><!----> <p>在模型测试目录中为每个视觉处理组件添加测试。使用共享的测试混入类,并提供模型特定的初始化 kwargs。图像测试通过共享的测试器自动发现处理器后端,而视频测试则显式配置其处理器类和测试夹具。</p> <!--[2--><h3 class="relative group"><a id="image-processor-tests" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#image-processor-tests"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->图像处理器测试<!----></span></h3><!--]--><!----> <p>图像处理器测试位于 <code>tests/models/<model_name>/test_image_processing_<model_name>.py</code>。请定义一个继承自 <code>ImageProcessingTester</code> 的测试器,以及一个继承自 <code>ImageProcessingTestMixin</code> 和 <code>unittest.TestCase</code> 的测试类。</p> <p>图像处理器测试器定义了默认测试属性,例如处理器初始化参数以及用于创建测试随机输入的参数。仅当需要与图像处理器默认值不同时,才覆盖这些参数。例如,我们通常使用较小的图像尺寸进行测试,以保持测试速度。图像处理器类会从 <code>IMAGE_PROCESSOR_MAPPING_NAMES</code> 中自动推断。</p> <p>在测试类上设置 <code>image_processor_tester_class</code>。该混入会创建 <code>self.image_processor_tester</code>,并且已经涵盖了大多数必需的测试方法。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> unittest | |
| <span class="hljs-keyword">from</span> transformers.testing_utils <span class="hljs-keyword">import</span> require_torch, require_vision | |
| <span class="hljs-keyword">from</span> ...test_image_processing_common <span class="hljs-keyword">import</span> ImageProcessingTestMixin, ImageProcessingTester | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessingTester</span>(<span class="hljs-title class_ inherited__">ImageProcessingTester</span>): | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, **kwargs</span>): | |
| <span class="hljs-comment"># Random test inputs kwargs</span> | |
| kwargs.setdefault(<span class="hljs-string">"batch_size"</span>, <span class="hljs-number">2</span>) <span class="hljs-comment"># default: 7 </span> | |
| kwargs.setdefault(<span class="hljs-string">"num_channels"</span>, <span class="hljs-number">4</span>) <span class="hljs-comment"># default: 3</span> | |
| kwargs.setdefault(<span class="hljs-string">"min_resolution"</span>, <span class="hljs-number">20</span>) <span class="hljs-comment"># default: 30</span> | |
| kwargs.setdefault(<span class="hljs-string">"max_resolution"</span>, <span class="hljs-number">100</span>) <span class="hljs-comment"># default: 400</span> | |
| <span class="hljs-comment"># Image processor init kwargs</span> | |
| kwargs.setdefault(<span class="hljs-string">"size"</span>, {<span class="hljs-string">"shortest_edge"</span>: <span class="hljs-number">20</span>}) | |
| kwargs.setdefault(<span class="hljs-string">"crop_size"</span>, {<span class="hljs-string">"height"</span>: <span class="hljs-number">18</span>, <span class="hljs-string">"width"</span>: <span class="hljs-number">18</span>}) | |
| <span class="hljs-built_in">super</span>().__init__(**kwargs) | |
| <span class="hljs-meta">@require_torch</span> | |
| <span class="hljs-meta">@require_vision</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessingTest</span>(ImageProcessingTestMixin, unittest.TestCase): | |
| image_processor_tester_class = MyModelImageProcessingTester | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_inherits_from_mixin</span>(<span class="hljs-params">self</span>): | |
| <span class="hljs-comment"># Always iterate over all image processing classes</span> | |
| <span class="hljs-keyword">for</span> image_processing_class <span class="hljs-keyword">in</span> <span class="hljs-variable language_">self</span>.image_processing_classes.values(): | |
| <span class="hljs-comment"># self.image_processor_dict contains the default processor init kwargs</span> | |
| <span class="hljs-comment"># including overrides from the tester.</span> | |
| image_processor = image_processing_class(**<span class="hljs-variable language_">self</span>.image_processor_dict) | |
| <span class="hljs-variable language_">self</span>.assertIsInstance(image_processor, ImageProcessingMixin)<!----></pre></div><!----> <p>为 Mixin 无法推断的行为添加针对性测试,例如自定义调整大小规则或特定于模型的 kwargs。</p> <p>后处理测试混入类位于 <code>tests/test_image_processing_common.py</code> 中,并叠加在 <code>ImageProcessingTestMixin</code> 之上。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessingTest</span>(ImageProcessingTestMixin, MyTaskPostProcessTestMixin, unittest.TestCase): | |
| image_processor_tester_class = MyModelImageProcessingTester<!----></pre></div><!----> <p>测试会自动验证您的模型是否使用了正确的 mixins。新任务的 mixins 必须添加到 <code>tests/test_image_processing_common.py</code> 中。</p> <!--[2--><h3 class="relative group"><a id="video-processor-tests" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#video-processor-tests"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->视频处理器测试<!----></span></h3><!--]--><!----> <p>视频处理器的测试通常位于 <code>tests/models/<model_name>/test_video_processing_<model_name>.py</code>,并继承自 <code>VideoProcessingTestMixin</code>。请设置 <code>fast_video_processing_class</code>,定义 <code>video_processor_dict</code>,如果模型使用的键不是 <code>pixel_values_videos</code>,则需重写 <code>input_name</code>。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers.testing_utils <span class="hljs-keyword">import</span> require_torch, require_vision | |
| <span class="hljs-keyword">from</span> transformers.utils <span class="hljs-keyword">import</span> is_torchvision_available | |
| <span class="hljs-keyword">from</span> ...test_video_processing_common <span class="hljs-keyword">import</span> VideoProcessingTestMixin | |
| <span class="hljs-meta">@require_torch</span> | |
| <span class="hljs-meta">@require_vision</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelVideoProcessingTest</span>(VideoProcessingTestMixin, unittest.TestCase): | |
| fast_video_processing_class = MyModelVideoProcessor <span class="hljs-keyword">if</span> is_torchvision_available() <span class="hljs-keyword">else</span> <span class="hljs-literal">None</span> | |
| input_name = <span class="hljs-string">"pixel_values_videos"</span> | |
| <span class="hljs-meta"> @property</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">video_processor_dict</span>(<span class="hljs-params">self</span>): | |
| <span class="hljs-keyword">return</span> {<span class="hljs-string">"size"</span>: {<span class="hljs-string">"shortest_edge"</span>: <span class="hljs-number">224</span>}, <span class="hljs-string">"num_frames"</span>: <span class="hljs-number">16</span>}<!----></pre></div><!----> <p>添加针对帧采样、元数据处理、解码后的视频输入、帧列表输入以及输出形状的聚焦视频测试。如果你的处理器重命名了 <code>pixel_values_videos</code>,请断言返回的是重命名后的键。</p> <p>如果模型还包含一个封装图像或视频处理器的 <a href="/docs/transformers/main/zh/main_classes/processors#transformers.ProcessorMixin">ProcessorMixin</a>,请添加 <code>tests/models/<model_name>/test_processing_<model_name>.py</code> 并继承自 <code>ProcessorTesterMixin</code>。设置 <code>processor_class</code>,并为那些无法在不提供参数的情况下构建的组件重写 <code>_setup_<component>()</code> 类方法。使用 <code>_setup_test_attributes()</code> 来暴露通用处理器测试所使用的占位符标记。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> ...test_processing_common <span class="hljs-keyword">import</span> ProcessorTesterMixin | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelProcessorTest</span>(ProcessorTesterMixin, unittest.TestCase): | |
| processor_class = MyModelProcessor | |
| <span class="hljs-meta"> @classmethod</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">_setup_image_processor</span>(<span class="hljs-params">cls</span>): | |
| <span class="hljs-keyword">return</span> cls._get_component_class_from_processor(<span class="hljs-string">"image_processor"</span>)(size={<span class="hljs-string">"shortest_edge"</span>: <span class="hljs-number">224</span>}) | |
| <span class="hljs-meta"> @classmethod</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">_setup_video_processor</span>(<span class="hljs-params">cls</span>): | |
| <span class="hljs-keyword">return</span> cls._get_component_class_from_processor(<span class="hljs-string">"video_processor"</span>)(num_frames=<span class="hljs-number">2</span>) | |
| <span class="hljs-meta"> @classmethod</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">_setup_test_attributes</span>(<span class="hljs-params">cls, processor</span>): | |
| cls.image_token = <span class="hljs-built_in">getattr</span>(processor, <span class="hljs-string">"image_token"</span>, <span class="hljs-string">""</span>) | |
| cls.video_token = <span class="hljs-built_in">getattr</span>(processor, <span class="hljs-string">"video_token"</span>, <span class="hljs-string">""</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="next-steps" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#next-steps"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->后续步骤<!----></span></h2><!--]--><!----> <ul><li>阅读<a href="./auto_docstring">自动生成文档字符串</a>指南,使用<code>@auto_docstring</code>自动生成一致的文档字符串。</li> <li>请参阅<a href="./image_processors">图像处理器</a>和<a href="./video_processors">视频处理器</a>指南,了解面向用户的预处理行为。</li></ul> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/en/add_vision_processing_components.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]--> | |
| <script> | |
| { | |
| __sveltekit_1m6yd5j = { | |
| base: "/docs/transformers/main/en", | |
| assets: "/docs/transformers/main/en" | |
| }; | |
| const element = document.currentScript.parentElement; | |
| Promise.all([ | |
| import("/docs/transformers/main/en/_app/immutable/entry/start.DYIO-UII.js"), | |
| import("/docs/transformers/main/en/_app/immutable/entry/app.D2_KizbC.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 6], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 58 kB
- Xet hash:
- b5784116a3b37dc445104abd3f36abe3d7528d2e495dad1ad3ebbca5b06b7545
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.