Cannot convert a symbolic Keras input/output to a numpy array.

2,038 views
Skip to first unread message

Jubayer Hossain Ahad 1712940010

unread,
Apr 21, 2021, 2:35:18 AM4/21/21
to Keras-users
This is a simple problem which I was given but I cannot get around. I am new to tensorflow and here I have to create a Convolutional Network. 

1.  Input  -- CMYK Image, 128x128 resolution. All pixel values are real numbers scaled between 0-1.

2. Convolutional -- 30, 3x3 filters. Stride =1 -- RelU

3. Convolutional -- 12, 5x5 filters. Stride = 1 -- RelU

4. Max Pooling -- 2x2, Stride = 1 

5. Custom Layer, Channel Normalization -- The mean and standard deviation is  calculated for each incoming channel. The  output is a tensor where each channel is  separately normalized (z-scores).

6. Flattening Layer

7. Fully Connected -- 157 neurons -- softmax

These are the specification that I need to meet. My problem is with the 5th layer about to create it. 

And when I am  trying to create a np array from the 4th layer output, it is showing me an error. 

error - Cannot convert a symbolic Keras input/output to a numpy array.


Here is the code up to which I have done,

from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten, Input
from tensorflow.keras.models import Model
import numpy as np


x = tf.keras.Input(shape=(128, 128, 4))
conv = Conv2D(30, (3, 3), activation='relu',input_shape=(128, 128, 4))(x)
conv = Conv2D(12, (5,5))(conv)
conv = MaxPooling2D(pool_size=(2,2))(conv)
print(conv[2])
conv = np.array(conv[2]) # <---- here is the problem
input_mean = np.mean(conv[1:], axis=0)
input_std = np.std(conv, axis=0)
conv = (conv - input_mean) / input_std

conv = Flatten()(conv)
conv = Dense(157, activation='relu')(conv)
model = Model(inputs = x, outputs = conv)
#model.summary()

pushpalatha murthy

unread,
Apr 21, 2021, 8:35:35 AM4/21/21
to Keras-users
Hi,
u can batch normalization or layer normalization to find the mean and std.keras as inbuilt methods, refer these links
To build custom normalization refer below link
the belo is code uses keras to normalize the values which mst be included after activation or before activation function(Relu in this case).pls check below code, which can be modified to write your own custom function


from tensorflow.keras.layers import Dense, Conv2D, MaxPooling2D, Dropout, Flatten, Input,BatchNormalization
from tensorflow.keras.models import Model
import numpy as np
import tensorflow as tf


x = tf.keras.Input(shape=(128, 128, 4))
print(type(x))
conv = Conv2D(30, (3, 3), activation='relu',input_shape=(128, 128, 4))(x)
conv=BatchNormalization()(conv)
print(type(conv))
conv = Conv2D(12, (5,5))(conv)
conv=BatchNormalization()(conv)
conv = MaxPooling2D(pool_size=(2,2))(conv)
conv = Flatten()(conv)
conv = Dense(157, activation='relu')(conv)
model = Model(inputs = x, outputs = conv)
model.summary()

Pls Note u can convert <class 'tensorflow.python.keras.engine.keras_tensor.KerasTensor'> which is type of conv/x to np.array, so get the error.

regards,
pushpalatha M

Vaishnavi Chandilkar

unread,
Jan 18, 2023, 4:39:23 PM1/18/23
to Keras-users
Hi,
I still didno understand how to convert keras tensor into np.array! Can you please explain through code?

Thank you

Lance Norskog

unread,
Jan 18, 2023, 8:01:43 PM1/18/23
to Vaishnavi Chandilkar, Keras-users
Tensorflow tensors are not just a different kind of array. In Tensorflow, you create a directed graph of array processing operations using the tensor data structure. Tensorflow then compiles this directed graph into custom binary code for a regular computer, a GPU computer, or a TPU (tensor processing unit). You cannot put a Numpy array in the middle of this chain of tensor operations.

You cannot use a numpy array in the middle of a Tensorflow deep learning model. I do not know where this extremely basic fact is explained in the Tensorflow manuals.

In this case, you need to create custom Tensorflow operations and use the keras Lambda layer to insert them into the middle of your deep learning model.

Good luck!

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/keras-users/64c9aecb-f71a-4c44-a6e3-0e554122e133n%40googlegroups.com.


--
Lance Norskog
lance....@gmail.com
Redwood City, CA
Reply all
Reply to author
Forward
0 new messages