BlakePeavy commited on
Commit
ba3b7b4
·
verified ·
1 Parent(s): 3124de5

Fix: README.md - Colab-ready instructions + suppress char/digit/country/stl labels

Browse files
Files changed (1) hide show
  1. README.md +44 -12
README.md CHANGED
@@ -101,12 +101,17 @@ A single fine-tuned **ConvNeXt-Small** model that identifies a wide range of sub
101
 
102
  ## Quick Start
103
 
 
 
104
  ```python
 
105
  from PIL import Image
106
- from photo_identifier import PhotoIdentifierModel, PhotoIdentifierConfig
107
 
108
- # Load from HuggingFace Hub
109
- model = PhotoIdentifierModel.from_pretrained("BlakePeavy/photo-identifier-v3")
 
 
 
110
  model.eval()
111
 
112
  # Run inference
@@ -126,40 +131,67 @@ for label, score in results:
126
 
127
  ### Using `transformers` pipeline
128
 
 
 
 
129
  ```python
130
- from transformers import pipeline
131
 
 
 
 
 
 
132
  pipe = pipeline(
133
  "image-classification",
134
  model="BlakePeavy/photo-identifier-v3",
 
135
  trust_remote_code=True,
136
  )
137
  results = pipe("my_photo.jpg", top_k=5)
 
 
138
  ```
139
 
140
  ---
141
 
142
  ## Loading the Model Manually
143
 
 
 
 
 
 
144
  ```python
 
145
  import torch
146
  import json
147
  from torchvision import models, transforms
 
 
148
  from PIL import Image
149
 
150
- # Load metadata
151
- with open("config.json") as f:
 
 
 
 
 
 
152
  cfg = json.load(f)
153
- classes = cfg["id2label"] # {0: "class_name", ...}
154
 
155
- # Rebuild the backbone
156
- model = models.convnext_small()
157
  in_f = model.classifier[-1].in_features
158
  model.classifier[-1] = torch.nn.Linear(in_f, len(classes))
159
 
160
- # Load EMA weights
161
- ck = torch.load("pytorch_model.bin", map_location="cpu", weights_only=False)
162
- model.load_state_dict(ck)
 
 
163
  model.eval()
164
 
165
  # Preprocess
 
101
 
102
  ## Quick Start
103
 
104
+ > **Google Colab / fresh environment:** run `!pip install -q transformers torchvision safetensors Pillow huggingface_hub` first.
105
+
106
  ```python
107
+ from transformers import AutoModelForImageClassification
108
  from PIL import Image
 
109
 
110
+ # Load from HuggingFace Hub (trust_remote_code required for custom backbone)
111
+ model = AutoModelForImageClassification.from_pretrained(
112
+ "BlakePeavy/photo-identifier-v3",
113
+ trust_remote_code=True,
114
+ )
115
  model.eval()
116
 
117
  # Run inference
 
131
 
132
  ### Using `transformers` pipeline
133
 
134
+ The image processor must be loaded explicitly because this model uses a
135
+ custom `model_type` not registered in the default transformers auto-registry.
136
+
137
  ```python
138
+ from transformers import pipeline, AutoImageProcessor
139
 
140
+ # Load the image processor from the repo's preprocessor_config.json
141
+ processor = AutoImageProcessor.from_pretrained(
142
+ "BlakePeavy/photo-identifier-v3",
143
+ use_fast=False,
144
+ )
145
  pipe = pipeline(
146
  "image-classification",
147
  model="BlakePeavy/photo-identifier-v3",
148
+ image_processor=processor,
149
  trust_remote_code=True,
150
  )
151
  results = pipe("my_photo.jpg", top_k=5)
152
+ for r in results:
153
+ print(f"{r['score']:.1%} {r['label']}")
154
  ```
155
 
156
  ---
157
 
158
  ## Loading the Model Manually
159
 
160
+ Useful when you want plain PyTorch with no `transformers` dependency.
161
+ The weights are stored as `model.safetensors` (not `pytorch_model.bin`).
162
+ Keys have a `convnext.` prefix that must be stripped before loading into
163
+ a bare `torchvision.models.convnext_small`.
164
+
165
  ```python
166
+ # !pip install -q torch torchvision safetensors Pillow huggingface_hub
167
  import torch
168
  import json
169
  from torchvision import models, transforms
170
+ from safetensors.torch import load_file
171
+ from huggingface_hub import hf_hub_download
172
  from PIL import Image
173
 
174
+ REPO = "BlakePeavy/photo-identifier-v3"
175
+
176
+ # Download model files
177
+ config_path = hf_hub_download(REPO, "config.json")
178
+ weights_path = hf_hub_download(REPO, "model.safetensors")
179
+
180
+ # Load label map
181
+ with open(config_path) as f:
182
  cfg = json.load(f)
183
+ classes = cfg["id2label"] # {"0": "class_name", ...}
184
 
185
+ # Rebuild the backbone (weights=None — we load from safetensors below)
186
+ model = models.convnext_small(weights=None)
187
  in_f = model.classifier[-1].in_features
188
  model.classifier[-1] = torch.nn.Linear(in_f, len(classes))
189
 
190
+ # Load from safetensors — strip the "convnext." wrapper prefix
191
+ sd = load_file(weights_path)
192
+ sd = {k.replace("convnext.", "", 1): v for k, v in sd.items()
193
+ if k.startswith("convnext.")}
194
+ model.load_state_dict(sd)
195
  model.eval()
196
 
197
  # Preprocess