matjesg commited on
Commit
7a7548a
·
1 Parent(s): 5a6b006

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -19
app.py CHANGED
@@ -1,33 +1,27 @@
1
  import numpy as np
2
  import gradio as gr
3
- import onnxruntime as ort
4
- from matplotlib import pyplot as plt
5
  from huggingface_hub import hf_hub_download
6
 
7
- def create_model_for_provider(model_path, provider="CPUExecutionProvider"):
8
- options = ort.SessionOptions()
9
- options.intra_op_num_threads = 1
10
- options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
11
- session = ort.InferenceSession(str(model_path), options, providers=[provider])
12
- session.disable_fallback()
13
- return session
14
-
15
  def inference(repo_id, model_name, img):
16
- model = hf_hub_download(repo_id=repo_id, filename=model_name)
17
- ort_session = create_model_for_provider(model)
18
- n_channels = ort_session.get_inputs()[0].shape[-1]
 
19
 
20
- img = img[...,:n_channels]/255
21
- ort_inputs = {ort_session.get_inputs()[0].name: img.astype(np.float32)}
 
22
 
23
- ort_outs = ort_session.run(None, ort_inputs)
24
 
25
- return ort_outs[0]*255, ort_outs[2]/0.25
 
26
 
27
  title="deepflash2"
28
  description='deepflash2 is a deep-learning pipeline for the segmentation of ambiguous microscopic images.\n deepflash2 uses deep model ensembles to achieve more accurate and reliable results. Thus, inference time will be more than a minute in this space.'
29
- examples=[['matjesg/deepflash2_demo', 'cFOS_ensemble.onnx', 'cFOS_example.png'],
30
- ['matjesg/deepflash2_demo', 'YFP_ensemble.onnx', 'YFP_example.png']
31
  ]
32
 
33
  gr.Interface(inference,
 
1
  import numpy as np
2
  import gradio as gr
3
+ import torch
 
4
  from huggingface_hub import hf_hub_download
5
 
 
 
 
 
 
 
 
 
6
  def inference(repo_id, model_name, img):
7
+ #model_path = hf_hub_download(repo_id=repo_id, filename=model_name)
8
+ model_path = 'trained_models/cFOS_in_HC/cFOS_in_HC_ensemble_1.pt'
9
+ model = torch.jit.load(model_path, map_location='cpu')
10
+ n_channels = len(model.norm.mean)
11
 
12
+ # Remove redundant channels
13
+ img = img[...,:n_channels]
14
+ inp = torch.from_numpy(img).float()
15
 
16
+ argmax, softmax, stdeviation = model(inp)
17
 
18
+ return argmax*255, stdeviation
19
+
20
 
21
  title="deepflash2"
22
  description='deepflash2 is a deep-learning pipeline for the segmentation of ambiguous microscopic images.\n deepflash2 uses deep model ensembles to achieve more accurate and reliable results. Thus, inference time will be more than a minute in this space.'
23
+ examples=[['matjesg/deepflash2_demo', 'cFOS_in_HC_ensemble.pt', 'cFOS_example.png'],
24
+ ['matjesg/deepflash2_demo', 'YFP_in_CTX_ensemble.pt', 'YFP_example.png']
25
  ]
26
 
27
  gr.Interface(inference,