Upload model.py
Browse files
model.py
CHANGED
@@ -1,19 +1,16 @@
|
|
1 |
|
2 |
-
|
3 |
-
|
4 |
import tensorflow as tf
|
5 |
from tensorflow.keras import layers, models, callbacks
|
6 |
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
7 |
import numpy as np
|
8 |
import matplotlib.pyplot as plt
|
9 |
import datetime
|
10 |
-
from sklearn.metrics import classification_report, confusion_matrix
|
11 |
-
import seaborn as sns
|
12 |
import os
|
13 |
import zipfile
|
14 |
from google.colab import files
|
15 |
-
from
|
16 |
-
|
|
|
17 |
|
18 |
print("TensorFlow version:", tf.__version__)
|
19 |
|
@@ -21,30 +18,16 @@ print("TensorFlow version:", tf.__version__)
|
|
21 |
uploaded = files.upload()
|
22 |
zip_filename = list(uploaded.keys())[0]
|
23 |
|
24 |
-
with zipfile.ZipFile(zip_filename, 'r') as zip_ref:
|
25 |
-
zip_ref.extractall('extracted_dataset')
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
def organize_dataset(input_dir, output_dir):
|
31 |
-
|
32 |
-
os.makedirs(os.path.join(output_dir, 'cat'), exist_ok=True)
|
33 |
-
os.makedirs(os.path.join(output_dir, 'dog'), exist_ok=True)
|
34 |
-
|
35 |
-
|
36 |
-
for file in Path(input_dir).glob('cat.*.jpg'):
|
37 |
-
move(str(file), os.path.join(output_dir, 'cat', file.name))
|
38 |
-
|
39 |
-
|
40 |
-
for file in Path(input_dir).glob('dog.*.jpg'):
|
41 |
-
move(str(file), os.path.join(output_dir, 'dog', file.name))
|
42 |
|
|
|
|
|
|
|
43 |
|
44 |
-
input_path = 'extracted_dataset/custom_dataset/train'
|
45 |
-
output_path = 'organized_dataset/train'
|
46 |
-
organize_dataset(input_path, output_path)
|
47 |
|
|
|
|
|
|
|
|
|
48 |
|
49 |
|
50 |
IMG_SIZE = (150, 150)
|
@@ -53,18 +36,20 @@ BATCH_SIZE = 32
|
|
53 |
|
54 |
train_datagen = ImageDataGenerator(
|
55 |
rescale=1./255,
|
56 |
-
rotation_range=
|
57 |
-
width_shift_range=0.
|
58 |
-
height_shift_range=0.
|
59 |
-
shear_range=0.
|
60 |
-
zoom_range=0.
|
61 |
horizontal_flip=True,
|
62 |
-
|
|
|
|
|
|
|
63 |
)
|
64 |
|
65 |
-
|
66 |
train_generator = train_datagen.flow_from_directory(
|
67 |
-
'
|
68 |
target_size=IMG_SIZE,
|
69 |
batch_size=BATCH_SIZE,
|
70 |
class_mode='binary',
|
@@ -72,9 +57,8 @@ train_generator = train_datagen.flow_from_directory(
|
|
72 |
shuffle=True
|
73 |
)
|
74 |
|
75 |
-
|
76 |
validation_generator = train_datagen.flow_from_directory(
|
77 |
-
'
|
78 |
target_size=IMG_SIZE,
|
79 |
batch_size=BATCH_SIZE,
|
80 |
class_mode='binary',
|
@@ -82,86 +66,147 @@ validation_generator = train_datagen.flow_from_directory(
|
|
82 |
shuffle=True
|
83 |
)
|
84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
class_names = list(train_generator.class_indices.keys())
|
86 |
print("\nDetected classes:", class_names)
|
87 |
-
print("
|
88 |
-
print("
|
89 |
-
|
90 |
-
|
91 |
-
plt.figure(figsize=(12, 9))
|
92 |
-
for i in range(9):
|
93 |
-
img, label = next(train_generator)
|
94 |
-
plt.subplot(3, 3, i+1)
|
95 |
-
plt.imshow(img[i])
|
96 |
-
plt.title(class_names[int(label[i])])
|
97 |
-
plt.axis('off')
|
98 |
-
plt.suptitle("Sample Training Images")
|
99 |
-
plt.show()
|
100 |
|
101 |
|
102 |
-
def
|
103 |
model = models.Sequential([
|
104 |
-
|
|
|
|
|
|
|
|
|
105 |
layers.MaxPooling2D((2,2)),
|
|
|
106 |
|
107 |
-
|
|
|
|
|
|
|
|
|
108 |
layers.MaxPooling2D((2,2)),
|
|
|
|
|
109 |
|
110 |
-
layers.Conv2D(
|
|
|
|
|
|
|
111 |
layers.MaxPooling2D((2,2)),
|
|
|
112 |
|
|
|
113 |
layers.Flatten(),
|
114 |
layers.Dense(512, activation='relu'),
|
|
|
115 |
layers.Dropout(0.5),
|
116 |
-
layers.Dense(1, activation='sigmoid')
|
117 |
])
|
118 |
|
|
|
|
|
119 |
model.compile(
|
120 |
-
optimizer=
|
121 |
loss='binary_crossentropy',
|
122 |
-
metrics=['accuracy']
|
123 |
)
|
124 |
return model
|
125 |
|
126 |
-
model =
|
127 |
model.summary()
|
128 |
|
129 |
|
130 |
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
|
131 |
|
|
|
132 |
callbacks = [
|
133 |
-
callbacks.EarlyStopping(patience=5, restore_best_weights=True),
|
134 |
-
callbacks.ModelCheckpoint('best_model.h5', save_best_only=True),
|
135 |
callbacks.TensorBoard(log_dir=log_dir),
|
136 |
-
callbacks.ReduceLROnPlateau(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
]
|
138 |
|
|
|
139 |
history = model.fit(
|
140 |
train_generator,
|
141 |
steps_per_epoch=train_generator.samples // BATCH_SIZE,
|
142 |
epochs=30,
|
143 |
validation_data=validation_generator,
|
144 |
validation_steps=validation_generator.samples // BATCH_SIZE,
|
145 |
-
callbacks=callbacks
|
|
|
|
|
146 |
)
|
147 |
|
148 |
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
plt.subplot(1, 2, 1)
|
151 |
-
plt.plot(history.history['accuracy'], label='Train')
|
152 |
-
plt.plot(history.history['val_accuracy'], label='Validation')
|
153 |
-
plt.title('Accuracy')
|
|
|
|
|
154 |
plt.legend()
|
155 |
|
156 |
plt.subplot(1, 2, 2)
|
157 |
-
plt.plot(history.history['loss'], label='Train')
|
158 |
-
plt.plot(history.history['val_loss'], label='Validation')
|
159 |
-
plt.title('Loss')
|
|
|
|
|
160 |
plt.legend()
|
161 |
plt.show()
|
162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
-
|
|
|
|
|
165 |
|
166 |
|
167 |
converter = tf.lite.TFLiteConverter.from_keras_model(model)
|
@@ -169,4 +214,7 @@ tflite_model = converter.convert()
|
|
169 |
with open('cat_dog.tflite', 'wb') as f:
|
170 |
f.write(tflite_model)
|
171 |
|
172 |
-
print("\
|
|
|
|
|
|
|
|
1 |
|
|
|
|
|
2 |
import tensorflow as tf
|
3 |
from tensorflow.keras import layers, models, callbacks
|
4 |
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
5 |
import numpy as np
|
6 |
import matplotlib.pyplot as plt
|
7 |
import datetime
|
|
|
|
|
8 |
import os
|
9 |
import zipfile
|
10 |
from google.colab import files
|
11 |
+
from sklearn.metrics import classification_report, confusion_matrix
|
12 |
+
import seaborn as sns
|
13 |
+
from sklearn.utils import class_weight
|
14 |
|
15 |
print("TensorFlow version:", tf.__version__)
|
16 |
|
|
|
18 |
uploaded = files.upload()
|
19 |
zip_filename = list(uploaded.keys())[0]
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
+
extract_path = 'dataset'
|
23 |
+
with zipfile.ZipFile(zip_filename, 'r') as zip_ref:
|
24 |
+
zip_ref.extractall(extract_path)
|
25 |
|
|
|
|
|
|
|
26 |
|
27 |
+
print("\nExtracted files:")
|
28 |
+
!ls {extract_path}
|
29 |
+
print("\nTrain folder contents:")
|
30 |
+
!ls {extract_path}/train
|
31 |
|
32 |
|
33 |
IMG_SIZE = (150, 150)
|
|
|
36 |
|
37 |
train_datagen = ImageDataGenerator(
|
38 |
rescale=1./255,
|
39 |
+
rotation_range=40,
|
40 |
+
width_shift_range=0.3,
|
41 |
+
height_shift_range=0.3,
|
42 |
+
shear_range=0.3,
|
43 |
+
zoom_range=0.3,
|
44 |
horizontal_flip=True,
|
45 |
+
vertical_flip=True,
|
46 |
+
brightness_range=[0.8, 1.2],
|
47 |
+
validation_split=0.2,
|
48 |
+
fill_mode='nearest'
|
49 |
)
|
50 |
|
|
|
51 |
train_generator = train_datagen.flow_from_directory(
|
52 |
+
os.path.join(extract_path, 'train'),
|
53 |
target_size=IMG_SIZE,
|
54 |
batch_size=BATCH_SIZE,
|
55 |
class_mode='binary',
|
|
|
57 |
shuffle=True
|
58 |
)
|
59 |
|
|
|
60 |
validation_generator = train_datagen.flow_from_directory(
|
61 |
+
os.path.join(extract_path, 'train'),
|
62 |
target_size=IMG_SIZE,
|
63 |
batch_size=BATCH_SIZE,
|
64 |
class_mode='binary',
|
|
|
66 |
shuffle=True
|
67 |
)
|
68 |
|
69 |
+
|
70 |
+
class_weights = class_weight.compute_class_weight(
|
71 |
+
'balanced',
|
72 |
+
classes=np.unique(train_generator.classes),
|
73 |
+
y=train_generator.classes
|
74 |
+
)
|
75 |
+
class_weights = dict(enumerate(class_weights))
|
76 |
+
|
77 |
class_names = list(train_generator.class_indices.keys())
|
78 |
print("\nDetected classes:", class_names)
|
79 |
+
print("Training samples:", train_generator.samples)
|
80 |
+
print("Validation samples:", validation_generator.samples)
|
81 |
+
print("Class weights:", class_weights)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
|
84 |
+
def build_enhanced_model(input_shape):
|
85 |
model = models.Sequential([
|
86 |
+
|
87 |
+
layers.Conv2D(64, (3,3), activation='relu', padding='same', input_shape=input_shape),
|
88 |
+
layers.BatchNormalization(),
|
89 |
+
layers.Conv2D(64, (3,3), activation='relu', padding='same'),
|
90 |
+
layers.BatchNormalization(),
|
91 |
layers.MaxPooling2D((2,2)),
|
92 |
+
layers.Dropout(0.3),
|
93 |
|
94 |
+
|
95 |
+
layers.Conv2D(128, (3,3), activation='relu', padding='same'),
|
96 |
+
layers.BatchNormalization(),
|
97 |
+
layers.Conv2D(128, (3,3), activation='relu', padding='same'),
|
98 |
+
layers.BatchNormalization(),
|
99 |
layers.MaxPooling2D((2,2)),
|
100 |
+
layers.Dropout(0.3),
|
101 |
+
|
102 |
|
103 |
+
layers.Conv2D(256, (3,3), activation='relu', padding='same'),
|
104 |
+
layers.BatchNormalization(),
|
105 |
+
layers.Conv2D(256, (3,3), activation='relu', padding='same'),
|
106 |
+
layers.BatchNormalization(),
|
107 |
layers.MaxPooling2D((2,2)),
|
108 |
+
layers.Dropout(0.4),
|
109 |
|
110 |
+
|
111 |
layers.Flatten(),
|
112 |
layers.Dense(512, activation='relu'),
|
113 |
+
layers.BatchNormalization(),
|
114 |
layers.Dropout(0.5),
|
115 |
+
layers.Dense(1, activation='sigmoid')
|
116 |
])
|
117 |
|
118 |
+
optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001)
|
119 |
+
|
120 |
model.compile(
|
121 |
+
optimizer=optimizer,
|
122 |
loss='binary_crossentropy',
|
123 |
+
metrics=['accuracy', tf.keras.metrics.AUC(name='auc')]
|
124 |
)
|
125 |
return model
|
126 |
|
127 |
+
model = build_enhanced_model(input_shape=(IMG_SIZE[0], IMG_SIZE[1], 3))
|
128 |
model.summary()
|
129 |
|
130 |
|
131 |
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
|
132 |
|
133 |
+
|
134 |
callbacks = [
|
|
|
|
|
135 |
callbacks.TensorBoard(log_dir=log_dir),
|
136 |
+
callbacks.ReduceLROnPlateau(
|
137 |
+
monitor='val_loss',
|
138 |
+
factor=0.5,
|
139 |
+
patience=3,
|
140 |
+
min_lr=1e-7,
|
141 |
+
verbose=1
|
142 |
+
),
|
143 |
+
callbacks.ModelCheckpoint(
|
144 |
+
'best_model.keras',
|
145 |
+
monitor='val_auc',
|
146 |
+
mode='max',
|
147 |
+
save_best_only=True,
|
148 |
+
save_weights_only=False,
|
149 |
+
verbose=1
|
150 |
+
)
|
151 |
]
|
152 |
|
153 |
+
print("\nStarting training for full 30 epochs...")
|
154 |
history = model.fit(
|
155 |
train_generator,
|
156 |
steps_per_epoch=train_generator.samples // BATCH_SIZE,
|
157 |
epochs=30,
|
158 |
validation_data=validation_generator,
|
159 |
validation_steps=validation_generator.samples // BATCH_SIZE,
|
160 |
+
callbacks=callbacks,
|
161 |
+
class_weight=class_weights,
|
162 |
+
verbose=1
|
163 |
)
|
164 |
|
165 |
|
166 |
+
print("\nTraining complete. Saving final model...")
|
167 |
+
|
168 |
+
model.save('final_model.keras')
|
169 |
+
|
170 |
+
|
171 |
+
history_df = pd.DataFrame(history.history)
|
172 |
+
history_df.to_csv('training_history.csv', index=False)
|
173 |
+
|
174 |
+
|
175 |
+
plt.figure(figsize=(12, 5))
|
176 |
plt.subplot(1, 2, 1)
|
177 |
+
plt.plot(history.history['accuracy'], label='Train Accuracy')
|
178 |
+
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
|
179 |
+
plt.title('Model Accuracy')
|
180 |
+
plt.ylabel('Accuracy')
|
181 |
+
plt.xlabel('Epoch')
|
182 |
plt.legend()
|
183 |
|
184 |
plt.subplot(1, 2, 2)
|
185 |
+
plt.plot(history.history['loss'], label='Train Loss')
|
186 |
+
plt.plot(history.history['val_loss'], label='Validation Loss')
|
187 |
+
plt.title('Model Loss')
|
188 |
+
plt.ylabel('Loss')
|
189 |
+
plt.xlabel('Epoch')
|
190 |
plt.legend()
|
191 |
plt.show()
|
192 |
|
193 |
+
val_preds = model.predict(validation_generator)
|
194 |
+
val_preds = (val_preds > 0.5).astype(int)
|
195 |
+
|
196 |
+
|
197 |
+
cm = confusion_matrix(validation_generator.classes, val_preds)
|
198 |
+
plt.figure(figsize=(6, 6))
|
199 |
+
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues',
|
200 |
+
xticklabels=class_names, yticklabels=class_names)
|
201 |
+
plt.title('Confusion Matrix')
|
202 |
+
plt.ylabel('True Label')
|
203 |
+
plt.xlabel('Predicted Label')
|
204 |
+
plt.show()
|
205 |
+
|
206 |
|
207 |
+
print("\nClassification Report:")
|
208 |
+
print(classification_report(validation_generator.classes, val_preds,
|
209 |
+
target_names=class_names))
|
210 |
|
211 |
|
212 |
converter = tf.lite.TFLiteConverter.from_keras_model(model)
|
|
|
214 |
with open('cat_dog.tflite', 'wb') as f:
|
215 |
f.write(tflite_model)
|
216 |
|
217 |
+
print("\nAll models saved successfully:")
|
218 |
+
print("- final_model.keras (model after all epochs)")
|
219 |
+
print("- best_model.keras (best validation AUC model)")
|
220 |
+
print("- cat_dog.tflite (TFLite version)")
|