Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #363 from flojoy-ai/update-node-docs-234
Browse files Browse the repository at this point in the history
[WIP] TORCHSCRIPT_CLASSIFIER node for any user-provided .torchscript AI model - nodes PR:234
  • Loading branch information
Roulbac authored Sep 8, 2023
2 parents e14b687 + 49f867f commit 96e4756
Show file tree
Hide file tree
Showing 45 changed files with 2,812 additions and 577 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
The ONE_HOT_ENCODING node creates a one hot encoding from a dataframe containing categorical features.

The ONE_HOT_ENCODING node creates a one hot encoding from a dataframe and columns dataframe containing categorical features.
Inputs
------
data : DataFrame
The input dataframe containing the categorical features.

Returns
-------
DataFrame
The one hot encoding of the input features.
Parameters
----------
feature_col: DataFrame, optional
A dataframe whose columns are used to create the one hot encoding.
For example, if 'data' has columns ['a', 'b', 'c'] and 'feature_col' has columns ['a', 'b'],
then the one hot encoding will be created only for columns ['a', 'b'] against 'data'.
Defaults to None, meaning that all columns of categorizable objects are encoded.

Returns
-------
DataFrame
The one hot encoding of the input features.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

[//]: # (Custom component imports)

import DocString from '@site/src/components/DocString';
import PythonCode from '@site/src/components/PythonCode';
import AppDisplay from '@site/src/components/AppDisplay';
import SectionBreak from '@site/src/components/SectionBreak';
import AppendixSection from '@site/src/components/AppendixSection';

[//]: # (Docstring)

import DocstringSource from '!!raw-loader!./a1-[autogen]/docstring.txt';
import PythonSource from '!!raw-loader!./a1-[autogen]/python_code.txt';

<DocString>{DocstringSource}</DocString>
<PythonCode GLink='AI_ML/CLASSIFICATION/TORCHSCRIPT_CLASSIFIER/TORCHSCRIPT_CLASSIFIER.py'>{PythonSource}</PythonCode>

<SectionBreak />



[//]: # (Examples)

## Examples

import Example1 from './examples/EX1/example.md';
import App1 from '!!raw-loader!./examples/EX1/app.json';
import appImg from './examples/EX1/app.jpeg'
import outputImg from './examples/EX1/output.jpeg'



<AppDisplay
nodeLabel='TORCHSCRIPT_CLASSIFIER'
appImg={appImg}
outputImg={outputImg}
>
{App1}
</AppDisplay>

<Example1 />

<SectionBreak />



[//]: # (Appendix)

import Notes from './appendix/notes.md';
import Hardware from './appendix/hardware.md';
import Media from './appendix/media.md';

## Appendix

<AppendixSection index={0} folderPath='nodes/AI_ML/CLASSIFICATION/TORCHSCRIPT_CLASSIFIER/appendix/'><Notes /></AppendixSection>
<AppendixSection index={1} folderPath='nodes/AI_ML/CLASSIFICATION/TORCHSCRIPT_CLASSIFIER/appendix/'><Hardware /></AppendixSection>
<AppendixSection index={2} folderPath='nodes/AI_ML/CLASSIFICATION/TORCHSCRIPT_CLASSIFIER/appendix/'><Media /></AppendixSection>


Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

Execute a torchscript classifier against an input image.

Inputs
----------
input_image : Image
The image to classify.
class_names : DataFrame
A dataframe containing the class names.

Parameters
----------
model_path : str
The path to the torchscript model.

Returns
----------
DataFrame
A dataframe containing the class name and confidence score.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from flojoy import flojoy, run_in_venv, Image, DataFrame


@flojoy
@run_in_venv(
pip_dependencies=[
"torch==2.0.1",
"torchvision==0.15.2",
"numpy",
"Pillow",
]
)
def TORCHSCRIPT_CLASSIFIER(
input_image: Image, class_names: DataFrame, model_path: str
) -> DataFrame:


import torch
import torchvision
import pandas as pd
import numpy as np
import PIL.Image

# Load model
model = torch.jit.load(model_path)
channels = [input_image.r, input_image.g, input_image.b]
mode = "RGB"

if input_image.a is not None:
channels.append(input_image.a)
mode += "A"

input_image_pil = PIL.Image.fromarray(
np.stack(channels).transpose(1, 2, 0), mode=mode
).convert("RGB")
input_tensor = torchvision.transforms.functional.to_tensor(
input_image_pil
).unsqueeze(0)

# Run model
with torch.inference_mode():
output = model(input_tensor)

# Get class name and confidence score
_, pred = torch.max(output, 1)
class_name = class_names.m.iloc[pred.item()].item()
confidence = torch.nn.functional.softmax(output, dim=1)[0][pred.item()].item()

return DataFrame(
df=pd.DataFrame({"class_name": [class_name], "confidence": [confidence]})
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This node does not require any peripheral hardware to operate. Please see INSTRUMENTS for nodes that interact with the physical world through connected hardware.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No supporting screenshots, photos, or videos have been added to the media.md file for this node.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No theory or technical notes have been contributed for this node yet.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 96e4756

Please sign in to comment.