-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain2.py
105 lines (83 loc) · 2.83 KB
/
train2.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
import sys
import os
from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras import optimizers
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dropout, Flatten, Dense, Activation
from tensorflow.python.keras.layers import Convolution2D, MaxPooling2D
from tensorflow.python.keras import backend as K
from tensorflow.python.keras import applications
#model = VGG_16('vgg16_weights.h5')
#from keras.models import load_model
#model = load_model('model.h5')
## copiame vgg copia las capas , agrega una capa
def modelo():
vgg=applications.vgg16.VGG16('vgg16_weights_tf_dim_ordering_tf_kernels.h5')
cnn=Sequential()
for capa in vgg.layers:
cnn.add(capa)
cnn.layers.pop()
for layer in cnn.layers:
layer.trainable=False
cnn.add(Dense(3,activation='softmax'))
return cnn
K.clear_session()
data_entrenamiento = './data/entrenamiento'
data_validacion = './data/validacion'
epocas=1
longitud, altura = 224, 224
batch_size = 32
pasos = 1000
validation_steps = 300
filtrosConv1 = 32
filtrosConv2 = 64
tamano_filtro1 = (3, 3)
tamano_filtro2 = (2, 2)
tamano_pool = (2, 2)
clases = 3
lr = 0.0004
##Preparamos nuestras imagenes
entrenamiento_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
entrenamiento_generador = entrenamiento_datagen.flow_from_directory(
data_entrenamiento,
target_size=(altura, longitud),
batch_size=batch_size,
class_mode='categorical')
validacion_generador = test_datagen.flow_from_directory(
data_validacion,
target_size=(altura, longitud),
batch_size=batch_size,
class_mode='categorical')
print(entrenamiento_generador.class_indices)
##TODO ESTO ES SUSTITUIDO POR LA FUNCION QUE CREA LA RED VGG16
'''cnn = Sequential()
cnn.add(Convolution2D(filtrosConv1, tamano_filtro1, padding ="same", input_shape=(longitud, altura, 3), activation='relu'))
cnn.add(MaxPooling2D(pool_size=tamano_pool))
cnn.add(Convolution2D(filtrosConv2, tamano_filtro2, padding ="same"))
cnn.add(MaxPooling2D(pool_size=tamano_pool))
cnn.add(Flatten())
cnn.add(Dense(256, activation='relu'))
cnn.add(Dropout(0.5))
cnn.add(Dense(clases, activation='softmax'))
'''
##CREAR LA RED VGG16
cnn=modelo() ## reemplaza a las lineas comentadas anteriormente
cnn.compile(loss='categorical_crossentropy',
optimizer=optimizers.Adam(lr=lr),
metrics=['accuracy'])
cnn.fit_generator(
entrenamiento_generador,
steps_per_epoch=pasos,
epochs=epocas,
validation_data=validacion_generador,
validation_steps=validation_steps)
target_dir = './modelo/'
if not os.path.exists(target_dir):
os.mkdir(target_dir)
cnn.save('./modelo/modelo.h5')
cnn.save_weights('./modelo/pesos.h5')