3

I have seen other similar questions here but they didn't help me. I'm using tensorflow 2.3 and I'm having an issue when i try to call a function that returns a keras.Model .

 AttributeError: 'Tensor' object has no attribute 'numpy' Tensorflow 2.3

I write here a snippet of my model

    inputs = Input(shape=(n_ch, height, width), batch_size=batchSize)

    conv_1 = Conv2D(16, (5, 5), kernel_initializer='he_normal', padding='same',data_format='channels_first')(inputs)
    conv_1 = BatchNormalization(axis=1)(conv_1)
    conv_1 = Activation("relu")(conv_1)
    conv_2 = Conv2D(16, (5, 5), kernel_initializer='he_normal', padding='same',data_format='channels_first')(conv_1)

    conv_2 = BatchNormalization(axis=1)(conv_2)
    conv_2 = Activation("relu")(conv_2)


    pool_1 = MaxPooling2D(pool_size=(2, 2), data_format='channels_first')(conv_2)


    unpool_1 = Lambda(unpool)(pool_1)

    conv_25 = Conv2D(16, (5, 5), kernel_initializer='he_normal', padding='same',data_format='channels_first')(unpool_1)
    conv_25 = BatchNormalization(axis=1)(conv_25)
    conv_25 = Activation("relu")(conv_25)
    conv_26 = Conv2D(1, (1, 1),data_format="channels_first",activation="sigmoid")(conv_25)
    print("Build decoder done..")

    model = Model(inputs=inputs, outputs=conv_26, name="SegNet")

    return model

I get an error here:

def unpool(pool):
    updates = pool.numpy()
    [...]

It says that pool is a Tensor object but I don't understand why. I want to use eager mode and tf.executing_eagerly() returns True. I also tried to set run_eagerly=True and/or tf.config.run_functions_eagerly(True) but nothing.

Any suggestions? I don't know what to do. Thank you

6
  • How are the two code snippets related to each other? What do you call unpool with? Please provide a complete code example that produces the error. Commented Oct 16, 2020 at 22:48
  • 1
    Btw eager execution is active by default in tensorflow 2.x, the stated commands are not necessary. Commented Oct 16, 2020 at 22:48
  • @Alperino unpool is used with Lambda layers, look at the code I know that eager execution is active by default but I tried that anyway Commented Oct 16, 2020 at 23:18
  • I believe a tf.keras model ends up being decorated with tf.function, so each command within the model is not executing eagerly anymore. Try with experimental_run_tf_function=False in your call to model.compile. Commented Oct 16, 2020 at 23:50
  • I guess you are trying to insert some numpy-operations as calculation steps into a tf.model, which is not possible the way you implied. Please be more specific on what you are trying to achieve and what unpool is meant to do. Commented Oct 17, 2020 at 22:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.