top of page

Recognizing Emergency Vehicles Using Pytorch and GPU | Realcode4you


In this task (T1-T3), you will be developing neural networks to recognize the images of emergencyvehicles (e.g., police cars, fire trucks, ambulances, etc.). Imagine a smart CCTV/traffic light that can automatically recognize emergencyvehicles and then adjust traffic lights/alarm road traffic accordingly. You will be given an image dataset with 681 images on emergencyvehicles and 965 on non-emergencyvehicles in A3-data folder.

Your Tasks

T1. Read and preprocess the data properly.

  • Upload the data to Google Drive and read them into PyTorch dataloader.

  • You should strategically separate 75% of the data into training while the rest 25% for validation. Here “strategically” means in both training and validation, the proportion of emergency data should be the same or very close.

  • You may copy some from Reference (4 ipynb, FMnist_Transfer and Fashion_MNIST_and_CNN) as attached while there is a need to add some lines/structures of codes that are not covered. The type for this task should be similar to the Reference.

T2. Develop a CNN-based neural network to recognize emergency vehicles.

  • Follow the structure shown in Figure 1 to write your network.

  • Train and validate your network using GPU. To save your time, set epoch<=10.

  • Draw a feasible line chart to monitor your train/validation loss.

  • Adjust/optimize your parameters and settings if the curve of validation loss is not nice.

T3. Develop a network to recognize the emergency vehicles based on transfer learning.

  • Use ResNet-50 (model = models.resnet50(pretrained=TRUE)) as your base model to establish the transfer learning. Modify and train the last fully connected layer of ResNet-50 and freeze other layers. Note that PyTorch has predefined ResNet-50 in torchvision.models, so you don’t need to develop its structure one more time.

  • Train and validate your transferred network using GPU. To save your time, set epoch<=10.

  • Draw a feasible line chart to monitor your train/validation loss.


1. Please output your code into ipynb

2. Try not to continuously open GPU > 12 hours. Google may block you for 24 hours if you do so.


T1. Read and preprocess the data properly.

#draft solution (Missing 'no"?)
import os
import numpy as np
import torch
from import Dataset, DataLoader
from torchvision.transforms import ToTensor
from PIL import Image

# Mount Google Drive
from google.colab import drive

# Define dataset class for A3-data
class A3Dataset(Dataset):
    def __init__(self, root_dir, transform=None):
        self.root_dir = root_dir
        self.transform = transform
        self.images = []
        self.labels = []
        for label in os.listdir(root_dir):
            label_dir = os.path.join(root_dir, label)
            if os.path.isdir(label_dir):
                for filename in os.listdir(label_dir):
                    image =, filename))
                    self.labels.append(int(label == 'yes'))

    def __len__(self):
        return len(self.images)

    def __getitem__(self, idx):
        image = self.images[idx]
        label = self.labels[idx]
        if self.transform:
            image = self.transform(image)
        return image, label

# Define transforms to convert PIL images to tensors
transform = ToTensor()

# Load data into A3Dataset
data_dir = '/content/drive/MyDrive/A3-data'
dataset = A3Dataset(data_dir, transform)

# Split dataset into training and validation sets
n_samples = len(dataset)
train_size = int(0.75 * n_samples)
val_size = n_samples - train_size
train_dataset, val_dataset =, [train_size, val_size])

# Create DataLoaders
train_dataloader = DataLoader(train_dataset, batch_size=32, shuffle=True)
val_dataloader = DataLoader(val_dataset, batch_size=32, shuffle=True)

To get complete code of this or need help to do this code or any other related to Deep Learning and Pytorch with GPU then don't worry. Our expert ready to help you to do your work with an reasonable price.

For more details you can send your requirement details at:


bottom of page