|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using HDF.PInvoke; |
| 5 | +using NumSharp; |
| 6 | +using Tensorflow.Keras.Engine; |
| 7 | +using HDF5CSharp; |
| 8 | +using static Tensorflow.Binding; |
| 9 | +using static Tensorflow.KerasApi; |
| 10 | +namespace Tensorflow.Keras.Saving |
| 11 | +{ |
| 12 | + public class fdf5_format |
| 13 | + { |
| 14 | + |
| 15 | + public static void load_model_from_hdf5(string filepath = "", Dictionary<string, object> custom_objects = null, bool compile = false) |
| 16 | + { |
| 17 | + long root = Hdf5.OpenFile(filepath,true); |
| 18 | + load_model_from_hdf5(root, custom_objects, compile); |
| 19 | + } |
| 20 | + public static void load_model_from_hdf5(long filepath = -1, Dictionary<string, object> custom_objects = null, bool compile = false) |
| 21 | + { |
| 22 | + //long fileId = filepath; |
| 23 | + //try |
| 24 | + //{ |
| 25 | + // groupId = H5G.open(fileId, "/"); |
| 26 | + // (bool success, string[] attrId) = Hdf5.ReadStringAttributes(groupId, "model_config", ""); |
| 27 | + // H5G.close(groupId); |
| 28 | + // if (success == true) { |
| 29 | + // Console.WriteLine(attrId[0]); |
| 30 | + // } |
| 31 | + //} |
| 32 | + //catch (Exception ex) |
| 33 | + //{ |
| 34 | + // if (filepath != -1) { |
| 35 | + // Hdf5.CloseFile(filepath); |
| 36 | + // } |
| 37 | + // if (groupId != -1) { |
| 38 | + // H5G.close(groupId); |
| 39 | + // } |
| 40 | + // throw new Exception(ex.ToString()); |
| 41 | + //} |
| 42 | + |
| 43 | + } |
| 44 | + public static void save_model_to_hdf5(long filepath = -1, Dictionary<string, object> custom_objects = null, bool compile = false) |
| 45 | + { |
| 46 | + |
| 47 | + } |
| 48 | + public static void preprocess_weights_for_loading(long filepath = -1, Dictionary<string, object> custom_objects = null, bool compile = false) |
| 49 | + { |
| 50 | + |
| 51 | + } |
| 52 | + public static void _convert_rnn_weights(long filepath = -1, Dictionary<string, object> custom_objects = null, bool compile = false) |
| 53 | + { |
| 54 | + |
| 55 | + } |
| 56 | + public static void save_optimizer_weights_to_hdf5_group(long filepath = -1, Dictionary<string, object> custom_objects = null, bool compile = false) |
| 57 | + { |
| 58 | + |
| 59 | + } |
| 60 | + public static void load_optimizer_weights_from_hdf5_group(long filepath = -1, Dictionary<string, object> custom_objects = null, bool compile = false) |
| 61 | + { |
| 62 | + |
| 63 | + } |
| 64 | + public static void save_weights_to_hdf5_group(long filepath = -1, Dictionary<string, object> custom_objects = null, bool compile = false) |
| 65 | + { |
| 66 | + |
| 67 | + } |
| 68 | + public static void load_weights_from_hdf5_group(long f=-1,Model model=null) |
| 69 | + { |
| 70 | + string original_keras_version = "1"; |
| 71 | + string original_backend = null; |
| 72 | + if (Hdf5.AttributeExists(f, "keras_version")) |
| 73 | + { |
| 74 | + (bool success, string[] attr) = Hdf5.ReadStringAttributes(f, "keras_version", ""); |
| 75 | + if (success) |
| 76 | + { |
| 77 | + original_keras_version = attr[0]; |
| 78 | + } |
| 79 | + } |
| 80 | + if (Hdf5.AttributeExists(f, "backend")) |
| 81 | + { |
| 82 | + (bool success, string[] attr) = Hdf5.ReadStringAttributes(f, "backend", ""); |
| 83 | + if (success) |
| 84 | + { |
| 85 | + original_backend = attr[0]; |
| 86 | + } |
| 87 | + } |
| 88 | + List<ILayer> filtered_layers = new List<ILayer>(); |
| 89 | + List<Tensor> weights; |
| 90 | + foreach (var layer in model.Layers) |
| 91 | + { |
| 92 | + weights = _legacy_weights(layer); |
| 93 | + if (weights.Count>0) |
| 94 | + { |
| 95 | + filtered_layers.append(layer); |
| 96 | + } |
| 97 | + } |
| 98 | + string[] layer_names = load_attributes_from_hdf5_group(f,"layer_names"); |
| 99 | + List<NDArray> weight_values=new List<NDArray>(); |
| 100 | + foreach (var i in filtered_layers) { |
| 101 | + long g = H5G.open(f, i.Name); |
| 102 | + string[] weight_names = null; |
| 103 | + if (g != -1) |
| 104 | + { |
| 105 | + weight_names = load_attributes_from_hdf5_group(g, "weight_names"); |
| 106 | + } |
| 107 | + if (weight_names != null) |
| 108 | + { |
| 109 | + foreach (var i_ in weight_names) { |
| 110 | + (bool success, Array result) = Hdf5.ReadDataset<float>(g, i_); |
| 111 | + // |
| 112 | + weight_values.Add(np.array(result)); |
| 113 | + } |
| 114 | + } |
| 115 | + H5G.close(g); |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + public static void toarrayf4(long filepath = -1, Dictionary<string, object> custom_objects = null, bool compile = false) |
| 120 | + { |
| 121 | + |
| 122 | + } |
| 123 | + public static void load_weights_from_hdf5_group_by_name(long filepath = -1, Dictionary<string, object> custom_objects = null, bool compile = false) |
| 124 | + { |
| 125 | + |
| 126 | + } |
| 127 | + public static void save_attributes_to_hdf5_group(long filepath = -1, Dictionary<string, object> custom_objects = null, bool compile = false) |
| 128 | + { |
| 129 | + |
| 130 | + } |
| 131 | + public static string[] load_attributes_from_hdf5_group(long f = -1, string name = "") |
| 132 | + { |
| 133 | + if (Hdf5.AttributeExists(f, name)) |
| 134 | + { |
| 135 | + (bool success, string[] attr) = Hdf5.ReadStringAttributes(f, name, ""); |
| 136 | + if (success) |
| 137 | + { |
| 138 | + return attr; |
| 139 | + } |
| 140 | + } |
| 141 | + return null; |
| 142 | + } |
| 143 | + public static void load_attributes_from_hdf5_group(long filepath = -1, Dictionary<string, object> custom_objects = null, bool compile = false) |
| 144 | + { |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + public static List<Tensor> _legacy_weights(ILayer layer) |
| 149 | + { |
| 150 | + |
| 151 | + List<Tensor> weights= new List<Tensor>(); |
| 152 | + if (layer.trainable_weights.Count != 0) |
| 153 | + { |
| 154 | + Tensor[] trainable_weights = Array.ConvertAll<IVariableV1, Tensor>(layer.trainable_weights.ToArray(), s => s.AsTensor()); |
| 155 | + Tensor[] non_trainable_weights =null; |
| 156 | + if (layer.non_trainable_weights.Count != 0) |
| 157 | + { |
| 158 | + non_trainable_weights = Array.ConvertAll<IVariableV1, Tensor>(layer.non_trainable_weights.ToArray(), s => s.AsTensor()); |
| 159 | + } |
| 160 | + foreach (var i in trainable_weights) { |
| 161 | + if (non_trainable_weights != null) |
| 162 | + { |
| 163 | + foreach (var i_ in non_trainable_weights) |
| 164 | + { |
| 165 | + weights.Add(i + i_); |
| 166 | + } |
| 167 | + } |
| 168 | + else { |
| 169 | + weights.Add(i); |
| 170 | + }; |
| 171 | + |
| 172 | + |
| 173 | + } |
| 174 | + } |
| 175 | + return weights; |
| 176 | + } |
| 177 | + } |
| 178 | +} |
| 179 | + |
0 commit comments