简体   繁体   English

Tensorflow image_dataset_from_directory function label 形状

[英]Tensorflow image_dataset_from_directory function label shape

I am a tad new to Tensorflow and I am having trouble running this simple CNN.我对 Tensorflow 有点陌生,我在运行这个简单的 CNN 时遇到了麻烦。 I have my images separated into separate directories for each class, which I load into train_dataset using image_dataset_from_directory .我将图像分成每个 class 的单独目录,我使用image_dataset_from_directory将其加载到 train_dataset 中。

from the documentation, this should yield a tuple (images, labels), where images has shape (batch_size, image_size[0], image_size[1], num_channels), and labels are a float32 tensor of shape (batch_size, num_classes).从文档中,这应该产生一个元组(图像,标签),其中图像具有形状(batch_size,image_size [0],image_size [1],num_channels),并且标签是形状的float32张量(batch_size,num_classes)。 num_channels is 3 as the images are rgb num_channels 为 3,因为图像是 rgb

However when I try to fit using my model, I get an error saying that the predictions are [32,5] and labels shape [160].但是,当我尝试使用 model 进行拟合时,我收到一条错误消息,指出预测为 [32,5] 并且标签形状为 [160]。 It seems to me the batches in the labels have 'collapsed'.在我看来,标签中的批次已经“崩溃”了。

Here's some snippets:以下是一些片段:

BATCH_SIZE = 32
EPOCHS = 1
IMG_SIZE=(300, 300)
SEED = 1

train_dataset = tf.keras.preprocessing.image_dataset_from_directory(
    directory='train/train_images/', label_mode='categorical', class_names=class_names, color_mode='rgb', batch_size=BATCH_SIZE, image_size=IMG_SIZE)

IMG_SHAPE = IMG_SIZE + (3,)
n_classes = len(train_dataset.class_names)

def build_model():
    model = tf.keras.Sequential([
        tf.keras.layers.Conv2D(input_shape=IMG_SHAPE, kernel_size=(5, 5), filters=32, activation='relu'), 
        tf.keras.layers.MaxPool2D(pool_size=(3, 3)),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(256, activation='relu'),
        tf.keras.layers.Dropout(0.25),
        tf.keras.layers.Dense(units=n_classes, activation='softmax')
    ])
    
    return model

model = build_model()

model.compile(optimizer=tf.keras.optimizers.Adam(),
              loss=tf.keras.losses.sparse_categorical_crossentropy, 
              metrics=['accuracy'])

model.fit(train_dataset, epochs = EPOCHS, batch_size = BATCH_SIZE)

Error Message:错误信息:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-19-86d96e744ef0> in <module>
----> 1 model.fit(train_dataset, epochs = EPOCHS, batch_size = BATCH_SIZE)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in _method_wrapper(self, *args, **kwargs)
    106   def _method_wrapper(self, *args, **kwargs):
    107     if not self._in_multi_worker_mode():  # pylint: disable=protected-access
--> 108       return method(self, *args, **kwargs)
    109 
    110     # Running inside `run_distribute_coordinator` already.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_batch_size, validation_freq, max_queue_size, workers, use_multiprocessing)
   1096                 batch_size=batch_size):
   1097               callbacks.on_train_batch_begin(step)
-> 1098               tmp_logs = train_function(iterator)
   1099               if data_handler.should_sync:
   1100                 context.async_wait()

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in __call__(self, *args, **kwds)
    778       else:
    779         compiler = "nonXla"
--> 780         result = self._call(*args, **kwds)
    781 
    782       new_tracing_count = self._get_tracing_count()

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/def_function.py in _call(self, *args, **kwds)
    805       # In this case we have created variables on the first call, so we run the
    806       # defunned version which is guaranteed to never create variables.
--> 807       return self._stateless_fn(*args, **kwds)  # pylint: disable=not-callable
    808     elif self._stateful_fn is not None:
    809       # Release the lock early so that multiple threads can perform the call

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/function.py in __call__(self, *args, **kwargs)
   2827     with self._lock:
   2828       graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
