Как подключить объект Tensor к input_tensor модели в Tensorflow 2.x - PullRequest
0 голосов
/ 06 февраля 2020

В программе tenorflow 2.x я могу инициализировать предварительно обученную модель, такую ​​как MobileNetV2, и настроить количество прогнозов, как показано здесь :

inputTensor = Input(shape=(224, 224, 3))
model = MobileNetV2(include_top=False, weights='imagenet', input_tensor=inputTensor)

# add a global spatial average pooling layer
x = model.output
x = GlobalAveragePooling2D()(x)

# add a fully connected layer
x = Dense(1024, activation='relu')(x)

# and add a logistic layer -- we have nbr_classes
predictions = Dense(nb_classes, activation='softmax')(x)
model = models.Model(inputs=model.input, outputs=predictions)

Это работает хорошо. Однако теперь я пытаюсь передать в качестве входных данных для MobileNetV2 выходные данные из другой сети, которую я предварительно обучил (segModel):

# Load the input Network
segModel = tf.keras.models.load_model('/data2/OCT-project/KaggleTraining/SegmentationModel/Unet-Kaggle.h5')

# convert the shape of the output from the segModel Network (304, 304, 10) to match the expected input of the MobileNetV2 Network (224, 224, 3)
result = tf.math.reduce_max(segModel.output, axis=-1)
result = tf.image.convert_image_dtype(result, tf.float32)    
result = tf.stack([result, result, result], axis=-1)    
result = tf.image.resize(result, (224, 224), name="resizer-tensor")
inputTensor = Input(tensor=result)

# pass the inputTensor as the input_tensor parameter to MobileNetV2
classModel = MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights='imagenet', input_tensor=inputTensor)
x = classModel.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(4, activation='softmax')(x)

model = models.Model(inputs=segModel.input, outputs=predictions)

Когда я пытаюсь создать эту модель, я получаю следующее ошибка:

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("resizer-tensor_1/ResizeBilinear:0", shape=(None, 224, 224, 3), dtype=float32) at layer "input_4". The following previous layers were accessed without issue: []

Я пытался удалить конструктор Input и передать result непосредственно конструктору MobileNetV2, но я получаю то же сообщение об ошибке. Почему тензор потока не может получить значение для resizer-tensor?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...