-
Notifications
You must be signed in to change notification settings - Fork 0
/
Digits_Keras_v4.2.py
151 lines (114 loc) · 4.2 KB
/
Digits_Keras_v4.2.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
"""
Competicion de Kaggle (Digits)
Reconocimiento de digitos escritos a mano:
- Procesamos imagenes de (28x28x1)
- Red Neuronal creada desde cero manualmente:
* Input (28x28x1)
* Conv2D (128f, kernel=3x3)
* Conv2D (128f, kernel=3x3)
* Pooling (Max, 2x2)
* Dropout (0.25)
------
* Conv2D (32f, kernel=3x3)
* Conv2D (32f, kernel=3x3)
* Pooling (Max, 2x2)
* Dropout (0.25)
-----
* Conv2D (32f, kernel=3x3)
* Conv2D (32f, kernel=3x3)
* Pooling (Max, 2x2)
* Dropout (0.25)
------
* Full Connected (Flatten)
* Capa 256 neuronas
* Dropout (0.25)
* Output (10 clases)
- Data Augmentation:
* rotation_range = 10, Rango de rotacion en 10º
* zoom_range = 0.1, 10% de zoom de la imagen
* width_shift_range = 0.1, 10% de movimiento lateral de la imagen
* height_shift_range = 0.1, 10% de movimiento vertical de la imagen
Modelo:
-AWS (GPU=V100)
acc_model: loss: 0.1460 - acc: 0.9746
-Local
acc_model: loss: - acc:
Resultado Leaderboard Kaggle:
acc:
Pos:
"""
import pandas as pd
import numpy as np
from PIL import Image,ImageFilter
import os
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing import image
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.models import Sequential
from keras.optimizers import RMSprop
from keras.utils import np_utils
from keras.callbacks import ReduceLROnPlateau
from keras.models import Model
ruta = os.getcwd()
ruta_train = ruta + '/Data/train.csv'
ruta_test = ruta + '/Data/test.csv'
data = pd.read_csv(ruta_train)
x_train = data[data.columns[1:]].values
y_train = data[data.columns[0]].values
# Normalizamos las imagenes
x_train = x_train/255.0
# Reestructuramos la estructura de la imagen para la NN
x_train = x_train.reshape(x_train.shape[0],28,28,1)
# Vectorizamos las salidas
y_train = np_utils.to_categorical(y_train,10)
"""
Arquitectura Red Neuronal
"""
model = Sequential()
model.add(Conv2D(filters = 128, kernel_size = (3,3),padding = 'Same',
activation ='relu', input_shape = (28,28,1)))
model.add(Conv2D(filters = 128, kernel_size = (3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Conv2D(filters = 32, kernel_size = (3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 32, kernel_size = (3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
# funcion para modificar el factor de aprendizaje en funcion de su evolucion
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
# Data Augmentation
datagen = ImageDataGenerator(
rotation_range=10,
zoom_range=0.1,
width_shift_range=0.1,
height_shift_range=0.1)
datagen.fit(x_train)
epochs = 30 #
batch_size = 86
history = model.fit_generator(datagen.flow(x_train,y_train, batch_size=batch_size),
epochs = epochs,
verbose = 1,
# steps_per_epoch = x_train.shape[0] // batch_size,
steps_per_epoch = 1000,
callbacks = [learning_rate_reduction])
# Persistimos el modelo
model.save('Model_newNN_GPU_v4.2.h5')