| import torch
|
| from torch import nn as nn
|
| from models import normalization
|
| from utilities import common
|
| from models import regDGCNN_seg
|
|
|
| device = torch.device('cuda')
|
| class Model(nn.Module):
|
| def __init__(self, params, core_model_name="regDGCNN_seg"):
|
|
|
| super(Model, self).__init__()
|
| self._params = params
|
| self._output_life_normalizer = normalization.Normalizer(size=1, name='output_pos_normalizer')
|
| self.k = params['k']
|
| self._model_type = params['model'].__name__
|
| self._displacement_base = None
|
|
|
| self.core_model_name = core_model_name
|
|
|
| if core_model_name == 'regDGCNN_seg':
|
| self.core_model = regDGCNN_seg
|
| self.is_multigraph = False,
|
|
|
| self.learned_model = regDGCNN_seg.regDGCNN_seg(
|
| output_size=params['output_size'],
|
| input_dims=params['input_size'],
|
| k=self.k,
|
| emb_dims=1024,
|
| dropout=0.1
|
| )
|
| else:
|
| raise ValueError(f"Unsupported core model: {self.core_model_name}")
|
|
|
| def forward(self, inputs, is_training):
|
| if is_training:
|
| return self.learned_model(self.process_inputs(inputs, device))
|
| else:
|
| return self._update(self.learned_model(self.process_inputs(inputs, device)))
|
| """β
So, what does regDGCNN_seg.regDGCNN_seg return?
|
| From conventions in DGCNN-style networks:
|
|
|
| Input: [B, in_channels, N] β e.g., [1, 3, 1024] for batch size 1, 3D points
|
|
|
| Output: [B, out_channels, N] β e.g., [1, 1, 1024] if you're doing per-node regression"""
|
| """self.process_inputs(...)
|
|
|
| Prepares the input tensor (shape: [1, input_features, num_points])
|
|
|
| Example: converts [N, 3] β [1, 3, N] to match expected input of DGCNN.
|
|
|
| self.learned_model(...)
|
|
|
| Applies the core neural network (regDGCNN_seg) to this input.
|
|
|
| Returns the raw output, e.g., predicted fatigue life per node (normalized)."""
|
|
|
| def _update(self, per_node_network_output):
|
| """Integrate model outputs."""
|
|
|
| fatigue_pred = self._output_life_normalizer.inverse(per_node_network_output[:, :])
|
|
|
| return (fatigue_pred)
|
|
|
| def process_inputs(self, inputs, device):
|
|
|
| """
|
| Processes input tensors and prepares features for graph construction.
|
|
|
| Args:
|
| inputs (dict): A dictionary containing 'curr_pos' and 'node_type'.
|
| device (torch.device): The device to move tensors to.
|
|
|
| Returns:
|
| torch.Tensor: The processed feature tensor (1, num_features, num_points).
|
| torch.Tensor: Node type tensor for filtering.
|
| """
|
| device = next(self.parameters()).device
|
| mesh_pos = inputs['mesh_pos'].to(device)
|
| node_type = inputs['node_type'].to(device)
|
| radial_distance = inputs['radial_distance'].to(device)
|
| r_norm = inputs['r_norm'].to(device)
|
| z_norm = inputs['z_norm'].to(device)
|
| d_step = inputs['d_step'].to(device)
|
| d_step_norm = inputs['d_step_norm'].to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| x = torch.cat([
|
| mesh_pos,
|
| radial_distance,
|
| r_norm,
|
| z_norm,
|
| d_step,
|
| d_step_norm,
|
| node_type
|
| ], dim=1)
|
|
|
|
|
| x = x.T.unsqueeze(0)
|
|
|
|
|
| return x
|
|
|
|
|
| def get_output_life_normalizer(self):
|
| return (self._output_life_normalizer)
|
|
|
| def save_model(self, path):
|
| torch.save(self.learned_model, path + "_learned_model.pth")
|
| torch.save(self._output_life_normalizer, path + "_output_life_normalizer.pth")
|
|
|
| def load_model(self, path):
|
| self.learned_model = torch.load(path + "_learned_model.pth")
|
| self._output_life_normalizer = torch.load(path + "_output_life_normalizer.pth")
|
|
|
| def evaluate(self):
|
| self.eval()
|
| self.learned_model.eval()
|
|
|