Skip to content

Commit

Permalink
MPSCNNHelloWorld: Version 1.1, 2016-11-17
Browse files Browse the repository at this point in the history
Fixes a typo that affected performance

This sample is a port of the open source library, TensorFlow trained networks trained on MNIST Dataset (http://yann.lecun.com/exdb/mnist/) via inference using Metal Performance Shaders. The sample demonstrates how to encode different layers to the GPU and perform image recognition using trained parameters(weights and bias) that have been fetched from, pre-trained and saved network on TensorFlow.
  • Loading branch information
Lax committed Dec 24, 2016
1 parent 95df298 commit bc16dc0
Show file tree
Hide file tree
Showing 45 changed files with 1,706 additions and 0 deletions.
42 changes: 42 additions & 0 deletions MPSCNNHelloWorld/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Sample code project: MPSCNNHelloWorld: Simple Digit Detection Convolution Neural Networks (CNN)
Version: 1.1

IMPORTANT: This Apple software is supplied to you by Apple
Inc. ("Apple") in consideration of your agreement to the following
terms, and your use, installation, modification or redistribution of
this Apple software constitutes acceptance of these terms. If you do
not agree with these terms, please do not use, install, modify or
redistribute this Apple software.

In consideration of your agreement to abide by the following terms, and
subject to these terms, Apple grants you a personal, non-exclusive
license, under Apple's copyrights in this original Apple software (the
"Apple Software"), to use, reproduce, modify and redistribute the Apple
Software, with or without modifications, in source and/or binary forms;
provided that if you redistribute the Apple Software in its entirety and
without modifications, you must retain this notice and the following
text and disclaimers in all such redistributions of the Apple Software.
Neither the name, trademarks, service marks or logos of Apple Inc. may
be used to endorse or promote products derived from the Apple Software
without specific prior written permission from Apple. Except as
expressly stated in this notice, no other rights or licenses, express or
implied, are granted by Apple herein, including but not limited to any
patent rights that may be infringed by your derivative works or by other
works in which the Apple Software may be incorporated.

The Apple Software is provided by Apple on an "AS IS" basis. APPLE
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.

IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

Copyright (C) 2016 Apple Inc. All Rights Reserved.
447 changes: 447 additions & 0 deletions MPSCNNHelloWorld/MPSCNNHelloWorld.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions MPSCNNHelloWorld/MPSCNNHelloWorld/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information

