0

I have a dataset which I have to process in such a way that it works with a convolutional neural network of PyTorch (I'm completely new to PyTorch). The data is stored in a dataframe with a column for pictures (28 x 28 ndarrays with int32 entries) and a column with its class labels. The pixels of the images merely adopt values +1 and -1 (since it is simulation data of a classical 2d Ising Model). The dataframe looks like this.

I imported the following (a lot of this is not relevant for now, but I included everything for completeness. "data_loader" is a custom py file.):

import numpy as np
import matplotlib.pyplot as plt
import data_loader
import pandas as pd
import torch
import torchvision.transforms as T
from torchvision.utils import make_grid
from torch.nn import Module
from torch.nn import Conv2d
from torch.nn import Linear
from torch.nn import MaxPool2d
from torch.nn import ReLU
from torch.nn import LogSoftmax
from torch import flatten
from sklearn.metrics import classification_report
import time as time
from torch.utils.data import DataLoader, Dataset

Then, I want to get this in the correct shape in order to make it useful for PyTorch. I do this by defining the following class

class MetropolisDataset(Dataset):
    def __init__(self, data_frame, transform=None):
        self.data_frame = data_frame
        self.transform = transform
    def __len__(self):
        return len(self.data_frame)
    def __getitem__(self,idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()
        label = self.data_frame['label'].iloc[idx]
        image = self.data_frame['image'].iloc[idx]
        image = np.array(image)
        if self.transform:
            image = self.transform(image)
        return (image, label)

I call instances of this class as:

train_set = MetropolisDataset(data_frame = df_train, 
                             transform = T.Compose([
        T.ToPILImage(),
        T.ToTensor()]))

validation_set = MetropolisDataset(data_frame = df_validation, 
                             transform = T.Compose([
        T.ToPILImage(),
        T.ToTensor()]))

test_set = MetropolisDataset(data_frame = df_test, 
                             transform = T.Compose([
        T.ToPILImage(),
        T.ToTensor()]))

The problem does not yet arise here, because I am able to read out and show images from these instances of the above defined class.

Then, as far as I found out, it is necessary to let this go through the DataLoader of PyTorch, which I do as follows:

batch_size = 64
train_dl = DataLoader(train_set, batch_size, shuffle=True, num_workers=3, pin_memory=True)
validation_dl = DataLoader(validation_set, batch_size, shuffle=True, num_workers=3, pin_memory=True)
test_dl = DataLoader(test_set, batch_size, shuffle=True, num_workers=3, pin_memory=True)

However, if I want to use these instances of the DataLoader, simply nothing happens. I neither get an error, nor the computation seems to get anywhere. I tried to run a CNN but it does not seem to compute anything. Something else I tried was to show some sample images with the code provided by this article, but the same issue occurs. The sample code is:

def show_images(images, nmax=10):
    fig, ax = plt.subplots(figsize=(8, 8))
    ax.set_xticks([]); ax.set_yticks([])
    ax.imshow(make_grid((images.detach()[:nmax]), nrow=8).permute(1, 2, 0))
def show_batch(dl, nmax=64):
    for images in dl:
        show_images(images, nmax)
        break
show_batch(test_dl)

It seems that there is some error in the implementation of my MetropolisDataset class or with the DataLoader itself. How could this problem be solved?

Juri V
  • 1
  • 3
  • There are undefined identifiers in your sample code: `transforms` (I assume it's from `torchvision`) and `make_grid`. Can you post their code/clarify where you import them from? Can you also post an example input data frame as text? It makes it easier to copy and paste. – kmkurn Dec 09 '22 at 23:27
  • @kmkurn thanks! I have edited my question but I partly found a solution (I'm running into new problems but they're too specific to the data such that I try to find out the problem by myself first). One issue has been that I had to turn num_workers to 0 because I'm using Jupyter, as described here https://stackoverflow.com/questions/71261347/runtimeerror-dataloader-worker-exited-unexpectedly – Juri V Dec 10 '22 at 09:16
  • If you've found a solution, do you mind posting an answer and accepting it? It could help others and also marks this question as answered. – kmkurn Dec 14 '22 at 00:58
  • you're right of course - i added a brief answer to the problem! – Juri V Dec 14 '22 at 16:12

1 Answers1

0

As mentioned in the comments, the problem was partly solved by setting num_workers to zero since I was working in a Jupyter notebook, as answered here. However, this left open one further problem that I got errors when I wanted to apply the DataLoader to run a CNN. The issue was then that my data did consist of int32 numbers instead of float32. I do not include further codes, because this was related directly to my data - however, the issue was (as very often) merely a wrong datatype.

Juri V
  • 1
  • 3