Skip to content

Commit

Permalink
Add cache into engineservice for intermediate data.
Browse files Browse the repository at this point in the history
Signed-off-by: Bofu Chen (bafu) <bofu@dt42.io>
  • Loading branch information
bafu committed Dec 13, 2017
1 parent ca209d4 commit 092d4a3
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions inference/engineservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@


class DLEngine(object):
def __init__(self):
self.model_input_cache = []
self.model_output_cache = ''
self.cache = {
'model_input': [],
'model_output': '',
'model_output_filepath': ''
}

def process_input(self, tensor):
return tensor

Expand All @@ -51,9 +60,12 @@ def inference(self, tensor):
def process_output(self, output):
return output

def save_output(self, output, filepath):
with open(filepath, 'w') as f:
f.write(output)
def cache_data(self, key, value):
self.cache[key] = value

def save_cache(self):
with open(self.cache['model_output_filepath'], 'w') as f:
f.write(self.model_output_cache)


class EventHandler(PatternMatchingEventHandler):
Expand Down Expand Up @@ -99,14 +111,17 @@ def server(self):
while True:
input_name = self.image_queue.get()
image_data = cv2.imread(input_name).astype(np.float32)
self.cache_data('model_input', image_data)
image_data = self.engine.process_input(image_data)

output = self.engine.inference(image_data)
model_outputs = self.engine.process_output(output)
self.cache_data('model_ouptut', model_outputs)

output_name = input_name + '.txt'
output_done_name = output_name + '.done'
self.engine.save_output(model_outputs, output_name)
self.cache_data('model_output_filepath', output_name)
self.engine.save_cache()
self.touch(output_done_name)
logging.debug(input_name + " classified!")

Expand Down

0 comments on commit 092d4a3

Please sign in to comment.