Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

93
LINES

< > BotCompany Repo | #329 // Simple perceptron [not really a perceptron]

Lua code - Snippet producer

get("#265") -- json.encode

images = {"#1000061", "A", "#1000056", "H", "#1000064", "P", "#1000063", "X", "#1000062", "B"}
features = {"#326", "#327", "#328"}

-- make a letter lookup map
image2letter = {}
for i=1, #images, 2 do image2letter[images[i]] = images[i+1] end

-- find out which results to load
toget = {}
for i=1, #images, 2 do table.insert(toget, images[i]) end
for i=1, #features do table.insert(toget, features[i]) end
results = getirresults(unpack(toget))

--map: feature -> letter -> value range

map = {}
for image, subresults in pairs(results) do
  letter = image2letter[image]
  for feature, result in pairs(subresults) do
    _, _, value = string.find(result, ": ([0-9.]+)")
    if value ~= nil then
      value = tonumber(value)
      if map[feature] == nil then map[feature] = {} end
      submap = map[feature]
      range = submap[letter]
      if range == nil then
        range = {value, value}
      else
        range = {math.min(range[1], value), math.max(range[2], value)}
      end
      submap[letter] = range
    end
  end
end

-- get serpent to serialize map into Lua code
serpent = go("#158")

--if all features are within the known value ranges for the letter, just take the intersection of all the matching letters within each feature.

-- serpent options:
-- block(map, {comment=false}) is that indented stuff
-- line(map, {comment=false}) is all in one line
-- dump(map) is...?
-- line(map, {comment=false, compact=true, sparse=true}) is in one line, very compact
-- line(map, {comment=false, compact=true, sparse=false}) is the same

recognizer = "  map = "..serpent.line(map, {comment=false, compact=true, sparse=false}).."\n"..[[
  allletters = {}
  outmap = {}
  
  for feature, submap in pairs(map) do
    f = otherresults[feature]
    if f == nil then error("Need recalc, waiting for "..feature) end
    _, _, value = string.find(f, ": ([0-9.]+)")
    if value ~= nil then
      value = tonumber(value)
      for letter, range in pairs(submap) do
        allletters[letter] = true
        if value < range[1] or value > range[2] then
          outmap[letter] = true -- can't be this letter
        end
      end
    end
  end
  
  candidates = {}
  for letter, _ in pairs(allletters) do
    if not outmap[letter] then
      table.insert(candidates, letter)
    end
  end
  
  if #candidates ~= 0 then
    return "Candidates: "..table.concat(candidates, ", ")
  end
]]

print(recognizer)

snippet = {
  type = 26, -- Lua code - Image recognition
  text = recognizer,
  title = "A machine-made letter recognizer, v3"
}

return json.encode(snippet)

--todo: otherwise, expand all value ranges uniformly and try again.

--more todo: repeat & increase ranges exponentially, finally at least one letter will match.

Author comment

I'm redefining the term PERCEPTRON... ^^

It means a production facility for a general "decider" based on a list of examples and feature extractors.

test run  test run with input  produce a snippet  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Image recognition results

Recognizer Recognition Result Visualize Recalc
#308 javax.imageio.IIOException: Can't get input stream from URL! [visualize]

Snippet ID: #329
Snippet name: Simple perceptron [not really a perceptron]
Eternal ID of this version: #329/2
Text MD5: bd993491c90d73e238bdb59189c62608
Author: stefan
Category: meta letter recognizers
Type: Lua code - Snippet producer
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-09-05 17:12:14
Source code size: 2846 bytes / 93 lines
Pitched / IR pitched: No / No
Views / Downloads: 1008 / 163
Version history: 1 change(s)
Referenced in: [show references]