我正在尝试创建一个模型,该模型将使用K均值聚类来预测图像中的主色。我已经建立了所有的数据,但我不确定在拟合模型后如何继续。谢谢
from sklearn.cluster import KMeans
import h5py
train_data = h5py.File('x_train.h5','r')
test_data = h5py.File('x_test.h5','r')
x_train = train_data['train'][:]
x_test = test_data['test'][:]
print(x_train.shape) # (429-number of images, 416-height,416-width, 3-channels)
x_train = x_train/255.0
x_test = x_test/255.0
X_train = x_train.reshape(len(x_train),-1)
X_test = x_test.reshape(len(x_test),-1)
kmeans = KMeans(n_clusters = 5)
# Fitting the model to training set
kmeans.fit(X_train)
#------edit------
pred = kmeans.predict(X_test[0])
labels=pred.labels_
labels=list(labels)
centroid=pred.cluster_centers_
percent=[]
for i in range(len(centroid)):
x=labels.count(i)
x=x/(len(labels))
percent.append(x)
get_label_index = percent.index(max(percent))
get_rgb_of_dominant_color = centroid[get_label_index][:]
print(get_rgb_of_dominant_color)发布于 2021-05-26 20:19:27
这是我能想到的一种方法。假设您正在将集群修复为"5“,就像您的代码中所做的那样。
使用:kmeans.cluster_centers_确定5个集群质心
根据与每个集群质心关联的数据点的数量,按1到5的顺序对集群质心进行排名。与其关联的数据点数量最多的群集质心将是占主导地位的群集质心。使用集群质心的RBG值和可视化来查看颜色。
编辑-添加代码以了解详细说明
下面的代码中,我加载了一个图像,然后试图找到最主要的颜色。
import numpy as np
import pandas as pd
from PIL import Image
import matplotlib.pyplot as plt
from matplotlib.pyplot import imshow
from sklearn.cluster import KMeans
%matplotlib inline
#Load the image
arr_img = np.array(Image.open("beach.bmp"), dtype='int32')
plt.imshow(arr_img)

#reshape array from 3D to 2D
r, c, l = arr_img.shape
reshape_img = np.reshape(arr_img, (r*c, l), order="C")
#fit the model with 5 clusters
kmeans = KMeans(n_clusters = 5 ,max_iter=1000, init='random')
kmeans.fit(reshape_img)
# Looking at the labels and their associated data points
unique, counts = np.unique(kmeans.labels_, return_counts=True)
print("The labels are: ",unique)
print("Count of items: ",counts)
# Find the most dense cluster label
idx = np.where(counts == counts.max())[0]
# Pick the mose dense cluster centroid
s = tuple(map(int,kmeans.cluster_centers_[idx][0]))
# Visualize the color
plt.imshow([[s]])

您可以看到,Kmeans正确地将蓝色识别为最主要的颜色。
发布于 2021-05-26 20:24:35
有人告诉我,当惯性改善降到20%以下时,首先要检查查看所需的kmeans数,这可以通过以下方法完成:
test = []
K = range(1,10)
for k in K:
model = KMeans(n_clusters=k)
model.fit(X)
test.append(model.inertia_)
for index, x in enumerate(test):
if index == 0:
continue
else:
print(index, (((test[index - 1] - x) / test[index - 1]) * 100))但我猜对你来说,它是不同颜色的数量。我建议你使用这个数字,我假设你的代码是5。
在那之后,要预测颜色,您可以这样做
preds = kmeans.predict(whateverYouWantToPredict)这些将是您的最终预测,但请注意,这是一个unsupervised method。您可以通过将预测附加到数据帧来使用此预测,然后使用该数据为另一个预测训练supervised model。
https://stackoverflow.com/questions/67701185
复制相似问题