首页 > 解决方案 > 为什么我在 tensorflow 中使用 .predict() 时没有得到预测的类号?

问题描述

def create_dataset():
    data_set = []
    X = []
    y = []
    for category in categories:
        print(category)
        path = os.path.join(data_directory, category)
        class_num = categories.index(category)
        list_directory = os.listdir(path)
        # Only 1000 images per class
        for i in range(1000):
            image = list_directory[i]
            img_array = cv2.imread(os.path.join(path, image))
            resized_array = cv2.resize(img_array, (img_size, img_size))
            # plt.imshow(resized_array)
            # plt.show()
            data_set.append([resized_array, class_num])
            X.append(resized_array)
            y.append(class_num)
    print(len(y))
    X = np.array(X).reshape(-1, img_size, img_size, 3)
    y = np.array(y)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 1)
    X_test, X_val, y_test, y_val = train_test_split(X_test, y_test, test_size = 0.5, random_state = 1)
    return X_train, y_train, X_test, y_test, X_val, y_val
  
X_train, y_train, X_test, y_test, X_val, y_val = create_dataset()

# Here I make the model and evaluate it, but I'm not including it because it is not needed

print(X_train[0].shape)
pred = classifier.predict(X_train[0].reshape(-1, 224, 224, 3))

我正在制作一个模型来对不同类型的图像进行分类。我遇到的问题是最后一行代码。当我运行它时,我得到:

[[3.91389449e-06 1.11615881e-08 2.24666564e-07 9.99938607e-01
  2.00164246e-10 1.83405991e-05 2.06463611e-08 6.69430600e-09
  5.39688244e-07 1.77330275e-10 3.19867821e-09 6.77656585e-12
  1.98875694e-10 5.10717770e-12 1.31831293e-05 2.20323855e-05
  1.35836979e-06 8.74839923e-11 6.57615900e-08 8.09217404e-09
  5.62074649e-12 4.38901693e-10 1.99592010e-10 1.74975312e-11
  3.11663750e-11 7.04729430e-08 3.29025940e-09 4.51312729e-07
  5.99216732e-10 9.16396118e-07 2.29962542e-11 1.12027525e-08
  2.36132919e-10 1.46701609e-10]]

这很奇怪,因为我期待一个从 0 到 33 的数字,每个对应一个类。当我运行评估时,它是 0.88,所以看起来评估正在工作。我使用预测的方式或我在最后一行重新调整数据的方式有问题吗?

标签: pythontensorflow

解决方案


如果您在最后一个密集层中使用loss = 'categorical_crossentropy'softmax,则输出总和将等于 1。您可以将它们解释为属于每个类别的概率。然后最大索引应该给你预测的类。简单的例子:

import numpy as np
prediction = np.array([[0.1, 0.2, 0.7], [0.3,0.5,0.2] , [0.1,0.1,0.8]])
index = np.argmax(prediction, axis = 1)
--> index
Out[8]: array([2, 1, 2], dtype=int64)

如果你在二元分类中使用sigmoid了 with binary_crossentropy,你会得到一个类似 的输出np.array([[0.1],[0.3],[0.8]]),然后你会设置一个阈值,即 0.5,如果预测大于这个阈值,它属于第二类。使用np.argmax将毫无意义。


推荐阅读