-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from nKandel/master
Tensorflow 2 support added with custom node feature Reviewed-By: Roger Dev <[email protected]> Merged-by: Gavin Halliday <[email protected]>
- Loading branch information
Showing
90 changed files
with
9,251 additions
and
262 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/*############################################################################## | ||
## HPCC SYSTEMS software Copyright (C) 2023 HPCC Systems. All rights reserved. | ||
############################################################################## */ | ||
/* | ||
About this test: | ||
Test the usability of Pre-trained Model ConvNeXtBase. | ||
Reference: https://www.tensorflow.org/api_docs/python/tf/keras/applications/convnext/ConvNeXtBase | ||
Input shape = (224, 224, 3) | ||
Note: The outputs of convnext.preprocess_input are integers | ||
Results: | ||
class probability | ||
tusker 9.285942077636719 | ||
African_elephant 8.67857837677002 | ||
Indian_elephant 3.142804145812988 | ||
*/ | ||
|
||
IMPORT Python3 AS Python; | ||
IMPORT $.^ AS GNN; | ||
IMPORT GNN.GNNI; | ||
IMPORT GNN.Tensor; | ||
IMPORT GNN.Internal AS int; | ||
IMPORT GNN.Internal.Types AS iTypes; | ||
IMPORT Std.System.Thorlib; | ||
IMPORT STD; | ||
|
||
kString := iTypes.kString; | ||
kStrType := iTypes.kStrType; | ||
t_Tensor := Tensor.R4.t_Tensor; | ||
TensData := Tensor.R4.TensData; | ||
|
||
// load the test data, an image of an elephant | ||
imageRecord := RECORD | ||
STRING filename; | ||
DATA image; | ||
//first 4 bytes contain the length of the image data | ||
UNSIGNED8 RecPos{virtual(fileposition)}; | ||
END; | ||
|
||
imageData := DATASET('~le::elephant',imageRecord,FLAT); | ||
OUTPUT(imageData, NAMED('elephant')); | ||
|
||
result := (STRING)(imageData[1].image); | ||
|
||
SET OF INTEGER hexToNparry(DATA byte_array):= EMBED(Python) | ||
from PIL import Image | ||
import numpy as np | ||
import io | ||
try: | ||
import tensorflow as tf # V2.x | ||
except: | ||
assert 1 == 0, 'tensorflow not found' | ||
bytes_data = bytes(byte_array) | ||
image = Image.open(io.BytesIO(bytes_data)) | ||
image = image.resize((224,224)) | ||
I_array = np.array(image) | ||
I_array = tf.keras.applications.convnext.preprocess_input(I_array) | ||
return I_array.flatten().tolist() | ||
ENDEMBED; | ||
|
||
valueRec := RECORD | ||
INTEGER value; | ||
END; | ||
|
||
idValueRec := RECORD | ||
UNSIGNED8 id; | ||
INTEGER value; | ||
END; | ||
|
||
imageNpArray := hexToNparry(imageData[1].image); | ||
x1 := DATASET(imageNpArray, valueRec); | ||
x2 := PROJECT(x1, TRANSFORM(idValueRec, SELF.id := COUNTER - 1, SELF.value := LEFT.value)); | ||
x3 := PROJECT(x2, TRANSFORM(TensData, | ||
SELF.indexes := [1, TRUNCATE(LEFT.id/(224*3)) + 1, TRUNCATE(LEFT.id/3)%224 + 1, LEFT.id%3 + 1], | ||
SELF.value := LEFT.value)); | ||
x := Tensor.R4.MakeTensor([0,224,224,3], x3); | ||
|
||
// load the model | ||
s := GNNI.GetSession(1); | ||
ldef := ['''applications.convnext.ConvNeXtBase(weights = "imagenet")''']; | ||
mod := GNNI.DefineModel(s, ldef); | ||
|
||
// Predict | ||
preds_tens := GNNI.Predict(mod, x); | ||
preds := Tensor.R4.GetData(preds_tens); | ||
|
||
predictRes := RECORD | ||
STRING class; | ||
REAL4 probability; | ||
END; | ||
|
||
// decode predictions | ||
DATASET(predictRes) decodePredictions(DATASET(TensData) preds, INTEGER topK = 3) := EMBED(Python) | ||
try: | ||
from tensorflow.keras.applications.convnext import decode_predictions | ||
except: | ||
assert 1 == 0, 'tensorflow not found' | ||
import numpy as np | ||
predsNp = np.zeros((1, 1000)) | ||
for pred in preds: | ||
predsNp[0, pred[0][1]-1] = pred[1] | ||
res = decode_predictions(predsNp, top=topK)[0] | ||
ret = [] | ||
for i in range(topK): | ||
ret.append((res[i][1], res[i][2])) | ||
return ret | ||
ENDEMBED; | ||
|
||
OUTPUT(decodePredictions(preds), NAMED('predictions')); |
Oops, something went wrong.