Image Semantic Segmentation of Caffe

Source: Internet
Author: User
Tags load net

(i) Download model

The author has open source code on GitHub: https://github.com/shelhamer/fcn.berkeleyvision.org, we first download the code and unzip it into the home directory.

Project file structure is very clear, if you want to train own model, only need to modify some file path settings, here we apply already train good model to test our own picture:

We download voc-fcn32s,voc-fcn16s and voc-fcn8s Caffemodel (according to the provided Caffemodel-url), Fcn-16s and fcn32s are lack of deploy.prototxt, we can change according to the train.prototxt slightly. Note that here Caffemode-url in fact in the folder of the various models have been provided to us, please look for the reader carefully, to see if there is a Caffemode-url file under each folder. Open it will have the download address of the model.

(b) To modify the addition of the infer.py file Caffe path, since the FCN code and Caffe code are separate folders, the Caffe Python interface must be added to the path. Here are two scenarios, one of which is to include the import caffe in all of your code:

1 Import sys
2 sys.path.append (' Caffe root directory/python ')
Another way to do this once and for all is to add the interface to the Pythonpath in the terminal or BASHRC:
Export Pythonpath=caffe root directory/python: $PYTHONPATH

This time we use the latter.

A file was found in the root directory of the decompression code: infer.py. Slightly modified infer.py, you can test our own pictures, please according to their actual situation to make changes.

im = Image.open (' voc-fcn8s/test.jpeg ') here refers to the path of the test picture.

NET = caffe.net (' voc-fcn8s/deploy.prototxt ', ' Voc-fcn8s/fcn8s-heavy-pascal.caffemodel ', Caffe. TEST), which refers to the deployment files and models under the voc-fcn8s file. Note that each model under FCN corresponds to a folder, and each folder should have the Caffemodel file and Prototxt file for the model.

Plt.savefig (' Test.png '), which refers to the path in which the result of the final partition should be placed, we all know that the result of semantic segmentation should be a picture.

The modified infer.py is as follows:

1 Import numpy as NP
 2 from PIL import Image
 3 import matplotlib.pyplot as PLT
 4 import Caffe
 5 
 6 # Lo Ad image, switch to BGR, subtract mean, and make dims C x H x W for Caffe
 7 im = Image.open (' voc-fcn8s/test.jpeg ') 
  
   8 in_ = Np.array (IM, dtype=np.float32)
 9 in_ = in_[:,:,::-1]
in_-= Np.array (( 104.00698793,116.66876762,122.67891434)
In_ = In_.transpose ((2,0,1)) 
# load Net net
= Caffe.net (' Voc-fcn8s/deploy.prototxt ', ' Voc-fcn8s/fcn8s-heavy-pascal.caffemodel ', Caffe. TEST)
# shape for input (data blob is N x C x H x W), set data
net.blobs[' data '].reshape (1, *in_.shape) 
   17 net.blobs[' data '].data[...] = in_
# Run NET and take Argmax for prediction Net.forward
()
= net.blobs[' score '].data[0].argmax (axis=0) 
plt.imshow (out,cmap= ' Gray ');
Plt.axis (' off ')
plt.savefig (' test.png ')
#plt. Show ()
  

(iii) Problems encountered: Insufficient memory


Since I was using the Ubuntu14.04 virtual machine to run under the beginning of the allocation of only 2G of memory will be reported that this error is not allowed

This is because there is not enough memory

Increased the amount of memory that this machine in VMware gives to virtual machines can be successful.

(d) At the end of the run, a segmented picture test.png is generated in the root directory of the software.

We can look at the difference between the original picture and the last generated picture:

Reference from:

Http://www.cnblogs.com/xuanxufeng/p/6240659.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.