Skip to content

Commit

Permalink
Adapt to TFJava 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
karllessard committed Nov 28, 2021
1 parent 0489e6b commit ed75c1c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 79 deletions.
5 changes: 3 additions & 2 deletions tensorflow-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
<!-- The maven compiler plugin defaults to a lower version -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<tensorflow.version>0.4.0</tensorflow.version>
</properties>

<dependencies>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-core-platform</artifactId>
<version>0.3.1</version>
<version>${tensorflow.version}</version>
</dependency>
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>tensorflow-framework</artifactId>
<version>0.3.1</version>
<version>${tensorflow.version}</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ The given SavedModel SignatureDef contains the following output(s):
*/


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import org.tensorflow.Graph;
import org.tensorflow.Operand;
import org.tensorflow.SavedModelBundle;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.Operand;
import org.tensorflow.ndarray.FloatNdArray;
import org.tensorflow.ndarray.Shape;
import org.tensorflow.op.Ops;
Expand All @@ -121,12 +125,6 @@ The given SavedModel SignatureDef contains the following output(s):
import org.tensorflow.types.TUint8;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;


/**
* Loads an image using ReadFile and DecodeJpeg and then uses the saved model
* faster_rcnn/inception_resnet_v2_1024x1024/1 to detect objects with a detection score greater than 0.3
Expand Down Expand Up @@ -254,7 +252,6 @@ public static void main(String[] params) {
Constant<TString> fileName = tf.constant(imagePath);
ReadFile readFile = tf.io.readFile(fileName);
Session.Runner runner = s.runner();
s.run(tf.init());
DecodeJpeg.Options options = DecodeJpeg.channels(3L);
DecodeJpeg decodeImage = tf.image.decodeJpeg(readFile.contents(), options);
//fetch image from file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,20 @@
import org.tensorflow.Graph;
import org.tensorflow.Operand;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.framework.optimizers.AdaDelta;
import org.tensorflow.framework.optimizers.AdaGrad;
import org.tensorflow.framework.optimizers.AdaGradDA;
import org.tensorflow.framework.optimizers.Adam;
import org.tensorflow.framework.optimizers.GradientDescent;
import org.tensorflow.framework.optimizers.Momentum;
import org.tensorflow.framework.optimizers.Optimizer;
import org.tensorflow.framework.optimizers.RMSProp;
import org.tensorflow.model.examples.datasets.ImageBatch;
import org.tensorflow.model.examples.datasets.mnist.MnistDataset;
import org.tensorflow.ndarray.ByteNdArray;
import org.tensorflow.ndarray.FloatNdArray;
import org.tensorflow.ndarray.Shape;
import org.tensorflow.ndarray.index.Indices;
import org.tensorflow.op.Op;
import org.tensorflow.op.Ops;
import org.tensorflow.op.core.Constant;
Expand All @@ -38,20 +49,8 @@
import org.tensorflow.op.nn.MaxPool;
import org.tensorflow.op.nn.Relu;
import org.tensorflow.op.nn.Softmax;
import org.tensorflow.op.nn.raw.SoftmaxCrossEntropyWithLogits;
import org.tensorflow.op.nn.SoftmaxCrossEntropyWithLogits;
import org.tensorflow.op.random.TruncatedNormal;
import org.tensorflow.ndarray.Shape;
import org.tensorflow.ndarray.ByteNdArray;
import org.tensorflow.ndarray.FloatNdArray;
import org.tensorflow.ndarray.index.Indices;
import org.tensorflow.framework.optimizers.AdaDelta;
import org.tensorflow.framework.optimizers.AdaGrad;
import org.tensorflow.framework.optimizers.AdaGradDA;
import org.tensorflow.framework.optimizers.Adam;
import org.tensorflow.framework.optimizers.GradientDescent;
import org.tensorflow.framework.optimizers.Momentum;
import org.tensorflow.framework.optimizers.Optimizer;
import org.tensorflow.framework.optimizers.RMSProp;
import org.tensorflow.types.TFloat32;
import org.tensorflow.types.TUint8;

Expand All @@ -75,7 +74,6 @@ public class CnnMnist {
public static final String TARGET = "target";
public static final String TRAIN = "train";
public static final String TRAINING_LOSS = "training_loss";
public static final String INIT = "init";

private static final String TRAINING_IMAGES_ARCHIVE = "mnist/train-images-idx3-ubyte.gz";
private static final String TRAINING_LABELS_ARCHIVE = "mnist/train-labels-idx1-ubyte.gz";
Expand Down Expand Up @@ -160,8 +158,7 @@ public static Graph build(String optimizerName) {
// Loss function & regularization
OneHot<TFloat32> oneHot = tf
.oneHot(labels, tf.constant(10), tf.constant(1.0f), tf.constant(0.0f));
SoftmaxCrossEntropyWithLogits<TFloat32> batchLoss = tf.nn.raw
.softmaxCrossEntropyWithLogits(logits, oneHot);
SoftmaxCrossEntropyWithLogits<TFloat32> batchLoss = tf.nn.softmaxCrossEntropyWithLogits(logits, oneHot);
Mean<TFloat32> labelLoss = tf.math.mean(batchLoss.loss(), tf.constant(0));
Add<TFloat32> regularizers = tf.math.add(tf.nn.l2Loss(fc1Weights), tf.math
.add(tf.nn.l2Loss(fc1Biases),
Expand Down Expand Up @@ -197,19 +194,13 @@ public static Graph build(String optimizerName) {
default:
throw new IllegalArgumentException("Unknown optimizer " + optimizerName);
}
logger.info("Optimizer = " + optimizer.toString());
logger.info("Optimizer = " + optimizer);
Op minimize = optimizer.minimize(loss, TRAIN);

tf.init();

return graph;
}

public static void train(Session session, int epochs, int minibatchSize, MnistDataset dataset) {
// Initialises the parameters.
session.runner().addTarget(INIT).run();
logger.info("Initialised the model parameters");

int interval = 0;
// Train the model
for (int i = 0; i < epochs; i++) {
Expand Down Expand Up @@ -274,7 +265,7 @@ public static void test(Session session, int minibatchSize, MnistDataset dataset
sb.append("\n");
}

System.out.println(sb.toString());
System.out.println(sb);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
*/
package org.tensorflow.model.examples.cnn.vgg;

