簡體   English   中英

如何使用 Conv2d 解決輸入大小錯誤?

[英]How can i solve the Input Size Error with Conv2d?

RuntimeError:預期 3D(未批處理)或 4D(批處理)輸入到 conv2d,但得到大小輸入:[16, 1280] 即使我的 inputs.shape 是 torch.Size([16, 3, 120, 120])

嘿,我是 pytorch 的新用戶,所以請不要對我太苛刻。

我正在嘗試訓練以下 model 將圖像分類為 12 個標簽:

model =  models.efficientnet_b1(weights='DEFAULT').to(device) # put it to GPU

model.classifier = nn.Sequential(
    nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1),
    nn.ReLU(inplace=True),
    nn.MaxPool2d(kernel_size=2, stride=2),
    nn.Flatten(),
    nn.Linear(32*16*16, 128),
    nn.ReLU(inplace=True),
    nn.Linear(128, 12)
).to(device)

for param in model.parameters():
            param.requires_grad = False


optimizer = torch.optim.AdamW(model.classifier.parameters(), lr=5e-4, weight_decay=0.1) 

並像這樣訓練它:

# Define the loss function and the optimizer
criterion = nn.CrossEntropyLoss()

# Define the number of training epochs
num_epochs = 10

# Training loop
for epoch in range(num_epochs):
    # Set the model to train mode
    model.train()

    # Initialize the running loss for this epoch
    running_loss = 0.0
    
    # Iterate over the training data
    for i, data in enumerate(train_loader):
        inputs, labels = data
        inputs, labels = inputs.to(device), labels.to(device)

        # Zero the gradients
        optimizer.zero_grad()

        # Forward pass
  
        print(inputs.shape) // Print: torch.Size([16, 3, 120, 120])

        outputs = model(inputs) //Error
        loss = criterion(outputs, labels)

        # Backward pass and optimization
        loss.backward()
        optimizer.step()

        # Update the running loss
        running_loss += loss.item()

我的問題是,我收到錯誤:RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: [16, 1280] 我不明白為什么會收到此錯誤,因為輸入大小是 [16, 3, 120, 120](如印刷)

非常感謝您的幫助!

您正在用自己的模塊替換model.classifier 問題是原始模塊需要一個形式為 = (batch, 1280) 的輸入,而您正在用一個需要形式為 (batch, channels, height, width) 輸入的模塊替換它。 你可以這樣做:

net.classifier = nn.Sequential(
    nn.Dropout()
    nn.Linear(1280, 12)
)

這只是一個工作示例,我不知道您要做什么! 我想你想要 output 等於 12

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM