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

nlp #238

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

nlp #238

Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/my_squad_answer.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
copenhagen telephone exchange
1 change: 1 addition & 0 deletions data/my_squad_question.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Where did Erlang work?
1 change: 1 addition & 0 deletions data/my_squad_source.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Erlang worked for the Copenhagen Telephone Exchange and wanted to analyze and optimize its operations
1 change: 1 addition & 0 deletions modules/7_nlp/include/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ class SQuADModel {
private:
Tokenizer tokenizer;
InferenceEngine::InferRequest req;
std::string outputName;
};
2 changes: 1 addition & 1 deletion modules/7_nlp/include/tokenizer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ class Tokenizer {
std::vector<int> tokensToIndices(const std::vector<std::string>& tokens, int maxNumTokens=128);

private:
std::vector<std::string> vocab;
std::map<std::string, int> vocabMap;
std::vector<std::string> vocab;
};
46 changes: 41 additions & 5 deletions modules/7_nlp/src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ using namespace InferenceEngine;
using namespace cv;
using namespace cv::utils::fs;

Blob::Ptr wrapVecToBlob(std::vector<int> v) {
dkurt marked this conversation as resolved.
Show resolved Hide resolved
std::vector<size_t> dims = {1, v.size()};
return make_shared_blob<int32_t>(TensorDesc(Precision::I32, dims, Layout::NC), (int*)v.data());
}

SQuADModel::SQuADModel() : tokenizer(join(DATA_FOLDER, "bert-large-uncased-vocab.txt")) {
Core ie;

// Load deep learning network into memory
CNNNetwork net = ie.ReadNetwork(join(DATA_FOLDER, "distilbert.xml"),
join(DATA_FOLDER, "distilbert.bin"));

InputInfo::Ptr inputInfo = net.getInputsInfo()["input.1"];
// inputInfo->setLayout(Layout::HW);
inputInfo->setPrecision(Precision::I32);
outputName = net.getOutputsInfo().begin()->first;
// Initialize runnable object on CPU device
ExecutableNetwork execNet = ie.LoadNetwork(net, "CPU");

// Create a single processing thread
req = execNet.CreateInferRequest();
}
Expand All @@ -39,8 +46,37 @@ std::string SQuADModel::getAnswer(const std::string& question, const std::string
tokens.push_back("[SEP]");

std::vector<int> indices = tokenizer.tokensToIndices(tokens);
Blob::Ptr input = wrapVecToBlob(indices);
req.SetBlob("input.1", input);
req.Infer();
float* output1 = req.GetBlob(outputName)->buffer();
float* output2 = req.GetBlob("Squeeze_438")->buffer();
float max1 = output1[0], max2 = output2[0];
int indMax1 = 0, indMax2 = 0;
for (int i = 0; i < 128; i++) {
if (output1[i] > max1) {
max1 = output1[i];
indMax1 = i;
}

// TODO: forward indices through the network and return an answer

return "";
if (output2[i] > max2) {
max2 = output2[i];
indMax2 = i;
}
}

std::string result;
for (int i = indMax1; i < indMax2 + 1; i++) {
dkurt marked this conversation as resolved.
Show resolved Hide resolved
std::string word = tokens[i];
if (word[0] == '#') {
result.pop_back();
result += word.substr(2, word.length() - 2);
result += ' ';
}
else {
result += word + ' ';
}
}
result.pop_back();
return result;
}
21 changes: 20 additions & 1 deletion modules/7_nlp/src/tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,26 @@
#include <opencv2/opencv.hpp>

std::vector<std::string> basicTokenize(const std::string& text) {
CV_Error(cv::Error::StsNotImplemented, "basicTokenize");
std::vector<std::string> basicTokens;
std::string currToken = "";
for (auto ch : text) {
if (isspace(ch)) {
if (!currToken.empty())
basicTokens.push_back(currToken);
currToken = "";
} else if (ispunct(ch)) {
if (!currToken.empty())
basicTokens.push_back(currToken);
currToken = ""; currToken += ch;
basicTokens.push_back(currToken);
currToken = "";
} else {
currToken += tolower(ch);
}
}
if (!currToken.empty())
basicTokens.push_back(currToken);
return basicTokens;
}

std::vector<std::string> wordTokenize(const std::string& word,
Expand Down