Thursday 28 January 2021

test flights

 expense
Jan 29 - driving 200km
Jan 30 - driving 200km, fuel, jeffery's dinner, dollarama
Jan 31 - driving 200km, fuel x 2, field dinner, hard drive
Feb 1 - driving 200km, fuel, hard drive, canadian tire
Feb 2 - driving 160km

time sheet
Jan 28 -  build mount - 8h - document review 2h
Jan 29 - install old new tabi - 14h
Jan 30 - trouble shoot new tabi shutter problem - 8h, bundle flight old, new tabi - 8h
Jan 31 - medicine hat survey 8h, documentation + backup 8h
Feb 1 - install uv 8h, bundle, water bath, power line 8h
Feb 2 - install new tabi + uv - 8h

THE GAMESTONK

Tuesday 26 January 2021

keras 18 RNN generate music

 
reference:

music vocab

install music21

pip install music21

#in pycharm type following code,
from music21 import *
configure.run()

# press enter for all default option -> install MuseScore 3
#in pycharm type
from music21 import *
s = corpus.parse('bach/bwv65.2.xml')
s.show()
#musescore3 opens -> click play music

Monday 25 January 2021

Chinese Auto Show 2021 Models



keras 17 RNN predict stock price 2


apple stock zoomed in view


tesla stock zoomed in view









#stock_xsample.py
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
from tensorflow.keras.models import load_model
import matplotlib.dates as mdates
import pandas_datareader.data as web
import datetime as dt

def predict_stock_trend(ticker, start, end, train_sample, train_epoch, forecast_days):

    df = web.DataReader(ticker, 'yahoo', start, end)

    data = df['Adj Close'].values
    #print(data)

    #scale input between 0, 1
    scaler = MinMaxScaler(feature_range=(0, 1))
    scaled_data = scaler.fit_transform(data.reshape(-1, 1))
    #print(scaled_data)
    #print(scaled_data.shape[0])

    x = []
    y = []
    sample = train_sample

    #y lags x by n samples
    for i in range(sample, scaled_data.shape[0]):
        x.append(scaled_data[i-sample:i, 0])
        y.append(scaled_data[i, 0])

    x, y = np.array(x), np.array(y)

    x = np.reshape(x, (x.shape[0], x.shape[1], 1))
    #print(x.shape)
    #(1208, n, 1)
    #print(x[0])
    print(y.shape)
    #print(y)

    #train on GPU
    pysical_devices = tf.config.experimental.list_physical_devices('GPU')
    #print("Num GPUs Available: ", len(pysical_devices))
    tf.config.experimental.set_memory_growth(pysical_devices[0], True)

    model = Sequential()
    model.add(LSTM(128, input_shape=(sample, 1), return_sequences=True))
    model.add(Dropout(0.2))

    model.add(LSTM(128, return_sequences=True))
    model.add(Dropout(0.1))

    model.add(LSTM(128, return_sequences=True))
    model.add(Dropout(0.1))

    model.add(LSTM(128))
    model.add(Dropout(0.1))

    model.add(Dense(1))
    model.summary()

    model.compile(
        loss='mean_squared_error',
        optimizer='adam',
    )

    model.fit(x,
              y,
              epochs=train_epoch,
              batch_size=32)

    model.save("stock_xsample.h5")

    x_forecast = scaled_data.flatten()
    #print(x_forecast.shape)
    #print(x_forecast[-50:])

    x_forecast = x_forecast[-sample:]
    y_forecast = np.array([])
    forecast_period = forecast_days
    #print(x_forecast)

    #model = load_model('stock_xsample.h5')

    #assume sample = 50. use last 50 known stock price to predict the next one
    #then use last 49 known stock price + previous predicted price to predict next one...
    #loop for forecast peroid, all forecasted price are recorded in y_forecast
    for i in range(0, forecast_period):
        if i < sample:
            x_forecast_new = np.append(x_forecast[i:], y_forecast)
        else:
            x_forecast_new = y_forecast[i-sample:]

        x_forecast_reshaped = np.reshape(x_forecast_new, (1, sample, 1))
        predicted_stock_price_single = model.predict(x_forecast_reshaped)
        y_forecast = np.append(y_forecast, predicted_stock_price_single[0][0])

    #print(y_forecast)
    #print(predicted_stock_price.shape)
    #print(predicted_stock_price)

    y_forecast = np.reshape(y_forecast, (len(y_forecast), 1))
    predicted_stock_price = scaler.inverse_transform(y_forecast)
    predicted_stock_price = predicted_stock_price.flatten()
    #print(predicted_stock_price)

    ax = plt.figure(figsize=(7, 5), dpi=100).add_subplot(111)
    #fig, ax = plt.subplots()

    t = df['Adj Close'].index
    t = pd.to_datetime(t)
    #print(t)

    #create xaxis for predicted stock price
    t_predicted = pd.bdate_range(end.strftime('%Y-%m-%d'), periods=forecast_period, freq="D")
    #print(t_predicted)

    ax.plot(t_predicted, predicted_stock_price, label="Predicted stock price", color="red")
    ax.plot(t, data, label="Real Stock Price", color="blue")

    ax.legend()
    ax.xaxis.set_major_locator(mdates.DayLocator(interval=300))
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d %Y'))

    ax.set_title(ticker + " Price Prediction")
    plt.ylabel("USD")

    ax.xaxis.grid(True, which="major")
    ax.yaxis.grid(True, which="major")

    plt.xticks(rotation=45)
    plt.subplots_adjust(bottom=.2)

    zoom_factory(ax)
    plt.show()


