-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsplit.py
More file actions
194 lines (158 loc) · 5.54 KB
/
split.py
File metadata and controls
194 lines (158 loc) · 5.54 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
###############################################################################
# split.py
#
# sheneman@uidaho.edu
#
# NOTE: ***This tool is currently deprecated and unused***
#
# A simple tool for helping to create a train/validate/test splits based on
# source directories of files and specified split ratios.
#
# Usage:
# python split.py [ --help | --verbose | --config=<YAML config file> ]
#
###############################################################################
import os
import getopt
import yaml
from os import listdir
from os.path import isfile, join
import pprint
import glob
import sys
from time import time
from random import seed, random, shuffle
from math import floor
#
# This script takes a directory of input files and partitions them into a list of
# input files to use for TRAINING, VALIDATION, and TESTING
#
# set our random seed based on current time
now = int(time())
seed(now)
###############################################################################
#
# HANDLE Command line arguments
#
#
def usage():
print("python split.py [ --help | --verbose | --config=<YAML config filename> ] ")
try:
opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "config="])
except getopt.GetoptError as err:
print(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
configfile = None
verbose = False
for o, a in opts:
if o == "-v":
verbose = True
elif o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-c", "--config"):
configfile = a
else:
assert False, "unhandled option"
if(configfile == None):
configfile = "split.yaml"
#################################################################################
#################################################################################
#
# Format and Example config YAML file:
#
# FORMAT:
# -------
# rawdir: <path to raw images>
# bindir: <path to bin images>
# train_fraction: <fraction between 0 and 1.0>
# validation_fraction: <fraction between 0 and 1.0>
# test_fraction: <fraction between 0 and 1.0>
# file_filter: <posix file filter for selecting files>
# trainlist_out: <output path for training images>
# validationlist_out: <output path for validation images>
# testlist_out: <output path for test images>
#
# EXAMPLE:
# --------
# rawdir: "../images/raw"
# bindir: "../images/binary"
# train_fraction: 0.60
# validation_fraxtion: 0.20
# test_fraction: 0.20
# file_filter: "Po1g_100_1*.tif"
# trainlist_out: "./trainlist.txt"
# validationlist_out: "./validationlist.txt"
# testlist_out: "./testlist.txt"
#
#################################################################################
cf = open(configfile, "r")
config = yaml.load(cf, Loader=yaml.FullLoader)
print("YAML CONFIG:")
for c in config:
print(" [%s]:\"%s\"" %(c,config[c]))
print("\n")
cf.close()
# Set some paths for our image library of raw and binary labeled data
IMG_RAWPATH = config["rawdir"]
IMG_BINPATH = config["bindir"]
# Set output filenames
TRAIN_FILENAME = config["trainlist_out"]
VALIDATION_FILENAME = config["validationlist_out"]
TEST_FILENAME = config["testlist_out"]
FILE_FILTER = config["file_filter"]
# The fraction of the image library that will be used for training, validation, and testing
# The totals must add up to 1.0
TRAIN_FRACTION = config["train_fraction"]
VALIDATION_FRACTION = config["validation_fraction"]
TEST_FRACTION = config["test_fraction"]
# Get all of the filenames that match the filter and shuffle them in place
cwd = os.getcwd()
os.chdir(IMG_RAWPATH)
filenames = glob.glob(FILE_FILTER)
os.chdir(cwd)
shuffle(filenames)
num_filenames = len(filenames)
if(TRAIN_FRACTION < 0 or TRAIN_FRACTION > 1.0):
print("ERROR: TRAIN_FRACTION must be between 0,0 and 1,0")
exit(-1)
train_partition_start = 0
train_partition_end = int(floor(num_filenames*TRAIN_FRACTION-1))
validation_partition_size = int(floor(num_filenames*VALIDATION_FRACTION))
if(train_partition_end == num_filenames-1):
test_partition_start = -1
test_partition_end = -1
validation_partition_start = -1
validation_partition_end = -1
else:
validation_partition_start = train_partition_end+1
validation_partition_end = validation_partition_start + validation_partition_size
test_partition_start = validation_partition_end + 1
test_partition_end = num_filenames-1
if(train_partition_start < 0 or train_partition_end < 0 or train_partition_end < train_partition_start):
train_partition_start = -1
train_partiiton_end = -1
print("TOTAL NUMBER OF FILENAMES: %d" %num_filenames)
print("TRAIN: %d thru %d" %(train_partition_start,train_partition_end))
print("VALIDATION: %d thru %d" %(validation_partition_start,validation_partition_end))
print("TEST: %d thru %d" %(test_partition_start,test_partition_end))
# Write the training set input file
if(train_partition_start >= 0):
file = open(TRAIN_FILENAME, "w")
for i in range(train_partition_start, train_partition_end+1):
file.write(filenames[i] + "\n")
file.close()
# Write the validation set input file
if(validation_partition_start >= 0):
file = open(VALIDATION_FILENAME, "w")
for i in range(validation_partition_start, validation_partition_end+1):
file.write(filenames[i] + "\n")
file.close()
# Write the test set input file
if(test_partition_start >= 0):
file = open(TEST_FILENAME, "w")
for i in range(test_partition_start, test_partition_end+1):
file.write(filenames[i] + "\n")
file.close()
exit(0)