简单的融合模型:基于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_input
import numpy as np

model_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 StandardScaler
stds = StandardScaler()
X_norm = stds.fit_transform(X)

from sklearn.decomposition import PCA
pca = 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 bandwidth
bw = estimate_bandwidth(X_pca,n_samples=140)
print(bw)
#set up meanshift model
cnn_pca_ms = MeanShift(bandwidth=bw)
cnn_pca_ms.fit(X_pca)
使用MeanShift 分类
y_predict_pca_ms = cnn_pca_ms.predict(X_pca)

# 测试集验证
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)



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值