Commit ·
53f0b4b
1
Parent(s): d01e1f1
Bug fixes
Browse files- .gitignore +8 -0
- flaring/forecasting/inference/evaluation.py +5 -5
- flaring/forecasting/inference/inference.py +7 -0
- flaring/forecasting/inference/inference_on_patch.py +6 -0
- flaring/forecasting/inference/inference_on_patch_config.yaml +3 -3
- flaring/forecasting/models/efficientnet.py +0 -45
- flaring/forecasting/models/kan_success.py +0 -219
- flaring/forecasting/models/linear_and_hybrid.py +5 -0
- flaring/forecasting/models/vision_transformer_custom.py +6 -5
- flaring/forecasting/training/config.yaml +7 -7
- flaring/forecasting/training/train.py +6 -0
- flaring/outputs/outputs.txt +0 -0
- flaring/vision_transformers/__init__.py +1 -0
.gitignore
CHANGED
|
@@ -172,3 +172,11 @@ cython_debug/
|
|
| 172 |
|
| 173 |
# PyPI configuration file
|
| 174 |
.pypirc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
# PyPI configuration file
|
| 174 |
.pypirc
|
| 175 |
+
|
| 176 |
+
.vscode/sftp.json
|
| 177 |
+
|
| 178 |
+
.DS_Store
|
| 179 |
+
|
| 180 |
+
.cursorignore
|
| 181 |
+
|
| 182 |
+
wandb/
|
flaring/forecasting/inference/evaluation.py
CHANGED
|
@@ -838,7 +838,7 @@ class SolarFlareEvaluator:
|
|
| 838 |
|
| 839 |
#plt.suptitle(f'Timestamp: {timestamp}', fontsize=14)
|
| 840 |
#plt.tight_layout()
|
| 841 |
-
plt.savefig(save_path, dpi=500
|
| 842 |
plt.close()
|
| 843 |
|
| 844 |
print(f"Worker {os.getpid()}: Completed {timestamp}")
|
|
@@ -941,10 +941,10 @@ class SolarFlareEvaluator:
|
|
| 941 |
|
| 942 |
if __name__ == "__main__":
|
| 943 |
# Example paths - replace with your actual paths
|
| 944 |
-
vit_csv = "/mnt/data/ML-
|
| 945 |
baseline_results_csv = ""
|
| 946 |
-
aia_data = "/mnt/data/ML-
|
| 947 |
-
weights_directory = "/mnt/data/ML-
|
| 948 |
|
| 949 |
# Sample timestamps - Fixed the datetime generation
|
| 950 |
start_time = datetime(2023, 8, 5, 20,30,00)
|
|
@@ -962,7 +962,7 @@ if __name__ == "__main__":
|
|
| 962 |
baseline_csv_path=baseline_results_csv,
|
| 963 |
aia_dir=aia_data,
|
| 964 |
weight_path=weights_directory,
|
| 965 |
-
output_dir="/mnt/data/ML-
|
| 966 |
)
|
| 967 |
|
| 968 |
# Run complete evaluation with baseline comparison and uncertainties
|
|
|
|
| 838 |
|
| 839 |
#plt.suptitle(f'Timestamp: {timestamp}', fontsize=14)
|
| 840 |
#plt.tight_layout()
|
| 841 |
+
plt.savefig(save_path, dpi=500)
|
| 842 |
plt.close()
|
| 843 |
|
| 844 |
print(f"Worker {os.getpid()}: Completed {timestamp}")
|
|
|
|
| 941 |
|
| 942 |
if __name__ == "__main__":
|
| 943 |
# Example paths - replace with your actual paths
|
| 944 |
+
vit_csv = "/mnt/data/ML-READY/output/final_epoch_patch.csv"
|
| 945 |
baseline_results_csv = ""
|
| 946 |
+
aia_data = "/mnt/data/ML-READY/AIA/test/"
|
| 947 |
+
weights_directory = "/mnt/data/ML-READY/final_epoch_patch_weights_final"
|
| 948 |
|
| 949 |
# Sample timestamps - Fixed the datetime generation
|
| 950 |
start_time = datetime(2023, 8, 5, 20,30,00)
|
|
|
|
| 962 |
baseline_csv_path=baseline_results_csv,
|
| 963 |
aia_dir=aia_data,
|
| 964 |
weight_path=weights_directory,
|
| 965 |
+
output_dir="/mnt/data/ML-READY/solar_flare_comparison_results/improving_patch"
|
| 966 |
)
|
| 967 |
|
| 968 |
# Run complete evaluation with baseline comparison and uncertainties
|
flaring/forecasting/inference/inference.py
CHANGED
|
@@ -6,6 +6,13 @@ import torch
|
|
| 6 |
import numpy as np
|
| 7 |
from torch.utils.checkpoint import checkpoint
|
| 8 |
from torch.utils.data import DataLoader
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
from flaring.forecasting.data_loaders.SDOAIA_dataloader import AIA_GOESDataset
|
| 10 |
import flaring.forecasting.models as models
|
| 11 |
from flaring.forecasting.models.vision_transformer_custom import ViT
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
from torch.utils.checkpoint import checkpoint
|
| 8 |
from torch.utils.data import DataLoader
|
| 9 |
+
|
| 10 |
+
from pathlib import Path
|
| 11 |
+
|
| 12 |
+
# Add project root to Python path
|
| 13 |
+
PROJECT_ROOT = Path(__file__).parent.parent.parent.parent.absolute()
|
| 14 |
+
sys.path.insert(0, str(PROJECT_ROOT))
|
| 15 |
+
|
| 16 |
from flaring.forecasting.data_loaders.SDOAIA_dataloader import AIA_GOESDataset
|
| 17 |
import flaring.forecasting.models as models
|
| 18 |
from flaring.forecasting.models.vision_transformer_custom import ViT
|
flaring/forecasting/inference/inference_on_patch.py
CHANGED
|
@@ -1,6 +1,12 @@
|
|
| 1 |
import argparse
|
| 2 |
import re
|
| 3 |
import sys
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import pandas as pd
|
| 5 |
import torch
|
| 6 |
import numpy as np
|
|
|
|
| 1 |
import argparse
|
| 2 |
import re
|
| 3 |
import sys
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
# Add project root to Python path
|
| 7 |
+
PROJECT_ROOT = Path(__file__).parent.parent.parent.parent.absolute()
|
| 8 |
+
sys.path.insert(0, str(PROJECT_ROOT))
|
| 9 |
+
|
| 10 |
import pandas as pd
|
| 11 |
import torch
|
| 12 |
import numpy as np
|
flaring/forecasting/inference/inference_on_patch_config.yaml
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
base_data_dir: "/mnt/data/ML-
|
| 2 |
output_path: "${base_data_dir}/output/final_epoch_patch.csv"
|
| 3 |
weight_path: "${base_data_dir}/final_epoch_patch_weights_final/"
|
| 4 |
flux_path: "${base_data_dir}/patch_flux/"
|
|
@@ -25,7 +25,7 @@ data:
|
|
| 25 |
sxr_dir:
|
| 26 |
"${base_data_dir}/SXR"
|
| 27 |
sxr_norm_path:
|
| 28 |
-
"/mnt/data/ML-
|
| 29 |
checkpoint_path:
|
| 30 |
-
"/mnt/data/ML-
|
| 31 |
|
|
|
|
| 1 |
+
base_data_dir: "/mnt/data/ML-READY/" # Change this line for different datasets
|
| 2 |
output_path: "${base_data_dir}/output/final_epoch_patch.csv"
|
| 3 |
weight_path: "${base_data_dir}/final_epoch_patch_weights_final/"
|
| 4 |
flux_path: "${base_data_dir}/patch_flux/"
|
|
|
|
| 25 |
sxr_dir:
|
| 26 |
"${base_data_dir}/SXR"
|
| 27 |
sxr_norm_path:
|
| 28 |
+
"/mnt/data/ML-READY/SXR/normalized_sxr.npy"
|
| 29 |
checkpoint_path:
|
| 30 |
+
"/mnt/data/ML-READY/new-checkpoint/vit-16-higher-weight-lower-decay-epoch=288-val_total_loss=0.0385.ckpt"
|
| 31 |
|
flaring/forecasting/models/efficientnet.py
DELETED
|
@@ -1,45 +0,0 @@
|
|
| 1 |
-
import torchvision
|
| 2 |
-
from torch import nn
|
| 3 |
-
from torch.nn import HuberLoss
|
| 4 |
-
from irradiance.models.base_model import BaseModel
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
class EfficientnetIrradiance(BaseModel):
|
| 8 |
-
|
| 9 |
-
def __init__(self, d_input, d_output, eve_norm, loss_func=HuberLoss(), model='efficientnet_b0', dp=0.75, lr=1e-4):
|
| 10 |
-
if model == 'efficientnet_b0':
|
| 11 |
-
model = torchvision.models.efficientnet_b0(pretrained=True)
|
| 12 |
-
elif model == 'efficientnet_b1':
|
| 13 |
-
model = torchvision.models.efficientnet_b1(pretrained=True)
|
| 14 |
-
elif model == 'efficientnet_b2':
|
| 15 |
-
model = torchvision.models.efficientnet_b2(pretrained=True)
|
| 16 |
-
elif model == 'efficientnet_b3':
|
| 17 |
-
model = torchvision.models.efficientnet_b3(pretrained=True)
|
| 18 |
-
elif model == 'efficientnet_b4':
|
| 19 |
-
model = torchvision.models.efficientnet_b4(pretrained=True)
|
| 20 |
-
elif model == 'efficientnet_b5':
|
| 21 |
-
model = torchvision.models.efficientnet_b5(pretrained=True)
|
| 22 |
-
elif model == 'efficientnet_b6':
|
| 23 |
-
model = torchvision.models.efficientnet_b6(pretrained=True)
|
| 24 |
-
elif model == 'efficientnet_b7':
|
| 25 |
-
model = torchvision.models.efficientnet_b7(pretrained=True)
|
| 26 |
-
conv1_out = model.features[0][0].out_channels
|
| 27 |
-
model.features[0][0] = nn.Conv2d(d_input, conv1_out, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
|
| 28 |
-
|
| 29 |
-
lin_in = model.classifier[1].in_features
|
| 30 |
-
# consider adding average pool of full image(s)
|
| 31 |
-
classifier = nn.Sequential(nn.Dropout(p=dp, inplace=True),
|
| 32 |
-
nn.Linear(in_features=lin_in, out_features=d_output, bias=True))
|
| 33 |
-
model.classifier = classifier
|
| 34 |
-
# set all dropouts to 0.75
|
| 35 |
-
# TODO: other dropout values?
|
| 36 |
-
for m in model.modules():
|
| 37 |
-
if m.__class__.__name__.startswith('Dropout'):
|
| 38 |
-
m.p = dp
|
| 39 |
-
model = model
|
| 40 |
-
|
| 41 |
-
super().__init__(model=model, eve_norm=eve_norm, loss_func=loss_func, lr=lr)
|
| 42 |
-
|
| 43 |
-
def forward(self, x):
|
| 44 |
-
x = self.model(x)
|
| 45 |
-
return x
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
flaring/forecasting/models/kan_success.py
DELETED
|
@@ -1,219 +0,0 @@
|
|
| 1 |
-
# Copyright 2024 Li, Ziyao
|
| 2 |
-
#
|
| 3 |
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
-
# you may not use this file except in compliance with the License.
|
| 5 |
-
# You may obtain a copy of the License at
|
| 6 |
-
#
|
| 7 |
-
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
-
#
|
| 9 |
-
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
-
# See the License for the specific language governing permissions and
|
| 13 |
-
# limitations under the License.
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
import torch
|
| 17 |
-
import torch.nn as nn
|
| 18 |
-
import torch.nn.functional as F
|
| 19 |
-
from typing import *
|
| 20 |
-
from torch.nn import HuberLoss
|
| 21 |
-
from irradiance.models.base_model import BaseModel
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
class SplineLinear(nn.Linear):
|
| 25 |
-
def __init__(self, in_features: int, out_features: int, init_scale: float = 0.1, **kw) -> None:
|
| 26 |
-
self.init_scale = init_scale
|
| 27 |
-
super().__init__(in_features, out_features, bias=False, **kw)
|
| 28 |
-
|
| 29 |
-
def reset_parameters(self) -> None:
|
| 30 |
-
nn.init.trunc_normal_(self.weight, mean=0, std=self.init_scale)
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
class RadialBasisFunction(nn.Module):
|
| 34 |
-
def __init__(
|
| 35 |
-
self,
|
| 36 |
-
grid_min: float = -2.,
|
| 37 |
-
grid_max: float = 2.,
|
| 38 |
-
num_grids: int = 8,
|
| 39 |
-
denominator: float = None, # larger denominators lead to smoother basis
|
| 40 |
-
):
|
| 41 |
-
super().__init__()
|
| 42 |
-
self.grid_min = grid_min
|
| 43 |
-
self.grid_max = grid_max
|
| 44 |
-
self.num_grids = num_grids
|
| 45 |
-
grid = torch.linspace(grid_min, grid_max, num_grids)
|
| 46 |
-
self.grid = torch.nn.Parameter(grid, requires_grad=False)
|
| 47 |
-
self.denominator = denominator or (grid_max - grid_min) / (num_grids - 1)
|
| 48 |
-
|
| 49 |
-
def forward(self, x):
|
| 50 |
-
return torch.exp(-((x[..., None] - self.grid) / self.denominator) ** 2)
|
| 51 |
-
|
| 52 |
-
class FastKANLayer(nn.Module):
|
| 53 |
-
def __init__(
|
| 54 |
-
self,
|
| 55 |
-
input_dim: int,
|
| 56 |
-
output_dim: int,
|
| 57 |
-
grid_min: float = -2.,
|
| 58 |
-
grid_max: float = 2.,
|
| 59 |
-
num_grids: int = 8,
|
| 60 |
-
use_base_update: bool = True,
|
| 61 |
-
use_layernorm: bool = True,
|
| 62 |
-
base_activation = F.silu,
|
| 63 |
-
spline_weight_init_scale: float = 0.1,
|
| 64 |
-
) -> None:
|
| 65 |
-
super().__init__()
|
| 66 |
-
self.input_dim = input_dim
|
| 67 |
-
self.output_dim = output_dim
|
| 68 |
-
self.layernorm = None
|
| 69 |
-
if use_layernorm:
|
| 70 |
-
assert input_dim > 1, "Do not use layernorms on 1D inputs. Set `use_layernorm=False`."
|
| 71 |
-
self.layernorm = nn.LayerNorm(input_dim)
|
| 72 |
-
self.rbf = RadialBasisFunction(grid_min, grid_max, num_grids)
|
| 73 |
-
self.spline_linear = SplineLinear(input_dim * num_grids, output_dim, spline_weight_init_scale)
|
| 74 |
-
self.use_base_update = use_base_update
|
| 75 |
-
if use_base_update:
|
| 76 |
-
self.base_activation = base_activation
|
| 77 |
-
self.base_linear = nn.Linear(input_dim, output_dim)
|
| 78 |
-
|
| 79 |
-
def forward(self, x, use_layernorm=True):
|
| 80 |
-
if self.layernorm is not None and use_layernorm:
|
| 81 |
-
spline_basis = self.rbf(self.layernorm(x))
|
| 82 |
-
else:
|
| 83 |
-
spline_basis = self.rbf(x)
|
| 84 |
-
ret = self.spline_linear(spline_basis.view(*spline_basis.shape[:-2], -1))
|
| 85 |
-
if self.use_base_update:
|
| 86 |
-
base = self.base_linear(self.base_activation(x))
|
| 87 |
-
ret = ret + base
|
| 88 |
-
return ret
|
| 89 |
-
|
| 90 |
-
def plot_curve(
|
| 91 |
-
self,
|
| 92 |
-
input_index: int,
|
| 93 |
-
output_index: int,
|
| 94 |
-
num_pts: int = 1000,
|
| 95 |
-
num_extrapolate_bins: int = 2
|
| 96 |
-
):
|
| 97 |
-
'''this function returns the learned curves in a FastKANLayer.
|
| 98 |
-
input_index: the selected index of the input, in [0, input_dim) .
|
| 99 |
-
output_index: the selected index of the output, in [0, output_dim) .
|
| 100 |
-
num_pts: num of points sampled for the curve.
|
| 101 |
-
num_extrapolate_bins (N_e): num of bins extrapolating from the given grids. The curve
|
| 102 |
-
will be calculate in the range of [grid_min - h * N_e, grid_max + h * N_e].
|
| 103 |
-
'''
|
| 104 |
-
ng = self.rbf.num_grids
|
| 105 |
-
h = self.rbf.denominator
|
| 106 |
-
assert input_index < self.input_dim
|
| 107 |
-
assert output_index < self.output_dim
|
| 108 |
-
w = self.spline_linear.weight[
|
| 109 |
-
output_index, input_index * ng : (input_index + 1) * ng
|
| 110 |
-
] # num_grids,
|
| 111 |
-
x = torch.linspace(
|
| 112 |
-
self.rbf.grid_min - num_extrapolate_bins * h,
|
| 113 |
-
self.rbf.grid_max + num_extrapolate_bins * h,
|
| 114 |
-
num_pts
|
| 115 |
-
) # num_pts, num_grids
|
| 116 |
-
with torch.no_grad():
|
| 117 |
-
y = (w * self.rbf(x.to(w.dtype))).sum(-1)
|
| 118 |
-
return x, y
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
class FastKANIrradiance(BaseModel):
|
| 122 |
-
def __init__(
|
| 123 |
-
self,
|
| 124 |
-
eve_norm,
|
| 125 |
-
layers_hidden: List[int],
|
| 126 |
-
grid_min: float = -2.,
|
| 127 |
-
grid_max: float = 2.,
|
| 128 |
-
num_grids: int = 8,
|
| 129 |
-
use_base_update: bool = True,
|
| 130 |
-
base_activation = F.silu,
|
| 131 |
-
spline_weight_init_scale: float = 0.1,
|
| 132 |
-
loss_func = HuberLoss(),
|
| 133 |
-
lr=1e-4,
|
| 134 |
-
use_std=False
|
| 135 |
-
) -> None:
|
| 136 |
-
super().__init__(model=None, eve_norm=eve_norm, loss_func=loss_func, lr=lr)
|
| 137 |
-
self.use_std = use_std
|
| 138 |
-
if use_std:
|
| 139 |
-
layers_hidden[0] = layers_hidden[0]*2
|
| 140 |
-
self.layers = nn.ModuleList([
|
| 141 |
-
FastKANLayer(
|
| 142 |
-
in_dim, out_dim,
|
| 143 |
-
grid_min=grid_min,
|
| 144 |
-
grid_max=grid_max,
|
| 145 |
-
num_grids=num_grids,
|
| 146 |
-
use_base_update=use_base_update,
|
| 147 |
-
base_activation=base_activation,
|
| 148 |
-
spline_weight_init_scale=spline_weight_init_scale,
|
| 149 |
-
) for in_dim, out_dim in zip(layers_hidden[:-1], layers_hidden[1:])
|
| 150 |
-
])
|
| 151 |
-
|
| 152 |
-
def forward(self, x):
|
| 153 |
-
# Calculating mean and std of images to take them as input to 1D KAN
|
| 154 |
-
mean_irradiance = torch.torch.mean(x, dim=(2,3))
|
| 155 |
-
std_irradiance = torch.torch.std(x, dim=(2,3))
|
| 156 |
-
if self.use_std:
|
| 157 |
-
x = torch.cat((mean_irradiance, std_irradiance), dim=1)
|
| 158 |
-
else:
|
| 159 |
-
x = mean_irradiance
|
| 160 |
-
for layer in self.layers:
|
| 161 |
-
x = layer(x)
|
| 162 |
-
return x
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
class AttentionWithFastKANTransform(nn.Module):
|
| 166 |
-
|
| 167 |
-
def __init__(
|
| 168 |
-
self,
|
| 169 |
-
q_dim: int,
|
| 170 |
-
k_dim: int,
|
| 171 |
-
v_dim: int,
|
| 172 |
-
head_dim: int,
|
| 173 |
-
num_heads: int,
|
| 174 |
-
gating: bool = True,
|
| 175 |
-
):
|
| 176 |
-
super(AttentionWithFastKANTransform, self).__init__()
|
| 177 |
-
|
| 178 |
-
self.num_heads = num_heads
|
| 179 |
-
total_dim = head_dim * self.num_heads
|
| 180 |
-
self.gating = gating
|
| 181 |
-
self.linear_q = FastKANLayer(q_dim, total_dim)
|
| 182 |
-
self.linear_k = FastKANLayer(k_dim, total_dim)
|
| 183 |
-
self.linear_v = FastKANLayer(v_dim, total_dim)
|
| 184 |
-
self.linear_o = FastKANLayer(total_dim, q_dim)
|
| 185 |
-
self.linear_g = None
|
| 186 |
-
if self.gating:
|
| 187 |
-
self.linear_g = FastKANLayer(q_dim, total_dim)
|
| 188 |
-
# precompute the 1/sqrt(head_dim)
|
| 189 |
-
self.norm = head_dim**-0.5
|
| 190 |
-
|
| 191 |
-
def forward(
|
| 192 |
-
self,
|
| 193 |
-
q: torch.Tensor,
|
| 194 |
-
k: torch.Tensor,
|
| 195 |
-
v: torch.Tensor,
|
| 196 |
-
bias: torch.Tensor = None, # additive attention bias
|
| 197 |
-
) -> torch.Tensor:
|
| 198 |
-
|
| 199 |
-
wq = self.linear_q(q).view(*q.shape[:-1], 1, self.num_heads, -1) * self.norm # *q1hc
|
| 200 |
-
wk = self.linear_k(k).view(*k.shape[:-2], 1, k.shape[-2], self.num_heads, -1) # *1khc
|
| 201 |
-
att = (wq * wk).sum(-1).softmax(-2) # *qkh
|
| 202 |
-
del wq, wk
|
| 203 |
-
if bias is not None:
|
| 204 |
-
att = att + bias[..., None]
|
| 205 |
-
|
| 206 |
-
wv = self.linear_v(v).view(*v.shape[:-2],1, v.shape[-2], self.num_heads, -1) # *1khc
|
| 207 |
-
o = (att[..., None] * wv).sum(-3) # *qhc
|
| 208 |
-
del att, wv
|
| 209 |
-
|
| 210 |
-
o = o.view(*o.shape[:-2], -1) # *q(hc)
|
| 211 |
-
|
| 212 |
-
if self.linear_g is not None:
|
| 213 |
-
# gating, use raw query input
|
| 214 |
-
g = self.linear_g(q)
|
| 215 |
-
o = torch.sigmoid(g) * o
|
| 216 |
-
|
| 217 |
-
# merge heads
|
| 218 |
-
o = self.linear_o(o)
|
| 219 |
-
return o
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
flaring/forecasting/models/linear_and_hybrid.py
CHANGED
|
@@ -1,6 +1,11 @@
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
from torch.nn import HuberLoss
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from flaring.forecasting.models.base_model import BaseModel
|
| 5 |
from torchvision.models import resnet18
|
| 6 |
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn as nn
|
| 3 |
from torch.nn import HuberLoss
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import sys
|
| 6 |
+
# Add project root to Python path
|
| 7 |
+
PROJECT_ROOT = Path(__file__).parent.parent.parent.parent.absolute()
|
| 8 |
+
sys.path.insert(0, str(PROJECT_ROOT))
|
| 9 |
from flaring.forecasting.models.base_model import BaseModel
|
| 10 |
from torchvision.models import resnet18
|
| 11 |
|
flaring/forecasting/models/vision_transformer_custom.py
CHANGED
|
@@ -27,7 +27,7 @@ class ViT(pl.LightningModule):
|
|
| 27 |
filtered_kwargs = dict(model_kwargs)
|
| 28 |
filtered_kwargs.pop('lr', None)
|
| 29 |
self.model = VisionTransformer(**filtered_kwargs)
|
| 30 |
-
self.adaptive_loss = SXRRegressionDynamicLoss(window_size=
|
| 31 |
self.sxr_norm = sxr_norm
|
| 32 |
|
| 33 |
def forward(self, x, return_attention=True):
|
|
@@ -284,7 +284,8 @@ class SXRRegressionDynamicLoss:
|
|
| 284 |
}
|
| 285 |
|
| 286 |
def calculate_loss(self, preds_squeezed, sxr, sxr_un, preds_squeezed_un):
|
| 287 |
-
base_loss = F.huber_loss(preds_squeezed, sxr, delta=1.0, reduction='none')
|
|
|
|
| 288 |
weights = self._get_adaptive_weights(sxr_un, preds_squeezed_un, base_loss)
|
| 289 |
self._update_tracking(sxr_un, preds_squeezed_un, base_loss)
|
| 290 |
weighted_loss = base_loss * weights
|
|
@@ -299,13 +300,13 @@ class SXRRegressionDynamicLoss:
|
|
| 299 |
self.quiet_errors, max_multiplier=1.5, min_multiplier=0.5, sensitivity=2.0, sxrclass = 'quiet'
|
| 300 |
)
|
| 301 |
c_mult = self._get_performance_multiplier(
|
| 302 |
-
self.c_errors, max_multiplier=
|
| 303 |
)
|
| 304 |
m_mult = self._get_performance_multiplier(
|
| 305 |
-
self.m_errors, max_multiplier=7.0, min_multiplier=0.
|
| 306 |
)
|
| 307 |
x_mult = self._get_performance_multiplier(
|
| 308 |
-
self.x_errors, max_multiplier=15.0, min_multiplier=0.
|
| 309 |
)
|
| 310 |
|
| 311 |
quiet_weight = self.base_weights['quiet'] * quiet_mult
|
|
|
|
| 27 |
filtered_kwargs = dict(model_kwargs)
|
| 28 |
filtered_kwargs.pop('lr', None)
|
| 29 |
self.model = VisionTransformer(**filtered_kwargs)
|
| 30 |
+
self.adaptive_loss = SXRRegressionDynamicLoss(window_size=1500)
|
| 31 |
self.sxr_norm = sxr_norm
|
| 32 |
|
| 33 |
def forward(self, x, return_attention=True):
|
|
|
|
| 284 |
}
|
| 285 |
|
| 286 |
def calculate_loss(self, preds_squeezed, sxr, sxr_un, preds_squeezed_un):
|
| 287 |
+
#base_loss = F.huber_loss(preds_squeezed, sxr, delta=1.0, reduction='none')
|
| 288 |
+
base_loss = F.mse_loss(preds_squeezed, sxr, reduction='none')
|
| 289 |
weights = self._get_adaptive_weights(sxr_un, preds_squeezed_un, base_loss)
|
| 290 |
self._update_tracking(sxr_un, preds_squeezed_un, base_loss)
|
| 291 |
weighted_loss = base_loss * weights
|
|
|
|
| 300 |
self.quiet_errors, max_multiplier=1.5, min_multiplier=0.5, sensitivity=2.0, sxrclass = 'quiet'
|
| 301 |
)
|
| 302 |
c_mult = self._get_performance_multiplier(
|
| 303 |
+
self.c_errors, max_multiplier=5.0, min_multiplier=0.5, sensitivity=2.5, sxrclass = 'c_class'
|
| 304 |
)
|
| 305 |
m_mult = self._get_performance_multiplier(
|
| 306 |
+
self.m_errors, max_multiplier=7.0, min_multiplier=0.5, sensitivity=3.0, sxrclass = 'm_class'
|
| 307 |
)
|
| 308 |
x_mult = self._get_performance_multiplier(
|
| 309 |
+
self.x_errors, max_multiplier=15.0, min_multiplier=0.5, sensitivity=4.0, sxrclass = 'x_class'
|
| 310 |
)
|
| 311 |
|
| 312 |
quiet_weight = self.base_weights['quiet'] * quiet_mult
|
flaring/forecasting/training/config.yaml
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
|
| 2 |
#Base directories - change these to switch datasets
|
| 3 |
-
base_data_dir: "/mnt/data/ML-
|
| 4 |
-
base_checkpoint_dir: "/mnt/data/ML-
|
| 5 |
wavelengths: [94, 131, 171, 193, 211, 304] # AIA wavelengths in Angstroms
|
| 6 |
# Model configuration
|
| 7 |
selected_model: "ViT Patch" # Options: "cnn", "vit",
|
| 8 |
-
batch_size:
|
| 9 |
epochs: 500
|
| 10 |
oversample: false
|
| 11 |
balance_strategy: "upsample_minority"
|
|
@@ -21,9 +21,9 @@ vit_custom:
|
|
| 21 |
embed_dim: 512
|
| 22 |
num_channels: 6
|
| 23 |
num_classes: 1
|
| 24 |
-
patch_size:
|
| 25 |
-
num_patches:
|
| 26 |
-
hidden_dim:
|
| 27 |
num_heads: 8
|
| 28 |
num_layers: 6
|
| 29 |
dropout: 0.1
|
|
@@ -48,5 +48,5 @@ wandb:
|
|
| 48 |
- aia
|
| 49 |
- sxr
|
| 50 |
- regression
|
| 51 |
-
wb_name: vit-
|
| 52 |
notes: Regression from AIA images (6 channels) to GOES SXR flux
|
|
|
|
| 1 |
|
| 2 |
#Base directories - change these to switch datasets
|
| 3 |
+
base_data_dir: "/mnt/data/ML-READY" # Change this line for different datasets
|
| 4 |
+
base_checkpoint_dir: "/mnt/data/ML-READY" # Change this line for different datasets
|
| 5 |
wavelengths: [94, 131, 171, 193, 211, 304] # AIA wavelengths in Angstroms
|
| 6 |
# Model configuration
|
| 7 |
selected_model: "ViT Patch" # Options: "cnn", "vit",
|
| 8 |
+
batch_size: 80
|
| 9 |
epochs: 500
|
| 10 |
oversample: false
|
| 11 |
balance_strategy: "upsample_minority"
|
|
|
|
| 21 |
embed_dim: 512
|
| 22 |
num_channels: 6
|
| 23 |
num_classes: 1
|
| 24 |
+
patch_size: 16
|
| 25 |
+
num_patches: 1024
|
| 26 |
+
hidden_dim: 1024
|
| 27 |
num_heads: 8
|
| 28 |
num_layers: 6
|
| 29 |
dropout: 0.1
|
|
|
|
| 48 |
- aia
|
| 49 |
- sxr
|
| 50 |
- regression
|
| 51 |
+
wb_name: vit-16-MSE-deeper
|
| 52 |
notes: Regression from AIA images (6 channels) to GOES SXR flux
|
flaring/forecasting/training/train.py
CHANGED
|
@@ -12,6 +12,12 @@ from pytorch_lightning import Trainer
|
|
| 12 |
from pytorch_lightning.loggers import WandbLogger
|
| 13 |
from pytorch_lightning.callbacks import ModelCheckpoint
|
| 14 |
from torch.nn import MSELoss
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
from flaring.forecasting.data_loaders.SDOAIA_dataloader import AIA_GOESDataModule
|
| 16 |
from flaring.forecasting.models.vision_transformer_custom import ViT
|
| 17 |
from flaring.forecasting.models.linear_and_hybrid import LinearIrradianceModel, HybridIrradianceModel
|
|
|
|
| 12 |
from pytorch_lightning.loggers import WandbLogger
|
| 13 |
from pytorch_lightning.callbacks import ModelCheckpoint
|
| 14 |
from torch.nn import MSELoss
|
| 15 |
+
from pathlib import Path
|
| 16 |
+
import sys
|
| 17 |
+
# Add project root to Python path
|
| 18 |
+
PROJECT_ROOT = Path(__file__).parent.parent.parent.parent.absolute()
|
| 19 |
+
sys.path.insert(0, str(PROJECT_ROOT))
|
| 20 |
+
|
| 21 |
from flaring.forecasting.data_loaders.SDOAIA_dataloader import AIA_GOESDataModule
|
| 22 |
from flaring.forecasting.models.vision_transformer_custom import ViT
|
| 23 |
from flaring.forecasting.models.linear_and_hybrid import LinearIrradianceModel, HybridIrradianceModel
|
flaring/outputs/outputs.txt
DELETED
|
File without changes
|
flaring/vision_transformers/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# Vision Transformers module
|