philipperemy/keract

Interaction with submodels?

LuisFMCuriel opened this issue · 5 comments

Hi,
First of all, very nice job! It seems like a very useful tool. I have been trying to plot a convolution layer in a submodel, but it doesn't find the layer. My model includes a subblock where I perform the convolutions in my NN. Just to be clear about what I am doing; the convolutions are inside a submodel in my main model (the shapes and number of convolution layers in the submodel image are not correct because I changed it to create the model, but is just a way to visualize the problem):
imagen

When I try to plot the convolution layers, it does not find the layers:

layers= ["conv"]
activations= keract.get_activations(model, batch_test, layer_names= layers, nodes_to_evaluate= None, output_format= 'simple', auto_compile= True)

'Could not find a layer with name: [conv]. Network layers are [conv, conv_1, flatten, dense, dense_1, dense_2]'

I tried to go directly into the submodel by changing model by model.layers[0]

layers= ["conv2d"]
activations= keract.get_activations(model.layers[0], batch_test, layer_names= layers, nodes_to_evaluate= None, output_format= 'simple', auto_compile= True)

'Could not find a layer with name: [conv2d]. Network layers are [conv2d, conv2d_1, dropout]'

and if I just try to plot all the layers in the submodel

activations= keract.get_activations(model.layers[0], batch_test, layer_names= layers, nodes_to_evaluate= None, output_format= 'simple', auto_compile= True)

'Nodes list is empty. Or maybe the model is empty.'

which I don't think is correct because I just trained it. I would really appreciate it if you can give me some feedback to use the tool.

Thanks in advance.

@LuisFMCuriel hey!! Thanks for reporting!
Can you provide a quick snippet that I can run to see which it's failing here?

Hey @philipperemy, thanks for replying.

Sure, you can use this code to create the same model:

#Generate the model
class Conv(tf.keras.models.Model):
  def __init__(self, filters = 256, layers = 3):
    super().__init__()
    self.conv = [tf.keras.layers.Conv2D(filters=filters, kernel_size=(3,3), padding="same", activation="relu") for _ in range(layers)]
    self.drop = tf.keras.layers.Dropout(0.2)

  def call(self, input):
    x = input
    for i, layer in enumerate(self.conv):
      x = layer(x)
      if i == 0:
          x = self.drop(x)
    return x
  
  def plot_model(self):
    dims = (256,256,3)
    x = tf.keras.layers.Input(dims)
    return tf.keras.models.Model(inputs=[x], outputs=[self.call(x)])


class Model(tf.keras.models.Model):
  def __init__(self, down = 2, filters = [32,64]):
    super(Model, self).__init__()
    self.down = [Conv(filters=filters[i], layers = 2) for i in range(down)]
    self.conv = tf.keras.layers.Conv2D(128, activation="relu", kernel_size=(3,3))
    self.flatten = tf.keras.layers.Flatten()
    self.Dense = tf.keras.layers.Dense(128, activation="relu")
    self.Dense2 = tf.keras.layers.Dense(64, activation="relu")
    self.out = tf.keras.layers.Dense(1, activation="sigmoid")

  def call(self, inputs):
    x = inputs
    for layer in self.down:
        x = layer(x)
        x = tf.keras.layers.MaxPool2D()(x)
    x = self.conv(x)
    x = self.flatten(x)
    x = self.Dense(x)
    x = self.Dense2(x)
    x = self.out(x)
    return x

  
  def plot_model(self):
    x = tf.keras.layers.Input(shape=(256,256,3))
    return tf.keras.models.Model(inputs=[x], outputs=[self.call(x)])

model = Model()

Please if you need more information, do not hesitate to write me.

Thanks!!

@LuisFMCuriel So I've been researching extensively with the sub models and I could not find a way to make it work all the time...

So long story short, submodels are not properly handled. That's a limitation of the lib.... I'll add it to the README.