diff --git a/README.md b/README.md index 95d9d69..edd4853 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,7 @@ Each notebook will teach a maximum of 3 big ideas. See the project page for work-in-progress board - https://github.com/users/mrdbourke/projects/1 -* **Working on:** making materials for section 07: experiment tracking +* **Working on:** making materials for section 07: experiment tracking + section 08: paper replicating * **Total video count:** 186 * **Done skeleton code for:** 00, 01, 02, 03, 04, 05, 06, 07 * **Done annotations (text) for:** 00, 01, 02, 03, 04, 05, 06 @@ -71,6 +71,7 @@ See the project page for work-in-progress board - https://github.com/users/mrdbo Almost daily updates of what's happening. +* 17 June 2022 - fix up 06, 07 v2 for upcoming torchvision version upgrade, add plenty of annotations to 08 * 13 June 2022 - add notebook 08 first version, starting to replicate the Vision Transformer paper * 10 June 2022 - add annotations for 07 v2 * 09 June 2022 - create 07 v2 for `torchvision` v0.13 (this will replace 07 v1 when `torchvision=0.13` is released) diff --git a/helper_functions.py b/helper_functions.py index d53ca00..6d7745d 100644 --- a/helper_functions.py +++ b/helper_functions.py @@ -9,6 +9,13 @@ import numpy as np from torch import nn +import os +import zipfile + +from pathlib import Path + +import requests + def plot_decision_boundary(model: torch.nn.Module, X: torch.Tensor, y: torch.Tensor): """Plots decision boundaries of model predicting on X in comparison to y. @@ -211,3 +218,59 @@ def pred_and_plot_image( plt.title(title) plt.axis(False) +def set_seeds(seed: int=42): + """Sets random sets for torch operations. + + Args: + seed (int, optional): Random seed to set. Defaults to 42. + """ + # Set the seed for general torch operations + torch.manual_seed(seed) + # Set the seed for CUDA torch operations (ones that happen on the GPU) + torch.cuda.manual_seed(seed) + +def download_data(source: str, + destination: str, + remove_source: bool = True) -> Path: + """Downloads a zipped dataset from source and unzips to destination. + + Args: + source (str): A link to a zipped file containing data. + destination (str): A target directory to unzip data to. + remove_source (bool): Whether to remove the source after downloading and extracting. + + Returns: + pathlib.Path to downloaded data. + + Example usage: + download_data(source="https://github.com/mrdbourke/pytorch-deep-learning/raw/main/data/pizza_steak_sushi.zip", + destination="pizza_steak_sushi") + """ + # Setup path to data folder + data_path = Path("data/") + image_path = data_path / destination + + # If the image folder doesn't exist, download it and prepare it... + if image_path.is_dir(): + print(f"[INFO] {image_path} directory exists, skipping download.") + else: + print(f"[INFO] Did not find {image_path} directory, creating one...") + image_path.mkdir(parents=True, exist_ok=True) + + # Download pizza, steak, sushi data + target_file = Path(source).name + with open(data_path / target_file, "wb") as f: + request = requests.get(source) + print(f"[INFO] Downloading {target_file} from {source}...") + f.write(request.content) + + # Unzip pizza, steak, sushi data + with zipfile.ZipFile(data_path / target_file, "r") as zip_ref: + print(f"[INFO] Unzipping {target_file} data...") + zip_ref.extractall(image_path) + + # Remove .zip file + if remove_source: + os.remove(data_path / target_file) + + return image_path \ No newline at end of file