From 1f98512b86097d5b49ad489e7d329fdaf19d8b03 Mon Sep 17 00:00:00 2001 From: ml server Date: Tue, 21 Jan 2020 11:45:59 -0700 Subject: [PATCH] Add voila web app --- jupyter/wut-web.ipynb | 329 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 329 insertions(+) create mode 100644 jupyter/wut-web.ipynb diff --git a/jupyter/wut-web.ipynb b/jupyter/wut-web.ipynb new file mode 100644 index 0000000..6264bff --- /dev/null +++ b/jupyter/wut-web.ipynb @@ -0,0 +1,329 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# wut-predict --- What U Think? SatNOGS Observation AI, makes predictions.\n", + "#\n", + "# https://spacecruft.org/spacecruft/satnogs-wut\n", + "# Based on data/train and data/val directories builds a wut.h5 file.\n", + "# Reads wut.h5 and tests files in data/test/unvetted/" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# GPLv3+" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Built using Jupyter, Tensorflow, Keras" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import tensorflow.python.keras" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from tensorflow.python.keras import Sequential\n", + "from tensorflow.python.keras.layers import Activation, Dropout, Flatten, Dense\n", + "from tensorflow.python.keras.preprocessing.image import ImageDataGenerator\n", + "from tensorflow.python.keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D\n", + "from tensorflow.python.keras import optimizers\n", + "from tensorflow.python.keras.preprocessing import image\n", + "from tensorflow.python.keras.models import load_model\n", + "from tensorflow.python.keras.preprocessing.image import load_img\n", + "from tensorflow.python.keras.preprocessing.image import img_to_array" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Visualization\n", + "%matplotlib inline\n", + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "from sklearn.decomposition import PCA" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Seaborn pip dependency\n", + "import seaborn as sns" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Interact\n", + "# https://ipywidgets.readthedocs.io/en/stable/examples/Using%20Interact.html\n", + "from __future__ import print_function\n", + "from ipywidgets import interact, interactive, fixed, interact_manual\n", + "import ipywidgets as widgets" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Display Images\n", + "from IPython.display import display, Image" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "model = load_model('data/models/wut-DUV-201912.tf')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "test_dir = os.path.join('data/', 'test')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "num_test = len(os.listdir(test_dir))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Good results\n", + "#batch_size = 128\n", + "#epochs = 6\n", + "# Testing, faster more inaccurate results\n", + "batch_size = 32\n", + "epochs = 3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Half size\n", + "IMG_HEIGHT = 416\n", + "IMG_WIDTH= 804\n", + "# Full size, machine barfs probably needs more RAM\n", + "#IMG_HEIGHT = 832\n", + "#IMG_WIDTH = 1606" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "test_image_generator = ImageDataGenerator(\n", + " rescale=1./255\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "test_data_gen = test_image_generator.flow_from_directory(batch_size=batch_size,\n", + " directory=test_dir,\n", + " target_size=(IMG_HEIGHT, IMG_WIDTH),\n", + " class_mode='binary')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "sample_test_images, _ = next(test_data_gen)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# This function will plot images in the form of a grid with 1 row and 3 columns where images are placed in each column.\n", + "def plotImages(images_arr):\n", + " fig, axes = plt.subplots(1, 3, figsize=(20,20))\n", + " axes = axes.flatten()\n", + " for img, ax in zip( images_arr, axes):\n", + " ax.imshow(img)\n", + " ax.axis('off')\n", + " plt.tight_layout()\n", + " plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "plotImages(sample_test_images[0:1])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "prediction = model.predict(\n", + " x=test_data_gen,\n", + " verbose=0\n", + ")\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "predictions=[]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "prediction_bool = (prediction >0.8)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "predictions = prediction_bool.astype(int)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "if prediction_bool[0] == False:\n", + " rating = 'bad'\n", + "else:\n", + " rating = 'good'\n", + "print('Observation: %s' % (rating))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# The End" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.3" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}