Skip to content

Instantly share code, notes, and snippets.

View SkalskiP's full-sized avatar

Piotr Skalski SkalskiP

View GitHub Profile
@SkalskiP
SkalskiP / saving_model.py
Last active May 15, 2018 22:29
saving trained Python model in from readable by TensorFlow.js
import tensorflowjs as tfjs
# creating and training of model using Keras
# ...
tfjs.converters.save_keras_model(model, './ModelJS')
@SkalskiP
SkalskiP / saving_model.sh
Last active May 16, 2018 16:09
saving trained Python model in from readable by TensorFlow.js
$ tensorflowjs_converter --input_format keras ./ModelPY/model.h5 ./../ModelJS
@SkalskiP
SkalskiP / loading_model.js
Created May 15, 2018 23:30
Loading the model to the application
import * as tf from '@tensorflow/tfjs';
// Definition of the component supporting the model
// ...
protected async loadModel() {
this.model = await tf.loadModel(AppSettings.mnistModelUrl);
}
@SkalskiP
SkalskiP / make_prediction.js
Created May 16, 2018 16:15
making predictions based on image data
protected async predict(imageData: ImageData) {
const pred = await tf.tidy(() => {
let img:any = tf.fromPixels(imageData, 1);
img = img.reshape([1, 28, 28, 1]);
img = tf.cast(img, 'float32');
const output = this.model.predict(img) as any;
@SkalskiP
SkalskiP / adding_tensorflow.html
Created May 16, 2018 16:33
adding tensorflow/tfjs to your project
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]"> </script>
@SkalskiP
SkalskiP / first_model_layer.py
Created May 16, 2018 17:30
parameters of the first layer in Keras Mnist model
model.add(Conv2D(
filters = 32,
kernel_size = (5,5),
padding = 'Same',
activation ='relu',
input_shape = (28,28,1)
))
@SkalskiP
SkalskiP / simple_nn.py
Created August 13, 2018 16:32
Simple KERAS neural network for binary classification
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(4, input_dim=2,activation='relu'))
model.add(Dense(6, activation='relu'))
model.add(Dense(6, activation='relu'))
model.add(Dense(4, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
@SkalskiP
SkalskiP / create_frames.py
Created September 9, 2018 22:16
Function to create animation frames
def create_frames(dt, steps, padding, output_dir):
fig, ax = create_blank_chart_with_styling((6, 6))
# creation of data describing the trajectory
xs, ys, zs = build_lorenz_trajectory(dt, steps)
# setting the fixed range of axes
ax.set_xlim3d(xs.min() - padding, xs.max() + padding)
ax.set_ylim3d(ys.min() - padding, ys.max() + padding)
ax.set_zlim3d(zs.min() - padding, zs.max() + padding)
for i in range(steps-1):
@SkalskiP
SkalskiP / lorenz_animation_imports.py
Last active September 13, 2018 18:36
Libraries that have to be imported to create 3D animation using matplotlib.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib.animation import FuncAnimation
@SkalskiP
SkalskiP / lorenz_animation_init.py
Created September 13, 2018 18:12
Animation initiation process
# Calculation of the points belonging to the three trajectories,
# based on the given starting conditions
plots_data = [build_lorenz_trajectory(DELTA_T, STEPS,
initial_values=initial_conditions[i]) for i in range(3)]
# Creation of an empty chart
fig, ax = create_blank_chart_with_styling(plots_data, PADDING, (8, 8))
# Setting up (for the time being empty) data sequences for each trajectory
plots = [ax.plot([],[],[], color=colors[i], label=str(initial_conditions[i]),