Automatic Mixed Precision Tutorials using pytorch. Based on PyTorch 1.6 Official Features (Automatic Mixed Precision), implement classification codebase using custom dataset.
pip install -r requirements.txt
This Data contains around 25k images of size 150x150 distributed under 6 categories. {'buildings' -> 0, 'forest' -> 1, 'glacier' -> 2, 'mountain' -> 3, 'sea' -> 4, 'street' -> 5 }
data
folder and move dataset into data
folder.
python main.py --checkpoint_name baseline;
In PyTorch 1.6, Automatic Mixed Precision Training is very easy to use! Thanks to PyTorch!
for batch_idx, (inputs, labels) in enumerate(data_loader):
self.optimizer.zero_grad()
outputs = self.model(inputs)
loss = self.criterion(outputs, labels)
loss.backward()
self.optimizer.step()
""" define loss scaler for automatic mixed precision """
scaler = torch.cuda.amp.GradScaler()
for batch_idx, (inputs, labels) in enumerate(data_loader):
self.optimizer.zero_grad()
with torch.cuda.amp.autocast():
outputs = self.model(inputs)
loss = self.criterion(outputs, labels)
# Scales the loss, and calls backward()
# to create scaled gradients
self.scaler.scale(loss).backward()
# Unscales gradients and calls
# or skips optimizer.step()
self.scaler.step(self.optimizer)
# Updates the scale for next iteration
self.scaler.update()
python main.py --checkpoint_name baseline_amp --amp;
Algorithm | Test Accuracy | GPU Memory | Total Training Time |
---|---|---|---|
B - 1080 Ti | 94.13 | 10737MB | 64.9m |
B - 2080 Ti | 94.17 | 10855MB | 54.3m |
AMP - 1080 Ti | 94.07 | 6615MB | 64.7m |
AMP - 2080 Ti | 94.23 | 7799MB | 37.3m |