簡體   English   中英

如何順序組合2個張量流模型?

[英]How to sequentially combine 2 tensorflow models?

我有 2 個具有相同架構 (Unet-3d) 的 Tensorflow 模型。 我目前的流程是:

預處理 -> 模型 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
_________________________________________________________________
​

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM