Skip to content
Snippets Groups Projects
Commit f79c9949 authored by Brian Christopher Wasels's avatar Brian Christopher Wasels
Browse files

postprocessing script verbessert

parent a70b5994
Branches
No related tags found
No related merge requests found
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import torch import torch
import numpy as np import numpy as np
import torch.nn as nn import torch.nn as nn
from torch.utils.data import TensorDataset from torch.utils.data import TensorDataset
import sklearn.preprocessing as sp import sklearn.preprocessing as sp
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
min_label, max_label,angles_min_max = np.load('E:/Data/damask3/UNet/Input/Norm_min_max_64.npy', allow_pickle= True)
```
%% Cell type:code id: tags:
``` python
training_data = np.load('E:/Data/damask3/UNet/Input/Training_data_64.npy') training_data = np.load('E:/Data/damask3/UNet/Input/Training_data_64.npy')
training_label = np.load('E:/Data/damask3/UNet/Input/Training_labels_64.npy') training_label = np.load('E:/Data/damask3/UNet/Input/Training_labels_64.npy')
training_data = training_data[0:10]
training_label = training_label[0:10]
if training_data.shape[0] != training_label.shape[0]: if training_data.shape[0] != training_label.shape[0]:
print('label and data have not the same size') print('label and data have not the same size')
#Desired input shape: (N,C,D,H,W) N=Batchsize, C=Channel, D=Depth, H=Height, W = Width #Desired input shape: (N,C,D,H,W) N=Batchsize, C=Channel, D=Depth, H=Height, W = Width
data = np.moveaxis(training_data,4,1) # GAN output has shape(N,W,H,D,C) but no need to change spatial dimensions, but Channel dimension must be changed data = np.moveaxis(training_data,4,1) # GAN output has shape(N,W,H,D,C) but no need to change spatial dimensions, but Channel dimension must be changed
#input[0-3]: Orientation, input[4]: Phase(one for martinsite) #input[0-3]: Orientation, input[4]: Phase(one for martinsite)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
angles = data[:,0:4,...] angles = data[:,0:4,...]
angles_min_max= np.zeros((2,angles.shape[1])) angles_min_max= np.zeros((2,angles.shape[1]))
for i in range(angles.shape[1]): for i in range(angles.shape[1]):
min = angles_min_max[0,i]
max = angles_min_max[1,i]
s_batch,_,width, height, depth = angles.shape s_batch,_,width, height, depth = angles.shape
column= angles[:,i,...] column= angles[:,i,...]
angles_min_max[0,i] = column.min() angles_min_max[0,i] = column.min()
angles_min_max[1,i] = column.max() angles_min_max[1,i] = column.max()
#column_normalized = column.view(angles.shape[0], -1) column -= min
column -= column.min() column /= (max - min)
column /= (column.max() - column.min())
column = column.reshape(s_batch,width, height, depth) column = column.reshape(s_batch,width, height, depth)
#column = column[:,np.newaxis,...]
angles[:,i,...] = column angles[:,i,...] = column
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#training_label = training_label[:,np.newaxis,...] #training_label = training_label[:,np.newaxis,...]
phase= data[:,4,:,:,:].reshape(data.shape[0], 1,64,64,64) phase= data[:,4,:,:,:].reshape(data.shape[0], 1,64,64,64)
new_phase = np.ones(phase.shape) - phase #input[4]: martinsite, input[5]:ferrit new_phase = np.ones(phase.shape) - phase #input[4]: martinsite, input[5]:ferrit
#new_training_data = np.append(data,new_channel,axis=1) #new_training_data = np.append(data,new_channel,axis=1)
input = np.append(angles,phase,axis=1) input = np.append(angles,phase,axis=1)
input = np.append(input,new_phase,axis=1) input = np.append(input,new_phase,axis=1)
label = torch.from_numpy(training_label) label = torch.from_numpy(training_label)
input = torch.from_numpy(input) input = torch.from_numpy(input)
if label.dtype != torch.float64: if label.dtype != torch.float64:
label = label.double() label = label.double()
if input.dtype != torch.float64: if input.dtype != torch.float64:
input = input.double() input = input.double()
#angles = torch.from_numpy(angles) #angles = torch.from_numpy(angles)
print(f'size of input is {input.size()}') print(f'size of input is {input.size()}')
print(f'size of label is {label.size()}') print(f'size of label is {label.size()}')
``` ```
%% Output %% Output
size of input is torch.Size([458, 6, 64, 64, 64]) size of input is torch.Size([10, 6, 64, 64, 64])
size of label is torch.Size([458, 64, 64, 64]) size of label is torch.Size([10, 64, 64, 64])
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
min_label = training_label.min() min_label = training_label.min(1, keepdim=True)
max_label = training_label.max() max_label = training_label.max(1, keepdim=True)
s_batch, width, height, depth = label.size() s_batch, width, height, depth = label.size()
label_normalized = label.view(label.size(0), -1) label_normalized = label.view(label.size(0), -1)
label_normalized -= label_normalized.min(1, keepdim=True)[0] label_normalized -= min_label
label_normalized /= label_normalized.max(1, keepdim=True)[0] label_normalized /= max_label
label_normalized = label_normalized.view(s_batch, width, height, depth) label_normalized = label_normalized.view(s_batch, width, height, depth)
label_normalized = label_normalized[:,np.newaxis,...] label_normalized = label_normalized[:,np.newaxis,...]
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
#dataset = TensorDataset(input,label_normalized) # create the pytorch dataset dataset = TensorDataset(input,label_normalized) # create the pytorch dataset
#np.save('E:/Data/damask3/UNet/Input/Norm_min_max_64.npy',[min_label, max_label,angles_min_max]) #np.save('E:/Data/damask3/UNet/Input/Norm_min_max_64.npy',[min_label, max_label,angles_min_max])
torch.save(dataset,'E:/Data/damask3/UNet/Input/Training_Dataset_normalized_64.pt') torch.save(dataset,'E:/Data/damask3/UNet/Input/Training_Dataset_normalized_64_sample.pt')
``` ```
......
...@@ -50,10 +50,10 @@ class head_layer(nn.Module): ...@@ -50,10 +50,10 @@ class head_layer(nn.Module):
#return self.sig(self.pointwise(self.depthwise(x))) #convolution #return self.sig(self.pointwise(self.depthwise(x))) #convolution
class Encoder(nn.Module): class Encoder(nn.Module):
def __init__(self,kernel_size, chs, padding=(0,"same","same",)): def __init__(self,kernel_size, chs, padding=(0,"same","same")):
super().__init__() super().__init__()
self.channels = chs self.channels = chs
self.enc_blocks = nn.ModuleList([depthwise_separable_conv(chs[i], chs[i+1],kernel_size=kernel_size, padding=padding[i]) for i in range(len(chs)-1)]) self.enc_blocks = nn.ModuleList([depthwise_separable_conv(chs[i][0], chs[i][1],kernel_size=kernel_size, padding=padding[i]) for i in range(len(chs))])
self.pool = nn.MaxPool3d(kernel_size=2, stride=2) self.pool = nn.MaxPool3d(kernel_size=2, stride=2)
#self.batch_norm = nn.ModuleList([nn.BatchNorm3d( chs[i][2]) for i in range(len(chs))]) #self.batch_norm = nn.ModuleList([nn.BatchNorm3d( chs[i][2]) for i in range(len(chs))])
self.periodic_upsample = nn.ReflectionPad3d(int((kernel_size-1)/2)) self.periodic_upsample = nn.ReflectionPad3d(int((kernel_size-1)/2))
...@@ -74,13 +74,13 @@ class Encoder(nn.Module): ...@@ -74,13 +74,13 @@ class Encoder(nn.Module):
return ftrs return ftrs
class Decoder(nn.Module): class Decoder(nn.Module):
def __init__(self,kernel_size, chs_upsampling, chs_conv, padding=("same","same","same",)): def __init__(self,kernel_size, chs_upsampling, chs_conv, padding=("same","same","same")):
super().__init__() super().__init__()
assert len(chs_conv) == len(chs_upsampling) assert len(chs_conv) == len(chs_upsampling)
self.chs = chs_upsampling self.chs = chs_upsampling
self.upconvs = nn.ModuleList([nn.ConvTranspose3d(chs_upsampling[i], chs_upsampling[i], 2, 2) for i in range(len(chs_upsampling))]) self.upconvs = nn.ModuleList([nn.ConvTranspose3d(chs_upsampling[i], chs_upsampling[i], 2, 2) for i in range(len(chs_upsampling))])
self.dec_blocks = nn.ModuleList([depthwise_separable_conv(chs_conv[i][0], chs_conv[i][1],kernel_size=kernel_size, padding=padding[i]) for i in range(len(chs_conv))]) self.dec_blocks = nn.ModuleList([depthwise_separable_conv(chs_conv[i][0], chs_conv[i][1],kernel_size=kernel_size, padding=padding[i]) for i in range(len(chs_conv))])
self.head = head_layer(chs_conv[-1][2]) self.head = head_layer(chs_conv[-1][1])
def forward(self, x, encoder_features): def forward(self, x, encoder_features):
for i in range(len(self.chs)): for i in range(len(self.chs)):
x = self.upconvs[i](x) x = self.upconvs[i](x)
...@@ -170,8 +170,8 @@ def fit(epochs, lr, model, train_loader, val_loader, path, opt_func=torch.optim. ...@@ -170,8 +170,8 @@ def fit(epochs, lr, model, train_loader, val_loader, path, opt_func=torch.optim.
result['train_loss'] = torch.stack(train_losses).mean().item() result['train_loss'] = torch.stack(train_losses).mean().item()
model.epoch_end(epoch, result) model.epoch_end(epoch, result)
history.append(result) history.append(result)
torch.save(model.state_dict(),f'{path}/Unet_dict_V9.pth') torch.save(model.state_dict(),f'{path}/Unet_dict_V10.pth')
torch.save(history,f'{path}/history_V9.pt') torch.save(history,f'{path}/history_V10.pt')
return history return history
def get_default_device(): def get_default_device():
...@@ -220,10 +220,10 @@ def Create_Dataloader(path, batch_size = 100, percent_val = 0.2): ...@@ -220,10 +220,10 @@ def Create_Dataloader(path, batch_size = 100, percent_val = 0.2):
if __name__ == '__main__': if __name__ == '__main__':
#os.chdir('F:/RWTH/HiWi_IEHK/DAMASK3/UNet/Trainingsdata') #os.chdir('F:/RWTH/HiWi_IEHK/DAMASK3/UNet/Trainingsdata')
use_seeds = False use_seeds = True
seed = 373686838 seed = 2193910023
num_epochs = 1300 num_epochs = 1300
b_size = 8 b_size = 32
opt_func = torch.optim.Adam opt_func = torch.optim.Adam
lr = 0.00001 lr = 0.00001
kernel = 5 kernel = 5
......
...@@ -137,7 +137,7 @@ def accuracy(outputs, labels, threshold = 0.05): ...@@ -137,7 +137,7 @@ def accuracy(outputs, labels, threshold = 0.05):
return percentage return percentage
class UNet(UNetBase): class UNet(UNetBase):
def __init__(self,kernel_size = 5, enc_chs=((2,16,32), (32,32,64), (64,64,128)), dec_chs_up=(128, 128, 64), dec_chs_conv=((192,128, 128),(160,64,64),(66,32,32))): def __init__(self,kernel_size = 7, enc_chs=((2,16,32), (32,32,64), (64,64,128)), dec_chs_up=(128, 128, 64), dec_chs_conv=((192,128, 128),(160,64,64),(66,32,32))):
super().__init__() super().__init__()
self.encoder = Encoder(kernel_size = kernel_size, chs = enc_chs) self.encoder = Encoder(kernel_size = kernel_size, chs = enc_chs)
self.decoder = Decoder(kernel_size = kernel_size, chs_upsampling = dec_chs_up, chs_conv = dec_chs_conv) self.decoder = Decoder(kernel_size = kernel_size, chs_upsampling = dec_chs_up, chs_conv = dec_chs_conv)
...@@ -227,10 +227,10 @@ if __name__ == '__main__': ...@@ -227,10 +227,10 @@ if __name__ == '__main__':
use_seeds = False use_seeds = False
seed = 373686838 seed = 373686838
num_epochs = 1300 num_epochs = 1300
b_size = 8 b_size = 32
opt_func = torch.optim.Adam opt_func = torch.optim.Adam
lr = 0.00001 lr = 0.00001
kernel = 9 kernel = 5
print(f'number auf epochs: {num_epochs}') print(f'number auf epochs: {num_epochs}')
print(f'batchsize: {b_size}') print(f'batchsize: {b_size}')
print(f'learning rate: {lr}') print(f'learning rate: {lr}')
......
import torch
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import UNet_V4 as UNet
import pyvista as pv
from matplotlib.colors import ListedColormap
import copy
def predict_stress(image_id, normalization, model, dataset, threshold = 0.05):
input, output = dataset[image_id]
input = copy.deepcopy(input)
output = copy.deepcopy(output)
input = torch.unsqueeze(input,0)
output = torch.unsqueeze(output,0)
xb = UNet.to_device(input, device)
model.eval()
prediction = model(xb)
input = input.detach().numpy()
prediction = prediction.detach().numpy()
output = output.detach().numpy()
prediction = rescale(prediction, normalization)
output = rescale(output, normalization)
error = (abs(output - prediction)/output)
print(f'Maximum error is : {error.max()*100.:.4} %')
print(f'average error is : {error.mean()*100.:.4} %')
right_predic = (error < threshold).sum()
print(f'{(right_predic/error.size)*100.:.4}% of voxels have a diviation less than {threshold*100.}%')
grains = grain_matrix(input)
plot_difference(error,grains,output, threshold)
def rescale(output, normalization):
output_rescale = output.reshape(32,32,32)
min_label, max_label,_ = normalization
output_rescale *= max_label
output_rescale += min_label
return output_rescale
def get_colormap(mesh, threshold):
black = np.array([11/256, 11/256, 11/256, 1])
yellow = np.array([255/256, 237/256, 0/256, 1])
orange = np.array([245/256, 167/256, 0/256, 1])
red = np.array([203/256, 6/256, 29/256, 1])
bordeaux = np.array([160/256, 15/256, 53/256, 1])
mapping = np.linspace(mesh['error'].min(), mesh['error'].max(),256)
newcolors = np.empty((256,4))
newcolors[mapping >=0.75] = bordeaux
newcolors[mapping <0.75] = red
newcolors[mapping <0.5] = orange
newcolors[mapping <0.25] = yellow
newcolors[mapping <threshold] = black
return ListedColormap(newcolors)
def plot_losses(history):
losses = [x['val_loss'] for x in history[50:]]
plt.plot(losses, '-x')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.title('Loss vs. No. of epochs')
def grain_matrix(input):
matrix = input[0,0,:,:,:]
unique_angles = np.unique(matrix)
for index, angle in enumerate(unique_angles):
matrix[matrix == angle] = index
return matrix
def plot_difference(error, grains,stress, threshold):
#opacity = np.where(error < 0.1, 1, 0.)
grid_1 = pv.UniformGrid()
grid_1.dimensions = np.array(error.shape) +1
grid_1.spacing = (1,1,1)
grid_1.cell_data["error"] = error.flatten(order = "F")
grid_2 = pv.UniformGrid()
grid_2.dimensions = np.array(grains.shape) +1
grid_2.spacing = (1,1,1)
grid_2.cell_data["grains"] = grains.flatten(order = "F")
grid_3 = pv.UniformGrid()
grid_3.dimensions = np.array(stress.shape) +1
grid_3.spacing = (1,1,1)
grid_3.cell_data["stress"] = stress.flatten(order = "F")
my_colormap = get_colormap(grid_1, threshold)
#grid.cell_data["opacity"] = opacity.flatten(order = "F")
plotter = pv.Plotter(shape=(3,1))
#plotter.add_mesh(grid.copy(), scalars='error', opacity='opacity', use_transparency=True, show_edges=True)
plotter.subplot(0,0)
plotter.add_mesh_clip_plane(grid_2,scalars = 'grains', show_edges=True)
plotter.subplot(2,0)
plotter.add_mesh_clip_plane(grid_1,scalars = 'error', show_edges=True, cmap = my_colormap)
plotter.subplot(1,0)
plotter.add_mesh_clip_plane(grid_3,scalars = 'stress', show_edges=True)
plotter.link_views()
plotter.show()
if __name__ == '__main__':
dataset = torch.load('E:/Data/damask3/UNet/Input/Training_Dataset_normalized_V2.pt')
#history = torch.load('F:/RWTH/HiWi_IEHK/DAMASK3/UNet/output/V4/history_4.pt')
#history_2 = torch.load('E:/Data/damask3/UNet/output/history_test.pt')
normalization = np.load('E:/Data/damask3/UNet/Input/Norm_min_max_V2.npy', allow_pickle=True)
model = UNet.UNet()
model.load_state_dict(torch.load('E:/Data/damask3/UNet/output/V4/Unet_dict_V4.pth',map_location=torch.device('cpu')))
device = UNet.get_default_device()
model = UNet.to_device(model.double(), device)
predict_stress(34,normalization = normalization, model=model,dataset=dataset)
\ No newline at end of file
%% Cell type:code id: tags:
``` python
import torch
import numpy as np
import matplotlib.pyplot as plt
import UNet_V9 as UNet
import pyvista as pv
from matplotlib.colors import ListedColormap
import copy
from pyvista import examples
from torch.utils.data import TensorDataset
```
%% Cell type:code id: tags:
``` python
def predict_stress(image_id, normalization, model, dataset,grain_data, threshold = 0.1):
input, output = dataset[image_id]
grain,_ = grain_data[image_id]
grain = copy.deepcopy(grain)
grain = torch.unsqueeze(grain,0)
grain = grain.detach().numpy()
input = copy.deepcopy(input)
output = copy.deepcopy(output)
input = torch.unsqueeze(input,0)
output = torch.unsqueeze(output,0)
xb = UNet.to_device(input, device)
model.eval()
prediction = model(xb)
input = input.detach().numpy()
prediction = prediction.detach().numpy()
output = output.detach().numpy()
prediction = rescale(prediction, normalization)
output = rescale(output, normalization)
error = (abs(output - prediction)/output)
print(f'Maximum error is : {error.max()*100.:.4} %')
print(f'average error is : {error.mean()*100.:.4} %')
right_predic = (error < threshold).sum()
print(f'{(right_predic/error.size)*100.:.4}% of voxels have a diviation less than {threshold*100.}%')
grains = grain_matrix_colormap(grain)
plot_difference(error,grains,output, threshold)
def rescale(output, normalization):
output_rescale = output.reshape(output.shape[2],output.shape[3],output.shape[4])
if normalization is not None:
min_label, max_label = normalization
output_rescale *= max_label
output_rescale += min_label
return output_rescale
def get_colormap(mesh, threshold):
black = np.array([11/256, 11/256, 11/256, 1])
yellow = np.array([255/256, 237/256, 0/256, 1])
orange = np.array([245/256, 167/256, 0/256, 1])
red = np.array([203/256, 6/256, 29/256, 1])
bordeaux = np.array([160/256, 15/256, 53/256, 1])
mapping = np.linspace(mesh['error'].min(), mesh['error'].max(),256)
newcolors = np.empty((256,4))
newcolors[mapping >=0.5] = bordeaux
newcolors[mapping <0.5] = red
newcolors[mapping <0.3] = orange
newcolors[mapping <0.1] = yellow
newcolors[mapping <0.05] = black
return ListedColormap(newcolors)
def plot_losses(history):
train_losses = [x['train_loss'] for x in history[50:]]
val_acc = [x['val_acc'] for x in history[50:]]
val_loss = [x['val_loss'] for x in history[50:]]
plt.plot(train_losses, '-x',)
plt.plot(val_acc, '-x',)
plt.plot(val_loss, '-x',)
plt.xlabel('epoch')
plt.ylabel('loss')
plt.title('Loss vs. No. of epochs')
def grain_matrix_colormap(input):
matrix_grains = input[0,0,:,:,:]
matrix_ferrit = input[0,5,:,:,:] #matrix with elements = 1 if the phase is ferrit else 0
#unique_angles = np.unique(matrix_grains)
matrix_ferrit_grains = np.multiply(matrix_grains, matrix_ferrit)# matrix where only the ferrit grains are nonzero
index_ferrit_angles = np.unique(matrix_ferrit_grains[matrix_ferrit_grains != 0])
index_martensite_angles = np.setdiff1d(np.unique(matrix_grains),index_ferrit_angles)
for index, angle in enumerate(index_ferrit_angles):
matrix_grains[matrix_grains == angle] = (index) # matrix with id for each grain add 1 to perfome the elementwise multiplication to get the index of phase grains
for index, angle in enumerate(index_martensite_angles):
matrix_grains[matrix_grains == angle] = (index + len(index_ferrit_angles) +100) # matrix with id for each grain add 1 to perfome the elementwise multiplication to get the index of phase grains
return matrix_grains
```
%% Cell type:code id: tags:
``` python
def plot_difference(error, grains, stress, threshold):
grid_1 = pv.UniformGrid()
grid_1.dimensions = np.array(error.shape) +1
grid_1.spacing = (1,1,1)
grid_1.cell_data["error"] = error.flatten(order = "F")
grid_2 = pv.UniformGrid()
grid_2.dimensions = np.array(grains.shape) +1
grid_2.spacing = (1,1,1)
grid_2.cell_data["grain"] = grains.flatten(order = "F")
grid_3 = pv.UniformGrid()
grid_3.dimensions = np.array(stress.shape) +1
grid_3.spacing = (1,1,1)
grid_3.cell_data["stress"] = stress.flatten(order = "F")
colormap_error = get_colormap(grid_1, threshold)
p = pv.Plotter(notebook=False,shape=(3,1))
sargs = dict(height=0.75, vertical=True, position_x=0.1, position_y=0.05)
def my_plane_func(normal, origin):
slc_1 = grid_1.slice(normal=normal, origin=origin)
slc_2 = grid_2.slice(normal=normal, origin=origin)
slc_3 = grid_3.slice(normal=normal, origin=origin)
p.subplot(0,0)
p.add_mesh(slc_2, name="my_slice_2", cmap = 'RdBu', annotations = annotations, scalar_bar_args=sargs)
p.subplot(2,0)
p.add_mesh(slc_1, name="my_slice_1", cmap = colormap_error)
p.subplot(1,0)
p.add_mesh(slc_3, name="my_slice_3")
p.subplot(0,0)
annotations = {
0: 'Ferrite',
grains.max(): 'Martensite',
}
p.add_title('Grains',font_size=10)
p.add_mesh(grid_2 ,opacity=0, cmap = 'RdBu', annotations = annotations, scalar_bar_args=sargs)
p.add_plane_widget(my_plane_func)
p.subplot(2,0)
p.add_title('Error',font_size=10)
p.add_mesh(grid_1,scalars = "error" ,opacity=0,cmap = colormap_error)
p.add_plane_widget(my_plane_func)
p.subplot(1,0)
p.add_title('Stress',font_size=10)
p.add_mesh(grid_3,scalars = "stress" ,opacity=0)
p.add_plane_widget(my_plane_func)
p.link_views()
p.show()
```
%% Cell type:code id: tags:
``` python
Training_data = torch.load('E:/Data/damask3/UNet/Input/TD_norm_32_phase_only.pt')
grain_data = torch.load('E:/Data/damask3/UNet/Input/TD_norm_32.pt')
#history = torch.load('E:/Data/damask3/UNet/output/V6_64/history_V6_2_64.pt')
#history_2 = torch.load('E:/Data/damask3/UNet/output/history_test.pt')
normalization = np.load('E:/Data/damask3/UNet/Input/Norm_min_max_32_V2.npy', allow_pickle=True)
model = UNet.UNet()
model.load_state_dict(torch.load('E:/Data/damask3/UNet/output/V9/Unet_dict_V9.pth',map_location=torch.device('cpu')))
device = UNet.get_default_device()
model = UNet.to_device(model.double(), device)
```
%% Output
no GPU found
%% Cell type:code id: tags:
``` python
sample_index = np.random.randint(low=0, high=len(Training_data))
print(f'sample number: {sample_index}')
predict_stress(np.random.randint(low=0, high=len(Training_data)), normalization = normalization, model = model, dataset = Training_data,grain_data =grain_data)
```
%% Output
sample number: 876
Maximum error is : 313.1 %
average error is : 11.93 %
53.96% of voxels have a diviation less than 10.0%
%% Cell type:code id: tags:
``` python
train_losses = [x['train_loss'] for x in history[50:]]
plt.plot(train_losses)
plt.show()
```
%% Output
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_4608/2766589794.py in <module>
----> 1 train_losses = [x['train_loss'] for x in history[50:]]
2 plt.plot(train_losses)
3 plt.show()
NameError: name 'history' is not defined
%% Cell type:code id: tags:
``` python
plot_difference_2(error, grains,output, threshold)
```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
import torch import torch
import numpy as np import numpy as np
import matplotlib
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
import UNet_V4 as UNet import UNet_V9 as UNet
import pyvista as pv import pyvista as pv
from matplotlib.colors import ListedColormap from matplotlib.colors import ListedColormap
import copy import copy
from pyvista import examples from pyvista import examples
from torch.utils.data import TensorDataset
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
for a, b in dataset:
if torch.max(a) > 1.0 :
print("dataset kaputt")
#if torch.max(b) > 1.0 :
#print("dataset kaputt")
```
%% Output
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_16728/3899246243.py in <module>
----> 1 for a, b in dataset:
2 if torch.max(a) > 1.0 :
3 print("dataset kaputt")
4 #if torch.max(b) > 1.0 :
5 #print("dataset kaputt")
NameError: name 'dataset' is not defined
%% Cell type:code id: tags:
``` python
def predict_stress(image_id, normalization, model, dataset, threshold = 0.05): def predict_stress(image_id, normalization, model, dataset, threshold = 0.05):
input, output = dataset[image_id] input, output = dataset[image_id]
input = copy.deepcopy(input) input = copy.deepcopy(input)
output = copy.deepcopy(output) output = copy.deepcopy(output)
input = torch.unsqueeze(input,0) input = torch.unsqueeze(input,0)
output = torch.unsqueeze(output,0) output = torch.unsqueeze(output,0)
xb = UNet.to_device(input, device) xb = UNet.to_device(input, device)
model.eval() model.eval()
prediction = model(xb) prediction = model(xb)
input = input.detach().numpy() input = input.detach().numpy()
prediction = prediction.detach().numpy() prediction = prediction.detach().numpy()
output = output.detach().numpy() output = output.detach().numpy()
prediction = rescale(prediction, normalization) prediction = rescale(prediction, normalization)
output = rescale(output, normalization) output = rescale(output, normalization)
error = (abs(output - prediction)/output) error = (abs(output - prediction)/output)
print(f'Maximum error is : {error.max()*100.:.4} %') print(f'Maximum error is : {error.max()*100.:.4} %')
print(f'average error is : {error.mean()*100.:.4} %') print(f'average error is : {error.mean()*100.:.4} %')
right_predic = (error < threshold).sum() right_predic = (error < threshold).sum()
print(f'{(right_predic/error.size)*100.:.4}% of voxels have a diviation less than {threshold*100.}%') print(f'{(right_predic/error.size)*100.:.4}% of voxels have a diviation less than {threshold*100.}%')
grains = grain_matrix(input) grains,colormap = grain_matrix_colormap(input)
plot_difference(error,grains,output, threshold) plot_difference(error,grains,colormap,output, threshold)
def rescale(output, normalization): def rescale(output, normalization):
output_rescale = output.reshape(32,32,32) output_rescale = output.reshape(output.shape[2],output.shape[3],output.shape[4])
if normalization is not None: if normalization is not None:
min_label, max_label,_ = normalization min_label, max_label,_ = normalization
output_rescale *= max_label output_rescale *= max_label
output_rescale += min_label output_rescale += min_label
return output_rescale return output_rescale
def get_colormap_grains(grains,index_ferrit_grains,index_martensite_grains):
black = np.array([11/256, 11/256, 11/256, 1])
red = np.array([203/256, 6/256, 29/256, 1])
new_color = np.empty(np.unique(grains),4)
new_color[index_ferrit_grains] = black
new_color[index_ferrit_grains] = red
return ListedColormap(new_color)
def get_colormap(mesh, threshold): def get_colormap(mesh, threshold):
black = np.array([11/256, 11/256, 11/256, 1]) black = np.array([11/256, 11/256, 11/256, 1])
yellow = np.array([255/256, 237/256, 0/256, 1]) yellow = np.array([255/256, 237/256, 0/256, 1])
orange = np.array([245/256, 167/256, 0/256, 1]) orange = np.array([245/256, 167/256, 0/256, 1])
red = np.array([203/256, 6/256, 29/256, 1]) red = np.array([203/256, 6/256, 29/256, 1])
bordeaux = np.array([160/256, 15/256, 53/256, 1]) bordeaux = np.array([160/256, 15/256, 53/256, 1])
mapping = np.linspace(mesh['error'].min(), mesh['error'].max(),256) mapping = np.linspace(mesh['error'].min(), mesh['error'].max(),256)
newcolors = np.empty((256,4)) newcolors = np.empty((256,4))
newcolors[mapping >=0.75] = bordeaux newcolors[mapping >=0.75] = bordeaux
newcolors[mapping <0.75] = red newcolors[mapping <0.75] = red
newcolors[mapping <0.5] = orange newcolors[mapping <0.5] = orange
newcolors[mapping <0.25] = yellow newcolors[mapping <0.25] = yellow
newcolors[mapping <threshold] = black newcolors[mapping <threshold] = black
return ListedColormap(newcolors) return ListedColormap(newcolors)
def plot_losses(history): def plot_losses(history):
losses = [x['val_loss'] for x in history[50:]] train_losses = [x['train_loss'] for x in history[50:]]
plt.plot(losses, '-x') val_acc = [x['val_acc'] for x in history[50:]]
val_loss = [x['val_loss'] for x in history[50:]]
plt.plot(train_losses, '-x',)
plt.plot(val_acc, '-x',)
plt.plot(val_loss, '-x',)
plt.xlabel('epoch') plt.xlabel('epoch')
plt.ylabel('loss') plt.ylabel('loss')
plt.title('Loss vs. No. of epochs') plt.title('Loss vs. No. of epochs')
def grain_matrix(input): def grain_matrix_colormap(input):
matrix_grains = input[0,0,:,:,:] matrix_grains = input[0,0,:,:,:]
matrix_ferrit = input[0,5,:,:,:] #matrix with elements = 1 if the phase is ferrit else 0 matrix_ferrit = input[0,5,:,:,:] #matrix with elements = 1 if the phase is ferrit else 0
unique_angles = np.unique(matrix_grains) unique_angles = np.unique(matrix_grains)
for index, angle in enumerate(unique_angles): for index, angle in enumerate(unique_angles):
matrix_grains[matrix_grains == angle] = (index + 1) # matrix with id for each grain add 1 to perfome the elementwise multiplication to get the index of phase grains matrix_grains[matrix_grains == angle] = (index + 1) # matrix with id for each grain add 1 to perfome the elementwise multiplication to get the index of phase grains
matrix_ferrit_grains = np.multiply(matrix_grains, matrix_ferrit)# matrix where only the ferrit grains are nonzero matrix_ferrit_grains = np.multiply(matrix_grains, matrix_ferrit)# matrix where only the ferrit grains are nonzero
index_ferrit_grains = np.unique(matrix_ferrit_grains[matrix_ferrit_grains != 0]) index_ferrit_grains = np.unique(matrix_ferrit_grains[matrix_ferrit_grains != 0])
index_martensite_grains = np.setdiff1d(np.unique(matrix_grains),index_ferrit_grains) index_martensite_grains = np.setdiff1d(np.unique(matrix_grains),index_ferrit_grains)
return matrix_grains,index_ferrit_grains,index_martensite_grains black = np.array([11/256, 11/256, 11/256, 1])
``` red = np.array([203/256, 6/256, 29/256, 1])
new_color = np.empty((np.unique(matrix_grains),4))
%% Cell type:code id: tags: new_color[index_ferrit_grains] = black
new_color[index_martensite_grains] = red
``` python return matrix_grains,ListedColormap(new_color)
input, label = dataset[0]
input = copy.deepcopy(input)
input= input[np.newaxis,...]
_,index_ferrit_grains,index_martensite_grains = grain_matrix(input)
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
def plot_difference(error, grains,stress, threshold): def plot_difference(error, grains, colormap_grains, stress, threshold):
grid_1 = pv.UniformGrid() grid_1 = pv.UniformGrid()
grid_1.dimensions = np.array(error.shape) +1 grid_1.dimensions = np.array(error.shape) +1
grid_1.spacing = (1,1,1) grid_1.spacing = (1,1,1)
grid_1.cell_data["error"] = error.flatten(order = "F") grid_1.cell_data["error"] = error.flatten(order = "F")
grid_2 = pv.UniformGrid() grid_2 = pv.UniformGrid()
grid_2.dimensions = np.array(grains.shape) +1 grid_2.dimensions = np.array(grains.shape) +1
grid_2.spacing = (1,1,1) grid_2.spacing = (1,1,1)
grid_2.cell_data["grain"] = grains.flatten(order = "F") grid_2.cell_data["grain"] = grains.flatten(order = "F")
grid_3 = pv.UniformGrid() grid_3 = pv.UniformGrid()
grid_3.dimensions = np.array(stress.shape) +1 grid_3.dimensions = np.array(stress.shape) +1
grid_3.spacing = (1,1,1) grid_3.spacing = (1,1,1)
grid_3.cell_data["stress"] = stress.flatten(order = "F") grid_3.cell_data["stress"] = stress.flatten(order = "F")
my_colormap = get_colormap(grid_1, threshold) colormap_error = get_colormap(grid_1, threshold)
p = pv.Plotter(notebook=False,shape=(3,1)) p = pv.Plotter(notebook=False,shape=(3,1))
def my_plane_func(normal, origin): def my_plane_func(normal, origin):
slc_1 = grid_1.slice(normal=normal, origin=origin) slc_1 = grid_1.slice(normal=normal, origin=origin)
slc_2 = grid_2.slice(normal=normal, origin=origin) slc_2 = grid_2.slice(normal=normal, origin=origin)
slc_3 = grid_3.slice(normal=normal, origin=origin) slc_3 = grid_3.slice(normal=normal, origin=origin)
p.subplot(0,0) p.subplot(0,0)
p.add_mesh(slc_2, name="my_slice_2", cmap = 'tab20c') p.add_mesh(slc_2, name="my_slice_2", cmap = colormap_grains)
p.subplot(2,0) p.subplot(2,0)
p.add_mesh(slc_1, name="my_slice_1", cmap=my_colormap) p.add_mesh(slc_1, name="my_slice_1", cmap = colormap_error)
p.subplot(1,0) p.subplot(1,0)
p.add_mesh(slc_3, name="my_slice_3") p.add_mesh(slc_3, name="my_slice_3")
p.subplot(0,0) p.subplot(0,0)
p.add_title('Grains',font_size=10) p.add_title('Grains',font_size=10)
p.add_mesh(grid_2,scalars = "grain" ,opacity=0, cmap = 'tab20c') p.add_mesh(grid_2,scalars = "grain" ,opacity=0, cmap = colormap_grains)
p.add_plane_widget(my_plane_func) p.add_plane_widget(my_plane_func)
p.subplot(2,0) p.subplot(2,0)
p.add_title('Error',font_size=10) p.add_title('Error',font_size=10)
p.add_mesh(grid_1,scalars = "error" ,opacity=0,cmap=my_colormap) p.add_mesh(grid_1,scalars = "error" ,opacity=0,cmap = colormap_error)
p.add_plane_widget(my_plane_func) p.add_plane_widget(my_plane_func)
p.subplot(1,0) p.subplot(1,0)
p.add_title('Stress',font_size=10) p.add_title('Stress',font_size=10)
p.add_mesh(grid_3,scalars = "stress" ,opacity=0) p.add_mesh(grid_3,scalars = "stress" ,opacity=0)
p.add_plane_widget(my_plane_func) p.add_plane_widget(my_plane_func)
p.link_views() p.link_views()
p.show() p.show()
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
dataset = torch.load('E:/Data/damask3/UNet/Input/Training_Dataset_normalized.pt') dataset = torch.load('E:/Data/damask3/UNet/Input/Training_Dataset_normalized__32_V2.pt')
#history = torch.load('F:/RWTH/HiWi_IEHK/DAMASK3/UNet/output/V4/history_4.pt') #history = torch.load('E:/Data/damask3/UNet/output/V6_64/history_V6_2_64.pt')
#history_2 = torch.load('E:/Data/damask3/UNet/output/history_test.pt') #history_2 = torch.load('E:/Data/damask3/UNet/output/history_test.pt')
normalization = np.load('E:/Data/damask3/UNet/Input/Norm_min_max.npy', allow_pickle=True) normalization = np.load('E:/Data/damask3/UNet/Input/Norm_min_max_32_V2.npy', allow_pickle=True)
model = UNet.UNet() model = UNet.UNet()
model.load_state_dict(torch.load('E:/Data/damask3/UNet/output/V4/Unet_dict.pth',map_location=torch.device('cpu'))) model.load_state_dict(torch.load('E:/Data/damask3/UNet/output/V9/Unet_dict_V9.pth',map_location=torch.device('cpu')))
device = UNet.get_default_device() device = UNet.get_default_device()
model = UNet.to_device(model.double(), device) model = UNet.to_device(model.double(), device)
``` ```
%% Output %% Output
no GPU found no GPU found
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
predict_stress(7, normalization = normalization, model = model, dataset = dataset)
predict_stress(7,normalization = normalization, model=model,dataset=dataset)
``` ```
%% Output %% Output
Maximum error is : 335.0 % Maximum error is : 272.0 %
average error is : 22.09 % average error is : 14.51 %
17.76% of voxels have a diviation less than 5.0% 24.97% of voxels have a diviation less than 5.0%
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
IndexError Traceback (most recent call last) IndexError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_21620/3913701557.py in <module> ~\AppData\Local\Temp/ipykernel_24852/1619662343.py in <module>
----> 1 predict_stress(7,normalization = normalization, model=model,dataset=dataset) ----> 1 predict_stress(7, normalization = normalization, model = model, dataset = dataset)
~\AppData\Local\Temp/ipykernel_21620/2230561446.py in predict_stress(image_id, normalization, model, dataset, threshold) ~\AppData\Local\Temp/ipykernel_24852/1249646142.py in predict_stress(image_id, normalization, model, dataset, threshold)
18 right_predic = (error < threshold).sum() 18 right_predic = (error < threshold).sum()
19 print(f'{(right_predic/error.size)*100.:.4}% of voxels have a diviation less than {threshold*100.}%') 19 print(f'{(right_predic/error.size)*100.:.4}% of voxels have a diviation less than {threshold*100.}%')
---> 20 grains = grain_matrix(input) ---> 20 grains,colormap = grain_matrix_colormap(input)
21 plot_difference(error,grains,output, threshold) 21 plot_difference(error,grains,colormap,output, threshold)
22 22
~\AppData\Local\Temp/ipykernel_21620/2230561446.py in grain_matrix(input) ~\AppData\Local\Temp/ipykernel_24852/1249646142.py in grain_matrix_colormap(input)
55 def grain_matrix(input): 70 def grain_matrix_colormap(input):
56 matrix_grains = input[0,:,:,:] 71 matrix_grains = input[0,0,:,:,:]
---> 57 matrix_ferrit = input[5,:,:,:] #matrix with elements = 1 if the phase is ferrit else 0 ---> 72 matrix_ferrit = input[0,5,:,:,:] #matrix with elements = 1 if the phase is ferrit else 0
58 unique_angles = np.unique(matrix_grains) 73 unique_angles = np.unique(matrix_grains)
59 for index, angle in enumerate(unique_angles): 74 for index, angle in enumerate(unique_angles):
IndexError: index 5 is out of bounds for axis 0 with size 1 IndexError: index 5 is out of bounds for axis 1 with size 2
%% Cell type:code id: tags:
``` python
train_losses = [x['train_loss'] for x in history[50:]]
plt.plot(train_losses)
plt.show()
```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` python ``` python
plot_difference_2(error, grains,output, threshold) plot_difference_2(error, grains,output, threshold)
``` ```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment