首页 > 解决方案 > 我不明白机器学习python中的空数据集是什么意思

问题描述

import numpy as np
np.random.seed(10)
import matplotlib.pyplot as plt
from keras.models import load_model
import glob,cv2

def show_images_labels_predictions(images,labels,
                                  predictions,start_id,num=10):
    plt.gcf().set_size_inches(12, 14)
    if num>25: num=25 
    for i in range(0, num):
        ax=plt.subplot(5,5, 1+i)
       
        ax.imshow(images[start_id])
        
        
        if( len(predictions) > 0 ) :
            title = 'ai = ' + str(predictions[start_id])
           
            title += (' (o)' if predictions[start_id]==labels[start_id] else ' (x)') 
            title += '\nlabel = ' + str(labels[start_id])
      
        else :
            title = 'label = ' + str(labels[start_id])
            
         
        ax.set_title(title,fontsize=12) 
        ax.set_xticks([]);ax.set_yticks([])        
        start_id+=1 
    plt.show()
    
   
files = glob.glob("imagedata\*.jpg" )
test_feature=[]
test_label=[]
dict_labels = {"Cat":0, "Dog":1}
size = (80,80) 
for file in files:
    img=cv2.imread(file) 
    img = cv2.resize(img, dsize=size)       
    test_feature.append(img)
    label=file[10:13] 
    test_label.append(dict_labels[label])
   
test_feature=np.array(test_feature) 
test_label=np.array(test_label)     


test_feature_vector =test_feature.reshape(len(test_feature), 40,40,3).astype('float32')


test_feature_normalize = test_feature_vector/255

  

model = load_model('Pet_cnn_model.h5')
        

prediction = model.predict(test_feature_normalize)
prediction = np.argmax(prediction,axis=1)
    
   
show_images_labels_predictions(test_feature,test_label,prediction,0,len(test_feature))

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_14064/1284467577.py in <module>
     58 
     59     #預測
---> 60 prediction = model.predict(test_feature_normalize)
     61 prediction = np.argmax(prediction,axis=1)
     62 

~\anaconda3\lib\site-packages\keras\engine\training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
   1764             callbacks.on_predict_batch_end(end_step, {'outputs': batch_outputs})
   1765       if batch_outputs is None:
-> 1766         raise ValueError('Expect x to be a non-empty array or dataset.')
   1767       callbacks.on_predict_end()
   1768     all_outputs = tf.__internal__.nest.map_structure_up_to(batch_outputs, concat, outputs)

ValueError: Expect x to be a non-empty array or dataset.

对于这个程序,任何人都可以解释该行中发生了什么问题prediction = model.predict(test_feature_normalize)吗?由于我是在 Python 中学习机器学习的初学者,并且我不明白Expect x to be a non-empty array or dataset.错误消息中的含义,因此我们将不胜感激。但是,对于其他图像预测程序,我可以运行结果并显示哪些图像有预测错误,以便任何人都可以帮助我找出问题所在?

标签: pythonmachine-learningkerasdataset

解决方案


推荐阅读