-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnn_classifier.py
More file actions
67 lines (55 loc) · 2 KB
/
Copy pathcnn_classifier.py
File metadata and controls
67 lines (55 loc) · 2 KB
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
import tensorflow as tf
import tensorflow_datasets as tfds
from sklearn.metrics import classification_report, accuracy_score, f1_score, confusion_matrix
import numpy as np
# Configuración
IMG_SIZE = 128
BATCH_SIZE = 32
EPOCHS = 5
NUM_CLASSES = 3
# Función para preprocesar imágenes
def preprocess(image, label):
image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
image = tf.cast(image, tf.float32) / 255.0
return image, label
# Cargar dataset rock_paper_scissors
(train_ds, test_ds), ds_info = tfds.load(
'rock_paper_scissors',
split=['train', 'test'],
as_supervised=True,
with_info=True
)
# Preprocesar
train_ds = train_ds.map(preprocess).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)
test_ds = test_ds.map(preprocess).batch(BATCH_SIZE).prefetch(tf.data.AUTOTUNE)
# Construir la red convolucional
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(IMG_SIZE, IMG_SIZE, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(NUM_CLASSES, activation='softmax')
])
# Compilar modelo
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Entrenar
model.fit(train_ds, epochs=EPOCHS, validation_data=test_ds)
# Evaluación con sklearn
X_test, y_test = [], []
for img, label in tfds.as_numpy(test_ds.unbatch()):
X_test.append(img)
y_test.append(label)
X_test = np.stack(X_test)
y_test = np.array(y_test)
y_pred_probs = model.predict(X_test)
y_pred = np.argmax(y_pred_probs, axis=1)
# Métricas
print("Accuracy:", accuracy_score(y_test, y_pred))
print("F1 Score (macro):", f1_score(y_test, y_pred, average='macro'))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))
print("Classification Report:\n", classification_report(y_test, y_pred))