DTS304TC CW2 Paper
DTS304TC CW2 Paper
I certify that I have read and understood the University’s Policy for dealing with Plagiarism, Collusion
and the Fabrication of Data (available on Learning Mall Online). With reference to this policy I certify
that:
1) My work does not contain any instances of plagiarism and/or collusion.
My work does not contain any fabricated data.
By uploading my assignment onto Learning Mall Online, I formally declare that all of the
above information is true to the best of my knowledge and belief.
Scoring – For Tutor Use
Student ID
• Please read the coursework instructions and requirements carefully. Not following these instructions
and requirements may result in loss of marks.
• The formal procedure for submitting coursework at XJTLU is strictly followed. Submission link on
Learning Mall will be provided in due course. The submission timestamp on Learning Mall will be
used to check late submission.
__________________________________________________________________
Before starting this project, please ensure that you have downloaded the pretrained model, assessment
codebase, and the example Jupyter notebook.
Notes:
1. Although a GPU-powered machine is preferable for running this project, you can efficiently
complete it on a CPU-only machine, such as those provided in school labs. This is due to the small size
of both the model and the fine-tuning dataset.
2. You are strongly encouraged to read the provided code and accompanying training notebook as
you will be referring to this code and modifying it during the project. You are encouraged to utilize code
that was covered during our Lab sessions, as well as other online resources for assistance. Please ensure
that you provide proper citations and links to any external resources you employ in your work. However,
the use of Generative AI for content generation (such as ChatGPT) is not permitted on all assessed
coursework in this module.
4) Unsupervised domain adaptation using adaptive batch normalization (AdaBN) (10 marks):
Our pretrained UNet model, which includes batch normalization layers and was initially trained using
bright, sunny images, will undergo an adaptation process through Adaptive Batch Normalization
(AdaBN) to better perform on a target domain featuring dark, cloudy images. The adaptation process is
outlined as follows:
⚫ Model Preparation: Adjust the model in the training mode to allow the batch normalization layers
to update their running statistics, and at the same time ensure the pretrained convolution weights
remain untouched.
⚫ Statistics Update: Conduct forward passes with the target domain data, specifically using our
cloudy training dataloader, through the UNet model. This step aims to recalibrate the batch
normalization layers by modifying their running mean and variance to align with the target
domain's data distribution.
⚫ Switch to Inference: Once the batch normalization layers have been updated to reflect the target
domain statistics, transition the model to inference mode.
For more details refer to the paper Li, Y., Wang, N., Shi, J., Liu, J., & Hou, X. (2016). Revisiting batch
normalization for practical domain adaptation. arXiv preprint arXiv:1603.04779.
In [1]: # example unet model training code for sunny bright image set
# major library dependencies: jupyter, numpy, matplotlib, pytorch, scikit-image,
In [2]: # hyper-parameters
lr = 0.0002
epochs = 30
batch_size = 8
num_workers = 8
# we are training on the sunny camvid dataset.
data_root = './CamVid/sunny'
num_classes = 14 # number of classes is always 14 for this project.
labels = ['Sky', 'Building', 'Pole', 'Road', 'LaneMarking', 'SideWalk', 'Pavemen
'Fence', 'Car_Bus', 'Pedestrian', 'Bicyclist', 'Others']
localhost:8888/lab/tree/code_example_train_unet_sunny.ipynb 1/7
2/9/24, 8:30 AM code_example_train_unet_sunny
localhost:8888/lab/tree/code_example_train_unet_sunny.ipynb 2/7
2/9/24, 8:30 AM code_example_train_unet_sunny
plt.figure(i * 2 + 0)
plt.imshow(img)
plt.figure(i * 2 + 1)
plt.imshow(label_rgb_vis)
localhost:8888/lab/tree/code_example_train_unet_sunny.ipynb 3/7
2/9/24, 8:30 AM code_example_train_unet_sunny
localhost:8888/lab/tree/code_example_train_unet_sunny.ipynb 4/7
2/9/24, 8:30 AM code_example_train_unet_sunny
localhost:8888/lab/tree/code_example_train_unet_sunny.ipynb 5/7
2/9/24, 8:30 AM code_example_train_unet_sunny
# validation loop
unet.eval()
val_loss = 0
count = 0
for idx_batch, (imagergb, labelmask, filename) in enumerate(val_loader):
with torch.no_grad(): # no gradient computation required during validati
x = imagergb.to(device)
y_ = labelmask.to(device)
y = unet(x)
loss = loss_func(y, y_)
val_loss += loss.item()
count += 1
localhost:8888/lab/tree/code_example_train_unet_sunny.ipynb 6/7
2/9/24, 8:30 AM code_example_train_unet_sunny
localhost:8888/lab/tree/code_example_train_unet_sunny.ipynb 7/7
2/9/24, 8:30 AM part1
In [1]: # part 1 experimentation: Evaluate the pretrained model on bright sunny test ima
# major library dependencies: jupyter, numpy, matplotlib, pytorch, scikit-image,
import torch
from dataset import camvidLoader
import numpy as np
In [4]: # calculate global image accuracy and per-class accuracy for the entire dataset
In [5]: # implement image ranking code according to the per-image global image accuracy
localhost:8888/lab/tree/part1.ipynb 1/1
2/9/24, 8:31 AM part2
In [1]: # part 2 experimentation: Evaluate and fine-tune the pretrained model on darker
# major library dependencies: jupyter, numpy, matplotlib, pytorch, scikit-image,
import torch
from dataset import camvidLoader
import numpy as np
data_root = './CamVid/cloudy'
test_data = camvidLoader(root=data_root, split='test', is_aug=False, img_size =
In [ ]: # Plot training/validation loss curves and determine the best fine-tuned model.
localhost:8888/lab/tree/part2.ipynb 1/1
2/9/24, 8:31 AM part3a
data_root = './CamVid/cloudy'
test_data = camvidLoader(root=data_root, split='test', is_aug=False, img_size =
In [ ]: # write the function that computes the entropy map for the unet output
def compute_entropy_map(model_output):
pass
In [ ]: # write the function that computes the segmentation error map for the unet outpu
def compute_segmentation_error_map(model_output, gt):
pass
In [ ]: # write a evaluation loop to calculate the entropy map and segmenation error map
# observe the relationship between entropy map and segmenation error map by visu
localhost:8888/lab/tree/part3a.ipynb 1/1
2/9/24, 8:31 AM part3b
data_root = './CamVid/cloudy'
test_data = camvidLoader(root=data_root, split='test', is_aug=False, img_size =
In [2]: batch_size = 4
num_workers = 8
# we set is_pytorch_transform to true, in order to perform network training/test
train_dataset = camvidLoader(root=data_root, split='train', is_aug=False, img_si
is_pytorch_transform = True, aug = None)
train_loader = DataLoader(train_dataset, num_workers=num_workers, batch_size=bat
In [ ]: # This is the code of model fine-tuning on the cloudy dataset with pseudo-ground
# Note that loss calculation part of the code is imcomplete.
lr = 5e-6
epochs = 1
loss_func = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(unet.parameters(), lr=lr)
localhost:8888/lab/tree/part3b.ipynb 1/2
2/9/24, 8:31 AM part3b
if idx_batch % 2 == 0:
print("train epoch = " + str(epoch) + " | batch = " + str(idx_batch)
localhost:8888/lab/tree/part3b.ipynb 2/2
2/9/24, 8:31 AM Q4
data_root = './CamVid/cloudy'
test_data = camvidLoader(root=data_root, split='test', is_aug=False, img_size =
localhost:8888/lab/tree/Q4.ipynb 1/1