Skip to content

Commit 08fceec

Browse files
authored
Merge pull request #577 from jhwisdom/hut101-19-jhwisdom
Hut101 19 jhwisdom
2 parents 87578a4 + fdbc4ed commit 08fceec

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ _Add new changes here_
2222

2323
### Added
2424

25+
- Added Hugging Face model support to ClassifierContainer
2526
- Added `piffle` package as dependency ([#575](https://github.com/maps-as-data/MapReader/pull/575))
2627

2728
## [v1.8.1](https://github.com/Living-with-machines/MapReader/releases/tag/v1.8.1) (2025-08-11)

docs/source/using-mapreader/step-by-step-guide/4-classify/train.rst

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,25 +344,24 @@ There are a number of options for the ``model`` argument:
344344

345345
If you use this option, your optimizer, scheduler and loss function will be loaded from last time.
346346

347-
**4. To load a** `hugging face model <https://huggingface.co/models>`__\ **, choose your model, follow the "Use in Transformers" or "Use in timm" instructions to load it and then pass this as the ``model`` argument.**
347+
**4. To load a** `hugging face model <https://huggingface.co/models>`__\ **, pass the model's repository ID as a string and set ``huggingface=True``.**
348348

349-
e.g. `This model <https://huggingface.co/davanstrien/autotrain-mapreader-5000-40830105612>`__ is based on our `*gold standard* dataset <https://huggingface.co/datasets/Livingwithmachines/MapReader_Data_SIGSPATIAL_2022>`__.
350-
It can be loaded using the `transformers <https://github.com/huggingface/transformers>`__ library:
349+
MapReader will automatically download the model and its corresponding image processor from the Hugging Face Hub using the `transformers <https://github.com/huggingface/transformers>`__ library.
351350

351+
e.g. `This model <https://huggingface.co/davanstrien/autotrain-mapreader-5000-40830105612>`__ is based on our `*gold standard* dataset <https://huggingface.co/datasets/Livingwithmachines/MapReader_Data_SIGSPATIAL_2022>`__.
352+
It can be loaded directly like this:
353+
352354
.. code-block:: python
353355
354356
#EXAMPLE
355357
import torch
356-
from transformers import AutoFeatureExtractor, AutoModelForImageClassification
357-
358358
from mapreader import ClassifierContainer
359359
360-
extractor = AutoFeatureExtractor.from_pretrained("davanstrien/autotrain-mapreader-5000-40830105612")
361-
my_model = AutoModelForImageClassification.from_pretrained("davanstrien/autotrain-mapreader-5000-40830105612")
360+
my_model = "davanstrien/autotrain-mapreader-5000-40830105612"
362361
363362
device = 'cuda' if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'
364363
365-
my_classifier = ClassifierContainer(my_model, annotated_images.labels_map, dataloaders, device=device)
364+
my_classifier = ClassifierContainer(my_model, annotated_images.labels_map, dataloaders, device=device, huggingface=True)
366365
367366
.. note:: You will need to install the `transformers <https://github.com/huggingface/transformers>`__ library to do this (``pip install transformers``).
368367

mapreader/classify/classifier.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ def __init__(
108108
is_inception: bool = False,
109109
load_path: str | None = None,
110110
force_device: bool = False,
111+
huggingface: bool = False,
111112
**kwargs,
112113
):
113114
# set up device
@@ -149,7 +150,31 @@ def __init__(
149150
self.input_size = input_size
150151
self.is_inception = is_inception
151152
elif isinstance(model, str):
152-
self._initialize_model(model, **kwargs)
153+
if huggingface:
154+
try:
155+
from transformers import AutoModelForImageClassification, AutoImageProcessor
156+
except ImportError:
157+
raise ImportError(
158+
"Hugging Face models require the 'transformers' library: 'pip install transformers'."
159+
)
160+
print(f"[INFO] Initializing Hugging Face model: {model}")
161+
num_labels = len(self.labels_map)
162+
self.model = AutoModelForImageClassification.from_pretrained(
163+
model,
164+
num_labels=num_labels,
165+
ignore_mismatched_sizes=True
166+
).to(self.device)
167+
hf_processor = AutoImageProcessor.from_pretrained(model)
168+
size = getattr(hf_processor, "size", {})
169+
if "height" in size and "width" in size:
170+
size = (size["height"], size["width"])
171+
elif "shortest_edge" in size:
172+
size = (size["shortest_edge"], size["shortest_edge"])
173+
else:
174+
size = input_size
175+
self.is_inception = False
176+
else:
177+
self._initialize_model(model, **kwargs)
153178

154179
self.optimizer = None
155180
self.scheduler = None

0 commit comments

Comments
 (0)