Skip to content

Commit

Permalink
add sources
Browse files Browse the repository at this point in the history
  • Loading branch information
duangenquan committed Dec 7, 2017
0 parents commit c14cdab
Show file tree
Hide file tree
Showing 22 changed files with 1,713 additions and 0 deletions.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,32 @@
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app
46 changes: 46 additions & 0 deletions Makefile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,46 @@

CC = gcc
CPP = g++
PYTHON_VERSION = 3.5m
PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)

IDIRS = -I$(PYTHON_INCLUDE) -I.
DEFINES = -D_ISOC99_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -DPIC -DZLIB_CONST -D_GNU_SOURCE=1 -D_REENTRANT -D__STDC_CONSTANT_MACROS
CFLAGS = -fomit-frame-pointer -fPIC -pthread -Wall -Wextra -DNDEBUG -O3 -g -rdynamic $(IDIRS) $(DEFINES)

LIBRARIES = -L/usr/lib -L/usr/lib/python$(PYTHON_VERSION)/config -L/usr/lib/x86_64-linux-gnu/ -lpython$(PYTHON_VERSION) -lboost_python-py35


CPPFLAGS = -std=c++11 $(CFLAGS)

LFLAGS = -lm -lstdc++ -llzma -lz -ldl -lpthread
LDFLAGS = $(LIBRARIES) $(LFLAGS)


SRC = ./src

