【问题标题】:predicting class of perticular image after training CNN using keras使用 keras 训练 CNN 后预测特定图像的类别
【发布时间】:2020-01-16 12:31:34
【问题描述】:

培训

        import keras  
        import numpy as np  
        import matplotlib.pyplot as plt





        from keras.preprocessing.image import ImageDataGenerator  




        datagen= ImageDataGenerator(rotation_range=40,width_shift_range=0.2             
        ,height_shift_range=0.2,zoom_range=0.2,rescale=1./255.)





        type(datagen)




        from keras.models import Sequential  
        from keras.layers import Conv2D,MaxPool2D,Flatten,Dense,Activation  
        from keras.activations import relu , softmax  
        from keras.losses import categorical_crossentropy  
        from keras.optimizers import SGD,RMSprop  

        from keras.callbacks import TensorBoard  




        model=Sequential()  

        model.add(Conv2D(32,(3,3),input_shape=(150,150,3),activation="relu"))  
        model.add(MaxPool2D(pool_size=(2,2)))  

        model.add(Conv2D(32,(3,3),activation="relu"))  
        model.add(MaxPool2D(pool_size=(2,2)))  

        model.add(Conv2D(64,(3,3),activation="relu"))  
        model.add(MaxPool2D(pool_size=(2,2)))  


        model.add(Flatten())  

        model.add(Dense(1024,activation="relu"))  
        model.add(Dense(512,activation="relu"))  
        model.add(Dense(512,activation="relu"))  
        model.add(Dense(512,activation="relu"))  
        model.add(Dense(512,activation="relu"))  
        model.add(Dense(512,activation="relu"))  
        model.add(Dense(5,activation="softmax"))  





        model.compile(loss="categorical_crossentropy" , optimizer=SGD(),metrics=["acc"])  




        train_gen=datagen.flow_from_directory("/home/vishu//Desktop/basics/dataset",target_size=    
        (150,150),batch_size=100)  




        tb=TensorBoard(log_dir=".")  




        model_history=model.fit_generator(train_gen,epochs=2)  

预测

        import cv2  


        CATEGORIES = ['1','2','3','4','5']  

        def prepare(filepath):  
            IMG_SIZE = 150  
            img_array = cv2.imread(filepath, cv2.IMREAD_COLOR)  
            new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))  
            return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 3)   

        prediction =      model.predict_classes([prepare('/home/vishu/Desktop/basics/dataset/d2.jpeg')])  

        print(prediction)  

使用它后它总是给我输出 4
我应该如何预测正确的图像类别?
在这里,我从文件夹中获取输入图像 我为 5 个类别创建了 5 个文件夹,那么我应该如何预测图像的类别?

【问题讨论】:

  • 您忘记进行重新缩放(除以 255)。
  • 很可能预测数字4表示第5类(第5文件夹)。
  • 但它也显示了其他文件夹中图像的相同输出
  • @li.SQ 完全错误,模型是用重新缩放的数据训练的,没有重新缩放的测试数据,你正在使神经元饱和,这会产生具有恒定类的输出。

标签: python keras deep-learning prediction multiclass-classification


【解决方案1】:

您忘记在 ImageDataGenerator 中进行重新缩放(除以 255),这需要使用新的测试数据来完成,因此您必须在 prepare 函数中执行此操作。

【讨论】:

  • cv2.resize(img_array, (IMG_SIZE,IMG_SIZE)).astype('float32')/255 我已经在我的准备函数中添加了这个,但它仍然是 showu=ng 错误输出
猜你喜欢
  • 2017-07-07
  • 1970-01-01
  • 1970-01-01
  • 2019-12-30
  • 2017-07-18
  • 1970-01-01
  • 2019-05-17
  • 2018-04-15
  • 1970-01-01
相关资源
最近更新 更多