Upload files to "/"

This commit is contained in:
David Rotermund 2025-04-08 15:21:11 +02:00
parent dbb382398a
commit 9f931f0f04
15 changed files with 796 additions and 0 deletions

17
cluster.bash Normal file
View file

@ -0,0 +1,17 @@
Directory="$(dirname "$(readlink -f "$0")")"
Priority="-450"
echo $Directory
mkdir -p $Directory/logs/cluster
project_name="MAHA"
python="/home/mahbod/P3.11/bin/python3"
for i in {1..3}
do
run="$python run_train.py -r $i"
echo "Running $run"
echo "hostname; nvidia-smi; cd $Directory ; $run" | qsub -o $Directory/logs/cluster -j y -p $Priority -q gp4u -N $project_name
done
# run="$python run_train.py -r 4"
# echo "Running $run"
# echo "hostname; nvidia-smi; cd $Directory ; $run" | qsub -o $Directory/logs/cluster -j y -p $Priority -q gp4u -N $project_name

13
config_data.json Normal file
View file

@ -0,0 +1,13 @@
{
// "CIFAR10", "FashionMNIST", "MNIST"
"dataset": "CIFAR10",
"batch_size_train": 50,
"batch_size_test": 50,
// Data augmentation
"flip_p": 0.5,
"jitter_brightness": 0.5,
"jitter_contrast": 0.1,
"jitter_saturation": 0.1,
"jitter_hue": 0.15,
"da_auto_mode": false
}

11
config_lr_parameter.json Normal file
View file

@ -0,0 +1,11 @@
{
"loss_mode": 0,
"loss_coeffs_mse": 0.5,
"loss_coeffs_kldiv": 1.0,
"lr_limit": 1e-9,
"lr_initial_neuron_a": 0.01,
"lr_initial_neuron_b": 0.001,
"lr_initial_norm": 0.001,
"lr_initial_batchnorm2d": 0.001,
"number_of_epoch": 500
}

232
config_network.json Normal file
View file

@ -0,0 +1,232 @@
{
// Number of NNMF iterations
"iterations": 20,
"epsilon": "None",
"number_of_neurons_a": [
32,
64,
96,
10
],
"number_of_neurons_b": [
-1,
-1,
-1,
-1
],
"kernel_size_conv": [
[
5,
5
],
[
5,
5
],
[
-1,
-1
],
[
1,
1
]
],
"stride_conv": [
[
1,
1
],
[
1,
1
],
[
1,
1
],
[
1,
1
]
],
"padding_conv": [
[
0,
0
],
[
0,
0
],
[
0,
0
],
[
0,
0
]
],
"dilation_conv": [
[
1,
1
],
[
1,
1
],
[
1,
1
],
[
1,
1
]
],
"kernel_size_pool": [
[
2,
2
],
[
2,
2
],
[
-1,
-1
],
[
-1,
-1
]
],
"stride_pool": [
[
2,
2
],
[
2,
2
],
[
-1,
-1
],
[
-1,
-1
]
],
"padding_pool": [
[
0,
0
],
[
0,
0
],
[
0,
0
],
[
0,
0
]
],
"dilation_pool": [
[
1,
1
],
[
1,
1
],
[
1,
1
],
[
1,
1
]
],
// 0 == No pooling, 1 = Average Pooling, 2 = Max Pooling, 3 == Perceptron, 4 == NNMF
"type_of_pooling": [
1,
1,
1,
1
],
"local_learning": [
false,
false,
false,
false
],
"local_learning_kl": [
false,
false,
false,
false
],
// NNMF:
"local_learning_pooling": [
false,
false,
false,
false
],
// NNMF:
"local_learning_use_kl_pooling": [
false,
false,
false,
false
],
// 1 == NNMF, 2 == Perceptron
"type_of_neuron_a": [
1,
1,
1,
1
],
// 0 = None, 1 == NNMF, 2 == Perceptron
"type_of_neuron_b": [
3,
3,
3,
3
],
"batch_norm_neuron_a": [
true,
true,
true,
true
],
"batch_norm_neuron_b": [
true,
true,
true,
true
],
"bias_norm_neuron_a": [
false,
false,
false,
false
],
"bias_norm_neuron_b": [
true,
true,
true,
true
]
}

31
convert_log_to_numpy.py Normal file
View file

