Networking-Forums.com

Professional Discussions => Programming Goodies and Software-Defined Networking => Topic started by: cloudytechi147 on February 16, 2022, 04:28:59 AM

Title: Viewing intermediate activations: too many indices for array: array is 2-dimensi
Post by: cloudytechi147 on February 16, 2022, 04:28:59 AM
I wrote this model:

def create_network():
  model = Sequential()
  model.add(Input(shape=(150,150,3)))

  model.add(Conv2D(32, kernel_size=3,strides=(1, 1),activation='relu',kernel_initializer="glorot_uniform", padding='valid', dilation_rate=1))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.5))

  model.add(Conv2D(64, kernel_size=3, strides=(1, 1), activation='relu',kernel_initializer="glorot_uniform", padding='valid', dilation_rate=1))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.5))

  model.add(Conv2D(128, kernel_size=3, strides=(1, 1), activation='relu',kernel_initializer="glorot_uniform",  padding='valid', dilation_rate=1))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.5))

  model.add(Conv2D(128, kernel_size=3, strides=(1, 1), activation='relu',kernel_initializer="glorot_uniform",padding='valid', dilation_rate=1))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.5))

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

  model.add(Dense(1, activation='sigmoid'))
  model.compile(optimizer = 'adam',
                   loss = 'binary_crossentropy',
                   metrics = ['accuracy'])
  return model

create_network()


And I fit it to some data:

def fit_model(train_generator=train_generator, validation_generator=validation_generator,network=create_network()):
  checkpoint_path = "/content/drive/model_checkpoint.h5"
  checkpoint_dir = os.path.dirname(checkpoint_path)

  callbacks_list = [
      callbacks.EarlyStopping(
          monitor = 'accuracy',
          patience = 2,
      ),

      callbacks.ModelCheckpoint(
          filepath=checkpoint_path,
          monitor = 'val_loss',
          #save_weights_only=True,
          save_best_only=True,
      ),
  ]



  model = network
  history = model.fit(train_generator,
                      epochs=1000,
                      validation_data=validation_generator,
                      callbacks = callbacks_list,
                      verbose=1
                      )
  score = model.evaluate(validation_generator)
  return history,model

history,model = fit_model(train_generator,validation_generator)


I can read in the model from the Network Monitoring tools:

model = load_model('/content/drive/model_checkpoint.h5')


For an image, I want to visualise every channel in every intermediate activation for the model.

I can read in the image:

from keras.preprocessing import image
file_list = ['/content/drive/image.JPEG']
file_list = file_list[0]


test_image = image.load_img(file_list,target_size=(150,150))
images = image.img_to_array(test_image)
images /= 255.0
images = np.expand_dims(images, axis=0)


To visualise the activations, I wrote:

layer_outputs = [layer.output for layer in model.layers[:]]
activation_model = tf.keras.Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict(images)


for i in range(len(layer_outputs)):
  first_layer_activation = activations[i]
  print(first_layer_activation.shape)
  plt.matshow(first_layer_activation[0, :, :, 1], cmap='viridis')


The output prints:

(1, 148, 148, 32)
(1, 74, 74, 32)
(1, 74, 74, 32)
(1, 72, 72, 64)
(1, 36, 36, 64)
(1, 36, 36, 64)
(1, 34, 34, 128)
(1, 17, 17, 128)
(1, 17, 17, 128)
(1, 15, 15, 128)
(1, 7, 7, 128)
(1, 7, 7, 128)
(1, 6272)


And then an error that says:

      2   first_layer_activation = activations[i]
      3   print(first_layer_activation.shape)
----> 4   plt.matshow(first_layer_activation[0, :, :, 1], cmap='viridis')

IndexError: too many indices for array: array is 2-dimensional, but 4 were indexed


Followed by some images.

Could someone explain what that error is saying and/or how to fix it? I'm not understanding how I get an error, but then it prints some intermediate activations images, so then I'm not sure I've done this properly?
Title: Re: Viewing intermediate activations: too many indices for array: array is 2-dimensi
Post by: deanwebb on February 16, 2022, 09:05:28 AM
There may be others with coding experience that can give you more targeted info... but my first go-to would be to look up the error on StackOverflow and see what the comments on it are.

It looks like you get the data from the network module, but the error is in the indexing of the data.
Title: Re: Viewing intermediate activations: too many indices for array: array is 2-dimensi
Post by: deanwebb on February 16, 2022, 09:06:01 AM
Also, removed link that looked to be advertising.
Title: Re: Viewing intermediate activations: too many indices for array: array is 2-dimensi
Post by: Otanx on February 16, 2022, 10:15:06 AM
In that line 4 it does not like the "first_layer_activation[0, :, :, 1]" It is expecting only two values not four. I am not familiar with the libraries you are using so I can't help much more.

-Otanx