<p>这将首先创建一个以<code>x_input=(229,229,15)</code>作为输入的模型,并执行卷积以将通道减少到3。然后将此模型的输出馈送到<code>base_ model</code>(InceptionResNetV2),并添加一些层,例如<code>GlobalAveragePooling</code>和<code>Dense</code>层。最终的模型是以<code>x_input</code>作为第一层,以<code>Dense</code>层预测10个类作为输出层</p>
<pre><code>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()
</code></pre>
<p><a href="https://i.stack.imgur.com/cXD1Q.png" rel="nofollow noreferrer">View Model Summary</a></p>