-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotMNIST_p5_sanitazing.py
executable file
·176 lines (153 loc) · 5.92 KB
/
notMNIST_p5_sanitazing.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# -*- coding: utf-8 -*-
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
import tarfile
from IPython.display import display, Image
from scipy import ndimage
from sklearn.linear_model import LogisticRegression
from six.moves.urllib.request import urlretrieve
from six.moves import cPickle as pickle
import re
import IPython
ip = IPython.get_ipython()
ip.enable_pylab()
#folder = r'C:\Users\YoelS\Desktop\Udacity'
#f = open(os.path.join(folder, 'notMNIST.pickle'), 'rb')
#data = pickle.load(f)
image_size = 28
key = 'train_dataset' # 'valid_dataset' # 'test_dataset' # 'train_dataset'
n_set = len(data[key]) - 1
n_partial = 3
index_duplicates = np.int32(np.array([]))
for k in np.arange(n_set - 1, 1, -1):
if any(k == index_duplicates):
continue
if k % 1000 == 0:
index_duplicates = np.unique(index_duplicates)
print('found {} duplicates, {} images left to go'.format(
str(len(index_duplicates)), str(k)))
partial_diffs = np.zeros(k - 1)
for r in range(n_partial):
for c in range(n_partial):
partial_diffs += np.abs(np.subtract(
data[key][: k - 1, r, c], data[key][k, r, c]))
index_candidates = np.argwhere(partial_diffs == 0).flatten()
n_cand = len(index_candidates)
if n_cand == 0:
continue
partial_diffs = partial_diffs[index_candidates]
for r in range(n_partial, 2*n_partial + 2):
for c in range(n_partial, 2*n_partial + 2):
partial_diffs += np.abs(np.subtract(
data[key][index_candidates, r, c], data[key][k, r, c]))
index_cand2 = np.argwhere(partial_diffs == 0).flatten()
n_cand = len(index_cand2)
if n_cand == 0:
continue
partial_diffs = partial_diffs[index_cand2]
index_candidates = index_candidates[index_cand2]
for r in range(2*n_partial + 2, 3*n_partial + 4):
for c in range(2*n_partial + 2, 3*n_partial + 4):
partial_diffs += np.abs(np.subtract(
data[key][index_candidates, r, c], data[key][k, r, c]))
index_cand2 = np.argwhere(partial_diffs == 0).flatten()
n_cand = len(index_cand2)
if n_cand == 0:
continue
full_diffs = np.zeros(n_cand)
index_candidates = index_candidates[index_cand2]
for r in range(image_size):
for c in range(image_size):
full_diffs += np.abs(np.subtract(
data[key][index_candidates, r, c], data[key][k, r, c]))
index_full_match = np.argwhere(full_diffs == 0).flatten()
index_full_match = index_candidates[index_full_match]
n_full_match = len(index_full_match)
if n_full_match > 1:
index_duplicates = np.append(
index_duplicates, np.int32(index_full_match[1:]))
print('marking {} copies'.format(n_full_match))
if n_full_match > 0:
index_duplicates = np.append(index_duplicates, np.int32(k))
print('found {} duplicates, out of {}'.format(
str(len(index_duplicates)), str(n_set + 1)))
index_keep = np.arange(n_set + 1)
index_keep = np.delete(index_keep, index_duplicates)
data_sanitized[key] = data[key][index_keep, :, :]
key = key.replace('_dataset', '_labels')
data_sanitized[key] = data[key][index_keep]
## cross checking - yielded zero duplicates
#
#key0 = 'train_dataset'
#key1 = 'test_dataset'
#n0 = len(data_sanitized[key0])
#n1 = len(data_sanitized[key1])
#n_partial = 3
#index_duplicates = np.int32(np.array([]))
#for k1 in range(n1):
#
# if k1 % 500 == 0:
# index_duplicates = np.unique(index_duplicates)
# print('found {} duplicates, {} images left to go'.format(
# str(len(index_duplicates)), str(n1 - k1)))
#
# partial_diffs = np.zeros(n0)
# for r in range(n_partial):
# for c in range(n_partial):
# partial_diffs += np.abs(np.subtract(
# data_sanitized[key0][:, r, c],
# data_sanitized[key1][k1, r, c]))
#
# index_candidates = np.argwhere(partial_diffs == 0).flatten()
# n_cand = len(index_candidates)
# if n_cand == 0:
# continue
#
# partial_diffs = partial_diffs[index_candidates]
# for r in range(n_partial, 2*n_partial + 2):
# for c in range(n_partial, 2*n_partial + 2):
# partial_diffs += np.abs(np.subtract(
# data_sanitized[key0][index_candidates, r, c],
# data_sanitized[key1][k1, r, c]))
#
# index_cand2 = np.argwhere(partial_diffs == 0).flatten()
# n_cand = len(index_cand2)
# if n_cand == 0:
# continue
#
# partial_diffs = partial_diffs[index_cand2]
# index_candidates = index_candidates[index_cand2]
# for r in range(2*n_partial + 2, 3*n_partial + 4):
# for c in range(2*n_partial + 2, 3*n_partial + 4):
# partial_diffs += np.abs(np.subtract(
# data_sanitized[key0][index_candidates, r, c],
# data_sanitized[key1][k1, r, c]))
#
# index_cand2 = np.argwhere(partial_diffs == 0).flatten()
# n_cand = len(index_cand2)
# if n_cand == 0:
# continue
#
# full_diffs = np.zeros(n_cand)
# index_candidates = index_candidates[index_cand2]
# for r in range(image_size):
# for c in range(image_size):
# full_diffs += np.abs(np.subtract(
# data_sanitized[key0][index_candidates, r, c],
# data_sanitized[key1][k1, r, c]))
#
# index_full_match = np.argwhere(full_diffs == 0).flatten()
# index_full_match = index_candidates[index_full_match]
# n_full_match = len(index_full_match)
# if n_full_match > 0:
# index_duplicates = np.append(
# index_duplicates, np.int32(index_full_match[1:]))
#
#print('found {} duplicates, out of {}'.format(
# str(len(index_duplicates)), str(n1)))
#
#index_keep = np.arange(n0)
#index_keep = np.delete(index_keep, index_duplicates)