import numpy as np caffe_root = "/home/your_name/Downloads/caffe-master/" import sys sys.path.insert(0, caffe_root+"python") import os import caffe import io
caffe.set_mode_cpu() MODEL_FILE = caffe_root + "models/colon_caffenet/colon_deploy.prototxt" #change the deploy file same as the train_val.prototxt PRETRAINED = caffe_root + "models/colon_caffenet/caffenet_train_iter_80.caffemodel" MEAN = caffe_root + "examples/colon/colon_mean.npy" # later I will tell how to tranform *.binaryproto to *.npy net = caffe.Classifier(MODEL_FILE, PRETRAINED, mean = np.load(MEAN).mean(1).mean(1), channel_swap=(2,1,0), raw_scale=255, image_dims=(256, 256))
filewriter = open(caffe_root+"data/colon/test_result.txt","w+") for root,dirs,files in os.walk(caffe_root+"data/colon/test/"): # all the images are in test folder for file in files IMAGE_FILE = os.path.join(root,file).decode('gbk').encode('utf8') input_image = caffe.io.load_image(IMAGE_FILE) prediction = net.predict([input_image]) string = os.path.basename(IMAGE_FILE)+" "+str(prediction[0].argmax())+"\n" filewriter(string) print os.path.basename(IMAGE_FILE), prediction[0].argmax()
filewriter.close()
convert binaryproto to npy
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import caffe import numpy as np import sys
if len(sys.argv) != 3: print"Usage: python convert_protomean.py proto.mean out.npy" sys.exit()
blob = caffe.proto.caffe_pb2.BlobProto() data = open( sys.argv[1] , 'rb' ).read() blob.ParseFromString(data) arr = np.array( caffe.io.blobproto_to_array(blob) ) out = arr[0] np.save( sys.argv[2] , out )
Meets lots of problems when import caffe
When python executes “import caffe”, many problems comes out. All problems are about the python packages. Although I have set up the python library pointing to anaconda2 lib, it seems programme will find all the dependencies in /usr/lib/python2.7/dist-packages. So if you have encountered the same issues, copy all dependencies in /anaconda2/lib/python2.7/site-packages to /usr/lib/python2.7/dist-packages