如何将Conv2d图层输出作为Keras模型的输入?

2024-05-16 15:25:42 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在Keras模型上添加Conv2D层? 我的输入形状为(299299,15),为了使用预训练权重(imagenet),输入通道必须为3,因此我的想法是添加一个conv2d层,将通道从15更改为3

image = Input(shape=(299, 299, 15))
x = Conv2D(3, kernel_size=(8,8), strides=(2,2), activation='relu')(image)
model1 = Model(inputs=image, outputs=x)

model2 = InceptionResNetV2(include_top=False, weights = 'imagenet', input_tensor=None, input_shape=(299,299,3))

Tags: 模型imageinputsizeactivationkernelkeras权重
2条回答

试一试

image = Input(shape=(299, 299, 15))
x = Conv2D(3, kernel_size=(8,8), strides=(2,2), activation='relu')(image)
model1 = Model(inputs=image, outputs=x)
x=model1.output
x=tf.keras.applications.InceptionResNetV2(include_top=False, weights = 'imagenet', input_tensor=None)(x)
model2=Model(inputs=image, outputs=x)
print(model2.summary())

您可能希望将pooling='max'参数添加到InceptionResNetV2参数中。这将导致输出是一个一维向量,可以输入到一个稠密的层中。 模型摘要如下所示:

Model: "functional_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 299, 299, 15)]    0         
_________________________________________________________________
conv2d (Conv2D)              (None, 146, 146, 3)       2883      
_________________________________________________________________
inception_resnet_v2 (Functio (None, None, None, 1536)  54336736  
=================================================================
Total params: 54,339,619
Trainable params: 54,279,075
Non-trainable params: 60,544

这将首先创建一个以x_input=(229,229,15)作为输入的模型,并执行卷积以将通道减少到3。然后将此模型的输出馈送到base_ model(InceptionResNetV2),并添加一些层,例如GlobalAveragePoolingDense层。最终的模型是以x_input作为第一层,以Dense层预测10个类作为输出层

import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D

# define input
x_input = tf.keras.layers.Input(shape=(229, 229, 15))
# convolve to go from 15 channels to 3
x_conv = tf.keras.layers.Conv2D(3,1)(x_input)
# model that performs convolution
conv_model = Model(inputs=x_input, outputs=x_conv)
# storing the model output, which will be later used as input for the base model
conv_output=conv_model.output

# defining base model
base_model = tf.keras.applications.InceptionResNetV2(
    weights='imagenet',
    include_top=False
)(conv_output)

# add a global spatial average pooling layer
x = GlobalAveragePooling2D()(base_model)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
# and a logistic layer   let's say we have 10 classes
predictions = Dense(10, activation='softmax')(x)

# this is the model we will train
model = Model(inputs=x_input, outputs=predictions)

model.summary()

View Model Summary

相关问题 更多 >