Pytorch LSTM in ONNX.js - Uncaught (in promise) Error: unrecognized input '' for node: LSTM_4 - javascript

I am trying to run a Pytorch LSTM network in browser. But I am getting this error:
graph.ts:313 Uncaught (in promise) Error: unrecognized input '' for node: LSTM_4
at t.buildGraph (graph.ts:313)
at new t (graph.ts:139)
at Object.from (graph.ts:77)
at t.load (model.ts:25)
at session.ts:85
at t.event (instrument.ts:294)
at e.initialize (session.ts:81)
at e.<anonymous> (session.ts:63)
at onnx.min.js:14
at Object.next (onnx.min.js:14)
How can I resolve this? Here is my code for saving the model to onnx:
net = torch.load('trained_model/trained_model.pt')
net.eval()
with torch.no_grad():
input = torch.tensor([[1,2,3,4,5,6,7,8,9]])
h0, c0 = net.init_hidden(1)
output, (hn, cn) = net.forward(input, (h0,c0))
torch.onnx.export(net, (input, (h0, c0)), 'trained_model/trained_model.onnx',
input_names=['input', 'h0', 'c0'],
output_names=['output', 'hn', 'cn'],
dynamic_axes={'input': {0: 'sequence'}})
I put input as the only dynamic axis since it is the only one that can vary in size. With this code, the model saves properly as trained_model.onnx. It does give me a warning:
UserWarning: Exporting a model to ONNX with a batch_size other than 1, with a variable length with LSTM can cause an error when running the ONNX model with a different batch size. Make sure to save the model with a batch size of 1, or define the initial states (h0/c0) as inputs of the model.
warnings.warn("Exporting a model to ONNX with a batch_size other than 1, "
This warning a little confusing since I am exporting it with a batch_size of 1:
input has shape torch.Size([1, 9])
h0 has shape torch.Size([2, 1, 256]) - corresponding to (num_lstm_layers, batch_size, hidden_dim)
c0 also has shape torch.Size([2, 1, 256])
But since I do define h0/c0 as inputs of the model I don't think this relates to the problem.
This is my javascript code for running in the browser:
<script src="https://cdn.jsdelivr.net/npm/onnxjs/dist/onnx.min.js"></script>
<!-- Code that consume ONNX.js -->
<script>
// create a session
const myOnnxSession = new onnx.InferenceSession();
console.log('trying to load the model')
// load the ONNX model file
myOnnxSession.loadModel("./trained_model.onnx").then(() => {
console.log('successfully loaded model!')
// after this I generate input and run the model
// since my code fails before this it isn't relevant
});
</script>
Based on the console.log statements, it is failing to load to the model. How should I resolve this? If relevant I'm using Python 3.8.5, Pytorch 1.6.0, ONNX 1.8.0.

For anyone coming across this in the future, I believe I'm getting this error because even though ONNX supports Pytorch LSTM networks, ONNX.js does not support it yet.
To get around this, instead of running in the browser I may use a simple web application framework called streamlit.

Related

Failed to execute 'setRemoteDescription' on 'RTCPeerConnection': cannot convert to dictionary

I am building a webrtc system and currently trying to have the session description sent back to the local connection. Below is the code block I am using to have the session description reinserted on the local side, but I am getting the error below. The "lc.setRemoteDescription()" line works when I manually type the description into lc.setRemoteDescription() in the devtool console. Any ideas what this could be due to?
Javascript code
...
console.log("broadcastlist");
broadcastlist.push(data.message);
console.log(data.message);
connectcounter ++;
console.log(connectcounter)
var user = data.user
var csrftoken = $("[name=csrfmiddlewaretoken]").val();
lc.setRemoteDescription(data.message)
...
Output on browser dev tools console. ("lc.setRemoteDescription(data.message)" corresponds to the error displayed in the picture below.
That error happens when you attempt to pass a string to setRemoteDescription:
const pc = new RTCPeerConnection();
pc.setRemoteDescription("")
Assuming that your data.message is a JSON-serialized object you need to convert it to an object with JSON.parse first.

Buffer Overflow - ZAP Scanning report- Node JS microservice in typescript file

Person schema:
import JoiBase from '#hapi/joi';
import JoiDate from '#hapi/joi-date';
const Joi = JoiBase.extend(JoiDate);
const personSchema = Joi.object().keys({
person: Joi.object().keys({
personId: Joi.string().max(255).allow(null, '').required()
}),
});
export default Joi.alternatives().try(
personSchema,
);
POST: https://localhost:8080/api/personData/12345/change
POST Payload:
{"person":{"personId":"satishkonda"}}
Node js web service validating the request payload using person schema.
calling the above rest post request on the purpose of DAST scan using OWSAP ZAP 2.9.0 getting below buffer overflow medium scanning report
Buffer overflow errors are characterized by the overwriting of memory spaces of the background web process, which should have never been modified intentionally or unintentionally. Overwriting values of the IP (Instruction Pointer), BP (Base Pointer), and other registers cause exceptions, segmentation faults, and other process errors to occur. Usually, these errors end the execution of the application in an unexpected way.
I am bit new to the DAST SCAN. So I am trying to find the root cause by searching google but no use. Please help over this
ZAP is Open Source so you can look at the source code of the scan rule yourself. It is available here: https://github.com/zaproxy/zap-extensions/blob/master/addOns/ascanrules/src/main/java/org/zaproxy/zap/extension/ascanrules/BufferOverflow.java
That'll let you see exactly why it's triggering for you.
Of course it is possible that the result is a False Positive.
From my quick review it sends a giant param value (2100 chars) and checks the response for Internal Server Error and Connection: close.
If that's how your app behaves:
Perhaps it is vulnerable.
Perhaps it needs some more robust error/input handling.
Perhaps the finding is a False Positive and you filter it out going forward:
https://www.zaproxy.org/docs/desktop/addons/alert-filters/
https://www.zaproxy.org/docs/docker/baseline-scan/#configuration-file

TaskCanceledException: a task was cancelled while rendering jsreport

I'm trying to render a jsreport that contains too much data and when it takes too long to render shows me this message:
TaskCanceledException: a task was cancelled.
If I load less data the report works well.
My question is, there is a way to avoid TaskCanceledException and let the rendering time take what it has to take to render the report?
Thanks Ankit Vijay for your response, the answer has to be with the time out as you mention, my solution was the next one
I was using the jsreport embbedserver and i set the time out like this
embeddedReportingServer.ReportingService.HttpClientTimeout = TimeSpan.FromMinutes(40);
With this line of code i stop recieving this error:
TaskCanceledException: a task was cancelled while rendering jsreport
Then when i was testing again i got this error with the phanton process:
Unable to render template. Error during rendering report: Timeout when executing in phantom.
At the end i solved it by adding configuration to my embbed server like this
Helper.embeddedReportingServer.Configuration = new
{
phantom = new
{
timeout = 900000,
numberOfWorkers = 2
},
tasks = new
{
numberOfWorkers = 2,
timeout = 900000,
}
};
The TaskCanceledException most likely should be coming due to timeout.
Try increase timeout through jsreport configuration. You can read more about jsreport configuration here. I have not tried it myself but it should be tasks.timeout property.

Clear Chrome browser logs in Selenium/Python

I have a large application and I am using Headless Chrome, Selenium and Python to test each module. I want to go through each module and get all the JS console errors produced while inside that specific module.
However, since each module is inside a different test case and each case executes in a separate session, the script first has to login on every test. The login process itself produces a number of errors that show up in the console. When testing each module I don't want the unrelated login errors to appear in the log.
Basically, clear anything that is in the logs right now -> go to the module and do something -> get logs that have been added to the console.
Is this not possible? I tried doing driver.execute_script("console.clear()") but the messages in the console were not removed and the login-related messages were still showing after doing something and printing the logs.
State in 2017 and late 2018
The logging API is not part of the official Webdriver specification yet.
In fact, it's requested to be defined for the level 2 specification. In mid 2017 only the Chromedriver has an undocumented non-standard implementation of that command.
In the sources there's no trace of a method for clearing logs:
The public API Webdriver.get_log()
which references internal Command names
which translate to acutal requests in RemoteConnection
Possible Workaround
The returned (raw) data structure is a dictionary that looks like this:
{
u'source': u'console-api',
u'message': u'http://localhost:7071/console.html 8:9 "error"',
u'timestamp': 1499611688822,
u'level': u'SEVERE'
}
It contains a timestamp that can be remembered so that subsequent calls to get_log() may filter for newer timestamps.
Facade
class WebdriverLogFacade(object):
last_timestamp = 0
def __init__(self, webdriver):
self._webdriver = webdriver
def get_log(self):
last_timestamp = self.last_timestamp
entries = self._webdriver.get_log("browser")
filtered = []
for entry in entries:
# check the logged timestamp against the
# stored timestamp
if entry["timestamp"] > self.last_timestamp:
filtered.append(entry)
# save the last timestamp only if newer
# in this set of logs
if entry["timestamp"] > last_timestamp:
last_timestamp = entry["timestamp"]
# store the very last timestamp
self.last_timestamp = last_timestamp
return filtered
Usage
log_facade = WebdriverLogFacade(driver)
logs = log_facade.get_log()
# more logs will be generated
logs = log_facade.get_log()
# newest log returned only
This thread is a few years old, but in case anyone else finds themselves here trying to solve a similar problem:
I also tried using driver.execute_script('console.clear()') to clear the console log between my login process and the page I wanted to check to no avail.
It turns out that calling driver.get_log('browser') returns the browser log and also clears it.
After navigating through pages for which you want to ignore the console logs, you can clear them with something like
_ = driver.get_log('browser')

Steam Trading Bot Issue - Empty String

I have an issue with a SteamBot I have recently set up to handle deposits and winnings for a CS:GO Gambling site. It works well for depositing and sending trade offers for the first couple of minutes, before throwing me this error:
C:\Users\Frederik\Desktop\bot\node_modules\mysql\lib\protocol\Parser.js:82
throw err;
^
Error: number format error: empty string
at Error (native)
at Function.Long.fromString (C:\Users\Frederik\Desktop\bot\node_modules\steam-tradeoffers\node_modules\long\dist\Long.js:180:19)
at toAccountId (C:\Users\Frederik\Desktop\bot\node_modules\steam-tradeoffers\index.js:376:15)
at SteamTradeOffers.makeOffer (C:\Users\Frederik\Desktop\bot\node_modules\steam-tradeoffers\index.js:396:42)
at Query._callback (C:\Users\Frederik\Desktop\bot\sell.js:160:13)
at Query.Sequence.end (C:\Users\Frederik\Desktop\bot\node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24)
at Query._handleFinalResultPacket (C:\Users\Frederik\Desktop\bot\node_modules\mysql\lib\protocol\sequences\Query.js:144:8)
at Query.EofPacket (C:\Users\Frederik\Desktop\bot\node_modules\mysql\lib\protocol\sequences\Query.js:128:8)
at Protocol._parsePacket (C:\Users\Frederik\Desktop\bot\node_modules\mysql\lib\protocol\Protocol.js:271:23)
at Parser.write (C:\Users\Frederik\Desktop\bot\node_modules\mysql\lib\protocol\Parser.js:77:12)
I have searched everywhere for the solution to this, however haven't managed to find anything. Any help or solutions would be very much appreciated! I am unsure as to what piece of code may be causing the issue, so here is a pastebin with the whole lot of it: http://pastebin.com/x9YkhkCX
Fixing it comes down to understanding what is going wrong. Once you know what is wrong, you automatically know the way it should be.
Rather than saying exactly what needs to be changed to fix it, I hope to show you how to find out for yourself.
If this is the first time you've come across a stacktrace, here's how to read this one.
It starts like this:
C:\Users\Frederik\Desktop\bot\node_modules\mysql\lib\protocol\Parser.js:82
throw err;
^
a 'Parser' inside a mysql library throws an error; that tells you some SQL query is failing.
Next it goes on to explain the err (^):
Error: number format error: empty string
The error is that a number is not formatted properly, because it is an empty string. It is like handing a function a blank piece of paper when it expects a number to be written onto it. But it isn't, so it doesn't know what to do, and raises an Error.
So now we know what is going wrong - at a low level. Let's see where it is going wrong, so that we know why.
We find this in the stacktrace: the function calls, all the way from the beginning of the program, to where the error was raised:
at Error (native)
This first line says that this error orinates in an Error function at some place called 'native': somewhere within the execution environment (the javascript engine), not in the program being executed.
This doesn't give us any new context - we know there is an error already.
at Function.Long.fromString (C:\Users\Frederik\Desktop\bot\node_modules\steam-tradeoffers\node_modules\long\dist\Long.js:180:19)
Here, we get something related to numbers: it seems to be a function converting a string to a Long, which is a type of large number. Not much news here either, as the error message told us empty string already. But at least we are on the right track.
at toAccountId (C:\Users\Frederik\Desktop\bot\node_modules\steam-tradeoffers\index.js:376:15)
Here we see that the number/string is probably an 'account id', used in the stream-tradeoffers module. For now let's assume that the empty string is passed to this function, and in the process of converting it to an account id it is turned into a number. Before we want to debug third party modules, lets first see if the problem is not in the main program:
at SteamTradeOffers.makeOffer (C:\Users\Frederik\Desktop\bot\node_modules\steam-tradeoffers\index.js:396:42)
Again the library, followed by:
at Query._callback (C:\Users\Frederik\Desktop\bot\sell.js:160:13)
In OP's pastebin we find at that line:
offers.makeOffer ({
partnerSteamId: row[i].userid,
itemsFromMe: item,
accessToken: row[i].token,
itemsFromThem: [],
message: 'Congratulations! You won a game on '+sitename+'. Your game ID is #'+gamenum
}, function(err,response){....
Here we find the call to makeOffer, and what parameters are used. It is likely that one of them is the empty string, or is an object with an empty-string property that is read by the makeOffer method; to find out, we'd have to check files mentioned in the previous two stacktrace lines.
To save time, we don't really need to look at the rest of the stack trace, since from here on it only references the mysql library, and it's unlikely the problem is there:
at Query.Sequence.end (C:\Users\Frederik\Desktop\bot\node_modules\mysql\lib\protocol\sequences\Sequence.js:96:24)
at Query._handleFinalResultPacket (C:\Users\Frederik\Desktop\bot\node_modules\mysql\lib\protocol\sequences\Query.js:144:8)
at Query.EofPacket (C:\Users\Frederik\Desktop\bot\node_modules\mysql\lib\protocol\sequences\Query.js:128:8)
at Protocol._parsePacket (C:\Users\Frederik\Desktop\bot\node_modules\mysql\lib\protocol\Protocol.js:271:23)
at Parser.write (C:\Users\Frederik\Desktop\bot\node_modules\mysql\lib\protocol\Parser.js:77:12)
From my experience the issue comes from the mysql database, table games & row userid and row token as below.
partnerSteamId: row[i].userid
accessToken: row[i].token
The PHP script that executes executes the mysql_query doesn't execute it correctly so it doesn't enter any userid & token in the database if another database row is too big.
That's my theory.

Categories