Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a layer's type in Keras?

Tags:

python

keras

I'm trying to select the last Conv2D layer for a given (general) model. model.summary() provides a list of layers with their type, but how can I access this to find the layer of that type?

Output from model.summary():

Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 224, 224, 3)       0         
_________________________________________________________________
block1_conv1 (Conv2D)        (None, 224, 224, 64)      1792      
_________________________________________________________________
...
_________________________________________________________________
predictions (Dense)          (None, 1000)              4097000   
=================================================================
Total params: 138,357,544
Trainable params: 138,357,544
Non-trainable params: 0
like image 812
TIF Avatar asked Dec 11 '22 02:12

TIF


2 Answers

I think what you are looking for is this:

layer.__class__.__name__

where layer is in

model.layers

like image 184
AlexConfused Avatar answered Jan 05 '23 01:01

AlexConfused


You can iterate over model.layers in reverse order and check layer types via isinstance:

next(x for x in model.layers[::-1] if isinstance(x, keras.layers.Conv2D))
like image 34
a_guest Avatar answered Jan 05 '23 01:01

a_guest