How can I print fllaten Layers Output in CNN

Viewed 30

I use a two path CNN for image processing. I want to print the Output of flatten layer. but when I want to print the output of flatten layer i am encountering this error:

None Traceback (most recent call last):

extracted_features = model.layers32

File "C:\Miniconda3\envs\py36\lib\site-packages\keras\engine\base_layer.py", line 440, in call self.assert_input_compatibility(inputs)

File "C:\Miniconda3\envs\py36\lib\site-packages\keras\engine\base_layer.py", line 285, in assert_input_compatibility str(inputs) + '. All inputs to the layer '

ValueError: Layer flatten_39 was called with an input that isn't a symbolic tensor. Received type: <class 'numpy.ndarray'>. Full input: [array([[[[0.51611316, 0.39746055, 0.5936753 ], [0.13327277, 0.34352553, 0.7586124 ], [0.99989164, 0.43303576, 0.5733409 ], ..., [0.05266873, 0.37530503, 0.12953256], [0.2471831 , 0.71032923, 0.25836825], [0.90023595, 0.96091986, 0.3706109 ]],

    [[0.14299522, 0.1676571 , 0.55554783],
     [0.01679909, 0.32093376, 0.8943776 ],
     [0.53657365, 0.83452356, 0.1076134 ],
     ...,
     [0.77388775, 0.6101447 , 0.14687575],
     [0.47331956, 0.5162232 , 0.5501466 ],
     [0.8316039 , 0.12746046, 0.23557535]],

    [[0.745321  , 0.12560171, 0.43350193],
     [0.7007471 , 0.5507861 , 0.19451097],
     [0.55038804, 0.05661515, 0.03471742],
     ...,
     [0.70201683, 0.1263634 , 0.13524196],
     [0.24133243, 0.564834  , 0.64897084],
     [0.15477973, 0.9518493 , 0.5475725 ]],

    ...,

    [[0.8317609 , 0.5864187 , 0.48500198],
     [0.14422454, 0.05594445, 0.5835567 ],
     [0.67728573, 0.63966084, 0.18180694],
     ...,
     [0.5717842 , 0.41698074, 0.22059603],
     [0.9030291 , 0.51441693, 0.2996456 ],
     [0.9422357 , 0.03952885, 0.5567205 ]],

    [[0.28786188, 0.4799473 , 0.92904633],
     [0.08306517, 0.9738849 , 0.9621486 ],
     [0.06010618, 0.47993308, 0.5765873 ],
     ...,
     [0.8719943 , 0.43593037, 0.53257006],
     [0.11550295, 0.48797372, 0.66570866],
     [0.32191348, 0.88470393, 0.19814569]],

    [[0.39397806, 0.78230894, 0.41833642],
     [0.38489413, 0.43042594, 0.76435673],
     [0.94453263, 0.5084677 , 0.8752447 ],
     ...,
     [0.16410913, 0.45611975, 0.23094064],
     [0.6859947 , 0.57103187, 0.3729119 ],
     [0.7035603 , 0.72346485, 0.63314646]]]], dtype=float32)]. All inputs to the layer should be tensors.

Code:


input_shape = (IMAGE_WIDTH, IMAGE_HEIGHT, 3)

num_classes = 3

# Data
path = "D:/Datesets/4- Card/data"
modelName = '2_Class'
training_data_dir = path + "/training" # 500 * 2
validation_data_dir = path + "/validation" # 100 * 2
test_data_dir = path + "/test" # 150
MODEL_FILE = modelName+ '-Vgg&ResnetBachSize'+ str(BATCH_SIZE)+ 'Epoch'+ str(EPOCHS)+ '.h5'

input1 = modelBuilder.defineInput()
input2 = modelBuilder.defineInput()

# First Path Of Training
finput1=modelBuilder.configInitialModel(input1)           finput2=modelBuilder.configInitialModel(input2)  
                                                          
output=modelBuilder.concatinateflattenInputs(finput1,finput2)

model=modelBuilder.makeModel(input1, input2, output) #(input1, input2, output)
print(model.summary())

img = np.random.random((1, 200, 200, 3)).astype("float32")

extracted_features = model.layers[32](img)

extracted_features = model.layers[33](img)
1 Answers

I think you are facing this issue because you are passing a numpy array to the layer (check the error message). Could you try transforming the numpy array to a tensor?

Something like

img = torch.tensor(img)
extracted_features = model.layers[32](img)

If you are working with keras, the step would look like this

Edit Copied from the answer above -

For keras/tensorflow, try this

import tensor as tf
#Considering y variable holds numpy array
y_tensor = tf.convert_to_tensor(y, dtype=tf.int64) 
Related