ValueError:输入数组应具有与目标数组相同数量的样本。找到5100个输入样本和1700个目标样本

时间:2019-07-24 09:52:13

标签: tensorflow artificial-intelligence

我有5100张图片用于训练,处理时显示错误:ValueError:输入数组的样本数应与目标数组相同。找到了5100个输入样本和1700个目标样本。

我正在加载数据并使用'pickle'保存

'''python 导入操作系统 导入cv2 将numpy导入为np 随机导入 进口泡菜

Data_preprocessing类

'Data preprocessing before goes to ML'

# Train by data list initilization
training_data = []

def __init__(self, datadir, categories, img_size):
    Data_preprocessing.img_size = img_size
    Data_preprocessing.datadir = datadir
    Data_preprocessing.categories = categories




def Create_training_data(self):

    for category in Data_preprocessing.categories:
        # path to cats or dogs dir
        path = os.path.join(Data_preprocessing.datadir, category)
        class_num = Data_preprocessing.categories.index(category)
        # After having the directory for images
        # Started to read image by using OpenCv and directly convert it to GRAYSCALE
        for img in os.listdir(path):
            try:
                img_read = cv2.imread(os.path.join(path, img))
                new_array = cv2.resize(img_read, (Data_preprocessing.img_size, Data_preprocessing.img_size))
                Data_preprocessing.training_data.append([new_array, class_num])
            except Exception as e:
                pass

    self.Saving_processed_data()

def Saving_processed_data(self):

    random.shuffle(Data_preprocessing.training_data)
    x = []
    y = []
    for features, label in Data_preprocessing.training_data:
        x.append(features)
        y.append(label)


    x = np.array(x).reshape(-1, Data_preprocessing.img_size, Data_preprocessing.img_size, 1)
    # Saving data by using "pickle"
    pickle_out = open("x.pickle", "wb")
    pickle.dump(x, pickle_out)
    pickle_out.close()

    pickle_out = open("y.pickle", "wb")
    pickle.dump(y, pickle_out)
    pickle_out.close()

类别= [“ A”,“ B”,“ C”,“ D”,“ E”,“ F”,“ G”,“ H”,“ I”,“ K”,“ L”, “ M”,“ N”,“ O”,“ P”,“ Q”,“ R”,“ S”,“ T”,“ U”,“ V”,“ W”,“ X”,“ Y “,” Z“] data_preprocessing = Data_preprocessing(“ ADSL_training”,类别50) data_preprocessing.Create_training_data()'''

培训模式

'''python 导入cv2 将tensorflow作为tf导入 从tensorflow.keras.models导入顺序 从tensorflow.keras.layers导入Dense,Dropout,Activation,Flatten,Conv2D,MaxPooling2D 进口泡菜

learning_model类:

pass


def TrainModel(self):

    self.x = pickle.load(open("x.pickle", "rb"))
    self.y = pickle.load(open("y.pickle", "rb"))

    self.x = self.x/255.0


    model = Sequential()

    model.add(Conv2D(128, (3, 3), input_shape = self.x.shape[1:]))
    model.add(Activation("relu"))
    model.add(MaxPooling2D(pool_size=(2, 2)))

    model.add(Conv2D(128, (3, 3)))
    model.add(Activation("relu"))
    model.add(MaxPooling2D(pool_size=(2, 2)))


    model.add(Flatten())
    model.add(Dense(64))

    model.add(Dense(25))
    model.add(Activation('softmax'))

    model.compile(loss="sparse_categorical_crossentropy",
                  optimizer="adam",
                  metrics=['accuracy'])

    model.fit(self.x, self.y, batch_size=10, epochs=5)

    model.save("64x3-CNN-ASL4.model")

trained_model =学习模型() training_model.TrainModel() '''

我希望它在训练模型时不会出现任何错误。

0 个答案:

没有答案