### Cloning the Clay Foundation Model Repository (Bash) Source: https://github.com/clay-foundation/model/blob/main/docs/getting-started/installation.md These commands clone the Clay Foundation Model repository from GitHub to the local machine and then change the current directory into the newly cloned 'model' folder, preparing the environment for subsequent installation steps. ```bash git clone https://github.com/Clay-foundation/model cd model ``` -------------------------------- ### Verifying Mamba Environment Installation (Bash) Source: https://github.com/clay-foundation/model/blob/main/docs/getting-started/installation.md This command lists all packages installed within the currently active Mamba virtual environment. It serves as a crucial final check to confirm that all required libraries and dependencies for the Clay Foundation Model have been successfully installed and are accessible. ```bash mamba list ``` -------------------------------- ### Creating Mamba Environment for Clay Foundation Model (Bash) Source: https://github.com/clay-foundation/model/blob/main/docs/getting-started/installation.md This command utilizes Mamba to create a new virtual environment based on the specifications in the 'environment.yml' file. This ensures that all necessary Python packages and JupyterLab are installed, fulfilling the project's dependencies. ```bash mamba env create --file environment.yml ``` -------------------------------- ### Activating Clay Foundation Model Mamba Environment (Bash) Source: https://github.com/clay-foundation/model/blob/main/docs/getting-started/installation.md This command activates the 'claymodel' virtual environment, which was previously created by Mamba. Activating the environment ensures that all subsequent commands and operations use the Python interpreter and libraries installed within this specific isolated environment. ```bash mamba activate claymodel ``` -------------------------------- ### Setting up and Running Jupyter Lab for Clay Model Source: https://github.com/clay-foundation/model/blob/main/docs/getting-started/basic_use.md This snippet outlines the steps to activate the 'claymodel' mamba environment, install the ipykernel for Jupyter, verify kernel installation, and finally launch Jupyter Lab in the background. It prepares the environment for interactive model development. ```Shell mamba activate claymodel python -m ipykernel install --user --name claymodel # to install virtual env properly jupyter kernelspec list --json # see if kernel is installed jupyter lab & ``` -------------------------------- ### Setting Up Development Environment for Clay Model Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/classify.md This snippet provides the bash commands to clone the Clay model repository, navigate into its directory, create a dedicated development environment using `mamba` from the `environment.yml` file, and activate it. This ensures all necessary dependencies are installed and ready for use. ```bash git clone cd model mamba env create --file environment.yml mamba activate claymodel ``` -------------------------------- ### Setting Up Clay Model Environment (Bash) Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/segment.md This snippet provides commands to clone the Clay model repository, navigate into its directory, create a new Mamba environment from `environment.yml`, and activate it. This sets up the necessary Python environment for running the Clay model and its fine-tuning examples. ```bash git clone cd model mamba env create --file environment.yml mamba activate claymodel ``` -------------------------------- ### Initial Environment Setup - Python Source: https://github.com/clay-foundation/model/blob/main/finetune/segment/chesapeake_inference.ipynb This snippet configures the Python environment by adding the parent directory to the system path to resolve module imports and suppresses warnings for cleaner output during execution. ```Python import sys import warnings sys.path.append("../../") warnings.filterwarnings("ignore") ``` -------------------------------- ### Launching Jupyter Lab with Clay Model Kernel - Shell Source: https://github.com/clay-foundation/model/blob/main/README.md This sequence of commands activates the `claymodel` environment, installs the `ipykernel` for Jupyter to recognize the virtual environment, lists available kernels to verify installation, and then launches Jupyter Lab in the background. This sets up the interactive development environment. ```Shell mamba activate claymodel python -m ipykernel install --user --name claymodel # to install virtual env properly jupyter kernelspec list --json # see if kernel is installed jupyter lab & ``` -------------------------------- ### Installing 7-Zip for Data Extraction (Bash) Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/regression.md This command installs the `p7zip-full` package on a Debian/Ubuntu-based system. This utility is required to handle multi-file zip archives, such as those used for the BioMassters dataset, which are too large for standard zip tools. ```bash sudo apt install p7zip-full ``` -------------------------------- ### Previewing Local Documentation Site - Python Source: https://github.com/clay-foundation/model/blob/main/README.md This command starts a simple HTTP server using Python's built-in `http.server` module. It serves the static documentation files located in the `_build/html` directory, allowing users to preview the generated documentation site locally in a web browser. ```Shell python -m http.server --directory _build/html ``` -------------------------------- ### Installing Lonboard Library in Python Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/finetune-on-embeddings.ipynb This snippet provides a commented-out command to install the `lonboard` library using pip. It's a prerequisite for visualizing geospatial data in the notebook, ensuring the environment has the necessary dependencies. ```python # If not installed, add lonboard to the environment by uncommenting the following line # ! pip install lonboard ``` -------------------------------- ### Listing Installed Libraries in Mamba Environment - Shell Source: https://github.com/clay-foundation/model/blob/main/README.md This command lists all packages and their versions installed within the currently active `mamba` virtual environment. It's used to verify that all required libraries have been successfully installed after environment creation. ```Shell mamba list ``` -------------------------------- ### Running Datacube Generation Script (CLI) Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/data_datacube.md This command-line example executes the `datacube.py` script to generate a datacube. It specifies an MGRS sample file, an S3 bucket for output, a pixel window for subsetting data (for testing/debugging), and the row index of the MGRS tile to process. ```bash python datacube.py --sample /home/user/Desktop/mgrs_sample.fgb --bucket "my-bucket" --subset "1000,1000,2000,2000" --index 1 ``` -------------------------------- ### Installing Jupyter Book - Python Source: https://github.com/clay-foundation/model/blob/main/README.md This command installs or upgrades the `jupyter-book` package using `pip`. Jupyter Book is used for building the project's documentation, allowing for the creation of interactive and executable books from Jupyter notebooks and other content. ```Shell pip install -U jupyter-book ``` -------------------------------- ### Initializing ONNX Runtime Session for CPU (Python) Source: https://github.com/clay-foundation/model/blob/main/finetune/embedder/how-to-embed.ipynb This snippet first ensures the `datacube` tensors are on the CPU. Then, it initializes an `onnxruntime.InferenceSession` for the downloaded ONNX model, explicitly specifying `CPUExecutionProvider` to ensure inference runs on the CPU. This setup prepares the model for execution. ```python datacube = {k: v.to("cpu") for k, v in datacube.items()} onnx_embedder = ort.InferenceSession( "clay-v1.5-encoder-cpu.onnx", providers=["CPUExecutionProvider"] ) ``` -------------------------------- ### Example Batch Dictionary for Finetuning Data - Python Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/model_finetuning.md This dictionary illustrates the structure of a data batch prepared for finetuning the Clay model. It includes 'labels' for segmentation masks, 'pixels' for input image data, and metadata like 'bbox', 'epsg', 'date', 'latlon', 'timestep', and 'source_url'. The 'labels' and 'pixels' are PyTorch tensors, indicating the data format expected by the model. ```Python {'labels': tensor([[[[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]]]), 'pixels': tensor([[[[-0.5994, -0.6108, -0.6034, ..., -0.5610, -0.5590, -0.5614], [-0.5767, -0.5950, -0.6004, ..., -0.5619, -0.5536, -0.5610], [-0.5841, -0.5762, -0.5930, ..., -0.5491, -0.5304, -0.5373], ..., [-0.5087, -0.5447, -0.4351, ..., -0.6162, -0.6083, -0.6044], [-0.4184, -0.5432, -0.5003, ..., -0.6108, -0.6128, -0.6073], [-0.2496, -0.5348, -0.5225, ..., -0.6137, -0.6167, -0.6128]], [[-0.6371, -0.6435, -0.6425, ..., -0.5834, -0.5898, -0.5923], [-0.6296, -0.6410, -0.6385, ..., -0.5794, -0.5983, -0.5958], [-0.6167, -0.6177, -0.6182, ..., -0.5545, -0.5913, -0.5834], ..., [-0.4800, -0.5153, -0.4308, ..., -0.6525, -0.6410, -0.6331], [-0.4104, -0.5034, -0.4318, ..., -0.6331, -0.6226, -0.6087], [-0.2404, -0.5222, -0.4522, ..., -0.6231, -0.6241, -0.6177]], [[-0.7068, -0.7217, -0.7101, ..., -0.6118, -0.6178, -0.6290], [-0.7087, -0.7022, -0.6924, ..., -0.6141, -0.6146, -0.6234], [-0.7017, -0.6998, -0.6831, ..., -0.5927, -0.6085, -0.6104], ..., [-0.5563, -0.5480, -0.4571, ..., -0.7106, -0.7045, -0.6933], [-0.4725, -0.5526, -0.4781, ..., -0.6975, -0.6789, -0.6807], [-0.3117, -0.4995, -0.5000, ..., -0.6952, -0.6835, -0.6845]], ..., ]), 'bbox': tensor([[ 661415., 5369305., 666535., 5374425.]], dtype=torch.float64), 'epsg': tensor([32633], dtype=torch.int32), 'date': ['2020-10-20'], 'latlon': tensor([[-0.8192, -0.7854]]), 'timestep': tensor([[-1.2217, 2.7132, -2.4086]]), 'source_url': ['S2A_L2A_20201022T100051_N0209_R122_T33UXP_20201022T111023_06144-02560_S1B_IW_GRDH_1SDV_20201020T164222_20201020T164247_023899_02D6C4_rtc']} ``` -------------------------------- ### Changing Working Directory for Clay Project Setup Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/partial-inputs.ipynb This snippet ensures that the current working directory is set to the repository's root, which is a common prerequisite for running scripts within a project structure, allowing relative paths to function correctly. ```python # Ensure working directory is the repo home import os os.chdir("..") ``` -------------------------------- ### Clay v0 Model Configuration Parameters Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/specification-v0.md This snippet outlines the detailed configuration parameters for the `clay_small` model setup used in Clay v0. It specifies key training parameters such as the masked patch percentage, input and patch dimensions, and comprehensive settings for the Adam optimizer (learning rate, weight decay, beta values). Additionally, it defines the CosineAnnealingWarmRestarts scheduler parameters and the architectural specifications for both the encoder and decoder, including dimensions, depth, number of heads, and dropout rates. ```Configuration MASKED PATCHES = 75% INPUT SIZE = 13 bands x 512 width x 512 height PATCH SIZE = 32 x 32 OPTIMIZER Adam Learning rate = 1e-4 Weight decay = 0.05 Beta 1 = 0.9 Beta 2 = 0.95 SCHEDULER CosineAnnealingWarmRestarts T_0 = 1000 T_mult = 2 eta_min = Learning rate * 10 ENCODER dim = 768 depth = 12 heads = 12 dim_head = 64 mlp_ratio = 4 dropout = 0.0 emb_dropout = 0.0 DECODER decoder_dim = 512 decoder_depth = 8 decoder_heads = 8 decoder_dim_head = 64 decoder_mlp_ratio = 4 decoder_dropout = 0.0 ``` -------------------------------- ### Loading Clay Model and Data Module for Embeddings - Python Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/partial-inputs-flood-tutorial.ipynb This Python snippet initializes the Clay model by loading a pre-trained checkpoint from Hugging Face, configuring it for specific band groups and setting it to evaluation mode. It also defines and sets up a custom `ClayDataModuleMulti` with specific normalization parameters (MEAN/STD) for loading data from a specified directory, preparing it for prediction. This setup is crucial for preparing the model and data for embedding generation. ```Python DATA_DIR = "data/minicubes" CKPT_PATH = "https://huggingface.co/made-with-clay/Clay/resolve/main/Clay_v0.1_epoch-24_val-loss-0.46.ckpt" # Load model multi_model = CLAYModule.load_from_checkpoint( CKPT_PATH, mask_ratio=0.0, band_groups={"rgb": (2, 1, 0), "nir": (3,), "swir": (4, 5)}, bands=6, strict=False, # ignore the extra parameters in the checkpoint ) # Set the model to evaluation mode multi_model.eval() # Load the datamodule, with the reduced set of class ClayDataModuleMulti(ClayDataModule): MEAN = [ 1369.03, # red 1597.68, # green 1741.10, # blue 2893.86, # nir 2303.00, # swir16 1807.79, # swir22 ] STD = [ 2026.96, # red 2011.88, # green 2146.35, # blue 1917.12, # nir 1679.88, # swir16 1568.06, # swir22 ] data_dir = Path(DATA_DIR) dm = ClayDataModuleMulti(data_dir=str(data_dir.absolute()), batch_size=2) dm.setup(stage="predict") trn_dl = iter(dm.predict_dataloader()) ``` -------------------------------- ### Loading Training Data for Classification in Python Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/finetune-on-embeddings.ipynb This snippet loads a GeoJSON file containing training data points for classification. It defaults to loading 'marinas.geojson' but includes a commented-out line to switch to 'baseball.geojson'. This data will be spatially joined with the embeddings to create labeled examples for model training, with a 'class' column indicating positive (1) or negative (0) examples. ```python # Open marinas training data points = gpd.read_file( "../../data/classify-embeddings-sf-baseball-marinas/marinas.geojson" ) # Uncomment this to use the baseball training dataset. # points = gpd.read_file( # "../../data/classify-embeddings-sf-baseball-marinas/baseball.geojson" # ) ``` -------------------------------- ### Displaying Trainer CLI Help - Python Source: https://github.com/clay-foundation/model/blob/main/README.md This command executes the `trainer.py` script with the `--help` flag, which displays all available options, hyperparameters, and configurations for running the neural network model via `LightningCLI v2`. It's useful for understanding command-line arguments. ```Shell python trainer.py --help ``` -------------------------------- ### Activating Mamba Virtual Environment - Shell Source: https://github.com/clay-foundation/model/blob/main/README.md This command activates the `claymodel` virtual environment previously created by `mamba`. Activating the environment ensures that all subsequent commands use the Python interpreter and libraries installed within this specific environment. ```Shell mamba activate claymodel ``` -------------------------------- ### Building Project Documentation - Shell Source: https://github.com/clay-foundation/model/blob/main/README.md This command builds the project's documentation using Jupyter Book. It processes the source files located in the `docs/` directory and generates the static HTML site, which can then be previewed or deployed. ```Shell jupyter-book build docs/ ``` -------------------------------- ### Displaying Image Chips for Cluster 1 - Python Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/clay-v0-location-embeddings.ipynb This snippet calls the `show_cluster` function to display specific image chips identified by their indices (87, 37, 40). This visualizes examples from a particular cluster, helping to understand the semantic content captured by the location embeddings. ```python show_cluster((87, 37, 40)) ``` -------------------------------- ### Initializing Clay DataModule and DataLoader Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/clay-v0-interpolation.ipynb Initializes a `ClayDataset` by globbing all `.tif` files within the `DATA_DIR` to find image chips. A `ClayDataModule` is then set up with the data directory and a batch size of 2. Finally, `dm.setup(stage="fit")` prepares the data, and an iterator for the training DataLoader (`trn_dl`) is created to fetch batches. ```python data_dir = Path(DATA_DIR) # Load the Clay DataModule ds = ClayDataset(chips_path=list(data_dir.glob("**/*.tif"))) dm = ClayDataModule(data_dir=str(data_dir), batch_size=2) dm.setup(stage="fit") # Load the train DataLoader trn_dl = iter(dm.train_dataloader()) ``` -------------------------------- ### Displaying Clay Model Training Options (Python) Source: https://github.com/clay-foundation/model/blob/main/docs/getting-started/basic_use.md This command uses the LightningCLI to display all available command-line options and hyperparameter configurations for the 'trainer.py' script. It's useful for understanding the model's configurable parameters. ```Python python trainer.py --help ``` -------------------------------- ### Training Classification Head with PyTorch Lightning CLI Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/classify.md This bash command initiates the training process for the classification head using the PyTorch Lightning Command Line Interface (CLI). It executes the `classify.py` script located in `finetune/classify` and applies the configurations defined in `configs/classify_eurosat.yaml`, ensuring the repository root is in the Python path for proper imports. ```bash python -m finetune.classify.classify fit --config configs/classify_eurosat.yaml ``` -------------------------------- ### Displaying First Image Sample Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/clay-v0-interpolation.ipynb Calls the `show` utility function to display the first extracted image sample (`sample1`). This visualizes the raw pixel data of the first image in the batch without saving it to a file. ```python show(sample1) ``` -------------------------------- ### Defining Area of Interest (AOI) and Date Range - Python Source: https://github.com/clay-foundation/model/blob/main/finetune/embedder/how-to-embed.ipynb This snippet defines the geographic coordinates (latitude, longitude) for the Area of Interest (AOI) and the start and end dates for data retrieval, specifically targeting a period of a large forest fire in Monchique, Portugal. ```python # Point over Monchique Portugal lat, lon = 37.30939, -8.57207 # Dates of a large forest fire start = "2018-07-01" end = "2018-09-01" ``` -------------------------------- ### Decoder Network Architecture - PyTorch Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/model_finetuning.md This snippet shows the initial layers of the decoder network used for the downstream segmentation task. It's a PyTorch `Sequential` model starting with a `Conv2d` layer, which processes the embeddings generated by the Clay model's encoder. This decoder is finetuned to compute loss and adapt to the specific dataset. ```Python Model( (decoder): Sequential( (0): Conv2d(4608, 64, kernel_size=(1, 1), stride=(1, 1)) ``` -------------------------------- ### Training Regression Model using Finetune (Bash) Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/regression.md This command executes the `finetune.regression.regression` module as a script to train a regression model. It uses the `fit` subcommand and specifies the configuration file `configs/regression_biomasters.yaml` for model parameters and training settings. Ensure the repository root is in the Python path for correct module imports. ```bash python -m finetune.regression.regression fit --config configs/regression_biomasters.yaml ``` -------------------------------- ### Visualizing Training Data with PolygonLayer (Python) Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/finetune-on-embeddings.ipynb This code snippet visualizes the merged training data using `PolygonLayer` from `geopandas`. It applies a categorical colormap to differentiate between positive (green squares) and negative (blue squares) examples based on the 'class' column. The resulting layer is then added to a `Map` object for display. ```python training_layer = PolygonLayer.from_geopandas( merged, get_fill_color=apply_categorical_cmap( merged["class"], {0: [0, 150, 255, 100], 1: [0, 255, 150, 150]} ), get_line_color=[0, 100, 100, 0], ) m = Map(training_layer) m ``` -------------------------------- ### Downloading Clay Embedder ExportedProgram Model for CPU (Python) Source: https://github.com/clay-foundation/model/blob/main/finetune/embedder/how-to-embed.ipynb This snippet downloads the Clay embedder model, specifically the CPU-optimized version in ExportedProgram format, from a Hugging Face repository. It uses `wget` to fetch the `.pt2` file, which is a compiled PyTorch model. ```python !wget -q https://huggingface.co/made-with-clay/Clay/resolve/main/v1.5/compiled/clay-v1.5-encoder-cpu.pt2 ``` -------------------------------- ### Performing Spatial Join of Embeddings with Training Points (Python) Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/finetune-on-embeddings.ipynb This snippet performs a spatial join operation between an 'embeddings' GeoDataFrame and 'points' GeoDataFrame. It then prints statistics about the merged dataset, including the total number of embeddings found, marked (positive) locations, and negative examples. The 'merged' GeoDataFrame is also displayed. ```python merged = embeddings.sjoin(points) print(f"Found {len(merged)} embeddings to train on") print(f"{sum(merged['class'])} marked locations") print(f"{len(merged) - sum(merged['class'])} negative examples") merged ``` -------------------------------- ### Displaying Image Chips for Cluster 2 - Python Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/clay-v0-location-embeddings.ipynb This code calls the `show_cluster` function to display another set of specific image chips (23, 11, 41). This further demonstrates the semantic information captured by location embeddings by showing examples from a different cluster, allowing for visual comparison. ```python show_cluster((23, 11, 41)) ``` -------------------------------- ### Training Segmentation Model with LightningCLI (Bash) Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/segment.md This command initiates the model training process using PyTorch Lightning's CLI. It runs the `segment` module within `finetune/segment` in 'fit' mode, loading configurations from `configs/segment_chesapeake.yaml`. Ensure the repository root is in the Python path for correct imports. ```bash python -m finetune.segment.segment fit --config configs/segment_chesapeake.yaml ``` -------------------------------- ### Loading Clay Embedder ExportedProgram Model on CPU (Python) Source: https://github.com/clay-foundation/model/blob/main/finetune/embedder/how-to-embed.ipynb This snippet loads the previously downloaded Clay embedder model (`.pt2` file) into memory using `torch.export.load()`. The `.module()` call prepares the loaded program for inference on the CPU. The `%%time` magic command measures execution time. ```python %%time ep_embedder_cpu = torch.export.load("clay-v1.5-encoder-cpu.pt2").module() ``` -------------------------------- ### Visualizing Second Short-Wave Infrared (SWIR22) Band - Python Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/partial-inputs-flood-tutorial.ipynb This snippet displays the second Short-Wave Infrared (SWIR22) band from the satellite imagery stack. It shows a similar pattern to the SWIR16 band, with a strong signal indicating flood presence starting around August 26th. This band further supports the identification and analysis of flood extent. ```python stack.sel(band=["swir22", "swir22", "swir22"]).plot.imshow( row="time", rgb="band", vmin=0, vmax=2000, col_wrap=6 ) ``` -------------------------------- ### Packaging and Uploading Worldcover Batch Application to S3 (Bash) Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/worldcover-embeddings.md This Bash script prepares the application for AWS Batch by zipping source code and scripts, including the `run.py` entry point, and then uploads the resulting `batch-fetch-and-run-wc.zip` package to the `clay-fetch-and-run-packages` S3 bucket. This package is used by AWS Batch's fetch-and-run job definition. ```bash # Add clay src and scripts to zip file zip -FSr batch-fetch-and-run-wc.zip src scripts -x *.pyc -x scripts/worldcover/wandb/**\* # Add run to home dir, so that fetch-and-run can see it. zip -uj batch-fetch-and-run-wc.zip scripts/worldcover/run.py # Upload fetch-and-run package to S3 aws s3api put-object --bucket clay-fetch-and-run-packages --key "batch-fetch-and-run-wc.zip" --body "batch-fetch-and-run-wc.zip" ``` -------------------------------- ### Training Clay Model with Specified Configuration (Python) Source: https://github.com/clay-foundation/model/blob/main/docs/getting-started/basic_use.md This command initiates the full training process for the 'ClayMAEModule' using the 'ClayDataModule' and the provided 'config.yaml' file. It's the standard command for training the neural network model. ```Python python trainer.py fit --model ClayMAEModule --data ClayDataModule --config configs/config.yaml ``` -------------------------------- ### Training Clay Foundation Model - Python Source: https://github.com/clay-foundation/model/blob/main/README.md This command initiates the full training process for the Clay Foundation Model. It uses `LightningCLI v2` to fit the `ClayMAEModule` model with data from `ClayDataModule`, applying the specified configurations from `configs/config.yaml`. ```Shell python trainer.py fit --model ClayMAEModule --data ClayDataModule --config configs/config.yaml ``` -------------------------------- ### Setting Up Python Path Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/clay-v0-interpolation.ipynb Appends the parent directory to the Python system path, allowing the script to import custom modules from the project root, specifically those located in the 'src' directory. ```python import sys sys.path.append("../") ``` -------------------------------- ### Quick Testing Clay Model on a Single Validation Batch (Python) Source: https://github.com/clay-foundation/model/blob/main/docs/getting-started/basic_use.md This command runs a quick test of the 'ClayMAEModule' using 'ClayDataModule' and a specified configuration file. The '--trainer.fast_dev_run=True' flag ensures the model processes only one batch from the validation set, ideal for rapid development and debugging. ```Python python trainer.py fit --model ClayMAEModule --data ClayDataModule --config configs/config.yaml --trainer.fast_dev_run=True ``` -------------------------------- ### Import CLAY Model and Data Dependencies in Python Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/clay-v0-reconstruction.ipynb Imports necessary libraries for data handling, model definition, and visualization. Key imports include `Path` for file system operations, `torch` for deep learning, `einops` for tensor manipulation, `matplotlib` for plotting, `pandas` for data structures, and custom modules `ClayDataModule`, `ClayDataset`, and `CLAYModule` from the `src` directory. ```python from pathlib import Path import einops import matplotlib.pyplot as plt import pandas as pd import torch from einops import rearrange from src.datamodule import ClayDataModule, ClayDataset from src.model_clay import CLAYModule ``` -------------------------------- ### Copying Chesapeake Bay Dataset Files (Bash) Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/segment.md These `s5cmd` commands are used to download and copy specific image and label files (`*_lc.tif` and `*_naip-new.tif`) from an S3 bucket to local `data/cvpr/files/train/` and `data/cvpr/files/val/` directories. This step prepares the raw dataset for subsequent preprocessing. ```bash s5cmd --no-sign-request cp --include "*_lc.tif" --include "*_naip-new.tif" "s3://us-west-2.opendata.source.coop/agentmorris/lila-wildlife/lcmcvpr2019/cvpr_chesapeake_landcover/ny_1m_2013_extended-debuffered-train_tiles/*" data/cvpr/files/train/ s5cmd --no-sign-request cp --include "*_lc.tif" --include "*_naip-new.tif" "s3://us-west-2.opendata.source.coop/agentmorris/lila-wildlife/lcmcvpr2019/cvpr_chesapeake_landcover/ny_1m_2013_extended-debuffered-val_tiles/*" data/cvpr/files/val/ ``` -------------------------------- ### Packaging Scripts and Uploading to S3 for Batch Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/data_datacube.md These commands package the necessary Python scripts for the datacube pipeline into a ZIP file and upload it to an S3 bucket. The `zip` command creates an archive containing files from the `scripts/pipeline` directory, excluding `.pyc` files. The `aws s3api put-object` command then uploads this ZIP file to a specified S3 bucket, making it available for AWS Batch jobs using the fetch-and-run approach. ```bash zip -FSrj "batch-fetch-and-run.zip" ./scripts/pipeline* -x "scripts/pipeline*.pyc" aws s3api put-object --bucket clay-fetch-and-run-packages --key "batch-fetch-and-run.zip" --body "batch-fetch-and-run.zip" ``` -------------------------------- ### Downloading and Visualizing Sentinel-2 Imagery in Python Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/patch_level_cloud_cover.ipynb This snippet extracts the coordinate system, converts the area of interest to the target projection, and defines bounds for a 512x512 pixel image at 10m resolution. It then uses `stackstac` to retrieve and stack RGB and SCL bands, computes the stack, and visualizes the RGB imagery, asserting the correct shape. ```python # Extract coordinate system from first item epsg = items_L2A[0].properties["proj:epsg"] # Convert point from lon/lat to UTM projection poidf = gpd.GeoDataFrame(crs="OGC:CRS84", geometry=[area_of_interest.centroid]).to_crs( epsg ) geom = poidf.iloc[0].geometry # Create bounds of the correct size, the model # requires 512x512 pixels at 10m resolution. bounds = (geom.x - 2560, geom.y - 2560, geom.x + 2560, geom.y + 2560) # Retrieve the pixel values, for the bounding box in # the target projection. In this example we use only # the RGB and SCL band groups. stack_L2A = stackstac.stack( items_L2A[0], bounds=bounds, snap_bounds=False, epsg=epsg, resolution=10, dtype="float32", rescale=False, fill_value=0, assets=BAND_GROUPS_L2A["rgb"] + BAND_GROUPS_L2A["scl"], resampling=Resampling.nearest, xy_coords="center", ) stack_L2A = stack_L2A.compute() print(stack_L2A.shape) assert stack_L2A.shape == (1, 4, 512, 512) stack_L2A.sel(band=["red", "green", "blue"]).plot.imshow( row="time", rgb="band", vmin=0, vmax=2000 ) ``` -------------------------------- ### Displaying Second Image Sample Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/clay-v0-interpolation.ipynb Calls the `show` utility function to display the second extracted image sample (`sample2`). This visualizes the raw pixel data of the second image in the batch without saving it to a file. ```python show(sample2) ``` -------------------------------- ### Loading Clay Embedder ExportedProgram Model on GPU (Python) Source: https://github.com/clay-foundation/model/blob/main/finetune/embedder/how-to-embed.ipynb This snippet loads the GPU-optimized Clay embedder model (`.pt2` file) using `torch.export.load()`. The model is then prepared for inference on the GPU. The `%%time` magic command measures the loading duration. ```python %%time ep_embedder = torch.export.load("clay-v1.5-encoder.pt2").module() ``` -------------------------------- ### Downloading Imagery Chips with datacube.py (Bash) Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/run_region.md This bash loop executes the `datacube.py` script for a range of MGRS tile indices, downloading Sentinel-1 and Sentinel-2 imagery chips. It specifies the input MGRS AOI file, local output path, and date ranges for imagery acquisition. The `--subset` argument is used here for demonstration to reduce download size, but should be removed for full data downloads in real applications. ```bash for i in {0..5}; do python scripts/pipeline/datacube.py \ --sample data/mgrs/mgrs_aoi.fgb \ --localpath data/chips \ --index $i \ --dateranges 2020-01-01/2020-04-01,2021-06-01/2021-09-15 \ --subset 1500,1500,2524,2524; done ``` -------------------------------- ### Preprocessing Training Features for Regression (Python) Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/regression.md This Python script preprocesses the extracted training features, aggregating time steps by taking the average for each tile. It takes the input features directory, an output cubes directory, and uses 12 processes for parallel processing. The `--sample=1` argument indicates processing all samples, and `--overwrite` allows overwriting existing files. ```python python finetune/regression/preprocess_data.py \ --features=/home/tam/Desktop/biomasters/train_features/ \ --cubes=/home/tam/Desktop/biomasters/train_cubes/ \ --processes=12 \ --sample=1 \ --overwrite ``` -------------------------------- ### Building and Pushing Docker Image to ECR Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/data_datacube.md This sequence of bash commands builds a Docker image for the datacube pipeline and pushes it to an Amazon Elastic Container Registry (ECR) repository. It involves navigating to the Dockerfile directory, building the image with a specified ECR tag, authenticating Docker with ECR, and finally pushing the built image to the ECR repository. ```bash ecr_repo_id=12345 cd scripts/pipeline/batch docker build -t $ecr_repo_id.dkr.ecr.us-east-1.amazonaws.com/fetch-and-run . aws ecr get-login-password --profile clay --region us-east-1 | docker login --username AWS --password-stdin $ecr_repo_id.dkr.ecr.us-east-1.amazonaws.com docker push $ecr_repo_id.dkr.ecr.us-east-1.amazonaws.com/fetch-and-run ``` -------------------------------- ### Visualizing CLAY Model Reconstruction - Matplotlib - Python Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/clay-v0-reconstruction.ipynb This snippet visualizes the reconstruction process using Matplotlib, displaying four key stages: the initial masked input, the purely reconstructed chip, the reconstruction combined with unmasked patches, and the original input. It provides a clear visual comparison of the model's performance and the impact of the reconstruction. ```python fig, axes = plt.subplots(1, 4, figsize=(20, 5)) for ax in axes: ax.set_axis_off() axes[0].imshow(masked_chip) axes[0].set_title("Masked Input") axes[1].imshow(recreated_chip) axes[1].set_title("Reconstruction") axes[2].imshow(recreated_chip_with_unmasked_patches) axes[2].set_title("Reconstruction + Unmasked Patches") axes[3].imshow(chip[band]) axes[3].set_title("Original Input") ``` -------------------------------- ### Preprocessing Test Features for Regression (Python) Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/regression.md This Python script preprocesses the extracted test features, similar to the training features, by averaging all available timesteps per tile. It specifies the input features directory, an output cubes directory, and utilizes 12 processes for efficiency. The `--sample=1` argument ensures all samples are processed, and `--overwrite` allows overwriting existing files. ```python python finetune/regression/preprocess_data.py \ --features=/home/tam/Desktop/biomasters/test_features/ \ --cubes=/home/tam/Desktop/biomasters/test_cubes/ \ --processes=12 \ --sample=1 \ --overwrite ``` -------------------------------- ### Extracting Test AGBM Data with 7-Zip (Bash) Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/regression.md This command extracts the `test_agbm.tar` archive, containing Above Ground Biomass (AGBM) data for the test set, to the specified output directory. This data serves as the ground truth for evaluating the regression model's performance on unseen data. ```bash 7z e -o/home/tam/Desktop/biomasters/test_agbm/ /datadisk/biomasters/raw/test_agbm.tar ``` -------------------------------- ### Defining Image Visualization Utility Function Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/clay-v0-interpolation.ipynb Defines a `show` function to visualize image samples. It first creates an 'animate' directory if it doesn't exist. The function denormalizes the input sample using `dm.STD` and `dm.MEAN`, rearranges channels for proper display, and then uses Matplotlib to show the RGB image. It also provides an option to save the displayed image to a file. ```python def show(sample, idx=None, save=False): Path("animate").mkdir(exist_ok=True) sample = rearrange(sample, "c h w -> h w c") denorm_sample = sample * torch.as_tensor(dm.STD) + torch.as_tensor(dm.MEAN) rgb = denorm_sample[..., [2, 1, 0]] plt.imshow((rgb - rgb.min()) / (rgb.max() - rgb.min())) plt.axis("off") if save: plt.savefig(f"animate/chip_{idx}.png") ``` -------------------------------- ### Creating Output Directory for Data - Python Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/partial-inputs-flood-tutorial.ipynb This code creates a directory named `data/minicubes` where the processed satellite imagery will be saved as TIFF files. The `mkdir` function with `exist_ok=True` prevents an error if the directory already exists, and `parents=True` ensures that any necessary parent directories are also created, preparing the file system for data storage. ```python outdir = Path("data/minicubes") outdir.mkdir(exist_ok=True, parents=True) ``` -------------------------------- ### Setting Working Directory - Python Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/partial-inputs-flood-tutorial.ipynb This snippet ensures that the current working directory is set to the repository's root. It uses the `os` module to change the directory one level up from the current script's location, which is a common practice for consistent path resolution in projects. ```python # Ensure working directory is the repo home import os os.chdir("..") ``` -------------------------------- ### Downloading and Visualizing Satellite Data - Python Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/partial-inputs-flood-tutorial.ipynb This code downloads and processes the satellite imagery found in the previous step. It extracts the coordinate system, converts the POI to the image projection, and defines a 512x512 pixel bounding box (2560m x 2560m at 10m resolution). `stackstac` is then used to retrieve and stack the RGB, NIR, and SWIR bands, which are subsequently visualized as true-color images over time. ```python # Extract coordinate system from first item epsg = items[0].properties["proj:epsg"] # Convert point into the image projection poidf = gpd.GeoDataFrame( pd.DataFrame(), crs="EPSG:4326", geometry=[Point(poi[1], poi[0])], ).to_crs(epsg) coords = poidf.iloc[0].geometry.coords[0] # Create bounds of the correct size, the model # requires 512x512 pixels at 10m resolution. bounds = ( coords[0] - 2560, coords[1] - 2560, coords[0] + 2560, coords[1] + 2560, ) # Retrieve the pixel values, for the bounding box in # the target projection. In this example we use the # the RGB, NIR and SWIR band groups. stack = stackstac.stack( items, bounds=bounds, snap_bounds=False, epsg=epsg, resolution=10, dtype="float32", rescale=False, fill_value=0, assets=BAND_GROUPS["rgb"] + BAND_GROUPS["nir"] + BAND_GROUPS["swir"], resampling=Resampling.nearest, ) stack = stack.compute() stack.sel(band=["red", "green", "blue"]).plot.imshow( row="time", rgb="band", vmin=0, vmax=2000, col_wrap=6 ) ``` -------------------------------- ### Initialize CLAY DataModule and DataLoader in Python Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/clay-v0-reconstruction.ipynb Initializes the `ClayDataset` by finding all `.tif` chips within the `DATA_DIR`. It then sets up the `ClayDataModule` with a batch size of 8 and prepares it for the 'fit' stage. Finally, it creates an iterator for the training DataLoader (`trn_dl`) to fetch batches of data. ```python data_dir = Path(DATA_DIR) # Load the Clay DataModule ds = ClayDataset(chips_path=list(data_dir.glob("**/*.tif"))) dm = ClayDataModule(data_dir=str(data_dir), batch_size=8) dm.setup(stage="fit") # Load the train DataLoader trn_dl = iter(dm.train_dataloader()) ``` -------------------------------- ### Preparing Data with BioMastersDataModule in Python Source: https://github.com/clay-foundation/model/blob/main/finetune/regression/biomasters_inference.ipynb The `get_data` function initializes and sets up a `BioMastersDataModule` for data loading. It takes various directory paths, metadata path, batch size, and number of workers as input. It then prepares the validation dataloader, fetches the first batch, and returns the batch along with the dataset metadata. ```Python def get_data( train_chip_dir, train_label_dir, val_chip_dir, val_label_dir, metadata_path, batch_size, num_workers, ): dm = BioMastersDataModule( train_chip_dir=train_chip_dir, train_label_dir=train_label_dir, val_chip_dir=val_chip_dir, val_label_dir=val_label_dir, metadata_path=metadata_path, batch_size=batch_size, num_workers=num_workers, ) dm.setup(stage="fit") val_dl = iter(dm.val_dataloader()) batch = next(val_dl) metadata = dm.metadata return batch, metadata ``` -------------------------------- ### Preparing Data with ChesapeakeDataModule - Python Source: https://github.com/clay-foundation/model/blob/main/finetune/segment/chesapeake_inference.ipynb This function initializes and sets up a `ChesapeakeDataModule` using provided directory paths, metadata, and batching parameters. It prepares the data loaders, retrieves a batch from the validation set, and extracts dataset metadata for subsequent use. ```Python def get_data( train_chip_dir, train_label_dir, val_chip_dir, val_label_dir, metadata_path, batch_size, num_workers, platform, ): dm = ChesapeakeDataModule( train_chip_dir=train_chip_dir, train_label_dir=train_label_dir, val_chip_dir=val_chip_dir, val_label_dir=val_label_dir, metadata_path=metadata_path, batch_size=batch_size, num_workers=num_workers, platform=platform, ) dm.setup(stage="fit") val_dl = iter(dm.val_dataloader()) batch = next(val_dl) metadata = dm.metadata return batch, metadata ``` -------------------------------- ### Extracting Training Features with 7-Zip (Bash) Source: https://github.com/clay-foundation/model/blob/main/docs/finetune/regression.md This command uses 7-Zip to extract the `train_features.zip` archive to a specified output directory. The `-e` flag extracts files with full paths, and `-o` specifies the output directory. This step is crucial for preparing the training data for the regression head. ```bash 7z e -o/home/tam/Desktop/biomasters/train_features/ /datadisk/biomasters/raw/train_features.zip ``` -------------------------------- ### Load CLAY Model from Checkpoint in Python Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/clay-v0-reconstruction.ipynb Loads the CLAY model from the specified checkpoint path (`CKPT_PATH`) and sets its `mask_ratio` to 0.7. The model is then switched to evaluation mode (`model.eval()`) to disable dropout and batch normalization updates, preparing it for inference. ```python # Load the model & set in eval mode model = CLAYModule.load_from_checkpoint(CKPT_PATH, mask_ratio=0.7) model.eval(); ``` -------------------------------- ### Loading Clay Model and Setting Up Data Module Source: https://github.com/clay-foundation/model/blob/main/docs/clay-v0/partial-inputs.ipynb This snippet loads a pre-trained Clay model from a Hugging Face checkpoint, specifying the input band groups (RGB and NIR) and the total number of bands. It configures the model for inference by setting `mask_ratio` to 0.0 and handles potential strictness issues during checkpoint loading, preparing the model for generating embeddings from the prepared TIFF files. ```python DATA_DIR = "data/minicubes" CKPT_PATH = "https://huggingface.co/made-with-clay/Clay/resolve/main/Clay_v0.1_epoch-24_val-loss-0.46.ckpt" # Load model rgb_model = CLAYModule.load_from_checkpoint( CKPT_PATH, mask_ratio=0.0, band_groups={"rgb": (2, 1, 0), "nir": (3,)}, bands=4, strict=False # ignore the extra parameters in the checkpoint ) ```