代码回滚:增加未知类别负样本之后,模型不收敛,训练效果特别差,现在进行回滚。回滚之后效果非常不错。
This commit is contained in:
+18
-15
@@ -73,9 +73,11 @@ TASKS = {
|
||||
embedding_dim=512,
|
||||
batch_size=16,
|
||||
lr=1e-3,
|
||||
triplet_margin=0.25,
|
||||
center_loss_weight=0.1,
|
||||
aug_strength="shape",
|
||||
# triplet_margin=0.25,
|
||||
triplet_margin=0.5,
|
||||
# center_loss_weight=0.5,
|
||||
center_loss_weight=20,
|
||||
aug_strength="medium",
|
||||
),
|
||||
}
|
||||
|
||||
@@ -142,11 +144,11 @@ class TripletDataset(Dataset):
|
||||
三元组数据集,用于三元组损失训练
|
||||
每个样本包含:锚点(anchor)、正样本(positive)、负样本(negative)
|
||||
"""
|
||||
|
||||
|
||||
def __init__(self, dataset_path: str, transform=None, samples_per_class: int = 100):
|
||||
"""
|
||||
初始化三元组数据集
|
||||
|
||||
|
||||
Args:
|
||||
dataset_path: 数据集路径
|
||||
transform: 数据变换
|
||||
@@ -155,13 +157,13 @@ class TripletDataset(Dataset):
|
||||
self.dataset_path = dataset_path
|
||||
self.transform = transform
|
||||
self.samples_per_class = samples_per_class
|
||||
|
||||
|
||||
# 加载数据集
|
||||
self.class_to_idx = {}
|
||||
self.idx_to_class = {}
|
||||
self.samples_by_class = defaultdict(list)
|
||||
self.all_samples = []
|
||||
|
||||
|
||||
self._load_dataset()
|
||||
|
||||
def _load_dataset(self):
|
||||
@@ -191,24 +193,24 @@ class TripletDataset(Dataset):
|
||||
logger.info(f"加载数据集完成:")
|
||||
for class_name, class_idx in self.class_to_idx.items():
|
||||
logger.info(f" {class_name}: {len(self.samples_by_class[class_idx])} 张图片")
|
||||
|
||||
|
||||
def __len__(self):
|
||||
return len(self.all_samples)
|
||||
|
||||
|
||||
def __getitem__(self, idx):
|
||||
"""
|
||||
获取三元组样本
|
||||
|
||||
|
||||
Returns:
|
||||
tuple: (anchor, positive, negative, anchor_label)
|
||||
"""
|
||||
# 获取锚点样本
|
||||
anchor_path, anchor_label = self.all_samples[idx]
|
||||
anchor_img = self._load_image(anchor_path)
|
||||
|
||||
|
||||
# 获取正样本(同类别的不同图片)
|
||||
# 需要从自己所在类别中先把自己给排除掉
|
||||
positive_candidates = [path for path in self.samples_by_class[anchor_label]
|
||||
positive_candidates = [path for path in self.samples_by_class[anchor_label]
|
||||
if path != anchor_path]
|
||||
if positive_candidates:
|
||||
# 从这里可以看出来是随机选的
|
||||
@@ -216,7 +218,7 @@ class TripletDataset(Dataset):
|
||||
else:
|
||||
positive_path = anchor_path # 如果只有一张图片,使用自己作为正样本
|
||||
positive_img = self._load_image(positive_path)
|
||||
|
||||
|
||||
# 获取负样本(不同类别的图片)
|
||||
# 获得其它类别
|
||||
negative_classes = [cls for cls in self.samples_by_class.keys() if cls != anchor_label]
|
||||
@@ -225,7 +227,7 @@ class TripletDataset(Dataset):
|
||||
# 随便选一个路径
|
||||
negative_path = random.choice(self.samples_by_class[negative_class])
|
||||
negative_img = self._load_image(negative_path)
|
||||
|
||||
|
||||
return anchor_img, positive_img, negative_img, anchor_label
|
||||
|
||||
def _load_image(self, image_path: str):
|
||||
@@ -818,5 +820,6 @@ if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("task", choices=list(TASKS.keys()), nargs="?", default="dish")
|
||||
args = parser.parse_args()
|
||||
main("processed_ingredient")
|
||||
# main("whole_ingredient")
|
||||
main("dish")
|
||||
# main("dish")
|
||||
Reference in New Issue
Block a user