Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Ajouter des composants de traitement de la vision","local":"add-vision-processing-components","sections":[{"title":"Processeurs d'images","local":"image-processors","sections":[{"title":"torchvision","local":"torchvision","sections":[],"depth":3},{"title":"PIL","local":"pil","sections":[],"depth":3},{"title":"Ajouter des méthodes de post-traitement","local":"add-post-processing-methods","sections":[],"depth":3}],"depth":2},{"title":"Processeur vidéo","local":"video-processor","sections":[],"depth":2},{"title":"Enregistrer les classes","local":"register-the-classes","sections":[],"depth":2},{"title":"Tests","local":"testing","sections":[{"title":"Tests du processeur d'images","local":"image-processor-tests","sections":[],"depth":3},{"title":"Tests du processeur vidéo","local":"video-processor-tests","sections":[],"depth":3}],"depth":2},{"title":"Étapes suivantes","local":"next-steps","sections":[],"depth":2}],"depth":1}"/> | |
| <link href="/docs/transformers/main/en/_app/immutable/entry/start.DYIO-UII.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DSYYoqAA.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/BovLmb3z.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/entry/app.D2_KizbC.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/WyYpyc-L.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DYBfcveH.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DOLBYqcr.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/nodes/0.ByO_lNbj.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/ZtBhN-Ei.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/nodes/6.B05ZmGdC.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/D-dvW--N.js" rel="modulepreload"> | |
| <!--1o4t0vs--><meta name="hf:doc:metadata" content="{"title":"Add vision processing components","local":"add-vision-processing-components","sections":[{"title":"Image processors","local":"image-processors","sections":[{"title":"torchvision","local":"torchvision","sections":[],"depth":3},{"title":"PIL","local":"pil","sections":[],"depth":3},{"title":"Add post-processing methods","local":"add-post-processing-methods","sections":[],"depth":3}],"depth":2},{"title":"Video processor","local":"video-processor","sections":[],"depth":2},{"title":"Register the classes","local":"register-the-classes","sections":[],"depth":2},{"title":"Testing","local":"testing","sections":[{"title":"Image processor tests","local":"image-processor-tests","sections":[],"depth":3},{"title":"Video processor tests","local":"video-processor-tests","sections":[],"depth":3}],"depth":2},{"title":"Next steps","local":"next-steps","sections":[],"depth":2}],"depth":1}"/><!----> | |
| <link href="/docs/transformers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="add-vision-processing-components" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#add-vision-processing-components"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Ajouter des composants de traitement de la vision<!----></span></h1><!--]--><!----> <p>L’ajout d’un modèle de vision nécessite des composants de traitement d’images ou de vidéos en plus de l’approche <a href="./modular_transformers">modulaire</a> standard. Les modèles uniquement pour images ont besoin de processeurs d’images et les modèles pour vidéos ont besoin d’un processeur de vidéos, tous deux accessibles via les points d’entrée <a href="/docs/transformers/main/fr/model_doc/auto#transformers.AutoImageProcessor">AutoImageProcessor</a> et <a href="/docs/transformers/main/fr/model_doc/auto#transformers.AutoVideoProcessor">AutoVideoProcessor</a>.</p> <blockquote class="note"><p>Pour les étapes de modélisation et de configuration, suivez d'abord le guide <a href="./modular_transformers">modulaire</a>.</p></blockquote> <!--[1--><h2 class="relative group"><a id="image-processors" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#image-processors"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Processeurs d'images<!----></span></h2><!--]--><!----> <p>Créez des processeurs d’images lorsque le modèle consomme des images. L’arrière-plan <a href="https://docs.pytorch.org/vision/stable/index.html" rel="nofollow">torchvision</a> est celui par défaut et prend en charge l’accélération GPU. <a href="https://pillow.readthedocs.io/en/stable/index.html" rel="nofollow">PIL</a> est utilisé en secours lorsque torchvision n’est pas disponible.</p> <p>Les deux classes de processeur d’image partagent la même logique de prétraitement mais utilisent des backends différents. Leurs signatures de constructeur et leurs valeurs par défaut doivent être identiques. <a href="/docs/transformers/main/fr/model_doc/auto#transformers.AutoImageProcessor.from_pretrained">AutoImageProcessor.from_pretrained()</a> sélectionne le backend au moment du chargement et bascule sur PIL si torchvision n’est pas disponible. Des signatures non conformes font en sorte qu’une même configuration enregistrée se comporte différemment selon les environnements.</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>Créez <code>image_processing_<model_name>.py</code> avec une classe qui hérite de <a href="/docs/transformers/main/fr/main_classes/image_processor#transformers.TorchvisionBackend">TorchvisionBackend</a>. Si votre processeur a besoin de paramètres personnalisés en plus des [ImagesKwargs] standard, définissez une classe 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> Copié</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> ...image_processing_backends <span class="hljs-keyword">import</span> TorchvisionBackend | |
| <span class="hljs-keyword">from</span> ...image_utils <span class="hljs-keyword">import</span> OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, PILImageResampling | |
| <span class="hljs-keyword">from</span> ...processing_utils <span class="hljs-keyword">import</span> ImagesKwargs, Unpack | |
| <span class="hljs-keyword">from</span> ...utils <span class="hljs-keyword">import</span> auto_docstring | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessorKwargs</span>(ImagesKwargs, total=<span class="hljs-literal">False</span>): | |
| tile_size: <span class="hljs-built_in">int</span> <span class="hljs-comment"># any model-specific kwargs</span> | |
| <span class="hljs-meta">@auto_docstring</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessor</span>(<span class="hljs-title class_ inherited__">TorchvisionBackend</span>): | |
| resample = PILImageResampling.BICUBIC | |
| image_mean = OPENAI_CLIP_MEAN | |
| image_std = OPENAI_CLIP_STD | |
| size = {<span class="hljs-string">"shortest_edge"</span>: <span class="hljs-number">224</span>} | |
| do_resize = <span class="hljs-literal">True</span> | |
| do_rescale = <span class="hljs-literal">True</span> | |
| do_normalize = <span class="hljs-literal">True</span> | |
| do_convert_rgb = <span class="hljs-literal">True</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, **kwargs: Unpack[MyModelImageProcessorKwargs]</span>): | |
| <span class="hljs-built_in">super</span>().__init__(**kwargs)<!----></pre></div><!----> <blockquote class="tip"><p>Voir <a href="/docs/transformers/main/fr/model_doc/llava_onevision#transformers.LlavaOnevisionImageProcessor">LlavaOnevisionImageProcessor</a> pour référence.</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>Créez <code>image_processing_pil_<model_name>.py</code> avec une classe qui hérite de <a href="/docs/transformers/main/fr/main_classes/image_processor#transformers.PilBackend">PilBackend</a>. Dupliquez la classe kwargs ici au lieu de l’importer depuis le fichier torchvision, car cela peut échouer si torchvision n’est pas installé. Ajoutez un commentaire <code># Adapted from</code> pour que les deux restent synchronisés. Pour les processeurs sans paramètres personnalisés, utilisez directement <a href="/docs/transformers/main/fr/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> Copié</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> ...image_processing_backends <span class="hljs-keyword">import</span> PilBackend | |
| <span class="hljs-keyword">from</span> ...image_utils <span class="hljs-keyword">import</span> OPENAI_CLIP_MEAN, OPENAI_CLIP_STD, PILImageResampling | |
| <span class="hljs-keyword">from</span> ...processing_utils <span class="hljs-keyword">import</span> ImagesKwargs, Unpack | |
| <span class="hljs-keyword">from</span> ...utils <span class="hljs-keyword">import</span> auto_docstring | |
| <span class="hljs-comment"># Adapted from transformers.models.my_model.image_processing_my_model.MyModelImageProcessorKwargs</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessorKwargs</span>(ImagesKwargs, total=<span class="hljs-literal">False</span>): | |
| tile_size: <span class="hljs-built_in">int</span> <span class="hljs-comment"># any model-specific kwargs</span> | |
| <span class="hljs-meta">@auto_docstring</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessorPil</span>(<span class="hljs-title class_ inherited__">PilBackend</span>): | |
| resample = PILImageResampling.BICUBIC | |
| image_mean = OPENAI_CLIP_MEAN | |
| image_std = OPENAI_CLIP_STD | |
| size = {<span class="hljs-string">"shortest_edge"</span>: <span class="hljs-number">224</span>} | |
| do_resize = <span class="hljs-literal">True</span> | |
| do_rescale = <span class="hljs-literal">True</span> | |
| do_normalize = <span class="hljs-literal">True</span> | |
| do_convert_rgb = <span class="hljs-literal">True</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, **kwargs: Unpack[MyModelImageProcessorKwargs]</span>): | |
| <span class="hljs-built_in">super</span>().__init__(**kwargs)<!----></pre></div><!----> <blockquote class="tip"><p>Voir <a href="/docs/transformers/main/fr/model_doc/llava_onevision#transformers.LlavaOnevisionImageProcessorPil">LlavaOnevisionImageProcessorPil</a> pour référence.</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><!---->Ajouter des méthodes de post-traitement<!----></span></h3><!--]--><!----> <p>Ajoutez des méthodes de post-traitement directement à la classe de processeur. Les méthodes de post-traitement sont appelées avec les sorties du modèle (<code>outputs</code>) et les arguments supplémentaires requis pour la méthode de post-traitement spécifique.</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> Copié</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>Les post-processeurs renvoient soit une liste d'objets simples (<code>list[str]</code> ou <code>list[torch.Tensor]</code>), soit une liste d'objets complexes (<code>list[MyTaskPostProcessorOutput]</code> ou <code>list[dict]</code>). Les sorties des post-processeurs sont définies dans <code>src/transformers/image_processing_outputs.py</code> et héritent de <a href="/docs/transformers/main/fr/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> Copié</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><!---->Processeur vidéo<!----></span></h2><!--]--><!----> <p>Ajoutez un processeur de vidéo lorsque le modèle consomme des vidéos ou des images vidéo échantillonnées.</p> <p>Créez <code>video_processing_<model_name>.py</code> dans le répertoire du modèle. <a href="/docs/transformers/main/fr/main_classes/video_processor#transformers.BaseVideoProcessor">BaseVideoProcessor</a> hérite de <a href="/docs/transformers/main/fr/main_classes/image_processor#transformers.TorchvisionBackend">TorchvisionBackend</a> et fournit un comportement commun pour le décodage, l'échantillonnage des images, le redimensionnement, le rééchelonnement, la normalisation, l'enregistrement et le chargement.</p> <p>Les attributs de classe sont les valeurs de prétraitement par défaut. Les utilisateurs peuvent les remplacer lors de l’initialisation ou de l’appel. Utilisez les mêmes noms que <a href="/docs/transformers/main/fr/main_classes/processors#transformers.VideosKwargs">VideosKwargs</a> lorsque c’est possible, tels que <code>size</code>, <code>crop_size</code>, <code>do_resize</code>, <code>do_sample_frames</code>, <code>num_frames</code> et <code>fps</code>.</p> <p>Définissez une classe kwargs si votre processeur vidéo nécessite des paramètres personnalisés en plus des paramètres standard <a href="/docs/transformers/main/fr/main_classes/processors#transformers.VideosKwargs">VideosKwargs</a>. Définissez-la comme <code>valid_kwargs</code> et utilisez-la pour annoter <code>__init__</code> afin d’assurer à la fois la validation à l’exécution et la génération automatique de la docstring.</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> Copié</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> ...processing_utils <span class="hljs-keyword">import</span> Unpack, VideosKwargs | |
| <span class="hljs-keyword">from</span> ...utils <span class="hljs-keyword">import</span> auto_docstring | |
| <span class="hljs-keyword">from</span> ...video_processing_utils <span class="hljs-keyword">import</span> BaseVideoProcessor | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelVideoProcessorKwargs</span>(VideosKwargs, total=<span class="hljs-literal">False</span>): | |
| min_frames: <span class="hljs-built_in">int</span> | |
| max_frames: <span class="hljs-built_in">int</span> | |
| <span class="hljs-meta">@auto_docstring</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelVideoProcessor</span>(<span class="hljs-title class_ inherited__">BaseVideoProcessor</span>): | |
| size = {<span class="hljs-string">"shortest_edge"</span>: <span class="hljs-number">224</span>} | |
| crop_size = {<span class="hljs-string">"height"</span>: <span class="hljs-number">224</span>, <span class="hljs-string">"width"</span>: <span class="hljs-number">224</span>} | |
| do_resize = <span class="hljs-literal">True</span> | |
| do_center_crop = <span class="hljs-literal">True</span> | |
| do_normalize = <span class="hljs-literal">True</span> | |
| do_sample_frames = <span class="hljs-literal">True</span> | |
| num_frames = <span class="hljs-number">16</span> | |
| model_input_names = [<span class="hljs-string">"pixel_values_videos"</span>] | |
| valid_kwargs = MyModelVideoProcessorKwargs | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, **kwargs: Unpack[MyModelVideoProcessorKwargs]</span>): | |
| <span class="hljs-built_in">super</span>().__init__(**kwargs)<!----></pre></div><!----> <p>N’implémentez <a href="/docs/transformers/main/fr/main_classes/video_processor#transformers.BaseVideoProcessor.sample_frames">sample_frames()</a> que si le modèle exige une règle d’échantillonnage que l’échantillonneur uniforme de base ne peut pas exprimer. Par exemple, certains modèles imposent un nombre minimal ou maximal de frames, ou effectuent l’échantillonnage en fonction de contraintes spécifiques au modèle.</p> <p>Si la méthode forward du modèle s’attend à un nom d’entrée hérité, surchargez <code>preprocess</code> et renommez la clé après avoir appelé l’implémentation de base.</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> Copié</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelVideoProcessor</span>(<span class="hljs-title class_ inherited__">BaseVideoProcessor</span>): | |
| model_input_names = [<span class="hljs-string">"pixel_values"</span>] | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">preprocess</span>(<span class="hljs-params">self, videos, **kwargs</span>): | |
| batch = <span class="hljs-built_in">super</span>().preprocess(videos, **kwargs) | |
| batch[<span class="hljs-string">"pixel_values"</span>] = batch.pop(<span class="hljs-string">"pixel_values_videos"</span>) | |
| <span class="hljs-keyword">return</span> batch<!----></pre></div><!----> <p>Enregistrez le processeur de vidéo avec le point de contrôle en l’instanciant dans le script de conversion et en appelant <a href="/docs/transformers/main/fr/main_classes/video_processor#transformers.VideoProcessorMixin.save_pretrained">save_pretrained()</a>. Si un <a href="/docs/transformers/main/fr/main_classes/processors#transformers.ProcessorMixin">ProcessorMixin</a> encapsule le processeur de vidéo, appelez plutôt <a href="/docs/transformers/main/fr/model_doc/wav2vec2#transformers.Wav2Vec2Processor.save_pretrained">save_pretrained()</a>. Ne créez ni ne modifiez manuellement les fichiers de configuration de prétraitement.</p> <blockquote class="tip"><p>Voir <a href="/docs/transformers/main/fr/model_doc/qwen3_vl#transformers.Qwen3VLVideoProcessor">Qwen3VLVideoProcessor</a> pour référence.</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><!---->Enregistrer les classes<!----></span></h2><!--]--><!----> <p>Ajoutez les nouvelles classes à <code>__all__</code> en bas du fichier, puis régénérez <code>__init__.py</code> avec :</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> Copié</div><!----></button><!----></div> <pre class="language-bash "><!---->python utils/check_inits.py --fix_and_overwrite<!----></pre></div><!----> <p>Associez les nouvelles classes à la configuration du modèle afin que les classes <code>Auto</code> puissent les charger. Le fichier de correspondance auto généré comporte un avertissement en haut. Ne le modifiez pas manuellement. Ajoutez ou mettez à jour la configuration du modèle, puis exécutez :</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> Copié</div><!----></button><!----></div> <pre class="language-bash "><!---->python utils/check_auto.py --fix_and_overwrite<!----></pre></div><!----> <p>Une fois la correspondance générée, vérifiez que le type de modèle apparaît dans les correspondances pertinentes de <code>src/transformers/models/auto/auto_mappings.py</code>.</p> <ul><li><code>IMAGE_PROCESSOR_MAPPING_NAMES</code> pour <a href="/docs/transformers/main/fr/model_doc/auto#transformers.AutoImageProcessor">AutoImageProcessor</a></li> <li><code>VIDEO_PROCESSOR_MAPPING_NAMES</code> pour <a href="/docs/transformers/main/fr/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><!---->Tests<!----></span></h2><!--]--><!----> <p>Ajoutez des tests pour chaque composant de traitement visuel dans le répertoire de tests du modèle. Utilisez les mixins de tests partagés et fournissez les kwargs d’initialisation spécifiques au modèle. Les tests d’images découvrent automatiquement les backends du processeur via un testeur partagé, tandis que les tests de vidéo configurent explicitement leur classe de processeur et leurs fixtures.</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><!---->Tests du processeur d'images<!----></span></h3><!--]--><!----> <p>Les tests de processeur d’images se trouvent dans <code>tests/models/<model_name>/test_image_processing_<model_name>.py</code>. Définissez un tester qui hérite de <code>ImageProcessingTester</code> et une classe de test qui hérite de <code>ImageProcessingTestMixin</code> et de <code>unittest.TestCase</code>.</p> <p>Le testeur de traitement d’images définit des attributs de test par défaut, tels que les arguments d’initialisation du processeur et les paramètres pour créer des entrées aléatoires pour les tests. N’écrit par-dessus les arguments que s’ils doivent différer des valeurs par défaut du processeur d’images. Par exemple, nous testons généralement avec des tailles d’images plus petites pour garder les tests rapides. Les classes de processeur d’images sont inférées automatiquement à partir de <code>IMAGE_PROCESSOR_MAPPING_NAMES</code>.</p> <p>Définissez <code>image_processor_tester_class</code> sur la classe de test. Le mixin crée <code>self.image_processor_tester</code> et couvre déjà la plupart des méthodes de test requises.</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> Copié</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> unittest | |
| <span class="hljs-keyword">from</span> transformers.testing_utils <span class="hljs-keyword">import</span> require_torch, require_vision | |
| <span class="hljs-keyword">from</span> ...test_image_processing_common <span class="hljs-keyword">import</span> ImageProcessingTestMixin, ImageProcessingTester | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessingTester</span>(<span class="hljs-title class_ inherited__">ImageProcessingTester</span>): | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, **kwargs</span>): | |
| <span class="hljs-comment"># Random test inputs kwargs</span> | |
| kwargs.setdefault(<span class="hljs-string">"batch_size"</span>, <span class="hljs-number">2</span>) <span class="hljs-comment"># default: 7 </span> | |
| kwargs.setdefault(<span class="hljs-string">"num_channels"</span>, <span class="hljs-number">4</span>) <span class="hljs-comment"># default: 3</span> | |
| kwargs.setdefault(<span class="hljs-string">"min_resolution"</span>, <span class="hljs-number">20</span>) <span class="hljs-comment"># default: 30</span> | |
| kwargs.setdefault(<span class="hljs-string">"max_resolution"</span>, <span class="hljs-number">100</span>) <span class="hljs-comment"># default: 400</span> | |
| <span class="hljs-comment"># Image processor init kwargs</span> | |
| kwargs.setdefault(<span class="hljs-string">"size"</span>, {<span class="hljs-string">"shortest_edge"</span>: <span class="hljs-number">20</span>}) | |
| kwargs.setdefault(<span class="hljs-string">"crop_size"</span>, {<span class="hljs-string">"height"</span>: <span class="hljs-number">18</span>, <span class="hljs-string">"width"</span>: <span class="hljs-number">18</span>}) | |
| <span class="hljs-built_in">super</span>().__init__(**kwargs) | |
| <span class="hljs-meta">@require_torch</span> | |
| <span class="hljs-meta">@require_vision</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelImageProcessingTest</span>(ImageProcessingTestMixin, unittest.TestCase): | |
| image_processor_tester_class = MyModelImageProcessingTester | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">test_inherits_from_mixin</span>(<span class="hljs-params">self</span>): | |
| <span class="hljs-comment"># Always iterate over all image processing classes</span> | |
| <span class="hljs-keyword">for</span> image_processing_class <span class="hljs-keyword">in</span> <span class="hljs-variable language_">self</span>.image_processing_classes.values(): | |
| <span class="hljs-comment"># self.image_processor_dict contains the default processor init kwargs</span> | |
| <span class="hljs-comment"># including overrides from the tester.</span> | |
| image_processor = image_processing_class(**<span class="hljs-variable language_">self</span>.image_processor_dict) | |
| <span class="hljs-variable language_">self</span>.assertIsInstance(image_processor, ImageProcessingMixin)<!----></pre></div><!----> <p>Ajoutez des tests ciblés pour les comportements que le mixin ne peut pas inférer, tels que des règles de redimensionnement personnalisées ou des kwargs spécifiques au modèle.</p> <p>Les mixins de test de post-traitement sont disponibles dans <code>tests/test_image_processing_common.py</code> et sont ajoutés en plus de <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> Copié</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>Les tests vérifient automatiquement que les mixins corrects sont utilisés pour votre modèle. Les mixins pour les nouvelles tâches doivent être ajoutés à <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><!---->Tests du processeur vidéo<!----></span></h3><!--]--><!----> <p>Les tests de processeur vidéo se trouvent généralement dans <code>tests/models/<model_name>/test_video_processing_<model_name>.py</code> et héritent de <code>VideoProcessingTestMixin</code>. Définissez <code>fast_video_processing_class</code>, définissez <code>video_processor_dict</code> et surchargez <code>input_name</code> si le modèle utilise une clé autre que <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> Copié</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers.testing_utils <span class="hljs-keyword">import</span> require_torch, require_vision | |
| <span class="hljs-keyword">from</span> transformers.utils <span class="hljs-keyword">import</span> is_torchvision_available | |
| <span class="hljs-keyword">from</span> ...test_video_processing_common <span class="hljs-keyword">import</span> VideoProcessingTestMixin | |
| <span class="hljs-meta">@require_torch</span> | |
| <span class="hljs-meta">@require_vision</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelVideoProcessingTest</span>(VideoProcessingTestMixin, unittest.TestCase): | |
| fast_video_processing_class = MyModelVideoProcessor <span class="hljs-keyword">if</span> is_torchvision_available() <span class="hljs-keyword">else</span> <span class="hljs-literal">None</span> | |
| input_name = <span class="hljs-string">"pixel_values_videos"</span> | |
| <span class="hljs-meta"> @property</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">video_processor_dict</span>(<span class="hljs-params">self</span>): | |
| <span class="hljs-keyword">return</span> {<span class="hljs-string">"size"</span>: {<span class="hljs-string">"shortest_edge"</span>: <span class="hljs-number">224</span>}, <span class="hljs-string">"num_frames"</span>: <span class="hljs-number">16</span>}<!----></pre></div><!----> <p>Ajoutez des tests vidéo ciblés pour l'échantillonnage des images, la gestion des métadonnées, les entrées vidéo décodées, les entrées de listes d'images et les formes de sortie. Si votre processeur renomme <code>pixel_values_videos</code>, vérifiez que la clé renommée est bien retournée.</p> <p>Si le modèle dispose également d'un <a href="/docs/transformers/main/fr/main_classes/processors#transformers.ProcessorMixin">ProcessorMixin</a> qui encapsule le processeur d'images ou de vidéos, ajoutez <code>tests/models/<model_name>/test_processing_<model_name>.py</code> et héritez de <code>ProcessorTesterMixin</code>. Définissez <code>processor_class</code> et surchargez les méthodes de classe <code>_setup_<component>()</code> pour les composants qui ne peuvent pas être construits sans arguments. Utilisez <code>_setup_test_attributes()</code> pour exposer les jetons de substitution utilisés par les tests de processeur communs.</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> Copié</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> ...test_processing_common <span class="hljs-keyword">import</span> ProcessorTesterMixin | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModelProcessorTest</span>(ProcessorTesterMixin, unittest.TestCase): | |
| processor_class = MyModelProcessor | |
| <span class="hljs-meta"> @classmethod</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">_setup_image_processor</span>(<span class="hljs-params">cls</span>): | |
| <span class="hljs-keyword">return</span> cls._get_component_class_from_processor(<span class="hljs-string">"image_processor"</span>)(size={<span class="hljs-string">"shortest_edge"</span>: <span class="hljs-number">224</span>}) | |
| <span class="hljs-meta"> @classmethod</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">_setup_video_processor</span>(<span class="hljs-params">cls</span>): | |
| <span class="hljs-keyword">return</span> cls._get_component_class_from_processor(<span class="hljs-string">"video_processor"</span>)(num_frames=<span class="hljs-number">2</span>) | |
| <span class="hljs-meta"> @classmethod</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">_setup_test_attributes</span>(<span class="hljs-params">cls, processor</span>): | |
| cls.image_token = <span class="hljs-built_in">getattr</span>(processor, <span class="hljs-string">"image_token"</span>, <span class="hljs-string">""</span>) | |
| cls.video_token = <span class="hljs-built_in">getattr</span>(processor, <span class="hljs-string">"video_token"</span>, <span class="hljs-string">""</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="next-steps" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#next-steps"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Étapes suivantes<!----></span></h2><!--]--><!----> <ul><li>Lisez le guide <a href="./auto_docstring">Génération automatique des docstrings</a> pour générer automatiquement des docstrings cohérentes avec <code>@auto_docstring</code>.</li> <li>Consultez les guides <a href="./image_processors">Image processors</a> et <a href="./video_processors">Video processors</a> pour le comportement de prétraitement destiné aux utilisateurs.</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.1 kB
- Xet hash:
- ec973dbdfd59d97b7dd5c877867229074c7c20bfa87fbf7506ec4356f59ff17f
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.