Class DGCNN<T>
- Namespace
- AiDotNet.PointCloud.Models
- Assembly
- AiDotNet.dll
Implements Dynamic Graph CNN (DGCNN) for point cloud processing.
public class DGCNN<T> : NeuralNetworkBase<T>, INeuralNetworkModel<T>, IInterpretableModel<T>, IInputGradientComputable<T>, IDisposable, IPointCloudClassification<T>, IPointCloudSegmentation<T>, IPointCloudModel<T>, INeuralNetwork<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>
Type Parameters
TThe numeric type used for calculations (e.g., float, double).
- Inheritance
-
DGCNN<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
For Beginners: DGCNN treats point clouds as graphs and uses edge convolutions to learn features.
Key innovations of DGCNN: - Dynamic graph construction: Rebuilds neighborhood graph at each layer based on learned features - Edge convolution: Learns features from edges connecting nearby points - Captures local geometric structure more effectively than PointNet - Adapts to the feature space, not just spatial coordinates
How DGCNN differs from PointNet: - PointNet: Processes each point independently, then aggregates - DGCNN: Explicitly models relationships between neighboring points - Dynamic graphs: Neighborhoods change as features evolve through layers
Edge Convolution explained: 1. For each point, find K nearest neighbors (in feature space or spatial) 2. Compute edge features: combine point feature with neighbor features 3. Apply MLP to edge features 4. Aggregate (max pool) edge features for each point 5. Result: New features that incorporate local structure
Why dynamic graphs are powerful: - Early layers: Use spatial proximity (XYZ coordinates) - Later layers: Use semantic similarity (learned features) - Example: Points on same chair leg become neighbors even if spatially distant
Architecture: 1. Multiple EdgeConv layers with increasing feature dimensions 2. Each layer rebuilds k-NN graph based on current features 3. Concatenate features from all EdgeConv layers 4. Max pooling for global features 5. Fully connected layers for classification/segmentation
Applications: - Classification: Achieves state-of-the-art on ModelNet40 - Part segmentation: Excellent for identifying object parts - Semantic segmentation: Captures fine-grained geometric details - Better than PointNet at capturing local structure
Example - chair classification: - Layer 1: Find spatial neighbors (nearby points) - Layer 2: Find points with similar low-level features (edges, corners) - Layer 3: Find points with similar mid-level features (vertical bars, flat surfaces) - Layer 4: Find points with similar high-level features (legs, back, seat) - Final: Combine all levels to recognize "chair"
Reference: "Dynamic Graph CNN for Learning on Point Clouds" by Wang et al., ACM Transactions on Graphics 2019
Constructors
DGCNN()
Initializes a new instance of the DGCNN class with default options.
public DGCNN()
DGCNN(DGCNNOptions, ILossFunction<T>?)
Initializes a new instance of the DGCNN class with configurable options.
public DGCNN(DGCNNOptions options, ILossFunction<T>? lossFunction = null)
Parameters
optionsDGCNNOptionsConfiguration options for the DGCNN model.
lossFunctionILossFunction<T>Optional loss function for training.
DGCNN(int, int, int[]?, bool, double, ILossFunction<T>?)
Initializes a new instance of the DGCNN class.
public DGCNN(int numClasses, int knnK = 20, int[]? edgeConvChannels = null, bool useDropout = true, double dropoutRate = 0.5, ILossFunction<T>? lossFunction = null)
Parameters
numClassesintNumber of output classes for classification.
knnKintNumber of nearest neighbors for graph construction.
edgeConvChannelsint[]Output channel dimensions for each EdgeConv layer.
useDropoutboolWhether to use dropout regularization.
dropoutRatedoubleDropout rate (if dropout is enabled).
lossFunctionILossFunction<T>Optional loss function for training.
Remarks
For Beginners: Creates a DGCNN model for point cloud processing with dynamic graphs.
Parameters explained:
- numClasses: How many categories to classify (e.g., 40 for ModelNet40)
- knnK: How many neighbors to consider for each point
- Typical values: 20-40
- Larger K: More context, but more computation
- Smaller K: Faster, but might miss important relationships
- edgeConvChannels: Feature dimensions at each EdgeConv layer
Example: [64, 64, 128, 256]
- Layer 1: 64-dimensional features
- Layer 2: 64-dimensional features
- Layer 3: 128-dimensional features
- Layer 4: 256-dimensional features
- useDropout: Prevents overfitting by randomly dropping neurons during training
- dropoutRate: Fraction of neurons to drop (e.g., 0.5 means drop 50%)
Example configuration for ModelNet40:
- knnK: 20
- edgeConvChannels: [64, 64, 128, 256]
- useDropout: true
- dropoutRate: 0.5
The network will:
- Build k-NN graph (find 20 nearest neighbors for each point)
- Apply EdgeConv to learn from local neighborhoods
- Rebuild graph based on new features
- Repeat for all EdgeConv layers
- Aggregate global features and classify
Properties
SupportsTraining
Indicates whether this network supports training (learning from data).
public override bool SupportsTraining { get; }
Property Value
Remarks
For Beginners: Not all neural networks can learn. Some are designed only for making predictions with pre-set parameters. This property tells you if the network can learn from data.
Methods
Backpropagate(Tensor<T>)
Performs backpropagation to compute gradients for network parameters.
public override Tensor<T> Backpropagate(Tensor<T> outputGradient)
Parameters
outputGradientTensor<T>
Returns
- Tensor<T>
The gradients of the loss with respect to the network inputs.
Remarks
For Beginners: Backpropagation is how neural networks learn. After making a prediction, the network calculates how wrong it was (the error). Then it works backward through the layers to figure out how each parameter contributed to that error. This method handles that backward flow of information.
The "gradients" are numbers that tell us how to adjust each parameter to reduce the error.
API Change Note: The signature changed from Vector<T> to Tensor<T> to support multi-dimensional gradients. This is a breaking change. If you need backward compatibility, consider adding an overload that accepts Vector<T> and converts it internally to Tensor<T>.
Exceptions
- InvalidOperationException
Thrown when the network is not in training mode or doesn't support training.
ClassifyPointCloud(Tensor<T>)
Classifies a point cloud into one of the predefined categories.
public Vector<T> ClassifyPointCloud(Tensor<T> pointCloud)
Parameters
pointCloudTensor<T>Input point cloud tensor of shape [N, 3+F].
Returns
- Vector<T>
A vector of class probabilities of length C, where C is the number of classes.
Remarks
For Beginners: This method determines what object the point cloud represents.
The classification process:
- Takes the entire point cloud as input
- Processes all points together to understand the overall shape
- Returns probabilities for each possible category
Example with furniture classification:
- Input: Point cloud of a furniture piece
- Output: [0.85 chair, 0.10 stool, 0.03 table, 0.02 bench]
- The model predicts it's most likely a chair (85% confidence)
The returned probabilities sum to 1.0, allowing you to:
- Pick the most likely category (highest probability)
- Understand the model's confidence in its prediction
- Consider alternative possibilities if the top prediction has low confidence
CreateNewInstance()
Creates a new instance of the same type as this neural network.
protected override IFullModel<T, Tensor<T>, Tensor<T>> CreateNewInstance()
Returns
- IFullModel<T, Tensor<T>, Tensor<T>>
A new instance of the same neural network type.
Remarks
For Beginners: This creates a blank version of the same type of neural network.
It's used internally by methods like DeepCopy and Clone to create the right type of network before copying the data into it.
DeserializeNetworkSpecificData(BinaryReader)
Deserializes network-specific data that was not covered by the general deserialization process.
protected override void DeserializeNetworkSpecificData(BinaryReader reader)
Parameters
readerBinaryReaderThe BinaryReader to read the data from.
Remarks
This method is called at the end of the general deserialization process to allow derived classes to read any additional data specific to their implementation.
For Beginners: Continuing the suitcase analogy, this is like unpacking that special compartment. After the main deserialization method has unpacked the common items (layers, parameters), this method allows each specific type of neural network to unpack its own unique items that were stored during serialization.
ExtractGlobalFeatures(Tensor<T>)
Extracts global features from a point cloud.
public Vector<T> ExtractGlobalFeatures(Tensor<T> pointCloud)
Parameters
pointCloudTensor<T>Input point cloud tensor of shape [N, 3+F] where N is number of points, and 3+F represents XYZ coordinates plus F additional features.
Returns
- Vector<T>
A feature vector representing the global characteristics of the point cloud.
Remarks
For Beginners: This method extracts a compact representation of the entire point cloud.
It's like creating a summary or "fingerprint" of the 3D object:
- Input: Many individual 3D points (could be thousands or millions)
- Output: A single feature vector that captures the essential characteristics
- This summary can be used for classification, detection, or comparison
For example, extracting global features from point clouds of chairs would produce similar feature vectors for all chairs, despite differences in specific details.
ExtractPointFeatures(Tensor<T>)
Extracts per-point features from a point cloud.
public Tensor<T> ExtractPointFeatures(Tensor<T> pointCloud)
Parameters
pointCloudTensor<T>Input point cloud tensor of shape [N, 3+F].
Returns
- Tensor<T>
A tensor of shape [N, D] where D is the feature dimension for each point.
Remarks
For Beginners: This method extracts features for each individual point.
Unlike global features which summarize the entire cloud, per-point features describe each point:
- Input: N points with XYZ coordinates
- Output: N feature vectors, one for each point
- Each feature vector captures local and contextual information about that point
This is useful for tasks like:
- Point cloud segmentation (labeling each point)
- Finding specific features or parts in the 3D data
- Understanding the local geometry around each point
ForwardWithMemory(Tensor<T>)
Performs a forward pass through the network while storing intermediate values for backpropagation.
public override Tensor<T> ForwardWithMemory(Tensor<T> input)
Parameters
inputTensor<T>The input data to the network.
Returns
- Tensor<T>
The output of the network.
Remarks
For Beginners: This method passes data through the network from input to output, but also remembers all the intermediate values. This is necessary for the learning process, as the network needs to know these values when figuring out how to improve.
API Change Note: The signature changed from Vector<T> to Tensor<T> to support multi-dimensional inputs. This is a breaking change. For backward compatibility, consider adding an overload that accepts Vector<T> and converts it internally to Tensor<T>.
Exceptions
- InvalidOperationException
Thrown when the network doesn't support training.
GetModelMetadata()
Gets the metadata for this neural network model.
public override ModelMetadata<T> GetModelMetadata()
Returns
- ModelMetadata<T>
A ModelMetaData object containing information about the model.
InitializeLayers()
Initializes the layers of the neural network based on the architecture.
protected override void InitializeLayers()
Remarks
For Beginners: This method sets up all the layers in your neural network according to the architecture you've defined. It's like assembling the parts of your network before you can use it.
Predict(Tensor<T>)
Makes a prediction using the neural network.
public override Tensor<T> Predict(Tensor<T> input)
Parameters
inputTensor<T>The input data to process.
Returns
- Tensor<T>
The network's prediction.
Remarks
For Beginners: This is the main method you'll use to get results from your trained neural network. You provide some input data (like an image or text), and the network processes it through all its layers to produce an output (like a classification or prediction).
SegmentPointCloud(Tensor<T>)
Performs semantic segmentation on a point cloud.
public Tensor<T> SegmentPointCloud(Tensor<T> pointCloud)
Parameters
pointCloudTensor<T>Input point cloud tensor of shape [N, 3+F].
Returns
- Tensor<T>
A tensor of shape [N, C] containing class probabilities for each point, where C is the number of classes.
Remarks
For Beginners: This method assigns a category label to each point.
For each point, it predicts which category it belongs to:
- Input: Point cloud with N points
- Output: For each point, probabilities for each possible category
- The category with highest probability is the predicted label
Example for indoor scene segmentation:
- Categories might be: floor, wall, ceiling, furniture, etc.
- Each point gets probabilities: [0.01 floor, 0.95 wall, 0.02 ceiling, 0.02 furniture]
- The point would be labeled as "wall" (highest probability)
SerializeNetworkSpecificData(BinaryWriter)
Serializes network-specific data that is not covered by the general serialization process.
protected override void SerializeNetworkSpecificData(BinaryWriter writer)
Parameters
writerBinaryWriterThe BinaryWriter to write the data to.
Remarks
This method is called at the end of the general serialization process to allow derived classes to write any additional data specific to their implementation.
For Beginners: Think of this as packing a special compartment in your suitcase. While the main serialization method packs the common items (layers, parameters), this method allows each specific type of neural network to pack its own unique items that other networks might not have.
Train(Tensor<T>, Tensor<T>)
Trains the neural network on a single input-output pair.
public override void Train(Tensor<T> input, Tensor<T> expectedOutput)
Parameters
inputTensor<T>The input data.
expectedOutputTensor<T>The expected output for the given input.
Remarks
This method performs one training step on the neural network using the provided input and expected output. It updates the network's parameters to reduce the error between the network's prediction and the expected output.
For Beginners: This is how your neural network learns. You provide: - An input (what the network should process) - The expected output (what the correct answer should be)
The network then:
- Makes a prediction based on the input
- Compares its prediction to the expected output
- Calculates how wrong it was (the loss)
- Adjusts its internal values to do better next time
After training, you can get the loss value using the GetLastLoss() method to see how well the network is learning.
UpdateParameters(Vector<T>)
Updates the network's parameters with new values.
public override void UpdateParameters(Vector<T> parameters)
Parameters
parametersVector<T>The new parameter values to set.
Remarks
For Beginners: During training, a neural network's internal values (parameters) get adjusted to improve its performance. This method allows you to update all those values at once by providing a complete set of new parameters.
This is typically used by optimization algorithms that calculate better parameter values based on training data.