Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python cifar10数据的gabor滤波器实现_Python_Tensorflow_Pytorch - Fatal编程技术网

Python cifar10数据的gabor滤波器实现

Python cifar10数据的gabor滤波器实现,python,tensorflow,pytorch,Python,Tensorflow,Pytorch,我使用gabor函数实现了对cifar10数据的代码过滤。我不知道为什么经过过滤和堆叠后的图像不清晰。我怀疑我没有使用gabor滤波器参数的最佳值。这是我的代码“你可以从注释“计算sigmas…”开始阅读,在这之前,这只是例行的事情 import torch import torch.nn as nn import torch.nn.functional as F import torchvision from torchvision import datasets, transforms im

我使用gabor函数实现了对cifar10数据的代码过滤。我不知道为什么经过过滤和堆叠后的图像不清晰。我怀疑我没有使用gabor滤波器参数的最佳值。这是我的代码“你可以从注释“计算sigmas…”开始阅读,在这之前,这只是例行的事情

import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torchvision import datasets, transforms
import tensorflow as tf
import tensorflow_io as tfio
import numpy as np
import math
from math import pi
from math import sqrt
import matplotlib.pyplot as plt
# Read config to figure out whether to use CUDA.
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# CUDA-related parameters. (Got this from pytorch's official tutorials.)
kwargs = {'num_workers': 1, 'pin_memory': True} if torch.cuda.is_available()else {}



# Load Cifar10 dataset using standard torchvision package.
data_transform = transforms.Compose([transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

cifar_trainset = torch.utils.data.DataLoader(datasets.CIFAR10(root='./data', 
 train=True, download=True,transform=data_transform), 
 batch_size= 10, shuffle=True, **kwargs)
    
    
cifar_valset = torch.utils.data.DataLoader(datasets.CIFAR10(root='./data', 
 train=True, download=True,transform=data_transform), 
 batch_size= 10, shuffle=True, **kwargs)   

cifar_testset = torch.utils.data.DataLoader( datasets.CIFAR10(root='./data', 
 train=False, download=True, transform=data_transform),
    batch_size= 10, shuffle=False, **kwargs)

# to show the images before and after gabor filter
def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    npimg = plt.imshow(np.transpose(npimg, (1, 2, 0)))
    
#calculating sigmas using the formula from the paper 'Adding biological constraints 
#to CNNs makes image classification more human-like and robust'
lambdaa = 5
b = [1,1.4,1.8]
gamma = [0.5,1]

sigma_x1 = (lambdaa/pi)*(sqrt(np.log(2)/2))*((2**b[0] + 1)/(2**b[0] - 1))
sigma_x2 = (lambdaa/pi)*(sqrt(np.log(2)/2))*((2**b[1] + 1)/(2**b[1] - 1))
sigma_x3 = (lambdaa/pi)*(sqrt(np.log(2)/2))*((2**b[2] + 1)/(2**b[2] - 1))
sigma_x4 = (lambdaa/pi)*(sqrt(np.log(2)/2))*((2**b[0] + 1)/(2**b[0] - 1))
sigma_x5 = (lambdaa/pi)*(sqrt(np.log(2)/2))*((2**b[1] + 1)/(2**b[1] - 1))
sigma_x6 = (lambdaa/pi)*(sqrt(np.log(2)/2))*((2**b[2] + 1)/(2**b[2] - 1))
sigma_y1 = sigma_x1/gamma[1]
sigma_y2 = sigma_x2/gamma[1]
sigma_y3 = sigma_x3/gamma[1]
sigma_y4 = sigma_x1/gamma[0]
sigma_y5 = sigma_x2/gamma[0]
sigma_y6 = sigma_x3/gamma[0]

thetas = np.array([0, pi/8, pi/4, pi*3/8, pi/2, pi*5/8, pi*3/4, pi*7/8])
offsets = np.array([0, pi/2, pi, pi*3/2])
sigmas = np.array([[sigma_x1, sigma_y1], [sigma_x2, sigma_y2], [sigma_x3, sigma_y3],
                   [sigma_x4, sigma_y4], [sigma_x5, sigma_y5], [sigma_x6, sigma_y6]])


for batch_idx, (x, t) in enumerate(cifar_trainset):
    current_batch_size = x.data.size()[0]
    x, t = x.to(device), t.to(device)

    #imshow(torchvision.utils.make_grid(x))
    for theta in thetas:
        for offset in offsets:
            for sigma in sigmas:
                x1 = tfio.experimental.filter.gabor(x, freq = 0.2,
                         sigma = sigma,theta = theta, nstds=31, offset = offset)
               
                     
                     
    x1 = tf.add(x1, 1)
    x1 = x1.numpy()       # converting tensorflow tensor to numpy
    x1 = np.array(x1)    # done to becaue dtype = complex64 had to be converted
    x1 = np.uint8(x1)
    x2 = torch.from_numpy(x1)  # converting numpy to pytorch tensor
    np.add(x1, 1, out=x1)
    
    imshow(torchvision.utils.make_grid(x2))
是否有人使用cifar10数据尝试了gabor滤波器参数的特定值,并且该值有效?或者你认为问题出在代码本身