-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
861dddd
commit 3246e1d
Showing
5 changed files
with
67 additions
and
9 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# %% | ||
import torch | ||
from cellmap_data import CellMapDataset, CellMapDataLoader | ||
from torchvision.models import resnet18 | ||
|
||
# %% | ||
# Define the dataset files to use | ||
dataset_dict = { | ||
"train": {"raw": "train_data.zarr/raw", "gt": "train_data.zarr/gt", "weight": 1.0}, | ||
"val": {"raw": "val_data.zarr/raw", "gt": "val_data.zarr/gt"}, | ||
"test": {"raw": "test_data.zarr/raw", "gt": "test_data.zarr/gt"}, | ||
} | ||
|
||
# %% | ||
# Create the dataset and dataloader | ||
dataset = CellMapDataset(dataset_dict) | ||
dataloader = CellMapDataLoader(dataset, batch_size=4, shuffle=True, num_workers=0) | ||
|
||
# %% | ||
# Create the network | ||
model = resnet18(num_classes=2) | ||
|
||
# %% | ||
# Define the loss function and optimizer | ||
loss = torch.nn.MSELoss() | ||
optimizer = torch.optim.Adam(model.parameters(), lr=0.001) | ||
|
||
# %% | ||
# Train the network | ||
for epoch in range(10): | ||
for i, data in enumerate(dataloader): | ||
inputs, targets = data | ||
optimizer.zero_grad() | ||
outputs = model(inputs) | ||
loss_value = loss(outputs, targets) | ||
loss_value.backward() | ||
optimizer.step() | ||
print(f"Epoch {epoch}, Batch {i}, Loss {loss_value.item()}") | ||
# %% | ||
# Save the trained model | ||
torch.save(model.state_dict(), "model.pth") |
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 |
---|---|---|
|
@@ -14,3 +14,6 @@ | |
|
||
__author__ = "Jeff Rhoades" | ||
__email__ = "[email protected]" | ||
|
||
from .dataset import CellMapDataset | ||
from .dataloader import CellMapDataLoader |
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,6 @@ | ||
from torch.utils.data import DataLoader | ||
from cellmap_data.load import transforms | ||
|
||
|
||
class CellMapDataLoader(DataLoader): | ||
def __init__(self, dataset, batch_size=1, shuffle=False, num_workers=0): ... |
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