Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ea454e8

Browse files
committedJun 4, 2023
adding rock paper scissor model
1 parent 8007672 commit ea454e8

File tree

7 files changed

+104
-4
lines changed

7 files changed

+104
-4
lines changed
 

‎Data/custom/1000/rock1.jpg

60.6 KB
Loading

‎MLMacosPerf.xcodeproj/project.pbxproj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
D02F52F82A2CC645002D734C /* Vision.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D02F52F72A2CC645002D734C /* Vision.framework */; };
11+
D02F52FC2A2CF928002D734C /* rock1.jpg in Resources */ = {isa = PBXBuildFile; fileRef = D02F52FB2A2CF928002D734C /* rock1.jpg */; };
12+
D02F52FE2A2D12EF002D734C /* rockpaperscissor_small_wpreview.mlpackage in Sources */ = {isa = PBXBuildFile; fileRef = D02F52FD2A2D12EF002D734C /* rockpaperscissor_small_wpreview.mlpackage */; };
1013
D06DA1C92978372200E64E61 /* chicken.jpg in Resources */ = {isa = PBXBuildFile; fileRef = D06DA1C82978372200E64E61 /* chicken.jpg */; };
1114
D0708F1D27446F3000B26B9B /* densenet121_keras_applications.mlmodel in Sources */ = {isa = PBXBuildFile; fileRef = D0C37BA1272856FE000EECA9 /* densenet121_keras_applications.mlmodel */; };
1215
D0C37B89272795BA000EECA9 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0C37B88272795BA000EECA9 /* AppDelegate.swift */; };
@@ -29,6 +32,9 @@
2932
/* End PBXBuildFile section */
3033

