[d255cc]: / training.py

Download this file

265 lines (210 with data), 9.4 kB

  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
import os
import cv2
import numpy as np
import pandas as pd
import torch
from torch.cuda.amp import GradScaler, autocast
# 导入SAM2相关模块
from sam2.build_sam import build_sam2
from sam2.sam2_image_predictor import SAM2ImagePredictor
def initialize_sam2_predictor(model_cfg_path, checkpoint_path, device="cuda"):
"""
初始化SAM2预测器
Args:
model_cfg_path: SAM2模型配置文件路径
checkpoint_path: 预训练模型检查点路径
device: 使用的设备 ('cuda' 或 'cpu')
Returns:
SAM2ImagePredictor: 初始化好的预测器实例
"""
# 构建SAM2模型
sam_model = build_sam2(
model_cfg_path,
checkpoint_path,
device="cuda"
)
# 创建预测器
predictor = SAM2ImagePredictor(sam_model)
# 冻结图像编码器参数以防止过拟合
for param in predictor.model.image_encoder.parameters():
param.requires_grad = False
print("图像编码器已冻结,仅掩码解码器和提示编码器将进行微调")
return predictor
def read_batch(df, images_dir, masks_dir):
"""
读取和预处理一批数据
Args:
df: 包含图像和掩码信息的DataFrame
images_dir: 图像目录路径
masks_dir: 掩码目录路径
Returns:
tuple: (图像, 掩码, 点坐标, 点标签)
"""
# 随机选择一个图像
image_ids = df['ImageId'].unique()
selected_image = np.random.choice(image_ids)
# 获取该图像的所有掩码
image_masks = df[df['ImageId'] == selected_image]
# 读取图像
image_path = os.path.join(images_dir, selected_image)
img = cv2.imread(image_path)[...,::-1] # BGR to RGB
# 调整图像大小
r = np.min([1024 / img.shape[1], 1024 / img.shape[0]])
img = cv2.resize(img, (int(img.shape[1] * r), int(img.shape[0] * r)))
masks = []
points = []
# 处理每个掩码
for _, row in image_masks.iterrows():
mask_path = os.path.join(masks_dir, row['MaskId'])
mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
# 调整掩码大小,与图像一致
mask = cv2.resize(mask, (int(mask.shape[1] * r), int(mask.shape[0] * r)),
interpolation=cv2.INTER_NEAREST)
# 二值化掩码(如果不是二值的)
binary_mask = (mask > 0).astype(np.uint8)
masks.append(binary_mask)
# 使用CSV中提供的点坐标,并根据缩放调整
point_x = int(row['PointX'] * r)
point_y = int(row['PointY'] * r)
points.append([[point_x, point_y]])
return img, np.array(masks), np.array(points), np.ones([len(masks), 1])
def train_sam2_model(predictor, df, images_dir, masks_dir, max_iterations=50000, learning_rate=1e-5, weight_decay=4e-5):
"""
训练SAM2模型
Args:
predictor: SAM2图像预测器实例
df: 包含图像和掩码信息的DataFrame
images_dir: 图像目录路径
masks_dir: 掩码目录路径
max_iterations: 最大迭代次数
learning_rate: 学习率
weight_decay: 权重衰减率
"""
# 启用训练模式
predictor.model.sam_mask_decoder.train(True) # 启用掩码解码器的训练
predictor.model.sam_prompt_encoder.train(True) # 启用提示编码器的训练
# 设置优化器
optimizer = torch.optim.AdamW(
params=[p for p in predictor.model.parameters() if p.requires_grad],
lr=learning_rate,
weight_decay=weight_decay
)
# 设置混合精度训练
scaler = GradScaler()
mean_iou = 0
# 创建保存模型的目录
os.makedirs("models", exist_ok=True)
print(f"开始训练,共{max_iterations}次迭代...")
for itr in range(max_iterations):
# 使用混合精度训练
with torch.amp.autocast('cuda'):
# 加载数据批次
image, masks, points, labels = read_batch(df, images_dir, masks_dir)
# 忽略空批次
if len(masks) == 0:
continue
# 确保数据格式正确
gt_masks = torch.tensor(masks, dtype=torch.float32, device=predictor.device)
# 对图像应用SAM图像编码器
predictor.set_image(image)
# 对每个点/掩码对进行处理
batch_loss = 0
batch_iou = 0
for i in range(len(points)):
point = points[i:i+1]
gt_mask = gt_masks[i:i+1]
label = labels[i:i+1]
# 准备提示
mask_input, unnorm_coords, point_labels, unnorm_box = predictor._prep_prompts(
point,
label,
box=None,
mask_logits=None,
normalize_coords=True
)
# 生成嵌入
sparse_embeddings, dense_embeddings = predictor.model.sam_prompt_encoder(
points=(unnorm_coords, point_labels),
boxes=None,
masks=None,
)
batched_mode = unnorm_coords.shape[0] > 1
# 准备高分辨率特征
high_res_features = [
feat_level[-1].unsqueeze(0)
for feat_level in predictor._features["high_res_feats"]
]
# 生成掩码
low_res_masks, prd_scores, _, _ = predictor.model.sam_mask_decoder(
image_embeddings=predictor._features["image_embed"][-1].unsqueeze(0),
image_pe=predictor.model.sam_prompt_encoder.get_dense_pe(),
sparse_prompt_embeddings=sparse_embeddings,
dense_prompt_embeddings=dense_embeddings,
multimask_output=True,
repeat_image=batched_mode, # 添加这个参数
high_res_features=high_res_features,
)
# 后处理掩码到原始图像分辨率
prd_masks = predictor._transforms.postprocess_masks(
low_res_masks,
predictor._orig_hw[-1]
)
# 将logit图转换为概率图
prd_mask = torch.sigmoid(prd_masks[:, 0])
# 计算交叉熵损失
seg_loss = (-gt_mask * torch.log(prd_mask + 1e-5) -
(1 - gt_mask) * torch.log((1 - prd_mask) + 1e-5)).mean()
# 计算IoU
inter = (gt_mask * (prd_mask > 0.5)).sum()
union = gt_mask.sum() + (prd_mask > 0.5).sum() - inter
iou = inter / (union + 1e-8) # 添加小值防止除零
# 计算得分损失
score_loss = torch.abs(prd_scores[:, 0] - iou).mean()
# 混合损失
mask_loss = seg_loss + score_loss * 0.05
batch_loss += mask_loss
batch_iou += iou.item()
# 计算平均损失和IoU
if len(masks) > 0:
avg_loss = batch_loss / len(masks)
avg_iou = batch_iou / len(masks)
else:
continue
# 清空梯度
optimizer.zero_grad()
# 反向传播(使用混合精度)
scaler.scale(avg_loss).backward()
# 更新权重
scaler.step(optimizer)
scaler.update()
# 定期保存模型
if itr % 1000 == 0 and itr > 0:
torch.save(predictor.model.state_dict(), f"models/model_{itr}.torch")
# 更新平均IoU(使用指数移动平均)
if itr == 0:
mean_iou = avg_iou
else:
mean_iou = mean_iou * 0.99 + 0.01 * avg_iou
# 打印训练进度
if itr % 100 == 0:
print(f"步骤 {itr}, 准确率 (IoU) = {mean_iou:.4f}, 损失 = {avg_loss.item():.4f}")
# 训练结束,保存最终模型
torch.save(predictor.model.state_dict(), "models/model_final.torch")
print(f"训练完成。最终准确率 (IoU) = {mean_iou:.4f}")
# 主程序
if __name__ == "__main__":
# 数据路径
csv_path = "/media/ps/data/zhy/Sam2_new/sam2/data_train/train.csv"
images_dir = "/media/ps/data/zhy/Sam2_new/sam2/data_train/JPEGImages"
masks_dir = "/media/ps/data/zhy/Sam2_new/sam2/data_train/Annotations"
# SAM2模型路径
model_cfg_path = "configs/sam2.1/sam2.1_hiera_l.yaml" # 修改为实际的配置文件路径
checkpoint_path = "checkpoints/sam2.1_hiera_large.pt" # 修改为实际的检查点路径
# 加载数据
df = pd.read_csv(csv_path)
print(f"加载了{len(df)}条训练数据")
# 初始化SAM2预测器
predictor = initialize_sam2_predictor(model_cfg_path, checkpoint_path)
print("SAM2预测器初始化完成")
# 开始训练
train_sam2_model(predictor, df, images_dir, masks_dir, max_iterations=50000)