File size: 3,527 Bytes
127bcdc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import cv2
import torch
import torchvision.transforms as transforms
from torch.utils.data import Dataset
import utils


class AVLip(Dataset):
    def __init__(self, opt):
        assert opt.data_label in ["train", "val"]
        self.data_label = opt.data_label
        self.real_list = utils.get_list(opt.real_list_path)
        self.fake_list = utils.get_list(opt.fake_list_path)
        self.label_dict = dict()
        for i in self.real_list:
            self.label_dict[i] = 0
        for i in self.fake_list:
            self.label_dict[i] = 1
        self.total_list = self.real_list + self.fake_list

    def __len__(self):
        return len(self.total_list)

    def __getitem__(self, idx):
        # 防止无限递归:记录已尝试的索引
        tried_indices = set()
        return self._get_item_with_skip(idx, tried_indices)
    
    def _get_item_with_skip(self, idx, tried_indices):
        # 如果已尝试所有样本,抛出异常
        if len(tried_indices) >= len(self.total_list):
            raise RuntimeError("All samples are corrupted or cannot be read!")
        
        tried_indices.add(idx)
        img_path = self.total_list[idx]
        label = self.label_dict[img_path]
        
        # 尝试读取图像,如果失败则跳过该文件
        try:
            # 检查文件是否存在
            if not os.path.exists(img_path):
                print(f"WARNING: File not found, skipping: {img_path}")
                # 跳过当前文件,尝试下一个样本
                return self._get_item_with_skip((idx + 1) % len(self.total_list), tried_indices)
            
            # 读取图像
            img_cv = cv2.imread(img_path)
            if img_cv is None:
                print(f"WARNING: Failed to read image, skipping: {img_path}")
                # 跳过当前文件,尝试下一个样本
                return self._get_item_with_skip((idx + 1) % len(self.total_list), tried_indices)
            
            img = torch.tensor(img_cv, dtype=torch.float32)
            img = img.permute(2, 0, 1)
        except Exception as e:
            print(f"WARNING: Error processing {img_path}: {e}, skipping...")
            # 跳过当前文件,尝试下一个样本
            return self._get_item_with_skip((idx + 1) % len(self.total_list), tried_indices)
        crops = transforms.Normalize(mean=[0.48145466, 0.4578275, 0.40821073],
                                     std=[0.26862954, 0.26130258, 0.27577711])(img)
        # crop images
        # crops[0]: 1.0x, crops[1]: 0.65x, crops[2]: 0.45x
        # NB: bottom strip layout is [face0|face1|face2|face3|face4] each 500x500.
        # Original code used `i:i+500 for i in range(5)` which only sampled the
        # left-most 504 columns (5 near-identical 1-px-shifted views of face0).
        # Fixed to `i*500:(i+1)*500` so 5 distinct face patches reach the model.
        crops = [[transforms.Resize((224, 224))(img[:, 500:, i*500:(i+1)*500]) for i in range(5)], [], []]
        crop_idx = [(28, 196), (61, 163)]
        for i in range(len(crops[0])):
            crops[1].append(transforms.Resize((224, 224))
                            (crops[0][i][:, crop_idx[0][0]:crop_idx[0][1], crop_idx[0][0]:crop_idx[0][1]]))
            crops[2].append(transforms.Resize((224, 224))
                            (crops[0][i][:, crop_idx[1][0]:crop_idx[1][1], crop_idx[1][0]:crop_idx[1][1]]))
        img = transforms.Resize((1120, 1120))(img)

        return img, crops, label, img_path