简体   繁体   English

我的 Pytorch 卷积神经网络中的每个时期是否应用了随机变换? (数据增强)

[英]Are the random transforms applied at each epoch in my Pytorch convolutional neural net? (data augmentation)

I'm new to Pytorch and I try to make a convolutional neural net to classify a set of images (personal iris recognition problem).我是 Pytorch 的新手,我尝试制作一个卷积神经网络来对一组图像进行分类(个人虹膜识别问题)。 My issue is that I have a small number of images (10 classes and 20 images per class).我的问题是我有少量图像(10 个类和每个类 20 个图像)。 I tried to make data augmentation (random transforms for every epoch) but I'm not sure that these are applied at each epoch as I entended.我尝试进行数据增强(每个时期的随机变换),但我不确定这些是否如我所愿在每个时期都应用。 Here's my code.这是我的代码。 If anyone can confirm that I'm doing it right or if it's not ok, is there a way to make the transforms inside the loop?如果有人可以确认我做对了还是不行,有没有办法在循环内进行转换?

from torch import utils, nn, optim, no_grad
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from ConvNet import ConvNet
from ImagesDataset import ImagesDataset, AddGaussianNoise

DATABASE_PATH = "C://Users//Maria//Downloads//ees//CASIA-IrisV2"
MODEL_PATH = "entire_model.pt"
dataArray = []

# Device configuration
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# data augmentation by applying some transforms randomly for every batch
transform = transforms.Compose([transforms.RandomCrop(5), transforms.RandomHorizontalFlip(p=0.1),
                                transforms.ColorJitter(brightness=0.1, contrast=0.2, saturation=0, hue=0),
                                AddGaussianNoise(0.1, 0.05), transforms.ToTensor()])
dataset = ImagesDataset(csv_file="generate_csv//generate_csv_correctly_detected.csv", root_dir=DATABASE_PATH, transform=transforms.ToTensor())
num_epochs = 300
num_classes = 10
batch_size = 100
learning_rate = 0.01

# the dataset is partitioned in 5 subsets to perform cross validation
sum_percents = 0
data_set = utils.data.random_split(dataset, [40, 40, 40, 40, 40])

for i in range(5):
    test_set = data_set[i]
    train_set = []
    for j in range(5):
        if j != i:
            train_set += data_set[j]

    train_loader = DataLoader(dataset=train_set, batch_size=batch_size, shuffle=True)
    test_loader = DataLoader(dataset=test_set, batch_size=batch_size, shuffle=True)

    model = ConvNet(0).to(device)

    criterion = nn.CrossEntropyLoss()
    optimizer = optim.Adam(model.parameters(), lr=learning_rate)
    
    # Train the model
    total_step = len(train_loader)
    loss_list = []
    acc_list = []

    # delete contents of loss1 file
    file = open("loss1.txt", "r+")
    file.truncate(0)
    file.close()

    for epoch in range(num_epochs):
        print("Epoch: " + str(epoch))
        for i, (images, labels) in enumerate(train_loader):
            # Run the forward pass
            images = images.to(device)
            labels = labels.to(device)
            outputs = model(images)
            loss = criterion(outputs, labels)  
            # set the gradients to zero
            optimizer.zero_grad()
            # compute gradients
            loss.backward()
            # update the parameters
            optimizer.step()

            # Track the accuracy
            total = labels.size(0)
            _, predicted = torch.max(outputs.data, 1)
            correct = (predicted == labels).sum().item()
            acc_list.append(correct / total)

    # Save
    torch.save(model, MODEL_PATH)

    # Test the model
    model.eval()
    with no_grad():
        correct = 0
        total = 0
        for images, labels in test_loader:
            outputs = model(images)
            _, predicted = torch.max(outputs.data, 1)
            total += labels.size(0)
            correct += (predicted == labels).sum().item()

        print('Test Accuracy of the model on the 132 test images: {} %'.format((correct / total) * 100))
    sum_percents += (correct / total) * 100
print('Average accuracy is {}%'.format((sum_percents/5)))

Hi what i meant wasn't like that but the following and i cannot completely reproduce since i dont have your function AddGaussianNoise嗨,我的意思不是那样,但以下内容,我无法完全复制,因为我没有你的 function AddGaussianNoise

import torchvision.transforms as T
import numpy as np

