Skip to content

Commit 5df131d

Browse files
committed
[tfjs-converter] add encodeBase64, decodeBase64
1 parent 51dc27a commit 5df131d

File tree

5 files changed

+118
-2
lines changed

5 files changed

+118
-2
lines changed

tfjs-converter/docs/supported_ops.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@
266266
|Not mapped|ifft|
267267
|Not mapped|rfft|
268268

269+
## Operations - Strings
270+
271+
|Tensorflow Op Name|Tensorflow.js Op Name|
272+
|---|---|
273+
|DecodeBase64|decodeBase64|
274+
|EncodeBase64|encodeBase64|
275+
269276
## Tensors - Transformations
270277

271278
|Tensorflow Op Name|Tensorflow.js Op Name|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
[
2+
{
3+
"tfOpName": "DecodeBase64",
4+
"category": "string",
5+
"inputs": [
6+
{
7+
"start": 0,
8+
"name": "input",
9+
"type": "tensor"
10+
}
11+
]
12+
},
13+
{
14+
"tfOpName": "EncodeBase64",
15+
"category": "string",
16+
"inputs": [
17+
{
18+
"start": 0,
19+
"name": "input",
20+
"type": "tensor"
21+
}
22+
],
23+
"attrs": [
24+
{
25+
"tfName": "pad",
26+
"name": "pad",
27+
"type": "bool"
28+
}
29+
]
30+
}
31+
]
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @license
3+
* Copyright 2019 Google Inc. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* =============================================================================
16+
*/
17+
18+
import * as tfc from '@tensorflow/tfjs-core';
19+
20+
import {NamedTensorsMap} from '../../data/types';
21+
import {ExecutionContext} from '../../executor/execution_context';
22+
import {InternalOpExecutor, Node} from '../types';
23+
24+
import {getParamValue} from './utils';
25+
26+
export let executeOp: InternalOpExecutor =
27+
(node: Node, tensorMap: NamedTensorsMap,
28+
context: ExecutionContext): tfc.Tensor[] => {
29+
switch (node.op) {
30+
case 'DecodeBase64': {
31+
const input =
32+
getParamValue('str', node, tensorMap, context) as tfc.Tensor;
33+
return [tfc.decodeBase64(input)];
34+
}
35+
case 'EncodeBase64': {
36+
const input =
37+
getParamValue('str', node, tensorMap, context) as tfc.Tensor;
38+
const pad = getParamValue('pad', node, tensorMap, context) as boolean;
39+
return [tfc.encodeBase64(input, pad)];
40+
}
41+
default:
42+
throw TypeError(`Node type ${node.op} is not implemented`);
43+
}
44+
};
45+
46+
export const CATEGORY = 'string';
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {OpMapper} from '../types';
2+
3+
/**
4+
* @license
5+
* Copyright 2019 Google LLC. All Rights Reserved.
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
* =============================================================================
18+
*/
19+
20+
export const json: OpMapper[] = [
21+
{
22+
'tfOpName': 'DecodeBase64',
23+
'category': 'string',
24+
'inputs': [{'start': 0, 'name': 'input', 'type': 'tensor'}]
25+
},
26+
{
27+
'tfOpName': 'EncodeBase64',
28+
'category': 'string',
29+
'inputs': [{'start': 0, 'name': 'input', 'type': 'tensor'}],
30+
'attrs': [{'tfName': 'pad', 'name': 'pad', 'type': 'bool'}]
31+
}
32+
];

tfjs-converter/src/operations/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2018 Google LLC. All Rights Reserved.
3+
* Copyright 2019 Google LLC. All Rights Reserved.
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
@@ -25,7 +25,7 @@ export type ParamType = 'number'|'string'|'string[]'|'number[]'|'bool'|'bool[]'|
2525
export type Category =
2626
'arithmetic'|'basic_math'|'control'|'convolution'|'custom'|'dynamic'|
2727
'evaluation'|'image'|'creation'|'graph'|'logical'|'matrices'|
28-
'normalization'|'reduction'|'slice_join'|'spectral'|'transformation';
28+
'normalization'|'reduction'|'slice_join'|'spectral'|'string'|'transformation';
2929

3030
// For mapping input or attributes of NodeDef into TensorFlow.js op param.
3131
export declare interface ParamMapper {

0 commit comments

Comments
 (0)