Update creacion_modelo.py
Browse files- creacion_modelo.py +60 -79
creacion_modelo.py
CHANGED
|
@@ -1,79 +1,60 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from tensorflow.keras.preprocessing import image
|
| 3 |
-
import numpy as np
|
| 4 |
-
from sklearn.model_selection import train_test_split
|
| 5 |
-
from tensorflow.keras.models import Sequential
|
| 6 |
-
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
|
| 7 |
-
from tensorflow.keras.optimizers import Adam
|
| 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 |
-
MaxPooling2D(2, 2),
|
| 62 |
-
|
| 63 |
-
# Aplanar las salidas para conectarlas a una capa densa
|
| 64 |
-
Flatten(),
|
| 65 |
-
|
| 66 |
-
# Capa densa (fully connected)
|
| 67 |
-
Dense(512, activation='relu'),
|
| 68 |
-
Dropout(0.5), # Dropout para evitar sobreajuste
|
| 69 |
-
Dense(1, activation='sigmoid') # Salida binaria: gato o perro
|
| 70 |
-
])
|
| 71 |
-
|
| 72 |
-
# Compilar el modelo
|
| 73 |
-
model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])
|
| 74 |
-
|
| 75 |
-
# Entrenar el modelo
|
| 76 |
-
model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_val, y_val))
|
| 77 |
-
|
| 78 |
-
# Guardar el modelo entrenado
|
| 79 |
-
model.save('dogs_vs_cats_cnn.h5')
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from tensorflow.keras.preprocessing import image
|
| 3 |
+
import numpy as np
|
| 4 |
+
from sklearn.model_selection import train_test_split
|
| 5 |
+
from tensorflow.keras.models import Sequential
|
| 6 |
+
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
|
| 7 |
+
from tensorflow.keras.optimizers import Adam
|
| 8 |
+
|
| 9 |
+
dataset_path = 'train'
|
| 10 |
+
|
| 11 |
+
def load_images(dataset_path):
|
| 12 |
+
images = []
|
| 13 |
+
labels = []
|
| 14 |
+
|
| 15 |
+
for filename in os.listdir(dataset_path):
|
| 16 |
+
img_path = os.path.join(dataset_path, filename)
|
| 17 |
+
|
| 18 |
+
if filename.lower().endswith(('jpg', 'jpeg', 'png')):
|
| 19 |
+
img = image.load_img(img_path, target_size=(150, 150))
|
| 20 |
+
img_array = image.img_to_array(img) / 255.0
|
| 21 |
+
images.append(img_array)
|
| 22 |
+
|
| 23 |
+
if 'cat' in filename.lower():
|
| 24 |
+
labels.append(0)
|
| 25 |
+
elif 'dog' in filename.lower():
|
| 26 |
+
labels.append(1)
|
| 27 |
+
|
| 28 |
+
images = np.array(images)
|
| 29 |
+
labels = np.array(labels)
|
| 30 |
+
return images, labels
|
| 31 |
+
|
| 32 |
+
images, labels = load_images(dataset_path)
|
| 33 |
+
|
| 34 |
+
X_train, X_val, y_train, y_val = train_test_split(images, labels, test_size=0.2, random_state=42)
|
| 35 |
+
|
| 36 |
+
print(f"Tama帽o del conjunto de entrenamiento: {X_train.shape}")
|
| 37 |
+
print(f"Tama帽o del conjunto de validaci贸n: {X_val.shape}")
|
| 38 |
+
|
| 39 |
+
model = Sequential([
|
| 40 |
+
Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)),
|
| 41 |
+
MaxPooling2D(2, 2),
|
| 42 |
+
|
| 43 |
+
Conv2D(64, (3, 3), activation='relu'),
|
| 44 |
+
MaxPooling2D(2, 2),
|
| 45 |
+
|
| 46 |
+
Conv2D(128, (3, 3), activation='relu'),
|
| 47 |
+
MaxPooling2D(2, 2),
|
| 48 |
+
|
| 49 |
+
Flatten(),
|
| 50 |
+
|
| 51 |
+
Dense(512, activation='relu'),
|
| 52 |
+
Dropout(0.5),
|
| 53 |
+
Dense(1, activation='sigmoid')
|
| 54 |
+
])
|
| 55 |
+
|
| 56 |
+
model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])
|
| 57 |
+
|
| 58 |
+
model.fit(X_train, y_train, epochs=10, batch_size=32, validation_data=(X_val, y_val))
|
| 59 |
+
|
| 60 |
+
model.save('dogs_vs_cats_cnn.h5')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|