Skip to content

Commit d85ad26

Browse files
committed
release
1 parent 5afb267 commit d85ad26

35 files changed

+243
-135
lines changed

.idea/modules.xml

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
## 0.0.1
22

3-
* TODO: Describe initial release.
3+
* Initial Release

LICENSE

+93-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,93 @@
1-
TODO: Add your license here.
1+
pytorch_mobile
2+
3+
Copyright (c) 2020, Fynn Maarten Belderink
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions
8+
are met:
9+
10+
* Redistributions of source code must retain the above copyright
11+
notice, this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above
13+
copyright notice, this list of conditions and the following
14+
disclaimer in the documentation and/or other materials provided
15+
with the distribution.
16+
* Neither the name of Fynn Maarten Belderink nor the names of its
17+
contributors may be used to endorse or promote products derived
18+
from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
21+
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
22+
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23+
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25+
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27+
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30+
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
31+
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32+
SUCH DAMAGE.
33+
34+
--------------------------------------------------------------------------------
35+
path_provider
36+
37+
Copyright 2017, the Flutter project authors. All rights reserved.
38+
Redistribution and use in source and binary forms, with or without
39+
modification, are permitted provided that the following conditions are
40+
met:
41+
42+
* Redistributions of source code must retain the above copyright
43+
notice, this list of conditions and the following disclaimer.
44+
* Redistributions in binary form must reproduce the above
45+
copyright notice, this list of conditions and the following
46+
disclaimer in the documentation and/or other materials provided
47+
with the distribution.
48+
* Neither the name of Google Inc. nor the names of its
49+
contributors may be used to endorse or promote products derived
50+
from this software without specific prior written permission.
51+
52+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63+
64+
--------------------------------------------------------------------------------
65+
path
66+
67+
Copyright 2014, the Dart project authors. All rights reserved.
68+
Redistribution and use in source and binary forms, with or without
69+
modification, are permitted provided that the following conditions are
70+
met:
71+
72+
* Redistributions of source code must retain the above copyright
73+
notice, this list of conditions and the following disclaimer.
74+
* Redistributions in binary form must reproduce the above
75+
copyright notice, this list of conditions and the following
76+
disclaimer in the documentation and/or other materials provided
77+
with the distribution.
78+
* Neither the name of Google Inc. nor the names of its
79+
contributors may be used to endorse or promote products derived
80+
from this software without specific prior written permission.
81+
82+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
83+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
84+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
85+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
86+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
87+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
88+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
89+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
90+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
91+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
92+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
93+

README.md

+46-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,50 @@
1-
# torch_mobile
1+
# pytorch_mobile
22

3-
A new flutter plugin project.
3+
A flutter plugin for pytorch model inference.
4+
Since this is still being developed, the plugin is only supported for Android.
5+
An iOS version is going to come soon
46

5-
## Getting Started
7+
## Usage
68

7-
This project is a starting point for a Flutter
8-
[plug-in package](https://flutter.dev/developing-packages/),
9-
a specialized package that includes platform-specific implementation code for
10-
Android and/or iOS.
9+
### Installation
1110

12-
For help getting started with Flutter, view our
13-
[online documentation](https://flutter.dev/docs), which offers tutorials,
14-
samples, guidance on mobile development, and a full API reference.
11+
To use this plugin, add `pytorch_mobile` as a [dependency in your pubspec.yaml file](https://flutter.dev/docs/development/packages-and-plugins/using-packages).
12+
13+
Create a `assets` folder with your pytorch model and labels if needed. Modify `pubspec.yaml` accoringly.
14+
15+
```yaml
16+
assets:
17+
- assets/models/model.pt
18+
- assets/labels.csv
19+
```
20+
21+
Run `flutter pub get`
22+
23+
### Import the library
24+
25+
```dart
26+
import 'package:pytorch_mobile/pytorch_mobile.dart';
27+
```
28+
29+
### Load model
30+
31+
Either custom model:
32+
```dart
33+
Model customModel = await PyTorchMobile.loadModel(model: 'assets/models/custom_model.pt');
34+
```
35+
Or image model:
36+
```dart
37+
Model imageModel = await PyTorchMobile.loadModel(model: 'assets/models/resnet18.pt');
38+
```
39+
40+
### Get custom prediction
41+
42+
```dart
43+
List prediction = await customModel.getPrediction([1, 2, 3, 4], [1, 2, 2], DType.float32);
44+
```
45+
46+
### Get prediction for image
47+
48+
```dart
49+
String prediction = await _imageModel.getImagePrediction(image, 224, 224, "assets/labels/labels.csv");
50+
```

android/.project

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<projectDescription>
3-
<name>torch_mobile</name>
4-
<comment>Project torch_mobile created by Buildship.</comment>
3+
<name>pytorch_mobile</name>
4+
<comment>Project pytorch_mobile created by Buildship.</comment>
55
<projects>
66
</projects>
77
<buildSpec>

android/.settings/org.eclipse.buildship.core.prefs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ arguments=
22
auto.sync=false
33
build.scans.enabled=false
44
connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(6.3))
5-
connection.project.dir=../example/android
5+
connection.project.dir=
66
eclipse.preferences.version=1
77
gradle.user.home=
88
java.home=/usr/lib/jvm/java-11-openjdk-amd64

android/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
group 'io.fynn.torch_mobile'
1+
group 'io.fynn.pytorch_mobile'
22
version '1.0'
33

44
buildscript {

android/settings.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = 'torch_mobile'
1+
rootProject.name = 'pytorch_mobile'

android/src/main/AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.example.torch_mobile">
2+
package="com.example.pytorch_mobile">
33
</manifest>

android/src/main/java/io/fynn/torch_mobile/Convert.java renamed to android/src/main/java/io/fynn/pytorch_mobile/Convert.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.fynn.torch_mobile;
1+
package io.fynn.pytorch_mobile;
22

33
public class Convert {
44

android/src/main/java/io/fynn/torch_mobile/TorchMobilePlugin.java renamed to android/src/main/java/io/fynn/pytorch_mobile/PyTorchMobilePlugin.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.fynn.torch_mobile;
1+
package io.fynn.pytorch_mobile;
22

33
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
@@ -24,20 +24,20 @@
2424
import java.util.List;
2525

2626
/** TorchMobilePlugin */
27-
public class TorchMobilePlugin<T> implements FlutterPlugin, MethodCallHandler {
27+
public class PyTorchMobilePlugin implements FlutterPlugin, MethodCallHandler {
2828

2929
ArrayList<Module> modules = new ArrayList<>();
3030

3131
@Override
3232
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
3333
final MethodChannel channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(),
34-
"torch_mobile");
35-
channel.setMethodCallHandler(new TorchMobilePlugin());
34+
"pytorch_mobile");
35+
channel.setMethodCallHandler(new PyTorchMobilePlugin());
3636
}
3737

3838
public static void registerWith(Registrar registrar) {
39-
final MethodChannel channel = new MethodChannel(registrar.messenger(), "torch_mobile");
40-
channel.setMethodCallHandler(new TorchMobilePlugin());
39+
final MethodChannel channel = new MethodChannel(registrar.messenger(), "pytorch_mobile");
40+
channel.setMethodCallHandler(new PyTorchMobilePlugin());
4141
}
4242

4343
@Override
@@ -50,7 +50,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
5050
result.success(modules.size() - 1);
5151
} catch (Exception e) {
5252
String assetPath = call.argument("assetPath");
53-
Log.e("TorchMobile", assetPath + " is not a proper model", e);
53+
Log.e("PyTorchMobile", assetPath + " is not a proper model", e);
5454
}
5555
break;
5656
case "predict":
@@ -72,7 +72,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
7272
data = dataList.toArray(new Double[dataList.size()]);
7373

7474
}catch(Exception e){
75-
Log.e("TorchMobile", "error parsing arguments", e);
75+
Log.e("PyTorchMobile", "error parsing arguments", e);
7676
}
7777

7878
//prepare input tensor
@@ -83,7 +83,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
8383
try {
8484
outputTensor = module.forward(IValue.from(inputTensor)).toTensor();
8585
}catch(RuntimeException e){
86-
Log.e("TorchMobile", "Your input type " + dtype.toString().toLowerCase() + " (" + Convert.dtypeAsPrimitive(dtype.toString()) +") " + "does not match with model input type",e);
86+
Log.e("PyTorchMobile", "Your input type " + dtype.toString().toLowerCase() + " (" + Convert.dtypeAsPrimitive(dtype.toString()) +") " + "does not match with model input type",e);
8787
result.success(null);
8888
}
8989

@@ -106,7 +106,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
106106
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
107107

108108
}catch (Exception e){
109-
Log.e("TorchMobile", "error reading image", e);
109+
Log.e("PyTorchMobile", "error reading image", e);
110110
}
111111

