Feature Extraction
Transformers
Safetensors
English
lessvit
hyperspectral
remote-sensing
geospatial
vision-transformer
custom_code
Instructions to use GFM-Bench/LESSViT-Base with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use GFM-Bench/LESSViT-Base with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="GFM-Bench/LESSViT-Base", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("GFM-Bench/LESSViT-Base", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download hyperspectral_patch_embed.py from GFM-Bench/LESSViT-Base: direct link, hf CLI and curl.
- Browser
- Download file 1.78 kB
-
https://huggingface.co/GFM-Bench/LESSViT-Base/resolve/main/hyperspectral_patch_embed.py
- Command line
-
hf download hf://GFM-Bench/LESSViT-Base/hyperspectral_patch_embed.py
-
curl -L -o hyperspectral_patch_embed.py https://huggingface.co/GFM-Bench/LESSViT-Base/resolve/main/hyperspectral_patch_embed.py
1.78 kB
| import torch | |
| import torch.nn as nn | |
| import numpy as np | |
| class HyperspectralPatchEmbed(nn.Module): | |
| """Image to Patch Embedding.""" | |
| def __init__( | |
| self, | |
| patch_size: int = 16, | |
| embed_dim: int = 768, | |
| ): | |
| super().__init__() | |
| self.patch_size = patch_size | |
| # self.proj = nn.Conv3d( | |
| # 1, | |
| # embed_dim, | |
| # kernel_size=(1, patch_size, patch_size), | |
| # stride=(1, patch_size, patch_size), | |
| # ) | |
| self.proj = nn.Sequential( | |
| nn.Conv3d( | |
| 1, | |
| embed_dim*2, #2 | |
| kernel_size=(1, patch_size, patch_size), | |
| stride=(1, patch_size, patch_size), | |
| ), | |
| nn.ReLU(), | |
| nn.Conv3d( | |
| embed_dim*2, | |
| embed_dim, | |
| kernel_size=(1, 1, 1), | |
| stride=(1, 1, 1), | |
| ), | |
| ) | |
| self.initialize_weights() | |
| def initialize_weights(self): | |
| try: | |
| w = self.proj.weight.data | |
| torch.nn.init.xavier_uniform_(w.view([w.shape[0], -1])) | |
| except: | |
| w1 = self.proj[0].weight.data | |
| w2 = self.proj[2].weight.data | |
| torch.nn.init.xavier_uniform_(w1.view([w1.shape[0], -1])) | |
| torch.nn.init.xavier_uniform_(w2.view([w2.shape[0], -1])) | |
| def forward(self, x): | |
| # x: B, Cin, H, W | |
| assert x.shape[2] % self.patch_size == 0, "image height should be divisible by patch size" | |
| assert x.shape[3] % self.patch_size == 0, "image width should be divisible by patch size" | |
| x = self.proj(x.unsqueeze(1)) # B, D, C, H, W | |
| B, D, C, H, W = x.shape | |
| return x.permute(0, 2, 3, 4, 1).reshape(B, C, H*W, D) # B, C, H, W, D | |