如何在预训练的CNN的开始处添加重塑层

时间:2020-05-12 01:10:34

标签: python tensorflow keras deep-learning conv-neural-network

我正在使用预训练的Inception V3模型进行面部识别任务。但是,我想在此模型的开头添加一个Reshape层,以将示例转换为具有不同形状的张量,然后将其馈送给经过预训练的模型。我知道有一种方法可以使用构造函数的input_shape参数为Inception V3配置Input形状:

inception_model = InceptionV3(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3)))

但这不能解决我的问题。由于技术限制的原因,我希望我的模型能够收到(150528, 1, 1)的向量,并希望在将其传递到模型之前将其重塑为(224, 244, 3)

所以,像这样:

reshape_layer = Reshape((224, 224, 3), input_shape=(150528,1,1))
inception_model = InceptionV3(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3)))
# join the reshape layer to ensure input passes through reshape_layer and into inception_model

# then I have my custom layers after that
head_model = inception_model.output
head_model = Flatten(name="flatten")(headModel)

# ...and so on

我是Tensorflow的新手,有点受阻,如果人们可以建议在预先训练的模型之上包含这种转换层的方法,我将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:0)

如果要将数据输入转换为形状(224、224、3),则可以检查数据的形状,并使用assert可以检查它是否等于224 * 224 * 3。如果是这样,请对其进行修改,否则,将引发错误。输入数据应被224 * 224 * 3整除,以将其重塑为该形状。

答案 1 :(得分:0)

我认为tf.image.resize(image, (IMG_SIZE, IMG_SIZE))可以解决您的问题。 要更多使用,enter link description here