Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HOWTO: Loading MNIST from torchvision #325

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/howtos.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ Scheduled Sampling
.. raw:: html
:file: _formatted_howtos/scheduled-sampling.diff.html

Torchvision Datasets
----------------

⟶ `View as a side-by-side diff <https://github.com/google/flax/compare/master..howto/torchvision-mnist?diff=split>`_

.. raw:: html
:file: _formatted_howtos/torchvision-mnist.diff.html

How do HOWTOs work?
-------------------
Expand Down
1 change: 1 addition & 0 deletions examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tensorflow>=2.1
tensorflow-datasets
torchvision
30 changes: 30 additions & 0 deletions howtos/diffs/torchvision-mnist.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
diff --git a/examples/mnist/train.py b/examples/mnist/train.py
index 9c21985..0c3a129 100644
--- a/examples/mnist/train.py
+++ b/examples/mnist/train.py
@@ -34,7 +34,7 @@ import jax.numpy as jnp

import numpy as onp

-import tensorflow_datasets as tfds
+import torchvision


FLAGS = flags.FLAGS
@@ -164,10 +164,12 @@ def eval_model(model, test_ds):

def get_datasets():
"""Load MNIST train and test datasets into memory."""
- ds_builder = tfds.builder('mnist')
- ds_builder.download_and_prepare()
- train_ds = tfds.as_numpy(ds_builder.as_dataset(split='train', batch_size=-1))
- test_ds = tfds.as_numpy(ds_builder.as_dataset(split='test', batch_size=-1))
+ train_ds = torchvision.datasets.MNIST('./data', train=True, download=True)
+ test_ds = torchvision.datasets.MNIST('./data', train=False, download=True)
+ train_ds = {'image': onp.expand_dims(train_ds.data.numpy(), 3),
+ 'label': train_ds.targets.numpy()}
+ test_ds = {'image': onp.expand_dims(test_ds.data.numpy(), 3),
+ 'label': test_ds.targets.numpy()}
train_ds['image'] = jnp.float32(train_ds['image']) / 255.
test_ds['image'] = jnp.float32(test_ds['image']) / 255.
return train_ds, test_ds
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"pytest-xdist",
"tensorflow",
"tensorflow_datasets",
"torchvision"
]

setup(
Expand Down