{"id":36205,"date":"2024-11-01T09:46:38","date_gmt":"2024-11-01T09:46:38","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=36205"},"modified":"2024-11-01T09:46:38","modified_gmt":"2024-11-01T09:46:38","slug":"using-hugging-face-transformers-tutorial-sample-image-dataset","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/36205\/","title":{"rendered":"Using Hugging Face Transformers Tutorial, Sample Image Dataset"},"content":{"rendered":"<p>Recently, the Hugging Face library has been widely used in both natural language processing (NLP) and computer vision (CV) fields within artificial intelligence and deep learning. In this article, we will explain how to process image datasets and train models using the Hugging Face Transformers library, and we will explore this in detail with example code.<\/p>\n<h2>1. What are Hugging Face and Transformers?<\/h2>\n<p>Hugging Face is a library that provides various pre-trained models related to natural language processing, making it easy to use models like BERT, GPT-2, and T5. However, recently, image processing models such as Vision Transformer (ViT) and CLIP have been added, which demonstrate strong performance in computer vision tasks as well.<\/p>\n<h2>2. Required Packages and Environment Setup<\/h2>\n<p>Before using the Hugging Face Transformers library, you must first install the required packages. You can easily install them with the following code.<\/p>\n<pre><code>pip install transformers torchvision torch<\/code><\/pre>\n<h2>3. Sample Image Dataset<\/h2>\n<p>In this tutorial, we will use the CIFAR-10 dataset as sample data. CIFAR-10 consists of 60,000 32&#215;32 color images distributed across 10 classes (airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck). This dataset is very suitable for image classification problems.<\/p>\n<h3>3.1 Loading the Dataset<\/h3>\n<p>We will use the torchvision library in Python to load the CIFAR-10 dataset and split it into training and validation sets.<\/p>\n<pre><code>import torchvision\nimport torchvision.transforms as transforms\n\n# Set data transformations\ntransform = transforms.Compose([\n    transforms.ToTensor(),\n    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),\n])\n\n# Load training and validation datasets\ntrainset = torchvision.datasets.CIFAR10(root='.\/data', train=True, download=True, transform=transform)\ntrainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)\n\ntestset = torchvision.datasets.CIFAR10(root='.\/data', train=False, download=True, transform=transform)\ntestloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)\n<\/code><\/pre>\n<h3>3.2 Data Preprocessing<\/h3>\n<p>The loaded dataset needs to undergo a transformation process where the image data is converted to tensor form and normalized. In the code above, the ToTensor() method is used to convert images to tensors, and the Normalize() method performs normalization based on the mean and standard deviation.<\/p>\n<h2>4. Building the Vision Transformer (ViT) Model<\/h2>\n<p>Now we will build the ViT model to classify images from the CIFAR-10 dataset. The model definition can be easily implemented using the Hugging Face Transformers library.<\/p>\n<pre><code>from transformers import ViTForImageClassification, ViTFeatureExtractor\n\n# Initialize ViT model and feature extractor\nfeature_extractor = ViTFeatureExtractor.from_pretrained('google\/vit-base-patch16-224')\nmodel = ViTForImageClassification.from_pretrained('google\/vit-base-patch16-224', num_labels=10)\n<\/code><\/pre>\n<p>The above code initializes the Vision Transformer model, where the `num_labels` parameter is used to set the number of classes. Here, we set it to 10, as we have 10 classes.<\/p>\n<h2>5. Model Training<\/h2>\n<p>To train the model, we need to define the loss function and optimization algorithm. In this case, we will use the CrossEntropyLoss loss function and the Adam optimizer.<\/p>\n<pre><code>import torch.optim as optim\n\n# Define loss function and optimization algorithm\ncriterion = torch.nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# Set device for model training\ndevice = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\nmodel.to(device)\n\n# Train the model\nfor epoch in range(10):  # Train for 10 epochs\n    running_loss = 0.0\n    for i, data in enumerate(trainloader, 0):\n        inputs, labels = data\n        inputs, labels = inputs.to(device), labels.to(device)\n\n        # Zero the gradients\n        optimizer.zero_grad()\n\n        # Forward + backward + optimize\n        outputs = model(inputs).logits\n        loss = criterion(outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        running_loss += loss.item()\n        if i % 2000 == 1999:  # Print every 2000 mini-batches\n            print(f\"[{epoch + 1}, {i + 1}] loss: {running_loss \/ 2000:.3f}\")\n            running_loss = 0.0\n<\/code><\/pre>\n<p>The above code represents the process of training the model according to the number of epochs and mini-batches. It calculates the loss for each batch and updates the weights through backpropagation.<\/p>\n<h2>6. Model Evaluation<\/h2>\n<p>To evaluate the trained model, we will use the test dataset. The method for evaluating the model&#8217;s accuracy is as follows.<\/p>\n<pre><code>correct = 0\ntotal = 0\n\nwith torch.no_grad():\n    for data in testloader:\n        images, labels = data\n        images, labels = images.to(device), labels.to(device)\n        outputs = model(images).logits\n        _, predicted = torch.max(outputs.data, 1)\n        total += labels.size(0)\n        correct += (predicted == labels).sum().item()\n\nprint(f'Accuracy of the network on the 10000 test images: {100 * correct \/ total:.2f}%')\n<\/code><\/pre>\n<p>The above code calculates the model&#8217;s accuracy on the test dataset. It compares the predicted values against the actual labels for each image to compute the accuracy.<\/p>\n<h2>7. Conclusion<\/h2>\n<p>In this article, we explored how to process CIFAR-10 data using the Hugging Face Transformers library and how to build a Vision Transformer model to classify images. Utilizing the Hugging Face library allows for easily constructing complex models and optimizing performance with various datasets. We encourage you to continue exploring deep learning use cases with diverse models and datasets in the future.<\/p>\n<p>If you have any questions or need additional information, please feel free to leave a comment.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently, the Hugging Face library has been widely used in both natural language processing (NLP) and computer vision (CV) fields within artificial intelligence and deep learning. In this article, we will explain how to process image datasets and train models using the Hugging Face Transformers library, and we will explore this in detail with example &hellip; <a href=\"https:\/\/atmokpo.com\/w\/36205\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Using Hugging Face Transformers Tutorial, Sample Image Dataset&#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-36205","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>Using Hugging Face Transformers Tutorial, Sample Image Dataset - \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\/36205\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Hugging Face Transformers Tutorial, Sample Image Dataset - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Recently, the Hugging Face library has been widely used in both natural language processing (NLP) and computer vision (CV) fields within artificial intelligence and deep learning. In this article, we will explain how to process image datasets and train models using the Hugging Face Transformers library, and we will explore this in detail with example &hellip; \ub354 \ubcf4\uae30 &quot;Using Hugging Face Transformers Tutorial, Sample Image Dataset&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/36205\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:46:38+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\/36205\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36205\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Using Hugging Face Transformers Tutorial, Sample Image Dataset\",\"datePublished\":\"2024-11-01T09:46:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36205\/\"},\"wordCount\":520,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Using Hugging Face\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/36205\/\",\"url\":\"https:\/\/atmokpo.com\/w\/36205\/\",\"name\":\"Using Hugging Face Transformers Tutorial, Sample Image Dataset - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:46:38+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/36205\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/36205\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/36205\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Hugging Face Transformers Tutorial, Sample Image Dataset\"}]},{\"@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":"Using Hugging Face Transformers Tutorial, Sample Image Dataset - \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\/36205\/","og_locale":"ko_KR","og_type":"article","og_title":"Using Hugging Face Transformers Tutorial, Sample Image Dataset - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Recently, the Hugging Face library has been widely used in both natural language processing (NLP) and computer vision (CV) fields within artificial intelligence and deep learning. In this article, we will explain how to process image datasets and train models using the Hugging Face Transformers library, and we will explore this in detail with example &hellip; \ub354 \ubcf4\uae30 \"Using Hugging Face Transformers Tutorial, Sample Image Dataset\"","og_url":"https:\/\/atmokpo.com\/w\/36205\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:46:38+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\/36205\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/36205\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Using Hugging Face Transformers Tutorial, Sample Image Dataset","datePublished":"2024-11-01T09:46:38+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/36205\/"},"wordCount":520,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Using Hugging Face"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/36205\/","url":"https:\/\/atmokpo.com\/w\/36205\/","name":"Using Hugging Face Transformers Tutorial, Sample Image Dataset - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:46:38+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/36205\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/36205\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/36205\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Using Hugging Face Transformers Tutorial, Sample Image Dataset"}]},{"@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\/36205","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=36205"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36205\/revisions"}],"predecessor-version":[{"id":36206,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/36205\/revisions\/36206"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=36205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=36205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=36205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}