Python K-means实现简单图像聚类的示例代码

 更新时间:2021年10月21日 11:27:50   作者:xiongxyowo  
本文主要介绍了Python K-means实现简单图像聚类的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

这里直接给出第一个版本的直接实现:

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做的是对向量的聚类,也就是说,假设要处理的是224×224×3的RGB图像,那么就得先将其转为1维的向量。在上面的做法里,我们是直接对其展平:

image = image.reshape(-1, )

那么这么做的缺陷也是十分明显的。例如,对于两张一模一样的图像,我们将前者向左平移一个像素。这么做下来后两张图像在感官上几乎没有任何区别,但由于整体平移会导致两者的图像矩阵逐像素比较的结果差异巨大。以橘子汽车聚类为例,实验结果如下:

在这里插入图片描述

在这里插入图片描述

可以看到结果是比较差的。因此,我们进行改进,利用ResNet-50进行图像特征的提取(embedding),在特征的基础上聚类而非直接在像素上聚类,代码如下:

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)

可以发现结果明显改善:

在这里插入图片描述

在这里插入图片描述

到此这篇关于Python K-means实现简单图像聚类的示例代码的文章就介绍到这了,更多相关Python K-means图像聚类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Tensorflow加载预训练模型和保存模型的实例

    Tensorflow加载预训练模型和保存模型的实例

    今天小编就为大家分享一篇Tensorflow加载预训练模型和保存模型的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • python利用多线程+队列技术爬取中介网互联网网站排行榜

    python利用多线程+队列技术爬取中介网互联网网站排行榜

    这篇文章主要介绍了python利用多线程+队列技术爬取中介网互联网网站排行榜,文章基于python的相关内容展开详细介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-05-05
  • python针对Oracle常见查询操作实例分析

    python针对Oracle常见查询操作实例分析

    这篇文章主要介绍了python针对Oracle常见查询操作,结合实例形式分析了python针对Oracle常见的子查询、多表查询等相关原理、操作技巧与使用注意事项,需要的朋友可以参考下
    2020-04-04
  • 对Python信号处理模块signal详解

    对Python信号处理模块signal详解

    今天小编就为大家分享一篇对Python信号处理模块signal详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-01-01
  • Python 字符替换的四方法

    Python 字符替换的四方法

    本文主要介绍了Python 字符替换的四方法,主要包括replace、translate、maketrans 和正则这是四种方法,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • python编程控制Android手机操作技巧示例

    python编程控制Android手机操作技巧示例

    这篇文章主要为大家介绍了如何利用python编程来实现控制手机的操作技巧,文中含有各种操作的示例,有需要的朋友可以借鉴参考下
    2021-10-10
  • Python 函数list&read&seek详解

    Python 函数list&read&seek详解

    这篇文章主要介绍了Python 函数list&read&seek详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Python版的文曲星猜数字游戏代码

    Python版的文曲星猜数字游戏代码

    最近开始研究python,于是写了个Python版的文曲星猜数字游戏,喜欢的朋友可以参考下
    2013-09-09
  • python版本的仿windows计划任务工具

    python版本的仿windows计划任务工具

    这篇文章主要介绍了python版本的仿windows计划任务工具,计划任务工具根据自己设定的具体时间,频率,命令等属性来规定所要执行的计划,当然功能不是很全大家可以补充
    2018-04-04
  • 浅谈Tensorflow由于版本问题出现的几种错误及解决方法

    浅谈Tensorflow由于版本问题出现的几种错误及解决方法

    今天小编就为大家分享一篇浅谈Tensorflow由于版本问题出现的几种错误及解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06

最新评论