SOURCES = $(wildcard $(SRC)/*.cpp)

EXECUTABLE = ./libpydetector.so

OBJECTS = $(patsubst %.cpp,%.o,$(SOURCES))

all: $(SOURCES) $(EXECUTABLE)

%.o : %.c
@echo Compiling: $<
@$(CC) $(CFLAGS) -c $< -o $@

%.o : %.cpp
@echo Compiling: $<
@$(CPP) $(CPPFLAGS) -c $< -o $@

clean:
rm -f $(OBJECTS)
rm -f $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
@echo Linking: $@
@$(CPP) -shared -Wl,--export-dynamic $(OBJECTS) $(LDFLAGS) -o $@
cp -f $(EXECUTABLE) ./detectionExample/$(EXECUTABLE)

60 changes: 60 additions & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,60 @@
# YOLOv2 for Intel/Movidius Neural Compute Stick (NCS)

*This project shows how to run tiny yolov2 (20 classes) with movidius stick:*
+ A python convertor from yolo to caffe
+ A c/c++ implementation and python wrapper for region layer of yolov2
+ A sample for running yolov2 with movidius stick

---

# How To Use
The following experiments are done on an Intel NUC with ubuntu 16.04.

### Step 1. Compile Python Wrapper
```make```

### Step 2. Convert Caffe to NCS
```
mvNCCompile ./models/caffemodels/yoloV2Tiny20.prototxt -w ./models/caffemodels/yoloV2Tiny20.caffemodel -s 12
```
There will be a file *graph* generated as converted models for NCS.

### Step 3. Run tests
```
python3 ./detectionExample/Main.py --image ./data/dog.jpg
```
This loads *graph* by default and results will be like this:
![](/data/yolo_dog.jpg)

# Run Other YoloV2 models
### Convert Yolo to Caffe
```
Install caffe and config the python environment path.
sh ./models/convertyo.sh
```
Tips:

Please ignore the error message similar as "Region layer is not supported".

The converted caffe models should end with "prototxt" and "caffemodel".

### Update parameters

Please update parameters (biases, object names, etc) in ./src/CRegionLayer.cpp, and parameters (dim, blockwd, targetBlockwd, classe, etc) in ./detectionExample/ObjectWrapper.py.

Please read ./src/CRegionLayer.cpp and ./detectionExample/ObjectWrapper.py for details.


# References
+ [caffe](https://github.com/BVLC/caffe)
+ [yolo](https://github.com/pjreddie/darknet)
+ [caffe-yolo](https://github.com/xingwangsfu/caffe-yolo)
+ [yoloNCS](https://github.com/gudovskiy/yoloNCS)

---

# License
Research Only

# Author
duangenquan@gmail.com
Binary file added data/dog.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/yolo_dog.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions detectionExample/Main.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,37 @@
import sys,os,time,csv,getopt,cv2,argparse
import numpy as np
from datetime import datetime

from ObjectWrapper import *
from Visualize import *

if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--graph', dest='graph', type=str,
default='graph', help='MVNC graphs.')
parser.add_argument('--image', dest='image', type=str,
default='./images/dog.jpg', help='An image path.')
args = parser.parse_args()

network_blob=args.graph
imagefile = args.image

detector = ObjectWrapper(network_blob)

# image preprocess
img = cv2.imread(imagefile)
start = datetime.now()

results = detector.Detect(img)

end = datetime.now()
elapsedTime = end-start

print ('total time is " milliseconds', elapsedTime.total_seconds()*1000)

imdraw = Visualize(img, results)
cv2.imshow('Demo',imdraw)
cv2.imwrite('test.jpg',imdraw)
cv2.waitKey(10000)


119 changes: 119 additions & 0 deletions detectionExample/ObjectWrapper.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,119 @@
from libpydetector import YoloDetector
import os, io, numpy, time
import numpy as np
from mvnc import mvncapi as mvnc
from skimage.transform import resize

class BBox(object):
def __init__(self, bbox):
self.left = bbox.left
self.top = bbox.top
self.right = bbox.right
self.bottom = bbox.bottom
self.confidence = bbox.confidence
self.objType = bbox.objType
self.name = bbox.name

class ObjectWrapper():
def __init__(self, graphfile):
select = 1
self.detector = YoloDetector(select)
mvnc.SetGlobalOption(mvnc.GlobalOption.LOG_LEVEL, 2)
devices = mvnc.EnumerateDevices()
if len(devices) == 0:
print('No MVNC devices found')
quit()
self.device = mvnc.Device(devices[0])
self.device.OpenDevice()
opt = self.device.GetDeviceOption(mvnc.DeviceOption.OPTIMISATION_LIST)
# load blob
with open(graphfile, mode='rb') as f:
blob = f.read()
self.graph = self.device.AllocateGraph(blob)
self.graph.SetGraphOption(mvnc.GraphOption.ITERATIONS, 1)
iterations = self.graph.GetGraphOption(mvnc.GraphOption.ITERATIONS)

self.dim = (416,416)
self.blockwd = 12
self.wh = self.blockwd*self.blockwd
self.targetBlockwd = 12
self.classes = 20
self.threshold = 0.2
self.nms = 0.4


def __del__(self):
self.graph.DeallocateGraph()
self.device.CloseDevice()
def PrepareImage(self, img, dim):
imgw = img.shape[1]
imgh = img.shape[0]
imgb = np.empty((dim[0], dim[1], 3))
imgb.fill(0.5)

if imgh/imgw > dim[1]/dim[0]:
neww = int(imgw * dim[1] / imgh)
newh = dim[1]
else:
newh = int(imgh * dim[0] / imgw)
neww = dim[0]
offx = int((dim[0] - neww)/2)
offy = int((dim[1] - newh)/2)

imgb[offy:offy+newh,offx:offx+neww,:] = resize(img.copy()/255.0,(newh,neww),1)
im = imgb[:,:,(2,1,0)]
return im,offx,offy

def Reshape(self, out, dim):
shape = out.shape
out = np.transpose(out.reshape(self.wh, int(shape[0]/self.wh)))
out = out.reshape(shape)
return out

def Detect(self, img):
imgw = img.shape[1]
imgh = img.shape[0]

im,offx,offy = self.PrepareImage(img, self.dim)
self.graph.LoadTensor(im.astype(np.float16), 'user object')
out, userobj = self.graph.GetResult()
out = self.Reshape(out, self.dim)

internalresults = self.detector.Detect(out.astype(np.float32), int(out.shape[0]/self.wh), self.blockwd, self.blockwd, self.classes, imgw, imgh, self.threshold, self.nms, self.targetBlockwd)
pyresults = [BBox(x) for x in internalresults]
return pyresults



































25 changes: 25 additions & 0 deletions detectionExample/Visualize.py
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,25 @@
import os, cv2

colornum = 12
colors = [(128,128,128),(128,0,0),(192,192,128),(255,69,0),(128,64,128),(60,40,222),(128,128,0),(192,128,128),(64,64,128),(64,0,128),(64,64,0),(0,128,192),(0,0,0)];

def Visualize(img, results):
img_cp = img.copy()
detectedNum = len(results)
if detectedNum > 0:
for i in range(detectedNum):

clr = colors[results[i].objType % colornum]
txt = results[i].name

left = results[i].left
top = results[i].top
right = results[i].right
bottom = results[i].bottom

cv2.rectangle(img_cp, (left,top), (right,bottom), clr, thickness=3)
cv2.rectangle(img_cp, (left,top-20),(right,top),(255,255,255),-1)
cv2.putText(img_cp,txt,(left+5,top-7),cv2.FONT_HERSHEY_SIMPLEX,0.5,clr,1)

return img_cp

Binary file added detectionExample/libpydetector.so
Binary file not shown.
Binary file added models/caffemodels/yoloV2Tiny20.caffemodel
Binary file not shown.
Loading

0 comments on commit c14cdab

Please sign in to comment.