首页 > 解决方案 > 我面临一个空的training_Data

问题描述

所以我尝试了这个教程(11:58 分钟),试图在我的 CNN 上实现,它由数据集上的 10 个物种组成。

我加载数据没有错误

DATADIR = "dataset"
CATEGORIES = ["Dendrobium_crumenatum","Grammatophyllum_speciosum", "Coelogyne_swaniana", 
              "Bulbophyllum_purpurascens", "Agrostophyllum_stipulatum", 
              "Spathoglottis_plicata", "Phalaenopsis_amabilis", "Nabaluia_angustifolia", 
              "Habenaria_rhodocheila_hance"]

 here the example of the output
[![enter image description here][2]][2]

那么下一节是

training_data = []
def create_training_data():
    for category in CATEGORIES:
        path = os.path.join(DATADIR,category)
        class_num = CATEGORIES.index(category)
        for img in os.listdir(path):
            try:
                img_array = cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
                new_array = cv2.resize(img_array, (IMG_SIZE,IMG_SIZE))
                training_data.append([new,array, class_num])
            except Exception as e:
                pass
create_training_data()        

但是当我打印prin(len(training_data))

我把这个作为输出

0

当我尝试

import random
random.shuffle(training_data)
for sample in training_data[:10]:
    print (sample[1])

它没有显示输出。这是否意味着,我的训练数据是空的?还是因为正在使用的类别索引?因为我使用的是 10 课,而在教程中使用的是 2 课。

标签: tensorflowmachine-learningkeras

解决方案


使您的 training_data 全球化

training_data =[]
def create_training_data():
    global training_data
    for category in CATEGORIES:
        path = os.path.join(DATADIR,category)
        class_num = CATEGORIES.index(category)
        for img in os.listdir(path):
            try:
                img_array = cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
                new_array = cv2.resize(img_array,(IMG_SIZE, IMG_SIZE))
                training_data.append([new_array,class_num])
            except Exception as e:
                pass
create_training_data()

推荐阅读