Buckets:

hf-doc-build/doc / transformers /main /vi /add_vision_processing_components.html
mishig's picture
download
raw
60.5 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Thêm các thành phần xử lý thị giác&quot;,&quot;local&quot;:&quot;add-vision-processing-components&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Bộ xử lý hình ảnh&quot;,&quot;local&quot;:&quot;image-processors&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;torchvision&quot;,&quot;local&quot;:&quot;torchvision&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;PIL&quot;,&quot;local&quot;:&quot;pil&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Thêm các phương pháp xử lý hậu kỳ&quot;,&quot;local&quot;:&quot;add-post-processing-methods&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Bộ xử lý video&quot;,&quot;local&quot;:&quot;video-processor&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Đăng ký các lớp&quot;,&quot;local&quot;:&quot;register-the-classes&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Kiểm thử&quot;,&quot;local&quot;:&quot;testing&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Kiểm tra bộ xử lý ảnh&quot;,&quot;local&quot;:&quot;image-processor-tests&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Kiểm tra bộ xử lý video&quot;,&quot;local&quot;:&quot;video-processor-tests&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Các bước tiếp theo&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;: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="{&quot;title&quot;:&quot;Add vision processing components&quot;,&quot;local&quot;:&quot;add-vision-processing-components&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image processors&quot;,&quot;local&quot;:&quot;image-processors&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;torchvision&quot;,&quot;local&quot;:&quot;torchvision&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;PIL&quot;,&quot;local&quot;:&quot;pil&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Add post-processing methods&quot;,&quot;local&quot;:&quot;add-post-processing-methods&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Video processor&quot;,&quot;local&quot;:&quot;video-processor&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Register the classes&quot;,&quot;local&quot;:&quot;register-the-classes&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Testing&quot;,&quot;local&quot;:&quot;testing&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image processor tests&quot;,&quot;local&quot;:&quot;image-processor-tests&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Video processor tests&quot;,&quot;local&quot;:&quot;video-processor-tests&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Next steps&quot;,&quot;local&quot;:&quot;next-steps&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;: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><!---->Thêm các thành phần xử lý thị giác<!----></span></h1><!--]--><!----> <p>Việc thêm một mô hình thị giác đòi hỏi các thành phần xử lý ảnh hoặc video bên cạnh cách tiếp cận <a href="./modular_transformers">modular</a> tiêu chuẩn. Các mô hình chỉ xử lý ảnh cần bộ xử lý ảnh, còn các mô hình video cần bộ xử lý video, cả hai đều có thể truy cập thông qua các điểm vào <a href="/docs/transformers/main/vi/model_doc/auto#transformers.AutoImageProcessor">AutoImageProcessor</a> và <a href="/docs/transformers/main/vi/model_doc/auto#transformers.AutoVideoProcessor">AutoVideoProcessor</a>.</p> <blockquote class="note"><p>Đối với các bước mô hình hóa và cấu hình, hãy làm theo hướng dẫn <a href="./modular_transformers">modular</a> trước.</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><!---->Bộ xử lý hình ảnh<!----></span></h2><!--]--><!----> <p>Tạo bộ xử lý ảnh khi mô hình tiêu thụ ảnh. Backend <a href="https://docs.pytorch.org/vision/stable/index.html" rel="nofollow">torchvision</a> là mặc định và hỗ trợ tăng tốc GPU. <a href="https://pillow.readthedocs.io/en/stable/index.html" rel="nofollow">PIL</a> là phương án dự phòng khi torchvision không khả dụng.</p> <p>Cả hai lớp image processor đều chia sẻ cùng một logic tiền xử lý nhưng có các backend khác nhau. Chữ ký constructor và các giá trị mặc định của chúng phải giống hệt nhau. <a href="/docs/transformers/main/vi/model_doc/auto#transformers.AutoImageProcessor.from_pretrained">AutoImageProcessor.from_pretrained()</a> sẽ chọn backend tại thời điểm tải và chuyển sang PIL khi torchvision không khả dụng. Việc chữ ký không khớp sẽ khiến cùng một config đã lưu có hành vi khác nhau giữa các môi trường.</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>Tạo <code>image_processing_&lt;model_name>.py</code> với một class kế thừa từ <a href="/docs/transformers/main/vi/main_classes/image_processor#transformers.TorchvisionBackend">TorchvisionBackend</a>. Nếu processor của bạn cần các tham số tùy chỉnh ngoài [ImagesKwargs] chuẩn, hãy định nghĩa một class 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> Đã sao chép</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">&quot;shortest_edge&quot;</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>Xem <a href="/docs/transformers/main/vi/model_doc/llava_onevision#transformers.LlavaOnevisionImageProcessor">LlavaOnevisionImageProcessor</a> để tham khảo.</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>Tạo <code>image_processing_pil_&lt;model_name>.py</code> với một class kế thừa từ <a href="/docs/transformers/main/vi/main_classes/image_processor#transformers.PilBackend">PilBackend</a>. Sao chép class kwargs tại đây thay vì import từ file torchvision vì nó có thể thất bại khi torchvision chưa được cài đặt. Thêm chú thích <code># Adapted from</code> để hai file đồng bộ với nhau. Đối với các processor không có tham số tùy chỉnh, hãy dùng trực tiếp <a href="/docs/transformers/main/vi/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> Đã sao chép</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">&quot;shortest_edge&quot;</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>Xem <a href="/docs/transformers/main/vi/model_doc/llava_onevision#transformers.LlavaOnevisionImageProcessorPil">LlavaOnevisionImageProcessorPil</a> để tham khảo.</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><!---->Thêm các phương pháp xử lý hậu kỳ<!----></span></h3><!--]--><!----> <p>Thêm các phương pháp xử lý hậu kỳ trực tiếp vào lớp processor. Các phương pháp xử lý hậu kỳ được gọi với đầu ra của mô hình (<code>outputs</code>) và bất kỳ đối số bổ sung nào cần thiết cho phương pháp xử lý hậu kỳ cụ thể.</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> Đã sao chép</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>Post-processor trả về một danh sách các đối tượng đơn giản (<code>list[str]</code> hoặc <code>list[torch.Tensor]</code>) hoặc một danh sách các đối tượng phức tạp (<code>list[MyTaskPostProcessorOutput]</code> hoặc <code>list[dict]</code>). Đầu ra của post-processor được định nghĩa trong <code>src/transformers/image_processing_outputs.py</code> và kế thừa từ <a href="/docs/transformers/main/vi/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> Đã sao chép</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><!---->Bộ xử lý video<!----></span></h2><!--]--><!----> <p>Thêm một video processor khi mô hình sử dụng video hoặc các khung hình video được lấy mẫu.</p> <p>Tạo <code>video_processing_&lt;model_name>.py</code> trong thư mục model. <a href="/docs/transformers/main/vi/main_classes/video_processor#transformers.BaseVideoProcessor">BaseVideoProcessor</a> kế thừa từ <a href="/docs/transformers/main/vi/main_classes/image_processor#transformers.TorchvisionBackend">TorchvisionBackend</a> và cung cấp các hành vi chung cho việc giải mã, lấy mẫu khung hình, thay đổi kích thước, thay đổi tỷ lệ, chuẩn hóa, lưu và tải.</p> <p>Các thuộc tính của lớp là các giá trị tiền xử lý mặc định. Người dùng có thể ghi đè chúng khi khởi tạo hoặc khi gọi. Hãy sử dụng cùng tên với <a href="/docs/transformers/main/vi/main_classes/processors#transformers.VideosKwargs">VideosKwargs</a> khi có thể, chẳng hạn như <code>size</code>, <code>crop_size</code>, <code>do_resize</code>, <code>do_sample_frames</code>, <code>num_frames</code> và <code>fps</code>.</p> <p>Định nghĩa một lớp kwargs nếu trình xử lý video của bạn cần các tham số tùy chỉnh ngoài <a href="/docs/transformers/main/vi/main_classes/processors#transformers.VideosKwargs">VideosKwargs</a> tiêu chuẩn. Đặt nó làm <code>valid_kwargs</code> và sử dụng nó để chú thích <code>__init__</code> cho cả xác thực thời gian chạy và docstring được tạo tự động.</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> Đã sao chép</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">&quot;shortest_edge&quot;</span>: <span class="hljs-number">224</span>}
crop_size = {<span class="hljs-string">&quot;height&quot;</span>: <span class="hljs-number">224</span>, <span class="hljs-string">&quot;width&quot;</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">&quot;pixel_values_videos&quot;</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>Chỉ ghi đè <a href="/docs/transformers/main/vi/main_classes/video_processor#transformers.BaseVideoProcessor.sample_frames">sample_frames()</a> khi mô hình yêu cầu một quy tắc lấy mẫu mà bộ lấy mẫu đều cơ sở không thể biểu diễn. Ví dụ, một số mô hình áp đặt số khung hình tối thiểu hoặc tối thiểu, hoặc lấy mẫu dựa trên các ràng buộc cụ thể của mô hình.</p> <p>Nếu phương thức forward của model kỳ vọng một tên input cũ, hãy ghi đè <code>preprocess</code> và đổi tên key sau khi gọi implementation gốc.</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> Đã sao chép</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">&quot;pixel_values&quot;</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">&quot;pixel_values&quot;</span>] = batch.pop(<span class="hljs-string">&quot;pixel_values_videos&quot;</span>)
<span class="hljs-keyword">return</span> batch<!----></pre></div><!----> <p>Lưu video processor cùng với checkpoint bằng cách khởi tạo nó trong script chuyển đổi và gọi <a href="/docs/transformers/main/vi/main_classes/video_processor#transformers.VideoProcessorMixin.save_pretrained">save_pretrained()</a>. Nếu <a href="/docs/transformers/main/vi/main_classes/processors#transformers.ProcessorMixin">ProcessorMixin</a> bao bọc video processor, hãy gọi <a href="/docs/transformers/main/vi/model_doc/wav2vec2#transformers.Wav2Vec2Processor.save_pretrained">save_pretrained()</a> thay thế. Không tự động tạo hoặc chỉnh sửa các tệp cấu hình tiền xử lý.</p> <blockquote class="tip"><p>Xem <a href="/docs/transformers/main/vi/model_doc/qwen3_vl#transformers.Qwen3VLVideoProcessor">Qwen3VLVideoProcessor</a> để tham khảo.</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><!---->Đăng ký các lớp<!----></span></h2><!--]--><!----> <p>Thêm các lớp mới vào <code>__all__</code> ở cuối tệp, sau đó tạo lại <code>__init__.py</code> bằng:</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-bash "><!---->python utils/check_inits.py --fix_and_overwrite<!----></pre></div><!----> <p>Ánh xạ các lớp mới vào cấu hình mô hình để các lớp <code>Auto</code> có thể tải chúng. Tệp ánh xạ auto được tạo có một cảnh báo ở đầu. Không chỉnh sửa thủ công. Thêm hoặc cập nhật cấu hình mô hình, sau đó chạy:</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-bash "><!---->python utils/check_auto.py --fix_and_overwrite<!----></pre></div><!----> <p>Sau khi ánh xạ được tạo, hãy kiểm tra xem loại mô hình có xuất hiện trong các ánh xạ liên quan trong <code>src/transformers/models/auto/auto_mappings.py</code> hay không.</p> <ul><li><code>IMAGE_PROCESSOR_MAPPING_NAMES</code> cho <a href="/docs/transformers/main/vi/model_doc/auto#transformers.AutoImageProcessor">AutoImageProcessor</a></li> <li><code>VIDEO_PROCESSOR_MAPPING_NAMES</code> cho <a href="/docs/transformers/main/vi/model_doc/auto#transformers.AutoVideoProcessor">AutoVideoProcessor</a></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><!---->Kiểm thử<!----></span></h2><!--]--><!----> <p>Thêm các bài kiểm tra cho từng thành phần xử lý thị giác trong thư mục kiểm tra mô hình. Sử dụng các mixin kiểm tra dùng chung và cung cấp các tham số khởi tạo cụ thể cho mô hình. Các bài kiểm tra ảnh tự động phát hiện các backend của processor thông qua một tester dùng chung, trong khi các bài kiểm tra video cấu hình lớp processor và các fixture một cách tường minh.</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><!---->Kiểm tra bộ xử lý ảnh<!----></span></h3><!--]--><!----> <p>Các bài kiểm tra bộ xử lý ảnh nằm trong <code>tests/models/&lt;model_name>/test_image_processing_&lt;model_name>.py</code>. Hãy định nghĩa một tester kế thừa từ <code>ImageProcessingTester</code> và một lớp kiểm thử kế thừa từ <code>ImageProcessingTestMixin</code> và <code>unittest.TestCase</code>.</p> <p>Trình kiểm tra xử lý ảnh định nghĩa các thuộc tính kiểm thử mặc định, chẳng hạn như các đối số khởi tạo bộ xử lý và các tham số để tạo đầu vào ngẫu nhiên cho các bài kiểm tra. Chỉ ghi đè các đối số nếu chúng cần khác với giá trị mặc định của bộ xử lý ảnh. Ví dụ, chúng ta thường kiểm thử với kích thước ảnh nhỏ hơn để giữ cho các bài kiểm tra chạy nhanh. Các lớp bộ xử lý ảnh được suy ra tự động từ <code>IMAGE_PROCESSOR_MAPPING_NAMES</code>.</p> <p>Đặt <code>image_processor_tester_class</code> trên lớp kiểm thử. Mixin tạo ra <code>self.image_processor_tester</code> và đã bao phủ hầu hết các phương thức kiểm thử cần thiết.</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> Đã sao chép</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">&quot;batch_size&quot;</span>, <span class="hljs-number">2</span>) <span class="hljs-comment"># default: 7 </span>
kwargs.setdefault(<span class="hljs-string">&quot;num_channels&quot;</span>, <span class="hljs-number">4</span>) <span class="hljs-comment"># default: 3</span>
kwargs.setdefault(<span class="hljs-string">&quot;min_resolution&quot;</span>, <span class="hljs-number">20</span>) <span class="hljs-comment"># default: 30</span>
kwargs.setdefault(<span class="hljs-string">&quot;max_resolution&quot;</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">&quot;size&quot;</span>, {<span class="hljs-string">&quot;shortest_edge&quot;</span>: <span class="hljs-number">20</span>})
kwargs.setdefault(<span class="hljs-string">&quot;crop_size&quot;</span>, {<span class="hljs-string">&quot;height&quot;</span>: <span class="hljs-number">18</span>, <span class="hljs-string">&quot;width&quot;</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>Thêm các bài kiểm tra tập trung cho hành vi mà mixin không thể suy luận, chẳng hạn như các quy tắc thay đổi kích thước tùy chỉnh hoặc các tham số kwargs cụ thể cho từng model.</p> <p>Các mixin kiểm tra hậu xử lý có sẵn trong <code>tests/test_image_processing_common.py</code> và được thêm vào bên trên <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> Đã sao chép</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>Các bài kiểm tra tự động xác minh rằng các mixin đúng được sử dụng cho mô hình của bạn. Các mixin cho các tác vụ mới phải được thêm vào <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><!---->Kiểm tra bộ xử lý video<!----></span></h3><!--]--><!----> <p>Các bài kiểm tra video processor thường nằm trong <code>tests/models/&lt;model_name>/test_video_processing_&lt;model_name>.py</code> và kế thừa từ <code>VideoProcessingTestMixin</code>. Hãy đặt <code>fast_video_processing_class</code>, định nghĩa <code>video_processor_dict</code>, và ghi đè <code>input_name</code> nếu model sử dụng một key khác với <code>pixel_values_videos</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> Đã sao chép</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">&quot;pixel_values_videos&quot;</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">&quot;size&quot;</span>: {<span class="hljs-string">&quot;shortest_edge&quot;</span>: <span class="hljs-number">224</span>}, <span class="hljs-string">&quot;num_frames&quot;</span>: <span class="hljs-number">16</span>}<!----></pre></div><!----> <p>Thêm các bài kiểm tra video tập trung cho việc lấy mẫu khung hình, xử lý metadata, đầu vào video đã giải mã, đầu vào dạng danh sách khung hình và hình dạng đầu ra. Nếu bộ xử lý của bạn đổi tên <code>pixel_values_videos</code>, hãy khẳng định rằng khóa đã đổi tên được trả về.</p> <p>Nếu model cũng có <a href="/docs/transformers/main/vi/main_classes/processors#transformers.ProcessorMixin">ProcessorMixin</a> bao bọc image hoặc video processor, hãy thêm <code>tests/models/&lt;model_name>/test_processing_&lt;model_name>.py</code> và kế thừa từ <code>ProcessorTesterMixin</code>. Đặt <code>processor_class</code> và ghi đè các class method <code>_setup_&lt;component>()</code> cho các component không thể được khởi tạo mà không có đối số. Sử dụng <code>_setup_test_attributes()</code> để phơi bày các placeholder token được dùng bởi các processor tests chung.</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> Đã sao chép</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">&quot;image_processor&quot;</span>)(size={<span class="hljs-string">&quot;shortest_edge&quot;</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">&quot;video_processor&quot;</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">&quot;image_token&quot;</span>, <span class="hljs-string">&quot;&quot;</span>)
cls.video_token = <span class="hljs-built_in">getattr</span>(processor, <span class="hljs-string">&quot;video_token&quot;</span>, <span class="hljs-string">&quot;&quot;</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><!---->Các bước tiếp theo<!----></span></h2><!--]--><!----> <ul><li>Đọc hướng dẫn <a href="./auto_docstring">Tự động tạo docstring</a> để tự động tạo các docstring nhất quán với <code>@auto_docstring</code>.</li> <li>Đọc các hướng dẫn <a href="./image_processors">Image processors</a> và <a href="./video_processors">Video processors</a> để tìm hiểu về hành vi tiền xử lý dành cho người dùng.</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:
60.5 kB
·
Xet hash:
5d8ed9e5e8161e5a847cb8583b7fb42baf2905b79638f04bb75ee38cd5353eca

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.