IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> Patch-GAN (Discriminative Network) 的Pytorch实现 -> 正文阅读

[人工智能]Patch-GAN (Discriminative Network) 的Pytorch实现

Patch-GAN (Discriminative Network)

Patch-GAN可以在对抗学习中被用作discriminator,参考Markovian Generative Adversarial Networks结构中的Discriminative network部分(图1)进行Pytorch代码实现。网络结构的具体细节可参考下述文献:

[1] Li, C. , and M. Wand . “Precomputed Real-Time Texture Synthesis with Markovian Generative Adversarial Networks.” European Conference on Computer Vision Springer, Cham, 2016.

在这里插入图片描述
图1 Markovian GAN

Pytorch代码实现

Discriminative Network

class Discriminative_Net(nn.Module):
    def __init__(self, in_channel, out_channel, kernel_size=4, padding=1, bias=True, stride=2, in_features=128*4*4, out_features=1):
        super().__init__()
        self.in_channel  = in_channel
        self.out_channel = out_channel
        self.kernel_size = kernel_size
        self.stride      = stride
        self.padding     = padding
        self.bias        = bias
        self.in_features = in_features
        self.out_features = out_features

        self.Discriminator = Discriminator(in_channel=in_channel, out_channel=out_channel, stride=stride, 
                                            in_features=in_features, out_features=out_features, kernel_size=kernel_size, padding=padding)
    def forward(self, input): # input: 2*1*512*512
        vgg19_relu3_1_net = vgg19_bn(layer_name='Relu3_1')
        vgg19_relu3_1_net = vgg19_relu3_1_net.cuda()
        vgg19_relu3_1 = vgg19_relu3_1_net(input, layer_name='Relu3_1') # 2*256*128*128
        patches = Neural_Patches(vgg19_relu3_1, patches_size=8, stride=4) # list [2*256*8*8,...,2*256*8*8] list中有31*31=961个元素
        patches = torch.cat(patches,0) # 所有8*8的patches在batchsize维度上进行合并  961*256*8*8
        # Sobel
        patches_sobel = sobel_conv2d(patches, patches.shape[0], patches.shape[1]) # 961*256*8*8
        patches = torch.cat((patches,patches_sobel), 1)  # 961*512*8*8
        classification_score_texture = self.Discriminator(patches) # 961*1
        return classification_score_texture

Sobel卷积

def sobel_conv2d(im, batchSize, channelNum):
    sobel_kernel_h = np.array([[1,2,1], [0,0,0], [-1,-2,-1]], dtype='float32')  # emphasize horizontal edges
    sobel_kernel_h = torch.from_numpy(sobel_kernel_h).type(torch.FloatTensor)
    sobel_kernel_h = sobel_kernel_h.repeat(channelNum,channelNum,1,1)
    weight = Variable(sobel_kernel_h).cuda()
    edge_detect_h = F.conv2d(Variable(im), weight, padding=1) # weight的size相当于kernel的size为3
    sobel_kernel_v = np.array([[1,0,-1], [2,0,-2], [1,0,-1]], dtype='float32')  # emphasize vertical edges
    sobel_kernel_v = torch.from_numpy(sobel_kernel_v).type(torch.FloatTensor)
    sobel_kernel_v = sobel_kernel_v.repeat(channelNum,channelNum,1,1)
    weight = Variable(sobel_kernel_v).cuda()
    edge_detect_v = F.conv2d(Variable(im), weight, padding=1)    
    edge_detect = edge_detect_h + edge_detect_v
    return edge_detect

Neural Patches函数

def Neural_Patches(img, patches_size=8, stride=4):
    patches = []
    times = (img.shape[-1]-(patches_size-stride))/stride
    for row in range(int(times)):
        for col in range(int(times)):
            patches.append(img[:, :, row*(patches_size-stride):patches_size+row*(patches_size-stride), col*(patches_size-stride):patches_size+col*(patches_size-stride)])
    return patches

Discriminator

class Discriminator(nn.Module):
    def __init__(self, in_channel, out_channel, stride, in_features, out_features, kernel_size, padding, bias=True):
        super().__init__()
        self.in_channel  = in_channel
        self.out_channel = out_channel
        self.kernel_size = kernel_size
        self.stride      = stride
        self.padding     = padding
        self.bias        = bias
        self.in_features = in_features
        self.out_features = out_features

        self.add_module('bn',nn.BatchNorm2d(self.out_channel))
        self.add_module('relu',nn.ReLU(inplace=True))
        self.add_module('LeakyRelu',nn.LeakyReLU(inplace=True))
        self.add_module('conv',nn.Conv2d(self.in_channel,self.out_channel,kernel_size=self.kernel_size,
                                         stride=self.stride,padding=self.padding,bias=self.bias))
        self.add_module('FC',nn.Linear(self.in_features, self.out_features))
        self.add_module('sigmoid',nn.Sigmoid()) # 将discriminator的输出控制在[0,1]
    def forward(self, input_patches):
        # input_patches = torch.cat(input_patches,0) # 所有8*8的patches在batchsize维度上进行合并
        out = self.LeakyRelu(input_patches) # 961*512*8*8
        out = self.conv(out) # 961*128*4*4
        out = self.bn(out) # 961*128*4*4
        out = self.relu(out) # 961*128*4*4
        out = self.FC(out.reshape((out.shape[0],out.shape[1]*out.shape[2]*out.shape[3]))) # 961*1
        out = self.sigmoid(out) # 961*1
        return out

vgg19_bn函数的实现见博文:

https://blog.csdn.net/kouwang9779/article/details/118801860

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-07-17 11:54:48  更:2021-07-17 11:57:22 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/25 21:50:32-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码