Невозможно добавить тензор к партии: количество элементов не совпадает. Формы: [тензор]: [2], [партия]: [5] - PullRequest
0 голосов
/ 12 июля 2020
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [2], [batch]: [5]

Я получил эту ошибку, когда увеличил размер партии 1 до любого числа

def train():
    print('train')
    print(tf.__version__)
    batch_size= 8
    images,boxes,labels,difficulties,new_boxes= PascalVOCDataset()
    new_boxes = list(new_boxes)


    boxes = tf.ragged.constant(boxes)
    labels = tf.ragged.constant(labels)
    new_boxes = tf.ragged.constant(new_boxes)

    print('boxes->',boxes.shape)
    print('labels->',labels.shape)
    print('images->', np.array(images).shape)

    dataset = tf.data.Dataset.from_tensor_slices((images,new_boxes,labels))
    run_train(dataset.map(resize_image_bbox, num_parallel_calls=tf.data.experimental.AUTOTUNE).batch(batch_size).prefetch(tf.data.experimental.AUTOTUNE))

и результат формы будет

boxes-> (5717, None, None)
labels-> (5717, None)
images-> (5717,)


def run_train(dataset, num_epochs=1):
    start_time = time.perf_counter()
    print('run_train')
    model = SSD(n_classes=20)
    criterion = MultiBoxLoss(priors_cxcy=model.priors_cxcy)
    print('dataset->',dataset)

    for _ in tf.data.Dataset.range(num_epochs):
        for idx,(images,boxes,labels) in enumerate(dataset): # (batch_size (N), 300, 300, 3)
            print('=========================================================')
            images = np.array(images)
            labels = np.array(labels)
            boxes = np.array(list(boxes))
            print('image_shape->', images.shape)
            print('labels_shape->',labels.shape)
            print('boxes_shape->',boxes.shape)

            if isprint: tf.print(type(images), type(labels),images.shape,labels.shape)
            predicted_locs, predicted_socres = model(images)# (N, 8732, 4), (N, 8732, n_classes)
            loss = criterion(predicted_locs,predicted_socres,boxes,labels)
            print('loss->',loss)
            if idx ==10: break
        pass

все сообщение об ошибке, как показано ниже

Traceback (most recent call last):
  File "/home/jake/Gits/ssd_tensorflow/train.py", line 62, in <module>
    main()
  File "/home/jake/Gits/ssd_tensorflow/train.py", line 60, in main
    train()
  File "/home/jake/Gits/ssd_tensorflow/train.py", line 57, in train
    run_train(dataset.map(resize_image_bbox, num_parallel_calls=tf.data.experimental.AUTOTUNE).batch(batch_size).prefetch(tf.data.experimental.AUTOTUNE))
  File "/home/jake/Gits/ssd_tensorflow/train.py", line 24, in run_train
    for idx,(images,boxes,labels) in enumerate(dataset): # (batch_size (N), 300, 300, 3)
  File "/home/jake/venv/lib/python3.7/site-packages/tensorflow/python/data/ops/iterator_ops.py", line 631, in __next__
    return self.next()
  File "/home/jake/venv/lib/python3.7/site-packages/tensorflow/python/data/ops/iterator_ops.py", line 670, in next
    return self._next_internal()
  File "/home/jake/venv/lib/python3.7/site-packages/tensorflow/python/data/ops/iterator_ops.py", line 661, in _next_internal
    return structure.from_compatible_tensor_list(self._element_spec, ret)
  File "/usr/lib/python3.7/contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "/home/jake/venv/lib/python3.7/site-packages/tensorflow/python/eager/context.py", line 1989, in execution_mode
    executor_new.wait()
  File "/home/jake/venv/lib/python3.7/site-packages/tensorflow/python/eager/executor.py", line 67, in wait
    pywrap_tfe.TFE_ExecutorWaitForAllPendingNodes(self._handle)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot add tensor to the batch: number of elements does not match. Shapes are: [tensor]: [2], [batch]: [5]

Process finished with exit code 1
...