Python ';非类型';对象没有属性';数据类型';-热图函数问题
我正在尝试为黑白图像制作GradCam热图。我已经成功地定义了一个函数,它将获取一个图像的路径,并为它创建一个热图。我一定是做错了什么,因为我犯了个错误Python ';非类型';对象没有属性';数据类型';-热图函数问题,python,tensorflow,machine-learning,Python,Tensorflow,Machine Learning,我正在尝试为黑白图像制作GradCam热图。我已经成功地定义了一个函数,它将获取一个图像的路径,并为它创建一个热图。我一定是做错了什么,因为我犯了个错误 AttributeError Traceback (most recent call last) <ipython-input-34-f75f6e449e46> in <module>() 1 img_path2 = "/content/CBIS
AttributeError Traceback (most recent call last)
<ipython-input-34-f75f6e449e46> in <module>()
1 img_path2 = "/content/CBIS_DDSM/png_images/calc_case_description_train_set/Calc-Training_P_00005_RIGHT_CC_1-1.3.6.1.4.1.9590.100.1.2.328778919012412769218080124214088709081-1.3.6.1.4.1.9590.100.1.2.393344010211719049419601138200355094682-000001.NOTHING-base_top.png"
2
----> 3 get_class_activation_map(img_path2)
1 frames
/tensorflow-1.15.2/python3.7/keras/backend/tensorflow_backend.py in mean(x, axis, keepdims)
1808 {{np_implementation}}
1809 """
-> 1810 if x.dtype.base_dtype == tf.bool:
1811 x = tf.cast(x, floatx())
1812 return tf.reduce_mean(x, axis, keepdims)
AttributeError: 'NoneType' object has no attribute 'dtype'
请发布完整的错误跟踪;您所发布的内容没有提供任何有关它在get\u class\u activation\u map()
函数中的确切位置的信息。
def get_class_activation_map(path) :
img_path = path
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (150, 150))
img = np.expand_dims(img,axis=0)
img = np.expand_dims(img,axis=1)
img = np.moveaxis(img, 0, -1)
predict = model.predict(img)
target_class = np.argmax(predict[0])
last_conv = model.get_layer('densenet121')
grads =K.gradients(model.output[:,target_class],last_conv.output)[0]
pooled_grads = K.mean(grads,axis=(0,1,2))
iterate = K.function([model.input],[pooled_grads,last_conv.output[0]])
pooled_grads_value,conv_layer_output = iterate([img])
for i in range(512):
conv_layer_output[:,:,i] *= pooled_grads_value[i]
heatmap = np.mean(conv_layer_output,axis=-1)
for x in range(heatmap.shape[0]):
for y in range(heatmap.shape[1]):
heatmap[x,y] = np.max(heatmap[x,y],0)
heatmap = np.maximum(heatmap,0)
heatmap /= np.max(heatmap)
im = plt.imshow(heatmap)
return im