Permalink
Browse files

Merge branch 'feature-engine-selection' into develop

  • Loading branch information...
Bofu Chen (bafu)
Bofu Chen (bafu) committed Mar 1, 2018
2 parents a09b794 + d6a617e commit 84d8726754ad6e0af3a16fc6a53f137ff2f528b0
@@ -0,0 +1,2 @@
*.pyc
__pycache__
@@ -0,0 +1,4 @@
from logzero import setup_logger


logger = setup_logger(name='berrynet-logger', logfile='/tmp/berrynet.log')
@@ -0,0 +1,43 @@
#!/usr/bin/python3

import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

from berrynet import logger
from logzero import setup_logger


def on_connect(client, userdata, flags, rc):
logger.debug('Connected with result code ' + str(rc))
for topic in client.comm_config['subscribe'].keys():
logger.debug('Subscribe topic {}'.format(topic))
client.subscribe(topic)


def on_message(client, userdata, msg):
"""Dispatch received message to its bound functor.
"""
logger.debug('Receive message from topic {}'.format(msg.topic))
#logger.debug('Message payload {}'.format(msg.payload))
client.comm_config['subscribe'][msg.topic](msg.payload)


class Communicator(object):
def __init__(self, comm_config, debug=False):
self.client = mqtt.Client()
self.client.comm_config = comm_config
self.client.on_connect = on_connect
self.client.on_message = on_message

def run(self):
self.client.connect(
self.client.comm_config['broker']['address'],
self.client.comm_config['broker']['port'],
60)
self.client.loop_forever()

def send(self, topic, payload):
logger.debug('Send message to topic {}'.format(topic))
#logger.debug('Message payload {}'.format(payload))
publish.single(topic, payload,
hostname=self.client.comm_config['broker']['address'])
@@ -0,0 +1,80 @@
import base64
import json

from datetime import datetime

import cv2
import numpy as np


def stringify_jpg(jpg_bytes):
return base64.b64encode(jpg_bytes).decode('utf-8')


def destringify_jpg(stringified_jpg):
"""
:return: JPEG bytes
:rtype: bytes
"""
return base64.b64decode(stringified_jpg.encode('utf-8'))


def jpg2bgr(jpg_bytes):
"""
:return: BGR bytes
:rtype: numpy array
"""
array = np.frombuffer(jpg_bytes, dtype=np.uint8)
return cv2.imdecode(array, flags=1)


def jpg2rgb(jpg_bytes):
"""
:return: RGB bytes
:rtype: numpy array
"""
return cv2.cvtColor(jpg2bgr(jpg_bytes), cv2.COLOR_BGR2RGB)


def serialize_payload(json_object):
return json.dumps(json_object)


def serialize_jpg(jpg_bytes):
"""Create Serialized JSON object consisting of image bytes and meta
:param imarray: JPEG bytes
:type imarray: bytes
:return: serialized image JSON
:rtype: string
"""
obj = {}
obj['timestamp'] = datetime.now().isoformat()
obj['bytes'] = stringify_jpg(jpg_bytes)
return json.dumps(obj)


def deserialize_payload(payload):
return json.loads(payload)


#def deserialize_jpg(jpg_json):
# """Deserialized JSON object created by josnify_image.
#
# :param string :
# :return:
# :rtype:
# """
# return json.loads(jpg_json)


if __name__ == '__main__':
im = cv2.imread('/home/debug/codes/darknet/data/dog.jpg')
retval, jpg_bytes = cv2.imencode('.jpg', im)

# size of stringified dog.jpg is 1.33x larger than original
s_jpg = serialize_jpg(jpg_bytes)
d_jpg = deserialize_payload(s_jpg)
# TODO: Can we write JPEG bytes into file directly to prevent
# bytes -> numpy array -> decode RGB -> write encoded JPEG
cv2.imwrite('/tmp/dog.jpg', jpg2bgr(destringify_jpg(d_jpg['bytes'])))
@@ -0,0 +1,64 @@
# Copyright 2017 DT42
#
# This file is part of BerryNet.
#
# BerryNet is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BerryNet is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BerryNet. If not, see <http://www.gnu.org/licenses/>.

"""
DL Model Manager, following the DLModelBox model package
speccification.
"""

from __future__ import print_function

import argparse
import json
import os

from berrynet import logger


class DLModelManager(object):
def __init__(self):
self.basedir = '/var/lib/dlmodels'

def get_model_names(self):
return os.listdir(self.basedir)

def get_model_meta(self, modelname):
meta_filepath = os.path.join(self.basedir, modelname, 'meta.json')
with open(meta_filepath, 'r') as f:
meta = json.load(f)
meta['model'] = os.path.join(self.basedir, modelname, meta['model'])
meta['label'] = os.path.join(self.basedir, modelname, meta['label'])
for k, v in meta['config'].items():
meta['config'][k] = os.path.join(self.basedir, modelname,
meta['config'][k])
return meta


