I'm trying to view every channel for all of the intermediate activations for my model for an image, following this tutorial.
I wrote this:
import glob
import matplotlib
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import imageio as im
from keras import models
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers import Dropout
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint
images = []
for img_path in glob.glob('/content/pic1*.JPEG'):
image1 = mpimg.imread(img_path)
open_file = image1 / 255
resize = cv2.resize(open_file,(150,150))
images.append(open_file)
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(model.summary())
layer_outputs = [layer.output for layer in model.layers[:]]
activation_model = models.Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict(images[0])
[1]: https://towardsdatascience.com/visualizing-intermediate-activation-in-convolutional-neural-networks-with-keras-260b36d60d0
I get the error:
ValueError: Input 0 of layer "model_12" is incompatible with the layer: expected shape=(None, 150, 150, 3), found shape=(32, 448, 3)
I understand that it's saying that I have the wrong shape input, but is the line cv2.resize(open_file, (150,150)) not meant to be changing that?
CodePudding user response:
You are appending the image before resizing.
for img_path in glob.glob('/content/pic1*.JPEG'):
image1 = mpimg.imread(img_path)
open_file = image1 / 255
resize = cv2.resize(open_file,(150,150))
# images.append(open_file) # WRONG
images.append(resize)
Also, I think the input shape for your model is 4-dimensional.
# activations = activation_model.predict(images[0]) # not sure this works
activations = activation_model.predict(np.expand_dims(images[0]), axis=0)
For details, check out section 'Predicting the class of unseen images' of the tutorial you mentioned.