# for mouse scroll zoom
def zoom_factory(ax,base_scale = 1.2):
    def zoom_fun(event):
        # get the current x and y limits
        cur_xlim = ax.get_xlim()
        cur_ylim = ax.get_ylim()
        # set the range
        cur_xrange = (cur_xlim[1] - cur_xlim[0])*.5
        cur_yrange = (cur_ylim[1] - cur_ylim[0])*.5
        xdata = event.xdata # get event x location
        ydata = event.ydata # get event y location
        if event.button == 'up':
            # deal with zoom in
            scale_factor = 1/base_scale
        elif event.button == 'down':
            # deal with zoom out
            scale_factor = base_scale
        else:
            # deal with something that should never happen
            scale_factor = 1
            print (event.button)
        # set new limits
        ax.set_xlim([xdata - cur_xrange*scale_factor,
                     xdata + cur_xrange*scale_factor])
        ax.set_ylim([ydata - cur_yrange*scale_factor,
                     ydata + cur_yrange*scale_factor])
        ax.figure.canvas.draw_idle() # force re-draw the next time the GUI refreshes

    fig = ax.get_figure() # get the figure of interest
    # attach the call back
    fig.canvas.mpl_connect('scroll_event',zoom_fun)

    #return the function
    return zoom_fun


#predict_stock_trend(ticker, start, end, train_sample, train_epoch, forecast_days)
predict_stock_trend("FDX", dt.datetime(2015, 1, 1), dt.datetime.today(), 50, 100, 90)

reference:

pandas date_range, bdate_range

matplotlib zoom on scroll

python .gitignore

Sunday 24 January 2021

keras 16 RNN predict stock price

#training data from Jan 3 2012 to Dec 30 2016 
            Date    Open    High     Low   Close      Volume
0       1/3/2012  325.25  332.83  324.97  663.59   7,380,500
1       1/4/2012  331.27  333.87  329.08  666.45   5,749,400
2       1/5/2012  329.83  330.75  326.89  657.21   6,590,300
3       1/6/2012  328.34  328.77  323.68  648.24   5,405,900
4       1/9/2012  322.04  322.29  309.46  620.76  11,688,800
...          ...     ...     ...     ...     ...         ...
1253  12/23/2016  790.90  792.74  787.28  789.91     623,400
1254  12/27/2016  790.68  797.86  787.66  791.55     789,100
1255  12/28/2016  793.70  794.23  783.20  785.05   1,153,800
1256  12/29/2016  783.33  785.93  778.92  782.79     744,300
1257  12/30/2016  782.75  782.78  770.41  771.82   1,770,000

