def build_model(network):
model = Sequential()
model.add(Conv2D(6, (5,5), padding='valid', activation = 'relu', kernel_initializer='he_normal', input_shape=(32,32,3)))
print(np.asarray(model.get_weights())[0].shape)
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Conv2D(16, (5,5), padding='valid', activation = 'relu', kernel_initializer='he_normal'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(120, activation = 'relu', kernel_initializer='he_normal'))
model.add(Dense(84, activation = 'relu', kernel_initializer='he_normal'))
model.add(Dense(10, activation = 'softmax', kernel_initializer='he_normal'))
sgd = optimizers.SGD(lr=learning_rate)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
return model
这是Keras的模特。我想在第一个转换层中可视化过滤器。我想绘制滤镜本身,而不是当我们将渐变反向传播到图像时出现的图案。
我找到了一种获取权重的方法 - 使用model.get_weights()
如何绘制此权重? np.asarray(model.get_weights())[0]
的形状是(5,5,3,6)。
如何制作6个尺寸为5x5x3的过滤器?
答案 0 :(得分:0)
使用matplotlib可以绘制切片。
您应该自己分开切片,例如:
import matplotlib.pyplot as plt
#normalize these filters first, otherwise they won't be in a suitable range for plotting:
maxVal = filters.max()
minVal = filters.min()
absMax = max(abs(minVal),abs(maxVal))
filters = (filters / absMax)*255
for outputChannel in range(6):
for inputChannel in range(3):
filt = filters[:,:,inputChannel,outputChannel]
#a trick to see negatives as blue and positives as red
imageRed = np.array(filt)
imageBlue = np.array(filt)
imageRed[imageRed<0] = 0
imageBlue[imageBlue>0]= 0
print(imageRed)
print(imageBlue)
finalImage = np.zeros((filt.shape[0],filt.shape[1],3))
finalImage[:,:,0] = imageRed
finalImage[:,:,2] = -imageBlue
#plot image here
plt.figure()
plt.imshow(finalImage)