Image feature extraction with Caffe
Including some examples classify the Oxford-IIIT Pet Dataset using scikit-learn
Caffe, Python 2, NumPy, scikit-learn, matplotlib
Download the CaffeNet modelfile to caffe/models/bvlc_reference_caffenet/ and other dependent files by this script
➜ ./data/ilsvrc12/get_ilsvrc_aux.sh
Modify the deploy.prototxt file as following
➜ cp models/bvlc_reference_caffenet/deploy.prototxt
models/bvlc_reference_caffenet/deploy_feature.prototxt
➜ gedit models/bvlc_reference_caffenet/deploy_feature.prototxt
# line 152
layer {
name: "fc6"
type: "InnerProduct"
bottom: "pool5"
# top: "fc6" # comment out
top: "fc6wi" # add
inner_product_param {
num_output: 4096
}
}
layer {
name: "relu6"
type: "ReLU"
# bottom: "fc6" # comment out
bottom: "fc6wi" # add
top: "fc6"
}
Make sure that the path to caffe_root (-c), images (-p) are correctly designated.
You should prepare a '.npy' file (-i) contains image filenames in numpy.array.
If you use a GPU, set the device ID (-g).
➜ python feature_extract.py -c caffe/ -p images/ -i image_filenames.npy -g 0
> accuracy = sklearn.metrics.accuracy_score(test_labels, predicts)
> print(accuracy)
0.829838709677
> report = sklearn.metrics.classification_report(test_labels, predicts, target_names)
> print(report)
precision recall f1-score support
american bulldog 0.66 0.80 0.72 50
american pit bull terrier 0.66 0.58 0.62 50
basset hound 0.78 0.80 0.79 50
beagle 0.71 0.60 0.65 50
boxer 0.71 0.78 0.74 50
chihuahua 0.89 0.78 0.83 50
english cocker spaniel 0.81 0.84 0.82 50
english setter 0.85 0.78 0.81 50
german shorthaired 0.95 0.82 0.88 50
great pyrenees 0.84 0.84 0.84 50
havanese 0.88 0.84 0.86 50
japanese chin 0.93 0.86 0.90 50
keeshond 0.98 0.98 0.98 50
leonberger 0.88 0.92 0.90 50
miniature pinscher 0.87 0.82 0.85 50
newfoundland 0.85 0.92 0.88 50
pomeranian 0.95 0.78 0.86 50
pug 0.96 0.98 0.97 50
saint bernard 0.81 0.86 0.83 50
samoyed 0.84 0.94 0.89 50
scottish terrier 0.90 0.90 0.90 49
shiba inu 0.79 0.88 0.83 50
staffordshire bull terrier 0.56 0.66 0.61 41
wheaten terrier 0.79 0.84 0.82 50
yorkshire terrier 0.96 0.92 0.94 50
avg / total 0.83 0.83 0.83 1240