-
Notifications
You must be signed in to change notification settings - Fork 4
/
split_folds.py
119 lines (93 loc) · 3.28 KB
/
split_folds.py
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
import argparse
import os
from collections import defaultdict
import numpy as np
import pandas as pd
from numpy.random.mtrand import RandomState
from sklearn.model_selection import StratifiedKFold
from utils.config import load_config
CLASSES = {'Fish': 0,
'Flower': 1,
'Gravel': 2,
'Sugar': 3
}
def stratified_group_k_fold(
label: str,
group_column: str,
df: pd.DataFrame = None,
file: str = None,
n_splits=5,
seed: int = 0
):
random_state = RandomState(seed)
if file is not None:
df = pd.read_csv(file)
labels = defaultdict(set)
for g, l in zip(df[group_column], df[label]):
labels[g].add(l)
group_labels = dict()
groups = []
Y = []
for k, v in labels.items():
group_labels[k] = random_state.choice(list(v))
Y.append(group_labels[k])
groups.append(k)
index = np.arange(len(group_labels))
folds = StratifiedKFold(n_splits=n_splits, shuffle=True,
random_state=random_state).split(index, Y)
group_folds = dict()
for i, (train, val) in enumerate(folds):
for j in val:
group_folds[groups[j]] = i
res = np.zeros(len(df))
for i, g in enumerate(df[group_column]):
res[i] = group_folds[g]
return res.astype(np.int)
def stratified_k_fold(
label: str, df: pd.DataFrame = None, file: str = None, n_splits=5,
seed: int = 0
):
random_state = RandomState(seed)
if file is not None:
df = pd.read_csv(file)
index = np.arange(df.shape[0])
res = np.zeros(index.shape)
folds = StratifiedKFold(n_splits=n_splits,
random_state=random_state,
shuffle=True).split(index, df[label])
for i, (train, val) in enumerate(folds):
res[val] = i
return res.astype(np.int)
def split_folds(config_file):
config = load_config(config_file)
os.makedirs(config.work_dir, exist_ok=True)
df = pd.read_csv(config.data.train_df_path)
df['ImageId'] = df['Image_Label'].map(lambda x: x.split('_')[0])
df['ClassId'] = df['Image_Label'].map(
lambda x: x.split('_')[1]).map(CLASSES).astype(int)
df['exists'] = df['EncodedPixels'].notnull().astype(int)
df['ClassId0'] = [row.ClassId if row.exists else 0 for row in df.itertuples()]
df['fold'] = stratified_group_k_fold(
label='ClassId0', group_column='ImageId', df=df, n_splits=config.data.params.num_folds
)
pv_df = df.pivot(index='ImageId', columns='ClassId',
values='EncodedPixels')
pv_df = pv_df.merge(df[['ImageId', 'fold']], on='ImageId', how='left')
pv_df = pv_df.drop_duplicates()
pv_df = pv_df.set_index('ImageId')
pv_df.to_csv('folds.csv')
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--config', dest='config_file',
help='configuration file path',
default=None, type=str)
return parser.parse_args()
def main():
print('split train dataset')
args = parse_args()
if args.config_file is None:
raise Exception('no configuration file')
print('load config from {}'.format(args.config_file))
split_folds(args.config_file)
if __name__ == '__main__':
main()