错误:“NoneType”对象没有属性''u inbound'u nodes'

2024-10-03 02:48:20 发布

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

[在此处输入图像描述][1]我正在尝试建立一个并行的ANN网络。 我计划:

  • 输入120X120图像。在
  • 把它分解成9张40x40的图像。在
  • 运行卷积网。在
  • 以相同的模式合并输出。在
  • 在合并层上运行另一个conv网络。在

def conv_net(): 
    input_shape = [120,120,1]
    inp=Input(shape=input_shape)
    print(type(inp))
    print(inp.shape)
    row_layers  = []
    col_layers  = []

    # fn = lambda x: self.conv(x)
    for i in range(0, 120, 40):
        row_layers = []

        for j in range(0, 120, 40):
            # out = (self.conv(inp[:,i:i+39,j:j+39]))
            inputs = inp[:, i:i + 40, j:j + 40]

            x = Dense(64, activation='relu')(inputs)
            out = Dense(64, activation='relu')(x)
            print(out.shape)
            row_layers.append(out)
        col_layers.append(keras.layers.concatenate(row_layers, axis=2))
        print((len(col_layers)))
    merged = keras.layers.concatenate(col_layers, axis=1)
    print(merged.shape)
    con = Conv2D(1, kernel_size=5, strides=2, padding='same', activation='relu')(merged)
    print(con.shape)
    output = Flatten()(con)
    output = Dense(1)(output)
    print(output.shape)

    model = Model(inputs=inp, outputs=output)
    # plot_model(model,to_file='model.png')
    return model

我得到一个错误NoneType对象没有属性_inbound_nodes。在

我调试了一下。错误是由于这条线。在

^{pr2}$

错误:

Traceback (most recent call last):
  File "C:/Users/Todd Letcher/machine_learning_examples/unsupervised_class3/slicing_img.py", line 83, in <module>
    conv_net()
  File "C:/Users/Todd Letcher/machine_learning_examples/unsupervised_class3/slicing_img.py", line 80, in conv_net
    model = Model(inputs=inp, outputs = output)
  File "C:\Users\Todd Letcher\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\Todd Letcher\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 91, in __init__
    self._init_graph_network(*args, **kwargs)
  File "C:\Users\Todd Letcher\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 235, in _init_graph_network
    self.inputs, self.outputs)
  File "C:\Users\Todd Letcher\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 1406, in _map_graph_network
    tensor_index=tensor_index)
  File "C:\Users\Todd Letcher\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 1393, in build_map
    node_index, tensor_index)
  File "C:\Users\Todd Letcher\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 1393, in build_map
    node_index, tensor_index)
  File "C:\Users\Todd Letcher\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 1393, in build_map
    node_index, tensor_index)
  File "C:\Users\Todd Letcher\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\engine\network.py", line 1365, in build_map
    node = layer._inbound_nodes[node_index]
AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

感谢帮助。谢谢你

注:我删除了切片线inp[:,i:i+39,j:j+39],它运行正常。在

图像显示了我的意图。唯一的区别是我想把图像分成9块。在这里,相同的图像被传送到所有的并行Conv网络。在

[1]: https://i.stack.imgur.com/Z7nt0.png

Tags: inpyindexlocallayerslinenetworkusers
1条回答
网友
1楼 · 发布于 2024-10-03 02:48:20

终于有了答案。尽管我仍然想知道为什么我以前的代码抛出错误,我只是添加了lambda层来分割。在

    def conv_net(self): # Add dropout if  Overfiting  
    input_shape = [120,120,1]

    inp=Input(shape=input_shape)
    col_layers  = []
    def sliced(x,i,j):
        return x[:,i:i+40,j:j+40]

    for i in range(0,120,40):
        row_layers  = []

        for j in range(0,120,40):

            #out = (self.conv(inp[:,i:i+39,j:j+39]))

            inputs = Lambda(sliced,arguments={'i':i,'j':j})(inp)


            #inputs = Input(shape=input_shape_small)

            out = (self.conv(inputs))
            print(out.shape)

            row_layers.append(out)
        col_layers.append(keras.layers.concatenate(row_layers, axis=2))
        print((len(col_layers)))

    merged = keras.layers.concatenate(col_layers,axis=1)
    print(merged.shape)


    #merged = Reshape((3,3,1))(merged)
    print(merged.shape)

    con = Conv2D(1,kernel_size=5,strides=2,padding='same',activation='relu')(merged)
    con = (BatchNormalization(momentum=0.8))(con)
    print(con.shape)
    #con = Conv2D(1,kernel_size=5,strides=2,padding='same',activation='relu')(inp)
    output = Flatten()(con)
    output = Dense(1)(output)
    print(output.shape)


    model = Model(inputs=inp, outputs = output)
    #plot_model(model,to_file='model.png')
    print(model.summary())

    plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)
    return model

这是没有错误的。在

相关问题 更多 >