<jupyter_start><jupyter_text>Feature LossThis is the notebook for the feature loss training technique.  Imports<jupyter_code>from fastai import *
from fastai.tabular import *
import pandas as pd
from torchsummary import summary
import torch
from torch import nn
import imageio
import torch
import glob
from fastai.vision import *
import os
from torch import nn
import torch.nn.functional as F
from torchvision.models import vgg16_bn<jupyter_output><empty_output><jupyter_text>Data<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/data.zip" .
  !unzip -q data.zip
  image_path = "data"
  %cp "/content/drive/My Drive/autoencoder-training/model_layers.py" .
  %cp "/content/drive/My Drive/autoencoder-training/baseline_model.py" .
  %cp "/content/drive/My Drive/autoencoder-training/featureLoss_function.py" .
  import baseline_model
  import featureLoss_function
else: 
  os.chdir("../")
  image_path = os.getcwd() + "/data"
  from resnet_autoencoder_training import baseline_model
  from resnet_autoencoder_training import featureLoss_function
np.random.seed(3333)
torch.manual_seed(3333)

size = 32
batchsize = 128

#tfms = get_transforms(do_flip = True)
tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=10, max_zoom=1.1, max_lighting=0.2, max_warp=0.2, p_affine=0, 
                      p_lighting=0.75)
src = (ImageImageList.from_folder(image_path).split_by_folder().label_from_func(lambda x: x))
data = (src.transform(tfms, size=size, tfm_y=True)
        .databunch(bs=batchsize)
        .normalize(imagenet_stats, do_y = False))<jupyter_output><empty_output><jupyter_text>Model  Loss Function<jupyter_code>vgg_m = vgg16_bn(True).features.cuda().eval()

requires_grad(vgg_m, False)
blocks = [i-1 for i,o in enumerate(children(vgg_m)) if isinstance(o,nn.MaxPool2d)]

base_loss = F.mse_loss

feat_loss = featureLoss_function.FeatureLoss(vgg_m, blocks[0:3], [30,20,10], base_loss)<jupyter_output>Downloading: "https://download.pytorch.org/models/vgg16_bn-6c64b313.pth" to /root/.cache/torch/checkpoints/vgg16_bn-6c64b313.pth<jupyter_text>Model and Learner<jupyter_code>autoencoder = baseline_model.autoencoder()
learn = Learner(data, autoencoder, loss_func = feat_loss, metrics = [mean_squared_error, mean_absolute_error])<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>learn.fit_one_cycle(5)
learn.metrics = [mean_squared_error, mean_absolute_error, r2_score, explained_variance]
learn.lr_find()
learn.recorder.plot(suggestion=True)
learn.fit_one_cycle(10, max_lr=1e-03)<jupyter_output><empty_output><jupyter_text>Results  Training<jupyter_code>learn.show_results(ds_type=DatasetType.Train)<jupyter_output><empty_output><jupyter_text>Validation<jupyter_code>learn.show_results(ds_type=DatasetType.Valid)
torch.save(autoencoder, "/content/drive/My Drive/autoencoder-training/featureLoss-Cifar10.pt")<jupyter_output><empty_output><jupyter_text>Final optimisation (not included in results)Change back to MSE loss function to do final few epochs to optimise images. As can be seen below when just optimsiing the MSE the image start to become more blurred. This is due to the first few layers in the VGG being edge and shape detectors meaning the recreated images more represent the input's shape.<jupyter_code>learn.loss_func = F.mse_loss
learn.fit_one_cycle(5, max_lr = 1e-06)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10392609699769054, 'fraction_numerical': 0.019630484988452657, 'mean_word_length': 6.532608695652174, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '32171', 'license': 'MIT', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1212, 'n_words': 239, 'path': 'notebooks/cifar-10/featureLoss.ipynb', 'repo_name': 'henriwoodcock/Applying-Modern-Best-Practices-to-Autoencoders', 'size': '3464'}