Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ultralytics Code Refactor https://ultralytics.com/actions #31

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ You can run the WAVE models using the following scripts:

Explore the beauty of waveform signals and training process visualization with the images below:

![Waveform Signals](https://github.com/ultralytics/wave/blob/main/data/waveforms.png "Waveform signals captured by the detector.") ![Training Visualization](https://github.com/ultralytics/wave/blob/main/data/wave.png "Visualization of the training process.")
![Waveform Signals](https://github.com/ultralytics/wave/blob/main/data/waveforms.png%20%22Waveform%20signals%20captured%20by%20the%20detector.%22) ![Training Visualization](https://github.com/ultralytics/wave/blob/main/data/wave.png%20%22Visualization%20of%20the%20training%20process.%22)

# πŸ“œ Citation

Expand All @@ -51,20 +51,20 @@ If you use this code in your research or wish to refer to the WAVE methodology,

# 🀝 Contribute

We value each contribution and invite you to participate in developing this pioneering ML approach for physics! Whether you're sharpening bugs, proposing new features, or enriching our documentation, find out how to contribute through our [Contributing Guide](https://docs.ultralytics.com/help/contributing). Also, let us know your thoughts by completing our [Survey](https://ultralytics.com/survey?utm_source=github&utm_medium=social&utm_campaign=Survey). A massive thank you πŸ™ to everyone involved!
We value each contribution and invite you to participate in developing this pioneering ML approach for physics! Whether you're sharpening bugs, proposing new features, or enriching our documentation, find out how to contribute through our [Contributing Guide](https://docs.ultralytics.com/help/contributing). Also, let us know your thoughts by completing our [Survey](https://www.ultralytics.com/survey?utm_source=github&utm_medium=social&utm_campaign=Survey). A massive thank you πŸ™ to everyone involved!

![Ultralytics Open-Source Contributors](https://github.com/ultralytics/assets/raw/main/im/image-contributors.png "Thanks to our community of contributors!")
![Ultralytics Open-Source Contributors](https://github.com/ultralytics/assets/raw/main/im/image-contributors.png%20%22Thanks%20to%20our%20community%20of%20contributors!%22)

# πŸ“„ License

Ultralytics is pleased to offer dual licensing options to accommodate a wide range of uses:

- **AGPL-3.0 License**: Our default [open-source license](https://github.com/ultralytics/ultralytics/blob/main/LICENSE), which is OSI-approved and encourages open collaboration and knowledge sharing, is perfect for students, educators, and enthusiasts.
- **Enterprise License**: For commercial applications that require a more flexible licensing arrangement, our enterprise license allows integration of Ultralytics software into proprietary products and services. Reach out through [Ultralytics Licensing](https://ultralytics.com/license) for more details.
- **Enterprise License**: For commercial applications that require a more flexible licensing arrangement, our enterprise license allows integration of Ultralytics software into proprietary products and services. Reach out through [Ultralytics Licensing](https://www.ultralytics.com/license) for more details.

# πŸ“¬ Contact Us

For bug reports, feature requests, and contributions, head to [GitHub Issues](https://github.com/ultralytics/velocity/issues). For questions and discussions about this project and other Ultralytics endeavors, join us on [Discord](https://ultralytics.com/discord)!
For bug reports, feature requests, and contributions, head to [GitHub Issues](https://github.com/ultralytics/velocity/issues). For questions and discussions about this project and other Ultralytics endeavors, join us on [Discord](https://discord.com/invite/ultralytics)!

<br>
<div align="center">
Expand Down
34 changes: 19 additions & 15 deletions gcp/wave_pytorch_gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def runexample(H, model, str, lr=0.001, amsgrad=False):

cuda = torch.cuda.is_available()
os.makedirs(f"{pathr}models", exist_ok=True)
name = (data[:-4] + "%s%glr%s" % (H[:], lr, str)).replace(", ", ".").replace("[", "_").replace("]", "_")
name = (data[:-4] + f"{H[:]}{lr:g}lr{str}").replace(", ", ".").replace("[", "_").replace("]", "_")

tica = time.time()
device = torch.device("cuda:0" if cuda else "cpu")
print("Running %s on %s\n%s" % (name, device.type, torch.cuda.get_device_properties(0) if cuda else ""))
print("Running {} on {}\n{}".format(name, device.type, torch.cuda.get_device_properties(0) if cuda else ""))

if not os.path.isfile(pathd + data):
os.system(f"wget -P data/ https://storage.googleapis.com/ultralytics/{data}")
Expand Down Expand Up @@ -75,7 +75,7 @@ def runexample(H, model, str, lr=0.001, amsgrad=False):
if L[i, 1] < best[1]:
best = (i, L[i, 1], copy.deepcopy(model.state_dict()))
if (i - best[0]) > validations:
print("\n%g validation checks exceeded at epoch %g." % (validations, i))
print(f"\n{validations:g} validation checks exceeded at epoch {i:g}.")
break

if i % printInterval == 0: # print and save progress
Expand All @@ -94,11 +94,11 @@ def runexample(H, model, str, lr=0.001, amsgrad=False):
model.load_state_dict(best[2])
dt = time.time() - tica

print("\nFinished %g epochs in %.3fs (%.3f epochs/s)\nBest results from epoch %g:" % (i + 1, dt, i / dt, best[0]))
print(f"\nFinished {i + 1:g} epochs in {dt:.3f}s ({i / dt:.3f} epochs/s)\nBest results from epoch {best[0]:g}:")
loss, std = np.zeros(3), np.zeros((3, ny))
for i, (xi, yi) in enumerate(((x, y), (xv, yv), (xt, yt))):
loss[i], std[i] = stdpt(model(xi) - yi, ys)
print("%.5f %s %s" % (loss[i], std[i, :], labels[i]))
print(f"{loss[i]:.5f} {std[i, :]} {labels[i]}")
# scipy.io.savemat(pathr + name + '.mat', dict(bestepoch=best[0], loss=loss, std=std, L=L, name=name))
# files.download(pathr + name + '.mat')

Expand All @@ -117,11 +117,13 @@ def runexample(H, model, str, lr=0.001, amsgrad=False):


class LinearAct(torch.nn.Module):
"""Applies a linear transformation followed by a Tanh activation to input tensors for neural network layers."""

def __init__(self, nx, ny):
"""Initializes the LinearAct module with input and output dimensions and defines a linear transformation
followed by a Tanh activation.
"""
super(LinearAct, self).__init__()
super().__init__()
self.Linear1 = torch.nn.Linear(nx, ny)
self.act = torch.nn.Tanh()

Expand All @@ -131,9 +133,11 @@ def forward(self, x):


class WAVE(torch.nn.Module):
"""A neural network model for processing waveform data using linear layers and activation functions."""

def __init__(self, n): # n = [512, 108, 23, 5, 1]
"""Initializes the WAVE model with specified linear layers and activation functions."""
super(WAVE, self).__init__()
super().__init__()
self.fc0 = LinearAct(n[0], n[1])
self.fc1 = LinearAct(n[1], n[2])
self.fc2 = torch.nn.Linear(n[2], n[3])
Expand All @@ -156,7 +160,7 @@ def tsact(): # TS activation function

class LinearAct(torch.nn.Module):
def __init__(self, nx, ny):
super(LinearAct, self).__init__()
super().__init__()
self.Linear1 = torch.nn.Linear(nx, ny)
self.act = eval(f"torch.nn.{a}()")

Expand All @@ -165,7 +169,7 @@ def forward(self, x):

class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
super(WAVE, self).__init__()
super().__init__()
self.fc0 = LinearAct(n[0], n[1])
self.fc1 = LinearAct(n[1], n[2])
self.fc2 = torch.nn.Linear(n[2], n[3])
Expand All @@ -189,7 +193,7 @@ def tsnoact(): # TS activation function

class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
super(WAVE, self).__init__()
super().__init__()
self.fc0 = torch.nn.Linear(n[0], n[1])
self.fc1 = torch.nn.Linear(n[1], n[2])
self.fc2 = torch.nn.Linear(n[2], n[3])
Expand Down Expand Up @@ -242,7 +246,7 @@ def tsshape(): # TS network shape

class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
super(WAVE, self).__init__()
super().__init__()
self.fc0 = LinearAct(n[0], n[1])
self.fc1 = torch.nn.Linear(n[1], n[2])

Expand All @@ -254,7 +258,7 @@ def forward(self, x):

class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
super(WAVE, self).__init__()
super().__init__()
self.fc0 = LinearAct(n[0], n[1])
self.fc1 = LinearAct(n[1], n[2])
self.fc2 = torch.nn.Linear(n[2], n[3])
Expand All @@ -269,7 +273,7 @@ def forward(self, x):

class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
super(WAVE, self).__init__()
super().__init__()
self.fc0 = LinearAct(n[0], n[1])
self.fc1 = LinearAct(n[1], n[2])
self.fc2 = LinearAct(n[2], n[3])
Expand All @@ -285,7 +289,7 @@ def forward(self, x):

class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
super(WAVE, self).__init__()
super().__init__()
self.fc0 = LinearAct(n[0], n[1])
self.fc1 = LinearAct(n[1], n[2])
self.fc2 = LinearAct(n[2], n[3])
Expand All @@ -302,7 +306,7 @@ def forward(self, x):

class WAVE(torch.nn.Module):
def __init__(self, n): # n = [512, 108, 23, 5, 1]
super(WAVE, self).__init__()
super().__init__()
self.fc0 = LinearAct(n[0], n[1])
self.fc1 = LinearAct(n[1], n[2])
self.fc2 = LinearAct(n[2], n[3])
Expand Down
20 changes: 14 additions & 6 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def train(H, model, str, lr=0.001):

cuda = torch.cuda.is_available()
os.makedirs(f"{pathr}models", exist_ok=True)
name = (data[:-4] + "%s%glr%s" % (H[:], lr, str)).replace(", ", ".").replace("[", "_").replace("]", "_")
name = (data[:-4] + f"{H[:]}{lr:g}lr{str}").replace(", ", ".").replace("[", "_").replace("]", "_")
print(f"Running {name}")

device = select_device()
Expand Down Expand Up @@ -112,7 +112,7 @@ def train(H, model, str, lr=0.001):
r = stopper.bestmodel(xi) - yi # residuals, ().detach?
loss[i] = (r**2).mean().cpu().item()
std[i] = r.std(0).cpu().numpy() * ys
print("%.5f %s %s" % (loss[i], std[i, :], labels[i]))
print(f"{loss[i]:.5f} {std[i, :]} {labels[i]}")

scipy.io.savemat(pathr + name + ".mat", dict(bestepoch=stopper.bestloss, loss=loss, std=std, L=L, name=name))
# files.download(pathr + name + '.mat')
Expand All @@ -122,9 +122,11 @@ def train(H, model, str, lr=0.001):

# 400 5.1498e-05 0.023752 12.484 0.15728 # var 0
class WAVE(torch.nn.Module):
"""Implements a neural network model for waveform data processing using a multi-layer perceptron architecture."""

def __init__(self, n=(512, 64, 8, 2)):
"""Initializes the WAVE model architecture with specified layer sizes."""
super(WAVE, self).__init__()
super().__init__()
self.fc0 = nn.Linear(n[0], n[1])
self.fc1 = nn.Linear(n[1], n[2])
self.fc2 = nn.Linear(n[2], n[3])
Expand All @@ -139,9 +141,11 @@ def forward(self, x): # x.shape = [bs, 512]
# https://github.com/yunjey/pytorch-tutorial/tree/master/tutorials/02-intermediate
# 121 0.47059 0.0306 14.184 0.1608
class WAVE4(nn.Module):
"""Implements a convolutional neural network for waveform data processing with customizable output layers."""

def __init__(self, n_out=2):
"""Initializes the WAVE4 model with specified output layers and configurations for convolutional layers."""
super(WAVE4, self).__init__()
super().__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=(1, 9), stride=(1, 2), padding=(0, 4), bias=False),
nn.BatchNorm2d(32),
Expand Down Expand Up @@ -170,11 +174,13 @@ def forward(self, x): # x.shape = [bs, 512]

# 65 4.22e-05 0.021527 11.883 0.14406
class WAVE3(nn.Module):
"""WAVE3 implements a convolutional neural network for feature extraction and classification on waveform data."""

def __init__(self, n_out=2):
"""Initializes the WAVE3 class with neural network layers for feature extraction and classification in a
sequential manner.
"""
super(WAVE3, self).__init__()
super().__init__()
n = 32
self.layer1 = nn.Sequential(
nn.Conv2d(in_channels=2, out_channels=n, kernel_size=(1, 33), stride=(1, 2), padding=(0, 16), bias=False),
Expand Down Expand Up @@ -215,9 +221,11 @@ def forward(self, x): # x.shape = [bs, 512]

# 121 2.6941e-05 0.021642 11.923 0.14201 # var 1
class WAVE2(nn.Module):
"""Implements a convolutional neural network for waveform data processing with configurable output dimensions."""

def __init__(self, n_out=2):
"""Initializes the WAVE2 model architecture components."""
super(WAVE2, self).__init__()
super().__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=(2, 30), stride=(1, 2), padding=(1, 15), bias=False),
nn.BatchNorm2d(32),
Expand Down
8 changes: 4 additions & 4 deletions train_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def runexample(H, model, str):
tf.set_random_seed(1)
path = "data/"
os.makedirs(f"{path}models", exist_ok=True)
name = (data[:-4] + "%s%glr%geps%s" % (H[:], lr, eps, str)).replace(", ", "_").replace("[", "_").replace("]", "_")
name = (data[:-4] + f"{H[:]}{lr:g}lr{eps:g}eps{str}").replace(", ", "_").replace("[", "_").replace("]", "_")

tica = time.time()
device = "/gpu:0" if cuda else "/cpu:0"
Expand Down Expand Up @@ -92,7 +92,7 @@ def criteria(y_pred, y): # MSE
if L[i, 1] < best[1]:
best = (i, L[i, 1], None)
if (i - best[0]) > validations:
print("\n%g validation checks exceeded at epoch %g." % (validations, i))
print(f"\n{validations:g} validation checks exceeded at epoch {i:g}.")
break

if i % printInterval == 0: # print and save progress
Expand All @@ -109,11 +109,11 @@ def criteria(y_pred, y): # MSE
# model.load_state_dict(best[2])
dt = time.time() - tica

print("\nFinished %g epochs in %.3fs (%.3f epochs/s)\nBest results from epoch %g:" % (i + 1, dt, i / dt, best[0]))
print(f"\nFinished {i + 1:g} epochs in {dt:.3f}s ({i / dt:.3f} epochs/s)\nBest results from epoch {best[0]:g}:")
loss, std = np.zeros(3), np.zeros((3, ny))
for i, (xi, yi) in enumerate(((x, y), (xv, yv), (xt, yt))):
loss[i], std[i] = stdtf(model(xi) - yi, ys)
print("%.5f %s %s" % (loss[i], std[i, :], labels[i]))
print(f"{loss[i]:.5f} {std[i, :]} {labels[i]}")
# scipy.io.savemat(path + name + '.mat', dict(bestepoch=best[0], loss=loss, std=std, L=L, name=name))
# files.download(path + name + '.mat')

Expand Down
13 changes: 7 additions & 6 deletions utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ def model_info(model):
"%5g %40s %9s %12g %20s %10.3g %10.3g"
% (i, name, p.requires_grad, p.numel(), list(p.shape), p.mean(), p.std())
)
print("Model Summary: %g layers, %g parameters, %g gradients" % (i + 1, n_p, n_g))
print(f"Model Summary: {i + 1:g} layers, {n_p:g} parameters, {n_g:g} gradients")


class patienceStopper(object):
class patienceStopper:
"""Monitors training loss and metrics to halt early when no improvement is seen for a specified patience period."""

def __init__(self, patience=10, verbose=True, epochs=1000, printerval=10):
"""Initialize a patience stopper with given parameters for early stopping in training."""
self.patience = patience
Expand Down Expand Up @@ -113,10 +115,10 @@ def step(self, loss, metrics=None, model=None):
self.bestmodel = copy.deepcopy(model)

if self.num_bad_epochs > self.patience:
self.final("%g Patience exceeded at epoch %g." % (self.patience, self.epoch))
self.final(f"{self.patience:g} Patience exceeded at epoch {self.epoch:g}.")
return True
elif self.epoch >= self.epochs:
self.final("WARNING: %g Patience not exceeded by epoch %g (train longer)." % (self.patience, self.epoch))
self.final(f"WARNING: {self.patience:g} Patience not exceeded by epoch {self.epoch:g} (train longer).")
return True
else:
return False
Expand All @@ -139,7 +141,6 @@ def final(self, msg):
"""Print final results and completion message with total elapsed time and best training epoch details."""
dt = time.time() - self.t0
print(
"%s\nFinished %g epochs in %.3fs (%.3f epochs/s). Best results:"
% (msg, self.epochs + 1, dt, (self.epochs + 1) / dt)
f"{msg}\nFinished {self.epochs + 1:g} epochs in {dt:.3f}s ({(self.epochs + 1) / dt:.3f} epochs/s). Best results:"
)
self.printepoch(self.bestepoch, self.bestloss, self.bestmetrics)