Buckets:

hf-doc-build/doc / transformers /main /vi /image_processors.html
mishig's picture
download
raw
52.2 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Bộ xử lý hình ảnh&quot;,&quot;local&quot;:&quot;image-processors&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Các lớp bộ xử lý ảnh&quot;,&quot;local&quot;:&quot;image-processor-classes&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Bộ xử lý backend Torchvision&quot;,&quot;local&quot;:&quot;torchvision-backend-processors&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Độ phân giải động&quot;,&quot;local&quot;:&quot;dynamic-resolution&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Tiền xử lý&quot;,&quot;local&quot;:&quot;preprocess&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Đệm&quot;,&quot;local&quot;:&quot;padding&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/transformers/main/en/_app/immutable/entry/start.DS27pc2p.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/Bbt6a5UJ.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DGxd7hEC.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/entry/app.JevHRe6k.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/hD794eHD.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DG_DvbXP.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.Dperbprz.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DIDdACb8.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/8.LcC4poxC.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/CBFQlbiL.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/BUd-buby.js" rel="modulepreload">
<!--1iac3mg--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Image processors&quot;,&quot;local&quot;:&quot;image-processors&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Image processor classes&quot;,&quot;local&quot;:&quot;image-processor-classes&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Torchvision backend processors&quot;,&quot;local&quot;:&quot;torchvision-backend-processors&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Dynamic resolution&quot;,&quot;local&quot;:&quot;dynamic-resolution&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Preprocess&quot;,&quot;local&quot;:&quot;preprocess&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Padding&quot;,&quot;local&quot;:&quot;padding&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&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="image-processors" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#image-processors"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Bộ xử lý hình ảnh<!----></span></h1><!--]--><!----> <p>Bộ xử lý ảnh chuyển đổi ảnh thành các giá trị pixel, tức là các tensor đại diện cho màu sắc và kích thước của ảnh. Các giá trị pixel này là đầu vào cho một mô hình thị giác. Để đảm bảo một mô hình được huấn luyện trước nhận được đầu vào chính xác, bộ xử lý ảnh có thể thực hiện các thao tác sau để đảm bảo ảnh hoàn toàn giống với các ảnh mà mô hình đã được huấn luyện trước đó.</p> <ul><li>center-crop hoặc resize một hình ảnh</li> <li>chuẩn hóa hoặc điều chỉnh lại giá trị pixel</li></ul> <p>Sử dụng <a href="/docs/transformers/main/vi/internal/image_processing_utils#transformers.ImageProcessingMixin.from_pretrained">from_pretrained()</a> để tải cấu hình bộ xử lý ảnh (kích thước ảnh, có chuẩn hóa và co giãn hay không, v.v.) từ một mô hình thị giác trên <a href="https://hf.co" rel="nofollow">Hub</a> của Hugging Face hoặc thư mục cục bộ. Cấu hình cho mỗi mô hình được huấn luyện trước được lưu trong tệp <a href="https://huggingface.co/google/vit-base-patch16-224/blob/main/preprocessor_config.json" rel="nofollow">preprocessor_config.json</a>.</p> <p>Khi bộ xử lý ảnh là một phần của bộ xử lý đa phương thức, cấu hình của nó được lồng trong <code>processor_config.json</code>. Xem <a href="./processors#save-and-load-a-processor">Lưu và tải bộ xử lý</a> để biết chi tiết.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoImageProcessor
image_processor = AutoImageProcessor.from_pretrained(<span class="hljs-string">&quot;google/vit-base-patch16-224&quot;</span>)<!----></pre></div><!----> <p>Truyền một hình ảnh vào bộ xử lý hình ảnh để chuyển đổi nó thành các giá trị pixel, và đặt <code>return_tensors="pt"</code> để trả về các tensor PyTorch. Bạn có thể in ra các đầu vào để xem hình ảnh trông như thế nào dưới dạng tensor.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
<span class="hljs-keyword">import</span> requests
url = <span class="hljs-string">&quot;https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/image_processor_example.png&quot;</span>
image = Image.<span class="hljs-built_in">open</span>(requests.get(url, stream=<span class="hljs-literal">True</span>).raw).convert(<span class="hljs-string">&quot;RGB&quot;</span>)
inputs = image_processor(image, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)<!----></pre></div><!----> <p>Hướng dẫn này bao gồm lớp image processor và cách tiền xử lý hình ảnh cho các mô hình thị giác.</p> <!--[1--><h2 class="relative group"><a id="image-processor-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="#image-processor-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><!---->Các lớp bộ xử lý ảnh<!----></span></h2><!--]--><!----> <p>Bộ xử lý hình ảnh sử dụng kiến trúc dựa trên backend với hai backend:</p> <ul><li><a href="/docs/transformers/main/vi/main_classes/image_processor#transformers.TorchvisionBackend">TorchvisionBackend</a> — triển khai mặc định dựa trên <a href="https://pytorch.org/vision/stable/index.html" rel="nofollow">torchvision</a>. Được tăng tốc bằng GPU và nhanh hơn backend PIL tới 33 lần đối với các lô đầu vào dạng <a href="https://pytorch.org/docs/stable/tensors.html" rel="nofollow">torch.Tensor</a>. Tất cả các model đều hỗ trợ backend này; các model mới hơn chỉ hỗ trợ duy nhất backend này.</li> <li><a href="/docs/transformers/main/vi/main_classes/image_processor#transformers.PilBackend">PilBackend</a> — phương án thay thế bằng PIL/NumPy. Di động và chỉ chạy trên CPU. Chỉ khả dụng cho các mô hình cũ, nơi nó hữu ích để tái tạo chính xác các đầu ra số của bản triển khai gốc.</li></ul> <p>Backend đang hoạt động trên một processor đã được nạp có thể được kiểm tra thông qua thuộc tính <code>backend</code> của nó (ví dụ: <code>processor.backend == "torchvision"</code>). Mỗi image processor kế thừa từ <a href="/docs/transformers/main/vi/internal/image_processing_utils#transformers.ImageProcessingMixin">ImageProcessingMixin</a>, cung cấp các phương thức <a href="/docs/transformers/main/vi/internal/image_processing_utils#transformers.ImageProcessingMixin.from_pretrained">from_pretrained()</a> và <a href="/docs/transformers/main/vi/internal/image_processing_utils#transformers.ImageProcessingMixin.save_pretrained">save_pretrained()</a>.</p> <p>Có hai cách để bạn nạp trình xử lý ảnh: bằng <a href="/docs/transformers/main/vi/model_doc/auto#transformers.AutoImageProcessor">AutoImageProcessor</a> hoặc trực tiếp từ một lớp cụ thể của mô hình.</p> <div class="flex space-x-2 items-center my-1.5 mr-8 h-7 !pl-0 -mx-3 md:mx-0"><!--[--><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd border-gray-800 bg-black dark:bg-gray-700 text-white">AutoImageProcessor</div><div class="flex items-center border rounded-lg px-1.5 py-1 leading-none select-none text-smd text-gray-500 cursor-pointer opacity-90 hover:text-gray-700 dark:hover:text-gray-200 hover:shadow-sm">bộ xử lý ảnh cụ thể cho mô hình</div><!--]--></div> <div class="language-select"><div><p>API <a href="./model_doc/auto">AutoClass</a> cung cấp một phương pháp tiện lợi để nạp bộ xử lý hình ảnh mà không cần chỉ định trực tiếp mô hình mà bộ xử lý hình ảnh đó liên kết.</p> <p>Sử dụng <a href="/docs/transformers/main/vi/model_doc/auto#transformers.AutoImageProcessor.from_pretrained">from_pretrained()</a> với tham số <code>backend</code> để chọn backend. Khi <code>backend</code> bị bỏ qua (mặc định), torchvision sẽ được chọn nếu đã được cài đặt, ngược lại PIL sẽ được sử dụng. Lưu ý rằng <code>backend="pil"</code> chỉ được hỗ trợ cho các model cũ; các model mới chỉ cung cấp backend torchvision.</p> <p>Để có sự khớp gần nhất với tiền xử lý trong quá trình huấn luyện của mô hình, hãy sử dụng backend được chỉ định trong tài liệu hoặc mã huấn luyện của nó. Nếu pipeline huấn luyện sử dụng PIL và mô hình hỗ trợ cả hai backend, hãy truyền <code>backend="pil"</code>. Việc chuyển đổi backend có thể làm thay đổi giá trị pixel sau khi resize, và các bản cập nhật của Pillow hoặc torchvision cũng có thể gây ra thay đổi tương tự. Để tiền xử lý có thể tái lập được, hãy giữ nguyên phiên bản của backend và thư viện ảnh.</p> <blockquote class="note"><p>Một nhóm nhỏ các model cũ (Chameleon, Flava, Idefics3, SmolVLM) sử dụng nội suy Lanczos. Backend mặc định của chúng phụ thuộc vào phiên bản torchvision của bạn. Với torchvision &gt; 0.27, Lanczos được hỗ trợ bản địa và các model này mặc định dùng torchvision. Các phiên bản torchvision cũ hơn sẽ chuyển sang BICUBIC, do đó mặc định là PIL để giữ nguyên đầu ra ban đầu. Truyền <code>backend="torchvision"</code> một cách tường minh để ghi đè mặc định.</p></blockquote> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoImageProcessor
<span class="hljs-comment"># Default: picks torchvision if available, otherwise pil</span>
image_processor = AutoImageProcessor.from_pretrained(<span class="hljs-string">&quot;google/vit-base-patch16-224&quot;</span>)
<span class="hljs-comment"># Explicitly request the torchvision backend</span>
image_processor = AutoImageProcessor.from_pretrained(<span class="hljs-string">&quot;google/vit-base-patch16-224&quot;</span>, backend=<span class="hljs-string">&quot;torchvision&quot;</span>)
<span class="hljs-comment"># Explicitly request the PIL backend (only for models that support it)</span>
image_processor = AutoImageProcessor.from_pretrained(<span class="hljs-string">&quot;google/vit-base-patch16-224&quot;</span>, backend=<span class="hljs-string">&quot;pil&quot;</span>)<!----></pre></div><!----><!----></div><!----> <div hidden=""><p>Mỗi image processor được liên kết với một mô hình thị giác được huấn luyện trước cụ thể, và cấu hình của nó chứa kích thước mong đợi và các tham số chuẩn hóa của mô hình.</p> <p>Tải bộ xử lý backend torchvision trực tiếp từ lớp cụ thể của mô hình.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> ViTImageProcessor
image_processor = ViTImageProcessor.from_pretrained(<span class="hljs-string">&quot;google/vit-base-patch16-224&quot;</span>)<!----></pre></div><!----> <p>Đối với các mô hình hỗ trợ, bạn có thể nạp backend PIL bằng lớp có hậu tố <code>Pil</code>. Điều này hữu ích khi bạn cần sự tương thích số học chính xác với bản triển khai gốc.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> ViTImageProcessorPil
image_processor = ViTImageProcessorPil.from_pretrained(<span class="hljs-string">&quot;google/vit-base-patch16-224&quot;</span>)<!----></pre></div><!----><!----></div><!----><!----></div><!----> <!--[1--><h2 class="relative group"><a id="torchvision-backend-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="#torchvision-backend-processors"><span><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg><!----></span></a> <span><!---->Bộ xử lý backend Torchvision<!----></span></h2><!--]--><!----> <p><a href="/docs/transformers/main/vi/main_classes/image_processor#transformers.TorchvisionBackend">TorchvisionBackend</a> là backend <strong>mặc định</strong>. Hãy đảm bảo <a href="https://pytorch.org/get-started/locally/#mac-installation" rel="nofollow">torchvision</a> đã được cài đặt, sau đó tải nó bằng <code>backend="torchvision"</code> (hoặc đơn giản là bỏ qua <code>backend</code>, vì torchvision sẽ được chọn tự động khi có sẵn).</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoImageProcessor
processor = AutoImageProcessor.from_pretrained(<span class="hljs-string">&quot;facebook/detr-resnet-50&quot;</span>, backend=<span class="hljs-string">&quot;torchvision&quot;</span>)<!----></pre></div><!----> <p>Điều khiển thiết bị thực hiện xử lý bằng đối số <code>device</code>. Mặc định, xử lý được thực hiện trên cùng thiết bị với đầu vào nếu đầu vào là tensor, ngược lại sẽ chuyển sang CPU. Ví dụ bên dưới chạy xử lý trên bộ tăng tốc.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> torchvision.io <span class="hljs-keyword">import</span> read_image
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> DetrImageProcessor
device = torch.accelerator.current_accelerator().<span class="hljs-built_in">type</span> <span class="hljs-keyword">if</span> torch.accelerator.is_available() <span class="hljs-keyword">else</span> <span class="hljs-string">&quot;cpu&quot;</span>
images = read_image(<span class="hljs-string">&quot;image.jpg&quot;</span>)
processor = DetrImageProcessor.from_pretrained(<span class="hljs-string">&quot;facebook/detr-resnet-50&quot;</span>)
images_processed = processor(images, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>, device=device)<!----></pre></div><!----> <details><summary>Benchmarks</summary> <p>Các kết quả benchmark được lấy từ một instance <a href="https://aws.amazon.com/ec2/instance-types/g5/" rel="nofollow">AWS EC2 g5.2xlarge</a> với GPU NVIDIA A10G Tensor Core.</p> <div class="flex"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/benchmark_results_full_pipeline_detr_fast_padded.png"/></div> <div class="flex"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/benchmark_results_full_pipeline_detr_fast_batched_compiled.png"/></div> <div class="flex"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/benchmark_results_full_pipeline_rt_detr_fast_single.png"/></div> <div class="flex"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/benchmark_results_full_pipeline_rt_detr_fast_batched.png"/></div></details> <!--[1--><h2 class="relative group"><a id="dynamic-resolution" 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="#dynamic-resolution"><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><!---->Độ phân giải động<!----></span></h2><!--]--><!----> <p>Hầu hết bộ xử lý ảnh (image processor) đều điều chỉnh kích thước mọi ảnh về một độ phân giải cố định. Độ phân giải động cho phép một số mô hình giữ nguyên tỷ lệ khung hình gốc và để lượng dữ liệu tiền xử lý tăng theo kích thước ảnh, nhờ đó một bức ảnh chi tiết sẽ nhận được nhiều patch hơn một biểu tượng nhỏ và mô hình sẽ phân bổ sức mạnh tính toán vào những nơi thực sự có nội dung đáng chú ý.</p> <p>Cách hình ảnh được chia phụ thuộc vào từng mô hình. Một số mô hình cắt hình ảnh thành một số lượng biến đổi các mảnh có kích thước cố định, bị giới hạn bởi các tham số như <code>min_patches</code> và <code>max_patches</code>. Các mô hình khác sẽ thay đổi kích thước hình ảnh để phù hợp nhất với độ phân giải, được chọn từ một danh sách các tỷ lệ khung hình có sẵn hoặc được suy ra từ ngân sách pixel.</p> <p>Tham khảo trang tài liệu của mô hình để biết các tham số, giá trị mặc định và hình dạng đầu ra.</p> <!--[1--><h2 class="relative group"><a id="preprocess" 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="#preprocess"><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><!---->Tiền xử lý<!----></span></h2><!--]--><!----> <p>Mô hình thị giác của Transformers kỳ vọng đầu vào dưới dạng tensor PyTorch của các giá trị pixel. Bộ xử lý ảnh đảm nhận việc chuyển đổi ảnh thành các giá trị pixel, được biểu diễn bởi kích thước lô (batch size), số kênh, chiều cao và chiều rộng. Để đạt được điều này, ảnh sẽ được thay đổi kích thước (cắt theo tâm) và các giá trị pixel được chuẩn hóa cũng như điều chỉnh tỷ lệ về các giá trị mà mô hình kỳ vọng.</p> <p>Tiền xử lý ảnh không giống với <em>tăng cường ảnh</em>. Tăng cường ảnh thực hiện các thay đổi (độ sáng, màu sắc, xoay, v.v.) đối với một ảnh nhằm mục đích tạo ra các ví dụ huấn luyện mới hoặc ngăn chặn quá khớp. Tiền xử lý ảnh thực hiện các thay đổi đối với một ảnh nhằm mục đích khớp với định dạng đầu vào mong đợi của một mô hình được huấn luyện trước.</p> <p>Thông thường, hình ảnh sẽ được tăng cường (để tăng hiệu suất) và sau đó được tiền xử lý trước khi được truyền vào mô hình. Bạn có thể sử dụng bất kỳ thư viện nào (<a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_albumentations.ipynb" rel="nofollow">Albumentations</a>, <a href="https://colab.research.google.com/github/huggingface/notebooks/blob/main/examples/image_classification_kornia.ipynb" rel="nofollow">Kornia</a>) để tăng cường và một bộ xử lý hình ảnh để tiền xử lý.</p> <p>Hướng dẫn này sử dụng module <a href="https://pytorch.org/vision/stable/transforms.html" rel="nofollow">transforms</a> của torchvision để tăng cường dữ liệu.</p> <p>Bắt đầu bằng cách tải một mẫu nhỏ của bộ dữ liệu <a href="https://hf.co/datasets/ethz/food101" rel="nofollow">food101</a>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset
dataset = load_dataset(<span class="hljs-string">&quot;ethz/food101&quot;</span>, split=<span class="hljs-string">&quot;train[:100]&quot;</span>)<!----></pre></div><!----> <p>Từ module <a href="https://pytorch.org/vision/stable/transforms.html" rel="nofollow">transforms</a>, hãy sử dụng API <a href="https://pytorch.org/vision/master/generated/torchvision.transforms.Compose.html" rel="nofollow">Compose</a> để kết nối <a href="https://pytorch.org/vision/main/generated/torchvision.transforms.RandomResizedCrop.html" rel="nofollow">RandomResizedCrop</a> và <a href="https://pytorch.org/vision/main/generated/torchvision.transforms.ColorJitter.html" rel="nofollow">ColorJitter</a>. Các transform này sẽ cắt ngẫu nhiên và thay đổi kích thước một hình ảnh, đồng thời điều chỉnh màu sắc của hình ảnh một cách ngẫu nhiên.</p> <p>Kích thước ảnh để cắt ngẫu nhiên có thể được lấy từ bộ xử lý ảnh. Đối với một số mô hình, chiều cao và chiều rộng chính xác được yêu cầu, trong khi với các mô hình khác, chỉ cần <code>shortest_edge</code>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">from</span> torchvision.transforms <span class="hljs-keyword">import</span> RandomResizedCrop, ColorJitter, Compose
size = (
image_processor.size[<span class="hljs-string">&quot;shortest_edge&quot;</span>]
<span class="hljs-keyword">if</span> <span class="hljs-string">&quot;shortest_edge&quot;</span> <span class="hljs-keyword">in</span> image_processor.size
<span class="hljs-keyword">else</span> (image_processor.size[<span class="hljs-string">&quot;height&quot;</span>], image_processor.size[<span class="hljs-string">&quot;width&quot;</span>])
)
_transforms = Compose([RandomResizedCrop(size), ColorJitter(brightness=<span class="hljs-number">0.5</span>, hue=<span class="hljs-number">0.5</span>)])<!----></pre></div><!----> <p>Áp dụng các phép biến đổi lên các hình ảnh và chuyển chúng sang định dạng RGB. Sau đó, truyền các hình ảnh đã tăng cường vào bộ xử lý hình ảnh để trả về các giá trị pixel.</p> <p>Tham số <code>do_resize</code> được đặt thành <code>False</code> vì các hình ảnh đã được thay đổi kích thước ở bước tăng cường dữ liệu bằng <a href="https://pytorch.org/vision/main/generated/torchvision.transforms.RandomResizedCrop.html" rel="nofollow">RandomResizedCrop</a>. Nếu bạn không tăng cường dữ liệu hình ảnh, trình xử lý hình ảnh sẽ tự động thay đổi kích thước và chuẩn hóa các hình ảnh bằng các giá trị <code>image_mean</code> và <code>image_std</code>. Các giá trị này được tìm thấy trong tệp cấu hình tiền xử lý.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">def</span> <span class="hljs-title function_">transforms</span>(<span class="hljs-params">examples</span>):
images = [_transforms(img.convert(<span class="hljs-string">&quot;RGB&quot;</span>)) <span class="hljs-keyword">for</span> img <span class="hljs-keyword">in</span> examples[<span class="hljs-string">&quot;image&quot;</span>]]
examples[<span class="hljs-string">&quot;pixel_values&quot;</span>] = image_processor(images, do_resize=<span class="hljs-literal">False</span>, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)[<span class="hljs-string">&quot;pixel_values&quot;</span>]
<span class="hljs-keyword">return</span> examples<!----></pre></div><!----> <p>Áp dụng hàm tăng cường và tiền xử lý kết hợp lên toàn bộ tập dữ liệu theo thời gian thực bằng <a href="https://huggingface.co/docs/datasets/main/en/package_reference/main_classes#datasets.Dataset.set_transform" rel="nofollow">set_transform</a>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!---->dataset.set_transform(transforms)<!----></pre></div><!----> <p>Chuyển các giá trị pixel trở lại thành hình ảnh để xem hình ảnh đã được tăng cường và tiền xử lý như thế nào.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np
<span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt
img = dataset[<span class="hljs-number">0</span>][<span class="hljs-string">&quot;pixel_values&quot;</span>]
plt.imshow(img.permute(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">0</span>))<!----></pre></div><!----> <div class="flex gap-4"><div><img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/vision-preprocess-tutorial.png"/> <figcaption class="mt-2 text-center text-sm text-gray-500">trước đó</figcaption></div> <div><img class="rounded-xl" src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/preprocessed_image.png"/> <figcaption class="mt-2 text-center text-sm text-gray-500">sau</figcaption></div></div> <p>Đối với các tác vụ thị giác khác như phát hiện đối tượng hoặc phân đoạn, bộ xử lý hình ảnh bao gồm các phương pháp xử lý hậu kỳ để chuyển đổi đầu ra thô của mô hình thành các dự đoán có ý nghĩa như hộp bao quanh hoặc bản đồ phân đoạn.</p> <!--[2--><h3 class="relative group"><a id="padding" 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="#padding"><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><!---->Đệm<!----></span></h3><!--]--><!----> <p>Một số mô hình, chẳng hạn như <a href="./model_doc/detr">DETR</a>, áp dụng <a href="https://paperswithcode.com/method/image-scale-augmentation" rel="nofollow">scale augmentation</a> trong quá trình huấn luyện, điều này có thể khiến các hình ảnh trong một batch có kích thước khác nhau. Các hình ảnh có kích thước khác nhau không thể được batch cùng nhau.</p> <p>Để khắc phục điều này, hãy đệm các ảnh bằng token đệm đặc biệt <code>0</code>. Sử dụng phương thức <a href="https://github.com/huggingface/transformers/blob/9578c2597e2d88b6f0b304b5a05864fd613ddcc1/src/transformers/models/detr/image_processing_detr.py#L1151" rel="nofollow">pad</a> để đệm các ảnh và định nghĩa một hàm collate tùy chỉnh để nhóm chúng thành một batch.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">def</span> <span class="hljs-title function_">collate_fn</span>(<span class="hljs-params">batch</span>):
pixel_values = [item[<span class="hljs-string">&quot;pixel_values&quot;</span>] <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> batch]
encoding = image_processor.pad(pixel_values, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)
labels = [item[<span class="hljs-string">&quot;labels&quot;</span>] <span class="hljs-keyword">for</span> item <span class="hljs-keyword">in</span> batch]
batch = {}
batch[<span class="hljs-string">&quot;pixel_values&quot;</span>] = encoding[<span class="hljs-string">&quot;pixel_values&quot;</span>]
batch[<span class="hljs-string">&quot;pixel_mask&quot;</span>] = encoding[<span class="hljs-string">&quot;pixel_mask&quot;</span>]
batch[<span class="hljs-string">&quot;labels&quot;</span>] = labels
<span class="hljs-keyword">return</span> batch<!----></pre></div><!----> <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/image_processors.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_p1l6wl = {
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.DS27pc2p.js"),
import("/docs/transformers/main/en/_app/immutable/entry/app.JevHRe6k.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 8],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
52.2 kB
·
Xet hash:
cafec06fd0f276fa48c8f50ebfafb482d80910dadffd89f37c24dfbf453d442a

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