Last active
December 11, 2017 15:37
-
-
Save wtnb75/80f5a2f1892f9ee349d55b0d865a023e to your computer and use it in GitHub Desktop.
cpu-caffe vs. movidius ncs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mvnc import mvncapi as mvnc | |
import cv2 | |
import numpy | |
import os | |
import time | |
import subprocess | |
import caffe | |
import click | |
import struct | |
class RunByCaffe: | |
def __init__(self, labelfn, meanfn, prototxt, caffemodel): | |
self.dim = (227, 227) | |
self.labels = numpy.loadtxt(labelfn, str, delimiter="\t") | |
self.mean = numpy.load(meanfn).mean(1).mean(1) | |
self.net = caffe.Net(prototxt, caffemodel, caffe.TEST) | |
self.transformer = caffe.io.Transformer( | |
{'data': self.net.blobs['data'].data.shape}) | |
self.transformer.set_transpose('data', (2, 0, 1)) | |
self.transformer.set_mean('data', self.mean) | |
self.transformer.set_raw_scale('data', 255) | |
self.transformer.set_channel_swap('data', (2, 1, 0)) | |
caffe.set_mode_cpu() | |
def loadimg(self, fn): | |
# print("loading", fn) | |
image = caffe.io.load_image(fn) | |
transformed_image = self.transformer.preprocess('data', image) | |
return transformed_image | |
def classify(self, img): | |
# self.net.blobs['data'].reshape(1, 3, self.dim[0], self.dim[1]) | |
self.net.blobs['data'].data[...] = [img] | |
output = self.net.forward() | |
output_prob = output['prob'][0] | |
return output_prob | |
class RunByMovidius(RunByCaffe): | |
def __init__(self, labelfn, meanfn, graph): | |
self.dim = [227, 227] | |
self.channel = 3 | |
with open(graph, "rb") as ifp: | |
ifp.seek(0xfa) | |
cpus, = struct.unpack("h", ifp.read(2)) | |
print("cpus", cpus + 1) | |
ifp.seek(0x178) | |
width, height, channel = struct.unpack("3i", ifp.read(4 * 3)) | |
self.dim[0] = width | |
self.dim[1] = height | |
self.channels = channel | |
print("width", width, "height", height, "channel", channel) | |
self.labels = numpy.loadtxt(labelfn, str, delimiter="\t") | |
self.mean = numpy.load(meanfn).mean(1).mean(1) | |
if False: | |
self.transformer = caffe.io.Transformer( | |
{'data': (1, self.channel, self.dim[0], self.dim[1])}) | |
self.transformer.set_transpose('data', (2, 0, 1)) | |
self.transformer.set_mean('data', self.mean) | |
self.transformer.set_raw_scale('data', 255) | |
self.transformer.set_channel_swap('data', (2, 1, 0)) | |
mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2) | |
devices = mvnc.EnumerateDevices() | |
if len(devices) == 0: | |
print('No devices found') | |
quit() | |
self.device = mvnc.Device(devices[0]) | |
self.device.OpenDevice() | |
with open(graph, mode='rb') as f: | |
blob = f.read() | |
self.graph = self.device.AllocateGraph(blob) | |
def loadimg(self, fn): | |
# print("loading", fn) | |
img = cv2.imread(fn) | |
img = cv2.resize(img, tuple(self.dim)) | |
img = img.astype(numpy.float16) | |
img[:, :, 0] = (img[:, :, 0] - self.mean[0]) | |
img[:, :, 1] = (img[:, :, 1] - self.mean[1]) | |
img[:, :, 2] = (img[:, :, 2] - self.mean[2]) | |
return img | |
def classify(self, img): | |
self.graph.LoadTensor(img.astype(numpy.float16), 'user object') | |
output, userobj = self.graph.GetResult() | |
return output | |
def __del__(self): | |
if hasattr(self, "graph"): | |
self.graph.DeallocateGraph() | |
if hasattr(self, "device"): | |
self.device.CloseDevice() | |
class RunByMovidius2(RunByMovidius): | |
def __init__(self, labelfn, meanfn, prototxt, caffemodel): | |
if not os.path.exists("graph"): | |
res = subprocess.run(["mvNCCompile", "-w", caffemodel, "-s", "12", prototxt]) | |
print("result", res) | |
RunByMovidius.__init__(self, labelfn, meanfn, "graph") | |
def getts(ts): | |
return list(map(lambda f: f[1] - f[0], zip(ts, ts[1:]))) | |
@click.group(invoke_without_command=True) | |
@click.pass_context | |
def cli(ctx): | |
if ctx.invoked_subcommand is None: | |
print(ctx.get_help()) | |
else: | |
print('gonna invoke %s' % ctx.invoked_subcommand) | |
def do_solve(solver, images): | |
print("images", images) | |
print("|file|time(load)|time(eval)|id|prob|label|") | |
print("|----|---------:|---------:|--|----|-----|") | |
for i in images: | |
ts = [time.time()] | |
img = solver.loadimg(i) | |
ts.append(time.time()) | |
output = solver.classify(img) | |
ts.append(time.time()) | |
loadtime, evaltime = getts(ts) | |
am = output.argmax() | |
print("|%s|%.3f|%.3f|%s|%.2f|%s|" % (os.path.basename(i), | |
loadtime, evaltime, am, output[am], solver.labels[am])) | |
@cli.command() | |
@click.option("--label") | |
@click.option("--mean") | |
@click.option("--prototxt") | |
@click.option("--caffemodel") | |
@click.argument("images", nargs=-1) | |
def bycaffe(label, mean, prototxt, caffemodel, images): | |
solver = RunByCaffe(label, mean, prototxt, caffemodel) | |
do_solve(solver, images) | |
@cli.command() | |
@click.option("--label") | |
@click.option("--mean") | |
@click.option("--graph") | |
@click.argument("images", nargs=-1) | |
def byncs(label, mean, graph, images): | |
solver = RunByMovidius(label, mean, graph) | |
do_solve(solver, images) | |
@cli.command() | |
@click.option("--label") | |
@click.option("--mean") | |
@click.option("--prototxt") | |
@click.option("--caffemodel") | |
@click.argument("images", nargs=-1) | |
def byncs2(label, mean, prototxt, caffemodel, images): | |
solver = RunByMovidius2(label, mean, prototxt, caffemodel) | |
do_solve(solver, images) | |
def main(): | |
cli() | |
if __name__ == "__main__": | |
main() |
GoogLeNet, caffe
file | time(load) | time(eval) | id | prob | label |
---|---|---|---|---|---|
nps_acoustic_guitar.png | 0.084 | 0.718 | 402 | 1.00 | n02676566 acoustic guitar |
nps_backpack.png | 0.071 | 0.639 | 414 | 1.00 | n02769748 backpack, back pack, knapsack, packsack, rucksack, haversack |
nps_baseball.png | 0.063 | 0.672 | 429 | 1.00 | n02799071 baseball |
nps_chair.png | 0.067 | 0.650 | 559 | 1.00 | n03376595 folding chair |
nps_electric_guitar.png | 0.069 | 0.711 | 546 | 1.00 | n03272010 electric guitar |
nps_guac.png | 0.072 | 0.655 | 924 | 0.98 | n07583066 guacamole |
nps_keyboard.png | 0.071 | 0.796 | 508 | 0.63 | n03085013 computer keyboard, keypad |
nps_mouse.png | 0.082 | 0.814 | 673 | 1.00 | n03793489 mouse, computer mouse |
nps_mug.png | 0.075 | 0.779 | 504 | 0.91 | n03063599 coffee mug |
nps_screwdriver.png | 0.091 | 0.740 | 784 | 0.72 | n04154565 screwdriver |
GoogLeNet, ncs
file | time(load) | time(eval) | id | prob | label |
---|---|---|---|---|---|
nps_acoustic_guitar.png | 0.083 | 0.124 | 753 | 0.29 | n04040759 radiator |
nps_backpack.png | 0.067 | 0.111 | 912 | 0.45 | n04604644 worm fence, snake fence, snake-rail fence, Virginia fence |
nps_baseball.png | 0.069 | 0.108 | 418 | 0.39 | n02783161 ballpoint, ballpoint pen, ballpen, Biro |
nps_chair.png | 0.071 | 0.105 | 58 | 0.45 | n01737021 water snake |
nps_electric_guitar.png | 0.069 | 0.108 | 807 | 0.28 | n04258138 solar dish, solar collector, solar furnace |
nps_guac.png | 0.081 | 0.103 | 912 | 0.36 | n04604644 worm fence, snake fence, snake-rail fence, Virginia fence |
nps_keyboard.png | 0.073 | 0.102 | 395 | 0.45 | n02641379 gar, garfish, garpike, billfish, Lepisosteus osseus |
nps_mouse.png | 0.063 | 0.109 | 418 | 0.33 | n02783161 ballpoint, ballpoint pen, ballpen, Biro |
nps_mug.png | 0.071 | 0.107 | 418 | 0.55 | n02783161 ballpoint, ballpoint pen, ballpen, Biro |
nps_screwdriver.png | 0.078 | 0.102 | 581 | 0.46 | n03459775 grille, radiator grille |
SqueezeNet, caffe
file | time(load) | time(eval) | id | prob | label |
---|---|---|---|---|---|
nps_acoustic_guitar.png | 0.082 | 0.174 | 402 | 0.99 | n02676566 acoustic guitar |
nps_backpack.png | 0.084 | 0.168 | 414 | 1.00 | n02769748 backpack, back pack, knapsack, packsack, rucksack, haversack |
nps_baseball.png | 0.074 | 0.179 | 429 | 1.00 | n02799071 baseball |
nps_chair.png | 0.074 | 0.187 | 559 | 0.99 | n03376595 folding chair |
nps_electric_guitar.png | 0.082 | 0.178 | 546 | 0.99 | n03272010 electric guitar |
nps_guac.png | 0.076 | 0.151 | 924 | 0.98 | n07583066 guacamole |
nps_keyboard.png | 0.073 | 0.146 | 681 | 0.19 | n03832673 notebook, notebook computer |
nps_mouse.png | 0.064 | 0.151 | 673 | 0.95 | n03793489 mouse, computer mouse |
nps_mug.png | 0.071 | 0.153 | 504 | 0.99 | n03063599 coffee mug |
nps_screwdriver.png | 0.078 | 0.164 | 784 | 0.75 | n04154565 screwdriver |
SqueezeNet, ncs
file | time(load) | time(eval) | id | prob | label |
---|---|---|---|---|---|
nps_acoustic_guitar.png | 0.082 | 0.073 | 406 | 0.64 | n02699494 altar |
nps_backpack.png | 0.066 | 0.055 | 528 | 0.59 | n03187595 dial telephone, dial phone |
nps_baseball.png | 0.056 | 0.053 | 429 | 0.96 | n02799071 baseball |
nps_chair.png | 0.061 | 0.054 | 134 | 0.74 | n02012849 crane |
nps_electric_guitar.png | 0.065 | 0.054 | 456 | 0.86 | n02879718 bow |
nps_guac.png | 0.071 | 0.056 | 532 | 0.33 | n03201208 dining table, board |
nps_keyboard.png | 0.068 | 0.054 | 784 | 0.84 | n04154565 screwdriver |
nps_mouse.png | 0.059 | 0.053 | 731 | 0.76 | n03970156 plunger, plumber's helper |
nps_mug.png | 0.065 | 0.054 | 622 | 0.71 | n03657121 lens cap, lens cover |
nps_screwdriver.png | 0.070 | 0.055 | 772 | 0.91 | n04127249 safety pin |
rev2: fix loading image
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nnn...
AlexNet, bycaffe
AlexNet, byncs2