-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCats_vs_Dogs_Classifier.py
More file actions
90 lines (70 loc) · 2.48 KB
/
Copy pathCats_vs_Dogs_Classifier.py
File metadata and controls
90 lines (70 loc) · 2.48 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.callbacks import EarlyStopping
import matplotlib.pyplot as plt
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=60,
width_shift_range=0.4,
height_shift_range=0.4,
shear_range=0.4,
zoom_range=0.4,
brightness_range=[0.6, 1.4],
horizontal_flip=True,
fill_mode='nearest',
validation_split=0.2
)
train_generator = train_datagen.flow_from_directory(
'/content/dogs_vs_cats/train',
target_size=(150, 150),
batch_size=32,
class_mode='binary',
subset='training'
)
validation_generator = train_datagen.flow_from_directory(
'/content/dogs_vs_cats/train',
target_size=(150, 150),
batch_size=32,
class_mode='binary',
subset='validation'
)
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(512, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model(inputs=base_model.input, outputs=predictions)
for layer in base_model.layers:
layer.trainable = False
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.0001),
loss='binary_crossentropy',
metrics=['accuracy'])
history = model.fit(
train_generator,
epochs=5,
validation_data=validation_generator
)
for layer in base_model.layers[-4:]:
layer.trainable = True
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=1e-5),
loss='binary_crossentropy',
metrics=['accuracy'])
early_stopping = EarlyStopping(monitor='val_accuracy', patience=3, restore_best_weights=True)
history = model.fit(
train_generator,
epochs=20,
validation_data=validation_generator,
callbacks=[early_stopping]
)
model.save('/content/drive/MyDrive/cats_vs_dogs_model_vgg16_finetuned.keras')
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.savefig('/content/drive/MyDrive/accuracy_plot_vgg16_finetuned.png')
plt.show()