3134
/* Begin PBXFileReference section */
35+
D02F52F72A2CC645002D734C /* Vision.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Vision.framework; path = System/Library/Frameworks/Vision.framework; sourceTree = SDKROOT; };
36+
D02F52FB2A2CF928002D734C /* rock1.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; name = rock1.jpg; path = Data/custom/1000/rock1.jpg; sourceTree = "<group>"; };
37+
D02F52FD2A2D12EF002D734C /* rockpaperscissor_small_wpreview.mlpackage */ = {isa = PBXFileReference; lastKnownFileType = folder.mlpackage; name = rockpaperscissor_small_wpreview.mlpackage; path = Models/rockpaperscissor_small_wpreview.mlpackage; sourceTree = "<group>"; };
3238
D06DA1C82978372200E64E61 /* chicken.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; name = chicken.jpg; path = Data/custom/1000/chicken.jpg; sourceTree = "<group>"; };
3339
D0C37B85272795BA000EECA9 /* MLMacosPerf.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MLMacosPerf.app; sourceTree = BUILT_PRODUCTS_DIR; };
3440
D0C37B88272795BA000EECA9 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -58,6 +64,7 @@
5864
isa = PBXFrameworksBuildPhase;
5965
buildActionMask = 2147483647;
6066
files = (
67+
D02F52F82A2CC645002D734C /* Vision.framework in Frameworks */,
6168
D0C37B99272795D9000EECA9 /* CoreML.framework in Frameworks */,
6269
);
6370
runOnlyForDeploymentPostprocessing = 0;
@@ -68,9 +75,11 @@
6875
D0C37B7C272795BA000EECA9 = {
6976
isa = PBXGroup;
7077
children = (
71-
D06DA1C82978372200E64E61 /* chicken.jpg */,
78+
D02F52FD2A2D12EF002D734C /* rockpaperscissor_small_wpreview.mlpackage */,
7279
D0D43E99297829290018D9A9 /* ChickenDetector.mlpackage */,
7380
D0C37BB72728588D000EECA9 /* densenet121_keras_applications.mlmodel */,
81+
D06DA1C82978372200E64E61 /* chicken.jpg */,
82+
D02F52FB2A2CF928002D734C /* rock1.jpg */,
7483
D0C37BA327285839000EECA9 /* test01.jpg */,
7584
D0C37BA527285839000EECA9 /* test02.jpg */,
7685
D0C37BA727285839000EECA9 /* test03.jpg */,
@@ -112,6 +121,7 @@
112121
D0C37B97272795D9000EECA9 /* Frameworks */ = {
113122
isa = PBXGroup;
114123
children = (
124+
D02F52F72A2CC645002D734C /* Vision.framework */,
115125
D0C37B98272795D9000EECA9 /* CoreML.framework */,
116126
);
117127
name = Frameworks;
@@ -179,6 +189,7 @@
179189
D0C37BAE27285839000EECA9 /* test08.jpg in Resources */,
180190
D0C37BAF27285839000EECA9 /* test02.jpg in Resources */,
181191
D06DA1C92978372200E64E61 /* chicken.jpg in Resources */,
192+
D02F52FC2A2CF928002D734C /* rock1.jpg in Resources */,
182193
D0C37BB027285839000EECA9 /* test04.jpg in Resources */,
183194
D0C37BB127285839000EECA9 /* test03.jpg in Resources */,
184195
D0C37BB227285839000EECA9 /* test10.jpg in Resources */,
@@ -201,6 +212,7 @@
201212
D0708F1D27446F3000B26B9B /* densenet121_keras_applications.mlmodel in Sources */,
202213
D0D43E9A297829290018D9A9 /* ChickenDetector.mlpackage in Sources */,
203214
D0C37B8B272795BA000EECA9 /* ViewController.swift in Sources */,
215+
D02F52FE2A2D12EF002D734C /* rockpaperscissor_small_wpreview.mlpackage in Sources */,
204216
D0C37B9B2727A66D000EECA9 /* Predictor.swift in Sources */,
205217
D0C37B89272795BA000EECA9 /* AppDelegate.swift in Sources */,
206218
);

‎MLMacosPerf/Base.lproj/Main.storyboard

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="21507" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
2+
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="21701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
33
<dependencies>
44
<deployment identifier="macosx"/>
5-
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21507"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21701"/>
66
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
77
</dependencies>
88
<scenes>
@@ -731,6 +731,17 @@
731731
<action selector="startChickenTestAction:" target="XfG-lQ-9wD" id="wxF-zh-XdZ"/>
732732
</connections>
733733
</button>
734+
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="VLl-aM-iYR">
735+
<rect key="frame" x="162" y="268" width="177" height="32"/>
736+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
737+
<buttonCell key="cell" type="push" title="Start RockPaperScissor" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="ryB-Ke-JnV">
738+
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
739+
<font key="font" metaFont="system"/>
740+
</buttonCell>
741+
<connections>
742+
<action selector="startRPSTestAction:" target="XfG-lQ-9wD" id="TLH-nu-uOl"/>
743+
</connections>
744+
</button>
734745
</subviews>
735746
<constraints>
736747
<constraint firstItem="swQ-0C-0tw" firstAttribute="top" secondItem="m2S-Jp-Qdl" secondAttribute="top" constant="30" id="MkI-br-eZ1"/>

‎MLMacosPerf/ViewController.swift

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,49 @@ class ViewController: NSViewController {
101101
}
102102
}
103103

104+
@IBAction func startRPSTestAction(_ sender:NSButton) {
105+
sender.title = "Running";
106+
if resultSummary != nil {
107+
resultSummary?.removeFromSuperview();
108+
}
109+
DispatchQueue.main.async {
110+
var summary = "";
111+
for modelName in [
112+
"rockpaperscissor_small_wpreview"
113+
] {
114+
for device in ["ANE", "GPU", "CPU"] {
115+
// warmup
116+
_ = try! self.runRPSPredict(
117+
modelName: modelName,
118+
device: device,
119+
numIter: 100
120+
)
121+
// real test
122+
let latency = try! self.runRPSPredict(
123+
modelName: modelName,
124+
device: device,
125+
numIter: 4000
126+
)
127+
print(modelName)
128+
print("Latency \(device) : \(latency)")
129+
print("RPS \(device) : \(1 / latency)")
130+
print()
131+
summary += "\(modelName)\n"
132+
summary += "Latency \(device) : \(latency) \n"
133+
summary += "RPS \(device) : \(1/latency) \n\n"
134+
}
135+
sender.title = "Done!";
136+
}
137+
self.resultSummary = NSTextField(string: summary);
138+
self.view.addSubview(self.resultSummary!);
139+
self.resultSummary?.translatesAutoresizingMaskIntoConstraints = false;
140+
let align = NSLayoutConstraint(item: self.resultSummary!, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 1);
141+
let width = NSLayoutConstraint(item: self.resultSummary!, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 400);
142+
let height = NSLayoutConstraint(item: self.resultSummary!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 245);
143+
self.view.addConstraints([align, width, height]);
144+
}
145+
}
146+
104147
override func viewDidLoad() {
105148
super.viewDidLoad()
106149
// Do any additional setup after loading the view.
@@ -160,7 +203,7 @@ class ViewController: NSViewController {
160203
let model = try Predictor(contentsOf: modelName, configuration: mlconfig)
161204

162205
let imageFeatureValue = try MLFeatureValue (
163-
imageAt: url(forResource: "chicken", withExtension: "jpg"),
206+
imageAt: url(forResource: "rock1", withExtension: "jpg"),
164207
constraint: model.mlmodel.modelDescription.inputDescriptionsByName["imagePath"]!.imageConstraint!,
165208
options: [.cropAndScale: VNImageCropAndScaleOption.centerCrop.rawValue]
166209
)
@@ -176,6 +219,28 @@ class ViewController: NSViewController {
176219
return elapsed / Double(numIter)
177220
}
178221

222+
func runRPSPredict(modelName: String, device: String, numIter: Int = 100) throws -> Double {
223+
let mlconfig = configureDevice(device: device)
224+
225+
let model = try Predictor(contentsOf: modelName, configuration: mlconfig)
226+
227+
let imageFeatureValue = try MLFeatureValue (
228+
imageAt: url(forResource: "rock1", withExtension: "jpg"),
229+
constraint: model.mlmodel.modelDescription.inputDescriptionsByName["conv2d_input"]!.imageConstraint!,
230+
options: [.cropAndScale: VNImageCropAndScaleOption.centerCrop.rawValue]
231+
)
232+
let input = try MLDictionaryFeatureProvider(
233+
dictionary: ["conv2d_input": imageFeatureValue.imageBufferValue!])
234+
235+
let start = CACurrentMediaTime()
236+
for _ in 0..<numIter {
237+
_ = try! model.predict(input: input)
238+
}
239+
let end = CACurrentMediaTime()
240+
let elapsed = end - start
241+
return elapsed / Double(numIter)
242+
}
243+
179244
func runTrainBenchmark(modelName: String, device: String, withExtension: String) throws {
180245
let mlconfig = configureDevice(device: device)
181246

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"fileFormatVersion": "1.0.0",
3+
"itemInfoEntries": {
4+
"BFBC5DCC-8B88-46BC-A6FF-E565DF35C74A": {
5+
"author": "com.apple.CoreML",
6+
"description": "CoreML Model Specification",
7+
"name": "model.mlmodel",
8+
"path": "com.apple.CoreML/model.mlmodel"
9+
}
10+
},
11+
"rootModelIdentifier": "BFBC5DCC-8B88-46BC-A6FF-E565DF35C74A"
12+
}

0 commit comments

Comments
 (0)
Failed to load comments.