From f36f544e18a03a9ab7077f243c5e7b306954aafb Mon Sep 17 00:00:00 2001 From: Jacob Date: Sun, 5 May 2019 17:34:26 +0200 Subject: [PATCH] Add iris classification --- iris_model.h5 | Bin 0 -> 23504 bytes magazine/__pycache__/views.cpython-37.pyc | Bin 1618 -> 2080 bytes magazine/static/magazine/forklift.js | 110 ++++++----- magazine/static/magazine/sketch.js | 219 ++++++++++++---------- magazine/templates/magazine/index.html | 20 +- magazine/views.py | 25 ++- train_model.py | 28 +++ 7 files changed, 229 insertions(+), 173 deletions(-) create mode 100644 iris_model.h5 create mode 100644 train_model.py diff --git a/iris_model.h5 b/iris_model.h5 new file mode 100644 index 0000000000000000000000000000000000000000..386edca7d44c42c3f976644e4137cef978443daf GIT binary patch literal 23504 zcmeGk3vd(H@nn96n2-4c6QBqKtpS52Tef7$mgMkL2m#6`xELy*?XxXMmK;eoc8G}~ zgi^}K5Xv~D1SfrjoT&&H(X4x@kkE@tNU0JLrJKt(^6cu}{?tHcgqcR-Dc6+Q+vliiV zJY{@eG}4LnB*bGj#SWVX3!y{b*=&!kg!Q28jyj@UoV}Q*aC6oy8;gnPIA|eopcTl5 zZbi!Cl?UO(N-VHDT~5zRhb`CXDikT>=C~Ym3lU8!R(UyA1`Qcxh}E3S=@zk;=*Tmp z61%v(Vms@i64T-Nf!f9J_ON-%tnFecPc+YNel9#djd+$41eQc>rJz%7RbvI)nQV!o zev_FXnM}7Wuh41Bk>|7ILC)ve@^X|1XnJJ=<&6O}AJHvGTfTwxPm&YPC%&DWU-l3t znY0{M98Go{eyZ6jm0CqEjaHMa)g-F18)R`F7Woh+&?WPiHc6eRQ)}YYfQ{#|2#4=h z$LqA3Bvk^wH;2n+%Ls1-aYb&MolgyFZk9@mde}mDo{PnioXgtX93N?RryFOA=xo-* zG?6PQ_pvaz<%dz?>Xpp+u!Xi6I zH@H^3<3oc`93B$3zO)xlOWPF=6Vuise~~R9TR^seYysHg6M>p(H2N2gspuSK;>w!Rbg~E)XH(M*PZoaz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?#57z?X z=S!&Wv7f6`6Y%Y zg8G#&uaJAiaOn9)N%KvnKqzUx$rs=|8wgatii#&A>uBBxmS0-=UA3HFI(xplD}Ql* zQBpzEssDi7on61Wr6wam{Yvb|Acx!vEg-I6nYBW^7=pZbh3-Sd_`V8D*B9ab72b>Y zwIQE}(DfujU~1`l@^JUBzH{&q{O^zWm$Pk-JS%x=H|Xz=`1gIOvypdzVdo-H8=b+l z|9gj0`u;&PO$fo(v8Xh{xBXFsW6DtPvp}GAOeuPg1;1L3wFiF&OTfCLgN}P_VV^91 zZzh7NQUp8a#YgC{PaaCggBHsfXY-(6BjTT`_#N;1M33WXo<=|Qz`u~;4}d`{A817%`iJ=WK@5j(pK5BK3c-|jcmII+fHrjfl|LeiD}(6&aPx;ixE=rm^Jm|@ z5oN@1=<{i&^O-5IQg`>~2bb@vss24J-wE?S>|3D?-5-uodqBn{ZXxxDha2Cp@3c^_ zZCxU8UkQ*-d%}M#K?yqYDb%z~&@BN#`%B9uC_%?o3bm{dbW4CL+GAcNL3L_}H19<5 zIiWv{4+s5Wd?M%% r<6}U77@z(5!}!2Y0FzTbm@n`vd{1|{y3XI;?-Bmib@r&~5%%~$R{GW+ literal 0 HcmV?d00001 diff --git a/magazine/__pycache__/views.cpython-37.pyc b/magazine/__pycache__/views.cpython-37.pyc index 57eb524be25a73d8045c1631848c0697c115fdf5..faa5968f2cb08712c7cfffada6f21d8058d1d636 100644 GIT binary patch delta 872 zcmZ`%Pfyf96z^+i+G*MCq7cF&0*S^@491Hd)WifYVh9%xCKD5~Vao18>DK9N2-*#Y z<)HDxrWcRPXW-(=4`5>abL=xLam;d`eJZpBlUtI>Hg`)EEMw0!|D zjbQ3hAF)095R5Kfz~j-M6|0pb`64VsEq?;@4Hs#K@(d;YYUDp*_9O#gHjROW+CoiG z6Vz0h`_?`gQ$f#RU}yH&R?e7ZW@cq1qixcLbKCz`~Ss-Ghp$f zN$$y288*POFB9S_&Z@XzSVn|9V9L$dP+!;E=3W{mt>{%(s1Bbh?X56wqvIu^nh>R3 z5pU~V*-dv2dm@bPbsk7K!BJd-22+ED5O@XIPkV7eqFzfBrrP85Ae61Nu*`-jP~)FY7k$(9$D>-mCxXYM-sdK@*qO|%FjwVA(3M8|yq delta 406 zcmX|5Jxc>Y5Z$@k+r96^L?l7fRN^Ywh!$d{5K$x`HgYII*aMN6!@Uy=(@0X-9$RS@ zYfFEE|G+Y}P5y$Fvk}~79&g{wynW;9R~YX{kx%f*_WMPbm2snbZeBz}kK956K+4_a z?S!F1dpzONk!(Z~H+3YJ?uZN_@sNAcpK)K(Ga`biu`FK&bjr_T*wHbx^cE~ZpiiN( z2iT-o2UvNGP>=$~)_}ah5O5tj!d5ow6MBJ?(%($u=VE@1kn?qRCG24izCX?`#XJlU=vbD`w0#TX$ zn#3kTD!VGZyHE2%g*Z2%{Y<8+W>)I@g%xZbY2Ma#e#BJ^xd?Q|PZs`M8)nWm5Ey9b SZ@yYH`9i>Jx!^i34V_>5_DfX& diff --git a/magazine/static/magazine/forklift.js b/magazine/static/magazine/forklift.js index dfe9c57..9c714a8 100644 --- a/magazine/static/magazine/forklift.js +++ b/magazine/static/magazine/forklift.js @@ -1,68 +1,66 @@ class Forklift { - constructor(x, y) { - this.positoin = createVector(x, y); - this.speed = 5; - this.path = []; - this.currentTarget = ''; - this.targetId = 0; - this.velocity = createVector(0, 0); - this.direction = createVector(0, 0); - this.end = false; - this.currentSection = 0; - } + constructor(x, y) { + this.positoin = createVector(x, y); + this.speed = 5; + this.path = []; + this.currentTarget = ''; + this.targetId = 0; + this.velocity = createVector(0, 0); + this.direction = createVector(0, 0); + this.end = false; + this.currentSection = 0; + } - draw() { - fill(225, 255, 0); - ellipse(this.positoin.x, this.positoin.y, 20); - } + draw() { + fill(225, 255, 0); + ellipse(this.positoin.x, this.positoin.y, 20); + } - setPath(path) { - this.end = false; - this.path = path; - this.currentTarget = this.path[this.targetId]; - this.setVelocity(); - } + setPath(path) { + this.end = false; + this.path = path; + this.currentTarget = this.path[this.targetId]; + this.setVelocity(); + } - nextTarget() { - this.targetId += 1; - if (this.targetId < this.path.length) { - this.currentTarget = this.path[this.targetId]; - } else { - this.end = true; - this.targetId = 0; - } + nextTarget() { + this.targetId += 1; + if (this.targetId < this.path.length) { + this.currentTarget = this.path[this.targetId]; + } else { + this.end = true; + this.targetId = 0; } + } - targetReached() { - this.currentSection = this.currentTarget; - return this.end; - } + targetReached() { + this.currentSection = this.currentTarget; + return this.end; + } - setVelocity() { - debugger; - this.direction = this.sub(sections[this.currentTarget], this.positoin); - this.velocity = this.direction.setMag(this.speed); - } + setVelocity() { + this.direction = this.sub(sections[this.currentTarget], this.positoin); + this.velocity = this.direction.setMag(this.speed); + } - move() { - this.positoin = this.add(this.positoin, this.velocity); - if ( - Math.abs(this.positoin.x - sections[this.currentTarget].x) <= - this.speed && - Math.abs(this.positoin.y - sections[this.currentTarget].y) <= - this.speed - ) { - this.positoin = sections[this.currentTarget]; - this.nextTarget(); - this.setVelocity(); - } + move() { + this.positoin = this.add(this.positoin, this.velocity); + if ( + Math.abs(this.positoin.x - sections[this.currentTarget].x) <= + this.speed && + Math.abs(this.positoin.y - sections[this.currentTarget].y) <= this.speed + ) { + this.positoin = sections[this.currentTarget]; + this.nextTarget(); + this.setVelocity(); } + } - sub(target, pos) { - return createVector(target.x - pos.x, target.y - pos.y); - } + sub(target, pos) { + return createVector(target.x - pos.x, target.y - pos.y); + } - add(target, pos) { - return createVector(target.x + pos.x, target.y + pos.y); - } + add(target, pos) { + return createVector(target.x + pos.x, target.y + pos.y); + } } diff --git a/magazine/static/magazine/sketch.js b/magazine/static/magazine/sketch.js index 45ab43c..6ffe613 100644 --- a/magazine/static/magazine/sketch.js +++ b/magazine/static/magazine/sketch.js @@ -1,130 +1,149 @@ +const serverUrl = 'http://localhost:8000'; let sections; let roads; let packageClaim; let going = false; let forklift; -let target; - // This runs once at start function setup() { - createCanvas(600, 600).parent('canvas'); + createCanvas(600, 600).parent('canvas'); - frameRate(30); + frameRate(30); - createMagazineLayout(); + createMagazineLayout(); - select('#button').mousePressed(deliver); - target = select('#target'); - // Create a forklift instance - forklift = new Forklift(sections[0].x, sections[0].y); + select('#button').mousePressed(getIrisType); + sepalWidth = select('#sepalWidth'); + sepalLength = select('#sepalLength'); + petalWidth = select('#petalWidth'); + petalLength = select('#petalLength'); + // Create a forklift instance + forklift = new Forklift(sections[0].x, sections[0].y); } // This runs every frame function draw() { - background(64); - drawMagazine(); - forklift.draw(); - if (going) { - if (forklift.targetReached()) { - going = false; - } else { - forklift.move(); - } + background(64); + drawMagazine(); + forklift.draw(); + if (going) { + if (forklift.targetReached()) { + going = false; + } else { + forklift.move(); } + } } function drawMagazine() { - noFill(); - stroke(220); - strokeWeight(10); - // Draw all the roads in the magazine - for (let road of roads) { - line( - sections[road[0]].x, - sections[road[0]].y, - sections[road[1]].x, - sections[road[1]].y, - ); - } - noStroke(); - textAlign(CENTER, CENTER); - // Draw all sections in the magazine - for (let section of Object.keys(sections)) { - if (section === 0) { - fill(80); - rect(sections[section].x - 15, sections[section].y, 30, 30); - } else { - fill(30); - ellipse(sections[section].x, sections[section].y, 30); - fill(255); - text(section, sections[section].x, sections[section].y); - } + noFill(); + stroke(220); + strokeWeight(10); + // Draw all the roads in the magazine + for (let road of roads) { + line( + sections[road[0]].x, + sections[road[0]].y, + sections[road[1]].x, + sections[road[1]].y, + ); + } + noStroke(); + textAlign(CENTER, CENTER); + // Draw all sections in the magazine + for (let section of Object.keys(sections)) { + if (section === 0) { + fill(80); + rect(sections[section].x - 15, sections[section].y, 30, 30); + } else { + fill(30); + ellipse(sections[section].x, sections[section].y, 30); + fill(255); + text(section, sections[section].x, sections[section].y); } + } } -function deliver() { - let data = { - graph: magazineToGraph(), - start_node: forklift.currentSection, - dest_node: int(target.value()), - }; - httpPost( - 'http://localhost:8000/shortestPath', - data, - response => { - path = response.split('').map(Number); - forklift.currentTarget = path[0]; - forklift.setPath(path); - going = true; - }, - error => { - console.log(error); - }, - ); +function getIrisType() { + let sw = select('#sepalWidth').value(); + let sl = select('#sepalLength').value(); + let pw = select('#petalWidth').value(); + let pl = select('#petalLength').value(); + let data = { + sepalWidth: sw, + sepalLength: sl, + petalWidth: pw, + petalLength: pl, + }; + httpPost(serverUrl + '/classify', data, response => { + deliver(response); + }); +} + +function deliver(targetSection) { + let data = { + graph: magazineToGraph(), + start_node: forklift.currentSection, + dest_node: int(targetSection), + }; + console.log(data); + httpPost( + serverUrl + '/shortestPath', + data, + response => { + path = response.split('').map(Number); + forklift.currentTarget = path[0]; + forklift.setPath(path); + going = true; + }, + error => { + console.log(error); + }, + ); } function createMagazineLayout() { - unit = width / 6; + unit = width / 6; - sections = { - 0: { x: 2 * unit, y: unit }, - 1: createVector(unit, 2 * unit), - 2: createVector(unit, 3 * unit), - 3: createVector(unit, 4 * unit), - 4: createVector(3 * unit, 2 * unit), - 5: createVector(3 * unit, 3 * unit), - 6: createVector(3 * unit, 4 * unit), - }; - roads = [ - [1, 5], - [2, 3], - [0, 1], - [0, 4], - [4, 5], - [5, 6], - [3, 6], - [4, 2], - [5, 3], - ]; + sections = { + 0: { x: 2 * unit, y: unit }, + 1: createVector(unit, 2 * unit), + 2: createVector(unit, 3 * unit), + 3: createVector(unit, 4 * unit), + 4: createVector(3 * unit, 2 * unit), + 5: createVector(3 * unit, 3 * unit), + 6: createVector(3 * unit, 4 * unit), + }; + roads = [ + [1, 5], + [2, 3], + [0, 1], + [0, 4], + [4, 5], + [5, 6], + [3, 6], + [4, 2], + [5, 3], + ]; } function magazineToGraph() { - graph = {}; - for (let key of Object.keys(sections)) { - graph[key] = {}; - } - for (let road of roads) { - start = road[0]; - end = road[1]; - graph[start][end] = Math.sqrt( - Math.pow(sections[start].x - sections[end].x, 2) + - Math.pow(sections[start].y - sections[end].y, 2), - ); - graph[end][start] = Math.sqrt( - Math.pow(sections[start].x - sections[end].x, 2) + - Math.pow(sections[start].y - sections[end].y, 2), - ); - } - return graph; + graph = {}; + for (let key of Object.keys(sections)) { + graph[key] = {}; + } + for (let road of roads) { + start = road[0]; + end = road[1]; + graph[start][end] = Math.sqrt( + Math.pow(sections[start].x - sections[end].x, 2) + + Math.pow(sections[start].y - sections[end].y, 2), + ); + graph[end][start] = Math.sqrt( + Math.pow(sections[start].x - sections[end].x, 2) + + Math.pow(sections[start].y - sections[end].y, 2), + ); + } + return graph; } diff --git a/magazine/templates/magazine/index.html b/magazine/templates/magazine/index.html index dc60ca3..a1941a2 100644 --- a/magazine/templates/magazine/index.html +++ b/magazine/templates/magazine/index.html @@ -42,27 +42,21 @@

Package description

- + - + - + - - - +

Sections

-

A - Cartons

-

B - Barrels

-

C - Plastic boxes

-

D - ______

-

E - ______

-

F - ______

-

G - ______

+

1 - Setosa

+

2 - Versicolor

+

3 - Viginica

diff --git a/magazine/views.py b/magazine/views.py index aaac5f9..6b43039 100644 --- a/magazine/views.py +++ b/magazine/views.py @@ -1,10 +1,11 @@ +import math +import json from django.shortcuts import render from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt -import json -import math - +import tensorflow as tf +import numpy as np # Create your views here. @@ -14,7 +15,21 @@ def index(request): @csrf_exempt def classify(request): - return HttpResponse(json.load(request)) + loaded_request = json.load(request) + sw = loaded_request['sepalWidth'] + sl = loaded_request['sepalLength'] + pw = loaded_request['petalWidth'] + pl = loaded_request['petalLength'] + + model = tf.keras.models.load_model('iris_model.h5') + output = model.predict(np.array([[sw, sl, pw, pl]])) + if output[0][0] > output[0][1] and output[0][0] > output[0][1]: + guess = 1 + elif output[0][1] > output[0][0] and output[0][1] > output[0][2]: + guess = 2 + else: + guess = 3 + return HttpResponse(guess) @csrf_exempt @@ -61,4 +76,6 @@ def shortestPath(request): current = predecessor[current] path[node] = p[::-1] + print(path) + return HttpResponse(path[dest_node][1:]) diff --git a/train_model.py b/train_model.py new file mode 100644 index 0000000..1e07ce7 --- /dev/null +++ b/train_model.py @@ -0,0 +1,28 @@ +import numpy as np +import tensorflow as tf + +from sklearn.datasets import load_iris +from sklearn.model_selection import train_test_split + +from tensorflow.keras import layers +from tensorflow.keras.utils import to_categorical + +# Getting data +data_set = load_iris() +x = data_set['data'] +y = to_categorical(data_set['target']) + +train_x, test_x, train_y, test_y = train_test_split(x, y, test_size=0.2) + +# Building the model +model = tf.keras.Sequential() + +model.add(layers.Dense(8, activation='relu', input_dim=4)) +model.add(layers.Dense(3, activation='sigmoid')) + +model.compile(optimizer='adam', loss='categorical_crossentropy', + metrics=['accuracy']) +# Training the model +model.fit(train_x, train_y, validation_data=(test_x, test_y), epochs=2000) + +model.save('iris_model.h5')