@ -0,0 +1,31 @@
import os
import glob
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
from tensorboard.backend.event_processing import event_accumulator # type: ignore
import numpy as np
def get_data(path: str = "log_cnn"):
acc = event_accumulator.EventAccumulator(path)
acc.Reload()
which_scalar = "Test Number Correct"
te = acc.Scalars(which_scalar)
np_temp = np.zeros((len(te), 2))
for id in range(0, len(te)):
np_temp[id, 0] = te[id].step
np_temp[id, 1] = te[id].value
print(np_temp[:, 1] / 100)
np_temp = np.nan_to_num(np_temp)
return np_temp
for path in glob.glob("log_*"):
print(path)
data = get_data(path)
np.save("data_" + path + ".npy", data)

22
load_model.py Normal file
View file

@ -0,0 +1,22 @@
# %%
import torch
# load the state dict of the model
state = torch.load("Models/Model_seed_4_30.pt")
print(state)
# %%
from tools.make_network import make_network
model, _, _ = make_network(
input_dim_x=28,
input_dim_y=28,
input_number_of_channel=3,
device="cuda",
config_network_filename="config_network.json",
)
model.load_state_dict(state)
print(model)
# %%
(state['8.parametrizations.weight.original'] < 0).sum(), (model[8].weight < 0).sum()
# %%

102
noise_holes.py Normal file
View file

@ -0,0 +1,102 @@
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import argh
import numpy as np
import torch
rand_seed: int = 21
torch.manual_seed(rand_seed)
torch.cuda.manual_seed(rand_seed)
np.random.seed(rand_seed)
from get_the_data_uniform import get_the_data
def main(
dataset: str = "CIFAR10", # "CIFAR10", "FashionMNIST", "MNIST"
only_print_network: bool = False,
model_name: str = "Model_iter20_lr_1.0000e-03_1.0000e-02_1.0000e-03_.pt",
) -> None:
torch_device: torch.device = (
torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
)
torch.set_default_dtype(torch.float32)
# Some parameters
batch_size_test: int = 50 # 0
loss_mode: int = 0
loss_coeffs_mse: float = 0.5
loss_coeffs_kldiv: float = 1.0
print(
"loss_mode: ",
loss_mode,
"loss_coeffs_mse: ",
loss_coeffs_mse,
"loss_coeffs_kldiv: ",
loss_coeffs_kldiv,
)
if dataset == "MNIST" or dataset == "FashionMNIST":
input_dim_x: int = 24
input_dim_y: int = 24
else:
input_dim_x = 28
input_dim_y = 28
test_dataloader, test_processing_chain = get_the_data(
dataset,
batch_size_test,
torch_device,
input_dim_x,
input_dim_y,
)
network = torch.load(model_name)
network.to(device=torch_device)
print(network)
if only_print_network:
exit()
# Switch the network into evalution mode
network.eval()
number_of_noise_steps = 20
noise_scale = torch.arange(0, number_of_noise_steps + 1) / float(
number_of_noise_steps
)
results = torch.zeros_like(noise_scale)
with torch.no_grad():
for position in range(0, noise_scale.shape[0]):
test_correct: int = 0
test_number: int = 0
eta: float = noise_scale[position]
for image, target in test_dataloader:
noise = torch.rand_like(image) > eta
image = image * noise
image = image / (image.sum(dim=(1, 2, 3), keepdim=True) + 1e-20)
output = network(test_processing_chain(image))
test_correct += (output.argmax(dim=1) == target).sum().cpu().numpy()
test_number += target.shape[0]
perfomance_test_correct: float = 100.0 * test_correct / test_number
results[position] = perfomance_test_correct
print(f"{eta:.2f}: {perfomance_test_correct:.2f}%")
np.save("noise_holes_results.npy", results.cpu().numpy())
return
if __name__ == "__main__":
argh.dispatch_command(main)

107
noise_holes_w_noise.py Normal file
View file

@ -0,0 +1,107 @@
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import argh
import numpy as np
import torch
rand_seed: int = 21
torch.manual_seed(rand_seed)
torch.cuda.manual_seed(rand_seed)
np.random.seed(rand_seed)
from get_the_data_uniform import get_the_data
def main(
dataset: str = "CIFAR10", # "CIFAR10", "FashionMNIST", "MNIST"
only_print_network: bool = False,
model_name: str = "Model_iter20_lr_1.0000e-03_1.0000e-02_1.0000e-03_.pt",
) -> None:
torch_device: torch.device = (
torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
)
torch.set_default_dtype(torch.float32)
# Some parameters
batch_size_test: int = 50 # 0
loss_mode: int = 0
loss_coeffs_mse: float = 0.5
loss_coeffs_kldiv: float = 1.0
print(
"loss_mode: ",
loss_mode,
"loss_coeffs_mse: ",
loss_coeffs_mse,
"loss_coeffs_kldiv: ",
loss_coeffs_kldiv,
)
if dataset == "MNIST" or dataset == "FashionMNIST":
input_dim_x: int = 24
input_dim_y: int = 24
else:
input_dim_x = 28
input_dim_y = 28
test_dataloader, test_processing_chain = get_the_data(
dataset,
batch_size_test,
torch_device,
input_dim_x,
input_dim_y,
)
network = torch.load(model_name)
network.to(device=torch_device)
print(network)
if only_print_network:
exit()
# Switch the network into evalution mode
network.eval()
number_of_noise_steps = 20
noise_scale = torch.arange(0, number_of_noise_steps + 1) / float(
number_of_noise_steps
)
results = torch.zeros_like(noise_scale)
with torch.no_grad():
for position in range(0, noise_scale.shape[0]):
test_correct: int = 0
test_number: int = 0
eta: float = noise_scale[position]
for image, target in test_dataloader:
noise = torch.rand_like(image) > eta
noise_2 = torch.rand_like(image)
noise_2 = noise_2 / (noise_2.sum(dim=(1, 2, 3), keepdim=True) + 1e-20)
image = (
image * noise.type(dtype=torch.float32)
+ (1.0 - noise.type(dtype=torch.float32)) * noise_2
)
image = image / (image.sum(dim=(1, 2, 3), keepdim=True) + 1e-20)
output = network(test_processing_chain(image))
test_correct += (output.argmax(dim=1) == target).sum().cpu().numpy()
test_number += target.shape[0]
perfomance_test_correct: float = 100.0 * test_correct / test_number
results[position] = perfomance_test_correct
print(f"{eta:.2f}: {perfomance_test_correct:.2f}%")
np.save("noise_holes_w_noise_results.npy", results.cpu().numpy())
return
if __name__ == "__main__":
argh.dispatch_command(main)

110
noise_picture.py Normal file
View file

@ -0,0 +1,110 @@
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import argh
import numpy as np
import torch
rand_seed: int = 21
torch.manual_seed(rand_seed)
torch.cuda.manual_seed(rand_seed)
np.random.seed(rand_seed)
from get_the_data_picture import get_the_data
def main(
dataset: str = "CIFAR10", # "CIFAR10", "FashionMNIST", "MNIST"
only_print_network: bool = False,
model_name: str = "Model_iter20_lr_1.0000e-03_1.0000e-02_1.0000e-03_.pt",
) -> None:
torch_device: torch.device = (
torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
)
torch.set_default_dtype(torch.float32)
# Some parameters
batch_size_test: int = 50 # 0
loss_mode: int = 0
loss_coeffs_mse: float = 0.5
loss_coeffs_kldiv: float = 1.0
print(
"loss_mode: ",
loss_mode,
"loss_coeffs_mse: ",
loss_coeffs_mse,
"loss_coeffs_kldiv: ",
loss_coeffs_kldiv,
)
if dataset == "MNIST" or dataset == "FashionMNIST":
input_dim_x: int = 24
input_dim_y: int = 24
else:
input_dim_x = 28
input_dim_y = 28
network = torch.load(model_name)
network.to(device=torch_device)
print(network)
if only_print_network:
exit()
# Switch the network into evalution mode
network.eval()
number_of_noise_steps = 20
noise_scale = (
0.5 * torch.arange(0, number_of_noise_steps + 1) / float(number_of_noise_steps)
)
results = torch.zeros_like(noise_scale)
with torch.no_grad():
for position in range(0, noise_scale.shape[0]):
train_dataloader, test_dataloader, test_processing_chain = get_the_data(
dataset,
batch_size_test,
batch_size_test,
torch_device,
input_dim_x,
input_dim_y,
)
train_dataloader_iter = iter(train_dataloader)
test_dataloader_iter = iter(test_dataloader)
test_correct: int = 0
test_number: int = 0
eta: float = noise_scale[position]
max_iters = len(test_dataloader)
for _ in range(0, max_iters):
(image, target) = next(test_dataloader_iter)
(noise, _) = next(train_dataloader_iter)
noise = noise / (noise.sum(dim=(1, 2, 3), keepdim=True) + 1e-20)
image = image / (image.sum(dim=(1, 2, 3), keepdim=True) + 1e-20)
output = network(
test_processing_chain((1.0 - eta) * image + eta * noise)
)
test_correct += (output.argmax(dim=1) == target).sum().cpu().numpy()
test_number += target.shape[0]
perfomance_test_correct: float = 100.0 * test_correct / test_number
results[position] = perfomance_test_correct
print(f"{eta:.2f}: {perfomance_test_correct:.2f}%")
np.save("noise_picture_results.npy", results.cpu().numpy())
return
if __name__ == "__main__":
argh.dispatch_command(main)

