-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNN_Adam.py
138 lines (120 loc) · 4.41 KB
/
CNN_Adam.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
import numpy as np
import pandas as pd
import os
import tensorflow as tf
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import precision_score, recall_score, f1_score
from sklearn.model_selection import train_test_split
import datetime
import matplotlib.pyplot as plt
# Define dataset directory
dataset_dir = "bird_classification"
# Split data into train, test, and validation sets
train_dir = os.path.join(dataset_dir, "train")
test_dir = os.path.join(dataset_dir, "test")
valid_dir = os.path.join(dataset_dir, "valid")
# Define ImageDataGenerator for data augmentation
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=20,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1,
zoom_range=0.1,
horizontal_flip=True,
fill_mode='nearest'
)
test_datagen = ImageDataGenerator(rescale=1./255)
# Define batch size
batch_size = 32
# Create train, test, and validation data generators
train_generator = train_datagen.flow_from_directory(
train_dir,
target_size=(224, 224),
batch_size=batch_size,
class_mode="categorical"
)
test_generator = test_datagen.flow_from_directory(
test_dir,
target_size=(224, 224),
batch_size=batch_size,
class_mode="categorical",
shuffle=False
)
valid_generator = test_datagen.flow_from_directory(
valid_dir,
target_size=(224, 224),
batch_size=batch_size,
class_mode="categorical",
shuffle=False
)
# Verify GPU availability
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if not physical_devices:
print("GPU is not available. Make sure you have installed the necessary CUDA libraries.")
else:
tf.config.experimental.set_memory_growth(physical_devices[0], True)
# Define the CNN model
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 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.Conv2D(128, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(525, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Define callbacks
log_dir = "logs/cnn_bird_model/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
# Add ModelCheckpoint callback to save the best model
checkpoint_filepath = 'cnn_adam.h5'
model_checkpoint_callback = tf.keras.callbacks.ModelCheckpoint(
filepath=checkpoint_filepath,
save_best_only=True,
monitor='val_accuracy',
mode='max',
verbose=1
)
# Train the model
history = model.fit(
train_generator,
steps_per_epoch=train_generator.samples // batch_size,
epochs=100,
validation_data=valid_generator,
validation_steps=valid_generator.samples // batch_size,
callbacks=[tensorboard_callback]
)
# Load the best model
model = tf.keras.models.load_model(checkpoint_filepath)
# Evaluate the model
test_loss, test_accuracy = model.evaluate(test_generator, steps=test_generator.samples // batch_size)
print("Test Accuracy:", test_accuracy)
# Calculate precision, recall, and F1-score
y_pred = model.predict(test_generator, steps=test_generator.samples // batch_size)
y_pred_classes = np.argmax(y_pred, axis=1)
y_true = test_generator.classes
precision = precision_score(y_true, y_pred_classes, average='macro')
recall = recall_score(y_true, y_pred_classes, average='macro')
f1 = f1_score(y_true, y_pred_classes, average='macro')
print("Precision:", precision)
print("Recall:", recall)
print("F1-Score:", f1)
# Plot accuracy and loss
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Accuracy / Loss')
plt.title('Model Accuracy and Loss')
plt.legend()
plt.show()