File size: 21,635 Bytes
c2b1b26 | 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 | """
大图浒苔分割预测脚本 - 256x256瓦片版本
读取大图遥感图像,切割为256x256小patch,对每个patch进行预测
预测结果格式与predict_seaweed_256x256_simple.py保持一致
"""
import os
import torch
import numpy as np
from pathlib import Path
import json
import time
from datetime import datetime
import cv2
from tqdm import tqdm
import rasterio
from rasterio.windows import Window
import warnings
warnings.filterwarnings('ignore')
# 导入自定义模块
from dinov3_deeplabv3plus import DinoV3DeepLabV3Plus
def load_model(model_path, config, device):
"""加载训练好的模型 - 与predict_seaweed_256x256_simple.py保持一致"""
print(f"正在加载模型: {model_path}")
# 创建模型
model = DinoV3DeepLabV3Plus(
num_classes=config['num_classes'],
backbone_name=config['backbone_name'],
pretrained=False, # 不加载预训练权重,使用我们自己的权重
weights=config['backbone_weights'],
use_4channel=config['use_4channel']
).to(device)
# 加载权重
checkpoint = torch.load(model_path, map_location=device)
# 处理不同的checkpoint格式
if 'model_state_dict' in checkpoint:
model.load_state_dict(checkpoint['model_state_dict'])
print(f"加载模型权重 (epoch {checkpoint.get('epoch', 'unknown')})")
elif 'state_dict' in checkpoint:
model.load_state_dict(checkpoint['state_dict'])
print("加载模型权重")
else:
# 直接加载state dict
model.load_state_dict(checkpoint)
print("加载模型权重")
model.eval()
print("模型加载完成,进入评估模式")
return model
def process_image_for_model(image_path, target_size=256, use_4channel=True):
"""处理图像用于模型预测 - 完全复用predict_seaweed_256x256_simple.py的逻辑"""
# 使用OpenCV读取16位TIF图像
img_16bit = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
if img_16bit is None:
raise ValueError(f"无法读取图像文件: {image_path}")
# 使用rasterio方式读取多波段数据
try:
import rasterio
with rasterio.open(image_path) as src:
# 读取所有波段
image = src.read()
# 转换为HWC格式
image = np.transpose(image, (1, 2, 0))
except Exception as e:
print(f"rasterio读取失败,使用OpenCV: {e}")
# 回退到OpenCV
image = img_16bit
# 处理通道数
if use_4channel and image.shape[2] >= 4:
# 使用4通道
processed_image = image[:, :, :4]
else:
# 使用3通道假彩色(432波段)
if image.shape[2] >= 4:
# 选择第4,3,2波段(索引3,2,1)
processed_image = image[:, :, [3, 2, 1]]
else:
# 如果通道数不足,使用可用通道
processed_image = image[:, :, :3]
if image.shape[2] < 3:
# 如果还是不足3通道,重复填充
while processed_image.shape[2] < 3:
processed_image = np.concatenate([processed_image, processed_image[:, :, -1:]], axis=2)
# 调整尺寸
if processed_image.shape[0] != target_size or processed_image.shape[1] != target_size:
processed_image = cv2.resize(processed_image, (target_size, target_size), interpolation=cv2.INTER_LINEAR)
# 转换为float32并标准化到0-1范围
processed_image = processed_image.astype(np.float32)
if processed_image.max() > 1.0:
processed_image = processed_image / 65535.0 # 16位最大值
# 标准化
if use_4channel:
# 4通道标准化
mean = np.array([0.430, 0.411, 0.296, 0.350])
std = np.array([0.213, 0.156, 0.143, 0.180])
else:
# 3通道标准化
mean = np.array([0.430, 0.411, 0.296])
std = np.array([0.213, 0.156, 0.143])
# 应用标准化
for i in range(processed_image.shape[2]):
processed_image[:, :, i] = (processed_image[:, :, i] - mean[i]) / std[i]
# 转换为tensor并添加batch维度
img_tensor = torch.from_numpy(processed_image).permute(2, 0, 1).float()
return img_tensor.unsqueeze(0) # 添加batch维度
def predict_image(model, image_tensor, device, config, threshold=0.5):
"""
预测单张图像 - 与predict_seaweed_256x256_simple.py保持一致
Args:
model: 训练好的模型
image_tensor: 图像tensor
device: 设备
config: 配置字典
threshold: 浒苔预测阈值,只有当浒苔概率超过此阈值时才预测为浒苔(默认0.5)
"""
# 预测
with torch.no_grad():
start_time = time.time()
output = model(image_tensor)
inference_time = time.time() - start_time
# 处理输出格式
if isinstance(output, dict):
output = output['out']
# 获取预测结果
pred = torch.softmax(output, dim=1)
# 计算浒苔概率
seaweed_prob = pred[0, 1].squeeze(0).cpu().numpy() # 类别1是浒苔
# 使用阈值进行预测:只有当浒苔概率超过阈值时才预测为浒苔
# 这样可以避免在无浒苔图片中,由于两个类别概率接近而误判
pred_class = (seaweed_prob > threshold).astype(np.uint8)
return {
'prediction': pred_class,
'seaweed_probability': seaweed_prob,
'inference_time': inference_time
}
def save_prediction_results(prediction, seaweed_prob, output_path, base_name):
"""保存预测结果到文件 - 与predict_seaweed_256x256_simple.py保持一致"""
# 保存预测掩码 (0-1)
pred_mask = prediction.astype(np.uint8)
np.save(str(output_path / f"{base_name}_prediction.npy"), pred_mask)
# 保存概率图 (0-1)
np.save(str(output_path / f"{base_name}_probability.npy"), seaweed_prob)
# 保存为PNG图像
pred_png = (prediction * 255).astype(np.uint8)
prob_png = (seaweed_prob * 255).astype(np.uint8)
cv2.imwrite(str(output_path / f"{base_name}_prediction.png"), pred_png)
cv2.imwrite(str(output_path / f"{base_name}_probability.png"), prob_png)
def calculate_statistics(prediction, seaweed_prob):
"""计算统计信息 - 与predict_seaweed_256x256_simple.py保持一致"""
total_pixels = prediction.size
seaweed_pixels = np.sum(prediction == 1)
seaweed_ratio = seaweed_pixels / total_pixels
# 计算平均概率
avg_probability = np.mean(seaweed_prob)
max_probability = np.max(seaweed_prob)
return {
'total_pixels': int(total_pixels),
'seaweed_pixels': int(seaweed_pixels),
'seaweed_ratio': float(seaweed_ratio),
'avg_probability': float(avg_probability),
'max_probability': float(max_probability)
}
def get_tile_info_from_large_image(image_path, tile_size=256, overlap=0):
"""
获取大图的瓦片信息,但不实际读取数据(内存友好)
Args:
image_path: 大图路径
tile_size: 瓦片大小
overlap: 重叠大小
Returns:
list: 瓦片信息列表,每个元素包含(x, y, window, tile_id)
"""
tile_info_list = []
with rasterio.open(image_path) as src:
height, width = src.height, src.width
bands = src.count
print(f"大图信息: {width}x{height}, {bands}波段")
# 计算步长
stride = tile_size - overlap
# 计算瓦片数量
tiles_y = (height - tile_size) // stride + 1
tiles_x = (width - tile_size) // stride + 1
# 处理边界情况
if (height - tile_size) % stride != 0:
tiles_y += 1
if (width - tile_size) % stride != 0:
tiles_x += 1
total_tiles = tiles_x * tiles_y
print(f"将提取 {total_tiles} 个瓦片 ({tiles_x} x {tiles_y})")
# 生成瓦片信息
tile_count = 0
for y in range(0, height - tile_size + 1, stride):
for x in range(0, width - tile_size + 1, stride):
# 处理边界情况
actual_x = min(x, width - tile_size)
actual_y = min(y, height - tile_size)
# 创建窗口信息
window = Window(actual_x, actual_y, tile_size, tile_size)
tile_info_list.append({
'x': actual_x,
'y': actual_y,
'window': window,
'tile_id': tile_count
})
tile_count += 1
return tile_info_list
def read_tile_data(image_path, window, bands):
"""读取单个瓦片的数据"""
with rasterio.open(image_path) as src:
# 读取多波段数据
if bands >= 4:
# 使用4通道
tile_data = src.read([1, 2, 3, 4], window=window)
else:
# 使用可用通道
tile_data = src.read(list(range(1, min(bands, 4) + 1)), window=window)
# 转换为HWC格式
tile_data = np.transpose(tile_data, (1, 2, 0))
return tile_data
def save_tile_temporarily(tile_data, temp_dir, tile_id):
"""临时保存瓦片为文件,用于process_image_for_model函数"""
temp_path = os.path.join(temp_dir, f"temp_tile_{tile_id}.tif")
# 确保临时目录存在
os.makedirs(temp_dir, exist_ok=True)
# 保存为临时TIFF文件
with rasterio.open(
temp_path, 'w',
driver='GTiff',
height=tile_data.shape[0],
width=tile_data.shape[1],
count=tile_data.shape[2],
dtype=tile_data.dtype,
compress='lzw'
) as dst:
for i in range(tile_data.shape[2]):
dst.write(tile_data[:, :, i], i + 1)
return temp_path
def predict_tiles(tiles, model, device, config, output_dir, threshold=0.5):
"""
对所有瓦片进行预测
Args:
tiles: 瓦片列表
model: 模型
device: 设备
config: 配置
output_dir: 输出目录
threshold: 预测阈值
"""
results = []
total_inference_time = 0
# 创建临时目录
temp_dir = os.path.join(output_dir, "temp_tiles")
print(f"\n开始预测 {len(tiles)} 个瓦片...")
# 使用tqdm显示进度条
for tile_info in tqdm(tiles, desc="预测瓦片"):
try:
tile_id = tile_info['tile_id']
tile_data = tile_info['data']
# 临时保存瓦片为文件
temp_tile_path = save_tile_temporarily(tile_data, temp_dir, tile_id)
# 处理图像用于模型
image_tensor = process_image_for_model(
temp_tile_path,
target_size=config['image_size'],
use_4channel=config['use_4channel']
).to(device)
# 预测(使用阈值)
result = predict_image(model, image_tensor, device, config, threshold=threshold)
# 计算统计信息
stats = calculate_statistics(result['prediction'], result['seaweed_probability'])
# 保存预测结果
base_name = f"patch{tile_id:05d}"
save_prediction_results(
result['prediction'],
result['seaweed_probability'],
Path(output_dir),
base_name
)
# 保存结果信息
result_info = {
'tile_id': tile_id,
'x': tile_info['x'],
'y': tile_info['y'],
'inference_time': float(result['inference_time']),
'statistics': stats
}
results.append(result_info)
total_inference_time += result['inference_time']
# 清理临时文件
if os.path.exists(temp_tile_path):
os.remove(temp_tile_path)
except Exception as e:
print(f" - 瓦片 {tile_id} 预测失败: {str(e)}")
continue
# 清理临时目录
if os.path.exists(temp_dir):
try:
os.rmdir(temp_dir)
except:
pass
return results, total_inference_time
def predict_tiles_memory_friendly(tile_info_list, image_path, model, device, config, output_dir, threshold=0.5):
"""
内存友好的瓦片预测 - 逐块处理
Args:
tile_info_list: 瓦片信息列表
image_path: 图像路径
model: 模型
device: 设备
config: 配置
output_dir: 输出目录
threshold: 预测阈值
"""
results = []
total_inference_time = 0
# 创建临时目录
temp_dir = os.path.join(output_dir, "temp_tiles")
os.makedirs(temp_dir, exist_ok=True)
# 获取图像信息
with rasterio.open(image_path) as src:
bands = src.count
print(f"\n开始预测 {len(tile_info_list)} 个瓦片...")
# 使用tqdm显示进度条
for tile_info in tqdm(tile_info_list, desc="预测瓦片"):
try:
tile_id = tile_info['tile_id']
# 读取瓦片数据
tile_data = read_tile_data(image_path, tile_info['window'], bands)
# 临时保存瓦片为文件
temp_tile_path = save_tile_temporarily(tile_data, temp_dir, tile_id)
# 处理图像用于模型
image_tensor = process_image_for_model(
temp_tile_path,
target_size=config['image_size'],
use_4channel=config['use_4channel']
).to(device)
# 预测(使用阈值)
result = predict_image(model, image_tensor, device, config, threshold=threshold)
# 计算统计信息
stats = calculate_statistics(result['prediction'], result['seaweed_probability'])
# 保存预测结果
base_name = f"patch{tile_id:05d}"
save_prediction_results(
result['prediction'],
result['seaweed_probability'],
Path(output_dir),
base_name
)
# 保存结果信息
result_info = {
'tile_id': tile_id,
'x': tile_info['x'],
'y': tile_info['y'],
'inference_time': float(result['inference_time']),
'statistics': stats
}
results.append(result_info)
total_inference_time += result['inference_time']
# 清理临时文件
if os.path.exists(temp_tile_path):
os.remove(temp_tile_path)
except Exception as e:
print(f" - 瓦片 {tile_id} 预测失败: {str(e)}")
continue
# 清理临时目录
try:
os.rmdir(temp_dir)
except:
pass
return results, total_inference_time
def main():
"""主函数"""
print("=" * 60)
print("大图瓦片浒苔分割预测系统 - 256x256版本")
print("=" * 60)
# 配置
model_path = "seaweed_segmentation_improved_epoch500/best_checkpoint.pth"
config_path = "seaweed_segmentation_improved_epoch500/config.json"
large_image_dir = "E:\GF6\山科影像\浒苔\GF6_PMS_E121.1_N33.6_20250604_L1A1420584616-MUX_fuse.tif"
output_dir = "outputs/test_large"
# 瓦片配置
tile_size = 256
overlap = 0 # 无重叠
# 预测阈值:只有当浒苔概率超过此阈值时才预测为浒苔
# 建议值:0.5-0.7,可以根据验证集调整
prediction_threshold = 0.5
# 创建设备
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"使用设备: {device}")
# 加载配置
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
print(f"模型配置:")
print(f" - 输入通道: {4 if config['use_4channel'] else 3}")
print(f" - 图像尺寸: {config['image_size']}x{config['image_size']}")
print(f" - 类别数: {config['num_classes']}")
print(f" - 瓦片大小: {tile_size}x{tile_size}")
print(f" - 重叠: {overlap}")
print(f" - 预测阈值: {prediction_threshold}")
# 加载模型
model = load_model(model_path, config, device)
# 创建输出目录
output_path = Path(output_dir)
output_path.mkdir(parents=True, exist_ok=True)
# 获取大图文件
image_extensions = ['.tif', '.tiff']
large_images = []
for ext in image_extensions:
large_images.extend(Path(large_image_dir).glob(f"*{ext}"))
print(f"\n找到 {len(large_images)} 个大图文件")
# 处理每个大图
all_results = []
for image_file in large_images:
print(f"\n处理大图: {image_file.name}")
try:
# 获取瓦片信息(不读取实际数据)
tile_info_list = get_tile_info_from_large_image(
str(image_file),
tile_size=tile_size,
overlap=overlap
)
if not tile_info_list:
print(f" - 未能生成瓦片信息,跳过")
continue
# 内存友好的瓦片预测
results, total_inference_time = predict_tiles_memory_friendly(
tile_info_list, str(image_file), model, device, config, output_path, threshold=prediction_threshold
)
if results:
# 计算平均推理时间
avg_inference_time = total_inference_time / len(results)
# 计算平均统计
all_ratios = [r['statistics']['seaweed_ratio'] for r in results]
avg_seaweed_ratio = np.mean(all_ratios)
print(f"\n瓦片预测完成:")
print(f" - 总瓦片数: {len(results)}")
print(f" - 平均推理时间: {avg_inference_time:.3f}s")
print(f" - 平均浒苔比例: {avg_seaweed_ratio:.2%}")
# 保存大图处理结果
image_results = {
'image_file': str(image_file),
'total_tiles': len(results),
'avg_inference_time': avg_inference_time,
'avg_seaweed_ratio': float(avg_seaweed_ratio),
'tile_results': results
}
all_results.append(image_results)
# 保存结果文件
result_file = output_path / f"{image_file.stem}_tile_results.json"
with open(result_file, 'w', encoding='utf-8') as f:
json.dump(image_results, f, indent=2, ensure_ascii=False, default=str)
print(f" - 结果保存: {result_file}")
except Exception as e:
print(f" - 处理失败: {str(e)}")
import traceback
traceback.print_exc()
continue
# 保存总体结果
if all_results:
summary = {
'timestamp': datetime.now().isoformat(),
'total_images': len(all_results),
'total_tiles': sum([r['total_tiles'] for r in all_results]),
'results': all_results
}
summary_file = output_path / "large_image_tile_summary.json"
with open(summary_file, 'w', encoding='utf-8') as f:
json.dump(summary, f, indent=2, ensure_ascii=False, default=str)
print(f"\n" + "=" * 60)
print("大图瓦片预测完成总结")
print("=" * 60)
print(f"总图像数: {len(all_results)}")
print(f"总瓦片数: {summary['total_tiles']}")
print(f"结果保存目录: {output_path}")
print(f"总结文件: {summary_file}")
# 显示浒苔分布统计
all_ratios = []
for result in all_results:
all_ratios.extend([r['statistics']['seaweed_ratio'] for r in result['tile_results']])
if all_ratios:
print(f"\n浒苔分布统计:")
print(f" - 最小浒苔比例: {min(all_ratios):.2%}")
print(f" - 最大浒苔比例: {max(all_ratios):.2%}")
print(f" - 浒苔比例标准差: {np.std(all_ratios):.2%}")
print("\n🎉 大图瓦片浒苔预测完成!")
print(f"预测结果保存在: {output_dir}")
if __name__ == "__main__":
main()
|