-> 2829     return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
   2830 
   2831   @property

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _filtered_call(self, args, kwargs, cancellation_manager)
   1846                            resource_variable_ops.BaseResourceVariable))],
   1847         captured_inputs=self.captured_inputs,
-> 1848         cancellation_manager=cancellation_manager)
   1849 
   1850   def _call_flat(self, args, captured_inputs, cancellation_manager=None):

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
   1922       # No tape is watching; skip to running the function.
   1923       return self._build_call_outputs(self._inference_function.call(
-> 1924           ctx, args, cancellation_manager=cancellation_manager))
   1925     forward_backward = self._select_forward_and_backward_functions(
   1926         args,

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/function.py in call(self, ctx, args, cancellation_manager)
    548               inputs=args,
    549               attrs=attrs,
--> 550               ctx=ctx)
    551         else:
    552           outputs = execute.execute_with_cancellation(

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     58     ctx.ensure_initialized()
     59     tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
---> 60                                         inputs, attrs, num_outputs)
     61   except core._NotOkStatusException as e:
     62     if name is not None:

InvalidArgumentError:  logits and labels must have the same first dimension, got logits shape [32,5] and labels shape [160]
     [[node sparse_categorical_crossentropy/SparseSoftmaxCrossEntropyWithLogits/SparseSoftmaxCrossEntropyWithLogits (defined at <ipython-input-18-1904262c6a7b>:1) ]] [Op:__inference_train_function_928]

Function call stack:
train_function

I think you should explicitly compile your model before executing.我认为你应该在执行之前明确编译你的 model 。 For that, you may omit 'input_shape' parameter in first layer.为此,您可以在第一层省略“input_shape”参数。 See model.compile in keras documentation.请参阅 keras 文档中的 model.compile。 make sure that you keep loss as "categorical_crossentropy" or tf.keras.losses.CategoricalCrossentroy().确保将损失保持为“categorical_crossentropy”或 tf.keras.losses.CategoricalCrossentroy()。 Then try again.然后再试一次。 I hope this helps.我希望这有帮助。

Also it would help if you share your file structure.如果您共享文件结构也会有所帮助。

暂无
暂无

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

相关问题 如何在专辑 label 中调整数据集 label 的大小以使用 tensorflow image_dataset_from_directory ZC1C425268E687A945D - How resize dataset label in albumentations label to work with tensorflow image_dataset_from_directory function? Tensorflow image_dataset_from_directory 用于输入数据集和 output 数据集 - Tensorflow image_dataset_from_directory for input dataset and output dataset 丢失 function 与 image_dataset_from_directory 一起使用 - Loss function to use with image_dataset_from_directory 使用 tensorflow image_dataset_from_directory 时从数据集中获取标签 - Get labels from dataset when using tensorflow image_dataset_from_directory 使用 image_dataset_from_directory 时是否可以将 tensorflow 数据集拆分为训练、验证和测试数据集? - Is it possible to split a tensorflow dataset into train, validation AND test datasets when using image_dataset_from_directory? image_dataset_from_directory 用于多标签分类 - image_dataset_from_directory for multilabel classifcation 导入错误:无法从“tensorflow.keras.preprocessing”(未知位置)导入名称“image_dataset_from_directory” - ImportError: cannot import name 'image_dataset_from_directory' from 'tensorflow.keras.preprocessing' (unknown location) 从 image_dataset_from_directory function 生成的数据集不包括 batch size - Dataset generated from image_dataset_from_directory function does not include batch size tensorflow image_dataset_from_directory 通过索引列表获取某些图片的方法 - tensorflow image_dataset_from_directory get certain pictures of the method by index list image_dataset_from_directory label 列表似乎是按随机顺序分配的 - image_dataset_from_directory label list appear to be assigned in a random order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM