BERT-Relation-Extraction icon indicating copy to clipboard operation
BERT-Relation-Extraction copied to clipboard

Infer from code (args.pkl)

Open igormis opened this issue 4 years ago • 5 comments

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.

igormis avatar Mar 12 '21 08:03 igormis

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

ManiBio avatar May 05 '22 10:05 ManiBio

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

ManiBio avatar May 18 '22 08:05 ManiBio

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?

minkz94 avatar May 03 '23 18:05 minkz94

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.

GabrielSoranzoUPEC avatar May 23 '23 10:05 GabrielSoranzoUPEC

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

Kashi-2002 avatar Apr 15 '24 07:04 Kashi-2002