I managed to print the requested shapes and lengths, except the target’s shape. I also changed the input image size from 416 to 512, in case it was the reason of this problem, but still received the error, after some more training iterations. Here is the output:
Number of training samples: 142
Number of validation samples: 7
23544773
cpu
EPOCH 1 of 10
Training
0%| | 0/48 [00:00<?, ?it/s]
Images shape torch.Size([3, 3, 512, 512])
Images length 3
Targets length 3
FIRST: torch.Size([3, 64, 256, 256])
MAXPOOL: torch.Size([3, 64, 128, 128])
LAYER1: torch.Size([3, 256, 128, 128])
LAYER2: torch.Size([3, 512, 64, 64])
LAYER3: torch.Size([3, 1024, 32, 32])
LAYER4: torch.Size([3, 2048, 16, 16])
AVG: torch.Size([3, 2048, 1, 1])
FC: torch.Size([3, 2048])
Predictions shape torch.Size([3, 5])
Predictions length 3
Loss: 2.0571: 2%|█ | 1/48 [00:02<01:38, 2.10s/it]
Images shape torch.Size([3, 3, 512, 512])
Images length 3
Targets length 3
FIRST: torch.Size([3, 64, 256, 256])
MAXPOOL: torch.Size([3, 64, 128, 128])
LAYER1: torch.Size([3, 256, 128, 128])
LAYER2: torch.Size([3, 512, 64, 64])
LAYER3: torch.Size([3, 1024, 32, 32])
LAYER4: torch.Size([3, 2048, 16, 16])
AVG: torch.Size([3, 2048, 1, 1])
FC: torch.Size([3, 2048])
Predictions shape torch.Size([3, 5])
Predictions length 3
Loss: 1.3844: 4%|██▏ | 2/48 [00:03<01:25, 1.85s/it]
Images shape torch.Size([3, 3, 512, 512])
Images length 3
Targets length 3
FIRST: torch.Size([3, 64, 256, 256])
MAXPOOL: torch.Size([3, 64, 128, 128])
LAYER1: torch.Size([3, 256, 128, 128])
LAYER2: torch.Size([3, 512, 64, 64])
LAYER3: torch.Size([3, 1024, 32, 32])
LAYER4: torch.Size([3, 2048, 16, 16])
AVG: torch.Size([3, 2048, 1, 1])
FC: torch.Size([3, 2048])
Predictions shape torch.Size([3, 5])
Predictions length 3
Loss: 2.8780: 6%|███▎ | 3/48 [00:05<01:21, 1.82s/it]
Images shape torch.Size([3, 3, 512, 512])
Images length 3
Targets length 3
FIRST: torch.Size([3, 64, 256, 256])
MAXPOOL: torch.Size([3, 64, 128, 128])
LAYER1: torch.Size([3, 256, 128, 128])
LAYER2: torch.Size([3, 512, 64, 64])
LAYER3: torch.Size([3, 1024, 32, 32])
LAYER4: torch.Size([3, 2048, 16, 16])
AVG: torch.Size([3, 2048, 1, 1])
FC: torch.Size([3, 2048])
Predictions shape torch.Size([3, 5])
Predictions length 3
Loss: 1.8169: 8%|████▍ | 4/48 [00:07<01:20, 1.82s/it]
Images shape torch.Size([3, 3, 512, 512])
Images length 3
Targets length 3
FIRST: torch.Size([3, 64, 256, 256])
MAXPOOL: torch.Size([3, 64, 128, 128])
LAYER1: torch.Size([3, 256, 128, 128])
LAYER2: torch.Size([3, 512, 64, 64])
LAYER3: torch.Size([3, 1024, 32, 32])
LAYER4: torch.Size([3, 2048, 16, 16])
AVG: torch.Size([3, 2048, 1, 1])
FC: torch.Size([3, 2048])
Predictions shape torch.Size([3, 5])
Predictions length 3
Loss: 1.9297: 10%|█████▌ | 5/48 [00:09<01:17, 1.80s/it]
Images shape torch.Size([3, 3, 512, 512])
Images length 3
Targets length 3
FIRST: torch.Size([3, 64, 256, 256])
MAXPOOL: torch.Size([3, 64, 128, 128])
LAYER1: torch.Size([3, 256, 128, 128])
LAYER2: torch.Size([3, 512, 64, 64])
LAYER3: torch.Size([3, 1024, 32, 32])
LAYER4: torch.Size([3, 2048, 16, 16])
AVG: torch.Size([3, 2048, 1, 1])
FC: torch.Size([3, 2048])
Predictions shape torch.Size([3, 5])
Predictions length 3
Loss: 1.6944: 12%|██████▋ | 6/48 [00:10<01:13, 1.76s/it]
Images shape torch.Size([3, 3, 512, 512])
Images length 3
Targets length 3
FIRST: torch.Size([3, 64, 256, 256])
MAXPOOL: torch.Size([3, 64, 128, 128])
LAYER1: torch.Size([3, 256, 128, 128])
LAYER2: torch.Size([3, 512, 64, 64])
LAYER3: torch.Size([3, 1024, 32, 32])
LAYER4: torch.Size([3, 2048, 16, 16])
AVG: torch.Size([3, 2048, 1, 1])
FC: torch.Size([3, 2048])
Predictions shape torch.Size([3, 5])
Predictions length 3
Loss: 1.5850: 15%|███████▋ | 7/48 [00:12<01:09, 1.70s/it]
Images shape torch.Size([3, 3, 512, 512])
Images length 3
Targets length 3
FIRST: torch.Size([3, 64, 256, 256])
MAXPOOL: torch.Size([3, 64, 128, 128])
LAYER1: torch.Size([3, 256, 128, 128])
LAYER2: torch.Size([3, 512, 64, 64])
LAYER3: torch.Size([3, 1024, 32, 32])
LAYER4: torch.Size([3, 2048, 16, 16])
AVG: torch.Size([3, 2048, 1, 1])
FC: torch.Size([3, 2048])
Predictions shape torch.Size([3, 5])
Predictions length 3
Loss: 0.6640: 17%|████████▊ | 8/48 [00:14<01:07, 1.69s/it]
Images shape torch.Size([3, 3, 512, 512])
Images length 3
Targets length 3
FIRST: torch.Size([3, 64, 256, 256])
MAXPOOL: torch.Size([3, 64, 128, 128])
LAYER1: torch.Size([3, 256, 128, 128])
LAYER2: torch.Size([3, 512, 64, 64])
LAYER3: torch.Size([3, 1024, 32, 32])
LAYER4: torch.Size([3, 2048, 16, 16])
AVG: torch.Size([3, 2048, 1, 1])
FC: torch.Size([3, 2048])
Predictions shape torch.Size([3, 5])
Predictions length 3
Loss: 0.9549: 19%|█████████▉ | 9/48 [00:15<01:06, 1.71s/it]
Images shape torch.Size([3, 3, 512, 512])
Images length 3
Targets length 3
FIRST: torch.Size([3, 64, 256, 256])
MAXPOOL: torch.Size([3, 64, 128, 128])
LAYER1: torch.Size([3, 256, 128, 128])
LAYER2: torch.Size([3, 512, 64, 64])
LAYER3: torch.Size([3, 1024, 32, 32])
LAYER4: torch.Size([3, 2048, 16, 16])
AVG: torch.Size([3, 2048, 1, 1])
FC: torch.Size([3, 2048])
Predictions shape torch.Size([3, 5])
Predictions length 3
Loss: 3.3928: 21%|██████████▊ | 10/48 [00:17<01:08, 1.80s/it]
Images shape torch.Size([3, 3, 512, 512])
Images length 3
Targets length 3
FIRST: torch.Size([3, 64, 256, 256])
MAXPOOL: torch.Size([3, 64, 128, 128])
LAYER1: torch.Size([3, 256, 128, 128])
LAYER2: torch.Size([3, 512, 64, 64])
LAYER3: torch.Size([3, 1024, 32, 32])
LAYER4: torch.Size([3, 2048, 16, 16])
AVG: torch.Size([3, 2048, 1, 1])
FC: torch.Size([3, 2048])
Predictions shape torch.Size([3, 5])
Predictions length 3
Loss: 3.3928: 21%|██████████▊ | 10/48 [00:18<01:10, 1.85s/it]
Traceback (most recent call last):
File "/lustre/alice3/scratch/aiadapt/va95/od/2/20211025_Custom_Object_Detection_using_PyTorch_Faster_RCNN/src/engine.py", line 138, in <module>
train_loss = train(train_loader, model)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/lustre/alice3/scratch/aiadapt/va95/od/2/20211025_Custom_Object_Detection_using_PyTorch_Faster_RCNN/src/engine.py", line 47, in train
loss = model.compute_loss(predictions, targets)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/lustre/alice3/scratch/aiadapt/va95/od/2/20211025_Custom_Object_Detection_using_PyTorch_Faster_RCNN/src/model.py", line 139, in compute_loss
loss = F.cross_entropy(predictions, target_labels)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/v/va95/miniconda3/envs/odenv/lib/python3.11/site-packages/torch/nn/functional.py", line 3053, in cross_entropy
return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Expected input batch_size (3) to match target batch_size (4).