Skip to content
Snippets Groups Projects
Commit c73727c0 authored by Christopher Randolph Rhodes's avatar Christopher Randolph Rhodes
Browse files

Abstracted away inference logic from API, successfully tested

parent e0945465
No related branches found
No related tags found
No related merge requests found
......@@ -8,4 +8,7 @@ czifile = {
'h': 1024,
'c': 4,
'z': 1,
}
\ No newline at end of file
}
output_path = root / 'testing_output'
output_path.mkdir(parents=True, exist_ok=True)
\ No newline at end of file
......@@ -5,6 +5,7 @@ from fastapi import FastAPI, HTTPException
from image import generate_file_accessor, WriteableTiffFileAccessor
from session import Session
from workflow import infer_image_to_image
app = FastAPI()
session = Session()
......@@ -39,28 +40,35 @@ def infer_img(model_id: str, imgf: str, channel: int = None) -> dict:
status_code=409,
detail=f'Model {model_id} has not been loaded'
)
model = session.models[model_id]
# read image file into memory
t0 = time()
img = generate_file_accessor(session.inbound / imgf)
dt_fi = time() - t0
# run model inference
outdata, record = model.infer(img.data, channel=channel)
dt_inf = time() - t0
# write output to file
outpath = session.outbound / img.fpath.stem / '.tif'
WriteableTiffFileAccessor(outpath).write(outdata)
dt_fo = time() - t0
record['output_file'] = outpath
record['times'] = {
'file_input': dt_fi,
'inference': dt_inf - dt_fi,
'file_output': dt_fo - dt_fi - dt_inf
}
session.register_inference(record)
return record
return infer_image_to_image(
session.inbound / imgf,
session.models[model_id],
session.outbound,
channel=channel
)
# model = session.models[model_id]
#
# # read image file into memory
# # maybe this isn't accurate if e.g. czifile loads lazily
# t0 = time()
# img = generate_file_accessor(session.inbound / imgf)
# dt_fi = time() - t0
#
# # run model inference
# outdata, record = model.infer(img.data, channel=channel)
# dt_inf = time() - t0
#
# # write output to file
# outpath = session.outbound / img.fpath.stem / '.tif'
# WriteableTiffFileAccessor(outpath).write(outdata)
# dt_fo = time() - t0
#
# record['output_file'] = outpath
# record['times'] = {
# 'file_input': dt_fi,
# 'inference': dt_inf - dt_fi,
# 'file_output': dt_fo - dt_fi - dt_inf
# }
# session.register_inference(record)
\ No newline at end of file
......@@ -38,7 +38,7 @@ class WriteableTiffFileAccessor(GenericImageFileAccessor):
def generate_file_accessor(fpath):
if fpath.upper().endswith('.CZI'):
if str(fpath).upper().endswith('.CZI'):
return CziImageFileAccessor(fpath)
else:
raise FileAccessorError(f'Could not match a file accessor with {fpath}')
......
......@@ -9,7 +9,7 @@ class Model(object):
pass
def infer(self, img: GenericImageFileAccessor, channel:int=None) -> (np.ndarray, dict): # return json describing inference result
if channel >= img.chroma:
if channel and channel >= img.chroma:
raise ChannelTooHighError(f'Requested channel {channel} but image contains only {img.chroma} channels')
def reload(self):
pass
......@@ -25,7 +25,7 @@ class DummyImageToImageModel(Model):
def load(self):
self.loaded = True
def infer(self, img, channel=None) -> (np.ndarray, dict):
def infer(self, img: GenericImageFileAccessor, channel=None) -> (np.ndarray, dict):
super().infer(img, channel)
w = img.shape_dict['X']
h = img.shape_dict['Y']
......
"""
Implementation of image analysis work behind API endpoints, without knowledge of persistent data in server session.
"""
from time import time
from model_server.image import generate_file_accessor, WriteableTiffFileAccessor
def infer_image_to_image(fpi, model, where_output, **kwargs) -> dict:
# read image file into memory
# maybe this isn't accurate if e.g. czifile loads lazily
t0 = time()
img = generate_file_accessor(fpi)
dt_fi = time() - t0
# run model inference
ch = kwargs.get('channel')
outdata, record = model.infer(img, channel=ch)
dt_inf = time() - t0
# write output to file
outpath = where_output / (img.fpath.stem + '.tif')
WriteableTiffFileAccessor(outpath).write(outdata)
dt_fo = time() - t0
record['output_file'] = outpath
# TODO: smoother step-timing e.g. w/ decorate
record['times'] = {
'file_input': dt_fi,
'inference': dt_inf - dt_fi,
'file_output': dt_fo - dt_fi - dt_inf
}
return record
\ No newline at end of file
import unittest
from conf.testing import czifile, output_path
from model_server.model import DummyImageToImageModel
from model_server.workflow import infer_image_to_image
class TestGetSessionObject(unittest.TestCase):
def setUp(self) -> None:
self.model = DummyImageToImageModel()
def test_single_session_instance(self):
result = infer_image_to_image(czifile['path'], self.model, output_path)
print(result)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment