Created
December 14, 2017 04:26
-
-
Save kieranbrowne/4f9fec38396e56cef88227c91283f242 to your computer and use it in GitHub Desktop.
Script for producing "neural network fooling images" with CPPNs. This is a simplified version of the algorithm described in Nguyen, Anh, Jason Yosinski, and Jeff Clune. "Deep neural networks are easily fooled: High confidence predictions for unrecognizable images." In Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition,…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MIT License | |
Copyright (c) 2017 Kieran Browne | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
This script is designed to interact with Felix Andrews' CPPNX | |
https://floybix.github.io/cppnx/ | |
It was built with browserify and run with Arturo Guzman's 'Custom | |
JavaScript for websites' https://github.com/guzart/customjs | |
*/ | |
const Clarifai = require('clarifai'); | |
const $ = require('jquery'); | |
const app = new Clarifai.App({ | |
apiKey: 'YOUR_API_KEY' | |
}); | |
scores = [ // these also control which classes will be optimised for | |
{name: 'train', value: 0}, | |
{name: 'cat', value: 0}, | |
{name: 'fish', value: 0}, | |
{name: 'house', value: 0}, | |
{name: 'starfish', value: 0}, | |
{name: 'baseball', value: 0}, | |
{name: 'remote control', value: 0}, | |
{name: 'guitar', value: 0}, | |
{name: 'tentacle', value: 0}] | |
function compare(a,b) { | |
if (a.value < b.value) | |
return 1; | |
if (a.value > b.value) | |
return -1; | |
return 0; | |
} | |
writeScores = function() { | |
if(scores.length > 9) scores = scores.slice(0,-1); | |
let scrs = scores.sort(compare).slice(0,8); | |
$('#scores').remove() | |
$('.cppnx-main-canvas').parent().prepend( | |
"<table id='scores'><tr>"+(scrs.map(s=>'<td><img style="margin-left: 2px; margin-bottom: 2px; width: 100px; height: 100px;" src="'+s.src+'"></td>').join(''))+"</tr><tr>"+(scrs.map(s=>'<td><p>'+s.value+'</p></td>').join(''))+"</tr><tr>"+(scrs.map(s=>'<td><p>'+s.name+'</p></td>').join(''))+"</tr></table>") | |
} | |
function options() { | |
return $('img').not('#scores img'); | |
} | |
let index = 0; | |
function predict() { | |
let imgs = options(); | |
if(index>imgs.length-2) index = 0; | |
else index++; | |
console.log("trying ", index); | |
return app.models.predict( | |
Clarifai.GENERAL_MODEL, | |
{"base64": $(imgs.get(index)).attr('src') | |
.replace(/^data:image\/(.*);base64,/, '')}, | |
{selectConcepts: scores.map(x=>{return {name: x.name}})}) | |
} | |
const updateScores = (res) => { | |
return new Promise(function (resolve,reject) { | |
let imageScores = res.rawData.outputs[0].data.concepts; | |
let imgs = options(); | |
// regenerate options if no improvements exist | |
let breed = false; | |
for(let i=0; i<scores.length; i++) { | |
let imageScore = $.grep(imageScores, x=>x.name == scores[i].name)[0] | |
if(imageScore.value > scores[i].value) { | |
console.log('new high score for '+scores[i].name); | |
// add image src and new high score to scores array | |
scores[i].value = imageScore.value | |
scores[i].src = $(imgs.get(index)).attr('src') | |
// update the scores table | |
writeScores(); | |
// breed this image | |
breed = true; | |
// break; // don't continue checking scores | |
} | |
} | |
if(breed) $(imgs.get(index)).click(); | |
else if(index == 0) $('.btn.btn-default.btn-lg').click(); | |
})} | |
main = () => { | |
predict().then(updateScores) | |
setTimeout(() => { main() }, 6000) | |
} | |
setTimeout(() => { main() }, 1000) // begin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment