I need to do some experiments with digital mammograms to see if I get the best results with large images (maximum image size will be [3, 1024, 1024). So I try to change the input size of DenseNet121 with this:
import torch.nn as nn
import torchvision
import torch.nn.functional as F
import src.experiment.settings as config
class DenseNet121_reshaped(nn.Module):
def __init__(self):
super(DenseNet121_reshaped, self).__init__()
self.densenet121 = torchvision.models.densenet121(weights=torchvision.models.DenseNet121_Weights.IMAGENET1K_V1).features
self.maxpool = nn.AdaptiveMaxPool2d(1)
self.relu = nn.ReLU()
self.mlp = nn.Linear(in_features=config.IMG_SIZE, out_features=config.NUM_CLASSES)
self.sigmoid = nn.Sigmoid()
def maxpool(self ,x):
x = F.max_pool2d(x, kernel_size=x.size()[2:])
return x
def forward(self, x):
x = self.densenet121(x)
x = self.relu(x)
x = self.maxpool(x)
x = x.view(-1, config.IMG_SIZE)
x = self.mlp(x)
return x
settings.py with config parameters.
import torch.nn as nn
import torch.optim as optim
# Neural networks config
EPOCHS = 60
LEARNING_RATE = 0.0001 # 10**(-4)
LOSS_FUNCTION = nn.CrossEntropyLoss
OPTIMIZATION_FUNCTION = optim.Adam
NUM_CLASSES = 2
NUM_WORKERS = 4
BATCH_SIZE = 4
IMG_SIZE = 512
The code works with IMG_SIZE=1024 but crashes when I change IMG_SIZE to 512, giving this error msg.
Traceback (most recent call last):
File "train.py", line 59, in <module>
main(train_csv, val_csv, test_csv, img_path)
File "train.py", line 45, in main
train_dense(train_csv, val_csv, test_csv, img_path)
File "train.py", line 36, in train_dense
SimpleRunner.train(task, model, logger_description='DenseNet121 reshaped input size with AdaptiveMaxPool2d on Mammogramns', train_csv_file = train_csv, val_csv_file = val_csv, test_csv_file = test_csv)
File "/home/project/src/experiment/runner.py", line 100, in train
train_summary, val_summary = train(model=model,
File "/home/project/src/experiment/training.py", line 279, in train
train_summary = train_epoch(
File "/home/project/src/experiment/training.py", line 221, in train_epoch
predictions, loss = train_batch(
File "/home/project/src/experiment/training.py", line 189, in train_batch
loss = loss_fn(predictions, batch_labels)
File "/home/project/medical_images/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
return forward_call(*input, **kwargs)
File "/home/project/medical_images/lib/python3.8/site-packages/torch/nn/modules/loss.py", line 1164, in forward
return F.cross_entropy(input, target, weight=self.weight,
File "/home/project/medical_images/lib/python3.8/site-packages/torch/nn/functional.py", line 3014, 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 (8) to match target batch_size (4).
I use the torch info pkg to print the two reshapes denses.
from torchinfo import summary
summary(model, input_size=(config.BATCH_SIZE, 3, image_size, image_size), verbose=1, device='cuda')
this is with img_size=1024
============
Layer (type:depth-idx) Output Shape Param #
============
DenseNet121_change_avg [4, 2] β
ββSequential: 1-1 [4, 1024, 32, 32] β
β ββConv2d: 2-1 [4, 64, 512, 512] 9,408
β ββBatchNorm2d: 2-2 [4, 64, 512, 512] 128
β ββReLU: 2-3 [4, 64, 512, 512] β
β ββMaxPool2d: 2-4 [4, 64, 256, 256] β
β ββ_DenseBlock: 2-5 [4, 256, 256, 256] β
β β ββ_DenseLayer: 3-1 [4, 32, 256, 256] 45,440
β β ββ_DenseLayer: 3-2 [4, 32, 256, 256] 49,600
β β ββ_DenseLayer: 3-3 [4, 32, 256, 256] 53,760
β β ββ_DenseLayer: 3-4 [4, 32, 256, 256] 57,920
β β ββ_DenseLayer: 3-5 [4, 32, 256, 256] 62,080
β β ββ_DenseLayer: 3-6 [4, 32, 256, 256] 66,240
β ββ_Transition: 2-6 [4, 128, 128, 128] β
β β ββBatchNorm2d: 3-7 [4, 256, 256, 256] 512
β β ββReLU: 3-8 [4, 256, 256, 256] β
β β ββConv2d: 3-9 [4, 128, 256, 256] 32,768
β β ββAvgPool2d: 3-10 [4, 128, 128, 128] β
β ββ_DenseBlock: 2-7 [4, 512, 128, 128] β
β β ββ_DenseLayer: 3-11 [4, 32, 128, 128] 53,760
β β ββ_DenseLayer: 3-12 [4, 32, 128, 128] 57,920
β β ββ_DenseLayer: 3-13 [4, 32, 128, 128] 62,080
β β ββ_DenseLayer: 3-14 [4, 32, 128, 128] 66,240
β β ββ_DenseLayer: 3-15 [4, 32, 128, 128] 70,400
β β ββ_DenseLayer: 3-16 [4, 32, 128, 128] 74,560
β β ββ_DenseLayer: 3-17 [4, 32, 128, 128] 78,720
β β ββ_DenseLayer: 3-18 [4, 32, 128, 128] 82,880
β β ββ_DenseLayer: 3-19 [4, 32, 128, 128] 87,040
β β ββ_DenseLayer: 3-20 [4, 32, 128, 128] 91,200
β β ββ_DenseLayer: 3-21 [4, 32, 128, 128] 95,360
β β ββ_DenseLayer: 3-22 [4, 32, 128, 128] 99,520
β ββ_Transition: 2-8 [4, 256, 64, 64] β
β β ββBatchNorm2d: 3-23 [4, 512, 128, 128] 1,024
β β ββReLU: 3-24 [4, 512, 128, 128] β
β β ββConv2d: 3-25 [4, 256, 128, 128] 131,072
β β ββAvgPool2d: 3-26 [4, 256, 64, 64] β
β ββ_DenseBlock: 2-9 [4, 1024, 64, 64] β
β β ββ_DenseLayer: 3-27 [4, 32, 64, 64] 70,400
β β ββ_DenseLayer: 3-28 [4, 32, 64, 64] 74,560
β β ββ_DenseLayer: 3-29 [4, 32, 64, 64] 78,720
β β ββ_DenseLayer: 3-30 [4, 32, 64, 64] 82,880
β β ββ_DenseLayer: 3-31 [4, 32, 64, 64] 87,040
β β ββ_DenseLayer: 3-32 [4, 32, 64, 64] 91,200
β β ββ_DenseLayer: 3-33 [4, 32, 64, 64] 95,360
β β ββ_DenseLayer: 3-34 [4, 32, 64, 64] 99,520
β β ββ_DenseLayer: 3-35 [4, 32, 64, 64] 103,680
β β ββ_DenseLayer: 3-36 [4, 32, 64, 64] 107,840
β β ββ_DenseLayer: 3-37 [4, 32, 64, 64] 112,000
β β ββ_DenseLayer: 3-38 [4, 32, 64, 64] 116,160
β β ββ_DenseLayer: 3-39 [4, 32, 64, 64] 120,320
β β ββ_DenseLayer: 3-40 [4, 32, 64, 64] 124,480
β β ββ_DenseLayer: 3-41 [4, 32, 64, 64] 128,640
β β ββ_DenseLayer: 3-42 [4, 32, 64, 64] 132,800
β β ββ_DenseLayer: 3-43 [4, 32, 64, 64] 136,960
β β ββ_DenseLayer: 3-44 [4, 32, 64, 64] 141,120
β β ββ_DenseLayer: 3-45 [4, 32, 64, 64] 145,280
β β ββ_DenseLayer: 3-46 [4, 32, 64, 64] 149,440
β β ββ_DenseLayer: 3-47 [4, 32, 64, 64] 153,600
β β ββ_DenseLayer: 3-48 [4, 32, 64, 64] 157,760
β β ββ_DenseLayer: 3-49 [4, 32, 64, 64] 161,920
β β ββ_DenseLayer: 3-50 [4, 32, 64, 64] 166,080
β ββ_Transition: 2-10 [4, 512, 32, 32] β
β β ββBatchNorm2d: 3-51 [4, 1024, 64, 64] 2,048
β β ββReLU: 3-52 [4, 1024, 64, 64] β
β β ββConv2d: 3-53 [4, 512, 64, 64] 524,288
β β ββAvgPool2d: 3-54 [4, 512, 32, 32] β
β ββ_DenseBlock: 2-11 [4, 1024, 32, 32] β
β β ββ_DenseLayer: 3-55 [4, 32, 32, 32] 103,680
β β ββ_DenseLayer: 3-56 [4, 32, 32, 32] 107,840
β β ββ_DenseLayer: 3-57 [4, 32, 32, 32] 112,000
β β ββ_DenseLayer: 3-58 [4, 32, 32, 32] 116,160
β β ββ_DenseLayer: 3-59 [4, 32, 32, 32] 120,320
β β ββ_DenseLayer: 3-60 [4, 32, 32, 32] 124,480
β β ββ_DenseLayer: 3-61 [4, 32, 32, 32] 128,640
β β ββ_DenseLayer: 3-62 [4, 32, 32, 32] 132,800
β β ββ_DenseLayer: 3-63 [4, 32, 32, 32] 136,960
β β ββ_DenseLayer: 3-64 [4, 32, 32, 32] 141,120
β β ββ_DenseLayer: 3-65 [4, 32, 32, 32] 145,280
β β ββ_DenseLayer: 3-66 [4, 32, 32, 32] 149,440
β β ββ_DenseLayer: 3-67 [4, 32, 32, 32] 153,600
β β ββ_DenseLayer: 3-68 [4, 32, 32, 32] 157,760
β β ββ_DenseLayer: 3-69 [4, 32, 32, 32] 161,920
β β ββ_DenseLayer: 3-70 [4, 32, 32, 32] 166,080
β ββBatchNorm2d: 2-12 [4, 1024, 32, 32] 2,048
ββAdaptiveMaxPool2d: 1-2 β β
ββReLU: 1-3 [4, 1024, 32, 32] β
ββLinear: 1-4 [4, 2] 2,050
ββSigmoid: 1-5 β β
============
Total params: 6,955,906
Trainable params: 6,955,906
Non-trainable params: 0
Total mult-adds (G): 236.83
============
Input size (MB): 50.33
Forward/backward pass size (MB): 15091.11
Params size (MB): 27.82
Estimated Total Size (MB): 15169.26
============
and this was img_size=512
============
Layer (type:depth-idx) Output Shape Param #
============
DenseNet121_change_avg [8, 2] β
ββSequential: 1-1 [4, 1024, 16, 16] β
β ββConv2d: 2-1 [4, 64, 256, 256] 9,408
β ββBatchNorm2d: 2-2 [4, 64, 256, 256] 128
β ββReLU: 2-3 [4, 64, 256, 256] β
β ββMaxPool2d: 2-4 [4, 64, 128, 128] β
β ββ_DenseBlock: 2-5 [4, 256, 128, 128] β
β β ββ_DenseLayer: 3-1 [4, 32, 128, 128] 45,440
β β ββ_DenseLayer: 3-2 [4, 32, 128, 128] 49,600
β β ββ_DenseLayer: 3-3 [4, 32, 128, 128] 53,760
β β ββ_DenseLayer: 3-4 [4, 32, 128, 128] 57,920
β β ββ_DenseLayer: 3-5 [4, 32, 128, 128] 62,080
β β ββ_DenseLayer: 3-6 [4, 32, 128, 128] 66,240
β ββ_Transition: 2-6 [4, 128, 64, 64] β
β β ββBatchNorm2d: 3-7 [4, 256, 128, 128] 512
β β ββReLU: 3-8 [4, 256, 128, 128] β
β β ββConv2d: 3-9 [4, 128, 128, 128] 32,768
β β ββAvgPool2d: 3-10 [4, 128, 64, 64] β
β ββ_DenseBlock: 2-7 [4, 512, 64, 64] β
β β ββ_DenseLayer: 3-11 [4, 32, 64, 64] 53,760
β β ββ_DenseLayer: 3-12 [4, 32, 64, 64] 57,920
β β ββ_DenseLayer: 3-13 [4, 32, 64, 64] 62,080
β β ββ_DenseLayer: 3-14 [4, 32, 64, 64] 66,240
β β ββ_DenseLayer: 3-15 [4, 32, 64, 64] 70,400
β β ββ_DenseLayer: 3-16 [4, 32, 64, 64] 74,560
β β ββ_DenseLayer: 3-17 [4, 32, 64, 64] 78,720
β β ββ_DenseLayer: 3-18 [4, 32, 64, 64] 82,880
β β ββ_DenseLayer: 3-19 [4, 32, 64, 64] 87,040
β β ββ_DenseLayer: 3-20 [4, 32, 64, 64] 91,200
β β ββ_DenseLayer: 3-21 [4, 32, 64, 64] 95,360
β β ββ_DenseLayer: 3-22 [4, 32, 64, 64] 99,520
β ββ_Transition: 2-8 [4, 256, 32, 32] β
β β ββBatchNorm2d: 3-23 [4, 512, 64, 64] 1,024
β β ββReLU: 3-24 [4, 512, 64, 64] β
β β ββConv2d: 3-25 [4, 256, 64, 64] 131,072
β β ββAvgPool2d: 3-26 [4, 256, 32, 32] β
β ββ_DenseBlock: 2-9 [4, 1024, 32, 32] β
β β ββ_DenseLayer: 3-27 [4, 32, 32, 32] 70,400
β β ββ_DenseLayer: 3-28 [4, 32, 32, 32] 74,560
β β ββ_DenseLayer: 3-29 [4, 32, 32, 32] 78,720
β β ββ_DenseLayer: 3-30 [4, 32, 32, 32] 82,880
β β ββ_DenseLayer: 3-31 [4, 32, 32, 32] 87,040
β β ββ_DenseLayer: 3-32 [4, 32, 32, 32] 91,200
β β ββ_DenseLayer: 3-33 [4, 32, 32, 32] 95,360
β β ββ_DenseLayer: 3-34 [4, 32, 32, 32] 99,520
β β ββ_DenseLayer: 3-35 [4, 32, 32, 32] 103,680
β β ββ_DenseLayer: 3-36 [4, 32, 32, 32] 107,840
β β ββ_DenseLayer: 3-37 [4, 32, 32, 32] 112,000
β β ββ_DenseLayer: 3-38 [4, 32, 32, 32] 116,160
β β ββ_DenseLayer: 3-39 [4, 32, 32, 32] 120,320
β β ββ_DenseLayer: 3-40 [4, 32, 32, 32] 124,480
β β ββ_DenseLayer: 3-41 [4, 32, 32, 32] 128,640
β β ββ_DenseLayer: 3-42 [4, 32, 32, 32] 132,800
β β ββ_DenseLayer: 3-43 [4, 32, 32, 32] 136,960
β β ββ_DenseLayer: 3-44 [4, 32, 32, 32] 141,120
β β ββ_DenseLayer: 3-45 [4, 32, 32, 32] 145,280
β β ββ_DenseLayer: 3-46 [4, 32, 32, 32] 149,440
β β ββ_DenseLayer: 3-47 [4, 32, 32, 32] 153,600
β β ββ_DenseLayer: 3-48 [4, 32, 32, 32] 157,760
β β ββ_DenseLayer: 3-49 [4, 32, 32, 32] 161,920
β β ββ_DenseLayer: 3-50 [4, 32, 32, 32] 166,080
β ββ_Transition: 2-10 [4, 512, 16, 16] β
β β ββBatchNorm2d: 3-51 [4, 1024, 32, 32] 2,048
β β ββReLU: 3-52 [4, 1024, 32, 32] β
β β ββConv2d: 3-53 [4, 512, 32, 32] 524,288
β β ββAvgPool2d: 3-54 [4, 512, 16, 16] β
β ββ_DenseBlock: 2-11 [4, 1024, 16, 16] β
β β ββ_DenseLayer: 3-55 [4, 32, 16, 16] 103,680
β β ββ_DenseLayer: 3-56 [4, 32, 16, 16] 107,840
β β ββ_DenseLayer: 3-57 [4, 32, 16, 16] 112,000
β β ββ_DenseLayer: 3-58 [4, 32, 16, 16] 116,160
β β ββ_DenseLayer: 3-59 [4, 32, 16, 16] 120,320
β β ββ_DenseLayer: 3-60 [4, 32, 16, 16] 124,480
β β ββ_DenseLayer: 3-61 [4, 32, 16, 16] 128,640
β β ββ_DenseLayer: 3-62 [4, 32, 16, 16] 132,800
β β ββ_DenseLayer: 3-63 [4, 32, 16, 16] 136,960
β β ββ_DenseLayer: 3-64 [4, 32, 16, 16] 141,120
β β ββ_DenseLayer: 3-65 [4, 32, 16, 16] 145,280
β β ββ_DenseLayer: 3-66 [4, 32, 16, 16] 149,440
β β ββ_DenseLayer: 3-67 [4, 32, 16, 16] 153,600
β β ββ_DenseLayer: 3-68 [4, 32, 16, 16] 157,760
β β ββ_DenseLayer: 3-69 [4, 32, 16, 16] 161,920
β β ββ_DenseLayer: 3-70 [4, 32, 16, 16] 166,080
β ββBatchNorm2d: 2-12 [4, 1024, 16, 16] 2,048
ββAdaptiveMaxPool2d: 1-2 β β
ββReLU: 1-3 [4, 1024, 16, 16] β
ββLinear: 1-4 [8, 2] 1,026
ββSigmoid: 1-5 β β
============
Total params: 6,954,882
Trainable params: 6,954,882
Non-trainable params: 0
Total mult-adds (G): 59.21
============
Input size (MB): 12.58
Forward/backward pass size (MB): 3772.78
Params size (MB): 27.82
Estimated Total Size (MB): 3813.18
============
I canβt see whatβs wrongβ¦ can anyone help?