Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ndelima/training validation #61

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
isaac_ws/datasets/**
model/*
bag_ws/*
training_ws/runs
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,17 @@ After the training ends, a `model.pth` file will be available inside `model`. Ad
docker compose -f docker/docker-compose.yml --profile training_test up
```

This will evaluate every image in the `DATASET_NAME` and generate annotated images in the `model` folder.
This will evaluate every image in the `DATASET_NAME` and generate annotated images in the `model/test_output` folder.

### Training logs visualization

The logs generated when training a model are stored in the `model/runs` folder and they can be visualized using the profile `training_vis`. This profile runs Tensorboard over `localhost:6006`, and can be accessed via a web browser. To run the Tensorboard server, execute:

```bash
docker compose -f docker/docker-compose.yml --profile training_vis up
```

![Tensorboard dashboard](./doc/tensorboard.png)

agalbachicar marked this conversation as resolved.
Show resolved Hide resolved
## Run

Expand Down
Binary file added doc/tensorboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 13 additions & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ services:
- ../isaac_ws/datasets/${DATASET_NAME}:/root/training_ws/data
- ../model/:/root/training_ws/model
- torch_cache:/root/.cache/torch
- ../model/runs/:/root/training_ws/runs
deploy:
resources:
reservations:
Expand All @@ -39,7 +40,7 @@ services:
"-d",
"./data",
"-o",
"./model"]
"./model/test_output"]
volumes:
- ../isaac_ws/datasets/${DATASET_NAME}:/root/training_ws/data
- ../model/:/root/training_ws/model
Expand All @@ -52,6 +53,17 @@ services:
count: 1
capabilities:
- gpu
training_log_visualization:
build:
context: ..
dockerfile: docker/training.dockerfile
target: training_prod
container_name: training_vis
profiles: ["training_vis"]
entrypoint: ["tensorboard", "--logdir=/root/training_ws/runs/"]
volumes:
- ../model/runs:/root/training_ws/runs
network_mode: host
detection:
build:
context: ..
Expand Down
2 changes: 2 additions & 0 deletions training_ws/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ def decode_output(output, labels_list, threshold=0.05):
def main():
"""Run inference on dataset and store images with bounding boxes."""
options, args = parse_input()
if not os.path.exists(options.output_folder):
os.makedirs(options.output_folder)
dataset = FruitDataset(options.data_dir, get_transform())

validloader = torch.utils.data.DataLoader(
Expand Down
2 changes: 1 addition & 1 deletion training_ws/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
torch==2.4.0
torchvision==0.19.0
tensorboard==2.17
tensorboard==2.17.1
24 changes: 20 additions & 4 deletions training_ws/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,18 @@ def main():
options, args = parse_input()
dataset = FruitDataset(options.data_dir, get_transform(train=True))
train_size = int(len(dataset) * TRAINING_PARTITION_RATIO)
unused_size = len(dataset) - train_size
valid_size = len(dataset) - train_size

train, unused = torch.utils.data.random_split(
train, valid = torch.utils.data.random_split(
dataset,
[train_size, unused_size],
[train_size, valid_size],
)
train_loader = torch.utils.data.DataLoader(
train, batch_size=1, shuffle=True, num_workers=0, collate_fn=collate_fn
)
valid_loader = torch.utils.data.DataLoader(
valid, batch_size=1, shuffle=True, num_workers=0, collate_fn=collate_fn
)

device = None
if torch.cuda.is_available():
Expand Down Expand Up @@ -268,13 +271,26 @@ def main():
] # Format the annotations for model consumption
loss_dict = model(imgs, annotations)
losses = sum(loss for loss in loss_dict.values())
writer.add_scalar("Loss/train", losses, epoch)
writer.add_scalar("Loss/Train", losses, epoch)

losses.backward()
optimizer.step()

print(f"Iteration: {i}/{len_dataloader}, Loss: {losses}")

loss_sum = 0
for imgs, annotations in valid_loader:
imgs = list(img.to(device) for img in imgs)
annotations = [
{k: v.to(device) for k, v in t.items()} for t in annotations
] # Format the annotations for model consumption
loss_dict = model(imgs, annotations)
losses = sum(loss for loss in loss_dict.values())
loss_sum = loss_sum + losses.item()
avg_loss = loss_sum / len(valid_loader)
print(f"Epoch: {epoch}, Validation loss (average): {avg_loss}")
writer.add_scalar("Loss/Validation", avg_loss, epoch)

writer.close()
torch.save(model.state_dict(), options.output_file)
print(
Expand Down
Loading