forked from tinygrad/tinygrad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* conv3d, needs test * test passes, padding wrong on unet * unet3d * no conv3d on images
- Loading branch information
Showing
7 changed files
with
57 additions
and
29 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
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 |
---|---|---|
@@ -1,31 +1,42 @@ | ||
# https://github.com/wolny/pytorch-3dunet | ||
from pathlib import Path | ||
from extra.utils import download_file, fake_torch_load | ||
from extra.utils import download_file, fake_torch_load, get_child | ||
import tinygrad.nn as nn | ||
|
||
class SingleConv: | ||
def __init__(self, in_channels, out_channels): | ||
self.groupnorm = nn.GroupNorm(1, in_channels) # 1 group? | ||
self.conv = nn.Conv2d(in_channels, out_channels, (3,3,3), bias=False) | ||
# TODO: make 2D conv generic for 3D, might already work with kernel_size=(3,3,3) | ||
self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=(3,3,3), padding=(1,1,1,1,1,1), bias=False) | ||
def __call__(self, x): | ||
return self.conv(self.groupnorm(x)).relu() | ||
|
||
def get_basic_module(c0, c1, c2): return {"SingleConv1": SingleConv(c0, c1), "SingleConv2": SingleConv(c1, c2)} | ||
class BasicModule: | ||
def __init__(self, c0, c1, c2): | ||
self.basic_module = {"SingleConv1": SingleConv(c0, c1), "SingleConv2": SingleConv(c1, c2)} | ||
def __call__(self, x): | ||
return self.basic_module['SingleConv2'](self.basic_module['SingleConv1'](x)) | ||
|
||
class UNet3D: | ||
def __init__(self): | ||
ups = [16,32,64,128,256] | ||
self.encoders = [get_basic_module(ups[i] if i != 0 else 1, ups[i], ups[i+1]) for i in range(4)] | ||
self.decoders = [get_basic_module(ups[-1-i] + ups[-2+i], ups[-2+i], ups[-2+i]) for i in range(3)] | ||
self.encoders = [BasicModule(ups[i] if i != 0 else 1, ups[i], ups[i+1]) for i in range(4)] | ||
self.decoders = [BasicModule(ups[-1-i] + ups[-2-i], ups[-2-i], ups[-2-i]) for i in range(3)] | ||
self.final_conv = nn.Conv2d(32, 1, (1,1,1)) | ||
|
||
def __call__(self, x): | ||
# TODO: make 2D conv generic for 3D, might already work with kernel_size=(3,3,3) | ||
pass | ||
intermediates = [x] | ||
for e in self.encoders: intermediates.append(e(intermediates[-1])) | ||
ret = intermediates[-1] | ||
for d,i in zip(self.decoders, intermediates[:-1][::-1]): ret = d(ret.cat(i, dim=1)) | ||
return ret | ||
|
||
def load_from_pretrained(self): | ||
fn = Path(__file__).parent.parent / "weights/unet-3d.ckpt" | ||
download_file("https://oc.embl.de/index.php/s/61s67Mg5VQy7dh9/download?path=%2FLateral-Root-Primordia%2Funet_bce_dice_ds1x&files=best_checkpoint.pytorch", fn) | ||
state = fake_torch_load(open(fn, "rb").read())['model_state_dict'] | ||
for x in state.keys(): | ||
print(x, state[x].shape) | ||
state_dict = fake_torch_load(open(fn, "rb").read())['model_state_dict'] | ||
for k, v in state_dict.items(): | ||
print(k, v.shape) | ||
obj = get_child(self, k) | ||
assert obj.shape == v.shape, (k, obj.shape, v.shape) | ||
obj.assign(v.numpy()) |
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
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