Skip to content

Instantly share code, notes, and snippets.

@wtnb75
Last active December 11, 2017 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wtnb75/80f5a2f1892f9ee349d55b0d865a023e to your computer and use it in GitHub Desktop.
Save wtnb75/80f5a2f1892f9ee349d55b0d865a023e to your computer and use it in GitHub Desktop.
cpu-caffe vs. movidius ncs
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()
@wtnb75
Copy link
Author

wtnb75 commented Nov 5, 2017

nnn...

AlexNet, bycaffe

ubuntu@ubuntu-xenial:/vagrant/ncsdk/examples/caffe/AlexNet$ python3 ../bymovidius.py bycaffe --label ../../data/ilsvrc12/synset_words.txt --mean ../../data/ilsvrc12/ilsvrc_2012_mean.npy --prototxt deploy.prototxt --caffemodel bvlc_alexnet.caffemodel ../../data/images/nps_*.*
file time(load) time(eval) id prob label
nps_acoustic_guitar.png 0.081 0.395 402 0.99 n02676566 acoustic guitar
nps_backpack.png 0.077 0.393 414 0.98 n02769748 backpack, back pack, knapsack, packsack, rucksack, haversack
nps_baseball.png 0.061 0.364 429 1.00 n02799071 baseball
nps_chair.png 0.074 0.368 559 0.96 n03376595 folding chair
nps_electric_guitar.png 0.066 0.378 546 0.95 n03272010 electric guitar
nps_guac.png 0.071 0.375 924 0.43 n07583066 guacamole
nps_keyboard.png 0.070 0.413 458 0.57 n02892201 brass, memorial tablet, plaque
nps_mouse.png 0.084 0.404 673 0.66 n03793489 mouse, computer mouse
nps_mug.png 0.078 0.398 504 0.91 n03063599 coffee mug
nps_screwdriver.png 0.076 0.395 845 0.51 n04376876 syringe

AlexNet, byncs2

ubuntu@ubuntu-xenial:/vagrant/ncsdk/examples/caffe/AlexNet$ python3 ../bymovidius.py byncs2 --label ../../data/ilsvrc12/synset_words.txt --mean ../../data/ilsvrc12/ilsvrc_2012_mean.npy --prototxt deploy.prototxt --caffemodel bvlc_alexnet.caffemodel ../../data/images/nps_*.*
file time(load) time(eval) id prob label
nps_acoustic_guitar.png 0.083 0.104 456 0.17 n02879718 bow
nps_backpack.png 0.066 0.097 746 0.22 n04019541 puck, hockey puck
nps_baseball.png 0.060 0.098 574 0.88 n03445777 golf ball
nps_chair.png 0.062 0.098 310 0.92 n02219486 ant, emmet, pismire
nps_electric_guitar.png 0.065 0.097 456 0.41 n02879718 bow
nps_guac.png 0.074 0.098 729 0.58 n03961711 plate rack
nps_keyboard.png 0.066 0.097 563 0.25 n03388183 fountain pen
nps_mouse.png 0.058 0.097 999 0.76 n15075141 toilet tissue, toilet paper, bathroom tissue
nps_mug.png 0.071 0.097 732 0.28 n03976467 Polaroid camera, Polaroid Land camera
nps_screwdriver.png 0.069 0.098 313 0.14 n02231487 walking stick, walkingstick, stick insect

@wtnb75
Copy link
Author

wtnb75 commented Nov 5, 2017

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

@wtnb75
Copy link
Author

wtnb75 commented Nov 5, 2017

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

@wtnb75
Copy link
Author

wtnb75 commented Nov 6, 2017

rev2: fix loading image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment