How to copy a Dataset/ load two of the same datasets - Unexpected behaviour

nerdymuppet: <p>Basically I would like to do what this <a href="https://stackoverflow.com/questions/60157858/how-can-one-clone-a-pytoch-dataset-into-another-variable" rel="noopener nofollow ugc">stackoverflow thread</a> asks. But the answer does not work for me. All I want to do is copy the FashionMNIST dataset.<br>
Here is a minimal example to show what I mean:</p>
<pre><code class="lang-auto">transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])
dat1 = datasets.FashionMNIST('./', train=True, download=True, transform=transform) 
dat2 = datasets.FashionMNIST('./', train=True, download=True, transform=transform)
print(dat1.classes)
dat1.classes[0] = 'whacky name 3000'
print(dat2.classes)
</code></pre>
<p>This gives</p>
<pre><code class="lang-auto">['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
['whacky name 3000', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
</code></pre>
<p>I would expect it to give me the original list and not a changed list. I only changed dat1 and not dat2.<br>
Even if I try using deepcopy it doesn’t work. Can someone explain the inner workings and a solution?</p>

ejguan: <aside class="quote no-group" data-username="nerdymuppet" data-post="1" data-topic="161707">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/n/87869e/48.png" class="avatar"> nerdymuppet:</div>
<blockquote>
<p>Can someone explain the inner workings and a solution?</p>
</blockquote>
</aside>
<p>I can explain the reason that the <code>classes</code> is attached to Dataset <code>class</code> rather than Dataset instance. See: <a href="https://github.com/pytorch/vision/blob/6ca9c76adb6daf2695d603ad623a9cf1c4f4806f/torchvision/datasets/mnist.py#L227" rel="noopener nofollow ugc">https://github.com/pytorch/vision/blob/6ca9c76adb6daf2695d603ad623a9cf1c4f4806f/torchvision/datasets/mnist.py#L227</a></p>
<p>In terms of solution, it depends on what would be your use case. Do you want to create multiple non-overlapping subsets from a Dataset or you want to have duplicate data from a Dataset?</p>

nerdymuppet: <p>Thank you for the link.<br>
I would like to change the labels for an experiment I am doing. So in each run I change the class names and labels. Ideally, I load the dataset change the labels and the class names train the model and repeat several times.<br>
But I change the labes and classes and then when I want to reload/ revert. It goes haywire because it copies the old dataset to the new dataset. I cannot reload or copy a temp dataset.</p>

nerdymuppet: <p>Another solution I tried, was to just make a copy of the information I needed from the dataset.</p>
<pre><code class="lang-auto">    cls_copy = copy.deepcopy(dat1.classes)
    targets_copy = copy.deepcopy(dat1.targets)
</code></pre>
<p>and just copy that Info back into the dataset in each run to ‘reset’ it. <strong>This also breaks</strong>. There are dependencies I don’t quite understand.<br>
I would still like to know why I cannot copy a dataset without keeping the reference to the old dataset or reload a clean copy.</p>

ejguan: <p>When you <code>reset</code>, could you please try to do <code>type(dat1).classes = cls_copy</code>?</p>