ValueError: [E143] Labels for component 'tagger' not initialized - named-entity-recognition

I've been following this tutorial to create a custom NER. However, I keep getting this error:
ValueError: [E143] Labels for component 'tagger' not initialized. This can be fixed by calling add_label, or by providing a representative batch of examples to the component's initialize method.
This is how I defined the spacy model:
import spacy
from spacy.tokens import DocBin
from tqdm import tqdm
nlp = spacy.blank("ro") # load a new spacy model
source_nlp = spacy.load("ro_core_news_lg")
nlp.tokenizer.from_bytes(source_nlp.tokenizer.to_bytes())
nlp.add_pipe("tagger", source=source_nlp)
doc_bin = DocBin() # create a DocBin object

I just meet the same problem. The picture of setting the config file is misleading you.
If you just want to run through the tutrital, you can set the config file like this.
only click the check box on ner

Related

How to run huggingface Helsinki-NLP models

I am trying to use the Helsinki-NLP models from huggingface, but
I cannot find any instructions on how to do it.
The README files are computer generated and do not contain explanations.
Can some one point me to a getting started guide, or show an example of how to run a model like opus-mt-en-es?
On the model's page here there's a Use in Transformers link that you can use to see the code to load it in their transformers package as shown below:
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
tokenizer = AutoTokenizer.from_pretrained("Helsinki-NLP/opus-mt-es-en")
model = AutoModelForSeq2SeqLM.from_pretrained("Helsinki-NLP/opus-mt-es-en")
then use it as you would any transformer model:
inp = "Me llamo Wolfgang y vivo en Berlin"
input_ids = tokenizer(inp, return_tensors="pt").input_ids
outputs = model.generate(input_ids=input_ids, num_beams=5, num_return_sequences=3)
print("Generated:", tokenizer.batch_decode(outputs, skip_special_tokens=True))
Output:
Generated: ['My name is Wolfgang and I live in Berlin', 'My name is Wolfgang and I live in Berlin.', "My name's Wolfgang and I live in Berlin."]
To use on the fly, you can check the huggingFace course here. They provide pipelines that help you run this on the fly, consider:
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-es-en")
translator("your-text-to-translate-here")

Unable to use openvino model

I am using person-detection-action-recognition-0005 pre-trained model from openvino to detect the person and their action.
https://docs.openvinotoolkit.org/latest/_models_intel_person_detection_action_recognition_0005_description_person_detection_action_recognition_0005.html
From the above link, I wrote a python script to get detections.
This is the script.
import cv2
def main():
print(cv2.__file__)
frame = cv2.imread('/home/naveen/Downloads/person.jpg')
actionNet = cv2.dnn.readNet('person-detection-action-recognition-0005.bin',
'person-detection-action-recognition-0005.xml')
actionBlob = cv2.dnn.blobFromImage(frame, size=(680, 400))
actionNet.setInput(actionBlob)
# detection output
actionOut = actionNet.forward(['mbox_loc1/out/conv/flat',
'mbox_main_conf/out/conv/flat/softmax/flat',
'out/anchor1','out/anchor2',
'out/anchor3','out/anchor4'])
# this is the part where I dont know how to get person bbox
# and action label for those person fro actionOut
for detection in actionOut[2].reshape(-1, 3):
print('sitting ' +str( detection[0]))
print('standing ' +str(detection[1]))
print('raising hand ' +str(detection[2]))
Now, I don't know how to get bbox and action label from the output variable(actionOut). I am unable to find any documentation or blog explaining this.
Does someone have any idea or suggestion, how it can be done?
There is a demo called smart_classroom_demo: link
This demo uses the network you are trying to run.
The parsing of outputs is located here
The implementation is in C++ but it should help you to understand how outputs of the network are parsed.
Hope it will help.

unknown url type: '//drive.google.com/drive/folders/11XfAPOgFv7qJbdUdPpHKy8pt6aItGvyg'