#testing data from Jan 3 2017 to Jan 31 2017
         Date    Open    High     Low   Close     Volume
0    1/3/2017  778.81  789.63  775.80  786.14  1,657,300
1    1/4/2017  788.36  791.34  783.16  786.90  1,073,000
2    1/5/2017  786.08  794.48  785.02  794.02  1,335,200
3    1/6/2017  795.26  807.90  792.20  806.15  1,640,200
...
16  1/26/2017  837.81  838.00  827.01  832.15  2,973,900
17  1/27/2017  834.71  841.95  820.44  823.31  2,965,800
18  1/30/2017  814.66  815.84  799.80  802.32  3,246,600
19  1/31/2017  796.86  801.25  790.52  796.79  2,160,600

#goal is to construct a model using 5 years' stock price to forecast next month stock trend

predicted trend follows the real one

#stock.py
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
from tensorflow.keras.models import load_model
import matplotlib.dates as mdates

csv = pd.read_csv("stock_train.csv")
#print(csv)

data = csv.loc[:, ("Open")].values
#print(data)

#scale input between 0, 1
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(data.reshape(-1, 1))
#print(scaled_data)
#print(scaled_data.shape[0])

x = []
y = []
#model is trained by sampling 50 days' stock price as input to fit the 51st day price as output
#y lags x by 50 samples
for i in range(50, scaled_data.shape[0]):
    x.append(scaled_data[i-50:i, 0])
    y.append(scaled_data[i, 0])

x, y = np.array(x), np.array(y)

x = np.reshape(x, (x.shape[0], x.shape[1], 1))
#print(x.shape)
#(1208, 50, 1)
#print(x[0])
#print(y.shape)
#(1208,)
#print(y)

#train on GPU
pysical_devices = tf.config.experimental.list_physical_devices('GPU')
#print("Num GPUs Available: ", len(pysical_devices))
tf.config.experimental.set_memory_growth(pysical_devices[0], True)
"""
model = Sequential()
model.add(LSTM(128, input_shape=(50, 1), return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(128, return_sequences=True))
model.add(Dropout(0.1))

model.add(LSTM(128, return_sequences=True))
model.add(Dropout(0.1))

model.add(LSTM(128))
model.add(Dropout(0.1))

model.add(Dense(1))
model.summary()

model.compile(
    loss='mean_squared_error',
    optimizer='adam',
)

model.fit(x,
          y,
          epochs=100,
          batch_size=32)

model.save("stock.h5")
"""
#append test.csv to train.csv
csv2 = pd.read_csv("stock_test.csv")
print(csv2)
csv3 = pd.concat((csv, csv2), axis=0)

#because model use previous 50 days's sample to predict 51st day, 
#needs to grab last 50 sample from train data and concat with test data
#get last 50 from train.csv plus all data from test.csv
data = csv3.loc[:, ("Open")].iloc[x.shape[0]:, ].values
#print(data.shape)
#print(data)

scaled_data = scaler.fit_transform(data.reshape(-1, 1))

x_test = []

for i in range(50, scaled_data.shape[0]):
    x_test.append(scaled_data[i-50:i, 0])

x_test = np.array(x_test)
x_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], 1))

print(x_test.shape)
#(20, 50, 1)

model = load_model('stock.h5')

predicted_stock_price = model.predict(x_test)
#print(predicted_stock_price.shape)
#print(predicted_stock_price)

#modal output is scaled between 0 and 1, needs convert back to real value
predicted_stock_price = scaler.inverse_transform(predicted_stock_price)
#rint(predicted_stock_price)

ax = plt.figure(figsize=(7, 5), dpi=100).add_subplot(111)

t = csv2.loc[:, "Date"].values
t = pd.to_datetime(t)

ax.plot(t, csv2.loc[:, ("Open")].values, label="Real stock price", color="red")
ax.plot(t, predicted_stock_price, label="Predicted Stock Price", color="blue")

