import numpy as np import cv2 import matplotlib.pyplot as… · The Chat Archive
Explore / Programming import numpy as np import cv2 import matplotlib.pyplot as plt from sklearn.metrics… Save Share Download ▾ Reportimport numpy as np
import cv2
import matplotlib.pyplot as plt
from sklearn.metrics import mean_absolute_error, mean_squared_error, precision_score, recall_score, roc_curve, auc
from skimage.transform import resize # For resizing the mask
from keras.models import load_model
from keras_preprocessing.image import img_to_array, load_img
from sklearn.metrics import accuracy_score, f1_score
1. 加载u-net 模型
model = load_model(r"C:\Users\jack8\OneDrive\LSTM and ARIMA\LSTM\lstm_model.h5")
2. 定义辅助函数
def image_to_patches(image, patch_size):
patches = []
positions = []
img_height, img_width = image.shape[:2]
patch_height, patch_width = patch_size
for y in range(0, img_height, patch_height):
for x in range(0, img_width, patch_width):
patch = image[y:y+patch_height, x:x+patch_width]
# 如果最后的块尺寸不足,进行填充
if patch.shape[0] != patch_height or patch.shape[1] != patch_width:
pad_height = patch_height - patch.shape[0]
pad_width = patch_width - patch.shape[1]
patch = np.pad(patch, ((0, pad_height), (0, pad_width), (0, 0)), mode='constant')
patches.append(patch)
positions.append((y, x))
return np.array(patches), positions
def patches_to_image(patches, positions, image_shape):
reconstructed_image = np.zeros(image_shape, dtype=patches.dtype)
patch_height, patch_width = patches.shape[1:3]
for patch, (y, x) in zip(patches, positions):
y_end = min(y + patch_height, image_shape[0])
x_end = min(x + patch_width, image_shape[1])
h = y_end - y
w = x_end - x
reconstructed_image[y:y_end, x:x_end] = patch[:h, :w]
return reconstructed_image
3. 加载并预处理输入图像
加载大图
input_img_path = r"C:\Users\jack8\OneDrive\LSTM and ARIMA\LSTM\data\south\Finish\segmentation-vidas\2024.jpg"
new_img = load_img(input_img_path)
new_img_array = img_to_array(new_img) / 255.0
获取原始图像的尺寸
original_height, original_width = new_img_array.shape[:2]
定义模型输入尺寸
patch_size = (640, 640) # 根据您的模型的输入尺寸调整
将大图切割成小块
patches, positions = image_to_patches(new_img_array, patch_size)
4. 对每个小块进行预测
predicted_patches = []
for patch in patches:
patch_input = np.expand_dims(patch, axis=0)
prediction = model.predict(patch_input)
predicted_patches.append(prediction[0])
predicted_patches = np.array(predicted_patches) # Shape: (num_patches, patch_height, patch_width, num_classes)
将预测结果二值化
prediction_binary_patches = (predicted_patches > 0.5).astype(np.uint8)
重建预测的掩膜图
prediction_mask = patches_to_image(prediction_binary_patches, positions, (original_height, original_width, 1))
5. 加载并预处理实际的掩膜图像(mask)
mask_img_path = r"C:\Users\jack8\OneDrive\LSTM and ARIMA\LSTM\data\south\Finish\cover\2024.png"
mask_img = load_img(mask_img_path, color_mode='grayscale')
mask_img_array = img_to_array(mask_img) / 255.0
mask_img_array = mask_img_array[:, :, 0] # 只保留单通道
Assistant2024-10-05 14:25 UTC Copy response
Copy first prompt
Details
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date Oct 5, 2024
Added to archive Nov 16, 2026
ID 01m7h41wnd41ncmdscpm6z4qbhSource & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: f7201db75923c7edf26545891095380b
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
确保实际掩膜的尺寸与预测掩膜一致 if mask_img_array.shape != prediction_mask.shape[:2]:
# 如果尺寸不一致,调整实际掩膜的尺寸
mask_img_array_resized = resize(mask_img_array, prediction_mask.shape[:2], preserve_range=True)
else:
mask_img_array_resized = mask_img_array
为了确保掩膜是二值的,应用阈值 mask_img_array_resized = (mask_img_array_resized > 0.5).astype(np.uint8)
确保预测概率掩膜是连续的概率值 prediction_prob_mask = patches_to_image(predicted_patches, positions, (original_height, original_width, 1))
prediction_prob_mask = prediction_prob_mask[:, :, 0] # 只取单通道
展平成一维数组 y_true_roc = mask_img_array_resized.flatten()
y_scores = prediction_prob_mask.flatten()
检查 y_true 是否为二进制 unique_values = np.unique(y_true_roc)
print("Unique values in y_true_roc:", unique_values)
if not set(unique_values).issubset({0, 1}):
print("Error: y_true_roc contains non-binary values.")
# 可以选择强制二值化
y_true_roc = (y_true_roc > 0.5).astype(int)
6. 应用闭运算
将预测的掩膜图像(prediction_mask)转换为 0 和 255 的图像,用于 OpenCV 函数 prediction_mask_uint8 = (prediction_mask[:, :, 0] * 255).astype(np.uint8)
定义结构元素的大小和形状 kernel_size = 29
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size))
应用闭运算 closed_mask = cv2.morphologyEx(prediction_mask_uint8, cv2.MORPH_CLOSE, kernel)
将处理后的掩膜图像转换回 0 和 1 的二值图像 closed_mask_binary = (closed_mask > 127).astype(np.uint8)
展平成一维数组 true_values = mask_img_array_resized.flatten()
predicted_values = closed_mask_binary.flatten()
检查值范围 print("True values min:", np.min(true_values), "max:", np.max(true_values))
print("Predicted values min:", np.min(predicted_values), "max:", np.max(predicted_values))
二值化处理 true_values = (true_values > 0.5).astype(np.float32)
predicted_values = (predicted_values > 0.5).astype(np.float32)
再次检查值范围 print("After thresholding:")
print("True values min:", np.min(true_values), "max:", np.max(true_values))
print("Predicted values min:", np.min(predicted_values), "max:", np.max(predicted_values))
检查数组形状是否一致 print("True values shape:", true_values.shape)
print("Predicted values shape:", predicted_values.shape)
计算 MAPE def mean_absolute_percentage_error(y_true, y_pred):
# 获取 y_true 不为零的索引
non_zero_indices = y_true != 0
# 避免除以零,忽略 y_true 为零的元素
y_true_non_zero = y_true[non_zero_indices]
y_pred_non_zero = y_pred[non_zero_indices]
if len(y_true_non_zero) == 0:
print("Warning: No non-zero elements in y_true. MAPE is undefined.")
mape = np.nan
else:
mape = np.mean(np.abs((y_true_non_zero - y_pred_non_zero) / y_true_non_zero)) * 100
return mape
mape = mean_absolute_percentage_error(true_values, predicted_values)
print(f"MAPE: {mape}%")
计算 MAE mae = mean_absolute_error(true_values, predicted_values)
print(f"MAE: {mae}")
计算 RMSE rmse = np.sqrt(mean_squared_error(true_values, predicted_values))
print(f"RMSE: {rmse}")
计算 IoU intersection = np.logical_and(true_values, predicted_values).sum()
union = np.logical_or(true_values, predicted_values).sum()
iou_score = intersection / union if union != 0 else 1 # 避免除以零
print("IoU (Jaccard Index):", iou_score)
计算准确率 accuracy = accuracy_score(true_values, predicted_values)
print("Accuracy:", accuracy)
计算 F1 Score f1 = f1_score(true_values, predicted_values, zero_division=0)
print("F1 Score:", f1)
计算 Dice 系数 dice_score = 2 * intersection / (true_values.sum() + predicted_values.sum()) if (true_values.sum() + predicted_values.sum()) != 0 else 1
print("Dice Coefficient:", dice_score)
计算精确率和召回率 precision = precision_score(true_values, predicted_values, zero_division=0)
recall = recall_score(true_values, predicted_values, zero_division=0)
print("Precision:", precision)
print("Recall:", recall)
可视化结果 plt.figure(figsize=(18, 6))
plt.subplot(1, 3, 1)
plt.title("Ground Truth Mask")
plt.imshow(mask_img_array_resized, cmap='gray')
plt.axis('off')
plt.subplot(1, 3, 2)
plt.title("Prediction Mask")
plt.imshow(prediction_mask[:, :, 0], cmap='gray')
plt.axis('off')
plt.subplot(1, 3, 3)
plt.title("Closed Mask Binary")
plt.imshow(closed_mask_binary, cmap='gray')
plt.axis('off')
----------------------- 添加 ROC 曲线的绘制部分 -----------------------
为了绘制 ROC 曲线,使用模型预测的概率值和二值化后的真实标签
y_true_roc 已确保是二进制
检查 y_true_roc 是否为二进制 unique_values = np.unique(y_true_roc)
print("Unique values in y_true_roc after binarization:", unique_values)
if not set(unique_values).issubset({0, 1}):
raise ValueError("y_true_roc 必须是二进制标签 (0 和 1)。请检查掩膜图像的二值化步骤。")
计算 ROC 曲线和 AUC fpr, tpr, thresholds = roc_curve(y_true_roc, y_scores)
roc_auc = auc(fpr, tpr)
print(f"ROC AUC: {roc_auc}")
绘制 ROC 曲线 plt.figure(figsize=(8, 6))
plt.plot(fpr, tpr, color='blue', lw=2, label=f'ROC curve (AUC = {roc_auc:.2f})')
plt.plot([0, 1], [0, 1], color='red', lw=2, linestyle='--', label='Random Guess')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate', fontsize=14)
plt.ylabel('True Positive Rate', fontsize=14)
plt.title('Receiver Operating Characteristic (ROC) Curve', fontsize=16)
plt.legend(loc="lower right", fontsize=12)
plt.grid(True)
plt.show()
##請改成其他的都不要,只需要Closed Mask Binary這張圖,並且Closed Mask Binary需和Ground Truth Mask尺寸大小一樣
好的,以下是简化后的代码,仅保留生成 Closed Mask Binary 的部分,并确保其尺寸与 Ground Truth Mask 一致。该代码将加载模型、处理输入图像、进行预测、应用闭运算,并最终生成与真实掩膜尺寸相同的二值化闭运算掩膜图像。
import numpy as np
import cv2
from skimage.transform import resize # For resizing the mask
from keras.models import load_model
from keras_preprocessing.image import img_to_array, load_img
# 1. 加载模型
model = load_model(r"C:\Users\jack8\OneDrive\LSTM and ARIMA\LSTM\lstm_model.h5")
# 2. 定义辅助函数
def image_to_patches(image, patch_size):
patches = []
positions = []
img_height, img_width = image.shape[:2]
patch_height, patch_width = patch_size
for y in range(0, img_height, patch_height):
for x in range(0, img_width, patch_width):
patch = image[y:y+patch_height, x:x+patch_width]
# 如果最后的块尺寸不足,进行填充
if patch.shape[0] != patch_height or patch.shape[1] != patch_width:
pad_height = patch_height - patch.shape[0]
pad_width = patch_width - patch.shape[1]
patch = np.pad(patch, ((0, pad_height), (0, pad_width), (0, 0)), mode='constant')
patches.append(patch)
positions.append((y, x))
return np.array(patches), positions
def patches_to_image(patches, positions, image_shape):
reconstructed_image = np.zeros(image_shape, dtype=patches.dtype)
patch_height, patch_width = patches.shape[1:3]
for patch, (y, x) in zip(patches, positions):
y_end = min(y + patch_height, image_shape[0])
x_end = min(x + patch_width, image_shape[1])
h = y_end - y
w = x_end - x
reconstructed_image[y:y_end, x:x_end] = patch[:h, :w]
return reconstructed_image
# 3. 加载并预处理输入图像
input_img_path = r"C:\Users\jack8\OneDrive\LSTM and ARIMA\LSTM\data\south\Finish\segmentation-vidas\2024.jpg"
new_img = load_img(input_img_path)
new_img_array = img_to_array(new_img) / 255.0
# 获取原始图像的尺寸
original_height, original_width = new_img_array.shape[:2]
# 定义模型输入尺寸
patch_size = (640, 640) # 根据您的模型的输入尺寸调整
# 将大图切割成小块
patches, positions = image_to_patches(new_img_array, patch_size)
# 4. 对每个小块进行预测
predicted_patches = []
for patch in patches:
patch_input = np.expand_dims(patch, axis=0)
prediction = model.predict(patch_input)
predicted_patches.append(prediction[0])
predicted_patches = np.array(predicted_patches) # Shape: (num_patches, patch_height, patch_width, num_classes)
# 将预测结果二值化
prediction_binary_patches = (predicted_patches > 0.5).astype(np.uint8)
# 重建预测的掩膜图
prediction_mask = patches_to_image(prediction_binary_patches, positions, (original_height, original_width, 1))
# 5. 加载并预处理实际的掩膜图像(mask)
mask_img_path = r"C:\Users\jack8\OneDrive\LSTM and ARIMA\LSTM\data\south\Finish\cover\2024.png"
mask_img = load_img(mask_img_path, color_mode='grayscale')
mask_img_array = img_to_array(mask_img) / 255.0
mask_img_array = mask_img_array[:, :, 0] # 只保留单通道
# 确保实际掩膜的尺寸与预测掩膜一致
if mask_img_array.shape != prediction_mask.shape[:2]:
# 如果尺寸不一致,调整实际掩膜的尺寸
mask_img_array_resized = resize(mask_img_array, prediction_mask.shape[:2], preserve_range=True)
else:
mask_img_array_resized = mask_img_array
# 为了确保掩膜是二值的,应用阈值
mask_img_array_resized = (mask_img_array_resized > 0.5).astype(np.uint8)
# 6. 应用闭运算
# 将预测的掩膜图像(prediction_mask)转换为 0 和 255 的图像,用于 OpenCV 函数
prediction_mask_uint8 = (prediction_mask[:, :, 0] * 255).astype(np.uint8)
# 定义结构元素的大小和形状
kernel_size = 29
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size, kernel_size))
# 应用闭运算
closed_mask = cv2.morphologyEx(prediction_mask_uint8, cv2.MORPH_CLOSE, kernel)
# 将处理后的掩膜图像转换回 0 和 1 的二值图像
closed_mask_binary = (closed_mask > 127).astype(np.uint8)
# 7. 确保 Closed Mask Binary 的尺寸与 Ground Truth Mask 一致
# 如果需要调整尺寸
if closed_mask_binary.shape[:2] != mask_img_array_resized.shape:
closed_mask_binary_resized = resize(closed_mask_binary, mask_img_array_resized.shape, preserve_range=True)
closed_mask_binary_resized = (closed_mask_binary_resized > 0.5).astype(np.uint8)
else:
closed_mask_binary_resized = closed_mask_binary
# 8. 保存或显示 Closed Mask Binary
# 保存图像
output_path = r"C:\Users\jack8\OneDrive\LSTM and ARIMA\LSTM\data\south\Finish\closed_mask_binary_2024.png"
cv2.imwrite(output_path, closed_mask_binary_resized * 255)
print(f"Closed Mask Binary 已保存到: {output_path}")
# 或者显示图像
import matplotlib.pyplot as plt
plt.figure(figsize=(10, 10))
plt.title("Closed Mask Binary")
plt.imshow(closed_mask_binary_resized, cmap='gray')
plt.axis('off')
plt.show()
代码说明
加载模型 :
使用 load_model 方法加载预训练的模型。请确保模型路径正确。
辅助函数 :
image_to_patches:将大图切割成指定大小的小块,处理边缘补齐。
patches_to_image:将预测的小块重新拼接成完整的图像。
加载并预处理输入图像 :
使用 load_img 和 img_to_array 加载输入图像,并进行归一化处理。
图像切割与预测 :
将大图切割成小块后,对每个小块进行预测,并将预测结果二值化。
重建预测掩膜 :
加载并预处理真实掩膜 :
加载真实的掩膜图像,调整尺寸(如有必要),并进行二值化处理。
应用闭运算 :
使用 OpenCV 的闭运算(MORPH_CLOSE)处理预测的掩膜,以消除小的洞或连接断裂的部分。
确保尺寸一致 :
检查并调整 Closed Mask Binary 的尺寸,确保与 Ground Truth Mask 一致。
保存或显示结果 :
将最终的 Closed Mask Binary 保存到指定路径,或使用 Matplotlib 显示图像。
注意事项
模型路径 :请确保 lstm_model.h5 是您实际用于图像分割的模型。如果这是一个 LSTM 模型,可能需要调整以适应图像分割任务。
输入图像和掩膜路径 :确保输入图像 (2024.jpg) 和真实掩膜图像 (2024.png) 的路径正确,且文件存在。
闭运算参数 :kernel_size 可以根据需要进行调整,以获得最佳的闭运算效果。
保存路径 :请确认 output_path 目录存在,并且具有写入权限。
环境要求
示例输出
运行上述代码后,您将获得一个名为 closed_mask_binary_2024.png 的图像文件,该文件是经过闭运算处理后的二值化掩膜,尺寸与真实掩膜一致。同时,程序会显示该图像以供直观查看。
如果有任何进一步的问题或需要更多的自定义,请随时告知!
Copy first response
Not yet included in a published release.