Skip to content

Commit 0716e8c

Browse files
committed
got some preliminary training going. The loss kept increasing in the negative direction with negative values. Will start implementing pixel normalization and use dice loss instead of binary cross-entropy loss.
1 parent 1e373cf commit 0716e8c

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ Pillow
88
scipy
99
argparse
1010
albumentations
11-
pytorch3d
11+
pytorch3d
12+
cuda-python

tutorial/dataset.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from PIL import Image
33
from torch.utils.data import Dataset
44
import numpy as np
5+
import torch
56
import scipy
7+
import torchvision.transforms.functional as TF
68

79

810
# def readKinematics(path):
@@ -30,8 +32,15 @@ def __getitem__(self, index):
3032
img_path = os.path.join(self.image_dir, self.images[index])
3133
mask_path = os.path.join(self.mask_dir, self.images[index])
3234

33-
image = np.array(Image.open(img_path).convert('RGB'))
34-
mask = np.array(Image.open(mask_path).convert('L'), dtype=np.float32)
35+
#image = TF.pil_to_tensor(Image.open(img_path).convert('RGB'))
36+
#mask = TF.pil_to_tensor(Image.open(mask_path).convert('L'))
37+
38+
image = torch.from_numpy(np.array(Image.open(img_path).convert('RGB'))/255.0).float()
39+
image = torch.permute(image, (2, 0, 1))
40+
mask = torch.from_numpy(np.array(Image.open(mask_path).convert('L'), dtype=np.float32)/255.0).float()
41+
42+
#print('image dim: ', image.size())
43+
#print('mask dim: ', mask.size())
3544

3645
#mask[mask == 255.0] = 1.0
3746

tutorial/train.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# Hyperparameters
1212
LEARNING_RATE = 1e-4
1313
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
14-
BATCH_SIZE = 10
14+
BATCH_SIZE = 5
1515
NUM_EPOCHS = 3
1616
NUM_WORKERS = 2 # What does this do?
1717
IMAGE_HEIGHT = 538 # 1280 originally
@@ -47,7 +47,7 @@ def train_fn(loader, model, optimizer, loss_fn, scaler):
4747
def main():
4848

4949
model = UNET(in_channels=3, out_channels=1).to(device=DEVICE)
50-
loss_fn = nn.BCEWithLogitsLoss()
50+
loss_fn = nn.BCEWithLogitsLoss() # LOSS FUNCTION DEFINED HERE. Perhaps change it to dice score
5151
optimizer = optim.Adam(model.parameters(), lr=LEARNING_RATE)
5252

5353
train_loader, val_loader = get_loaders(train_dir=TRAIN_IMG_DIR,
@@ -63,6 +63,7 @@ def main():
6363
scaler = torch.cuda.amp.GradScaler()
6464

6565
for epoch in range(NUM_EPOCHS):
66+
print(type(train_loader))
6667
train_fn(train_loader, model, optimizer, loss_fn, scaler)
6768

6869
# save model

0 commit comments

Comments
 (0)