FIRST INSTALL LIBRARIES RELATED TO CAFFE COMPILATION
brew install leveldb gperftools snappy glog gflags lmdb openblas boost boost-python
DOWNLOAD CAFFE FROM
git clone https://github.com/alexgkendall/caffe-segnet.git
INSTALL PYENV PYTHON 2.7.15
env PYTHON_CONFIGURE_OPTS="--enable-framework CC=clang" pyenv install 2.7.15
pyenv global 2.7.15
pyenv virtualenv caffes
pyenv activate caffes
pip install opencv-python scikit-image scikit-learn
COMPILING CAFFE USING THIS MAKEFILE
CPU_ONLY := 1
#CUDA_DIR := /usr/local/cuda
#CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
# -gencode arch=compute_20,code=sm_21 \
# -gencode arch=compute_30,code=sm_30 \
# -gencode arch=compute_35,code=sm_35 \
# -gencode arch=compute_50,code=sm_50 \
# -gencode arch=compute_50,code=compute_50
BLAS := open
BLAS_INCLUDE := $(shell brew --prefix openblas)/include
BLAS_LIB := $(shell brew --prefix openblas)/lib
WITH_PYTHON_LAYER := 1
PYTHON_INCLUDE := /Users/robsalasco/.pyenv/versions/2.7.15/include/python2.7/ \
/Users/robsalasco/.pyenv/versions/2.7.15/envs/caffes/lib/python2.7/site-packages/numpy/core/include \
PYTHON_LIB := /Users/robsalasco/.pyenv/versions/2.7.15/lib \
/usr/local/Cellar/boost-python/1.67.0/lib
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
USE_PKG_CONFIG := 1
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
TEST_GPUID := 0
Q ?= @
START THE COMPILATION
make all
make pycaffe
EDIT PATHS OF LIB
install_name_tool -change /System/Library/Frameworks/Python.framework/Versions/2.7/Python /usr/local/Cellar/python/2.7.15/Frameworks/Python.framework/Versions/2.7/Python ./python/caffe/_caffe.so
export PYTHONPATH=/Users/robsalasco/Dev/caffe-segnet/distribute/python:$PYTHONPATH
export PYTHONPATH=/Users/robsalasco/Dev/caffe-segnet/python/:$PYTHONPATH
DOWNLOAD SEGNET TUTORIAL
https://github.com/alexgkendall/SegNet-Tutorial.git
Download weights
http://mi.eng.cam.ac.uk/~agk34/resources/SegNet/segnet_weights_driving_webdemo.caffemodel
edit webcam_demo
import numpy as np
import caffe
import matplotlib.pyplot as plt
import os.path
import scipy
import argparse
import math
import cv2
import sys
import time
sys.path.append('/Users/robsalasco/.pyenv/versions/2.7.15/envs/caffes/lib/python2.7/site-packages/')
# Make sure that caffe is on the python path:
caffe_root = '/Users/robsalasco/Dev/caffe-segnet'
sys.path.insert(0, caffe_root + 'python')
# Import arguments
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, required=True)
parser.add_argument('--weights', type=str, required=True)
parser.add_argument('--colours', type=str, required=True)
args = parser.parse_args()
net = caffe.Net(args.model,
args.weights,
caffe.TEST)
caffe.set_mode_cpu()
input_shape = net.blobs['data'].data.shape
output_shape = net.blobs['argmax'].data.shape
label_colours = cv2.imread(args.colours).astype(np.uint8)
cv2.namedWindow("Input")
cv2.namedWindow("SegNet")
cap = cv2.VideoCapture(0) # Change this to your webcam ID, or file name for your video file
rval = True
while rval:
start = time.time()
rval, frame = cap.read()
if rval == False:
break
end = time.time()
print '%30s' % 'Grabbed camera frame in ', str((end - start)*1000), 'ms'
start = time.time()
frame = cv2.resize(frame, (input_shape[3],input_shape[2]))
input_image = frame.transpose((2,0,1))
# input_image = input_image[(2,1,0),:,:] # May be required, if you do not open your data with opencv
input_image = np.asarray([input_image])
end = time.time()
print '%30s' % 'Resized image in ', str((end - start)*1000), 'ms'
start = time.time()
out = net.forward_all(data=input_image)
end = time.time()
print '%30s' % 'Executed SegNet in ', str((end - start)*1000), 'ms'
start = time.time()
segmentation_ind = np.squeeze(net.blobs['argmax'].data)
segmentation_ind_3ch = np.resize(segmentation_ind,(3,input_shape[2],input_shape[3]))
segmentation_ind_3ch = segmentation_ind_3ch.transpose(1,2,0).astype(np.uint8)
segmentation_rgb = np.zeros(segmentation_ind_3ch.shape, dtype=np.uint8)
cv2.LUT(segmentation_ind_3ch,label_colours,segmentation_rgb)
segmentation_rgb = segmentation_rgb.astype(float)/255
end = time.time()
print '%30s' % 'Processed results in ', str((end - start)*1000), 'ms\n'
cv2.imshow("Input", frame)
cv2.imshow("SegNet", segmentation_rgb)
key = cv2.waitKey(1)
if key == 27: # exit on ESC
break
cap.release()
cv2.destroyAllWindows()
RUN WITH
python Scripts/webcam_demo.py --model Example_Models/segnet_model_driving_webdemo.prototxt --weights Example_Models/segnet_weights_driving_webdemo.caffemodel --colours Scripts/camvid12.png