Вот моя модель:
X, Y_segmentation, Y_classification, X_filenames, Y_filenames = shuffle(
np.array(X), np.array(Y_segmentation), np.array(Y_classification),
X_filenames, Y_filenames, random_state=42
)
# Train-test split
X_train, X_val, Y_seg_train, Y_seg_val, Y_cls_train, Y_cls_val, X_train_filenames, X_val_filenames, Y_train_filenames, Y_val_filenames = train_test_split(
X, Y_segmentation, Y_classification, X_filenames, Y_filenames, test_size=0.2, random_state=42
)
def build_unet_with_resnet50_dual_output(input_shape=(128, 128, 3)):
inputs = Input(shape=input_shape)
base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=inputs)
# Encoder - skip connections
skip1 = base_model.get_layer("conv1_relu").output # 64x64
skip2 = base_model.get_layer("conv2_block3_out").output # 32x32
skip3 = base_model.get_layer("conv3_block4_out").output # 16x16
skip4 = base_model.get_layer("conv4_block6_out").output # 8x8
encoder_output = base_model.get_layer("conv5_block3_out").output # 4x4
# Decoder for segmentation
x = UpSampling2D((2, 2))(encoder_output)
x = Concatenate()([x, skip4])
x = Conv2D(256, (3, 3), activation="relu", padding="same")(x)
x = UpSampling2D((2, 2))(x)
x = Concatenate()([x, skip3])
x = Conv2D(128, (3, 3), activation="relu", padding="same")(x)
x = UpSampling2D((2, 2))(x)
x = Concatenate()([x, skip2])
x = Conv2D(64, (3, 3), activation="relu", padding="same")(x)
x = UpSampling2D((2, 2))(x)
x = Concatenate()([x, skip1])
x = Conv2D(32, (3, 3), activation="relu", padding="same")(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation="relu", padding="same")(x)
segmentation_output = Conv2D(1, (1, 1), activation="sigmoid", padding="same", name="segmentation_output")(x)
# Classification head for binary output
y = GlobalAveragePooling2D()(encoder_output) # Pool the encoder output
classification_output = Dense(1, activation="sigmoid", name="classification_output")(y) # Output 2
# Final model with dual output
final_model = Model(inputs, [segmentation_output, classification_output])
return final_model
компиляция:
# Compile and train the model
model = build_unet_with_resnet50_dual_output(input_shape=(128, 128, 3))
optimizer = Adam(learning_rate=1e-4)
# Compile the model
model.compile(
optimizer=optimizer,
loss={
"segmentation_output": "binary_crossentropy", # For segmentation masks
"classification_output": "binary_crossentropy" # For binary classification
},
metrics={
"segmentation_output": [ # Metrics for segmentation
"accuracy",
tf.keras.metrics.Precision(name="precision_segmentation"),
tf.keras.metrics.Recall(name="recall_segmentation"),
f1_score # F1-score for segmentation
],
"classification_output": [ # Metrics for classification
"accuracy",
tf.keras.metrics.Precision(name="precision_classification"),
tf.keras.metrics.Recall(name="recall_classification"),
f1_score # F1-score for classification
]
}
)
######################
early_stopping = EarlyStopping(
monitor='val_loss',
patience=5,
restore_best_weights=True
)
checkpoint = ModelCheckpoint(
filepath=f'{image_pre_processing}trained_models/best_model_{current_date}.keras', # Use .keras extension
monitor='val_loss', # Monitor the validation loss
save_best_only=True, # Save only the best model
verbose=1 # Print a message when a model is saved
)
подгонка:
Y_seg_train = np.expand_dims(Y_seg_train, axis=-1) # Ensure shape (batch_size, 128, 128, 1)
Y_seg_val = np.expand_dims(Y_seg_val, axis=-1)
Y_cls_train = np.reshape(Y_cls_train, (-1, 1)) # Ensure shape (batch_size, 1)
Y_cls_val = np.reshape(Y_cls_val, (-1, 1))
print(f"X_train shape: {X_train.shape}")
print(f"Y_seg_train shape: {Y_seg_train.shape}")
print(f"Y_cls_train shape: {Y_cls_train.shape}")
history = model.fit(
X_train,
{"segmentation_output": Y_seg_train, "classification_output": Y_cls_train},
validation_data=(X_val, {"segmentation_output": Y_seg_val, "classification_output": Y_cls_val}),
epochs=30,
batch_size=16,
callbacks=[checkpoint, early_stopping],
verbose=2
)
# Save the model
model_name = f'{image_pre_processing}unet_with_resnet50_model_{current_date}.h5'
результат:
Selected image pre-processing method:
X_train shape: (10048, 128, 128, 3)
Y_seg_train shape: (10048, 128, 128, 1, 1)
Y_cls_train shape: (10048, 1)
Epoch 1/30
Traceback (most recent call last):
File "/Users/xxx/python/.venv/lib/python3.9/site-packages/keras/src/utils/traceback_utils.py", line 122, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/Users/xxx/python/.venv/lib/python3.9/site-packages/keras/src/backend/tensorflow/nn.py", line 694, in binary_crossentropy
raise ValueError(
ValueError: Arguments `target` and `output` must have the same rank (ndim). Received: target.shape=(16, 1), output.shape=(16, 128, 128, 1)
python-BaseException
Как решить эту проблему?
Я проверил фигуры данных:
X_train shape: (10048, 128, 128, 3)
Y_seg_train shape: (10048, 128, 128, 1, 1)
Y_cls_train shape: (10048, 1)
но
я до сих пор не могу понять, как это исправить:
ValueError: Arguments `target` and `output` must have the same rank (ndim). Received: target.shape=(16, 1), output.shape=(16, 128, 128, 1)
Подробнее здесь: https://stackoverflow.com/questions/792 ... he-same-ra
Модель Keras с несколькими выходами. Аргументы «target» и «output» должны иметь одинаковый ранг (ndim). ⇐ Python
Программы на Python
1733338424
Anonymous
Вот моя модель:
X, Y_segmentation, Y_classification, X_filenames, Y_filenames = shuffle(
np.array(X), np.array(Y_segmentation), np.array(Y_classification),
X_filenames, Y_filenames, random_state=42
)
# Train-test split
X_train, X_val, Y_seg_train, Y_seg_val, Y_cls_train, Y_cls_val, X_train_filenames, X_val_filenames, Y_train_filenames, Y_val_filenames = train_test_split(
X, Y_segmentation, Y_classification, X_filenames, Y_filenames, test_size=0.2, random_state=42
)
def build_unet_with_resnet50_dual_output(input_shape=(128, 128, 3)):
inputs = Input(shape=input_shape)
base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=inputs)
# Encoder - skip connections
skip1 = base_model.get_layer("conv1_relu").output # 64x64
skip2 = base_model.get_layer("conv2_block3_out").output # 32x32
skip3 = base_model.get_layer("conv3_block4_out").output # 16x16
skip4 = base_model.get_layer("conv4_block6_out").output # 8x8
encoder_output = base_model.get_layer("conv5_block3_out").output # 4x4
# Decoder for segmentation
x = UpSampling2D((2, 2))(encoder_output)
x = Concatenate()([x, skip4])
x = Conv2D(256, (3, 3), activation="relu", padding="same")(x)
x = UpSampling2D((2, 2))(x)
x = Concatenate()([x, skip3])
x = Conv2D(128, (3, 3), activation="relu", padding="same")(x)
x = UpSampling2D((2, 2))(x)
x = Concatenate()([x, skip2])
x = Conv2D(64, (3, 3), activation="relu", padding="same")(x)
x = UpSampling2D((2, 2))(x)
x = Concatenate()([x, skip1])
x = Conv2D(32, (3, 3), activation="relu", padding="same")(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation="relu", padding="same")(x)
segmentation_output = Conv2D(1, (1, 1), activation="sigmoid", padding="same", name="segmentation_output")(x)
# Classification head for binary output
y = GlobalAveragePooling2D()(encoder_output) # Pool the encoder output
classification_output = Dense(1, activation="sigmoid", name="classification_output")(y) # Output 2
# Final model with dual output
final_model = Model(inputs, [segmentation_output, classification_output])
return final_model
компиляция:
# Compile and train the model
model = build_unet_with_resnet50_dual_output(input_shape=(128, 128, 3))
optimizer = Adam(learning_rate=1e-4)
# Compile the model
model.compile(
optimizer=optimizer,
loss={
"segmentation_output": "binary_crossentropy", # For segmentation masks
"classification_output": "binary_crossentropy" # For binary classification
},
metrics={
"segmentation_output": [ # Metrics for segmentation
"accuracy",
tf.keras.metrics.Precision(name="precision_segmentation"),
tf.keras.metrics.Recall(name="recall_segmentation"),
f1_score # F1-score for segmentation
],
"classification_output": [ # Metrics for classification
"accuracy",
tf.keras.metrics.Precision(name="precision_classification"),
tf.keras.metrics.Recall(name="recall_classification"),
f1_score # F1-score for classification
]
}
)
######################
early_stopping = EarlyStopping(
monitor='val_loss',
patience=5,
restore_best_weights=True
)
checkpoint = ModelCheckpoint(
filepath=f'{image_pre_processing}trained_models/best_model_{current_date}.keras', # Use .keras extension
monitor='val_loss', # Monitor the validation loss
save_best_only=True, # Save only the best model
verbose=1 # Print a message when a model is saved
)
подгонка:
Y_seg_train = np.expand_dims(Y_seg_train, axis=-1) # Ensure shape (batch_size, 128, 128, 1)
Y_seg_val = np.expand_dims(Y_seg_val, axis=-1)
Y_cls_train = np.reshape(Y_cls_train, (-1, 1)) # Ensure shape (batch_size, 1)
Y_cls_val = np.reshape(Y_cls_val, (-1, 1))
print(f"X_train shape: {X_train.shape}")
print(f"Y_seg_train shape: {Y_seg_train.shape}")
print(f"Y_cls_train shape: {Y_cls_train.shape}")
history = model.fit(
X_train,
{"segmentation_output": Y_seg_train, "classification_output": Y_cls_train},
validation_data=(X_val, {"segmentation_output": Y_seg_val, "classification_output": Y_cls_val}),
epochs=30,
batch_size=16,
callbacks=[checkpoint, early_stopping],
verbose=2
)
# Save the model
model_name = f'{image_pre_processing}unet_with_resnet50_model_{current_date}.h5'
результат:
Selected image pre-processing method:
X_train shape: (10048, 128, 128, 3)
Y_seg_train shape: (10048, 128, 128, 1, 1)
Y_cls_train shape: (10048, 1)
Epoch 1/30
Traceback (most recent call last):
File "/Users/xxx/python/.venv/lib/python3.9/site-packages/keras/src/utils/traceback_utils.py", line 122, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/Users/xxx/python/.venv/lib/python3.9/site-packages/keras/src/backend/tensorflow/nn.py", line 694, in binary_crossentropy
raise ValueError(
ValueError: Arguments `target` and `output` must have the same rank (ndim). Received: target.shape=(16, 1), output.shape=(16, 128, 128, 1)
python-BaseException
Как решить эту проблему?
Я проверил фигуры данных:
X_train shape: (10048, 128, 128, 3)
Y_seg_train shape: (10048, 128, 128, 1, 1)
Y_cls_train shape: (10048, 1)
но
я до сих пор не могу понять, как это исправить:
ValueError: Arguments `target` and `output` must have the same rank (ndim). Received: target.shape=(16, 1), output.shape=(16, 128, 128, 1)
Подробнее здесь: [url]https://stackoverflow.com/questions/79252037/multi-output-keras-model-arguments-target-and-output-must-have-the-same-ra[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия