Instructions to use harness-race/opencode-r3 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use harness-race/opencode-r3 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("object-detection", model="harness-race/opencode-r3")# pip install -U transformers accelerate # Load model directly from transformers import AutoImageProcessor, AutoModelForObjectDetection processor = AutoImageProcessor.from_pretrained("harness-race/opencode-r3") model = AutoModelForObjectDetection.from_pretrained("harness-race/opencode-r3", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload train_detr.py with huggingface_hub
Browse files- train_detr.py +8 -6
train_detr.py
CHANGED
|
@@ -25,7 +25,7 @@ def parse_args():
|
|
| 25 |
p.add_argument("--base_model", default="facebook/detr-resnet-50")
|
| 26 |
p.add_argument("--hub_id", default="harness-race/opencode-r3")
|
| 27 |
p.add_argument("--epochs", type=int, default=25)
|
| 28 |
-
p.add_argument("--batch_size", type=int, default=
|
| 29 |
p.add_argument("--lr", type=float, default=1e-4)
|
| 30 |
p.add_argument("--patience", type=int, default=4)
|
| 31 |
p.add_argument("--seed", type=int, default=42)
|
|
@@ -178,7 +178,8 @@ def evaluate():
|
|
| 178 |
for images, anns, sizes, idx in tqdm(val_dl, desc="eval"):
|
| 179 |
enc = processor(images=images, return_tensors="pt")
|
| 180 |
pv = enc["pixel_values"].to(device); pm = enc["pixel_mask"].to(device)
|
| 181 |
-
|
|
|
|
| 182 |
target_sizes = torch.tensor([val_ds.sizes[ii][::-1] for ii in idx.astype(int)] if hasattr(idx,'astype') else [[val_ds.sizes[ii][1], val_ds.sizes[ii][0]] for ii in idx], device=device)
|
| 183 |
# idx is list of ints already
|
| 184 |
sz = [[val_ds.sizes[ii][1], val_ds.sizes[ii][0]] for ii in idx]
|
|
@@ -224,9 +225,10 @@ for epoch in range(1, ARGS.epochs+1):
|
|
| 224 |
enc = processor(images=images, annotations=anns_pp, return_tensors="pt")
|
| 225 |
pv = enc["pixel_values"].to(device); pm = enc["pixel_mask"].to(device)
|
| 226 |
labels = [{k: v.to(device) for k,v in l.items()} for l in enc.get("labels")]
|
| 227 |
-
out = model(pixel_values=pv, pixel_mask=pm, labels=labels)
|
| 228 |
-
loss = out.loss
|
| 229 |
optim.zero_grad()
|
|
|
|
|
|
|
|
|
|
| 230 |
loss.backward()
|
| 231 |
optim.step()
|
| 232 |
ep_loss += loss.item()
|
|
@@ -269,8 +271,8 @@ else:
|
|
| 269 |
best = evaluate() or {}
|
| 270 |
run_outputs["best"] = best
|
| 271 |
|
| 272 |
-
model.push_to_hub(ARGS.hub_id
|
| 273 |
-
processor.push_to_hub(ARGS.hub_id)
|
| 274 |
log("pushed weights + preprocessor")
|
| 275 |
|
| 276 |
# build model card
|
|
|
|
| 25 |
p.add_argument("--base_model", default="facebook/detr-resnet-50")
|
| 26 |
p.add_argument("--hub_id", default="harness-race/opencode-r3")
|
| 27 |
p.add_argument("--epochs", type=int, default=25)
|
| 28 |
+
p.add_argument("--batch_size", type=int, default=4)
|
| 29 |
p.add_argument("--lr", type=float, default=1e-4)
|
| 30 |
p.add_argument("--patience", type=int, default=4)
|
| 31 |
p.add_argument("--seed", type=int, default=42)
|
|
|
|
| 178 |
for images, anns, sizes, idx in tqdm(val_dl, desc="eval"):
|
| 179 |
enc = processor(images=images, return_tensors="pt")
|
| 180 |
pv = enc["pixel_values"].to(device); pm = enc["pixel_mask"].to(device)
|
| 181 |
+
with torch.amp.autocast(device_type="cuda", dtype=torch.float16, enabled=(device.type=="cuda")):
|
| 182 |
+
out = model(pixel_values=pv, pixel_mask=pm)
|
| 183 |
target_sizes = torch.tensor([val_ds.sizes[ii][::-1] for ii in idx.astype(int)] if hasattr(idx,'astype') else [[val_ds.sizes[ii][1], val_ds.sizes[ii][0]] for ii in idx], device=device)
|
| 184 |
# idx is list of ints already
|
| 185 |
sz = [[val_ds.sizes[ii][1], val_ds.sizes[ii][0]] for ii in idx]
|
|
|
|
| 225 |
enc = processor(images=images, annotations=anns_pp, return_tensors="pt")
|
| 226 |
pv = enc["pixel_values"].to(device); pm = enc["pixel_mask"].to(device)
|
| 227 |
labels = [{k: v.to(device) for k,v in l.items()} for l in enc.get("labels")]
|
|
|
|
|
|
|
| 228 |
optim.zero_grad()
|
| 229 |
+
with torch.amp.autocast(device_type="cuda", dtype=torch.float16, enabled=(device.type=="cuda")):
|
| 230 |
+
out = model(pixel_values=pv, pixel_mask=pm, labels=labels)
|
| 231 |
+
loss = out.loss
|
| 232 |
loss.backward()
|
| 233 |
optim.step()
|
| 234 |
ep_loss += loss.item()
|
|
|
|
| 271 |
best = evaluate() or {}
|
| 272 |
run_outputs["best"] = best
|
| 273 |
|
| 274 |
+
model.push_to_hub(ARGS.hub_id)
|
| 275 |
+
processor.push_to_hub(ARGS.hub_id)
|
| 276 |
log("pushed weights + preprocessor")
|
| 277 |
|
| 278 |
# build model card
|