## here is my landmarkembedding class:
class LandmarkEmbedding(nn.Module):
def __init__(self, units, name):
super(LandmarkEmbedding, self).__init__()
self.units = units
self.name = name
# Embedding for missing landmark in frame, initialized with zeros
self.empty_embedding = nn.Parameter(torch.zeros(units), requires_grad=True)
# Embedding
self.dense = nn.Sequential(
nn.Linear(units, UNITS_ENCODER, bias=False),
nn.GELU(),
nn.Linear(UNITS_ENCODER, UNITS_ENCODER, bias=False)
)
def forward(self, x):
batch_size, num_landmarks, num_units = x.shape
# Reshape x to (batch_size * num_landmarks, num_units)
x = x.view(-1, num_units)
# If there are missing landmarks, use the empty embedding for those positions
missing_landmark_mask = (torch.sum(x, dim=1, keepdim=True) == 0).float()
# print(x)
# print(self.dense[0].weight)
# print(self.dense.size())
print("x shape:", x.shape)
print("missing_landmark_embeddings shape:", missing_landmark_embeddings.shape)
for i, layer in enumerate(self.dense.children()):
if isinstance(layer, nn.Linear):
print(f"Layer {i} weight shape:", layer.weight.shape)
print(x.size())
missing_landmark_embeddings = self.dense(x)
# Reshape missing_landmark_embeddings to (batch_size, num_landmarks, units)
missing_landmark_embeddings = missing_landmark_embeddings.view(batch_size, num_landmarks, self.units)
# If there are missing landmarks, use the empty embedding for those positions
missing_landmarks = missing_landmark_mask * self.empty_embedding
missing_landmarks += (1 - missing_landmark_mask) * missing_landmark_embeddings
# Concatenate the embeddings along the last dimension (features) to get the final embedded_landmark tensor
embedded_landmark = torch.cat([missing_landmarks, x.view(batch_size, num_landmarks, num_units)], dim=-1)
return embedded_landmark
## here is my model:
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
# Frames Input
self.frames_inp = nn.Identity()
# Embedding
self.embedding = Embedding()
print(NUM_BLOCKS_ENCODER)
# Encoder Transformer Blocks
self.encoder = Encoder(NUM_BLOCKS_ENCODER)
# Decoder
self.decoder = Decoder(NUM_BLOCKS_DECODER)
# Classifier
self.classifier = nn.Sequential(
nn.Dropout(CLASSIFIER_DROPOUT_RATIO),
nn.Linear(UNITS_DECODER, N_UNIQUE_CHARACTERS),
nn.Softmax(dim=-1),
)
def forward(self, frames):
# Frames
x = self.frames_inp(frames)
print(x)
# Embedding
x = self.embedding(x)
# Encoder Transformer Blocks
x = self.encoder(x)
# Decoder
x = self.decoder(x)
# Classifier
outputs = self.classifier(x)
return outputs
# Create PyTorch Model
model = Model()
# Loss Function with Label Smoothing
loss_fn = scce_with_ls
# Adam Optimizer with weight decay
optimizer = torch.optim.Adam(model.parameters(), weight_decay=1e-5)
# TopK Metrics
metrics = [
TopKAccuracy(1),
TopKAccuracy(5),
]
# Loss Weights (if needed)
loss_weights = None
# Move the model to GPU if available
if torch.cuda.is_available():
model = model.cuda()
## here is the code I am trying to run
from torch.utils.data import DataLoader, Dataset
# Define a custom dataset class
class CustomDataset(Dataset):
def __init__(self, X, y):
self.X = X
self.y = y
def __len__(self):
return len(self.X)
def __getitem__(self, idx):
frames = self.X[idx] # Access the 'frames' tensor directly
phrase = self.y[idx]
return frames, phrase
# Instantiate your custom dataset
train_dataset = CustomDataset(X_train, y_train)
# Instantiate the DataLoader with your custom dataset and batch size
train_dataloader = DataLoader(
train_dataset,
batch_size=BATCH_SIZE,
shuffle=True, # Set to True for training to shuffle the data
num_workers=0 # Increase this if you want to use multiple CPU cores for data loading
)
# Move the model to the GPU
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
model.to(device)
# Iterate through the data in batches
for batch in train_dataloader:
frames, phrase = batch
frames = frames.to(device) # Move the data to the GPU
phrase = phrase.to(device) # Move the data to the GPU
# Forward pass to get the predictions
outputs = model(frames)
# Convert the predictions to numpy array and append to the list
predictions.append(outputs.cpu().numpy())
# Concatenate all predictions into a single numpy array
y_pred = np.concatenate(predictions, axis=0)
print(f'# NaN Values In Predictions: {np.isnan(y_pred).sum()}')
这是我的错误消息:
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
Input In [38], in <cell line: 34>()
37 phrase = phrase.to(device) # Move the data to the GPU
39 # Forward pass to get the predictions
---> 40 outputs = model(frames)
42 # Convert the predictions to numpy array and append to the list
43 predictions.append(outputs.cpu().numpy())
File ~/.local/lib/python3.9/site-packages/torch/nn/modules/module.py:1501, in Module._call_impl(self, *args, **kwargs)
1496 # If we don't have any hooks, we want to skip the rest of the logic in
1497 # this function, and just call forward.
1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1499 or _global_backward_pre_hooks or _global_backward_hooks
1500 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1501 return forward_call(*args, **kwargs)
1502 # Do not call functions when jit is used
1503 full_backward_hooks, non_full_backward_hooks = [], []
Input In [34], in Model.forward(self, frames)
33 print(x)
35 # Embedding
---> 36 x = self.embedding(x)
38 # Encoder Transformer Blocks
39 x = self.encoder(x)
File ~/.local/lib/python3.9/site-packages/torch/nn/modules/module.py:1501, in Module._call_impl(self, *args, **kwargs)
1496 # If we don't have any hooks, we want to skip the rest of the logic in
1497 # this function, and just call forward.
1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1499 or _global_backward_pre_hooks or _global_backward_hooks
1500 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1501 return forward_call(*args, **kwargs)
1502 # Do not call functions when jit is used
1503 full_backward_hooks, non_full_backward_hooks = [], []
Input In [26], in Embedding.forward(self, x)
18 def forward(self, x):
19 # Normalize
20 # x = torch.where(
(...)
25
26 # Dominant Hand
---> 27 x = self.dominant_hand_embedding(x)
29 # Add Positional Encoding
30 x = x + self.positional_embedding
File ~/.local/lib/python3.9/site-packages/torch/nn/modules/module.py:1501, in Module._call_impl(self, *args, **kwargs)
1496 # If we don't have any hooks, we want to skip the rest of the logic in
1497 # this function, and just call forward.
1498 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
1499 or _global_backward_pre_hooks or _global_backward_hooks
1500 or _global_forward_hooks or _global_forward_pre_hooks):
-> 1501 return forward_call(*args, **kwargs)
1502 # Do not call functions when jit is used
1503 full_backward_hooks, non_full_backward_hooks = [], []
Input In [25], in LandmarkEmbedding.forward(self, x)
30 # print(x)
31 # print(self.dense[0].weight)
32 # print(self.dense.size())
34 print("x shape:", x.shape)
---> 35 print("missing_landmark_embeddings shape:", missing_landmark_embeddings.shape)
36 for i, layer in enumerate(self.dense.children()):
37 if isinstance(layer, nn.Linear):
UnboundLocalError: local variable 'missing_landmark_embeddings' referenced before assignment
I have tried print debugging but I still don't know what I am doing wrong. The size of frames is (1024, 128, 164)
.