zynapse commited on
Commit
d3a2bf3
·
verified ·
1 Parent(s): e5ad5e7

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -17
app.py CHANGED
@@ -74,10 +74,14 @@ class PlantClassifier:
74
  'hf_repo_id': 'zynapse/dinov3-dann-plantclef',
75
  'hf_filename': 'dino_dann.pth',
76
  'hf_token_env': 'DANN_TOKEN',
77
- # Baseline backbone from HF
78
- 'hf_baseline_repo_id': 'zynapse/dinov3-base-dann',
79
- 'hf_baseline_filename': 'backbone_model_dann.pth',
80
- 'hf_baseline_token_env': 'BASELINE_TOKEN',
 
 
 
 
81
  'loader': 'external_module',
82
  'external_type': 'dann',
83
  'display_name': 'DINOv3 DANN',
@@ -492,13 +496,10 @@ class PlantClassifier:
492
  except Exception as e:
493
  print(f"⚠️ Drive download/load failed: {e}")
494
 
495
- # 2c. Fallback: HF baseline file if available
496
  base_obj = None if 'base_obj' not in locals() else base_obj
497
  if backbone is None and not base_obj:
498
- if 'baseline_path' in config and os.path.exists(config['baseline_path']):
499
- base_obj = torch.load(config['baseline_path'], map_location=self.device, weights_only=False)
500
- else:
501
- raise RuntimeError("DANN provided as state_dict but no usable backbone source found. Provide hub_repo+hub_weights_url or drive_weights_url or baseline_path.")
502
 
503
  # Resolve baseline backbone module
504
  if backbone is None:
@@ -557,17 +558,37 @@ class PlantClassifier:
557
  nn.Linear(512, num_domains)
558
  )
559
  def forward(self, x):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560
  if hasattr(self.backbone, 'forward_features'):
561
  outputs = self.backbone.forward_features(x)
562
- if isinstance(outputs, dict) and 'x_norm_clstoken' in outputs:
563
- feats = outputs['x_norm_clstoken']
564
- elif isinstance(outputs, torch.Tensor):
565
- feats = outputs
566
- else:
567
- # attempt common key
568
- feats = outputs.get('x_norm_clstoken', next(iter(outputs.values())))
569
  else:
570
- feats = self.backbone(x)
 
571
  class_logits = self.fc(feats)
572
  domain_logits = self.domain_fc(self.grl(feats))
573
  return class_logits, domain_logits, feats
 
74
  'hf_repo_id': 'zynapse/dinov3-dann-plantclef',
75
  'hf_filename': 'dino_dann.pth',
76
  'hf_token_env': 'DANN_TOKEN',
77
+ # Backbone via torch.hub or direct URL (no Hugging Face dependency)
78
+ # Fill these with your actual repo and direct weights URL
79
+ # Example:
80
+ # 'hub_repo': 'AUTHOR/REPO_NAME',
81
+ # 'hub_entry': 'dinov3_vitl16',
82
+ # 'hub_weights_url': 'https://drive.usercontent.google.com/download?id=YOUR_ID&export=download',
83
+ # Or provide only a direct URL:
84
+ # 'drive_weights_url': 'https://drive.usercontent.google.com/download?id=YOUR_ID&export=download',
85
  'loader': 'external_module',
86
  'external_type': 'dann',
87
  'display_name': 'DINOv3 DANN',
 
496
  except Exception as e:
497
  print(f"⚠️ Drive download/load failed: {e}")
498
 
499
+ # 2c. No HF fallback: require at least hub or drive source
500
  base_obj = None if 'base_obj' not in locals() else base_obj
501
  if backbone is None and not base_obj:
502
+ raise RuntimeError("DANN provided as state_dict but no usable backbone source found. Provide hub_repo+hub_weights_url or drive_weights_url.")
 
 
 
503
 
504
  # Resolve baseline backbone module
505
  if backbone is None:
 
558
  nn.Linear(512, num_domains)
559
  )
560
  def forward(self, x):
561
+ def select_feats(out):
562
+ # Robustly select CLS token features as a 2D tensor (B, C)
563
+ if isinstance(out, dict):
564
+ if 'x_norm_clstoken' in out:
565
+ t = out['x_norm_clstoken']
566
+ return t
567
+ # Common variants: tokens with CLS at index 0
568
+ for k in ['x_norm_tokens', 'tokens', 'last_tokens']:
569
+ if k in out:
570
+ t = out[k]
571
+ if isinstance(t, torch.Tensor) and t.dim() == 3:
572
+ return t[:, 0, :]
573
+ # Fallback to first value in dict
574
+ v = next(iter(out.values()))
575
+ if isinstance(v, torch.Tensor) and v.dim() == 3:
576
+ return v[:, 0, :]
577
+ return v
578
+ if isinstance(out, torch.Tensor):
579
+ if out.dim() == 3:
580
+ return out[:, 0, :]
581
+ if out.dim() == 2:
582
+ return out
583
+ return out.view(out.size(0), -1)
584
+ return out
585
+
586
  if hasattr(self.backbone, 'forward_features'):
587
  outputs = self.backbone.forward_features(x)
588
+ feats = select_feats(outputs)
 
 
 
 
 
 
589
  else:
590
+ outputs = self.backbone(x)
591
+ feats = select_feats(outputs)
592
  class_logits = self.fc(feats)
593
  domain_logits = self.domain_fc(self.grl(feats))
594
  return class_logits, domain_logits, feats