102201

部署运行你感兴趣的模型镜像
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
import copy
from einops import rearrange


class SinusoidalPosEmb(nn.Module):
    def __init__(self, dim):
        super().__init__()
        self.dim = dim

    def forward(self, x):
        device = x.device
        half_dim = self.dim // 2
        emb = math.log(10000) / (half_dim - 1)
        emb = torch.exp(torch.arange(half_dim, device=device) * -emb)
        emb = x[:, None] * emb[None, :]
        emb = torch.cat((emb.sin(), emb.cos()), dim=-1)
        return emb


class single_conv(nn.Module):
    def __init__(self, in_ch, out_ch):
        super(single_conv, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(in_ch, out_ch, 3, padding=1),
            nn.ReLU(inplace=True)
        )

    def forward(self, x):
        return self.conv(x)


class up(nn.Module):
    def __init__(self, in_ch):
        super(up, self).__init__()
        self.up = nn.ConvTranspose2d(in_ch, in_ch // 2, 2, stride=2)
        self.conv_concat = nn.Conv2d(in_ch, in_ch // 2, 3, 1, 1)
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x1, x2):
        x1 = self.up(x1)

        # input is CHW
        diffY = x2.size()[2] - x1.size()[2]
        diffX = x2.size()[3] - x1.size()[3]

        x1 = F.pad(x1, (diffX // 2, diffX - diffX // 2,
                        diffY // 2, diffY - diffY // 2))

        x = torch.cat((x1, x2), dim=1)
        x = self.conv_concat(x)
        x = self.relu(x)
        return x


# diff
class down(nn.Module):
    def __init__(self, in_channels, dilation_rates=(1, 2, 3)):
        super(down, self).__init__()
        self.conv = nn.ModuleList([
            nn.Sequential(
                nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=2, padding=d, dilation=d, bias=False),
                nn.ReLU(inplace=True)
            )
            for d in dilation_rates
        ])
        self.conv_2 = nn.Conv2d(in_channels*3, in_channels, 3, 1, 1)
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        outputs = [conv(x) for conv in self.conv]
        out = torch.cat(outputs, dim=1)
        out = self.conv_2(out)
        out = self.relu(out)
        return out


class outconv(nn.Module):
    def __init__(self, in_ch, out_ch):
        super(outconv, self).__init__()
        self.conv = nn.Conv2d(in_ch, out_ch, 1)

    def forward(self, x):
        x = self.conv(x)
        return x


class adjust_net(nn.Module):
    def __init__(self, out_channels=64, middle_channels=32):
        super(adjust_net, self).__init__()

        self.model = nn.Sequential(
            nn.Conv2d(2, middle_channels, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.AvgPool2d(2),

            nn.Conv2d(middle_channels, middle_channels * 2, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.AvgPool2d(2),

            nn.Conv2d(middle_channels * 2, middle_channels * 4, 3, padding=1),
            nn.ReLU(inplace=True),
            nn.AvgPool2d(2),

            nn.Conv2d(middle_channels * 4, out_channels * 2, 1, padding=0)
        )

    def forward(self, x):
        out = self.model(x)
        out = F.adaptive_avg_pool2d(out, (1, 1))
        out1 = out[:, :out.shape[1] // 2]
        out2 = out[:, out.shape[1] // 2:]
        return out1, out2


# The architecture of U-Net refers to "Toward Convolutional Blind Denoising of Real Photographs",
# official MATLAB implementation: https://github.com/GuoShi28/CBDNet.
# unofficial PyTorch implementation: https://github.com/IDKiro/CBDNet-pytorch/tree/master.
# We improved it by adding time step embedding and EMM module, while removing the noise estimation network.
class UNet(nn.Module):
    def __init__(self, in_channels=2, out_channels=1):
        super(UNet, self).__init__()

        dim = 32
        self.time_mlp = nn.Sequential(
            SinusoidalPosEmb(dim),
            nn.Linear(dim, dim * 4),
            nn.GELU(),
            nn.Linear(dim * 4, dim)
        )

        self.inc = nn.Sequential(
            single_conv(in_channels, 64),
            single_conv(64, 64)
        )

        self.down1 = down(64)
        self.mlp1 = nn.Sequential(
            nn.GELU(),
            nn.Linear(dim, 64)
        )
        self.adjust1 = adjust_net(64)
        self.conv1 = nn.Sequential(
            single_conv(64, 128),
            single_conv(128, 128),
            single_conv(128, 128)
        )

        self.down2 = down(128)
        self.mlp2 = nn.Sequential(
            nn.GELU(),
            nn.Linear(dim, 128)
        )
        self.adjust2 = adjust_net(128)
        self.conv2 = nn.Sequential(
            single_conv(128, 256),
            single_conv(256, 256),
            single_conv(256, 256),
            single_conv(256, 256),
            single_conv(256, 256),
            single_conv(256, 256)
        )

        self.up1 = up(256)
        self.mlp3 = nn.Sequential(
            nn.GELU(),
            nn.Linear(dim, 128)
        )
        self.adjust3 = adjust_net(128)
        self.conv3 = nn.Sequential(
            single_conv(128, 128),
            single_conv(128, 128),
            single_conv(128, 128)
        )

        self.up2 = up(128)
        self.mlp4 = nn.Sequential(
            nn.GELU(),
            nn.Linear(dim, 64)
        )
        self.adjust4 = adjust_net(64)
        self.conv4 = nn.Sequential(
            single_conv(64, 64),
            single_conv(64, 64)
        )

        self.outc = outconv(64, out_channels)

    def forward(self, x, t, x_adjust, adjust):
        inx = self.inc(x)
        time_emb = self.time_mlp(t)
        down1 = self.down1(inx)
        condition1 = self.mlp1(time_emb)
        b, c = condition1.shape
        condition1 = rearrange(condition1, 'b c -> b c 1 1')
        if adjust:
            gamma1, beta1 = self.adjust1(x_adjust)
            down1 = down1 + gamma1 * condition1 + beta1
        else:
            down1 = down1 + condition1
        conv1 = self.conv1(down1)

        down2 = self.down2(conv1)
        condition2 = self.mlp2(time_emb)
        b, c = condition2.shape
        condition2 = rearrange(condition2, 'b c -> b c 1 1')
        if adjust:
            gamma2, beta2 = self.adjust2(x_adjust)
            down2 = down2 + gamma2 * condition2 + beta2
        else:
            down2 = down2 + condition2
        conv2 = self.conv2(down2)

        up1 = self.up1(conv2, conv1)
        condition3 = self.mlp3(time_emb)
        b, c = condition3.shape
        condition3 = rearrange(condition3, 'b c -> b c 1 1')
        if adjust:
            gamma3, beta3 = self.adjust3(x_adjust)
            up1 = up1 + gamma3 * condition3 + beta3
        else:
            up1 = up1 + condition3
        conv3 = self.conv3(up1)

        up2 = self.up2(conv3, inx)
        condition4 = self.mlp4(time_emb)
        b, c = condition4.shape
        condition4 = rearrange(condition4, 'b c -> b c 1 1')
        if adjust:
            gamma4, beta4 = self.adjust4(x_adjust)
            up2 = up2 + gamma4 * condition4 + beta4
        else:
            up2 = up2 + condition4
        conv4 = self.conv4(up2)

        out = self.outc(conv4)
        return out


class Network(nn.Module):
    def __init__(self, in_channels=3, out_channels=1, context=True):
        super(Network, self).__init__()
        self.unet = UNet(in_channels=in_channels, out_channels=out_channels)
        self.context = context

    def forward(self, x, t, y, x_end, adjust=True):
        if self.context:
            x_middle = x[:, 1].unsqueeze(1)
        else:
            x_middle = x

        x_adjust = torch.cat((y, x_end), dim=1)
        out = self.unet(x, t, x_adjust, adjust=adjust) + x_middle

        return out


# WeightNet of the one-shot learning framework
class WeightNet(nn.Module):
    def __init__(self, weight_num=10):
        super(WeightNet, self).__init__()
        init = torch.ones([1, weight_num, 1, 1]) / weight_num
        self.weights = nn.Parameter(init)

    def forward(self, x):
        weights = F.softmax(self.weights, 1)
        out = weights * x
        out = out.sum(dim=1, keepdim=True)

        return out, weights

您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

数据完整性约束 ①创建一个员工奖金发放表pr (lD,je),其中pr表中 ID 为主键,其值必须是 Employees 表员工编号列中已有的员工号,并且当删除和修改 Employees 表中员工编号列时,在pr 表员工编号列中的数据也要随之变化。 创建pr表,建立相关的完整性约束。 -- phpMyAdmin SQL Dump -- version 2.10.2 -- http://www.phpmyadmin.net -- -- 主机: localhost -- 生成日期: 2013 年 08 月 26 日 04:43 -- 服务器版本: 5.0.45 -- PHP 版本: 5.2.3 SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; -- -- 数据库: `yggl` -- CREATE DATABASE `yggl` DEFAULT CHARACTER SET gb2312 COLLATE gb2312_chinese_ci; USE `yggl`; -- -------------------------------------------------------- -- -- 表的结构 `departments` -- CREATE TABLE `departments` ( `部门编号` char(3) NOT NULL COMMENT '部门编号', `部门名称` char(20) NOT NULL COMMENT '部门名', `备注` text COMMENT '备注', PRIMARY KEY (`部门编号`) ) ENGINE=MyISAM DEFAULT CHARSET=gb2312; -- -- 导出表中的数据 `departments` -- INSERT INTO `departments` VALUES ('1', '财务部', NULL); INSERT INTO `departments` VALUES ('2', '人力资源部', NULL); INSERT INTO `departments` VALUES ('3', '经理办公室', NULL); INSERT INTO `departments` VALUES ('4', '研发部', NULL); INSERT INTO `departments` VALUES ('5', '市场部', NULL); -- -------------------------------------------------------- -- -- 表的结构 `employees` -- CREATE TABLE `employees` ( `员工编号` char(6) NOT NULL, `姓名` char(10) NOT NULL, `学历` char(4) NOT NULL, `出生日期` date NOT NULL, `性别` char(2) NOT NULL, `工作年限` tinyint(1) default NULL, `地址` varchar(20) default NULL, `电话号码` char(12) default NULL, `员工部门号` char(3) NOT NULL, PRIMARY KEY (`员工编号`) ) ENGINE=MyISAM DEFAULT CHARSET=gb2312; -- -- 导出表中的数据 `employees` -- INSERT INTO `employees` VALUES ('000001', '王林', '大专', '1966-01-23', '1', 8, '中山路32-1-508', '83355668', '2'); INSERT INTO `employees` VALUES ('010008', '伍容华', '本科', '1976-03-28', '1', 3, '北京东路100-2', '83321321', '1'); INSERT INTO `employees` VALUES ('020010', '王向容', '硕士', '1982-12-09', '1', 2, '四牌楼10-0-108', '83792361', '1'); INSERT INTO `employees` VALUES ('020018', '李丽', '大专', '1960-07-30', '0', 6, '中山东路102-2', '83413301', '1'); INSERT INTO `employees` VALUES ('102201', '刘明', '本科', '1972-10-18', '1', 3, '虎距路100-2', '83606608', '5'); INSERT INTO `employees` VALUES ('102208', '朱俊', '硕士', '1965-09-28', '1', 2, '牌楼巷5-3-106', '84708817', '5'); INSERT INTO `employees` VALUES ('108991', '钟敏', '硕士', '1979-08-10', '0', 4, '中山路10-3-105', '83346722', '3'); INSERT INTO `employees` VALUES ('111006', '张石兵', '本科', '1974-10-01', '1', 1, '解放路34-1-203', '84563418', '5'); INSERT INTO `employees` VALUES ('210678', '林涛', '大专', '1977-04-02', '1', 2, '中山北路24-35', '83467336', '3'); INSERT INTO `employees` VALUES ('302566', '李玉珉', '本科', '1968-09-20', '1', 3, '热和路209-3', '58765991', '4'); INSERT INTO `employees` VALUES ('308759', '叶凡', '本科', '1978-11-18', '1', 2, '北京西路3-7-52', '83308901', '4'); INSERT INTO `employees` VALUES ('504209', '陈林琳', '大专', '1969-09-03', '0', 5, '汉中路120-4-12', '84468158', '4'); -- -------------------------------------------------------- -- -- 表的结构 `salary` -- CREATE TABLE `salary` ( `员工编号` char(6) NOT NULL COMMENT '员工编号', `收入` float NOT NULL COMMENT '收入', `支出` float NOT NULL COMMENT '支出', PRIMARY KEY (`员工编号`) ) ENGINE=MyISAM DEFAULT CHARSET=gb2312; -- -- 导出表中的数据 `salary` -- INSERT INTO `salary` VALUES ('000001', 2100.8, 123.09); INSERT INTO `salary` VALUES ('010008', 1582.62, 88.03); INSERT INTO `salary` VALUES ('020010', 2860, 198); INSERT INTO `salary` VALUES ('020018', 2347.68, 180); INSERT INTO `salary` VALUES ('102201', 2569.88, 185.65); INSERT INTO `salary` VALUES ('102208', 1980, 100); INSERT INTO `salary` VALUES ('108991', 3259.98, 281.52); INSERT INTO `salary` VALUES ('111006', 1987.01, 79.58); INSERT INTO `salary` VALUES ('210678', 2240, 121); INSERT INTO `salary` VALUES ('302566', 2980.7, 210.2); INSERT INTO `salary` VALUES ('308759', 2531.98, 199.08); INSERT INTO `salary` VALUES ('504209', 2066.15, 108); 根据数据库,回答完整性约束的题
12-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值