HuggingFace镜像/Human-Action-Recognition-VIT-Base-patch16-224
模型介绍文件和版本分析
下载使用量0

Human-Action-Recognition-VIT-Base-patch16-224

该模型是 google/vit-base-patch16-224 在 Bingsu/Human_Action_Recognition 数据集上的微调版本。
其在评估集上取得了以下结果:

  • 损失:0.4005
  • 准确率:0.8786

模型说明

视觉Transformer(ViT)是一种Transformer编码器模型(类BERT),在大量图像集合上以监督方式进行预训练,即ImageNet-21k,分辨率为224x224像素。随后,该模型在ImageNet(也称为ILSVRC2012)上进行微调,该数据集包含100万张图像和1000个类别,分辨率同样为224x224。

图像以固定大小的补丁序列(分辨率16x16)形式输入模型,并进行线性嵌入。为用于分类任务,还会在序列开头添加一个[CLS]标记。在将序列输入Transformer编码器各层之前,还会添加绝对位置嵌入。

通过预训练模型,其学习到图像的内部表示,进而可用于提取对下游任务有用的特征:例如,若有带标签的图像数据集,可在预训练编码器之上放置一个线性层来训练标准分类器。通常将线性层置于[CLS]标记之上,因为该标记的最后隐藏状态可视为整个图像的表示。

预期用途与局限性

您可以将该模型用于图像分类。

使用方法

以下是使用此模型将人类动作图像分类为以下类别之一的方法:
calling, clapping, cycling, dancing, drinking, eating, fighting, hugging, laughing, listening_to_music, running, sitting, sleeping, texting, using_laptop

from transformers import ViTImageProcessor, ViTForImageClassification
from PIL import Image
import numpy as np
import requests
import torch
import torch_npu
import os
import argparse
from openmind import pipeline, is_torch_npu_available

if is_torch_npu_available():
    device = "npu:0"
else:
    device = "cpu"

parser = argparse.ArgumentParser()
parser.add_argument("--model_name_or_path", type=str, default="./")
args = parser.parse_args()
model_path = args.model_name_or_path

image = Image.open("./pexels-photo-175658.jpg")
tensor_img = torch.from_numpy(np.array(image)).permute(2, 0, 1).float()/255.0

processor = ViTImageProcessor.from_pretrained('./')
model = ViTForImageClassification.from_pretrained(model_path, torch_dtype=torch.float16)

model = model.to(device)

inputs = processor(images=tensor_img.npu(), return_tensors="pt")
inputs = inputs.to(device)
outputs = model(**inputs)

logits = outputs.logits
# model predicts one of the 1000 ImageNet classes
predicted_class_idx = logits.argmax(-1).item()
print("Predicted class:", model.config.id2label[predicted_class_idx])

训练与评估数据

需要更多信息

训练过程

训练超参数

训练过程中使用了以下超参数:

  • learning_rate: 5e-05
  • train_batch_size: 64
  • eval_batch_size: 64
  • seed: 42
  • gradient_accumulation_steps: 4
  • total_train_batch_size: 256
  • optimizer: Adam,参数betas=(0.9,0.999),epsilon=1e-08
  • lr_scheduler_type: linear
  • lr_scheduler_warmup_ratio: 0.1
  • num_epochs: 20

训练结果

训练损失轮次步数验证损失准确率
2.63960.99392.04360.4425
1.45792.0790.75530.7917
0.83422.991180.52960.8417
0.66494.01580.49780.8496
0.61374.991970.44600.8595
0.53746.02370.43560.8627
0.5146.992760.43490.8615
0.4758.03160.40050.8786
0.46638.993550.41640.8659
0.417810.03950.41280.8738
0.422610.994340.41150.8690
0.389612.04740.41120.875
0.386612.995130.40720.8714
0.363214.05530.41060.8718
0.359614.995920.40430.8714
0.342116.06320.41280.8675
0.34416.996710.41810.8643
0.344718.07110.41280.8687
0.340718.997500.40970.8714
0.326719.757800.40970.8683

框架版本

  • Transformers 4.35.2
  • Pytorch 2.1.0+cu118
  • Datasets 2.15.0
  • Tokenizers 0.15.0

微调脚本

Google Colaboratory Notebook