<jupyter_start><jupyter_text>Accuracy Using a Classifier<jupyter_code>%matplotlib inline
from fastai.vision import *
import numpy as np
import pandas as pd
import os
import matplotlib.pyplot as plt
import os<jupyter_output><empty_output><jupyter_text>Data and Imports<jupyter_code>colab = True
if colab:
  from google.colab import drive
  drive.mount('/content/drive', force_remount = True)
  %cp "/content/drive/My Drive/autoencoder-training/baseline-MNIST.pt" .
  %cp "/content/drive/My Drive/autoencoder-training/featureLoss-MNIST.pt" .
  %cp "/content/drive/My Drive/autoencoder-training/pixelShuffle-MNIST.pt" .
  %cp "/content/drive/My Drive/autoencoder-training/preTrained-MNIST.pt" .
  %cp "/content/drive/My Drive/autoencoder-training/preTrained_nonweights-MNIST.pt" .
  %cp "/content/drive/My Drive/autoencoder-training/progResize-MNIST.pt" .

  %cp "/content/drive/My Drive/autoencoder-training/baseline_model.py" .
  %cp "/content/drive/My Drive/autoencoder-training/model_layers.py" .
  %cp "/content/drive/My Drive/autoencoder-training/featureLoss_function.py" .
  %cp "/content/drive/My Drive/autoencoder-training/pixelShuffle_model.py" .
  %cp "/content/drive/My Drive/autoencoder-training/pretrainedModel.py" .
  %cp "/content/drive/My Drive/autoencoder-training/progressiveResizing_model.py" .
  image_path = "data"
  import baseline_model
  import model_layers
  import featureLoss_function
  import pixelShuffle_model
  import pretrainedModel
  import progressiveResizing_model
else: 
  os.chdir("../")
  image_path = os.getcwd() + "/data"
np.random.seed(3333)
torch.manual_seed(3333)

size = 32
batchsize = 128

path = untar_data(URLs.MNIST)
data = ImageDataBunch.from_folder(path, train='training', valid='testing',size=32,ds_tfms=get_transforms(p_affine=0), bs = 128
                                 ).normalize(imagenet_stats)<jupyter_output>Downloading https://s3.amazonaws.com/fast-ai-imageclas/mnist_png<jupyter_text>Training Classifier<jupyter_code>learn = cnn_learner(data, models.resnet34, metrics=error_rate)
learn.fit_one_cycle(1)
learn.lr_find()
learn.recorder.plot(suggestion = True)
learn.fit_one_cycle(10, max_lr = 1e-03)
learn.unfreeze()
learn.fit_one_cycle(10, max_lr = slice(1e-06, 1e-03))
torch.save(learn.model, "baselineClassification.pt")
torch.save(learn.model, "/content/drive/My Drive/autoencoder-training/baselineClassificationMNIST.pt")
classifier = torch.load("baselineClassification.pt")

learn = Learner(data, classifier, metrics = [error_rate, accuracy])
learn.validate(learn.data.valid_dl)<jupyter_output><empty_output><jupyter_text>Classifier Accuracy  Baseline<jupyter_code>class baseline(nn.Module):
    def __init__(self):
        super(baseline, self).__init__()

        self.autoencoder = torch.load("baseline-MNIST.pt")
        self.classifier = torch.load("baselineClassification.pt")
        self.mean = torch.tensor(imagenet_stats[0]).cuda()
        self.std = torch.tensor(imagenet_stats[1]).cuda()

    def forward(self, x):
        x = torch.clamp(self.autoencoder(x), min=0, max=1)
        x = (x - self.mean[...,None,None])/self.std[...,None,None]
        x = self.classifier(x)
        return x
baseline_evaluation = baseline()
learn = Learner(data, baseline_evaluation, metrics = [error_rate, accuracy])
learn.validate(learn.data.valid_dl)<jupyter_output><empty_output><jupyter_text>FeatureLoss<jupyter_code>class featureloss_mod(nn.Module):
    def __init__(self):
        super(featureloss_mod, self).__init__()

        self.autoencoder = torch.load("featureLoss-MNIST.pt")
        self.classifier = torch.load("baselineClassification.pt")
        self.mean = torch.tensor(imagenet_stats[0]).cuda()
        self.std = torch.tensor(imagenet_stats[1]).cuda()

    def forward(self, x):
        x = torch.clamp(self.autoencoder(x), min=0, max=1)
        x = (x - self.mean[...,None,None])/self.std[...,None,None]
        x = self.classifier(x)
        return x
