【问题标题】:How to find out misclassified images filenames如何找出错误分类的图像文件名
【发布时间】:2020-01-05 08:19:02
【问题描述】:

我正在构建一个模型来检测太阳能电池板的缺陷。我正在使用 255*255 的图像来训练模型,我想使用混淆矩阵来改进我的模型。

矩阵给了我错误分类的图像,但我需要找出误报和误报图像的确切文件名

我怎样才能实现这个目标?

我在下面提供了我的代码:

import numpy as np
import os
import time
import keras
from keras.applications.resnet50 import  preprocess_input
from keras.preprocessing import image
from keras.layers import GlobalAveragePooling2D, Dense, Dropout
#from keras.layers import GlobalAveragePooling2D, Dense, Dropout,Activation,Flatten
#from keras.layers import Input
from keras.models import Model
from keras.utils import np_utils
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
from keras.layers import Input
from keras import models
from keras import layers
from keras import optimizers
from keras_applications.resnet import ResNet101
from keras.optimizers import SGD, Adagrad, Adadelta, RMSprop, Adam
from keras.callbacks import LearningRateScheduler
from keras.models import load_model
from keras import regularizers
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img

###########################################################################################################################
## Model Initials

IMAGE_SIZE = (255, 255)


BATCH_SIZE = 24

NUM_EPOCHS = 1

WEIGHTS_FINAL = 'defectdetection.hdf5'
MODEL_FINAL = 'defectdetection.h5'
BEST_WEIGHT ='1defectdetection.hdf5'

##############################################################################################
## Loading dataset for the training process
## Define data path
# Loading the training data


img_path = 'C:/Users/TeamSoloMid/SolarCellsImages/dataset/Sample0001.jpg'
img = image.load_img(img_path, target_size=(255, 255))
x = image.img_to_array(img)
print (x.shape)
x = np.expand_dims(x, axis=0)
print (x.shape)
x = preprocess_input(x)
print('Input image shape:', x.shape)

PATH = os.getcwd()


data_path = 'C:/Users/TeamSoloMid/SolarCellsImages'
data_dir_list = os.listdir(data_path)


img_data_list=[]
for dataset in data_dir_list:
    img_list=os.listdir(data_path+'/'+ dataset)
    print ('Loaded the images of dataset-'+'{}\n'.format(dataset))
    for img in img_list:
        img_path = data_path + '/'+ dataset + '/'+ img 
        img = image.load_img(img_path, target_size=(255,255))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        #print('Input image shape:', x.shape)
        img_data_list.append(x)

img_data = np.array(img_data_list)
print (img_data.shape)
img_data=np.rollaxis(img_data,1,0)
print (img_data.shape)
img_data=img_data[0]
print (img_data.shape)

#t=time.time()
# Define the number of classes
num_classes = 2
num_of_samples = img_data.shape[0]
labels = np.ones((num_of_samples,),dtype='int64')

labels[0:1603]=0
labels[1604:3225]=1




names = ['Defect', 'Almost']

Y = np_utils.to_categorical(labels, num_classes)

#Shuffle the dataset
x,y = shuffle(img_data,Y, random_state=2)
# Split the dataset

TestPcnt = 0.2
X_train, X_test, y_train, y_test = train_test_split(x, y,
                                                    test_size=TestPcnt,
                                                    random_state=2)


epoch=NUM_EPOCHS

###############################################################################################
# Fine tune the resnet 101
image_input = Input(shape=(255, 255, 3))
model = ResNet101(include_top=False,
                        input_tensor=image_input,
                        weights='imagenet',  
                        backend=keras.backend,
                        layers=keras.layers,
                        models=keras.models,
                        utils=keras.utils)


# Freeze all the layers
for layer in model.layers[:-3]:
    layer.trainable = False

#model.summary()

last_layer = model.output
# add a global spatial average pooling layer
x = GlobalAveragePooling2D()(last_layer)
x = Dense(256, activation='relu',name='fc-1')(x)
x = Dropout(0.5)(x)
out = Dense(num_classes, activation='softmax',name='output_layer')(x)

# this is the model we will train
net_model = Model(inputs=model.input, outputs=out)

net_model.summary()

for layer in net_model.layers[:-5]:
    layer.trainable = False

net_model.summary()

for layer in net_model.layers:
    print(layer, layer.trainable)

#my_opti= optimizers.Adam(lr=0.00002)
#my_opti= optimizers.Adam(lr=0.00001)

################################################################################################
#Define learning Rate
learning_rate = 0.00002
decay_rate = learning_rate / epoch
momentum = 0.9
sgd = SGD(lr=learning_rate, momentum=momentum,
                    decay=decay_rate,
                     nesterov=False)

##############################################################################
## we will keep the weights of the epoch that scores highest in terms of accuracy on the test set.
from keras.callbacks import ModelCheckpoint
checkpointer = ModelCheckpoint(filepath=BEST_WEIGHT,
                                monitor = 'val_acc',
                                verbose=1, 
                                save_best_only=True,
                                mode = 'max')
###################################################################

callback_list = [checkpointer]

net_model.compile(loss='categorical_crossentropy',
                  optimizer=sgd,
                  metrics=['accuracy'])

t=time.time()
hist = net_model.fit(X_train, y_train, batch_size=BATCH_SIZE,
                     epochs=NUM_EPOCHS, verbose=1,
                     callbacks = [checkpointer],
                     validation_data=(X_test, y_test))
print('Training time: %s' % (time.time()-1))
(loss, accuracy) = net_model.evaluate(X_test, y_test,
                                      batch_size=BATCH_SIZE,
                                      verbose=1)

print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss,accuracy * 100))
############################################################################################
## Saving The weights of the model after training
net_model.save_weights(WEIGHTS_FINAL)
print('1. Weights Saved')
net_model.save_weights(BEST_WEIGHT)
print('2. Best Weights Saved')
##############################################################################
## Saving The Complete model after training
net_model.save(MODEL_FINAL)
print('3. Model Saved')

############################################################################################
import matplotlib.pyplot as plt
# visualizing losses and accuracy
train_loss=hist.history['loss']
val_loss=hist.history['val_loss']
train_acc=hist.history['acc']
val_acc=hist.history['val_acc']
xc=range(NUM_EPOCHS)

plt.figure(1,figsize=(7,5))
plt.plot(xc,train_loss)
plt.plot(xc,val_loss)
plt.xlabel('num of Epochs')
plt.ylabel('loss')
plt.title('train_loss vs val_loss')
plt.grid(True)
plt.legend(['train','val'])
#print plt.style.available # use bmh, classic,ggplot for big pictures
plt.style.use(['classic'])

plt.figure(2,figsize=(7,5))
plt.plot(xc,train_acc)
plt.plot(xc,val_acc)
plt.xlabel('num of Epochs')
plt.ylabel('accuracy')
plt.title('train_acc vs val_acc')
plt.grid(True)
plt.legend(['train','val'],loc=4)
#print plt.style.available # use bmh, classic,ggplot for big pictures
plt.style.use(['classic'])

############################################################################
from sklearn.metrics import confusion_matrix, classification_report
import itertools
from sklearn.utils.multiclass import unique_labels
from sklearn import metrics
import seaborn as sns
import pandas as pd
from sklearn.datasets import load_files
from sklearn.svm import LinearSVC
from sklearn import svm

LABELS= ['Defect', 'Almost']

# Print confusion matrix for training data
y_pred_train = net_model.predict(X_train)


def show_confusion_matrix(validations, predictions):

    matrix = metrics.confusion_matrix(validations, predictions)
    plt.figure(figsize=(10, 10))
    sns.heatmap(matrix,
                cmap='coolwarm',
                linecolor='white',
                linewidths=1,
                xticklabels=LABELS,
                yticklabels=LABELS,
                annot=True,
                fmt='d')
    plt.title('Confusion Matrix')
    plt.ylabel('True Label')
    plt.xlabel('Predicted Label')
    plt.show()

