前提・実現したいこと
以下のコードは、どのような動作を期待したものなのでしょうか。
train_inds, valid_inds = split_inds[flags.target_fold]
5分割交差検証を行うことを想定しているようなのですが、コードを見る限り、0~4の内、0foldのデータセットしか準備していないように感じております。
ご教示いただければ幸いです。
該当のソースコード
Python
1import cv2 2import numpy as np 3 4from dataclasses import dataclass, field 5from typing import Dict, Any, Tuple, Union, List 6 7 8@dataclass 9class Flags: 10 # General 11 debug: bool = True 12 outdir: str = "results/det" 13 device: str = "cuda:0" 14 15 # Data config 16 imgdir_name: str = "vinbigdata-chest-xray-resized-png-256x256" 17 # split_mode: str = "all_train" # all_train or valid20 18 seed: int = 111 19 target_fold: int = 0 # 0~4 20 label_smoothing: float = 0.0 21 # Model config 22 model_name: str = "adv_inception_v3"#"resnet18" 23 model_mode: str = "normal" # normal, cnn_fixed supported 24 # Training config 25 epoch: int = 20 26 batchsize: int = 8 27 valid_batchsize: int = 16 28 num_workers: int = 4 29 snapshot_freq: int = 5 30 ema_decay: float = 0.999 # negative value is to inactivate ema. 31 scheduler_type: str = "" 32 scheduler_kwargs: Dict[str, Any] = field(default_factory=lambda: {}) 33 scheduler_trigger: List[Union[int, str]] = field(default_factory=lambda: [1, "iteration"]) 34 aug_kwargs: Dict[str, Dict[str, Any]] = field(default_factory=lambda: {}) 35 mixup_prob: float = -1.0 # Apply mixup augmentation when positive value is set. 36 37 def update(self, param_dict: Dict) -> "Flags": 38 # Overwrite by `param_dict` 39 for key, value in param_dict.items(): 40 if not hasattr(self, key): 41 raise ValueError(f"[ERROR] Unexpected key for flag = {key}") 42 setattr(self, key, value) 43 return self 44 45flags_dict = { 46 "debug": False, # Change to True for fast debug run! 47 "outdir": "results/tmp_debug", 48 # Data 49 "imgdir_name": "vinbigdata-chest-xray-resized-png-256x256", 50 # Model 51 "model_name": "adv_inception_v3",#"resnet18", 52 # Training 53 "num_workers": 4, 54 "epoch": 15, 55 "batchsize": 8, 56 "scheduler_type": "CosineAnnealingWarmRestarts", 57 "scheduler_kwargs": {"T_0": 28125},#{"T_0": 14063},#{"T_0": 7031},#{"T_0": 28125}, # 15000 * 15 epoch // (batchsize=8) 58 "scheduler_trigger": [1, "iteration"], 59 "aug_kwargs": { 60 "HorizontalFlip": {"p": 0.5}, 61 "ShiftScaleRotate": {"scale_limit": 0.15, "rotate_limit": 10, "p": 0.5}, 62 "RandomBrightnessContrast": {"p": 0.5}, 63 "CoarseDropout": {"max_holes": 8, "max_height": 25, "max_width": 25, "p": 0.5}, 64 "Blur": {"blur_limit": [3, 7], "p": 0.5}, 65 "Downscale": {"scale_min": 0.25, "scale_max": 0.9, "p": 0.3}, 66 "RandomGamma": {"gamma_limit": [80, 120], "p": 0.6}, 67 } 68} 69 70class VinbigdataTwoClassDataset(DatasetMixin): 71 def __init__(self, dataset_dicts, image_transform=None, transform=None, train: bool = True, 72 mixup_prob: float = -1.0, label_smoothing: float = 0.0): 73 super(VinbigdataTwoClassDataset, self).__init__(transform=transform) 74 self.dataset_dicts = dataset_dicts 75 self.image_transform = image_transform 76 self.train = train 77 self.mixup_prob = mixup_prob 78 self.label_smoothing = label_smoothing 79 80 def _get_single_example(self, i): 81 d = self.dataset_dicts[i] 82 filename = d["file_name"] 83 84 img = cv2.imread(filename) 85 if self.image_transform: 86 img = self.image_transform(img) 87 img = torch.tensor(np.transpose(img, (2, 0, 1)).astype(np.float32)) 88 89 if self.train: 90 label = int(len(d["annotations"]) > 0) # 0 normal, 1 abnormal 91 if self.label_smoothing > 0: 92 if label == 0: 93 return img, float(label) + self.label_smoothing 94 else: 95 return img, float(label) - self.label_smoothing 96 else: 97 return img, float(label) 98 else: 99 # Only return img 100 return img, None 101 102 def get_example(self, i): 103 img, label = self._get_single_example(i) 104 if self.mixup_prob > 0. and np.random.uniform() < self.mixup_prob: 105 j = np.random.randint(0, len(self.dataset_dicts)) 106 p = np.random.uniform() 107 img2, label2 = self._get_single_example(j) 108 img = img * p + img2 * (1 - p) 109 if self.train: 110 label = label * p + label2 * (1 - p) 111 112 if self.train: 113 label_logit = torch.tensor([1 - label, label], dtype=torch.float32) 114 return img, label_logit 115 else: 116 # Only return img 117 return img 118 119 def __len__(self): 120 return len(self.dataset_dicts) 121 122dataset_dicts = get_vinbigdata_dicts(imgdir, train, debug=debug) 123dataset = VinbigdataTwoClassDataset(dataset_dicts) 124 125######以下のコードで何をやっているかの詳細を知りたいです###### 126skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=flags.seed) 127y = np.array([int(len(d["annotations"]) > 0) for d in dataset_dicts]) 128split_inds = list(skf.split(dataset_dicts, y)) 129train_inds, valid_inds = split_inds[flags.target_fold] # 0th fold 130train_dataset = VinbigdataTwoClassDataset( 131 [dataset_dicts[i] for i in train_inds], 132 image_transform=Transform(flags.aug_kwargs), 133 mixup_prob=flags.mixup_prob, 134 label_smoothing=flags.label_smoothing, 135) 136valid_dataset = VinbigdataTwoClassDataset([dataset_dicts[i] for i in valid_inds]) 137 138
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/03/11 02:47