<a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..featureMaps'}, '*')">featureMaps = <a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..model'}, '*')">model.predict(<a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..img'}, '*')">img)
## Plotting Features
for a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..maps'}, '*')">maps in <a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..featureMaps'}, '*')">featureMaps:
plt.<a onclick="parent.postMessage({'referent':'.matplotlib.pyplot.figure'}, '*')">figure(figsize=(20,20))
<a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..pltNum'}, '*')">pltNum = 1
for <a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..a'}, '*')">a in range(8):
for <a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..b'}, '*')">b in <a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..range'}, '*')">range(8):
plt.<a onclick="parent.postMessage({'referent':'.matplotlib.pyplot.subplot'}, '*')">subplot(8, 8, <a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..pltNum'}, '*')">pltNum)
plt.<a onclick="parent.postMessage({'referent':'.matplotlib.pyplot.imshow'}, '*')">imshow(<a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..maps'}, '*')">maps[: ,: ,<a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..pltNum'}, '*')">pltNum - 1], cmap='gray')
<a onclick="parent.postMessage({'referent':'.kaggle.usercode.12234793.44545592.ShowMeWhatYouLearnt..pltNum'}, '*')">pltNum += 1
plt.<a onclick="parent.postMessage({'referent':'.matplotlib.pyplot.show'}, '*')">show()
接下来我们将重点介绍如何来创建我们的聚类算法。设计图像聚类算法在本节中,我们使用Kaggle上的 keep-babies-safe 数据集。https://www.kaggle.com/akash14/keep-babies-safe首先,我们创建一个图像聚类模型,来将给定的图像分为两类,即玩具或消费品,以下是来自该数据集的一些图像。
以下代码实现我们的聚类算法:##################### Making Essential Imports ############################
import sklearn
import os
import sys
import matplotlib.pyplot as plt
import cv2
import pytesseract
import numpy as np
import pandas as pd
import tensorflow as tf
conf = r'-- oem 2'
#####################################
# Defining a skeleton for our #
# DataFrame #
#####################################
DataFrame = {
'photo_name' : [],
'flattenPhoto' : [],
'text' : [],
}
#######################################################################################
# The Approach is to apply transfer learning hence using Resnet50 as my #
# pretrained model #
#######################################################################################
MyModel = tf.keras.models.Sequential()
MyModel.add(tf.keras.applications.ResNet50(
include_top = False, weights='imagenet', pooling='avg',
))
# freezing weights for 1st layer
MyModel.layers[0].trainable = False
### Now defining dataloading Function
def LoadDataAndDoEssentials(path, h, w):
img = cv2.imread(path)
DataFrame['text'].append(pytesseract.image_to_string(img, config = conf))
img = cv2.resize(img, (h, w))
## Expanding image dims so this represents 1 sample
img = img = np.expand_dims(img, 0)
img = tf.keras.applications.resnet50.preprocess_input(img)
extractedFeatures = MyModel.predict(img)
extractedFeatures = np.array(extractedFeatures)
DataFrame['flattenPhoto'].append(extractedFeatures.flatten())
### with this all done lets write the iterrrative loop
def ReadAndStoreMyImages(path):
list_ = os.listdir(path)
for mem in list_:
DataFrame['photo_name'].append(mem)
imagePath = path + '/' + mem
LoadDataAndDoEssentials(imagePath, 224, 224)
### lets give the address of our Parent directory and start
path = 'enter your data's path here'
ReadAndStoreMyImages(path)
######################################################
# lets now do clustering #
######################################################
Training_Feature_vector = np.array(DataFrame['flattenPhoto'], dtype = 'float64')
from sklearn.cluster import AgglomerativeClustering
kmeans = AgglomerativeClustering(n_clusters = 2)
kmeans.fit(Training_Feature_vector)
A little explanation for the above code:
上面的代码使用Resnet50(一种经过预先训练的CNN)进行特征提取,我们只需移除其头部或用于预测类别的神经元的最后一层,然后将图像输入到CNN并获得特征向量作为输出,实际上,这是我们的CNN在Resnet50的倒数第二层学习到的所有特征图的扁平数组。可以将此输出向量提供给进行图像聚类的任何聚类算法。让我向你展示通过这种方法创建的簇。
该可视化的代码如下## lets make this a dataFrame
import seaborn as sb
import matplotlib.pyplot as plt
dimReducedDataFrame = pd.DataFrame(Training_Feature_vector)
dimReducedDataFrame = dimReducedDataFrame.rename(columns = { 0: 'V1', 1 : 'V2'})
dimReducedDataFrame['Category'] = list (df['Class_of_image'])
plt.figure(figsize = (10, 5))
sb.scatterplot(data = dimReducedDataFrame, x = 'V1', y = 'V2',hue = 'Category')
plt.grid(True)
plt.show()
结论本文通过解释如何使用深度学习和聚类将视觉上相似的图像聚在一起形成簇,而无需创建数据集并在其上训练CNN。