|
| 1 | +import numpy as np |
| 2 | +import ThrustRTC as trtc |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +from matplotlib import collections as mc |
| 5 | + |
| 6 | +def demo_k_means(ctx, d_x, d_y, k): |
| 7 | + |
| 8 | + n = d_x.size() |
| 9 | + |
| 10 | + # create a zipped vector for convenience |
| 11 | + d_points = trtc.DVZipped(ctx, [d_x, d_y], ['x','y']) |
| 12 | + |
| 13 | + # operations |
| 14 | + point_plus = trtc.Functor(ctx, { }, ['pos1', "pos2"], |
| 15 | +''' |
| 16 | + return decltype(pos1)({pos1.x + pos2.x, pos1.y + pos2.y}); |
| 17 | +''') |
| 18 | + |
| 19 | + point_div = trtc.Functor(ctx, { }, ['pos', "count"], |
| 20 | +''' |
| 21 | + return decltype(pos)({pos.x/(float)count, pos.y/(float)count}); |
| 22 | +''') |
| 23 | + |
| 24 | + # initialize centers |
| 25 | + center_ids = [0] * k |
| 26 | + d_min_dis = trtc.device_vector(ctx, "float", n) |
| 27 | + |
| 28 | + for i in range(1, k): |
| 29 | + d_count = trtc.DVInt32(i) |
| 30 | + d_center_ids = trtc.device_vector_from_list(ctx, center_ids[0:i], 'int32_t') |
| 31 | + calc_min_dis = trtc.Functor(ctx, {"points": d_points, "center_ids": d_center_ids, "count": d_count }, ['pos'], |
| 32 | +''' |
| 33 | + float minDis = FLT_MAX; |
| 34 | + for (int i=0; i<count; i++) |
| 35 | + { |
| 36 | + int j = center_ids[i]; |
| 37 | + float dis = (pos.x - points[j].x)*(pos.x - points[j].x); |
| 38 | + dis+= (pos.y - points[j].y)*(pos.y - points[j].y); |
| 39 | + if (dis<minDis) minDis = dis; |
| 40 | + } |
| 41 | + return minDis; |
| 42 | +''') |
| 43 | + trtc.Transform(ctx, d_points, d_min_dis, calc_min_dis) |
| 44 | + center_ids[i] = trtc.Max_Element(ctx, d_min_dis) |
| 45 | + |
| 46 | + d_count = trtc.DVInt32(k) |
| 47 | + d_center_ids = trtc.device_vector_from_list(ctx, center_ids, 'int32_t') |
| 48 | + |
| 49 | + # initialize group-average values |
| 50 | + d_group_aves_x = trtc.device_vector(ctx, "float", k) |
| 51 | + d_group_aves_y = trtc.device_vector(ctx, "float", k) |
| 52 | + d_group_aves = trtc.DVZipped(ctx, [d_group_aves_x, d_group_aves_y], ['x','y']) |
| 53 | + |
| 54 | + trtc.Gather(ctx, d_center_ids, d_points, d_group_aves) |
| 55 | + |
| 56 | + # initialize labels |
| 57 | + d_labels = trtc.device_vector(ctx, "int32_t", n) |
| 58 | + trtc.Fill(ctx, d_labels, trtc.DVInt32(-1)) |
| 59 | + |
| 60 | + # buffer for new-calculated lables |
| 61 | + d_labels_new = trtc.device_vector(ctx, "int32_t", n) |
| 62 | + |
| 63 | + d_labels_sink = trtc.DVDiscard(ctx, "int32_t", k) |
| 64 | + d_group_sums = trtc.device_vector(ctx, d_points.name_elem_cls(), k) |
| 65 | + d_group_cumulate_counts = trtc.device_vector(ctx, "int32_t", k) |
| 66 | + d_group_counts = trtc.device_vector(ctx, "int32_t", k) |
| 67 | + |
| 68 | + d_counter = trtc.DVCounter(ctx, trtc.DVInt32(0), k) |
| 69 | + |
| 70 | + # iterations |
| 71 | + while True: |
| 72 | + # calculate new labels |
| 73 | + calc_new_labels = trtc.Functor(ctx, {"aves": d_group_aves, "count": d_count }, ['pos'], |
| 74 | +''' |
| 75 | + float minDis = FLT_MAX; |
| 76 | + int label = -1; |
| 77 | + for (int i=0; i<count; i++) |
| 78 | + { |
| 79 | + float dis = (pos.x - aves[i].x)*(pos.x - aves[i].x); |
| 80 | + dis+= (pos.y - aves[i].y)*(pos.y - aves[i].y); |
| 81 | + if (dis<minDis) |
| 82 | + { |
| 83 | + minDis = dis; |
| 84 | + label = i; |
| 85 | + } |
| 86 | + } |
| 87 | + return label; |
| 88 | +''') |
| 89 | + trtc.Transform(ctx, d_points, d_labels_new, calc_new_labels) |
| 90 | + if trtc.Equal(ctx, d_labels, d_labels_new): |
| 91 | + break |
| 92 | + trtc.Copy(ctx, d_labels_new, d_labels) |
| 93 | + |
| 94 | + # recalculate group-average values |
| 95 | + trtc.Sort_By_Key(ctx, d_labels, d_points) |
| 96 | + trtc.Reduce_By_Key(ctx, d_labels, d_points, d_labels_sink, d_group_sums, trtc.EqualTo(), point_plus) |
| 97 | + trtc.Upper_Bound_V(ctx, d_labels, d_counter, d_group_cumulate_counts) |
| 98 | + trtc.Adjacent_Difference(ctx, d_group_cumulate_counts, d_group_counts) |
| 99 | + trtc.Transform_Binary(ctx, d_group_sums, d_group_counts, d_group_aves, point_div) |
| 100 | + |
| 101 | + h_x = d_x.to_host() |
| 102 | + h_y = d_y.to_host() |
| 103 | + h_labels = d_labels.to_host() |
| 104 | + h_group_aves_x = d_group_aves_x.to_host() |
| 105 | + h_group_aves_y = d_group_aves_y.to_host() |
| 106 | + h_group_counts = d_group_counts.to_host() |
| 107 | + |
| 108 | + lines = [] |
| 109 | + |
| 110 | + for i in range(n): |
| 111 | + label = h_labels[i] |
| 112 | + lines.append([(h_x[i], h_y[i]), (h_group_aves_x[label], h_group_aves_y[label]) ] ) |
| 113 | + |
| 114 | + lc = mc.LineCollection(lines) |
| 115 | + |
| 116 | + fig, ax = plt.subplots() |
| 117 | + ax.set_xlim((0, 1000)) |
| 118 | + ax.set_ylim((0, 1000)) |
| 119 | + |
| 120 | + ax.add_collection(lc) |
| 121 | + |
| 122 | + plt.show() |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | +if __name__ == '__main__': |
| 127 | + |
| 128 | + ctx = trtc.Context() |
| 129 | + |
| 130 | + h_x = np.random.rand(1000).astype(np.float32)*1000.0 |
| 131 | + h_y = np.random.rand(1000).astype(np.float32)*1000.0 |
| 132 | + |
| 133 | + d_x = trtc.device_vector_from_numpy(ctx, h_x) |
| 134 | + d_y = trtc.device_vector_from_numpy(ctx, h_y) |
| 135 | + |
| 136 | + demo_k_means(ctx, d_x, d_y, 50) |
0 commit comments