Thanks for the code.
Unfortunately I still cannot reproduce it using predefined arrays as seen here:
import torch
import torchvision
from torchvision import transforms
from torch.utils.data import Dataset, DataLoader
import numpy as np
import ctypes
import multiprocessing as mp
import os
import pandas as pd

class TestDataset(Dataset):
    def __init__(self, imfolder: str, transforms = None):

        self.imfolder = imfolder
        self.transforms = transforms
        self.n_images = 10
        c=3
        h=256
        w=256

        shared_array_base = mp.Array(ctypes.c_uint, self.n_images*c*h*w)
        shared_array = np.ctypeslib.as_array(shared_array_base.get_obj())
        self.shared_array = shared_array.reshape(self.n_images, h, w, c)
        self.shared_array = self.shared_array.astype("uint8")
        self.use_cache = False

    def __getitem__(self, index):
        if not self.use_cache:
            x = np.ones((256, 256, 3)).astype(np.uint8) * index
            self.shared_array[index] = x
        x = self.shared_array[index]

        if self.transforms:
            x = self.transforms(x)

        return x

    def set_use_cache(self, use_cache):
        self.use_cache = use_cache

    def __len__(self):
        return self.n_images

transform = transforms.Compose([
    transforms.ToTensor(),
    ])
pytorch_dataset = TestDataset(imfolder= "./test", transforms=transform)
pytorch_dataloader = DataLoader(dataset=pytorch_dataset, batch_size=10, shuffle=True,  pin_memory=True, num_workers=1)

# Works
pytorch_dataset = TestDataset(imfolder= "./test", transforms=transform)
pytorch_dataloader = DataLoader(dataset=pytorch_dataset)
pytorch_dataloader.dataset.set_use_cache(use_cache=False)
pytorch_dataloader.num_workers = 0

for idx, x in enumerate(pytorch_dataloader):
    print("Loading Cache {}".format(idx*pytorch_dataloader.batch_size))
    print(x.sum())

pytorch_dataloader.dataset.set_use_cache(use_cache=True)

images = next(iter(pytorch_dataloader))
image = images[0]
print(image.sum())

# Does not work
pytorch_dataset = TestDataset(imfolder= "./test", transforms=transform)
pytorch_dataloader = DataLoader(dataset=pytorch_dataset)
pytorch_dataloader.dataset.set_use_cache(use_cache=False)
pytorch_dataloader.num_workers = 1

for idx, x in enumerate(pytorch_dataloader):
    print("Loading Cache {}".format(idx*pytorch_dataloader.batch_size))
    print(x.sum())

pytorch_dataloader.dataset.set_use_cache(use_cache=True)

images = next(iter(pytorch_dataloader))
image = images[0]
print(image.sum())

I removed the Normalization to manually verify that the result tensors show the expected results and not random values.
Also, since I’m working on a remote server, I just used the sum() of the output tensors instead of matplotlib.
Here is the output:
Loading Cache 0
tensor(0.)
Loading Cache 1
tensor(771.0118)
Loading Cache 2
tensor(1542.0236)
Loading Cache 3
tensor(2313.0354)
Loading Cache 4
tensor(3084.0471)
Loading Cache 5
tensor(3855.0588)
Loading Cache 6
tensor(4626.0708)
Loading Cache 7
tensor(5397.0825)
Loading Cache 8
tensor(6168.0942)
Loading Cache 9
tensor(6939.1060)
tensor(0.)
Loading Cache 0
tensor(0.)
Loading Cache 1
tensor(771.0118)
Loading Cache 2
tensor(1542.0236)
Loading Cache 3
tensor(2313.0354)
Loading Cache 4
tensor(3084.0471)
Loading Cache 5
tensor(3855.0588)
Loading Cache 6
tensor(4626.0708)
Loading Cache 7
tensor(5397.0825)
Loading Cache 8
tensor(6168.0942)
Loading Cache 9
tensor(6939.1060)
tensor(0.)