Accessing labels and confidence values with Tensorflow.js custom model? - tensorflow.js

(EDIT: partly solved, please see the end of the post) I have successfully uploaded a custom Tensorflow.js classifier that makes predictions of my webcam feed. It's trained on two classes, "good" and "bad". I get results as an array (correct me if I'm using the terms wrong as I'm a newbie!)
As the next step, I want to print the labels and confidence values on my browser. However, I find it a bit hard to grasp how I access my "good" and "bad" labels and confidence values from this result format that I get:
const result = await model.predict(t4d);
console.log(result);
(console)
t {kept: false, isDisposedInternal: false, shape: Array(2), dtype: "float32", size: 2, …}
dataId:
__proto__: Object
dtype: "float32"
id: 685817
isDisposedInternal: false
kept: false
rankType: "2"
scopeId: 883969
shape: (2) [1, 2]
size: 2
strides: [2]
isDisposed: (...)
rank: (...)
__proto__: Object
If I understand right, it gives me shape 2 = class 2 as a result (or is it just the shape and not my class yet?). Can anybody give advice on how to manually assign labels for these shapes 1 and 2 so that they can be printed? And how would I access confidence values - with argMax?
Ideally I would want it print something like this:
Good: 0.703
Bad: 0.298
Reference is this out-of-the-box mobilenet example from Tensorflow.js tutorials:
const webcam = await tf.data.webcam(webcamElement);
while (true) {
const img = await webcam.capture();
const result = await net.classify(img);
document.getElementById('console').innerText = `
prediction: ${result[0].className}\n
probability: ${result[0].probability}
`;
Where it prints the most probable class label and confidence value (like cat, 0.3444).
Warm thanks for any help!
EDIT: By following this tutorial, I was able to extract the label by making an array of my labels:
const labels = ["good", "bad"];

Related

Is there a way of reading from sub arrays?

I am currently building an iOS application that stores user added products using Google Firestore. Each product that is added is concatenated into a single, user specific "products" array (as shown below - despite having separate numbers they are part of the same array but separated in the UI by Google to show each individual sub-array more clearly)
I use the following syntax to return the data from the first sub-array of the "products" field in the database
let group_array = document["product"] as? [String] ?? [""]
if (group_array.count) == 1 {
let productName1 = group_array.first ?? "No data to display :("`
self.tableViewData =
[cellData(opened: false, title: "Item 1", sectionData: [productName1])]
}
It is returned in the following format:
Product Name: 1, Listing Price: 3, A brief description: 4, Product URL: 2, Listing active until: 21/04/2021 10:22:17
However I am trying to query each of the individual sections of this sub array, so for example, I can return "Product Name: 1" instead of the whole sub-array. As let productName1 = group_array.first is used to return the first sub-array, I have tried let productName1 = group_array.first[0] to try and return the first value in this sub-array however I receive the following error:
Cannot infer contextual base in reference to member 'first'
So my question is, referring to the image from my database (at the top of my question), if I wanted to just return "Product Name: 1" from the example sub-array, is this possible and if so, how would I extract it?
I would reconsider storing the products as long strings that need to be parsed out because I suspect there are more efficient, and less error-prone, patterns. However, this pattern is how JSON works so if this is how you want to organize product data, let's go with it and solve your problem.
let productRaw = "Product Name: 1, Listing Price: 3, A brief description: 4, Product URL: 2, Listing active until: 21/04/2021 10:22:17"
First thing you can do is parse the string into an array of components:
let componentsRaw = productRaw.components(separatedBy: ", ")
The result:
["Product Name: 1", "Listing Price: 3", "A brief description: 4", "Product URL: 2", "Listing active until: 21/04/2021 10:22:17"]
Then you can search this array using substrings but for efficiency, let's translate it into a dictionary for easier access:
var product = [String: String]()
for component in componentsRaw {
let keyVal = component.components(separatedBy: ": ")
product[keyVal[0]] = keyVal[1]
}
The result:
["Listing active until": "21/04/2021 10:22:17", "A brief description": "4", "Product Name": "1", "Product URL": "2", "Listing Price": "3"]
And then simply find the product by its key:
if let productName = product["Product Name"] {
print(productName)
} else {
print("not found")
}
There are lots of caveats here. The product string must always be uniform in that commas and colons must always adhere to this strict formatting. If product names have colons and commas, this will not work. You can modify this to handle those cases but it could turn into a bowl of spaghetti pretty quickly, which is also why I suggest going with a different data pattern altogether. You can also explore other methods of translating the array into a dictionary such as with reduce or grouping but there are big-O performance warnings. But this would be a good starting point if this is the road you want to go down.
All that said, if you truly want to use this data pattern, consider adding a delimiter to the product string. For example, a custom delimiter would greatly reduce the need for handling edge cases:
let productRaw = "Product Name: 1**Listing Price: 3**A brief description: 4**Product URL: 2**Listing active until: 21/04/2021 10:22:17"
With a delimiter like **, the values can contain commas without worry. But for complete safety (and efficiency), I would add a second delimiter so that values can contain commas or colons:
let productRaw = "name$$1**price$$3**description$$4**url$$2**expy$$21/04/2021 10:22:17"
With this string, you can much more safely parse the components by ** and the value from the key by $$. And it would look something like this:
let productRaw = "name$$1**price$$3**description$$4**url$$2**expy$$21/04/2021 10:22:17"
let componentsRaw = productRaw.components(separatedBy: "**")
var product = [String: String]()
for component in componentsRaw {
let keyVal = component.components(separatedBy: "$$")
product[keyVal[0]] = keyVal[1]
}
if let productName = product["name"] {
print(productName)
} else {
print("not found")
}

Cant figure out how to go through array in Swift and find specific data

Hello I have question about arrays.
I have an array with following data, also I have corresponding Struct for SpiritRelation():
var spiritRelations = [
SpiritRelation(relationName: "Thunder Lantern", relationSpirit1: "Razor", relationSpirit2: "Lamp Genie", relationSpirit3: "", relationSpirit4: "", relationStats: "Double resist +5%, ATK +1600", relationSpiritIcons: ["razor", "genie"]),
SpiritRelation(relationName: "Illusive Fantasy", relationSpirit1: "Heavenly Maiden", relationSpirit2: "Lamp Genie", relationSpirit3: "", relationSpirit4: "", relationStats: "Excellent strike +15%, Dmg Penetration +15%, Max HP +11500", relationSpiritIcons: ["maiden", "genie"]),
SpiritRelation(relationName: "Grand Demonlord Gathering", relationSpirit1: "Sand Golem", relationSpirit2: "Lamp Genie", relationSpirit3: "", relationSpirit4: "", relationStats: "Excellency Resist +20%, Double Dmg +5%, ATK +1600", relationSpiritIcons: ["golem", "genie"])
}
array which contains data which will be selected by user:
var selectedSpiritsForRelation = [String]()
array of type String because I put there values which corresponds to image names in Assets. I need that to display images
array where I want to keep found relations and use it to display all found relationStats in UI
var foundRelations = [SpiritRelation]()
My problems is:
lets say user has selected 2 spirits for example: selectedSpiritsForRelation["golem", "genie"]
I’m able to find and save correctly found relation by
let result3 = spiritRelations.filter{$0.relationSpiritIcons == (selectedSpiritsForRelation) } // = 3rd relation in spiritRelations[]
foundRelations.append(contentsOf: result3)
but after user select another one spirit and array become: selectedSpiritsForRelation["golem", "genie", "maiden"]
same code as for result3 does not work anymore, because how I understand it tries to filter exactly combination of 3, but my expectation is that 2nd relation from spiritRelation[] will be found also
and here is my problem, I cant figure out how to correctly go through spiritRelations[] and find all relations related to selectedSpiritsForRelation[] every time user selects new spirit
You need to use allSatisfy in your filter by checking that all relationSpiritIcons elements exists in selectedSpiritsForRelation
foundRelations = spiritRelations.filter {
$0.relationSpiritIcons.allSatisfy { icon in
selectedSpiritsForRelation.contains(icon)
}
}

Creating a data structure that will hold objects in React

I'm building a program in React that is basically a questionnaire / survey, so far I've got users answering a bunch of questions then what I am trying to do is take the answers, filter them a bit then return an object to store using Redux. Each question is an object it self that holds different factors than I am trying to measure. Although, I'm getting stuck with actually trying to create the structure of the final object.
I want the object to look something like this:
Results {
Question1 {
factor1: 1,
factor2: 2,
factor3: 3
},
Question2 {
factor1: 3,
factor2: 6
},
and so on...
}
But when I try create create it by saying for example
let results = {
Question1 {
factor1: 0
}
}
React doesn't like it. As soon as I try nest another object inside it returns an error.. Am I missing something simple or do I need to try another approach, any help would be appreciated
It should be like this, property and value needs to be separated by a :
let results = {
Question1 :{
factor1: 0
}
}
let Results= {
Question1: {
factor1: 1,
factor2: 2,
factor3: 3
},
Question2: {
factor1: 3,
factor2: 6
},
and so on...
}

Get first object from array of objects in react [duplicate]

This question already has answers here:
How to get the first element of an array?
(35 answers)
Closed 4 years ago.
I am trying to get the first object from an array of objects. Fetched data in componentDidMount and return:
var array1 = [
{
Id: 1,
Categories: 200
},
{
Id: 2,
Categories: 100
}
]
(real data is much more complicated but in the same format)
What I want is this:
array2 = [
Id: 1,
Categories: 200
]
I was using
var iterator1 = array1.entries();
let array2 = iterator1.next().value
and when I console.log(array2) it shows the array data (all key and value is in index 1 of array), then when I console.log(array2[1].Id)
it shows undefined right inside the render method in class component. I use the same code in other IDE but not using react the code work. Could someone explain to me please? Appreciate for any help.
Edit:
At the end I found that I asked the wrong question. I failed to get the data because I declared the data in my reducer to object instead of array. I mark the answer by larz since you just need to put index 0 to get the first object from the array of objects.
Grab the first item in your array using [0], which returns an object in your case. Then you can call .Id on it to get the id.
var array1 = [
{
Id: 1,
Categories: 200
},
{
Id: 2,
Categories: 100
}
];
var first = array1[0];
console.log(first, first.Id);

Get delta at cursor (no selection) without splitting it

Here's a delta with an attribute:
Trying to get the delta with
editor.getContents(range.index, range.length);
returns
Delta: {
ops: []
}
which is expected - range.length is 0.
Is there a way of returning the entire delta (from left to right) so it looks like this:
Delta: {
ops: [
{
attributes: { test: '123' },
insert: 'A selection'
},
...
]
}
Assuming a slightly more complex example to disambiguate and assuming the test 123 attribute is implemented with a class Attributor, given the document:
<div class="ql-editor">
<p><strong>ab</strong><span class="ql-test=123">cd<em>ef</em></span></p>
</div>
I think what you are asking however is getting the Delta for the "cdef" text when the user's cursor is between the "e" and "f" and so your range is index: 5.
This is an experimental/undocumented API but quill.scroll.path(5) will get you an array [[blockBlot, 0], [inlineBlot, 2], [italicBlot, 1]] and the blot you want in this case is the second one so by summing the offsets to it you will have 2 (0 + 2) and then you can call quill.getContents(2, blot.length()).
If the class is unique (or you can access the DOM node some other way) you can also do:
const Parchment = Quill.import("parchment");
let node = document.querySelector('.ql-test-123');
let blot = Parchment.find(node);
let offset = quill.scroll.offset(blot);
quill.getContents(offset, blot.length());

Resources