Code: Select all
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import MobileNet
batch_size = 8
num_epochs = 20
num_classes = 4
train_dir = '/Users/borakarakus/Desktop/dataset/Training'
test_dir = '/Users/borakarakus/Desktop/dataset/Testing'
train_datagen = ImageDataGenerator()
test_datagen = ImageDataGenerator()
train_data = train_datagen.flow_from_directory(
train_dir,
target_size=(300, 300),
batch_size=batch_size,
class_mode='categorical',
shuffle=False
)
test_data = test_datagen.flow_from_directory(
test_dir,
target_size=(300, 300),
batch_size=batch_size,
class_mode='categorical',
shuffle=False
)
# Create MobileNet model
model = MobileNet(input_shape=(300, 300, 3),
include_top=False,
weights='/Users/borakarakus/.keras/models/mobilenet_1_0_224_tf_no_top.h5')
# Freeze layers
for layer in model.layers:
layer.trainable = False
# Add new top layers
x = model.output
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Dense(128, activation='relu')(x)
x = tf.keras.layers.Dropout(0.2)(x)
predictions = tf.keras.layers.Dense(num_classes, activation='softmax')(x)
model = tf.keras.models.Model(inputs=model.input, outputs=predictions)
# Compile model
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
# Train model
history = model.fit(train_data,
epochs=20)
# Evaluate model
score = model.evaluate(test_data)
# Print accuracy
print('Test accuracy:', score[1])
model.save('SA_HUS_MobileNetmodel_F1.h5')
< /code>
Mein Code gibt mir 0,90 Testgenauigkeitswert, aber andere Metriken sind so schlecht, bitte helfen Sie < /p>
Test accuracy: 0.909229576587677
Classification Report:
precision recall f1-score support
0 0.24 0.27 0.25 300
1 0.25 0.19 0.22 306
2 0.34 0.36 0.35 405
3 0.22 0.24 0.23 300
accuracy 0.27 1311
macro avg 0.26 0.26 0.26 1311
weighted avg 0.27 0.27 0.27 1311
Accuracy: 0.2707856598016781
Precision (Weighted): 0.27067005240444436``
Recall (Weighted): 0.2707856598016781
F1 Score (Weighted): 0.2693025334531395