| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 1088e1f commit be89140
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -54,9 +54,27 @@ private IntPtr Allocate(NDArray nd) | |||
| 54 | 54 | case "Double": | |
| 55 | 55 | Marshal.Copy(nd.Data<double>(), 0, dotHandle, nd.size); | |
| 56 | 56 | break; | |
| 57 | + case "Byte": | ||
| 58 | + var bb = nd.Data<byte>(); | ||
| 59 | + var bytes = Marshal.AllocHGlobal(bb.Length) ; | ||
| 60 | + ulong bytes_len = c_api.TF_StringEncodedSize((ulong)bb.Length); | ||
| 61 | + var dataTypeByte = ToTFDataType(nd.dtype); | ||
| 62 | + // shape | ||
| 63 | + var dims2 = nd.shape.Select(x => (long)x).ToArray(); | ||
| 64 | + | ||
| 65 | + var tfHandle2 = c_api.TF_AllocateTensor(dataTypeByte, | ||
| 66 | + dims2, | ||
| 67 | + nd.ndim, | ||
| 68 | + bytes_len + sizeof(Int64)); | ||
| 69 | + | ||
| 70 | + dotHandle = c_api.TF_TensorData(tfHandle2); | ||
| 71 | + Marshal.WriteInt64(dotHandle, 0); | ||
| 72 | + c_api.TF_StringEncode(bytes, (ulong)bb.Length, dotHandle + sizeof(Int64), bytes_len, status); | ||
| 73 | + return tfHandle2; | ||
| 57 | 74 | case "String": | |
| 58 | - var str = nd.Data<string>()[0]; | ||
| 59 | - ulong dst_len = c_api.TF_StringEncodedSize((ulong)str.Length); | ||
| 75 | + string ss = nd.Data<string>()[0]; | ||
| 76 | + var str = Marshal.StringToHGlobalAnsi(ss); | ||
| 77 | + ulong dst_len = c_api.TF_StringEncodedSize((ulong)ss.Length); | ||
| 60 | 78 | var dataType1 = ToTFDataType(nd.dtype); | |
| 61 | 79 | // shape | |
| 62 | 80 | var dims1 = nd.shape.Select(x => (long)x).ToArray(); | |
@@ -68,7 +86,7 @@ private IntPtr Allocate(NDArray nd) | |||
| 68 | 86 | ||
| 69 | 87 | dotHandle = c_api.TF_TensorData(tfHandle1); | |
| 70 | 88 | Marshal.WriteInt64(dotHandle, 0); | |
| 71 | - c_api.TF_StringEncode(str, (ulong)str.Length, dotHandle + sizeof(Int64), dst_len, status); | ||
| 89 | + c_api.TF_StringEncode(str, (ulong)ss.Length, dotHandle + sizeof(Int64), dst_len, status); | ||
| 72 | 90 | return tfHandle1; | |
| 73 | 91 | default: | |
| 74 | 92 | throw new NotImplementedException("Marshal.Copy failed."); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -164,6 +164,7 @@ public TF_DataType ToTFDataType(Type type) | |||
| 164 | 164 | return TF_DataType.TF_FLOAT; | |
| 165 | 165 | case "Double": | |
| 166 | 166 | return TF_DataType.TF_DOUBLE; | |
| 167 | + case "Byte": | ||
| 167 | 168 | case "String": | |
| 168 | 169 | return TF_DataType.TF_STRING; | |
| 169 | 170 | default: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -120,7 +120,7 @@ public partial class c_api | |||
| 120 | 120 | /// <param name="status">TF_Status*</param> | |
| 121 | 121 | /// <returns>On success returns the size in bytes of the encoded string.</returns> | |
| 122 | 122 | [DllImport(TensorFlowLibName)] | |
| 123 | - public static extern ulong TF_StringEncode(string src, ulong src_len, IntPtr dst, ulong dst_len, IntPtr status); | ||
| 123 | + public static extern ulong TF_StringEncode(IntPtr src, ulong src_len, IntPtr dst, ulong dst_len, IntPtr status); | ||
| 124 | 124 | ||
| 125 | 125 | /// <summary> | |
| 126 | 126 | /// Decode a string encoded using TF_StringEncode. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -57,5 +57,12 @@ public static Session Session() | |||
| 57 | 57 | defaultSession = new Session(); | |
| 58 | 58 | return defaultSession; | |
| 59 | 59 | } | |
| 60 | + | ||
| 61 | + public static Session Session(Graph graph) | ||
| 62 | + { | ||
| 63 | + g = graph; | ||
| 64 | + defaultSession = new Session(); | ||
| 65 | + return defaultSession; | ||
| 66 | + } | ||
| 60 | 67 | } | |
| 61 | 68 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,31 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.IO; | ||
| 4 | + using System.IO.Compression; | ||
| 5 | + using System.Linq; | ||
| 6 | + using System.Net; | ||
| 7 | + using System.Text; | ||
| 8 | + using Tensorflow; | ||
| 9 | + | ||
| 10 | + namespace TensorFlowNET.Examples | ||
| 11 | + { | ||
| 12 | + public class ImageRecognition : Python, IExample | ||
| 13 | + { | ||
| 14 | + public void Run() | ||
| 15 | + { | ||
| 16 | + var graph = new Graph(); | ||
| 17 | + // 从文件加载序列化的GraphDef | ||
| 18 | + //导入GraphDef | ||
| 19 | + graph.Import("tmp/tensorflow_inception_graph.pb"); | ||
| 20 | + with<Session>(tf.Session(graph), sess => | ||
| 21 | + { | ||
| 22 | + var labels = File.ReadAllLines("tmp/imagenet_comp_graph_label_strings.txt"); | ||
| 23 | + var files = Directory.GetFiles("img"); | ||
| 24 | + foreach(var file in files) | ||
| 25 | + { | ||
| 26 | + var tensor = new Tensor(File.ReadAllBytes(file)); | ||
| 27 | + } | ||
| 28 | + }); | ||
| 29 | + } | ||
| 30 | + } | ||
| 31 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,133 @@ | |||
| 1 | + | ||
| 2 | + from __future__ import absolute_import, division, print_function | ||
| 3 | + | ||
| 4 | + import tensorflow as tf | ||
| 5 | + from tensorflow import keras | ||
| 6 | + | ||
| 7 | + import numpy as np | ||
| 8 | + | ||
| 9 | + print(tf.__version__) | ||
| 10 | + | ||
| 11 | + imdb = keras.datasets.imdb | ||
| 12 | + | ||
| 13 | + (train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000) | ||
| 14 | + | ||
| 15 | + print("Training entries: {}, labels: {}".format(len(train_data), len(train_labels))) | ||
| 16 | + print(train_data[0]) | ||
| 17 | + len(train_data[0]), len(train_data[1]) | ||
| 18 | + | ||
| 19 | + # A dictionary mapping words to an integer index | ||
| 20 | + word_index = imdb.get_word_index() | ||
| 21 | + | ||
| 22 | + # The first indices are reserved | ||
| 23 | + word_index = {k:(v+3) for k,v in word_index.items()} | ||
| 24 | + word_index["<PAD>"] = 0 | ||
| 25 | + word_index["<START>"] = 1 | ||
| 26 | + word_index["<UNK>"] = 2 # unknown | ||
| 27 | + word_index["<UNUSED>"] = 3 | ||
| 28 | + | ||
| 29 | + reverse_word_index = dict([(value, key) for (key, value) in word_index.items()]) | ||
| 30 | + | ||
| 31 | + def decode_review(text): | ||
| 32 | + return ' '.join([reverse_word_index.get(i, '?') for i in text]) | ||
| 33 | + | ||
| 34 | + decode_review(train_data[0]) | ||
| 35 | + | ||
| 36 | + | ||
| 37 | + train_data = keras.preprocessing.sequence.pad_sequences(train_data, | ||
| 38 | + value=word_index["<PAD>"], | ||
| 39 | + padding='post', | ||
| 40 | + maxlen=256) | ||
| 41 | + | ||
| 42 | + test_data = keras.preprocessing.sequence.pad_sequences(test_data, | ||
| 43 | + value=word_index["<PAD>"], | ||
| 44 | + padding='post', | ||
| 45 | + maxlen=256) | ||
| 46 | + | ||
| 47 | + | ||
| 48 | + print(train_data[0]) | ||
| 49 | + | ||
| 50 | + # input shape is the vocabulary count used for the movie reviews (10,000 words) | ||
| 51 | + vocab_size = 10000 | ||
| 52 | + | ||
| 53 | + model = keras.Sequential() | ||
| 54 | + model.add(keras.layers.Embedding(vocab_size, 16)) | ||
| 55 | + model.add(keras.layers.GlobalAveragePooling1D()) | ||
| 56 | + model.add(keras.layers.Dense(16, activation=tf.nn.relu)) | ||
| 57 | + model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid)) | ||
| 58 | + | ||
| 59 | + model.summary() | ||
| 60 | + | ||
| 61 | + model.compile(optimizer='adam', | ||
| 62 | + loss='binary_crossentropy', | ||
| 63 | + metrics=['accuracy']) | ||
| 64 | + | ||
| 65 | + | ||
| 66 | + x_val = train_data[:10000] | ||
| 67 | + partial_x_train = train_data[10000:] | ||
| 68 | + | ||
| 69 | + y_val = train_labels[:10000] | ||
| 70 | + partial_y_train = train_labels[10000:] | ||
| 71 | + | ||
| 72 | + history = model.fit(partial_x_train, | ||
| 73 | + partial_y_train, | ||
| 74 | + epochs=40, | ||
| 75 | + batch_size=512, | ||
| 76 | + validation_data=(x_val, y_val), | ||
| 77 | + verbose=1) | ||
| 78 | + | ||
| 79 | + results = model.evaluate(test_data, test_labels) | ||
| 80 | + | ||
| 81 | + # serialize model to JSON | ||
| 82 | + model_json = model.to_json() | ||
| 83 | + with open("model.json", "w") as json_file: | ||
| 84 | + json_file.write(model_json) | ||
| 85 | + # serialize weights to HDF5 | ||
| 86 | + model.save_weights("model.h5") | ||
| 87 | + print("Saved model to disk") | ||
| 88 | + | ||
| 89 | + # load json and create model | ||
| 90 | + json_file = open('model.json', 'r') | ||
| 91 | + loaded_model_json = json_file.read() | ||
| 92 | + json_file.close() | ||
| 93 | + loaded_model = model_from_json(loaded_model_json) | ||
| 94 | + # load weights into new model | ||
| 95 | + loaded_model.load_weights("model.h5") | ||
| 96 | + print("Loaded model from disk") | ||
| 97 | + | ||
| 98 | + print(results) | ||
| 99 | + | ||
| 100 | + history_dict = history.history | ||
| 101 | + history_dict.keys() | ||
| 102 | + | ||
| 103 | + import matplotlib.pyplot as plt | ||
| 104 | + | ||
| 105 | + acc = history_dict['acc'] | ||
| 106 | + val_acc = history_dict['val_acc'] | ||
| 107 | + loss = history_dict['loss'] | ||
| 108 | + val_loss = history_dict['val_loss'] | ||
| 109 | + | ||
| 110 | + epochs = range(1, len(acc) + 1) | ||
| 111 | + | ||
| 112 | + # "bo" is for "blue dot" | ||
| 113 | + plt.plot(epochs, loss, 'bo', label='Training loss') | ||
| 114 | + # b is for "solid blue line" | ||
| 115 | + plt.plot(epochs, val_loss, 'b', label='Validation loss') | ||
| 116 | + plt.title('Training and validation loss') | ||
| 117 | + plt.xlabel('Epochs') | ||
| 118 | + plt.ylabel('Loss') | ||
| 119 | + plt.legend() | ||
| 120 | + | ||
| 121 | + plt.show() | ||
| 122 | + | ||
| 123 | + | ||
| 124 | + plt.clf() # clear figure | ||
| 125 | + | ||
| 126 | + plt.plot(epochs, acc, 'bo', label='Training acc') | ||
| 127 | + plt.plot(epochs, val_acc, 'b', label='Validation acc') | ||
| 128 | + plt.title('Training and validation accuracy') | ||
| 129 | + plt.xlabel('Epochs') | ||
| 130 | + plt.ylabel('Accuracy') | ||
| 131 | + plt.legend() | ||
| 132 | + | ||
| 133 | + plt.show() | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments