-
Notifications
You must be signed in to change notification settings - Fork 329
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
TileDatasets #1353
Open
calebrob6
wants to merge
5
commits into
main
Choose a base branch
from
l7irish
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
TileDatasets #1353
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
module: | ||
_target_: torchgeo.trainers.SemanticSegmentationTask | ||
loss: "ce" | ||
model: "unet" | ||
backbone: "resnet18" | ||
weights: true | ||
learning_rate: 1e-4 | ||
learning_rate_schedule_patience: 6 | ||
in_channels: 8 | ||
num_classes: 5 | ||
num_filters: 64 | ||
ignore_index: 0 | ||
weight_decay: 0 | ||
|
||
datamodule: | ||
_target_: torchgeo.datamodules.L7IrishTileDataModule | ||
root: "/home/calebrobinson/ssdprivate/torchgeo/data/L7IrishSimple/" | ||
batch_size: 32 | ||
patch_size: 256 | ||
train_batches_per_epoch: 2000 | ||
val_batches_per_epoch: 200 | ||
num_workers: 6 | ||
|
||
trainer: | ||
_target_: lightning.pytorch.Trainer | ||
accelerator: gpu | ||
devices: | ||
- 3 | ||
min_epochs: 50 | ||
max_epochs: 100 | ||
|
||
program: | ||
seed: 0 | ||
output_dir: output/l7irish/ | ||
log_dir: logs/l7irish/ | ||
overwrite: True | ||
experiment_name: unet_imagenet_lr1e-4_wd0 |
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,81 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
"""Runs the train script with a grid of hyperparameters.""" | ||
import itertools | ||
import os | ||
import subprocess | ||
from multiprocessing import Process, Queue | ||
|
||
# list of GPU IDs that we want to use, one job will be started for every ID in the list | ||
GPUS = [0, 0, 1, 1, 2, 2, 3, 3] | ||
DRY_RUN = False # if False then print out the commands to be run, if True then run | ||
|
||
# Hyperparameter options | ||
model_options = ["unet", "fcn"] | ||
backbone_options = ["resnet18"] | ||
lr_options = [0.001, 0.0003, 0.0001, 0.00003] | ||
loss_options = ["ce"] | ||
wd_options = [0, 0.1, 0.01] | ||
weight_options = [True, False] | ||
seed_options = [0, 1, 2] | ||
|
||
|
||
def do_work(work: "Queue[str]", gpu_idx: int) -> bool: | ||
"""Process for each ID in GPUS.""" | ||
while not work.empty(): | ||
experiment = work.get() | ||
experiment = experiment.replace("GPU", str(gpu_idx)) | ||
print(experiment) | ||
if not DRY_RUN: | ||
subprocess.call(experiment.split(" ")) | ||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
work: "Queue[str]" = Queue() | ||
|
||
for model, backbone, lr, loss, wd, weights, seed in itertools.product( | ||
model_options, | ||
backbone_options, | ||
lr_options, | ||
loss_options, | ||
wd_options, | ||
weight_options, | ||
seed_options, | ||
): | ||
if model == "fcn" and not weights: | ||
continue | ||
|
||
if model != "unet": | ||
experiment_name = f"{model}_{backbone}_{lr}_{loss}_{wd}_{weights}_{seed}" | ||
else: | ||
experiment_name = f"{model}_{lr}_{loss}_{wd}_{weights}_{seed}" | ||
|
||
config_file = os.path.join("conf", "l7irishtile.yaml") | ||
|
||
command = ( | ||
"python train.py" | ||
+ f" config_file={config_file}" | ||
+ f" module.model={model}" | ||
+ f" module.backbone={backbone}" | ||
+ f" module.learning_rate={lr}" | ||
+ f" module.loss={loss}" | ||
+ f" module.weight_decay={wd}" | ||
+ f" module.weights={weights}" | ||
+ f" program.seed={seed}" | ||
+ f" program.experiment_name={experiment_name}" | ||
+ " trainer.devices=[GPU]" | ||
) | ||
command = command.strip() | ||
|
||
work.put(command) | ||
|
||
processes = [] | ||
for gpu_idx in GPUS: | ||
p = Process(target=do_work, args=(work, gpu_idx)) | ||
processes.append(p) | ||
p.start() | ||
for p in processes: | ||
p.join() |
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
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,59 @@ | ||
import rasterio | ||
import rasterio.io | ||
import rasterio.merge | ||
import rasterio.windows | ||
import torch | ||
from torch.utils.data import Dataset | ||
|
||
|
||
class TileDataset(Dataset): | ||
|
||
def __init__(self, image_fns, mask_fns=None, transforms=None, sanity_check=False): | ||
super().__init__() | ||
self.image_fns = image_fns | ||
self.mask_fns = mask_fns | ||
if self.mask_fns is not None: | ||
assert len(image_fns) == len(mask_fns) | ||
|
||
if sanity_check and mask_fns is not None: | ||
for image_fn, mask_fn in zip(image_fns, mask_fns): | ||
with rasterio.open(image_fn) as f: | ||
image_height, image_width = f.shape | ||
with rasterio.open(mask_fn) as f: | ||
mask_height, mask_width = f.shape | ||
assert image_height == mask_height | ||
assert image_width == mask_width | ||
|
||
self.transforms = transforms | ||
|
||
def __len__(self): | ||
return len(self.image_fns) | ||
|
||
def __getitem__(self, index): | ||
i, y, x, patch_size = index | ||
assert 0 <= i < len(self.image_fns) | ||
|
||
sample = { | ||
"y": y, | ||
"x": x, | ||
} | ||
|
||
window = rasterio.windows.Window( | ||
x, y, patch_size, patch_size | ||
) | ||
|
||
image_fn = self.image_fns[i] | ||
with rasterio.open(image_fn) as f: | ||
image = f.read(window=window) | ||
sample["image"] = torch.from_numpy(image).float() | ||
|
||
if self.mask_fns is not None: | ||
mask_fn = self.mask_fns[i] | ||
with rasterio.open(mask_fn) as f: | ||
mask = f.read(window=window) | ||
sample["mask"] = torch.from_numpy(mask).long() | ||
|
||
if self.transforms is not None: | ||
sample = self.transforms(sample) | ||
|
||
return sample |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we make one
run_{downstream_task}.py
script that has the config file name as an additional variable at the beginning of the file? Then we can use this one script for all the different downstream tasks, or are there some differences beyond the config files that we need to account for?