-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
CustomOpTests.java
52 lines (40 loc) · 1.74 KB
/
CustomOpTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package org.nd4j.imports.TFGraphs;
import lombok.val;
import org.junit.Test;
import org.nd4j.linalg.api.buffer.DataType;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.api.ops.DynamicCustomOp;
import org.nd4j.linalg.factory.Nd4j;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class CustomOpTests {
@Test
public void testPad(){
INDArray in = Nd4j.create(DataType.FLOAT, 1, 28, 28, 264);
INDArray pad = Nd4j.createFromArray(new int[][]{{0,0},{0,1},{0,1},{0,0}});
INDArray out = Nd4j.create(DataType.FLOAT, 1, 29, 29, 264);
DynamicCustomOp op = DynamicCustomOp.builder("pad")
.addInputs(in, pad)
.addOutputs(out)
.addIntegerArguments(0) //constant mode, with no constant specified
.build();
val outShape = Nd4j.getExecutioner().calculateOutputShape(op);
assertEquals(1, outShape.size());
assertArrayEquals(new long[]{1, 29, 29, 264}, outShape.get(0).getShape());
Nd4j.getExecutioner().exec(op); //Crash here
}
@Test
public void testResizeBilinearEdgeCase(){
INDArray in = Nd4j.ones(DataType.FLOAT, 1, 1, 1, 3);
INDArray size = Nd4j.createFromArray(8, 8);
INDArray out = Nd4j.create(DataType.FLOAT, 1, 8, 8, 3);
DynamicCustomOp op = DynamicCustomOp.builder("resize_bilinear")
.addInputs(in, size)
.addOutputs(out)
.addIntegerArguments(1) //1 = center. Though TF works with align_corners == false or true
.build();
Nd4j.getExecutioner().exec(op);
INDArray exp = Nd4j.ones(DataType.FLOAT, 1, 8, 8, 3);
assertEquals(exp, out);
}
}