-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathprepare_distance_dataset.py
executable file
·68 lines (54 loc) · 2.25 KB
/
prepare_distance_dataset.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
import pandas as pd
import random
import os
import shutil
random.seed(0)
import numpy as np
from sklearn.model_selection import StratifiedKFold
train=pd.read_csv('dataset/train/train_meta.csv')
img_list_0=[]
img_list_1=[]
for index, row in train.iterrows():
if(row['distancing']==1):
img_list_1.append(row['fname'])
if(row['distancing']==0) :
img_list_0.append(row['fname'])
img_list=img_list_0+img_list_1
label_list=[0]*len(img_list_0)+[1]*len(img_list_1)
img_list = np.array(img_list)
label_list = np.array(label_list)
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
skf.get_n_splits(img_list, label_list)
fold = 1
for train_index, val_index in skf.split(img_list, label_list):
X_train_fold = list(img_list[train_index])
X_val_fold = list(img_list[val_index])
y_train_fold = list(label_list[train_index])
y_val_fold = list(label_list[val_index])
os.makedirs("dataset/distance_kfolddata/fold"+str(fold)+"/train/0/")
os.makedirs("dataset/distance_kfolddata/fold"+str(fold)+"/train/1/")
os.makedirs("dataset/distance_kfolddata/fold"+str(fold)+"/val/0/")
os.makedirs("dataset/distance_kfolddata/fold"+str(fold)+"/val/1/")
for i in range(len(X_train_fold)):
if(y_train_fold[i]==0):
shutil.copy(os.path.join("dataset/train/images", X_train_fold[i]), "dataset/distance_kfolddata/fold"+str(fold)+"/train/0/")
if(y_train_fold[i]==1):
shutil.copy(os.path.join("dataset/train/images", X_train_fold[i]), "dataset/distance_kfolddata/fold"+str(fold)+"/train/1/")
for i in range(len(X_val_fold)):
if(y_val_fold[i]==0):
shutil.copy(os.path.join("dataset/train/images", X_val_fold[i]), "dataset/distance_kfolddata/fold"+str(fold)+"/val/0/")
if(y_val_fold[i]==1):
shutil.copy(os.path.join("dataset/train/images", X_val_fold[i]), "dataset/distance_kfolddata/fold"+str(fold)+"/val/1/")
f=open("dataset/distance_kfolddata/train_fold_"+str(fold)+".txt",'w')
string=""
for i in X_train_fold:
string=string+i+"\n"
f.write(string)
f.close()
f=open("dataset/distance_kfolddata/val_fold_"+str(fold)+".txt",'w')
string=""
for i in X_val_fold:
string=string+i+"\n"
f.write(string)
f.close()
fold += 1