如何使用Python將給定的圖像集進(jìn)行聚類(lèi)?
<a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..featureMaps'}, '*')">featureMaps = <a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..model'}, '*')">model.predict(<a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..img'}, '*')">img)
## Plotting Features
for a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..maps'}, '*')">maps in <a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..featureMaps'}, '*')">featureMaps:
plt.<a onclick="parent.postMessage({'referent':'.matplotlib.pyplot.figure'}, '*')">figure(figsize=(20,20))
<a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..pltNum'}, '*')">pltNum = 1
for <a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..a(chǎn)'}, '*')">a in range(8):
for <a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..b'}, '*')">b in <a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..range'}, '*')">range(8):
plt.<a onclick="parent.postMessage({'referent':'.matplotlib.pyplot.subplot'}, '*')">subplot(8, 8, <a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..pltNum'}, '*')">pltNum)
plt.<a onclick="parent.postMessage({'referent':'.matplotlib.pyplot.imshow'}, '*')">imshow(<a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..maps'}, '*')">maps[: ,: ,<a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..pltNum'}, '*')">pltNum - 1], cmap='gray')
<a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..pltNum'}, '*')">pltNum += 1
plt.<a onclick="parent.postMessage({'referent':'.matplotlib.pyplot.show'}, '*')">show()
接下來(lái)我們將重點(diǎn)介紹如何來(lái)創(chuàng)建我們的聚類(lèi)算法。設(shè)計(jì)圖像聚類(lèi)算法在本節(jié)中,我們使用Kaggle上的 keep-babies-safe 數(shù)據(jù)集。https://www.kaggle.com/akash14/keep-babies-safe首先,我們創(chuàng)建一個(gè)圖像聚類(lèi)模型,來(lái)將給定的圖像分為兩類(lèi),即玩具或消費(fèi)品,以下是來(lái)自該數(shù)據(jù)集的一些圖像。
以下代碼實(shí)現(xiàn)我們的聚類(lèi)算法:##################### Making Essential Imports ############################
import sklearn
import os
import sys
import matplotlib.pyplot as plt
import cv2
import pytesseract
import numpy as np
import pandas as pd
import tensorflow as tf
conf = r'-- oem 2'
#####################################
# Defining a skeleton for our #
# DataFrame #
#####################################
DataFrame = {
'photo_name' : [],
'flattenPhoto' : [],
'text' : [],
}
#######################################################################################
# The Approach is to apply transfer learning hence using Resnet50 as my #
# pretrained model #
#######################################################################################
MyModel = tf.keras.models.Sequential()
MyModel.a(chǎn)dd(tf.keras.a(chǎn)pplications.ResNet50(
include_top = False, weights='imagenet', pooling='avg',
))
# freezing weights for 1st layer
MyModel.layers[0].trainable = False
### Now defining dataloading Function
def LoadDataAndDoEssentials(path, h, w):
img = cv2.imread(path)
DataFrame['text'].a(chǎn)ppend(pytesseract.image_to_string(img, config = conf))
img = cv2.resize(img, (h, w))
## Expanding image dims so this represents 1 sample
img = img = np.expand_dims(img, 0)
img = tf.keras.a(chǎn)pplications.resnet50.preprocess_input(img)
extractedFeatures = MyModel.predict(img)
extractedFeatures = np.a(chǎn)rray(extractedFeatures)
DataFrame['flattenPhoto'].a(chǎn)ppend(extractedFeatures.flatten())
### with this all done lets write the iterrrative loop
def ReadAndStoreMyImages(path):
list_ = os.listdir(path)
for mem in list_:
DataFrame['photo_name'].a(chǎn)ppend(mem)
imagePath = path + '/' + mem
LoadDataAndDoEssentials(imagePath, 224, 224)
### lets give the address of our Parent directory and start
path = 'enter your data's path here'
ReadAndStoreMyImages(path)
######################################################
# lets now do clustering #
######################################################
Training_Feature_vector = np.a(chǎn)rray(DataFrame['flattenPhoto'], dtype = 'float64')
from sklearn.cluster import AgglomerativeClustering
kmeans = AgglomerativeClustering(n_clusters = 2)
kmeans.fit(Training_Feature_vector)
A little explanation for the above code:
上面的代碼使用Resnet50(一種經(jīng)過(guò)預(yù)先訓(xùn)練的CNN)進(jìn)行特征提取,我們只需移除其頭部或用于預(yù)測(cè)類(lèi)別的神經(jīng)元的最后一層,然后將圖像輸入到CNN并獲得特征向量作為輸出,實(shí)際上,這是我們的CNN在Resnet50的倒數(shù)第二層學(xué)習(xí)到的所有特征圖的扁平數(shù)組?梢詫⒋溯敵鱿蛄刻峁┙o進(jìn)行圖像聚類(lèi)的任何聚類(lèi)算法。讓我向你展示通過(guò)這種方法創(chuàng)建的簇。
該可視化的代碼如下## lets make this a dataFrame
import seaborn as sb
import matplotlib.pyplot as plt
dimReducedDataFrame = pd.DataFrame(Training_Feature_vector)
dimReducedDataFrame = dimReducedDataFrame.rename(columns = { 0: 'V1', 1 : 'V2'})
dimReducedDataFrame['Category'] = list (df['Class_of_image'])
plt.figure(figsize = (10, 5))
sb.scatterplot(data = dimReducedDataFrame, x = 'V1', y = 'V2',hue = 'Category')
plt.grid(True)
plt.show()
結(jié)論本文通過(guò)解釋如何使用深度學(xué)習(xí)和聚類(lèi)將視覺(jué)上相似的圖像聚在一起形成簇,而無(wú)需創(chuàng)建數(shù)據(jù)集并在其上訓(xùn)練CNN。

