首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误的输入形状densenet212

错误的输入形状densenet212
EN

Stack Overflow用户
提问于 2021-04-07 20:42:48
回答 1查看 29关注 0票数 0

我正在尝试在DenseNet121中为灰度图像创建梯度凸轮显着图。我在运行我的代码时遇到了问题,无法使我的图像符合所需的输入形状。

这是我的热图代码

代码语言:javascript
复制
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)
    
    
    predict = model.predict(img)
    target_class = np.argmax(predict[0])
    last_conv = model.get_layer('conv2d_119')
    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)
    plt.imshow(heatmap)

我得到的错误是

代码语言:javascript
复制
ValueError: Error when checking input: expected densenet121_input to have shape (150, 150, 1) but got array with shape (1, 150, 150)

你能告诉我如何改变我的代码,使它改变尺寸本身吗?

EN

回答 1

Stack Overflow用户

发布于 2021-04-07 20:55:43

在执行直线预测之前:

代码语言:javascript
复制
predict = model.predict(img)

执行以下操作:

代码语言:javascript
复制
img = np.moveaxis(img, -1, 0)

这将使形状从channels_first反转为channels_last,这是您的模型中所期望的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66986296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档