Buckets:

hf-doc-build/doc / transformers /main /id /add_vision_processing_components.html
mishig's picture
download
raw
58.8 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Tambahkan komponen pemrosesan visi&quot;,&quot;local&quot;:&quot;add-vision-processing-components&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Pemroses gambar&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;Tambahkan metode pasca-pemrosesan&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;Pemroses video&quot;,&quot;local&quot;:&quot;video-processor&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Daftarkan kelas&quot;,&quot;local&quot;:&quot;register-the-classes&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Pengujian&quot;,&quot;local&quot;:&quot;testing&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Uji pemroses gambar&quot;,&quot;local&quot;:&quot;image-processor-tests&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Uji pemroses 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;Langkah selanjutnya&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><!---->Tambahkan komponen pemrosesan visi<!----></span></h1><!--]--><!----> <p>Menambahkan model visi memerlukan komponen pemrosesan gambar atau video di atas pendekatan <a href="./modular_transformers">modular</a> standar. Model yang hanya menggunakan gambar memerlukan image processor, sedangkan model video memerlukan video processor, keduanya dapat diakses melalui titik masuk <a href="/docs/transformers/main/id/model_doc/auto#transformers.AutoImageProcessor">AutoImageProcessor</a> dan <a href="/docs/transformers/main/id/model_doc/auto#transformers.AutoVideoProcessor">AutoVideoProcessor</a>.</p> <blockquote class="note"><p>Untuk langkah pemodelan dan konfigurasi, ikuti panduan <a href="./modular_transformers">modular</a> terlebih dahulu.</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><!---->Pemroses gambar<!----></span></h2><!--]--><!----> <p>Buat image processor ketika model mengonsumsi gambar. Backend <a href="https://docs.pytorch.org/vision/stable/index.html" rel="nofollow">torchvision</a> adalah default dan mendukung akselerasi GPU. <a href="https://pillow.readthedocs.io/en/stable/index.html" rel="nofollow">PIL</a> adalah fallback ketika torchvision tidak tersedia.</p> <p>Kedua kelas image processor berbagi logika pra-pemrosesan yang sama tetapi memiliki backend yang berbeda. Tanda tangan konstruktor dan nilai default mereka harus identik. <a href="/docs/transformers/main/id/model_doc/auto#transformers.AutoImageProcessor.from_pretrained">AutoImageProcessor.from_pretrained()</a> memilih backend saat pemuatan dan beralih ke PIL ketika torchvision tidak tersedia. Tanda tangan yang tidak cocok menyebabkan konfigurasi yang disimpan berperilaku berbeda di berbagai lingkungan.</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>Buat <code>image_processing_&lt;model_name>.py</code> dengan kelas yang mewarisi dari <a href="/docs/transformers/main/id/main_classes/image_processor#transformers.TorchvisionBackend">TorchvisionBackend</a>. Jika prosesor Anda membutuhkan parameter kustom di luar [ImagesKwargs] standar, definisikan kelas 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> Disalin</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>Lihat <a href="/docs/transformers/main/id/model_doc/llava_onevision#transformers.LlavaOnevisionImageProcessor">LlavaOnevisionImageProcessor</a> untuk referensi.</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>Buat <code>image_processing_pil_&lt;model_name>.py</code> dengan kelas yang mewarisi dari <a href="/docs/transformers/main/id/main_classes/image_processor#transformers.PilBackend">PilBackend</a>. Duplikat kelas kwargs di sini alih-alih mengimpor dari file torchvision karena dapat gagal saat torchvision tidak terpasang. Tambahkan komentar <code># Adapted from</code> agar keduanya tetap sinkron. Untuk prosesor tanpa parameter kustom, gunakan <a href="/docs/transformers/main/id/main_classes/processors#transformers.ImagesKwargs">ImagesKwargs</a> secara langsung.</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> Disalin</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>Lihat <a href="/docs/transformers/main/id/model_doc/llava_onevision#transformers.LlavaOnevisionImageProcessorPil">LlavaOnevisionImageProcessorPil</a> untuk referensi.</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><!---->Tambahkan metode pasca-pemrosesan<!----></span></h3><!--]--><!----> <p>Tambahkan metode pascaproses langsung ke kelas prosesor. Metode pascaproses dipanggil dengan keluaran model (<code>outputs</code>) dan argumen tambahan apa pun yang diperlukan untuk metode pascaproses tertentu.</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> Disalin</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 mengembalikan daftar objek sederhana (<code>list[str]</code> atau <code>list[torch.Tensor]</code>) atau daftar objek kompleks (<code>list[MyTaskPostProcessorOutput]</code> atau <code>list[dict]</code>). Output post-processor didefinisikan di <code>src/transformers/image_processing_outputs.py</code> dan mewarisi dari <a href="/docs/transformers/main/id/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> Disalin</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><!---->Pemroses video<!----></span></h2><!--]--><!----> <p>Tambahkan video processor ketika model mengonsumsi video atau bingkai video yang disampelkan.</p> <p>Buat <code>video_processing_&lt;model_name>.py</code> di direktori model. <a href="/docs/transformers/main/id/main_classes/video_processor#transformers.BaseVideoProcessor">BaseVideoProcessor</a> mewarisi dari <a href="/docs/transformers/main/id/main_classes/image_processor#transformers.TorchvisionBackend">TorchvisionBackend</a> dan menyediakan perilaku bersama untuk decoding, pengambilan sampel frame, pengubahan ukuran, penskalaan ulang, normalisasi, penyimpanan, dan pemuatan.</p> <p>Atribut kelas adalah nilai pra-pemrosesan default. Pengguna dapat menimpanya saat inisialisasi atau saat pemanggilan. Gunakan nama yang sama dengan <a href="/docs/transformers/main/id/main_classes/processors#transformers.VideosKwargs">VideosKwargs</a> jika memungkinkan, seperti <code>size</code>, <code>crop_size</code>, <code>do_resize</code>, <code>do_sample_frames</code>, <code>num_frames</code>, dan <code>fps</code>.</p> <p>Definisikan kelas kwargs jika video processor Anda memerlukan parameter kustom di luar <a href="/docs/transformers/main/id/main_classes/processors#transformers.VideosKwargs">VideosKwargs</a> standar. Atur sebagai <code>valid_kwargs</code> dan gunakan untuk mengannotasi <code>__init__</code> untuk validasi runtime dan docstring yang dibuat otomatis.</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> Disalin</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>Override <a href="/docs/transformers/main/id/main_classes/video_processor#transformers.BaseVideoProcessor.sample_frames">sample_frames()</a> hanya ketika model memerlukan aturan sampling yang tidak dapat diekspresikan oleh sampler uniform dasar. Sebagai contoh, beberapa model menerapkan jumlah minimum atau maksimum frame, atau melakukan sampling berdasarkan kendala yang spesifik pada model.</p> <p>Jika metode forward model mengharapkan nama input lama, timpa <code>preprocess</code> dan ubah nama kunci setelah memanggil implementasi dasar.</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> Disalin</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>Simpan video processor bersama checkpoint dengan menginstansiasikannya di skrip konversi dan memanggil <a href="/docs/transformers/main/id/main_classes/video_processor#transformers.VideoProcessorMixin.save_pretrained">save_pretrained()</a>. Jika <a href="/docs/transformers/main/id/main_classes/processors#transformers.ProcessorMixin">ProcessorMixin</a> membungkus video processor, panggil <a href="/docs/transformers/main/id/model_doc/wav2vec2#transformers.Wav2Vec2Processor.save_pretrained">save_pretrained()</a> sebagai gantinya. Jangan membuat atau mengedit file konfigurasi pra-pemrosesan secara manual.</p> <blockquote class="tip"><p>Lihat <a href="/docs/transformers/main/id/model_doc/qwen3_vl#transformers.Qwen3VLVideoProcessor">Qwen3VLVideoProcessor</a> untuk referensi.</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><!---->Daftarkan kelas<!----></span></h2><!--]--><!----> <p>Tambahkan kelas baru ke <code>__all__</code> di bagian bawah file, lalu buat ulang <code>__init__.py</code> dengan:</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> Disalin</div><!----></button><!----></div> <pre class="language-bash "><!---->python utils/check_inits.py --fix_and_overwrite<!----></pre></div><!----> <p>Petakan kelas baru ke konfigurasi model agar kelas <code>Auto</code> dapat memuatnya. File pemetaan auto yang dihasilkan memiliki peringatan di bagian atas. Jangan mengeditnya secara manual. Tambahkan atau perbarui konfigurasi model, lalu jalankan:</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> Disalin</div><!----></button><!----></div> <pre class="language-bash "><!---->python utils/check_auto.py --fix_and_overwrite<!----></pre></div><!----> <p>Setelah pemetaan dibuat, pastikan tipe model muncul dalam pemetaan yang relevan di <code>src/transformers/models/auto/auto_mappings.py</code>.</p> <ul><li><code>IMAGE_PROCESSOR_MAPPING_NAMES</code> untuk <a href="/docs/transformers/main/id/model_doc/auto#transformers.AutoImageProcessor">AutoImageProcessor</a></li> <li><code>VIDEO_PROCESSOR_MAPPING_NAMES</code> untuk <a href="/docs/transformers/main/id/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><!---->Pengujian<!----></span></h2><!--]--><!----> <p>Tambahkan tes untuk setiap komponen pemrosesan visi di direktori tes model. Gunakan mixin tes bersama dan berikan kwargs inisialisasi yang spesifik untuk model. Tes gambar menemukan backend prosesor secara otomatis melalui tester bersama, sedangkan tes video mengonfigurasi kelas prosesor dan fixture secara eksplisit.</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><!---->Uji pemroses gambar<!----></span></h3><!--]--><!----> <p>Uji pemroses gambar berada di <code>tests/models/&lt;model_name>/test_image_processing_&lt;model_name>.py</code>. Definisikan tester yang mewarisi dari <code>ImageProcessingTester</code> dan kelas uji yang mewarisi dari <code>ImageProcessingTestMixin</code> dan <code>unittest.TestCase</code>.</p> <p>Tester pemrosesan gambar mendefinisikan atribut uji default seperti argumen inisialisasi prosesor dan parameter untuk membuat input acak untuk pengujian. Hanya timpa argumen jika seharusnya berbeda dari default prosesor gambar. Misalnya, kami biasanya menguji dengan ukuran gambar yang lebih kecil agar pengujian tetap cepat. Kelas prosesor gambar disimpulkan secara otomatis dari <code>IMAGE_PROCESSOR_MAPPING_NAMES</code>.</p> <p>Set <code>image_processor_tester_class</code> pada kelas pengujian. Mixin membuat <code>self.image_processor_tester</code> dan sudah mencakup sebagian besar metode pengujian yang diperlukan.</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> Disalin</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>Tambahkan pengujian yang berfokus pada perilaku yang tidak dapat disimpulkan oleh mixin, seperti aturan pengubahan ukuran kustom atau kwargs yang spesifik untuk model.</p> <p>Mixin pengujian pasca-pemrosesan tersedia di <code>tests/test_image_processing_common.py</code> dan ditambahkan di atas <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> Disalin</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>Uji coba secara otomatis memverifikasi bahwa mixin yang benar digunakan untuk model Anda. Mixin untuk tugas baru harus ditambahkan ke <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><!---->Uji pemroses video<!----></span></h3><!--]--><!----> <p>Uji pemroses video biasanya berada di <code>tests/models/&lt;model_name>/test_video_processing_&lt;model_name>.py</code> dan mewarisi dari <code>VideoProcessingTestMixin</code>. Atur <code>fast_video_processing_class</code>, definisikan <code>video_processor_dict</code>, dan timpa <code>input_name</code> jika model menggunakan kunci selain <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> Disalin</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>Tambahkan pengujian video yang berfokus pada pengambilan sampel frame, penanganan metadata, input video yang telah didekode, input berupa daftar frame, dan bentuk output. Jika prosesor Anda mengganti nama <code>pixel_values_videos</code>, pastikan kunci yang telah diganti nama tersebut dikembalikan.</p> <p>Jika model juga memiliki <a href="/docs/transformers/main/id/main_classes/processors#transformers.ProcessorMixin">ProcessorMixin</a> yang membungkus prosesor gambar atau video, tambahkan <code>tests/models/&lt;model_name>/test_processing_&lt;model_name>.py</code> dan warisi dari <code>ProcessorTesterMixin</code>. Atur <code>processor_class</code> dan timpa metode kelas <code>_setup_&lt;component>()</code> untuk komponen yang tidak dapat dikonstruksi tanpa argumen. Gunakan <code>_setup_test_attributes()</code> untuk mengekspos token placeholder yang digunakan oleh tes prosesor umum.</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> Disalin</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><!---->Langkah selanjutnya<!----></span></h2><!--]--><!----> <ul><li>Baca panduan <a href="./auto_docstring">Menghasilkan docstring secara otomatis</a> untuk menghasilkan docstring yang konsisten secara otomatis dengan <code>@auto_docstring</code>.</li> <li>Baca panduan <a href="./image_processors">Image processors</a> dan <a href="./video_processors">Video processors</a> untuk perilaku pra-pemrosesan yang berorientasi pada pengguna.</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.8 kB
·
Xet hash:
fa9f21542d306823ec4188f0b38b8a7739164c5ea24fba38a5b385f6228f1266

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