Esempio n. 1
0
    def forward(self, x):
        x = quant(x)
        x = roundmax(x)
        tmp = Variable(torch.zeros(1, 3, 32, 32).cuda())
        f = fft.Fft2d()
        fft_rout, fft_iout = f(x, tmp)
        mag = torch.sqrt(
            torch.mul(fft_rout, fft_rout) + torch.mul(fft_iout, fft_iout))
        tmp = torch.zeros(1, 1, 32, 32).cuda()
        tmp = torch.add(torch.add(mag[:, 0, :, :], mag[:, 1, :, :]),
                        mag[:, 2, :, :])
        f = open(args.outputfile, 'a+')
        for i in range(0, 32):
            for j in range(0, 32):
                print(tmp[0, i, j].data[0] / 3, file=f)
        f.close()
        '''
		f = open('clean_train_freq.txt','w')
		for i in range(0,32):
			for j in range(0,32):
				print(tmp.data[0,i,j]/3,file = f)
		f.close()
		'''
        #print(mag.size())
        #print(tmp.size())
        #####################################
        # print original image
        '''
		f = open('Fixedimage.txt','a+')
		for i in range(0,32):
			for j in range(0,32):
				tmp2 = torch.add(torch.add(x[0,0,i,j],x[0,1,i,j]),x[0,2,i,j])
				print(tmp2.data[0]/3,file = f, end='\t')
				#print(mag[0,1,i,j].data[0],file = f)
				#print(mag[0,2,i,j].data[0],file = f)
			print('',file=f)
		f.close()
		'''
        #####################################

        #####################################
        # print fft image
        '''
		f = open('Fixedfft.txt','a+')
		for i in range(0,mag.size()[2]):
			for j in range(0,mag.size()[3]):
				print(tmp[0,i,j].data[0]/3,file = f, end='\t')
				#print(mag[0,1,i,j].data[0],file = f)
				#print(mag[0,2,i,j].data[0],file = f)
			print('',file=f)
		f.close()
		'''
        #####################################
        '''
    def forward(self, x):
        x = quant(x)
        x = roundmax(x)
        tmp = Variable(torch.zeros(1, 3, 32, 32).cuda())
        f = fft.Fft2d()
        fft_rout, fft_iout = f(x, tmp)
        mag = torch.sqrt(
            torch.mul(fft_rout, fft_rout) + torch.mul(fft_iout, fft_iout))
        tmp = torch.zeros(1, 1, 32, 32).cuda()
        tmp = torch.add(torch.add(mag[:, 0, :, :], mag[:, 1, :, :]),
                        mag[:, 2, :, :])
        tmp = torch.abs(tmp)
        PFSUM = 0
        '''
		for i in range(0,32):
			for j in range(0,32):
				if (i+j) > 15:
					print_value = 0
				elif (i-j) < 17:
					print_value = 0
				elif (j-i) < 17:
					print_value = 0
				elif (i+j) < 48:
					print_value = 0
				else:
					PFSUM = PFSUM + tmp[0,i,j]
		'''
        freq_l = 0
        freq_h = 0
        for i in range(0, 32):
            for j in range(0, 32):
                #'''
                if (i + j) < 11:
                    freq_l = freq_l + torch.abs(tmp[0, i, j])
                elif (i - j) > 21:
                    freq_l = freq_l + torch.abs(tmp[0, i, j])
                elif (j - i) > 21:
                    freq_l = freq_l + torch.abs(tmp[0, i, j])
                elif (i + j) > 52:
                    freq_l = freq_l + torch.abs(tmp[0, i, j])
                else:
                    freq_h = freq_h + torch.abs(tmp[0, i, j])
        PFSUM = freq_l - freq_h
        f = open(args.outputfile, 'a+')
        print(PFSUM.item(), file=f)
        f.close()
        '''
		f = open('clean_train_freq.txt','w')
		for i in range(0,32):
			for j in range(0,32):
				print(tmp.data[0,i,j]/3,file = f)
		f.close()
		'''
        #print(mag.size())
        #print(tmp.size())
        #####################################
        # print original image
        '''
		f = open('Fixedimage.txt','a+')
		for i in range(0,32):
			for j in range(0,32):
				tmp2 = torch.add(torch.add(x[0,0,i,j],x[0,1,i,j]),x[0,2,i,j])
				print(tmp2.data[0]/3,file = f, end='\t')
				#print(mag[0,1,i,j].data[0],file = f)
				#print(mag[0,2,i,j].data[0],file = f)
			print('',file=f)
		f.close()
		'''
        #####################################

        #####################################
        # print fft image
        '''
		f = open('Fixedfft.txt','a+')
		for i in range(0,mag.size()[2]):
			for j in range(0,mag.size()[3]):
				print(tmp[0,i,j].data[0]/3,file = f, end='\t')
				#print(mag[0,1,i,j].data[0],file = f)
				#print(mag[0,2,i,j].data[0],file = f)
			print('',file=f)
		f.close()
		'''
        #####################################
        '''
Esempio n. 3
0
    def forward(self, x):
        if (args.gau == 0) & (args.blur == 0):
            #no noise
            pass

        elif (args.blur == 0) & (args.gau != 0):
            #gaussian noise add

            gau_kernel = torch.randn(x.size()) * args.gau
            x = Variable(gau_kernel.cuda()) + x

        elif (args.gau == 0) & (args.blur != 0):
            #blur noise add
            blur_kernel_partial = torch.FloatTensor(
                utils.genblurkernel(args.blur))
            blur_kernel_partial = torch.matmul(
                blur_kernel_partial.unsqueeze(1),
                torch.transpose(blur_kernel_partial.unsqueeze(1), 0, 1))
            kernel_size = blur_kernel_partial.size()[0]
            zeros = torch.zeros(kernel_size, kernel_size)
            blur_kernel = torch.cat(
                (blur_kernel_partial, zeros, zeros, zeros, blur_kernel_partial,
                 zeros, zeros, zeros, blur_kernel_partial), 0)
            blur_kernel = blur_kernel.view(3, 3, kernel_size, kernel_size)
            blur_padding = int((blur_kernel_partial.size()[0] - 1) / 2)
            #x = torch.nn.functional.conv2d(x, weight=blur_kernel.cuda(), padding=blur_padding)
            x = torch.nn.functional.conv2d(x,
                                           weight=Variable(blur_kernel.cuda()),
                                           padding=blur_padding)

        elif (args.gau != 0) & (args.blur != 0):
            #both gaussian and blur noise added
            blur_kernel_partial = torch.FloatTensor(
                utils.genblurkernel(args.blur))
            blur_kernel_partial = torch.matmul(
                blur_kernel_partial.unsqueeze(1),
                torch.transpose(blur_kernel_partial.unsqueeze(1), 0, 1))
            kernel_size = blur_kernel_partial.size()[0]
            zeros = torch.zeros(kernel_size, kernel_size)
            blur_kernel = torch.cat(
                (blur_kernel_partial, zeros, zeros, zeros, blur_kernel_partial,
                 zeros, zeros, zeros, blur_kernel_partial), 0)
            blur_kernel = blur_kernel.view(3, 3, kernel_size, kernel_size)
            blur_padding = int((blur_kernel_partial.size()[0] - 1) / 2)
            x = torch.nn.functional.conv2d(x,
                                           weight=Variable(blur_kernel.cuda()),
                                           padding=blur_padding)
            gau_kernel = torch.randn(x.size()) * args.gau
            x = Variable(gau_kernel.cuda()) + x
        else:
            print("Something is wrong in noise adding part")
            exit()

        tmp = Variable(torch.zeros(1, 3, 224, 224).cuda())
        f = fft.Fft2d()
        fft_rout, fft_iout = f(x, tmp)
        mag = torch.sqrt(
            torch.mul(fft_rout, fft_rout) + torch.mul(fft_iout, fft_iout))
        tmp = torch.zeros(1, 1, 224, 224).cuda()
        tmp = torch.add(torch.add(mag[:, 0, :, :], mag[:, 1, :, :]),
                        mag[:, 2, :, :])
        tmp = torch.abs(tmp)
        PFSUM = 0
        for i in range(0, 224):
            for j in range(0, 224):
                if (i + j) < 167:
                    print_value = 0
                elif (i - j) > 56:
                    print_value = 0
                elif (j - i) > 56:
                    print_value = 0
                elif (i + j) > 279:
                    print_value = 0
                else:
                    PFSUM = PFSUM + tmp[0, i, j]
        f = open(args.outputfile, 'a+')
        print(PFSUM.item(), file=f)
        f.close()
        #f = open(args.outputfile,'a+')
        #try:
        #	print(PFSUM.item(),file=f)
        #except:
        #	print(PFSUM,file=f)
        #f.close()
        '''
		f = open(args.outputfile,'a+')
		for i in range(0,224):
			for j in range(0,224):
				print(tmp[0,i,j].item()/3,file = f)
		f.close()
		exit()
		'''
        """
		if args.fixed:
			x = quant(x)
			x = roundmax(x)

		out = self.conv1(x)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.conv2(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.conv3(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.conv4(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.conv5(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.conv6(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.conv7(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.conv8(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.conv9(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.conv10(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.conv11(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.conv12(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.conv13(out)
		out = out.view(out.size(0), -1)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.linear1(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.linear2(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)

		out = self.linear3(out)

		if args.fixed:
			out = quant(out)
			out = roundmax(out)
		"""
        out = torch.zeros(1000)
        return out