├── LICENSE ├── P4DTensorflowDemos ├── Demos │ ├── Machine_Learning_with_TensorFlow │ │ ├── Chapter02 │ │ │ ├── E00_overview.rtf │ │ │ ├── E01_types.py │ │ │ ├── E01_types_desc.rtf │ │ │ ├── E02_main.py │ │ │ ├── E02_main_desc.rtf │ │ │ ├── E03_interactive_session.py │ │ │ ├── E03_interactive_session_desc.rtf │ │ │ ├── E04_log_example.py │ │ │ ├── E04_log_example_desc.rtf │ │ │ ├── E05_spikes.py │ │ │ ├── E05_spikes_desc.rtf │ │ │ ├── E06_saving_vars.py │ │ │ ├── E06_saving_vars_desc.rtf │ │ │ ├── E06_saving_vars_dlg.xml │ │ │ ├── E07_loading_vars.py │ │ │ ├── E07_loading_vars_desc.rtf │ │ │ ├── E07_loading_vars_dlg.xml │ │ │ ├── E08_moving_avg.py │ │ │ ├── E08_moving_avg_desc.rtf │ │ │ ├── E09_using_queues.py │ │ │ ├── E09_using_queues_desc.rtf │ │ │ ├── E10_queue_text_from_file.py │ │ │ └── E10_queue_text_from_file_desc.rtf │ │ ├── Chapter03 │ │ │ ├── E00_overview.rtf │ │ │ ├── E01_linear_regression.py │ │ │ ├── E01_linear_regression.rtf │ │ │ ├── E02_polynomial_regression.py │ │ │ ├── E02_polynomial_regression.rtf │ │ │ ├── E03_regularization.py │ │ │ ├── E03_regularization.rtf │ │ │ └── data_reader.py │ │ ├── Chapter04 │ │ │ ├── E00_overview.rtf │ │ │ ├── E01_linear_regression_classification.py │ │ │ ├── E01_linear_regression_classification.rtf │ │ │ ├── E02_logistic_regression.py │ │ │ ├── E02_logistic_regression.rtf │ │ │ ├── E03_logistic_regression_2d.py │ │ │ ├── E03_logistic_regression_2d.rtf │ │ │ ├── E04_softmax classification.py │ │ │ └── E04_softmax classification.rtf │ │ ├── Chapter05 │ │ │ ├── E00_overview.rtf │ │ │ ├── E01_clustering.py │ │ │ ├── E01_clustering.rtf │ │ │ ├── E02_segmentation.py │ │ │ ├── E02_segmentation.rtf │ │ │ ├── E03_self_org_map.py │ │ │ └── E03_self_org_map.rtf │ │ ├── Chapter06 │ │ │ ├── E00_overview.rtf │ │ │ ├── E01_forward_algorithm.py │ │ │ ├── E01_forward_algorithm.rtf │ │ │ ├── E02_viterbi_decode.py │ │ │ └── E02_viterbi_decode.rtf │ │ └── Chapter07 │ │ │ ├── E00_overview.rtf │ │ │ ├── E01_autoencoder.py │ │ │ ├── E01_autoencoder.rtf │ │ │ ├── E02_autoencoder_with_images.py │ │ │ ├── E02_autoencoder_with_images.rtf │ │ │ ├── E03_denoising_autoencoder.py │ │ │ ├── E03_denoising_autoencoder.rtf │ │ │ └── autoencoder.py │ └── TFDelphiProps │ │ ├── E00_overview.rtf │ │ ├── E01_props.py │ │ ├── E01_props_desc.rtf │ │ ├── E01_props_dlg.xml │ │ ├── E02_props.py │ │ ├── E02_props_desc.rtf │ │ ├── E02_props_dlg.xml │ │ ├── E03_props.py │ │ ├── E03_props_desc.rtf │ │ ├── E03_props_dlg.xml │ │ ├── E04_props.py │ │ ├── E04_props_desc.rtf │ │ ├── E04_props_dlg.xml │ │ ├── E05_props.py │ │ ├── E05_props_desc.rtf │ │ ├── E05_props_dlg.xml │ │ ├── E06_props.py │ │ ├── E06_props_desc.rtf │ │ ├── E06_props_dlg.xml │ │ ├── E07_props.py │ │ ├── E07_props_desc.rtf │ │ └── E07_props_dlg.xml ├── P4DDemoControl │ ├── P4DDemoControl.dpr │ ├── P4DDemoControl.dproj │ ├── P4DDemoControl.ico │ ├── P4DDemoControl.res │ ├── fmMain.dfm │ └── fmMain.pas ├── audio_cough_1.wav ├── big.txt ├── runP4DDemoControl_for_TFDelphiProps.bat ├── runP4DDemoControl_for_TF_Chapter02.bat ├── runP4DDemoControl_for_TF_Chapter03.bat ├── runP4DDemoControl_for_TF_Chapter04.bat ├── runP4DDemoControl_for_TF_Chapter05.bat ├── runP4DDemoControl_for_TF_Chapter06.bat └── runP4DDemoControl_for_TF_Chapter07.bat ├── README.md ├── api ├── TensorFlow.DApi.pas ├── TensorFlow.DApiBase.pas ├── TensorFlow.DApiOperations.pas ├── TensorFlow.LowLevelAPI.pas └── TensorFlow._Helpers.pas ├── api_tests ├── TensorFlow.DApiUnitTests.pas ├── TensorFlow.LowLevelUnitTests.pas ├── TensorFlow.LowLevelUnitTestsUtil.pas ├── TestWithConsoleDUnitX │ ├── ConsoleTestTensorFlow.dpr │ ├── ConsoleTestTensorFlow.dproj │ ├── ConsoleTestTensorFlow.res │ └── TestTensorFlow.res ├── TestWithFMXUnitX │ ├── DUNitX.Loggers.GUIX.fmx │ ├── DUNitX.Loggers.GUIX.pas │ ├── DUnitX.MemoryLeaks.inc │ ├── DUnitX.Stacktrace.inc │ ├── DUnitX.inc │ ├── FMXTestTensorFlow.dpr │ ├── FMXTestTensorFlow.dproj │ ├── FMXTestTensorFlow.res │ └── TestTensorFlow.res └── TestWithVCLUnitX │ ├── DUnitX.Loggers.GUI.VCL.RichEdit.pas │ ├── DUnitX.Loggers.GUI.VCL.dfm │ ├── DUnitX.Loggers.GUI.VCL.pas │ ├── DUnitX.MemoryLeakMonitor.FastMM4.pas │ ├── DUnitX.ResStrs.pas │ ├── DUnitX.Stacktrace.inc │ ├── DUnitX.inc │ ├── HexDataInterpreter.dfm │ ├── HexDataInterpreter.pas │ ├── VCLTestTensorFlow.dpr │ ├── VCLTestTensorFlow.dproj │ └── VCLTestTensorFlow.res ├── bin ├── FastMM_FullDebugMode64.dll ├── SavedModels │ ├── half_plus_two │ │ └── 00000123 │ │ │ ├── assets │ │ │ └── foo.txt │ │ │ ├── saved_model.pb │ │ │ └── variables │ │ │ ├── variables.data-00000-of-00001 │ │ │ └── variables.index │ ├── half_plus_two_main_op │ │ └── 00000123 │ │ │ ├── assets │ │ │ └── foo.txt │ │ │ ├── saved_model.pb │ │ │ └── variables │ │ │ ├── variables.data-00000-of-00001 │ │ │ └── variables.index │ └── half_plus_two_pbtxt │ │ └── 00000123 │ │ ├── assets │ │ └── foo.txt │ │ ├── saved_model.pbtxt │ │ └── variables │ │ ├── variables.data-00000-of-00001 │ │ └── variables.index └── tensorflow.dll ├── c_api_extensions ├── c_api_ex.cc └── c_api_ex.h ├── clean.bat └── third_party ├── ATViewer ├── ATBinHex.pas ├── ATBinHexOptions.inc ├── ATBinHexPrint.inc ├── ATBinHexResources.res ├── ATFileNotification.pas ├── ATFileNotificationSimple.pas ├── ATStreamSearch.pas ├── ATStreamSearchOptions.inc ├── ATViewerMsg.pas ├── ATViewerOptions.inc ├── ATxClipboard.pas ├── ATxCodepageList.pas ├── ATxCodepages.pas ├── ATxFProc.pas ├── ATxSHex.pas ├── ATxSProc.pas ├── ATxTimer.pas └── ATxUtf8Detect.pas └── FastMM4 ├── FastMM4.pas ├── FastMM4Messages.pas └── FastMM4Options.inc /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E00_overview.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}{\f1\fswiss\fprq2\fcharset0 Calibri;}} 2 | {\colortbl ;\red0\green0\blue255;} 3 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 4 | \pard\sl240\slmult1\b\f0\fs28\lang7 Machine Learning with TensorFlow - Chapter 02\fs24\par 5 | {\fs22{\field{\*\fldinst{HYPERLINK https://github.com/BinRoot/TensorFlow-Book/tree/master/ch02_basics }}{\fldrslt{https://github.com/BinRoot/TensorFlow-Book/tree/master/ch02_basics\ul0\cf0}}}}\f0\fs22\par 6 | \fs24\par 7 | \i\fs20 CMD-Commands:\par 8 | \b0 Please check: >pip show \f1\lang1031 tensorflow\par 9 | \f0\lang7 If not installed tensorflow 1.4.x: >pip3 install --upgrade \f1\lang1031 tensorflow\f0\lang7\par 10 | \b\i0\fs22\par 11 | Example 1:\b0\par 12 | 13 | \pard\sa200\sl276\slmult1 Defining tensors\par 14 | 15 | \pard\sl240\slmult1\b Example 2:\b0\par 16 | 17 | \pard\sa200\sl276\slmult1 Evaluating ops\par 18 | 19 | \pard\sl240\slmult1\b Example 3:\b0\par 20 | 21 | \pard\sa200\sl276\slmult1 Interactive session\par 22 | 23 | \pard\sl240\slmult1\b Example 4:\b0\par 24 | 25 | \pard\sa200\sl276\slmult1 Session logging\par 26 | 27 | \pard\sl240\slmult1\b Example 5:\b0\par 28 | 29 | \pard\sa200\sl276\slmult1 Using variables\par 30 | 31 | \pard\sl240\slmult1\b Example 6:\b0\par 32 | 33 | \pard\sa200\sl276\slmult1 Saving variables\par 34 | 35 | \pard\sl240\slmult1\b Example 7:\b0\par 36 | 37 | \pard\sa200\sl276\slmult1 Loading variables\par 38 | 39 | \pard\sl240\slmult1\b Example 8:\b0\par 40 | 41 | \pard\sa200\sl276\slmult1 Using TensorBoard\par 42 | 43 | \pard\sl240\slmult1\b Example 9:\b0\par 44 | 45 | \pard\sa200\sl276\slmult1 Using Queues\par 46 | 47 | \pard\sl240\slmult1\b Example 10:\b0\par 48 | 49 | \pard\sa200\sl276\slmult1 Queue of text from file\par 50 | \par 51 | } 52 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E01_types.py: -------------------------------------------------------------------------------- 1 | # # Tensor Types 2 | 3 | import tensorflow as tf 4 | import numpy as np 5 | 6 | # Define a 2x2 matrix in 3 different ways 7 | 8 | m1 = [[1.0, 2.0], [3.0, 4.0]] 9 | m2 = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32) 10 | m3 = tf.constant([[1.0, 2.0], [3.0, 4.0]]) 11 | 12 | print(type(m1)) 13 | print(type(m2)) 14 | print(type(m3)) 15 | 16 | 17 | # Create tensor objects out of various types 18 | 19 | t1 = tf.convert_to_tensor(m1, dtype=tf.float32) 20 | t2 = tf.convert_to_tensor(m2, dtype=tf.float32) 21 | t3 = tf.convert_to_tensor(m3, dtype=tf.float32) 22 | 23 | print(type(t1)) 24 | print(type(t2)) 25 | print(type(t3)) 26 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E01_types_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 1: \b0 Defining tensors\par 4 | \b\fs22\par 5 | Line 3-4\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Import TensorFlow and Numpy\par 8 | 9 | \pard\sl240\slmult1\b Line 8-10\b0\par 10 | 11 | \pard\sa200\sl276\slmult1 Define a 2x2 matrix in different ways\par 12 | 13 | \pard\sl240\slmult1\b Line 12-14\b0\par 14 | 15 | \pard\sa200\sl276\slmult1 Let's see what happens when we print them:\par 16 | 17 | \pard\sl240\slmult1 \par 18 | \par 19 | 20 | \pard\sa200\sl240\slmult1 \par 21 | 22 | \pard\sl240\slmult1\b Line 19-21\b0\par 23 | By the way, there's a function called convert_to_tensor(...) that does exactly what you might expect.\par 24 | Let's use it to create tensor objects out of various types:\par 25 | \par 26 | \b Line 23-25\b0\par 27 | 28 | \pard\sa200\sl276\slmult1 Ok, ok! Time for the reveal:\par 29 | 30 | \pard\sl276\slmult1 \par 31 | \par 32 | 33 | \pard\sa200\sl276\slmult1 \par 34 | \par 35 | } 36 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E02_main.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | x = tf.constant([[1, 2]]) 4 | neg_x = tf.negative(x) 5 | 6 | print(neg_x) 7 | 8 | with tf.Session() as sess: 9 | result = sess.run(neg_x) 10 | print(result) 11 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E02_main_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 2: \b0 Evaluating ops\par 4 | \b\fs22\par 5 | Line 3-4\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Import TensorFlow\par 8 | 9 | \pard\sl240\slmult1\b Line 3\b0\par 10 | 11 | \pard\sa200\sl276\slmult1 Start with a 1x2 matrix\par 12 | 13 | \pard\sl240\slmult1\b Line 4\b0\par 14 | 15 | \pard\sa200\sl276\slmult1 Let's negate it. Define the negation op to be run on the matrix.\par 16 | 17 | \pard\sl240\slmult1\b Line 6\b0\par 18 | 19 | \pard\sl276\slmult1 It's nothing special if you print it out. In fact, it doesn't even perform the negation computation. Check out what happens when you simply print it:\par 20 | 21 | \pard\sa200\sl276\slmult1 Tensor("Neg:0", shape=(1, 2), dtype=int32)\par 22 | 23 | \pard\sl240\slmult1\b Line 8-10\b0\par 24 | 25 | \pard\sl276\slmult1 You need to summon a session so you can launch the negation op:\par 26 | 27 | \pard\sa200\sl276\slmult1 [[-1 -2]]\par 28 | \par 29 | \par 30 | } 31 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E03_interactive_session.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | sess = tf.InteractiveSession() 3 | 4 | x = tf.constant([[1., 2.]]) 5 | neg_op = tf.negative(x) 6 | 7 | result = neg_op.eval() 8 | print(result) 9 | 10 | sess.close() 11 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E03_interactive_session_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 3: \b0 Interactive session\par 4 | \b\fs22\par 5 | Line 1-2\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Interactive sessions are another way to use a session. Go ahead and define one.\par 8 | 9 | \pard\sl240\slmult1\b Line 4-5\b0\par 10 | 11 | \pard\sa200\sl276\slmult1 We have a matrix we want to invert.\par 12 | 13 | \pard\sl240\slmult1\b Line 7-8\b0\par 14 | 15 | \pard\sl276\slmult1 Since we're using an interactive session, we can just call the eval() method on the op and print: [[-1. -2.]]\par 16 | \par 17 | 18 | \pard\sl240\slmult1\b Line 10\b0\par 19 | 20 | \pard\sa200\sl276\slmult1 Don't forget to close the session.\par 21 | \par 22 | \par 23 | } 24 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E04_log_example.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | x = tf.constant([[1, 2]]) 4 | neg_op = tf.negative(x) 5 | 6 | with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess: 7 | result = sess.run(neg_op) 8 | print(result) 9 | 10 | 11 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E04_log_example_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | \viewkind4\uc1\pard\sl240\slmult1\lang7\b\f0\fs28 Example 4: \b0 Session logging\par 3 | \b\fs22\par 4 | Line 1-4\b0\par 5 | \pard\sa200\sl276\slmult1 Define an op on a tensor.\par 6 | \pard\sl240\slmult1\b Line 6-8\b0\par 7 | \pard\sa200\sl276\slmult1 Now let's use a session with a special argument passed in. The result is: [[-1 -2]]\par 8 | } 9 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E05_spikes.py: -------------------------------------------------------------------------------- 1 | # # Using Variables in TensorFlow 2 | 3 | import tensorflow as tf 4 | sess = tf.InteractiveSession() 5 | 6 | # Create a boolean variable called `spike` to detect sudden a sudden increase in a series of numbers. 7 | # 8 | # Since all variables must be initialized, initialize the variable by calling `run()` on its `initializer`. 9 | raw_data = [1., 2., 8., -1., 0., 5.5, 6., 13] 10 | spike = tf.Variable(False) 11 | spike.initializer.run() 12 | 13 | # Loop through the data and update the spike variable when there is a significant increase 14 | for i in range(1, len(raw_data)): 15 | if raw_data[i] - raw_data[i-1] > 5: 16 | updater = tf.assign(spike, tf.constant(True)) 17 | updater.eval() 18 | else: 19 | tf.assign(spike, False).eval() 20 | print("Spike", spike.eval()) 21 | 22 | sess.close() 23 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E05_spikes_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 5: \b0 Using variables\par 4 | \b\fs22\par 5 | Line 4\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Here we go, here we go, here we go! Moving on from those simple examples, let's get a better understanding of variables. Start with a session:\par 8 | 9 | \pard\sl240\slmult1\b Line 9\b0\par 10 | 11 | \pard\sa200\sl276\slmult1 Left is a series of numbers. Don't worry what they mean. Just for fun, let's think of them as neural activations.\par 12 | 13 | \pard\sl240\slmult1\b Line 10-11\b0\par 14 | 15 | \pard\sa200\sl276\slmult1 Create a boolean variable called spike to detect a sudden increase in the values. All variables must be initialized. Go ahead and initialize the variable by calling run() on its initializer.\par 16 | 17 | \pard\sl240\slmult1\b Line 14-20\b0\par 18 | 19 | \pard\sl276\slmult1 Loop through the data and update the spike variable when there is a significant increase:\par 20 | Spike False\par 21 | Spike True\par 22 | Spike False\par 23 | Spike False\par 24 | Spike True\par 25 | Spike False\par 26 | 27 | \pard\sa200\sl276\slmult1 Spike True\par 28 | 29 | \pard\sl240\slmult1\b Line 22\b0\par 30 | 31 | \pard\sa200\sl276\slmult1 You forgot to close the session!\par 32 | \par 33 | \par 34 | } 35 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E06_saving_vars.py: -------------------------------------------------------------------------------- 1 | # # Saving Variables in TensorFlow 2 | 3 | import tensorflow as tf 4 | sess = tf.InteractiveSession() 5 | 6 | # Create a boolean vector called `spike` to locate a sudden spike in data. 7 | # 8 | # Since all variables must be initialized, initialize the variable by calling `run()` on its `initializer`. 9 | raw_data = [1., 2., 8., -1., 0., 5.5, 6., 13] 10 | spikes = tf.Variable([False] * len(raw_data), name='spikes') 11 | spikes.initializer.run() 12 | 13 | # The saver op will enable saving and restoring 14 | saver = tf.train.Saver() 15 | 16 | # Loop through the data and update the spike variable when there is a significant increase 17 | for i in range(1, len(raw_data)): 18 | if raw_data[i] - raw_data[i-1] > 5: 19 | spikes_val = spikes.eval() 20 | spikes_val[i] = True 21 | updater = tf.assign(spikes, spikes_val) 22 | updater.eval() 23 | 24 | filename = P4DProps.Value['save_filename'] 25 | save_path = saver.save(sess, filename) 26 | print("spikes data saved in file: %s" % save_path) 27 | 28 | sess.close() 29 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E06_saving_vars_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | \viewkind4\uc1\pard\sl240\slmult1\lang7\b\f0\fs28 Example 6: \b0 Saving variables\par 3 | \b\fs22\par 4 | Line 3-11\b0\par 5 | \pard\sa200\sl276\slmult1 Create an interactive session and initialize a variable.\par 6 | \pard\sl240\slmult1\b Line 14\b0\par 7 | \pard\sa200\sl276\slmult1 The saver op will enable saving and restoring.\par 8 | \pard\sl240\slmult1\b Line 17-22\b0\par 9 | \pard\sa200\sl276\slmult1 Loop through the data and update the spike variable when there is a significant increase.\par 10 | \pard\sl240\slmult1\b Line 24\b0\par 11 | \pard\sa200\sl276\slmult1 Get filename property.\par 12 | \pard\sl240\slmult1\b Line 25\b0\par 13 | \pard\sa200\sl276\slmult1 Now, save your variable to disk! spikes data saved in file: "spikes.ckpt"\par 14 | \pard\sl240\slmult1\b Line 28\b0\par 15 | \pard\sa200\sl276\slmult1 Adieu!\par 16 | } 17 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E06_saving_vars_dlg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | save_filename 5 | dlg_file 6 | Spikes data save in file: 7 | string 8 | /tmp/spikes.ckpt 9 | 10 | 11 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E07_loading_vars.py: -------------------------------------------------------------------------------- 1 | # # Loading Variables in TensorFlow 2 | 3 | import tensorflow as tf 4 | sess = tf.InteractiveSession() 5 | 6 | # Create a boolean vector called `spike` to locate a sudden spike in data. 7 | # 8 | # Since all variables must be initialized, initialize the variable by calling `run()` on its `initializer`. 9 | spikes = tf.Variable([False]*8, name='spikes') 10 | saver = tf.train.Saver() 11 | 12 | filename = P4DProps.Value['save_filename'] 13 | try: 14 | saver.restore(sess, filename) 15 | print(spikes.eval()) 16 | except: 17 | print('file not found') 18 | 19 | sess.close() 20 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E07_loading_vars_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | \viewkind4\uc1\pard\sl240\slmult1\lang7\b\f0\fs28 Example 7: \b0 Loading variables\par 3 | \b\fs22\par 4 | Line 4\b0\par 5 | \pard\sa200\sl276\slmult1 Concept 06 was about saving variables. This one's about loading what you saved. Start by creating an interactive session.\par 6 | \pard\sl276\slmult1\b Line 9\par 7 | \pard\sa200\sl276\slmult1\b0 Create a boolean vector called spikes of the same dimensions as before.\par 8 | \pard\sl276\slmult1\b Line 12\b0\par 9 | \pard\sa200\sl276\slmult1 Get filename property.\par 10 | \pard\sl276\slmult1\b Line 14-15\b0\par 11 | \pard\sa200\sl276\slmult1 Restored the variable data from disk, serve warm, and enjoy.\par 12 | \pard\sl240\slmult1\b Line 19\b0\par 13 | \pard\sa200\sl276\slmult1 Show's over, goodnight.\b\par 14 | \b0\par 15 | } 16 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E07_loading_vars_dlg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | save_filename 5 | dlg_file 6 | Spikes data restore from file: 7 | string 8 | /tmp/spikes.ckpt 9 | 10 | 11 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E08_moving_avg.py: -------------------------------------------------------------------------------- 1 | ## Using TensorBoard 2 | 3 | # mkdir logs 4 | 5 | import tensorflow as tf 6 | import numpy as np 7 | 8 | raw_data = np.random.normal(10, 1, 100) 9 | 10 | alpha = tf.constant(0.05) 11 | curr_value = tf.placeholder(tf.float32) 12 | prev_avg = tf.Variable(0.) 13 | 14 | update_avg = alpha * curr_value + (1 - alpha) * prev_avg 15 | 16 | avg_hist = tf.summary.scalar("running_average", update_avg) 17 | value_hist = tf.summary.scalar("incoming_values", curr_value) 18 | 19 | merged = tf.summary.merge_all() 20 | writer = tf.summary.FileWriter("/tmp/logs") 21 | 22 | init = tf.global_variables_initializer() 23 | 24 | with tf.Session() as sess: 25 | sess.run(init) 26 | for i in range(len(raw_data)): 27 | summary_str, curr_avg = sess.run([merged, update_avg], feed_dict={curr_value: raw_data[i]}) 28 | sess.run(tf.assign(prev_avg, curr_avg)) 29 | print(raw_data[i], curr_avg) 30 | writer.add_summary(summary_str, i) 31 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E08_moving_avg_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 8: \b0 Using TensorBoard\par 4 | \fs22 TensorBoard is a great way to visualize what's happening behind the code.\par 5 | In this example, we'll loop through some numbers to improve our guess of the average value. Then we can visualize the results on TensorBoard.\par 6 | Let's just set ourselves up with some data to work with.\par 7 | \b\par 8 | Line 10-14\b0\par 9 | 10 | \pard\sa200\sl276\slmult1 The moving average is defined as follows.\par 11 | 12 | \pard\sl240\slmult1\b Line 16-20\b0\par 13 | 14 | \pard\sa200\sl276\slmult1 Here's what we care to visualize.\par 15 | 16 | \pard\sl240\slmult1\b Line 22-30\b0\par 17 | 18 | \pard\sl276\slmult1 Time to compute the moving averages. We'll also run the merged op to track how the values change.\par 19 | Check out the visualization by running TensorBoard from the terminal:\par 20 | 21 | \pard\sa200\sl276\slmult1 $ tensorboard --logdir=path/to/logs\par 22 | \par 23 | } 24 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E09_using_queues.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import multiprocessing 4 | 5 | NUM_THREADS = multiprocessing.cpu_count() 6 | 7 | xs = np.random.randn(100, 3) 8 | ys = np.random.randint(0, 2, size=100) 9 | 10 | xs_and_ys = zip(xs, ys) 11 | for _ in range(5): 12 | x, y = next(xs_and_ys) 13 | print('Input {} ---> Output {}'.format(x, y)) 14 | 15 | queue = tf.FIFOQueue(capacity=1000, dtypes=[tf.float32, tf.int32]) 16 | 17 | enqueue_op = queue.enqueue_many([xs, ys]) 18 | x_op, y_op = queue.dequeue() 19 | 20 | qr = tf.train.QueueRunner(queue, [enqueue_op] * 4) 21 | 22 | sess = tf.InteractiveSession() 23 | 24 | coord = tf.train.Coordinator() 25 | enqueue_threads = qr.create_threads(sess, coord=coord, start=True) 26 | 27 | for _ in range(100): 28 | if coord.should_stop(): 29 | break 30 | x, y = sess.run([x_op, y_op]) 31 | print(x, y) 32 | coord.request_stop() 33 | coord.join(enqueue_threads) -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E09_using_queues_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | \viewkind4\uc1\pard\sl240\slmult1\lang7\b\f0\fs28 Example 9: \b0 Using Queues\par 3 | \fs22 If you have a lot of training data, you probably don't want to load it all into memory at once. The QueueRunner in TensorFlow is a tool to efficiently employ a queue data-structure in a multi-threaded way.\par 4 | \b\par 5 | Line 3-5\b0\par 6 | \pard\sa200\sl276\slmult1 We will be running multiple threads, so let's figure out the number of CPUs.\par 7 | \pard\sl240\slmult1\b Line 7-8\b0\par 8 | \pard\sa200\sl276\slmult1 Generate some fake data to work with.\par 9 | \pard\sl240\slmult1\b Line 10-13\b0\par 10 | \pard\sl276\slmult1 Here's a couple concrete examples of our data.\par 11 | \pard\sl240\slmult1\b\par 12 | Line 15\b0\par 13 | \pard\sa200\sl276\slmult1 Define a queue.\par 14 | \pard\sl240\slmult1\b Line 17-18\b0\par 15 | \pard\sa200\sl276\slmult1 Set up the enqueue and dequeue ops.\par 16 | \pard\sl240\slmult1\b Line 20\b0\par 17 | \pard\sa200\sl276\slmult1 Define a QueueRunner.\par 18 | \pard\sl240\slmult1\b Line 22\b0\par 19 | \pard\sa200\sl276\slmult1 Now that all variables and ops have been defined, let's get started with a session.\par 20 | \pard\sl240\slmult1\b Line 24-25\b0\par 21 | \pard\sa200\sl276\slmult1 Create threads for the QueueRunner.\par 22 | \pard\sl240\slmult1\b Line 27-33\b0\par 23 | \pard\sa200\sl276\slmult1 Test out dequeueing\par 24 | } 25 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E10_queue_text_from_file.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | filename_queue = tf.train.string_input_producer(["big.txt"]) 4 | 5 | reader = tf.TextLineReader() 6 | key_op, value_op = reader.read(filename_queue) 7 | 8 | sess = tf.InteractiveSession() 9 | coord = tf.train.Coordinator() 10 | threads = tf.train.start_queue_runners(coord=coord) 11 | 12 | for i in range(100): 13 | key, value = sess.run([key_op, value_op]) 14 | print(key, value) -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter02/E10_queue_text_from_file_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | \viewkind4\uc1\pard\sl240\slmult1\lang7\b\f0\fs28 Example 10: \b0 Queue of text from file\par 3 | \b\fs22\par 4 | \b0 The previous notebook (Concept 09) introduced queues. Let's see how we can use them with real world data.\par 5 | Download a corpus of text: http://norvig.com/big.txt\par 6 | \b\par 7 | Line 3\b0\par 8 | \pard\sa200\sl276\slmult1 Create a queue of filenames.\par 9 | \pard\sl240\slmult1\b Line 5-6\b0\par 10 | \pard\sa200\sl276\slmult1 Define an op to dequeue a line from file.\par 11 | \pard\sl240\slmult1\b Line 8-10\b0\par 12 | \pard\sa200\sl276\slmult1 Start all queue runners collected in the graph.\par 13 | \pard\sl240\slmult1\b Line 12-14\b0\par 14 | \pard\sa200\sl276\slmult1 Try reading lines from the file by dequeuing.\par 15 | \par 16 | } 17 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter03/E00_overview.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\colortbl ;\red0\green0\blue255;} 3 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 4 | \pard\sl240\slmult1\b\f0\fs28\lang7 Machine Learning with TensorFlow - Chapter 05\fs24\par 5 | {\fs22{\field{\*\fldinst{HYPERLINK https://github.com/BinRoot/TensorFlow-Book/tree/master/ch03_regression }}{\fldrslt{https://github.com/BinRoot/TensorFlow-Book/tree/master/ch03_regression\ul0\cf0}}}}\f0\fs24\par 6 | \fs22\par 7 | \i\fs20 CMD-Commands:\par 8 | \b0 Please check: >pip show scipy\par 9 | If not installed : >pip3 install --upgrade scipy\b\par 10 | \i0\fs22\par 11 | Example 1:\b0\par 12 | 13 | \pard\sa200\sl276\slmult1 Linear regression\par 14 | 15 | \pard\sl240\slmult1\b Example 2:\b0\par 16 | 17 | \pard\sa200\sl276\slmult1 Polynomial regression\par 18 | 19 | \pard\sl240\slmult1\b Example 3:\b0\par 20 | 21 | \pard\sa200\sl276\slmult1 Regularization\par 22 | } 23 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter03/E01_linear_regression.py: -------------------------------------------------------------------------------- 1 | # 2 | import tensorflow as tf 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | 6 | learning_rate = 0.01 7 | training_epochs = 100 8 | 9 | x_train = np.linspace(-1, 1, 101) 10 | y_train = 2 * x_train + np.random.randn(*x_train.shape) * 0.33 11 | 12 | plt.scatter(x_train, y_train) 13 | 14 | X = tf.placeholder("float") 15 | Y = tf.placeholder("float") 16 | 17 | def model(X, w): 18 | return tf.multiply(X, w) 19 | 20 | w = tf.Variable(0.0, name="weights") 21 | 22 | y_model = model(X, w) 23 | cost = tf.reduce_mean(tf.square(Y-y_model)) 24 | 25 | train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 26 | 27 | sess = tf.Session() 28 | init = tf.global_variables_initializer() 29 | sess.run(init) 30 | 31 | for epoch in range(training_epochs): 32 | for (x, y) in zip(x_train, y_train): 33 | sess.run(train_op, feed_dict={X: x, Y: y}) 34 | 35 | w_val = sess.run(w) 36 | 37 | sess.close() 38 | 39 | plt.scatter(x_train, y_train) 40 | y_learned = x_train*w_val 41 | plt.plot(x_train, y_learned, 'r') 42 | plt.show() 43 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter03/E01_linear_regression.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | \viewkind4\uc1\pard\sl240\slmult1\lang7\b\f0\fs28 Example 1\b0 : Concept 01 - Linear regression\par 3 | \b\fs22\par 4 | Line 2-4\b0\par 5 | \pard\sa200\sl276\slmult1 Import TensorFlow for the learning algorithm. We'll need NumPy to set up the initial data. And we'll use matplotlib to visualize our data.\par 6 | \pard\sl240\slmult1\b Line 6-7\b0\par 7 | \pard\sa200\sl276\slmult1 Define some constants used by the learning algorithm. There are called hyper-parameters.\par 8 | \pard\sl240\slmult1\b Line 9-10\b0\par 9 | \pard\sa200\sl276\slmult1 Set up fake data that we will use to to find a best fit line.\par 10 | \pard\sl240\slmult1\b Line 12\b0\par 11 | Plot the raw data.\par 12 | \par 13 | \b Line 14-15\b0\par 14 | \pard\sa200\sl276\slmult1 Set up the input and output nodes as placeholders since the value will be injected by x_train and y_train.\par 15 | \pard\sl240\slmult1\b Line 17-18\b0\par 16 | Define the model as y = w'*x.\par 17 | \b\par 18 | Line 20\b0\par 19 | Set up the weights variable\par 20 | \b\par 21 | Line 22-23\b0\par 22 | Define the cost function as the mean squared error.\par 23 | \b\par 24 | Line 25\b0\par 25 | Define the operation that will be called on each iteration of the learning algorithm\par 26 | \par 27 | \b Line 27-29\b0\par 28 | Initialize all variables\par 29 | \par 30 | \b Line 31-33\b0\par 31 | Train on each (x, y) pair multiple times.\par 32 | \par 33 | \b Line 35\b0\par 34 | Fetch the value of the learned parameter.\par 35 | \par 36 | \b Line 37\b0\par 37 | Close the session.\par 38 | \par 39 | \b Line 39-42\b0\par 40 | Visualize the best fit curve.\par 41 | \pard\sa200\sl276\slmult1\par 42 | } 43 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter03/E02_polynomial_regression.py: -------------------------------------------------------------------------------- 1 | # 2 | import tensorflow as tf 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | 6 | learning_rate = 0.01 7 | training_epochs = 40 8 | 9 | trX = np.linspace(-1, 1, 101) 10 | 11 | num_coeffs = 6 12 | trY_coeffs = [1, 2, 3, 4, 5, 6] 13 | trY = 0 14 | for i in range(num_coeffs): 15 | trY += trY_coeffs[i] * np.power(trX, i) 16 | 17 | trY += np.random.randn(*trX.shape) * 1.5 18 | 19 | plt.scatter(trX, trY) 20 | plt.show() 21 | 22 | X = tf.placeholder("float") 23 | Y = tf.placeholder("float") 24 | 25 | def model(X, w): 26 | terms = [] 27 | for i in range(num_coeffs): 28 | term = tf.multiply(w[i], tf.pow(X, i)) 29 | terms.append(term) 30 | return tf.add_n(terms) 31 | 32 | w = tf.Variable([0.] * num_coeffs, name="parameters") 33 | y_model = model(X, w) 34 | 35 | cost = tf.reduce_sum(tf.square(Y-y_model)) 36 | train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 37 | 38 | sess = tf.Session() 39 | init = tf.global_variables_initializer() 40 | sess.run(init) 41 | 42 | for epoch in range(training_epochs): 43 | for (x, y) in zip(trX, trY): 44 | sess.run(train_op, feed_dict={X: x, Y: y}) 45 | 46 | w_val = sess.run(w) 47 | print(w_val) 48 | 49 | sess.close() 50 | 51 | plt.scatter(trX, trY) 52 | trY2 = 0 53 | for i in range(num_coeffs): 54 | trY2 += w_val[i] * np.power(trX, i) 55 | plt.plot(trX, trY2, 'r') 56 | plt.show() 57 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter03/E02_polynomial_regression.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 2\b0 : Concept 02 - Polynomial regression\par 4 | \b\fs22\par 5 | Line 2-7\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Import the relevant libraries and initialize the hyper-parameters.\par 8 | 9 | \pard\sl240\slmult1\b Line 9\b0\par 10 | 11 | \pard\sa200\sl276\slmult1 Set up some fake raw input data.\par 12 | 13 | \pard\sl240\slmult1\b Line 11-15\b0\par 14 | 15 | \pard\sa200\sl276\slmult1 Set up raw output data based on a degree 6 polynomial\par 16 | 17 | \pard\sl240\slmult1\b Line 17\b0\par 18 | Add some noise\par 19 | \par 20 | \b Line 19-20\b0\par 21 | Plot the raw data\par 22 | \par 23 | \b Line 22-23\b0\par 24 | Define the nodes to hold values for input/output pairs\par 25 | \b\par 26 | Line 25-30\b0\par 27 | Define our polynomial model\par 28 | \b\par 29 | Line 32-33\b0\par 30 | Set up the parameter vector to all zero\par 31 | \b\par 32 | Line 35-36\b0\par 33 | Define the cost function just as before\par 34 | \par 35 | \b Line 38-47\b0\par 36 | Set up the session and run the learning algorithm just as before\par 37 | \par 38 | \b Line 49\b0\par 39 | Close the session when done\par 40 | \par 41 | \b Line 51-56\b0\par 42 | Plot the result\par 43 | 44 | \pard\sa200\sl276\slmult1\par 45 | } 46 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter03/E03_regularization.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | 5 | np.random.seed(100) 6 | 7 | learning_rate = 0.001 8 | training_epochs = 1000 9 | reg_lambda = 0. 10 | 11 | def split_dataset(x_dataset, y_dataset, ratio): 12 | arr = np.arange(x_dataset.size) 13 | np.random.shuffle(arr) 14 | num_train = int(ratio * x_dataset.size) 15 | x_train = x_dataset[arr[0:num_train]] 16 | y_train = y_dataset[arr[0:num_train]] 17 | x_test = x_dataset[arr[num_train:x_dataset.size]] 18 | y_test = y_dataset[arr[num_train:x_dataset.size]] 19 | return x_train, x_test, y_train, y_test 20 | 21 | x_dataset = np.linspace(-1, 1, 100) 22 | 23 | num_coeffs = 9 24 | y_dataset_params = [0.] * num_coeffs 25 | y_dataset_params[2] = 1 26 | y_dataset = 0 27 | for i in range(num_coeffs): 28 | y_dataset += y_dataset_params[i] * np.power(x_dataset, i) 29 | y_dataset += np.random.randn(*x_dataset.shape) * 0.3 30 | 31 | (x_train, x_test, y_train, y_test) = split_dataset(x_dataset, y_dataset, 0.7) 32 | 33 | X = tf.placeholder("float") 34 | Y = tf.placeholder("float") 35 | 36 | def model(X, w): 37 | terms = [] 38 | for i in range(num_coeffs): 39 | term = tf.multiply(w[i], tf.pow(X, i)) 40 | terms.append(term) 41 | return tf.add_n(terms) 42 | 43 | w = tf.Variable([0.] * num_coeffs, name="parameters") 44 | y_model = model(X, w) 45 | cost = tf.div(tf.add(tf.reduce_sum(tf.square(Y-y_model)), 46 | tf.multiply(reg_lambda, tf.reduce_sum(tf.square(w)))), 47 | 2*x_train.size) 48 | train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 49 | 50 | sess = tf.Session() 51 | init = tf.global_variables_initializer() 52 | sess.run(init) 53 | 54 | for reg_lambda in np.linspace(0,1,100): 55 | for epoch in range(training_epochs): 56 | sess.run(train_op, feed_dict={X: x_train, Y: y_train}) 57 | final_cost = sess.run(cost, feed_dict={X: x_test, Y:y_test}) 58 | print('reg lambda', reg_lambda) 59 | print('final cost', final_cost) 60 | 61 | sess.close() -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter03/E03_regularization.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 3\b0 : Concept 03 - Regularization\par 4 | \b\fs22\par 5 | Line 1-9\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Import the relevant libraries and initialize the hyper-parameters.\par 8 | 9 | \pard\sl240\slmult1\b Line 11-19\b0\par 10 | create a helper method to split the dataset\par 11 | \par 12 | \b Line 21-29\b0\par 13 | 14 | \pard\sa200\sl276\slmult1 Create a fake dataset. y = x^2\par 15 | 16 | \pard\sl240\slmult1\b Line 31\b0\par 17 | Split the dataset into 70% training and testing 30% \par 18 | \par 19 | \b Line 33-34\b0\par 20 | Set up the input/output placeholders\par 21 | \par 22 | \b Line 36-41\b0\par 23 | Define our model \par 24 | \b\par 25 | Line 43-48\b0\par 26 | Define the regularized cost function\par 27 | \b\par 28 | Line 50-52\b0\par 29 | Set up the session\par 30 | \b\par 31 | Line 54-59\b0\par 32 | Try out various regularization parameters\par 33 | \par 34 | \b Line 61\b0\par 35 | Close the session\par 36 | \b\par 37 | } 38 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter03/data_reader.py: -------------------------------------------------------------------------------- 1 | import csv 2 | import time 3 | 4 | # 5 | # crime14_freq = data_reader.read('crimes_2014.csv', 1, '%d-%b-%y %H:%M:%S', 2014) 6 | # freq = read('311.csv', 0, '%m/%d/%Y', 2014) 7 | 8 | def read(filename, date_idx, date_parse, year, bucket=7): 9 | 10 | days_in_year = 365 11 | 12 | # Create initial frequency map 13 | freq = {} 14 | for period in range(0, int(days_in_year/bucket)): 15 | freq[period] = 0 16 | 17 | # Read data and aggregate crimes per day 18 | with open(filename, 'rb') as csvfile: 19 | csvreader = csv.reader(csvfile) 20 | csvreader.next() 21 | for row in csvreader: 22 | if row[date_idx] == '': 23 | continue 24 | t = time.strptime(row[date_idx], date_parse) 25 | if t.tm_year == year and t.tm_yday < (days_in_year-1): 26 | freq[int(t.tm_yday / bucket)] += 1 27 | 28 | return freq 29 | 30 | if __name__ == '__main__': 31 | freq = read('311.csv', 0, '%m/%d/%Y', 2014) 32 | print freq -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter04/E00_overview.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\colortbl ;\red0\green0\blue255;} 3 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 4 | \pard\sl240\slmult1\b\f0\fs28\lang7 Machine Learning with TensorFlow - Chapter 04\fs24\par 5 | {\fs22{\field{\*\fldinst{HYPERLINK https://github.com/BinRoot/TensorFlow-Book/tree/master/ch04_classification }}{\fldrslt{https://github.com/BinRoot/TensorFlow-Book/tree/master/ch04_classification\ul0\cf0}}}}\f0\fs24\par 6 | \fs22\par 7 | Example 1:\b0\par 8 | 9 | \pard\sa200\sl276\slmult1 Linear regression for classification\par 10 | 11 | \pard\sl240\slmult1\b Example 2:\b0\par 12 | 13 | \pard\sa200\sl276\slmult1 Logistic regression\par 14 | 15 | \pard\sl240\slmult1\b Example 3:\b0\par 16 | 17 | \pard\sa200\sl276\slmult1 2D Logistic regression\par 18 | 19 | \pard\sl240\slmult1\b Example 4:\b0\par 20 | 21 | \pard\sa200\sl276\slmult1 Softmax classification\par 22 | \par 23 | } 24 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter04/E01_linear_regression_classification.py: -------------------------------------------------------------------------------- 1 | # 2 | import tensorflow as tf 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | 6 | x_label0 = np.random.normal(5, 1, 10) 7 | x_label1 = np.random.normal(2, 1, 10) 8 | xs = np.append(x_label0, x_label1) 9 | labels = [0.] * len(x_label0) + [1.] * len(x_label1) 10 | 11 | plt.scatter(xs, labels) 12 | 13 | learning_rate = 0.001 14 | training_epochs = 1000 15 | 16 | X = tf.placeholder("float") 17 | Y = tf.placeholder("float") 18 | 19 | w = tf.Variable([0., 0.], name="parameters") 20 | 21 | def model(X, w): 22 | return tf.add(tf.multiply(w[1], tf.pow(X, 1)), 23 | tf.multiply(w[0], tf.pow(X, 0))) 24 | 25 | y_model = model(X, w) 26 | cost = tf.reduce_sum(tf.square(Y-y_model)) 27 | 28 | train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 29 | correct_prediction = tf.equal(Y, tf.to_float(tf.greater(y_model, 0.5))) 30 | accuracy = tf.reduce_mean(tf.to_float(correct_prediction)) 31 | 32 | sess = tf.Session() 33 | init = tf.global_variables_initializer() 34 | sess.run(init) 35 | 36 | for epoch in range(training_epochs): 37 | sess.run(train_op, feed_dict={X: xs, Y: labels}) 38 | current_cost = sess.run(cost, feed_dict={X: xs, Y: labels}) 39 | if epoch % 100 == 0: 40 | print(epoch, current_cost) 41 | 42 | w_val = sess.run(w) 43 | print('learned parameters', w_val) 44 | 45 | print('accuracy', sess.run(accuracy, feed_dict={X: xs, Y: labels})) 46 | 47 | sess.close() 48 | 49 | all_xs = np.linspace(0, 10, 100) 50 | plt.plot(all_xs, all_xs*w_val[1] + w_val[0]) 51 | plt.scatter(xs, labels) 52 | plt.show() 53 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter04/E01_linear_regression_classification.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}{\f1\fnil\fcharset0 MS Sans Serif;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 1\b0 : Concept 01 - Linear regression\par 4 | \b\fs22\par 5 | Line 2-4\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Import the usual libraries\par 8 | 9 | \pard\sl240\slmult1\b Line 6-11\b0\par 10 | 11 | \pard\sa200\sl276\slmult1 Let's say we have numbers that we want to classify. They'll just be 1-dimensional values. Numbers close to 5 will be given the label [0], and numbers close to 2 will be given the label [1], as designed here.\par 12 | 13 | \pard\sl240\slmult1\b Line 13-19\b0\par 14 | 15 | \pard\sa200\sl276\slmult1 Define the hyper-parameters, placeholders, and variables,\par 16 | 17 | \pard\sl240\slmult1\b Line 21-23\b0\par 18 | Define the model.\par 19 | \par 20 | \b Line 25-26\b0\par 21 | 22 | \pard\sa200\sl276\slmult1 Given a model, define the cost function.\par 23 | 24 | \pard\sl240\slmult1\b Line 28-30\b0\par 25 | Set up the training op, and also introduce a couple ops to calculate some metrics, such as accuracy.\par 26 | \b\par 27 | Line 32-34\b0\par 28 | Prepare the session\par 29 | \b\par 30 | Line 36-40\b0\par 31 | Run the training op multiple times on the input data.\par 32 | \b\par 33 | Line 42-47\b0\par 34 | Show some final metrics/results:\par 35 | 36 | \pard\li720\f1\fs16\lang1031 learned parameters [ 1.23912168 -0.19643714]\par 37 | accuracy 0.95\par 38 | 39 | \pard\li720\sl240\slmult1\f0\fs22\lang7\par 40 | 41 | \pard\sl240\slmult1\b Line 49-52\b0\par 42 | Plot the learned function.\par 43 | \par 44 | 45 | \pard\sa200\sl276\slmult1\par 46 | } 47 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter04/E02_logistic_regression.py: -------------------------------------------------------------------------------- 1 | # 2 | import numpy as np 3 | import tensorflow as tf 4 | import matplotlib.pyplot as plt 5 | 6 | learning_rate = 0.01 7 | training_epochs = 1000 8 | 9 | x1 = np.random.normal(-4, 2, 1000) 10 | x2 = np.random.normal(4, 2, 1000) 11 | xs = np.append(x1, x2) 12 | ys = np.asarray([0.] * len(x1) + [1.] * len(x2)) 13 | 14 | plt.scatter(xs, ys) 15 | 16 | X = tf.placeholder(tf.float32, shape=(None,), name="x") 17 | Y = tf.placeholder(tf.float32, shape=(None,), name="y") 18 | w = tf.Variable([0., 0.], name="parameter", trainable=True) 19 | y_model = tf.sigmoid(w[1] * X + w[0]) 20 | cost = tf.reduce_mean(-Y * tf.log(y_model) - (1 - Y) * tf.log(1 - y_model)) 21 | 22 | train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 23 | 24 | with tf.Session() as sess: 25 | sess.run(tf.global_variables_initializer()) 26 | prev_err = 0 27 | for epoch in range(training_epochs): 28 | err, _ = sess.run([cost, train_op], {X: xs, Y: ys}) 29 | if epoch % 100 == 0: 30 | print(epoch, err) 31 | if abs(prev_err - err) < 0.0001: 32 | break 33 | prev_err = err 34 | w_val = sess.run(w, {X: xs, Y: ys}) 35 | 36 | all_xs = np.linspace(-10, 10, 100) 37 | with tf.Session() as sess: 38 | predicted_vals = sess.run(tf.sigmoid(all_xs * w_val[1] + w_val[0])) 39 | plt.plot(all_xs, predicted_vals) 40 | plt.scatter(xs, ys) 41 | plt.show() 42 | 43 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter04/E02_logistic_regression.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 2\b0 : Concept 02 - Logistic regression\par 4 | \b\fs22\par 5 | Line 2-7\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Import the usual libraries, and set up the usual hyper-parameters.\par 8 | 9 | \pard\sl240\slmult1\b Line 9-14\b0\par 10 | Set up some data to work with:\par 11 | \par 12 | \b Line 16-22\b0\par 13 | Define the placeholders, variables, model, cost function, and training op.\par 14 | \par 15 | \b Line 24-34\b0\par 16 | Train the logistic model on the data.\par 17 | \par 18 | \b Line 36-41\b0\par 19 | 20 | \pard\sa200\sl276\slmult1 Now let's see how well our logistic function matched the training data points.\par 21 | 22 | \pard\sl240\slmult1\b Line 43-30\b0\par 23 | Now let's see how well our logistic function matched the training data points.\par 24 | 25 | \pard\sa200\sl276\slmult1\par 26 | } 27 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter04/E03_logistic_regression_2d.py: -------------------------------------------------------------------------------- 1 | # 2 | import numpy as np 3 | import tensorflow as tf 4 | import matplotlib.pyplot as plt 5 | 6 | learning_rate = 0.1 7 | training_epochs = 2000 8 | 9 | x1_label1 = np.random.normal(3, 1, 1000) 10 | x2_label1 = np.random.normal(2, 1, 1000) 11 | x1_label2 = np.random.normal(7, 1, 1000) 12 | x2_label2 = np.random.normal(6, 1, 1000) 13 | x1s = np.append(x1_label1, x1_label2) 14 | x2s = np.append(x2_label1, x2_label2) 15 | ys = np.asarray([0.] * len(x1_label1) + [1.] * len(x1_label2)) 16 | 17 | X1 = tf.placeholder(tf.float32, shape=(None,), name="x1") 18 | X2 = tf.placeholder(tf.float32, shape=(None,), name="x2") 19 | Y = tf.placeholder(tf.float32, shape=(None,), name="y") 20 | w = tf.Variable([0., 0., 0.], name="w", trainable=True) 21 | 22 | y_model = tf.sigmoid(-(w[2] * X2 + w[1] * X1 + w[0])) 23 | cost = tf.reduce_mean(-tf.log(y_model * Y + (1 - y_model) * (1 - Y))) 24 | train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 25 | 26 | with tf.Session() as sess: 27 | sess.run(tf.global_variables_initializer()) 28 | prev_err = 0 29 | for epoch in range(training_epochs): 30 | err, _ = sess.run([cost, train_op], {X1: x1s, X2: x2s, Y: ys}) 31 | if epoch % 100 == 0: 32 | print(epoch, err) 33 | if abs(prev_err - err) < 0.0001: 34 | break 35 | prev_err = err 36 | 37 | w_val = sess.run(w, {X1: x1s, X2: x2s, Y: ys}) 38 | 39 | x1_boundary, x2_boundary = [], [] 40 | with tf.Session() as sess: 41 | for x1_test in np.linspace(0, 10, 20): 42 | for x2_test in np.linspace(0, 10, 20): 43 | z = sess.run(tf.sigmoid(-x2_test*w_val[2] - x1_test*w_val[1] - w_val[0])) 44 | if abs(z - 0.5) < 0.05: 45 | x1_boundary.append(x1_test) 46 | x2_boundary.append(x2_test) 47 | 48 | plt.scatter(x1_boundary, x2_boundary, c='b', marker='o', s=20) 49 | plt.scatter(x1_label1, x2_label1, c='r', marker='x', s=20) 50 | plt.scatter(x1_label2, x2_label2, c='g', marker='1', s=20) 51 | 52 | plt.show() -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter04/E04_softmax classification.py: -------------------------------------------------------------------------------- 1 | # 2 | import numpy as np 3 | import tensorflow as tf 4 | import matplotlib.pyplot as plt 5 | 6 | learning_rate = 0.01 7 | training_epochs = 1000 8 | num_labels = 3 9 | batch_size = 100 10 | 11 | x1_label0 = np.random.normal(1, 1, (100, 1)) 12 | x2_label0 = np.random.normal(1, 1, (100, 1)) 13 | x1_label1 = np.random.normal(5, 1, (100, 1)) 14 | x2_label1 = np.random.normal(4, 1, (100, 1)) 15 | x1_label2 = np.random.normal(8, 1, (100, 1)) 16 | x2_label2 = np.random.normal(0, 1, (100, 1)) 17 | 18 | plt.scatter(x1_label0, x2_label0, c='r', marker='o', s=60) 19 | plt.scatter(x1_label1, x2_label1, c='g', marker='x', s=60) 20 | plt.scatter(x1_label2, x2_label2, c='b', marker='_', s=60) 21 | plt.show() 22 | 23 | xs_label0 = np.hstack((x1_label0, x2_label0)) 24 | xs_label1 = np.hstack((x1_label1, x2_label1)) 25 | xs_label2 = np.hstack((x1_label2, x2_label2)) 26 | 27 | xs = np.vstack((xs_label0, xs_label1, xs_label2)) 28 | labels = np.matrix([[1., 0., 0.]] * len(x1_label0) + [[0., 1., 0.]] * len(x1_label1) + [[0., 0., 1.]] * len(x1_label2)) 29 | 30 | arr = np.arange(xs.shape[0]) 31 | np.random.shuffle(arr) 32 | xs = xs[arr, :] 33 | labels = labels[arr, :] 34 | 35 | test_x1_label0 = np.random.normal(1, 1, (10, 1)) 36 | test_x2_label0 = np.random.normal(1, 1, (10, 1)) 37 | test_x1_label1 = np.random.normal(5, 1, (10, 1)) 38 | test_x2_label1 = np.random.normal(4, 1, (10, 1)) 39 | test_x1_label2 = np.random.normal(8, 1, (10, 1)) 40 | test_x2_label2 = np.random.normal(0, 1, (10, 1)) 41 | test_xs_label0 = np.hstack((test_x1_label0, test_x2_label0)) 42 | test_xs_label1 = np.hstack((test_x1_label1, test_x2_label1)) 43 | test_xs_label2 = np.hstack((test_x1_label2, test_x2_label2)) 44 | 45 | test_xs = np.vstack((test_xs_label0, test_xs_label1, test_xs_label2)) 46 | test_labels = np.matrix([[1., 0., 0.]] * 10 + [[0., 1., 0.]] * 10 + [[0., 0., 1.]] * 10) 47 | 48 | train_size, num_features = xs.shape 49 | 50 | X = tf.placeholder("float", shape=[None, num_features]) 51 | Y = tf.placeholder("float", shape=[None, num_labels]) 52 | 53 | W = tf.Variable(tf.zeros([num_features, num_labels])) 54 | b = tf.Variable(tf.zeros([num_labels])) 55 | y_model = tf.nn.softmax(tf.matmul(X, W) + b) 56 | 57 | cost = -tf.reduce_sum(Y * tf.log(y_model)) 58 | train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) 59 | 60 | correct_prediction = tf.equal(tf.argmax(y_model, 1), tf.argmax(Y, 1)) 61 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 62 | 63 | with tf.Session() as sess: 64 | tf.global_variables_initializer().run() 65 | 66 | for step in range(training_epochs * train_size // batch_size): 67 | offset = (step * batch_size) % train_size 68 | batch_xs = xs[offset:(offset + batch_size), :] 69 | batch_labels = labels[offset:(offset + batch_size)] 70 | err, _ = sess.run([cost, train_op], feed_dict={X: batch_xs, Y: batch_labels}) 71 | if step % 100 == 0: 72 | print (step, err) 73 | 74 | W_val = sess.run(W) 75 | print('w', W_val) 76 | b_val = sess.run(b) 77 | print('b', b_val) 78 | print("accuracy", accuracy.eval(feed_dict={X: test_xs, Y: test_labels})) 79 | 80 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter04/E04_softmax classification.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 4\b0 : Softmax classification\par 4 | \b\fs22\par 5 | Line 2-4\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Import the usual libraries.\par 8 | 9 | \pard\sl240\slmult1\b Line 6-21\b0\par 10 | Generated some initial 2D data\par 11 | \par 12 | \b Line 23-28\b0\par 13 | 14 | \pard\sa200\sl276\slmult1 Define the labels and shuffle the data.\par 15 | 16 | \pard\sl240\slmult1\b Line 30-46\b0\par 17 | We'll get back to this later, but the following are test inputs that we'll use to evaluate the model.\par 18 | \par 19 | \b Line 48-61\b0\par 20 | 21 | \pard\sa200\sl276\slmult1 Again, define the placeholders, variables, model, and cost function.\par 22 | 23 | \pard\sl240\slmult1\b Line 63-78\b0\par 24 | Train the softmax classification model.\par 25 | 26 | \pard\sa200\sl276\slmult1\par 27 | } 28 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter05/E00_overview.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\colortbl ;\red0\green0\blue255;} 3 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 4 | \pard\sl240\slmult1\b\f0\fs28\lang7 Machine Learning with TensorFlow - Chapter 05\fs24\par 5 | {\fs22{\field{\*\fldinst{HYPERLINK https://github.com/BinRoot/TensorFlow-Book/tree/master/ch05_clustering }}{\fldrslt{https://github.com/BinRoot/TensorFlow-Book/tree/master/ch05_clustering\ul0\cf0}}}}\f0\fs22\par 6 | \par 7 | \i\fs20 Requirements:\par 8 | {{\field{\*\fldinst{HYPERLINK https://github.com/hartmutdavid/BregmanToolkit }}{\fldrslt{https://github.com/hartmutdavid/BregmanToolkit\ul0\cf0}}}}\f0\fs20 \b0 for Python 3.x\par 9 | \b\par 10 | CMD-Commands:\par 11 | \b0 Please check: >pip show scipy\par 12 | If not installed : >pip3 install --upgrade scipy\par 13 | \par 14 | Please check: >pip show image\par 15 | If not installed : >pip3 install --upgrade image\i0\fs22\par 16 | \b\par 17 | Example 1:\b0\par 18 | 19 | \pard\sa200\sl276\slmult1 Clustering\par 20 | 21 | \pard\sl240\slmult1\b Example 2:\b0\par 22 | 23 | \pard\sa200\sl276\slmult1 Segmentation\par 24 | 25 | \pard\sl240\slmult1\b Example 3:\b0\par 26 | 27 | \pard\sa200\sl276\slmult1 Self-organizing map\par 28 | } 29 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter05/E01_clustering.py: -------------------------------------------------------------------------------- 1 | # Failed with Python 3.x !!! 2 | import tensorflow as tf 3 | import numpy as np 4 | from bregman.suite import * 5 | 6 | k = 2 7 | max_iterations = 100 8 | 9 | # filenames = tf.train.match_filenames_once('*.wav') 10 | filenames = ['audio_cough_1.wav'] 11 | count_num_files = tf.size(filenames) 12 | filename_queue = tf.train.string_input_producer(filenames) 13 | reader = tf.WholeFileReader() 14 | filename, file_contents = reader.read(filename_queue) 15 | 16 | chromo = tf.placeholder(tf.float32) 17 | max_freqs = tf.argmax(chromo, 0) 18 | 19 | def get_next_chromogram(sess): 20 | audio_file = sess.run(filename) 21 | print('Loading {}'.format(audio_file)) 22 | F = Chromagram(audio_file, nfft=16384, wfft=8192, nhop=2205) 23 | return F.x, audio_file 24 | 25 | def extract_feature_vector(sess, chromo_data): 26 | num_features, num_samples = np.shape(chromo_data) 27 | freq_vals = sess.run(max_freqs, feed_dict={chromo: chromo_data}) 28 | hist, bins = np.histogram(freq_vals, bins=range(num_features + 1)) 29 | normalized_hist = hist.astype(float) / num_samples 30 | return normalized_hist 31 | 32 | def get_dataset(sess): 33 | num_files = sess.run(count_num_files) 34 | coord = tf.train.Coordinator() 35 | threads = tf.train.start_queue_runners(coord=coord) 36 | xs = list() 37 | names = list() 38 | 39 | for _ in range(num_files): 40 | plt.figure() 41 | chromo_data, filename = get_next_chromogram(sess) 42 | 43 | plt.subplot(1, 2, 1) 44 | #DAV>>> Failed!!! plt.imshow(chromo_data, cmap='Greys', interpolation='nearest') 45 | plt.title('Visualization of Sound Spectrum') 46 | 47 | plt.subplot(1, 2, 2) 48 | freq_vals = sess.run(max_freqs, feed_dict={chromo: chromo_data}) 49 | plt.hist(freq_vals) 50 | plt.title('Histogram of Notes') 51 | plt.xlabel('Musical Note') 52 | plt.ylabel('Count') 53 | plt.savefig('{}.png'.format(filename)) 54 | plt.show() 55 | 56 | names.append(filename) 57 | x = extract_feature_vector(sess, chromo_data) 58 | xs.append(x) 59 | xs = np.asmatrix(xs) 60 | coord.request_stop() 61 | coord.join(threads) 62 | return xs, names 63 | 64 | def initial_cluster_centroids(X, k): 65 | return X[0:k, :] 66 | 67 | def assign_cluster(X, centroids): 68 | expanded_vectors = tf.expand_dims(X, 0) 69 | expanded_centroids = tf.expand_dims(centroids, 1) 70 | distances = tf.reduce_sum(tf.square(tf.subtract(expanded_vectors, expanded_centroids)), 2) 71 | mins = tf.argmin(distances, 0) 72 | return mins 73 | 74 | def recompute_centroids(X, Y): 75 | sums = tf.unsorted_segment_sum(X, Y, k) 76 | counts = tf.unsorted_segment_sum(tf.ones_like(X), Y, k) 77 | return sums / counts 78 | 79 | with tf.Session() as sess: 80 | sess.run(tf.global_variables_initializer()) 81 | X, names = get_dataset(sess) 82 | centroids = initial_cluster_centroids(X, k) 83 | i, converged = 0, False 84 | while not converged and i < max_iterations: 85 | i += 1 86 | Y = assign_cluster(X, centroids) 87 | centroids = sess.run(recompute_centroids(X, Y)) 88 | print(zip(sess.run(Y), names)) 89 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter05/E01_clustering.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 1\b0 : Clustering\par 4 | \b\fs22\par 5 | Line 2-4\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Import TensorFlow and bregman.suite.\par 8 | 9 | \pard\sl240\slmult1\b Line 6-7\b0\par 10 | 11 | \pard\sa200\sl276\slmult1 Define some hyper-parameters for clustering.\par 12 | 13 | \pard\sl240\slmult1\b Line 9-17\b0\par 14 | 15 | \pard\sa200\sl276\slmult1 Select the location for the audio files.\par 16 | 17 | \pard\sl240\slmult1\b Line 19-23\b0\par 18 | Create a helper function to get the next audio file's Chromogram.\par 19 | \par 20 | \b Line 25-30\b0\par 21 | 22 | \pard\sa200\sl276\slmult1 And create a helper function to extract a feature vector from the Chromogram data. \par 23 | 24 | \pard\sl240\slmult1\b Line 32-62\b0\par 25 | Extract a dataset of feature vectors by calling our helper functions above.\par 26 | \b\par 27 | Line 64-77\b0\par 28 | k-means clustering requires 2 main phases: clustering and assignment. We'll also throw in an initialization helper function for good measure. Here are the three components.\par 29 | \b\par 30 | Line 79-88\b0\par 31 | Open a session, obtain a dataset, and cluster the data. \par 32 | \b\par 33 | } 34 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter05/E02_segmentation.py: -------------------------------------------------------------------------------- 1 | # 2 | import tensorflow as tf 3 | import numpy as np 4 | from bregman.suite import * 5 | 6 | k = 2 7 | segment_size = 50 8 | max_iterations = 100 9 | 10 | chromo = tf.placeholder(tf.float32) 11 | max_freqs = tf.argmax(chromo, 0) 12 | 13 | def get_chromogram(audio_file): 14 | F = Chromagram(audio_file, nfft=16384, wfft=8192, nhop=2205) 15 | return F.X 16 | 17 | def get_dataset(sess, audio_file): 18 | chromo_data = get_chromogram(audio_file) 19 | print('chromo_data', np.shape(chromo_data)) 20 | chromo_length = np.shape(chromo_data)[1] 21 | print(' chromo_length=', chromo_length, 'segment_size=', segment_size) 22 | xs = [] 23 | for i in range(chromo_length//segment_size): 24 | chromo_segment = chromo_data[:, i*segment_size:(i+1)*segment_size] 25 | x = extract_feature_vector(sess, chromo_segment) 26 | if len(xs) == 0: 27 | xs = x 28 | else: 29 | xs = np.vstack((xs, x)) 30 | return xs 31 | 32 | def initial_cluster_centroids(X, k): 33 | return X[0:k, :] 34 | 35 | def assign_cluster(X, centroids): 36 | expanded_vectors = tf.expand_dims(X, 0) 37 | expanded_centroids = tf.expand_dims(centroids, 1) 38 | distances = tf.reduce_sum(tf.square(tf.subtract(expanded_vectors, expanded_centroids)), 2) 39 | mins = tf.argmin(distances, 0) 40 | return mins 41 | 42 | def recompute_centroids(X, Y): 43 | sums = tf.unsorted_segment_sum(X, Y, k) 44 | counts = tf.unsorted_segment_sum(tf.ones_like(X), Y, k) 45 | return sums / counts 46 | 47 | def extract_feature_vector(sess, chromo_data): 48 | num_features, num_samples = np.shape(chromo_data) 49 | freq_vals = sess.run(max_freqs, feed_dict={chromo: chromo_data}) 50 | hist, bins = np.histogram(freq_vals, bins=range(num_features + 1)) 51 | return hist.astype(float) / num_samples 52 | 53 | with tf.Session() as sess: 54 | X = get_dataset(sess, 'TalkingMachinesPodcast.wav') 55 | print(np.shape(X)) 56 | centroids = initial_cluster_centroids(X, k) 57 | i, converged = 0, False 58 | # prev_Y = None 59 | while not converged and i < max_iterations: 60 | i += 1 61 | Y = assign_cluster(X, centroids) 62 | # if prev_Y == Y: 63 | # converged = True 64 | # break 65 | # prev_Y = Y 66 | centroids = sess.run(recompute_centroids(X, Y)) 67 | if i % 50 == 0: 68 | print('iteration', i) 69 | segments = sess.run(Y) 70 | for i in range(len(segments)): 71 | seconds = (i * segment_size) / float(20) 72 | min, sec = divmod(seconds, 60) 73 | time_str = str(min) + 'm ' + str(sec) + 's' 74 | print(time_str, segments[i]) 75 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter05/E02_segmentation.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 2\b0 : Segmentation\par 4 | \b\fs22\par 5 | Line 2-8\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Import libraries and define hyper-parameters.\par 8 | 9 | \pard\sl240\slmult1\b Line 10-29\b0\par 10 | Define functions to get the chromogram and the dataset\par 11 | \par 12 | \b Line 31-44\b0\par 13 | 14 | \pard\sa200\sl276\slmult1 As required for the k-means algorithm, specify the assignment and re-centering code \par 15 | 16 | \pard\sl240\slmult1\b Line 46-50\b0\par 17 | Given a chromogram, extract a histogram of sound frequencies as our feature vector \par 18 | \par 19 | \b Line 52-73\b0\par 20 | 21 | \pard\sa200\sl276\slmult1 In a session, segment an audio file using k-means \par 22 | \b\par 23 | } 24 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter05/E03_self_org_map.py: -------------------------------------------------------------------------------- 1 | # 2 | import tensorflow as tf 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | 6 | class SOM: 7 | 8 | def __init__(self, width, height, dim): 9 | self.num_iters = 100 10 | self.width = width 11 | self.height = height 12 | self.dim = dim 13 | self.node_locs = self.get_locs() 14 | 15 | # Each node is a vector of dimension `dim` 16 | # For a 2D grid, there are `width * height` nodes 17 | nodes = tf.Variable(tf.random_normal([width*height, dim])) 18 | self.nodes = nodes 19 | 20 | # These two ops are inputs at each iteration 21 | x = tf.placeholder(tf.float32, [dim]) 22 | iter = tf.placeholder(tf.float32) 23 | 24 | self.x = x 25 | self.iter = iter 26 | 27 | # Find the node that matches closest to the input 28 | bmu_loc = self.get_bmu_loc(x) 29 | 30 | self.propagate_nodes = self.get_propagation(bmu_loc, x, iter) 31 | 32 | def get_propagation(self, bmu_loc, x, iter): 33 | ''' 34 | Define the weight propagation function that will update weights of the best matching unit (BMU). 35 | The intensity of weight updates decreases over time, as dictated by the `iter` variable. 36 | ''' 37 | num_nodes = self.width * self.height 38 | rate = 1.0 - tf.div(iter, self.num_iters) 39 | alpha = rate * 0.5 40 | sigma = rate * tf.to_float(tf.maximum(self.width, self.height)) / 2. 41 | expanded_bmu_loc = tf.expand_dims(tf.to_float(bmu_loc), 0) 42 | sqr_dists_from_bmu = tf.reduce_sum(tf.square(tf.subtract(expanded_bmu_loc, self.node_locs)), 1) 43 | neigh_factor = tf.exp(-tf.div(sqr_dists_from_bmu, 2 * tf.square(sigma))) 44 | rate = tf.multiply(alpha, neigh_factor) 45 | rate_factor = tf.stack([tf.tile(tf.slice(rate, [i], [1]), [self.dim]) for i in range(num_nodes)]) 46 | nodes_diff = tf.multiply(rate_factor, tf.subtract(tf.stack([x for i in range(num_nodes)]), self.nodes)) 47 | update_nodes = tf.add(self.nodes, nodes_diff) 48 | return tf.assign(self.nodes, update_nodes) 49 | 50 | def get_bmu_loc(self, x): 51 | ''' 52 | Define a helper function to located the BMU: 53 | ''' 54 | expanded_x = tf.expand_dims(x, 0) 55 | sqr_diff = tf.square(tf.subtract(expanded_x, self.nodes)) 56 | dists = tf.reduce_sum(sqr_diff, 1) 57 | bmu_idx = tf.argmin(dists, 0) 58 | bmu_loc = tf.stack([tf.mod(bmu_idx, self.width), tf.div(bmu_idx, self.width)]) 59 | return bmu_loc 60 | 61 | def get_locs(self): 62 | ''' 63 | Build a grid of nodes: 64 | ''' 65 | locs = [[x, y] 66 | for y in range(self.height) 67 | for x in range(self.width)] 68 | return tf.to_float(locs) 69 | 70 | def train(self, data): 71 | ''' 72 | Define a function to training the SOM on a given dataset: 73 | ''' 74 | with tf.Session() as sess: 75 | sess.run(tf.global_variables_initializer()) 76 | for i in range(self.num_iters): 77 | for data_x in data: 78 | sess.run(self.propagate_nodes, feed_dict={self.x: data_x, self.iter: i}) 79 | centroid_grid = [[] for i in range(self.width)] 80 | self.nodes_val = list(sess.run(self.nodes)) 81 | self.locs_val = list(sess.run(self.node_locs)) 82 | for i, l in enumerate(self.locs_val): 83 | centroid_grid[int(l[0])].append(self.nodes_val[i]) 84 | self.centroid_grid = centroid_grid 85 | 86 | 87 | colors = np.array( 88 | [[0., 0., 1.], 89 | [0., 0., 0.95], 90 | [0., 0.05, 1.], 91 | [0., 1., 0.], 92 | [0., 0.95, 0.], 93 | [0., 1, 0.05], 94 | [1., 0., 0.], 95 | [1., 0.05, 0.], 96 | [1., 0., 0.05], 97 | [1., 1., 0.]]) 98 | 99 | som = SOM(4, 4, 3) 100 | som.train(colors) 101 | 102 | plt.imshow(som.centroid_grid) 103 | plt.show() -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter05/E03_self_org_map.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 3\b0 : Self-organizing map\par 4 | \b\fs22\par 5 | Line 2-4\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Imports\par 8 | 9 | \pard\sl240\slmult1\b Line 6-84\b0\par 10 | Define a class called SOM. The constructor builds a grid of nodes, and also defines some helper ops\par 11 | \par 12 | \b Line 87-103\b0\par 13 | 14 | \pard\sa200\sl276\slmult1 Time to use our newfound powers. Let's test it out on some data\par 15 | \b\par 16 | } 17 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter06/E00_overview.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\colortbl ;\red0\green0\blue255;} 3 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 4 | \pard\sl240\slmult1\b\f0\fs28\lang7 Machine Learning with TensorFlow - Chapter 06\fs24\par 5 | {\fs22{\field{\*\fldinst{HYPERLINK https://github.com/BinRoot/TensorFlow-Book/tree/master/ch06_hmm }}{\fldrslt{https://github.com/BinRoot/TensorFlow-Book/tree/master/ch06_hmm\ul0\cf0}}}}\f0\fs24\par 6 | \fs22\par 7 | \par 8 | Example 1:\b0\par 9 | 10 | \pard\sa200\sl276\slmult1 Forward algorithm\par 11 | 12 | \pard\sl240\slmult1\b Example 2:\b0\par 13 | 14 | \pard\sa200\sl276\slmult1 Viterbi decode\par 15 | \b\par 16 | } 17 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter06/E01_forward_algorithm.py: -------------------------------------------------------------------------------- 1 | # 2 | import numpy as np 3 | import tensorflow as tf 4 | 5 | class HMM(object): 6 | def __init__(self, initial_prob, trans_prob, obs_prob): 7 | self.N = np.size(initial_prob) 8 | self.initial_prob = initial_prob 9 | self.trans_prob = trans_prob 10 | self.emission = tf.constant(obs_prob) 11 | 12 | assert self.initial_prob.shape == (self.N, 1) 13 | assert self.trans_prob.shape == (self.N, self.N) 14 | assert obs_prob.shape[0] == self.N 15 | 16 | self.obs_idx = tf.placeholder(tf.int32) 17 | self.fwd = tf.placeholder(tf.float64) 18 | 19 | def get_emission(self, obs_idx): 20 | slice_location = [0, obs_idx] 21 | num_rows = tf.shape(self.emission)[0] 22 | slice_shape = [num_rows, 1] 23 | return tf.slice(self.emission, slice_location, slice_shape) 24 | 25 | def forward_init_op(self): 26 | obs_prob = self.get_emission(self.obs_idx) 27 | fwd = tf.multiply(self.initial_prob, obs_prob) 28 | return fwd 29 | 30 | def forward_op(self): 31 | transitions = tf.matmul(self.fwd, tf.transpose(self.get_emission(self.obs_idx))) 32 | weighted_transitions = transitions * self.trans_prob 33 | fwd = tf.reduce_sum(weighted_transitions, 0) 34 | return tf.reshape(fwd, tf.shape(self.fwd)) 35 | 36 | def forward_algorithm(sess, hmm, observations): 37 | fwd = sess.run(hmm.forward_init_op(), feed_dict={hmm.obs_idx: observations[0]}) 38 | for t in range(1, len(observations)): 39 | fwd = sess.run(hmm.forward_op(), feed_dict={hmm.obs_idx: observations[t], hmm.fwd: fwd}) 40 | prob = sess.run(tf.reduce_sum(fwd)) 41 | return prob 42 | 43 | if __name__ == '__main__': 44 | initial_prob = np.array([[0.6], [0.4]]) 45 | trans_prob = np.array([[0.7, 0.3], [0.4, 0.6]]) 46 | obs_prob = np.array([[0.1, 0.4, 0.5], [0.6, 0.3, 0.1]]) 47 | 48 | hmm = HMM(initial_prob=initial_prob, trans_prob=trans_prob, obs_prob=obs_prob) 49 | 50 | observations = [0, 1, 1, 2, 1] 51 | with tf.Session() as sess: 52 | prob = forward_algorithm(sess, hmm, observations) 53 | print('Probability of observing {} is {}'.format(observations, prob)) -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter06/E01_forward_algorithm.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 1\b0 : Hidden Markov model forward algorithm\par 4 | \b\fs22\par 5 | \b0 Of this code's a bit complicated if you don't already know how HMMs work. Please see the book chapter for step-by-step explanations. I'll try to improve the documentation, or feel free to send a pull request with your own documentation!\par 6 | \b\par 7 | Line 2-3\b0\par 8 | 9 | \pard\sa200\sl276\slmult1 First, let's import TensorFlow and NumPy.\par 10 | 11 | \pard\sl240\slmult1\b Line 5-34\b0\par 12 | 13 | \pard\sa200\sl276\slmult1 Define the HMM model.\par 14 | 15 | \pard\sl240\slmult1\b Line 36-41\b0\par 16 | 17 | \pard\sa200\sl276\slmult1 Define the forward algorithm.\par 18 | 19 | \pard\sl240\slmult1\b Line 43-53\b0\par 20 | Let's try it out.\par 21 | \par 22 | \b\par 23 | } 24 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter06/E02_viterbi_decode.py: -------------------------------------------------------------------------------- 1 | # 2 | import numpy as np 3 | import tensorflow as tf 4 | 5 | # initial parameters can be learned on training data 6 | # theory reference https://web.stanford.edu/~jurafsky/slp3/8.pdf 7 | # code reference https://phvu.net/2013/12/06/sweet-implementation-of-viterbi-in-python/ 8 | class HMM(object): 9 | def __init__(self, initial_prob, trans_prob, obs_prob): 10 | self.N = np.size(initial_prob) 11 | self.initial_prob = initial_prob 12 | self.trans_prob = trans_prob 13 | self.obs_prob = obs_prob 14 | self.emission = tf.constant(obs_prob) 15 | assert self.initial_prob.shape == (self.N, 1) 16 | assert self.trans_prob.shape == (self.N, self.N) 17 | assert self.obs_prob.shape[0] == self.N 18 | self.obs = tf.placeholder(tf.int32) 19 | self.fwd = tf.placeholder(tf.float64) 20 | self.viterbi = tf.placeholder(tf.float64) 21 | 22 | def get_emission(self, obs_idx): 23 | slice_location = [0, obs_idx] 24 | num_rows = tf.shape(self.emission)[0] 25 | slice_shape = [num_rows, 1] 26 | return tf.slice(self.emission, slice_location, slice_shape) 27 | 28 | def forward_init_op(self): 29 | obs_prob = self.get_emission(self.obs) 30 | fwd = tf.multiply(self.initial_prob, obs_prob) 31 | return fwd 32 | 33 | def forward_op(self): 34 | transitions = tf.matmul(self.fwd, tf.transpose(self.get_emission(self.obs))) 35 | weighted_transitions = transitions * self.trans_prob 36 | fwd = tf.reduce_sum(weighted_transitions, 0) 37 | return tf.reshape(fwd, tf.shape(self.fwd)) 38 | 39 | def decode_op(self): 40 | transitions = tf.matmul(self.viterbi, tf.transpose(self.get_emission(self.obs))) 41 | weighted_transitions = transitions * self.trans_prob 42 | viterbi = tf.reduce_max(weighted_transitions, 0) 43 | return tf.reshape(viterbi, tf.shape(self.viterbi)) 44 | 45 | def backpt_op(self): 46 | back_transitions = tf.matmul(self.viterbi, np.ones((1, self.N))) 47 | weighted_back_transitions = back_transitions * self.trans_prob 48 | return tf.argmax(weighted_back_transitions, 0) 49 | 50 | def forward_algorithm(sess, hmm, observations): 51 | fwd = sess.run(hmm.forward_init_op(), feed_dict={hmm.obs: observations[0]}) 52 | for t in range(1, len(observations)): 53 | fwd = sess.run(hmm.forward_op(), feed_dict={hmm.obs: observations[t], hmm.fwd: fwd}) 54 | prob = sess.run(tf.reduce_sum(fwd)) 55 | return prob 56 | 57 | def viterbi_decode(sess, hmm, observations): 58 | viterbi = sess.run(hmm.forward_init_op(), feed_dict={hmm.obs: observations[0]}) 59 | backpts = np.ones((hmm.N, len(observations)), 'int32') * -1 60 | for t in range(1, len(observations)): 61 | viterbi, backpt = sess.run([hmm.decode_op(), hmm.backpt_op()], 62 | feed_dict={hmm.obs: observations[t], 63 | hmm.viterbi: viterbi}) 64 | backpts[:, t] = backpt 65 | tokens = [viterbi[:, -1].argmax()] 66 | for i in range(len(observations) - 1, 0, -1): 67 | tokens.append(backpts[tokens[-1], i]) 68 | return tokens[::-1] 69 | 70 | if __name__ == '__main__': 71 | states = ('Healthy', 'Fever') 72 | # observations = ('normal', 'cold', 'dizzy') 73 | # start_probability = {'Healthy': 0.6, 'Fever': 0.4} 74 | # transition_probability = { 75 | # 'Healthy': {'Healthy': 0.7, 'Fever': 0.3}, 76 | # 'Fever': {'Healthy': 0.4, 'Fever': 0.6} 77 | # } 78 | # emission_probability = { 79 | # 'Healthy': {'normal': 0.5, 'cold': 0.4, 'dizzy': 0.1}, 80 | # 'Fever': {'normal': 0.1, 'cold': 0.3, 'dizzy': 0.6} 81 | # } 82 | initial_prob = np.array([[0.6], [0.4]]) 83 | trans_prob = np.array([[0.7, 0.3], [0.4, 0.6]]) 84 | obs_prob = np.array([[0.5, 0.4, 0.1], [0.1, 0.3, 0.6]]) 85 | hmm = HMM(initial_prob=initial_prob, trans_prob=trans_prob, obs_prob=obs_prob) 86 | 87 | observations = [0, 1, 1, 2, 1] 88 | with tf.Session() as sess: 89 | prob = forward_algorithm(sess, hmm, observations) 90 | print('Probability of observing {} is {}'.format(observations, prob)) 91 | 92 | seq = viterbi_decode(sess, hmm, observations) 93 | print('Most likely hidden states are {}'.format(seq)) -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter06/E02_viterbi_decode.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 2\b0 : Viterbi parse of a Hidden Markov model\par 4 | \b\fs22\par 5 | Line 2-3\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Import TensorFlow and Numpy.\par 8 | 9 | \pard\sl240\slmult1\b Line 8-48\b0\par 10 | 11 | \pard\sa200\sl276\slmult1 Create the same HMM model as before. This time, we'll include a couple additional functions.\par 12 | 13 | \pard\sl240\slmult1\b Line 50-55\b0\par 14 | 15 | \pard\sa200\sl276\slmult1 Define the forward algorithm from Concept01.\par 16 | 17 | \pard\sl240\slmult1\b Line 57-68\b0\par 18 | Now, let's compute the Viterbi likelihood of the observed sequence.\par 19 | \par 20 | \b Line 70-93\b0\par 21 | 22 | \pard\sa200\sl276\slmult1 Let's try it out on some example data.\par 23 | \b\par 24 | } 25 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter07/E00_overview.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\colortbl ;\red0\green0\blue255;} 3 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 4 | \pard\sl240\slmult1\b\f0\fs28\lang7 Machine Learning with TensorFlow - Chapter 07\fs24\par 5 | {\fs22{\field{\*\fldinst{HYPERLINK https://github.com/BinRoot/TensorFlow-Book/tree/master/ch07_autoencoder }}{\fldrslt{https://github.com/BinRoot/TensorFlow-Book/tree/master/ch07_autoencoder\ul0\cf0}}}}\f0\fs24\par 6 | \fs22\par 7 | \i\fs20 CMD-Commands:\par 8 | \b0 Please check: >pip show sklearn\par 9 | If not installed : >pip3 install --upgrade sklearn\i0\par 10 | \b\fs22\par 11 | Example 1:\b0\par 12 | 13 | \pard\sa200\sl276\slmult1 Autoencoder\par 14 | 15 | \pard\sl240\slmult1\b Example 2:\b0\par 16 | 17 | \pard\sa200\sl276\slmult1 Applying an autoencoder to images\par 18 | 19 | \pard\sl240\slmult1\b Example 3:\b0\par 20 | 21 | \pard\sa200\sl276\slmult1 Denoising autoencoder\par 22 | } 23 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter07/E01_autoencoder.py: -------------------------------------------------------------------------------- 1 | # 2 | import tensorflow as tf 3 | import numpy as np 4 | from sklearn import datasets 5 | 6 | def get_batch(X, size): 7 | a = np.random.choice(len(X), size, replace=False) 8 | return X[a] 9 | 10 | class Autoencoder: 11 | def __init__(self, input_dim, hidden_dim, epoch=500, batch_size=10, learning_rate=0.001): 12 | self.epoch = epoch 13 | self.batch_size = batch_size 14 | self.learning_rate = learning_rate 15 | 16 | # Define input placeholder 17 | x = tf.placeholder(dtype=tf.float32, shape=[None, input_dim]) 18 | 19 | # Define variables 20 | with tf.name_scope('encode'): 21 | weights = tf.Variable(tf.random_normal([input_dim, hidden_dim], dtype=tf.float32), name='weights') 22 | biases = tf.Variable(tf.zeros([hidden_dim]), name='biases') 23 | encoded = tf.nn.sigmoid(tf.matmul(x, weights) + biases) 24 | with tf.name_scope('decode'): 25 | weights = tf.Variable(tf.random_normal([hidden_dim, input_dim], dtype=tf.float32), name='weights') 26 | biases = tf.Variable(tf.zeros([input_dim]), name='biases') 27 | decoded = tf.matmul(encoded, weights) + biases 28 | 29 | self.x = x 30 | self.encoded = encoded 31 | self.decoded = decoded 32 | 33 | # Define cost function and training op 34 | self.loss = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(self.x, self.decoded)))) 35 | 36 | self.all_loss = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(self.x, self.decoded)), 1)) 37 | self.train_op = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss) 38 | 39 | # Define a saver op 40 | self.saver = tf.train.Saver() 41 | 42 | def train(self, data): 43 | with tf.Session() as sess: 44 | sess.run(tf.global_variables_initializer()) 45 | for i in range(self.epoch): 46 | for j in range(500): 47 | batch_data = get_batch(data, self.batch_size) 48 | l, _ = sess.run([self.loss, self.train_op], feed_dict={self.x: batch_data}) 49 | if i % 50 == 0: 50 | print('epoch {0}: loss = {1}'.format(i, l)) 51 | self.saver.save(sess, './model.ckpt') 52 | self.saver.save(sess, './model.ckpt') 53 | 54 | def test(self, data): 55 | with tf.Session() as sess: 56 | self.saver.restore(sess, './model.ckpt') 57 | hidden, reconstructed = sess.run([self.encoded, self.decoded], feed_dict={self.x: data}) 58 | print('input', data) 59 | print('compressed', hidden) 60 | print('reconstructed', reconstructed) 61 | return reconstructed 62 | 63 | def get_params(self): 64 | with tf.Session() as sess: 65 | self.saver.restore(sess, './model.ckpt') 66 | weights, biases = sess.run([self.weights1, self.biases1]) 67 | return weights, biases 68 | 69 | def classify(self, data, labels): 70 | with tf.Session() as sess: 71 | sess.run(tf.global_variables_initializer()) 72 | self.saver.restore(sess, './model.ckpt') 73 | hidden, reconstructed = sess.run([self.encoded, self.decoded], feed_dict={self.x: data}) 74 | reconstructed = reconstructed[0] 75 | # loss = sess.run(self.all_loss, feed_dict={self.x: data}) 76 | print('data', np.shape(data)) 77 | print('reconstructed', np.shape(reconstructed)) 78 | loss = np.sqrt(np.mean(np.square(data - reconstructed), axis=1)) 79 | print('loss', np.shape(loss)) 80 | horse_indices = np.where(labels == 7)[0] 81 | not_horse_indices = np.where(labels != 7)[0] 82 | horse_loss = np.mean(loss[horse_indices]) 83 | not_horse_loss = np.mean(loss[not_horse_indices]) 84 | print('horse', horse_loss) 85 | print('not horse', not_horse_loss) 86 | return hidden[7,:] 87 | 88 | def decode(self, encoding): 89 | with tf.Session() as sess: 90 | sess.run(tf.global_variables_initializer()) 91 | self.saver.restore(sess, './model.ckpt') 92 | reconstructed = sess.run(self.decoded, feed_dict={self.encoded: encoding}) 93 | img = np.reshape(reconstructed, (32, 32)) 94 | return img 95 | 96 | hidden_dim = 1 97 | data = datasets.load_iris().data 98 | input_dim = len(data[0]) 99 | ae = Autoencoder(input_dim, hidden_dim) 100 | ae.train(data) 101 | ae.test([[8, 4, 6, 2]]) 102 | 103 | 104 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter07/E01_autoencoder.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 1\b0 : Concept 01 - Linear regression\par 4 | \b\fs22\par 5 | Line 2-4\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 Imports.\par 8 | 9 | \pard\sl240\slmult1\b Line 6-8\b0\par 10 | 11 | \pard\sa200\sl276\slmult1 Instead of feeding all the training data to the training op, we will feed data in small batches.\par 12 | 13 | \pard\sl240\slmult1\b Line 10-94\b0\par 14 | 15 | \pard\sa200\sl276\slmult1 Define the autoencoder class.\par 16 | 17 | \pard\sl240\slmult1\b Line 96-101\b0\par 18 | The Iris dataset is often used as a simple training dataset to check whether a classification algorithm is working. \par 19 | \par 20 | \b\par 21 | } 22 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter07/E02_autoencoder_with_images.py: -------------------------------------------------------------------------------- 1 | # 2 | from matplotlib import pyplot as plt 3 | import pickle 4 | import numpy as np 5 | from autoencoder import Autoencoder 6 | 7 | def unpickle(file): 8 | fo = open(file, 'rb') 9 | dict = pickle.load(fo, encoding='latin1') 10 | fo.close() 11 | return dict 12 | 13 | def grayscale(a): 14 | return a.reshape(a.shape[0], 3, 32, 32).mean(1).reshape(a.shape[0], -1) 15 | 16 | names = unpickle('./cifar-10-batches-py/batches.meta')['label_names'] 17 | data, labels = [], [] 18 | for i in range(1, 6): 19 | filename = './cifar-10-batches-py/data_batch_' + str(i) 20 | batch_data = unpickle(filename) 21 | if len(data) > 0: 22 | data = np.vstack((data, batch_data['data'])) 23 | labels = np.hstack((labels, batch_data['labels'])) 24 | else: 25 | data = batch_data['data'] 26 | labels = batch_data['labels'] 27 | 28 | data = grayscale(data) 29 | x = np.matrix(data) 30 | y = np.array(labels) 31 | 32 | horse_indices = np.where(y == 7)[0] 33 | horse_x = x[horse_indices] 34 | print(np.shape(horse_x)) # (5000, 3072) 35 | 36 | print('Some examples of horse images we will feed to the autoencoder for training') 37 | plt.rcParams['figure.figsize'] = (10, 10) 38 | num_examples = 5 39 | for i in range(num_examples): 40 | horse_img = np.reshape(horse_x[i, :], (32, 32)) 41 | plt.subplot(1, num_examples, i+1) 42 | plt.imshow(horse_img, cmap='Greys_r') 43 | plt.show() 44 | 45 | input_dim = np.shape(horse_x)[1] 46 | hidden_dim = 100 47 | ae = Autoencoder(input_dim, hidden_dim) 48 | ae.train(horse_x) 49 | 50 | test_data = unpickle('./cifar-10-batches-py/test_batch') 51 | test_x = grayscale(test_data['data']) 52 | test_labels = np.array(test_data['labels']) 53 | encodings = ae.classify(test_x, test_labels) 54 | 55 | plt.rcParams['figure.figsize'] = (100, 100) 56 | plt.figure() 57 | for i in range(20): 58 | plt.subplot(20, 2, i*2 + 1) 59 | original_img = np.reshape(test_x[i, :], (32, 32)) 60 | plt.imshow(original_img, cmap='Greys_r') 61 | 62 | plt.subplot(20, 2, i*2 + 2) 63 | reconstructed_img = ae.decode([encodings[i]]) 64 | plt.imshow(reconstructed_img, cmap='Greys_r') 65 | 66 | plt.show() 67 | 68 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter07/E02_autoencoder_with_images.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\colortbl ;\red0\green0\blue255;} 3 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 4 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 2\b0 : Autoencoder with images\par 5 | \b\fs22\par 6 | \par 7 | Line 2-5\b0\par 8 | 9 | \pard\sa200\sl276\slmult1 Import the autoencoder class we wrote earlier.\par 10 | 11 | \pard\sl240\slmult1\b Line 7-14\b0\par 12 | 13 | \pard\sa200\sl276\slmult1 Define some helper function to load and preprocess the data.\par 14 | 15 | \pard\sl240\slmult1\b Line 16-30\b0\par 16 | 17 | \pard\sa200\sl276\slmult1 Download the CIFAR-10 dataset in Python from {{\field{\*\fldinst{HYPERLINK https://www.cs.toronto.edu/~kriz/cifar.html }}{\fldrslt{https://www.cs.toronto.edu/~kriz/cifar.html\ul0\cf0}}}}\f0\fs22 . Then we can load the data using the code.\par 18 | 19 | \pard\sl240\slmult1\b Line 50-53\b0\par 20 | Test the autoencoder on other images. \par 21 | \par 22 | \b Line 55-66\b0\par 23 | Plot. \par 24 | \par 25 | \b\par 26 | } 27 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter07/E03_denoising_autoencoder.py: -------------------------------------------------------------------------------- 1 | # 2 | import tensorflow as tf 3 | import numpy as np 4 | import time 5 | 6 | def get_batch(X, Xn, size): 7 | a = np.random.choice(len(X), size, replace=False) 8 | return X[a], Xn[a] 9 | 10 | class Denoiser: 11 | 12 | def __init__(self, input_dim, hidden_dim, epoch=10000, batch_size=50, learning_rate=0.001): 13 | self.epoch = epoch 14 | self.batch_size = batch_size 15 | self.learning_rate = learning_rate 16 | 17 | self.x = tf.placeholder(dtype=tf.float32, shape=[None, input_dim], name='x') 18 | self.x_noised = tf.placeholder(dtype=tf.float32, shape=[None, input_dim], name='x_noised') 19 | with tf.name_scope('encode'): 20 | self.weights1 = tf.Variable(tf.random_normal([input_dim, hidden_dim], dtype=tf.float32), name='weights') 21 | self.biases1 = tf.Variable(tf.zeros([hidden_dim]), name='biases') 22 | self.encoded = tf.nn.sigmoid(tf.matmul(self.x_noised, self.weights1) + self.biases1, name='encoded') 23 | with tf.name_scope('decode'): 24 | weights = tf.Variable(tf.random_normal([hidden_dim, input_dim], dtype=tf.float32), name='weights') 25 | biases = tf.Variable(tf.zeros([input_dim]), name='biases') 26 | self.decoded = tf.matmul(self.encoded, weights) + biases 27 | self.loss = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(self.x, self.decoded)))) 28 | self.train_op = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss) 29 | self.saver = tf.train.Saver() 30 | 31 | def add_noise(self, data): 32 | noise_type = 'mask-0.2' 33 | if noise_type == 'gaussian': 34 | n = np.random.normal(0, 0.1, np.shape(data)) 35 | return data + n 36 | if 'mask' in noise_type: 37 | frac = float(noise_type.split('-')[1]) 38 | temp = np.copy(data) 39 | for i in temp: 40 | n = np.random.choice(len(i), round(frac * len(i)), replace=False) 41 | i[n] = 0 42 | return temp 43 | 44 | def train(self, data): 45 | data_noised = self.add_noise(data) 46 | with open('log.csv', 'w') as writer: 47 | with tf.Session() as sess: 48 | sess.run(tf.global_variables_initializer()) 49 | for i in range(self.epoch): 50 | for j in range(50): 51 | batch_data, batch_data_noised = get_batch(data, data_noised, self.batch_size) 52 | l, _ = sess.run([self.loss, self.train_op], feed_dict={self.x: batch_data, self.x_noised: batch_data_noised}) 53 | if i % 10 == 0: 54 | print('epoch {0}: loss = {1}'.format(i, l)) 55 | self.saver.save(sess, './model.ckpt') 56 | epoch_time = int(time.time()) 57 | row_str = str(epoch_time) + ',' + str(i) + ',' + str(l) + '\n' 58 | writer.write(row_str) 59 | writer.flush() 60 | self.saver.save(sess, './model.ckpt') 61 | 62 | def test(self, data): 63 | with tf.Session() as sess: 64 | self.saver.restore(sess, './model.ckpt') 65 | hidden, reconstructed = sess.run([self.encoded, self.decoded], feed_dict={self.x: data}) 66 | print('input', data) 67 | print('compressed', hidden) 68 | print('reconstructed', reconstructed) 69 | return reconstructed 70 | 71 | def get_params(self): 72 | with tf.Session() as sess: 73 | self.saver.restore(sess, './model.ckpt') 74 | weights, biases = sess.run([self.weights1, self.biases1]) 75 | return weights, biases -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter07/E03_denoising_autoencoder.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Example 3\b0 : Denoising autoencoder\par 4 | \b\fs22\par 5 | \par 6 | \b0 A denoising autoencoder is pretty much the same architecture as a normal autoencoder. The input is noised up, and cost function tries to denoise it by minimizing the construction error from denoised input to clean output.\par 7 | \b\par 8 | } 9 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/Machine_Learning_with_TensorFlow/Chapter07/autoencoder.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | import numpy as np 3 | 4 | def get_batch(X, size): 5 | a = np.random.choice(len(X), size, replace=False) 6 | return X[a] 7 | 8 | class Autoencoder: 9 | def __init__(self, input_dim, hidden_dim, epoch=1000, batch_size=50, learning_rate=0.001): 10 | self.epoch = epoch 11 | self.batch_size = batch_size 12 | self.learning_rate = learning_rate 13 | 14 | x = tf.placeholder(dtype=tf.float32, shape=[None, input_dim]) 15 | with tf.name_scope('encode'): 16 | weights = tf.Variable(tf.random_normal([input_dim, hidden_dim], dtype=tf.float32), name='weights') 17 | biases = tf.Variable(tf.zeros([hidden_dim]), name='biases') 18 | encoded = tf.nn.sigmoid(tf.matmul(x, weights) + biases) 19 | with tf.name_scope('decode'): 20 | weights = tf.Variable(tf.random_normal([hidden_dim, input_dim], dtype=tf.float32), name='weights') 21 | biases = tf.Variable(tf.zeros([input_dim]), name='biases') 22 | decoded = tf.matmul(encoded, weights) + biases 23 | 24 | self.x = x 25 | self.encoded = encoded 26 | self.decoded = decoded 27 | 28 | self.loss = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(self.x, self.decoded)))) 29 | self.train_op = tf.train.RMSPropOptimizer(self.learning_rate).minimize(self.loss) 30 | 31 | self.saver = tf.train.Saver() 32 | 33 | def train(self, data): 34 | with tf.Session() as sess: 35 | sess.run(tf.global_variables_initializer()) 36 | for i in range(self.epoch): 37 | for j in range(np.shape(data)[0] // self.batch_size): 38 | batch_data = get_batch(data, self.batch_size) 39 | l, _ = sess.run([self.loss, self.train_op], feed_dict={self.x: batch_data}) 40 | if i % 10 == 0: 41 | print('epoch {0}: loss = {1}'.format(i, l)) 42 | self.saver.save(sess, './model.ckpt') 43 | self.saver.save(sess, './model.ckpt') 44 | 45 | def test(self, data): 46 | with tf.Session() as sess: 47 | self.saver.restore(sess, './model.ckpt') 48 | hidden, reconstructed = sess.run([self.encoded, self.decoded], feed_dict={self.x: data}) 49 | print('input', data) 50 | print('compressed', hidden) 51 | print('reconstructed', reconstructed) 52 | return reconstructed 53 | 54 | def get_params(self): 55 | with tf.Session() as sess: 56 | self.saver.restore(sess, './model.ckpt') 57 | weights, biases = sess.run([self.weights1, self.biases1]) 58 | return weights, biases 59 | 60 | def classify(self, data, labels): 61 | with tf.Session() as sess: 62 | sess.run(tf.global_variables_initializer()) 63 | self.saver.restore(sess, './model.ckpt') 64 | hidden, reconstructed = sess.run([self.encoded, self.decoded], feed_dict={self.x: data}) 65 | reconstructed = reconstructed[0] 66 | # loss = sess.run(self.all_loss, feed_dict={self.x: data}) 67 | print('data', np.shape(data)) 68 | print('reconstructed', np.shape(reconstructed)) 69 | loss = np.sqrt(np.mean(np.square(data - reconstructed), axis=1)) 70 | print('loss', np.shape(loss)) 71 | horse_indices = np.where(labels == 7)[0] 72 | not_horse_indices = np.where(labels != 7)[0] 73 | horse_loss = np.mean(loss[horse_indices]) 74 | not_horse_loss = np.mean(loss[not_horse_indices]) 75 | print('horse', horse_loss) 76 | print('not horse', not_horse_loss) 77 | return hidden 78 | 79 | def decode(self, encoding): 80 | with tf.Session() as sess: 81 | sess.run(tf.global_variables_initializer()) 82 | self.saver.restore(sess, './model.ckpt') 83 | reconstructed = sess.run(self.decoded, feed_dict={self.encoded: encoding}) 84 | img = np.reshape(reconstructed, (32, 32)) 85 | return img 86 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E00_overview.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs28\lang7 Examples for Python4Delphi-Properties Handling\fs24\par 4 | \fs22\par 5 | Example 1:\b0\par 6 | 7 | \pard\sa200\sl276\slmult1 String Properties\par 8 | 9 | \pard\sl240\slmult1\b Example 2:\b0\par 10 | 11 | \pard\sa200\sl276\slmult1 Integer Properties\par 12 | 13 | \pard\sl240\slmult1\b Example 3:\b0\par 14 | 15 | \pard\sa200\sl276\slmult1 Radiogroup Properties\par 16 | 17 | \pard\sl240\slmult1\b Example 4:\b0\par 18 | 19 | \pard\sa200\sl276\slmult1 Checkbox Group Properties\par 20 | 21 | \pard\sl240\slmult1\b Example 5:\b0\par 22 | 23 | \pard\sa200\sl276\slmult1 File Selection Properties\par 24 | 25 | \pard\sl240\slmult1\b Example 6:\b0\par 26 | 27 | \pard\sa200\sl276\slmult1 Date Selection Properties\par 28 | 29 | \pard\sl240\slmult1\b Example 7:\b0\par 30 | 31 | \pard\sa200\sl276\slmult1 Property Collections\par 32 | \par 33 | } 34 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E01_props.py: -------------------------------------------------------------------------------- 1 | # Display the Properties content 2 | print (P4DProps) 3 | # Get single property 4 | print (P4DProps.Value['string1']) 5 | print (P4DProps.Value['string2']) 6 | print (P4DProps.Value['string3']) 7 | 8 | # Change one of the properties 9 | P4DProps.Value['string1'] = "Changed Text 1" 10 | P4DProps.Value['string2'] = "Changed Text 2" 11 | P4DProps.Value['string3'] = "Changed Text 3" 12 | # By reassigning the same object, we force the OnChange event 13 | # That will update the Delphi controls 14 | P4DProps.Value = P4DProps.Value 15 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E01_props_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | \viewkind4\uc1\pard\sl240\slmult1\lang7\b\f0\fs22\par 3 | Example 1: \b0 String Properties\par 4 | \par 5 | } 6 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E01_props_dlg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | string1 5 | dlg_string 6 | String 1 7 | string 8 | Default 1 9 | 10 | 11 | string2 12 | dlg_string 13 | String 2 14 | string 15 | Default 2 16 | 17 | 18 | string3 19 | dlg_string 20 | String 3 21 | string 22 | Default 3 23 | 24 | 25 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E02_props.py: -------------------------------------------------------------------------------- 1 | # Display the Properties content 2 | print (P4DProps) 3 | # Get single property 4 | print (P4DProps.Value['integer1']) 5 | print (P4DProps.Value['integer2']) 6 | 7 | # Change one of the properties 8 | P4DProps.Value['integer1'] = 111 9 | P4DProps.Value['integer2'] = 222 10 | # By reassigning the same object, we force the OnChange event 11 | # That will update the Delphi controls 12 | P4DProps.Value = P4DProps.Value 13 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E02_props_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | \viewkind4\uc1\pard\sl240\slmult1\lang7\b\f0\fs22\par 3 | \pard\sa200\sl276\slmult1 Example 2: \b0 Integer Properties\par 4 | \par 5 | } 6 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E02_props_dlg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | integer1 5 | dlg_integer 6 | Integer 1 7 | integer 8 | 1 9 | 10 | 11 | integer2 12 | dlg_integer 13 | Integer 2 14 | integer 15 | 2 16 | 17 | 18 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E03_props.py: -------------------------------------------------------------------------------- 1 | # Display the Properties content 2 | print (P4DProps) 3 | # Get single property 4 | print (P4DProps.Value['radiogroup1']) 5 | print (P4DProps.Value['radiogroup2']) 6 | 7 | # Change one of the properties 8 | P4DProps.Value['radiogroup1'] = 0 9 | P4DProps.Value['radiogroup2'] = 3 10 | # By reassigning the same object, we force the OnChange event 11 | # That will update the Delphi controls 12 | P4DProps.Value = P4DProps.Value 13 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E03_props_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | \viewkind4\uc1\pard\sl240\slmult1\lang7\b\f0\fs22\par 3 | \pard\sa200\sl276\slmult1 Example 3: \b0 Radiogroup Properties\par 4 | \par 5 | } 6 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E03_props_dlg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | radiogroup1 5 | dlg_radiogroup 6 | Radiogroup 1 7 | integer 8 | Item 1;Item 2 9 | 1 10 | 11 | 12 | radiogroup2 13 | dlg_radiogroup 14 | Radiogroup 2 15 | integer 16 | Item 1;Item 2;Item 3;Item 4 17 | 2 18 | 19 | 20 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E04_props.py: -------------------------------------------------------------------------------- 1 | # Display the Properties content 2 | print (P4DProps) 3 | # Get single property 4 | print (P4DProps.Value['checkboxgroup1']) 5 | print (P4DProps.Value['checkboxgroup2']) 6 | 7 | # Put single property 8 | P4DProps.Value['checkboxgroup1'] = [1,1] 9 | P4DProps.Value['checkboxgroup2'] = [0,0,0,0] 10 | 11 | # By reassigning the same object, we force the OnChange event 12 | # That will update the Delphi controls 13 | P4DProps.Value = P4DProps.Value 14 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E04_props_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | \viewkind4\uc1\pard\sl240\slmult1\lang7\b\f0\fs22\par 3 | \pard\sa200\sl276\slmult1 Example 4: \b0 Checkbox Group Properties\par 4 | \par 5 | } 6 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E04_props_dlg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | checkboxgroup1 5 | dlg_checkboxgroup 6 | Checkbox Group 1 7 | bool_array 8 | Item 1;Item 2 9 | T;F 10 | 11 | 12 | checkboxgroup2 13 | dlg_checkboxgroup 14 | Checkbox Group 2 15 | bool_array 16 | Item 1;Item 2;Item 3;Item 4 17 | T;F;T;F 18 | 19 | 20 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E05_props.py: -------------------------------------------------------------------------------- 1 | # Display the Properties content 2 | print (P4DProps) 3 | # Get single property 4 | print (P4DProps.Value['filename1']) 5 | print (P4DProps.Value['filename2']) 6 | 7 | # Change one of the properties 8 | P4DProps.Value['filename1'] = "NewFilename1" 9 | P4DProps.Value['filename2'] = "NewFilename2" 10 | # By reassigning the same object, we force the OnChange event 11 | # That will update the Delphi controls 12 | P4DProps.Value = P4DProps.Value 13 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E05_props_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0\nouicompat{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | {\*\generator Riched20 10.0.16299}\viewkind4\uc1 3 | \pard\sl240\slmult1\b\f0\fs22\lang7 Line 3-4\b0\par 4 | 5 | \pard\sa200\sl276\slmult1 Import TensorFlow and Numpy\par 6 | 7 | \pard\sl240\slmult1\b Line 8-10\b0\par 8 | 9 | \pard\sa200\sl276\slmult1 Define a 2x2 matrix in different ways\par 10 | 11 | \pard\sl240\slmult1\b Line 12-14\b0\par 12 | 13 | \pard\sa200\sl276\slmult1 Let's see what happens when we print them:\par 14 | 15 | \pard\sl240\slmult1 \par 16 | \par 17 | 18 | \pard\sa200\sl240\slmult1 \par 19 | 20 | \pard\sl240\slmult1\b Line 19-21\b0\par 21 | By the way, there's a function called convert_to_tensor(...) that does exactly what you might expect.\par 22 | Let's use it to create tensor objects out of various types:\par 23 | \par 24 | \b Line 23-25\b0\par 25 | 26 | \pard\sa200\sl276\slmult1 Ok, ok! Time for the reveal:\par 27 | 28 | \pard\sl276\slmult1 \par 29 | \par 30 | 31 | \pard\sa200\sl276\slmult1 \par 32 | \par 33 | } 34 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E05_props_dlg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | filename1 5 | dlg_file 6 | File Selection 1 7 | string 8 | fileXY1 9 | 10 | 11 | filename2 12 | dlg_file 13 | File Selection 2 14 | string 15 | fileXY2 16 | 17 | 18 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E06_props.py: -------------------------------------------------------------------------------- 1 | # Display the Properties content 2 | print (P4DProps) 3 | # Get single property 4 | print (P4DProps.Value['date1']) 5 | print (P4DProps.Value['date2']) 6 | print (P4DProps.Value['date3']) 7 | 8 | # Put single property 9 | P4DProps.Value['date1'] = ["01.01.2017",""] 10 | P4DProps.Value['date2'] = ["","31.12.2017"] 11 | P4DProps.Value['date3'] = ["01.01.2017","31.12.2017"] 12 | 13 | # By reassigning the same object, we force the OnChange event 14 | # That will update the Delphi controls 15 | P4DProps.Value = P4DProps.Value 16 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E06_props_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | \viewkind4\uc1\pard\sl240\slmult1\lang7\b\f0\fs22\par 3 | \pard\sa200\sl276\slmult1 Example 6: \b0 Date Selection Properties\par 4 | \par 5 | } 6 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E06_props_dlg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | date1 5 | dlg_date 6 | Date Selection 1 - from date ... 7 | date_array 8 | 01.01.2015 9 | - 10 | 11 | 12 | date2 13 | dlg_date 14 | Date Selection 2 - ... until date 15 | date_array 16 | - 17 | 31.12.2017 18 | 19 | 20 | date3 21 | dlg_date 22 | Date Selection 3 - from date... until date 23 | date_array 24 | 09.09.2015 25 | 31.08.2017 26 | 27 | 28 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E07_props.py: -------------------------------------------------------------------------------- 1 | # Display the Properties content 2 | print (P4DProps) 3 | # Get single property 4 | print (P4DProps.Value['string1']) 5 | print (P4DProps.Value['integer1']) 6 | print (P4DProps.Value['radiogroup1']) 7 | print (P4DProps.Value['checkboxgroup1']) 8 | print (P4DProps.Value['filename1']) 9 | print (P4DProps.Value['date1']) 10 | 11 | # Put single property 12 | P4DProps.Value['string1'] = "Changed Text XYZ" 13 | P4DProps.Value['integer1'] = 9999 14 | P4DProps.Value['radiogroup1'] = 3 15 | P4DProps.Value['checkboxgroup1'] = [1,1,1,1] 16 | P4DProps.Value['filename1'] = "NewFilenameXYZ" 17 | P4DProps.Value['date1'] = ["01.01.2017","31.12.2099"] 18 | 19 | # By reassigning the same object, we force the OnChange event 20 | # That will update the Delphi controls 21 | P4DProps.Value = P4DProps.Value 22 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E07_props_desc.rtf: -------------------------------------------------------------------------------- 1 | {\rtf1\ansi\ansicpg1252\deff0{\fonttbl{\f0\fnil\fcharset0 Calibri;}} 2 | \viewkind4\uc1\pard\sl240\slmult1\lang7\b\f0\fs22\par 3 | Example 7: \b0 Property Collections\par 4 | \pard\sa200\sl276\slmult1\par 5 | } 6 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/Demos/TFDelphiProps/E07_props_dlg.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | string1 5 | dlg_string 6 | String 1 7 | string 8 | Default 1 9 | 10 | 11 | integer1 12 | dlg_integer 13 | Integer 1 14 | integer 15 | 11 16 | 17 | 18 | radiogroup1 19 | dlg_radiogroup 20 | Radiogroup 1 21 | integer 22 | Item 1;Item 2;Item 3;Item 4 23 | 0 24 | 25 | 26 | checkboxgroup1 27 | dlg_checkboxgroup 28 | Checkbox Group 1 29 | bool_array 30 | Item 1;Item 2;Item 3;Item 4 31 | T;F;T;F 32 | 33 | 34 | filename1 35 | dlg_file 36 | File Selection 1 37 | string 38 | file_XYZ 39 | 40 | 41 | date1 42 | dlg_date 43 | Date Selection 1 - from date... until date 44 | date_array 45 | 09.09.2015 46 | 31.08.2017 47 | 48 | 49 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/P4DDemoControl/P4DDemoControl.dpr: -------------------------------------------------------------------------------- 1 | program P4DDemoControl; 2 | 3 | uses 4 | Forms, 5 | fmMain in 'fmMain.pas' {Main}; 6 | 7 | {$R *.res} 8 | 9 | begin 10 | Application.Initialize; 11 | Application.CreateForm(TMain, Main); 12 | Application.Run; 13 | end. 14 | -------------------------------------------------------------------------------- /P4DTensorflowDemos/P4DDemoControl/P4DDemoControl.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/P4DTensorflowDemos/P4DDemoControl/P4DDemoControl.ico -------------------------------------------------------------------------------- /P4DTensorflowDemos/P4DDemoControl/P4DDemoControl.res: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/P4DTensorflowDemos/P4DDemoControl/P4DDemoControl.res -------------------------------------------------------------------------------- /P4DTensorflowDemos/P4DDemoControl/fmMain.dfm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/P4DTensorflowDemos/P4DDemoControl/fmMain.dfm -------------------------------------------------------------------------------- /P4DTensorflowDemos/audio_cough_1.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/P4DTensorflowDemos/audio_cough_1.wav -------------------------------------------------------------------------------- /P4DTensorflowDemos/runP4DDemoControl_for_TFDelphiProps.bat: -------------------------------------------------------------------------------- 1 | P4DDemoControl.exe -Dir Demos\TFDelphiProps 2 | Pause -------------------------------------------------------------------------------- /P4DTensorflowDemos/runP4DDemoControl_for_TF_Chapter02.bat: -------------------------------------------------------------------------------- 1 | P4DDemoControl.exe -Dir Demos\Machine_Learning_with_TensorFlow\Chapter02 2 | Pause -------------------------------------------------------------------------------- /P4DTensorflowDemos/runP4DDemoControl_for_TF_Chapter03.bat: -------------------------------------------------------------------------------- 1 | P4DDemoControl.exe -Dir Demos\Machine_Learning_with_TensorFlow\Chapter03 2 | Pause -------------------------------------------------------------------------------- /P4DTensorflowDemos/runP4DDemoControl_for_TF_Chapter04.bat: -------------------------------------------------------------------------------- 1 | P4DDemoControl.exe -Dir Demos\Machine_Learning_with_TensorFlow\Chapter04 2 | Pause -------------------------------------------------------------------------------- /P4DTensorflowDemos/runP4DDemoControl_for_TF_Chapter05.bat: -------------------------------------------------------------------------------- 1 | P4DDemoControl.exe -Dir Demos\Machine_Learning_with_TensorFlow\Chapter05 2 | Pause -------------------------------------------------------------------------------- /P4DTensorflowDemos/runP4DDemoControl_for_TF_Chapter06.bat: -------------------------------------------------------------------------------- 1 | P4DDemoControl.exe -Dir Demos\Machine_Learning_with_TensorFlow\Chapter06 2 | Pause -------------------------------------------------------------------------------- /P4DTensorflowDemos/runP4DDemoControl_for_TF_Chapter07.bat: -------------------------------------------------------------------------------- 1 | P4DDemoControl.exe -Dir Demos\Machine_Learning_with_TensorFlow\Chapter07 2 | Pause -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TensorFlow4Delphi 2 | TensorFlow API (Wrapper) for Delphi 3 | 4 | Requirements and versions 5 | - Python 3.6 64-bit with installed Tensorflow 1.4.0 64-bit 6 | - Delphi 10.2 Tokyo 7 | * Set the enviroment variable DUnitX for Path to DUnitX-Source in the IDE options mask. 8 | - Visual Studio 2015 for C-Api-Extensions 9 | 10 | There are three DUnitX-Projects for console, fmx and vcl to test the Delphi-API. 11 | My preferred DUnitX-Project is: "api_tests\TestWithVCLUnitX\VCLTestTensorFlow.dproj". 12 | This DUnitX-Project allows you to take a closer look at the Tensorflow API. 13 | The project contains a hex viewer. 14 | 15 | For fast solutions with Tensorflow I prefer to use the package "Python4Delphi" (P4D). 16 | Prototyping is done directly in Python and for the later interaction with the users I use 17 | Delphi. See the directory "P4DTensorflowDemos" and the project "P4DDemoControl.dproj". 18 | The subdirectory "demos\Machine_Learning_with_TensorFlow" contains examples of the book 19 | "Machine Learning with TensorFlow" ( https://github.com/BinRoot/TensorFlow-Book ). 20 | 21 | 22 | You can find a corrected "Python4Delphi"-Version here: 23 | 24 | https://github.com/hartmutdavid/python4delphi 25 | 26 | This version includes customizations to Python 3.x, 64bit-Windows and FireDAC. 27 | -------------------------------------------------------------------------------- /api/TensorFlow.DApiOperations.pas: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/api/TensorFlow.DApiOperations.pas -------------------------------------------------------------------------------- /api_tests/TensorFlow.LowLevelUnitTests.pas: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/api_tests/TensorFlow.LowLevelUnitTests.pas -------------------------------------------------------------------------------- /api_tests/TensorFlow.LowLevelUnitTestsUtil.pas: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/api_tests/TensorFlow.LowLevelUnitTestsUtil.pas -------------------------------------------------------------------------------- /api_tests/TestWithConsoleDUnitX/ConsoleTestTensorFlow.dpr: -------------------------------------------------------------------------------- 1 | program ConsoleTestTensorFlow; 2 | 3 | {$IFNDEF TESTINSIGHT} 4 | {$APPTYPE CONSOLE} 5 | {$ENDIF}{$STRONGLINKTYPES ON} 6 | uses 7 | System.SysUtils, 8 | {$IFDEF TESTINSIGHT} 9 | TestInsight.DUnitX, 10 | {$ENDIF } 11 | DUnitX.Loggers.Console, 12 | DUnitX.Loggers.Xml.NUnit, 13 | DUnitX.TestFramework, 14 | DUnitX.MemoryLeakMonitor.FastMM4 in 'DUnitX.MemoryLeakMonitor.FastMM4.pas', 15 | FastMM4Messages in '..\..\third_party\FastMM4\FastMM4Messages.pas', 16 | TensorFlow.LowLevelAPI in '..\..\api\TensorFlow.LowLevelAPI.pas', 17 | TensorFlow.DApi in '..\..\api\TensorFlow.DApi.pas', 18 | TensorFlow.DApiOperations in '..\..\api\TensorFlow.DApiOperations.pas', 19 | TensorFlow.DApiBase in '..\..\api\TensorFlow.DApiBase.pas', 20 | TensorFlow._Helpers in '..\..\api\TensorFlow._Helpers.pas', 21 | TensorFlow.DApiUnitTests in '..\TensorFlow.DApiUnitTests.pas', 22 | TensorFlow.LowLevelUnitTests in '..\TensorFlow.LowLevelUnitTests.pas', 23 | TensorFlow.LowLevelUnitTestsUtil in '..\TensorFlow.LowLevelUnitTestsUtil.pas'; 24 | 25 | var 26 | runner : ITestRunner; 27 | results : IRunResults; 28 | logger : ITestLogger; 29 | nunitLogger : ITestLogger; 30 | begin 31 | {$IFDEF TESTINSIGHT} 32 | TestInsight.DUnitX.RunRegisteredTests; 33 | exit; 34 | {$ENDIF} 35 | try 36 | //Check command line options, will exit if invalid 37 | TDUnitX.CheckCommandLine; 38 | //Create the test runner 39 | runner := TDUnitX.CreateRunner; 40 | //Tell the runner to use RTTI to find Fixtures 41 | runner.UseRTTI := True; 42 | //tell the runner how we will log things 43 | //Log to the console window 44 | logger := TDUnitXConsoleLogger.Create(true); 45 | runner.AddLogger(logger); 46 | //Generate an NUnit compatible XML File 47 | nunitLogger := TDUnitXXMLNUnitFileLogger.Create(TDUnitX.Options.XMLOutputFile); 48 | runner.AddLogger(nunitLogger); 49 | runner.FailsOnNoAsserts := False; //When true, Assertions must be made during tests; 50 | 51 | //Run tests 52 | results := runner.Execute; 53 | if not results.AllPassed then 54 | System.ExitCode := EXIT_ERRORS; 55 | 56 | {$IFNDEF CI} 57 | //We don't want this happening when running under CI. 58 | if TDUnitX.Options.ExitBehavior = TDUnitXExitBehavior.Pause then 59 | begin 60 | System.Write('Done.. press key to quit.'); 61 | System.Readln; 62 | end; 63 | {$ENDIF} 64 | except 65 | on E: Exception do 66 | System.Writeln(E.ClassName, ': ', E.Message); 67 | end; 68 | end. 69 | -------------------------------------------------------------------------------- /api_tests/TestWithConsoleDUnitX/ConsoleTestTensorFlow.res: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/api_tests/TestWithConsoleDUnitX/ConsoleTestTensorFlow.res -------------------------------------------------------------------------------- /api_tests/TestWithConsoleDUnitX/TestTensorFlow.res: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/api_tests/TestWithConsoleDUnitX/TestTensorFlow.res -------------------------------------------------------------------------------- /api_tests/TestWithFMXUnitX/DUnitX.MemoryLeaks.inc: -------------------------------------------------------------------------------- 1 | {***************************************************************************} 2 | { } 3 | { DUnitX } 4 | { } 5 | { Copyright (C) 2015 Vincent Parrett & Contributors } 6 | { } 7 | { vincent@finalbuilder.com } 8 | { http://www.finalbuilder.com } 9 | { } 10 | { } 11 | {***************************************************************************} 12 | { } 13 | { Licensed under the Apache License, Version 2.0 (the "License"); } 14 | { you may not use this file except in compliance with the License. } 15 | { You may obtain a copy of the License at } 16 | { } 17 | { http://www.apache.org/licenses/LICENSE-2.0 } 18 | { } 19 | { Unless required by applicable law or agreed to in writing, software } 20 | { distributed under the License is distributed on an "AS IS" BASIS, } 21 | { WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. } 22 | { See the License for the specific language governing permissions and } 23 | { limitations under the License. } 24 | { } 25 | {***************************************************************************} 26 | 27 | 28 | 29 | // Uncomment to use FastMM4 Memory Leak Tracking. 30 | //NOTE : Memory leak tracking does not work very well at the moment, as it's 31 | //reporting leaks when logging information during tests (calls to .Status etc). 32 | {.$DEFINE USE_FASTMM4_LEAK_MONITOR} 33 | 34 | 35 | -------------------------------------------------------------------------------- /api_tests/TestWithFMXUnitX/DUnitX.Stacktrace.inc: -------------------------------------------------------------------------------- 1 | {***************************************************************************} 2 | { } 3 | { DUnitX } 4 | { } 5 | { Copyright (C) 2015 Vincent Parrett & Contributors } 6 | { } 7 | { vincent@finalbuilder.com } 8 | { http://www.finalbuilder.com } 9 | { } 10 | { } 11 | {***************************************************************************} 12 | { } 13 | { Licensed under the Apache License, Version 2.0 (the "License"); } 14 | { you may not use this file except in compliance with the License. } 15 | { You may obtain a copy of the License at } 16 | { } 17 | { http://www.apache.org/licenses/LICENSE-2.0 } 18 | { } 19 | { Unless required by applicable law or agreed to in writing, software } 20 | { distributed under the License is distributed on an "AS IS" BASIS, } 21 | { WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. } 22 | { See the License for the specific language governing permissions and } 23 | { limitations under the License. } 24 | { } 25 | {***************************************************************************} 26 | 27 | 28 | // Uncomment to use JCL for stacktrace support. 29 | {.$DEFINE USE_JCL} 30 | 31 | // Uncomment to use MadExcept 3 for stacktrace support. 32 | {.$DEFINE USE_MADEXCEPT3} 33 | 34 | // Uncomment to use MadExcept 4 for stacktrace support. 35 | {.$DEFINE USE_MADEXCEPT4} 36 | 37 | // Uncomment to use EurekaLog 7 for stacktrace support. 38 | {.$DEFINE USE_EUREKALOG7} 39 | 40 | 41 | -------------------------------------------------------------------------------- /api_tests/TestWithFMXUnitX/DUnitX.inc: -------------------------------------------------------------------------------- 1 | {***************************************************************************} 2 | { } 3 | { DUnitX } 4 | { } 5 | { Copyright (C) 2017 Vincent Parrett & Contributors } 6 | { } 7 | { vincent@finalbuilder.com } 8 | { http://www.finalbuilder.com } 9 | { } 10 | { } 11 | {***************************************************************************} 12 | { } 13 | { Licensed under the Apache License, Version 2.0 (the "License"); } 14 | { you may not use this file except in compliance with the License. } 15 | { You may obtain a copy of the License at } 16 | { } 17 | { http://www.apache.org/licenses/LICENSE-2.0 } 18 | { } 19 | { Unless required by applicable law or agreed to in writing, software } 20 | { distributed under the License is distributed on an "AS IS" BASIS, } 21 | { WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. } 22 | { See the License for the specific language governing permissions and } 23 | { limitations under the License. } 24 | { } 25 | {***************************************************************************} 26 | 27 | //Basic Version of Compiler Supported 28 | {$IFDEF CONDITIONALEXPRESSIONS} //Started being defined with D2009 29 | {$IF CompilerVersion < 21.0} // Before RAD Studio 2010 30 | {$DEFINE UNSUPPORTED_COMPILER_VERSION} 31 | {$IFEND} 32 | {$ELSE} 33 | {$DEFINE UNSUPPORTED_COMPILER_VERSION} 34 | {$ENDIF} 35 | 36 | {$IFDEF UNSUPPORTED_COMPILER_VERSION} 37 | Unsupported Compiler Version (Delphi 2010 or later required!) 38 | {$ENDIF} 39 | 40 | //Use namespaces 41 | {$DEFINE USE_NS} 42 | 43 | {$DEFINE DELPHIX_BERLIN_DOWN} 44 | {$DEFINE DELPHIX_SEATTLE_DOWN} 45 | {$DEFINE DELPHI_XE8_DOWN} 46 | {$DEFINE DELPHI_XE7_DOWN} 47 | {$DEFINE DELPHI_XE6_DOWN} 48 | {$DEFINE DELPHI_XE5_DOWN} 49 | {$DEFINE DELPHI_XE4_DOWN} 50 | {$DEFINE DELPHI_XE3_DOWN} 51 | {$DEFINE DELPHI_XE2_DOWN} 52 | {$DEFINE DELPHI_XE_DOWN} 53 | {$DEFINE DELPHI_2010_DOWN} 54 | 55 | {$IFDEF VER210} // RAD Studio 2010 56 | {$DEFINE DELPHI_2010} 57 | {$DEFINE DELPHI_2010_UP} 58 | {$DEFINE CPUX86} 59 | {$UNDEF USE_NS} 60 | {$ENDIF VER210} 61 | 62 | {$IFDEF VER220} // RAD Studio XE 63 | {$DEFINE DELPHI_2010_UP} 64 | {$DEFINE DELPHI_XE} 65 | {$DEFINE DELPHI_XE_UP} 66 | {$DEFINE CPUX86} 67 | {$UNDEF DELPHI_2010_DOWN} 68 | {$UNDEF USE_NS} 69 | {$ENDIF VER220} 70 | 71 | {$IFDEF VER230} // RAD Studio XE2 72 | {$DEFINE DELPHI_2010_UP} 73 | {$DEFINE DELPHI_XE_UP} 74 | {$DEFINE DELPHI_XE2} 75 | {$DEFINE DELPHI_XE2_UP} 76 | {$DEFINE SUPPORTS_REGEX} 77 | {$DEFINE USE_NS} 78 | {$UNDEF DELPHI_2010_DOWN} 79 | {$UNDEF DELPHI_XE_DOWN} 80 | {$ENDIF VER230} 81 | 82 | {$IFDEF VER240} // RAD Studio XE3 83 | {$DEFINE DELPHI_2010_UP} 84 | {$DEFINE DELPHI_XE_UP} 85 | {$DEFINE DELPHI_XE2_UP} 86 | {$DEFINE DELPHI_XE3} 87 | {$DEFINE DELPHI_XE3_UP} 88 | {$DEFINE SUPPORTS_REGEX} 89 | {$UNDEF DELPHI_2010_DOWN} 90 | {$UNDEF DELPHI_XE_DOWN} 91 | {$UNDEF DELPHI_XE2_DOWN} 92 | {$ENDIF VER240} 93 | 94 | {$IFDEF VER250} // RAD Studio XE4 95 | {$DEFINE DELPHI_2010_UP} 96 | {$DEFINE DELPHI_XE_UP} 97 | {$DEFINE DELPHI_XE2_UP} 98 | {$DEFINE DELPHI_XE3_UP} 99 | {$DEFINE DELPHI_XE4} 100 | {$DEFINE DELPHI_XE4_UP} 101 | {$DEFINE SUPPORTS_REGEX} 102 | {$DEFINE USE_NS} 103 | {$UNDEF DELPHI_2010_DOWN} 104 | {$UNDEF DELPHI_XE_DOWN} 105 | {$UNDEF DELPHI_XE2_DOWN} 106 | {$UNDEF DELPHI_XE3_DOWN} 107 | {$ENDIF VER250} 108 | 109 | {$IFDEF VER260} // RAD Studio XE5 110 | {$DEFINE DELPHI_2010_UP} 111 | {$DEFINE DELPHI_XE_UP} 112 | {$DEFINE DELPHI_XE2_UP} 113 | {$DEFINE DELPHI_XE3_UP} 114 | {$DEFINE DELPHI_XE4_UP} 115 | {$DEFINE DELPHI_XE5} 116 | {$DEFINE DELPHI_XE5_UP} 117 | {$DEFINE SUPPORTS_REGEX} 118 | {$DEFINE USE_NS} 119 | {$UNDEF DELPHI_2010_DOWN} 120 | {$UNDEF DELPHI_XE_DOWN} 121 | {$UNDEF DELPHI_XE2_DOWN} 122 | {$UNDEF DELPHI_XE3_DOWN} 123 | {$UNDEF DELPHI_XE4_DOWN} 124 | {$ENDIF VER260} 125 | 126 | {$IFDEF VER270} // RAD Studio XE6 127 | {$DEFINE DELPHI_2010_UP} 128 | {$DEFINE DELPHI_XE_UP} 129 | {$DEFINE DELPHI_XE2_UP} 130 | {$DEFINE DELPHI_XE3_UP} 131 | {$DEFINE DELPHI_XE4_UP} 132 | {$DEFINE DELPHI_XE5_UP} 133 | {$DEFINE DELPHI_XE6} 134 | {$DEFINE DELPHI_XE6_UP} 135 | {$DEFINE SUPPORTS_REGEX} 136 | {$DEFINE USE_NS} 137 | {$UNDEF DELPHI_2010_DOWN} 138 | {$UNDEF DELPHI_XE_DOWN} 139 | {$UNDEF DELPHI_XE2_DOWN} 140 | {$UNDEF DELPHI_XE3_DOWN} 141 | {$UNDEF DELPHI_XE4_DOWN} 142 | {$UNDEF DELPHI_XE5_DOWN} 143 | {$ENDIF VER270} 144 | 145 | {$IFDEF VER280} // RAD Studio XE7 146 | {$DEFINE DELPHI_2010_UP} 147 | {$DEFINE DELPHI_XE_UP} 148 | {$DEFINE DELPHI_XE2_UP} 149 | {$DEFINE DELPHI_XE3_UP} 150 | {$DEFINE DELPHI_XE4_UP} 151 | {$DEFINE DELPHI_XE5_UP} 152 | {$DEFINE DELPHI_XE6_UP} 153 | {$DEFINE DELPHI_XE7} 154 | {$DEFINE DELPHI_XE7_UP} 155 | {$DEFINE SUPPORTS_REGEX} 156 | {$DEFINE USE_NS} 157 | {$UNDEF DELPHI_2010_DOWN} 158 | {$UNDEF DELPHI_XE_DOWN} 159 | {$UNDEF DELPHI_XE2_DOWN} 160 | {$UNDEF DELPHI_XE3_DOWN} 161 | {$UNDEF DELPHI_XE4_DOWN} 162 | {$UNDEF DELPHI_XE5_DOWN} 163 | {$UNDEF DELPHI_XE6_DOWN} 164 | {$ENDIF VER280} 165 | 166 | {$IFDEF VER290} // RAD Studio XE8 167 | {$DEFINE DELPHI_2010_UP} 168 | {$DEFINE DELPHI_XE_UP} 169 | {$DEFINE DELPHI_XE2_UP} 170 | {$DEFINE DELPHI_XE3_UP} 171 | {$DEFINE DELPHI_XE4_UP} 172 | {$DEFINE DELPHI_XE5_UP} 173 | {$DEFINE DELPHI_XE6_UP} 174 | {$DEFINE DELPHI_XE7} 175 | {$DEFINE DELPHI_XE7_UP} 176 | {$DEFINE DELPHI_XE8_UP} 177 | 178 | {$DEFINE SUPPORTS_REGEX} 179 | {$DEFINE USE_NS} 180 | {$UNDEF DELPHI_2010_DOWN} 181 | {$UNDEF DELPHI_XE_DOWN} 182 | {$UNDEF DELPHI_XE2_DOWN} 183 | {$UNDEF DELPHI_XE3_DOWN} 184 | {$UNDEF DELPHI_XE4_DOWN} 185 | {$UNDEF DELPHI_XE5_DOWN} 186 | {$UNDEF DELPHI_XE6_DOWN} 187 | {$UNDEF DELPHI_XE7_DOWN} 188 | {$ENDIF VER290} 189 | 190 | {$IFDEF VER300} // RAD Studio 10 Seattle 191 | {$DEFINE DELPHI_2010_UP} 192 | {$DEFINE DELPHI_XE_UP} 193 | {$DEFINE DELPHI_XE2_UP} 194 | {$DEFINE DELPHI_XE3_UP} 195 | {$DEFINE DELPHI_XE4_UP} 196 | {$DEFINE DELPHI_XE5_UP} 197 | {$DEFINE DELPHI_XE6_UP} 198 | {$DEFINE DELPHI_XE7} 199 | {$DEFINE DELPHI_XE7_UP} 200 | {$DEFINE DELPHI_XE8_UP} 201 | {$DEFINE DELPHIX_SEATTLE_UP} 202 | {$DEFINE DELPHIX_SEATTLE} 203 | {$DEFINE SUPPORTS_REGEX} 204 | {$DEFINE USE_NS} 205 | {$UNDEF DELPHI_2010_DOWN} 206 | {$UNDEF DELPHI_XE_DOWN} 207 | {$UNDEF DELPHI_XE2_DOWN} 208 | {$UNDEF DELPHI_XE3_DOWN} 209 | {$UNDEF DELPHI_XE4_DOWN} 210 | {$UNDEF DELPHI_XE5_DOWN} 211 | {$UNDEF DELPHI_XE6_DOWN} 212 | {$UNDEF DELPHI_XE7_DOWN} 213 | {$UNDEF DELPHI_XE8_DOWN} 214 | {$ENDIF VER300} 215 | 216 | {$IFDEF VER310} // RAD Studio 10.1 Berlin 217 | {$DEFINE DELPHI_2010_UP} 218 | {$DEFINE DELPHI_XE_UP} 219 | {$DEFINE DELPHI_XE2_UP} 220 | {$DEFINE DELPHI_XE3_UP} 221 | {$DEFINE DELPHI_XE4_UP} 222 | {$DEFINE DELPHI_XE5_UP} 223 | {$DEFINE DELPHI_XE6_UP} 224 | {$DEFINE DELPHI_XE7} 225 | {$DEFINE DELPHI_XE7_UP} 226 | {$DEFINE DELPHI_XE8_UP} 227 | {$DEFINE DELPHIX_SEATTLE_UP} 228 | {$DEFINE DELPHIX_SEATTLE} 229 | {$DEFINE DELPHI_XE10_UP} 230 | {$DEFINE SUPPORTS_REGEX} 231 | {$DEFINE USE_NS} 232 | {$UNDEF DELPHI_2010_DOWN} 233 | {$UNDEF DELPHI_XE_DOWN} 234 | {$UNDEF DELPHI_XE2_DOWN} 235 | {$UNDEF DELPHI_XE3_DOWN} 236 | {$UNDEF DELPHI_XE4_DOWN} 237 | {$UNDEF DELPHI_XE5_DOWN} 238 | {$UNDEF DELPHI_XE6_DOWN} 239 | {$UNDEF DELPHI_XE7_DOWN} 240 | {$UNDEF DELPHI_XE8_DOWN} 241 | {$UNDEF DELPHIX_BERLIN_DOWN} 242 | 243 | {$ENDIF VER310} 244 | 245 | {$IFDEF VER320} // RAD Studio 10.2 Tokyo 246 | {$DEFINE DELPHI_2010_UP} 247 | {$DEFINE DELPHI_XE_UP} 248 | {$DEFINE DELPHI_XE2_UP} 249 | {$DEFINE DELPHI_XE3_UP} 250 | {$DEFINE DELPHI_XE4_UP} 251 | {$DEFINE DELPHI_XE5_UP} 252 | {$DEFINE DELPHI_XE6_UP} 253 | {$DEFINE DELPHI_XE7} 254 | {$DEFINE DELPHI_XE7_UP} 255 | {$DEFINE DELPHI_XE8_UP} 256 | {$DEFINE DELPHIX_SEATTLE_UP} 257 | {$DEFINE DELPHIX_SEATTLE} 258 | {$DEFINE DELPHI_XE10_UP} 259 | {$DEFINE DELPHI_XE101_UP} 260 | {$DEFINE SUPPORTS_REGEX} 261 | {$DEFINE USE_NS} 262 | {$UNDEF DELPHI_2010_DOWN} 263 | {$UNDEF DELPHI_XE_DOWN} 264 | {$UNDEF DELPHI_XE2_DOWN} 265 | {$UNDEF DELPHI_XE3_DOWN} 266 | {$UNDEF DELPHI_XE4_DOWN} 267 | {$UNDEF DELPHI_XE5_DOWN} 268 | {$UNDEF DELPHI_XE6_DOWN} 269 | {$UNDEF DELPHI_XE7_DOWN} 270 | {$UNDEF DELPHI_XE8_DOWN} 271 | {$UNDEF DELPHI_XE10_DOWN} 272 | {$UNDEF DELPHI_XE101_DOWN} 273 | {$ENDIF VER320} 274 | 275 | {$IFNDEF SUPPORTS_REGEX} 276 | {$IFDEF USE_TREGEXPR} 277 | {$DEFINE SUPPORTS_REGEX} 278 | {$ENDIF} 279 | {$ENDIF} 280 | 281 | {$I DUnitX.Stacktrace.inc} 282 | {$I DUnitX.MemoryLeaks.inc} 283 | 284 | //turn off debug info so you do not step into DUNITX when debugging your tests 285 | 286 | //Note from Ryan McGinty 04/25/2016 - I had legacy code in conditional IFDEF DEBUG 287 | //using XE. When I put in a directive of DUNITX-DEBUG in my project, it started 288 | //making the tests for IFDEF DEBUG true as well. I tested with an _ and had same 289 | //result, so added one without a spacer and it seems to work. Kept both values 290 | //as to not break any backward compatbilities. 291 | {$IFNDEF DUNITX-DEBUG} 292 | {$IFNDEF DUNITXDEBUG} 293 | {$D-} 294 | {$ENDIF} 295 | {$ENDIF} 296 | -------------------------------------------------------------------------------- /api_tests/TestWithFMXUnitX/FMXTestTensorFlow.dpr: -------------------------------------------------------------------------------- 1 | program FMXTestTensorFlow; 2 | 3 | uses 4 | FMX.Forms, 5 | System.SysUtils, 6 | DUNitX.Loggers.GUIX in 'DUNitX.Loggers.GUIX.pas' {GUIXTestRunner}, 7 | TensorFlow.DApiUnitTests in '..\TensorFlow.DApiUnitTests.pas', 8 | TensorFlow.LowLevelUnitTests in '..\TensorFlow.LowLevelUnitTests.pas', 9 | TensorFlow.LowLevelUnitTestsUtil in '..\TensorFlow.LowLevelUnitTestsUtil.pas', 10 | TensorFlow._Helpers in '..\..\api\TensorFlow._Helpers.pas', 11 | TensorFlow.DApi in '..\..\api\TensorFlow.DApi.pas', 12 | TensorFlow.DApiBase in '..\..\api\TensorFlow.DApiBase.pas', 13 | TensorFlow.DApiOperations in '..\..\api\TensorFlow.DApiOperations.pas', 14 | TensorFlow.LowLevelAPI in '..\..\api\TensorFlow.LowLevelAPI.pas'; 15 | 16 | {$R *.res} 17 | 18 | begin 19 | Application.Initialize; 20 | Application.CreateForm(TGUIXTestRunner, GUIXTestRunner); 21 | Application.Run; 22 | end. 23 | -------------------------------------------------------------------------------- /api_tests/TestWithFMXUnitX/FMXTestTensorFlow.res: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/api_tests/TestWithFMXUnitX/FMXTestTensorFlow.res -------------------------------------------------------------------------------- /api_tests/TestWithFMXUnitX/TestTensorFlow.res: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/api_tests/TestWithFMXUnitX/TestTensorFlow.res -------------------------------------------------------------------------------- /api_tests/TestWithVCLUnitX/DUnitX.Loggers.GUI.VCL.pas: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/api_tests/TestWithVCLUnitX/DUnitX.Loggers.GUI.VCL.pas -------------------------------------------------------------------------------- /api_tests/TestWithVCLUnitX/DUnitX.MemoryLeakMonitor.FastMM4.pas: -------------------------------------------------------------------------------- 1 | {***************************************************************************} 2 | { } 3 | { DUnitX } 4 | { } 5 | { Copyright (C) 2015 Vincent Parrett & Contributors } 6 | { } 7 | { vincent@finalbuilder.com } 8 | { http://www.finalbuilder.com } 9 | { } 10 | { } 11 | {***************************************************************************} 12 | { } 13 | { Licensed under the Apache License, Version 2.0 (the "License"); } 14 | { you may not use this file except in compliance with the License. } 15 | { You may obtain a copy of the License at } 16 | { } 17 | { http://www.apache.org/licenses/LICENSE-2.0 } 18 | { } 19 | { Unless required by applicable law or agreed to in writing, software } 20 | { distributed under the License is distributed on an "AS IS" BASIS, } 21 | { WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. } 22 | { See the License for the specific language governing permissions and } 23 | { limitations under the License. } 24 | { } 25 | {***************************************************************************} 26 | 27 | unit DUnitX.MemoryLeakMonitor.FastMM4; 28 | 29 | interface 30 | 31 | {$I DUnitX.inc} 32 | 33 | 34 | uses 35 | {$IFDEF USE_NS} 36 | System.Classes, 37 | {$ELSE} 38 | Classes, 39 | {$ENDIF} 40 | DUnitX.TestFramework; 41 | 42 | implementation 43 | 44 | uses 45 | DUnitX.MemoryLeakMonitor.Default, 46 | DUnitX.IoC; 47 | 48 | type 49 | TDUnitXFastMM4MemoryLeakMonitor = class(TInterfacedObject,IMemoryLeakMonitor) 50 | private 51 | FPreSetupAllocation : Int64; 52 | FPostSetupAllocation : Int64; 53 | FPreTestAllocation : Int64; 54 | FPostTestAllocation : Int64; 55 | FPreTearDownAllocation : Int64; 56 | FPostTearDownAllocation : Int64; 57 | 58 | function GetMemoryAllocated() : Int64; 59 | public 60 | procedure PreSetup; 61 | procedure PostSetUp; 62 | procedure PreTest; 63 | procedure PostTest; 64 | procedure PreTearDown; 65 | procedure PostTearDown; 66 | 67 | function SetUpMemoryAllocated: Int64; 68 | function TearDownMemoryAllocated: Int64; 69 | function TestMemoryAllocated: Int64; 70 | end; 71 | 72 | 73 | 74 | { TDUnitXFastMM4MemoryLeakMonitor } 75 | 76 | function TDUnitXFastMM4MemoryLeakMonitor.GetMemoryAllocated: Int64; 77 | var 78 | st: TMemoryManagerState; 79 | sb: TSmallBlockTypeState; 80 | begin 81 | GetMemoryManagerState(st); 82 | 83 | Result := st.TotalAllocatedMediumBlockSize + st.TotalAllocatedLargeBlockSize; 84 | 85 | for sb in st.SmallBlockTypeStates do 86 | begin 87 | Result := Result + Int64(sb.UseableBlockSize * sb.AllocatedBlockCount); 88 | end; 89 | end; 90 | 91 | procedure TDUnitXFastMM4MemoryLeakMonitor.PostSetUp; 92 | begin 93 | FPostSetupAllocation := GetMemoryAllocated(); 94 | end; 95 | 96 | procedure TDUnitXFastMM4MemoryLeakMonitor.PostTearDown; 97 | begin 98 | FPostTearDownAllocation := GetMemoryAllocated(); 99 | end; 100 | 101 | procedure TDUnitXFastMM4MemoryLeakMonitor.PostTest; 102 | begin 103 | FPostTestAllocation := GetMemoryAllocated(); 104 | end; 105 | 106 | procedure TDUnitXFastMM4MemoryLeakMonitor.PreSetup; 107 | begin 108 | FPreSetupAllocation := GetMemoryAllocated(); 109 | end; 110 | 111 | procedure TDUnitXFastMM4MemoryLeakMonitor.PreTearDown; 112 | begin 113 | FPreTearDownAllocation := GetMemoryAllocated(); 114 | end; 115 | 116 | procedure TDUnitXFastMM4MemoryLeakMonitor.PreTest; 117 | begin 118 | FPreTestAllocation := GetMemoryAllocated(); 119 | end; 120 | 121 | function TDUnitXFastMM4MemoryLeakMonitor.SetUpMemoryAllocated: Int64; 122 | begin 123 | Result := FPostSetupAllocation - FPreSetupAllocation; 124 | end; 125 | 126 | function TDUnitXFastMM4MemoryLeakMonitor.TearDownMemoryAllocated: Int64; 127 | begin 128 | Result := FPostTearDownAllocation - FPreTearDownAllocation; 129 | end; 130 | 131 | function TDUnitXFastMM4MemoryLeakMonitor.TestMemoryAllocated: Int64; 132 | begin 133 | Result := FPostTestAllocation - FPreTestAllocation; 134 | end; 135 | 136 | initialization 137 | {$IFDEF USE_FASTMM4_LEAK_MONITOR} 138 | TDUnitXIoC.DefaultContainer.RegisterType( 139 | function : IMemoryLeakMonitor 140 | begin 141 | result := TDUnitXFastMM4MemoryLeakMonitor.Create; 142 | end); 143 | {$ENDIF} 144 | 145 | end. 146 | 147 | -------------------------------------------------------------------------------- /api_tests/TestWithVCLUnitX/DUnitX.Stacktrace.inc: -------------------------------------------------------------------------------- 1 | {***************************************************************************} 2 | { } 3 | { DUnitX } 4 | { } 5 | { Copyright (C) 2015 Vincent Parrett & Contributors } 6 | { } 7 | { vincent@finalbuilder.com } 8 | { http://www.finalbuilder.com } 9 | { } 10 | { } 11 | {***************************************************************************} 12 | { } 13 | { Licensed under the Apache License, Version 2.0 (the "License"); } 14 | { you may not use this file except in compliance with the License. } 15 | { You may obtain a copy of the License at } 16 | { } 17 | { http://www.apache.org/licenses/LICENSE-2.0 } 18 | { } 19 | { Unless required by applicable law or agreed to in writing, software } 20 | { distributed under the License is distributed on an "AS IS" BASIS, } 21 | { WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. } 22 | { See the License for the specific language governing permissions and } 23 | { limitations under the License. } 24 | { } 25 | {***************************************************************************} 26 | 27 | 28 | // Uncomment to use JCL for stacktrace support. 29 | {.$DEFINE USE_JCL} 30 | 31 | // Uncomment to use MadExcept 3 for stacktrace support. 32 | {.$DEFINE USE_MADEXCEPT3} 33 | 34 | // Uncomment to use MadExcept 4 for stacktrace support. 35 | {.$DEFINE USE_MADEXCEPT4} 36 | 37 | // Uncomment to use EurekaLog 7 for stacktrace support. 38 | {.$DEFINE USE_EUREKALOG7} 39 | 40 | 41 | -------------------------------------------------------------------------------- /api_tests/TestWithVCLUnitX/DUnitX.inc: -------------------------------------------------------------------------------- 1 | {***************************************************************************} 2 | { } 3 | { DUnitX } 4 | { } 5 | { Copyright (C) 2017 Vincent Parrett & Contributors } 6 | { } 7 | { vincent@finalbuilder.com } 8 | { http://www.finalbuilder.com } 9 | { } 10 | { } 11 | {***************************************************************************} 12 | { } 13 | { Licensed under the Apache License, Version 2.0 (the "License"); } 14 | { you may not use this file except in compliance with the License. } 15 | { You may obtain a copy of the License at } 16 | { } 17 | { http://www.apache.org/licenses/LICENSE-2.0 } 18 | { } 19 | { Unless required by applicable law or agreed to in writing, software } 20 | { distributed under the License is distributed on an "AS IS" BASIS, } 21 | { WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. } 22 | { See the License for the specific language governing permissions and } 23 | { limitations under the License. } 24 | { } 25 | {***************************************************************************} 26 | 27 | //Basic Version of Compiler Supported 28 | {$IFDEF CONDITIONALEXPRESSIONS} //Started being defined with D2009 29 | {$IF CompilerVersion < 21.0} // Before RAD Studio 2010 30 | {$DEFINE UNSUPPORTED_COMPILER_VERSION} 31 | {$IFEND} 32 | {$ELSE} 33 | {$DEFINE UNSUPPORTED_COMPILER_VERSION} 34 | {$ENDIF} 35 | 36 | {$IFDEF UNSUPPORTED_COMPILER_VERSION} 37 | Unsupported Compiler Version (Delphi 2010 or later required!) 38 | {$ENDIF} 39 | 40 | //Use namespaces 41 | {$DEFINE USE_NS} 42 | 43 | {$DEFINE DELPHIX_BERLIN_DOWN} 44 | {$DEFINE DELPHIX_SEATTLE_DOWN} 45 | {$DEFINE DELPHI_XE8_DOWN} 46 | {$DEFINE DELPHI_XE7_DOWN} 47 | {$DEFINE DELPHI_XE6_DOWN} 48 | {$DEFINE DELPHI_XE5_DOWN} 49 | {$DEFINE DELPHI_XE4_DOWN} 50 | {$DEFINE DELPHI_XE3_DOWN} 51 | {$DEFINE DELPHI_XE2_DOWN} 52 | {$DEFINE DELPHI_XE_DOWN} 53 | {$DEFINE DELPHI_2010_DOWN} 54 | 55 | {$IFDEF VER210} // RAD Studio 2010 56 | {$DEFINE DELPHI_2010} 57 | {$DEFINE DELPHI_2010_UP} 58 | {$DEFINE CPUX86} 59 | {$UNDEF USE_NS} 60 | {$ENDIF VER210} 61 | 62 | {$IFDEF VER220} // RAD Studio XE 63 | {$DEFINE DELPHI_2010_UP} 64 | {$DEFINE DELPHI_XE} 65 | {$DEFINE DELPHI_XE_UP} 66 | {$DEFINE CPUX86} 67 | {$UNDEF DELPHI_2010_DOWN} 68 | {$UNDEF USE_NS} 69 | {$ENDIF VER220} 70 | 71 | {$IFDEF VER230} // RAD Studio XE2 72 | {$DEFINE DELPHI_2010_UP} 73 | {$DEFINE DELPHI_XE_UP} 74 | {$DEFINE DELPHI_XE2} 75 | {$DEFINE DELPHI_XE2_UP} 76 | {$DEFINE SUPPORTS_REGEX} 77 | {$DEFINE USE_NS} 78 | {$UNDEF DELPHI_2010_DOWN} 79 | {$UNDEF DELPHI_XE_DOWN} 80 | {$ENDIF VER230} 81 | 82 | {$IFDEF VER240} // RAD Studio XE3 83 | {$DEFINE DELPHI_2010_UP} 84 | {$DEFINE DELPHI_XE_UP} 85 | {$DEFINE DELPHI_XE2_UP} 86 | {$DEFINE DELPHI_XE3} 87 | {$DEFINE DELPHI_XE3_UP} 88 | {$DEFINE SUPPORTS_REGEX} 89 | {$UNDEF DELPHI_2010_DOWN} 90 | {$UNDEF DELPHI_XE_DOWN} 91 | {$UNDEF DELPHI_XE2_DOWN} 92 | {$ENDIF VER240} 93 | 94 | {$IFDEF VER250} // RAD Studio XE4 95 | {$DEFINE DELPHI_2010_UP} 96 | {$DEFINE DELPHI_XE_UP} 97 | {$DEFINE DELPHI_XE2_UP} 98 | {$DEFINE DELPHI_XE3_UP} 99 | {$DEFINE DELPHI_XE4} 100 | {$DEFINE DELPHI_XE4_UP} 101 | {$DEFINE SUPPORTS_REGEX} 102 | {$DEFINE USE_NS} 103 | {$UNDEF DELPHI_2010_DOWN} 104 | {$UNDEF DELPHI_XE_DOWN} 105 | {$UNDEF DELPHI_XE2_DOWN} 106 | {$UNDEF DELPHI_XE3_DOWN} 107 | {$ENDIF VER250} 108 | 109 | {$IFDEF VER260} // RAD Studio XE5 110 | {$DEFINE DELPHI_2010_UP} 111 | {$DEFINE DELPHI_XE_UP} 112 | {$DEFINE DELPHI_XE2_UP} 113 | {$DEFINE DELPHI_XE3_UP} 114 | {$DEFINE DELPHI_XE4_UP} 115 | {$DEFINE DELPHI_XE5} 116 | {$DEFINE DELPHI_XE5_UP} 117 | {$DEFINE SUPPORTS_REGEX} 118 | {$DEFINE USE_NS} 119 | {$UNDEF DELPHI_2010_DOWN} 120 | {$UNDEF DELPHI_XE_DOWN} 121 | {$UNDEF DELPHI_XE2_DOWN} 122 | {$UNDEF DELPHI_XE3_DOWN} 123 | {$UNDEF DELPHI_XE4_DOWN} 124 | {$ENDIF VER260} 125 | 126 | {$IFDEF VER270} // RAD Studio XE6 127 | {$DEFINE DELPHI_2010_UP} 128 | {$DEFINE DELPHI_XE_UP} 129 | {$DEFINE DELPHI_XE2_UP} 130 | {$DEFINE DELPHI_XE3_UP} 131 | {$DEFINE DELPHI_XE4_UP} 132 | {$DEFINE DELPHI_XE5_UP} 133 | {$DEFINE DELPHI_XE6} 134 | {$DEFINE DELPHI_XE6_UP} 135 | {$DEFINE SUPPORTS_REGEX} 136 | {$DEFINE USE_NS} 137 | {$UNDEF DELPHI_2010_DOWN} 138 | {$UNDEF DELPHI_XE_DOWN} 139 | {$UNDEF DELPHI_XE2_DOWN} 140 | {$UNDEF DELPHI_XE3_DOWN} 141 | {$UNDEF DELPHI_XE4_DOWN} 142 | {$UNDEF DELPHI_XE5_DOWN} 143 | {$ENDIF VER270} 144 | 145 | {$IFDEF VER280} // RAD Studio XE7 146 | {$DEFINE DELPHI_2010_UP} 147 | {$DEFINE DELPHI_XE_UP} 148 | {$DEFINE DELPHI_XE2_UP} 149 | {$DEFINE DELPHI_XE3_UP} 150 | {$DEFINE DELPHI_XE4_UP} 151 | {$DEFINE DELPHI_XE5_UP} 152 | {$DEFINE DELPHI_XE6_UP} 153 | {$DEFINE DELPHI_XE7} 154 | {$DEFINE DELPHI_XE7_UP} 155 | {$DEFINE SUPPORTS_REGEX} 156 | {$DEFINE USE_NS} 157 | {$UNDEF DELPHI_2010_DOWN} 158 | {$UNDEF DELPHI_XE_DOWN} 159 | {$UNDEF DELPHI_XE2_DOWN} 160 | {$UNDEF DELPHI_XE3_DOWN} 161 | {$UNDEF DELPHI_XE4_DOWN} 162 | {$UNDEF DELPHI_XE5_DOWN} 163 | {$UNDEF DELPHI_XE6_DOWN} 164 | {$ENDIF VER280} 165 | 166 | {$IFDEF VER290} // RAD Studio XE8 167 | {$DEFINE DELPHI_2010_UP} 168 | {$DEFINE DELPHI_XE_UP} 169 | {$DEFINE DELPHI_XE2_UP} 170 | {$DEFINE DELPHI_XE3_UP} 171 | {$DEFINE DELPHI_XE4_UP} 172 | {$DEFINE DELPHI_XE5_UP} 173 | {$DEFINE DELPHI_XE6_UP} 174 | {$DEFINE DELPHI_XE7} 175 | {$DEFINE DELPHI_XE7_UP} 176 | {$DEFINE DELPHI_XE8_UP} 177 | 178 | {$DEFINE SUPPORTS_REGEX} 179 | {$DEFINE USE_NS} 180 | {$UNDEF DELPHI_2010_DOWN} 181 | {$UNDEF DELPHI_XE_DOWN} 182 | {$UNDEF DELPHI_XE2_DOWN} 183 | {$UNDEF DELPHI_XE3_DOWN} 184 | {$UNDEF DELPHI_XE4_DOWN} 185 | {$UNDEF DELPHI_XE5_DOWN} 186 | {$UNDEF DELPHI_XE6_DOWN} 187 | {$UNDEF DELPHI_XE7_DOWN} 188 | {$ENDIF VER290} 189 | 190 | {$IFDEF VER300} // RAD Studio 10 Seattle 191 | {$DEFINE DELPHI_2010_UP} 192 | {$DEFINE DELPHI_XE_UP} 193 | {$DEFINE DELPHI_XE2_UP} 194 | {$DEFINE DELPHI_XE3_UP} 195 | {$DEFINE DELPHI_XE4_UP} 196 | {$DEFINE DELPHI_XE5_UP} 197 | {$DEFINE DELPHI_XE6_UP} 198 | {$DEFINE DELPHI_XE7} 199 | {$DEFINE DELPHI_XE7_UP} 200 | {$DEFINE DELPHI_XE8_UP} 201 | {$DEFINE DELPHIX_SEATTLE_UP} 202 | {$DEFINE DELPHIX_SEATTLE} 203 | {$DEFINE SUPPORTS_REGEX} 204 | {$DEFINE USE_NS} 205 | {$UNDEF DELPHI_2010_DOWN} 206 | {$UNDEF DELPHI_XE_DOWN} 207 | {$UNDEF DELPHI_XE2_DOWN} 208 | {$UNDEF DELPHI_XE3_DOWN} 209 | {$UNDEF DELPHI_XE4_DOWN} 210 | {$UNDEF DELPHI_XE5_DOWN} 211 | {$UNDEF DELPHI_XE6_DOWN} 212 | {$UNDEF DELPHI_XE7_DOWN} 213 | {$UNDEF DELPHI_XE8_DOWN} 214 | {$ENDIF VER300} 215 | 216 | {$IFDEF VER310} // RAD Studio 10.1 Berlin 217 | {$DEFINE DELPHI_2010_UP} 218 | {$DEFINE DELPHI_XE_UP} 219 | {$DEFINE DELPHI_XE2_UP} 220 | {$DEFINE DELPHI_XE3_UP} 221 | {$DEFINE DELPHI_XE4_UP} 222 | {$DEFINE DELPHI_XE5_UP} 223 | {$DEFINE DELPHI_XE6_UP} 224 | {$DEFINE DELPHI_XE7} 225 | {$DEFINE DELPHI_XE7_UP} 226 | {$DEFINE DELPHI_XE8_UP} 227 | {$DEFINE DELPHIX_SEATTLE_UP} 228 | {$DEFINE DELPHIX_SEATTLE} 229 | {$DEFINE DELPHI_XE10_UP} 230 | {$DEFINE SUPPORTS_REGEX} 231 | {$DEFINE USE_NS} 232 | {$UNDEF DELPHI_2010_DOWN} 233 | {$UNDEF DELPHI_XE_DOWN} 234 | {$UNDEF DELPHI_XE2_DOWN} 235 | {$UNDEF DELPHI_XE3_DOWN} 236 | {$UNDEF DELPHI_XE4_DOWN} 237 | {$UNDEF DELPHI_XE5_DOWN} 238 | {$UNDEF DELPHI_XE6_DOWN} 239 | {$UNDEF DELPHI_XE7_DOWN} 240 | {$UNDEF DELPHI_XE8_DOWN} 241 | {$UNDEF DELPHIX_BERLIN_DOWN} 242 | 243 | {$ENDIF VER310} 244 | 245 | {$IFDEF VER320} // RAD Studio 10.2 Tokyo 246 | {$DEFINE DELPHI_2010_UP} 247 | {$DEFINE DELPHI_XE_UP} 248 | {$DEFINE DELPHI_XE2_UP} 249 | {$DEFINE DELPHI_XE3_UP} 250 | {$DEFINE DELPHI_XE4_UP} 251 | {$DEFINE DELPHI_XE5_UP} 252 | {$DEFINE DELPHI_XE6_UP} 253 | {$DEFINE DELPHI_XE7} 254 | {$DEFINE DELPHI_XE7_UP} 255 | {$DEFINE DELPHI_XE8_UP} 256 | {$DEFINE DELPHIX_SEATTLE_UP} 257 | {$DEFINE DELPHIX_SEATTLE} 258 | {$DEFINE DELPHI_XE10_UP} 259 | {$DEFINE DELPHI_XE101_UP} 260 | {$DEFINE SUPPORTS_REGEX} 261 | {$DEFINE USE_NS} 262 | {$UNDEF DELPHI_2010_DOWN} 263 | {$UNDEF DELPHI_XE_DOWN} 264 | {$UNDEF DELPHI_XE2_DOWN} 265 | {$UNDEF DELPHI_XE3_DOWN} 266 | {$UNDEF DELPHI_XE4_DOWN} 267 | {$UNDEF DELPHI_XE5_DOWN} 268 | {$UNDEF DELPHI_XE6_DOWN} 269 | {$UNDEF DELPHI_XE7_DOWN} 270 | {$UNDEF DELPHI_XE8_DOWN} 271 | {$UNDEF DELPHI_XE10_DOWN} 272 | {$UNDEF DELPHI_XE101_DOWN} 273 | {$ENDIF VER320} 274 | 275 | {$IFNDEF SUPPORTS_REGEX} 276 | {$IFDEF USE_TREGEXPR} 277 | {$DEFINE SUPPORTS_REGEX} 278 | {$ENDIF} 279 | {$ENDIF} 280 | 281 | {$I DUnitX.Stacktrace.inc} 282 | {$I DUnitX.MemoryLeaks.inc} 283 | 284 | //turn off debug info so you do not step into DUNITX when debugging your tests 285 | 286 | //Note from Ryan McGinty 04/25/2016 - I had legacy code in conditional IFDEF DEBUG 287 | //using XE. When I put in a directive of DUNITX-DEBUG in my project, it started 288 | //making the tests for IFDEF DEBUG true as well. I tested with an _ and had same 289 | //result, so added one without a spacer and it seems to work. Kept both values 290 | //as to not break any backward compatbilities. 291 | {$IFNDEF DUNITX-DEBUG} 292 | {$IFNDEF DUNITXDEBUG} 293 | {$D-} 294 | {$ENDIF} 295 | {$ENDIF} 296 | -------------------------------------------------------------------------------- /api_tests/TestWithVCLUnitX/HexDataInterpreter.dfm: -------------------------------------------------------------------------------- 1 | object FormDataInterpreter: TFormDataInterpreter 2 | Left = 0 3 | Top = 0 4 | BorderStyle = bsToolWindow 5 | Caption = 'Hex Data Interpreter' 6 | ClientHeight = 348 7 | ClientWidth = 407 8 | Color = clBtnFace 9 | Font.Charset = DEFAULT_CHARSET 10 | Font.Color = clWindowText 11 | Font.Height = -11 12 | Font.Name = 'Tahoma' 13 | Font.Style = [] 14 | FormStyle = fsStayOnTop 15 | OldCreateOrder = False 16 | OnActivate = FormActivate 17 | OnShow = FormShow 18 | PixelsPerInch = 96 19 | TextHeight = 13 20 | object Label1: TLabel 21 | Left = 16 22 | Top = 19 23 | Width = 19 24 | Height = 13 25 | Caption = 'Hex' 26 | end 27 | object Label2: TLabel 28 | Left = 327 29 | Top = 324 30 | Width = 54 31 | Height = 10 32 | Caption = 'Win32: 10 Byte' 33 | Font.Charset = DEFAULT_CHARSET 34 | Font.Color = clWindowText 35 | Font.Height = -8 36 | Font.Name = 'Tahoma' 37 | Font.Style = [] 38 | ParentFont = False 39 | end 40 | object Label3: TLabel 41 | Left = 327 42 | Top = 334 43 | Width = 53 44 | Height = 10 45 | Caption = 'Win64: 8 Byte' 46 | Font.Charset = DEFAULT_CHARSET 47 | Font.Color = clWindowText 48 | Font.Height = -8 49 | Font.Name = 'Tahoma' 50 | Font.Style = [] 51 | ParentFont = False 52 | end 53 | object txfHex: TEdit 54 | Left = 120 55 | Top = 16 56 | Width = 249 57 | Height = 21 58 | TabOrder = 0 59 | OnExit = txfHexExit 60 | end 61 | object chbxInt8Signed: TCheckBox 62 | Left = 16 63 | Top = 56 64 | Width = 89 65 | Height = 17 66 | Caption = 'Int8, signed' 67 | Checked = True 68 | State = cbChecked 69 | TabOrder = 1 70 | OnClick = chbxDataTypeChangeClick 71 | end 72 | object txfInt8Signed: TEdit 73 | Left = 120 74 | Top = 54 75 | Width = 201 76 | Height = 21 77 | Color = clBtnFace 78 | ReadOnly = True 79 | TabOrder = 2 80 | end 81 | object txfInt8Unsigned: TEdit 82 | Left = 120 83 | Top = 81 84 | Width = 201 85 | Height = 21 86 | Color = clBtnFace 87 | ReadOnly = True 88 | TabOrder = 3 89 | end 90 | object chbxInt8Unsigned: TCheckBox 91 | Left = 16 92 | Top = 83 93 | Width = 98 94 | Height = 17 95 | Caption = 'Int8, unsigned' 96 | TabOrder = 4 97 | OnClick = chbxDataTypeChangeClick 98 | end 99 | object txfInt16Signed: TEdit 100 | Left = 120 101 | Top = 108 102 | Width = 201 103 | Height = 21 104 | Color = clBtnFace 105 | ReadOnly = True 106 | TabOrder = 5 107 | end 108 | object chbxInt16Signed: TCheckBox 109 | Left = 16 110 | Top = 110 111 | Width = 98 112 | Height = 17 113 | Caption = 'Int16, signed' 114 | TabOrder = 6 115 | OnClick = chbxDataTypeChangeClick 116 | end 117 | object txfInt16Unsigned: TEdit 118 | Left = 120 119 | Top = 135 120 | Width = 201 121 | Height = 21 122 | Color = clBtnFace 123 | ReadOnly = True 124 | TabOrder = 7 125 | end 126 | object chbxInt16Unsigned: TCheckBox 127 | Left = 16 128 | Top = 137 129 | Width = 98 130 | Height = 17 131 | Caption = 'Int16, unsigned' 132 | TabOrder = 8 133 | OnClick = chbxDataTypeChangeClick 134 | end 135 | object txfInt32Signed: TEdit 136 | Left = 120 137 | Top = 162 138 | Width = 201 139 | Height = 21 140 | Color = clBtnFace 141 | ReadOnly = True 142 | TabOrder = 9 143 | end 144 | object chbxInt32Signed: TCheckBox 145 | Left = 16 146 | Top = 164 147 | Width = 98 148 | Height = 17 149 | Caption = 'Int32, signed' 150 | Checked = True 151 | State = cbChecked 152 | TabOrder = 10 153 | OnClick = chbxDataTypeChangeClick 154 | end 155 | object txfInt32Unsigned: TEdit 156 | Left = 120 157 | Top = 189 158 | Width = 201 159 | Height = 21 160 | Color = clBtnFace 161 | ReadOnly = True 162 | TabOrder = 11 163 | end 164 | object chbxInt32Unsigned: TCheckBox 165 | Left = 16 166 | Top = 191 167 | Width = 98 168 | Height = 17 169 | Caption = 'Int32, unsigned' 170 | TabOrder = 12 171 | OnClick = chbxDataTypeChangeClick 172 | end 173 | object txfInt64Signed: TEdit 174 | Left = 120 175 | Top = 216 176 | Width = 201 177 | Height = 21 178 | Color = clBtnFace 179 | ReadOnly = True 180 | TabOrder = 13 181 | end 182 | object chbxInt64Signed: TCheckBox 183 | Left = 16 184 | Top = 218 185 | Width = 98 186 | Height = 17 187 | Caption = 'Int64, signed' 188 | TabOrder = 14 189 | OnClick = chbxDataTypeChangeClick 190 | end 191 | object txfInt64Unsigned: TEdit 192 | Left = 120 193 | Top = 241 194 | Width = 201 195 | Height = 21 196 | Color = clBtnFace 197 | ReadOnly = True 198 | TabOrder = 15 199 | end 200 | object chbxInt64Unsigned: TCheckBox 201 | Left = 16 202 | Top = 243 203 | Width = 98 204 | Height = 17 205 | Caption = 'Int64, unsigned' 206 | TabOrder = 16 207 | OnClick = chbxDataTypeChangeClick 208 | end 209 | object chbxSingle: TCheckBox 210 | Left = 16 211 | Top = 268 212 | Width = 98 213 | Height = 17 214 | Caption = 'Float / Single' 215 | TabOrder = 17 216 | OnClick = chbxDataTypeChangeClick 217 | end 218 | object txfSingle: TEdit 219 | Left = 120 220 | Top = 268 221 | Width = 201 222 | Height = 21 223 | Color = clBtnFace 224 | ReadOnly = True 225 | TabOrder = 18 226 | end 227 | object txfDouble: TEdit 228 | Left = 120 229 | Top = 295 230 | Width = 201 231 | Height = 21 232 | Color = clBtnFace 233 | ReadOnly = True 234 | TabOrder = 19 235 | end 236 | object chbxDouble: TCheckBox 237 | Left = 16 238 | Top = 295 239 | Width = 98 240 | Height = 17 241 | Caption = 'Double' 242 | TabOrder = 20 243 | OnClick = chbxDataTypeChangeClick 244 | end 245 | object txfExtended: TEdit 246 | Left = 120 247 | Top = 322 248 | Width = 201 249 | Height = 21 250 | Color = clBtnFace 251 | ReadOnly = True 252 | TabOrder = 21 253 | end 254 | object chbxExtended: TCheckBox 255 | Left = 16 256 | Top = 322 257 | Width = 98 258 | Height = 17 259 | Caption = 'Extended' 260 | TabOrder = 22 261 | OnClick = chbxDataTypeChangeClick 262 | end 263 | end 264 | -------------------------------------------------------------------------------- /api_tests/TestWithVCLUnitX/HexDataInterpreter.pas: -------------------------------------------------------------------------------- 1 | { 2 | Licensed under the Apache License, Version 2.0 (the "License"); 3 | you may not use this file except in compliance with the License. 4 | You may obtain a copy of the License at 5 | 6 | http://www.apache.org/licenses/LICENSE-2.0 7 | 8 | Unless required by applicable law or agreed to in writing, software 9 | distributed under the License is distributed on an "AS IS" BASIS, 10 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | See the License for the specific language governing permissions and 12 | limitations under the License. 13 | 14 | ==============================================================================} 15 | 16 | unit HexDataInterpreter; 17 | 18 | interface 19 | 20 | uses 21 | Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 22 | Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Types, System.StrUtils; 23 | 24 | type 25 | PInt8 = ^Int8; 26 | PUInt8 = ^UInt8; 27 | PInt16 = ^Int16; 28 | PUInt16 = ^UInt16; 29 | PInt32 = ^Int32; 30 | PUInt32 = ^UInt32; 31 | PInt64 = ^Int64; 32 | PUInt64 = ^UInt64; 33 | 34 | TFormDataInterpreter = class(TForm) 35 | txfHex: TEdit; 36 | Label1: TLabel; 37 | chbxInt8Signed: TCheckBox; 38 | txfInt8Signed: TEdit; 39 | txfInt8Unsigned: TEdit; 40 | chbxInt8Unsigned: TCheckBox; 41 | txfInt16Signed: TEdit; 42 | chbxInt16Signed: TCheckBox; 43 | txfInt16Unsigned: TEdit; 44 | chbxInt16Unsigned: TCheckBox; 45 | txfInt32Signed: TEdit; 46 | chbxInt32Signed: TCheckBox; 47 | txfInt32Unsigned: TEdit; 48 | chbxInt32Unsigned: TCheckBox; 49 | txfInt64Signed: TEdit; 50 | chbxInt64Signed: TCheckBox; 51 | txfInt64Unsigned: TEdit; 52 | chbxInt64Unsigned: TCheckBox; 53 | chbxSingle: TCheckBox; 54 | txfSingle: TEdit; 55 | txfDouble: TEdit; 56 | chbxDouble: TCheckBox; 57 | txfExtended: TEdit; 58 | chbxExtended: TCheckBox; 59 | Label2: TLabel; 60 | Label3: TLabel; 61 | procedure txfHexExit(Sender: TObject); 62 | procedure FormActivate(Sender: TObject); 63 | procedure FormShow(Sender: TObject); 64 | procedure chbxDataTypeChangeClick(Sender: TObject); 65 | private 66 | { Private-Deklarationen } 67 | procedure ClearFields; 68 | procedure Interprete; 69 | public 70 | { Public-Deklarationen } 71 | procedure SetHexText(aHexText: String); 72 | end; 73 | 74 | var 75 | FormDataInterpreter: TFormDataInterpreter; 76 | 77 | implementation 78 | 79 | {$R *.dfm} 80 | 81 | procedure TFormDataInterpreter.FormActivate(Sender: TObject); 82 | begin 83 | // 84 | end; 85 | 86 | procedure TFormDataInterpreter.FormShow(Sender: TObject); 87 | begin 88 | self.ClearFields; 89 | if Length(txfHex.Text) > 0 then 90 | self.Interprete; 91 | end; 92 | 93 | procedure TFormDataInterpreter.txfHexExit(Sender: TObject); 94 | begin 95 | if Length(txfHex.Text) > 0 then 96 | self.Interprete 97 | else 98 | self.ClearFields; 99 | end; 100 | 101 | procedure TFormDataInterpreter.chbxDataTypeChangeClick(Sender: TObject); 102 | begin 103 | if Length(txfHex.Text) > 0 then 104 | self.Interprete; 105 | end; 106 | 107 | procedure TFormDataInterpreter.ClearFields; 108 | begin 109 | txfInt8Signed.Clear; 110 | txfInt8Unsigned.Clear; 111 | txfInt16Signed.Clear; 112 | txfInt16Unsigned.Clear; 113 | txfInt32Signed.Clear; 114 | txfInt32Unsigned.Clear; 115 | txfInt64Signed.Clear; 116 | txfInt64Unsigned.Clear; 117 | txfSingle.Clear; 118 | txfDouble.Clear; 119 | txfExtended.Clear; 120 | end; 121 | 122 | procedure TFormDataInterpreter.SetHexText(aHexText: String); 123 | begin 124 | txfHex.Text := aHexText; 125 | self.Interprete; 126 | end; 127 | 128 | procedure TFormDataInterpreter.Interprete; 129 | var 130 | l_sStr: String; 131 | l_aStrings: TStringDynArray; 132 | i, lng, l_iHex: Integer; 133 | l_sAnsiText: AnsiString; 134 | l_pAnsiChar: PAnsiChar; 135 | l_iInt8: Int8; 136 | l_iUInt8: UInt8; 137 | l_iInt16: Int16; 138 | l_iUInt16: UInt16; 139 | l_iInt32: Int32; 140 | l_iUInt32: UInt32; 141 | l_iInt64: Int64; 142 | l_iUInt64: UInt64; 143 | l_fSingle: Single; 144 | l_fDouble: Double; 145 | l_fExtended: Extended; 146 | l_pInt8: PInt8; 147 | l_pUInt8: PUInt8; 148 | l_pInt16: PInt16; 149 | l_pUInt16: PUInt16; 150 | l_pInt32: PInt32; 151 | l_pUInt32: PUInt32; 152 | l_pInt64: PInt64; 153 | l_pUInt64: PUInt64; 154 | l_pSingle: PSingle; 155 | l_pDouble: PDouble; 156 | l_pExtended: PExtended; 157 | begin 158 | l_sStr := Trim(txfHex.Text); 159 | lng := Length(l_sStr); 160 | SetLength(l_sAnsiText,lng); 161 | self.ClearFields; 162 | l_aStrings := SplitString(l_sStr,' '); 163 | for i := 0 to Length(l_aStrings)-1 do begin 164 | l_sStr := '$' + l_aStrings[i]; 165 | l_iHex := StrToInt(l_sStr); 166 | l_sAnsiText[i+1] := AnsiChar(l_iHex); 167 | end; 168 | l_pAnsiChar := PAnsiChar(@(l_sAnsiText[1])); 169 | if chbxInt8Signed.Checked then begin 170 | l_pInt8 := PInt8(l_pAnsiChar); 171 | l_iInt8 := l_pInt8^; 172 | try 173 | txfInt8Signed.Text := IntToStr(l_iInt8); 174 | except end; 175 | end; 176 | if chbxInt8Unsigned.Checked then begin 177 | l_pUInt8 := PUInt8(l_pAnsiChar); 178 | l_iUInt8 := l_pUInt8^; 179 | try 180 | txfInt8Unsigned.Text := IntToStr(l_iUInt8); 181 | except end; 182 | end; 183 | if chbxInt16Signed.Checked then begin 184 | l_pInt16 := PInt16(l_pAnsiChar); 185 | l_iInt16 := l_pInt16^; 186 | try 187 | txfInt16Signed.Text := IntToStr(l_iInt16); 188 | except end; 189 | end; 190 | if chbxInt16Unsigned.Checked then begin 191 | l_pUInt16 := PUInt16(l_pAnsiChar); 192 | l_iUInt16 := l_pUInt16^; 193 | try 194 | txfInt16Unsigned.Text := IntToStr(l_iUInt16); 195 | except end; 196 | end; 197 | if chbxInt32Signed.Checked then begin 198 | l_pInt32 := PInt32(l_pAnsiChar); 199 | l_iInt32 := l_pInt32^; 200 | try 201 | txfInt32Signed.Text := IntToStr(l_iInt32); 202 | except end; 203 | end; 204 | if chbxInt32Unsigned.Checked then begin 205 | l_pUInt32 := PUInt32(l_pAnsiChar); 206 | l_iUInt32 := l_pUInt32^; 207 | try 208 | txfInt32Unsigned.Text := UIntToStr(l_iUInt32); 209 | except end; 210 | end; 211 | if chbxInt64Signed.Checked then begin 212 | l_pInt64 := PInt64(l_pAnsiChar); 213 | l_iInt64 := l_pInt64^; 214 | try 215 | txfInt64Signed.Text := IntToStr(l_iInt64); 216 | except end; 217 | end; 218 | if chbxInt64Unsigned.Checked then begin 219 | l_pUInt64 := PUInt64(l_pAnsiChar); 220 | l_iUInt64 := l_pUInt64^; 221 | try 222 | txfInt64Unsigned.Text := UIntToStr(l_iUInt64); 223 | except end; 224 | end; 225 | if chbxSingle.Checked then begin 226 | l_pSingle := PSingle(l_pAnsiChar); 227 | l_fSingle := l_pSingle^; 228 | try 229 | txfSingle.Text := FloatToStr(l_fSingle); 230 | except end; 231 | end; 232 | if chbxDouble.Checked then begin 233 | l_pDouble := PDouble(l_pAnsiChar); 234 | l_fDouble := l_pDouble^; 235 | try 236 | txfDouble.Text := FloatToStr(l_fDouble); 237 | except end; 238 | end; 239 | if chbxExtended.Checked then begin 240 | l_pExtended := PExtended(l_pAnsiChar); 241 | l_fExtended := l_pExtended^; 242 | try 243 | txfExtended.Text := FloatToStr(l_fExtended); 244 | except end; 245 | end; 246 | end; 247 | 248 | end. 249 | -------------------------------------------------------------------------------- /api_tests/TestWithVCLUnitX/VCLTestTensorFlow.dpr: -------------------------------------------------------------------------------- 1 | program VCLTestTensorFlow; 2 | 3 | {$R *.res} 4 | 5 | uses 6 | {$IF DEFINED(MSWINDOWS) and DEFINED(DEBUG)} 7 | FastMM4 in '..\..\third_party\FastMM4\FastMM4.pas', 8 | {$ENDIF } 9 | Vcl.Forms, 10 | System.SysUtils, 11 | FastMM4Messages in '..\..\third_party\FastMM4\FastMM4Messages.pas', 12 | DUnitX.Loggers.GUI.VCL in 'DUnitX.Loggers.GUI.VCL.pas' {GUIVCLTestRunner}, 13 | DUnitX.ResStrs in 'DUnitX.ResStrs.pas', 14 | ATBinHex in '..\..\third_party\ATViewer\ATBinHex.pas', 15 | HexDataInterpreter in 'HexDataInterpreter.pas' {FormDataInterpreter}, 16 | DUnitX.MemoryLeakMonitor.FastMM4 in 'DUnitX.MemoryLeakMonitor.FastMM4.pas', 17 | TensorFlow.LowLevelAPI in '..\..\api\TensorFlow.LowLevelAPI.pas', 18 | TensorFlow.DApi in '..\..\api\TensorFlow.DApi.pas', 19 | TensorFlow.DApiOperations in '..\..\api\TensorFlow.DApiOperations.pas', 20 | TensorFlow.DApiBase in '..\..\api\TensorFlow.DApiBase.pas', 21 | TensorFlow._Helpers in '..\..\api\TensorFlow._Helpers.pas', 22 | TensorFlow.DApiUnitTests in '..\TensorFlow.DApiUnitTests.pas', 23 | TensorFlow.LowLevelUnitTests in '..\TensorFlow.LowLevelUnitTests.pas', 24 | TensorFlow.LowLevelUnitTestsUtil in '..\TensorFlow.LowLevelUnitTestsUtil.pas'; 25 | 26 | begin 27 | Application.Initialize; 28 | Application.CreateForm(TGUIVCLTestRunner, GUIVCLTestRunner); 29 | Application.CreateForm(TFormDataInterpreter, FormDataInterpreter); 30 | Application.Run; 31 | end. 32 | -------------------------------------------------------------------------------- /api_tests/TestWithVCLUnitX/VCLTestTensorFlow.res: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/api_tests/TestWithVCLUnitX/VCLTestTensorFlow.res -------------------------------------------------------------------------------- /bin/FastMM_FullDebugMode64.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/bin/FastMM_FullDebugMode64.dll -------------------------------------------------------------------------------- /bin/SavedModels/half_plus_two/00000123/assets/foo.txt: -------------------------------------------------------------------------------- 1 | asset-file-contents -------------------------------------------------------------------------------- /bin/SavedModels/half_plus_two/00000123/saved_model.pb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/bin/SavedModels/half_plus_two/00000123/saved_model.pb -------------------------------------------------------------------------------- /bin/SavedModels/half_plus_two/00000123/variables/variables.data-00000-of-00001: -------------------------------------------------------------------------------- 1 | ?@@@ -------------------------------------------------------------------------------- /bin/SavedModels/half_plus_two/00000123/variables/variables.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/bin/SavedModels/half_plus_two/00000123/variables/variables.index -------------------------------------------------------------------------------- /bin/SavedModels/half_plus_two_main_op/00000123/assets/foo.txt: -------------------------------------------------------------------------------- 1 | asset-file-contents -------------------------------------------------------------------------------- /bin/SavedModels/half_plus_two_main_op/00000123/saved_model.pb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/bin/SavedModels/half_plus_two_main_op/00000123/saved_model.pb -------------------------------------------------------------------------------- /bin/SavedModels/half_plus_two_main_op/00000123/variables/variables.data-00000-of-00001: -------------------------------------------------------------------------------- 1 | ?@@@ -------------------------------------------------------------------------------- /bin/SavedModels/half_plus_two_main_op/00000123/variables/variables.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/bin/SavedModels/half_plus_two_main_op/00000123/variables/variables.index -------------------------------------------------------------------------------- /bin/SavedModels/half_plus_two_pbtxt/00000123/assets/foo.txt: -------------------------------------------------------------------------------- 1 | asset-file-contents -------------------------------------------------------------------------------- /bin/SavedModels/half_plus_two_pbtxt/00000123/variables/variables.data-00000-of-00001: -------------------------------------------------------------------------------- 1 | ?@@@ -------------------------------------------------------------------------------- /bin/SavedModels/half_plus_two_pbtxt/00000123/variables/variables.index: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/bin/SavedModels/half_plus_two_pbtxt/00000123/variables/variables.index -------------------------------------------------------------------------------- /bin/tensorflow.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/bin/tensorflow.dll -------------------------------------------------------------------------------- /c_api_extensions/c_api_ex.h: -------------------------------------------------------------------------------- 1 | #ifndef TENSORFLOW_C_C_API_EX_H_ 2 | #define TENSORFLOW_C_C_API_EX_H_ 3 | 4 | #include 5 | #include 6 | #include "tensorflow/c/c_api.h" 7 | 8 | // -------------------------------------------------------------------------- 9 | // C API Extensions for TensorFlow. 10 | // 11 | #ifdef SWIG 12 | #define TF_CAPI_EXPORT 13 | #else 14 | #if defined(COMPILER_MSVC) 15 | #ifdef TF_COMPILE_LIBRARY 16 | #define TF_CAPI_EXPORT __declspec(dllexport) 17 | #else 18 | #define TF_CAPI_EXPORT __declspec(dllimport) 19 | #endif // TF_COMPILE_LIBRARY 20 | #else 21 | #define TF_CAPI_EXPORT __attribute__((visibility("default"))) 22 | #endif // COMPILER_MSVC 23 | #endif // SWIG 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | TF_CAPI_EXPORT extern void* TFEX_AllocGraphDefFromBuffer(TF_Buffer* graph_def_buf); 30 | 31 | TF_CAPI_EXPORT extern void TFEX_DeleteGraphDef(void* graph_def); 32 | 33 | TF_CAPI_EXPORT extern void* TFEX_AllocMetaGraphDefFromBuffer(TF_Buffer* metagraph_buf); 34 | 35 | TF_CAPI_EXPORT extern void TFEX_DeleteMetaGraphDef(void* metagraph_def); 36 | 37 | TF_CAPI_EXPORT extern void* TFEX_AllocNodeDefFromBuffer(TF_Buffer* node_def_buf); 38 | 39 | TF_CAPI_EXPORT extern void TFEX_DeleteNodeDef(void* node_def); 40 | 41 | TF_CAPI_EXPORT extern void* TFEX_AllocAttrValueFromBuffer(TF_Buffer* attr_value_buf); 42 | 43 | TF_CAPI_EXPORT extern void TFEX_DeleteAttrValue(void* attr_value); 44 | 45 | TF_CAPI_EXPORT extern void* TFEX_AllocOpListFromBuffer(TF_Buffer* op_list_buf); 46 | 47 | TF_CAPI_EXPORT extern void TFEX_DeleteOpList(void* op_list); 48 | 49 | TF_CAPI_EXPORT extern int TFEX_GetOpListCount(void* op_list); 50 | 51 | TF_CAPI_EXPORT extern void* TFEX_GetOpDef(void* op_list, int index); 52 | 53 | TF_CAPI_EXPORT extern const char* TFEX_GetOpDefName(const void* op_def); 54 | 55 | TF_CAPI_EXPORT extern int TFEX_GetOpDefInputArgCount(const void* op_def); 56 | 57 | TF_CAPI_EXPORT extern void* TFEX_GetOpDefInputArg(const void* op_def, int idx); 58 | 59 | TF_CAPI_EXPORT extern int TFEX_GetOpDefOutputArgCount(const void* op_def); 60 | 61 | TF_CAPI_EXPORT extern void* TFEX_GetOpDefOutputArg(const void* op_def, int idx); 62 | 63 | TF_CAPI_EXPORT extern const char* TFEX_GetOpDefArgDefName(const void* arg_def); 64 | 65 | TF_CAPI_EXPORT extern const char* TFEX_GetOpDefArgDefDescription(const void* arg_def); 66 | 67 | TF_CAPI_EXPORT extern int TFEX_GetOpDefArgDefDataType(const void* arg_def); 68 | 69 | TF_CAPI_EXPORT extern const char* TFEX_GetOpDefArgDefTypeAttr(const void* arg_def); 70 | 71 | TF_CAPI_EXPORT extern int TFEX_GetOpDefAttrCount(const void* op_def) ; 72 | 73 | TF_CAPI_EXPORT extern void* TFEX_GetOpDefAttr(const void* op_def, int idx); 74 | 75 | TF_CAPI_EXPORT extern const char* TFEX_GetOpDefAttrName(const void* op_def_attr); 76 | 77 | TF_CAPI_EXPORT extern const char* TFEX_GetOpDefAttrDescription(const void* op_def_attr); 78 | 79 | TF_CAPI_EXPORT extern void* TFEX_GetOpDefAttrMetadata(const void* op_def_attr); 80 | 81 | TF_CAPI_EXPORT extern const char* TFEX_GetOpDefAttrType(const void* op_def_attr); 82 | 83 | TF_CAPI_EXPORT extern void* TFEX_GetOpDefAttrDefaultValue(const void* op_def_attr); 84 | 85 | TF_CAPI_EXPORT extern void* TFEX_AllocAttrValue(TF_Operation* oper, const char* attr_name, 86 | TF_Status* s); 87 | 88 | TF_CAPI_EXPORT extern int TFEX_GetAttrValueCase(const void* attr_value); 89 | 90 | TF_CAPI_EXPORT extern int TFEX_GetAttrValueType(const void* attr_value); 91 | 92 | TF_CAPI_EXPORT extern int TFEX_AttrValueHasTensor(const void* attr_value); 93 | 94 | TF_CAPI_EXPORT extern void* TFEX_GetAttrValue_tensor(const void* attr_value); 95 | 96 | TF_CAPI_EXPORT extern const char* TFEX_GetAttrValue_s(const void* attr_value); 97 | 98 | TF_CAPI_EXPORT extern int64_t TFEX_GetAttrValue_i(const void* attr_value); 99 | 100 | TF_CAPI_EXPORT extern float TFEX_GetAttrValue_f(const void* attr_value); 101 | 102 | TF_CAPI_EXPORT extern void* TFEX_AllocGraphDefFromGraph(const void* graph); 103 | 104 | TF_CAPI_EXPORT extern void* TFEX_AddNodeDefToGraphDef(const void* graph_def); 105 | 106 | TF_CAPI_EXPORT extern void TFEX_DeleteGraphDef(void* graph_def); 107 | 108 | TF_CAPI_EXPORT extern int TFEX_GetNodeDefsCount(const void* graph_def); 109 | 110 | TF_CAPI_EXPORT extern void* TFEX_GetNodeDef(const void* graph_def, int idx); 111 | 112 | TF_CAPI_EXPORT extern const char* TFEX_AllocGraphDefDebugString(const void* graph_def); 113 | 114 | TF_CAPI_EXPORT extern const char* TFEX_AllocNodeDefDebugString(const void* node_def); 115 | 116 | TF_CAPI_EXPORT extern void TFEX_DeleteDebugString(const char* debug_str); 117 | 118 | TF_CAPI_EXPORT extern const char* TFEX_GetNodeDefOp(const void* node_def); 119 | 120 | TF_CAPI_EXPORT extern const char* TFEX_GetNodeDefName(const void* node_def); 121 | 122 | TF_CAPI_EXPORT extern int TFEX_GetNodeDefInputCount(const void* node_def); 123 | 124 | TF_CAPI_EXPORT extern const char* TFEX_GetNodeDefInput(const void* node_def, int idx); 125 | 126 | TF_CAPI_EXPORT extern void* TFEX_GetNodeDefAttrMap(const void* node_def); 127 | 128 | TF_CAPI_EXPORT extern const void* TFEX_GetAttrMapAt(const void* map, const char* key); 129 | 130 | /* 131 | TF_CAPI_EXPORT extern int TFEX_GetAttrMapCount(const void* map); 132 | 133 | TF_CAPI_EXPORT extern void* TFEX_GetAttrMapIterator(const void* map); 134 | 135 | TF_CAPI_EXPORT extern void TFEX_DeleteAttrMapIterator(const void* it); 136 | 137 | TF_CAPI_EXPORT extern void* TFEX_AttrMapIteratorNext(const void* map, const void* it); 138 | 139 | TF_CAPI_EXPORT extern const char* TFEX_AttrMapIteratorKey(const void* it); 140 | 141 | TF_CAPI_EXPORT extern const void* TFEX_AttrMapIteratorValue(const void* it); 142 | */ 143 | 144 | TF_CAPI_EXPORT extern void* TFEX_GetSignatureDefMapFromMetaGraphDef(const void* metagraph_def); 145 | 146 | TF_CAPI_EXPORT extern void* TFEX_GetSignatureDefFromMap(const void* signature_def_map, const char* key); 147 | 148 | TF_CAPI_EXPORT extern const char* TFEX_GetInputNameFromSignatureDef(const void* signature_def); 149 | 150 | TF_CAPI_EXPORT extern const char* TFEX_GetOutputNameFromSignatureDef(const void* signature_def); 151 | 152 | TF_CAPI_EXPORT extern int TFEX_TensorIntValCount(const void* tensor); 153 | 154 | TF_CAPI_EXPORT extern int TFEX_TensorIntVal(const void* tensor, int idx); 155 | 156 | TF_CAPI_EXPORT extern int TFEX_ParseTensorName(const char* name, char* first, int* first_len); 157 | 158 | TF_CAPI_EXPORT extern const char* TFEX_ColocationAttrName(); 159 | 160 | TF_CAPI_EXPORT extern const char* TFEX_ColocationGroupPrefix(); 161 | 162 | TF_CAPI_EXPORT extern const char* TFEX_SavedModelTagServe(); 163 | 164 | TF_CAPI_EXPORT extern const char* TFEX_SavedModelTagTrain(); 165 | 166 | TF_CAPI_EXPORT extern int TFEX_RegisterOpsForTesting(); 167 | 168 | TF_CAPI_EXPORT extern int TFEX_SpecialTest1(char* protbuf, size_t protbuf_len, void(*prot_callback)(char* buf, size_t buf_len)); 169 | 170 | TF_CAPI_EXPORT extern int TFEX_SpecialTest2(char* protbuf, size_t protbuf_len, void(*prot_callback)(char* buf, size_t buf_len)); 171 | 172 | #ifdef __cplusplus 173 | } /* end extern "C" */ 174 | #endif 175 | 176 | #endif // TENSORFLOW_C_C_API_EX_H_ 177 | -------------------------------------------------------------------------------- /clean.bat: -------------------------------------------------------------------------------- 1 | rd /s /q api_tests\TestWithVCLUnitX\__history 2 | rd /s /q api_tests\TestWithVCLUnitX\ModelSupport_VCLTestTensorFlow 3 | rd /s /q api_tests\TestWithVCLUnitX\Win64 4 | del /q api_tests\TestWithVCLUnitX\VCLTestTensorFlow.dproj.local 5 | del /q api_tests\TestWithVCLUnitX\VCLTestTensorFlow.identcache 6 | del /q api_tests\TestWithVCLUnitX\VCLTestTensorFlow.stat 7 | del /q api_tests\TestWithVCLUnitX\VCLTestTensorFlow_project.tvsconfig 8 | 9 | rd /s /q api_tests\TestWithConsoleDUnitX\__history 10 | rd /s /q api_tests\TestWithConsoleDUnitX\ModelSupport_ConsoleTestTensorFlow 11 | rd /s /q api_tests\TestWithConsoleDUnitX\Win64 12 | del /q api_tests\TestWithConsoleDUnitX\ConsoleTestTensorFlow.dproj.local 13 | del /q api_tests\TestWithConsoleDUnitX\ConsoleTestTensorFlow.identcache 14 | del /q api_tests\TestWithConsoleDUnitX\ConsoleTestTensorFlow.stat 15 | del /q api_tests\TestWithConsoleDUnitX\ConsoleTestTensorFlow_project.tvsconfig 16 | 17 | rd /s /q api_tests\TestWithFMXUnitX\__history 18 | rd /s /q api_tests\TestWithFMXUnitX\ModelSupport_FMXTestTensorFlow 19 | rd /s /q api_tests\TestWithFMXUnitX\Win64 20 | del /q api_tests\TestWithFMXUnitX\FMXTestTensorFlow.dproj.local 21 | del /q api_tests\TestWithFMXUnitX\FMXTestTensorFlow.identcache 22 | del /q api_tests\TestWithFMXUnitX\FMXTestTensorFlow.stat 23 | del /q api_tests\TestWithFMXUnitX\FMXTestTensorFlow_project.tvsconfig 24 | 25 | rd /s /q api\__history 26 | rd /s /q api_tests\__history 27 | del /q bin\ConsoleTestTensorFlow.* 28 | del /q bin\FMXTestTensorFlow.* 29 | del /q bin\VCLTestTensorFlow.* 30 | del /q bin\dunitx.* 31 | del /q bin\*.xml 32 | 33 | rd /s /q P4DTensorflowDemos\P4DDemoControl\__history 34 | rd /s /q P4DTensorflowDemos\P4DDemoControl\__recovery 35 | rd /s /q P4DTensorflowDemos\P4DDemoControl\Win64 36 | del /q P4DTensorflowDemos\P4DDemoControl.exe 37 | del /q P4DTensorflowDemos\P4DDemoControl.rsm 38 | del /q P4DTensorflowDemos\P4DDemoControl\P4DDemoControl.dproj.local 39 | del /q P4DTensorflowDemos\P4DDemoControl\P4DDemoControl.identcache 40 | del /q P4DTensorflowDemos\P4DDemoControl\P4DDemoControl.stat 41 | del /q P4DTensorflowDemos\P4DDemoControl_MemoryManager_EventLog.txt 42 | 43 | Pause -------------------------------------------------------------------------------- /third_party/ATViewer/ATBinHexOptions.inc: -------------------------------------------------------------------------------- 1 | //ATBinHex options: 2 | 3 | {$define NOTIF} //Enable file change notification code. 4 | //Should be enabled. 5 | 6 | {$define PRINT} //Enable printing code. 7 | //Should be enabled. 8 | 9 | //{$define PREVIEW} //Enable ATPrintPreview usage. 10 | //Should be disabled. 11 | 12 | {$define SEARCH} //Enable searching code. 13 | //Should be enabled. 14 | 15 | {$define SCROLL} //Enable workaround for Windows bug: when horiz scrollbar temporary hides, 16 | //this may cause both horiz+vertical scrollbars + window border to disappear 17 | //completely. This workaround rises users' question: why horiz scrollbar 18 | //doesn't hide when lines become short enough. 19 | //Should be enabled. 20 | 21 | //{$define REGEX} //Enable DIRegEx usage. Used for URL hilighting. 22 | //Should be enabled (with DIRegEx). 23 | 24 | //{$define TEST} //Show debug form. 25 | //Must be disabled in release. 26 | -------------------------------------------------------------------------------- /third_party/ATViewer/ATBinHexResources.res: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/third_party/ATViewer/ATBinHexResources.res -------------------------------------------------------------------------------- /third_party/ATViewer/ATFileNotificationSimple.pas: -------------------------------------------------------------------------------- 1 | {*******************************************} 2 | { } 3 | { ATFileNotificationSimple Component } 4 | { Copyright (C) Alexey Torgashin } 5 | { http://www.uvviewsoft.com } 6 | { } 7 | {*******************************************} 8 | { 9 | ATFileNotificationSimple is a simple version of ATFileNotification, 10 | the advantages are: 11 | - It doesn't create any threads, it uses simple timer instead 12 | - It doesn't use FindFirstChangeNotification API 13 | - network files can be watched 14 | 15 | Disadvantages are: 16 | - It monitors file change only, not directory change 17 | - It doesn't react to file change immediately (but with a timer delay) 18 | - It polls disk repeatedly, may be slow on remote disks 19 | 20 | Example of usage: 21 | procedure TFormMain.NotifyFile; 22 | begin 23 | with ATFileNotificationSimple1 do 24 | begin 25 | Timer.Enabled := False; 26 | Timer.Interval := StrToIntDef(edDelay.Text, Timer.Interval); 27 | FileName := edFileName.Text; 28 | Timer.Enabled := True; 29 | end; 30 | end; 31 | } 32 | 33 | {$BOOLEVAL OFF} //Short boolean evaluation. 34 | 35 | unit ATFileNotificationSimple; 36 | 37 | interface 38 | 39 | uses 40 | Windows, SysUtils, Classes, VCL.ExtCtrls; 41 | 42 | type 43 | TATFileSimpleRec = record 44 | FExist: Boolean; 45 | FSizeLow, 46 | FSizeHigh: DWORD; 47 | FTimeWr: TFileTime; 48 | end; 49 | 50 | type 51 | TATFileNotificationSimple = class(TComponent) 52 | private 53 | { Private declarations } 54 | FFileName: WideString; 55 | FFileRec: TATFileSimpleRec; 56 | FTimer: TTimer; 57 | FTimerBusy: Boolean; 58 | FOnChanged: TNotifyEvent; 59 | procedure SetFileName(const AValue: WideString); 60 | procedure TimerTimer(Sender: TObject); 61 | procedure DoChanged; 62 | protected 63 | { Protected declarations } 64 | public 65 | { Public declarations } 66 | constructor Create(AOwner: TComponent); override; 67 | property FileName: WideString read FFileName write SetFileName; 68 | property Timer: TTimer read FTimer; 69 | published 70 | { Published declarations } 71 | property OnChanged: TNotifyEvent read FOnChanged write FOnChanged; 72 | end; 73 | 74 | procedure Register; 75 | 76 | 77 | implementation 78 | 79 | uses 80 | ATxFProc; 81 | 82 | { Helper functions } 83 | 84 | procedure FGetFileRec(const FileName: WideString; var Rec: TATFileSimpleRec); 85 | var 86 | h: THandle; 87 | fdA: TWin32FindDataA; 88 | fdW: TWin32FindDataW; 89 | begin 90 | FillChar(Rec, SizeOf(Rec), 0); 91 | if Win32Platform = VER_PLATFORM_WIN32_NT then 92 | begin 93 | h := FindFirstFileW(PWideChar(FileName), fdW); 94 | Rec.FExist := h <> INVALID_HANDLE_VALUE; 95 | if Rec.FExist then 96 | begin 97 | Rec.FSizeLow := fdW.nFileSizeLow; 98 | Rec.FSizeHigh := fdW.nFileSizeHigh; 99 | //Rec.FAttr := fdW.dwFileAttributes; 100 | Rec.FTimeWr := fdW.ftLastWriteTime; 101 | //Rec.FTimeCr := fdW.ftCreationTime; 102 | //Rec.FTimeAcc := fdW.ftLastAccessTime; 103 | Windows.FindClose(h); 104 | end; 105 | end 106 | else 107 | begin 108 | h := FindFirstFileA(PAnsiChar(AnsiString(FileName)), fdA); 109 | Rec.FExist := h <> INVALID_HANDLE_VALUE; 110 | if Rec.FExist then 111 | begin 112 | Rec.FSizeLow := fdA.nFileSizeLow; 113 | Rec.FSizeHigh := fdA.nFileSizeHigh; 114 | //Rec.FAttr := fdA.dwFileAttributes; 115 | Rec.FTimeWr := fdA.ftLastWriteTime; 116 | //Rec.FTimeCr := fdA.ftCreationTime; 117 | //Rec.FTimeAcc := fdA.ftLastAccessTime; 118 | Windows.FindClose(h); 119 | end; 120 | end; 121 | end; 122 | 123 | function FFileChanged(const FileName: WideString; var OldRec: TATFileSimpleRec): Boolean; 124 | var 125 | NewRec: TATFileSimpleRec; 126 | begin 127 | CloseHandle(FFileOpen(FileName)); //To update filetime, if log is still open 128 | FGetFileRec(FileName, NewRec); 129 | 130 | Result := 131 | ( OldRec.FExist <> NewRec.FExist ) or 132 | ( OldRec.FSizeLow <> NewRec.FSizeLow ) or 133 | ( OldRec.FSizeHigh <> NewRec.FSizeHigh) or 134 | ( OldRec.FTimeWr.dwLowDateTime <> NewRec.FTimeWr.dwLowDateTime ) or 135 | ( OldRec.FTimeWr.dwHighDateTime <> NewRec.FTimeWr.dwHighDateTime ); 136 | 137 | if Result then 138 | Move(NewRec, OldRec, SizeOf(NewRec)); 139 | end; 140 | 141 | 142 | { TATFileNotificationSimple } 143 | 144 | constructor TATFileNotificationSimple.Create(AOwner: TComponent); 145 | begin 146 | inherited; 147 | FFileName := ''; 148 | FillChar(FFileRec, SizeOf(FFileRec), 0); 149 | FTimer := TTimer.Create(Self); 150 | with FTimer do 151 | begin 152 | Enabled := False; 153 | Interval := 1000; 154 | OnTimer := TimerTimer; 155 | end; 156 | FTimerBusy := False; 157 | end; 158 | 159 | procedure TATFileNotificationSimple.SetFileName(const AValue: WideString); 160 | var 161 | En: Boolean; 162 | begin 163 | En := FTimer.Enabled; 164 | FTimer.Enabled := False; 165 | 166 | FFileName := AValue; 167 | FGetFileRec(FFileName, FFileRec); 168 | if (FFileName <> '') and (not FFileRec.FExist) then 169 | raise Exception.Create('File to watch doesn''t exist'); 170 | 171 | FTimer.Enabled := En; 172 | end; 173 | 174 | procedure TATFileNotificationSimple.TimerTimer(Sender: TObject); 175 | begin 176 | if not FTimerBusy then 177 | try 178 | FTimerBusy := True; 179 | if FFileChanged(FFileName, FFileRec) then 180 | DoChanged; 181 | finally 182 | FTimerBusy := False; 183 | end; 184 | end; 185 | 186 | procedure TATFileNotificationSimple.DoChanged; 187 | begin 188 | if Assigned(FOnChanged) then 189 | FOnChanged(Self); 190 | end; 191 | 192 | 193 | { Registration } 194 | 195 | procedure Register; 196 | begin 197 | RegisterComponents('Samples', [TATFileNotificationSimple]); 198 | end; 199 | 200 | end. 201 | -------------------------------------------------------------------------------- /third_party/ATViewer/ATStreamSearchOptions.inc: -------------------------------------------------------------------------------- 1 | //ATStreamSearch options: 2 | 3 | //{$define REGEX} //Enable RegEx search (DIRegEx library). 4 | 5 | //{$define TNT} //Enable unicode filenames support during search - 6 | //Tnt Unicode Controls are used for that. 7 | -------------------------------------------------------------------------------- /third_party/ATViewer/ATViewerMsg.pas: -------------------------------------------------------------------------------- 1 | unit ATViewerMsg; 2 | 3 | interface 4 | 5 | uses 6 | Windows; 7 | 8 | function MsgBox(const Msg, Title: WideString; Flags: Integer; hWnd: THandle = 0): Integer; 9 | procedure MsgInfo(const Msg: WideString; hWnd: THandle = 0); 10 | procedure MsgError(const Msg: WideString; hWnd: THandle = 0); 11 | procedure MsgWarning(const Msg: WideString; hWnd: THandle = 0); 12 | 13 | var 14 | ATViewerMessagesEnabled: Boolean = True; 15 | 16 | var 17 | MsgViewerCaption: Widestring = 'Viewer'; 18 | MsgViewerShowCfm: Widestring = 'Format unknown'#13'Click here to show binary dump'; 19 | MsgViewerShowEmpty: Widestring = 'File is empty'; 20 | MsgViewerErrCannotFindFile: Widestring = 'File not found: "%s"'; 21 | MsgViewerErrCannotFindFolder: Widestring = 'Folder not found: "%s"'; 22 | MsgViewerErrCannotOpenFile: Widestring = 'Cannot open file: "%s"'; 23 | MsgViewerErrCannotLoadFile: Widestring = 'Cannot load file: "%s"'; 24 | MsgViewerErrCannotReadFile: Widestring = 'Cannot read file: "%s"'; 25 | MsgViewerErrCannotReadStream: Widestring = 'Cannot read stream'; 26 | MsgViewerErrCannotReadPos: Widestring = 'Read error at offset %s'; 27 | MsgViewerErrDetect: Widestring = 'Program could not detect file format'#13'Dump is shown'; 28 | MsgViewerErrImage: Widestring = 'Unknown image format'; 29 | MsgViewerErrMedia: Widestring = 'Unknown multimedia format'; 30 | MsgViewerErrOffice: Widestring = 'MS Office module doesn''t support this file type'; 31 | MsgViewerErrInitControl: Widestring = 'Cannot initialize %s'; 32 | MsgViewerErrInitOffice: Widestring = 'Cannot initialize MS Office control'; 33 | MsgViewerErrCannotCopyData: Widestring = 'Cannot copy data to Clipboard'; 34 | MsgViewerWlxException: Widestring = 'Exception in plugin "%s" in function "%s"'; 35 | MsgViewerWlxParentNotSpecified: Widestring = 'Cannot load plugins: parent form not specified'; 36 | MsgViewerAniTitle: Widestring = 'Title: '; 37 | MsgViewerAniCreator: Widestring = 'Creator: '; 38 | MsgViewerPageHint: Widestring = 'Previous/Next page'#13'Current page: %d of %d'; 39 | 40 | implementation 41 | 42 | uses 43 | SysUtils, VCL.Forms; 44 | 45 | function MsgBox(const Msg, Title: WideString; Flags: Integer; hWnd: THandle = 0): Integer; 46 | begin 47 | if ATViewerMessagesEnabled then 48 | Result := MessageBoxW(hWnd, PWideChar(Msg), PWideChar(Title), 49 | Flags or MB_SETFOREGROUND or MB_TASKMODAL) 50 | else 51 | Result := IDCANCEL; 52 | end; 53 | 54 | procedure MsgInfo(const Msg: WideString; hWnd: THandle = 0); 55 | begin 56 | MsgBox(Msg, MsgViewerCaption, MB_OK or MB_ICONINFORMATION, hWnd); 57 | end; 58 | 59 | procedure MsgError(const Msg: WideString; hWnd: THandle = 0); 60 | begin 61 | MsgBox(Msg, MsgViewerCaption, MB_OK or MB_ICONERROR, hWnd); 62 | end; 63 | 64 | procedure MsgWarning(const Msg: WideString; hWnd: THandle = 0); 65 | begin 66 | MsgBox(Msg, MsgViewerCaption, MB_OK or MB_ICONEXCLAMATION, hWnd); 67 | end; 68 | 69 | end. 70 | -------------------------------------------------------------------------------- /third_party/ATViewer/ATViewerOptions.inc: -------------------------------------------------------------------------------- 1 | //-------------------------------------------------------------------------------- 2 | // ATViewer options: 3 | // 4 | // TNT Use Tnt Unicode Controls library. 5 | // GEX Use GraphicEx library. 6 | // GIF Use GIFImage library. 7 | // PNG Use PNGImage library. 8 | // IJL Use IJL library. 9 | // JP2 Use Jpeg2000 library. 10 | // ANI Use AmnAni library (dll). 11 | // TIFF Use LibTiff library (dll). 12 | // PREVIEW Use ATPrintPreview unit. 13 | // IVIEW Use nmzIrfanXnView unit (included). 14 | // 15 | // CE Enable Cubic Explorer media mode. 16 | // M6 Enable Windows Media Player 6.4 media mode. 17 | // M9 Enable Windows Media Player 9.x media mode. 18 | // 19 | // MSO Enable MS Office mode. 20 | // WLX Enable plugins mode. 21 | // WLX_FORM Enable helper "Total Commander [fake window]" hidden form. 22 | // WLX_UNLOAD Enable plugins unloading on closing. 23 | // This causes AV with some plugins: Syn, Hpg_Ed, IniEd. 24 | // IE4X Enable usage of WebBrowser4_TLB unit. 25 | // See desc. of WebAcceptAllFiles property. 26 | // NOTIF Enable file change notification code. 27 | // PRINT Enable printing code. 28 | // SEARCH Enable searching code. 29 | // OFFLINE Enable WebOffline property usage. 30 | // 31 | //-------------------------------------------------------------------------------- 32 | 33 | //{$define TNT} 34 | //{$define GEX} 35 | //{$define GIF} 36 | //{$define PNG} 37 | //{$define IJL} 38 | //{$define JP2} 39 | //{$define ANI} 40 | //{$define PREVIEW} 41 | //{$define IVIEW} 42 | 43 | //{$define CE} 44 | {$define M6} 45 | {$define M9} 46 | 47 | {$define WLX} 48 | //{$define WLX_FORM} 49 | //{$define WLX_UNLOAD} 50 | 51 | {$define IE4X} 52 | {$define NOTIF} 53 | {$define PRINT} 54 | {$define SEARCH} 55 | {$define OFFLINE} 56 | -------------------------------------------------------------------------------- /third_party/ATViewer/ATxClipboard.pas: -------------------------------------------------------------------------------- 1 | unit ATxClipboard; 2 | 3 | interface 4 | 5 | uses 6 | ATxCodepages; 7 | 8 | function SCopyToClipboard(const S: AnsiString; Enc: TATEncoding = vencANSI): Boolean; 9 | function SCopyToClipboardW(const S: WideString): Boolean; 10 | 11 | 12 | implementation 13 | 14 | uses 15 | Windows, SysUtils; 16 | 17 | function SOpenAndClearClipboard: Boolean; 18 | begin 19 | Result := OpenClipboard(0); 20 | if Result then 21 | EmptyClipboard; 22 | end; 23 | 24 | //CF_UNICODETEXT format is supported only under NT 25 | function SCopyToClipboardW_NT(const S: WideString; DoClear: Boolean): Boolean; 26 | var 27 | DataSize, BufferSize: Integer; 28 | hData: HGLOBAL; 29 | gData: Pointer; 30 | begin 31 | DataSize := Length(S) * 2; 32 | 33 | if DataSize > 0 then 34 | begin 35 | if DoClear then 36 | begin 37 | if not SOpenAndClearClipboard then 38 | begin Result := False; Exit end; 39 | end; 40 | 41 | BufferSize := DataSize + 2; 42 | hData := GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE, BufferSize); 43 | if hData <> 0 then 44 | begin 45 | gData := GlobalLock(hData); 46 | if gData <> nil then 47 | begin 48 | Move(S[1], gData^, BufferSize); 49 | GlobalUnlock(hData); 50 | SetClipboardData(CF_UNICODETEXT, hData); 51 | end; 52 | end; 53 | 54 | if DoClear then 55 | CloseClipboard; 56 | end; 57 | 58 | Result := True; 59 | end; 60 | 61 | function SCopyToClipboard(const S: AnsiString; Enc: TATEncoding = vencANSI): Boolean; 62 | var 63 | DataSize, BufferSize: Integer; 64 | hData: HGLOBAL; 65 | gData: Pointer; 66 | SCopy: AnsiString; 67 | SFormat: Integer; 68 | begin 69 | if Length(S) > 0 then 70 | begin 71 | if not SOpenAndClearClipboard then 72 | begin 73 | Result := False; 74 | Exit 75 | end; 76 | 77 | case Enc of 78 | vencANSI: 79 | begin 80 | SCopy := S; 81 | SFormat := CF_TEXT; 82 | end; 83 | vencOEM: 84 | begin 85 | SCopy := S; 86 | SFormat := CF_OEMTEXT; 87 | end; 88 | else 89 | begin 90 | SCopy := SCodepageToUnicode(S, Enc); 91 | SFormat := CF_TEXT; 92 | end; 93 | end; 94 | 95 | DataSize := Length(SCopy); 96 | if DataSize = 0 then 97 | begin 98 | Result := True; 99 | Exit 100 | end; 101 | 102 | BufferSize := DataSize + 1; 103 | hData := GlobalAlloc(GMEM_MOVEABLE or GMEM_DDESHARE, BufferSize); 104 | if hData <> 0 then 105 | begin 106 | gData := GlobalLock(hData); 107 | if gData <> nil then 108 | begin 109 | Move(SCopy[1], gData^, BufferSize); 110 | GlobalUnlock(hData); 111 | SetClipboardData(SFormat, hData); 112 | end; 113 | end; 114 | 115 | //Also copy in CF_UNICODETEXT format for compatability with Windows controls 116 | if Win32Platform = VER_PLATFORM_WIN32_NT then 117 | begin 118 | SCopyToClipboardW_NT(SCodepageToUnicode(S, Enc), False); 119 | end; 120 | 121 | CloseClipboard; 122 | end; 123 | 124 | Result := True; 125 | end; 126 | 127 | 128 | function SCopyToClipboardW(const S: WideString): Boolean; 129 | begin 130 | if Win32Platform = VER_PLATFORM_WIN32_NT then 131 | Result := SCopyToClipboardW_NT(S, True) 132 | else 133 | Result := SCopyToClipboard(AnsiString(S));; 134 | end; 135 | 136 | 137 | end. 138 | -------------------------------------------------------------------------------- /third_party/ATViewer/ATxCodepageList.pas: -------------------------------------------------------------------------------- 1 | unit ATxCodepageList; 2 | interface 3 | uses Windows; 4 | type TATCodepageRec = record Family, Name: string; ID: Integer end; 5 | const cCodepages: array[0..131] of TATCodepageRec = ( 6 | (Family: ''; Name: 'ANSI (Windows)'; ID: CP_ACP), 7 | (Family: ''; Name: 'OEM (DOS)'; ID: CP_OEMCP), 8 | (Family: ''; Name: 'Mac'; ID: CP_MACCP), 9 | (Family: 'Central European'; Name: 'ANSI (Windows-1250)'; ID: 1250), 10 | (Family: 'Central European'; Name: 'IBM EBCDIC (Multilingual Latin-2)'; ID: 870), 11 | (Family: 'Central European'; Name: 'ISO-8859-2'; ID: 28592), 12 | (Family: 'Central European'; Name: 'Mac'; ID: 10029), 13 | (Family: 'Central European'; Name: 'Mac (Croatian)'; ID: 10082), 14 | (Family: 'Central European'; Name: 'Mac (Romanian)'; ID: 10010), 15 | (Family: 'Central European'; Name: 'OEM (IBM852)'; ID: 852), 16 | (Family: 'Western European'; Name: 'ANSI (Windows-1252)'; ID: 1252), 17 | (Family: 'Western European'; Name: 'IA5 German (7-bit)'; ID: 20106), 18 | (Family: 'Western European'; Name: 'IA5 Norwegian (7-bit)'; ID: 20108), 19 | (Family: 'Western European'; Name: 'IA5 Swedish (7-bit)'; ID: 20107), 20 | (Family: 'Western European'; Name: 'IA5 IRV International Alphabet No.5'; ID: 20105), 21 | (Family: 'Western European'; Name: 'IBM EBCDIC (Denmark-Norway)'; ID: 1142), 22 | (Family: 'Western European'; Name: 'IBM EBCDIC (Finland-Sweden)'; ID: 1143), 23 | (Family: 'Western European'; Name: 'IBM EBCDIC (France)'; ID: 1147), 24 | (Family: 'Western European'; Name: 'IBM EBCDIC (Germany)'; ID: 1141), 25 | (Family: 'Western European'; Name: 'IBM EBCDIC (Icelandic)'; ID: 1149), 26 | (Family: 'Western European'; Name: 'IBM EBCDIC (International)'; ID: 1148), 27 | (Family: 'Western European'; Name: 'IBM EBCDIC (Italy)'; ID: 1144), 28 | (Family: 'Western European'; Name: 'IBM EBCDIC (Spain)'; ID: 1145), 29 | (Family: 'Western European'; Name: 'IBM EBCDIC (UK)'; ID: 1146), 30 | (Family: 'Western European'; Name: 'IBM EBCDIC (US-Canada)'; ID: 1140), 31 | (Family: 'Western European'; Name: 'IBM EBCDIC (Latin-1/Open System)'; ID: 20924), 32 | (Family: 'Western European'; Name: 'ISO-6937 (Non-Spacing Accent)'; ID: 20269), 33 | (Family: 'Western European'; Name: 'ISO-8859-1 (Latin-1)'; ID: 28591), 34 | (Family: 'Western European'; Name: 'ISO-8859-3 (Latin-3)'; ID: 28593), 35 | (Family: 'Western European'; Name: 'ISO-8859-9 (Latin-5)'; ID: 28599), 36 | (Family: 'Western European'; Name: 'ISO-8859-15 (Latin-9)'; ID: 28605), 37 | (Family: 'Western European'; Name: 'Mac (Icelandic)'; ID: 10079), 38 | (Family: 'Western European'; Name: 'Mac (Roman)'; ID: 10000), 39 | (Family: 'Western European'; Name: 'OEM (French Canadian)'; ID: 863), 40 | (Family: 'Western European'; Name: 'OEM (Icelandic)'; ID: 861), 41 | (Family: 'Western European'; Name: 'OEM (Nordic)'; ID: 865), 42 | (Family: 'Western European'; Name: 'OEM (Multilingual Latin-1)'; ID: 858), 43 | (Family: 'Western European'; Name: 'OEM (Portuguese)'; ID: 860), 44 | (Family: 'Western European'; Name: 'OEM (United States)'; ID: 437), 45 | (Family: 'Western European'; Name: 'T.61'; ID: 20261), 46 | (Family: 'Western European'; Name: 'US-ASCII (7-bit)'; ID: 20127), 47 | (Family: 'Baltic'; Name: 'ANSI (Windows-1257)'; ID: 1257), 48 | (Family: 'Baltic'; Name: 'ISO-8859-4'; ID: 28594), 49 | (Family: 'Baltic'; Name: 'ISO-8859-13 (Lithuanian)'; ID: 28603), 50 | (Family: 'Baltic'; Name: 'OEM (IBM775)'; ID: 775), 51 | (Family: 'Cyrillic'; Name: 'ANSI (Windows-1251)'; ID: 1251), 52 | (Family: 'Cyrillic'; Name: 'IBM EBCDIC (Russian)'; ID: 20880), 53 | (Family: 'Cyrillic'; Name: 'IBM EBCDIC (Serbian-Bulgarian)'; ID: 21025), 54 | (Family: 'Cyrillic'; Name: 'ISO-8859-5'; ID: 28595), 55 | (Family: 'Cyrillic'; Name: 'KOI8-R'; ID: 20866), 56 | (Family: 'Cyrillic'; Name: 'KOI8-U'; ID: 21866), 57 | (Family: 'Cyrillic'; Name: 'Mac'; ID: 10007), 58 | (Family: 'Cyrillic'; Name: 'Mac (Ukrainian)'; ID: 10017), 59 | (Family: 'Cyrillic'; Name: 'OEM (Russian)'; ID: 866), 60 | (Family: 'Cyrillic'; Name: 'OEM (primarily Russian)'; ID: 855), 61 | (Family: 'Cyrillic'; Name: 'RUSCII (IBM-1125)'; ID: 1125), 62 | (Family: 'Greek'; Name: 'ANSI (Windows-1253)'; ID: 1253), 63 | (Family: 'Greek'; Name: 'IBM EBCDIC (IBM423)'; ID: 20423), 64 | (Family: 'Greek'; Name: 'IBM EBCDIC (Modern)'; ID: 875), 65 | (Family: 'Greek'; Name: 'ISO-8859-7'; ID: 28597), 66 | (Family: 'Greek'; Name: 'Mac'; ID: 10006), 67 | (Family: 'Greek'; Name: 'OEM (IBM737)'; ID: 737), 68 | (Family: 'Greek'; Name: 'OEM (Modern)'; ID: 869), 69 | (Family: 'Greek'; Name: 'Mac'; ID: 10006), 70 | (Family: 'Turkey'; Name: 'ANSI (Windows-1254)'; ID: 1254), 71 | (Family: 'Turkey'; Name: 'IBM EBCDIC (Latin-5)'; ID: 1026), 72 | (Family: 'Turkey'; Name: 'IBM EBCDIC (IBM905)'; ID: 20905), 73 | (Family: 'Turkey'; Name: 'ISO-8859-3 (Latin-3)'; ID: 28593), 74 | (Family: 'Turkey'; Name: 'ISO-8859-9 (Latin-5)'; ID: 28599), 75 | (Family: 'Turkey'; Name: 'Mac'; ID: 10081), 76 | (Family: 'Turkey'; Name: 'OEM (IBM857)'; ID: 857), 77 | (Family: 'Indian'; Name: 'ISCII Devanagari'; ID: 57002), 78 | (Family: 'Indian'; Name: 'ISCII Bengali'; ID: 57003), 79 | (Family: 'Indian'; Name: 'ISCII Tamil'; ID: 57004), 80 | (Family: 'Indian'; Name: 'ISCII Telugu'; ID: 57005), 81 | (Family: 'Indian'; Name: 'ISCII Assamese'; ID: 57006), 82 | (Family: 'Indian'; Name: 'ISCII Oriya'; ID: 57007), 83 | (Family: 'Indian'; Name: 'ISCII Kannada'; ID: 57008), 84 | (Family: 'Indian'; Name: 'ISCII Malayalam'; ID: 57009), 85 | (Family: 'Indian'; Name: 'ISCII Gujarati'; ID: 57010), 86 | (Family: 'Indian'; Name: 'ISCII Punjabi'; ID: 57011), 87 | (Family: 'Arabic'; Name: 'ANSI (Windows-1256)'; ID: 1256), 88 | (Family: 'Arabic'; Name: 'ASMO 708'; ID: 708), 89 | (Family: 'Arabic'; Name: 'ASMO 449+, BCON V4'; ID: 709), 90 | (Family: 'Arabic'; Name: 'IBM EBCDIC (IBM420)'; ID: 20420), 91 | (Family: 'Arabic'; Name: 'ISO-8859-6'; ID: 28596), 92 | (Family: 'Arabic'; Name: 'Mac'; ID: 10004), 93 | (Family: 'Arabic'; Name: 'OEM (IBM864)'; ID: 864), 94 | (Family: 'Arabic'; Name: 'Transparent ASMO (DOS-720)'; ID: 720), 95 | (Family: 'Hebrew'; Name: 'ANSI (Windows-1255)'; ID: 1255), 96 | (Family: 'Hebrew'; Name: 'IBM EBCDIC (IBM424)'; ID: 20424), 97 | (Family: 'Hebrew'; Name: 'ISO-8859-8 (Logical Ordering)'; ID: 38598), 98 | (Family: 'Hebrew'; Name: 'ISO-8859-8 (Visual Ordering)'; ID: 28598), 99 | (Family: 'Hebrew'; Name: 'Mac'; ID: 10005), 100 | (Family: 'Hebrew'; Name: 'OEM (DOS-862)'; ID: 862), 101 | (Family: 'Chinese Simplified'; Name: 'EUC'; ID: 51936), 102 | (Family: 'Chinese Simplified'; Name: 'GB18030'; ID: 54936), 103 | (Family: 'Chinese Simplified'; Name: 'GB2312'; ID: 936), 104 | (Family: 'Chinese Simplified'; Name: 'GB2312-80'; ID: 20936), 105 | (Family: 'Chinese Simplified'; Name: 'HZ'; ID: 52936), 106 | (Family: 'Chinese Simplified'; Name: 'ISO-2022'; ID: 50227), 107 | (Family: 'Chinese Simplified'; Name: 'Mac'; ID: 10008), 108 | (Family: 'Chinese Traditional'; Name: 'Big5'; ID: 950), 109 | (Family: 'Chinese Traditional'; Name: 'CNS (Taiwan)'; ID: 20000), 110 | (Family: 'Chinese Traditional'; Name: 'Eten'; ID: 20002), 111 | (Family: 'Chinese Traditional'; Name: 'EUC'; ID: 51950), 112 | (Family: 'Chinese Traditional'; Name: 'IBM5550 (Taiwan)'; ID: 20003), 113 | (Family: 'Chinese Traditional'; Name: 'ISO-2022'; ID: 50229), 114 | (Family: 'Chinese Traditional'; Name: 'Mac'; ID: 10002), 115 | (Family: 'Chinese Traditional'; Name: 'TCA (Taiwan)'; ID: 20001), 116 | (Family: 'Chinese Traditional'; Name: 'TeleText (Taiwan)'; ID: 20004), 117 | (Family: 'Chinese Traditional'; Name: 'Wang (Taiwan)'; ID: 20005), 118 | (Family: 'Japanese'; Name: 'ANSI/OEM (Shift-JIS)'; ID: 932), 119 | (Family: 'Japanese'; Name: 'EUC'; ID: 51932), 120 | (Family: 'Japanese'; Name: 'IBM EBCDIC (Katakana Extended)'; ID: 20290), 121 | (Family: 'Japanese'; Name: 'ISO-2022 (no halfwidth Katakana)'; ID: 50220), 122 | (Family: 'Japanese'; Name: 'ISO-2022 (halfwidth Katakana)'; ID: 50221), 123 | (Family: 'Japanese'; Name: 'ISO-2022 (JIS X 0201-1989)'; ID: 50222), 124 | (Family: 'Japanese'; Name: 'JIS X 0208-1990 && 0121-1990'; ID: 20932), 125 | (Family: 'Japanese'; Name: 'Mac'; ID: 10001), 126 | (Family: 'Korean'; Name: 'ANSI/OEM (Unified Hangul Code)'; ID: 949), 127 | (Family: 'Korean'; Name: 'EUC'; ID: 51949), 128 | (Family: 'Korean'; Name: 'IBM EBCDIC (Korean Extended)'; ID: 20833), 129 | (Family: 'Korean'; Name: 'ISO-2022'; ID: 50225), 130 | (Family: 'Korean'; Name: 'Johab'; ID: 1361), 131 | (Family: 'Korean'; Name: 'Mac'; ID: 10003), 132 | (Family: 'Korean'; Name: 'Wansung'; ID: 20949), 133 | (Family: 'Thai'; Name: 'ANSI (Windows-874)'; ID: 874), 134 | (Family: 'Thai'; Name: 'IBM EBCDIC'; ID: 20838), 135 | (Family: 'Vietnamese'; Name: 'ANSI (Windows-1258)'; ID: 1258), 136 | (Family: 'Unicode'; Name: 'Unicode (UTF-8)'; ID: CP_UTF8), 137 | (Family: 'Unicode'; Name: 'Unicode (UTF-7)'; ID: CP_UTF7) 138 | ); 139 | implementation end. 140 | -------------------------------------------------------------------------------- /third_party/ATViewer/ATxSHex.pas: -------------------------------------------------------------------------------- 1 | unit ATxSHex; 2 | 3 | interface 4 | 5 | //Conversion from hex encoded string (e.g. '10 20 AA FF': 4 chars) to normal string. 6 | //Hex string must contain 2*N hex digits. Spaces are ignored. 7 | function SHexToNormal(const HexStr: string; var ResStr: string): Boolean; 8 | 9 | //Conversion of string to hex form, digits are separated with spaces. 10 | function SToHex(const S: AnsiString): AnsiString; 11 | 12 | //Conversion from hex to Integer. 13 | function HexToIntDef(const S: string; const Default: Int64): Int64; 14 | 15 | 16 | implementation 17 | 18 | uses 19 | SysUtils, ATxSProc; 20 | 21 | function SHexDigitToInt(Hex: char; var Int: LongWord): Boolean; 22 | var 23 | ch: char; 24 | begin 25 | Result := True; 26 | Int := 0; 27 | ch := UpCase(Hex); 28 | case ch of 29 | '0'..'9': 30 | Int := Ord(ch) - Ord('0'); 31 | 'A'..'F': 32 | Int := Ord(ch) - Ord('A') + 10; 33 | else 34 | Result := False; 35 | end; 36 | end; 37 | 38 | 39 | function SHexWordToInt(const Hex: string; var Int: LongWord): Boolean; 40 | var 41 | Int1, Int2: LongWord; 42 | begin 43 | Result := False; 44 | if Length(Hex) = 1 then 45 | Result := SHexDigitToInt(Hex[1], Int) 46 | else 47 | if Length(Hex) = 2 then 48 | begin 49 | Result := 50 | SHexDigitToInt(Hex[1], Int1) and 51 | SHexDigitToInt(Hex[2], Int2); 52 | if Result then 53 | Int := Int1 * 16 + Int2; 54 | end; 55 | end; 56 | 57 | 58 | function SHexToNormal(const HexStr: string; var ResStr: string): Boolean; 59 | var 60 | S: string; 61 | Int: LongWord; 62 | i: Integer; 63 | begin 64 | ResStr := ''; 65 | Result := False; 66 | 67 | S := HexStr; 68 | SReplaceAll(S, ' ', ''); 69 | 70 | if (Length(S) mod 2) > 0 then Exit; 71 | 72 | for i := 1 to Length(S) div 2 do 73 | begin 74 | if not SHexWordToInt(S[2 * i - 1] + S[2 * i], Int) then Exit; 75 | ResStr := ResStr + Chr(Int); 76 | end; 77 | 78 | Result := True; 79 | end; 80 | 81 | 82 | function SToHex(const S: AnsiString): AnsiString; 83 | var 84 | i: Integer; 85 | begin 86 | Result := ''; 87 | 88 | for i := 1 to Length(S) do 89 | Result := Result + IntToHex(Ord(S[i]), 2) + ' '; 90 | 91 | if Result <> '' then 92 | Delete(Result, Length(Result), 1); 93 | end; 94 | 95 | 96 | function HexToIntDef(const S: string; const Default: Int64): Int64; 97 | var 98 | i: Integer; 99 | N: LongWord; 100 | begin 101 | Result := 0; 102 | for i := 1 to Length(S) do 103 | begin 104 | if not SHexDigitToInt(S[i], N) then 105 | begin Result := Default; Exit end; 106 | Result := Result * $10 + N; 107 | end; 108 | end; 109 | 110 | end. 111 | -------------------------------------------------------------------------------- /third_party/ATViewer/ATxTimer.pas: -------------------------------------------------------------------------------- 1 | //The code is taken from Delphi source. 2 | //Copyright (c) Borland corp. 3 | unit ATxTimer; 4 | 5 | interface 6 | uses Windows, Messages, classes, VCL.Forms; 7 | type 8 | TOldTimer = class(TComponent) 9 | private 10 | FInterval: Cardinal; 11 | FWindowHandle: HWND; 12 | FOnTimer: TNotifyEvent; 13 | FEnabled: Boolean; 14 | procedure UpdateTimer; 15 | procedure SetEnabled(Value: Boolean); 16 | procedure SetInterval(Value: Cardinal); 17 | procedure SetOnTimer(Value: TNotifyEvent); 18 | procedure WndProc(var Msg: TMessage); 19 | protected 20 | procedure Timer; dynamic; 21 | {$IFDEF CLR} 22 | strict protected 23 | procedure Finalize; override; 24 | {$ENDIF} 25 | public 26 | constructor Create(AOwner: TComponent); override; 27 | destructor Destroy; override; 28 | published 29 | property Enabled: Boolean read FEnabled write SetEnabled default True; 30 | property Interval: Cardinal read FInterval write SetInterval default 1000; 31 | property OnTimer: TNotifyEvent read FOnTimer write SetOnTimer; 32 | end; 33 | 34 | implementation 35 | 36 | { TOldTimer } 37 | 38 | constructor TOldTimer.Create(AOwner: TComponent); 39 | begin 40 | inherited Create(AOwner); 41 | FEnabled := True; 42 | FInterval := 1000; 43 | FWindowHandle := AllocateHWnd(WndProc); 44 | end; 45 | 46 | destructor TOldTimer.Destroy; 47 | begin 48 | FEnabled := False; 49 | {$IFDEF CLR} 50 | if FWindowHandle <> 0 then 51 | begin 52 | UpdateTimer; 53 | DeallocateHWnd(FWindowHandle); 54 | FWindowHandle := 0; 55 | end; 56 | System.GC.SuppressFinalize(self); 57 | {$ELSE} 58 | DeallocateHWnd(FWindowHandle); 59 | {$ENDIF} 60 | inherited Destroy; 61 | end; 62 | 63 | {$IFDEF CLR} 64 | procedure TOldTimer.Finalize; 65 | begin 66 | FEnabled := False; 67 | if FWindowHandle <> 0 then 68 | begin 69 | KillTimer(FWindowHandle, 1); 70 | DeallocateHWnd(FWindowHandle); 71 | FWindowHandle := 0; 72 | end; 73 | inherited; 74 | end; 75 | {$ENDIF} 76 | 77 | procedure TOldTimer.WndProc(var Msg: TMessage); 78 | begin 79 | with Msg do 80 | if Msg = WM_TIMER then 81 | try 82 | Timer; 83 | except 84 | Application.HandleException(Self); 85 | end 86 | else 87 | Result := DefWindowProc(FWindowHandle, Msg, wParam, lParam); 88 | end; 89 | 90 | procedure TOldTimer.UpdateTimer; 91 | begin 92 | KillTimer(FWindowHandle, 1); 93 | if (FInterval <> 0) and FEnabled and Assigned(FOnTimer) then 94 | if SetTimer(FWindowHandle, 1, FInterval, nil) = 0 then 95 | raise EOutOfResources.Create('No more Timers'); 96 | end; 97 | 98 | procedure TOldTimer.SetEnabled(Value: Boolean); 99 | begin 100 | if Value <> FEnabled then 101 | begin 102 | FEnabled := Value; 103 | UpdateTimer; 104 | end; 105 | end; 106 | 107 | procedure TOldTimer.SetInterval(Value: Cardinal); 108 | begin 109 | if Value <> FInterval then 110 | begin 111 | FInterval := Value; 112 | UpdateTimer; 113 | end; 114 | end; 115 | 116 | procedure TOldTimer.SetOnTimer(Value: TNotifyEvent); 117 | begin 118 | FOnTimer := Value; 119 | UpdateTimer; 120 | end; 121 | 122 | procedure TOldTimer.Timer; 123 | begin 124 | if Assigned(FOnTimer) then FOnTimer(Self); 125 | end; 126 | 127 | 128 | end. 129 | -------------------------------------------------------------------------------- /third_party/ATViewer/ATxUtf8Detect.pas: -------------------------------------------------------------------------------- 1 | //Copyright (c) Christian Ghisler (ghisler.com) 2 | unit ATxUTF8Detect; 3 | 4 | interface 5 | 6 | //PartialAllowed must be set to true if the buffer is smaller than the file. 7 | function IsBufferUtf8(buf:PAnsiChar;PartialAllowed:boolean):boolean; 8 | 9 | implementation 10 | 11 | const bytesFromUTF8:array[AnsiChar] of byte = ( 12 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 32 13 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 64 14 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 96 15 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //128 16 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //160 17 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //192 18 | 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, //224 19 | 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5); //256 20 | 21 | function GetUtf8CharWidth(firstchar:AnsiChar):integer; 22 | begin 23 | result:=bytesFromUTF8[firstchar]+1; 24 | end; 25 | 26 | function IsFirstUTF8Char(thechar:AnsiChar):boolean; 27 | {The remaining bytes in a multi-byte sequence have 10 as their two most significant bits.} 28 | begin 29 | result:=(byte(thechar) and (128+64))<>128; 30 | end; 31 | 32 | function IsSecondaryUTF8Char(thechar:AnsiChar):boolean; 33 | {The remaining bytes in a multi-byte sequence have 10 as their two most significant bits.} 34 | begin 35 | result:=(byte(thechar) and (128+64))=128; 36 | end; 37 | 38 | function IsBufferUtf8(buf:PAnsiChar;PartialAllowed:boolean):boolean; 39 | {Buffer contains only valid UTF-8 characters, no secondary alone, 40 | no primary without the correct nr of secondary} 41 | var p:PAnsiChar; 42 | utf8bytes:integer; 43 | hadutf8bytes:boolean; 44 | begin 45 | p:=buf; 46 | hadutf8bytes:=false; 47 | result:=false; 48 | utf8bytes:=0; 49 | while p[0]<>#0 do begin 50 | if utf8bytes>0 then begin {Expecting secondary AnsiChar} 51 | hadutf8bytes:=true; 52 | if not IsSecondaryUTF8Char(p[0]) then exit; {Fail!} 53 | dec(utf8bytes); 54 | end else if IsFirstUTF8Char(p[0]) then 55 | utf8bytes:=GetUtf8CharWidth(p[0])-1 56 | else if IsSecondaryUTF8Char(p[0]) then 57 | exit; {Fail!} 58 | inc(p); 59 | end; 60 | result:=hadutf8bytes and (PartialAllowed or (utf8bytes=0)); 61 | end; 62 | 63 | end. 64 | -------------------------------------------------------------------------------- /third_party/FastMM4/FastMM4.pas: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hartmutdavid/TensorFlow4Delphi/8404a8de247cfdf6d94de2a4752a28cdbf7dfdc8/third_party/FastMM4/FastMM4.pas -------------------------------------------------------------------------------- /third_party/FastMM4/FastMM4Messages.pas: -------------------------------------------------------------------------------- 1 | { 2 | 3 | Fast Memory Manager: Messages 4 | 5 | English translation by Pierre le Riche. 6 | 7 | } 8 | 9 | unit FastMM4Messages; 10 | 11 | interface 12 | 13 | {$Include FastMM4Options.inc} 14 | 15 | const 16 | {The name of the debug info support DLL} 17 | FullDebugModeLibraryName32Bit = 'FastMM_FullDebugMode.dll'; 18 | FullDebugModeLibraryName64Bit = 'FastMM_FullDebugMode64.dll'; 19 | {Event log strings} 20 | LogFileExtension = '_MemoryManager_EventLog.txt'#0; 21 | CRLF = #13#10; 22 | EventSeparator = '--------------------------------'; 23 | {Class name messages} 24 | UnknownClassNameMsg = 'Unknown'; 25 | {Memory dump message} 26 | MemoryDumpMsg = #13#10#13#10'Current memory dump of 256 bytes starting at pointer address '; 27 | {Block Error Messages} 28 | BlockScanLogHeader = 'Allocated block logged by LogAllocatedBlocksToFile. The size is: '; 29 | ErrorMsgHeader = 'FastMM has detected an error during a '; 30 | GetMemMsg = 'GetMem'; 31 | FreeMemMsg = 'FreeMem'; 32 | ReallocMemMsg = 'ReallocMem'; 33 | BlockCheckMsg = 'free block scan'; 34 | OperationMsg = ' operation. '; 35 | BlockHeaderCorruptedMsg = 'The block header has been corrupted. '; 36 | BlockFooterCorruptedMsg = 'The block footer has been corrupted. '; 37 | FreeModifiedErrorMsg = 'FastMM detected that a block has been modified after being freed. '; 38 | FreeModifiedDetailMsg = #13#10#13#10'Modified byte offsets (and lengths): '; 39 | DoubleFreeErrorMsg = 'An attempt has been made to free/reallocate an unallocated block.'; 40 | WrongMMFreeErrorMsg = 'An attempt has been made to free/reallocate a block that was allocated through a different FastMM instance. Check your memory manager sharing settings.'; 41 | PreviousBlockSizeMsg = #13#10#13#10'The previous block size was: '; 42 | CurrentBlockSizeMsg = #13#10#13#10'The block size is: '; 43 | PreviousObjectClassMsg = #13#10#13#10'The block was previously used for an object of class: '; 44 | CurrentObjectClassMsg = #13#10#13#10'The block is currently used for an object of class: '; 45 | PreviousAllocationGroupMsg = #13#10#13#10'The allocation group was: '; 46 | PreviousAllocationNumberMsg = #13#10#13#10'The allocation number was: '; 47 | CurrentAllocationGroupMsg = #13#10#13#10'The allocation group is: '; 48 | CurrentAllocationNumberMsg = #13#10#13#10'The allocation number is: '; 49 | BlockErrorMsgTitle = 'Memory Error Detected'; 50 | VirtualMethodErrorHeader = 'FastMM has detected an attempt to call a virtual method on a freed object. An access violation will now be raised in order to abort the current operation.'; 51 | InterfaceErrorHeader = 'FastMM has detected an attempt to use an interface of a freed object. An access violation will now be raised in order to abort the current operation.'; 52 | BlockHeaderCorruptedNoHistoryMsg = ' Unfortunately the block header has been corrupted so no history is available.'; 53 | FreedObjectClassMsg = #13#10#13#10'Freed object class: '; 54 | VirtualMethodName = #13#10#13#10'Virtual method: '; 55 | VirtualMethodOffset = 'Offset +'; 56 | VirtualMethodAddress = #13#10#13#10'Virtual method address: '; 57 | {Stack trace messages} 58 | CurrentThreadIDMsg = #13#10#13#10'The current thread ID is 0x'; 59 | CurrentStackTraceMsg = ', and the stack trace (return addresses) leading to this error is:'; 60 | ThreadIDPrevAllocMsg = #13#10#13#10'This block was previously allocated by thread 0x'; 61 | ThreadIDAtAllocMsg = #13#10#13#10'This block was allocated by thread 0x'; 62 | ThreadIDAtFreeMsg = #13#10#13#10'The block was previously freed by thread 0x'; 63 | ThreadIDAtObjectAllocMsg = #13#10#13#10'The object was allocated by thread 0x'; 64 | ThreadIDAtObjectFreeMsg = #13#10#13#10'The object was subsequently freed by thread 0x'; 65 | StackTraceMsg = ', and the stack trace (return addresses) at the time was:'; 66 | {Installation Messages} 67 | AlreadyInstalledMsg = 'FastMM4 is already installed.'; 68 | AlreadyInstalledTitle = 'Already installed.'; 69 | OtherMMInstalledMsg = 'FastMM4 cannot be installed since another third party memory ' 70 | + 'manager has already installed itself.'#13#10'If you want to use FastMM4, ' 71 | + 'please make sure that FastMM4.pas is the very first unit in the "uses"' 72 | + #13#10'section of your project''s .dpr file.'; 73 | OtherMMInstalledTitle = 'Cannot install FastMM4 - Another memory manager is already installed'; 74 | MemoryAllocatedMsg = 'FastMM4 cannot install since memory has already been ' 75 | + 'allocated through the default memory manager.'#13#10'FastMM4.pas MUST ' 76 | + 'be the first unit in your project''s .dpr file, otherwise memory may ' 77 | + 'be allocated'#13#10'through the default memory manager before FastMM4 ' 78 | + 'gains control. '#13#10#13#10'If you are using an exception trapper ' 79 | + 'like MadExcept (or any tool that modifies the unit initialization ' 80 | + 'order),'#13#10'go into its configuration page and ensure that the ' 81 | + 'FastMM4.pas unit is initialized before any other unit.'; 82 | MemoryAllocatedTitle = 'Cannot install FastMM4 - Memory has already been allocated'; 83 | {Leak checking messages} 84 | LeakLogHeader = 'A memory block has been leaked. The size is: '; 85 | LeakMessageHeader = 'This application has leaked memory. '; 86 | SmallLeakDetail = 'The small block leaks are' 87 | {$ifdef HideExpectedLeaksRegisteredByPointer} 88 | + ' (excluding expected leaks registered by pointer)' 89 | {$endif} 90 | + ':'#13#10; 91 | LargeLeakDetail = 'The sizes of leaked medium and large blocks are' 92 | {$ifdef HideExpectedLeaksRegisteredByPointer} 93 | + ' (excluding expected leaks registered by pointer)' 94 | {$endif} 95 | + ': '; 96 | BytesMessage = ' bytes: '; 97 | AnsiStringBlockMessage = 'AnsiString'; 98 | UnicodeStringBlockMessage = 'UnicodeString'; 99 | LeakMessageFooter = #13#10 100 | {$ifndef HideMemoryLeakHintMessage} 101 | + #13#10'Note: ' 102 | {$ifdef RequireIDEPresenceForLeakReporting} 103 | + 'This memory leak check is only performed if Delphi is currently running on the same computer. ' 104 | {$endif} 105 | {$ifdef FullDebugMode} 106 | {$ifdef LogMemoryLeakDetailToFile} 107 | + 'Memory leak detail is logged to a text file in the same folder as this application. ' 108 | {$else} 109 | + 'Enable the "LogMemoryLeakDetailToFile" to obtain a log file containing detail on memory leaks. ' 110 | {$endif} 111 | {$else} 112 | + 'To obtain a log file containing detail on memory leaks, enable the "FullDebugMode" and "LogMemoryLeakDetailToFile" conditional defines. ' 113 | {$endif} 114 | + 'To disable this memory leak check, undefine "EnableMemoryLeakReporting".'#13#10 115 | {$endif} 116 | + #0; 117 | LeakMessageTitle = 'Memory Leak Detected'; 118 | {$ifdef UseOutputDebugString} 119 | FastMMInstallMsg = 'FastMM has been installed.'; 120 | FastMMInstallSharedMsg = 'Sharing an existing instance of FastMM.'; 121 | FastMMUninstallMsg = 'FastMM has been uninstalled.'; 122 | FastMMUninstallSharedMsg = 'Stopped sharing an existing instance of FastMM.'; 123 | {$endif} 124 | {$ifdef DetectMMOperationsAfterUninstall} 125 | InvalidOperationTitle = 'MM Operation after uninstall.'; 126 | InvalidGetMemMsg = 'FastMM has detected a GetMem call after FastMM was uninstalled.'; 127 | InvalidFreeMemMsg = 'FastMM has detected a FreeMem call after FastMM was uninstalled.'; 128 | InvalidReallocMemMsg = 'FastMM has detected a ReallocMem call after FastMM was uninstalled.'; 129 | InvalidAllocMemMsg = 'FastMM has detected an AllocMem call after FastMM was uninstalled.'; 130 | {$endif} 131 | 132 | implementation 133 | 134 | end. 135 | 136 | --------------------------------------------------------------------------------