ZQPei / deep_sort_pytorch

MOT using deepsort and yolov3 with pytorch
MIT License
2.81k stars 725 forks source link

size error #231

Open sekisek opened 3 years ago

sekisek commented 3 years ago

Hi, If I want to train and detect on 256X256 I need to replace every appearance of 64 128 in every .py in the project , correct? when I do that, I get this error: Epoch : 1 Traceback (most recent call last): File "D:\AI\Codnda\deep_sort_pytorch\deep_sort\deep\train.py", line 189, in <module> main() File "D:\AI\Codnda\deep_sort_pytorch\deep_sort\deep\train.py", line 181, in main train_loss, train_err = train(epoch) File "D:\AI\Codnda\deep_sort_pytorch\deep_sort\deep\train.py", line 84, in train outputs = net(inputs) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl return forward_call(*input, **kwargs) File "D:\AI\Codnda\deep_sort_pytorch\deep_sort\deep\model.py", line 94, in forward x = self.classifier(x) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl return forward_call(*input, **kwargs) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\nn\modules\container.py", line 139, in forward input = module(input) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl return forward_call(*input, **kwargs) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\nn\modules\linear.py", line 96, in forward return F.linear(input, self.weight, self.bias) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\nn\functional.py", line 1847, in linear return torch._C._nn.linear(input, weight, bias) RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x59904 and 512x256)

I think I need to change this part on the model.py

`class Net(nn.Module): def init(self, num_classes=751 ,reid=False): super(Net,self).init()

3 128 64

    self.conv = nn.Sequential(
        nn.Conv2d(3,64,3,stride=1,padding=1),
        nn.BatchNorm2d(64),
        nn.ReLU(inplace=True),
        # nn.Conv2d(32,32,3,stride=1,padding=1),
        # nn.BatchNorm2d(32),
        # nn.ReLU(inplace=True),
        nn.MaxPool2d(3,2,padding=1),
    )
    # 32 64 32
    self.layer1 = make_layers(64,64,2,False)
    # 32 64 32
    self.layer2 = make_layers(64,128,2,True)
    # 64 32 16
    self.layer3 = make_layers(128,256,2,True)
    # 128 16 8
    self.layer4 = make_layers(256,512,2,True)
    # 256 8 4
    self.avgpool = nn.AvgPool2d((8,4),1)
    # 256 1 1 
    self.reid = reid
    self.classifier = nn.Sequential(
        nn.Linear(512, 256),
        nn.BatchNorm1d(256),
        nn.ReLU(inplace=True),
        nn.Dropout(),
        nn.Linear(256, num_classes),
    )

def forward(self, x):
    x = self.conv(x)
    x = self.layer1(x)
    x = self.layer2(x)
    x = self.layer3(x)
    x = self.layer4(x)
    x = self.avgpool(x)
    x = x.view(x.size(0),-1)
    # B x 128
    if self.reid:
        x = x.div(x.norm(p=2,dim=1,keepdim=True))
        return x
    # classifier
    x = self.classifier(x)
    return x

if name == 'main': net = Net() x = torch.randn(4,3,256,256) y = net(x) import ipdb; ipdb.set_trace()

` any idea?

chenke225 commented 3 years ago

@sekisek in Net forward part, change x = self.avgpool(x) to x = self.adaptiveavgpool(x), and add self.adaptiveavgpool = nn.AdaptiveAvgPool2d(1) in class Net(nn.Module) init

If you only want to use 256x256 reid target image format for deepsort track, you can modify feature_extractor.py self.size = (128, 128) --> self.size = (256, 256)

If you want to use 256x256 for trainning, you must modify train and test input transform:

