資源簡介
python 程序的圖像目標(biāo)識(shí)別與分類程序。
代碼片段和文件信息
#!/usr/bin/env?python
#-*-coding:utf-8-*-
import?numpy?as?np??
import?sysos??
#?設(shè)置當(dāng)前的工作環(huán)境在caffe下??
caffe_root?=?‘/home/xxx/workspace/caffe-master/‘???
#?我們也把caffe/python也添加到當(dāng)前環(huán)境??
sys.path.insert(0?caffe_root?+?‘python‘)??
import?caffe??
os.chdir(caffe_root)#更換工作目錄??
#GPU模式
caffe.set_device(0)??#?如果你有多個(gè)GPU,那么選擇第一個(gè)
caffe.set_mode_gpu()
#?設(shè)置網(wǎng)絡(luò)結(jié)構(gòu)??
net_file=caffe_root?+?‘models/bvlc_reference_caffenet/deploy.prototxt‘??
#?添加訓(xùn)練之后的參數(shù)??
caffe_model=caffe_root?+?‘models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel‘??
#?均值文件??
mean_file=caffe_root?+?‘python/caffe/imagenet/ilsvrc_2012_mean.npy‘??
#?這里對任何一個(gè)程序都是通用的,就是處理圖片??
#?把上面添加的兩個(gè)變量都作為參數(shù)構(gòu)造一個(gè)Net??
net?=?caffe.Net(net_filecaffe_modelcaffe.TEST)??
#?得到data的形狀,這里的圖片是默認(rèn)matplotlib底層加載的??
transformer?=?caffe.io.Transformer({‘data‘:?net.blobs[‘data‘].data.shape})??
#?matplotlib加載的image是像素[0-1]圖片的數(shù)據(jù)格式[weighthighchannels],RGB??
#?caffe加載的圖片需要的是[0-255]像素,數(shù)據(jù)格式[channelsweighthigh]BGR,那么就需要轉(zhuǎn)換??
#?channel?放到前面??
transformer.set_transpose(‘data
評論
共有 條評論