def make_fcn_resnet(input_shape, nb_labels, use_pretraining, freeze_base):
nb_rows, nb_cols, _ = input_shape
input_tensor = Input(shape=input_shape)
weights = 'imagenet' if use_pretraining else None
model =ResnetBuilder.build(num_outputs=2,repetitions2=3, weights='present', input_shape=(1, 500, 500, 5))
if freeze_base:
for layer in model.layers:
layer.trainable = False
x32 = model.get_layer('act2').output
print("x32", x32._keras_shape)
x16 = model.get_layer('act3').output
print("x16", x16._keras_shape)
x8 = model.get_layer('act4').output
print("x8", x8._keras_shape)
c32 = Conv3D(nb_labels, (1, 1,5), name='conv_labels_32', padding='valid')(x32)
c32=Reshape((500,500,2))(c32)
print("c32", c32._keras_shape)
c16 = Conv3D(nb_labels, (1, 1,5), name='conv_labels_16', padding='valid')(x16)
c16=Reshape((250,250,2))(c16)
print("c16", c16._keras_shape)
c8 = Conv3D(nb_labels, (1, 1,5), name='conv_labels_8', padding='valid')(x8)
c8=Reshape((125,125,2))(c8)
print("c8", c8._keras_shape)
def resize_bilinear(images):
return tf.image.resize_bilinear(images, [nb_rows, nb_cols])
r32 = Lambda(resize_bilinear, name='resize_labels_32')(c32)
r16 = Lambda(resize_bilinear, name='resize_labels_16')(c16)
r8 = Lambda(resize_bilinear, name='resize_labels_8')(c8)
m = Add(name='merge_labels')([r32, r16, r8])
x = Reshape((nb_rows * nb_cols, nb_labels))(m)
x = Activation('softmax')(x)
x = Reshape((nb_rows, nb_cols, nb_labels))(x)
model = Model(inputs=input_tensor, outputs=x)
#print model.summary()
return model
在
ResnetBuilder.build
函数中,我使用conv3d层为Resnet编写了模型。在这里,我将使用我自己的模型Resnet并设计新模型。调用新模型时,出现图断开连接之类的错误。 最佳答案
在keras(或tensorflow)中,张量之间的操作用数据流图表示。使用数据流图时,模型的输入和输出(数据流图)之间应该有直接或间接的连接(链接)。在您的情况下,输入(input_tensor变量)和输出(x)之间没有连接。为了解决这个问题,您应该将input_tensor与ResNet模型连接。 Keras功能API具有在张量上调用模型的功能。
如果您需要更改,请在冻结ReseNet模型层之后进行以下更改。
model = model(input_tensor)
# Now model variable is just output tensor of resnet model.
x32 = model
print("x32", x32._keras_shape)
x16 = model
print("x16", x16._keras_shape)
x8 = model
print("x8", x8._keras_shape)