您好,登錄后才能下訂單哦!
這篇文章主要介紹“怎么用Python K-means實現(xiàn)簡單圖像聚類”,在日常操作中,相信很多人在怎么用Python K-means實現(xiàn)簡單圖像聚類問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用Python K-means實現(xiàn)簡單圖像聚類”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
這里直接給出第一個版本的直接實現(xiàn):
import os import numpy as np from sklearn.cluster import KMeans import cv2 from imutils import build_montages import matplotlib.image as imgplt image_path = [] all_images = [] images = os.listdir('./images') for image_name in images: image_path.append('./images/' + image_name) for path in image_path: image = imgplt.imread(path) image = image.reshape(-1, ) all_images.append(image) clt = KMeans(n_clusters=2) clt.fit(all_images) labelIDs = np.unique(clt.labels_) for labelID in labelIDs: idxs = np.where(clt.labels_ == labelID)[0] idxs = np.random.choice(idxs, size=min(25, len(idxs)), replace=False) show_box = [] for i in idxs: image = cv2.imread(image_path[i]) image = cv2.resize(image, (96, 96)) show_box.append(image) montage = build_montages(show_box, (96, 96), (5, 5))[0] title = "Type {}".format(labelID) cv2.imshow(title, montage) cv2.waitKey(0)
主要需要注意的問題是對K-Means原理的理解。K-means做的是對向量的聚類,也就是說,假設(shè)要處理的是224×224×3的RGB圖像,那么就得先將其轉(zhuǎn)為1維的向量。在上面的做法里,我們是直接對其展平:
image = image.reshape(-1, )
那么這么做的缺陷也是十分明顯的。例如,對于兩張一模一樣的圖像,我們將前者向左平移一個像素。這么做下來后兩張圖像在感官上幾乎沒有任何區(qū)別,但由于整體平移會導致兩者的圖像矩陣逐像素比較的結(jié)果差異巨大。以橘子汽車聚類為例,實驗結(jié)果如下:
可以看到結(jié)果是比較差的。因此,我們進行改進,利用ResNet-50進行圖像特征的提取(embedding),在特征的基礎(chǔ)上聚類而非直接在像素上聚類,代碼如下:
import os import numpy as np from sklearn.cluster import KMeans import cv2 from imutils import build_montages import torch.nn as nn import torchvision.models as models from PIL import Image from torchvision import transforms class Net(nn.Module): def __init__(self): super(Net, self).__init__() resnet50 = models.resnet50(pretrained=True) self.resnet = nn.Sequential(resnet50.conv1, resnet50.bn1, resnet50.relu, resnet50.maxpool, resnet50.layer1, resnet50.layer2, resnet50.layer3, resnet50.layer4) def forward(self, x): x = self.resnet(x) return x net = Net().eval() image_path = [] all_images = [] images = os.listdir('./images') for image_name in images: image_path.append('./images/' + image_name) for path in image_path: image = Image.open(path).convert('RGB') image = transforms.Resize([224,244])(image) image = transforms.ToTensor()(image) image = image.unsqueeze(0) image = net(image) image = image.reshape(-1, ) all_images.append(image.detach().numpy()) clt = KMeans(n_clusters=2) clt.fit(all_images) labelIDs = np.unique(clt.labels_) for labelID in labelIDs: idxs = np.where(clt.labels_ == labelID)[0] idxs = np.random.choice(idxs, size=min(25, len(idxs)), replace=False) show_box = [] for i in idxs: image = cv2.imread(image_path[i]) image = cv2.resize(image, (96, 96)) show_box.append(image) montage = build_montages(show_box, (96, 96), (5, 5))[0] title = "Type {}".format(labelID) cv2.imshow(title, montage) cv2.waitKey(0)
可以發(fā)現(xiàn)結(jié)果明顯改善:
到此,關(guān)于“怎么用Python K-means實現(xiàn)簡單圖像聚類”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。