'How to get not normalized MNIST dataset PyTorch C++

I'm trying to follow this C++ PyTorch example but I need to load the MNIST dataset with its standard values, between 0 and 255. I removed the application of the Normalize() method, but I continue getting value between 0 and 1. What am I doing wrong?

My code is:

int main(int argc, char* argv[]) {

  const int64_t batch_size = 1;

  // MNIST Dataset
  auto train_dataset = torch::data::datasets::MNIST("./mnist")
      .map(torch::data::transforms::Stack<>());

  // Number of samples in the training set
  auto num_train_samples = train_dataset.size().value();

  cout << "Number of training samples: " << num_train_samples << endl;
  
  // Data loaders
  auto train_loader = torch::data::make_data_loader<torch::data::samplers::RandomSampler>(
  std::move(train_dataset), batch_size);

  for (auto& batch : *train_loader) {
    auto data = batch.data.view({batch_size, -1}).to(device);
    auto record = data[0].clone();
    cout << "Max value: " << max(record) << endl;
    cout << "Min value: " << max(record) << endl;
    break;
  }
}

The MNIST dataset I downloaded is the original one, from the site.

Thank you in advance for your help.



Solution 1:[1]

I have looked at the source file and it appears that pytorch mnist dataset class performs the division by 255 to return only tensors within the [0,1] range. So you will have to multiply the batches by 255 yourself.

The normalize transform was not the culprit. It is used to change the mean and variance of your data

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 trialNerror