Material Clustering Benchmark
DispatcherKHMp.cs
Go to the documentation of this file.
1using UnityEngine;
2using System;
3
5{
7 {
8 [Serializable]
10 {
11 public float p;
12
13 public Parameters(float p)
14 {
15 this.p = p;
16 }
17
18 /// <summary>
19 /// Default value of <see cref="Parameters.p"/> is 2.5, which we experimentally confirmed to be optimal for our dataset (<see cref="BenchmarkGeneration.KHMp"/>). For values above p=3.0 we saw a sharp decline in quality for one of the video files.<para />
20 /// Integer powers are optimized by the compiler to use multiplication instead, which is faster. For non-integer powers <c>pow(a,b)</c> will be replaced with <c>exp2(b * log2(a))</c> because there is no hardware <c>pow()</c>.<para />
21 /// According to [1]: p=3.5 is the optimal value for 2-dimensional data in a general case; p=3 produces almost as good clustering quality as p=3.5; any value above p=2 should generally outperform K-means; for higher dimensions values of p snould be higher.<para />
22 /// In our application the samples are located on the edges of a triangle in two-dimensional space, which could explain lower value of p being optimal.<para />
23 /// [1] Zhang, B., 2000. Generalized k-harmonic means--boosting in unsupervised learning. HP LABORATORIES TECHNICAL REPORT HPL, 137.
24 /// </summary>
25 /// <returns></returns>
26 public static Parameters Default()
27 {
28 return new Parameters(p: 2.5f);
29 }
30 }
31
32 public override bool doesReadback => false;
33 public override DispatcherParameters abstractParameters => this.parameters;
34
35 private readonly Parameters parameters;
36
38 ComputeShader computeShader,
39 int numIterations,
42 Parameters parameters,
44 )
45 : base(
51 )
52 {
53 this.parameters = parameters; // hard-coded in shader
54 }
55
56 public override string name => "KHM";
57
58 /// <summary>
59 /// Each iteration first attributes pixels to clusters,
60 /// then updates cluster centers.
61 /// In order to use the resulting cluster centers,
62 /// one final cluster attribution is required!
63 /// </summary>
64 public override void RunClustering(ClusteringTextures clusteringTextures)
65 {
66 for (int i = 0; i < this.numIterations; i++)
67 {
68 this.KHMiteration(clusteringTextures);
69 }
70 }
71
72 /// <summary>
73 /// First attributes pixels to clusters.
74 /// Then updates cluster centers.
75 /// In order to use the resulting cluster centers,
76 /// one final cluster attribution is required!
77 /// </summary>
78 protected void KHMiteration(ClusteringTextures textures)
79 {
80 this.AttributeClustersKHM(textures, p: this.parameters.p);
81 this.UpdateClusterCenters(textures, rejectOld: false);
82 }
83
84 public override void SingleIteration(ClusteringTextures textures)
85 {
86 this.computeShader.SetBool(
87 "do_random_sample_empty_clusters",
89 );
90 this.computeShader.SetInt("num_clusters", this.clusteringRTsAndBuffers.numClusters);
91
92 this.KHMiteration(textures);
93 }
94 }
95}
void AttributeClustersKHM(ClusteringTextures clusteringTextures, float p)
Definition: ADispatcher.cs:118
ClusteringRTsAndBuffers clusteringRTsAndBuffers
Definition: ADispatcher.cs:11
void UpdateClusterCenters(ClusteringTextures clusteringTextures, bool rejectOld)
Definition: ADispatcher.cs:55
readonly ComputeShader computeShader
Definition: ADispatcher.cs:28
Call Allocate before using and Dispose after using.
static Parameters Default()
Default value of Parameters.p is 2.5, which we experimentally confirmed to be optimal for our dataset...
void KHMiteration(ClusteringTextures textures)
First attributes pixels to clusters. Then updates cluster centers. In order to use the resulting clus...
override DispatcherParameters abstractParameters
override void SingleIteration(ClusteringTextures textures)
DispatcherKHMp(ComputeShader computeShader, int numIterations, bool doRandomizeEmptyClusters, bool useFullResTexRef, Parameters parameters, ClusteringRTsAndBuffers clusteringRTsAndBuffers)
override void RunClustering(ClusteringTextures clusteringTextures)
Each iteration first attributes pixels to clusters, then updates cluster centers. In order to use the...
Call Dispose after using.