y_pred_test = net_model.predict(X_test)
# Take the class with the highest probability from the test predictions
max_y_pred_test = np.argmax(y_pred_test, axis=1)
max_y_test = np.argmax(y_test, axis=1)

show_confusion_matrix(max_y_test, max_y_pred_test)

print(classification_report(max_y_test, max_y_pred_test))

【问题讨论】:

    标签: python


    【解决方案1】:

    我会计算一个校验和(即 md5)并将其用作将图像路径保持为值的字典键,即

    import hashlib
    ...
    image_paths = {}
    ...
    for dataset in data_dir_list:
        img_list=os.listdir(data_path+'/'+ dataset)
        print ('Loaded the images of dataset-'+'{}\n'.format(dataset))
        for img in img_list:
            img_path = data_path + '/'+ dataset + '/'+ img 
            img = image.load_img(img_path, target_size=(255,255))
            x = image.img_to_array(img)
            x = np.expand_dims(x, axis=0)
            x = preprocess_input(x)
            #print('Input image shape:', x.shape)
            img_data_list.append(x)
    
    
            # in this example i am only hashing the first 5 pixels of the image. You would probably want to use all of the pixels of the image.
            image_hash = hashlib.md5(str(x[0]) + str(x[1]) + str(x[2]) + str(x[3]) + str(x[4])).hexdigest()
            image_paths[image_hash] = img_path
    
            ...
    

    当您想要解码图像路径时,您只需再次计算哈希并在字典中查找路径

    image_hash = hashlib.md5(str(x[0]) + str(x[1]) + str(x[2]) + str(x[3]) + str(x[4])).hexdigest()
    image_path = image_paths[image_hash]
    

    虽然这不是最灵活的方法,但我相信它仍然可以帮助您实现目标。

    请注意,如果您有很多图像,散列可能会非常昂贵,但如果您的图像没有改变,您只需对它们进行一次散列并保存在某个地方。在随后的尝试中,您只需要加载数据而不是再次对所有内容进行哈希处理

    【讨论】:

      【解决方案2】:

      感谢您的回答,我已经尝试过您的解决方案,并且出现 IndexError 如下:

      File "C:/Users/TeamSoloMid/Solar cells Defect Detection.py", line 102, in <module>
          image_hash = hashlib.md5(str(x[0])+str(x[1])+str(x[2])+str(x[3])+str(x[4])).hexdigest()
      
      IndexError: index 1 is out of bounds for axis 0 with size 1
      

      下面是我添加解决方案的代码

      import numpy as np
      import os
      import time
      import keras
      from keras.applications.resnet50 import  preprocess_input
      from keras.preprocessing import image
      from keras.layers import GlobalAveragePooling2D, Dense, Dropout
      #from keras.layers import GlobalAveragePooling2D, Dense, Dropout,Activation,Flatten
      #from keras.layers import Input
      from keras.models import Model
      from keras.utils import np_utils
      from sklearn.utils import shuffle
      from sklearn.model_selection import train_test_split
      from keras.layers import Input
      from keras import models
      from keras import layers
      from keras import optimizers
      from keras_applications.resnet import ResNet101
      from keras.optimizers import SGD, Adagrad, Adadelta, RMSprop, Adam
      from keras.callbacks import LearningRateScheduler
      from keras.models import load_model
      from keras import regularizers
      from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
      import hashlib
      
      ########################################################################################
      ## Model Initials
      
      IMAGE_SIZE = (255, 255)
      
      
      BATCH_SIZE = 24
      
      NUM_EPOCHS = 1
      
      WEIGHTS_FINAL = 'defectdetection.hdf5'
      MODEL_FINAL = 'defectdetection.h5'
      BEST_WEIGHT ='defectdetection.hdf5'
      
      #########################################################################################
      ## Loading dataset for the training process
      ## Define data path
      # Loading the training data
      
      
      img_path = 'C:/Users/TeamSoloMid/SolarCellsImages/dataset/Sample0001.jpg'
      img = image.load_img(img_path, target_size=(255, 255))
      x = image.img_to_array(img)
      print (x.shape)
      x = np.expand_dims(x, axis=0)
      print (x.shape)
      x = preprocess_input(x)
      print('Input image shape:', x.shape)
      
      PATH = os.getcwd()
      
      
      
      data_path = 'C:/Users/TeamSoloMid/SolarCellsImages'
      data_dir_list = os.listdir(data_path)
      
      
      image_paths = {}   
      img_data_list=[]
      for dataset in data_dir_list:
          img_list=os.listdir(data_path+'/'+ dataset)
          print ('Loaded the images of dataset-'+'{}\n'.format(dataset))
          for img in img_list:
              img_path = data_path + '/'+ dataset + '/'+ img 
              img = image.load_img(img_path, target_size=(255,255))
              x = image.img_to_array(img)
              x = np.expand_dims(x, axis=0)
              x = preprocess_input(x)
              #print('Input image shape:', x.shape)
              img_data_list.append(x)
      image_hash = hashlib.md5(str(x[0])+str(x[1])+str(x[2])+str(x[3])+str(x[4])).hexdigest()
      image_paths[image_hash] = img_path
      
      
      img_data = np.array(img_data_list)
      print (img_data.shape)
      img_data=np.rollaxis(img_data,1,0)
      print (img_data.shape)
      img_data=img_data[0]
      print (img_data.shape)
      
      #t=time.time()
      # Define the number of classes
      num_classes = 2
      num_of_samples = img_data.shape[0]
      labels = np.ones((num_of_samples,),dtype='int64')
      
      labels[0:1603]=0
      labels[1604:3225]=1
      #labels[3226:4847]=2
      
      
      
      
      names = ['Defect', 'Almost']
      
      # convert class labels to on-hot encoding
      Y = np_utils.to_categorical(labels, num_classes)
      
      #Shuffle the dataset
      x,y = shuffle(img_data,Y, random_state=2)
      # Split the dataset
      
      TestPcnt = 0.2
      X_train, X_test, y_train, y_test = train_test_split(x, y,
                                                          test_size=TestPcnt,
                                                          random_state=2)
      
      
      epoch=NUM_EPOCHS
      
      ########################################################################################
      # Fine tune the resnet 101
      image_input = Input(shape=(255, 255, 3))
      model = ResNet101(include_top=False,
                              input_tensor=image_input,
                              weights='imagenet',  
                              backend=keras.backend,
                              layers=keras.layers,
                              models=keras.models,
                              utils=keras.utils)
      
      
      # Freeze all the layers
      for layer in model.layers[:-3]:
          layer.trainable = False
      
      #model.summary()
      
      last_layer = model.output
      # add a global spatial average pooling layer
      x = GlobalAveragePooling2D()(last_layer)
      x = Dense(256, activation='relu',name='fc-1')(x)
      x = Dropout(0.5)(x)
      out = Dense(num_classes, activation='softmax',name='output_layer')(x)
      
      # this is the model we will train
      net_model = Model(inputs=model.input, outputs=out)
      
      net_model.summary()
      
      for layer in net_model.layers[:-5]:
          layer.trainable = False
      
      net_model.summary()
      
      for layer in net_model.layers:
          print(layer, layer.trainable)
      
      #my_opti= optimizers.Adam(lr=0.00002)
      #my_opti= optimizers.Adam(lr=0.00001)
      
      #########################################################################################
      Define learning Rate
      learning_rate = 0.00002
      decay_rate = learning_rate / epoch
      momentum = 0.9
      sgd = SGD(lr=learning_rate, momentum=momentum,
                          decay=decay_rate,
                           nesterov=False)
      
      ##############################################################################
      ## we will keep the weights of the epoch that scores highest in terms of accuracy on the test set.
      from keras.callbacks import ModelCheckpoint
      checkpointer = ModelCheckpoint(filepath=BEST_WEIGHT,
                                      monitor = 'val_acc',
                                      verbose=1, 
                                      save_best_only=True,
                                      mode = 'max')
      ###################################################################
      
      callback_list = [checkpointer]
      
      net_model.compile(loss='categorical_crossentropy',
                        optimizer=sgd,
                        metrics=['accuracy'])
      
      t=time.time()
      hist = net_model.fit(X_train, y_train, batch_size=BATCH_SIZE,
                           epochs=NUM_EPOCHS, verbose=1,
                           callbacks = [checkpointer],
                           validation_data=(X_test, y_test))
      print('Training time: %s' % (time.time()-1))
      (loss, accuracy) = net_model.evaluate(X_test, y_test,
                                            batch_size=BATCH_SIZE,
                                            verbose=1)
      
      print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss,accuracy * 100))
      ############################################################################################
      ## Saving The weights of the model after training
      net_model.save_weights(WEIGHTS_FINAL)
      print('1. Weights Saved')
      net_model.save_weights(BEST_WEIGHT)
      print('2. Best Weights Saved')
      ##############################################################################
      ## Saving The Complete model after training
      net_model.save(MODEL_FINAL)
      print('3. Model Saved')
      
      ############################################################################################
      import matplotlib.pyplot as plt
      # visualizing losses and accuracy
      train_loss=hist.history['loss']
      val_loss=hist.history['val_loss']
      train_acc=hist.history['acc']
      val_acc=hist.history['val_acc']
      xc=range(NUM_EPOCHS)
      
      plt.figure(1,figsize=(7,5))
      plt.plot(xc,train_loss)
      plt.plot(xc,val_loss)
      plt.xlabel('num of Epochs')
      plt.ylabel('loss')
      plt.title('train_loss vs val_loss')
      plt.grid(True)
      plt.legend(['train','val'])
      #print plt.style.available # use bmh, classic,ggplot for big pictures
      plt.style.use(['classic'])
      
      plt.figure(2,figsize=(7,5))
      plt.plot(xc,train_acc)
      plt.plot(xc,val_acc)
      plt.xlabel('num of Epochs')
      plt.ylabel('accuracy')
      plt.title('train_acc vs val_acc')
      plt.grid(True)
      plt.legend(['train','val'],loc=4)
      #print plt.style.available # use bmh, classic,ggplot for big pictures
      plt.style.use(['classic'])
      
      ############################################################################
      from sklearn.metrics import confusion_matrix, classification_report
      import itertools
      from sklearn.utils.multiclass import unique_labels
      from sklearn import metrics
      import seaborn as sns
      import pandas as pd
      from sklearn.datasets import load_files
      from sklearn.svm import LinearSVC
      from sklearn import svm
      
      LABELS= ['Defect', 'Almost']
      
      # Print confusion matrix for training data
      y_pred_train = net_model.predict(X_train)
      
      
      def show_confusion_matrix(validations, predictions):
      
          matrix = metrics.confusion_matrix(validations, predictions)
          plt.figure(figsize=(10, 10))
          sns.heatmap(matrix,
                      cmap='coolwarm',
                      linecolor='white',
                      linewidths=1,
                      xticklabels=LABELS,
                      yticklabels=LABELS,
                      annot=True,
                      fmt='d')
          plt.title('Confusion Matrix')
          plt.ylabel('True Label')
          plt.xlabel('Predicted Label')
          plt.show()
      
      y_pred_test = net_model.predict(X_test)
      # Take the class with the highest probability from the test predictions
      max_y_pred_test = np.argmax(y_pred_test, axis=1)
      max_y_test = np.argmax(y_test, axis=1)
      
      show_confusion_matrix(max_y_test, max_y_pred_test)
      
      print(classification_report(max_y_test, max_y_pred_test))
      
      image_hash = hashlib.md5(str(x[0]) + str(x[1]) + str(x[2]) + str(x[3]) + str(x[4])).hexdigest()
      image_path = image_paths[image_hash]
      

      【讨论】:

        猜你喜欢
        • 2016-08-16
        • 2017-03-02
        • 1970-01-01
        • 2020-04-16
        • 1970-01-01
        • 2019-09-23
        • 2020-03-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多