Я пытаюсь сделать классификатор для моих данных.Но, как новичок в машинном обучении и Python, я получаю странную ошибку, которую не могу понять.Мой код
КОД
from sklearn.preprocessing import OneHotEncoder
import tensorflow as tf
import numpy as np
import scipy.io as cio
import os
import matplotlib.pyplot as plt
import matplotlib.image as mpg
from random import shuffle
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
import cv2
a = cio.loadmat("D:/compCarsThesisData/data/misc/make_model_name.mat")
images = "D:/compCarsThesisData/data/image/"
IMG_SIZE = 64
MODEL_NAME = 'Classification'
LR = 1e-3
b = a['make_names']
d = []
for i in range(b.size):
d.append(b[i][0][0])
print(d)
labels_dic = {v: k for v, k in enumerate(d)}
print(labels_dic)
indices = np.arange(163)
depth = 163
y = tf.one_hot(indices,depth)
sess = tf.Session()
result = sess.run(y)#,feed_dict=None,options=None, run_metadata=labels_dic)
print(result)
labels = []
labels.append((result,labels_dic))
print(labels)
for root, _, files in os.walk(images):
cdp = os.path.abspath(root)
for f in files:
name,ext = os.path.splitext(f)
if ext == ".jpg":
cip = os.path.join(cdp,f)
ci = mpg.imread(cip)
image = cv2.cv2.resize(ci,(IMG_SIZE,IMG_SIZE))
image = np.array(image)
print(image)
training_data = []
training_data.append((image,labels))
shuffle(training_data)
np.save('training_data_make_model', training_data)
testing_data = []
testing_data.append((image,labels))
print("TestingDATA",testing_data)
shuffle(testing_data)
# if the data already created
# training_data = np.load('training_data_make_model.npy')
# testing_data = np.load('training_data_make_model.npy')
train = training_data[:-50000]
test = testing_data[-50000:]
X_train = np.array([i[0] for i in train]).reshape(-1, IMG_SIZE, IMG_SIZE, 3)
y_train = [i[1] for i in train]
X_test = np.array([i[0] for i in test]).reshape(-1, IMG_SIZE, IMG_SIZE, 3)
y_test = [i[1] for i in test]
print("YTEST",y_test)
tf.reset_default_graph()
convnet = input_data(shape=[None,IMG_SIZE,IMG_SIZE,3],name='input')
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 2, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')
model = tflearn.DNN(convnet, tensorboard_dir='log', tensorboard_verbose=0)
model.fit({'input': X_train}, {'targets': y_train}, n_epoch=10,
validation_set=0.1,
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
Моментальный снимок моего файла .mat:
.В файле mat содержатся массивы ячеек, так как на данный момент я использую ['make_names'], которые имеют названия автомобилей в качестве меток, преобразовывают их в один код в горячем виде и добавляют их вместе с данными обучения, как вы можете видеть выше.
.mat file
Прилагаемые данные с метками и .mat-файлами выглядят следующим образом
ЭТИКЕТКИ С ОДНИМ ГОРЯЧИМ ЭНКОДОМ
[[1. 0. 0. ... 0. 0. 0.]
[0. 1. 0. ... 0. 0. 0.]
[0. 0. 1. ... 0. 0. 0.]
...
[0. 0. 0. ... 1. 0. 0.]
[0. 0. 0. ... 0. 1. 0.]
[0. 0. 0. ... 0. 0. 1.]]
[(array([[1., 0., 0., ..., 0., 0., 0.],
[0., 1., 0., ..., 0., 0., 0.],
[0., 0., 1., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 1., 0., 0.],
[0., 0., 0., ..., 0., 1., 0.],
[0., 0., 0., ..., 0., 0., 1.]], dtype=float32), {0: 'ABT', 1: 'BAC', 2: 'Conquest', 3: 'DS', 4: 'Dacia', 5: 'Fisker', 6: 'GMC', 7: 'Gumpert', 8: 'Hennessey', 9: 'Icona', 10: 'Jeep', 11: 'KTM', 12: 'MELKUS', 13: 'MG', 14: 'MINI', 15: 'Mazzanti', 16: 'Noble', 17: 'PGO', 18: 'SPIRRA', 19: 'SSC', 20: 'Scion', 21: 'TESLA', 22: 'TVR', 23: 'Tramontana', 24: 'Zenvo', 25: 'smart', 26: 'Yiqi', 27: 'Mitsubishi', 28: 'Shangqidatong', 29: 'Spyker N.V.', 30: 'Dongnan', 31: 'Dongfeng', 32: 'Dongfengxiaokang', 33: 'Dongfengfengdu', 34: 'Dongfengfengshen', 35: 'Dongfengfengxing', 36: 'Zxauto', 37: 'Zhonghua', 38: 'Toyota', 39: 'Zinoro', 40: 'Jiulong', 41: 'Isuzu', 42: 'Wuling', 43: 'AC Chnitzer', 44: 'Zoyte', 45: 'Iveco', 46: 'Bufori', 47: 'Porsche', 48: 'Mitsuoka', 49: 'Chrysler', 50: 'Lamorghini ', 51: 'Kombat', 52: 'Cadillac', 53: 'Buck', 54: 'Lifan', 55: 'Lorinser', 56: 'Rolls-Royce', 57: 'BAW', 58: 'Baihc', 59: 'Beiqiweiwang', 60: 'Beiqihuansu', 61: 'Beiqi New Energy', 62: 'Huapu', 63: 'Huatai', 64: 'Huaqi', 65: 'Carlsson', 66: 'Shuanghuan', 67: 'Shuanglong', 68: 'Geely', 69: 'Venucia', 70: 'Haval', 71: 'Hafei', 72: 'Volkswagen', 73: 'Daihatsu', 74: 'Chrey', 75: 'Besturn', 76: 'Benz', 77: 'Audi', 78: 'Wisemann', 79: 'Wealeak', 80: 'BWM', 81: 'Baojun', 82: 'Bentley', 83: 'Brabus', 84: 'Bugatti', 85: 'Pagani', 86: 'Guangqichuanqi', 87: 'GAC', 88: 'Karry', 89: 'Ciimo', 90: 'CHTC', 91: 'Jaguar', 92: 'Morgan', 93: 'Subaru', 94: 'Skoda', 95: 'Xinkai', 96: 'Nissan', 97: 'Changhe', 98: 'RANZ', 99: 'Honda', 100: 'Lincoln', 101: 'Peugeot', 102: 'Opel', 103: 'Oley', 104: 'BYD', 105: 'Jonway', 106: 'Huizhong', 107: 'Jianghuai', 108: 'Jiangling', 109: 'Vauxhall', 110: 'Volvo', 111: 'Ferrari', 112: 'Haige', 113: 'Haima', 114: 'Haima(Zhengzhou)', 115: 'Cheetah', 116: 'Maserati', 117: 'Hyundai ', 118: 'Everus', 119:
'Ruiqi', 120: 'Fuqiqiteng', 121: 'Ford', 122: 'Futian', 123: 'Fudi', 124: 'Koenigsegg', 125: 'HongQi', 126: 'Luxgen', 127: 'SAAB', 128: 'Denza', 129: 'Yingzhi', 130: 'Infiniti', 131: 'Roewe', 132: 'Lotus', 133: 'FIAT', 134: 'Saab', 135: 'Lancia', 136: 'Seat', 137: 'Qoros', 138: 'Acura', 139: 'KIA', 140: 'Lotus', 141: 'LAND-ROVER', 142: 'McLaren', 143: 'Maybach', 144: 'Dodge', 145: 'Mustang', 146: 'Jinlv', 147: 'Jinbei', 148: 'Suzuki', 149: 'GreatWall', 150: 'Changan Business', 151: 'Changan', 152: 'Alfa Romeo', 153: 'Aston Martin', 154: 'Lufeng', 155: 'Shanqitongjia', 156: 'Chevy', 157: 'Citroen', 158: 'Lexus', 159: 'Renault', 160: 'Shouwang', 161: 'MAZDA', 162: 'Huanghai'})]
Но каждый раз, когда я пытаюсь запустить это, я получаю ошибку вроде.
ОШИБКА
Run id: Classification
Log directory: log/
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\zeele\Miniconda3\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\zeele\Miniconda3\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs) 1, in fill_batch_ids_queue
File "C:\Users\zeele\Miniconda3\lib\site-packages\tflearn\data_flow.py", line 201, in fill_batch_ids_queue 5, in next_batch_ids
ids = self.next_batch_ids()
File "C:\Users\zeele\Miniconda3\lib\site-packages\tflearn\data_flow.py", line 215, in next_batch_ids
batch_start, batch_end = self.batches[self.batch_index]
IndexError: list index out of range