【问题标题】:Saving and loading a KMeans clustering model with transfer learning使用迁移学习保存和加载 KMeans 聚类模型
【发布时间】:2021-08-20 15:02:14
【问题描述】:

我在 Python 中有以下 KMeans 聚类模型,它仅在大约 5000 个未标记的图像上运行。我想保存模型并将其加载到单独的 python 脚本中,以简单地为其提供 1 个图像,以便我可以看到该图像属于哪个集群。保存和加载模型时会出现问题,我不确定它是否正确完成,因为我只是将kmodel 保存在泡菜文件中。

height = 299
width = 299
input_dir = "D:\\Glenn\\CNN\\Data\\Images"
glob_dir = input_dir + '/*.png'
images = [cv2.resize(cv2.imread(file), (height, width)) for file in glob.glob(glob_dir)]
paths = [file for file in glob.glob(glob_dir)]
images = np.array(np.float64(images))

model = tf.keras.applications.Xception(include_top=False, weights = "imagenet", input_shape=(height, width, 3))
predictions = model.predict(images.reshape(-1, height, width, 3))
pred_images = predictions.reshape(images.shape[0], -1)

k = 30
kmodel = KMeans(n_clusters = k, n_jobs=-1, random_state=728)
kmodel.fit(pred_images)
kpredictions = kmodel.predict(pred_images)

总的来说,我有点困惑,我正在做的甚至是最佳的。我使用剪影方法找到了最佳集群数量。但我不确定如何在单独的 python 文件中保存和加载这个模型,以仅预测集群 1 图像将在什么中。

【问题讨论】:

    标签: python image machine-learning k-means transfer-learning


    【解决方案1】:

    您可以从经过训练的 tomotopy HDP 模型中提取主题,然后将 kmeans 聚类器初始化为 HDP 自动计算的主题数量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 2020-07-05
      • 2020-12-07
      • 1970-01-01
      • 2018-03-02
      • 2020-07-17
      相关资源
      最近更新 更多