Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize simple Image clustering with Python K-means

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use Python K-means to achieve simple image clustering". In daily operation, I believe that many people have doubts about how to use Python K-means to achieve simple image clustering. Xiaobian consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful for you to answer the doubt of "how to use Python K-means to achieve simple image clustering". Next, please follow the editor to study!

The direct implementation of the first version is given here:

Import osimport numpy as npfrom sklearn.cluster import KMeansimport cv2from imutils import build_montagesimport matplotlib.image as imgpltimage_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)

The main problem that needs to be paid attention to is the understanding of K-Means principle. What K-means does is to cluster the vectors, that is, if you want to deal with a 224x224x3 RGB image, you have to convert it into an one-dimensional vector first. In the above practice, we flatten it directly:

Image = image.reshape (- 1,)

Then the drawback of doing so is also very obvious. For example, for two identical images, we translate the former one pixel to the left. After doing so, there is almost no difference in the senses of the two images, but the overall translation will lead to a great difference in the result of pixel-by-pixel comparison of the two image matrices. Taking orange car clustering as an example, the experimental results are as follows:

We can see that the result is relatively poor. Therefore, we make an improvement, using ResNet-50 for image feature extraction (embedding), clustering on the basis of features rather than directly clustering on pixels, the code is as follows:

Import osimport numpy as npfrom sklearn.cluster import KMeansimport cv2from imutils import build_montagesimport torch.nn as nnimport torchvision.models as modelsfrom PIL import Imagefrom torchvision import transformsclass 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 xnet = 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 ([224244]) (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)

It can be found that the results have improved significantly:

At this point, the study of "how to use Python K-means to achieve simple image clustering" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report