Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,82 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
datasets:
|
| 4 |
+
- lucazsh/RadNet
|
| 5 |
+
tags:
|
| 6 |
+
- space
|
| 7 |
+
- radiation
|
| 8 |
+
- LEO
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
```
|
| 12 |
+
import json
|
| 13 |
+
import torch
|
| 14 |
+
import torch.nn as nn
|
| 15 |
+
import numpy as np
|
| 16 |
+
|
| 17 |
+
class RadNet(nn.Module):
|
| 18 |
+
def __init__(self, input_size=2, hidden_size=64, num_layers=2, output_size=2):
|
| 19 |
+
super(RadNet, self).__init__()
|
| 20 |
+
self.rnn = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True,
|
| 21 |
+
dropout=0.2)
|
| 22 |
+
self.fc = nn.Linear(hidden_size, output_size)
|
| 23 |
+
|
| 24 |
+
def forward(self, x):
|
| 25 |
+
# x = [batch_size, sequence_length, input_size]
|
| 26 |
+
out, _ = self.rnn(x) # [batch_size, sequence_length, hidden_size]
|
| 27 |
+
return self.fc(out[:, -1, :]) # [batch_size, output_size]
|
| 28 |
+
|
| 29 |
+
def main():
|
| 30 |
+
path = "./RadNet.pth" # path to the pre-trained RadNet checkpoint
|
| 31 |
+
data_path = "test.json" # path to the JSON file with test data
|
| 32 |
+
|
| 33 |
+
# load the data from JSON
|
| 34 |
+
with open(data_path, 'r') as f:
|
| 35 |
+
data = json.load(f)
|
| 36 |
+
|
| 37 |
+
# load the checkpoint to the preferred device, in our case cpu
|
| 38 |
+
checkpoint = torch.load(path, weights_only=False, map_location=torch.device('cpu'))
|
| 39 |
+
cfg = checkpoint["config"]
|
| 40 |
+
mean = checkpoint["mean"]
|
| 41 |
+
std = checkpoint["std"]
|
| 42 |
+
|
| 43 |
+
model = RadNet(hidden_size=64, num_layers=2)
|
| 44 |
+
model.load_state_dict(checkpoint["model_state_dict"])
|
| 45 |
+
model.eval()
|
| 46 |
+
|
| 47 |
+
tmp_data = []
|
| 48 |
+
for d in data:
|
| 49 |
+
solar_storm = d.get("solar_storm_score", 0.0)
|
| 50 |
+
daily_radiation = d.get("sv_per_day_mSv", 0.0)
|
| 51 |
+
tmp_data.append([solar_storm, daily_radiation])
|
| 52 |
+
|
| 53 |
+
raw_data = np.array(tmp_data, dtype=float)
|
| 54 |
+
|
| 55 |
+
print(f"RadNet was trained with a sequence of: {cfg['seq_length']} days.")
|
| 56 |
+
print(f"Available data in the JSON dataset: {len(raw_data)} days.")
|
| 57 |
+
|
| 58 |
+
user_seq_len = int(input(f"Enter the number of days: "))
|
| 59 |
+
if len(raw_data) < user_seq_len:
|
| 60 |
+
print(f"You do not have enough data in the JSON file for {user_seq_len} days.")
|
| 61 |
+
return
|
| 62 |
+
|
| 63 |
+
input_seq = raw_data[-user_seq_len:]
|
| 64 |
+
input_norm = (input_seq - mean) / (std + 1e-6)
|
| 65 |
+
input_tensor = torch.tensor(input_norm, dtype=torch.float32).unsqueeze(0)
|
| 66 |
+
|
| 67 |
+
# predict using the model
|
| 68 |
+
with torch.no_grad():
|
| 69 |
+
pred_norm = model(input_tensor).numpy()[0]
|
| 70 |
+
pred_values = (pred_norm * (std + 1e-6)) + mean
|
| 71 |
+
|
| 72 |
+
if user_seq_len == 1:
|
| 73 |
+
print(f"Prediction based on the last day:")
|
| 74 |
+
else:
|
| 75 |
+
print(f"Prediction based on the last {user_seq_len} days:")
|
| 76 |
+
|
| 77 |
+
print(f"Solar storm score: {pred_values[0]:.6f}")
|
| 78 |
+
print(f"Radiation (mSv): {pred_values[1]:.6f}")
|
| 79 |
+
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
main()
|
| 82 |
+
```
|