diff --git a/public/bai_model/group1-shard10of27.bin b/public/bai_model/group1-shard10of27.bin new file mode 100644 index 0000000..befbaf0 Binary files /dev/null and b/public/bai_model/group1-shard10of27.bin differ diff --git a/public/bai_model/group1-shard11of27.bin b/public/bai_model/group1-shard11of27.bin new file mode 100644 index 0000000..3dded6b Binary files /dev/null and b/public/bai_model/group1-shard11of27.bin differ diff --git a/public/bai_model/group1-shard12of27.bin b/public/bai_model/group1-shard12of27.bin new file mode 100644 index 0000000..97f603d Binary files /dev/null and b/public/bai_model/group1-shard12of27.bin differ diff --git a/public/bai_model/group1-shard13of27.bin b/public/bai_model/group1-shard13of27.bin new file mode 100644 index 0000000..9caa247 Binary files /dev/null and b/public/bai_model/group1-shard13of27.bin differ diff --git a/public/bai_model/group1-shard14of27.bin b/public/bai_model/group1-shard14of27.bin new file mode 100644 index 0000000..42bd208 Binary files /dev/null and b/public/bai_model/group1-shard14of27.bin differ diff --git a/public/bai_model/group1-shard15of27.bin b/public/bai_model/group1-shard15of27.bin new file mode 100644 index 0000000..3326740 Binary files /dev/null and b/public/bai_model/group1-shard15of27.bin differ diff --git a/public/bai_model/group1-shard16of27.bin b/public/bai_model/group1-shard16of27.bin new file mode 100644 index 0000000..a0b12f4 Binary files /dev/null and b/public/bai_model/group1-shard16of27.bin differ diff --git a/public/bai_model/group1-shard17of27.bin b/public/bai_model/group1-shard17of27.bin new file mode 100644 index 0000000..d88793a Binary files /dev/null and b/public/bai_model/group1-shard17of27.bin differ diff --git a/public/bai_model/group1-shard18of27.bin b/public/bai_model/group1-shard18of27.bin new file mode 100644 index 0000000..4752000 Binary files /dev/null and b/public/bai_model/group1-shard18of27.bin differ diff --git a/public/bai_model/group1-shard19of27.bin b/public/bai_model/group1-shard19of27.bin new file mode 100644 index 0000000..e917506 Binary files /dev/null and b/public/bai_model/group1-shard19of27.bin differ diff --git a/public/bai_model/group1-shard1of27.bin b/public/bai_model/group1-shard1of27.bin new file mode 100644 index 0000000..ebf3314 Binary files /dev/null and b/public/bai_model/group1-shard1of27.bin differ diff --git a/public/bai_model/group1-shard20of27.bin b/public/bai_model/group1-shard20of27.bin new file mode 100644 index 0000000..319bf07 Binary files /dev/null and b/public/bai_model/group1-shard20of27.bin differ diff --git a/public/bai_model/group1-shard21of27.bin b/public/bai_model/group1-shard21of27.bin new file mode 100644 index 0000000..d39ee4e Binary files /dev/null and b/public/bai_model/group1-shard21of27.bin differ diff --git a/public/bai_model/group1-shard22of27.bin b/public/bai_model/group1-shard22of27.bin new file mode 100644 index 0000000..6912a6d Binary files /dev/null and b/public/bai_model/group1-shard22of27.bin differ diff --git a/public/bai_model/group1-shard23of27.bin b/public/bai_model/group1-shard23of27.bin new file mode 100644 index 0000000..f3df9b9 Binary files /dev/null and b/public/bai_model/group1-shard23of27.bin differ diff --git a/public/bai_model/group1-shard24of27.bin b/public/bai_model/group1-shard24of27.bin new file mode 100644 index 0000000..0f79d69 Binary files /dev/null and b/public/bai_model/group1-shard24of27.bin differ diff --git a/public/bai_model/group1-shard25of27.bin b/public/bai_model/group1-shard25of27.bin new file mode 100644 index 0000000..1ad119e Binary files /dev/null and b/public/bai_model/group1-shard25of27.bin differ diff --git a/public/bai_model/group1-shard26of27.bin b/public/bai_model/group1-shard26of27.bin new file mode 100644 index 0000000..72839bb Binary files /dev/null and b/public/bai_model/group1-shard26of27.bin differ diff --git a/public/bai_model/group1-shard27of27.bin b/public/bai_model/group1-shard27of27.bin new file mode 100644 index 0000000..3a32bf0 Binary files /dev/null and b/public/bai_model/group1-shard27of27.bin differ diff --git a/public/bai_model/group1-shard2of27.bin b/public/bai_model/group1-shard2of27.bin new file mode 100644 index 0000000..c8d6096 Binary files /dev/null and b/public/bai_model/group1-shard2of27.bin differ diff --git a/public/bai_model/group1-shard3of27.bin b/public/bai_model/group1-shard3of27.bin new file mode 100644 index 0000000..3b6538b Binary files /dev/null and b/public/bai_model/group1-shard3of27.bin differ diff --git a/public/bai_model/group1-shard4of27.bin b/public/bai_model/group1-shard4of27.bin new file mode 100644 index 0000000..77a286c Binary files /dev/null and b/public/bai_model/group1-shard4of27.bin differ diff --git a/public/bai_model/group1-shard5of27.bin b/public/bai_model/group1-shard5of27.bin new file mode 100644 index 0000000..d972d4b Binary files /dev/null and b/public/bai_model/group1-shard5of27.bin differ diff --git a/public/bai_model/group1-shard6of27.bin b/public/bai_model/group1-shard6of27.bin new file mode 100644 index 0000000..b87768c Binary files /dev/null and b/public/bai_model/group1-shard6of27.bin differ diff --git a/public/bai_model/group1-shard7of27.bin b/public/bai_model/group1-shard7of27.bin new file mode 100644 index 0000000..b748adb Binary files /dev/null and b/public/bai_model/group1-shard7of27.bin differ diff --git a/public/bai_model/group1-shard8of27.bin b/public/bai_model/group1-shard8of27.bin new file mode 100644 index 0000000..1d0bedc Binary files /dev/null and b/public/bai_model/group1-shard8of27.bin differ diff --git a/public/bai_model/group1-shard9of27.bin b/public/bai_model/group1-shard9of27.bin new file mode 100644 index 0000000..85bbf4a Binary files /dev/null and b/public/bai_model/group1-shard9of27.bin differ diff --git a/public/bai_model/model.json b/public/bai_model/model.json new file mode 100644 index 0000000..bb3d899 --- /dev/null +++ b/public/bai_model/model.json @@ -0,0 +1 @@ +{"format": "layers-model", "generatedBy": "keras v2.7.0", "convertedBy": "TensorFlow.js Converter v3.12.0", "modelTopology": {"keras_version": "2.7.0", "backend": "tensorflow", "model_config": {"class_name": "Sequential", "config": {"name": "sequential", "layers": [{"class_name": "InputLayer", "config": {"batch_input_shape": [null, 550, 425, 3], "dtype": "float32", "sparse": false, "ragged": false, "name": "conv2d_input"}}, {"class_name": "Conv2D", "config": {"name": "conv2d", "trainable": true, "batch_input_shape": [null, 550, 425, 3], "dtype": "float32", "filters": 32, "kernel_size": [3, 3], "strides": [1, 1], "padding": "valid", "data_format": "channels_last", "dilation_rate": [1, 1], "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "MaxPooling2D", "config": {"name": "max_pooling2d", "trainable": true, "dtype": "float32", "pool_size": [2, 2], "padding": "valid", "strides": [2, 2], "data_format": "channels_last"}}, {"class_name": "Conv2D", "config": {"name": "conv2d_1", "trainable": true, "batch_input_shape": [null, 550, 425, 3], "dtype": "float32", "filters": 64, "kernel_size": [3, 3], "strides": [1, 1], "padding": "valid", "data_format": "channels_last", "dilation_rate": [1, 1], "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "MaxPooling2D", "config": {"name": "max_pooling2d_1", "trainable": true, "dtype": "float32", "pool_size": [2, 2], "padding": "valid", "strides": [2, 2], "data_format": "channels_last"}}, {"class_name": "Conv2D", "config": {"name": "conv2d_2", "trainable": true, "batch_input_shape": [null, 550, 425, 3], "dtype": "float32", "filters": 64, "kernel_size": [3, 3], "strides": [1, 1], "padding": "valid", "data_format": "channels_last", "dilation_rate": [1, 1], "groups": 1, "activation": "relu", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "MaxPooling2D", "config": {"name": "max_pooling2d_2", "trainable": true, "dtype": "float32", "pool_size": [2, 2], "padding": "valid", "strides": [2, 2], "data_format": "channels_last"}}, {"class_name": "Flatten", "config": {"name": "flatten", "trainable": true, "dtype": "float32", "data_format": "channels_last"}}, {"class_name": "Dense", "config": {"name": "dense", "trainable": true, "dtype": "float32", "units": 128, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Activation", "config": {"name": "activation", "trainable": true, "dtype": "float32", "activation": "relu"}}, {"class_name": "Dropout", "config": {"name": "dropout", "trainable": true, "dtype": "float32", "rate": 0.5, "noise_shape": null, "seed": null}}, {"class_name": "Dense", "config": {"name": "dense_1", "trainable": true, "dtype": "float32", "units": 1, "activation": "linear", "use_bias": true, "kernel_initializer": {"class_name": "GlorotUniform", "config": {"seed": null}}, "bias_initializer": {"class_name": "Zeros", "config": {}}, "kernel_regularizer": null, "bias_regularizer": null, "activity_regularizer": null, "kernel_constraint": null, "bias_constraint": null}}, {"class_name": "Activation", "config": {"name": "activation_1", "trainable": true, "dtype": "float32", "activation": "sigmoid"}}]}}, "training_config": {"loss": "binary_crossentropy", "metrics": [[{"class_name": "MeanMetricWrapper", "config": {"name": "accuracy", "dtype": "float32", "fn": "binary_accuracy"}}]], "weighted_metrics": null, "loss_weights": null, "optimizer_config": {"class_name": "Adam", "config": {"name": "Adam", "learning_rate": 0.0010000000474974513, "decay": 0.0, "beta_1": 0.8999999761581421, "beta_2": 0.9990000128746033, "epsilon": 1e-07, "amsgrad": false}}}}, "weightsManifest": [{"paths": ["group1-shard1of27.bin", "group1-shard2of27.bin", "group1-shard3of27.bin", "group1-shard4of27.bin", "group1-shard5of27.bin", "group1-shard6of27.bin", "group1-shard7of27.bin", "group1-shard8of27.bin", "group1-shard9of27.bin", "group1-shard10of27.bin", "group1-shard11of27.bin", "group1-shard12of27.bin", "group1-shard13of27.bin", "group1-shard14of27.bin", "group1-shard15of27.bin", "group1-shard16of27.bin", "group1-shard17of27.bin", "group1-shard18of27.bin", "group1-shard19of27.bin", "group1-shard20of27.bin", "group1-shard21of27.bin", "group1-shard22of27.bin", "group1-shard23of27.bin", "group1-shard24of27.bin", "group1-shard25of27.bin", "group1-shard26of27.bin", "group1-shard27of27.bin"], "weights": [{"name": "conv2d/kernel", "shape": [3, 3, 3, 32], "dtype": "float32"}, {"name": "conv2d/bias", "shape": [32], "dtype": "float32"}, {"name": "conv2d_1/kernel", "shape": [3, 3, 32, 64], "dtype": "float32"}, {"name": "conv2d_1/bias", "shape": [64], "dtype": "float32"}, {"name": "conv2d_2/kernel", "shape": [3, 3, 64, 64], "dtype": "float32"}, {"name": "conv2d_2/bias", "shape": [64], "dtype": "float32"}, {"name": "dense/kernel", "shape": [218688, 128], "dtype": "float32"}, {"name": "dense/bias", "shape": [128], "dtype": "float32"}, {"name": "dense_1/kernel", "shape": [128, 1], "dtype": "float32"}, {"name": "dense_1/bias", "shape": [1], "dtype": "float32"}]}]} \ No newline at end of file diff --git a/src/actions/index.js b/src/actions/index.js index afa02d8..b9199ef 100644 --- a/src/actions/index.js +++ b/src/actions/index.js @@ -1,5 +1,6 @@ import github from '../apis/github'; import api from '../apis/api'; +import * as tf from '@tensorflow/tfjs'; export const getUser = (username) => async (dispatch, getState) => { @@ -87,4 +88,37 @@ export const showNavigation = () => async (dispatch, getState) => { type: 'SET_NAVIGATION', payload: true, }); -} \ No newline at end of file +} + +export const downloadModel = () => async (dispatch, getState) => { + const model = await tf.loadLayersModel('/bai_model/model.json'); // Load Model + + dispatch({ + type: 'SET_MODEL', + payload: model + }); +} + +export const loadingModel = () => async (dispatch, getState) => { + + dispatch({ + type: 'SET_LOADING', + payload: "Loading Model. Please be Patient" + }); +}; + +export const predict = (image) => async (dispatch, getState) => { + let model = await getState().model.model; + + let imdata = await tf.browser.fromPixels(image).resizeNearestNeighbor([550, 425]).toFloat().expandDims(); + + let prediction = await model.predict(imdata).data(); + console.log(prediction); + + dispatch({ + type: 'SET_LAST_PRED', + payload: prediction + }); + + return prediction; +}; diff --git a/src/components/App.js b/src/components/App.js index ac7986f..28612c9 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -6,6 +6,7 @@ import Github from './Github'; import Articles from './Articles'; import About from './About'; import Intro from './Intro'; +import Bai from './Bai'; const App = (props) => { return ( @@ -18,6 +19,7 @@ const App = (props) => { } /> + diff --git a/src/components/Bai.js b/src/components/Bai.js new file mode 100644 index 0000000..de52368 --- /dev/null +++ b/src/components/Bai.js @@ -0,0 +1,87 @@ +import React from 'react'; +import { connect } from 'react-redux'; +import {downloadModel, loadingModel, predict} from '../actions' +//import { Link } from 'react-router-dom'; +import './css/Bai.css'; + + +class Bai extends React.Component { + /** + * + */ + + classes = {0: "Blank", 1: "Not Blank"} + + componentDidMount () { + document.title = "Blank AI"; + } + + async fileUpload ({target}) { + console.log(target); + const [file] = target.files; + let image = document.getElementById("preview") + + if (file) { + image.src = await URL.createObjectURL(file); + } + } + + async imagePredict (model) { + let image = document.getElementById("preview"); + console.log(image.src); + if (image.src !== "") { + let prediction = await this.props.predict(image); + console.log(prediction[0]); + console.log(this.declassify(prediction[0])); + } + } + + declassify(prediction) { + if (prediction > 0.5) { + return this.classes[1]; + } else { + return this.classes[0]; + } + } + + render() { + + let content = (this.props.model === null)? + ( +
+

Would you like to download the model?

+

By clicking Accept below, you will download the model which may be between 100 mb in size to 1 gb.

+ +

{this.props.loading}

+
+ ) + :( +
+

BAI Model Prediction

+ + preview of predicted file{this.imagePredict(this.props.model)}}/> + + + + + +

{this.props.last_prediction ? this.declassify(this.props.last_prediction[0]):"Waiting for Prediction"}

+
+ ); + + return ( +
+ {content} +

About

+

You can find the source, dataset and model created for BAI on github at + https://www.github.com/RaspberryProgramming/BAI +

+
+ ); + } +} +const mapStateToProps = (state) => { + return {model: state.model.model, loading: state.model.loading, last_prediction: state.model.last_prediction}; +} + +export default connect(mapStateToProps, {downloadModel, loadingModel, predict})(Bai); diff --git a/src/components/css/Bai.css b/src/components/css/Bai.css new file mode 100644 index 0000000..364b4de --- /dev/null +++ b/src/components/css/Bai.css @@ -0,0 +1,52 @@ +.Bai { + color: white; + display: flex; + flex-direction: column; + align-items: flex-start; + padding: 10%; +} + +.Bai * { + margin-top: 5px; +} + +.Bai h1 { + margin-left: auto; + margin-right: auto; +} + +.Bai p { + margin-left: auto; + margin-right: auto; + text-align: center; +} + +.Bai img { + margin-left: auto; + margin-right: auto; + width: 50vw; +} + +.Bai button { + font-size: 24px; + margin-left: auto; + margin-right: auto; + background-color: #4f5f49; + border-style: none; + border-radius: 5px; + padding: 15px; + color: white; + +} + +.Bai input { + width:0; + height:0; +} + +.Bai .prediction { + background-color: #373747; + border-radius: 10px; + padding: 8px; + font-size: 16px; +} \ No newline at end of file diff --git a/src/reducers/index.js b/src/reducers/index.js index c7375a8..00bd921 100644 --- a/src/reducers/index.js +++ b/src/reducers/index.js @@ -5,6 +5,7 @@ import contactModalReducer from "./contactModalReducer"; import introReducer from "./introReducer"; import navigationReducer from "./navigationReducer"; import articlesReducer from "./articlesReducer"; +import modelReducer from "./modelReducer"; export default combineReducers({ github: githubReducer, @@ -12,4 +13,5 @@ export default combineReducers({ intro: introReducer, navigation: navigationReducer, articles: articlesReducer, + model: modelReducer, }); diff --git a/src/reducers/modelReducer.js b/src/reducers/modelReducer.js new file mode 100644 index 0000000..5c82964 --- /dev/null +++ b/src/reducers/modelReducer.js @@ -0,0 +1,16 @@ +//SET_MODEL +let modelReducer = (state={model: null, loading: ""}, action) => { + switch(action.type) { + case 'SET_MODEL': + return { ...state, model: action.payload }; + case 'SET_LOADING': + return { ...state, loading: action.payload }; + case 'SET_LAST_PRED': + return { ...state, last_prediction: action.payload }; + default: + return state; + } + }; + + +export default modelReducer; \ No newline at end of file