transforms = T.Compose([
    T.ToPILImage(), # You need to add this to pil image
    T.RandomCrop(5), T.RandomHorizontalFlip(p=0.1),
    T.ColorJitter(brightness=0.1, contrast=0.2, saturation=0, hue=0),
    T.ToTensor()
])

transforms(np.random.randn(224, 224, 3).astype(np.uint8))
>>>tensor([[[0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
         [0.0000, 0.0000, 0.0000, 0.0000, 0.0039],
         [0.0000, 0.0000, 0.9882, 0.0000, 0.9882],
         [0.0039, 0.9882, 0.9882, 0.0000, 0.9882],
         [0.0000, 0.0039, 0.0000, 0.0000, 0.0000]],

        [[0.0039, 0.0000, 0.0000, 0.0039, 0.9882],
         [0.9882, 0.0000, 0.0000, 0.0000, 0.9882],
         [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
         [0.0000, 0.9882, 0.0000, 0.0000, 0.0000],
         [0.0000, 0.9882, 0.0000, 0.0000, 0.0039]],

        [[0.0000, 0.9882, 0.0000, 0.9882, 0.0000],
         [0.0000, 0.0039, 0.0000, 0.0000, 0.0000],
         [0.0039, 0.0000, 0.0000, 0.0000, 0.0039],
         [0.0000, 0.0000, 0.0000, 0.0000, 0.0000],
         [0.0039, 0.0039, 0.0000, 0.0000, 0.0000]]])

So this is another assumption but a transform should work like this no?所以这是另一个假设,但转换应该像这样工作,不是吗? Since i dont have any of your code so here it is因为我没有你的任何代码所以在这里

import torchvision.transforms as T

transforms = T.Compose([
    T.ToPILImage(), # You need to add this to pil image
    T.RandomCrop(5), T.RandomHorizontalFlip(p=0.1),
    T.ColorJitter(brightness=0.1, contrast=0.2, saturation=0, hue=0),
    T.ToTensor(), # Maybe you can add you gaussian noise augment here
])
dataset = ImagesDataset(csv_file="generate_csv//generate_csv_correctly_detected.csv", root_dir=DATABASE_PATH, transform=transforms)

I tried adding the ToPILImage() transform (without the gaussian noise transform) and the above errors disappear but I get a size mismatch later when the images are fed in the neural net.我尝试添加 ToPILImage() 变换(没有高斯噪声变换),上面的错误消失了,但是当图像被输入神经网络时,我得到了尺寸不匹配。 Is there any possibility that one of the transforms changes the image size?是否有可能其中一种变换会改变图像大小?

RuntimeError: size mismatch, m1: [100 x 32], m2: [55296 x 1000] at ..\aten\src\TH/generic/THTensorMath.cpp:41

the code for the Gaussian Noise transform is (currently not used)高斯噪声变换的代码是(当前未使用)

class AddGaussianNoise(object):
    def __init__(self, mean=0., std=1.):
        self.std = std
        self.mean = mean

    def __call__(self, tensor):
        return tensor + torch.randn(tensor.size()) * self.std + self.mean

    def __repr__(self):
        return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, self.std)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 随机裁剪数据增强卷积神经网络 - Random cropping data augmentation convolutional neural networks 如何在 Pytorch 中使用 torchvision.transforms 进行分割任务的数据增强? - How to use torchvision.transforms for data augmentation of segmentation task in Pytorch? 每个时期如何进行数据扩充? - How is data augmentation done in each epoch? PyTorch和卷积神经网络 - PyTorch and Convolutional Neural Networks 如何使用我自己的数据在 PyTorch 上测试这个卷积神经网络? - How can I use my own data to test this Convolutional Neural Network on PyTorch? 对于使用数据增强进行图像分类的卷积神经网络,如何在 keras 中获得可重现的结果? - How can I get reproducible results in keras for a convolutional neural network using data augmentation for image classification? PyTorch 中的数据扩充 - Data Augmentation in PyTorch PyTorch DataLoader 在每个时期使用相同的随机变换 - PyTorch DataLoader uses identical random transformation across each epoch 我的神经网络上的权重没有更新(Pytorch) - Weights not updating on my neural net (Pytorch) 将 Keras (Tensorflow) 卷积神经网络转换为 PyTorch 卷积网络? - Converting Keras (Tensorflow) convolutional neural networks to PyTorch convolutional networks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM