有人看到图形断开是从哪里来的吗?

2024-03-29 00:11:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下错误抛出,虽然我没有看到断开发生的地方。你知道吗

我想知道有没有人知道怎么修?我将附上下面的代码和错误的完整回溯。提前谢谢你的帮助。你知道吗

from keras.layers import Conv2D, UpSampling2D, LeakyReLU, Concatenate, Lambda, Input, UpSampling2D
from tensorflow.keras import Model
from keras.applications.densenet import DenseNet169


'''
Following is to get layers for skip connection and num_filters
'''
base_model = DenseNet169(include_top=False,input_shape=(224,224,3))
base_model_output_shape=base_model.layers[-1].output.shape
decoder_filters = int(base_model_output_shape[-1]/2)

def UpProject(array,filters,name,concat_with):
    up_i = UpSampling2D((2,2),interpolation='bilinear')(array)
    up_i=Concatenate(name=name+'_concat')([up_i,base_model.get_layer(concat_with).output])  #skip connection
    up_i=Conv2D(filters=filters,kernel_size=3,strides=1,padding='same',name=name+'_convA')(up_i)
    up_i=LeakyReLU(alpha=.2)(up_i)
    up_i=Conv2D(filters=filters,kernel_size=3,strides=1,padding='same',name=name+'_convB')(up_i)
    up_i=LeakyReLU(alpha=.2)(up_i)

    return up_i

def get_Model():
    #encoder network
    img_inp_shape=(224,224,3)
    img_input = Input(img_inp_shape)
    normalized = Lambda(lambda x: x/256 - .5)(img_input)
    encoded = base_model(normalized)


    #begin decoding 
    decoder = Conv2D(filters=decoder_filters,kernel_size=1,padding='same',input_shape=base_model_output_shape,name='conv2')(encoded)
    decoder = UpProject(decoder,int(decoder_filters/2),'up_1','pool3_pool')
    decoder = UpProject(decoder,int(decoder_filters/4),'up_2','pool2_pool')
    decoder = UpProject(decoder,int(decoder_filters/8),'up_3','pool1')
    decoder = UpProject(decoder,int(decoder_filters/16),'up_4','conv1/relu')

    conv3 = Conv2D(filters=1,kernel_size=3,strides=1,padding='same',name='conv3')(decoder)

    model = Model(inputs=img_input,outputs=conv3)

    return model

model = get_Model()

错误消息:

Traceback (most recent call last):

  File "<ipython-input-2-eef68b7cc748>", line 1, in <module>
    model = get_Model()

  File "C:/Users/Alec/.spyder-py3/depth_functional.py", line 40, in get_Model
    model = Model(inputs=img_input,outputs=conv3)

  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 94, in __init__
    self._init_graph_network(*args, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 241, in _init_graph_network
    self.inputs, self.outputs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py", line 1511, in _map_graph_network
    str(layers_with_complete_input))

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

Tags: nameimginputbasegetmodellinenetwork
1条回答
网友
1楼 · 发布于 2024-03-29 00:11:43

问题在于从基本模型中获取层的输出,而在模型中没有任何地方使用生成这些输出的基本模型的输入。你知道吗

Problem here: base_model.get_layer(concat_with).output

对于您的案例,最简单的解决方案是将规范化直接放在数据或生成器中,并使用:

img_input = base_model.input
encoded = base_model.output

这样您就不需要更改UpProject。(这可能仍然有问题,具体取决于所选图层是否在基础模型中多次使用)

请注意,如果希望预训练的基础模型正常工作,则应对此模型使用正确的预处理:

keras.applications.densenet import preprocess_input
processed_images = preprocess_input(batch_of_images)

如果使用PIL或Keras标准生成器加载图像,则此操作有效。如果用cv2打开,则需要先翻转通道。你知道吗

相关问题 更多 >