featureloss_evaluation = featureloss_mod()
learn = Learner(data, featureloss_evaluation, metrics = [error_rate, accuracy])
learn.validate(learn.data.valid_dl)<jupyter_output><empty_output><jupyter_text>Pretrained Models  Pretrained<jupyter_code>class pretrained_weights(nn.Module):
    def __init__(self):
        super(pretrained_weights, self).__init__()

        self.autoencoder = torch.load("preTrained-MNIST.pt")
        self.classifier = torch.load("baselineClassification.pt")
        self.mean = torch.tensor(imagenet_stats[0]).cuda()
        self.std = torch.tensor(imagenet_stats[1]).cuda()

    def forward(self, x):
        x = torch.clamp(self.autoencoder(x), min=0, max=1)
        x = (x - self.mean[...,None,None])/self.std[...,None,None]
        x = self.classifier(x)
        return x
pretrained_weights_eval = pretrained_weights()
learn = Learner(data, pretrained_weights_eval, metrics = [error_rate, accuracy])
learn.validate(learn.data.valid_dl)<jupyter_output><empty_output><jupyter_text>Resnet<jupyter_code>class resnet_model(nn.Module):
    def __init__(self):
        super(resnet_model, self).__init__()

        self.autoencoder = torch.load("preTrained_nonweights-MNIST.pt")
        self.classifier = torch.load("baselineClassification.pt")
        self.mean = torch.tensor(imagenet_stats[0]).cuda()
        self.std = torch.tensor(imagenet_stats[1]).cuda()

    def forward(self, x):
        x = torch.clamp(self.autoencoder(x), min=0, max=1)
        x = (x - self.mean[...,None,None])/self.std[...,None,None]
        x = self.classifier(x)
        return x
resnet_eval = resnet_model()
learn = Learner(data, resnet_eval, metrics = [error_rate, accuracy])
learn.validate(learn.data.valid_dl)<jupyter_output><empty_output><jupyter_text>Pixel Shuffled<jupyter_code>class pixelShuffle_model(nn.Module):
    def __init__(self):
        super(pixelShuffle_model, self).__init__()

        self.autoencoder = torch.load("pixelShuffle-MNIST.pt")
        self.classifier = torch.load("baselineClassification.pt")
        self.mean = torch.tensor(imagenet_stats[0]).cuda()
        self.std = torch.tensor(imagenet_stats[1]).cuda()

    def forward(self, x):
        x = torch.clamp(self.autoencoder(x), min=0, max=1)
        x = (x - self.mean[...,None,None])/self.std[...,None,None]
        x = self.classifier(x)
        return x
pixelShuffle_eval = pixelShuffle_model()
learn = Learner(data, pixelShuffle_eval, metrics = [error_rate, accuracy])
learn.validate(learn.data.valid_dl)<jupyter_output><empty_output><jupyter_text>Progressive Resizing<jupyter_code>class progResize_model(nn.Module):
    def __init__(self):
        super(progResize_model, self).__init__()

        self.autoencoder = torch.load("progResize-MNIST.pt")
        self.classifier = torch.load("baselineClassification.pt")
        self.mean = torch.tensor(imagenet_stats[0]).cuda()
        self.std = torch.tensor(imagenet_stats[1]).cuda()

    def forward(self, x):
        x = torch.clamp(self.autoencoder(x), min=0, max=1)
        x = (x - self.mean[...,None,None])/self.std[...,None,None]
        x = self.classifier(x)
        return x
progResize_eval = progResize_model()
learn = Learner(data, progResize_eval, metrics = [error_rate, accuracy])
learn.validate(learn.data.valid_dl)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.140016570008285, 'fraction_numerical': 0.008285004142502071, 'mean_word_length': 4.267636363636363, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '37014', 'license': 'MIT', 'n_tokens_mistral': 2552, 'n_tokens_neox': 2469, 'n_words': 359, 'path': 'notebooks/mnist/AutoencodedClassificationAccuracy.ipynb', 'repo_name': 'henriwoodcock/Applying-Modern-Best-Practices-to-Autoencoders', 'size': '7242'}