ax.legend()
ax.xaxis.set_major_locator(mdates.DayLocator(interval=4))
ax.xaxis.set_major_formatter(mdates.DateFormatter('%b %d %Y'))

ax.set_title("Stock Price Prediction")
plt.ylabel("USD")

ax.xaxis.grid(True, which="major")
ax.yaxis.grid(True, which="major")

plt.xticks(rotation=45)
plt.subplots_adjust(bottom=.2)
plt.show()

------------------------------
#logs
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
lstm (LSTM)                  (None, 50, 128)           66560
_________________________________________________________________
dropout (Dropout)            (None, 50, 128)           0
_________________________________________________________________
lstm_1 (LSTM)                (None, 50, 128)           131584
_________________________________________________________________
dropout_1 (Dropout)          (None, 50, 128)           0
_________________________________________________________________
lstm_2 (LSTM)                (None, 50, 128)           131584
_________________________________________________________________
dropout_2 (Dropout)          (None, 50, 128)           0
_________________________________________________________________
lstm_3 (LSTM)                (None, 128)               131584
_________________________________________________________________
dropout_3 (Dropout)          (None, 128)               0
_________________________________________________________________
dense (Dense)                (None, 1)                 129
=================================================================
Total params: 461,441
Trainable params: 461,441
Non-trainable params: 0

Epoch 1/100
38/38 [==============================] - 5s 14ms/step - loss: 0.1101
Epoch 2/100
38/38 [==============================] - 1s 14ms/step - loss: 0.0039
Epoch 3/100
38/38 [==============================] - 1s 13ms/step - loss: 0.0030
...
Epoch 98/100
38/38 [==============================] - 0s 13ms/step - loss: 8.8894e-04
Epoch 99/100
38/38 [==============================] - 0s 13ms/step - loss: 8.8318e-04
Epoch 100/100
38/38 [==============================] - 0s 13ms/step - loss: 8.7235e-04

Super Mario Odyssey

Saturday 23 January 2021

智造美好生活 | 创

智造美好生活 | 深

keras 15 RNN lstm image recognition

pixel view of hand written digit,
training data shape (60000, 28, 28)
60000 images 28 x 28 pixels each
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM
import os
import numpy as np

mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()

"""
np.set_printoptions(linewidth=160)
for x in x_train[2]:
    print(x)
print(y_train[2])
print(x_train.shape)
"""

x_train = x_train/255.0
x_test = x_test/255.0

#print(x_train[0].shape)
# run on cpu
#os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

#train on GPU
pysical_devices = tf.config.experimental.list_physical_devices('GPU')
#print("Num GPUs Available: ", len(pysical_devices))
tf.config.experimental.set_memory_growth(pysical_devices[0], True)

model = Sequential()
model.add(LSTM(128, input_shape=(x_train.shape[1:]), return_sequences=True))
model.add(Dropout(0.2))

model.add(LSTM(128))
model.add(Dropout(0.1))

model.add(Dense(32, activation='relu'))
model.add(Dropout(0.2))

model.add(Dense(10, activation='softmax'))
model.summary()

opt = tf.keras.optimizers.Adam(lr=0.001, decay=1e-6)

model.compile(
    loss='sparse_categorical_crossentropy',
    optimizer=opt,
    metrics=['accuracy'],
)

model.fit(x_train,
          y_train,
          epochs=3,
          validation_data=(x_test, y_test))

------------------------------------
#logs
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
lstm (LSTM)                  (None, 28, 128)           80384
_________________________________________________________________
dropout (Dropout)            (None, 28, 128)           0
_________________________________________________________________
lstm_1 (LSTM)                (None, 128)               131584
_________________________________________________________________
dropout_1 (Dropout)          (None, 128)               0
_________________________________________________________________
dense (Dense)                (None, 32)                4128
_________________________________________________________________
dropout_2 (Dropout)          (None, 32)                0
_________________________________________________________________
dense_1 (Dense)              (None, 10)                330
=================================================================
Total params: 216,426
Trainable params: 216,426
Non-trainable params: 0

