大家经常见的是文章聚类、商品聚类,即根据文章中的关键词以及商品自身属性,然后从机器学习算法中选择一种常见的聚类算法去实现;那么关于如何进行图像聚类如何实现呢
背景
业务当中需要给图片风格进行标签化,关于图片格化处理,这里想的是根据图像分类的算法来做,因为分类属于有监督学习,需要整理相关的训练样本,如果上来就通过人工的方式去整理,成本太高,再加上前期大家认知的不同意,样本质量也难得到保障;所以需要先对已有商品进行聚类分析,在聚类的结果集上进行相关样本的整理。
实现流程
整个流程并不复杂,首先采用深度学习模型进行图片特征抽取,然后采用K-means对特征进行聚类。
特征抽取
- CNN模型是图片特征提取常用网络,这里将使用经典图像分类模型Vgg16的卷积网络进行相关图像处理
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np
import pylab
import io
from PIL import Image
from numpy import linalg as LA
base_model = VGG16(weights='imagenet', pooling='max',include_top=False) #这里也可以使用自己的数据集进行训练
def get_image_feature(path):
model = base_model
# print(model.summary())
img = Image.open(path)
img = img.convert('RGB')
img = img.resize((224,224), Image.NEAREST)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
return features[0]
features = get_image_feature()
vec = features/LA.norm(features)
print(vec.shape, vec.tolist())
图片聚类
采用k-means对图片特征进行聚类分析,特征长度512
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
import pandas as pd
import numpy as np
features = [
[],
[]
]
names = ['', '']
df = pd.DataFrame( features )
name_df = pd.DataFrame( names )
samples=df.values
#未进行标准化
kmeans=KMeans(n_clusters=3)
kmeans.fit(samples)#训练模型
labels=kmeans.predict(samples)#预测
name_df[labels==1]
完整代码
网上整理了凯旋门和埃菲尔铁塔的图片,简单测试下效果
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import os
import numpy as np
import pylab
import io
from PIL import Image
from numpy import linalg as LA
from sklearn.cluster import KMeans
base_model = VGG16(weights='imagenet', pooling='max',include_top=False) #这里也可以使用自己的数据集进行训练
def get_image_feature(path):
model = base_model
# print(model.summary())
img = Image.open(path)
img = img.convert('RGB')
img = img.resize((224,224), Image.NEAREST)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
return features[0]
feature_ls = []
names = []
for name in os.listdir("d:/tmp/天池/jd"):
features = get_image_feature(os.path.join("d:/tmp/天池/jd", name))
#特征标准化
vec = features/LA.norm(features)
#print(name,"==", vec.tolist())
names.append(name)
feature_ls.append(vec.tolist())
df = pd.DataFrame(feature_ls)
names_df = pd.DataFrame(names)
samples=df.values
kmeans=KMeans(n_clusters=3)
kmeans.fit(samples)#训练模型
labels=kmeans.predict(samples)#预测
print(labels, names_df[labels==0] )
效果
- 结果打印
# 将标签值为0的数据进行输出
[1 1 1 1 1 0 0 0 0 0 0 0] 0
5 test.jpg
6 tt1.jpg
7 tt2.jpg
8 tt3.jpg
9 tt4.jpg
10 tt5.jpg
11 ttkxm.jpg
可以看出所有的铁塔被划分到一个类了