-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrainEfficientNet.py
More file actions
49 lines (41 loc) · 1.74 KB
/
trainEfficientNet.py
File metadata and controls
49 lines (41 loc) · 1.74 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
import tensorflow as tf
from tensorflow.keras.applications import EfficientNetB0
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.callbacks import ModelCheckpoint
# Ana klasör yolunu belirtin
base_dir = './dataset3'
# Veri setini rastgele karıştırarak ve sonra eğitim ve test setlerine ayırmak için ImageDataGenerator nesnesi oluşturun
datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2)
# Eğitim setini oluşturun
train_generator = datagen.flow_from_directory(
base_dir,
target_size=(240, 320),
batch_size=12,
class_mode='categorical',
subset='training',
shuffle=True
)
# Test setini oluşturun
test_generator = datagen.flow_from_directory(
base_dir,
target_size=(240, 320),
batch_size=12,
class_mode='categorical',
subset='validation',
shuffle=True
)
# EfficientNet modelini yükleyin
base_model = EfficientNetB0(weights='imagenet', include_top=False, input_shape=(240, 320, 3))
# Modeli özelleştirin
model = tf.keras.models.Sequential()
model.add(base_model)
model.add(tf.keras.layers.GlobalAveragePooling2D())
model.add(tf.keras.layers.Dense(len(train_generator.class_indices), activation='softmax'))
# Modeli derleyin
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
early_stopping = EarlyStopping(monitor='val_loss', patience=3)
model_checkpoint = ModelCheckpoint('./model/model_{epoch}_{val_accuracy:.2f}.keras', monitor='val_accuracy', save_best_only=False, save_weights_only=False)
# Modeli eğitin
model.fit(train_generator, validation_data=test_generator, callbacks=[early_stopping,model_checkpoint], epochs=10)
model.save("./model/efficientNet.keras")