Skip to content

Instantly share code, notes, and snippets.

@petrosDemetrakopoulos
Created December 17, 2022 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petrosDemetrakopoulos/254b0301a5f9b55308ab560208974c83 to your computer and use it in GitHub Desktop.
Save petrosDemetrakopoulos/254b0301a5f9b55308ab560208974c83 to your computer and use it in GitHub Desktop.
Radar data preprocessing
def create_dataset_from_raw(directory_path, resize_to):
resize_width = resize_to[0]
resize_height = resize_to[1]
batch_names = [directory_path + name for name in os.listdir(directory_path) if os.path.isdir(os.path.join(directory_path, name))]
dataset = np.zeros(shape=(len(batch_names),36,resize_height,resize_width)) # (samples, filters, rows = height, cols = width)
for batch_idx,batch in enumerate(batch_names):
files = [x for x in os.listdir(batch) if x != '.DS_Store']
files.sort()
crn_batch = np.zeros(shape=(36, resize_height, resize_width))
for (idx,raster) in enumerate(files):
fn = batch + '/' + raster
img = h5py.File(fn)
original_image = np.array(img["image1"]["image_data"]).astype(float)
img = Image.fromarray(original_image)
# note that here it is (width, heigh) while in the tensor is in (rows = height, cols = width)
img = img.resize(size=(resize_width, resize_height))
original_image = np.array(img)
original_image = original_image / 255.0
crn_batch[idx] = original_image
dataset[batch_idx] = crn_batch
print("Importing batch:" + str(batch_idx+1))
return dataset
def split_data_xy(data):
x = data[:, 0 : 18, :, :]
y = data[:, 18 : 36, :, :]
return x, y
dataset = create_dataset_from_raw('./data/raw/', resize_to=(315,344))
dataset = np.expand_dims(dataset, axis=-1)
dataset_x, dataset_y = split_data_xy(dataset)
X_train, X_val, y_train, y_val = sk.train_test_split(dataset_x,dataset_y,test_size=0.2, random_state = 42)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment