简体   繁体   English

pred = model.predict_classes([prepare(file_path)]) AttributeError: 'Functional' object 没有属性 'predict_classes'

[英]pred = model.predict_classes([prepare(file_path)]) AttributeError: 'Functional' object has no attribute 'predict_classes'

I am trying to load in my keras model on a modified butterfly species classifier on tkinter I think the problem lies within how I trained my model我正在尝试将 keras model 加载到 tkinter 上的修改后的蝴蝶物种分类器上

import cv2
import tensorflow as tf

CATEGORIES = ["Abyssinians", "American Shorthair", "Bengals", "Birman",
              "British Shorthairs", "Devon Rex", "Exotic Shorthairs", "Maine Coon",
              "Oriental Shorthairs", "Persians", "Ragdoll", "Scottish Folds", "Siamese", "Somali", "Sphynx"]  # will use this to convert prediction num to string value
def prepare(filepath):
    IMG_SIZE = 100 # 50 in txt-based
    img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)  # read in the image, convert to grayscale
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))  # resize image to match model's expected sizing
    return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)  # return the image with shaping that TF wants.

model = tf.keras.models.load_model("CAT_BREEDS.model")

prediction = model.predict([prepare(r'D:\Desktop\CATS\validation\Abyssinians\45997693_52.jpg')])
print(prediction)

above are the codes I used to train my keras model but when trying to predict a class using the butterfly classifier I get this error以上是我用来训练我的 keras model 的代码,但是在尝试使用蝴蝶分类器预测 class 时出现此错误

pred = model.predict_classes([prepare(file_path)]) AttributeError: 'Functional' object has no attribute 'predict_classes' pred = model.predict_classes([prepare(file_path)]) AttributeError: 'Functional' object 没有属性 'predict_classes'

import numpy as np from tensorflow import keras from tensorflow.keras.layers import Dense, GlobalAveragePooling2D from tensorflow.keras.optimizers import Adam from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.models import Model from sklearn.metrics import confusion_matrix import itertools import matplotlib.pyplot as plt import numpy as np from tensorflow import keras from tensorflow.keras.layers import Dense, GlobalAveragePooling2D from tensorflow.keras.optimizers import Adam from tensorflow.keras.preprocessing.image import ImageDataGenerator from tensorflow.keras.models import Model from sklearn.metrics import confusion_matrix导入 itertools 导入 matplotlib.pyplot 作为 plt

train_path=r'D:\Desktop\CATS - Copy 2\train' valid_path=r'D:\Desktop\CATS - Copy 2\validation' test_path=r'D:\Desktop\CATS - Copy 2\test' train_path=r'D:\Desktop\CATS - 复制 2\train' valid_path=r'D:\Desktop\CATS - 复制 2\validation' test_path=r'D:\Desktop\CATS - 复制 2\test'

class_labels=["Abyssinians", "American Shorthair", "Bengals", "Birman", "British Shorthairs", "Devon Rex", "Exotic Shorthairs", "Maine Coon", "Oriental Shorthairs", "Persians", "Ragdoll", "Scottish Folds", "Siamese", "Somali", "Sphynx"] class_labels=["Abyssinians", "American Shorthair", "Bengals", "Birman", "British Shorthairs", "Devon Rex", "Exotic Shorthairs", "Maine Coon", "Oriental Shorthairs", "Persians", " Ragdoll”、“Scottish Folds”、“Siamese”、“Somali”、“Sphynx”]

train_batches=ImageDataGenerator(preprocessing_function=keras.applications.xception.preprocess_input) train_batches=ImageDataGenerator(preprocessing_function=keras.applications.xception.preprocess_input)
.flow_from_directory(train_path, target_size=(299,299),classes=class_labels,batch_size=5) valid_batches=ImageDataGenerator(preprocessing_function=keras.applications.xception.preprocess_input) .flow_from_directory(train_path, target_size=(299,299),classes=class_labels,batch_size=5) valid_batches=ImageDataGenerator(preprocessing_function=keras.applications.xception.preprocess_input)
.flow_from_directory(valid_path, target_size=(299,299),classes=class_labels,batch_size=5) test_batches=ImageDataGenerator(preprocessing_function=keras.applications.xception.preprocess_input) .flow_from_directory(valid_path, target_size=(299,299),classes=class_labels,batch_size=5) test_batches=ImageDataGenerator(preprocessing_function=keras.applications.xception.preprocess_input)
.flow_from_directory(test_path, target_size=(299,299),classes=class_labels,batch_size=5, shuffle=False) .flow_from_directory(test_path, target_size=(299,299),classes=class_labels,batch_size=5, shuffle=False)

base_model=keras.applications.xception.Xception(include_top=False) base_model=keras.applications.xception.Xception(include_top=False)

x=base_model.output x=GlobalAveragePooling2D()(x) x=Dense(1024, activation='relu')(x) x=Dense(15, activation='sigmoid')(x) model=Model(inputs=base_model.input, outputs=x) x=base_model.output x=GlobalAveragePooling2D()(x) x=Dense(1024, activation='relu')(x) x=Dense(15, activation='sigmoid')(x) model=Model(inputs=base_model .输入,输出=x)

base_model.trainable = False base_model.trainable = 假

N=1 N=1

model.compile(Adam(lr=.0001),loss='categorical_crossentropy', metrics=['accuracy']) history=model.fit_generator(train_batches, steps_per_epoch=200, validation_data=valid_batches, validation_steps=90,epochs=N,verbose=1) model.compile(Adam(lr=.0001),loss='categorical_crossentropy', metrics=['accuracy']) history=model.fit_generator(train_batches, steps_per_epoch=200, validation_data=valid_batches, validation_steps=90,epochs=N,详细=1)

model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) model.save_weights('model_weights.h5') model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) model.save_weights('model_weights.h5')

print("[INFO]evaluating model...")打印(“[信息]评估 model...”)

test_labels=test_batches.classes predictions=model.predict_generator(test_batches, steps=28, verbose=1) test_labels=test_batches.classes 预测=model.predict_generator(test_batches, steps=28, verbose=1)

model.save('CAT_BREEDS.model') model.save('CAT_BREEDS.model')

I copied your code.我复制了你的代码。 Since I do not have your dataset I used my own which had 2 classes.One error is the line x=Dense(15, activation='sigmoid')(x).因为我没有你的数据集,所以我使用了我自己的数据集,它有 2 个类。一个错误是行 x=Dense(15,activation='sigmoid')(x)。 Since you are doing classification your activation should be activation='softmax'.由于您正在进行分类,因此您的激活应该是 activation='softmax'。 The rest of the code seemed to run OK代码的 rest 似乎运行正常

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:“功能”对象没有属性“predict_classes” - AttributeError: 'Functional' object has no attribute 'predict_classes'' AttributeError:“功能”object 没有属性“predict_classes” - AttributeError: 'Functional' object has no attribute 'predict_classes' Keras AttributeError: 'Sequential' object 没有属性 'predict_classes' - Keras AttributeError: 'Sequential' object has no attribute 'predict_classes' AttributeError: 'Sequential' object 没有属性 'predict_classes'/ - AttributeError: 'Sequential' object has no attribute 'predict_classes'/ AttributeError: 'History' 对象没有属性 'predict_classes' - AttributeError: 'History' object has no attribute 'predict_classes' AttributeError: 'Sequential' object 没有属性 'predict_classes' - AttributeError: 'Sequential' object has no attribute 'predict_classes' 即使使用顺序 model,我也会收到“AttributeError: 'Model' object has no attribute 'predict_classes'” - Even when using Sequential model, I am getting “AttributeError: 'Model' object has no attribute 'predict_classes' ” 编码时出现错误:“AttributeError: 'Sequential' object has no attribute 'predict_classes'”。 这里错了 - I get the error: " AttributeError: 'Sequential' object has no attribute 'predict_classes' " while coding. Its wrong here 不理解 model.predict_classes 与生成器 - Not understanding model.predict_classes with generator model.predict(),model.predict_classes() 和 model.predict_on_batch() 似乎没有产生任何结果 - The model.predict(),model.predict_classes() and model.predict_on_batch() seems to produce no result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM