-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmstar_io.py
328 lines (241 loc) · 7.79 KB
/
mstar_io.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""
Andrew Player
September 2022
Script for parsing and working with MSTAR files and the datasets.
"""
import os
import time
import struct
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path
def read_mstar_file(
filename : str
):
"""
Parse the header and data from an MSTAR data file.
Parameters:
-----------
filename : str
The path to the *.0XX MSTAR file that should be read.
Returns:
--------
magnitude : np.ndarray()
The magnitude data from the MSTAR file. This is the image data.
phase : np.ndarray()
The phase data from the MSTAR file. (Not particullarly useful here.)
header : dict
The header information from the file. Contains target type, mission, resolution, etc...
"""
file = open(filename, "rb")
file_contents = file.read()
header_length = int(str(file_contents[46:51])[2:-1])
header_info = file_contents[0:header_length]
header_info = str(header_info)[2:-1].split('\\n')[2:-2]
header = {}
for info in header_info:
spl = info.split('= ')
name = spl[0]
val = spl[1]
header[name] = val
num_cols = int(header['NumberOfColumns'])
num_rows = int(header['NumberOfRows'])
num_pxls = num_rows * num_cols
format = ">" + str(num_pxls) + "f"
data_raw_mag = file_contents[header_length:num_pxls*4 + header_length]
data_raw_phs = file_contents[num_pxls*4 + header_length:]
mag_data_unpacked = struct.unpack(format, data_raw_mag)
phs_data_unpacked = struct.unpack(format, data_raw_phs)
phase = np.reshape(phs_data_unpacked, (num_rows, num_cols))
magnitude = np.reshape(mag_data_unpacked, (num_rows, num_cols))
return magnitude, phase, header
def get_files_and_types(
data_dir: str
):
"""
Gets the list of sample filepaths and the target_types.
Parameters:
-----------
data_dir : str
The directory which contains the data, before processing with make_dataset.
Returns:
--------
data_files : list[str]
The list of filepaths of data examples.
target_types : list[str]
The list of target types existing in the dataset.
"""
data_files = []
target_types = []
for root, _, files in os.walk(data_dir):
for filename in files:
if filename[-3:] != "JPG" and filename[-3:] != "HTM":
filepath = os.path.join(root, filename)
data_files.append(filepath)
_, _, header = read_mstar_file(filepath)
target_type = header['TargetType']
if not target_type in target_types:
target_types.append(target_type)
return data_files, target_types
def split_dataset(
dataset_path: str,
split: float
):
"""
Split the dataset into train and test folders
Parameters:
-----------
dataset_path : str
The path to the dataset to be split
split : float
The train/test split, 0 < Split < 1, size(validation) <= split
Returns:
--------
num_train : int
The number of elements that went to the training set.
num_validation : int
The number of elements that went to the validation set.
"""
train_dir = Path(dataset_path) / "train"
validation_dir = Path(dataset_path) / "validation"
try:
train_dir.mkdir()
validation_dir.mkdir()
except OSError:
print("\nTrain or Validation Dir already exists -- skipping.\n")
num_train = 0
num_validation = 0
for _, _, filenames in os.walk(dataset_path):
for filename in filenames:
old_path = Path(dataset_path) / filename
split_value = np.random.rand()
if split_value <= split:
num_validation += 1
new_path = validation_dir / filename
else:
num_train += 1
new_path = train_dir / filename
try:
os.rename(old_path, new_path)
except OSError:
pass
break
return num_train, num_validation
def make_dataset(
in_data_dir: str,
out_data_dir: str = ".",
validation_split: int = 0.1
):
"""
Create a ready-to-train dataset.
in_data_dir should just contain the folders with the raw and jpg files.
Parameters:
-----------
in_data_dir : str
-- BRDM_2
-- *.0XX
-- ...
-- ...
out_data_dir : str
-- train
-- *.0XX
-- validation
-- *.0XX
split : float
Decimal proportion that should go to validation. i.e. 0.1 == 10%
Returns:
--------
save_directory : str
The name of the directory that was saved to. Does not include the root path.
count : str
The number of samples included in the dataset.
labels : dict(str->int)
Dictionary mapping target type to its index in the one-hot-encoded label array.
target_types : list[str]
List containing the found and included target types.
"""
file_list, target_types = get_files_and_types(in_data_dir)
index = 0
labels = {}
for target_type in target_types:
labels[target_type] = index
index += 1
dir_name = f"MSTAR_DATASET_{time.time()}"
save_directory = Path(out_data_dir) / dir_name
if not save_directory.is_dir():
save_directory.mkdir()
count = 0
for file in file_list:
magnitude, _, header = read_mstar_file(file)
num_rows = int(header['NumberOfRows'])
num_cols = int(header['NumberOfColumns'])
if num_rows >= 128 and num_cols >= 128:
row_offset = (num_rows - 128) // 2
col_offset = (num_cols - 128) // 2
data = magnitude[row_offset:(128+row_offset), col_offset:(128+col_offset)]
target_type = header['TargetType']
out_name = header['ParentScene'] + target_type
label = np.zeros(len(target_types))
label[labels[target_type]] = 1
count += 1
np.savez(
os.path.join(save_directory, out_name),
magnitude=data,
label=label,
header=header
)
split_dataset(save_directory, validation_split)
return save_directory, count, labels, target_types
def plot_mstar_file(
filename: str
):
"""
Plot MSTAR sample via its filename
"""
magnitude, _, _ = read_mstar_file(filename)
plt.imshow(magnitude)
plt.show()
def plot_mstar_sample(
magnitude: str
):
"""
Plot MSTAR sample via its magnitude data.
"""
plt.imshow(magnitude)
plt.show()
def load_sample(
filename: str
):
"""
Load magnitude and label information from a dataset file.
"""
dataset_file = np.load(filename)
return dataset_file['magnitude'], dataset_file['label']
def test_with_val_data(
dataset_dir: str,
model_path: str
):
"""
Run predictions on the validation samples of a dataset.
"""
from tensorflow.keras.models import load_model
model = load_model(model_path)
val_path = dataset_dir + '/validation'
correct = 0
incorrect = 0
num_samples = 0
for filename in os.listdir(val_path):
val_file = os.path.join(val_path, filename)
magnitude, label_true = load_sample(val_file)
label = model.predict(magnitude.reshape((1, 128, 128, 1)))
target_true = np.argmax(label_true)
target_pred = np.argmax(label)
if target_true == target_pred:
correct += 1
else:
incorrect += 1
num_samples += 1
print(f"Samples {num_samples}")
print(f"Correct {correct}")
print(f"Incorrect {incorrect}")
print(f"Accuracy {100 * (correct / num_samples)}%")