Skip to content

Commit

Permalink
Address tfjs-react-native typos in documentation strings (#8217)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaikwadrahul8 authored Apr 12, 2024
1 parent c027d6a commit 63250ec
Show file tree
Hide file tree
Showing 7 changed files with 840 additions and 751 deletions.
13 changes: 7 additions & 6 deletions tfjs-react-native/DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# Development

This file will document some of the differences from the regular developement workflow in [DEVELOPMENT.md](../DEVELOPMENT.md). You should read that document first to get familiar with typical TensorFlow.js development workflow.
This file will document some of the differences from the regular development workflow in [DEVELOPMENT.md](../DEVELOPMENT.md). You should read that document first to get familiar with typical TensorFlow.js development workflow.

Development and testing for tfjs-react-native is somewhat different from the packages like tfjs-core or tfjs-layers for a few reasons:
- __Dependency on having a physical mobile device to run__: While the CPU backend can run in a simulator, the WebGL one requires running on a physical device. So most of the time you will want to test something using a mobile device connected to your computer.
- __No browser or node environment__: We are running JavaScript outside of a browser and outside of node. We thus have to make sure we don't include things that depend on those two environments.

- **Dependency on having a physical mobile device to run**: While the CPU backend can run in a simulator, the WebGL one requires running on a physical device. So most of the time you will want to test something using a mobile device connected to your computer.
- **No browser or node environment**: We are running JavaScript outside of a browser and outside of node. We thus have to make sure we don't include things that depend on those two environments.

## Key Terms & Caveats

These are a few key terms/technologies to be familiar with that are different from what we use for web or node.js development.

- [React Native](https://facebook.github.io/react-native/) — This is the framework that this package targets.
- [Metro](https://facebook.github.io/metro/) — This is the bundler used to create the JavaScript bundle that is loaded into the native app by react native.
- The bundle needs to be created at 'compile time' thus all imports/requires need to be resolved. Thus _dynamic_ `import`s/`require`s are __statically resolved__. So you cannot exclude a require with a conditional in JS code.
- The bundle needs to be created at 'compile time' thus all imports/requires need to be resolved. Thus _dynamic_ `import`s/`require`s are **statically resolved**. So you cannot exclude a require with a conditional in JS code.
- Since tfjs does dynamic `require`'s of certain node libraries that are not present in react native, files that do that need to be excluded from the metro build process. For end users, this is documented in the [README](../README.md), but it also happens in `integration_rn59/prep_tests.ts`.
- Metro does not play well with symlinks, so if you are trying to develop against a local build of tfjs, copy the dist folder into the app's node_modules as appropriate. Do not use yalc.
- [.ipa](https://en.wikipedia.org/wiki/.ipa) & [.apk](https://en.wikipedia.org/wiki/Android_application_package) — These are the formats for the final native bundle that is put on an iOS and Android device. They are created by their respective dev tools, [XCode](https://developer.apple.com/xcode/) and [Android Studio](https://developer.android.com/studio).
Expand All @@ -33,8 +33,9 @@ Unit tests from tfjs-core are imported into a react native application and run a
Because these are part of an app to run them you must compile and run the integration_rn59 of the target device. There is a button in that app to start the unit tests.

This is _automated in CI_ and runs on:
- Changes to tfjs-core: [Tests will be run against HEAD of tfjs-core](../tfjs-core/cloudbuild.yml)
- Changes to tfjs-react-native: [Tests will be run against the **published** version](./cloudbuild.yml) of tfjs on npm that is references in `integration_rn59/package.json`

- Changes to tfjs-core: [Tests will be run against HEAD of tfjs-core](../tfjs-core/cloudbuild.yml)
- Changes to tfjs-react-native: [Tests will be run against the **published** version](./cloudbuild.yml) of tfjs on npm that is references in `integration_rn59/package.json`

### Other integration tests

Expand Down
69 changes: 38 additions & 31 deletions tfjs-react-native/integration_rn59/components/ml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
* =============================================================================
*/

import * as mobilenet from '@tensorflow-models/mobilenet';
import * as tf from '@tensorflow/tfjs';
import {asyncStorageIO, bundleResourceIO} from '@tensorflow/tfjs-react-native';
import * as mobilenet from "@tensorflow-models/mobilenet";
import * as tf from "@tensorflow/tfjs";
import {
asyncStorageIO,
bundleResourceIO,
} from "@tensorflow/tfjs-react-native";

// All functions (i.e. 'runners") in this file are async
// functions that return a function that can be invoked to
Expand Down Expand Up @@ -64,11 +67,12 @@ export async function mobilenetRunner() {
* A runner that loads a model bundled with the app and runs a prediction
* through it.
*/
const modelJson = require('../assets/model/bundle_model_test.json');
const modelWeights = require('../assets/model/bundle_model_test_weights.bin');
const modelJson = require("../assets/model/bundle_model_test.json");
const modelWeights = require("../assets/model/bundle_model_test_weights.bin");
export async function localModelRunner() {
const model =
await tf.loadLayersModel(bundleResourceIO(modelJson, modelWeights));
const model = await tf.loadLayersModel(
bundleResourceIO(modelJson, modelWeights)
);

return async () => {
const res = model.predict(tf.randomNormal([1, 10])) as tf.Tensor;
Expand All @@ -81,11 +85,12 @@ export async function localModelRunner() {
* A runner that loads a model bundled with the app and runs a prediction
* through it.
*/
const modelJson2 = require('../assets/graph_model/model.json');
const modelWeights2 = require('../assets/graph_model/group1-shard1of1.bin');
const modelJson2 = require("../assets/graph_model/model.json");
const modelWeights2 = require("../assets/graph_model/group1-shard1of1.bin");
export async function localGraphModelRunner() {
const model =
await tf.loadGraphModel(bundleResourceIO(modelJson2, modelWeights2));
const model = await tf.loadGraphModel(
bundleResourceIO(modelJson2, modelWeights2)
);
return async () => {
const res = model.predict(tf.randomNormal([1, 10])) as tf.Tensor;
const data = await res.data();
Expand All @@ -97,43 +102,45 @@ export async function localGraphModelRunner() {
* A runner that loads a sharded model bundled with the app and runs a
* prediction through it.
*/
const shardedModelJson = require('../assets/sharded_model/model.json');
const shardedModelWeights1: number =
require('../assets/sharded_model/group1-shard1of2.bin');
const shardedModelWeights2: number =
require('../assets/sharded_model/group1-shard2of2.bin');
const shardedModelJson = require("../assets/sharded_model/model.json");
const shardedModelWeights1: number = require("../assets/sharded_model/group1-shard1of2.bin");
const shardedModelWeights2: number = require("../assets/sharded_model/group1-shard2of2.bin");

export async function localShardedGraphModelRunner() {
const model = await tf.loadGraphModel(bundleResourceIO(
shardedModelJson, [shardedModelWeights1, shardedModelWeights2]));
const model = await tf.loadGraphModel(
bundleResourceIO(shardedModelJson, [
shardedModelWeights1,
shardedModelWeights2,
])
);

return async () => {
const input = tf.zeros([1, 224, 224, 3]);
const res = model.predict(input) as tf.Tensor;
const data = await res.data();
return JSON.stringify({predictionsLength: data.length});
return JSON.stringify({ predictionsLength: data.length });
};
}

/**
* A runner that traines a model.
* A runner that trains a model.
*/
export async function trainModelRunner() {
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 5, inputShape: [1]}));
model.add(tf.layers.dense({units: 1}));
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
model.add(tf.layers.dense({ units: 5, inputShape: [1] }));
model.add(tf.layers.dense({ units: 1 }));
model.compile({ loss: "meanSquaredError", optimizer: "sgd" });

// Generate some synthetic data for training.
const xs = tf.tensor2d([1, 2, 3, 4], [4, 1]);
const ys = tf.tensor2d([1, 3, 5, 7], [4, 1]);

return async () => {
// Train the model using the data.
await model.fit(xs, ys, {epochs: 20});
await model.fit(xs, ys, { epochs: 20 });

return 'done';
return "done";
};
}

Expand All @@ -143,14 +150,14 @@ export async function trainModelRunner() {
export async function saveModelRunner() {
// Define a model for linear regression.
const model = tf.sequential();
model.add(tf.layers.dense({units: 5, inputShape: [1]}));
model.add(tf.layers.dense({units: 1}));
model.compile({loss: 'meanSquaredError', optimizer: 'sgd'});
model.add(tf.layers.dense({ units: 5, inputShape: [1] }));
model.add(tf.layers.dense({ units: 1 }));
model.compile({ loss: "meanSquaredError", optimizer: "sgd" });

return async () => {
await model.save(asyncStorageIO('custom-model-test'));
await tf.loadLayersModel(asyncStorageIO('custom-model-test'));
await model.save(asyncStorageIO("custom-model-test"));
await tf.loadLayersModel(asyncStorageIO("custom-model-test"));

return 'done';
return "done";
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class TestRunner extends Component<TestRunnerProps, TestRunnerState> {
const reactReporter: jasmine.CustomReporter = {
jasmineStarted: suiteInfo => {
// The console.warn below seems necessary in order for the spy on
// console.warn defined in one of the tests to run corrently.
// console.warn defined in one of the tests to run currently.
console.warn('starting tests');
//@ts-ignore
console.reportErrorsAsExceptions = false;
Expand Down
Loading

0 comments on commit 63250ec

Please sign in to comment.