【发布时间】:2020-06-17 15:15:57
【问题描述】:
我有一些食物图像存储在一个文件夹中。所有图像都没有标记,也没有存储在单独的文件夹中,例如“pasta”或“meat”。我目前的目标是将图像归类为多个类别,以便我以后可以评估同一类图像中描绘的食物的味道是否相似。
为此,我加载图像并以可输入 VGG16 进行特征提取的格式处理它们,然后将特征传递给我的 KMeans 以对图像进行聚类。我使用的代码是:
path = r'C:\Users\Hi\Documents\folder'
train_dir = os.path.join(path)
model = VGG16(weights='imagenet', include_top=False)
vgg16_feature_list = []
files = glob.glob(r'C:\Users\Hi\Documents\folder\*.jpg')
for i in enumerate(files):
img = image.load_img(img_path,target_size=(224,224))
img_data=image.img_to_array(img)
img_data=np.expand_dims(img_data,axis=0)
img_data=preprocess_input(img_data)
vgg16_feature = model.predict(img_data)
vgg16_feature_np = np.array(vgg16_feature)
vgg16_feature_list.append(vgg16_feature_np.flatten())
vgg16_feature_list_np=np.array(vgg16_feature_list)
print(vgg16_feature_list_np.shape)
print(vgg16_feature_np.shape)
kmeans = KMeans(n_clusters=3, random_state=0).fit(vgg16_feature_list_np)
print(kmeans.labels_)
问题是我收到以下警告:
ConvergenceWarning: Number of distinct clusters (1) found smaller than n_clusters (3). Possibly due to duplicate points in X.
我该如何解决这个问题?
【问题讨论】:
-
这能回答你的问题吗? SKLearn KMeans Convergence Warning
-
@xiawi 我在回答之前在我的搜索中看到了这个,但答案来自纯粹的编码方面(如何抑制警告本身),并且没有提供任何实际的补救措施,所以这就是为什么我继续自己提供一个(不同的)答案。
-
什么是“我的 KMeans”?你写了那个代码?它在某个标准库中吗?请相应地查看minimal reproducible example 和edit 您的问题。我希望 Python 代码以一些
import语句开头。
标签: python machine-learning computer-vision k-means vgg-net