gpt4 book ai didi

python - 预期 input_1 的形状为 (224, 224, 3),但得到的数组的形状为 (400, 401, 3)

转载 作者:太空宇宙 更新时间:2023-11-03 20:20:18 29 4
gpt4 key购买 nike

在 Keras 中运行文本分类模型时,调用 model.predict 函数时出现以下错误。

Expected input_1 to have shape (224, 224, 3) but got array with shape (400, 401, 3)

这是我的模型的代码

from flask import render_template, jsonify, Flask, redirect, url_for, request
from app import app
import random
import os
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

UPLOAD_FOLDER = '/Users/lorenzocastagno/Desktop/flaskSaaS-master/app/forms'

@app.route('/')
@app.route('/index')
def index():
return render_template('index.html', title='Home')




@app.route('/upload.php', methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST':
f = request.files['file']
path = os.path.join(app.config['UPLOAD_FOLDER'], f.filename)
model= ResNet50(weights='imagenet')
img = image.load_img(os.path.join(UPLOAD_FOLDER, f.filename))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
preds_decoded = decode_predictions(preds, top=3)[0]
print(decode_predictions(preds, top=3)[0])
f.save(path)
return render_template('uploaded.html', title='Success', predictions=preds_decoded, user_image=f.filename)





@app.route('/map')
def map():
return render_template('map.html', title='Map')


@app.route('/map/refresh', methods=['POST'])
def map_refresh():
points = [(random.uniform(48.8434100, 48.8634100),
random.uniform(2.3388000, 2.3588000))
for _ in range(random.randint(2, 9))]
return jsonify({'points': points})


@app.route('/contact')
def contact():
return render_template('contact.html', title='Contact')

我无法弄清楚问题出在哪里以及如何解决它。

我认为问题出在数据的维度上,我应该 reshape 它吗?

最佳答案

您需要将 x 的大小调整为 (224,224,3),因为您的模型仅接受此形状的输入。

关于python - 预期 input_1 的形状为 (224, 224, 3),但得到的数组的形状为 (400, 401, 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58208954/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com