PingPIng 23 Posted October 30, 2022 https://github.com/Pigrecos/TensorFlow.Delphi TensorFlow.Delphi provides a Delphi(Pascal)Standard binding for tensorflow It aims to implement the complete Tensorflow API in Delphi which allows Pascal developers to develop, train and deploy Machine Learning models with the Pascal Delphi(porting to free pascal in the future). Note: This is a work-in-progress. please treat it as such.Pull request are welcome 4 5 Share this post Link to post
PingPIng 23 Posted April 6, 2023 In progress..... function TMnistGAN.Make_Generator_model: Model; begin var mModel := TKerasApi.keras.Sequential(nil,'GENERATOR'); mModel.OnEpochBegin := On_Epoch_Begin; mModel.OnTrainBatchBegin := On_Train_Batch_Begin; mModel.OnEndSummary := On_End_Summary; mModel.OnTestBatchBegin := On_Train_Batch_Begin; mModel.Add( layers.Input(TFShape.Create([noise_dim])).first ); mModel.Add( layers.Dense(7*7*256, {activation}nil ,{kernel_initializer}nil, {use_bias}False) ); mModel.Add( layers.BatchNormalization); mModel.Add( layers.LeakyReLU); mModel.Add( layers.Reshape(TFShape.Create([7, 7, 256]))) ; Assert(mModel.OutputShape = TFShape.Create([-1, 7, 7, 256])); mModel.Add( layers.Conv2DTranspose(128, TFShape.Create([5, 5]), TFShape.Create([1, 1]), 'same', {data_format}'', {dilation_rate}nil, {activation}'relu', False)); Assert(mModel.OutputShape = TFShape.Create([-1, 7, 7, 128])); mModel.Add( layers.BatchNormalization); mModel.Add( layers.LeakyReLU); mModel.Add( layers.Conv2DTranspose(64, TFShape.Create([5, 5]), TFShape.Create([2, 2]), 'same', {data_format}'', {dilation_rate}nil, {activation}'relu', False)); Assert(mModel.OutputShape = TFShape.Create([-1, 14, 14, 64])); mModel.Add( layers.BatchNormalization); mModel.Add( layers.LeakyReLU); mModel.Add( layers.Conv2DTranspose(1, TFShape.Create([5, 5]), TFShape.Create([2, 2]), 'same', {data_format}'', {dilation_rate}nil, {activation}'tanh', False)); Assert(mModel.OutputShape = TFShape.Create([-1, 28, 28, 1])); mModel.summary; Result := mModel; end; function TMnistGAN.Make_Discriminator_model: Model; begin var model := TKerasApi.keras.Sequential(nil,'DISCRIMINATOR'); model.OnEpochBegin := On_Epoch_Begin; model.OnTrainBatchBegin := On_Train_Batch_Begin; model.OnEndSummary := On_End_Summary; model.OnTestBatchBegin := On_Train_Batch_Begin; model.Add( layers.Input(img_shape).first ); model.add(layers.Conv2D(64, TFShape.Create([5, 5]), TFShape.Create([2, 2]), 'same')); model.add(layers.LeakyReLU); model.add(layers.Dropout(0.3)); model.add(layers.Conv2D(128, TFShape.Create([5, 5]), TFShape.Create([2, 2]), 'same')); model.add(layers.LeakyReLU); model.add(layers.Dropout(0.3)) ; model.add(layers.Flatten) ; model.add(layers.Dense(1)); model.summary; Result := model; end; Share this post Link to post
PingPIng 23 Posted April 6, 2023 procedure LayersTest.TensorFlowOpLayer; var mean : TTensor; adv : TTensor; value : TFTensor; inputs : TFTensors; x : TFTensors; begin var l_layers := tf.keras.layers; inputs := l_layers.Input( TFShape.Create([24]) ); x := l_layers.Dense(128, 'relu').Apply(inputs); value := l_layers.Dense(24).Apply(x).first; adv := l_layers.Dense(1).Apply(x).First; var aAxis : TAxis := 1; mean := adv - tf.reduce_mean(adv, @aAxis, true);; adv := l_layers.Subtract.Apply(TFTensors.Create([adv, mean])).first; var outputs := l_layers.Add.Apply(TFTensors.Create([value, adv])); var model := tf.keras.Model(inputs, outputs); model.OnEpochBegin := On_Epoch_Begin; model.OnTrainBatchBegin := On_Train_Batch_Begin; model.OnEndSummary := On_End_Summary; model.compile(tf.keras.optimizers.RMSprop(Single(0.001)), tf.keras.losses.MeanSquaredError, [ 'acc' ]); model.summary; Assert.AreEqual(model.Layers.Count, 8); var res := model.predict(TFTensors.Create( tf.constant(np.arange(24).astype(np.np_float32)[ [np.newaxis, Slice.All] ]) )); Assert.Istrue(res.shape= TFShape.Create([1, 24])); model.fit(np.arange(24).astype(np.np_float32)[[np.newaxis, Slice.All]], np.arange(24).astype(np.np_float32)[[np.newaxis, Slice.All]],{Batch_Size} -1,{Epochs} 1,{Verbose} 0); end; procedure TestXor; var mModel : Sequential; begin var x := np.np_array<Single>([ [ 0, 0 ], [ 0, 1 ], [ 1, 0 ], [ 1, 1 ] ],np.np_float32); var y := np.np_array<Single>([ [ 0 ], [ 1 ], [ 1 ], [ 0 ] ],np.np_float32); mModel := TKerasApi.keras.Sequential; try mModel.OnEpochBegin := On_Epoch_Begin; mModel.OnTrainBatchBegin := On_Train_Batch_Begin; mModel.OnEndSummary := On_End_Summary; mModel.OnTestBatchEnd := On_Epoch_Begin; mModel.add(tf.keras.Input(2)); mModel.add(tf.keras.layers.Dense(32, tf.keras.activations.Relu)); mModel.add(tf.keras.layers.Dense(64, tf.keras.activations.Relu)); mModel.add(tf.keras.layers.Dense(1, tf.keras.activations.Sigmoid)); mModel.compile(tf.keras.optimizers.Adam, tf.keras.losses.MeanSquaredError, ['accuracy']); mModel.fit(x, y, {batch_size}-1, {epochs}50, {verbose}1); var s := mModel.predict(TFTensors.Create(x), 4).tostring; frmMain.mmo1.Lines.Add(s); finally mModel.free; end; end; procedure TUnitTest_Basic.TFRandomSeedTest; begin var initValue := np.arange(6).reshape(TFShape.create([3, 2])); tf.set_random_seed(1234); var a1 := tf.random_uniform(1); var b1 := tf.random_shuffle(tf.constant(initValue)); // This part we consider to be a refresh tf.set_random_seed(10); tf.random_uniform(1); tf.random_shuffle(tf.constant(initValue)); tf.set_random_seed(1234); var a2 := tf.random_uniform(1); var b2 := tf.random_shuffle(tf.constant(initValue)); Assert.IsTrue(a1.numpy.Equals(a2.numpy)); Assert.IsTrue(b1.numpy.Equals(b2.numpy)); end; And Other... Share this post Link to post
Reidinga 0 Posted July 28, 2023 Very nice! I have build several models with yolov5 for object detection. Would it be possible to use these models with this unit? Share this post Link to post
Kryvich 165 Posted July 29, 2023 I wonder if it is possible to create a similar binding for the PyTorch library? To use it directly from Delphi without installing Python. Share this post Link to post