使用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])
    
相关推荐
机智的叉烧9 分钟前
前沿重器[57] | sigir24:大模型推荐系统的文本ID对齐学习
人工智能·学习·机器学习
凳子花❀12 分钟前
强化学习与深度学习以及相关芯片之间的区别
人工智能·深度学习·神经网络·ai·强化学习
泰迪智能科技012 小时前
高校深度学习视觉应用平台产品介绍
人工智能·深度学习
盛派网络小助手2 小时前
微信 SDK 更新 Sample,NCF 文档和模板更新,更多更新日志,欢迎解锁
开发语言·人工智能·后端·架构·c#
算法小白(真小白)3 小时前
低代码软件搭建自学第二天——构建拖拽功能
python·低代码·pyqt
唐小旭3 小时前
服务器建立-错误:pyenv环境建立后python版本不对
运维·服务器·python
007php0073 小时前
Go语言zero项目部署后启动失败问题分析与解决
java·服务器·网络·python·golang·php·ai编程
Eric.Lee20213 小时前
Paddle OCR 中英文检测识别 - python 实现
人工智能·opencv·计算机视觉·ocr检测
cd_farsight3 小时前
nlp初学者怎么入门?需要学习哪些?
人工智能·自然语言处理
AI明说3 小时前
评估大语言模型在药物基因组学问答任务中的表现:PGxQA
人工智能·语言模型·自然语言处理·数智药师·数智药学