1875/1875 [==============================] - 87s 46ms/step - loss: 1.0635 - accuracy: 0.6280 - val_loss: 0.1618 - val_accuracy: 0.9562
Epoch 2/3
1875/1875 [==============================] - 85s 46ms/step - loss: 0.1688 - accuracy: 0.9536 - val_loss: 0.0958 - val_accuracy: 0.9709
Epoch 3/3
1875/1875 [==============================] - 87s 46ms/step - loss: 0.1132 - accuracy: 0.9702 - val_loss: 0.0888 - val_accuracy: 0.9743

reference:

cannot import name 'CuDNNLSTM'

智造美好生活 | 医

Wednesday 20 January 2021

新加坡的生活

keras 14 tensorflowjs mobileNet & ionic react 2

project site: http://chuanshuoge-ionic-tensorflowjs-mobilenet.surge.sh
open website on phone browser, browse local image
press predict, start calculating prediction result

predicted a soccer ball

predicted a basketball

open browser on desktop -> browser a goose image ->
copy paste image url -> predict

mobileNet distinguish duck from goose

//tensorflow_mobileNet.js

import React, { useState, useEffect } from 'react';
import * as tf from '@tensorflow/tfjs'
import imagenet_label from './imagenet_label.json'
import '../pages/Home.css';
import domtoimage from 'dom-to-image';

export default function MobileNet(props) {
    const [model, setModel] = useState(null)
    const [model_predictions, setModel_predictions] = useState(null)

    useEffect(() => { loadModel() }, [props.predict]);

    const loadModel = async () => {
        const m = await tf.loadLayersModel('/assets/model.json')
        setModel(m)

        if (props.predict > 0) {
            setModel_predictions(null)

            //convert media to png before processing image
            domtoimage.toPng(props.image)
                .then(function (dataUrl) {
                    var img = new Image();
                    img.src = dataUrl;

                    //wait for new png image
                    var timer = setInterval(function () {
                        if (img.src) {
                            processImage(img)
                            clearInterval(timer)
                        }
                    }, 100)

                })
                .catch(function (error) {
                    alert("error converting image to png");
                });
        }
    }

    const processImage = async (image) => {
        const pixels = tf.browser.fromPixels(image).resizeNearestNeighbor([224, 224]).toFloat()
        const offset = tf.scalar(127.5)
        const preprocessed_image = pixels.sub(offset).div(offset).expandDims()

        const predictions = await model.predict(preprocessed_image).data()

        //predictions is Float32Array(1000), find top5 value with their index
        let top5 = []

        for (let i = 0; i < 5; i++) {
            const imax = predictions.indexOf(Math.max(...predictions));

            const key = imagenet_label[imax.toString()]
            const value = predictions[imax].toExponential(2).toString()

            top5.push(<tr key={key}>
                <td>{key}</td>
                <td>{value}</td>
            </tr>)

            predictions[imax] = 0
        }

        /*top5
        Array(5)       ​
            0: Object { "bald eagle, American eagle, Haliaeetus leucocephalus": "9.60e-1" }
            1: Object { kite: "3.93e-2" }
            2: Object { "black grouse": "9.76e-5" }
            3: Object { vulture: "8.26e-5" }
            4: Object { "black stork, Ciconia nigra": "7.81e-5" }
        */

        setModel_predictions(top5)
    }

    return (
        <div>
            {props.predict > 0 && !model_predictions ? <span>Calculating...</span> : null}
            {model_predictions ?
                <table>
                    <thead><tr><th>Object</th><th>Probability</th></tr></thead>
                    <tbody>{model_predictions}</tbody>
                </table> : null}
        </div>
    )
}

------------------------------
//home.tsx

import {
  IonContent, IonHeader, IonPage, IonTitle, IonToolbar,
  IonCard, IonCardContent, IonCardHeader, IonCardSubtitle, IonCardTitle,
  IonItem, IonIcon, IonLabel, IonButton, IonInput
} from '@ionic/react';
import React, { useState } from 'react';
import './Home.css';
import MobileNet from '../components/tensorflow_mobileNet'

