I’ve trained a machine learning model in Orange that classifies dogs and cats with great accuracy. However, when I export the model to a pickle file and load it in Python, it consistently predicts “cat” regardless of the input data.
This is what i wrote in python:
import pickle
from PIL import Image
import numpy as np
modello = 'modelli/catDogsLogisticRegression.pkcls'
def load_model_from_pickle(modello):
try:
with open(modello, 'rb') as file_pickle:
model = pickle.load(file_pickle)
return model
except FileNotFoundError:
print(f"File {modello} non trovato.")
return None
def preprocess_image(image_path):
# Carica l'immagine
img = Image.open(image_path)
# Ridimensiona l'immagine e convertila in scala di grigi
img = img.resize((32, 64)).convert('L')
# Converte l'immagine in un array numpy e ridimensiona in un unico vettore
img_array = np.array(img).reshape(1, -1)
return img_array
# Esempio di utilizzo
loaded_model = load_model_from_pickle(modello)
if loaded_model:
print("Modello caricato con successo")
# Utilizzo del modello
# Carica e pre-elabora un'immagine
image_path = 'cane.jpg'
new_data = preprocess_image(image_path)
# Prevedere la classe del nuovo esempio
predicted_class = loaded_model.predict(new_data)[0]
print("Classe prevista:", 'Gatto' if predicted_class == 0 else 'Cane')
else:
print("Errore nel caricamento del modello.")
Any ideas why the model behaves differently in Python? How can I fix this issue?
New contributor
Jolo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.