深度學習與Face Service系列(4)__vggFace Service模型測試_Caffe

來源:互聯網
上載者:User

作者:wjmishuai 出處:http://blog.csdn.net/wjmishuai/article/details/50854168
代碼下載地址: https://github.com/PatienceKai/VGG_Face_Caffe_Model
運行代碼之前請把你的caffe環境配置好. 可以實現成C++版本的,由於項目是合作的,我不能上傳.python版是我自己寫的,流程是一樣的.

注意啊,有人反映代碼運行速度慢,主要原因是考慮到大部分人的電腦沒有支援cuda的獨顯,所以這裡使用的是cpu跑程式,如果你有獨顯並安裝了cuda,速度是飛快的....

代碼詳解:

# -*- coding: utf-8 -*-#想要加中文注釋就必須將字元編碼格式設定為utf8#作者:郭開import numpy as npimport osimport cv2import cv2.cv as cvfrom skimage import transform as tffrom PIL import Image, ImageDrawimport threadingfrom time import ctime,sleepimport timeimport sklearnimport matplotlib.pyplot as pltimport skimagecaffe_root = '/home/gk/caffe-master/'import syssys.path.insert(0, caffe_root + 'python')import caffeimport sklearn.metrics.pairwise as pw#我把GPU加速注釋掉了,所以沒有GPU加速,速度有點慢,你要在學校有條件找個有GeForce顯卡的電腦#caffe.set_mode_gpu()#載入caffe模型global netnet=caffe.Classifier('/home/gk/caffe-master/examples/VGG_net/deploy.prototxt',    '/home/gk/caffe-master/examples/VGG_net/VGG_face.caffemodel')def compar_pic(path1,path2):    global net    #載入驗證圖片    X=read_image(path1)    test_num=np.shape(X)[0]    #X  作為 模型的輸入    out = net.forward_all(data = X)    #fc7是模型的輸出,也就是特徵值    feature1 = np.float64(out['fc7'])    feature1=np.reshape(feature1,(test_num,4096))    #載入註冊圖片    X=read_image(path2)    #X  作為 模型的輸入    out = net.forward_all(data=X)    #fc7是模型的輸出,也就是特徵值    feature2 = np.float64(out['fc7'])    feature2=np.reshape(feature2,(test_num,4096))    #求兩個特徵向量的cos值,並作為是否相似的依據    predicts=pw.cosine_similarity(feature1,feature2)    return  predictsdef read_image(filelist):    averageImg = [129.1863,104.7624,93.5940]    X=np.empty((1,3,224,224))    word=filelist.split('\n')    filename=word[0]    im1=skimage.io.imread(filename,as_grey=False)    #歸一化    image =skimage.transform.resize(im1,(224, 224))*255    X[0,0,:,:]=image[:,:,0]-averageImg[0]    X[0,1,:,:]=image[:,:,1]-averageImg[1]    X[0,2,:,:]=image[:,:,2]-averageImg[2]    return Xif __name__ == '__main__':    #設定閾值,大於閾值是同一個人,反之    thershold=0.85    #載入註冊圖片與驗證圖片    #注意:人臉映像必須是N*N的!!!如果圖片的高和寬不一樣,進行歸一化的時候會對圖片進行展開,影響識別效果    reg_path="./2-1.png"    rec_path="./3-1.png"    #計算註冊圖片與驗證圖片的相似性    result=compar_pic(reg_path,rec_path)    print "%s和%s兩張圖片的相似性是:%f\n\n"%(reg_path,rec_path,result)    if result>=thershold:        print '是一個人!!!!\n\n'    else:        print '不是同一個人!!!!\n\n'



運行結果:
./1-1.png和./1-2.png兩張圖片的相似性是:0.917091
是一個人!!!!

./2-1.png和./2-2.png兩張圖片的相似性是:0.942353
是一個人!!!!

./3-1.png和./3-2.png兩張圖片的相似性是:0.890270
是一個人!!!!



./1-1.png和./2-1.png兩張圖片的相似性是:0.714049
不是同一個人!!!!

./1-1.png和./3-1.png兩張圖片的相似性是:0.790391
不是同一個人!!!!

./2-1.png和./3-1.png兩張圖片的相似性是:0.773282
不是同一個人!!!!

演算法效能:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.