const Home: React.FC = () => {
  const [predictClick, setPredictClick] = useState(0);
  const [imageUrl, setImageUrl] = useState('https://images.theconversation.com/files/308043/original/file-20191220-11924-iakhpb.jpeg?ixlib=rb-1.1.0&q=45&auto=format&w=754&fit=clip')

  const browseImg = (e: any) => {
    const fileURL = URL.createObjectURL(e.target.files[0])
    //get url where uploaded image is temporarily stored
    //blob:http://localhost:3000/11c28a94-d9df-43e1-ab6c-2185e1449eb6
    setImageUrl(fileURL)
  }

  return (
    <IonPage>
      <IonHeader>
        <IonToolbar>
          <IonTitle>
            Predict Image with MobileNet
          </IonTitle>
        </IonToolbar>
      </IonHeader>
      <IonContent>

        <IonCard>
          <IonCardHeader>
            <IonCardTitle>
              MobileNet Input Image
              </IonCardTitle>
          </IonCardHeader>
          <IonItem lines="none">
            <IonButton fill="outline"
              onClick={() => document.getElementById('input-image-browser')?.click()}>
              Browse Local Image</IonButton>
            <input hidden type="file" accept="image/*" id="input-image-browser"
              onChange={(e) => browseImg(e)}></input>
          </IonItem>
          <IonItem >
            <IonLabel slot="start">Paste Web Image URL</IonLabel>
            <IonInput type="url" placeholder="https://image.com/image.jpg"
              onIonChange={(e) => setImageUrl(e.detail.value!)}></IonInput>
          </IonItem>
          <IonItem lines="none">
            <img id='test_image' src={imageUrl} alt="Input Image"></img>
          </IonItem>
          <IonItem lines="none">
            <IonButton fill="outline" onClick={() => setPredictClick(predictClick + 1)}>Predict</IonButton>
          </IonItem>
          <IonCardHeader>
            <IonCardTitle>
              MobileNet Prediction Result
              </IonCardTitle>
          </IonCardHeader>
          <IonItem lines="none">
            <MobileNet image={document.getElementById('test_image')} predict={predictClick} />
          </IonItem>
        </IonCard>

      </IonContent>
    </IonPage>
  );
};

export default Home;

------------------------------
//package.json

{
  "name": "ionic-keras",
  "version": "0.0.1",
  "private": true,
  "dependencies": {
    "@capacitor/core": "2.4.6",
    "@ionic/react": "^5.0.7",
    "@ionic/react-router": "^5.0.7",
    "@tensorflow/tfjs": "^2.8.4",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.4.0",
    "@testing-library/user-event": "^8.0.3",
    "@types/jest": "^24.0.25",
    "@types/node": "^12.12.24",
    "@types/react": "^16.9.17",
    "@types/react-dom": "^16.9.4",
    "@types/react-router": "^5.1.4",
    "@types/react-router-dom": "^5.1.3",
    "dom-to-image": "^2.6.0",
    "ionicons": "^5.0.0",
    "react": "^16.13.0",
    "react-dom": "^16.13.0",
    "react-router": "^5.1.2",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.4.0",
    "typescript": "3.8.3"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "@capacitor/cli": "2.4.6"
  },
  "description": "An Ionic project"
}

reference:

Tuesday 19 January 2021

keras 13 tensorflowjs mobileNet & ionic react

create a python project to convert mobileNet model for tensorflowjs
#powershell
pip install tensorflow
pip install tensorflowjs

#main.py
import tensorflowjs as tfjs
import tensorflow as tf

mobileNet = tf.keras.applications.mobilenet.MobileNet()
tfjs.converters.save_keras_model(mobileNet,'mobileNet')

create ionic react project, 
copy paste model generated from python project into public/assets folder
//cmd 
ionic start app-name
npm install @tensorflow/tfjs
npm install dom-to-image

