Related
I am trying to train the spancat model without luck.
I am getting:
ValueError: [E143] Labels for component 'spancat' not initialized. This can be fixed by calling add_label, or by providing a representative batch of examples to the component's 'initialize' method.
I did convert my NER ents to spans:
def main(loc: Path, lang: str, span_key: str):
"""
Set the NER data into the doc.spans, under a given key.
The SpanCategorizer component uses the doc.spans, so that it can work with
overlapping or nested annotations, which can't be represented on the
per-token level.
"""
nlp = spacy.blank(lang)
docbin = DocBin().from_disk(loc)
docs = list(docbin.get_docs(nlp.vocab))
for doc in docs:
doc.spans[span_key] = list(doc.ents)
DocBin(docs=docs).to_disk(loc)
Here is my config file:
[paths]
train = null
dev = null
vectors = null
init_tok2vec = null
[system]
gpu_allocator = null
seed = 444
[nlp]
lang = "en"
pipeline = ["tok2vec","spancat"]
batch_size = 1000
disabled = []
before_creation = null
after_creation = null
after_pipeline_creation = null
tokenizer = {"#tokenizers":"spacy.Tokenizer.v1"}
[components]
[components.spancat]
factory = "spancat"
max_positive = null
scorer = {"#scorers":"spacy.spancat_scorer.v1"}
spans_key = "sc"
threshold = 0.5
[components.spancat.model]
#architectures = "spacy.SpanCategorizer.v1"
[components.spancat.model.reducer]
#layers = "spacy.mean_max_reducer.v1"
hidden_size = 128
[components.spancat.model.scorer]
#layers = "spacy.LinearLogistic.v1"
nO = null
nI = null
[components.spancat.model.tok2vec]
#architectures = "spacy.Tok2VecListener.v1"
width = ${components.tok2vec.model.encode.width}
upstream = "*"
[components.spancat.suggester]
#misc = "spacy.ngram_suggester.v1"
sizes = [1,2,3]
[components.tok2vec]
factory = "tok2vec"
[components.tok2vec.model]
#architectures = "spacy.Tok2Vec.v2"
[components.tok2vec.model.embed]
#architectures = "spacy.MultiHashEmbed.v2"
width = ${components.tok2vec.model.encode.width}
attrs = ["NORM","PREFIX","SUFFIX","SHAPE"]
rows = [5000,1000,2500,2500]
include_static_vectors = true
[components.tok2vec.model.encode]
#architectures = "spacy.MaxoutWindowEncoder.v2"
width = 256
depth = 8
window_size = 1
maxout_pieces = 3
[corpora]
[corpora.dev]
#readers = "spacy.Corpus.v1"
path = ${paths.dev}
max_length = 0
gold_preproc = false
limit = 0
augmenter = null
[corpora.train]
#readers = "spacy.Corpus.v1"
path = ${paths.train}
max_length = 0
gold_preproc = false
limit = 0
augmenter = null
[training]
dev_corpus = "corpora.dev"
train_corpus = "corpora.train"
max_epochs = 70
seed = ${system.seed}
gpu_allocator = ${system.gpu_allocator}
dropout = 0.1
accumulate_gradient = 1
patience = 1600
max_steps = 20000
eval_frequency = 200
frozen_components = []
annotating_components = []
before_to_disk = null
[training.batcher]
#batchers = "spacy.batch_by_words.v1"
discard_oversize = false
tolerance = 0.2
get_length = null
[training.batcher.size]
#schedules = "compounding.v1"
start = 100
stop = 1000
compound = 1.001
t = 0.0
[training.logger]
#loggers = "spacy.ConsoleLogger.v1"
progress_bar = false
[training.optimizer]
#optimizers = "Adam.v1"
beta1 = 0.9
beta2 = 0.999
L2_is_weight_decay = true
L2 = 0.01
grad_clip = 1.0
use_averages = false
eps = 0.00000001
learn_rate = 0.001
[training.score_weights]
spans_sc_f = 1.0
spans_sc_p = 0.0
spans_sc_r = 0.0
[pretraining]
[initialize]
vectors = ${paths.vectors}
init_tok2vec = ${paths.init_tok2vec}
vocab_data = null
lookups = null
before_init = null
after_init = null
[initialize.components]
[initialize.tokenizer]
I am using the "sc" key. Please advise how to solve it.
I have solved it using the following function, but one should address the spans Span(doc, start, end, label) according to the project/text for their task. It worked for me because all the text (a few words in my case) are labeled with a label and this is my need.
def convert_to_docbin(input, output_path="./train.spacy", lang='en'):
""" Convert a pair of text annotations into DocBin then save """
# Load a new spacy model:
nlp = spacy.blank(lang)
# Create a DocBin object:
db = DocBin()
for text, annotations in input: # Data in previous format
doc = nlp(text)
ents = []
spans = []
for start, end, label in annotations: # Add character indexes
spans.append(Span(doc, 0, len(doc), label=label))
span = doc.char_span(start, end, label=label)
ents.append(span)
doc.ents = ents # Label the text with the ents
group = SpanGroup(doc, name="sc", spans=spans)
doc.spans["sc"] = group
db.add(doc)
db.to_disk(output_path)
I want to do 3D-reconstruction using structure-from-motion algorithm. I am using opencv to do this in python. But some how the obtained pointcloud is breaking into 2 halves. My input images are:
Image 1
Image 2
Image 3.
I am matching every 2 images like image1 with image2 and image2 with image 3. I tried different feature detectors like SIFT, KAZE and SURF. With the obtained points I compute the essential matrix. I got the camera intrinsics from the camera calibration from Opencv and are stored in the variables 'mtx' and 'dist' in the code below.
```file = os.listdir('Path_to _images')
file.sort(key=lambda f: int(''.join(filter(str.isdigit,f))))
path = os.path.join(os.getcwd(),'Path_to_images/')
for i in range(0, len(file)-1):
if(i == len(file) - 1):
break
path1 = cv2.imread(path + file[i], 0)
path1 = cv2.equalizeHist(path1)
path2 = cv2.imread(path + file[i+1], 0)
path2 = cv2.equalizeHist(path2)
# Feature Detection #
sift = cv2.xfeatures2d.SIFT_create()
kp1, des1 = sift.detectAndCompute(path1,None)
kp2, des2 = sift.detectAndCompute(path2,None)
# Feature Matching #
FLANN_INDEX_KDTREE = 0
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
search_params = dict(checks=50)
flann = cv2.FlannBasedMatcher(index_params,search_params)
matches = flann.knnMatch(des1,des2,k=2)
good = []
pts1 = []
pts2 = []
for j, (m,n) in enumerate(matches):
if m.distance < 0.8*n.distance:
good.append(m)
pts2.append(kp2[m.trainIdx].pt)
pts1.append(kp1[m.queryIdx].pt)
pts1 = np.int32(pts1)
pts2 = np.int32(pts2)
pts1 = np.array([pts1],dtype=np.float32)
pts2 = np.array([pts2],dtype=np.float32)
# UNDISTORTING POINTS #
pts1_norm = cv2.undistortPoints(pts1, mtx, dist)
pts2_norm = cv2.undistortPoints(pts2, mtx, dist)
# COMPUTE FUNDAMENTAL MATRIX #
F, mask = cv2.findFundamentalMat(pts1_norm,pts2_norm,cv2.FM_LMEDS)
# COMPUTE ESSENTIAL MATRIX #
E, mask = cv2.findEssentialMat(pts1_norm, pts2_norm, focal=55.474, pp=(33.516, 16.630), method=cv2.FM_LMEDS, prob=0.999, threshold=3.0)
# POSE RECOVERY #
points, R, t, mask = cv2.recoverPose(E, pts1_norm, pts2_norm)
anglesBetweenImages = rotationMatrixToEulerAngles(R)
sys.stdout = open('path_to_folder/angles.txt', 'a')
print(anglesBetweenImages)
# COMPOSE PROJECTION MATRIX OF R, t #
matrix_1 = np.hstack((R, t))
matrix_2 = np.hstack((np.eye(3, 3), np.zeros((3, 1))))
projMat_1 = np.dot(mtx, matrix_1)
projMat_2 = np.dot(mtx, matrix_2)
# TRIANGULATE POINTS #
point_4d_hom = cv2.triangulatePoints(projMat_1[:3], projMat_2[:3], pts1[:2].T, pts2[:2].T)
# HOMOGENIZE THE 4D RESULT TO 3D #
point_4d = point_4d_hom
point_3d = point_4d[:3, :].T # Obtains 3D points
np.savetxt('/path_to_folder/'+ file[i] +'.txt', point_3d)
After cv2.triangulatePoints, I expected to obtain one pointcloud. But the result I got has 2 surfaces as shown in the image below.
Result 1.
I really appreciate if anyone can tell me what is going wrong with my algorithm. Thanks!
you need to do this interativilly
like this:
cv::Mat pointsMat1(2, 1, CV_64F);
cv::Mat pointsMat2(2, 1, CV_64F);
int size0 = m_history.getHistorySize();
for(int i = 0; i < size0; i++){
cv::Point pt1 = m_history.getOriginalPoint(0, i);
cv::Point pt2 = m_history.getOriginalPoint(1, i);
pointsMat1.at<double>(0,0) = pt1.x;
pointsMat1.at<double>(1,0) = pt1.y;
pointsMat2.at<double>(0,0) = pt2.x;
pointsMat2.at<double>(1,0) = pt2.y;
cv::Mat pnts3D(4, 1, CV_64F);
cv::triangulatePoints(m_projectionMat1, m_projectionMat2, pointsMat1, pointsMat2, pnts3D);
}
I'm writing serial monitor data from arduino to a csv file. But this error pops up every time:
"_line 27, in <module>
fR = float (dataarray[2])
ValueError: could not convert string to float:_"
ValueError, it's not only for 'fR' variable but for other variables too and not at the same runtime.
This is how the Serial monitor output looks like:
1,31.00,,195.00,0.95,-21.53,67.58,70.90,21.61,-12886,32468,3748,-438,-753,340,233.00
2,39.00,0.00,209.00,1.02,-21.32,67.73,70.99,21.66,-12750,32468,3844,-579,-780,345,526.00
3,39.00,0.00,221.00,1.08,-21.28,67.79,71.33,21.85,-12722,32468,3796,-688,-775,411,138.00
4,39.00,0.00,231.00,1.13,-21.55,67.56,71.33,21.85,-12898,32468,3748,-455,-755,332,601.00
import serial
import csv
Arduino = serial.Serial('COM5',9600)
Arduino.flush()
Arduino.reset_input_buffer()
while True:
while (Arduino.inWaiting()==0):
pass
data = Arduino.readline()
dataarray = data.decode().rstrip().split(',')
Arduino.reset_input_buffer()
iteration = int(dataarray[0])
hum = float (dataarray[1])
fR = float (dataarray[2])
ppm = float (dataarray[3])
Volt = float (dataarray[4])
pitch = float (dataarray[5])
roll = float (dataarray[6])
Tf = float (dataarray[7])
Tc = float (dataarray[8])
AcX = float (dataarray[9])
AcY = float (dataarray[10])
AcZ = float (dataarray[11])
GyX = float (dataarray[12])
GyY = float (dataarray[13])
GyZ = float (dataarray[14])
Knock = float (dataarray[15])
with open('DB_Store.csv', 'a') as outfile:
outfileWrite = csv.writer(outfile)
while True:
outfileWrite.writerow([iteration,hum,fR,ppm,Volt,pitch,roll,Tf,Tc,AcX,AcY,AcZ,GyX,GyY,GyZ,Knock])
outfile.close()
At every execution, the error changes without editing anything, although it still remains ValueError!! I don't understand how. This is a basic code to output the data to csv.
eg:
Run-1:
"fR = float (dataarray[2])
ValueError: could not convert string to float: "
+++++++++++++++++++++++++++++++
Run-2:
"iteration = int(dataarray[0])
ValueError: invalid literal for int() with base 10:"
and so on....
I have a script that produces 4 images (Below I only include 2 as an example of output). I have another function that determines what % cat or dog the picture is and I would like to call that function in the title of the following code
import cv2
import matplotlib.pyplot as plt
def Mpic():
plt.figure(figsize=(15,30))
path = r"data/dogscats1/pupper"
path1 = r"data/dogscatspeople/test1"
path2 = r"data/dogscatspeople/test1"
path3 = r"data/dogscats1/pupper"
imgpath1 = path + "/cat.jpg"
imgpath2 = path1 + "/1.jpg"
imgpath3 = path2 + "/2.jpg"
imgpath4 = path3 + "/dog.jpg"
img1 = cv2.imread(imgpath1, 1)
img2 = cv2.imread(imgpath2, 1)
img3 = cv2.imread(imgpath3, 1)
img4 = cv2.imread(imgpath4, 1)
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
img3 = cv2.cvtColor(img3, cv2.COLOR_BGR2RGB)
img4 = cv2.cvtColor(img4, cv2.COLOR_BGR2RGB)
titles = ['Kitty', '% Cat = , Dog % = ','% Cat = , Dog % =', 'Pupper']
images = [img1, img2, img3, img4]
for i in range(4):
plt.subplot(4,2,i+1)
plt.imshow(images[i])
plt.xticks([])
plt.title(titles[i])
plt.yticks([])
plt.show()
if __name__ == "__main__":
Mpic()
This is the original function that calls the array:
def pred_datsci(file_path):
prev_precompute = learn.precompute
learn.precompute = False
try:
trn_tfms, val_tfms = tfms_from_model(arch,sz)
test_img = open_image(file_path)
im = val_tfms(test_img)
pred = learn.predict_array(im[None])
class_index = (np.exp(pred))
class_index1 = np.argmax(np.exp(pred))
print(class_index*100)
return data.classes[class_index1]
finally:
learn.precompute = prev_precompute
Which can return something along the lines of:
pred_datsci(f"data/dogscats1/valid/dogs/12501.jpg")
I want it to call it in the form of something like this:
titles = [ cat % = pred_datsci(f"data/dogscats1/valid/cats/cat.1.jpg"),"etc"]
titles = [ "cat % = {}".format(pred_datsci("data/dogscats1/valid/cats/cat.1.jpg")),"etc"]
import numpy as np
udacity_set = np.array(
[[1,1,1,0],
[1,0,1,0],
[0,1,0,1],
[1,0,0,1]])
label = udacity_set[:,udacity_set.shape[1]-1]
fx = label.size
positive = label[label == 1].shape[0]
positive_probability = positive/fx
negative = label[label == 0].shape[0]
negative_probability = negative/fx
entropy = -negative_probability*np.log2(negative_probability) - positive_probability*np.log2(positive_probability)
atribute = 0
V = 1
attribute_set = udacity_set[np.where(udacity_set[:,atribute] == 1)] #selecting positive instance of occurance in attribute 14
instances = attribute_set.shape[0]
negative_labels = attribute_set[np.where(attribute_set[:,attribute_set.shape[1]-1]== 0)].shape[0]
positive_labels = attribute_set[np.where(attribute_set[:,attribute_set.shape[1]-1]== 1)].shape[0]
p0 = negative_labels/instances
p1 = positive_labels/instances
entropy2 = - p0*np.log2(p0) - p1*np.log2(p1)
attribute_set2 = udacity_set[np.where(udacity_set[:,atribute] == 0)] #selecting positive instance of occurance in attribute 14
instances2 = attribute_set2.shape[0]
negative_labels2 = attribute_set[np.where(attribute_set2[:,attribute_set2.shape[1]-1]== 0)].shape[0]
positive_labels2 = attribute_set[np.where(attribute_set2[:,attribute_set2.shape[1]-1]== 1)].shape[0]
p02 = negative_labels2/instances2
p12 = positive_labels2/instances2
entropy22 = - p02*np.log2(p02) - p12*np.log2(p12)
Problem is when attribute is pure and entropy is meant to be 0. But when i put this into a formula i get NaN. I know how to code workaround, but why is this formula rigged?