### Install PyTorchFire with Example Dependencies Source: https://github.com/xiazeyu/pytorchfire/blob/main/docs/index.md Install PyTorchFire along with dependencies required for running examples. ```shell pip install 'pytorchfire[examples]' ``` -------------------------------- ### Install PyTorchFire and Dependencies Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/calibration.ipynb Installs the pytorchfire library and essential dependencies like requests, matplotlib, and tqdm. This is a prerequisite for running other examples. ```python # @title Install pytorchfire and dependencies %pip install pytorchfire %pip install requests %pip install matplotlib %pip install tqdm ``` -------------------------------- ### Install PyTorchFire Source: https://github.com/xiazeyu/pytorchfire/blob/main/docs/index.md Install the PyTorchFire package with minimal dependencies. ```shell pip install pytorchfire ``` -------------------------------- ### Initialize and Configure DemoTrainer Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/calibration.ipynb Initializes the `DemoTrainer` with a `WildfireModel` and configures training parameters such as epochs, steps, learning rate, and seed. It also sets the device to CUDA if available, otherwise CPU. ```python # @title Define trainer from pytorchfire import WildfireModel, BaseTrainer from tqdm import tqdm import torch device = 'cuda' if torch.cuda.is_available() else 'cpu' print(f"Using device: {device}") trainer = DemoTrainer(model=WildfireModel({ 'p_veg': torch.tensor(p_veg), 'p_den': torch.tensor(p_den), 'wind_towards_direction': torch.tensor(wind_towards_direction[0]), 'wind_velocity': torch.tensor(wind_velocity[0]), 'slope': torch.tensor(slope), 'initial_ignition': torch.tensor(initial_ignition, dtype=torch.bool) }, { 'a': torch.tensor(.0), 'p_h': torch.tensor(.15), 'p_continue': torch.tensor(.3), 'c_1': torch.tensor(.0), 'c_2': torch.tensor(.0), }), device=torch.device(device)) trainer.max_epochs = 5 trainer.steps_update_interval = 10 trainer.max_steps = max_steps trainer.lr = 0.005 trainer.seed = None ``` -------------------------------- ### Download Demo Dataset Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/calibration.ipynb Downloads the demo dataset for PyTorchFire calibration. Set `upload_custom_data` to `True` to upload your own data. ```python # @title Download the dataset from google.colab import files import requests # @markdown If you want to upload your own data, set `upload_custom_data` to `True` and make sure to upload all the files listed in `file_list`. # @markdown The format should be either `.npy` or `.npz`. # @markdown If you want to use the demo dataset, set `upload_custom_data` to `False`. upload_custom_data = False # @param {type:"boolean"} file_list = [ 'initial_ignition', 'p_den', 'p_veg', 'slope', 'target', 'wind_towards_direction', 'wind_velocity', 'target', ] def download_file(url, filename): response = requests.get(url, stream=True) response.raise_for_status() with open(filename, 'wb') as file: for chunk in response.iter_content(chunk_size=8192): file.write(chunk) if upload_custom_data: uploaded = files.upload() for fn in uploaded.keys(): print('User uploaded file "{name}" with length {length} bytes'.format( name=fn, length=len(uploaded[fn]))) else: remote_map_name = 'Bear_2020' for file in file_list: file_url = f'https://github.com/xiazeyu/PyTorchFire/raw/refs/heads/main/examples/{remote_map_name}/{file}.npz' filename = f'{file}.npz' download_file(file_url, filename) print(f"File downloaded and saved as {filename}") ``` -------------------------------- ### Visualize Simulation (Before Calibration) Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/calibration.ipynb Generates an animation of the simulation results before calibration. Requires numpy, matplotlib, and IPython. The animation is saved as a GIF and displayed using HTML. ```python # @title Visualize the simulation (before calibration) import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation from IPython.display import HTML # generate uncalibrated simulation after_calibration = np.array(trainer.evaluate()) ground_truth = target[:max_steps] combined = np.concatenate((np.array(after_calibration), ground_truth), axis=2) fig, ax = plt.subplots() im = ax.imshow(combined[0], cmap='hot') ax.set_title('Left: Uncalibrated, Right: Target') def update(frame): im.set_array(combined[frame]) return [im] ani = FuncAnimation( fig, update, frames=len(combined), interval=100, blit=True ) ani.save('calibration_before.gif', fps=10) HTML(ani.to_jshtml()) ``` -------------------------------- ### Simulated Dataset Parameter Calibration (Table) Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/README.txt Commands for predicting, setting up, and calibrating model parameters on a simulated dataset, as referenced in a table. ```bash python simulated_tbl.py --mode predict --exp_id s0_0 --params_from_exp True --device cuda:0 --run_name target_s0_0 ``` ```bash python simulated_tbl.py --mode predict --exp_id s0_0 --device cuda:0 --run_name before_s0_0 ``` ```bash python simulated_tbl.py --mode train --exp_id s0_0 --device cuda:0 --max_epochs 30 --run_name trained_s0_0 ``` ```bash python simulated_tbl.py --mode predict_from_result --exp_id s0_0 --device cuda:0 --run_name s0_0 ``` -------------------------------- ### Simulated Dataset Parameter Calibration (Figure) Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/README.txt Scripts for repeating target predictions, predicting before calibration, and performing model training/calibration on a simulated dataset, as shown in a figure. ```bash python simulated_fig.py --mode repeat --exp_id Bear_2020 --device cuda:0 --run_name repeat_0_0 ``` ```bash python simulated_fig.py --mode predict --exp_id Bear_2020 --p_h 0.15 --steps_update_interval 10 --device cuda:0 --run_name 0_0.15 ``` ```bash python simulated_fig.py --mode train --exp_id Bear_2020 --p_h 0.15 --steps_update_interval 10 --max_epochs 30 --device cuda:0 --run_name calibrated_0_0.15 ``` ```bash python simulated_fig.py --mode predict --exp_id Bear_2020 --a 0.07944133877754211 --c_1 0.1871010810136795 --c_2 0.0030385444406419992 --p_h 0.2542177438735962 --seed 18324160971470526000 --device cuda:0 --run_name after_0_0.15 ``` -------------------------------- ### Visualize Simulation (After Calibration) Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/calibration.ipynb Generates an animation of the simulation results after calibration. Requires numpy, matplotlib, and IPython. The animation is saved as a GIF and displayed using HTML. ```python # @title Visualize the simulation (after calibration) import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation from IPython.display import HTML # generate calibrated simulation after_calibration = np.array(trainer.evaluate()) ground_truth = target[:max_steps] combined = np.concatenate((np.array(after_calibration), ground_truth), axis=2) fig, ax = plt.subplots() im = ax.imshow(combined[0], cmap='hot') ax.set_title('Left: Calibrated, Right: Target') def update(frame): im.set_array(combined[frame]) return [im] ani = FuncAnimation( fig, update, frames=len(combined), interval=100, blit=True ) ani.save('calibration_after.gif', fps=10) HTML(ani.to_jshtml()) ``` -------------------------------- ### Real Fire Parameter Calibration (Figure) Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/README.txt Commands for predicting fire spread before and after calibration, and performing the calibration training on real fire data, as depicted in a figure. ```bash python real.py --mode predict --exp_id Bear_2020 --p_h 0.15 --device cuda:0 --run_name real_Bear_2020_0.15_before ``` ```bash python real.py --mode train --exp_id Bear_2020 --p_h 0.15 --max_epochs 30 --device cuda:0 --run_name real_Bear_2020_0.15 ``` ```bash python real.py --mode predict --exp_id Bear_2020 --a 0 --c_1 0.00200487463735044 --c_2 0.2683422863483429 --p_continue 0.30000001192092896 --p_h 0.20000000298023224 --seed 14381954276780055000 --device cuda:0 --run_name after_Bear_2020_0.15 ``` -------------------------------- ### Download Demo Dataset Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/prediction.ipynb Downloads the 'Bear_2020' demo dataset if custom data upload is not selected. Requires the 'requests' library. ```python # @title Download the dataset from google.colab import files import requests # @markdown If you want to upload your own data, set `upload_custom_data` to `True` and make sure to upload all the files listed in `file_list`. # @markdown The format should be either `.npy` or `.npz`. # @markdown If you want to use the demo dataset, set `upload_custom_data` to `False`. upload_custom_data = False # @param {type:"boolean"} file_list = [ 'initial_ignition', 'p_den', 'p_veg', 'slope', 'target', 'wind_towards_direction', 'wind_velocity', ] def download_file(url, filename): response = requests.get(url, stream=True) response.raise_for_status() with open(filename, 'wb') as file: for chunk in response.iter_content(chunk_size=8192): file.write(chunk) if upload_custom_data: uploaded = files.upload() for fn in uploaded.keys(): print('User uploaded file "{name}" with length {length} bytes'.format( name=fn, length=len(uploaded[fn]))) else: remote_map_name = 'Bear_2020' for file in file_list: file_url = f'https://github.com/xiazeyu/PyTorchFire/raw/refs/heads/main/examples/{remote_map_name}/{file}.npz' filename = f'{file}.npz' download_file(file_url, filename) print(f"File downloaded and saved as {filename}") ``` -------------------------------- ### Perform Parameter Calibration Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/calibration.ipynb Initiates the training process for parameter calibration using the trainer object. ```python # @title Perform parameter calibration trainer.train() ``` -------------------------------- ### Load Dataset Components Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/prediction.ipynb Loads various components of the dataset (e.g., vegetation density, wind velocity) into NumPy arrays. Handles both .npy and .npz file formats. Requires the 'numpy' and 'os' libraries. ```python # @title Load the dataset import numpy as np import os def load_np_file(file_name): if os.path.exists(f'{file_name}.npy'): return np.load(f'{file_name}.npy') elif os.path.exists(f'{file_name}.npz'): ds = np.load(f'{file_name}.npz') if len(ds.files) == 1: return ds[ds.files[0]] else: raise ValueError(f"Multiple arrays found in {file_name}. Please provide a single array.") p_veg = load_np_file('p_veg') p_den = load_np_file('p_den') wind_towards_direction = load_np_file('wind_towards_direction') wind_velocity = load_np_file('wind_velocity') slope = load_np_file('slope') initial_ignition = load_np_file('initial_ignition') a = 0.13324953615665436 # @param {type:"number"} c_1 = 0.11270108073949814 # @param {type:"number"} c_2 = 0.15624772012233734 # @param {type:"number"} max_steps = 100 # @param {type:"integer"} p_continue = 0.2814338207244873 # @param {type:"slider", min:0, max:1, step:0.05} p_h = 0.3236876130104065 # @param {type:"slider", min:0, max:1, step:0.05} wind_step_interval = 15 # @param {type:"integer"} ``` -------------------------------- ### Parameter Calibration with PyTorchFire Source: https://github.com/xiazeyu/pytorchfire/blob/main/docs/index.md Perform parameter calibration for wildfire simulations using the BaseTrainer. This involves training and evaluating the model. ```python import torch from pytorchfire import WildfireModel, BaseTrainer model = WildfireModel() trainer = BaseTrainer(model) trainer.train() trainer.evaluate() ``` -------------------------------- ### Load Calibration Dataset Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/calibration.ipynb Loads various components of the calibration dataset into NumPy arrays. Supports both `.npy` and `.npz` file formats. ```python # @title Load the dataset import numpy as np import os def load_np_file(file_name): if os.path.exists(f'{file_name}.npy'): return np.load(f'{file_name}.npy') elif os.path.exists(f'{file_name}.npz'): ds = np.load(f'{file_name}.npz') if len(ds.files) == 1: return ds[ds.files[0]] else: raise ValueError(f"Multiple arrays found in {file_name}. Please provide a single array.") p_veg = load_np_file('p_veg') p_den = load_np_file('p_den') wind_towards_direction = load_np_file('wind_towards_direction') wind_velocity = load_np_file('wind_velocity') slope = load_np_file('slope') initial_ignition = load_np_file('initial_ignition') target = load_np_file('target') a = 0.13324953615665436 # @param {type:"number"} c_1 = 0.11270108073949814 # @param {type:"number"} c_2 = 0.15624772012233734 # @param {type:"number"} max_steps = 200 # @param {type:"integer"} p_continue = 0.2814338207244873 # @param {type:"slider", min:0, max:1, step:0.05} p_h = 0.3236876130104065 # @param {type:"slider", min:0, max:1, step:0.05} wind_step_interval = 15 # @param {type:"integer"} ``` -------------------------------- ### Run Wildfire Simulation Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/prediction.ipynb Initializes and runs a wildfire simulation. Ensure all necessary parameters like p_veg, p_den, wind_towards_direction, wind_velocity, slope, initial_ignition, a, p_h, p_continue, c_1, and c_2 are defined before execution. The simulation updates the model state iteratively. ```python # @title Run the simulation from pytorchfire import WildfireModel from tqdm import tqdm import torch device = 'cuda' if torch.cuda.is_available() else 'cpu' print(f"Using device: {device}") model = WildfireModel({ 'p_veg': torch.tensor(p_veg), 'p_den': torch.tensor(p_den), 'wind_towards_direction': torch.tensor(wind_towards_direction[0]), 'wind_velocity': torch.tensor(wind_velocity[0]), 'slope': torch.tensor(slope), 'initial_ignition': torch.tensor(initial_ignition, dtype=torch.bool) }, { 'a': torch.tensor(a), 'p_h': torch.tensor(p_h), 'p_continue': torch.tensor(p_continue), 'c_1': torch.tensor(c_1), 'c_2': torch.tensor(c_2), }) model.to(device) model.eval() affected_cell_count_outputs = [] affected_cell_count_targets = [] postfix = {} output_list = [] with tqdm(total=max_steps) as progress_bar: with torch.no_grad(): for steps in range(max_steps): postfix['steps'] = f'{steps + 1}/{max_steps}' if steps % wind_step_interval == 0: model.wind_towards_direction = torch.tensor( wind_towards_direction[steps // wind_step_interval], device=device) model.wind_velocity = torch.tensor(wind_velocity[steps // wind_step_interval], device=device) model.compute() outputs = model.state[0] | model.state[1] postfix['burning'] = model.state[0].sum().detach().cpu().item() postfix['burned'] = model.state[1].sum().detach().cpu().item() output_list.append(outputs.cpu().detach().numpy()) progress_bar.set_postfix(postfix) progress_bar.update(1) ``` -------------------------------- ### Visualize Wildfire Simulation Animation Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/prediction.ipynb Generates and saves an animation of the wildfire simulation. Requires matplotlib and IPython. The animation is created from the simulation output and can be displayed as HTML or saved as a GIF. ```python # @title Visualize the simulation import numpy as np import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation from IPython.display import HTML fig, ax = plt.subplots() im = ax.imshow(output_list[0], cmap='hot') def update(frame): im.set_array(output_list[frame]) return [im] ani = FuncAnimation( fig, update, frames=len(output_list), interval=100, blit=True ) ani.save('prediction.gif', fps=10) HTML(ani.to_jshtml()) ``` -------------------------------- ### Timing MPI-CA Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/README.txt Measures the execution time of MPI-CA with different dataset sizes. The '--allow-run-as-root' flag may be necessary in some environments. ```bash time mpirun -n 64 python mpica.py --size 50 ``` ```bash time mpirun -n 64 python mpica.py --size 100 ``` ```bash time mpirun --allow-run-as-root -n 64 python mpica.py --size 100 ``` -------------------------------- ### Timing PyTorchFire Speed Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/README.txt Tests the performance of PyTorchFire on different dataset sizes and devices (CPU and CUDA). ```bash time python speedtest.py --size 50 --device cpu ``` ```bash time python speedtest.py --size 50 --device cuda ``` ```bash python speedtest.py --size 100 --device cpu ``` ```bash python speedtest.py --size 100 --device cuda ``` ```bash python speedtest.py --size 1000 --device cuda ``` -------------------------------- ### Wildfire Prediction with PyTorchFire Source: https://github.com/xiazeyu/pytorchfire/blob/main/docs/index.md Perform wildfire prediction using the WildfireModel. Ensure the model is moved to the GPU for acceleration. The model can be reset with a seed for reproducible simulations. ```python from pytorchfire import WildfireModel model = WildfireModel() # Create a model with default parameters and environment data model = model.cuda() # Move the model to GPU # model.reset(seed=seed) # Reset the model with a seed for _ in range(100): # Run the model for 100 steps model.compute() # Compute the next state ``` -------------------------------- ### Define Trainer Class Source: https://github.com/xiazeyu/pytorchfire/blob/main/examples/calibration.ipynb Defines a custom `DemoTrainer` class inheriting from `BaseTrainer`. This class includes `train` and `evaluate` methods for managing the wildfire simulation process, including model updates, loss calculation, and progress visualization. ```python # @title Define Trainer class (similar to DataLoader) from pytorchfire import WildfireModel, BaseTrainer from tqdm import tqdm import torch class DemoTrainer(BaseTrainer): def train(self): self.reset() self.model.to(self.device) self.model.train() max_iterations = self.max_steps // self.steps_update_interval postfix = {} with tqdm() as progress_bar: for epochs in range(self.max_epochs): postfix['epoch'] = f'{epochs + 1}/{self.max_epochs}' self.model.reset() batch_seed = self.model.seed for iterations in range(max_iterations): postfix['iteration'] = f'{iterations + 1}/{max_iterations}' iter_max_steps = min(self.max_steps, (iterations + 1) * self.steps_update_interval) progress_bar.reset(total=iter_max_steps) for steps in range(iter_max_steps): postfix['step'] = f'{steps + 1}/{iter_max_steps}' if steps % wind_step_interval == 0: self.model.wind_towards_direction = torch.tensor( wind_towards_direction[steps // wind_step_interval], device=self.device) self.model.wind_velocity = torch.tensor(wind_velocity[steps // wind_step_interval], device=self.device) self.model.compute(attach=self.check_if_attach(steps, iter_max_steps)) progress_bar.set_postfix(postfix) progress_bar.update(1) outputs = self.model.accumulator targets = target[iter_max_steps - 1] targets = torch.tensor(targets, device=self.device) loss = self.criterion(outputs, targets) postfix['loss'] = f'{loss.item():.4f}' self.backward(loss) self.model.reset(seed=batch_seed) def evaluate(self): self.reset() self.model.to(self.device) self.model.eval() affected_cell_count_outputs = [] affected_cell_count_targets = [] postfix = {} output_list = [] with tqdm(total=self.max_steps) as progress_bar: with torch.no_grad(): for steps in range(self.max_steps): postfix['steps'] = f'{steps + 1}/{self.max_steps}' if steps % wind_step_interval == 0: self.model.wind_towards_direction = torch.tensor( wind_towards_direction[steps // wind_step_interval], device=device) self.model.wind_velocity = torch.tensor(wind_velocity[steps // wind_step_interval], device=device) self.model.compute() outputs = self.model.state[0] | self.model.state[1] postfix['burning'] = self.model.state[0].sum().detach().cpu().item() postfix['burned'] = self.model.state[1].sum().detach().cpu().item() output_list.append(outputs.cpu().detach().numpy()) progress_bar.set_postfix(postfix) progress_bar.update(1) return output_list ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.