# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="winOvrGHNswC" colab_type="text"
# # Pixel Shuffle
#
# This notebook is a comparison between two best practices. Pixel shuffle and upsampling followed by a convolution.

# + [markdown] id="5sHL-kiPN6ya" colab_type="text"
# ## Imports

# + id="mdWFhYGlN7y_" colab_type="code" colab={}
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

# + [markdown] id="tKt7uuOVN3K6" colab_type="text"
# ## Data

# + id="ESI2W8MpNUum" colab_type="code" outputId="bc3cf92c-a6c3-4c4b-c8a5-ed232260dcb2" colab={"base_uri": "https://localhost:8080/", "height": 122}
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/pixelShuffle_model.py" .
  import pixelShuffle_model
else: 
  os.chdir("../")
  image_path = os.getcwd() + "/data"

# + id="Ush7LmhZOTBW" colab_type="code" colab={}
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))

# + [markdown] id="nDEcCP-DOsit" colab_type="text"
# ## Model

# + id="TYftI_qzlPk0" colab_type="code" colab={}
autoencoder = pixelShuffle_model.autoencoder()

# + id="IzLzV4D0pgVA" colab_type="code" colab={}
learn = Learner(data, autoencoder, loss_func = F.mse_loss)

# + id="6ehs1DEMptJo" colab_type="code" outputId="7a2c85fb-0f97-47f2-98f9-1cb8ed53f15d" colab={"base_uri": "https://localhost:8080/", "height": 204}
learn.fit_one_cycle(5)

# + id="1swKe1aJpwqx" colab_type="code" outputId="2940a596-dcb0-4dec-9c55-6c9f3847d10c" colab={"base_uri": "https://localhost:8080/", "height": 112}
learn.lr_find()

# + id="kyMpS0q7pzGo" colab_type="code" outputId="f1ac6d1f-f334-4a82-a501-732720d15e63" colab={"base_uri": "https://localhost:8080/", "height": 313}
learn.recorder.plot(suggestion=True)

# + id="qeS3SYc8p365" colab_type="code" colab={}
learn.metrics = [mean_squared_error, mean_absolute_error, r2_score, explained_variance]

# + id="qH-DZFtWqyPV" colab_type="code" outputId="3926a4ec-b481-4c4e-f635-3ffaac8f6875" colab={"base_uri": "https://localhost:8080/", "height": 359}
learn.fit_one_cycle(10, max_lr = 1e-03)

# + [markdown] id="s9bTohzqsrjY" colab_type="text"
# ## Results

# + [markdown] id="vwiEn-NmsvOq" colab_type="text"
# ### Training

# + id="DulbmNm1ssvZ" colab_type="code" outputId="a9fe52d0-1418-4f9e-e397-eda812fdc252" colab={"base_uri": "https://localhost:8080/", "height": 1000}
learn.show_results(ds_type=DatasetType.Train)

# + [markdown] id="28NU7J6NszY6" colab_type="text"
# ### Validation

# + id="RE4Bz2hLsBPV" colab_type="code" outputId="9bf15d15-4e24-496b-a998-366be5fa675b" colab={"base_uri": "https://localhost:8080/", "height": 1000}
learn.show_results(ds_type=DatasetType.Valid)

# + id="6hbyvYknGDUs" colab_type="code" colab={}
torch.save(autoencoder, "/content/drive/My Drive/autoencoder-training/pixelShuffle-Cifar10.pt")

StarcoderdataJupyterScriptsDedupFiltered