我有2个Tensorflow模型,它们都具有相同的架构(Unet-3d)。我当前的流程是:
[预处理->从模型1进行预测->一些操作->从模型2进行预测->后处理
之间的操作可以在TF中完成。我们能否将两个模型以及在1个TF图之间的操作结合起来,以使流程看起来像这样:
预处理->模型1 + 2->后处理
谢谢。
您可以使用tf.keras
功能性api实现这一目标,这是一个玩具示例。
import tensorflow as tf
print('TensorFlow:', tf.__version__)
def preprocessing(tensor):
# preform your operations
return tensor
def some_operations(model_1_prediction):
# preform your operations
# assuming your operations result in a tensor
# which has shape matching with model_2's input
tensor = model_1_prediction
return tensor
def post_processing(tensor):
# preform your operations
return tensor
def get_model(name):
inp = tf.keras.Input(shape=[256, 256, 3])
x = tf.keras.layers.Conv2D(64, 3, 1, 'same')(inp)
x = tf.keras.layers.Conv2D(256, 3, 1, 'same')(x)
x = tf.keras.layers.Conv2D(512, 3, 1, 'same')(x)
x = tf.keras.layers.Conv2D(64, 3, 1, 'same')(x)
x = tf.keras.layers.Conv2D(3, 3, 1, 'same')(x)
# num_filters is set to 3 to make sure model_1's output
# matches model_2's input.
output = tf.keras.layers.Activation('sigmoid')(x)
return tf.keras.Model(inp, output, name=name)
model_1 = get_model('model-1')
model_2 = get_model('model-2')
x = some_operations(model_1.output)
out = model_2(x)
model_1_2 = tf.keras.Model(model_1.input, out, name='model-1+2')
model_1_2.summary()
输出:
TensorFlow: 2.1.0-rc0
Model: "model-1+2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) [(None, 256, 256, 3)] 0
_________________________________________________________________
conv2d (Conv2D) (None, 256, 256, 64) 1792
_________________________________________________________________
conv2d_1 (Conv2D) (None, 256, 256, 256) 147712
_________________________________________________________________
conv2d_2 (Conv2D) (None, 256, 256, 512) 1180160
_________________________________________________________________
conv2d_3 (Conv2D) (None, 256, 256, 64) 294976
_________________________________________________________________
conv2d_4 (Conv2D) (None, 256, 256, 3) 1731
_________________________________________________________________
activation (Activation) (None, 256, 256, 3) 0
_________________________________________________________________
model-2 (Model) (None, 256, 256, 3) 1626371
=================================================================
Total params: 3,252,742
Trainable params: 3,252,742
Non-trainable params: 0
_________________________________________________________________