發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
圖片新聞
-
機(jī)器人奧運(yùn)會(huì)戰(zhàn)報(bào):宇樹(shù)機(jī)器人摘下首金,天工Ultra搶走首位“百米飛人”
-
存儲(chǔ)圈掐架!江波龍起訴佰維,索賠121萬(wàn)
-
長(zhǎng)安汽車(chē)母公司突然更名:從“中國(guó)長(zhǎng)安”到“辰致科技”
-
豆包前負(fù)責(zé)人喬木出軌BP后續(xù):均被辭退
-
字節(jié)AI Lab負(fù)責(zé)人李航卸任后返聘,Seed進(jìn)入調(diào)整期
-
員工持股爆雷?廣汽埃安緊急回應(yīng)
-
中國(guó)“智造”背后的「關(guān)鍵力量」
-
小米汽車(chē)研發(fā)中心重磅落地,寶馬家門(mén)口“搶人”
最新活動(dòng)更多
-
即日-9.1立即下載>> 【限時(shí)下載】ADI中國(guó)三十周年感恩回饋助力企業(yè)升級(jí)!
-
即日-9.16點(diǎn)擊進(jìn)入 >> 【限時(shí)福利】TE 2025國(guó)際物聯(lián)網(wǎng)展·深圳站
-
10月23日立即報(bào)名>> Works With 開(kāi)發(fā)者大會(huì)深圳站
-
11月27日立即報(bào)名>> 【工程師系列】汽車(chē)電子技術(shù)在線大會(huì)
-
精彩回顧立即查看>> 【在線研討會(huì)】解析安森美(onsemi)高精度與超低功耗CGM系統(tǒng)解決方案
-
精彩回顧立即查看>> 【在線會(huì)議】CAE優(yōu)化設(shè)計(jì):醫(yī)療器械設(shè)計(jì)的應(yīng)用案例與方案解析
推薦專(zhuān)題
- 1 傳魏建軍與賈躍亭合作,長(zhǎng)城汽車(chē)出海美國(guó)
- 2 黃仁勛:與雷軍長(zhǎng)期合作,共探AI智駕
- 3 阿里首位程序員,“掃地僧”多隆已離職
- 4 先進(jìn)算力新選擇 | 2025華為算力場(chǎng)景發(fā)布會(huì)暨北京xPN伙伴大會(huì)成功舉辦
- 5 2025年第一支10倍股,來(lái)了!
- 6 清華跑出具身智能獨(dú)角獸:給機(jī)器人安上眼睛和大腦,融資近20億
- 7 特朗普要求英特爾首位華人 CEO 辭職
- 8 騰訊 Q2 財(cái)報(bào)亮眼:AI 已成第二增長(zhǎng)曲線
- 9 具身智能機(jī)器人量產(chǎn)前夜,標(biāo)準(zhǔn)機(jī)腦正在成型
- 10 共探合作新機(jī)遇!江門(mén)市新會(huì)區(qū)(深圳)“AI + 機(jī)器人” 產(chǎn)業(yè)對(duì)接會(huì)成功舉辦