Keras机器学习(IndexError:列表索引超出范围)

时间:2020-06-20 03:01:08

标签: python tensorflow machine-learning keras one-hot-encoding

我正在创建一个程序,该程序提供形状的训练图像,然后提供形状的测试数据集合,以查看机器学习的准确性。我的问题来自程序的训练部分,在该部分中我的代码不断出现索引错误:

import cv2                 # working with, mainly resizing, images
import numpy as np         # dealing with arrays
import os                  # dealing with directories
from random import shuffle # mixing up or currently ordered data that might lead our network astray in training.
from tqdm import tqdm      

TRAIN_DIR = '/Users/a/desktop/KerasShapes'
TEST_DIR = '/Users/a/desktop/test shapes'
IMG_SIZE = 50
LR = 1e-3
MODEL_NAME = 'square/triangle/-{}-{}.model'.format(LR, '3conv-basic') # just so we remember which saved model is which, sizes must match

def label_img(img):

word_label = img.split('.')[-2]
# conversion to one-hot array [square,triangle,star,optagon,heptagon,circle]
#                            
if word_label == 'square': return ([1,0,0,0,0,0])
#                            
elif word_label == 'triangle': return ([0,1,0,0,0,0])
#
elif word_label == 'star': return ([0,0,1,0,0,0])
#
elif word_label ==  'optagon': return ([0,0,0,1,0,0])
#
elif word_label ==  'heptagon': return ([0,0,0,0,1,0])
#
`elif` word_label ==  'circle': return ([0,0,0,0,0,1])



    def create_train_data():
        training_data = []
        for img in tqdm(os.listdir(TRAIN_DIR)):
            label = label_img(img)
            path = os.path.join(TRAIN_DIR,img)
            img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
            img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
            training_data.append([np.array(img),np.array(label)])
        shuffle(training_data)
        np.save('train_data.npy', training_data)
        return training_data
    def process_test_data():
        testing_data = []
        for img in tqdm(os.listdir(TEST_DIR)):
            path = os.path.join(TEST_DIR,img)
            img_num = img.split('.')[0]
            img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
            img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
            testing_data.append([np.array(img), img_num])

        shuffle(testing_data)
        np.save('test_data.npy', testing_data)
        return testing_data
    train_data = create_train_data()

调试器信息 当我运行代码时,我得到IndexError:在调试器中列出索引超出范围。

P.S预先感谢您能够为我提供的全新的python帮助。

0 个答案:

没有答案