question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

simpletransformers model trained on Colab doesn't work locally

See original GitHub issue

Hello! First of all, thank you for this library! It is awesome! I encountered the following problem while trying to work with simpletransformers: I’ve followed the instructions in this link and trained a BERT model on Google Colab. I then downloaded it and tried to use it locally on my computer. But something went wrong.

Here is my code for training the model (on Google Colab):

from simpletransformers.classification import ClassificationModel
train_args ={"reprocess_input_data": True,
             "fp16":False,
             "num_train_epochs": 1,
             "save_steps": 100_000,
             "logging_steps": 100}

model = ClassificationModel(
    "bert", "bert-base-german-cased",
    num_labels=21,
    args=train_args
)
model.train_model(train_df)

It worked completely fine on Colab but I need it locally, so I downloaded the archive that was created using the following code:

import os
import tarfile

def pack_model(model_path='',file_name=''):
  files = [files for root, dirs, files in os.walk(model_path)][0]
  with tarfile.open(file_name+ '.tar.gz', 'w:gz') as f:
    for file in files:
      f.add(f'{model_path}/{file}')

pack_model('outputs', 'model')

I then unpacked it and placed in the directory with the file with the following code for testing the model:

from simpletransformers.classification import ClassificationModel

train_args ={"reprocess_input_data": True,
             "fp16":False,
             "num_train_epochs": 1,
             "save_steps": 100_000,
             "logging_steps": 100}

model = ClassificationModel(
    "bert", "outputs/",
    num_labels=21,
    args=train_args,
    use_cuda=False
)

test = "Boris Johnson hält eine Pressekonferenz zum Coronavirus-Aktionsplan der Regierung"

predictions, raw_outputs = model.predict([test])
print([predictions[0])

So the only difference in how I create models is use_cuda=False (because I don’t have a GPU locally).

When I executed the code the following output appeared:

I0526 17:16:12.761774 14508 file_utils.py:39] PyTorch version 1.5.0+cpu available.
I0526 17:16:32.703790 14508 configuration_utils.py:283] loading configuration file outputs/config.json
I0526 17:16:32.706786 14508 configuration_utils.py:321] Model config BertConfig {
  "architectures": [
    "BertForSequenceClassification"
  ],
  "attention_probs_dropout_prob": 0.1,
  "hidden_act": "gelu",
  "hidden_dropout_prob": 0.1,
  "hidden_size": 768,
  "id2label": {
    "0": "LABEL_0",
    "1": "LABEL_1",
    "2": "LABEL_2",
    "3": "LABEL_3",
    "4": "LABEL_4",
    "5": "LABEL_5",
    "6": "LABEL_6",
    "7": "LABEL_7",
    "8": "LABEL_8",
    "9": "LABEL_9",
    "10": "LABEL_10",
    "11": "LABEL_11",
    "12": "LABEL_12",
    "13": "LABEL_13",
    "14": "LABEL_14",
    "15": "LABEL_15",
    "16": "LABEL_16",
    "17": "LABEL_17",
    "18": "LABEL_18",
    "19": "LABEL_19",
    "20": "LABEL_20"
  },
  "initializer_range": 0.02,
  "intermediate_size": 3072,
  "label2id": {
    "LABEL_0": 0,
    "LABEL_1": 1,
    "LABEL_10": 10,
    "LABEL_11": 11,
    "LABEL_12": 12,
    "LABEL_13": 13,
    "LABEL_14": 14,
    "LABEL_15": 15,
    "LABEL_16": 16,
    "LABEL_17": 17,
    "LABEL_18": 18,
    "LABEL_19": 19,
    "LABEL_2": 2,
    "LABEL_20": 20,
    "LABEL_3": 3,
    "LABEL_4": 4,
    "LABEL_5": 5,
    "LABEL_6": 6,
    "LABEL_7": 7,
    "LABEL_8": 8,
    "LABEL_9": 9
  },
  "layer_norm_eps": 1e-12,
  "max_position_embeddings": 512,
  "model_type": "bert",
  "num_attention_heads": 12,
  "num_hidden_layers": 12,
  "pad_token_id": 0,
  "type_vocab_size": 2,
  "vocab_size": 30000
}

