1
$\begingroup$

My goal is to train and evaluate The German Traffic Sign Recognition Benchmark (GTSRB) dataset using Pytorch.

I downloaded the datasets from the official site GTSRB_Final_Training_Images.zip and GTSRB_Final_Test_Images.zip.

But I get very poor results when I train the model.

I am using a simple CNN model.

The code:

import torch
import torch.nn as nn
import torch.optim as optim

from torchvision import datasets, transforms
from torch.utils.data import DataLoader

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

class Flatten(nn.Module):
   def forward(self, x):
     return x.view(x.shape[0], -1) 

odel_cnn = nn.Sequential(nn.Conv2d(3, 32, 3, padding=1), nn.ReLU(),
                      nn.Conv2d(32, 32, 3, padding=1, stride=2), nn.ReLU(),
                      nn.Conv2d(32, 64, 3, padding=1), nn.ReLU(),
                      nn.Conv2d(64, 64, 3, padding=1, stride=2), nn.ReLU(),
                      nn.Flatten(),
                      nn.Linear(4096, 100), nn.ReLU(),
                      nn.Linear(100, 44)).to(device)

data_transforms = transforms.Compose([
    transforms.Resize((32,32)),
    transforms.ToTensor(),
    #transforms.Normalize((0.5,), (1.0,))
    ])

train_data_path = "GTSRB/Train"
test_data_path = "GTSRB/Test"

train_data = datasets.ImageFolder(root = train_data_path,transform = data_transforms)
test_data = datasets.ImageFolder(root = test_data_path, transform = data_transforms)

train_loader = DataLoader(train_data, batch_size = 100, shuffle=True)
test_loader = DataLoader(test_data, batch_size = 100, shuffle=False)

def epoch(loader, model, opt=None):
total_loss, total_err = 0.,0.
for X,y in loader:
    X,y = X.to(device), y.to(device)
    yp = model(X)
    loss = nn.CrossEntropyLoss()(yp,y)
    if opt:
        opt.zero_grad()
        loss.backward()
        opt.step()
    
    total_err += (yp.max(dim=1)[1] != y).sum().item()
    total_loss += loss.item() * X.shape[0]
return total_err / len(loader.dataset), total_loss / len(loader.dataset)

opt = optim.SGD(model_cnn.parameters(), lr=1e-1)

for t in range(20):
    train_err, train_loss = epoch(train_loader, model_cnn, opt)
    test_err, test_loss = epoch(test_loader, model_cnn)
    if t == 4:
        for param_group in opt.param_groups:
            param_group["lr"] = 1e-2
    print(*("{:.6f}".format(i) for i in (train_err, train_loss, test_err, test_loss)), sep="\t")

The output:

0.518248    0.851532    1.000000    11.033573
0.514448    0.821411    0.999921    12.305183
0.515213    0.801951    1.000000 12.332444
0.519358    0.787077    1.000000    12.669552
0.511706    0.775102    0.997783    12.443266
0.509730    0.732133    0.999842    13.915797
0.508263    0.724747    0.999842    14.257156
0.510163    0.722937    1.000000    14.508054
0.510980    0.722053    0.999921    14.173411
0.511617    0.721190    0.999921    14.703455

The dataset contains ppm files. They are RGB.

I tried with another dataset containing png files but the result was also bad.

I have the entire code on Google Colab:

https://colab.research.google.com/drive/1br7sE2vI9Ilu3yKTpJdNABNVEmDZENmc?hl=en#scrollTo=qn3RgfRoAfVB

I don't know if there is problem with the model or the dataset.

Any suggestions?

$\endgroup$
0
$\begingroup$

Have you tried using a different learning rate? Yours seems to be quite high, you could try 1e-4 or something in that region.

Coming from the computer vision side, you could also try to improve image readability with prior grayscaling or blurring.

Here are some repos on GTSRB: https://github.com/Junth/Traffic-Sign-Classification-using-ConvNets https://github.com/surmenok/GTSRB

$\endgroup$
0
$\begingroup$

I fixed the problem by implementing a custom dataloader.

For more information about custom dataloader: https://debuggercafe.com/custom-dataset-and-dataloader-in-pytorch/ (Links to an external site.)

/Oualid

$\endgroup$
1
  • $\begingroup$ Close your question by accepting this answer. $\endgroup$ – The Pointer May 1 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.