David310/Detect_AI-generated_Image
4
1# Copyright (c) 2019, Adobe Inc. All rights reserved.2#3# This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike4# 4.0 International Public License. To view a copy of this license, visit5# https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode.6 7import torch8import torch.nn.parallel9import numpy as np10import torch.nn as nn11import torch.nn.functional as F12from IPython import embed13 14class Downsample(nn.Module):15 def __init__(self, pad_type='reflect', filt_size=3, stride=2, channels=None, pad_off=0):16 super(Downsample, self).__init__()17 self.filt_size = filt_size18 self.pad_off = pad_off19 self.pad_sizes = [int(1.*(filt_size-1)/2), int(np.ceil(1.*(filt_size-1)/2)), int(1.*(filt_size-1)/2), int(np.ceil(1.*(filt_size-1)/2))]20 self.pad_sizes = [pad_size+pad_off for pad_size in self.pad_sizes]21 self.stride = stride22 self.off = int((self.stride-1)/2.)23 self.channels = channels24 25 # print('Filter size [%i]'%filt_size)26 if(self.filt_size==1):27 a = np.array([1.,])28 elif(self.filt_size==2):29 a = np.array([1., 1.])30 elif(self.filt_size==3):31 a = np.array([1., 2., 1.])32 elif(self.filt_size==4): 33 a = np.array([1., 3., 3., 1.])34 elif(self.filt_size==5): 35 a = np.array([1., 4., 6., 4., 1.])36 elif(self.filt_size==6): 37 a = np.array([1., 5., 10., 10., 5., 1.])38 elif(self.filt_size==7): 39 a = np.array([1., 6., 15., 20., 15., 6., 1.])40 41 filt = torch.Tensor(a[:,None]*a[None,:])42 filt = filt/torch.sum(filt)43 self.register_buffer('filt', filt[None,None,:,:].repeat((self.channels,1,1,1)))44 45 self.pad = get_pad_layer(pad_type)(self.pad_sizes)46 47 def forward(self, inp):48 if(self.filt_size==1):49 if(self.pad_off==0):50 return inp[:,:,::self.stride,::self.stride] 51 else:52 return self.pad(inp)[:,:,::self.stride,::self.stride]53 else:54 return F.conv2d(self.pad(inp), self.filt, stride=self.stride, groups=inp.shape[1])55 56def get_pad_layer(pad_type):57 if(pad_type in ['refl','reflect']):58 PadLayer = nn.ReflectionPad2d59 elif(pad_type in ['repl','replicate']):60 PadLayer = nn.ReplicationPad2d61 elif(pad_type=='zero'):62 PadLayer = nn.ZeroPad2d63 else:64 print('Pad type [%s] not recognized'%pad_type)65 return PadLayer66 67 68class Downsample1D(nn.Module):69 def __init__(self, pad_type='reflect', filt_size=3, stride=2, channels=None, pad_off=0):70 super(Downsample1D, self).__init__()71 self.filt_size = filt_size72 self.pad_off = pad_off73 self.pad_sizes = [int(1. * (filt_size - 1) / 2), int(np.ceil(1. * (filt_size - 1) / 2))]74 self.pad_sizes = [pad_size + pad_off for pad_size in self.pad_sizes]75 self.stride = stride76 self.off = int((self.stride - 1) / 2.)77 self.channels = channels78 79 # print('Filter size [%i]' % filt_size)80 if(self.filt_size == 1):81 a = np.array([1., ])82 elif(self.filt_size == 2):83 a = np.array([1., 1.])84 elif(self.filt_size == 3):85 a = np.array([1., 2., 1.])86 elif(self.filt_size == 4):87 a = np.array([1., 3., 3., 1.])88 elif(self.filt_size == 5):89 a = np.array([1., 4., 6., 4., 1.])90 elif(self.filt_size == 6):91 a = np.array([1., 5., 10., 10., 5., 1.])92 elif(self.filt_size == 7):93 a = np.array([1., 6., 15., 20., 15., 6., 1.])94 95 filt = torch.Tensor(a)96 filt = filt / torch.sum(filt)97 self.register_buffer('filt', filt[None, None, :].repeat((self.channels, 1, 1)))98 99 self.pad = get_pad_layer_1d(pad_type)(self.pad_sizes)100 101 def forward(self, inp):102 if(self.filt_size == 1):103 if(self.pad_off == 0):104 return inp[:, :, ::self.stride]105 else:106 return self.pad(inp)[:, :, ::self.stride]107 else:108 return F.conv1d(self.pad(inp), self.filt, stride=self.stride, groups=inp.shape[1])109 110 111def get_pad_layer_1d(pad_type):112 if(pad_type in ['refl', 'reflect']):113 PadLayer = nn.ReflectionPad1d114 elif(pad_type in ['repl', 'replicate']):115 PadLayer = nn.ReplicationPad1d116 elif(pad_type == 'zero'):117 PadLayer = nn.ZeroPad1d118 else:119 print('Pad type [%s] not recognized' % pad_type)120 return PadLayer121 