期望输入_1具有形状(224,224,3),但具有形状为(400,401,3)的数组]]

问题描述 投票:0回答:1

在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')

我不知道问题出在哪里以及如何解决。

我认为问题在于数据的维度,我应该重塑它吗?

在Keras中运行文本分类模型时,调用model.predict函数时出现以下错误。期望input_1具有形状(224、224、3),但数组的形状为(...

python keras shapes
1个回答
0
投票

您需要将x的大小调整为(224,224,3),因为您的模型仅采用此形状的输入。此外,您最好进行与培训阶段相同的预处理,以获得一致的结果。

© www.soinside.com 2019 - 2024. All rights reserved.