-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
323 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +0,0 @@ | ||
[submodule "mmaction2"] | ||
path = mmaction2 | ||
url = https://github.com/iucario/mmaction2 | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
docker run -it \ | ||
--gpus=all \ | ||
--shm-size=4gb \ | ||
--user="$(id -u):$(id -g)" \ | ||
--volume="$PWD:/app" \ | ||
-w /app \ | ||
--entrypoint bash \ | ||
--name ani \ | ||
anibali/pytorch:1.10.2-cuda11.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import os | ||
|
||
os.environ['NCCL_DEBUG'] = 'INFO' | ||
os.environ['NCCL_DEBUG_SUBSYS'] = 'ENV' | ||
os.environ['TORCH_DISTRIBUTED_DEBUG'] = 'DETAIL' | ||
os.environ['NCCL_P2P_LEVEL'] = 'LOC' | ||
os.environ['CUDA_LAUNCH_BLOCKING'] = '0' | ||
os.environ['CUDA_VISIBLE_DEVICES'] = '0,1' | ||
|
||
from typing import Optional | ||
import torch | ||
from torch import nn | ||
from torch.nn import functional as F | ||
from torch.utils.data import random_split, DataLoader, Subset | ||
from torchvision import transforms as T | ||
from torchvision.datasets import CIFAR10, MNIST | ||
from torchvision.models import resnet50 | ||
|
||
|
||
def train_epoch(model, batch, loss_fn, optimizer, device): | ||
model.train() | ||
optimizer.zero_grad() | ||
x, y = batch | ||
x = x.to(device) | ||
y = y.to(device) | ||
z = model(x) | ||
loss = loss_fn(z, y) | ||
loss.backward() | ||
optimizer.step() | ||
return loss.item(), (z.argmax(dim=1) == y).sum().item() | ||
|
||
|
||
def val_epoch(model, batch, loss_fn, device): | ||
model.eval() | ||
with torch.no_grad(): | ||
x, y = batch | ||
x = x.to(device) | ||
y = y.to(device) | ||
z = model(x) | ||
loss = loss_fn(z, y) | ||
return loss.item(), (z.argmax(dim=1) == y).sum().item() | ||
|
||
|
||
def main(): | ||
device = 'cuda' | ||
|
||
dataset = CIFAR10('./data', | ||
'train', | ||
download=True, | ||
transform=T.Compose([ | ||
T.ToTensor(), | ||
T.Resize(size=(224, 224)), | ||
])) | ||
train_set, val_set = random_split(Subset(dataset, range(500)), [400, 100]) | ||
train_loader = DataLoader(train_set, batch_size=16, shuffle=True) | ||
val_loader = DataLoader(val_set, batch_size=16, shuffle=False) | ||
print(dataset[0][0].shape) | ||
|
||
model = resnet50(pretrained=True) | ||
model = model.to(device) | ||
|
||
y = model(torch.randn(1, 3, 224, 224).to(device)) | ||
print(y.shape) | ||
|
||
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3) | ||
loss_fn = nn.CrossEntropyLoss() | ||
|
||
for i in range(10): | ||
train_correct = 0 | ||
for batch in train_loader: | ||
loss, correct = train_epoch(model, batch, loss_fn, optimizer, device) | ||
train_correct += correct | ||
train_acc = train_correct / len(train_loader.dataset) | ||
print(f"Train loss: {loss:.4f}, Train acc: {train_acc:.4f}") | ||
val_correct = 0 | ||
for batch in val_loader: | ||
loss, correct = val_epoch(model, batch, loss_fn, device) | ||
val_correct += correct | ||
val_acc = val_correct / len(val_loader.dataset) | ||
print(f"Val loss: {loss:.4f}, Val acc: {val_acc:.4f}") | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import os | ||
|
||
os.environ['NCCL_DEBUG'] = 'INFO' | ||
os.environ['NCCL_DEBUG_SUBSYS'] = 'ENV' | ||
os.environ['TORCH_DISTRIBUTED_DEBUG'] = 'DETAIL' | ||
os.environ['NCCL_P2P_LEVEL'] = 'LOC' | ||
os.environ['CUDA_LAUNCH_BLOCKING'] = '0' | ||
os.environ['CUDA_VISIBLE_DEVICES'] = '0,1' | ||
|
||
import torch | ||
from torch import nn | ||
import torch.nn.functional as F | ||
from torch.utils.data import DataLoader | ||
from torchvision import transforms as T | ||
from torch.utils.data import random_split, DataLoader, Subset | ||
from torchvision.datasets import CIFAR10, MNIST | ||
from torchvision.models import resnet18 | ||
|
||
torch.autograd.set_detect_anomaly(True) | ||
|
||
|
||
class Net(nn.Module): | ||
|
||
def __init__(self, num_class: int = 10): | ||
super(Net, self).__init__() | ||
fx = resnet18(pretrained=True) | ||
fx.fc = nn.Linear(512, num_class) | ||
self.model = fx | ||
|
||
def forward(self, x): | ||
return self.model(x) | ||
|
||
|
||
def train(model, device, train_loader, optimizer): | ||
model.train() | ||
for batch_idx, (data, target) in enumerate(train_loader): | ||
data, target = data.to(device), target.to(device) | ||
optimizer.zero_grad() | ||
output = model(data) | ||
loss = F.cross_entropy(output, target) | ||
loss.backward() | ||
optimizer.step() | ||
print(f'{batch_idx}/{len(train_loader)}, loss={loss.item():.4f}') | ||
|
||
|
||
def main(): | ||
device = 'cuda' | ||
|
||
dataset = CIFAR10('./data', | ||
'train', | ||
download=True, | ||
transform=T.Compose([ | ||
T.ToTensor(), | ||
T.Resize(size=(224, 224)), | ||
T.Normalize(mean=[0.485, 0.456, 0.406], | ||
std=[0.229, 0.224, 0.225]), | ||
])) | ||
train_set = Subset(dataset, range(200)) | ||
train_loader = DataLoader(train_set, batch_size=20, shuffle=True, num_workers=1) | ||
print(dataset[0][0].shape) | ||
|
||
model = Net(num_class=10) | ||
model = model.to(device) | ||
if torch.cuda.device_count() > 1: | ||
print("Let's use", torch.cuda.device_count(), "GPUs!") | ||
model = nn.DataParallel(model).to(device) | ||
|
||
y = model(torch.randn(1, 3, 224, 224).to(device)) | ||
print(y.shape) | ||
|
||
optimizer = torch.optim.SGD(model.parameters(), lr=0.001) | ||
print('start training') | ||
for i in range(3): | ||
train(model, device, train_loader, optimizer) | ||
|
||
print('Done') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.