OpenNMT Forum

Error: OpenNMT-py Model Conversion to OpenNMT-CTranslate2 Model

Hi,

I am trying to convert a model trained using OpenNMT-py to CTranslate2 model and facing issues.

I am using the following code for conversion:

converter = ctranslate2.converters.OpenNMTPyConverter(model_path)
converter.convert(output_dir, ctranslate2.specs.TransformerBase())

Error encountered:

self = <ctranslate2.converters.opennmt_py.OpenNMTPyConverter object at 0x11eeb9fd0>
model_spec = <ctranslate2.specs.catalog.TransformerBase object at 0x11eeb9f28>
    def _load(self, model_spec):
        import torch
        checkpoint = torch.load(self._model_path, map_location="cpu")
        variables = checkpoint["model"]
>       variables["generator.weight"] = checkpoint["generator"]["0.weight"]
E       KeyError: '0.weight'
../ctranslate2/converters/opennmt_py.py:24: KeyError

Command used for training the model:

`python ./OpenNMT-py/train.py -save_model model_dir/ \`
`                -save_checkpoint_steps 1000 \`
`                -data data/data \`
`                -copy_attn \`
`                -global_attention mlp \`
`                -word_vec_size 300 \`
`                -pre_word_vecs_enc "data/embeddings.enc.pt" \`
`                -fix_word_vecs_enc \`
`                -pre_word_vecs_dec "data/embeddings.dec.pt" \`
`                -fix_word_vecs_dec \`
`                -rnn_size 512 \`
`                -layers 1 \`
`                -encoder_type brnn \`
`                -train_steps 100000 \`
`                -max_grad_norm 2 \`
`                -dropout 0. \`
`                -batch_size 16 \`
`                -valid_batch_size 16 \`
`                -optim adagrad \`
`                -learning_rate 0.15 \`
`                -adagrad_accumulator_init 0.1 \`
`                -reuse_copy_attn \`
`                -copy_loss_by_seqlength \`
`                -bridge \`
`                -seed 777 \`
`                -world_size 1 \`
`                -gpu_ranks 0`

Please help in resolving this issue.

Thanks!

CTranslate2 is only compatible with Transformer models: https://github.com/OpenNMT/CTranslate2#converting-models