import org.tensorflow.model.examples.datasets.mnist.MnistDataset;

import java.util.logging.Logger;
import org.tensorflow.model.examples.datasets.mnist.MnistDataset;

/**
* Trains and evaluates VGG'11 model on FashionMNIST dataset.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@
*/
package org.tensorflow.model.examples.cnn.vgg;

import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.tensorflow.Graph;
import org.tensorflow.Operand;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.framework.optimizers.Adam;
import org.tensorflow.framework.optimizers.Optimizer;
import org.tensorflow.model.examples.datasets.ImageBatch;
import org.tensorflow.model.examples.datasets.mnist.MnistDataset;
import org.tensorflow.ndarray.ByteNdArray;
import org.tensorflow.ndarray.FloatNdArray;
import org.tensorflow.ndarray.Shape;
import org.tensorflow.ndarray.index.Indices;
import org.tensorflow.op.Ops;
import org.tensorflow.op.core.Constant;
import org.tensorflow.op.core.OneHot;
Expand All @@ -35,19 +41,11 @@
import org.tensorflow.op.nn.Conv2d;
import org.tensorflow.op.nn.MaxPool;
import org.tensorflow.op.nn.Relu;
import org.tensorflow.op.nn.raw.SoftmaxCrossEntropyWithLogits;
import org.tensorflow.op.nn.SoftmaxCrossEntropyWithLogits;
import org.tensorflow.op.random.TruncatedNormal;
import org.tensorflow.ndarray.Shape;
import org.tensorflow.ndarray.ByteNdArray;
import org.tensorflow.ndarray.FloatNdArray;
import org.tensorflow.ndarray.index.Indices;
import org.tensorflow.types.TFloat32;
import org.tensorflow.types.TUint8;

