forked from ryoppippi/Gasyori100knock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer_90.py
67 lines (55 loc) · 1.5 KB
/
answer_90.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import cv2
import numpy as np
import matplotlib.pyplot as plt
from glob import glob
## Dicrease color
def dic_color(img):
img //= 63
img = img * 64 + 32
return img
## Database
train = glob("dataset/train_*")
train.sort()
db = np.zeros((len(train), 13), dtype=np.int32)
pdb = []
for i, path in enumerate(train):
img = dic_color(cv2.imread(path))
## histogram
for j in range(4):
db[i, j] = len(np.where(img[..., 0] == (64 * j + 32))[0])
db[i, j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0])
db[i, j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0])
## class
if 'akahara' in path:
cls = 0
elif 'madara' in path:
cls = 1
db[i, -1] = cls
pdb.append(path)
# k-Means
Class = 2
feats = db.copy()
np.random.seed(4)
## assign random class
for i in range(len(feats)):
if np.random.random() < 0.3:
feats[i, -1] = 0
else:
feats[i, -1] = 1
while True:
gs = np.zeros((Class, 12), dtype=np.float32)
change_count = 0
## compute gravity
for i in range(Class):
gs[i] = np.mean(feats[np.where(feats[..., -1] == i)[0], :12], axis=0)
## re-labeling
for i in range(len(feats)):
dis = np.sqrt(np.sum(np.square(np.abs(gs - feats[i, :12])), axis=1))
pred = np.argmin(dis, axis=0)
if int(feats[i, -1]) != pred:
change_count += 1
feats[i, -1] = pred
if change_count < 1:
break
for i in range(len(train)):
print(pdb[i], " Pred:", feats[i, -1])