使用Tensorflow2.x复现uxnet3D

models.py

python 复制代码
from nets.model_layers import UXNETBlock, DownSampleBlock, ResBlock3D
from tensorflow import keras

def uxnet3D(input_shape, num_classes):
    inputs = keras.Input(shape=input_shape)

    # out1
    embeddings = keras.layers.Conv3D(48,3,2,padding="same", name='embedding')(inputs)

    # out2
    x = UXNETBlock("uxblock_1")(embeddings)
    x = DownSampleBlock("down_1")(x)
    out2 = x
    
    # out3
    x = UXNETBlock("uxblock_2")(x)
    x = DownSampleBlock("down_2")(x)
    out3 = x

    # out4
    x = UXNETBlock("uxblock_3")(x)
    x = DownSampleBlock("down_3")(x)
    out4 = x

    # out5
    x = UXNETBlock("uxblock_4")(x)
    x = DownSampleBlock("down_4")(x)
    out5 = x

    out5 = ResBlock3D(out5.shape[-1], "res_block_5")(out5)
    out5_up = keras.layers.UpSampling3D(name='out5_up')(out5)
    ####################################

    out4_dim = out4.shape[-1]
    out4 = ResBlock3D(out4_dim, "res_block_4_1")(out4)
    out4 = keras.layers.Concatenate(name="cat_out4_out5Up")([out4, out5_up])
    out4 = ResBlock3D(out4_dim, "res_block_4_2")(out4)
    out4_up = keras.layers.UpSampling3D(name='out4_up')(out4)
    ####################################

    out3_dim = out3.shape[-1]
    out3 = ResBlock3D(out3_dim, "res_block_3_1")(out3)
    out3 = keras.layers.Concatenate(name="cat_out3_out4Up")([out3, out4_up])
    out3 = ResBlock3D(out3_dim, "res_block_3_2")(out3)
    out3_up = keras.layers.UpSampling3D(name='out3_up')(out3)    
    ####################################

    out2_dim = out2.shape[-1]
    out2 = ResBlock3D(out2_dim, "res_block_2_1")(out2)
    out2 = keras.layers.Concatenate(name="cat_out2_out3Up")([out2, out3_up])
    out2 = ResBlock3D(out2_dim, "res_block_2_2")(out2)
    out2_up = keras.layers.UpSampling3D(name='out2_up')(out2)   
    ####################################

    out1_dim = embeddings.shape[-1]
    out1 = ResBlock3D(out1_dim, "res_block_1_1")(embeddings)
    out1 = keras.layers.Concatenate(name="cat_out1_out2Up")([out1, out2_up])
    out1 = ResBlock3D(out1_dim, "res_block_1_2")(out1)
    out1_up = keras.layers.UpSampling3D(name='out1_up')(out1)   
    ####################################

    out1 = ResBlock3D(inputs.shape[-1], "res_block_0")(inputs)
    out1 = keras.layers.Concatenate(name="cat_out0_out1Up")([out1, out1_up])
    ####################################

    outputs = keras.layers.Conv3D(num_classes, 1, activation='sigmoid', name='outputs')(out1)

    model = keras.Model(inputs, outputs, name='uxnet3D')

    return model



model = uxnet3D(input_shape=[128,128,32,1],num_classes=2)
model.summary()
# print(len(model.layers))
keras.utils.plot_model(model,to_file=f'{model.name}.png',show_shapes=True)

layers.py

python 复制代码
import tensorflow as tf
from tensorflow import keras


class UXNETHalfBlock(keras.layers.Layer):
    def __init__(self, block_name):
        super(UXNETHalfBlock,self).__init__(name=block_name)
        self.block_name = block_name

        self.layer_norm_1 = keras.layers.LayerNormalization()
        self.layer_norm_2 = keras.layers.LayerNormalization()

        self.add1 = keras.layers.Add()
        self.add2 = keras.layers.Add()

    def get_config(self):
        config = super(UXNETHalfBlock, self).get_config()
        config.update(
            {
                "block_name": self.block_name
            }
        )
        return config

    def build(self, input_shape):
        num_filters = input_shape[-1]
        self.depth_wise_conv_1 = keras.layers.Conv3D(num_filters, 7, padding='same')
        self.depth_conv_scale_1 = keras.layers.Conv3D(int(num_filters * 4), 1, padding='same')
        self.depth_conv_scaleBack_1 = keras.layers.Conv3D(num_filters, 1, padding='same')

    
    def call(self, inputs):
        x = self.layer_norm_1(inputs)
        x = self.depth_wise_conv_1(x)

        x1 = self.add1([inputs, x])
        ################################

        x = self.layer_norm_2(x1)
        x = self.depth_conv_scale_1(x)
        x = tf.nn.gelu(x)
        x  = self.depth_conv_scaleBack_1(x)
        x = self.add2([x,x1])
        ################################

        return x


