-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
175 lines (114 loc) · 5.74 KB
/
model.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
import numpy as np
import tensorflow as tf
import os
from skimage import io
from skimage.transform import rescale, resize, downscale_local_mean
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import csv
class model:
def __init__(self,input_shape,n_outputs,test_size,learning_rate,epochs=9,batch_size = 100):
self.input_shape = input_shape
self.n_outputs = n_outputs
self.test_size = test_size
self.learning_rate = learning_rate
self.epochs =epochs
self.batch_size = batch_size
def load_data(self, folder_name):
"""
:param folder_name: path of the folder that contains
the subfolders named with the labels and their images
:return: two arrays with the data stored the working
directory ('x.npy' , 'y.npy')
"""
y =[]
X=[]
for i in os.listdir(folder_name):
boat_folder = os.path.join(folder_name, i)
for j in (os.listdir(boat_folder)):
label = int(boat_folder.split('/')[1][-2:])
img=(io.imread(os.path.join(boat_folder, j)))
image_resized = resize(img, (32, 32, 3),anti_aliasing=True)
y.append(label)
X.append(image_resized)
X = np.array(X)
y = np.array(y)
np.save('x.npy',X)
np.save('y.npy',y)
def permute_feature_label_data(self):
"""Generates a random order and permutes the feature and label data accordingly."""
feature_data = np.load('x.npy')
label_data = np.load('y.npy')
permutation = np.random.permutation(label_data.shape[0])
# Reorganizes the given feature data and its labels in the permutation order.
permuted_feature_data = feature_data[permutation, :, :, :]
permuted_label_data = label_data[permutation]
np.save('x.npy', permuted_feature_data)
np.save('y.npy', permuted_label_data)
return permuted_feature_data, permuted_label_data
def split(self,X,y):
"""
Normalize the images dividing them by 255 and split the data into train and test
:param X: images (N,H,W,C)
:param y: labels(N,)
:param test_size: size of the test sample (0-1)
:return: X_train, X_test, y_train, y_test according to the test size
"""
#X = X - np.mean(X)
X = X/255
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=self.test_size)
return X_train, X_test, y_train, y_test
def train_model(self,X_train,y_train,X_test,y_test):
"""
:param X_train:
:param y_train:
:param X_test:
:param y_test:
:return:
"""
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(filters=40, kernel_size=7, padding='same',activation=tf.nn.relu, input_shape=self.input_shape,kernel_regularizer=tf.keras.regularizers.l2(0.001)),
tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPool2D(pool_size=(2,2), strides=1),
tf.keras.layers.Conv2D(filters=20,kernel_size=(5,5),padding='same',activation=tf.nn.relu,kernel_regularizer=tf.keras.regularizers.l2(0.001)),
#tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPool2D(pool_size=(2,2), strides=1),
#tf.keras.layers.Dropout(rate=0.3),
tf.keras.layers.Conv2D(filters=10,kernel_size=(3,3),padding='same',activation=tf.nn.relu,kernel_regularizer=tf.keras.regularizers.l2(0.001)),
#tf.keras.layers.BatchNormalization(),
tf.keras.layers.MaxPool2D(pool_size=(2,2), strides=1),
tf.keras.layers.Flatten(),
#tf.keras.layers.Dropout(rate=0.2),
tf.keras.layers.Dense(units=1024,activation=tf.nn.relu,use_bias=True),
#tf.keras.layers.Dropout(rate=0.2),
#tf.keras.layers.BatchNormalization(),
tf.keras.layers.Dense(units=512,activation=tf.nn.relu,use_bias=True,kernel_regularizer=tf.keras.regularizers.l2(0.00)),
tf.keras.layers.Dense(units=self.n_outputs,use_bias=True,activation=tf.nn.softmax)
])
model.compile(optimizer = 'adam',loss = 'sparse_categorical_crossentropy',metrics =['accuracy'])
model.fit(X_train, y_train,batch_size=self.batch_size,epochs=self.epochs)
test_acc = model.evaluate(X_test, y_test)
print('Test accuracy:', test_acc)
model.save('my_model.h5')
def load_data_to_predict(self,folder_name):
X=[]
with open('test.csv', 'r') as f:
reader = csv.reader(f)
your_list = list(reader)
new_list = [your_list[i + 1][0] for i in range(len(your_list) - 1)]
for i in (new_list):
img=(io.imread(os.path.join(folder_name, i)))
image_resized = resize(img, (32, 32, 3), anti_aliasing=True)
image_resized = image_resized/255
X.append(image_resized)
images = np.array(X)
return images,new_list
def predict(self,X):
"""
:param X: images (N,H,W,C)
:return: predictions (N)
"""
model = tf.keras.models.load_model('my_model.h5')
predictions = model.predict(X)
predictions = [np.argmax(predictions[i]) for i in range(predictions.shape[0])]
return predictions