I am trying to use Haar cascade classifier for object detection.I have copied a code for haar cascade algorithm but its not working.It's giving error as
unknown url type: '//drive.google.com/drive/folders/11XfAPOgFv7qJbdUdPpHKy8pt6aItGvyg'
even though this link is working.
import urllib.request, urllib.error, urllib.parse
import cv2
import os
def store_raw_images():
neg_images_link = '//drive.google.com/drive/folders/11XfAPOgFv7qJbdUdPpHKy8pt6aItGvyg'
neg_image_urls = urllib.request.urlopen(neg_images_link).read().decode()
pic_num = 1
if not os.path.exists('neg'):
os.makedirs('neg')
for i in neg_image_urls.split('\n'):
try:
print(i)
urllib.request.urlretrieve(i, "neg/"+str(pic_num)+".jpg")
img = cv2.imread("neg/"+str(pic_num)+".jpg",cv2.IMREAD_GRAYSCALE)
# should be larger than samples / pos pic (so we can place our image on it)
resized_image = cv2.resize(img, (100, 100))
cv2.imwrite("neg/"+str(pic_num)+".jpg",resized_image)
pic_num += 1
except Exception as e:
print(str(e))
store_raw_images()
I am expecting output as set of negative images for creating dataset module for object detection.
I think the missing "https:" at the start of the url is the causing the specific error.
Furthermore, you cannot just load a drive folder when it is not shared (you should use the drive link) and event then it is not optimal, you have to parse the html response and it may not even work.
I strongly suggest you to use a normal HTTP server or the Google Drive python API.

how to install model_evaluation_utils

I'm trying to model evaluate the performance of our deep learning models. And below is my code. However, I still get
No module named 'model_evaluation_utils'
Is there any pip installation or conda that could solve this problem?
from keras.preprocessing.image import load_img, img_to_array, array_to_img
from keras.models import load_model
import model_evaluation_utils as meu # throws error
Implementation of "model_evaluation_utils" can be found in the following github repository:
link
I think it's not a publicly available library, there must be a model_evaluation_utils.py file which is imported in the code and is used.

How to use K.get_session in Tensorflow 2.0 or how to migrate it?

def __init__(self, **kwargs):
self.__dict__.update(self._defaults) # set up default values
self.__dict__.update(kwargs) # and update with user overrides
self.class_names = self._get_class()
self.anchors = self._get_anchors()
self.sess = K.get_session()
RuntimeError: get_session is not available when using TensorFlow 2.0.
Tensorflow 2.0 does not expose the backend.get_session directly any more but the code still there and expose for tf1.
https://github.com/tensorflow/tensorflow/blob/r2.0/tensorflow/python/keras/backend.py#L465
You can use it with tf1 compatible interface:
sess = tf.compat.v1.keras.backend.get_session()
Or import tenforflow backend with internal path:
import tensorflow.python.keras.backend as K
sess = K.get_session()
In order to avoid using get_session after tensorflow 2.0 upgrade, Use tf.distribute.Strategy to get model. To load model, use tf.keras.models.load_model
import tensorflow as tf
another_strategy = tf.distribute.MirroredStrategy()
with another_strategy.scope():
model = Service.load_deep_model()
def load_deep_model(self, model):
loaded_model = tf.keras.models.load_model("model.h5")
return loaded_model
Hope this helps. As this worked for me.
I have tried to explain same at this utility article as well. https://www.javacodemonk.com/runtimeerror-get_session-is-not-available-when-using-tensorflow-2-0-f7238546
Probably has something to do with tf 2.0 eager execution that is enabled by default.
Try
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
I had the same error and tried installing and uninstalling. In the end, I found that the library was not actually installed correctly.
I went through each library in my:
C:\Users\MyName\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\
I tracked down the file in within the site-packages in Keras, which was calling from the Tensorflow library, which was calling from another folder. I found the final folder had the get_session(), but this was not being called in. When I checked the directory, I found that get_session wasn't being loaded in. Within the file directory /tensorflow/keras/backend.py it was importing everything, but missed out the get_session.
To fix this I added this line:
from tensorflow.python.keras.backend import get_session
Then saved it. The next time I ran my code it was fine.
I gave the same answer for this page How to fix ' module 'keras.backend.tensorflow_backend' has no attribute '_is_tf_1''

Resources