Home > OS >  How to solve problem of : Graph disconnected: cannot obtain value for tensor Tensor?
How to solve problem of : Graph disconnected: cannot obtain value for tensor Tensor?

Time:09-20

I am trying to code a deep neural network but I am getting the following error:

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

Traceback (most recent call last):
  File "C:/Users/sanjay.g/PycharmProjects/Rethinking_Net/MIMO_Model2.py", line 99, in <module>
    gan = Pix2Pix()
  File "C:/Users/sanjay.g/PycharmProjects/Rethinking_Net/MIMO_Model2.py", line 21, in __init__
    self.generator = self.build_generator
  File "C:/Users/sanjay.g/PycharmProjects/Rethinking_Net/MIMO_Model2.py", line 97, in build_generator
    return Model(b3, S3)
  File "C:\Users\sanjay.g\Miniconda3\envs\pythonProject1\lib\site-packages\tensorflow\python\keras\engine\training.py", line 242, in __new__
    return functional.Functional(*args, **kwargs)
  File "C:\Users\sanjay.g\Miniconda3\envs\pythonProject1\lib\site-packages\tensorflow\python\training\tracking\base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "C:\Users\sanjay.g\Miniconda3\envs\pythonProject1\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 115, in __init__
    self._init_graph_network(inputs, outputs)
  File "C:\Users\sanjay.g\Miniconda3\envs\pythonProject1\lib\site-packages\tensorflow\python\training\tracking\base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "C:\Users\sanjay.g\Miniconda3\envs\pythonProject1\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 190, in _init_graph_network
    nodes, nodes_by_depth, layers, _ = _map_graph_network(
  File "C:\Users\sanjay.g\Miniconda3\envs\pythonProject1\lib\site-packages\tensorflow\python\keras\engine\functional.py", line 926, in _map_graph_network
    raise ValueError('Graph disconnected: '
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 256, 256, 3), dtype=float32) at layer "conv2d". The following previous layers were accessed without issue: []

How may I solve the above error?

from __future__ import print_function, division

import tensorflow as tf
from tensorflow.keras.layers import Input, Dense, Reshape, Flatten, Dropout, Concatenate
from tensorflow.keras.layers import BatchNormalization, Activation, ZeroPadding2D
from tensorflow.keras.models import Sequential, Model
from data_loader import DataLoader
from tensorflow.keras.layers import Conv2D, LeakyReLU, UpSampling2D, ReLU, Conv2DTranspose

class Pix2Pix():
    def __init__(self):
        # Input shape
        self.img_rows = 256
        self.img_cols = 256
        self.channels = 3
        self.img_shape = (self.img_rows, self.img_cols, self.channels)
        self.dataset_name = 'nayadata'
        self.data_loader = DataLoader(dataset_name=self.dataset_name, img_res=(self.img_rows, self.img_cols))
        self.gf = 32
        self.df = 32
        self.generator = self.build_generator
        img_A = Input(shape=self.img_shape)
        img_B = Input(shape=self.img_shape)
        fake_A = self.generator(img_B)
        self.combined = Model(inputs=[img_A, img_B], outputs=[fake_A])

    @property
    def build_generator(self):
        def convolutionLayer(layer_input, filters, kernel_size=3, stride=1, relu=True, bn=False, tran=False, bias=True):
            if bn and bias:
                bias = False
            if tran:
                d = Conv2DTranspose(filters, kernel_size=kernel_size, strides=stride, padding='same', use_bias=bias)(layer_input)
            else:
                d = Conv2D(filters, kernel_size=kernel_size, strides=stride, padding='same', use_bias=bias)(layer_input)
            if relu:
                d = ReLU()(d)
            if bn:
                d = BatchNormalization()(d)
            return d

        def residualBlocks(layer_input, filters, kernel_size=3, stride=1):
            d = convolutionLayer(layer_input, filters, kernel_size=kernel_size, stride=stride, relu=True)
            d = convolutionLayer(d, filters, kernel_size=kernel_size, stride=stride, relu=False)
            return d

        def SCM_Block(layer_input, filters, kernel_size=3, stride=1, relu=True, bn=False, tran=False, bias=True):
            d = convolutionLayer(layer_input, filters, kernel_size=kernel_size, stride=stride, relu=True)
            d = convolutionLayer(d, filters, kernel_size=kernel_size - 2, stride=stride, relu=True)
            d = convolutionLayer(d, filters, kernel_size=kernel_size, stride=stride, relu=True)
            d = convolutionLayer(d, filters, kernel_size=kernel_size - 2, stride=stride, relu=True)
            return d

        def eBlock(layer_input, filters, num_res=8):
            e = layer_input
            for num1 in range(num_res):
                e = residualBlocks(e, filters, kernel_size=3, stride=1)
            return e

        def dBlock(layer_input, filters, num_res=8):
            d = layer_input
            for num1 in range(num_res):
                d = residualBlocks(d, filters, kernel_size=3, stride=1)
            return d

        # Input Image
        b1 = Input(shape=self.img_shape)
        b2 = tf.image.resize(b1, [128, 128])
        b3 = tf.image.resize(b2, [64, 64])
        print(b3.shape)
        # Downsampling
        d1 = eBlock(b1, self.gf)
        #print(d1.shape)
        d1 = Conv2D(self.gf, kernel_size=3, strides=2, padding='same')(d1)
        #print(d1.shape)
        b_scm1 = SCM_Block(b2, self.gf)
        #print(b_scm1.shape)
        d2 = Concatenate()([b_scm1, d1])
        #print(d2.shape)
        d2 = eBlock(d2, self.gf*2)
        #print(d2.shape)

        d2 = Conv2D(self.gf*2, kernel_size=3, strides=2, padding='same')(d2)
        #print(d2.shape)
        b_scm2 = SCM_Block(b3, self.gf*2)
        #print(b_scm2.shape)
        d3 = Concatenate()([b_scm2, d2])
        #print(d3.shape)
        d3 = eBlock(d3, self.gf * 4)
        print(d3.shape)

        # # Upsampling
        u3 = dBlock(d3, self.gf * 4)
        print(u3.shape)
        S3 = Conv2D(3, kernel_size=3, strides=1, padding='same')(u3)
        print(S3.shape)
        return Model(b3, S3)
if __name__ == '__main__':
    gan = Pix2Pix()
    gan.train(epochs=201, batch_size=4, sample_interval=50)

CodePudding user response:

In the above example: Model(b3, S3) needs to replace by Model(b1, S3) as follows:

S3 = UpSampling2D(size=2)(S3)
S3 = UpSampling2D(size=2)(S3)
Model(b1, S3)

since the input image started with b1 but in the code, the intermediate image was used as Model(b3, S3) and that could be wrong.

  • Related