使用我在 tf == 1 中撰寫的這個函式后,我更新了 tensorflow 2.0。我面臨下面提到的相同錯誤
colors = tf.constant(img, dtype=tf.float32)
model = tf.keras.models.model_from_json(json.load(open("model.json"))["model"], custom_objects={})
model.load_weights("model_weights.h5")
predictions = model.predict(colors, batch_size=32, verbose=0)
# Output is one-hot vector for 9 class:["red","green","blue","orange","yellow","pink", "purple","brown","grey"]
predictions = tf.one_hot(np.argmax(predictions, 1), 9)
# Sum along the column, each entry indicates no of pixels
res = tf.reduce_sum(predictions, reduction_indices= 0 ).numpy()
# Threshold is 0.5 (accuracy is 96%) change threshold may cause accuracy decrease
if res[0] / (sum(res[:-1]) 1) > 0.5:
return "red"
elif res[1] / (sum(res[:-1]) 1) > 0.5:
return "green"
elif res[2] / (sum(res[:-1]) 1) > 0.5:
return "blue"
else:
return "other"
錯誤資訊如下
TypeError: reduce_sum() got an unexpected keyword argument 'reduction_indices'
uj5u.com熱心網友回復:
我認為您的問題是reduction_indices在 Tensorflow 2.x 中已棄用,因此請嘗試執行以下操作:
tf.reduce_sum(predictions, axis= 0)
這是等效的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/322395.html
下一篇:在影像上測量和標記長度
