Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the unpacking various sequence LSTM #40

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions ResNetCRNN_varylength/UCF101_ResNetCRNN_varlen.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ def validation(model, device, optimizer, test_loader):
device = torch.device("cuda" if use_cuda else "cpu") # use CPU or GPU

# Data loading parameters
params = {'batch_size': batch_size, 'shuffle': True, 'num_workers': 8, 'pin_memory': True} if use_cuda else {}
if use_cuda:
params = {'batch_size': batch_size, 'shuffle': True, 'num_workers': 8, 'pin_memory': True}
else:
params = {'batch_size': 2, 'shuffle': True} # Testing


# load UCF101 actions names
Expand Down Expand Up @@ -184,6 +187,8 @@ def validation(model, device, optimizer, test_loader):
all_names = []
all_length = [] # each video length
for f in fnames:
if not os.path.isdir(os.path.join(data_path, f)):
continue
loc1 = f.find('v_')
loc2 = f.find('_g')
actions.append(f[(loc1 + 2): loc2])
Expand Down Expand Up @@ -226,7 +231,7 @@ def validation(model, device, optimizer, test_loader):
list(cnn_encoder.module.fc2.parameters()) + list(cnn_encoder.module.bn2.parameters()) + \
list(cnn_encoder.module.fc3.parameters()) + list(rnn_decoder.parameters())

elif torch.cuda.device_count() == 1:
else:
crnn_params = list(cnn_encoder.fc1.parameters()) + list(cnn_encoder.bn1.parameters()) + \
list(cnn_encoder.fc2.parameters()) + list(cnn_encoder.bn2.parameters()) + \
list(cnn_encoder.fc3.parameters()) + list(rnn_decoder.parameters())
Expand Down
10 changes: 7 additions & 3 deletions ResNetCRNN_varylength/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,20 @@ def forward(self, x_RNN, x_lengths):
""" h_n shape (n_layers, batch, hidden_size), h_c shape (n_layers, batch, hidden_size) """
""" None represents zero initial hidden state. RNN_out has shape=(batch, time_step, output_size) """

RNN_out, _ = torch.nn.utils.rnn.pad_packed_sequence(packed_RNN_out, batch_first=True)
RNN_out, out_lengths = torch.nn.utils.rnn.pad_packed_sequence(packed_RNN_out, batch_first=True)
RNN_out = RNN_out.contiguous()
last_out = []
for i, length in enumerate(out_lengths):
last_out.append(RNN_out[i][length - 1])
last_out = torch.stack(last_out)
# RNN_out = RNN_out.view(-1, RNN_out.size(2))

# reverse back to original sequence order
_, unperm_idx = perm_idx.sort(0)
RNN_out = RNN_out[unperm_idx]
RNN_out = last_out[unperm_idx]

# FC layers
x = self.fc1(RNN_out[:, -1, :]) # choose RNN_out at the last time step
x = self.fc1(RNN_out) # choose RNN_out at the last time step
x = F.relu(x)
x = F.dropout(x, p=self.drop_p, training=self.training)
x = self.fc2(x)
Expand Down