Philipp Allgeuer commited on
Commit
2f3566f
·
1 Parent(s): d860b7c

First working demo

Browse files
Files changed (3) hide show
  1. app.py +13 -6
  2. app_novic.py +32 -11
  3. novic +1 -1
app.py CHANGED
@@ -9,6 +9,10 @@ import PIL.Image
9
  import gradio as gr
10
  import app_novic
11
 
 
 
 
 
12
  # Model checkpoints
13
  MODEL_CHECKPOINTS = {
14
  'NOVIC SigLIP B/16 FT2': os.path.join('ovod_20240610_105233', 'ovod_chunk0899_20240612_005748.train'),
@@ -16,17 +20,20 @@ MODEL_CHECKPOINTS = {
16
  'NOVIC DFN-5B H/14-378 FT2': os.path.join('ovod_20240620_162925', 'ovod_chunk0899_20240621_202727.train'),
17
  'NOVIC DFN-5B H/14-378 FT0': os.path.join('ovod_20240628_142131', 'ovod_chunk0433_20240630_235415.train'),
18
  }
 
19
 
20
- # Sample images
21
- IMAGE_EXTS = ('jpg', 'jpeg', 'png', 'webp')
22
- SAMPLE_IMAGES = sorted(image_path for image_ext in IMAGE_EXTS for image_path in glob.glob(os.path.join('sample_images', f'*.{image_ext}')))
 
 
 
23
 
24
  # Classify an image
25
  def classify_image(image: Optional[PIL.Image.Image], model: Optional[str]) -> dict[str, float]:
26
  if image is None or model is None:
27
  return {}
28
- checkpoint = os.path.join('checkpoints', MODEL_CHECKPOINTS[model])
29
- return app_novic.classify_image(image=image, checkpoint=checkpoint)
30
 
31
  # Gradio UI
32
  with gr.Blocks(
@@ -43,7 +50,7 @@ with gr.Blocks(
43
  with gr.Column(scale=1):
44
  input_model = gr.Dropdown(
45
  choices=list(MODEL_CHECKPOINTS),
46
- value='NOVIC DFN-5B H/14-378 FT0',
47
  type='value',
48
  multiselect=False,
49
  allow_custom_value=False,
 
9
  import gradio as gr
10
  import app_novic
11
 
12
+ # Sample images
13
+ IMAGE_EXTS = ('jpg', 'jpeg', 'png', 'webp')
14
+ SAMPLE_IMAGES = sorted(image_path for image_ext in IMAGE_EXTS for image_path in glob.glob(os.path.join('sample_images', f'*.{image_ext}')))
15
+
16
  # Model checkpoints
17
  MODEL_CHECKPOINTS = {
18
  'NOVIC SigLIP B/16 FT2': os.path.join('ovod_20240610_105233', 'ovod_chunk0899_20240612_005748.train'),
 
20
  'NOVIC DFN-5B H/14-378 FT2': os.path.join('ovod_20240620_162925', 'ovod_chunk0899_20240621_202727.train'),
21
  'NOVIC DFN-5B H/14-378 FT0': os.path.join('ovod_20240628_142131', 'ovod_chunk0433_20240630_235415.train'),
22
  }
23
+ DEFAULT_MODEL = 'NOVIC DFN-5B H/14-378 FT0'
24
 
25
+ # Get a checkpoint path
26
+ def get_checkpoint(model: str) -> str:
27
+ return os.path.join('checkpoints', MODEL_CHECKPOINTS[model])
28
+
29
+ # Ensure the default model is preloaded
30
+ app_novic.get_model(checkpoint=get_checkpoint(model=DEFAULT_MODEL))
31
 
32
  # Classify an image
33
  def classify_image(image: Optional[PIL.Image.Image], model: Optional[str]) -> dict[str, float]:
34
  if image is None or model is None:
35
  return {}
36
+ return app_novic.classify_image(image=image, checkpoint=get_checkpoint(model=model))
 
37
 
38
  # Gradio UI
39
  with gr.Blocks(
 
50
  with gr.Column(scale=1):
51
  input_model = gr.Dropdown(
52
  choices=list(MODEL_CHECKPOINTS),
53
+ value=DEFAULT_MODEL,
54
  type='value',
55
  multiselect=False,
56
  allow_custom_value=False,
app_novic.py CHANGED
@@ -2,11 +2,10 @@
2
 
3
  # Imports
4
  import sys
5
- import random # TODO: TEMP
6
  import pathlib
7
  import subprocess
8
  import PIL.Image
9
- # TODO: Then start coding infer.py (ANY additional import has to be clarified with novic_spaces/requirements.txt (pip install -r requirements.txt) and novic/requirements-infer.txt
10
 
11
  # Ensure access to NOVIC code
12
  REPO_ROOT = pathlib.Path(__file__).parent.resolve()
@@ -20,16 +19,38 @@ if not NOVIC_TEST.exists():
20
  if str(NOVIC_DIR) not in sys.path:
21
  sys.path.insert(0, str(NOVIC_DIR))
22
 
23
- # NOVIC imports
24
- import infer # TODO: TEMP
25
- print(infer) # TODO: TEMP
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
27
  # Classify an image
28
  def classify_image(image: PIL.Image.Image, checkpoint: str) -> dict[str, float]:
29
- # TODO: TEMP
30
- print(image, checkpoint)
31
- labels = ('thing', 'item', 'stuff', 'object', 'entity')
32
- probs = [random.random() for _ in labels]
33
- prob_sum = sum(probs)
34
- return {label: prob / prob_sum for label, prob in zip(labels, probs)}
35
  # EOF
 
2
 
3
  # Imports
4
  import sys
 
5
  import pathlib
6
  import subprocess
7
  import PIL.Image
8
+ import torch
9
 
10
  # Ensure access to NOVIC code
11
  REPO_ROOT = pathlib.Path(__file__).parent.resolve()
 
19
  if str(NOVIC_DIR) not in sys.path:
20
  sys.path.insert(0, str(NOVIC_DIR))
21
 
22
+ # Import NOVIC inference
23
+ import infer # noqa
24
+
25
+ # General inference setup
26
+ infer.utils.allow_tf32(enable=True)
27
+ DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
28
+ MODELS: dict[str, infer.NOVICModel] = {}
29
+
30
+ # Retrieve a NOVIC model
31
+ def get_model(checkpoint: str) -> infer.NOVICModel:
32
+
33
+ if (model := MODELS.get(checkpoint, None)) is not None:
34
+ return model
35
+
36
+ model = infer.NOVICModel(
37
+ checkpoint=checkpoint,
38
+ gencfg='beam_k10_vnone_gp_t1_a0',
39
+ guide_targets=None,
40
+ torch_compile=False,
41
+ batch_size=1,
42
+ device=DEVICE,
43
+ cfg_flat_override=None,
44
+ embedder_override=None,
45
+ )
46
+
47
+ model.__enter__()
48
+ MODELS[checkpoint] = model
49
+ return model
50
 
51
  # Classify an image
52
  def classify_image(image: PIL.Image.Image, checkpoint: str) -> dict[str, float]:
53
+ model = get_model(checkpoint=checkpoint)
54
+ output = model.classify_image(image=image)
55
+ return dict(zip(output.preds[0], output.probs[0], strict=True))
 
 
 
56
  # EOF
novic CHANGED
@@ -1 +1 @@
1
- Subproject commit cc56882720618202830c334a2d93d8d536cfd412
 
1
+ Subproject commit 23ab1652b174b0f45bf34139c4d85f45fd28dead