def parse_args():
ap = argparse.ArgumentParser()
ap.add_argument('--modelname',
help='Model package name (without version)')
return vars(ap.parse_args())


if __name__ == '__main__':
args = parse_args()
logger.debug('model package name: ', args['modelname'])

dlmm = DLModelManager()
for name in dlmm.get_model_names():
print(dlmm.get_model_meta(name))
@@ -0,0 +1,54 @@
# Copyright 2018 DT42
#
# This file is part of BerryNet.
#
# BerryNet is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BerryNet is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BerryNet. If not, see <http://www.gnu.org/licenses/>.

"""
Deep learning engine template provides unified interfaces for
different backends (e.g. TensorFlow, Caffe2, etc.)
"""

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

def create(self):
# Workaround to posepone TensorFlow initialization.
# If TF is initialized in __init__, and pass an engine instance
# to engine service, TF session will stuck in run().
pass

def process_input(self, tensor):
return tensor

def inference(self, tensor):
output = None
return output

def process_output(self, output):
return 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(str(self.cache['model_output']))
@@ -0,0 +1,131 @@
#!/usr/bin/python
#
# Copyright 2017 DT42
#
# This file is part of BerryNet.
#
# BerryNet is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BerryNet is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BerryNet. If not, see <http://www.gnu.org/licenses/>.

import os

import cv2
import numpy as np

from mvnc import mvncapi as mvnc


class MovidiusNeuralGraph(object):
def __init__(self, graph_filepath, label_filepath):
# mvnc.SetGlobalOption(mvnc.GlobalOption.LOGLEVEL, 2)
devices = mvnc.EnumerateDevices()
if len(devices) == 0:
raise Exception('No devices found')
self.device = mvnc.Device(devices[0])
self.device.OpenDevice()

# Load graph
with open(graph_filepath, mode='rb') as f:
graphfile = f.read()
self.graph = self.device.AllocateGraph(graphfile)

# Load labels
self.labels = []
with open(label_filepath, 'r') as f:
for line in f:
label = line.split('\n')[0]
if label != 'classes':
self.labels.append(label)
f.close()

def __exit__(self, exc_type, exc_value, traceback):
self.graph.DeallocateGraph()
self.device.CloseDevice()

def inference(self, data):
self.graph.LoadTensor(data.astype(np.float16), 'user object')
output, userobj = self.graph.GetResult()
return output

def get_graph(self):
return self.graph

def get_labels(self):
return self.labels


def process_inceptionv3_input(img):
image_size = 299
mean = 128
std = 1.0/128

dx, dy, dz = img.shape
delta = float(abs(dy - dx))
if dx > dy: # crop the x dimension
img = img[int(0.5*delta):dx-int(0.5*delta), 0:dy]
else:
img = img[0:dx, int(0.5*delta):dy-int(0.5*delta)]
img = cv2.resize(img, (image_size, image_size))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
for i in range(3):
img[:, :, i] = (img[:, :, i] - mean) * std
return img


def process_inceptionv3_output(output, labels):
processed_output = {'annotations': []}
decimal_digits = 2
top_k = 5
top_inds = output.argsort()[::-1][:top_k]
for i in range(top_k):
human_string = labels[top_inds[i]]
score = round(float(output[top_inds[i]]), decimal_digits)
anno = {
'type': 'classification',
'label': human_string,
'confidence': score
}
processed_output['annotations'].append(anno)
return processed_output
#return [(labels[top_inds[i]], output[top_inds[i]]) for i in range(5)]


def print_inceptionv3_output(output, labels):
top_inds = output.argsort()[::-1][:5]

for i in range(5):
print(top_inds[i], labels[top_inds[i]], output[top_inds[i]])


if __name__ == '__main__':
graph_filepath = '' # model filepath
label_filepath = '' # label filepath
path_to_images = '' # image dirpath
image_filenames = [os.path.join(path_to_images, image_name)
for image_name in []] # image filename list

movidius = MovidiusNeuralGraph(graph_filepath, label_filepath)
labels = movidius.get_labels()

print(''.join(['*' for i in range(79)]))
print('inception-v3 on NCS')
for image_filename in image_filenames:
img = cv2.imread(image_filename).astype(np.float32)
img = process_inceptionv3_input(img)
print(''.join(['*' for i in range(79)]))
print('Start download to NCS...')
output = movidius.inference(img)
print_inceptionv3_output(output, labels)

print(''.join(['*' for i in range(79)]))
print('Finished')
Oops, something went wrong.

0 comments on commit 84d8726

Please sign in to comment.