transform_train = torchvision.transforms.Compose([
    torchvision.transforms.Resize((256,256)),
    torchvision.transforms.RandomCrop((256, 256),padding=4),                            
    torchvision.transforms.RandomHorizontalFlip(),
    torchvision.transforms.ToTensor(),
    torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
transform_test = torchvision.transforms.Compose([
    torchvision.transforms.Resize((256, 256)),                                    
    torchvision.transforms.ToTensor(),
    torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
sekisek commented 3 years ago

@chenke225 thank you for your awesome help!!!!

would you also change something here:

     # 32 64 32
    self.layer1 = make_layers(64,64,2,False)
    # 32 64 32
    self.layer2 = make_layers(64,128,2,True)
    # 64 32 16
    self.layer3 = make_layers(128,256,2,True)
    # 128 16 8
    self.layer4 = make_layers(256,512,2,True)
    # 256 8 4
    self.avgpool = nn.AvgPool2d((8,4),1)

?

chenke225 commented 3 years ago

@chenke225 thank you for your awesome help!!!!

would you also change something here:

     # 32 64 32
    self.layer1 = make_layers(64,64,2,False)
    # 32 64 32
    self.layer2 = make_layers(64,128,2,True)
    # 64 32 16
    self.layer3 = make_layers(128,256,2,True)
    # 128 16 8
    self.layer4 = make_layers(256,512,2,True)
    # 256 8 4
    self.avgpool = nn.AvgPool2d((8,4),1)

?

@sekisek

# 32 64 32
    self.layer1 = make_layers(64,64,2,False)
    # 32 64 32
    self.layer2 = make_layers(64,128,2,True)
    # 64 32 16
    self.layer3 = make_layers(128,256,2,True)
    # 128 16 8
    self.layer4 = make_layers(256,512,2,True)
    # 256 8 4
    self.adaptiveavgpool = nn.AdaptiveAvgPool2d(1)    # define adaptiveavgpool

def forward(self, x):
    x = self.conv(x)
    x = self.layer1(x)
    x = self.layer2(x)
    x = self.layer3(x)
    x = self.layer4(x)
    x = self.adaptiveavgpool(x)   # avgpool -->  adaptiveavgpool
    x = x.view(x.size(0),-1)
    # B x 128
    if self.reid:
        x = x.div(x.norm(p=2,dim=1,keepdim=True))
        return x
    # classifier
    x = self.classifier(x)
    return x
remeberWei commented 2 years ago

Hi, If I want to train and detect on 256X256 I need to replace every appearance of 64 128 in every .py in the project , correct? when I do that, I get this error: Epoch : 1 Traceback (most recent call last): File "D:\AI\Codnda\deep_sort_pytorch\deep_sort\deep\train.py", line 189, in <module> main() File "D:\AI\Codnda\deep_sort_pytorch\deep_sort\deep\train.py", line 181, in main train_loss, train_err = train(epoch) File "D:\AI\Codnda\deep_sort_pytorch\deep_sort\deep\train.py", line 84, in train outputs = net(inputs) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl return forward_call(*input, **kwargs) File "D:\AI\Codnda\deep_sort_pytorch\deep_sort\deep\model.py", line 94, in forward x = self.classifier(x) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl return forward_call(*input, **kwargs) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\nn\modules\container.py", line 139, in forward input = module(input) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\nn\modules\module.py", line 1051, in _call_impl return forward_call(*input, **kwargs) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\nn\modules\linear.py", line 96, in forward return F.linear(input, self.weight, self.bias) File "C:\Users\baman\miniconda3\envs\yolov5\lib\site-packages\torch\nn\functional.py", line 1847, in linear return torch._C._nn.linear(input, weight, bias) RuntimeError: mat1 and mat2 shapes cannot be multiplied (32x59904 and 512x256)

I think I need to change this part on the model.py

`class Net(nn.Module): def init(self, num_classes=751 ,reid=False): super(Net,self).init() # 3 128 64 self.conv = nn.Sequential( nn.Conv2d(3,64,3,stride=1,padding=1), nn.BatchNorm2d(64), nn.ReLU(inplace=True), # nn.Conv2d(32,32,3,stride=1,padding=1), # nn.BatchNorm2d(32), # nn.ReLU(inplace=True), nn.MaxPool2d(3,2,padding=1), ) # 32 64 32 self.layer1 = make_layers(64,64,2,False) # 32 64 32 self.layer2 = make_layers(64,128,2,True) # 64 32 16 self.layer3 = make_layers(128,256,2,True) # 128 16 8 self.layer4 = make_layers(256,512,2,True) # 256 8 4 self.avgpool = nn.AvgPool2d((8,4),1) # 256 1 1 self.reid = reid self.classifier = nn.Sequential( nn.Linear(512, 256), nn.BatchNorm1d(256), nn.ReLU(inplace=True), nn.Dropout(), nn.Linear(256, num_classes), )

def forward(self, x):
    x = self.conv(x)
    x = self.layer1(x)
    x = self.layer2(x)
    x = self.layer3(x)
    x = self.layer4(x)
    x = self.avgpool(x)
    x = x.view(x.size(0),-1)
    # B x 128
    if self.reid:
        x = x.div(x.norm(p=2,dim=1,keepdim=True))
        return x
    # classifier
    x = self.classifier(x)
    return x

if name == 'main': net = Net() x = torch.randn(4,3,256,256) y = net(x) import ipdb; ipdb.set_trace()

` any idea?

hello! do you train your custom dataset by deepsort ? If yes, can you help me with a problem? I don't know how to train my own dataset by deepsort, help!