Repository navigation
Expand file tree
/
Copy pathnegative.py
More file actions
142 lines (109 loc) · 5.02 KB
/
Copy pathnegative.py
File metadata and controls
142 lines (109 loc) · 5.02 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
import numpy as np
import matplotlib.pyplot as plt
import random
import positive as pos
import os
import shutil
# Calculate the corner point of the picture
def corner_point(position):
x1, y1, x2, y2 = position
return int(min(x1, x2)), int(min(y1, y2)), int(max(x1, x2)), int(max(y1, y2))
# Refresh the data. Return the selected region of data
def data_refresh(data, x_final, y_final, window_shape_value):
data_refresh = data[y_final:y_final+window_shape_value,
x_final:x_final+window_shape_value]
return data_refresh
# Save the data and asteroid position into a *.npz file
def save_data_negative(name, index, count, data, data_dir):
dataPath = '{}\{}_{}'.format(data_dir, count, name)
np.savez(dataPath, matrix1=data)
print('Successfully save data for index:{} count:{} filename:{}'
.format(index, count, name))
# Plot the data and save into a *.jpg file
def plot_data_negative(name, count, data, pic_dir):
plt.imshow(data, cmap='gray', vmin=0, vmax=0.01)
plt.colorbar()
plt.savefig('{}\{}_{}.jpg'.format(pic_dir, count, name), dpi=300)
plt.clf()
# Find the posible negative pictures
def find_region(data, corner, window_shape_value, name, index, count, data_dir, pic_dir):
x_min, y_min, x_max, y_max = corner
x_final = []
y_final = []
if x_min > window_shape_value+500:
x_final.append(random.randint(0, x_min - window_shape_value - 300))
if (4000 - x_max) > window_shape_value+500:
x_final.append(random.randint(x_max+300, 4000 - window_shape_value))
if y_min > window_shape_value+500:
y_final.append(random.randint(0, y_min - window_shape_value - 300))
if (4000 - y_max) > window_shape_value+500:
y_final.append(random.randint(y_max+300, 4000 - window_shape_value))
if x_final == [] or y_final == []:
return count
for i in x_final:
for j in y_final:
data_new = data_refresh(data, i, j, window_shape_value)
if data_new.shape != (window_shape_value, window_shape_value):
print('Error! Count:{} Index:{} Data shape does not fit as expected')
continue
save_data_negative(name, index, count, data_new, data_dir)
plot_data_negative(name, count, data_new, pic_dir)
count += 1
return count
# Wrap it up
def generate_negative_data(father_dir, pic_dir, data_dir, start_index, end_count, window_shape_value=1000):
# Count the number of accepted negative picture
index = start_index
count = 0
# Iterate through the data
while count < end_count:
# Get the path and filename of the original data file (generated by download.py)
if index > len(os.listdir(father_dir))-2:
break
path, name = pos.path_of_data(father_dir, index)
print('Start preprocessing {}: {}'.format(index, name))
if index > 0 and index < len(os.listdir(father_dir)):
_, name_last = pos.path_of_data(father_dir, index-1)
_, name_next = pos.path_of_data(father_dir, index+1)
if name_next[-9:] == name[-9:] or name_last[-9:] == name[-9:]:
print(
f"Warning: {path} seems to have more than 1 asteroid trail. Skip this picture.")
index += 1
continue
# Load the *.npz file, and get the position of the asteroid trail
data, position = pos.load_saved_data(path)
# Convert the 'nan' value to 0 in the array
data = np.nan_to_num(data)
# Calculate the corner of the asteroid trail
corner = corner_point(position)
# Check if we can use this image to generate a negative picture.
# If so, randomly pick two pictures with no asteroid trails.
count = find_region(data, corner, window_shape_value,
name, index, count, data_dir, pic_dir)
index += 1
# Randomly split the data set to train and validation. We choose a 80/20 split.
def random_split(dir_train, dir_val, dir_test):
fileList = os.listdir(dir_train)
for file in fileList:
train_file = os.path.join(dir_train, file)
val_file = os.path.join(dir_val, file)
test_file = os.path.join(dir_test, file)
random_num = random.random()
if random_num < 0.15:
shutil.move(train_file, test_file)
print('File move from {} to {}'.format(train_file, test_file))
elif random_num < 0.3:
shutil.move(train_file, val_file)
print('File move from {} to {}'.format(train_file, val_file))
else:
print('File does not move')
# Main Function
if __name__ == '__main__':
generate_negative_data('.\data',
'.\pic_negative',
'.\\preprocess\\train\\negative',
start_index=0, end_count=1710,
window_shape_value=1000)
random_split('.\\preprocess\\train\\negative',
'.\\preprocess\\val\\negative',
'.\\preprocess\\test\\negative')