How do I train DenseNet in PyTorch?

I’m working on a project to classify a series of images (X-Rays) using ‘Densenet-121’ in PyTorch.

I have a folder containing the images in png format, and a csv file that contains the image file names along with their labels (and other data as well).

I was told to follow this tutorial, and replace the sample dataset they use with my own, and to change the classifier they used to DenseNet-121: Quickstart — PyTorch Tutorials 1.9.0+cu102 documentation

I don’t really understand where I’m supposed to change the code to use a DenseNet classifier instead. The PyTorch tutorial for DenseNet (Densenet | PyTorch) uses a pretrained DenseNet model, whereas I will need to train it myself, but I’m not sure where to start with this.

To be honest, I’d be totally fine just to setup a simpler neural network than densenet, just so I have something to show for now, but I’m a bit overwhelmed with all the tutorials and now really sure how to get my own dataset/labels into PyTorch and use a classifier on it.

Could someone point me in the right direction or assist me with this?

You can replace the code for the network in the Creating Models part for:

model = torch.hub.load('pytorch/vision:v0.10.0', 'densenet121', pretrained=True)

Remember to change the last layer though!

1 Like