src.core.lstm package#

Submodules#

src.core.lstm.dataset module#

class src.core.lstm.dataset.LSTMDataset(paths, transforms, tokens=None)[source]#

Bases: Dataset

LSTM Dataset

Parameters:
  • paths (list) – Path to images

  • transforms (albumentations.Compose) – Image transforms

  • tokens (list, optional) – Captions tokens, defaults to None

src.core.lstm.dataset.create_dataloader(paths, tokens=None, transforms=None, val_split=0.2, batch_size=32, seed=2024, data='train')[source]#

Creates Train, Validation and Test DataLoader

Parameters:
  • paths (list) – Image paths

  • tokens (list, optional) – List of tokens, defaults to None

  • transforms (albumentations.Compose, optional) – Image transforms, defaults to None

  • val_split (float, optional) – validation split, defaults to 0.2

  • batch_size (int, optional) – Batch size, defaults to 32

  • seed (int, optional) – Seed, defaults to 2024

  • data (str, optional) – Type of data, defaults to “train”

Returns:

Train, Val / Test dataloaders

Return type:

tuple (torch.utils.data.DataLoader, torch.utils.data.DataLoader) / torch.utils.data.DataLoader

src.core.lstm.lstm module#

class src.core.lstm.lstm.LSTM(config_dict)[source]#

Bases: object

A class to run LSTM data preprocessing, training and inference

Parameters:

config_dict (dict) – Config Params Dictionary

run()[source]#

Runs LSTM Training and saves output

run_infer()[source]#

Runs inference

Returns:

Test image paths, True captions, Predicted captions

Return type:

tuple (list, list, list)

save_output()[source]#

Saves Training and Inference results

src.core.lstm.model module#

class src.core.lstm.model.LSTMCell(h_dim, inp_x_dim, out_x_dim)[source]#

Bases: Module

LSTM Cell

Parameters:
  • h_dim (int) – Hidden state vector dimension

  • inp_x_dim (int) – Input vector dimension

  • out_x_dim (int) – Output vector dimension

forward(ht_1, ct_1, xt)[source]#

Forward propogation

Parameters:
  • ht_1 (torch.Tensor (batch_size, h_dim)) – Hidden state vector

  • ct_1 (torch.Tensor (batch_size, h_dim)) – Cell stae vector

  • xt (torch.Tensor (batch_size, embed_dim)) – Input vector

Returns:

New hidden, cell states, output

Return type:

tuple (torch.Tensor [batch_size, h_dim], torch.Tensor [batch_size, h_dim], torch.Tensor [batch_size, out_dim])

class src.core.lstm.model.LSTMModel(config_dict)[source]#

Bases: Module

LSTM Architecture

Parameters:

config_dict (dict) – Config Params Dictionary

forward(images, tokens=None)[source]#

Forward propogation

Parameters:
  • images (torch.Tensor (batch_size, num_channels, h_dim, w_dim)) – Images tensor

  • tokens (torch.Tensor (batch_size, seq_len), optional) – Captions tokens, defaults to None

Returns:

Predicted captions

Return type:

torch.Tensor (batch_size, seq_len, num_vocab)

init_hidden()[source]#

Initialized hidden states

Returns:

List of hidden states

Return type:

list

class src.core.lstm.model.LSTMTrainer(model, optimizer, config_dict)[source]#

Bases: Module

LSTM Trainer

Parameters:
  • model (torch.nn.Module) – LSTM model

  • optimizer (torch.optim) – Optimizer

  • config_dict (dict) – Config Params Dictionary

calc_loss(y_pred, y_true)[source]#

Crossentropy loss for predicted tokens

Parameters:
  • y_pred (torch.Tensor (batch_size, seq_len, num_vocab)) – Predicted tokens

  • y_true (torch.Tensor (batch_size, seq_len)) – True tokens

Returns:

BCE Loss

Return type:

torch.float32

fit(train_loader, val_loader)[source]#

Fits the model on dataset. Runs training and Validation steps for given epochs and saves best model based on the evaluation metric

Parameters:
  • train_loader (torch.utils.data.DataLoader) – Train Data loader

  • val_loader (torch.utils.data.DataLoader) – Validaion Data Loader

Returns:

Training History

Return type:

dict

predict(data_loader)[source]#

Runs inference to predict a translation of soruce sentence

Parameters:

data_loader (torch.utils.data.DataLoader) – Infer Data loader

Returns:

Predicted tokens

Return type:

numpy.ndarray (num_samples, seq_len, num_vocab)

train_one_epoch(data_loader, epoch)[source]#

Train step

Parameters:
  • data_loader (torch.utils.data.Dataloader) – Train Data Loader

  • epoch (int) – Epoch number

Returns:

Train Losse, Train Metrics

Return type:

tuple (torch.float32, dict)

val_one_epoch(data_loader)[source]#

Validation step

Parameters:

data_loader (torch.utils.data.Dataloader) – Validation Data Loader

Returns:

Validation Losse, Validation Metrics

Return type:

tuple (torch.float32, dict)

Module contents#