Skip to content

Commit

Permalink
autopep8
Browse files Browse the repository at this point in the history
  • Loading branch information
madhavkhoslaa committed Jun 28, 2019
1 parent f94b5be commit d4ce554
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
4 changes: 2 additions & 2 deletions dataloader/dataloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def __getitem__(self, index):
label = skimage.io.imread(self.target_images[index])
if self.transform:
image = self.transform(image)
image= torch.from_numpy(image)
label= torch.from_numpy(label)
image = torch.from_numpy(image)
label = torch.from_numpy(label)
if torch.cuda.is_available():
return {"Image": image.cuda(), "Label": label.cuda()}
else:
Expand Down
3 changes: 1 addition & 2 deletions hyperparams/hyperparams.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class hyperparameters:
def __init__(self, **kwargs):
self.hyperparameters= dict()
self.hyperparameters = dict()
for key, value in kwargs.items():
self.hyperparameters.update({key: value})

4 changes: 3 additions & 1 deletion loss/diceloss.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import torch
import torch.nn as nn
import torch.functional as F


class Loss:
@classmethod
def dice_loss(self, pred, target, smooth=1.):
pred = pred.contiguous()
target = target.contiguous()
intersection = (pred * target).sum(dim=2).sum(dim=2)
loss = (1 - ((2. * intersection + smooth) /
(pred.sum(dim=2).sum(dim=2) + target.sum(dim=2).sum(dim=2) + smooth)))
(pred.sum(dim=2).sum(dim=2) + target.sum(dim=2).sum(dim=2) + smooth)))
return loss.mean()

def calc_loss(self, pred, target, metrics, bce_weight=0.5):
Expand Down
6 changes: 4 additions & 2 deletions test/test_iter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@
train_percentage=params.hyperparameters['train_percentage'])
Train = TrainSet(Images.train_set, extension="tif", transform=None)
Test = TestSet(Images.test_set, extension="tif", transform=None)
for i in range(len(Train)):
print(Train[i].size())
TrainLoder = DataLoader(Train, batch_size=4)
for i, data in enumerate(TrainLoder, 0):
inputs, labels = data["Image"], data["Label"]
print(inputs.size())
15 changes: 9 additions & 6 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import torch
from torchvision import transforms

transforms_compose= transforms.Compose([transforms.ToTensor()])
transforms_compose = transforms.Compose([transforms.ToTensor()])
params = hyperparameters(train_percentage=0.6, batch_size=1, epoch=4)
if torch.cuda.is_available():
net= UNeT(n_class=1).cuda()
net = UNeT(n_class=1).cuda()
else:
net= net= UNeT(n_class=1)
net = net = UNeT(n_class=1)

IMAGE_DIR = "/Users/madhav/DataSets/AerialImageDataset/train/images/*.tif"
ANNOTATIONS_DIR = "/Users/madhav/DataSets/AerialImageDataset/train/gt/*.tif"
Expand All @@ -23,7 +23,10 @@
Annotations=ANNOTATIONS_DIR,
train_percentage=0.7)
loss_val = Loss()
Train = TrainSet(Images.train_set, extension="tif", transform=transforms_compose)
Train = TrainSet(
Images.train_set,
extension="tif",
transform=transforms_compose)
Test = TestSet(Images.test_set, extension="tif", transform=None)
TrainLoder = DataLoader(
Train,
Expand All @@ -39,7 +42,7 @@
metrics = defaultdict()
running_loss = 0.0
for i, data in enumerate(TrainLoder, 0):
inputs, labels= data["Image"], data["Label"]
inputs, labels = data["Image"], data["Label"]
#inputs, labels= inputs.permute(0, 3, 1, 2), labels.permute(0, 3, 1, 2)
optimizer.zero_grad()
outputs = net(inputs)
Expand All @@ -48,7 +51,7 @@
optimizer.step()
running_loss += loss.item()
print('[%d, %5d] loss: %.3f' %
(epoch + 1, i + 1, running_loss / 2000))
(epoch + 1, i + 1, running_loss / 2000))
running_loss = 0.

print('Finished Training')

0 comments on commit d4ce554

Please sign in to comment.