reference:


react serve static files

imagenet labels

dom to image - media source unsecure error

get temporary url for browsed local image

Sunday 17 January 2021

keras 12 vgg16 & django

code linkhttps://github.com/chuanshuoge6/keras-vgg16 

copy paste web image url, click submit

copy paste another web image url

browse image on computer

#views.py
import os
from django.shortcuts import render
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications import imagenet_utils
import numpy as np
import urllib.request

# Create your views here.
def index(request):
    return render(request, 'index.html')

def local_image(request):
    image_file = request.FILES['image_file']

    with open('static/static-image.jpg', 'wb') as f:
        for chunk in image_file.chunks():
            f.write(chunk)

    prediction = predict_image('static/static-image.jpg')

    return render(request, 'index.html', {'prediction': prediction, "local_image_file": image_file})

def web_image(request):
    image_file = request.POST['image_file']

    urllib.request.urlretrieve(image_file, "static/static-image.jpg")

    prediction = predict_image('static/static-image.jpg')

    return render(request, 'index.html', {'prediction': prediction, "web_image_file": image_file})

def predict_image(file):
    # run on cpu
    os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

    vgg16 = tf.keras.applications.vgg16.VGG16()

    img = image.load_img(file, target_size=(224, 224))
    img_array = image.img_to_array(img)
    img_array_expanded_dims = np.expand_dims(img_array, axis=0)

    preprocessed_image = tf.keras.applications.vgg16.preprocess_input(img_array_expanded_dims)
    predictions = vgg16.predict(preprocessed_image)

    return imagenet_utils.decode_predictions(predictions)[0]

-----------------------
#index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>VGG16</title>
    <style>
        table, th, td {
            border: 1px solid black;
        }
        tr:nth-child(even) {background-color: #f2f2f2;}
        th, td {padding: 10px;}
        img{height: 300px; width: 300px}
    </style>
</head>
<body>
    <h2>Predict Image with VGG16</h2>

    <h4>Select local image</h4>
    <form action="{% url 'local_image'  %}" enctype="multipart/form-data" method="post">
        {% csrf_token %}
        <input type="file" accept="image/*" name="image_file" id="local_image_file" onchange="local_image_change()">
        <input type="submit" id="submit_button" style="display:none">
    </form>

    <h4>Copy web image url</h4>
    <form action="{% url 'web_image'  %}" method="post">
        {% csrf_token %}
        {% if web_image_file %}
            <input name="image_file" type="url" id="web_image_url"
               value={{web_image_file}}
               onchange="web_image_change()">
        {% else %}
            <input name="image_file" type="url" id="web_image_url2"
               value="https://images.theconversation.com/files/308043/original/file-20191220-11924-iakhpb.jpeg?ixlib=rb-1.1.0&q=45&auto=format&w=754&fit=clip"
               onchange="web_image_change2()">
        {% endif %}
        <input type="submit" >
    </form>

    <h4>VGG16 input image</h4>
    {% if local_image_file %}
        {% load static %}
        <img id="selected_image3" src="{% static 'static-image.jpg' %}">
    {% elif web_image_file %}
        <img id="selected_image" src="{{web_image_file}}">
    {% else %}
        {% load static %}
        <img id="selected_image2" src="{% static 'spider.jpg' %}">
    {% endif %}

    <h4>VGG16 Prediction Result</h4>
    <table>
        <tr>
            <th>Object</th>
            <th>Probability</th>
        </tr>
        {% for row in prediction %}
            <tr>
                {% for col in row %}
                    {% if forloop.counter != 1 %}
                        <td>{{ col|safe }}</td>
                    {% endif %}
                {% endfor %}
            </tr>
        {% endfor %}
    </table>

    <script>
        function local_image_change(){
           var img_input = document.getElementById("local_image_file")
           try{document.getElementById("selected_image2").src = URL.createObjectURL(img_input.files[0])}catch(err){}
           try{document.getElementById("selected_image3").src = URL.createObjectURL(img_input.files[0])}catch(err){}
           document.getElementById("submit_button").click()
        }

        function web_image_change(){
           document.getElementById("selected_image").src =  document.getElementById("web_image_url").value
        }

        function web_image_change2(){
           document.getElementById("selected_image2").src =  document.getElementById("web_image_url2").value
        }
    </script>
</body>
</html>

-----------------------------------
#urls.py

from django.contrib import admin
from django.urls import path
from mobileNet import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name='index'),
    path('local_image/', views.local_image, name='local_image'),
    path('web_image/', views.web_image, name='web_image'),
]

