-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredNeuronal.py
314 lines (256 loc) · 9.61 KB
/
redNeuronal.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#Se hace la red nauronal en este producto
import numpy as np # linear algebra
import os
from time import time
from keras.preprocessing.image import ImageDataGenerator, img_to_array, image
from keras.utils import np_utils
import json
from PIL import Image
import os
import tensorflow as tf
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, Activation, BatchNormalization, \
GlobalAveragePooling2D
from keras.applications.resnet import ResNet50
from keras.models import Model
from tensorflow.keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
def entrenamiento():
# # When to save the model
checkpointer = ModelCheckpoint(filepath='model.weights.best.hdf5',
verbose=1,
save_best_only=True)
# # Reduce learning rate when loss doesn't improve after n epochs
scheduler = ReduceLROnPlateau(monitor='val_loss', factor=0.1,
patience=5, min_lr=1e-8, verbose=1)
# # Stop early if model doesn't improve after n epochs
early_stopper = EarlyStopping(monitor='val_loss', patience=10,
verbose=0, restore_best_weights=True)
# # Train the model
#Cambiar de fit_generator(...*) a fit(...*)
history = model.fit_generator(train_generator,
steps_per_epoch=num_train // batch_size,
epochs=5,
verbose=1,
callbacks=[checkpointer, scheduler],
validation_data=valid_generator,
validation_steps=num_valid // batch_size)
model.save('model3.h5')
def get_prediction(img, real_label):
img = img_to_array(img) / 255
# normalise image
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
img = (img - mean) / std
img_expand = np.expand_dims(img, axis=0)
prediction = model.predict(img_expand)
prediction_int = np.argmax(prediction)
print(prediction, " ", prediction_int)
dir_int = int_to_dir[prediction_int]
label_name = cat_2_name[str(dir_int)]
print(label_name, "*****" )#, real_label)
#print("Predicted: {}\nReal: {}".format(label_name, cat_2_name[str(real_label)]))
print()
#Comenzar entrenamiento de red neuronal
data_dir = "Spiderweb_kb"
data_train_path = data_dir + '/train'
data_valid_path = data_dir + '/valid'
data_test_path = data_dir + '/test'
print(os.listdir(data_dir))
with open('spiders.json', 'r') as json_file:
cat_2_name = json.load(json_file)
# print(cat_2_name['200'])
batch_size = 10
# Transforms
datagen_train = ImageDataGenerator(
rescale=1. / 255,
rotation_range=40,
width_shift_range=0.1, # randomly shift images horizontally
height_shift_range=0.1, # randomly shift images vertically
horizontal_flip=True,
featurewise_std_normalization=True, # Normalize images
samplewise_std_normalization=True)
datagen_valid = ImageDataGenerator(
rescale=1. / 255,
rotation_range=40,
width_shift_range=0.1, # randomly shift images horizontally
height_shift_range=0.1, # randomly shift images vertically
horizontal_flip=True,
featurewise_std_normalization=True,
samplewise_std_normalization=True)
datagen_test = ImageDataGenerator(
rescale=1. / 255,
featurewise_std_normalization=True,
samplewise_std_normalization=True)
print("Split datagen and generators")
train_generator = datagen_train.flow_from_directory(
data_train_path,
target_size=(224, 224),
batch_size=batch_size,
class_mode='categorical')
valid_generator = datagen_valid.flow_from_directory(
data_valid_path,
target_size=(224, 224),
batch_size=batch_size,
class_mode='categorical')
test_generator = datagen_test.flow_from_directory(
data_test_path,
target_size=(224, 224),
batch_size=batch_size,
class_mode='categorical')
# Lets have a look at some of our images
'''
images, labels = train_generator.next()
fig = plt.figure(figsize=(20, 10))
fig.subplots_adjust(wspace=0.2, hspace=0.4)
# Lets show the first 32 images of a batch
for i, img in enumerate(images[:6]):
ax = fig.add_subplot(2, 3, i + 1, xticks=[], yticks=[])
ax.imshow(img)
image_idx = np.argmax(labels[i])
'''
#Parte en la que se prepara el modelo de predicción
int_to_dir = {v: k for k, v in train_generator.class_indices.items()}
base_model = ResNet50(
include_top=False,
weights="imagenet"
)
# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='elu')(x)
x = Dropout(0.95)(x)
# and a logistic layer
predictions = Dense(3, activation='softmax')(x)
for layer in base_model.layers:
layer.trainable = True
# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)
print("Se ha generado el modelo")
# compile the model (should be done *after* setting layers to non-trainable)
model.compile(optimizer=Adam(lr=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
print("Se ha compilado el modelo")
num_train = len(train_generator.filenames)
num_valid = len(valid_generator.filenames)
num_test = len(train_generator.filenames)
#entrenamiento()
print("Probando el modelo")
model.load_weights('model.weights.best.hdf5')
#score = model.evaluate_generator(test_generator, steps=num_test // 1, verbose=1)
#for sc in score:
# print('Test accuracy: ', sc)
for i in range(10):
random_index = np.random.randint(0, len(test_generator.filenames))
data_dir = "Spiderweb_kb"
#data_dir = "C:/Users/Vrick/Desktop"
#Pruebas en conjunto
img = test_generator.filenames[random_index]
imgTest = image.load_img(data_dir + "/test/" + img, target_size=(224, 224))
real_label = test_generator.filenames[random_index].split("/")[0]
#imgName = "C:\\Users\\Vrick\\Desktop\\Prueba\\VN117.jpg"
#imgTest = image.load_img(imgName, target_size=(224, 224))
#real_label = "violinista" #test_generator.filenames[random_index].split("/")[0]
#Para pruebas particulares
#index_particular = "Violinista/6.jpg"
#img = index_particular
#imgTest = image.load_img(data_dir + "/test/" + img, target_size=(224, 224))
#real_label = index_particular.split("/")[0]
get_prediction(imgTest, real_label)
'''
import numpy as np # linear algebra
import os
from time import time
from keras.preprocessing.image import ImageDataGenerator, img_to_array, image
from keras.utils import np_utils
import json
from PIL import Image
import os
import tensorflow as tf
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, Activation, BatchNormalization, GlobalAveragePooling2D
from keras.applications import ResNet50
from keras.models import Model
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping
import cv2
data_dir = "coins/data3"
batch_size=5
data_train_path = data_dir + '/train'
with open('cat_to_name.json', 'r') as json_file:
cat_2_name = json.load(json_file)
datagen_train = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.1, # randomly shift images horizontally
height_shift_range=0.1, # randomly shift images vertically
horizontal_flip=True,
featurewise_std_normalization=True, # Normalize images
samplewise_std_normalization=True)
train_generator = datagen_train.flow_from_directory(
data_train_path,
target_size=(224, 224),
batch_size=batch_size,
class_mode='categorical')
base_model = ResNet50(
include_top=False,
weights="imagenet"
)
int_to_dir = {v: k for k, v in train_generator.class_indices.items()}
# add a global spatial average pooling layer
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='elu')(x)
x = Dropout(0.95)(x)
# and a logistic layer
predictions = Dense(3, activation='softmax')(x)
# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)
for layer in base_model.layers:
layer.trainable = True
model.load_weights('model.weights.best.hdf5')
font = cv2.FONT_HERSHEY_SIMPLEX
org = (50, 50)
# fontScale
fontScale = 1
# Blue color in BGR
color = (0, 0, 255)
# Line thickness of 2 px
thickness = 2
cap = cv2.VideoCapture(1)
label_name=""
while(True):
# Captura video cuadro a cuadro
ret, img = cap.read()
if ret == True:
img2=img
img2 = cv2.putText(img2, label_name, org, font,fontScale, color, thickness, cv2.LINE_AA)
cv2.imshow("Monedas", img2)
tecla=cv2.waitKey(20)
if tecla==ord('p'):
filename = 'savedImage.jpg'
cv2.imwrite(filename, img)
imDetect=image.load_img("savedImage.jpg",target_size=(224,224))
imDetec=image.img_to_array(imDetect)/255
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
imDetec=(imDetec-mean)/std
img_expand = np.expand_dims(imDetec, axis=0)
prediction = model.predict(img_expand)
prediction_int = np.argmax(prediction)
print(prediction," ",prediction_int)
dir_int = int_to_dir[prediction_int]
label_name = cat_2_name[str(dir_int)]
print("Moneda detectada",label_name)
# Using cv2.putText() method
cv2.waitKey(2000)
else:
if tecla==ord('s'):
break
# Cuando todo está listo, se libera la captura
else:
break
cap.release()
cv2.destroyAllWindows()
'''