質問編集履歴
3
コードの設計について補足
test
CHANGED
File without changes
|
test
CHANGED
@@ -28,7 +28,7 @@
|
|
28
28
|
|
29
29
|
### 該当のソースコード
|
30
30
|
|
31
|
-
|
31
|
+
以下のコードは、centroids=init_centroids(k-meansにおいて初めにランダムに中心点を初期化したもの)を引数として受け取ります。クラスタの中心点をアップデートする部分の関数です。
|
32
32
|
|
33
33
|
```python
|
34
34
|
|
@@ -118,6 +118,34 @@
|
|
118
118
|
|
119
119
|
```
|
120
120
|
|
121
|
+
* 全体の関数設計(補足)
|
122
|
+
|
123
|
+
```python
|
124
|
+
|
125
|
+
centroids_init = init_centroids(num_clusters, image)
|
126
|
+
|
127
|
+
centroids = update_centroids(centroids_init, image, max_iter, print_every)
|
128
|
+
|
129
|
+
```
|
130
|
+
|
131
|
+
init_centroids関数は、以下のようになっています。
|
132
|
+
|
133
|
+
```python
|
134
|
+
|
135
|
+
def init_centroids(num_clusters, image):
|
136
|
+
|
137
|
+
(h, w, c) = (image.shape[0], image.shape[1], image.shape[2])
|
138
|
+
|
139
|
+
image_flat = image.reshape(h * w, c)
|
140
|
+
|
141
|
+
init_centroid_index = np.array([random.randint(0, h * w) for _ in range(num_clusters)])
|
142
|
+
|
143
|
+
centroids_init = image_flat[init_centroid_index]
|
144
|
+
|
145
|
+
return centroids_init
|
146
|
+
|
147
|
+
```
|
148
|
+
|
121
149
|
|
122
150
|
|
123
151
|
### 仮説
|
2
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -72,18 +72,14 @@
|
|
72
72
|
|
73
73
|
"""
|
74
74
|
|
75
|
-
(h, w, c, num_clusters) = (image.shape[0], image.shape[1], image.shape[2], centroids.shape[0])
|
75
|
+
(h, w, c, num_clusters) = (image.shape[0], image.shape[1], image.shape[2], centroids.shape[0]) # 画像のh高さ、w幅、c色の数、num_clustersクラスタの数(16)
|
76
76
|
|
77
77
|
image_flat = image.reshape(h * w, c) # flatten the image --> each row: pixel, each column: color
|
78
78
|
|
79
79
|
for _iter in range(max_iter):
|
80
80
|
|
81
|
-
# Loop over all centroids and store distances in `dist`
|
82
|
-
|
83
81
|
dist = np.array([np.linalg.norm(pixel - centroids, ord=2, axis=1) for pixel in image_flat]) # 各pixelと各クラスタの中心点までの距離を計算
|
84
82
|
|
85
|
-
# idx = np.zeros((h * w, num_clusters))
|
86
|
-
|
87
83
|
idx = np.argmin(dist, axis=1)
|
88
84
|
|
89
85
|
# Find closest centroid and update `new_centroids`
|
@@ -92,9 +88,9 @@
|
|
92
88
|
|
93
89
|
#---この行がないと、kmeansが収束しない!---#
|
94
90
|
|
95
|
-
centroids = np.zeros((num_clusters, c))
|
91
|
+
centroids = np.zeros((num_clusters, c)) # centroidsをゼロで初期化
|
96
|
-
|
92
|
+
|
97
|
-
#--- ---#
|
93
|
+
#----- -----#
|
98
94
|
|
99
95
|
|
100
96
|
|
@@ -126,7 +122,7 @@
|
|
126
122
|
|
127
123
|
### 仮説
|
128
124
|
|
129
|
-
NumPy配列の変数アドレスの問題か何かだと思うのですが、毎イタレーションごとに、各要素を書き換えているので、問題ない気がしています。なのでなぜ該当行がないと動かないのか分かりません。
|
125
|
+
NumPy配列の変数アドレスの問題か何かだと思うのですが、毎イタレーションごとに、各要素を書き換えているので、問題ない気がしています。なのでなぜ該当行がないと動かないのか分かりません。メモリの問題?なお、distはshape=(128*128, 16)の配列です。
|
130
126
|
|
131
127
|
|
132
128
|
|
1
centroids配列の概要について補足。
test
CHANGED
File without changes
|
test
CHANGED
@@ -135,3 +135,261 @@
|
|
135
135
|
|
136
136
|
|
137
137
|
使用しているライブラリはNumPyのみです。
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
### 追記(2019/8/11/09:00)
|
142
|
+
|
143
|
+
なお、centroids配列は次のようになっています。
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
* 収束しない時(centroids配列をゼロで初期化しないとき)
|
148
|
+
|
149
|
+
```python
|
150
|
+
|
151
|
+
10 iterations done.
|
152
|
+
|
153
|
+
index of cluster (idx[::100]):
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
[ 9 9 6 15 9 9 15 14 15 15 15 9 9 15 13 1 9 14 15 9 9 9 9 9
|
158
|
+
|
159
|
+
11 9 9 15 9 14 14 9 9 9 9 9 9 15 15 7 9 11 9 13 9 9 9 14
|
160
|
+
|
161
|
+
9 15 9 15 14 14 15 9 14 9 15 14 15 14 14 9 9 14 11 11 9 6 9 14
|
162
|
+
|
163
|
+
15 9 9 15 9 9 11 15 14 15 9 9 15 9 15 9 13 9 15 9 9 9 9 15
|
164
|
+
|
165
|
+
9 15 11 14 9 9 13 9 15 9 14 13 9 9 14 9 9 15 9 9 9 9 15 9
|
166
|
+
|
167
|
+
9 9 9 14 9 9 9 15 9 9 9 14 9 9 9 9 15 15 9 9 14 15 9 9
|
168
|
+
|
169
|
+
9 9 15 12 9 14 15 7 14 9 2 15 9 14 3 2 9 9 9 1]
|
170
|
+
|
171
|
+
centroids' RGB values:
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
[[197 220 187]
|
176
|
+
|
177
|
+
[185 212 158]
|
178
|
+
|
179
|
+
[195 207 116]
|
180
|
+
|
181
|
+
[197 211 153]
|
182
|
+
|
183
|
+
[200 216 159]
|
184
|
+
|
185
|
+
[203 221 196]
|
186
|
+
|
187
|
+
[194 216 184]
|
188
|
+
|
189
|
+
[207 89 67]
|
190
|
+
|
191
|
+
[192 212 139]
|
192
|
+
|
193
|
+
[140 93 52]
|
194
|
+
|
195
|
+
[202 214 108]
|
196
|
+
|
197
|
+
[183 204 109]
|
198
|
+
|
199
|
+
[196 144 134]
|
200
|
+
|
201
|
+
[142 189 120]
|
202
|
+
|
203
|
+
[125 30 24]
|
204
|
+
|
205
|
+
[168 184 91]]
|
206
|
+
|
207
|
+
-------------------------
|
208
|
+
|
209
|
+
20 iterations done.
|
210
|
+
|
211
|
+
index of cluster (idx[::100]):
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
[ 7 7 2 9 7 7 9 7 15 15 9 9 7 9 11 12 7 7 1 7 1 7 7 9
|
216
|
+
|
217
|
+
12 1 7 6 7 1 1 7 7 7 1 1 7 9 15 1 7 12 7 0 7 9 1 7
|
218
|
+
|
219
|
+
7 9 7 9 7 1 9 7 14 7 9 14 9 1 7 9 7 14 6 12 7 3 7 7
|
220
|
+
|
221
|
+
9 7 7 6 7 14 12 9 14 12 14 7 9 7 9 7 9 9 12 7 7 9 7 9
|
222
|
+
|
223
|
+
7 6 6 14 7 7 9 7 9 7 14 7 7 7 7 9 7 9 7 7 7 7 9 7
|
224
|
+
|
225
|
+
7 7 9 14 7 7 7 9 7 9 7 14 7 7 7 7 12 9 14 7 14 9 7 7
|
226
|
+
|
227
|
+
7 14 7 13 7 14 12 1 14 7 12 12 7 14 3 12 7 7 7 12]
|
228
|
+
|
229
|
+
centroids' RGB values:
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
[[206 173 150]
|
234
|
+
|
235
|
+
[201 74 52]
|
236
|
+
|
237
|
+
[192 213 163]
|
238
|
+
|
239
|
+
[195 216 174]
|
240
|
+
|
241
|
+
[199 216 188]
|
242
|
+
|
243
|
+
[203 219 183]
|
244
|
+
|
245
|
+
[171 198 125]
|
246
|
+
|
247
|
+
[147 87 50]
|
248
|
+
|
249
|
+
[209 203 183]
|
250
|
+
|
251
|
+
[151 182 89]
|
252
|
+
|
253
|
+
[202 214 108]
|
254
|
+
|
255
|
+
[152 177 125]
|
256
|
+
|
257
|
+
[188 206 111]
|
258
|
+
|
259
|
+
[203 221 204]
|
260
|
+
|
261
|
+
[ 81 12 10]
|
262
|
+
|
263
|
+
[197 170 88]]
|
264
|
+
|
265
|
+
-------------------------
|
266
|
+
|
267
|
+
```
|
268
|
+
|
269
|
+
値が激しく変化しています。クラスタの配属変化も激しいです。
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
* 収束するとき(centroids配列をきちんとゼロで初期化する時)
|
274
|
+
|
275
|
+
```python
|
276
|
+
|
277
|
+
10 iterations done.
|
278
|
+
|
279
|
+
index of cluster (idx[::100]):
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
[11 10 3 2 11 1 6 15 14 7 8 10 15 2 6 3 15 15 0 1 15 15 4 2
|
284
|
+
|
285
|
+
7 15 1 3 10 15 15 15 1 1 15 15 4 6 14 14 1 7 10 5 15 10 1 15
|
286
|
+
|
287
|
+
15 8 4 6 15 15 0 10 12 11 8 12 2 15 15 2 4 12 5 7 11 3 15 15
|
288
|
+
|
289
|
+
2 14 4 6 1 9 8 2 4 7 9 4 2 13 2 15 13 10 7 9 9 13 13 2
|
290
|
+
|
291
|
+
9 5 3 12 4 15 6 13 8 9 12 13 1 0 15 13 10 2 4 9 1 1 2 15
|
292
|
+
|
293
|
+
13 1 0 12 9 10 1 8 4 13 10 12 9 9 10 1 8 8 12 4 12 2 9 10
|
294
|
+
|
295
|
+
1 12 0 3 12 12 7 15 12 4 7 7 11 12 5 7 1 11 11 5]
|
296
|
+
|
297
|
+
centroids' RGB values:
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
[[156.91060291 148.36382536 65.34303534]
|
302
|
+
|
303
|
+
[115.60351413 113.881589 55.83346066]
|
304
|
+
|
305
|
+
[149.43956044 180.28671329 82.11788212]
|
306
|
+
|
307
|
+
[192.38899083 218.05321101 191.79449541]
|
308
|
+
|
309
|
+
[106.05243902 60.4597561 34.56585366]
|
310
|
+
|
311
|
+
[189.56716418 201.32338308 148.12271973]
|
312
|
+
|
313
|
+
[151.72988506 196.17241379 129.73754789]
|
314
|
+
|
315
|
+
[191.81183317 204.19786615 92.38312318]
|
316
|
+
|
317
|
+
[172.34967623 194.25531915 84.92969473]
|
318
|
+
|
319
|
+
[143.43722564 21.54784899 24.15276558]
|
320
|
+
|
321
|
+
[115.6759195 156.87369882 74.57113116]
|
322
|
+
|
323
|
+
[172.82359081 52.58037578 41.93841336]
|
324
|
+
|
325
|
+
[ 64.18624044 6.95969423 5.67268937]
|
326
|
+
|
327
|
+
[126.88624339 180.91269841 96.17107584]
|
328
|
+
|
329
|
+
[203.56716418 106.52487562 76.47761194]
|
330
|
+
|
331
|
+
[200.2748184 49.78006457 41.54560129]]
|
332
|
+
|
333
|
+
-------------------------
|
334
|
+
|
335
|
+
20 iterations done.
|
336
|
+
|
337
|
+
index of cluster (idx[::100]):
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
[11 10 3 2 15 1 6 15 14 7 8 10 15 2 6 3 15 15 8 10 15 15 4 2
|
342
|
+
|
343
|
+
7 15 1 3 10 15 15 15 10 1 15 15 4 5 14 14 1 7 13 5 11 10 1 15
|
344
|
+
|
345
|
+
15 8 4 5 15 15 2 13 12 11 8 12 2 15 15 2 4 12 5 7 11 3 15 15
|
346
|
+
|
347
|
+
2 14 4 6 1 9 7 2 4 7 9 1 2 13 2 15 6 13 7 9 11 13 13 2
|
348
|
+
|
349
|
+
9 5 3 12 4 15 6 13 8 9 12 13 1 0 15 13 10 8 4 9 10 1 8 11
|
350
|
+
|
351
|
+
13 1 0 12 11 10 1 8 1 13 10 12 9 9 10 1 8 8 12 4 12 2 9 10
|
352
|
+
|
353
|
+
1 12 0 3 12 12 7 15 12 4 7 7 11 12 5 7 1 11 11 5]
|
354
|
+
|
355
|
+
centroids' RGB values:
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
[[154.13868613 140.83698297 62.75912409]
|
360
|
+
|
361
|
+
[116.4886562 106.15794066 53.08202443]
|
362
|
+
|
363
|
+
[147.43695015 177.84359726 80.51221896]
|
364
|
+
|
365
|
+
[192.87184116 218.00180505 191.34115523]
|
366
|
+
|
367
|
+
[104.56811989 55.91008174 32.53814714]
|
368
|
+
|
369
|
+
[184.55113636 201.33238636 147.74431818]
|
370
|
+
|
371
|
+
[144.18470418 192.13275613 118.23809524]
|
372
|
+
|
373
|
+
[189.66607302 205.07569012 93.33926981]
|
374
|
+
|
375
|
+
[172.52888087 190.85920578 82.72292419]
|
376
|
+
|
377
|
+
[140.20383451 19.89101917 22.60443996]
|
378
|
+
|
379
|
+
[112.52730375 147.36433447 68.76450512]
|
380
|
+
|
381
|
+
[171.89906542 43.12149533 38.25420561]
|
382
|
+
|
383
|
+
[ 63.49573257 6.6116643 5.42318634]
|
384
|
+
|
385
|
+
[122.00744048 174.20089286 88.81994048]
|
386
|
+
|
387
|
+
[202.94634146 107.63902439 76.84634146]
|
388
|
+
|
389
|
+
[200.11342685 52.06092184 42.48857715]]
|
390
|
+
|
391
|
+
-------------------------
|
392
|
+
|
393
|
+
```
|
394
|
+
|
395
|
+
中心点の値はそれほど変動せず、クラスタの配属が安定的です。
|