hf_mirrors/cvt-21-384-22k
Model Introduction
Files & versions
Analysis

Convolutional Vision Transformer (CvT)

CvT-21 model pre-trained on ImageNet-22k and fine-tuned on ImageNet-1k at resolution 384x384. It was introduced in the paper CvT: Introducing Convolutions to Vision Transformers by Wu et al. and first released in this repository.

Disclaimer: The team releasing CvT did not write a model card for this model so this model card has been written by the Hugging Face team.

Usage

Here is how to use this model to classify an image of the COCO 2017 dataset into one of the 1,000 ImageNet classes:

from openmind import AutoFeatureExtractor
from transformers  import CvtForImageClassification
from openmind import is_torch_npu_available
from openmind_hub import snapshot_download
from PIL import Image
import requests
import warnings
import argparse

def parse_args(): 
	parser = argparse.ArgumentParser() 
	parser.add_argument( "--model_name_or_path", type=str, help="Path to model", default=None) 
	args = parser.parse_args() 
	return args


def main():
    warnings.filterwarnings("ignore")
    url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
    image = Image.open(requests.get(url, stream=True).raw)
    device = "npu" if is_torch_npu_available()  else  "cpu"
    args = parse_args()
    if args.model_name_or_path:
	    model_path = args.model_name_or_path
    else:
	    model_path = snapshot_download(
		"ChongqingAscend/cvt-21-384-22k",
		revision="main",
		resume_download=True,
		ignore_patterns=["*.h5", "*.ot", "*.msgpack"]
	)
    feature_extractor = AutoFeatureExtractor.from_pretrained(model_path)
    model = CvtForImageClassification.from_pretrained(model_path).to(device)
    inputs = feature_extractor(images=image, return_tensors="pt").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])


if __name__ == "__main__":
    main()