在张量流中使用 CNN 进行特征提取后使用 PCA 作为降维

问题描述

我正在尝试使用 PCA 过滤器来减少从 tensorflow 中使用 CNN 的图像中提取的特征的维度 我的问题是我的模型没有学习,我认为梯度没有上升到我的 PCA 层以上,这是我的 PCA 函数

def PCAF(cov_mat):
eigen_values,eigen_vectors = tf.linalg.eigh(cov_mat)
sorted_index = tf.argsort(eigen_values)[::-1]

sorted_eigenvalues = tf.gather(eigen_values,sorted_index)
sorted_eigenvectors = tf.gather(eigen_vectors,sorted_index)
vard = eigen_values
eigenvector_subset = sorted_eigenvectors[:,0:256]

return eigenvector_subset

我确实把这个函数放在了一个 Lambda 层,这是我的完整模型

# the final model

# extracting the feature map from images using MobileNetV2
extracted_features = tf.keras.layers.Flatten()(last_output)

# the PCA filter

meanX = Lambda(lambda x: tf.reduce_mean(x,axis=0,keepdims=True))(extracted_features)
# subtract the mean from matrix
standardized = tf.keras.layers.Subtract()([extracted_features,meanX])
# create the covarience matrix
Cov_mat = Lambda(lambda x : tf.matmul(tf.transpose(x),x))(standardized)
Cov_mat = Lambda(lambda x: x / 256)(Cov_mat)

# extract the principal components from the cov matrix
principal_features = Lambda(PCAF)(Cov_mat)

# project my data on the principal axis extracted with PCA
whitened_encoding = Lambda(lambda x : tf.matmul(x[0],x[1]))([standardized,principal_features])

# classification phase 
class_layer1 = tf.keras.layers.Dense(1024,activation='relu')(whitened_encoding)
class_layer2 = tf.keras.layers.Dense(512,activation='relu')(class_layer1)
class_layer3 = tf.keras.layers.Dense(256,activation='relu')(class_layer2)
final_class_layer = tf.keras.layers.Dense(45,activation='softmax')(class_layer)
outputs = final_class_layer

model = tf.keras.Model(pre_trained_model.input,outputs)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)