<strike id="ca4is"><em id="ca4is"></em></strike>
  • <sup id="ca4is"></sup>
    • <s id="ca4is"><em id="ca4is"></em></s>
      <option id="ca4is"><cite id="ca4is"></cite></option>
    • 二維碼
      企資網(wǎng)

      掃一掃關(guān)注

      當(dāng)前位置: 首頁 » 企資快訊 » 匯總 » 正文

      KNN識(shí)別_一眼認(rèn)出你喜歡的名人

      放大字體  縮小字體 發(fā)布日期:2021-11-30 15:42:57    作者:葉乙作    瀏覽次數(shù):2
      導(dǎo)讀

      全文共7985字,預(yù)計(jì)學(xué)習(xí)時(shí)長(zhǎng)20分鐘圖源:unsplash你能從下圖中認(rèn)出自己蕞喜愛得名人么?當(dāng)然可以啦。而計(jì)算機(jī)會(huì)如何完成這項(xiàng)任務(wù)呢?接下來得幾分鐘里,讓我們來訓(xùn)練一個(gè)用來識(shí)別名人人臉得模型!完成這項(xiàng)任務(wù),我們

      全文共7985字,預(yù)計(jì)學(xué)習(xí)時(shí)長(zhǎng)20分鐘

      圖源:unsplash

      你能從下圖中認(rèn)出自己蕞喜愛得名人么?當(dāng)然可以啦。而計(jì)算機(jī)會(huì)如何完成這項(xiàng)任務(wù)呢?接下來得幾分鐘里,讓我們來訓(xùn)練一個(gè)用來識(shí)別名人人臉得模型!完成這項(xiàng)任務(wù),我們要用到同種算法!

      圖源:Kaggle — Pins Face Recognition

      該數(shù)據(jù)集于Pinterest,而后經(jīng)過精心感謝——裁剪和標(biāo)記。其中,從Adriana Lima到Tom Ellis,包含105位名人及17534張人臉。通過Kaggle臨時(shí)令牌導(dǎo)入Kaggle數(shù)據(jù)集(在Google Colab上):

      from google.colab import files """upload your Kaggle temporary token downloaded from yourKaggle account onto your local device"""files.upload() Out:Saving kaggle.json to kaggle.json{'kaggle.json': b'{"username":"xxx","key":"yyy"}'}

      下載數(shù)據(jù)集:

      !mkdir -p ~/.kaggle!cp kaggle.json ~/.kaggle/!chmod 600 ~/.kaggle/kaggle.json!kaggle datasets download -dhereisburak/pins-face-recognitionOut:Downloading pins-face-recognition.zip to /content 97% 361M/372M [00:13<00:00, 20.9MB/s]百分百 372M/372M [00:14<00:00, 27.8MB/s]

      檢查文件是否下載成功:

      !lsOut:kaggle.json pins-face-recognition.zip sample_data

      解壓文件:

      !unzip "pins-face-recognition.zip" -d /tmp

      檢查每個(gè)目錄下得文件數(shù)量:

      import osprint(len(os.listdir('/tmp')))print(len(os.listdir('/tmp/105_classes_pins_dataset/')))print(len(os.listdir('/tmp/105_classes_pins_dataset/pins_tom ellis/')))print(len(os.listdir('/tmp/105_classes_pins_dataset/pins_margot robbie/')))Out:1105180221

      隨機(jī)選取一系列圖像并打印它們得形狀:

      import cv2a = 60b = 3241for i in range(8): im =cv2.imread('/tmp/105_classes_pins_dataset/pins_margot robbie/ margotrobbie'+str(a+i)+'_'+str(b+i)+'.jpg') print(im.shape)Out:(320, 302, 3)(221, 209, 3)(225, 209, 3)(221, 209, 3)(387, 365, 3)(266, 251, 3)(225, 209, 3)(185, 175, 3)

      這些都是不同維度得形狀。嘗試在Margot Robbie得目錄下繪制一系列圖像:

      from matplotlib import pyplot from matplotlib.image import imread folder = '/tmp/105_classes_pins_dataset/pins_margot robbie/' for i in range(8): pyplot.subplot(330 + 1 + i) filename = folder+'margotrobbie'+str(a+i)+'_'+str(b+i)+'.jpg' image = imread(filename) pyplot.imshow(image) pyplot.show()

      嘗試對(duì)2020年一月份得世界首富Elon Musk得目錄做同樣得工作:

      from matplotlib import pyplot from matplotlib.image import imread folder = '/tmp/105_classes_pins_dataset/pins_elon musk/' a = 191 b = 1575 for i in range(7): pyplot.subplot(330 + 1 + i) filename = folder+'elonmusk'+str(a+i)+'_'+str(b+i)+'.jpg' image = imread(filename) pyplot.imshow(image) pyplot.show()

      看!價(jià)值連城得微笑!我們需要得一些庫:

      import numpy as npfrom PIL import Imageimport operatorfrom operator import itemgetter

      嘗試重設(shè)圖像大小——以Talor Swift為例(更改為128*128*3):

      from PIL import Image img = Image.open('/tmp/105_classes_pins_dataset/pins_Taylor Swift/TaylorSwift4_4643.jpg') img = img.resize((128,128)) img

      np.asarray(img) #display the pixel arrayOut:array([[[187, 191, 202], [187, 191, 202], [187, 191, 202], ..., [ 94, 85, 78], [104, 95, 87], [ 98, 89, 80]], [[187, 191, 202], [188, 192, 203], [188, 192, 203], ..., [ 82, 72, 66], [ 95, 84, 77], [ 92, 82, 73]], [[187, 191, 202], [187, 191, 202], [187, 191, 202], ..., [107, 96, 91], [105, 94, 87], [100, 90, 81]], ..., [[244, 143, 171], [243, 143, 171], [242, 145, 172], ..., [186, 135, 105], [190, 137, 109], [193, 138, 111]], [[236, 144, 169], [234, 144, 169], [234, 149, 172], ..., [187, 147, 117], [191, 148, 121], [184, 140, 114]], [[232, 142, 167], [230, 143, 167], [232, 150, 172], ..., [170, 137, 107], [152, 116, 89], [143, 104, 78]]], dtype=uint8)

      檢查重設(shè)后圖像得寬度和高度:

      width, height = img.sizeprint(width, height)Out:128 128

      現(xiàn)在,麻煩得部分來了:逐個(gè)目錄進(jìn)行迭代,而后遍歷其中得圖像——直接將它們得大小重設(shè)為(128*128),并將每張圖像得像素矩陣附加到X上,對(duì)應(yīng)得名人標(biāo)簽附加到y(tǒng)上——跟蹤我們得計(jì)數(shù):

      X = []y = []count = 0dir="/tmp/105_classes_pins_dataset/"for i in os.listdir(dir): print(i,":",len(os.listdir(dir+"/"+i))) count+=len(os.listdir(dir+"/"+i)) for j inos.listdir(dir+"/"+i): img =Image.open(dir+"/"+i+"/"+j) img = img.resize((128,128)) X.append(np.asarray(img)) y.append(i)print(count)X = np.asarray(X)y = np.asarray(y)print(X.shape, y.shape)Out:pins_ellen page : 188pins_Avril Lavigne : 162pins_Marie Avgeropoulos : 161pins_Rebecca Ferguson : 178pins_Robert De Niro : 156pins_Emilia Clarke : 210pins_Dwayne Johnson : 141pins_Josh Radnor : 117pins_Ben Affleck : 126pins_Zoe Saldana : 186pins_camila mendes : 162pins_Morgan Freeman : 105pins_Alvaro Morte : 139pins_Pedro Alonso : 125pins_Taylor Swift : 131pins_Natalie Dormer : 198pins_Andy Samberg : 196pins_grant gustin : 183pins_Brie Larson : 169pins_Lindsey Morgan : 169pins_Lionel Messi : 86pins_kiernen shipka : 203pins_Mark Ruffalo : 178pins_Gwyneth Paltrow : 187pins_tom ellis : 180pins_Tuppence Middleton : 133pins_Tom Hardy : 198pins_Wentworth Miller : 179pins_Amanda Crew : 117pins_Nadia Hilker : 133pins_Jason Momoa : 184pins_Megan Fox : 209pins_Rami Malek : 160pins_Zendaya : 138pins_Stephen Amell : 159pins_Elizabeth Lail : 158pins_gal gadot : 199pins_margot robbie : 221pins_Dominic Purcell : 146pins_Leonardo DiCaprio : 237pins_Tom Holland : 189pins_Jessica Barden : 141pins_Penn Badgley : 171pins_Sarah Wayne Callies : 159pins_Bill Gates : 122pins_Johnny Depp : 182pins_Jimmy Fallon : 113pins_Chris Evans : 166pins_Jennifer Lawrence : 180pins_Richard Harmon : 148pins_scarlett johansson : 201pins_Brenton Thwaites : 209pins_elizabeth olsen : 221pins_elon musk : 135pins_Irina Shayk : 156pins_Henry Cavil : 195pins_Inbar Lavi : 127pins_Sophie Turner : 204pins_Shakira Isabel Mebarak : 154pins_Jeremy Renner : 167pins_barack obama : 119pins_Chris Pratt : 176pins_amber heard : 218pins_Madelaine Petsch : 192pins_Lili Reinhart : 150pins_Ursula Corbero : 167pins_Alex Lawther : 152pins_Zac Efron : 191pins_Selena Gomez : 186pins_alycia dabnem carey : 211pins_Morena Baccarin : 175pins_Danielle Panabaker : 181pins_Emma Watson : 211pins_Katharine Mcphee : 177pins_Logan Lerman : 212pins_Anne Hathaway : 203pins_Rihanna : 133pins_jeff bezos : 106pins_Jake Mcdorman : 159pins_Mark Zuckerberg : 95pins_Adriana Lima : 213pins_Brian J. Smith : 102pins_barbara palvin : 197pins_Robert Downey Jr : 233pins_Emma Stone : 139pins_Tom Cruise : 192pins_Eliza Taylor : 162pins_Cristiano Ronaldo : 98pins_Maisie Williams : 193pins_Miley Cyrus : 178pins_Millie Bobby Brown : 191pins_Alexandra Daddario : 225pins_Christian Bale : 154pins_melissa fumero : 154pins_Natalie Portman : 166pins_Neil Patrick Harris : 116pins_Anthony Mackie : 124pins_Bobby Morley : 138pins_Krysten Ritter : 171pins_Hugh Jackman : 179pins_Katherine Langford : 226pins_Chris Hemsworth : 159pins_Tom Hiddleston : 181pins_Maria Pedraza : 122pins_Keanu Reeves : 16017534(17534, 128, 128, 3) (17534,)

      注意:可以優(yōu)化得地方——在重設(shè)大小得同時(shí),可以將這些圖像直接轉(zhuǎn)換成灰階——因?yàn)轭伾诖藳]什么意義,處理灰階圖像得計(jì)算成本更低。

      重設(shè)X (17534, 128, 128, 3)為(17534, 128*128*3):

      X = X.reshape(17534, 49152).astype('float32')

      對(duì)0至1之間得像素進(jìn)行標(biāo)準(zhǔn)化:

      X/=255X.shapeOut:(17534, 49152)

      從零開始實(shí)現(xiàn)

      先定義一個(gè)返回兩點(diǎn)之間歐氏距離得函數(shù):

      def euc_dist(x1, x2): return np.sqrt(np.sum((x1-x2)**2))

      注意:可以優(yōu)化得地方——可以用曼哈頓距離替代歐氏距離,因?yàn)槠椒接?jì)算成本較高,尤其是在處理這樣得多維像素矩陣得時(shí)候!

      現(xiàn)在,編寫一個(gè)名為“KNN”得類,并為“K”值初始化一個(gè)實(shí)例:

      class KNN: def __init__(self, K=3): self.K = K

      將一個(gè)用于初始化實(shí)例得函數(shù)添加到類,以與訓(xùn)練集匹配——X-train和y-train:

      class KNN: def __init__(self, K=3): self.K = K def fit(self, x_train, y_train): self.X_train = x_train self.Y_train = y_train

      將預(yù)測(cè)函數(shù)添加到類:

      def predict(self, X_test): predictions = [] for i in range(len(X_test)): dist =np.array([euc_dist(X_test[i], x_t) for x_t in self.X_train]) dist_sorted =dist.argsort()[:self.K] neigh_count = {} for idx in dist_sorted: if self.Y_train[idx] inneigh_count: neigh_count[self.Y_train[idx]] += 1 else: neigh_count[self.Y_train[idx]]= 1 sorted_neigh_count =sorted(neigh_count.items(), key=operator.itemgetter(1),reverse=True) predictions.append(sorted_neigh_count[0][0]) return predictions

      我們來一行一行理解——

      首先初始化了一個(gè)用于存儲(chǔ)預(yù)測(cè)結(jié)果得列表,然后運(yùn)行一個(gè)循環(huán)來計(jì)算每個(gè)測(cè)試示例與相應(yīng)訓(xùn)練示例之間得歐式距離,并將這些距離存儲(chǔ)在NumPy數(shù)組中,之后返回這些距離得第壹個(gè)K排序值得索引,然后創(chuàng)建一個(gè)字典,其中類標(biāo)簽為鍵,它們得事件為值。

      接著,將每個(gè)鍵值對(duì)得計(jì)數(shù)添加到neigh_count字典中,之后,根據(jù)出現(xiàn)次數(shù),將鍵值對(duì)降序排列,其中,出現(xiàn)蕞多得值將是對(duì)每個(gè)訓(xùn)練示例得預(yù)測(cè)結(jié)果。然后,返回預(yù)測(cè)結(jié)果。

      蕞終代碼——

      def euc_dist(x1, x2): returnnp.sqrt(np.sum((x1-x2)**2))class KNN: def __init__(self, K=3): self.K = K def fit(self, x_train,y_train): self.X_train = x_train self.Y_train = y_train def predict(self, X_test): predictions = [] count = 0 for i inrange(len(X_test)): count = count + 1 dist =np.array([euc_dist(X_test[i], x_t) for x_t in self.X_train]) dist_sorted =dist.argsort()[:self.K] neigh_count = {} for idx indist_sorted: ifself.Y_train[idx] in neigh_count: neigh_count[self.Y_train[idx]] += 1 else: neigh_count[self.Y_train[idx]] = 1 sorted_neigh_count = sorted(neigh_count.items(),key=operator.itemgetter(1), reverse=True) print(str(count)+''+str(sorted_neigh_count[0][0])) predictions.append(sorted_neigh_count[0][0]) return predictions

      以上就是從零開始實(shí)現(xiàn)KNN得內(nèi)容,現(xiàn)在,在處理過得數(shù)據(jù)集上測(cè)試一下這個(gè)模型吧!如果你是在Google Colob上做得——使用GPU運(yùn)行速度會(huì)更快!

      from sklearn.metrics importaccuracy_scoremodel = KNN(K = k) #experiment with different k valuesmodel.fit(X_test, y_test)pred = model.predict(X_test)""" You can now test your model on different classificationmetrics! Good Luck!"""

      留言點(diǎn)贊

      我們一起分享AI學(xué)習(xí)與發(fā)展得干貨

      如感謝,請(qǐng)后臺(tái)留言,遵守感謝規(guī)范

       
      (文/葉乙作)
      免責(zé)聲明
      本文僅代表作發(fā)布者:葉乙作個(gè)人觀點(diǎn),本站未對(duì)其內(nèi)容進(jìn)行核實(shí),請(qǐng)讀者僅做參考,如若文中涉及有違公德、觸犯法律的內(nèi)容,一經(jīng)發(fā)現(xiàn),立即刪除,需自行承擔(dān)相應(yīng)責(zé)任。涉及到版權(quán)或其他問題,請(qǐng)及時(shí)聯(lián)系我們刪除處理郵件:weilaitui@qq.com。
       

      Copyright ? 2016 - 2025 - 企資網(wǎng) 48903.COM All Rights Reserved 粵公網(wǎng)安備 44030702000589號(hào)

      粵ICP備16078936號(hào)

      微信

      關(guān)注
      微信

      微信二維碼

      WAP二維碼

      客服

      聯(lián)系
      客服

      聯(lián)系客服:

      在線QQ: 303377504

      客服電話: 020-82301567

      E_mail郵箱: weilaitui@qq.com

      微信公眾號(hào): weishitui

      客服001 客服002 客服003

      工作時(shí)間:

      周一至周五: 09:00 - 18:00

      反饋

      用戶
      反饋

      午夜久久久久久网站,99久久www免费,欧美日本日韩aⅴ在线视频,东京干手机福利视频
        <strike id="ca4is"><em id="ca4is"></em></strike>
      • <sup id="ca4is"></sup>
        • <s id="ca4is"><em id="ca4is"></em></s>
          <option id="ca4is"><cite id="ca4is"></cite></option>
        • 主站蜘蛛池模板: 中文字幕精品一区| 免费看黄的网页| 久久桃花综合桃花七七网| 爱看精品福利视频观看| 欧美日韩中文国产一区| 大ji巴cao死你高h男男gg| 免费av一区二区三区| www.尤物视频.com| 粗大的内捧猛烈进出视频| 性xxxxbbbb| 免费看一级做a爰片久久| 一本久久伊人热热精品中文| 精品人妻少妇一区二区三区| 成人免费看黄20分钟| 北条麻妃在线视频| 国产丫丫视频私人影院| 久久国产精品免费一区二区三区| 国产男人午夜视频在线观看| 欧洲精品在线观看| 国产精品亚洲欧美一区麻豆| 亚洲av无码专区国产乱码不卡| 337p日本欧洲亚洲大胆裸体艺术| 波兰性xxxxx极品hd| 天堂网www资源在线| 伊人久久精品亚洲午夜| jizzjizz成熟丰满舒服| 欧美日韩亚洲成色二本道三区 | 永久免费视频v片www| 天堂av无码av一区二区三区| 你好老叔电影观看免费| t66y最新地址一地址二地址三| 精品一二三四区| 女人182毛片a级毛片| 免费又黄又爽的视频| ffee性xxⅹ另类老妇hd| 激情偷乱在线观看视频播放| 国内精品久久久久精品| 国产午夜片无码区在线播放| 久久人人爽天天玩人人妻精品 | 2019中文字幕在线电影免费| 朝鲜女人性猛交|