import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Describes the VGGModel.
*/
Expand All @@ -64,7 +62,6 @@ public class VGGModel implements AutoCloseable {
public static final String TARGET = "target";
public static final String TRAIN = "train";
public static final String TRAINING_LOSS = "training_loss";
public static final String INIT = "init";

private static final Logger logger = Logger.getLogger(VGGModel.class.getName());

Expand Down Expand Up @@ -127,8 +124,6 @@ public static Graph compile() {

optimizer.minimize(loss, TRAIN);

tf.init();

return graph;
}

Expand Down Expand Up @@ -159,8 +154,7 @@ public static Add<TFloat32> buildFCLayersAndRegularization(Ops tf, Placeholder<T
// Loss function & regularization
OneHot<TFloat32> oneHot = tf
.oneHot(labels, tf.constant(10), tf.constant(1.0f), tf.constant(0.0f));
SoftmaxCrossEntropyWithLogits<TFloat32> batchLoss = tf.nn.raw
.softmaxCrossEntropyWithLogits(logits, oneHot);
SoftmaxCrossEntropyWithLogits<TFloat32> batchLoss = tf.nn.softmaxCrossEntropyWithLogits(logits, oneHot);
Mean<TFloat32> labelLoss = tf.math.mean(batchLoss.loss(), tf.constant(0));
Add<TFloat32> regularizers = tf.math.add(tf.nn.l2Loss(fc1Weights), tf.math
.add(tf.nn.l2Loss(fc1Biases),
Expand Down Expand Up @@ -193,10 +187,6 @@ public static Relu<TFloat32> vggConv2DLayer(String layerName, Ops tf, Operand<TF
}

public void train(MnistDataset dataset, int epochs, int minibatchSize) {
// Initialises the parameters.
session.runner().addTarget(INIT).run();
logger.info("Initialised the model parameters");

int interval = 0;
// Train the model
for (int i = 0; i < epochs; i++) {
Expand Down Expand Up @@ -261,7 +251,7 @@ public void test(MnistDataset dataset, int minibatchSize) {
sb.append("\n");
}

System.out.println(sb.toString());
System.out.println(sb);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@
import org.tensorflow.Graph;
import org.tensorflow.Operand;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.framework.optimizers.GradientDescent;
import org.tensorflow.framework.optimizers.Optimizer;
import org.tensorflow.model.examples.datasets.ImageBatch;
import org.tensorflow.model.examples.datasets.mnist.MnistDataset;
import org.tensorflow.ndarray.ByteNdArray;
import org.tensorflow.ndarray.Shape;
import org.tensorflow.op.Op;
import org.tensorflow.op.Ops;
import org.tensorflow.op.core.Placeholder;
import org.tensorflow.op.core.Variable;
import org.tensorflow.op.math.Mean;
import org.tensorflow.op.nn.Softmax;
import org.tensorflow.ndarray.Shape;
import org.tensorflow.ndarray.ByteNdArray;
import org.tensorflow.types.TFloat32;
import org.tensorflow.types.TInt64;

Expand Down Expand Up @@ -61,16 +60,11 @@ public void run() {

// Create weights with an initial value of 0
Shape weightShape = Shape.of(dataset.imageSize(), MnistDataset.NUM_CLASSES);
Variable<TFloat32> weights = tf.variable(weightShape, TFloat32.class);
tf.initAdd(tf.assign(weights, tf.zerosLike(weights)));
Variable<TFloat32> weights = tf.variable(tf.zeros(tf.constant(weightShape), TFloat32.class));

// Create biases with an initial value of 0
Shape biasShape = Shape.of(MnistDataset.NUM_CLASSES);
Variable<TFloat32> biases = tf.variable(biasShape, TFloat32.class);
tf.initAdd(tf.assign(biases, tf.zerosLike(biases)));

// Register all variable initializers for single execution
tf.init();
Variable<TFloat32> biases = tf.variable(tf.zeros(tf.constant(biasShape), TFloat32.class));

// Predict the class of each image in the batch and compute the loss
Softmax<TFloat32> softmax =
Expand Down Expand Up @@ -103,9 +97,6 @@ public void run() {
// Run the graph
try (Session session = new Session(graph)) {

// Initialize variables
session.run(tf.init());

// Train the model
for (ImageBatch trainingBatch : dataset.trainingBatches(TRAINING_BATCH_SIZE)) {
try (TFloat32 batchImages = preprocessImages(trainingBatch.images());
Expand Down Expand Up @@ -163,8 +154,8 @@ private static TFloat32 preprocessLabels(ByteNdArray rawLabels) {
).asTensor();
}

private Graph graph;
private MnistDataset dataset;
private final Graph graph;
private final MnistDataset dataset;

private SimpleMnist(Graph graph, MnistDataset dataset) {
this.graph = graph;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
*/
package org.tensorflow.model.examples.regression.linear;

import java.util.List;
import java.util.Random;
import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.framework.optimizers.GradientDescent;
import org.tensorflow.framework.optimizers.Optimizer;
import org.tensorflow.ndarray.Shape;
import org.tensorflow.op.Op;
import org.tensorflow.op.Ops;
import org.tensorflow.op.core.Placeholder;
Expand All @@ -29,12 +31,8 @@
import org.tensorflow.op.math.Div;
import org.tensorflow.op.math.Mul;
import org.tensorflow.op.math.Pow;
import org.tensorflow.ndarray.Shape;
import org.tensorflow.types.TFloat32;

import java.util.List;
import java.util.Random;

/**
* In this example TensorFlow finds the weight and bias of the linear regression during 1 epoch,
* training on observations one by one.
Expand Down Expand Up @@ -89,8 +87,6 @@ public static void main(String[] args) {
Op minimize = optimizer.minimize(mse);

try (Session session = new Session(graph)) {
// Initialize graph variables
session.run(tf.init());

// Train the model on data
for (int i = 0; i < xValues.length; i++) {
Expand Down

0 comments on commit ed75c1c

Please sign in to comment.