繁体   English   中英

Tensorflow 图像分类得到train_images/train_X和train_labels/train_y

[英]Tensorflow Image classification get train_images/train_X and train_labels/train_y

我正在研究 tensorflow model 来识别不同的蝴蝶。 我为此使用神经网络,我正在从文件夹中读取图像,所有数据都在训练数据集和验证数据集中拆分,但我想像这样拆分这些:

(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

代替:

train_ds = utils.image_dataset_from_directory(data_dir, validation_split=0.2, subset="training", seed=123, image_size=(img_height, img_width), batch_size=BATCH_SIZE)
val_ds = utils.image_dataset_from_directory(data_dir, validation_split=0.2, subset="validation", seed=123, image_size=(img_height, img_width), batch_size=BATCH_SIZE)

我试过这样做,但它使我的 model 的准确性真的很糟糕,所以我认为它不正确:

train_images = np.concatenate([x for x, y in train_ds], axis=0)
train_labels = np.concatenate([y for x, y in train_ds], axis=0)
test_images = np.concatenate([x for x, y in val_ds], axis=0)
test_labels = np.concatenate([y for x, y in val_ds], axis=0)

我已经尝试了很多来自stackoverflow的方法,但它们也不起作用。

我的 model:

model = tf.keras.Sequential([
   # Please reread this link for a better understanding of the data being entered:
   #https://www.codespeedy.com/determine-input-shape-in-keras-tensorflow/
   layers.Conv2D(32, (3, 3), activation='relu', input_shape=(180, 180, 3)),
   layers.MaxPooling2D((2, 2)),
   layers.Conv2D(64, (3, 3), activation='relu'),
   layers.MaxPooling2D((2, 2), strides=2),
   layers.Flatten(),
   layers.Dropout(0.2, input_shape=(180, 180, 3)),
   layers.Dense(64, activation='relu'), 
   layers.Dense(5, activation='softmax') # there are 5 classes_names/folders or 5 kinds of butterflies
])
for image_batch, labels_batch in train_ds:
   print(image_batch.shape)
   print(labels_batch.shape)
   break

我在这里找到的这段代码表明你得到的数据集只是一个你可以迭代的迭代器。

所以这应该适用于生成你想要的列表

list_images = [step[0] for step in train_ds]
list_labels = [step[1] for step in train_ds]

但是在从文件夹生成数据集时,我必须设置批量大小。 否则,这将起作用,但项目 list_images 是包含具有批量大小长度的图像的列表。

修复了问题:

for x, y in train_ds:
  train_images = x
  train_labels = y

train_images 和 train_labels 不必初始化!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM