I wanted to run caffe in anaconda environment, but it didn't work even if I changed Makefile.config. Make a note of the build procedure in an environment that does not use version control. If you make a mistake, I would appreciate it if you could contact me.
General dependencies
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
14.04
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
Click here for other versions http://caffe.berkeleyvision.org/install_apt.html
Click here for other OS http://caffe.berkeleyvision.org/installation.html
git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config
Makefile.config
# USE_CUDNN := 1
↓
USE_CUDNN := 1
#cuda modified to suit your environment
CUDA_DIR := /usr/local/cuda-7.0
#If I wanted to do it with anaconda, I should be able to change this, but it didn't work in my environment.
ANACONDA_HOME := $(HOME)/anaconda
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
PYTHON_LIB := $(ANACONDA_HOME)/lib
cmake .
make -j4 all
make install
make clean and make -j4 all make clean: Deletes the intermediate file created when creating an application and the application itself as a result of creation.
python compilation etc.
sudo apt-get install python-dev python-pip python-numpy python-skimage gfortran
sudo pip install -r ~/caffe/python/requirements.txt
make pycaffe
Pass through
export PYTHONPATH=/home/ubuntu/caffe/build/caffe/python/:$PYTHONPATH
OK if you can import
python
>>>import caffe
Set the path
.badhrc
export CAFFE_HOME=Where you installed caffe
export PATH=${CAFFE_HOME}/bin:${PATH}
export LD_LIBRARY_PATH=${CAFFE_HOME}/lib:${LD_LIBRARY_PATH}
export PYTHONPATH=${CAFFE_HOME}/python:${PYTHONPATH}
※Caution Even with this, classify.py may not work. http://qiita.com/Bonnnou_108/items/41e6dadeff1310b4eb5d
If you follow the yahoo article, it should work, but you may get an error. https://techblog.yahoo.co.jp/programming/caffe-intro/
python classify.py --raw_scale 255 ../101_ObjectCategories/airplanes/image_0001.jpg ../result.npy
Details of the error
ValueError: Mean shape incompatible with input shape
Edit here caffe/python/caffe/io.py
if ms != self.inputs[in_][1:]:
raise ValueError('Mean shape incompatible with input shape.')
↓
if ms != self.inputs[in_][1:]:
print(self.inputs[in_])
in_shape = self.inputs[in_][1:]
m_min, m_max = mean.min(), mean.max()
normal_mean = (mean - m_min) / (m_max - m_min)
mean = resize_image(normal_mean.transpose((1,2,0)),in_shape[1:]).transpose((2,0,1)) * (m_max - m_min) + m_min
#raise ValueError('Mean shape incompatible with input shape.')
Caffe Model Zoo
http://www.ics.uci.edu/~xzhu/face/ http://www.openu.ac.il/home/hassner/Adience/data.html
age_net.caffemodel ... age classification model deploy_age.prototxt ... Age classification numbers and labels mean.binaryproto ... for average images gender_net.caffemodel ... Gender classification model deploy_gender.prototxt ... Gender classification numbers and labels
wget http://www.openu.ac.il/home/hassner/projects/cnn_agegender/cnn_age_gender_models_and_data.0.0.1.zip
unzip cnn_age_gender_models_and_data.0.0.1.zip
import
import os
import numpy as np
import matplotlib.pyplot as plt
caffe_root = './caffe-master/'
import sys
sys.path.insert(0, caffe_root + 'python')
import caffe
plt.rcParams['figure.figsize'] = (10, 10)
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
mean_filename='./mean.binaryproto'
proto_data = open(mean_filename, "rb").read()
a = caffe.io.caffe_pb2.BlobProto.FromString(proto_data)
mean = caffe.io.blobproto_to_array(a)[0]
age_net_pretrained='./age_net.caffemodel'
age_net_model_file='./deploy_age.prototxt'
age_net = caffe.Classifier(age_net_model_file, age_net_pretrained,
mean=mean,
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
gender_net_pretrained='./gender_net.caffemodel'
gender_net_model_file='./deploy_gender.prototxt'
gender_net = caffe.Classifier(gender_net_model_file, gender_net_pretrained,
mean=mean,
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
Labels
age_list=['(0, 2)','(4, 6)','(8, 12)','(15, 20)','(25, 32)','(38, 43)','(48, 53)','(60, 100)']
gender_list=['Male','Female']
example_image = './example_image.jpg'
input_image = caffe.io.load_image(example_image)
_ = plt.imshow(input_image)
prediction = age_net.predict([input_image])
print 'predicted age:', age_list[prediction[0].argmax()]
Output result: predicted age: (0, 2)
prediction = gender_net.predict([input_image])
print 'predicted gender:', gender_list[prediction[0].argmax()]
Output result: predicted gender: Female
In CNN, weights are called filters.
def showimage(im):
if im.ndim == 3:
im = im[:, :, ::-1]
plt.set_cmap('jet')
plt.imshow(im)
def vis_square(data, padsize=1, padval=0):
data -= data.min()
data /= data.max()
#Force the number of filters to be square
n = int(np.ceil(np.sqrt(data.shape[0])))
padding = ((0, n ** 2 - data.shape[0]), (0, padsize), (0, padsize)) + ((0, 0),) * (data.ndim - 3)
data = np.pad(data, padding, mode='constant', constant_values=(padval, padval))
#Tile the filter to the image
data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
showimage(data)
age_net = caffe.Classifier(age_net_model_file, age_net_pretrained,
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
prediction = age_net.predict([input_image])
Functions that start with input or _ may appear. In python, _ may be used to mean that it can be referenced from an external class but not referenced.
_ = plt.imshow(input_image)
filters = age_net.params['conv1'][0].data[:49]
vis_square(filters.transpose(0, 2, 3, 1))
feat = age_net.blobs['conv1'].data[4, :49]
vis_square(feat, padval=1)
cd ~/caffe
scripts/download_model_binary.py models/bvlc_reference_caffenet
python examples/finetune_flickr_style/assemble_data.py --workers=1 --images=2000 --seed 831486
./build/tools/caffe train -solver models/finetune_flickr_style/solver.prototxt -weights models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel
Regression Methods for Localization
Mountains, roads, runners To understand the image ● Object localization ● Object segmentation ● Human pose estimation Formulated as a DNN-based regression ● Deep Neural Net-based Regression ● Object Mask Regression ● Object Bounding Box Regression ● Human Pose Estimation DNN-based Regression
fine-tuning http://hirotaka-hachiya.hatenablog.com/entry/2015/02/21/072255
https://bfeba431-a-62cb3a1a-s-sites.googlegroups.com/site/deeplearningcvpr2014/RegressionMethodsforLocalization.pdf?attachauth=ANoY7cpwt94hVCZXGTsfVWXtfKcakWqTiHT9TEYM6tLGdUk2jmlmBHiyEaL3qByRJEeBn-2EtPhanI3uoT58LSiRDl_A6JP51_8jm8LqcbyLZYo2bSMJpvbmCXlP4fMiRtJLT7nXmUu0QERcZEnYd_Ly-kka7TKKUEyk4-ez1iXr5ROM-G_2jjLa21y3y1y6s9sQK3Q0KJVvIHAAmzKn7uonJ4N2Q3f3dLVs7QQyw4MgIDd_ZiYcati9Ktkjq51cvzjLOPMfa7d6&attredirects=0
Recommended Posts