Skip to content

Add PyTorchModelHubMixin to enable downloads on HF #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 4 additions & 26 deletions hubconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
PyTorch Hub configuration for AnySat model.
"""

import os
import sys
import torch
import torch.nn as nn
from pathlib import Path
import warnings

from huggingface_hub import PyTorchModelHubMixin

REPO_ROOT = Path(__file__).parent
if str(REPO_ROOT / "src") not in sys.path:
sys.path.append(str(REPO_ROOT / "src"))

dependencies = ['torch']

class AnySat(nn.Module):
class AnySat(nn.Module, PyTorchModelHubMixin, repo_url="https://github.com/gastruc/AnySat", pipeline_tag="image-feature-extraction",
tags=["image-feature-extraction"], license="mit"):
"""
AnySat: Earth Observation Model for Any Resolutions, Scales, and Modalities

Expand Down Expand Up @@ -80,29 +81,6 @@ def __init__(self, model_size='base', flash_attn=True, **kwargs):
if device is not None:
self.model = self.model.to(device)

@classmethod
def from_pretrained(cls, model_size='base', **kwargs):
"""
Create a pretrained AnySat model

Args:
model_size (str): Model size - 'tiny', 'small', or 'base'
**kwargs: Additional arguments passed to the constructor
"""
model = cls(model_size=model_size, **kwargs)

checkpoint_urls = {
'base': 'https://huggingface.co/g-astruc/AnySat/resolve/main/models/AnySat.pth',
# 'small': 'https://huggingface.co/gastruc/anysat/resolve/main/anysat_small_geoplex.pth', COMING SOON
# 'tiny': 'https://huggingface.co/gastruc/anysat/resolve/main/anysat_tiny_geoplex.pth' COMING SOON
}

checkpoint_url = checkpoint_urls[model_size]
state_dict = torch.hub.load_state_dict_from_url(checkpoint_url, progress=True)['state_dict']

model.model.load_state_dict(state_dict)
return model

def forward(self, x, patch_size, output='patch', **kwargs):
assert output in ['patch', 'tile', 'dense', 'all'], "Output must be one of 'patch', 'tile', 'dense', 'all'"
sizes = {}
Expand Down