RuntimeError: Given groups=1, weight of size [256, 256, 3, 3], expected input[1, 512, 34, 25] to have 256 channels, but got 512 channels instead

username: Teerath
cooked: <p>I am running below script but getting error. Can anyone fix it?</p>
<p>import torch<br>
import torchvision<br>
from torchvision import transforms<br>
from torchvision.models.detection import fasterrcnn_resnet50_fpn<br>
from torchvision.models.vgg import vgg16<br>
import json</p>
<h1>
<a name="define-the-dataset-location-1" class="anchor" href="#define-the-dataset-location-1"></a>Define the dataset location</h1>
<p>train_dataset = torchvision.datasets.VOCDetection(<br>
root=‘./VOC2007’,<br>
year=‘2007’,<br>
image_set=‘train’,<br>
download=True<br>
)</p>
<p>label_map = {<br>
“background”: 0,<br>
“aeroplane”: 1,<br>
“bicycle”: 2,<br>
“bird”: 3,<br>
“boat”: 4,<br>
“bottle”: 5,<br>
“bus”: 6,<br>
“car”: 7,<br>
“cat”: 8,<br>
“chair”: 9,<br>
“cow”: 10,<br>
“diningtable”: 11,<br>
“dog”: 12,<br>
“horse”: 13,<br>
“motorbike”: 14,<br>
“person”: 15,<br>
“pottedplant”: 16,<br>
“sheep”: 17,<br>
“sofa”: 18,<br>
“train”: 19,<br>
“tvmonitor”: 20<br>
}</p>
<h1>
<a name="define-the-transform-pipeline-with-random-erasing-2" class="anchor" href="#define-the-transform-pipeline-with-random-erasing-2"></a>Define the transform pipeline with random erasing</h1>
<p>transform = transforms.Compose([<br>
transforms.Resize((224, 224)),<br>
transforms.RandomHorizontalFlip(p=0.5),<br>
transforms.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1, hue=0.1),<br>
transforms.RandomErasing(p=0.5, scale=(0.02, 0.33), ratio=(0.3, 3.3), value=‘random’),<br>
transforms.ToTensor,<br>
transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))<br>
])</p>
<h1>
<a name="create-a-data-loader-for-the-training-dataset-3" class="anchor" href="#create-a-data-loader-for-the-training-dataset-3"></a>Create a data loader for the training dataset</h1>
<p>def collate_fn(batch):<br>
return tuple(zip(*batch))</p>
<p>train_loader = torch.utils.data.DataLoader(<br>
train_dataset,<br>
batch_size=1,<br>
shuffle=True,<br>
num_workers=4,<br>
collate_fn=collate_fn<br>
)</p>
<h1>
<a name="define-the-fast-r-cnn-model-with-vgg16-as-the-backbone-network-4" class="anchor" href="#define-the-fast-r-cnn-model-with-vgg16-as-the-backbone-network-4"></a>Define the Fast R-CNN model with VGG16 as the backbone network</h1>
<p>model = fasterrcnn_resnet50_fpn(num_classes=21, pretrained_backbone=False, box_detections_per_img=1)<br>
backbone = vgg16(pretrained=True).features<br>
backbone.out_channels = 256<br>
model.backbone = backbone</p>
<p>device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)<br>
model.to(device)<br>
print(model)</p>
<h1>
<a name="define-the-optimizer-5" class="anchor" href="#define-the-optimizer-5"></a>Define the optimizer</h1>
<p>optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)</p>
<h1>
<a name="pass-the-data-to-the-model-for-training-6" class="anchor" href="#pass-the-data-to-the-model-for-training-6"></a>Pass the data to the model for training</h1>
<p>for images, targets in train_loader:<br>
# Move the images and targets to the device<br>
images = [transforms.ToTensor()(image).to(device) for image in images]</p>
<pre><code># images = transforms.ToTensor()(images).cuda()

# images = list(image.cuda() for image in images)
# targets = [{k: v for k, v in t.items()} for t in targets]
# convert PIL image to tensor
# image_tensor = transforms.ToTensor()(image)
# get the bounding box coordinates and class labels
boxes = []
labels = []

for tr in range(len(targets)):
    for obj in targets[tr]['annotation']['object']:
        bbox = obj['bndbox']
        xmin = float(bbox['xmin'])
        ymin = float(bbox['ymin'])
        xmax = float(bbox['xmax'])
        ymax = float(bbox['ymax'])
        boxes.append([xmin, ymin, xmax, ymax])
        labels.append(obj['name'])

# convert the bounding boxes and labels to tensors
boxes = torch.as_tensor(boxes, dtype=torch.float32)
labels = torch.as_tensor([label_map[label] for label in labels], dtype=torch.int64)

# create the target dictionary


targets_list = [{'boxes': boxes[i:i+1].cuda(), 'labels': labels[i:i+1].cuda()} for i in range(len(labels))]

print(len(images), len(targets_list[0]['boxes']), len(targets_list[0]['labels']))
# Assuming you have a loaded model called 'model'
print(images[0].size())
outputs = model(images, targets_list)

# iterate over the outputs and calculate losses
loss_dict = {}
for output in outputs:
    loss_dict.update(output['loss_classifier'])
    loss_dict.update(output['loss_box_reg'])
    loss_dict.update(output['loss_objectness'])
    loss_dict.update(output['loss_rpn_box_reg'])

losses = sum(loss for loss in loss_dict.values())
optimizer.zero_grad()
losses.backward()
optimizer.step()
</code></pre>

username: eqy
cooked: <p>What happens if the backbone <code>out_channels</code> field is modified e.g.,<br>
<code>backbone.out_channels = 512</code></p>

username: Teerath
cooked: <p><a class="mention" href="/u/eqy">@eqy</a> I still get the same error. I do not know why I am getting error.</p>