class UXNETBlock(keras.layers.Layer):
    def __init__(self, block_name):
        super(UXNETBlock, self).__init__(name=block_name)

        self.block_name = block_name

        self.block1 = UXNETHalfBlock(block_name=f"{block_name}_part1")
        self.block2 = UXNETHalfBlock(block_name=f"{block_name}_part2")

    def get_config(self):
        config = super(UXNETBlock, self).get_config()
        config.update(
            {
                "block_name": self.block_name
            }
        )
        return config

    def call(self, inputs):
        x = self.block1(inputs)
        x = self.block2(x)

        return x

class DownSampleBlock(keras.layers.Layer):
    def __init__(self, block_name):
        super(DownSampleBlock, self).__init__(name=block_name)

        self.block_name = block_name

    def build(self, input_shape):
        num_filters = int(input_shape[-1] * 2)
        self.conv = keras.layers.Conv3D(num_filters, 3, 2, padding='same')
        self.norm = keras.layers.BatchNormalization()
    
    def get_config(self):
        config = super(DownSampleBlock, self).get_config()
        config.update(
            {
                "block_name": self.block_name
            }
        )
        return config

    def call(self, inputs):
        x = self.conv(inputs)
        x = self.norm(x)
        x = tf.nn.gelu(x)
        return x


class ResBlock3D(keras.layers.Layer): 
    def __init__(self, out_channels, block_name):
        super(ResBlock3D, self).__init__(name=block_name)

        self.add = keras.layers.Add()
        self.out_channels = out_channels
        self.block_name = block_name

    def get_config(self):
        config = super(ResBlock3D,self).get_config()
        config.update(
            {
                "block_name": self.block_name,
                "out_channels": self.out_channels
            }
        )
        return config

    def build(self, input_shape):
        num_filters = input_shape[-1]
        self.conv1 = keras.layers.Conv3D(num_filters, 1, 1, padding='same')
        self.norm1 = keras.layers.BatchNormalization()
        
        self.conv2 = keras.layers.Conv3D(num_filters, 3, 1, padding='same')
        self.norm2 = keras.layers.BatchNormalization()

        self.conv3 = keras.layers.Conv3D(self.out_channels, 1, 1, padding='same')
        self.norm3 = keras.layers.BatchNormalization()

        self.conv4 = keras.layers.Conv3D(self.out_channels, 1, 1, padding='same')
        self.norm4 = keras.layers.BatchNormalization()
    
    def call(self, inputs):
        x = self.conv1(inputs)
        x = self.norm1(x)
        x = tf.nn.gelu(x)

        x = self.conv2(x)
        x = self.norm2(x)
        x = tf.nn.gelu(x)

        x = self.conv3(x)
        x = self.norm3(x)
        x1 = tf.nn.gelu(x)

        x2 = self.conv4(inputs)
        x2= self.norm4(x2)
        x2 = tf.nn.gelu(x2)

        return self.add([x1,x2])
    
相关推荐
搏博4 分钟前
神经网络问题之一:梯度消失(Vanishing Gradient)
人工智能·机器学习
z千鑫4 分钟前
【人工智能】深入理解PyTorch:从0开始完整教程!全文注解
人工智能·pytorch·python·gpt·深度学习·ai编程
YRr YRr12 分钟前
深度学习:神经网络的搭建
人工智能·深度学习·神经网络
威桑15 分钟前
CMake + mingw + opencv
人工智能·opencv·计算机视觉
爱喝热水的呀哈喽18 分钟前
torch张量与函数表达式写法
人工智能·pytorch·深度学习
MessiGo27 分钟前
Python 爬虫 (1)基础 | 基础操作
开发语言·python
肥猪猪爸1 小时前
使用卡尔曼滤波器估计pybullet中的机器人位置
数据结构·人工智能·python·算法·机器人·卡尔曼滤波·pybullet
LZXCyrus1 小时前
【杂记】vLLM如何指定GPU单卡/多卡离线推理
人工智能·经验分享·python·深度学习·语言模型·llm·vllm
Enougme1 小时前
Appium常用的使用方法(一)
python·appium
懷淰メ1 小时前
PyQt飞机大战游戏(附下载地址)
开发语言·python·qt·游戏·pyqt·游戏开发·pyqt5