将预处理层添加到模型
Add preprocess layer to a model
我使用的是预训练的 ResNet 模型,我正在使用我的数据集训练模型的几个层,但我想将 ResNet 的预处理作为模型的一个层。
如何操作?
在Input
层之后添加预处理层与在ResNet50
模型之前添加预处理层相同,
resnet = tf.keras.applications.ResNet50(
include_top=False ,
weights='imagenet' ,
input_shape=( 256 , 256 , 3) ,
pooling='avg' ,
classes=13
)
for layer in resnet.layers:
layer.trainable = False
# Some preprocessing layer here ...
preprocessing_layer = tf.keras.layers.Normalization( mean=0 , variance=1 , input_shape=( 256 , 256 , 3 ) )
model = tf.keras.models.Sequential( [
preprocessing_layer ,
resnet,
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(22, activation='softmax',name='output')
])
model.compile( loss='mse' , optimizer='adam' )
model.summary()
输出,
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
normalization (Normalizatio (None, 256, 256, 3) 0
n)
resnet50 (Functional) (None, 2048) 23587712
flatten (Flatten) (None, 2048) 0
output (Dense) (None, 22) 45078
=================================================================
Total params: 23,632,790
Trainable params: 45,078
Non-trainable params: 23,587,712
_________________________________________________________________
输入现在将首先通过 preprocessing_layer
,然后进入 resnet
模型。
如果你想使用tf.keras.applications.resnet50.preprocess_input()
,试试:
import tensorflow as tf
resnet = tf.keras.applications.ResNet50(
include_top=False ,
weights='imagenet' ,
input_shape=( 256 , 256 , 3) ,
pooling='avg' ,
classes=13
)
for layer in resnet.layers:
layer.trainable = False
model = tf.keras.Sequential()
model.add(tf.keras.layers.Lambda(tf.keras.applications.resnet50.preprocess_input, input_shape=(256, 256, 3)))
model.add(resnet)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(22, activation='softmax',name='output') )
model.compile( loss='mse' , optimizer='adam' )
print(model(tf.random.normal((1, 256, 256, 3))))
tf.Tensor(
[[0.12659772 0.02955576 0.13070999 0.0258545 0.0186768 0.01459627
0.07854564 0.010685 0.01598095 0.04758708 0.05001146 0.20679766
0.00975605 0.01047837 0.00401289 0.01095579 0.06127766 0.0313729
0.00884041 0.04098257 0.01187507 0.05484949]], shape=(1, 22), dtype=float32)
我使用的是预训练的 ResNet 模型,我正在使用我的数据集训练模型的几个层,但我想将 ResNet 的预处理作为模型的一个层。
如何操作?
在Input
层之后添加预处理层与在ResNet50
模型之前添加预处理层相同,
resnet = tf.keras.applications.ResNet50(
include_top=False ,
weights='imagenet' ,
input_shape=( 256 , 256 , 3) ,
pooling='avg' ,
classes=13
)
for layer in resnet.layers:
layer.trainable = False
# Some preprocessing layer here ...
preprocessing_layer = tf.keras.layers.Normalization( mean=0 , variance=1 , input_shape=( 256 , 256 , 3 ) )
model = tf.keras.models.Sequential( [
preprocessing_layer ,
resnet,
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(22, activation='softmax',name='output')
])
model.compile( loss='mse' , optimizer='adam' )
model.summary()
输出,
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
normalization (Normalizatio (None, 256, 256, 3) 0
n)
resnet50 (Functional) (None, 2048) 23587712
flatten (Flatten) (None, 2048) 0
output (Dense) (None, 22) 45078
=================================================================
Total params: 23,632,790
Trainable params: 45,078
Non-trainable params: 23,587,712
_________________________________________________________________
输入现在将首先通过 preprocessing_layer
,然后进入 resnet
模型。
如果你想使用tf.keras.applications.resnet50.preprocess_input()
,试试:
import tensorflow as tf
resnet = tf.keras.applications.ResNet50(
include_top=False ,
weights='imagenet' ,
input_shape=( 256 , 256 , 3) ,
pooling='avg' ,
classes=13
)
for layer in resnet.layers:
layer.trainable = False
model = tf.keras.Sequential()
model.add(tf.keras.layers.Lambda(tf.keras.applications.resnet50.preprocess_input, input_shape=(256, 256, 3)))
model.add(resnet)
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(22, activation='softmax',name='output') )
model.compile( loss='mse' , optimizer='adam' )
print(model(tf.random.normal((1, 256, 256, 3))))
tf.Tensor(
[[0.12659772 0.02955576 0.13070999 0.0258545 0.0186768 0.01459627
0.07854564 0.010685 0.01598095 0.04758708 0.05001146 0.20679766
0.00975605 0.01047837 0.00401289 0.01095579 0.06127766 0.0313729
0.00884041 0.04098257 0.01187507 0.05484949]], shape=(1, 22), dtype=float32)