Skip to content

Commit 1c73094

Browse files
Merge pull request #19 from raghavmecheri/normalise-transform
Added the Normalize transform + docs update + name change
2 parents 583e2fc + 00faa30 commit 1c73094

File tree

21 files changed

+213
-50
lines changed

21 files changed

+213
-50
lines changed

.github/PULL_REQUEST_TEMPLATE/Bugfix.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ please include a screenshot showing the fix. -->
2323

2424
<!--- If you have any questions, please reach out! We are here to help. -->
2525

26-
- [ ] I have read the [`CONTRIBUTING.md`](https://github.com/raghavmecheri/ptjs/blob/master/CONTRIBUTING.md) and followed its [Guidelines](https://github.com/raghavmecheri/ptjs/blob/master/CONTRIBUTING.md#guidelines)
26+
- [ ] I have read the [`CONTRIBUTING.md`](https://github.com/raghavmecheri/pytorchjs/blob/master/CONTRIBUTING.md) and followed its [Guidelines](https://github.com/raghavmecheri/pytorchjs/blob/master/CONTRIBUTING.md#guidelines)
2727
- [ ] I have linted my code locally, following the project's code style
2828
- [ ] I have updated the product documentation and ensured that the changes are reflected

.github/PULL_REQUEST_TEMPLATE/Documentation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ about: Improves documentation for the API or on the docs website.
2020

2121
<!--- If you have any questions, please reach out! We're here to help. -->
2222

23-
- [ ] I have read the [`CONTRIBUTING.md`](https://github.com/raghavmecheri/ptjs/blob/master/CONTRIBUTING.md) and followed its [Guidelines](https://github.com/raghavmecheri/ptjs/blob/master/CONTRIBUTING.md#guidelines)
23+
- [ ] I have read the [`CONTRIBUTING.md`](https://github.com/raghavmecheri/pytorchjs/blob/master/CONTRIBUTING.md) and followed its [Guidelines](https://github.com/raghavmecheri/pytorchjs/blob/master/CONTRIBUTING.md#guidelines)

.github/PULL_REQUEST_TEMPLATE/Feature.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
name: New Feature
3-
about: Adds a new feature to ptjs
3+
about: Adds a new feature to pytorchjs
44

55
---
66

@@ -43,6 +43,6 @@ comment. --->
4343

4444
<!--- If you have any questions, please reach out! We are here to help. -->
4545

46-
- [ ] I have read the [`CONTRIBUTING.md`](https://github.com/raghavmecheri/ptjs/blob/master/CONTRIBUTING.md) and followed its [Guidelines](https://github.com/raghavmecheri/ptjs/blob/master/CONTRIBUTING.md#guidelines)
46+
- [ ] I have read the [`CONTRIBUTING.md`](https://github.com/raghavmecheri/pytorchjs/blob/master/CONTRIBUTING.md) and followed its [Guidelines](https://github.com/raghavmecheri/pytorchjs/blob/master/CONTRIBUTING.md#guidelines)
4747
- [ ] I have linted my code locally, following the project's code style
4848
- [ ] I have tested my changes locally, and all tests pass

CONTRIBUTING.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Contributing
22

3-
Thank you for your interest in contributing to ptjs!
3+
Thank you for your interest in contributing to pytorchjs!
44

5-
We invite you to participate in our community by adding and commenting on [issues](https://github.com/raghavmecheri/ptjs/issues) (e.g., bug reports; new feature suggestions) or contributing code enhancements through a pull request.
5+
We invite you to participate in our community by adding and commenting on [issues](https://github.com/raghavmecheri/pytorchjs/issues) (e.g., bug reports; new feature suggestions) or contributing code enhancements through a pull request.
66

7-
If you have any general questions about contributing to ptjs, please feel free to email [Raghav](raghav.mecheri@columbia.edu), or just open an issue on [Github](https://github.com/raghavmecheri/ptjs/issues/new).
7+
If you have any general questions about contributing to pytorchjs, please feel free to email [Raghav](raghav.mecheri@columbia.edu), or just open an issue on [Github](https://github.com/raghavmecheri/pytorchjs/issues/new).
88
## Guidelines
99

10-
When submitting PRs to ptjs, please respect the following general
10+
When submitting PRs to pytorchjs, please respect the following general
1111
coding guidelines:
1212

1313
* All PRs should be accompanied by an appropriate label as per [lerna-changelog](https://github.com/lerna/lerna-changelog), and reference any issue they resolve.

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div align="center">
22

3-
<img src="https://raw.githubusercontent.com/raghavmecheri/ptjs/master/assets/img/ptjs.png" width="300px">
3+
<img src="https://raw.githubusercontent.com/raghavmecheri/pytorchjs/master/assets/img/ptjs.png" width="300px">
44

55

66
**Torch and TorchVision, for your Node servers.
@@ -39,11 +39,12 @@ const { load } = torch;
3939
const { DataLoader } = torch.utils.data;
4040
const { ImageFolder } = torchvision.datasets;
4141

42-
const { Compose, Resize, InvertAxes } = torchvision.transforms;
42+
const { Compose, Resize, InvertAxes, Normalize } = torchvision.transforms;
4343

4444
const squeezeNet = load("./test/resources/squeezenet_ts.pt");
4545
const transforms = new Compose([
4646
new Resize({height: 224, width: 224}),
47+
new Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
4748
new InvertAxes()
4849
]);
4950

@@ -52,7 +53,7 @@ const results = await squeezeNet(loader);
5253
```
5354

5455
### More Examples
55-
Additional examples of both setup and usage involving features like Torchvision Transforms and CUDA (in development) may be found [here](https://github.com/raghavmecheri/ptjs/tree/master/examples).
56+
Additional examples of both setup and usage involving features like Torchvision Transforms and CUDA (in development) may be found [here](https://github.com/raghavmecheri/pytorchjs/tree/master/examples/Usage).
5657

5758
## Key Features
5859
* Run your PyTorch models in a Javascript environment, without worrying about setting up Torchscript or downloading custom binaries

examples/Exporting/Exporting.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Exporting your PyTorch model for use with ptjs
2-
ptjs currently only supports models that are in TorchScript, which is an intermediate representation of a PYTorch model that can be run in other high performance environments like C++ and (thanks to this library!) JavaScript.<br>
1+
# Exporting your PyTorch model for use with pytorchjs
2+
pytorchjs currently only supports models that are in TorchScript, which is an intermediate representation of a PYTorch model that can be run in other high performance environments like C++ and (thanks to this library!) JavaScript.<br>
33
However, any PyTorch model can be saved as a TorchScript module. An example of the same may be found below.
44
```python
55
import torch

examples/Usage/Basic.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# Using ptjs to run your torchscript models in Node
1+
# Using pytorchjs to run your torchscript models in Node
22

33
## Background
4-
This example assumes that you have a pre-trained torch model, that has been exported to [torchscript](https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html), as well as an image dataset conforming to the structure that the ptjs DatasetFolder class expects. The structure may also be found below, for reference, and a sample script to export a torch model to torchscript can be found [here](../Exporting/Exporting.md)
4+
This example assumes that you have a pre-trained torch model, that has been exported to [torchscript](https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html), as well as an image dataset conforming to the structure that the pytorchjs DatasetFolder class expects. The structure may also be found below, for reference, and a sample script to export a torch model to torchscript can be found [here](../Exporting/Exporting.md)
55

66
## Required File Structure
77
```
@@ -22,7 +22,7 @@ import { torch, torchvision } from 'pytorchjs';
2222
const { load } = torch;
2323
const { DataLoader } = torch.utils.data;
2424
const { ImageFolder } = torchvision.datasets;
25-
const { Compose, Resize, InvertAxes } = torchvision.transforms;
25+
const { Compose, Resize, InvertAxes, Normalize } = torchvision.transforms;
2626
```
2727

2828
### Load Model
@@ -31,10 +31,11 @@ const squeezeNet = load("squeezenet_ts.pt");
3131
```
3232

3333
### Declare torchvision transforms
34-
Note: The images are resized since the [torchvision Squeezenet model](https://pytorch.org/hub/pytorch_vision_squeezenet/) requires an input of size 224x224
34+
Note: The images are resized and normalized since the [torchvision Squeezenet model](https://pytorch.org/hub/pytorch_vision_squeezenet/) requires a normalized input of size 224x224
3535
```js
3636
const transforms = new Compose([
3737
new Resize({height: 224, width: 224}),
38+
new Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
3839
new InvertAxes()
3940
]);
4041
```

examples/Usage/DataLoader.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The pytorchjs DataLoader class
2+
3+
## Background
4+
The DataLoader class allows you to lazy-load data just as you need to pass it to your model for inference. It lets you customise a couple of things:
5+
- The ImageFolder class that you're passing in
6+
- Your batch size
7+
- The transforms that you would like to apply on every image you load

examples/Usage/ImageFolder.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# The pytorchjs ImageFolder class
2+
3+
## Background
4+
The ImageFolder class is an encapsulation of an image (vision) dataset. It lets you do a couple of cool things, including:
5+
- Specify a custom `loader` function to load the image paths passed in. The default for this is a function that loads the image using `numjs`, which is what all our transforms expect :)
6+
- A transform object to be applied per image. This may be overriden if you pass this object into a DataLoader with it's own transform
7+
- `extensions`, a list of acceptable file extensions to load data from. The default array considered is: `[".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff"]`
8+
- An `isValidFile` method to check if a file at a given path should be loaded or not.
9+
10+
## Selective Loading
11+
Both `extensions` and the `isValidFile` function may be used to selectively load images from your dataset. Only one of these two parameters may be non-null. A sample isValidFile method that only selects .png images has been specified below.
12+
```js
13+
const customIsValidFile = (targetPath) => targetPath.endsWith(".png")
14+
```

examples/Usage/Transforms.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Using a custom pytorchjs transform in your workflow
2+
3+
## Background
4+
This example assumes that you have a pre-trained torch model, that has been exported to [torchscript](https://pytorch.org/tutorials/beginner/Intro_to_TorchScript_tutorial.html), as well as an image dataset conforming to the structure that the pytorchjs DatasetFolder class expects. The structure may also be found below, for reference, and a sample script to export a torch model to torchscript can be found [here](../Exporting/Exporting.md)
5+
6+
## Required File Structure
7+
```
8+
root/class_x/xxx.ext
9+
root/class_x/xxy.ext
10+
root/class_x/xxz.ext
11+
12+
root/class_y/123.ext
13+
root/class_y/nsdf3.ext
14+
root/class_y/asd932_.ext
15+
```
16+
17+
## Use Case
18+
To add a custom transform function to your `transforms.Compose` call
19+
20+
## Sample Script
21+
### Import Dependancies
22+
```js
23+
import { torch, torchvision } from 'pytorchjs';
24+
25+
const { load } = torch;
26+
const { DataLoader } = torch.utils.data;
27+
const { ImageFolder } = torchvision.datasets;
28+
const { Compose, Resize, InvertAxes, Normalize } = torchvision.transforms;
29+
```
30+
31+
### Load Model
32+
```js
33+
const mymodel = load("mymodel.pt");
34+
```
35+
36+
### Declare a custom transform
37+
Custom transform classes must conform to the following requirements. A sample may be found below:
38+
- Be callable. [This](https://hackernoon.com/creating-callable-objects-in-javascript-d21l3te1) is a good starting point, I prefer to just extend the Javascript `Function` interface, like how I've done in [`transforms.js`](https://github.com/raghavmecheri/pytorchjs/blob/master/src/torchvision/transforms/transforms.js)
39+
- Return a `numjs` object
40+
```js
41+
export class RandomAdd extends Function {
42+
/**
43+
* Create a new callable RandomAdd object
44+
*/
45+
constructor() {
46+
super();
47+
// NOTE: This is just some template code I found at the linked Hackernoon page to make the function callable :)
48+
return new Proxy(this, {
49+
apply: (_target, _thisArg, argumentsList) => {
50+
const x = argumentsList[0];
51+
const output = this.__call__(x);
52+
return output;
53+
},
54+
});
55+
}
56+
57+
// All I'm doing here is adding a random integer to every value, because why not
58+
__call__ = (x) => x.add(Math.floor(Math.random() * 10));
59+
}
60+
```
61+
62+
### Implement this custom transform
63+
```js
64+
const transforms = new Compose([
65+
new Resize({height: 224, width: 224}),
66+
new RandomAdd(),
67+
new InvertAxes()
68+
]);
69+
```
70+
71+
### Load data and get inference results
72+
```js
73+
const loader = new DataLoader(new ImageFolder("dataset"), 1, transforms);
74+
const results = await mymodel(loader);
75+
```

0 commit comments

Comments
 (0)