【发布时间】: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