{"id":18096494,"url":"https://github.com/andreaschandra/mnist","last_synced_at":"2025-06-26T05:05:24.422Z","repository":{"id":237422069,"uuid":"655691649","full_name":"andreaschandra/MNIST","owner":"andreaschandra","description":null,"archived":false,"fork":false,"pushed_at":"2023-06-19T12:02:02.000Z","size":437,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-12T09:39:22.699Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andreaschandra.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-19T12:01:48.000Z","updated_at":"2023-06-19T12:02:01.000Z","dependencies_parsed_at":"2024-05-01T16:40:34.217Z","dependency_job_id":"12a96fe2-ac45-4f6a-8868-44e6609d1732","html_url":"https://github.com/andreaschandra/MNIST","commit_stats":null,"previous_names":["andreaschandra/mnist"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreaschandra%2FMNIST","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreaschandra%2FMNIST/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreaschandra%2FMNIST/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andreaschandra%2FMNIST/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andreaschandra","download_url":"https://codeload.github.com/andreaschandra/MNIST/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247429823,"owners_count":20937745,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-31T19:14:23.753Z","updated_at":"2025-04-06T03:26:29.043Z","avatar_url":"https://github.com/andreaschandra.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MNIST in TensorFlow\n\nThis repository demonstrates using Paperspace Gradient to train and deploy a deep learning model to recognize handwritten characters, which is a canonical sample problem in machine learning.\n\nWe build a convolutional neural network to classify the [MNIST\ndataset](http://yann.lecun.com/exdb/mnist/) using the\n[tf.data](https://www.tensorflow.org/api_docs/python/tf/data),\n[tf.estimator.Estimator](https://www.tensorflow.org/api_docs/python/tf/estimator/Estimator),\nand\n[tf.layers](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf)\nAPIs.\n\n# Gradient Setup\n\n## Single Node Training on Gradient\n\n### Install Gradient CLI\n\n```\npip install -U gradient\n```\n\n[Please check our documentation on how to install Gradient CLI and obtain an API Key](https://docs.paperspace.com/gradient/get-started/install-the-cli)\n\n### Create project and get the project id\n\n[Please check our documentation on how to create a project and get the project id](https://docs.paperspace.com/gradient/get-started/managing-projects)\nYour project ID will look like `pr1234567`.\n\n### Create and start a workflow\n\n```\ngradient workflows create --name mnist-sample --projectId pr1234567\n+--------------+--------------------------------------+\n| Name         | ID                                   |\n+--------------+--------------------------------------+\n| mnist-sample | 12345678-1234-1234-1234-1234567890ab |\n+--------------+--------------------------------------+\n\n```\n\nClone this repo, and change directoru into it, or copy [mnist-sample.yaml](mnist-sample.yaml) to your local machine.\n\nThen run the workflow using the workflow ID from the create workflow command above.\n\n```\ngradient workflows run --id 12345678-1234-1234-1234-1234567890ab --path mnist-sample.yaml\n```\n\nThat's it!\n\n### Exporting a Model for inference\n\n#### Export your Tensorflow model\n\nIn order to serve a Tensorflow model, simply export a SavedModel from your Tensorflow program. [SavedModel](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md) is a language-neutral, recoverable, hermetic serialization format that enables higher-level systems and tools to produce, consume, and transform TensorFlow models.\n\nPlease refer to [Tensorflow documentation](https://www.tensorflow.org/guide/saved_model#save_and_restore_models) for detailed instructions on how to export SavedModels.\n\n#### Example code showing how to export your model:\n\n```\ntf.estimator.train_and_evaluate(mnist_classifier, train_spec, eval_spec)\n\n#Starting to Export model\nimage = tf.placeholder(tf.float32, [None, 28, 28])\ninput_fn = tf.estimator.export.build_raw_serving_input_receiver_fn({\n            'image': image,\n        })\nmnist_classifier.export_savedmodel(\u003cexport directory\u003e,\n                                    input_fn,\n                                    strip_default_attrs=True)\n#Model Exported\n```\n\nWe use TensorFlow's [SavedModelBuilder module](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/builder.py) to export the model. SavedModelBuilder saves a \"snapshot\" of the trained model to reliable storage so that it can be loaded later for inference.\n\nFor details on the SavedModel format, please see the documentation at [SavedModel README.md](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/saved_model/README.md).\n\nFor export directory, be sure to set it to `PS_MODEL_PATH` when running a model deployment on Gradient:\n\n```\nexport_dir = os.path.abspath(os.environ.get('PS_MODEL_PATH'))\n```\n\nYou can also use Gradient SDK to ensure you have the correct path:\n\n```\nfrom gradient_sdk.utils import data_dir, model_dir, export_dir\n```\n\n# (Optional) Local Setup using a Virtual Environment\n\nUsers sometimes run into local machine environment issues when trying to use Python. A common solution for this is to create and use a Python virtual environment to run Python from within. To do so:\n\n1. Create and activate a Python virtual environment (we recommend using python3.7+):\n\n```\ncd mnist-sample\n\npython3 -m venv venv\n\nsource venv/bin/activate\n```\n\n2. Install the required Python packages:\n\n```\npip install -r requirements-local.txt\n```\n\n# Local Training\n\nTo train a the mnist model locally:\n\n1. Make sure you have the latest version of TensorFlow installed.\n\n2. Also make sure you've [added the models folder to your Python path](https://github.com/mlcommons/training/blob/master/image_classification/tensorflow/official/README.md#running-the-models); otherwise you may encounter an error like `ImportError: No module named mnist`.\n\n3. Download the code from GitHub:\n\n```\ngit clone git@github.com:Paperspace/mnist-sample.git\n```\n\n4. Start training the model:\n\n```\npython mnist.py\n```\n\n_Note: local training will take a long time, so be prepared to wait!_\n\nIf you want to shorten model training time, you can change the max steps parameter:\n\n```\npython mnist.py --max_steps=1500\n```\n\nThe mnist dataset is downloaded to the `./data` directory.\n\nModel results are stored in the `./models` directory.\n\nBoth directories can be safely deleted if you would like to start the training over from the beginning.\n\n## Exporting the model to a specific directory\n\nYou can export the model into a specific directory, in the Tensorflow [SavedModel](https://www.tensorflow.org/guide/saved_model) format, by using the argument `--export_dir`:\n\n```\npython mnist.py --export_dir /tmp/mnist_saved_model\n```\n\nIf no export directory is specified, the model is saved to a timestamped directory under `./models` subdirectory (e.g. `mnist-sample/models/1513630966/`).\n\n## Testing a Tensorflow Serving-deployed model on your local machine using Docker\n\nOpen another terminal window and run the following in the directory where you cloned this repo:\n\n```\ndocker run -t --rm -p 8501:8501 -v \"$PWD/models:/models/mnist\" -e MODEL_NAME=mnist tensorflow/serving\n```\n\nNow you can test the local inference endpoint by running:\n\n```\npython serving_rest_client_test.py\n```\n\nOptionally you can provide a path to an image file to run a prediction on:\n\n```\npython serving_rest_client_test.py --path example3.png\n```\n\nOnce you've completed local testing using the tensorflow/serving docker container, stop the running container as follows:\n\n```\ndocker ps\ndocker kill \u003ccontainer-id-or-name\u003e\n```\n\n## Training the model on a node with a GPU for use with Tensorflow Serving on a node with only a CPU\n\nIf you are training on Tensorflow using a GPU but would like to export the model for use in Tensorflow Serving on a CPU-only server, you can train and/or export the model using `--data_format=channels_last`:\n\n```\npython mnist.py --data_format=channels_last\n```\n\nThe SavedModel will be saved in a timestamped directory under `models` subdirectory (e.g. `mnist-sample/models/1513630966/`).\n\n## Inspecting and getting predictions with the SavedModel file\n\nYou can also use the [`saved_model_cli`](https://www.tensorflow.org/guide/saved_model#cli_to_inspect_and_execute_savedmodel) tool to inspect and execute the SavedModel.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreaschandra%2Fmnist","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandreaschandra%2Fmnist","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandreaschandra%2Fmnist/lists"}