当im转换预测值时,这会给我IndexError:标量变量的无效索引


@app.route('/predict', methods=['GET', 'POST'])
def upload():
if request.method == 'POST':
# Get the file from post request
f = request.files['file']
# Save the file to ./uploads
basepath = os.path.dirname(__file__)
file_path = os.path.join(
basepath, 'uploads', secure_filename(f.filename))
f.save(file_path)
# Make prediction
preds = model_predict(file_path, model)
print('make predict', preds)
# Process your result for human
pred_class = preds.argmax(axis=-1)  # Simple argmax
print(pred_class)
# pred_class = decode_predictions(preds, top=1)   # ImageNet Decode
result = str(pred_class[0][0][1])  # Convert to string
return result
return None

result=str(pred_class[0][0][1](#转换为字符串IndexError:标量变量的索引无效。127.0.0.1-[2021年3月18日13:10:07]";POST/预测HTTP/1.1";500-

如果是make_predict = [[0. 1. 0. 0. 0. 0. 0.]],则尝试访问从pred_class[0][0]获得的整数0的第一个索引,因此通过删除result = str(pred_class[0][0][0])中的冗余索引器并将其更改为result = str(pred_class[0][0]) # Convert to string应该可以解决此问题。

干杯。

最新更新