Skip to content

Commit

Permalink
Get rolling input working
Browse files Browse the repository at this point in the history
  • Loading branch information
ange1a-j14 committed Aug 12, 2024
1 parent d99a9ed commit fe34171
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 39 deletions.
12 changes: 6 additions & 6 deletions decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def training_step(self, batch, batch_idx):
x, y = batch
preds = self.model(x)
loss = self.loss_function(preds, y)
acc = (preds == y).float().mean()
self.log("train_acc", acc, on_step=False, on_epoch=True)
# acc = (preds == y).float().mean()
# self.log("train_acc", acc, on_step=False, on_epoch=True)
self.log("train_loss", loss, prog_bar=True)
return loss

Expand All @@ -81,17 +81,17 @@ def validation_step(self, batch, batch_idx):
# print(f"y size {y.size()}")
preds = self.model(x)
loss = self.loss_function(preds, y)
acc = (preds == y).float().mean()
self.log("val_acc", acc, on_step=False, on_epoch=True)
# acc = (preds == y).float().mean()
# self.log("val_acc", acc, on_step=False, on_epoch=True)
self.log("val_loss", loss, prog_bar=True)
return loss

def test_step(self, batch, batch_idx):
x, y = batch
preds = self.model(x)
loss = self.loss_function(preds, y)
acc = (preds == y).float().mean()
self.log("test_acc", acc, on_step=False, on_epoch=True)
# acc = (preds == y).float().mean()
# self.log("test_acc", acc, on_step=False, on_epoch=True)
self.log("test_loss", loss, prog_bar=True)
return loss

Expand Down
6 changes: 3 additions & 3 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self, input_size, output_size, activation='LeakyReLU'):
act_fn_by_name[activation],
nn.MaxPool1d(2), # Lout = 26, given L = 53
nn.Dropout(0.1),
nn.Conv1d(64, 64, kernel_size=7), # Lout = 20, given L = 26
nn.Conv1d(64, output_size, kernel_size=7), # Lout = 20, given L = 26
act_fn_by_name[activation],
nn.MaxPool1d(2) # Lout = 10, given L = 20
)
Expand All @@ -30,7 +30,7 @@ def __init__(self, input_size, output_size, activation='LeakyReLU'):
)

def forward(self, x):
out = self.conv_layers(x) # expect out [128, 64, 10]
out = self.fc_layers(out) # expect out [128, 64, 1]
out = self.conv_layers(x) # expect out [128, num_groups, 10]
out = self.fc_layers(out) # expect out [128, num_groups, 1]
return out

57 changes: 27 additions & 30 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, h5_file):
print(self.h5_file)
self.opened_flag = False