5
noise_run.sh Normal file
View file

@ -0,0 +1,5 @@
/data_1/davrot/P3.12/bin/python3 noise_uniform.py
/data_1/davrot/P3.12/bin/python3 noise_picture.py
/data_1/davrot/P3.12/bin/python3 noise_holes.py
/data_1/davrot/P3.12/bin/python3 noise_holes_w_noise.py

103
noise_uniform.py Normal file
View file

@ -0,0 +1,103 @@
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import argh
import numpy as np
import torch
rand_seed: int = 21
torch.manual_seed(rand_seed)
torch.cuda.manual_seed(rand_seed)
np.random.seed(rand_seed)
from get_the_data_uniform import get_the_data
def main(
dataset: str = "CIFAR10", # "CIFAR10", "FashionMNIST", "MNIST"
only_print_network: bool = False,
model_name: str = "Model_iter20_lr_1.0000e-03_1.0000e-02_1.0000e-03_.pt",
) -> None:
torch_device: torch.device = (
torch.device("cuda:0") if torch.cuda.is_available() else torch.device("cpu")
)
torch.set_default_dtype(torch.float32)
# Some parameters
batch_size_test: int = 50 # 0
loss_mode: int = 0
loss_coeffs_mse: float = 0.5
loss_coeffs_kldiv: float = 1.0
print(
"loss_mode: ",
loss_mode,
"loss_coeffs_mse: ",
loss_coeffs_mse,
"loss_coeffs_kldiv: ",
loss_coeffs_kldiv,
)
if dataset == "MNIST" or dataset == "FashionMNIST":
input_dim_x: int = 24
input_dim_y: int = 24
else:
input_dim_x = 28
input_dim_y = 28
test_dataloader, test_processing_chain = get_the_data(
dataset,
batch_size_test,
torch_device,
input_dim_x,
input_dim_y,
)
network = torch.load(model_name)
network.to(device=torch_device)
print(network)
if only_print_network:
exit()
# Switch the network into evalution mode
network.eval()
number_of_noise_steps = 20
noise_scale = torch.arange(0, number_of_noise_steps + 1) / float(
number_of_noise_steps
)
results = torch.zeros_like(noise_scale)
with torch.no_grad():
for position in range(0, noise_scale.shape[0]):
test_correct: int = 0
test_number: int = 0
eta: float = noise_scale[position]
for image, target in test_dataloader:
noise = torch.rand_like(image)
noise = noise / (noise.sum(dim=(1, 2, 3), keepdim=True) + 1e-20)
image = image / (image.sum(dim=(1, 2, 3), keepdim=True) + 1e-20)
output = network(
test_processing_chain((1.0 - eta) * image + eta * noise)
)
test_correct += (output.argmax(dim=1) == target).sum().cpu().numpy()
test_number += target.shape[0]
perfomance_test_correct: float = 100.0 * test_correct / test_number
results[position] = perfomance_test_correct
print(f"{eta:.2f}: {perfomance_test_correct:.2f}%")
np.save("noise_uniform_results.npy", results.cpu().numpy())
return
if __name__ == "__main__":
argh.dispatch_command(main)

15
plot.py Normal file
View file

@ -0,0 +1,15 @@
import numpy as np
import matplotlib.pyplot as plt
data = np.load("data_log.npy")
plt.loglog(
data[:, 0],
100.0 * (1.0 - data[:, 1] / 10000.0),
"k",
)
plt.legend()
plt.xlabel("Epoch")
plt.ylabel("Error [%]")
plt.title("CIFAR10")
plt.show()

10
plot_test.py Normal file
View file

@ -0,0 +1,10 @@
# %%
import torch
import matplotlib.pyplot as plt
x = (torch.arange(0, 10000) / 1000) - 5.0
beta = 10.0
plt.plot(x, torch.nn.functional.relu(0.5 + 0.5 * torch.tanh(beta * x)))
plt.show()
# %%

9
run_test.py Normal file
View file

@ -0,0 +1,9 @@
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import argh
from tools.run_network_test import main
if __name__ == "__main__":
argh.dispatch_command(main)

9
run_train.py Normal file
View file

@ -0,0 +1,9 @@
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
import argh
from tools.run_network_train import main
if __name__ == "__main__":
argh.dispatch_command(main)