--------------------------------
#settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [(os.path.join(BASE_DIR, 'static'))]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

-------------------------------
#powershell

pip install whitenoise gunicorn
pip freeze > requirements.txt

------------------------------
#requirements

absl-py==0.11.0
asgiref==3.3.1
astunparse==1.6.3
cachetools==4.2.0
certifi==2020.12.5
chardet==4.0.0
Django==3.1.5
django-watchman==1.2.0
flatbuffers==1.12
gast==0.3.3
google-auth==1.24.0
google-auth-oauthlib==0.4.2
google-pasta==0.2.0
grpcio==1.32.0
gunicorn==20.0.4
h5py==2.10.0
idna==2.10
joblib==1.0.0
Keras-Preprocessing==1.1.2
Markdown==3.3.3
numpy==1.19.5
oauthlib==3.1.0
opt-einsum==3.3.0
Pillow==8.1.0
protobuf==3.14.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
pytz==2020.5
requests==2.25.1
requests-oauthlib==1.3.0
rsa==4.7
scikit-learn==0.24.0
six==1.15.0
sklearn==0.0
sqlparse==0.4.1
tensorboard==2.4.1
tensorboard-plugin-wit==1.7.0
tensorflow==2.4.0
tensorflow-estimator==2.4.0
termcolor==1.1.0
threadpoolctl==2.1.0
typing-extensions==3.7.4.3
urllib3==1.26.2
Werkzeug==1.0.1
whitenoise==5.2.0
wrapt==1.12.1

reference:

save web image to local

django template table

django serve static image

django multipart form file upload

python copy InMemoryUploadedFile object to disk

heroku deploy slug size too large
 Compiled slug size: 526.6M is too large (max is 500M).

How to mirror phone display to pc?

download vysor

connect usb between phone and pc
enable usb debugging on phone
start vysor
click play button on detected device

control phone from pc with mouse

drag mouse on pc to switch screen on phone
reference:

Saturday 16 January 2021

keras 11 mobileNet 2


mobileNet prediction
[[('n02510455', 'giant_panda', 0.9997156), 
('n02509815', 'lesser_panda', 0.00016485745), 
('n02447366', 'badger', 8.6640284e-05), 
('n02445715', 'skunk', 1.2499775e-05),
 ('n02443114', 'polecat', 1.1246146e-05)]]
#mobileNet.py
import  tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Activation, Dense, Flatten, Conv2D, MaxPool2D, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.metrics import categorical_crossentropy
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications import imagenet_utils
from sklearn.metrics import confusion_matrix
import itertools
import shutil
import random
import os
import glob
import matplotlib.pyplot as plt
from tensorflow.keras.models import load_model
import numpy as np

#train on GPU
pysical_devices = tf.config.experimental.list_physical_devices('GPU')
#print("Num GPUs Available: ", len(pysical_devices))
tf.config.experimental.set_memory_growth(pysical_devices[0], True)

mobile = tf.keras.applications.mobilenet.MobileNet()

def prepare_image(file):
    img = image.load_img('data/' + file, target_size=(224, 224))
    img_array = image.img_to_array(img)
    img_array_expanded_dims = np.expand_dims(img_array, axis=0)
    return tf.keras.applications.mobilenet.preprocess_input(img_array_expanded_dims)

preprocessed_image = prepare_image('panda.jpg')
predictions = mobile.predict(preprocessed_image)
results = imagenet_utils.decode_predictions(predictions)
print(results)

reference: