Created
January 4, 2020 01:21
-
-
Save rozeappletree/6f388189724199554629f203542770e4 to your computer and use it in GitHub Desktop.
swift-keras-tflite-mlir.ipynb
This file contains hidden or 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
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "name": "swift-keras-tflite-mlir.ipynb", | |
| "provenance": [], | |
| "collapsed_sections": [], | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "swift", | |
| "display_name": "Swift" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/rakesh4real/6f388189724199554629f203542770e4/swift-keras-tflite-mlir.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "DBX6ou638P3w", | |
| "colab_type": "code", | |
| "outputId": "4017f10d-53c4-4f75-ffbd-59c2d4eb8f0b", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 578 | |
| } | |
| }, | |
| "source": [ | |
| "%install-location $cwd/swift-install\n", | |
| "%install '.package(url: \"https://github.com/tensorflow/swift-models\", .branch(\"master\"))' Datasets" | |
| ], | |
| "execution_count": 1, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "Installing packages:\n", | |
| "\t.package(url: \"https://github.com/tensorflow/swift-models\", .branch(\"master\"))\n", | |
| "\t\tDatasets\n", | |
| "With SwiftPM flags: []\n", | |
| "Working in: /tmp/tmpci8y9p4v/swift-install\n", | |
| "Fetching https://github.com/tensorflow/swift-models\n", | |
| "Fetching https://github.com/kylef/Commander.git\n", | |
| "Fetching https://github.com/kylef/Spectre.git\n", | |
| "Cloning https://github.com/kylef/Commander.git\n", | |
| "Resolving https://github.com/kylef/Commander.git at 0.9.1\n", | |
| "Cloning https://github.com/kylef/Spectre.git\n", | |
| "Resolving https://github.com/kylef/Spectre.git at 0.9.0\n", | |
| "Cloning https://github.com/tensorflow/swift-models\n", | |
| "Resolving https://github.com/tensorflow/swift-models at master\n", | |
| "[1/7] Compiling ModelSupport Stderr.swift\n", | |
| "[2/7] Compiling ModelSupport FileManagement.swift\n", | |
| "[3/7] Compiling ModelSupport Image.swift\n", | |
| "[4/8] Merging module ModelSupport\n", | |
| "[5/14] Wrapping AST for ModelSupport for debugging\n", | |
| "[6/14] Compiling Datasets CIFAR10.swift\n", | |
| "[7/14] Compiling Datasets DatasetUtilities.swift\n", | |
| "[8/14] Compiling Datasets ImageClassificationDataset.swift\n", | |
| "[9/14] Compiling Datasets Imagenette.swift\n", | |
| "[10/14] Compiling Datasets LabeledExample.swift\n", | |
| "[11/14] Compiling Datasets MNIST.swift\n", | |
| "[12/15] Merging module Datasets\n", | |
| "[13/16] Wrapping AST for Datasets for debugging\n", | |
| "[14/16] Compiling jupyterInstalledPackages jupyterInstalledPackages.swift\n", | |
| "[15/17] Merging module jupyterInstalledPackages\n", | |
| "[16/17] Wrapping AST for jupyterInstalledPackages for debugging\n", | |
| "[17/17] Linking libjupyterInstalledPackages.so\n", | |
| "Initializing Swift...\n", | |
| "Installation complete!\n" | |
| ], | |
| "name": "stdout" | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "e32OUw3UxpuH", | |
| "colab_type": "code", | |
| "colab": {} | |
| }, | |
| "source": [ | |
| "import Python\n", | |
| "import TensorFlow" | |
| ], | |
| "execution_count": 0, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "Hrh7BrNR31Nx", | |
| "colab_type": "code", | |
| "colab": {} | |
| }, | |
| "source": [ | |
| "// ----------------------------------\n", | |
| "// Train CIFAR model\n", | |
| "// ----------------------------------\n", | |
| "\n", | |
| "struct CIFAR_Keras: Layer {\n", | |
| " typealias Input = Tensor<Float>\n", | |
| " typealias Output = Tensor<Float>\n", | |
| "\n", | |
| " var conv1a = Conv2D<Float>(filterShape: (3, 3, 3, 32), padding: .same, activation: relu)\n", | |
| " var conv1b = Conv2D<Float>(filterShape: (3, 3, 32, 32), activation: relu)\n", | |
| " var pool1 = MaxPool2D<Float>(poolSize: (2, 2), strides: (2, 2))\n", | |
| " // var dropout1 = Dropout<Float>(probability: 0.25)\n", | |
| "\n", | |
| " var conv2a = Conv2D<Float>(filterShape: (3, 3, 32, 64), padding: .same, activation: relu)\n", | |
| " var conv2b = Conv2D<Float>(filterShape: (3, 3, 64, 64), activation: relu)\n", | |
| " var pool2 = MaxPool2D<Float>(poolSize: (2, 2), strides: (2, 2))\n", | |
| " // var dropout2 = Dropout<Float>(probability: 0.25)\n", | |
| "\n", | |
| " var flatten = Flatten<Float>()\n", | |
| " var dense1 = Dense<Float>(inputSize: 64 * 6 * 6, outputSize: 512, activation: relu)\n", | |
| " //var dropout3 = Dropout<Float>(probability: 0.5)\n", | |
| " var dense2 = Dense<Float>(inputSize: 512, outputSize: 512, activation: relu)\n", | |
| " var output = Dense<Float>(inputSize: 512, outputSize: 10, activation: softmax)\n", | |
| "\n", | |
| " @differentiable\n", | |
| " func callAsFunction(_ input: Input) -> Output {\n", | |
| " let conv1 = input.sequenced(through: conv1a, conv1b, pool1) //, dropout1)\n", | |
| " let conv2 = conv1.sequenced(through: conv2a, conv2b, pool2) //, dropout2)\n", | |
| " return conv2.sequenced(through: flatten, dense1, dense2, output) //dropout3, dense2)\n", | |
| " }\n", | |
| "}" | |
| ], | |
| "execution_count": 0, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "ngqpop4c5h80", | |
| "colab_type": "code", | |
| "outputId": "863a24af-f85d-4c13-8b48-1c08c3590c49", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 320 | |
| } | |
| }, | |
| "source": [ | |
| "// TRAIN\n", | |
| "\n", | |
| "import Datasets\n", | |
| "\n", | |
| "let batchSize = 100\n", | |
| "\n", | |
| "let dataset = CIFAR10()\n", | |
| "let testBatches = dataset.testDataset.batched(batchSize)\n", | |
| "\n", | |
| "var model = CIFAR_Keras()\n", | |
| "let optimizer = RMSProp(for: model, learningRate: 0.0001, decay: 1e-6)\n", | |
| "\n", | |
| "print(\"Starting training...\")\n", | |
| "\n", | |
| "for epoch in 1...20 {\n", | |
| " Context.local.learningPhase = .training\n", | |
| " var trainingLossSum: Float = 0\n", | |
| " var trainingBatchCount = 0\n", | |
| " let trainingShuffled = dataset.trainingDataset.shuffled(\n", | |
| " sampleCount: dataset.trainingExampleCount, randomSeed: Int64(epoch))\n", | |
| " for batch in trainingShuffled.batched(batchSize) {\n", | |
| " let (labels, images) = (batch.label, batch.data)\n", | |
| " let (loss, gradients) = valueWithGradient(at: model) { model -> Tensor<Float> in\n", | |
| " let logits = model(images)\n", | |
| " return softmaxCrossEntropy(logits: logits, labels: labels)\n", | |
| " }\n", | |
| " trainingLossSum += loss.scalarized()\n", | |
| " trainingBatchCount += 1\n", | |
| " optimizer.update(&model, along: gradients)\n", | |
| " }\n", | |
| "\n", | |
| " Context.local.learningPhase = .inference\n", | |
| " var testLossSum: Float = 0\n", | |
| " var testBatchCount = 0\n", | |
| " var correctGuessCount = 0\n", | |
| " var totalGuessCount = 0\n", | |
| " for batch in testBatches {\n", | |
| " let (labels, images) = (batch.label, batch.data)\n", | |
| " let logits = model(images)\n", | |
| " testLossSum += softmaxCrossEntropy(logits: logits, labels: labels).scalarized()\n", | |
| " testBatchCount += 1\n", | |
| "\n", | |
| " let correctPredictions = logits.argmax(squeezingAxis: 1) .== labels\n", | |
| " correctGuessCount = correctGuessCount\n", | |
| " + Int(\n", | |
| " Tensor<Int32>(correctPredictions).sum().scalarized())\n", | |
| " totalGuessCount = totalGuessCount + batchSize\n", | |
| " }\n", | |
| "\n", | |
| " let accuracy = Float(correctGuessCount) / Float(totalGuessCount)\n", | |
| " print(\n", | |
| " \"\"\"\n", | |
| " [Epoch \\(epoch)] \\\n", | |
| " Accuracy: \\(correctGuessCount)/\\(totalGuessCount) (\\(accuracy)) \\\n", | |
| " Loss: \\(testLossSum / Float(testBatchCount))\n", | |
| " \"\"\"\n", | |
| " )\n", | |
| "}" | |
| ], | |
| "execution_count": 4, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "Preparing CIFAR dataset...\r\n", | |
| "Archive missing, downloading...\n", | |
| "Archive downloaded, processing...\n", | |
| "Unarchiving completed\n", | |
| "Starting training...\n", | |
| "[Epoch 1] Accuracy: 4236/10000 (0.4236) Loss: 2.0377867\n", | |
| "[Epoch 2] Accuracy: 4666/10000 (0.4666) Loss: 1.9923123\n", | |
| "[Epoch 3] Accuracy: 4818/10000 (0.4818) Loss: 1.9769447\n", | |
| "[Epoch 4] Accuracy: 5272/10000 (0.5272) Loss: 1.9336563\n", | |
| "[Epoch 5] Accuracy: 5492/10000 (0.5492) Loss: 1.9105333\n" | |
| ], | |
| "name": "stdout" | |
| }, | |
| { | |
| "output_type": "error", | |
| "ename": "", | |
| "evalue": "ignored", | |
| "traceback": [ | |
| "Current stack trace:", | |
| "\tframe #30: 0x00007f780dac7443 $__lldb_expr30`AD__$s14__lldb_expr_2911CIFAR_KerasV14callAsFunctiony10TensorFlow0H0VySfGAHF__pullback_src_0_wrt_1 at <Cell 3>:27:27", | |
| "\tframe #32: 0x00007f780da2aa7c $__lldb_expr36`partial apply for AD__$s14__lldb_expr_3510TensorFlow0C0VySfG02__a1_B3_2911CIFAR_KerasVcfU___pullback_src_0_wrt_0 [inlined] AD__$s14__lldb_expr_3510TensorFlow0C0VySfG02__a1_B3_2911CIFAR_KerasVcfU___pullback_src_0_wrt_0 at <Cell 4>:24:26", | |
| "\tframe #40: 0x00007f780da206c6 $__lldb_expr36`main at <Cell 4>:23:33" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "nn7yv1cDGayR", | |
| "colab_type": "code", | |
| "colab": {} | |
| }, | |
| "source": [ | |
| "// `model` is the swift trained model\n", | |
| "// Note: var model = CIFAR_Keras() in `TRAIN` section\n", | |
| "// `model` has all the required weights which will\n", | |
| "// be copied to python imported keras model\n", | |
| "let swift_model = model" | |
| ], | |
| "execution_count": 0, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "kZRlD4utdPuX", | |
| "colab_type": "code", | |
| "colab": {} | |
| }, | |
| "source": [ | |
| "func save_swift_kerash5(){\n", | |
| " let keras = Python.import(\"keras\")\n", | |
| " let model = keras.Sequential()\n", | |
| "\n", | |
| " let _conv2D = keras.layers.Conv2D\n", | |
| " let _maxPool2D = keras.layers.MaxPooling2D\n", | |
| " let _flatten = keras.layers.Flatten\n", | |
| " let _dense = keras.layers.Dense\n", | |
| "\n", | |
| " // 1\n", | |
| " model.add(\n", | |
| " _conv2D(\n", | |
| " filters: 32,\n", | |
| " kernel_size: [3, 3],\n", | |
| " padding: \"same\",\n", | |
| " activation: \"relu\",\n", | |
| " input_shape: [32, 32, 3]\n", | |
| " )\n", | |
| " )\n", | |
| " model.add(\n", | |
| " _conv2D(\n", | |
| " filters: 32,\n", | |
| " kernel_size: [3, 3],\n", | |
| " activation: \"relu\"\n", | |
| " )\n", | |
| " )\n", | |
| " model.add(\n", | |
| " _maxPool2D(\n", | |
| " pool_size: [2, 2],\n", | |
| " strides: [2, 2]\n", | |
| " )\n", | |
| " )\n", | |
| "\n", | |
| " // 2\n", | |
| " model.add(\n", | |
| " _conv2D(\n", | |
| " filters: 64,\n", | |
| " kernel_size: [3, 3],\n", | |
| " padding: \"same\",\n", | |
| " activation: \"relu\"\n", | |
| " )\n", | |
| " )\n", | |
| " model.add(\n", | |
| " _conv2D(\n", | |
| " filters: 64,\n", | |
| " kernel_size: [3, 3],\n", | |
| " activation: \"relu\"\n", | |
| " )\n", | |
| " )\n", | |
| " model.add(\n", | |
| " _maxPool2D(\n", | |
| " pool_size: [2, 2],\n", | |
| " strides: [2, 2]\n", | |
| " )\n", | |
| " )\n", | |
| "\n", | |
| " // then\n", | |
| " model.add(_flatten())\n", | |
| " model.add(_dense(512, activation: \"relu\", input_shape: [2304]))\n", | |
| " model.add(_dense(512, activation: \"relu\"))\n", | |
| " model.add(_dense(10, activation: \"softmax\"))\n", | |
| "\n", | |
| " // print summary\n", | |
| " print(model.summary())\n", | |
| "\n", | |
| " model.layers[0].set_weights([swift_model.conv1a.filter.makeNumpyArray(), swift_model.conv1a.bias.makeNumpyArray()])\n", | |
| " model.layers[1].set_weights([swift_model.conv1b.filter.makeNumpyArray(), swift_model.conv1b.bias.makeNumpyArray()])\n", | |
| "\n", | |
| " model.layers[3].set_weights([swift_model.conv2a.filter.makeNumpyArray(), swift_model.conv2a.bias.makeNumpyArray()])\n", | |
| " model.layers[4].set_weights([swift_model.conv2b.filter.makeNumpyArray(), swift_model.conv2b.bias.makeNumpyArray()])\n", | |
| "\n", | |
| " model.layers[7].set_weights([swift_model.dense1.weight.makeNumpyArray(), swift_model.dense1.bias.makeNumpyArray()])\n", | |
| " model.layers[8].set_weights([swift_model.dense2.weight.makeNumpyArray(), swift_model.dense2.bias.makeNumpyArray()])\n", | |
| " model.layers[9].set_weights([swift_model.output.weight.makeNumpyArray(), swift_model.output.bias.makeNumpyArray()])\n", | |
| "\n", | |
| " model.save(\"cifar.h5\")\n", | |
| "}" | |
| ], | |
| "execution_count": 0, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "metadata": { | |
| "id": "3uiYJKn1xfc6", | |
| "colab_type": "code", | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 887 | |
| }, | |
| "outputId": "1f84ec40-7aef-410e-a048-c705d08ac2ff" | |
| }, | |
| "source": [ | |
| "save_swift_kerash5()" | |
| ], | |
| "execution_count": 14, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "text": [ | |
| "Using TensorFlow backend.\n", | |
| "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:66: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n", | |
| "\n", | |
| "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:541: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n", | |
| "\n", | |
| "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:4432: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.\n", | |
| "\n", | |
| "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:4267: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.\n", | |
| "\n", | |
| "Model: \"sequential_1\"\n", | |
| "_________________________________________________________________\n", | |
| "Layer (type) Output Shape Param # \n", | |
| "=================================================================\n", | |
| "conv2d_1 (Conv2D) (None, 32, 32, 32) 896 \n", | |
| "_________________________________________________________________\n", | |
| "conv2d_2 (Conv2D) (None, 30, 30, 32) 9248 \n", | |
| "_________________________________________________________________\n", | |
| "max_pooling2d_1 (MaxPooling2 (None, 15, 15, 32) 0 \n", | |
| "_________________________________________________________________\n", | |
| "conv2d_3 (Conv2D) (None, 15, 15, 64) 18496 \n", | |
| "_________________________________________________________________\n", | |
| "conv2d_4 (Conv2D) (None, 13, 13, 64) 36928 \n", | |
| "_________________________________________________________________\n", | |
| "max_pooling2d_2 (MaxPooling2 (None, 6, 6, 64) 0 \n", | |
| "_________________________________________________________________\n", | |
| "flatten_1 (Flatten) (None, 2304) 0 \n", | |
| "_________________________________________________________________\n", | |
| "dense_1 (Dense) (None, 512) 1180160 \n", | |
| "_________________________________________________________________\n", | |
| "dense_2 (Dense) (None, 512) 262656 \n", | |
| "_________________________________________________________________\n", | |
| "dense_3 (Dense) (None, 10) 5130 \n", | |
| "=================================================================\n", | |
| "Total params: 1,513,514\n", | |
| "Trainable params: 1,513,514\n", | |
| "Non-trainable params: 0\n", | |
| "_________________________________________________________________\n", | |
| "None\n", | |
| "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:190: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.\n", | |
| "\n", | |
| "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:197: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.\n", | |
| "\n", | |
| "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:203: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.\n", | |
| "\n", | |
| "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:207: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead.\n", | |
| "\n", | |
| "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:216: The name tf.is_variable_initialized is deprecated. Please use tf.compat.v1.is_variable_initialized instead.\n", | |
| "\n", | |
| "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py:223: The name tf.variables_initializer is deprecated. Please use tf.compat.v1.variables_initializer instead.\n", | |
| "\n" | |
| ], | |
| "name": "stdout" | |
| } | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment