Home > OS >  Visualisation of intermediate activations in keras: 'History' object has no attribute 
Visualisation of intermediate activations in keras: 'History' object has no attribute 

Time:02-08

I have this function to generate a neural network:

def create_network(): 
  model = Sequential()

  model.add(Input(shape=(150,150,3)))
  model.add(Conv2D(32, kernel_size=3,strides=(1, 1),activation='relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))

  model.add(Flatten())
  model.add(Dense(512, activation='relu'))

  model.add(Dense(1, activation='relu'))

  model.compile(optimizer = 'rmsprop',
                   loss = 'binary_crossentropy', 
                   metrics = ['accuracy'])
  return model

and then I call the model:

network = create_network()

Then I find the model:

def fit_model(X_train, y_train, network=create_network()):
  model = network
  history = model.fit(X_train,
                      y_train,
                      epochs=2,
                      validation_data=(X_test, y_test),
                      batch_size=32,
                      verbose=2
                      )
  return history

and call this function:

model = fit_model(X_train,y_train)

I want to visualise the intermediate activations in this model (i.e. every channel in every intermediate activation), for a random image.

So I have this code:

images = []
for img_path in glob.glob('test_image*.JPEG'):
    image1 = mpimg.imread(img_path)
    open_file = image1 / 255
    resize = cv2.resize(open_file,(150,150))
    print(resize.shape)
    images.append(resize)
  
plt.figure(figsize=(20,10))
columns = 5
for i, image in enumerate(images):
    plt.subplot(len(images) / columns   1, columns, i   1)
    plt.imshow(image)

print(network.summary())

layer_outputs = [layer.output for layer in model.layers[:]]
activation_model = model.Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict(np.expand_dims(images[0]), axis=0)

When I run this I get the error:

'History' object has no attribute 'layers'

So then I thought ok I'm meant to run this on the network architecture with the layers itself, and not the model that's been fit, so I changed the last three lines to:

layer_outputs = [layer.output for layer in network.layers[:]]
activation_model = network.Model(inputs=network.input, outputs=layer_outputs)
activations = activation_model.predict(np.expand_dims(images[0]), axis=0)

And I get:

AttributeError: 'Sequential' object has no attribute 'Model'

Then I thought maybe initially I should be reading in the layers into layer_outputs, but then using the model in the activation_model:

layer_outputs = [layer.output for layer in network.layers[:]]
activation_model = model.Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict(np.expand_dims(images[0]), axis=0)

But I get:

'History' object has no attribute 'Model'

Could someone show me where I'm going wrong to visualise my intermediate activations?

CodePudding user response:

The problem is that model.fit actually returns a history object and not your model itself. Check the docs:

A History object. Its History.history attribute is a record of training loss values and metrics values at successive epochs, as well as validation loss values and validation metrics values

It does not have any layers etc. Try something like this:

import tensorflow as tf

def create_network(): 
  model = tf.keras.Sequential()
  model.add(tf.keras.layers.Conv2D(32, kernel_size=3,strides=(1, 1),activation='relu'))
  model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2)))

  model.add(tf.keras.layers.Flatten())
  model.add(tf.keras.layers.Dense(512, activation='relu'))

  model.add(tf.keras.layers.Dense(1, activation='relu'))

  model.compile(optimizer = 'rmsprop',
                   loss = 'binary_crossentropy', 
                   metrics = ['accuracy'])
  return model


def fit_model(network=create_network()):
  model = network
  history = model.fit(tf.random.normal((50, 150, 150, 3)),
                      tf.random.normal((50, 1)),
                      epochs=2,
                      validation_data=(tf.random.normal((50, 150, 150, 3)), tf.random.normal((50, 1))),
                      batch_size=32,
                      verbose=2
                      )
  return history, model

history, model = fit_model()
layer_outputs = [layer.output for layer in model.layers[:]]
activation_model = tf.keras.Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict(tf.random.normal((1, 150, 150, 3)))
  •  Tags:  
  • Related