【问题标题】:The output from CNN model is either 1.0 or 0.0CNN 模型的输出为 1.0 或 0.0
【发布时间】:2020-05-04 13:17:30
【问题描述】:

正如标题所暗示的那样,预测的输出是 1.0 或 0.0,我没有得到任何介于两者之间的值,导致我的输出要么是 100% 下降,要么是 100% 非下降。

for img in imagesList:
    test_image = image.load_img(path+img, target_size=(64, 64))
    test_images = image.img_to_array(test_image)
    test_images = np.expand_dims(test_image, axis = 0)
    result = classifier.predict(test_images)
    if result[0][0] <= 0.0:
      prediction = 'fall'

      print('The file is:{} while model output is: {}{} {} '.format(img[:-4],((1-result[0][0])*100),'%',prediction))
      print(result[0][0])
      #print('The file is:{} while model output is: {}{} {}'.format(img[:-4],((result[0][0])*100),'%',' nonfall'))
    else:
      prediction = 'nonfall'
      print('The file is:{} while model output is: {}{} {} '.format(img[:-4],((result[0][0])*100),'%',prediction))
      print(result[0][0])

【问题讨论】:

  • classifier 是如何定义的?
  • 分类器是cnn模型。

标签: tensorflow keras conv-neural-network


【解决方案1】:

在每个for 循环迭代中,您将获得单个图像,并且它的预测是任一类别。如果您想要批次的准确性,那么您需要附加批次的图像然后进行预测。代码应如下所示。

image_batch = []

for img in imagesList:
    test_image = image.load_img(path+img, target_size=(64, 64))
    test_images = image.img_to_array(test_image)
    test_images = np.expand_dims(test_image, axis = 0)
    image_batch.append(np.array(test_images))

result = classifier.predict(image_batch)

【讨论】:

  • 如果您不介意,请详细说明一下吗?我是 CNN 的新手。在许多示例中,他们打印“此图像是 90% 的 A 类”,并且他们获得图像的概率值(来自“结果”),而我只获得类标签作为输出。此外,当我使用 predict(image_batch) 时,它会抛出:` `` 层顺序需要 1 个输入,但它接收到 14 个输入张量。 ```
  • 我知道你得到了答案。有关 CNN 的更多详细信息,fashion_mnist 将帮助您更好地了解事情。
【解决方案2】:

编辑:

我使用model.predict_prob(img)解决了这个问题。

参考:https://stackoverflow.com/a/54532343/13467194

【讨论】:

    猜你喜欢
    • 2017-12-18
    • 2015-09-26
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多