Views
No views yet
1import timm
2import torch
3
4model = timm.create_model(
5 'vit_base_patch16_224',
6 in_chans=1,
7 num_classes=0,
8 global_pool='',
9 checkpoint_path="./checkpoint-1199.pth" # must use local path
10)
11
12model.eval()
13
14# for images, need to convert to single channel, 224, and normalize
15
16# transform example:
17# transform = transforms.Compose([
18# transforms.ToTensor(),
19# transforms.Resize((224, 224)),
20# transforms.Grayscale(num_output_channels=1),
21# transforms.Normalize(mean=[0.5], std=[0.5])
22# ])
23x = torch.randn(1, 1, 224, 224)
24with torch.no_grad():
25 features = model.forward_features(x) # shape [1, tokens, embed_dim]
26print(features.shape)
27
28cls_token = features[:, 0]
29patch_tokens = features[:, 1:]1from transformers import AutoModel, AutoImageProcessor
2
3model = AutoModel.from_pretrained("jfang/mars-vit-base-ctx2m")
4image_processor = AutoImageProcessor.from_pretrained("jfang/mars-vit-base-ctx2m")
5
6# Example usage
7from PIL import Image
8image = Image.open("some_image.png").convert("L") # 1-channel
9inputs = image_processor(image, return_tensors="pt")
10
11
12outputs = model(**inputs)