yellowdolphin commited on
Commit
d8877a5
·
1 Parent(s): 6677709

download models via hf_hub_download

Browse files
Files changed (2) hide show
  1. app.py +12 -11
  2. utils.py +5 -6
app.py CHANGED
@@ -14,8 +14,9 @@ from subprocess import run
14
  import json
15
 
16
  import gradio as gr
 
17
  from yolov5 import detect
18
- from utils import get_models, get_cfg, get_embeddings, get_comp_embeddings, get_test_embedding
19
 
20
 
21
  # YOLOv5 parameters
@@ -25,8 +26,8 @@ score_thr = 0.025
25
  iou_thr = 0.6
26
  max_det = 1
27
  working = Path(os.getcwd())
28
- modelbox = working / 'models'
29
- checkpoint_files = [modelbox / f'yolov5_l6_{yolo_input_size}_fold{x}.pt' for x in versions]
30
  image_root = working / 'images'
31
 
32
 
@@ -38,10 +39,11 @@ normalize_similarity = None # test-train, None
38
  gamma = 0.4
39
  threshold = 0.09951 if (normalize_similarity == 'test-train') else 0.6 # 0.381
40
  knn = 300
41
- emb_path = '/kaggle/input/happywhale-embeddings'
42
- rst_path = '/kaggle/input/happywhale-models'
43
- rst_files = sorted(glob(f'{rst_path}/*.h5'))
44
- n_models = len(rst_files)
 
45
 
46
 
47
  def fast_yolo_crop(image):
@@ -74,14 +76,13 @@ def fast_yolo_crop(image):
74
 
75
 
76
  # Preload embeddings for known individuals
77
- comp_embeddings = get_comp_embeddings(rst_files)
78
 
79
  # Preload embedding models, input sizes
80
  K.clear_session()
81
  embed_models, sizes = [], []
82
- for rst_file in rst_files:
83
- cfg = get_cfg(rst_file)
84
- npz_file = Path(rst_file.replace('.h5', '_emb.npz')).name
85
  assert cfg.FOLD_TO_RUN == use_fold[npz_file]
86
  cfg.pretrained = None # avoid weight downloads
87
  if isinstance(cfg.IMAGE_SIZE, int):
 
14
  import json
15
 
16
  import gradio as gr
17
+ from huggingface_hub import hf_hub_download
18
  from yolov5 import detect
19
+ from utils import get_model, get_cfg, get_embeddings, get_comp_embeddings, get_test_embedding
20
 
21
 
22
  # YOLOv5 parameters
 
26
  iou_thr = 0.6
27
  max_det = 1
28
  working = Path(os.getcwd())
29
+ modelbox = "yellowdolphin/happywhale-models"
30
+ checkpoint_files = [hf_hub_download(modelbox, f'yolov5_l6_{yolo_input_size}_fold{x}.pt') for x in versions]
31
  image_root = working / 'images'
32
 
33
 
 
39
  gamma = 0.4
40
  threshold = 0.09951 if (normalize_similarity == 'test-train') else 0.6 # 0.381
41
  knn = 300
42
+ rst_names = 'convnext_base_384_in22ft1k_colab220 efnv1b7_colab216 hub_efnv2xl_v73'.split()
43
+ cfg_files = [hf_hub_download(modelbox, f'{x}_config.json') for x in rst_names]
44
+ emb_files = [hf_hub_download(modelbox, f'{x}_emb.npz') for x in rst_names]
45
+ rst_files = [hf_hub_download(modelbox, f'{x}.h5') for x in rst_names]
46
+ n_models = len(rst_names)
47
 
48
 
49
  def fast_yolo_crop(image):
 
76
 
77
 
78
  # Preload embeddings for known individuals
79
+ comp_embeddings = get_comp_embeddings(emb_files)
80
 
81
  # Preload embedding models, input sizes
82
  K.clear_session()
83
  embed_models, sizes = [], []
84
+ for cfg_file, rst_file, npz_file in zip (cfg_files, rst_files, emb_files):
85
+ cfg = get_cfg(cfg_file)
 
86
  assert cfg.FOLD_TO_RUN == use_fold[npz_file]
87
  cfg.pretrained = None # avoid weight downloads
88
  if isinstance(cfg.IMAGE_SIZE, int):
utils.py CHANGED
@@ -19,8 +19,8 @@ class DotDict(dict):
19
  __delattr__ = dict.__delitem__
20
 
21
 
22
- def get_cfg(rst_file):
23
- json_file = str(rst_file).replace('.h5', '_config.json')
24
  config_dict = json.load(open(json_file))
25
  return DotDict(config_dict)
26
 
@@ -77,15 +77,14 @@ use_fold = {
77
  }
78
 
79
 
80
- def get_comp_embeddings(rst_files):
81
  "Load embeddings for competition images [n_images, embedding_size]"
82
 
83
  comp_embeddings = []
84
 
85
- for rst_file in rst_files:
86
  # Get embeddings for all competition images
87
- npz_file = Path(rst_file.replace('.h5', '_emb.npz')).name
88
- d = np.load(str(Path(emb_path) / npz_file))
89
  comp_train_emb = d['train']
90
  comp_test_emb = d['test']
91
 
 
19
  __delattr__ = dict.__delitem__
20
 
21
 
22
+ def get_cfg(json_file):
23
+ json_file = str(json_file)
24
  config_dict = json.load(open(json_file))
25
  return DotDict(config_dict)
26
 
 
77
  }
78
 
79
 
80
+ def get_comp_embeddings(emb_files):
81
  "Load embeddings for competition images [n_images, embedding_size]"
82
 
83
  comp_embeddings = []
84
 
85
+ for npz_file in emb_files:
86
  # Get embeddings for all competition images
87
+ d = np.load(str(npz_file))
 
88
  comp_train_emb = d['train']
89
  comp_test_emb = d['test']
90