-
Notifications
You must be signed in to change notification settings - Fork 0
/
noise3_check.py
227 lines (201 loc) · 7.22 KB
/
noise3_check.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
from scipy.io import loadmat
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import copy
from random import randint
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.optimizers import Adam
from keras import optimizers
from keras.layers.normalization import BatchNormalization
from keras.utils import np_utils
from keras.layers import Conv2D, MaxPooling2D, ZeroPadding2D, GlobalAveragePooling2D
from keras.layers.advanced_activations import LeakyReLU
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ModelCheckpoint
import h5py
data_path = "./data.mat"
data_raw = loadmat(data_path)
train_img = data_raw["train_img"]
test_img = data_raw["test_img"]
Y_train = data_raw["train_lbl"]
X_train = np.reshape(train_img,(len(train_img),28,28))
X_test = np.reshape(test_img, (len(test_img),28,28))
X_valid = X_train[40000:]
Y_valid = Y_train[40000:]
np.random.seed(25)
print("X_train original shape", X_train.shape)
print("Y_train original shape", Y_train.shape)
print("X_test original shape", X_test.shape)
print("X_valid original shape", X_valid.shape)
print("Y_valid original shape", Y_valid.shape)
#pattern 1 generation
pattern1_X_train = []
pattern1_Y_train = []
pattern1_num = 5
for i in range(len(X_train)):
print(i)
img = X_train[i]
for j in range(pattern1_num):
img_new = copy.deepcopy(img)
rand_x = randint(4,14)
rand_y = randint(4,14)
for x in range(10):
for y in range(10):
prob = randint(0,1)
img_new[rand_x+x,rand_y+y] = 255*prob
pattern1_X_train.append(img_new)
pattern1_Y_train.append(Y_train[i])
pattern1_X_train = np.array(pattern1_X_train)
pattern1_Y_train = np.array(pattern1_Y_train)
print(pattern1_X_train.shape)
# ###########################
#pattern 2 generation
pattern2_X_train = []
pattern2_Y_train = []
pattern2_num = 5
for i in range(len(X_train)):
print(i)
img = X_train[i]
for k in range(pattern2_num):
img_new = copy.deepcopy(img)
# rand_x = randint(4,23)
# rand_y = randint(4,23)
for x in range(4,24):
for y in range(4,24):
# prob = randint(0,1)
noise=np.random.normal(0,100)
noise=int(noise)
sum= img_new[x,y]+noise
if sum>255:
img_new[x,y]=255
elif sum<0:
img_new[x,y]=0
else:
img_new[x,y]=sum
pattern2_X_train.append(img_new)
pattern2_Y_train.append(Y_train[i])
pattern2_X_train = np.array(pattern2_X_train)
print(pattern2_X_train.shape)
# ###############################################
# validation set preparation
#pattern 1 generation
pattern1_X_valid = []
pattern1_Y_valid = []
pattern1_num = 2
for i in range(len(X_train)):
print(i)
img = X_train[i]
for j in range(pattern1_num):
img_new = copy.deepcopy(img)
rand_x = randint(4,14)
rand_y = randint(4,14)
for x in range(10):
for y in range(10):
prob = randint(0,1)
img_new[rand_x+x,rand_y+y] = 255*prob
pattern1_X_valid.append(img_new)
pattern1_Y_valid.append(Y_train[i])
pattern1_X_valid = np.array(pattern1_X_valid)
pattern1_Y_valid = np.array(pattern1_Y_valid)
print(pattern1_X_valid.shape)
# ###########################
#pattern 2 generation
pattern2_X_valid = []
pattern2_Y_valid = []
pattern2_num = 1
for i in range(len(X_train)):
print(i)
img = X_train[i]
for k in range(pattern2_num):
img_new = copy.deepcopy(img)
# rand_x = randint(4,23)
# rand_y = randint(4,23)
for x in range(4,24):
for y in range(4,24):
# prob = randint(0,1)
noise=np.random.normal(0,100)
noise=int(noise)
sum= img_new[x,y]+noise
if sum>255:
img_new[x,y]=255
elif sum<0:
img_new[x,y]=0
else:
img_new[x,y]=sum
pattern2_X_valid.append(img_new)
pattern2_Y_valid.append(Y_train[i])
pattern2_X_valid = np.array(pattern2_X_valid)
print(pattern2_X_valid.shape)
# ###############################################
X_valid = np.concatenate((X_train,pattern1_X_valid,pattern2_X_valid))
Y_valid = np.concatenate((Y_train,pattern1_Y_valid,pattern2_Y_valid))
X_train = np.concatenate((X_train,pattern1_X_train,pattern2_X_train))
Y_train = np.concatenate((Y_train,pattern1_Y_train,pattern2_Y_train))
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1)
X_valid = X_valid.reshape(X_valid.shape[0], 28, 28, 1)
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1)
X_train = X_train.astype('float32')
X_valid = X_valid.astype('float32')
X_test = X_test.astype('float32')
number_of_classes = 10
Y_train = np_utils.to_categorical(Y_train, number_of_classes)
Y_valid = np_utils.to_categorical(Y_valid, number_of_classes)
X_train/=255
X_valid/=255
X_test/=255
# model structure start
model = Sequential()
model.add(Conv2D(64, (5, 5), padding='valid', input_shape=(28,28,1)))
model.add(Activation('relu'))
BatchNormalization(axis=-1)
model.add(Conv2D(32, (5, 5), padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
BatchNormalization(axis=-1)
model.add(Conv2D(64, (5, 5), padding='same'))
model.add(Activation('relu'))
BatchNormalization(axis=-1)
#model.add(Conv2D(64, (3, 3)))
#model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(32,(1,1), padding='same'))
model.add(Flatten())
# Fully connected layer
BatchNormalization()
model.add(Dense(1024))
model.add(Activation('relu'))
#model.add(Dense(256))
#model.add(Activation('relu'))
BatchNormalization()
model.add(Dropout(0.5))
model.add(Dense(10))
# model.add(Convolution2D(10,3,3, border_mode='same'))
# model.add(GlobalAveragePooling2D())
model.add(Activation('softmax'))
model.summary()
#sgd = optimizers.SGD(lr=0.001, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=Adam(), metrics=['accuracy'])
gen = ImageDataGenerator(rotation_range=8, width_shift_range=0.06, shear_range=0.3,
height_shift_range=0.06, zoom_range=0.06)
#gen = ImageDataGenerator()
test_gen = ImageDataGenerator()
train_generator = gen.flow(X_train, Y_train, batch_size=64)
test_generator = test_gen.flow(X_valid, Y_valid, batch_size=64)
filepath="./adam/weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
model.fit_generator(train_generator, steps_per_epoch=len(X_train)//64, epochs=30,callbacks=callbacks_list,validation_data=test_generator, validation_steps=len(X_valid)//64)
# save and reload model
model.save('1_5_5_epochs_40_valid_structure_sgd_check.h5')
# prediction
score = model.evaluate(X_valid, Y_valid)
print()
print('valid accuracy: ', score[1])
# output the file
prediction = model.predict_classes(X_test)
prediction = list(prediction)
ID = list(range(1,20001))
sub = pd.DataFrame({'ID': ID, 'Prediction': prediction})
sub.to_csv('./1_5_5_epochs_40_valid_structure_sgd_check.csv', index=False)