【问题标题】:When im converting the predicted value this gives me IndexError: invalid index to scalar variable当我转换预测值时,这给了我 IndexError: invalid index to scalar variable
【发布时间】:2021-06-15 14:20:07
【问题描述】:
@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 - - [18/Mar/2021 13:10:07] “POST /predict HTTP/1.1”500 -

【问题讨论】:

  • 您能否提供一个可以独立运行的简单代码,它只重现您面临的问题?我的意思是 - pred_class 是什么?谢谢
  • @MichaelSidorov 只是它显示了这个预测 [[0. 1. 0. 0. 0. 0. 0.]] 预测 [[0. 1. 0. 0. 0. 0. 0.]] [1] - 这些只是打印语句 [1] 表示类的索引
  • print(pred_class) 语句的结果添加到您的问题中。您在评论中显示的值很难阅读,并且似乎不相关。您/我们需要弄清楚 3 级索引有什么问题。

标签: python pandas numpy tensorflow keras


【解决方案1】:

如果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 应该可以修复问题。

干杯。

【讨论】:

  • 是的,它可以工作,但是当 return 语句执行时,它会显示一个类似 Result : [0] 的数组,因此“0”显示相关的类索引。如何显示类名基本上是一个字符串
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
相关资源
最近更新 更多