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
class RotaryEmbedding(keras.layers.Layer): | |
def __init__(self, hidden_dim): | |
super().__init__() | |
self.hidden_dim = hidden_dim | |
def build(self, input_shape): | |
super().build(input_shape) | |
self.inverse_freq = self.add_weight( | |
"inverse_freq", shape=(self.hidden_dim // 2,), dtype=tf.float32 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
function (u::UNet)(x::AbstractArray) | |
enc_out = [] | |
out = x | |
for i in 1:4 | |
out = u.encoder[i](out) | |
push!(enc_out, out) | |
out = u.pool[i](out) | |
end |
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
def forward(self, x): | |
enc1 = self.encoder1(x) | |
enc2 = self.encoder2(self.pool1(enc1)) | |
enc3 = self.encoder3(self.pool2(enc2)) | |
enc4 = self.encoder4(self.pool3(enc3)) | |
bottleneck = self.bottleneck(self.pool4(enc4)) | |
dec4 = self.upconv4(bottleneck) | |
dec4 = torch.cat((dec4, enc4), dim=1) |
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
function upconv_block(in_chs::Int, out_chs::Int, kernel = (2, 2)) | |
return ConvTranspose(kernel, in_chs => out_chs; stride = (2, 2), init = _random_normal) | |
end | |
function UNet(in_channels::Integer = 3, inplanes::Integer = 32, | |
outplanes::Integer = inplanes) | |
features = inplanes | |
encoder_layers = [] | |
append!(encoder_layers, [unet_block(in_channels, features)]) |
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
struct UNet | |
encoder::Any | |
decoder::Any | |
upconv::Any | |
pool::Any | |
bottleneck::Any | |
final_conv::Any | |
end | |
@functor UNet |
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
function unet_block(in_chs::Int, out_chs::Int, kernel = (3, 3)) | |
return Chain( | |
conv1 = Conv(kernel, in_chs => out_chs; pad = (1, 1), | |
init = _random_normal), | |
norm1 = BatchNorm(out_chs, relu), | |
conv2 = Conv(kernel, out_chs => out_chs; pad = (1, 1), | |
init = _random_normal), | |
norm2 = BatchNorm(out_chs, relu) | |
) | |
end |
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
@staticmethod | |
def _block(in_channels, features, name): | |
return nn.Sequential( | |
OrderedDict( | |
[ | |
( | |
name + "conv1", | |
nn.Conv2d( | |
in_channels=in_channels, | |
out_channels=features, |
NewerOlder