Applying manual ZCA whitening is very slow

iAvicenna: <p>I am trying to write a clasifier for CIFAR10 images where the images are also whitened at the loader level. I have a seperate function</p>
<pre><code class="lang-auto">def computeZCAMAtrix(dataname):

    #compute ZCA whitening matrix, mean and std for the given database

    return (torch.from_numpy(ZCAMatrix).float(), mean, std)
</code></pre>
<p>which I call before loading my data. The data loader function is of the form</p>
<pre><code class="lang-auto">def getData(batch_size,dataname,Z,mean,std): 
  
  
    if(batch_size&lt;=0):
        batch_size=256

    if(dataname=='CIFAR10'):
        root = 'cifar10/'

        
        transform_train = transforms.Compose(
        [     transforms.RandomRotation(30),
              transforms.RandomHorizontalFlip(),
              transforms.RandomCrop(32, padding=4), 
              transforms.ToTensor(),
              transforms.Normalize(mean , std),
              transforms.LinearTransformation(Z), 
              ])

        #for test set we do not apply the random transformations
        transform_test = transforms.Compose(
        [     transforms.ToTensor(),
              transforms.Normalize(mean , std),
              transforms.LinearTransformation(Z)
              ])
        
        
        #load the training and test sets
        training_set = datasets.CIFAR10(root = root,
                                  transform = transform_train,
                                  train = True,
                                  download = True)
        
        test_set = datasets.CIFAR10(root = root,
                                  transform = transform_test,
                                  train = False,
                                  download = True)
 
    else:
        printf('Currently only CIFAR10 is allowed, terminating program')
        sys.exit()
        

    training_loader = torch.utils.data.DataLoader(dataset=training_set,
                                              batch_size=batch_size,
                                              shuffle=True)

    test_loader = torch.utils.data.DataLoader(dataset=test_set,
                                            batch_size=batch_size,
                                            shuffle=False)

    
    return (training_set,test_set,training_loader,test_loader)
</code></pre>
<p>So the training loader applies some random transformations and then does the normalization. For some reason this slows down the code considerably.  Is the application of  transforms.LinearTransformation really that slow or am I making a mistake. I take the batch size to be 256. I am trying the code on google colab, GPU enabled. Should apply the whitening transormation to the dataset and save it somewhere and the use it? That makes sense and I can just apply the ZCA whitening matrix at the level of np multplication to the train_data and test_data but unfortuntely I do not know how to save transformed datasets.</p>
<p>By the way applyin ZCA whitening worsens the accuracy quite considerably as well. I assume this is because I apply it after the random transormations?</p>

themightyoarfish: <p>I have the same issue, linear transform for cifar with a transformation matrix of size 3*1024 is actually unusably slow. It seems as pytorch does not prefetch samples while the gpu is running.</p>

alpapado: <p>Any news on this front? Applying ZCA whitening through LinearTransformation is unusably slow for me as well.</p>

SomeOneIntheWorld: <p>If you use ZCA in DataLoader, it would be very slow. Instead, I wrote a ZCA Transformation <a href="https://github.com/semi-supervised-paper/semi-supervised-paper-implementation/blob/e39b61ccab/semi_supervised/core/utils/data_util.py#L150" rel="nofollow noopener">here</a>, and it is used per minibatch, which is faster. The usage of ZCA Transformation is as below.</p>
<pre><code class="lang-auto">for data, target in dataloader:
     zca_data = zca(data)
</code></pre>

ttoosi: <p>I couldn’t find how you compute the transformation matrices in your repo. Could you elaborate more on how to define zca to work on batches of data?</p>

SomeOneIntheWorld: <p>You can see how to calculate  mean and ZCA mat from <a href="https://github.com/smlaine2/tempens/blob/a32743acbdea54560b7d9d90934c1eb5b49a81a7/zca_bn.py#L21" rel="nofollow noopener">this repo</a>. After calculating, save the mean and ZCA mat using scipy.io.save() api. Then, when training, you can load the mean and mat using <a href="https://github.com/semi-supervised-paper/semi-supervised-paper-implementation/blob/56a269f41da7cac11ccad7cf3921813e8bb9d6d4/semi_supervised/core/method/basic_method.py#L53" rel="nofollow noopener">this code</a>.</p>