Skip to content

Commit 9a6ee84

Browse files
committed
Fix torchhub
1 parent 7679965 commit 9a6ee84

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,16 @@ You can specify which model file to use with `--model MODEL.pth`.
155155
The training progress can be visualized in real-time using [Weights & Biases](https://wandb.ai/). Loss curves, validation curves, weights and gradient histograms, as well as predicted masks are logged to the platform.
156156

157157
When launching a training, a link will be printed in the console. Click on it to go to your dashboard. If you have an existing W&B account, you can link it
158-
by setting the `WANDB_API_KEY` environment variable.
158+
by setting the `WANDB_API_KEY` environment variable. If not, it will create an anonymous run which is automatically deleted after 7 days.
159159

160160

161161
## Pretrained model
162-
A [pretrained model](https://github.com/milesial/Pytorch-UNet/releases/tag/v2.0) is available for the Carvana dataset. It can also be loaded from torch.hub:
162+
A [pretrained model](https://github.com/milesial/Pytorch-UNet/releases/tag/v3.0) is available for the Carvana dataset. It can also be loaded from torch.hub:
163163

164164
```python
165-
net = torch.hub.load('milesial/Pytorch-UNet', 'unet_carvana', pretrained=True)
165+
net = torch.hub.load('milesial/Pytorch-UNet', 'unet_carvana', pretrained=True, scale=0.5)
166166
```
167-
The training was done with a 50% scale and bilinear upsampling.
167+
Available scales are 0.5 and 1.0.
168168

169169
## Data
170170
The Carvana data is available on the [Kaggle website](https://www.kaggle.com/c/carvana-image-masking-challenge/data).

hubconf.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
import torch
22
from unet import UNet as _UNet
33

4-
def unet_carvana(pretrained=False):
4+
def unet_carvana(pretrained=False, scale=0.5):
55
"""
66
UNet model trained on the Carvana dataset ( https://www.kaggle.com/c/carvana-image-masking-challenge/data ).
77
Set the scale to 0.5 (50%) when predicting.
88
"""
9-
net = _UNet(n_channels=3, n_classes=2, bilinear=True)
9+
net = _UNet(n_channels=3, n_classes=2, bilinear=False)
1010
if pretrained:
11-
checkpoint = 'https://github.com/milesial/Pytorch-UNet/releases/download/v2.0/unet_carvana_scale0.5_epoch1.pth'
11+
if scale == 0.5:
12+
checkpoint = 'https://github.com/milesial/Pytorch-UNet/releases/download/v3.0/unet_carvana_scale0.5_epoch2.pth'
13+
elif scale == 1.0:
14+
checkpoint = 'https://github.com/milesial/Pytorch-UNet/releases/download/v3.0/unet_carvana_scale1.0_epoch2.pth'
15+
else:
16+
raise RuntimeError('Only 0.5 and 1.0 scales are available')
17+
1218
net.load_state_dict(torch.hub.load_state_dict_from_url(checkpoint, progress=True))
1319

1420
return net

predict.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def get_args():
5757
help='Minimum probability value to consider a mask pixel white')
5858
parser.add_argument('--scale', '-s', type=float, default=0.5,
5959
help='Scale factor for the input images')
60+
parser.add_argument('--bilinear', action='store_true', default=False, help='Use bilinear upsampling')
6061

6162
return parser.parse_args()
6263

@@ -81,7 +82,7 @@ def mask_to_image(mask: np.ndarray):
8182
in_files = args.input
8283
out_files = get_output_filenames(args)
8384

84-
net = UNet(n_channels=3, n_classes=2)
85+
net = UNet(n_channels=3, n_classes=2, bilinear=args.bilinear)
8586

8687
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
8788
logging.info(f'Loading model {args.model}')

train.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def train_net(net,
2525
device,
2626
epochs: int = 5,
2727
batch_size: int = 1,
28-
learning_rate: float = 0.001,
28+
learning_rate: float = 1e-5,
2929
val_percent: float = 0.1,
3030
save_checkpoint: bool = True,
3131
img_scale: float = 0.5,
@@ -147,13 +147,14 @@ def get_args():
147147
parser = argparse.ArgumentParser(description='Train the UNet on images and target masks')
148148
parser.add_argument('--epochs', '-e', metavar='E', type=int, default=5, help='Number of epochs')
149149
parser.add_argument('--batch-size', '-b', dest='batch_size', metavar='B', type=int, default=1, help='Batch size')
150-
parser.add_argument('--learning-rate', '-l', metavar='LR', type=float, default=0.00001,
150+
parser.add_argument('--learning-rate', '-l', metavar='LR', type=float, default=1e-5,
151151
help='Learning rate', dest='lr')
152152
parser.add_argument('--load', '-f', type=str, default=False, help='Load model from a .pth file')
153153
parser.add_argument('--scale', '-s', type=float, default=0.5, help='Downscaling factor of the images')
154154
parser.add_argument('--validation', '-v', dest='val', type=float, default=10.0,
155155
help='Percent of the data that is used as validation (0-100)')
156156
parser.add_argument('--amp', action='store_true', default=False, help='Use mixed precision')
157+
parser.add_argument('--bilinear', action='store_true', default=False, help='Use bilinear upsampling')
157158

158159
return parser.parse_args()
159160

@@ -168,7 +169,7 @@ def get_args():
168169
# Change here to adapt to your data
169170
# n_channels=3 for RGB images
170171
# n_classes is the number of probabilities you want to get per pixel
171-
net = UNet(n_channels=3, n_classes=2, bilinear=True)
172+
net = UNet(n_channels=3, n_classes=2, bilinear=args.bilinear)
172173

173174
logging.info(f'Network:\n'
174175
f'\t{net.n_channels} input channels\n'

unet/unet_model.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
class UNet(nn.Module):
7-
def __init__(self, n_channels, n_classes, bilinear=True):
7+
def __init__(self, n_channels, n_classes, bilinear=False):
88
super(UNet, self).__init__()
99
self.n_channels = n_channels
1010
self.n_classes = n_classes

0 commit comments

Comments
 (0)