tensorflow 如何将model.predict()扩展为100%

ubof19bj  于 2023-10-23  发布在  其他
关注(0)|答案(1)|浏览(114)

tensorflow模型.predict()返回类结果。我想将它们缩放为sum %100,
预测功能:
img = "./img/hat.jpg"

def predict(img):
    img = preprocess_image(img)
    pred = model.predict(img)
    pred = np.squeeze(pred).astype(float)
    print(pred)
    return dict(zip(class_names, pred))

输出

{'dress': -1.1803117990493774, 'hat': -3.3129501342773438, 'longsleee': -4.05311393737793, 'outwear': -3.61092472076416, 'pants': -1.313247561454773, 'shirt': -4.622654438018799, 'shoes': 13.270466804504395, 'shorts': -1.8179067373275757, 'skirt': -5.134970188140869, 't-shirt': -2.795290231704712}

我想看看帽子:0.98,鞋子:0.003等等。

epfja78i

epfja78i1#

解决了

pred = np.squeeze(np.exp(pred)/np.sum(np.exp(pred))).astype(float)

函数返回的是百分比。

相关问题