|
| 1 | +import torch |
1 | 2 | import numpy as np |
2 | | -from multiprocessing import Pool, Value, Manager, Lock |
3 | 3 |
|
4 | | -def predict(params): |
5 | | - image, label, k = params |
6 | | - # 计算距离 |
7 | | - diss = np.sum(np.square(train_images - image), axis=1) |
8 | | - # 将距离和标签结合 |
9 | | - arr = [(x, y) for (x, y) in zip(diss, train_labels)] |
10 | | - # 排序得到前k个 |
11 | | - arr.sort() # build-in array的排序规则 |
12 | | - select = arr[:k] # 只要前K个 |
| 4 | +device = torch.device('cpu') |
| 5 | +# use gpu |
| 6 | +if torch.cuda.is_available(): |
| 7 | + device = torch.device('cuda') |
| 8 | + |
| 9 | + |
| 10 | +def predict(image, label, train_images, train_labels, k=1): |
| 11 | + diss = torch.sum(torch.pow(train_images - image, 2), dim=1).cpu().numpy() |
| 12 | + arr = [(x, y) for x, y in zip(diss, train_labels)] |
| 13 | + arr.sort() |
| 14 | + # find first k members |
| 15 | + select = arr[:k] |
13 | 16 | m = {} |
14 | 17 | for _, lab in select: |
15 | 18 | if lab in m.keys(): |
16 | 19 | m[lab] += 1 |
17 | 20 | else: |
18 | 21 | m[lab] = 1 |
19 | | - # 选出最大的的标记 |
20 | | - # key是label, value是数目 |
21 | 22 | pre = sorted(list(m.items()), key=lambda x: x[1], reverse=True)[0][0] |
22 | | - print(label, pre) |
23 | 23 | if pre == label: |
24 | | - return 1 |
| 24 | + return 1 |
25 | 25 | return 0 |
26 | 26 |
|
27 | 27 |
|
28 | | -print(np.__version__) |
29 | | -print('loading data') |
30 | | -train_images = np.load('train_images.npy', allow_pickle=True) |
31 | | -train_labels = np.load('train_labels.npy', allow_pickle=True) |
32 | | -test_images = np.load('test_images.npy', allow_pickle=True) |
33 | | -test_labels = np.load('test_labels.npy', allow_pickle=True) |
34 | | -print('load finished') |
35 | | - |
36 | | -if __name__ == "__main__": |
37 | | - pool = Pool(3) |
| 28 | +if __name__ == '__main__': |
| 29 | + print('loading data') |
| 30 | + train_images = np.load('data/train_images.npy', allow_pickle=True) |
| 31 | + train_labels = np.load('data/train_labels.npy', allow_pickle=True) |
| 32 | + test_images = np.load('data/test_images.npy', allow_pickle=True) |
| 33 | + test_labels = np.load('data/test_labels.npy', allow_pickle=True) |
| 34 | + print('load finished') |
| 35 | + # use GPU to accelerate knn |
| 36 | + train_images = torch.from_numpy(train_images).to(device=device) |
| 37 | + test_images = torch.from_numpy(test_images).to(device=device) |
| 38 | + sum_cnt = 10000 |
38 | 39 | k = 1 |
39 | | - # lock = manager.Lock() |
40 | | - result = pool.map(predict, [(*x, k) for x in zip(test_images, test_labels)]) |
41 | | - pool.close() |
42 | | - pool.terminate() |
43 | | - pool.join() |
44 | | - print('%.2f%%' % (sum(result) / 100)) |
| 40 | + corr = 0 |
| 41 | + |
| 42 | + for i in range(sum_cnt): |
| 43 | + corr += predict(test_images[i], test_labels[i], train_images, train_labels, k) |
| 44 | + if (i + 1) % 1000 == 0: |
| 45 | + print(corr / (i + 1) * 100, '%') |
| 46 | + print('correct rate: %.2f%%' % (corr / sum_cnt * 100)) |
| 47 | + ''' |
| 48 | + k = 1, 96.91% |
| 49 | + k = 2, 96.91% |
| 50 | + 3, 97.16 |
| 51 | + 5, 96.93 |
| 52 | + 8, 97.00 |
| 53 | + 16, 96.46 |
| 54 | + 32 95.93 |
| 55 | + ''' |
0 commit comments