1

I am getting the following error while calling the model.predict function when running a text classification model in Keras.

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

This is the code of my model

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

I am not able to figure out where the problem lies and how to fix it.

I think the problem lies on the dimension of the data, should I reshape it?

Opt ZqGeo
  • 19
  • 3

1 Answers1

0

You need to resize the x to (224,224,3) because your model only takes the input of this shape.

zihaozhihao
  • 4,197
  • 2
  • 15
  • 25