I0526 17:16:32.723798 14508 modeling_utils.py:648] loading weights file outputs/pytorch_model.bin
I0526 17:16:41.755029 14508 tokenization_utils.py:929] Model name 'outputs/' not found in model shortcut name list (bert-base-uncased, bert-large-uncased, bert-base-cased, bert-large-cased, bert-base-multilingual-uncased, bert-base-multilingual-cased, bert-base-chinese, bert-base-german-cased, bert-large-uncased-whole-word-masking, bert-large-cased-whole-word-masking, bert-large-uncased-whole-word-masking-finetuned-squad, bert-large-cased-whole-word-masking-finetuned-squad, bert-base-cased-finetuned-mrpc, bert-base-german-dbmdz-cased, bert-base-german-dbmdz-uncased, bert-base-finnish-cased-v1, bert-base-finnish-uncased-v1, bert-base-dutch-cased). Assuming 'outputs/' is a path, a model identifier, or url to a directory containing tokenizer files.
I0526 17:16:41.770021 14508 tokenization_utils.py:958] Didn't find file outputs/added_tokens.json. We won't load it.
I0526 17:16:41.779017 14508 tokenization_utils.py:1013] loading file outputs/vocab.txt
I0526 17:16:41.788011 14508 tokenization_utils.py:1013] loading file None
I0526 17:16:41.794008 14508 tokenization_utils.py:1013] loading file outputs/special_tokens_map.json
I0526 17:16:41.806000 14508 tokenization_utils.py:1013] loading file outputs/tokenizer_config.json
I0526 17:16:41.986897 14508 classification_model.py:801]  Converting to features started. Cache is not used.

  0%|          | 0/1 [00:00<?, ?it/s]

And it freezes on this moment. Nothing happens despite this progress bar. What should I do to make it work locally? Maybe there is another way?

My system:

  • Windows 10 Home
  • Python: 3.7.2
  • simpletransformers: 0.29.1

Thanks in advance for any help!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
moose-in-australiacommented, Jun 23, 2020

Problem solved, thank you! Turning off multiprocessing worked.

0reactions
g-laz77commented, Sep 7, 2020

By closing the threads, I mean adding p.close() and p.join()

if use_multiprocessing:
        with Pool(process_count) as p:
            features = list(
                tqdm(
                    p.imap(convert_example_to_feature, examples, chunksize=args.multiprocessing_chunksize),
                     total=len(examples),
                     disable=silent,
                )
            )
            p.close()
            p.join()
 else:
            features = [convert_example_to_feature(example) for example in tqdm(examples, disable=silent)]
 return features

However, I found that ThreadPoolExecuter() in the concurrent.futures library, works faster In convert_example_to_features(), the following snippet seems to work faster:

if use_multiprocessing:
        executor = ThreadPoolExecutor(max_workers=process_count)
        futures = []
        features = []
        for i in tqdm(range(len(examples))):
            futures.append(executor.submit(convert_example_to_feature, examples[i]))

        features = [future.result() for future in futures]
        executor.shutdown()
 else:
            features = [convert_example_to_feature(example) for example in tqdm(examples, disable=silent)]

return features
Read more comments on GitHub >

github_iconTop Results From Across the Web

simpletransformers model trained on Colab doesn't work locally
Here is my code for training the model (on Google Colab): from simpletransformers.classification import ClassificationModel train_args ...
Read more >
General Usage - Simple Transformers
Importing the task-specific model; Loading a pre-trained model; Loading a community model; Loading a local save. To CUDA or not to CUDA.
Read more >
Download models for local loading - Hugging Face Forums
Specifically, I'm using simpletransformers (built on top of ... where can I download this pretrained model so that I can load it locally?...
Read more >
Part 22, ChemBERTa - | notebook.community
Part 22, ChemBERTa: Pre-training a BERT-like model for masked language modelling of SMILES and molecular property prediction. ... Deep learning for chemistry and ......
Read more >
simpletransformers - PyPI
The high-level process of using Simple Transformers models follows the ... Any pretrained model of that type found in the Hugging Face docs...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found