def open_hdf5(self, group_size=256, step=1, num_groups=64):
def open_hdf5(self, rolling=True, group_size=256, step=128):
"""Set up inputs and targets. For each shot, buffer is split into rolling data.
Inputs include grouped photodiode trace of 'group_size', spaced interval 'step' apart.
Targets include average velocity of each group.
Expand All @@ -41,32 +41,28 @@ def open_hdf5(self, group_size=256, step=1, num_groups=64):
# solves issue where hdf5 file opened in __init__ prevents multiple
# workers: https://github.com/pytorch/pytorch/issues/11929
self.file = h5py.File(self.h5_file, 'r')
self.length = len(self.file['Time (s)']) # num shots
# print(torch.cuda.get_device_name(0))
pds = torch.Tensor(np.array(self.file['PD (V)'])) # [num_shots, buffer_size]
vels = torch.Tensor(np.array(self.file['Speaker (Microns/s)'])) # [num_shots, buffer_size]

grouped_pds = torch.stack(torch.split(pds, group_size, dim=1))
self.inputs = torch.transpose(grouped_pds, dim0=0, dim1=1)
grouped_vels = torch.stack(torch.split(vels, group_size, dim=1))
grouped_vels = torch.transpose(grouped_vels, dim0=0, dim1=1)
self.targets = torch.unsqueeze(torch.mean(grouped_vels, dim=2), dim=2)
# print(self.inputs.size()) # [2k, 64, 256]
# print(self.targets.size()) # [2k, 64, 1]
# grouped_pds = np.array(np.hsplit(self.file['PD (V)'], num_groups)) # [num_groups, num_shots, group_size]
# self.inputs = np.transpose(grouped_pds, [1, 0, 2]) # [num_shots, num_groups, group_size]
# grouped_vels = np.array(np.hsplit(self.file['Speaker (Microns/s)'], num_groups)) # [num_groups, num_shots, group_size]
# grouped_vels = np.transpose(grouped_vels, [1, 0, 2]) # [num_shots, num_groups, group_size]
# grouped_vels = np.average(grouped_vels, axis=2) # store average velocity per group per shot: [num_shots, num_groups]
# self.targets = np.expand_dims(grouped_vels, axis=2) # [num_shots, num_groups, 1]

## FOR ROLLING INPUT
# grouped_pds = np.array([pds[:, i:i+n] for i in range(0, len(pds[0])-n+1, m)]) # [num_groups, num_shots, group_size]
# self.inputs = np.transpose(grouped_pds, [1, 0, 2]) # [num_shots, num_groups, group_size]
# grouped_vels = np.array([vels[:, i:i+n] for i in range(0, len(vels[0])-n+1, m)]) # [num_groups, num_shots, group_size]
# grouped_vels = np.transpose(grouped_vels, [1, 0, 2]) # [num_shots, num_groups, group_size]
# grouped_vels = np.average(grouped_vels, axis=2) # store average velocity per group per shot: [num_shots, num_groups]
# self.targets = np.expand_dims(grouped_vels, axis=2) # [num_shots, num_groups, 1]
if rolling:
# ROLLING INPUT INDICES
num_groups = (pds.shape[1] - group_size) // step + 1
start_idxs = torch.arange(num_groups) * step # starting indices for each group
idxs = torch.arange(group_size)[:, None] + start_idxs
idxs = torch.transpose(idxs, dim0=0, dim1=1)
self.inputs = pds[:, idxs]
self.targets = torch.unsqueeze(torch.mean(vels[:, idxs], dim=2), dim=2)
else:
# STEP INPUT
grouped_pds = torch.stack(torch.split(pds, group_size, dim=1))
self.inputs = torch.transpose(grouped_pds, dim0=0, dim1=1)
grouped_vels = torch.stack(torch.split(vels, group_size, dim=1))
grouped_vels = torch.transpose(grouped_vels, dim0=0, dim1=1)
self.targets = torch.unsqueeze(torch.mean(grouped_vels, dim=2), dim=2)

# print(self.inputs.size()) # [2k, 64, 256]
# print(self.targets.size()) # [2k, 64, 1]

def __len__(self):
return self.length
Expand All @@ -80,13 +76,15 @@ def __getitem__(self, idx):
return FloatTensor(self.inputs[idx]), FloatTensor(self.targets[idx])

# def __getitems__(self, indices: list):
# if not hasattr(self, 'h5_file'):
# if not self.opened_flag:
# self.open_hdf5()
# self.opened_flag = True
# print("open_hdf5 in getitems")
# return FloatTensor(self.inputs[indices]), FloatTensor(self.targets[indices])

class TrainingRunner:
def __init__(self, training_h5, validation_h5, testing_h5,
velocity_only=False, num_groups=64):
velocity_only=True):
self.training_h5 = training_h5
self.validation_h5 = validation_h5
self.testing_h5 = testing_h5
Expand All @@ -97,12 +95,11 @@ def __init__(self, training_h5, validation_h5, testing_h5,
print("dataloaders set:", datetime.datetime.now())
# dimensions
input_ref = next(iter(self.train_loader))
output_ref = next(iter(self.train_loader))
# print("loaded next(iter", datetime.datetime.now())
self.input_size = num_groups #input_ref[0].size(-1) #** 2
self.output_size = num_groups # output_ref[1].size(-1)
self.input_size = input_ref[0].shape[1] # num_groups
self.output_size = input_ref[1].shape[1] # num_groups
print(f"input ref {len(input_ref)} , {input_ref[0].size()}")
print(f"output ref {len(output_ref)} , {output_ref[1].size()}")
print(f"output ref {len(input_ref)} , {input_ref[1].size()}")
# print(f"train.py input_size {self.input_size}")
# print(f"train.py output_size {self.output_size}")

Expand Down Expand Up @@ -161,7 +158,7 @@ def train_model(self, model_name, save_name=None, **kwargs):
default_root_dir=os.path.join(self.checkpoint_dir, save_name),
accelerator="gpu",
devices=[0],
max_epochs=1000,
max_epochs=2000,
callbacks=[early_stop_callback, checkpoint_callback],
check_val_every_n_epoch=10,
logger=logger
Expand Down

0 comments on commit fe34171

Please sign in to comment.