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

Infer from code (args.pkl) #35

Open
igormis opened this issue Mar 12, 2021 · 5 comments
Open

Infer from code (args.pkl) #35

igormis opened this issue Mar 12, 2021 · 5 comments

Comments

@igormis
Copy link

igormis commented Mar 12, 2021

HI, I have problem when infering from code

from src.tasks.infer import infer_from_trained

inferer = infer_from_trained(args, detect_entities=False)
test = "The surprise [E1]visit[/E1] caused a [E2]frenzy[/E2] on the already chaotic trading floor."
inferer.infer_sentence(test, detect_entities=False)

It throws error that args is not defined, can u help me with which values to fill args. If I put None it needs the args.pkl, but I cannot find the pickle after the training for semeval task.

@MaxHockey
Copy link

Hi I also faced same problem, when infer from code,

inferer = infer_from_trained(args, detect_entities=True)
test2 = "After eating the chicken, he developed a sore throat the next morning."
inferer.infer_sentence(test2, detect_entities=True)```

The following output, I got
```FileNotFoundError                         Traceback (most recent call last)
[<ipython-input-61-23477a2b8548>](https://localhost:8080/#) in <module>()
      1 from src.tasks.infer import infer_from_trained
----> 2 inferer = infer_from_trained(None, detect_entities=True)
      3 test2 = "After eating the chicken, he developed a sore throat the next morning."
      4 inferer.infer_sentence(test2, detect_entities=True)

1 frames
[/content/BERT-Relation-Extraction/src/tasks/infer.py](https://localhost:8080/#) in load_pickle(filename)
     28     completeName = os.path.join("./data/",\
     29                                 filename)
---> 30     with open(completeName, 'rb') as pkl_file:
     31         data = pickle.load(pkl_file)
     32     return data

FileNotFoundError: [Errno 2] No such file or directory: './data/args.pkl'  ```



if  I gave args=None, it throws error like No such file 'args.pkl'
How do solve this issue?
Thank you

@MaxHockey
Copy link

Hi I also faced same problem, when infer from code,

inferer = infer_from_trained(args, detect_entities=True)
test2 = "After eating the chicken, he developed a sore throat the next morning."
inferer.infer_sentence(test2, detect_entities=True)```

The following output, I got
```FileNotFoundError                         Traceback (most recent call last)
[<ipython-input-61-23477a2b8548>](https://localhost:8080/#) in <module>()
      1 from src.tasks.infer import infer_from_trained
----> 2 inferer = infer_from_trained(None, detect_entities=True)
      3 test2 = "After eating the chicken, he developed a sore throat the next morning."
      4 inferer.infer_sentence(test2, detect_entities=True)

1 frames
[/content/BERT-Relation-Extraction/src/tasks/infer.py](https://localhost:8080/#) in load_pickle(filename)
     28     completeName = os.path.join("./data/",\
     29                                 filename)
---> 30     with open(completeName, 'rb') as pkl_file:
     31         data = pickle.load(pkl_file)
     32     return data

FileNotFoundError: [Errno 2] No such file or directory: './data/args.pkl'  ```



if  I gave args=None, it throws error like No such file 'args.pkl'
How do solve this issue?
Thank you

I solved this issue.

Actually the problem was , we trying to predict the relation to the given sentence in outside main_task.py

Inside main_task.py, you will add these lines, it works fine and it's not throws error.

inferer = infer_from_trained(args, detect_entities=True)
test2 = "After eating the chicken, he developed a sore throat the next morning."
print(inferer.infer_sentence(test2, detect_entities=True))

Because, in main_task.py script, have the args values.
here, args means it comprises of all arguments which passed by the user or default arguments.

Therefore, it threw error, when you tried to run the code outside main_task.py

If you want to predict the relation for the given sentence outside main_task.py, you will add the the following lines into the man_task.py script.
"""
args_file = os.path.join("./data", "args.pkl")
with open(args_file, 'wb') as handle:
pickle.dump(args, handle, protocol=pickle.HIGHEST_PROTOCOL)
"""

then you will run the main_task.py script with arguments which you want and then you will execute the predicting the relation scripts and give "args=None"
from src.tasks.infer import infer_from_trained
inferer = infer_from_trained(args=None, detect_entities=True)
test2 = "After eating the chicken, he developed a sore throat the next morning."
print(inferer.infer_sentence(test2, detect_entities=True))

kindly make sure that you import all the packages in the main_task.py

@minkz94
Copy link

minkz94 commented May 3, 2023

Hi,

Could you please tell me where exactly I need to insert:
"""
args_file = os.path.join("./data", "args.pkl")
with open(args_file, 'wb') as handle:
pickle.dump(args, handle, protocol=pickle.HIGHEST_PROTOCOL)
"""

I've tried running this code outside main_task.py on a separate jupyter notebook:
inferer = infer_from_trained(args=None, detect_entities=False)
test = "The surprise [E1]visit[/E1] caused a [E2]frenzy[/E2] on the already chaotic trading floor."
inferer.infer_sentence(test, detect_entities=False)

I'm getting this error:

AttributeError Traceback (most recent call last)
Cell In[3], line 1
----> 1 inferer = infer_from_trained(args=None, detect_entities=False)
2 test = "The surprise [E1]visit[/E1] caused a [E2]frenzy[/E2] on the already chaotic trading floor."
3 inferer.infer_sentence(test, detect_entities=False)

File ~\Documents\Work\Uni\ARU\Thesis\Experiment\BERT-Relation-Extraction\src\tasks\infer.py:53, in infer_from_trained.init(self, args, detect_entities)
50 logger.info("Loading tokenizer and model...")
51 from .train_funcs import load_state
---> 53 if self.args.model_no == 0:
54 from ..model.BERT.modeling_bert import BertModel as Model
55 model = args.model_size #'bert-base-uncased'

AttributeError: 'Relations_Mapper' object has no attribute 'model_no'

I'm also trying to run a fine-tuned model insteading of pre-training the whole model with cnn.txt so not sure if that's causing the error?

@GabrielSoranzoUPEC
Copy link

GabrielSoranzoUPEC commented May 23, 2023

You can write it after args = parser.parse_args()

Note: I had to write args=self.args in src/tasks/infer.py after self.args = load_pickle("args.pkl") line 37.

@Kashi-2002
Copy link

self.rm = load_pickle("relations.pkl") in infer.py is also missing. Where to get this from?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants