Я работал над моделью CNN обнаружения бинарных объектов, используя Transfer Learning с встроенной моделью Resnet 50. Однако после нескольких раз тренировки более 100 эпох он возвращает координаты 0,0,0,1, что переводит XMIN, YMIN и XMAX = 0 и Y MAX = 300. Входные изображения, которые он при получении имеет форму (размер партии, 300,300,3), этикетки обрабатываются и находятся в форме (размер партии, 4) и нормализованы между 0 и 1, и, наконец, категориальные этикетки имеют форму (размер партии, 2) и одну горячую кодировку. Ниже приведена архитектура модели. < /P>
from tensorflow.keras import layers
from tensorflow.keras import models
from keras.applications.resnet50 import ResNet50
res = ResNet50(weights ='imagenet', include_top = False,
input_shape =(300, 300, 3))
x = res.output
x = layers.MaxPooling2D((2, 2))(x)
# Flatten and Fully connected layers
x = layers.BatchNormalization()(x)
x = layers.Flatten()(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(512, activation='relu')(x)
x = layers.BatchNormalization()(x)
x = layers.Dropout(0.50)(x)
x = layers.Dense(512)(x)
# Output layers with proper names
bbox_output = layers.Dense(4, activation='sigmoid', name='bbox_output')(x) # Bounding box output
class_output = layers.Dense(2, activation='softmax', name='class_output')(x) # Class output
# Define the model
model = models.Model(inputs = res.input, outputs=[bbox_output, class_output])
model.compile(
optimizer='Adam',
loss={'bbox_output': diou_loss, 'class_output': 'categorical_crossentropy'},
metrics={'bbox_output': 'MSE', 'class_output': 'accuracy'})
callbacks = [
keras.callbacks.EarlyStopping(
# Stop training when `val_loss` is no longer improving
monitor='val_bbox_output_loss',
# "no longer improving" being defined as "no better than 1e-2 less"
min_delta=1e-2,
# "no longer improving" being further defined as "for at least 2 epochs"
patience=20,
verbose=1,
)]
history = model.fit(X,
[bbox_labels,class_labels],
epochs=100,batch_size=50,
verbose=1,
validation_data=(X_test,[bbox_labels_test,class_labels_test]),
callbacks=callbacks)
< /code>
Ниже приведена функция потери Diou: < /p>
def diou_loss(y_true, y_pred, epsilon=1e-7):
# Use fixed image dimensions (256x256)
print('yt',y_true)
print('yp',y_pred)
x_min_inter = tf.maximum(y_true[..., 0], y_pred[..., 0])
y_min_inter = tf.maximum(y_true[..., 1], y_pred[..., 1])
x_max_inter = tf.minimum(y_true[..., 2], y_pred[..., 2])
y_max_inter = tf.minimum(y_true[..., 3], y_pred[..., 3])
inter_area = tf.maximum(0.0, x_max_inter - x_min_inter) * tf.maximum(0.0, y_max_inter - y_min_inter)
print('ia',inter_area)
true_area = tf.maximum(0.0, y_true[..., 2] - y_true[..., 0]) * tf.maximum(0.0, y_true[..., 3] - y_true[..., 1])
pred_area = tf.maximum(0.0, y_pred[..., 2] - y_pred[..., 0]) * tf.maximum(0.0, y_pred[..., 3] - y_pred[..., 1])
union_area = true_area + pred_area - inter_area
print('ua',union_area)
iou = inter_area / tf.maximum(union_area, epsilon)
print('iou',iou)
# Calculate the center coordinates of the true and predicted boxes
true_center_x = (y_true[..., 0] + y_true[..., 2]) / 2.0
true_center_y = (y_true[..., 1] + y_true[..., 3]) / 2.0
pred_center_x = (y_pred[..., 0] + y_pred[..., 2]) / 2.0
pred_center_y = (y_pred[..., 1] + y_pred[..., 3]) / 2.0
# Calculate the squared Euclidean distance between the centers
center_distance = (true_center_x - pred_center_x) ** 2 + (true_center_y - pred_center_y) ** 2
print('cd',center_distance)
# Calculate the coordinates of the smallest enclosing box
x_min_enclosing = tf.minimum(y_true[..., 0], y_pred[..., 0])
y_min_enclosing = tf.minimum(y_true[..., 1], y_pred[..., 1])
x_max_enclosing = tf.maximum(y_true[..., 2], y_pred[..., 2])
y_max_enclosing = tf.maximum(y_true[..., 3], y_pred[..., 3])
# Calculate the diagonal length squared of the enclosing box
enclosing_diagonal = (x_max_enclosing - x_min_enclosing) ** 2 + (y_max_enclosing - y_min_enclosing) ** 2
print('ed',enclosing_diagonal)
# Calculate the DIoU
# Return the DIoU loss
return 1.0 - iou + ((center_distance) / tf.maximum(enclosing_diagonal, 1e-7))
< /code>
Я попытался переключить функцию потерь на MSE, но у него все еще есть та же проблема, поэтому я не думаю, что это функция потери. Когда я печатаю bbox_labels, он дает массив с каждой строкой, которая выглядит примерно так:
n0.74666667 0,32333333 0,88333333 0,69]
Подробнее здесь: https://stackoverflow.com/questions/794 ... ema-values
CNN Model Learning неправильно возвращает значения экстремальных ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение