简介

code

#数据增强 
from tensorflow.keras.preprocessing.image import ImageDataGenerator
path = """original_data"""
dst_path = """gen_data"""

datagen = ImageDataGenerator(rotation_range=10,width_shift_range=0.1,
                             height_shift_range=0.02,horizontal_flip=True,
                             vertical_flip=True)
gen = datagen.flow_from_directory(path,target_size=(224,224),
                                  batch_size=2,save_to_dir=dst_path,
                                  save_prefix="gen",save_format="jpg")
#224,224 VGG 输入的大小 
for i in range(100):
    gen.next()

from tensorflow.keras.preprocessing.image import load_img,img_to_array
img_path = "train_data\\cat.0.jpg"
img = load_img(img_path,target_size=(224,224))
print(type(img))



%matplotlib inline
from matplotlib import pyplot as plt
fig1 = plt.figure(figsize=(5,5))
plt.imshow(img)


img = img_to_array(img)
type(img)


#加载模型
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.applications.vgg16 import preprocess_input
import numpy as np
model_vgg = VGG16(weights="imagenet",include_top=False)
X = np.expand_dims(img,axis=0)#增加一个维度
X = preprocess_input(X)
print(X.shape)

#特征提取
features = model_vgg.predict(X)
print(features.shape)

features = features.reshape(1,7*7*512)
print(features.shape)


import os 
folder = """train_data"""
dirs = os.listdir(folder)
print(dirs)
img_path = []
for i in dirs:
    #if os.path.splitext(i)[1]==".jpg":
    img_path.append(i)
img_path = [folder +"\\"+i for i in img_path]
print(img_path)

#特征提取方法
def modelProcess(img_path,model):
    img = load_img(img_path,target_size=(224,224))
    img = img_to_array(img)
    X = np.expand_dims(img,axis=0)#增加一个维度
    X = preprocess_input(X)
    X_VGG = model.predict(X)
    X_VGG = X_VGG.reshape(1,7*7*512)
    return X_VGG

features_train = np.zeros([len(img_path),7*7*512])
for i in range(len(img_path)):
    features_i = modelProcess(img_path[i],model_vgg)
    print("preprocessed:",img_path[i])
    features_train[i] = features_i
print("Done")

print(features_train.shape)

X = features_train

from sklearn.cluster import KMeans
cnn_kmeans = KMeans(n_clusters=2,max_iter=2000)
cnn_kmeans.fit(X)


#预测
y_predict_kmeans = cnn_kmeans.predict(X)
print(y_predict_kmeans)


#统计
from collections import Counter
print(Counter(y_predict_kmeans))

#假设普通苹果id为1
normal_apple_id = 1

fig2 = plt.figure(figsize=(10,40))
for i in range(30):
    for j in range(5):
        img = load_img(img_path[i*5+j])
        plt.subplot(45,5,i*5+j+1)
        plt.title("apple" if y_predict_kmeans[i*5+j] == normal_apple_id else "other")
        plt.imshow(img)
        plt.axis("off")


#载入数据
import os 
folder_test = """test_data"""
dirs_test = os.listdir(folder_test)
img_path_test = []
for i in dirs_test:
    #if os.path.splitext(i)[1]==".jpg":
    img_path_test.append(i)
img_path_test = [folder_test +"\\"+i for i in img_path_test]
print(img_path_test)


#数据处理
features_test = np.zeros([len(img_path_test),7*7*512])
for i in range(len(img_path_test)):
    features_i = modelProcess(img_path_test[i],model_vgg)
    print("preprocessed:",img_path_test[i])
    features_test[i] = features_i
print("Done")

X_test = features_test
print(X_test.shape)

#预测
y_predict_kmeans_test = cnn_kmeans.predict(X_test)
print(y_predict_kmeans_test)

#画图 测试集的 
fig3 = plt.figure(figsize=(10,10))
for i in range(3):
    for j in range(4):
        img = load_img(img_path[i*4+j])
        plt.subplot(3,4,i*4+j+1)
        plt.title("apple" if y_predict_kmeans[i*4+j] == normal_apple_id else "other")
        plt.imshow(img)
        plt.axis("off")


from sklearn.cluster import MeanShift,estimate_bandwidth

bw = estimate_bandwidth(X,n_samples = 140)
print(bw)

cnn_ms = MeanShift(bandwidth = bw)
cnn_ms.fit(X)


#预测
y_predict_ms = cnn_ms.predict(X)
print(y_predict_ms)

#统计
from collections import Counter
print(Counter(y_predict_ms))

normal_apple_id = 0

fig4 = plt.figure(figsize=(10,40))
for i in range(30):
    for j in range(5):
        img = load_img(img_path[i*5+j])
        plt.subplot(30,5,i*5+j+1)
        plt.title("apple" if y_predict_ms[i*5+j] == normal_apple_id else "other")
        plt.imshow(img)
        plt.axis("off")

#预测 测试集
y_predict_ms_test = cnn_ms.predict(X_test)
print(y_predict_ms_test)

#画图 测试集的 
fig3 = plt.figure(figsize=(10,10))
for i in range(3):
    for j in range(5):
        img = load_img(img_path[i*5+j])
        plt.subplot(4,5,i*5+j+1)
        plt.title("apple" if y_predict_ms_test[i*5+j] == normal_apple_id else "other")
        plt.imshow(img)
        plt.axis("off")


from sklearn.preprocessing import StandardScaler
stds = StandardScaler()
X_norm = stds.fit_transform(X)
#PCA analusis
from sklearn.decomposition import PCA
pca = PCA(n_components=200)
X_pca = pca.fit_transform(X_norm)
#降维之后的主成分

#calculate the variance ratio of each components
var_ratio = pca.explained_variance_ratio_
print(np.sum(var_ratio))

print(X_pca.shape,X.shape)
#降维了

#再meanshift
from sklearn.cluster import MeanShift,estimate_bandwidth

bw = estimate_bandwidth(X_pca,n_samples = 140) #用处理后的数据
print(bw)

cnn_pca_ms = MeanShift(bandwidth = bw)
cnn_pca_ms.fit(X_pca)

#预测
y_predict_pca_ms = cnn_pca_ms.predict(X_pca)
print(y_predict_pca_ms)


#统计
from collections import Counter
print(Counter(y_predict_pca_ms))


normal_apple_id = 0


fig4 = plt.figure(figsize=(10,40))
for i in range(40):
    for j in range(5):
        img = load_img(img_path[i*5+j])
        plt.subplot(40,5,i*5+j+1)
        plt.title("apple" if y_predict_pca_ms[i*5+j] == normal_apple_id else "other")
        plt.imshow(img)
        plt.axis("off")


#数据转换
X_norm_test = stds.transform(X_test)
X_pca_test = pca.transform(X_norm_test)


#预测 测试集
y_predict_pca_ms_test = cnn_pca_ms.predict(X_pca_test)
print(y_predict_pca_ms_test)

#画图 测试集的 
fig3 = plt.figure(figsize=(10,10))
for i in range(3):
    for j in range(5):
        img = load_img(img_path[i*5+j])
        plt.subplot(3,5,i*5+j+1)
        plt.title("apple" if y_predict_pca_ms_test[i*5+j] == normal_apple_id else "other")
        plt.imshow(img)
        plt.axis("off")

TIP

用猫和狗作为 apple 和 other,就算优化了一下,感觉预测的不是特别好相对于猫和狗,普通苹果和其他苹果可能更容易区分。

image

posted on 2022-04-30 17:50  HDU李少帅  阅读(32)  评论(0编辑  收藏  举报