Abstract:
Application delegate for the App
*/

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"images" : [
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "mnist-main-58.png",
"scale" : "2x"
},
{
"size" : "29x29",
"idiom" : "iphone",
"filename" : "mnist-main-87.png",
"scale" : "3x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "mnist-main-80.png",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "iphone",
"filename" : "mnist-main-120.png",
"scale" : "3x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "mnist-main-120-2.png",
"scale" : "2x"
},
{
"size" : "60x60",
"idiom" : "iphone",
"filename" : "mnist-main-180.png",
"scale" : "3x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "mnist-main-29.png",
"scale" : "1x"
},
{
"size" : "29x29",
"idiom" : "ipad",
"filename" : "mnist-main-58-2.png",
"scale" : "2x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "mnist-main-40.png",
"scale" : "1x"
},
{
"size" : "40x40",
"idiom" : "ipad",
"filename" : "mnist-main-80-2.png",
"scale" : "2x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "mnist-main-76.png",
"scale" : "1x"
},
{
"size" : "76x76",
"idiom" : "ipad",
"filename" : "mnist-main-152.png",
"scale" : "2x"
},
{
"size" : "83.5x83.5",
"idiom" : "ipad",
"filename" : "mnist-main-167.png",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11019" systemVersion="16A186" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11056.3"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>
96 changes: 96 additions & 0 deletions MPSCNNHelloWorld/MPSCNNHelloWorld/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11185.2" systemVersion="16A254d" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11151.3"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="Digit_Detector" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<label hidden="YES" opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Accuracy = " textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Itt-4A-FVR">
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fMB-WM-8YO">
<fontDescription key="fontDescription" type="system" pointSize="26"/>
<state key="normal" title="Use MNIST Test Set"/>
<connections>
<action selector="tappedTestSet:" destination="BYZ-38-t0r" eventType="touchUpInside" id="iwN-td-1sk"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="HEd-C6-X5l">
<fontDescription key="fontDescription" type="system" pointSize="26"/>
<state key="normal" title="Detect Digit"/>
<connections>
<action selector="tappedDetectDigit:" destination="BYZ-38-t0r" eventType="touchUpInside" id="Rtq-vr-h56"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="NsO-ek-NfA">
<fontDescription key="fontDescription" type="system" pointSize="25"/>
<state key="normal" title="Clear"/>
<connections>
<action selector="tappedClear:" destination="BYZ-38-t0r" eventType="touchUpInside" id="aCb-Qa-Y9q"/>
</connections>
</button>
<label hidden="YES" opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="0" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Aa3-61-EIv">
<fontDescription key="fontDescription" type="system" pointSize="23"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="PzX-rP-SKt" customClass="DrawView" customModule="Digit_Detector" customModuleProvider="target">
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="240" id="cpN-BA-HfR"/>
<constraint firstAttribute="width" constant="240" id="zZB-ag-7Eh"/>
</constraints>
</view>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="lv9-QR-RxB">
<fontDescription key="fontDescription" type="system" pointSize="26"/>
<state key="normal" title="Use Deep Net"/>
<connections>
<action selector="tappedDeepButton:" destination="BYZ-38-t0r" eventType="touchUpInside" id="wAp-88-Gdp"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="PzX-rP-SKt" firstAttribute="top" secondItem="NsO-ek-NfA" secondAttribute="bottom" constant="6" id="5Br-ID-zhu"/>
<constraint firstItem="lv9-QR-RxB" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="9CN-JK-3QO"/>
<constraint firstItem="Itt-4A-FVR" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="BPl-aC-Jiq"/>
<constraint firstItem="PzX-rP-SKt" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="LZI-YF-JxH"/>
<constraint firstItem="HEd-C6-X5l" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="P7g-P9-3IL"/>
<constraint firstItem="Aa3-61-EIv" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="Q2L-ju-FT5"/>
<constraint firstItem="NsO-ek-NfA" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="Q6a-Xz-gCB"/>
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="lv9-QR-RxB" secondAttribute="bottom" constant="8" id="RDs-tb-ujw"/>
<constraint firstItem="fMB-WM-8YO" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="T3b-5e-9x9"/>
<constraint firstItem="HEd-C6-X5l" firstAttribute="top" secondItem="PzX-rP-SKt" secondAttribute="bottom" constant="23" id="XZb-1B-Gug"/>
<constraint firstItem="PzX-rP-SKt" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="YHc-FZ-FDx"/>
<constraint firstItem="Aa3-61-EIv" firstAttribute="top" secondItem="HEd-C6-X5l" secondAttribute="bottom" constant="24" id="kKx-jg-CxZ"/>
<constraint firstItem="fMB-WM-8YO" firstAttribute="top" secondItem="y3c-jy-aDJ" secondAttribute="bottom" id="pRd-8b-Hib"/>
<constraint firstItem="Itt-4A-FVR" firstAttribute="top" secondItem="fMB-WM-8YO" secondAttribute="bottom" constant="24" id="zhT-JQ-n6p"/>
</constraints>
</view>
<connections>
<outlet property="DigitView" destination="PzX-rP-SKt" id="bZU-8T-7ZI"/>
<outlet property="accuracyLabel" destination="Itt-4A-FVR" id="REI-58-rzY"/>
<outlet property="predictionLabel" destination="Aa3-61-EIv" id="OkV-W4-Clr"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="-3427.734375" y="430.89311859443626"/>
</scene>
</scenes>
</document>
93 changes: 93 additions & 0 deletions MPSCNNHelloWorld/MPSCNNHelloWorld/DrawView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information

Abstract:
This file has routines for drwaing and detecting user touches (input digit)
*/

import UIKit

/**
This class is used to handle the drawing in the DigitView so we can get user input digit,
This class doesn't really have an MPS or Metal going in it, it is just used to get user input
*/
class DrawView: UIView {

// some parameters of how thick a line to draw 15 seems to work
// and we have white drawings on black background just like MNIST needs its input
var linewidth = CGFloat(15) { didSet { setNeedsDisplay() } }
var color = UIColor.white { didSet { setNeedsDisplay() } }

// we will keep touches made by user in view in these as a record so we can draw them.
var lines: [Line] = []
var lastPoint: CGPoint!

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
lastPoint = touches.first!.location(in: self)
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
let newPoint = touches.first!.location(in: self)
// keep all lines drawn by user as touch in record so we can draw them in view
lines.append(Line(start: lastPoint, end: newPoint))
lastPoint = newPoint
// make a draw call
setNeedsDisplay()
}

override func draw(_ rect: CGRect) {
super.draw(rect)

let drawPath = UIBezierPath()
drawPath.lineCapStyle = .round

for line in lines{
drawPath.move(to: line.start)
drawPath.addLine(to: line.end)
}

drawPath.lineWidth = linewidth
color.set()
drawPath.stroke()
}


/**
This function gets the pixel data of the view so we can put it in MTLTexture

- Returns:
Void
*/
func getViewContext() -> CGContext? {
// our network takes in only grayscale images as input
let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceGray()

// we have 3 channels no alpha value put in the network
let bitmapInfo = CGImageAlphaInfo.none.rawValue

// this is where our view pixel data will go in once we make the render call
let context = CGContext(data: nil, width: 28, height: 28, bitsPerComponent: 8, bytesPerRow: 28, space: colorSpace, bitmapInfo: bitmapInfo)

// scale and translate so we have the full digit and in MNIST standard size 28x28
context!.translateBy(x: 0 , y: 28)
context!.scaleBy(x: 28/self.frame.size.width, y: -28/self.frame.size.height)

// put view pixel data in context
self.layer.render(in: context!)

return context
}
}

/**
2 points can give a line and this class is just for that purpose, it keeps a record of a line
*/
class Line{
var start, end: CGPoint

init(start: CGPoint, end: CGPoint) {
self.start = start
self.end = end
}
}
Loading

0 comments on commit bc16dc0

Please sign in to comment.