简单的融合模型:基于keras 的少量样本集迁移学习 VGG16 MeanShift PAC降维混合模型的苹果识别
案例分析
更多是是一种思想 而不是具体实现
1 数据集
样本总数为30个 其中普通苹果和其他苹果各占一半 其中有10个苹果已经标注其他均无标签
2 数据集扩容
由于数据集中数据数量少无法满足模型训练 故而改变图片生成一部分模型
path = 'original_data'dst_path = 'zyccccc'datagen = ImageDataGenerator(rotation_range=15,width_shift_range=0.2,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')for i in range(100): gen.next()
3 VGG提取图像特征
取消VGG后面两个FC 层 获取 每个图像77512的特征 用于后期计算
from keras.applications.vgg16 import preprocess_inputimport numpy as npmodel_vgg = VGG16(weights='imagenet',include_top=False)X = np.expand_dims(img,axis = 0)print(X.shape)X = preprocess_input(X)print(X.shape)features = model_vgg.predict(X)features = features.reshape(1,7*7*512)print(features.shape)
获取图片批量特征信息
批量输入 上述的 VGG 模型中得到 得到批量图片的特征信息
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)): feature_i = modelProcess(img_path[i],model_vgg) print('preprocessed:',img_path[i]) features_train[i] = feature_i
讲得到特征 PCA 降低维度
减少图片中无用信息的干扰讲图片从224224 改变维度为200200
from sklearn.preprocessing import StandardScalerstds = StandardScaler()X_norm = stds.fit_transform(X)from sklearn.decomposition import PCApca = PCA(n_components=200)X_pca = pca.fit_transform(X_norm)
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#obtain the bandwidthbw = estimate_bandwidth(X_pca,n_samples=140)print(bw)#set up meanshift modelcnn_pca_ms = MeanShift(bandwidth=bw)cnn_pca_ms.fit(X_pca)```
赞 (0)