When using an intermediate layer of resnet50 to build DeepLabV3+ Network, the gradient tape can't update

53 Views Asked by At

This is my SemanticNetwork.In this network, I try to build a DeepLabV3+ Network, so I need to get the output of the intermediate layer in the resnet50, but when training my network, it displays some error message, seems like I can't find the gradient.

The error message is:

Traceback (most recent call last):
  File "C:/Users/ximen/Desktop/Carla research/Carla 鳥瞰語義分割/TrainSem.py", line 178, in <module>
    grads = tape.gradient(loss, model.trainable_variables)
  File "D:\Anaconda\envs\tf_2.5_py_3.7\lib\site-packages\tensorflow\python\eager\backprop.py", line 1080, in gradient
    unconnected_gradients=unconnected_gradients)
  File "D:\Anaconda\envs\tf_2.5_py_3.7\lib\site-packages\tensorflow\python\eager\imperative_grad.py", line 77, in imperative_grad
    compat.as_str(unconnected_gradients.value))
AttributeError: 'KerasTensor' object has no attribute '_id'
class SemanticNetwork(Model):
    def __init__(self, C):
        super().__init__()

        self.resnet_50 = ResNet50(weights="imagenet", include_top=False)
        self.dspp = DSPP()
        self.resize_layer = ResizeLayer(target_shape=50)
        self.cbr_1 = CBR(filters=48, kernel_size=1)
        self.cbr_2 = CBR()
        self.cbr_3 = CBR()
        self.cbr_4 = CBR(filters=C, kernel_size=1)
        self.up_sam = UpSampling2D(size=(4, 4), interpolation="bilinear")

    def call(self, inputs, training=None, mask=None):
        self.resnet_50(inputs)

        # 取出block_6所輸出的特徵,將其輸入給DSPP net
        output_1 = self.resnet_50.get_layer('conv4_block6_2_relu').output  # (B, 13, 13, 256)
        output_1 = self.dspp(output_1)  # (B, 13, 13, 256)

        # Resize output_1
        output_1 = self.resize_layer(output_1)  # (B, 50, 50, 256)

        # 取出block_3所輸出的特徵,並送入1x1捲積
        output_2 = self.resnet_50.get_layer('conv2_block3_2_relu').output  # (B, 50, 50, 64)
        output_2 = self.cbr_1(output_2)  # (B, 50, 50, 48)

        # 進行concatenate
        output = tf.concat([output_1, output_2], axis=-1)
        output = self.cbr_2(output)
        output = self.cbr_3(output)

        # Upsampling
        output = self.up_sam(output)

        # last layer
        output = self.cbr_4(output)

        return output

I don't have any idea where have trouble. Dose anyone can talk me how to fix this problem.

0

There are 0 best solutions below