In [ ]:
Copied!
# %pip install "hypercoast[all]"
# %pip install "hypercoast[all]"
Import libraries¶
In [ ]:
Copied!
import torch
import hypercoast
from hypercoast.chla import *
import torch
import hypercoast
from hypercoast.chla import *
Download sample data¶
In [ ]:
Copied!
chla_data_url = "https://github.com/opengeos/datasets/releases/download/hypercoast/chla_test_data.zip"
pace_data_url = "https://github.com/opengeos/datasets/releases/download/hypercoast/PACE_OCI.20241024T182127.L2.OC_AOP.V2_0.NRT.nc"
chla_data_url = "https://github.com/opengeos/datasets/releases/download/hypercoast/chla_test_data.zip"
pace_data_url = "https://github.com/opengeos/datasets/releases/download/hypercoast/PACE_OCI.20241024T182127.L2.OC_AOP.V2_0.NRT.nc"
In [ ]:
Copied!
hypercoast.download_file(chla_data_url, quiet=True)
hypercoast.download_file(chla_data_url, quiet=True)
In [ ]:
Copied!
pace_filepath = hypercoast.download_file(pace_data_url, quiet=True)
pace_filepath = hypercoast.download_file(pace_data_url, quiet=True)
Train the model¶
In [ ]:
Copied!
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load the training dataset
train_real_dl, test_real_dl, input_dim, output_dim = load_real_data(
"data/Chl_RC_PACE.csv", "data/Rrs_RC_PACE.csv"
)
# Load the validation dataset
test_real_Sep, _, _ = load_real_test(
"data/Chl_RC_PACE_Sep.csv", "data/Rrs_RC_PACE_Sep.csv"
)
# Model output path.
save_dir = "model/VAE_Chla_PACE"
os.makedirs(save_dir, exist_ok=True)
# Create the VAE model and optimizer
model = VAE(input_dim, output_dim).to(device)
opt = torch.optim.Adam(model.parameters(), lr=0.001, weight_decay=1e-3)
best_model_path = "model/vae_trans_model_best_Chl_PACE.pth"
train(model, train_real_dl, epochs=400, best_model_path=best_model_path)
# Load the optimal model
model.load_state_dict(torch.load(best_model_path, map_location=device))
predictions, actuals = evaluate(model, test_real_dl)
predictions_Sep, actuals_Sep = evaluate(model, test_real_Sep)
save_to_csv(predictions, os.path.join(save_dir, "predictions.csv"))
save_to_csv(actuals, os.path.join(save_dir, "actuals.csv"))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load the training dataset
train_real_dl, test_real_dl, input_dim, output_dim = load_real_data(
"data/Chl_RC_PACE.csv", "data/Rrs_RC_PACE.csv"
)
# Load the validation dataset
test_real_Sep, _, _ = load_real_test(
"data/Chl_RC_PACE_Sep.csv", "data/Rrs_RC_PACE_Sep.csv"
)
# Model output path.
save_dir = "model/VAE_Chla_PACE"
os.makedirs(save_dir, exist_ok=True)
# Create the VAE model and optimizer
model = VAE(input_dim, output_dim).to(device)
opt = torch.optim.Adam(model.parameters(), lr=0.001, weight_decay=1e-3)
best_model_path = "model/vae_trans_model_best_Chl_PACE.pth"
train(model, train_real_dl, epochs=400, best_model_path=best_model_path)
# Load the optimal model
model.load_state_dict(torch.load(best_model_path, map_location=device))
predictions, actuals = evaluate(model, test_real_dl)
predictions_Sep, actuals_Sep = evaluate(model, test_real_Sep)
save_to_csv(predictions, os.path.join(save_dir, "predictions.csv"))
save_to_csv(actuals, os.path.join(save_dir, "actuals.csv"))
In [ ]:
Copied!
plot_results(predictions, actuals, save_dir, mode="test")
plot_results(predictions, actuals, save_dir, mode="test")
In [ ]:
Copied!
plot_results(predictions_Sep, actuals_Sep, save_dir, mode="Sep")
plot_results(predictions_Sep, actuals_Sep, save_dir, mode="Sep")
Predict chlorophyll-a concentration¶
In [ ]:
Copied!
chla_data_file = pace_filepath.replace(".nc", ".npy")
chla_data_file = pace_filepath.replace(".nc", ".npy")
In [ ]:
Copied!
chla_predict(pace_filepath, best_model_path, chla_data_file, device)
chla_predict(pace_filepath, best_model_path, chla_data_file, device)
Visualize the results¶
In [ ]:
Copied!
rgb_image_tif_file = "data/snapshot-2024-08-10T00_00_00Z.tif"
output_file = "20241024-2.png"
title = "PACE Chla Prediction"
figsize = (12, 8)
cmap = "jet"
rgb_image_tif_file = "data/snapshot-2024-08-10T00_00_00Z.tif"
output_file = "20241024-2.png"
title = "PACE Chla Prediction"
figsize = (12, 8)
cmap = "jet"
In [ ]:
Copied!
chla_viz(rgb_image_tif_file, chla_data_file, output_file, title, figsize, cmap)
chla_viz(rgb_image_tif_file, chla_data_file, output_file, title, figsize, cmap)