Skip to content
Snippets Groups Projects
Commit a805a9aa authored by Lam Giang Tran's avatar Lam Giang Tran
Browse files

final scripts

parent 0226b66d
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:a2409e03-673f-4546-9b59-9f7bdee1fd6e tags:
``` python
## AUTOENCODER-IMPLEMENTATION - Summer Student Project
# based on ==> https://www.geeksforgeeks.org/implementing-an-autoencoder-in-pytorch/
# by Lam Giang Tran, Chiara Amendola, Pedro Silva
```
%% Cell type:code id:44e646d5 tags:
``` python
# Loading Libaries
import torch
import ROOT
import uproot
import numpy as np
import torch.nn as nn
import torch.optim as optim
import torchvision.datasets as datasets
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
from torch.utils.data import DataLoader,TensorDataset
```
%% Output
Welcome to JupyROOT 6.30/04
%% Cell type:code id:90df5809 tags:
``` python
#read the data
#fIn=ROOT('Events','/eos/user/l/latran/Autoencoder_Project/data/Test_Dataloader/Training_Dataset.root:Events')
## Start reading Data
# loading Dataset Training
Dataset_Training = uproot.open('/eos/user/l/latran/Autoencoder_Project/data/Training_Dataloader/Training_Dataset.root:Events');
channal_training = Dataset_Training['x'].array(library="np"); #print(ch.shape)
x_training = Dataset_Training['y'].array(library="np"); print(x_training[0]); #print(x.shape); print(x)
x_training = x_training.astype(dtype='f'); np.array(x_training)
# Loading Dataset Testing
Dataset_Testing = uproot.open('/eos/user/l/latran/Autoencoder_Project/data/Training_Dataloader/Test_Dataset.root:Events');
channal_testing = Dataset_Testing['x'].array(library="np"); #print(ch.shape)
x_testing = Dataset_Testing['y'].array(library="np"); print(x_testing[0]) #print(x.shape); print(x)
x_testing = x_testing.astype(dtype='f'); np.array(x_testing)
#convert to pytorch tensors
X_Training = torch.from_numpy(x_training)
X_Testing = torch.from_numpy(x_testing)
if torch.cuda.is_available():
print("Using CUDA as it's available")
X_Training = X_Training.cuda()
X_Testing = X_Testing.cuda()
#split in training and validation -> already splitted during data preperation
b_Training = X_Training.shape[0]; print(b_Training)
b_Testing = X_Testing.shape[0]; print(b_Testing)
# fsplit = 0.1 -> already splitted during data preperation
# t = int((1-fsplit)*b); print(t)
batch_size=8192; # batch_size=1024
#train_tensors = TensorDataset(X[:b-t],X[:b-t]); print(train_tensors)
#test_tensors = TensorDataset(X[b-t:b],X[b-t:b]); print(test_tensors)
#create the data loaders
train_tensors = TensorDataset(X_Training[:b_Training],X_Training[:b_Training]); # print(len(train_tensors))
train_dataloader = DataLoader(train_tensors, batch_size=batch_size, shuffle=True); # print(len(train_dataloader))
test_tensors = TensorDataset(X_Testing[:b_Testing],X_Testing[:b_Testing]); # print(len(test_tensors))
test_dataloader = DataLoader(test_tensors, batch_size=batch_size, shuffle=True); # print(len(test_dataloader))
```
%% Output
[ 89. 87. 79. 88. 87. 89. 83. 93. 93. 93. 95. 93. 89. 90.
87. 85. 93. 91. 92. 91. 95. 91. 89. 93. 89. 90. 93. 89.
90. 86. 89. 89. 86. 87. 91. 93. 86. 89. 91. 87. 89. 90.
83. 91. 94. 93. 95. 101. 93. 79. 89. 94. 106. 87. 89. 95.
89. 95. 93. 91. 91. 90. 91. 93. 87. 97. 91. 91. 89. 91.
91. 87. 90. 93.]
[ 79 83 77 83 83 79 77 87 92 86 181 85 81 83 77 82 83 95
89 91 91 81 83 82 82 86 57 84 91 79 75 83 77 82 79 94
163 87 87 84 83 84 95 83 89 95 87 93 895 83 85 95 117 185
90 95 91 89 87 89 86 85 85 89 86 94 83 85 89 87 85 84
85 85]
Using CUDA as it's available
599999
399999
%% Cell type:code id:e42c878b-b3a0-4b00-8aba-9eb8a8f8f066 tags:
``` python
%%time
# Define the autoencoder architecture
class Autoencoder(nn.Module):
def __init__(self):
super().__init__()
self.encoder = nn.Sequential(
nn.Linear(74, 148),
nn.ReLU(),
nn.Linear(148, 128),
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, 32),
nn.ReLU(),
nn.Linear(32, 16),
nn.ReLU(),
nn.Linear(16, 8),
nn.ReLU(),
nn.Linear(8, 4),
nn.ReLU(),
nn.Linear(4, 2),
nn.ReLU()
)
self.decoder = nn.Sequential(
nn.Linear(2, 4),
nn.ReLU(),
nn.Linear(4, 8),
nn.ReLU(),
nn.Linear(8, 16),
nn.ReLU(),
nn.Linear(16, 32),
nn.ReLU(),
nn.Linear(32, 64),
nn.ReLU(),
nn.Linear(64, 128),
nn.ReLU(),
nn.Linear(128, 148),
nn.ReLU(),
nn.Linear(148, 74),
nn.Sigmoid()
)
def forward(self, x):
encoded = self.encoder(x);
decoded = self.decoder(encoded);
return decoded
# Initialize the autoencoder
model = Autoencoder()
# Define transform
#transform = transforms.Compose([
# transforms.Resize((64, 64)),
# transforms.ToTensor(),
#])
# Move the model to GPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(device)
model.to(device)
# Define the loss function and optimizer
criterion = nn.MSELoss(reduction='mean')# mean square of difference of input- and output layer
optimizer = optim.Adam(model.parameters(), lr=0.0001, weight_decay = 1e-4)
# Train the autoencoder
num_epochs = 50
outputs = []
trained = []
losses = []
for epoch in range(num_epochs):
for data in train_dataloader:
img, _ = data; # print("img:",img); # print("_:", _)
img = img.to(device);
output = model(img); # print("output:", output)
optimizer.zero_grad()
loss = criterion(output, img) / 7400; # print("loss:", loss)
loss.backward()
optimizer.step()
trained.append(output)
losses.append(loss)
outputs.append((epoch, img, output))
if epoch % 5 == 0:
print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))
# Save the model
torch.save(model.state_dict(), 'autoencoder.pth')
```
%% Output
cuda
Epoch [1/50], Loss: 1.0179
Epoch [6/50], Loss: 1.0057
Epoch [11/50], Loss: 1.0014
Epoch [16/50], Loss: 1.0028
Epoch [21/50], Loss: 1.0063
Epoch [26/50], Loss: 1.0072
Epoch [31/50], Loss: 1.0030
Epoch [36/50], Loss: 1.0087
Epoch [41/50], Loss: 1.0078
Epoch [46/50], Loss: 1.0043
CPU times: user 8min 30s, sys: 3.02 s, total: 8min 33s
Wall time: 8min 34s
%% Cell type:code id:fdec8545 tags:
``` python
losses = torch.tensor(losses).detach().cpu().numpy()
# losses = losses.cpu().numpy()
# Defining the Plot Style
plt.figure(facecolor='white')
plt.style.use('fivethirtyeight')
plt.xlabel('Iterations / Epoch')
plt.ylabel('Loss')
plt.plot((losses[:350]))
print("COMPLETED optimizing loss function.")
```
%% Output
COMPLETED optimizing loss function.
%% Cell type:code id:98efdf90-ae7d-4e15-ac36-da7e6a961aa2 tags:
``` python
print(losses[40])
#print(np.size(losses))
```
%% Output
1.0159957
%% Cell type:code id:5d644d42 tags:
``` python
# Test the autoencoder
num_epochs = 50
outputs = []
tested = []
losses = []
model_state_dict = torch.load('autoencoder.pth');
model = Autoencoder();
model.load_state_dict(model_state_dict)
R=[];
model.cpu();
with torch.no_grad():
for b, xtest in enumerate(test_dataloader):
img, _ = xtest;
img = img.cpu();
# Apply the model
xpred = model.forward(img)
# Update test loss & accuracy for the epoch
losstest = criterion(xpred, img) / 19000
losses.append(losstest.item());
y = xpred - img;
R.append( y.numpy() )
print('Batch [{}], Loss: {:.4f}'.format(b, losstest.item()))
```
%% Output
Batch [0], Loss: 1.0065
Batch [1], Loss: 1.0061
Batch [2], Loss: 1.0049
Batch [3], Loss: 1.0064
Batch [4], Loss: 1.0057
Batch [5], Loss: 1.0057
Batch [6], Loss: 1.0062
Batch [7], Loss: 1.0062
Batch [8], Loss: 1.0058
Batch [9], Loss: 1.0072
Batch [10], Loss: 1.0047
Batch [11], Loss: 1.0063
Batch [12], Loss: 1.0049
Batch [13], Loss: 1.0064
Batch [14], Loss: 1.0060
Batch [15], Loss: 1.0061
Batch [16], Loss: 1.0046
Batch [17], Loss: 1.0064
Batch [18], Loss: 1.0056
Batch [19], Loss: 1.0060
Batch [20], Loss: 1.0054
Batch [21], Loss: 1.0068
Batch [22], Loss: 1.0071
Batch [23], Loss: 1.0055
Batch [24], Loss: 1.0050
Batch [25], Loss: 1.0062
Batch [26], Loss: 1.0064
Batch [27], Loss: 1.0064
Batch [28], Loss: 1.0059
Batch [29], Loss: 1.0052
Batch [30], Loss: 1.0049
Batch [31], Loss: 1.0067
Batch [32], Loss: 1.0067
Batch [33], Loss: 1.0051
Batch [34], Loss: 1.0053
Batch [35], Loss: 1.0053
Batch [36], Loss: 1.0058
Batch [37], Loss: 1.0062
Batch [38], Loss: 1.0051
Batch [39], Loss: 1.0064
Batch [40], Loss: 1.0051
Batch [41], Loss: 1.0050
Batch [42], Loss: 1.0057
Batch [43], Loss: 1.0056
Batch [44], Loss: 1.0055
Batch [45], Loss: 1.0073
Batch [46], Loss: 1.0054
Batch [47], Loss: 1.0065
Batch [48], Loss: 1.0073
%% Cell type:code id:032fea41 tags:
``` python
losses = torch.tensor(losses).detach().cpu().numpy()
print(losses)
```
%% Output
[1.0064617 1.0061032 1.0048829 1.0064017 1.0057319 1.0056655 1.0062454
1.0061747 1.0057641 1.0072342 1.0046877 1.0062848 1.0049303 1.0063547
1.006006 1.006109 1.0045859 1.0064313 1.0056365 1.0059724 1.0054065
1.0068065 1.0070988 1.0054579 1.0049857 1.0061715 1.0063528 1.0063652
1.0058655 1.005199 1.0048867 1.0066928 1.00673 1.0050893 1.005252
1.0052563 1.0057722 1.0062329 1.0051069 1.0063674 1.0050957 1.0049974
1.0057102 1.0055721 1.0055089 1.0072767 1.0053504 1.0065398 1.0072845]
%% Cell type:code id:b470f379 tags:
``` python
# Defining the Plot Style
plt.figure(facecolor='white')
plt.style.use('fivethirtyeight')
plt.xlabel('Batch / Epoch')
plt.ylabel('Loss')
plt.plot((losses[:100]))
print("COMPLETED optimizing loss function.")
```
%% Output
COMPLETED optimizing loss function.
%% Cell type:raw id:b9a582bd-b093-4c0d-8e7e-22bfbf9b7abf tags:
for i in enumerate(img):
# print(i[1])
plt.imshow(i[0])
# Reshape the array for plotting
for i in enumerate(trained):
# print(i[1])
plt.imshow(i[0])
%% Cell type:raw id:c3fde6db-58ca-4f10-b6fb-dc81cf134d1f tags:
print(img[1]);
print(trained[1]);
print(xpred[1]);
%% Cell type:code id:1ecd9a86 tags:
``` python
x_predicted_percentage = xpred.numpy();
x_predicted = x_predicted_percentage * img[1].numpy();
print(x_predicted)
```
%% Output
[[86.7538 88.747986 88.7403 ... 94.745834 90.75315 94.74665 ]
[86.7538 88.747986 88.7403 ... 94.745834 90.75315 94.74665 ]
[86.7538 88.747986 88.7403 ... 94.745834 90.75315 94.74665 ]
...
[86.7538 88.747986 88.7403 ... 94.745834 90.75315 94.74665 ]
[86.7538 88.747986 88.7403 ... 94.745834 90.75315 94.74665 ]
[86.7538 88.747986 88.7403 ... 94.745834 90.75315 94.74665 ]]
%% Cell type:code id:8fac20dd tags:
``` python
def plotting_ADC(_event_number, _adc_raw, _adc_reconstructed):
# Parameters
Event_raw = _adc_raw;
Event_reconstructed = _adc_reconstructed;
# Plotting Values
plot_values_channel = np.arange(np.size(Event_raw));
# Plotting Paramters
# Plotting Parameters
plot_titel = "Event Display"
plt.figure(facecolor='white')
# Plotting - normal
plt.plot(plot_values_channel, Event_raw, drawstyle = "steps-post", label = "raw signal")
plt.plot(plot_values_channel, Event_reconstructed, drawstyle = "steps-post", label = "reconstructed")
plt.title(plot_titel)
plt.legend(loc="upper left")
plt.xlabel("channel number")
plt.ylabel("ADC value")
#plt.grid()
plt.show
#plt.savefig('Event_comparison.eps', format='eps')
#plt.savefig('Event_comparison.png', format='png')
```
%% Cell type:code id:10a5cec0 tags:
``` python
Event_number = 19
Event_raw = img[Event_number].numpy(); print(Event_raw)
Event_predicted = x_predicted[Event_number]; print(np.round(Event_predicted))
plotting_ADC(Event_number, Event_raw-Event_raw, Event_predicted-Event_raw)
```
%% Output
[ 89. 87. 79. 85. 85. 84. 79. 87. 91. 86. 183. 87. 86. 86.
81. 88. 93. 93. 89. 91. 89. 89. 87. 89. 87. 87. 75. 87.
89. 85. 89. 90. 83. 89. 87. 92. 85. 91. 90. 85. 87. 88.
84. 111. 88. 95. 90. 86. 895. 83. 87. 95. 91. 191. 89. 95.
90. 98. 90. 89. 90. 91. 89. 89. 91. 93. 89. 89. 90. 89.
89. 91. 89. 93.]
[ 87. 89. 89. 90. 89. 90. 85. 93. 91. 89. 187. 90. 87. 91.
86. 91. 95. 91. 89. 91. 91. 93. 87. 95. 90. 91. 79. 89.
87. 89. 90. 93. 89. 91. 91. 93. 89. 94. 91. 93. 90. 94.
89. 94. 93. 93. 95. 90. 893. 89. 91. 94. 95. 194. 89. 95.
90. 95. 93. 93. 90. 91. 95. 95. 90. 95. 93. 95. 89. 95.
95. 95. 91. 95.]
%% Cell type:code id:9d53bf5e tags:
``` python
```
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment