I wanted to write an API where you can upload images and it says class A or B and I disconnected the model on my Windows PC and uploaded it to the Linux server but it can not load the model
error:
2023-09-12 15:07:06.198999: I tensorflow/tsl/cuda/cudart_stub.cc:28] Could not find cuda drivers on your machine, GPU will not be used.
2023-09-12 15:07:06.199626: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-09-12 15:07:09.020504: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
Traceback (most recent call last):
File "/var/www/panel.kanonen-shield.de/api/ai/eulen_erkennung-api/app.py", line 19, in <module>
model = tf.keras.models.load_model("/var/www/panel.kanonen-shield.de/api/ai/eulen_erkennung-api/my_model.keras")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/var/www/panel.kanonen-shield.de/api/ai/eulen_erkennung-api/myenv/lib/python3.11/site-packages/keras/src/saving/saving_api.py", line 230, in load_model
return saving_lib.load_model(
^^^^^^^^^^^^^^^^^^^^^^
File "/var/www/panel.kanonen-shield.de/api/ai/eulen_erkennung-api/myenv/lib/python3.11/site-packages/keras/src/saving/saving_lib.py", line 275, in load_model
raise e
File "/var/www/panel.kanonen-shield.de/api/ai/eulen_erkennung-api/myenv/lib/python3.11/site-packages/keras/src/saving/saving_lib.py", line 263, in load_model
_load_state(
File "/var/www/panel.kanonen-shield.de/api/ai/eulen_erkennung-api/myenv/lib/python3.11/site-packages/keras/src/saving/saving_lib.py", line 456, in _load_state
_load_container_state(
File "/var/www/panel.kanonen-shield.de/api/ai/eulen_erkennung-api/myenv/lib/python3.11/site-packages/keras/src/saving/saving_lib.py", line 513, in _load_container_state
_load_state(
File "/var/www/panel.kanonen-shield.de/api/ai/eulen_erkennung-api/myenv/lib/python3.11/site-packages/keras/src/saving/saving_lib.py", line 425, in _load_state
trackable.load_own_variables(weights_store.get(inner_path))
File "/var/www/panel.kanonen-shield.de/api/ai/eulen_erkennung-api/myenv/lib/python3.11/site-packages/keras/src/engine/base_layer.py", line 3539, in load_own_variables
raise ValueError(
ValueError: Layer 'conv2d' expected 2 variables, but received 0 variables during loading. Expected: ['conv2d/kernel:0', 'conv2d/bias:0']```
API code:
from werkzeug.utils import secure_filename
from flask import Flask, request, jsonify
import os
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import numpy as np
app = Flask(name)
UPLOAD_FOLDER = ‘E:/ki/test/img’
ALLOWED_EXTENSIONS = {‘png’, ‘jpg’, ‘jpeg’, ‘gif’}
app.config[‘UPLOAD_FOLDER’] = UPLOAD_FOLDER
def allowed_file(filename):
return ‘.’ in filename and filename.rsplit(‘.’, 1)[1].lower() in ALLOWED_EXTENSIONS
model = tf.keras.models.load_model(“E:/ki/test/my_model01.keras”)
def classify_single_image(img_path):
img = image.load_img(img_path, target_size=(150, 150))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = x/255.0
prediction = model.predict(x)
return prediction[0][0] > 0.5
@app.route(‘/predict’, methods=[‘POST’])
def predict():
if ‘photo’ not in request.files:
return jsonify(error=‘No photo provided’), 400
file = request.files['photo']
if file.filename == '':
return jsonify(error='No selected file'), 400
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
prediction = bool(classify_single_image(filepath))
return jsonify(prediction=prediction)
if name == ‘main’:
app.run(port=3010)