{"id":36097,"date":"2024-11-01T09:45:44","date_gmt":"2024-11-01T09:45:44","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36097"},"modified":"2024-11-01T09:45:44","modified_gmt":"2024-11-01T09:45:44","slug":"hugging-face-transformers-tutorial-loading-pretrained-model-based-on-clip","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36097\/","title":{"rendered":"Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP"},"content":{"rendered":"<p><body><\/p>\n<h2>1. Introduction<\/h2>\n<p>\n        Recent advancements in the field of artificial intelligence are progressing at a remarkable pace. In particular,<br \/>\n        deep learning models are demonstrating extraordinary performance in the fields of computer vision and natural<br \/>\n        language processing. Among them, the CLIP (Contrastive Language\u2013Image Pre-Training) model has gained attention as<br \/>\n        a powerful model capable of understanding and processing text and images simultaneously. In this course, we will<br \/>\n        provide detailed explanations on how to load and use CLIP-based pre-trained models utilizing the Hugging Face<br \/>\n        Transformers library.\n    <\/p>\n<h2>2. Concept of the CLIP Model<\/h2>\n<p>\n        The CLIP model is a model introduced by OpenAI, trained to connect and understand text and images. This model learns<br \/>\n        from large-scale datasets of text-image pairs, enabling it to generate descriptions for given images or select images<br \/>\n        that correspond to given text.\n    <\/p>\n<p>\n        The core idea of CLIP is &#8220;contrastive learning.&#8221; This approach ensures that pairs of similar text and images are<br \/>\n        positioned closely in vector space, while pairs with different content are learned to be far apart. This allows CLIP<br \/>\n        to exhibit remarkable performance even with unsupervised learning.\n    <\/p>\n<h2>3. Hugging Face Transformers Library<\/h2>\n<p>\n        The Hugging Face Transformers library is a tool that makes it easy to use various models related to natural language<br \/>\n        processing (NLP). Through this library, users can easily load various pre-trained models and perform tasks such as<br \/>\n        tokenization and data preprocessing. The CLIP model is also supported by this library.\n    <\/p>\n<h2>4. Environment Setup<\/h2>\n<p>\n        To use the CLIP model, you first need to install the necessary libraries. In a Python environment, you can install<br \/>\n        Transformers library and related packages using the command below.\n    <\/p>\n<pre><code>pip install transformers torch torchvision<\/code><\/pre>\n<h2>5. Loading the CLIP Model<\/h2>\n<p>\n        Now, let&#8217;s explain how to load the CLIP model in earnest. The library provides easy access to pre-trained CLIP models.<br \/>\n        We will look at an example of loading the CLIP model and tokenizer using the Python code below.\n    <\/p>\n<pre><code>from transformers import CLIPProcessor, CLIPModel\n\n# Load CLIP model and processor\nmodel = CLIPModel.from_pretrained('openai\/clip-vit-base-patch16')\nprocessor = CLIPProcessor.from_pretrained('openai\/clip-vit-base-patch16')<\/code><\/pre>\n<h3>5.1. Explanation of the Model and Processor<\/h3>\n<p>\n        In the code above, we use the `from_pretrained` method to load the pre-trained CLIP model and processor. The processor<br \/>\n        serves to process the input text and images, transforming them into a format the model can understand. In other words,<br \/>\n        it converts images into tensor format and tokenizes the text so that it can be accepted as model input.\n    <\/p>\n<h2>6. Input of Images and Text<\/h2>\n<p>\n        The CLIP model can take both images and text as input. The code below demonstrates the process of downloading a random<br \/>\n        image and inputting it into the model along with its corresponding text.\n    <\/p>\n<pre><code>import requests\nfrom PIL import Image\n\n# Download image\nurl = \"https:\/\/example.com\/sample.jpg\"\nimage = Image.open(requests.get(url, stream=True).raw)\n\n# Text input\ntext = \"A sample image description\"<\/code><\/pre>\n<h3>6.1. Preparing the Image File<\/h3>\n<p>\n        In the code above, the requests library is used to download the image file. Then, the Pillow library is used to open<br \/>\n        the image. You can specify the URL of the actual image you want to use for downloading, or you can use an image file<br \/>\n        stored locally.\n    <\/p>\n<h2>7. Inference with the CLIP Model<\/h2>\n<p>\n        Now, let&#8217;s input the image and text into the model and proceed with the inference. You can check the model&#8217;s output<br \/>\n        with the following code.\n    <\/p>\n<pre><code># Preprocess input data\ninputs = processor(text=text, images=image, return_tensors=\"pt\", padding=True)\n\n# Model inference\noutputs = model(**inputs)\n\n# Extract similarity scores\nlogits_per_image = outputs.logits_per_image\nprobs = logits_per_image.softmax(dim=1)\n\nprint(f\"Prediction probability: {probs}\")<\/code><\/pre>\n<h3>7.1. Explanation of the Model Inference Process<\/h3>\n<p>\n        After preprocessing the input text and image using the `processor`, we input that information into the model for inference.<br \/>\n        The logits returned from the model are then converted into a probability distribution using the softmax function to derive<br \/>\n        the final prediction probability.\n    <\/p>\n<h2>8. Example: Utilizing the CLIP Model<\/h2>\n<p>\n        Below is the full code showing how to actually utilize the CLIP model. This code evaluates the similarity of images<br \/>\n        based on the given text.\n    <\/p>\n<pre><code>import requests\nimport torch\nfrom PIL import Image\nfrom transformers import CLIPProcessor, CLIPModel\n\n# Load the model and processor\nmodel = CLIPModel.from_pretrained('openai\/clip-vit-base-patch16')\nprocessor = CLIPProcessor.from_pretrained('openai\/clip-vit-base-patch16')\n\n# Download image\nurl = \"https:\/\/example.com\/sample.jpg\"\nimage = Image.open(requests.get(url, stream=True).raw)\n\n# Text input\ntext = \"A sample image description\"\n\n# Preprocess input data\ninputs = processor(text=text, images=image, return_tensors=\"pt\", padding=True)\n\n# Model inference\nwith torch.no_grad():\n    outputs = model(**inputs)\n\n# Extract similarity scores\nlogits_per_image = outputs.logits_per_image\nprobs = logits_per_image.softmax(dim=1)\n\nprint(f\"Prediction probability: {probs}\")<\/code><\/pre>\n<h2>9. Conclusion<\/h2>\n<p>\n        In this lecture, we explained how to load a CLIP-based pre-trained model using the Hugging Face Transformers library and<br \/>\n        evaluate the similarity by inputting image-text pairs. The CLIP model has various applications and can contribute to<br \/>\n        the development of more advanced AI systems. We encourage you to continue expanding the possibilities of artificial<br \/>\n        intelligence using various deep learning technologies.\n    <\/p>\n<h2>10. References<\/h2>\n<ul>\n<li><a href=\"https:\/\/huggingface.co\/docs\/transformers\/model_doc\/clip\">Hugging Face&#8217;s Transformers Documentation<\/a><\/li>\n<li><a href=\"https:\/\/openai.com\/research\/clip\">CLIP: Connecting Text and Images<\/a><\/li>\n<li><a href=\"https:\/\/pytorch.org\/\">PyTorch Documentation<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Introduction Recent advancements in the field of artificial intelligence are progressing at a remarkable pace. In particular, deep learning models are demonstrating extraordinary performance in the fields of computer vision and natural language processing. Among them, the CLIP (Contrastive Language\u2013Image Pre-Training) model has gained attention as a powerful model capable of understanding and processing &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36097\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[108],"tags":[],"class_list":["post-36097","post","type-post","status-publish","format-standard","hentry","category---en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/36097\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"1. Introduction Recent advancements in the field of artificial intelligence are progressing at a remarkable pace. In particular, deep learning models are demonstrating extraordinary performance in the fields of computer vision and natural language processing. Among them, the CLIP (Contrastive Language\u2013Image Pre-Training) model has gained attention as a powerful model capable of understanding and processing &hellip; \ub354 \ubcf4\uae30 &quot;Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36097\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:45:44+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/36097\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36097\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP\",\"datePublished\":\"2024-11-01T09:45:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36097\/\"},\"wordCount\":651,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Using Hugging Face\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36097\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36097\/\",\"name\":\"Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:45:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36097\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36097\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36097\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/36097\/","og_locale":"ko_KR","og_type":"article","og_title":"Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"1. Introduction Recent advancements in the field of artificial intelligence are progressing at a remarkable pace. In particular, deep learning models are demonstrating extraordinary performance in the fields of computer vision and natural language processing. Among them, the CLIP (Contrastive Language\u2013Image Pre-Training) model has gained attention as a powerful model capable of understanding and processing &hellip; \ub354 \ubcf4\uae30 \"Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP\"","og_url":"https:\/\/atmokpo.com\/w\/36097\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:45:44+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/36097\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36097\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP","datePublished":"2024-11-01T09:45:44+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36097\/"},"wordCount":651,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Using Hugging Face"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36097\/","url":"https:\/\/atmokpo.com\/w\/36097\/","name":"Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:45:44+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36097\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36097\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36097\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Hugging Face Transformers Tutorial: Loading Pretrained Model Based on CLIP"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=36097"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36097\/revisions"}],"predecessor-version":[{"id":36098,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36097\/revisions\/36098"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}