<jupyter_start><jupyter_text>Download a PNG version of the **MNIST** dataset from [here](https://s3.amazonaws.com/fast-ai-imageclas/mnist_png.tgz) and set DATADIR to the extracted folder.<jupyter_code># load the data with some modest data augmentation
# We load as RGB even though we have grayscale images
# since some models only support RGB images.
DATADIR = 'data/mnist_png'
data_aug = vision.get_data_aug(featurewise_center=True, 
                              featurewise_std_normalization=True,
                               rotation_range=15,
                              zoom_range=0.1,
                              width_shift_range=0.1,
                              height_shift_range=0.1)
(train_data, val_data, preproc) = vision.images_from_folder(
                                              datadir=DATADIR,
                                              data_aug = data_aug,
                                              train_test_names=['training', 'testing'], 
                                              target_size=(32,32), color_mode='rgb')
# get a pre-canned 22-layer Wide Residual Network model
model = vision.image_classifier('wrn22', train_data, val_data)
# get a Learner object
learner = ktrain.get_learner(model=model, train_data=train_data, val_data=val_data, 
                             workers=8, use_multiprocessing=True, batch_size=64)
# find a good learning rate
learner.lr_find()
learner.lr_plot()
# train WRN-22 model for a single epoch
learner.autofit(2e-3, 1)
# get a Predictor object that we can use to classify (potentially unlabeled) images
predictor = ktrain.get_predictor(learner.model, preproc)
# let's see the class labels and their indices
predictor.get_classes()
# let's try to predict an image depicting a 7
predictor.predict_filename('/home/amaiya/data/mnist_png/testing/7/7021.png')
# let's try predicting an image showing a 0 and return probabilities for all classes
predictor.predict_filename('/home/amaiya/data/mnist_png/testing/0/101.png', return_proba=True)
# let's predict all images showing a 3 in our validation set
predictor.predict_folder('/home/amaiya/data/mnist_png/testing/3/')[:10]
# let's save the predictor for possible later deployment in an application
predictor.save('/tmp/mypredictor')
# reload the predictor from a file
predictor = ktrain.load_predictor('/tmp/mypredictor')
# let's use the reloaded predictor to verify it still works correctly
predictor.predict_filename('/home/amaiya/data/mnist_png/testing/7/7021.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08738631870304468, 'fraction_numerical': 0.017793594306049824, 'mean_word_length': 2.8625954198473282, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '375048', 'license': 'MIT', 'n_tokens_mistral': 773, 'n_tokens_neox': 721, 'n_words': 196, 'path': 'examples/vision/mnist-WRN22.ipynb', 'repo_name': 'MoraesCaio/ktrain', 'size': '2529'}