112112
final Tensor imageInputTensor = TensorImageUtils.bitmapToFloat32Tensor(bitmap,

example/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# torch_mobile_example
1+
# pytorch_mobile_example
22

3-
Demonstrates how to use the torch_mobile plugin.
3+
Demonstrates how to use the pytorch_mobile plugin.
44

55
## Getting Started
66

example/android/app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ android {
3333

3434
defaultConfig {
3535
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
36-
applicationId "io.fynn.torch_mobile_example"
36+
applicationId "io.fynn.pytorch_mobile_example"
3737
minSdkVersion 21
3838
targetSdkVersion 28
3939
versionCode flutterVersionCode.toInteger()

example/android/app/src/debug/AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.example.torch_mobile_example">
2+
package="com.example.pytorch_mobile_example">
33
<!-- Flutter needs it to communicate with the running application
44
to allow setting breakpoints, to provide hot reload, etc.
55
-->

example/android/app/src/main/AndroidManifest.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.example.torch_mobile_example">
2+
package="com.example.pytorch_mobile_example">
33
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
44
calls FlutterMain.startInitialization(this); in its onCreate method.
55
In most cases you can leave this as-is, but you if you want to provide
66
additional functionality it is fine to subclass or reimplement
77
FlutterApplication and put your custom class here. -->
88
<application
99
android:name="io.flutter.app.FlutterApplication"
10-
android:label="torch_mobile_example"
10+
android:label="pytorch_mobile_example"
1111
android:icon="@mipmap/ic_launcher">
1212
<activity
1313
android:name=".MainActivity"

example/android/app/src/main/java/com/example/torch_mobile_example/MainActivity.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.torch_mobile_example;
1+
package com.example.pytorch_mobile_example;
22

33
import androidx.annotation.NonNull;
44
import io.flutter.embedding.android.FlutterActivity;

example/android/app/src/profile/AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.example.torch_mobile_example">
2+
package="com.example.pytorch_mobile_example">
33
<!-- Flutter needs it to communicate with the running application
44
to allow setting breakpoints, to provide hot reload, etc.
55
-->

example/ios/Runner.xcodeproj/project.pbxproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@
320320
"$(inherited)",
321321
"$(PROJECT_DIR)/Flutter",
322322
);
323-
PRODUCT_BUNDLE_IDENTIFIER = com.example.torchMobileExample;
323+
PRODUCT_BUNDLE_IDENTIFIER = com.example.pytorchMobileExample;
324324
PRODUCT_NAME = "$(TARGET_NAME)";
325325
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
326326
SWIFT_VERSION = 5.0;
@@ -454,7 +454,7 @@
454454
"$(inherited)",
455455
"$(PROJECT_DIR)/Flutter",
456456
);
457-
PRODUCT_BUNDLE_IDENTIFIER = com.example.torchMobileExample;
457+
PRODUCT_BUNDLE_IDENTIFIER = com.example.pytorchMobileExample;
458458
PRODUCT_NAME = "$(TARGET_NAME)";
459459
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
460460
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
@@ -481,7 +481,7 @@
481481
"$(inherited)",
482482
"$(PROJECT_DIR)/Flutter",
483483
);
484-
PRODUCT_BUNDLE_IDENTIFIER = com.example.torchMobileExample;
484+
PRODUCT_BUNDLE_IDENTIFIER = com.example.pytorchMobileExample;
485485
PRODUCT_NAME = "$(TARGET_NAME)";
486486
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
487487
SWIFT_VERSION = 5.0;

example/ios/Runner/Info.plist

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<key>CFBundleInfoDictionaryVersion</key>
1212
<string>6.0</string>
1313
<key>CFBundleName</key>
14-
<string>torch_mobile_example</string>
14+
<string>pytorch_mobile_example</string>
1515
<key>CFBundlePackageType</key>
1616
<string>APPL</string>
1717
<key>CFBundleShortVersionString</key>

0 commit comments

Comments
 (0)