使用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])
    
相关推荐
Python大数据分析@27 分钟前
python操作CSV和excel,如何来做?
开发语言·python·excel
黑叶白树28 分钟前
简单的签到程序 python笔记
笔记·python
北京搜维尔科技有限公司29 分钟前
搜维尔科技:【应用】Xsens在荷兰车辆管理局人体工程学评估中的应用
人工智能·安全
说私域32 分钟前
基于开源 AI 智能名片 S2B2C 商城小程序的视频号交易小程序优化研究
人工智能·小程序·零售
YRr YRr32 分钟前
深度学习:Transformer Decoder详解
人工智能·深度学习·transformer
知来者逆37 分钟前
研究大语言模型在心理保健智能顾问的有效性和挑战
人工智能·神经网络·机器学习·语言模型·自然语言处理
Shy96041842 分钟前
Bert完形填空
python·深度学习·bert
云起无垠1 小时前
技术分享 | 大语言模型赋能软件测试:开启智能软件安全新时代
人工智能·安全·语言模型
上海_彭彭1 小时前
【提效工具开发】Python功能模块执行和 SQL 执行 需求整理
开发语言·python·sql·测试工具·element
老艾的AI世界1 小时前
新一代AI换脸更自然,DeepLiveCam下载介绍(可直播)
图像处理·人工智能·深度学习·神经网络·目标检测·机器学习·ai换脸·视频换脸·直播换脸·图片换脸