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

# # Tutorial N: Title
#
# **Filled notebook:** 
# [![View on Github](https://img.shields.io/static/v1.svg?logo=github&label=Repo&message=View%20On%20Github&color=lightgrey)](https://github.com/phlippe/uvadlc_notebooks/blob/master/docs/tutorial_notebooks/DL2/template/TemplateNotebook.ipynb)
# [![Open In Collab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/phlippe/uvadlc_notebooks/blob/master/docs/tutorial_notebooks/DL2/template/TemplateNotebook.ipynb)  
# **Pre-trained models:** 
# [![View files on Github](https://img.shields.io/static/v1.svg?logo=github&label=Repo&message=View%20On%20Github&color=lightgrey)](https://github.com/phlippe/saved_models/tree/main/DL2/template/)   
# **Recordings:** 
# [![YouTube - Part N](https://img.shields.io/static/v1.svg?logo=youtube&label=YouTube&message=Part%20N&color=red)](https://youtu.be/waVZDFR-06U)    
# **Authors:**
# Your name here

# __TODOs:__
#
# * Update the links for the filled notebook (both github and collab) to your new notebook
# * Update the link for the saved models
# * Update the link for the YouTube recording if you have any. If you want to upload one to the UvA DLC YouTube account, you can contact Phillip.
# * Fill in the author names

# Here, you are supposed to add some intro about the topic. Give a short abstract motivating the tutorial, and then detail what will be done. It is good to have pictures here as well. If you add images, make sure to use SVGs for best resolution, and put them in the same folder as your notebook. An example is given below (use any HTML editing you like).
#
# <center width="100%"><img src="example_image.svg" width="350px" style="padding: 20px"></center>
#
# The next cell is where you import all your packages that you need. In case you have non-standard packages, make sure to install them to make it executable on GoogleColab (see for instance the PyTorch Lightning install).

# +
## Standard libraries
import os
import numpy as np

## Imports for plotting
import matplotlib.pyplot as plt
plt.set_cmap('cividis')
# %matplotlib inline
from IPython.display import set_matplotlib_formats
set_matplotlib_formats('svg', 'pdf') # For export
import matplotlib
matplotlib.rcParams['lines.linewidth'] = 2.0
import seaborn as sns
sns.set()

## tqdm for loading bars
from tqdm.notebook import tqdm

## PyTorch
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data as data
import torch.optim as optim

## Torchvision  (TODO: ONLY NEEDED FOR VISION-BASED DATASETS)
import torchvision
from torchvision import transforms

# PyTorch Lightning
try:
    import pytorch_lightning as pl
except ModuleNotFoundError: # Google Colab does not have PyTorch Lightning installed by default. Hence, we do it here if necessary
    # !pip install --quiet pytorch-lightning>=1.4
    import pytorch_lightning as pl
from pytorch_lightning.callbacks import LearningRateMonitor, ModelCheckpoint

# Import tensorboard  (TODO: REMOVE IF YOU DO NOT WANT TO RUN TENSORBOARDS INTERACTIVELY)
# %load_ext tensorboard

# Path to the folder where the datasets are/should be downloaded (e.g. CIFAR10)
DATASET_PATH = "../data"
# Path to the folder where the pretrained models are saved   (TODO: UPDATE LINK BELOW TO A FOLDER WITH THE NAME OF YOUR NOTEBOOK FOLDER)
CHECKPOINT_PATH = "../../saved_models/DL2/template"

# Setting the seed
pl.seed_everything(42)

# Ensure that all operations are deterministic on GPU (if used) for reproducibility
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False

device = torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
print("Device:", device)
# -

# You will likely have some pretrained models that you want to share with the students, and download when running on GoogleColab. You can do this with the cell below. If you don't have any pretrained models, you can remove the cell.

# +
import urllib.request
from urllib.error import HTTPError
# Github URL where saved models are stored for this tutorial  (TODO: UPDATE URL BELOW TO YOUR NOTEBOOK FOLDER)
base_url = "https://raw.githubusercontent.com/phlippe/saved_models/main/DL2/template/"
# Files to download
pretrained_files = []  # (TODO: ADD A LIST OF STRINGS THAT ARE THE FILES YOU WANT TO DOWNLOAD. PATHS WITH RESPECT TO BASE_URL)
# Create checkpoint path if it doesn't exist yet
os.makedirs(CHECKPOINT_PATH, exist_ok=True)

# For each file, check whether it already exists. If not, try downloading it.
for file_name in pretrained_files:
    file_path = os.path.join(CHECKPOINT_PATH, file_name)
    if "/" in file_name:
        os.makedirs(file_path.rsplit("/",1)[0], exist_ok=True)
    if not os.path.isfile(file_path):
        file_url = base_url + file_name
        print(f"Downloading {file_url}...")
        try:
            urllib.request.urlretrieve(file_url, file_path)
        except HTTPError as e:
            print("Something went wrong. Please try later again, or contact the author with the full output including the following error:\n", e)
# -

# ## My Tutorial Topic
#
# Start your notebook from here. Introduce the topics, go step by step, don't forget to explain the code, etc.
#
# You can make use of different heading levels, they will be shown as tabs on the RTD website.

# ## Conclusion
#
# Give a conclusion and summary of the notebook. Give a retroview: what have the students learned from this notebook, what is there to further explore in this topic, anything critical to keep in mind?
#
# ### References
#
# Give a list of references, especially the papers that introduce the methods you implemented in this notebook.

StarcoderdataJupyterScriptsDedupFiltered