Keras - 一级CNN - 两级输入,每级一个。

问题描述 投票:1回答:1

我正在尝试训练一个One Class CNN,但只想在一个类上进行训练。因此,为了解决没有负类的问题,而不是将随机图像添加到 "无类 "标签上,我想对这些输入进行噪声训练。但我想在分类器NN之前,将噪声不作为随机生成的图像输入,而是作为特征向量输入,这在这篇论文中有解释。https:/arxiv.orgpdf1901.08688.pdf。

所以,一步将作为VGG16 conv层的特征向量,下一步我想用随机生成的特征向量来训练。

enter image description here

我正在使用tf.keras和Functional API试图为VGG16添加一个额外的输入.我已经尝试了我自己的输入生成器,但我不确定我是否可以通过代码和使用Keras来实现它,因为我不知道我是否可以在每一步中激活不同的inpunts.一种方法是从conv层中提取所有特征向量,生成类似数量的噪声向量,然后用生成的噪声和真实特征训练分类器。

我想这是可行的,但是我可以在同一个模型中完成所有的工作吗?

请您提供任何帮助。

谢谢你的帮助

python keras deep-learning tf.keras cnn
1个回答
1
投票

如果你稍微有创意地思考,你根本不需要有一个噪声输入层。

我们可以使用 GaussianNoise() 但它需要一个输入张量,技术上我们应该给它传递一个 ones 向量。我们可以使中间的VGG输出特征乘以一个密集层的零核,然后我们可以在密集层中加入 ones 偏向它,所以我们得到了我们的占位符。ones 向量,它将被传递给 GaussianNoise.

现在,你可以忘记任何复杂的数据生成器,只需使用常规的数据生成器或直接使用numpy数组与 fit.

from tensorflow.keras.applications import VGG16
from tensorflow.keras.layers import *
from tensorflow.keras.models import *
import tensorflow as tf

ip = Input((224,224,3))

base = VGG16((224,224,3))(ip)

# passing vgg features to a zero vector, making everything zero and then adding bias ones to make the output is always 1

dense_ones = Dense(1000, activation='linear', kernel_initializer = tf.keras.initializers.Zeros(), bias_initializer = tf.keras.initializers.Ones())(base)
gaussian = GaussianNoise(0.4)(dense_ones)

concat = Concatenate()([base, gaussian])

learn_feature = Dense(128, activation = 'relu')(concat) # change this part based on your needs
classification = Dense(2, activation = 'sigmoid')(learn_feature)

model = Model(ip, classification)
Model: "model_5"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_19 (InputLayer)           [(None, 224, 224, 3) 0                                            
__________________________________________________________________________________________________
vgg16 (Model)                   (None, 1000)         138357544   input_19[0][0]                   
__________________________________________________________________________________________________
dense_2 (Dense)                 (None, 1000)         1001000     vgg16[1][0]                      
__________________________________________________________________________________________________
gaussian_noise_5 (GaussianNoise (None, 1000)         0           dense_2[0][0]                    
__________________________________________________________________________________________________
concatenate_4 (Concatenate)     (None, 2000)         0           vgg16[1][0]                      
                                                                 gaussian_noise_5[0][0]           
==================================================================================================
Total params: 139,358,544
Trainable params: 139,358,544
Non-trainable params: 0
___________________________________
© www.soinside.com 2019 - 2024. All rights reserved.