【发布时间】:2021-01-05 21:50:24
【问题描述】:
当我附加标签时,我最终得到 y 长度的 20580,而我希望最终得到 120,即类别数。如何将类别附加到我的标签?
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import random as rand
import time
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Activation, Dropout, Flatten, MaxPooling2D
from tensorflow.keras.callbacks import TensorBoard
from tensorflow.keras.optimizers import Adam
config = tf.compat.v1.ConfigProto(gpu_options=tf.compat.v1.GPUOptions(allow_growth=True))
sess = tf.compat.v1.Session(config=config)
DATADIR = "C:/Users/samue/Documents/Datasets/DogBreeds/images/Images"
CATEGORIES = os.listdir("C:/Users/samue/Documents/Datasets/DogBreeds/images/Images")
IMG_SIZE = 100
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()
rand.shuffle(training_data)
X = []
y = []
for features, label in training_data:
X.append(features)
y.append(label)
X = np.array(X).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
y = np.array(y).reshape(-1,)
print(len(CATEGORIES))
print(len(X))
print(len(y))
我最后得到的输出是: 120 20580 20580
【问题讨论】:
-
看起来你有 20,580 个数据点,
X(输入数组)和y(目标大批)。为什么您期望y只有 120 个元素? -
好的,我会解释我认为应该发生的事情,也许我对功能和标签有完全的误解。我正在尝试建立一个机器学习模型。据我了解,在这种情况下,特征 (X) 是您的数据点或照片,标签 (y) 是您要将数据点放入的不同组。因此,如果我的数据集有 120 个类别,我将需要 120 个标签来对应。是这样的吗?
标签: python numpy tensorflow keras