|
1 | | -using System; |
| 1 | +using NumSharp; |
| 2 | +using System; |
2 | 3 | using System.Collections.Generic; |
3 | 4 | using System.Diagnostics; |
4 | 5 | using System.IO; |
@@ -105,7 +106,83 @@ private void get_or_create_bottleneck(Session sess, Dictionary<string, Dictionar |
105 | 106 | Tensor bottleneck_tensor, string module_name) |
106 | 107 | { |
107 | 108 | var label_lists = image_lists[label_name]; |
108 | | - var sub_dir_path = Path.Join(image_dir, label_name); |
| 109 | + var sub_dir_path = Path.Join(bottleneck_dir, label_name); |
| 110 | + Directory.CreateDirectory(sub_dir_path); |
| 111 | + string bottleneck_path = get_bottleneck_path(image_lists, label_name, index, |
| 112 | + bottleneck_dir, category, module_name); |
| 113 | + |
| 114 | + if (!File.Exists(bottleneck_path)) |
| 115 | + create_bottleneck_file(bottleneck_path, image_lists, label_name, index, |
| 116 | + image_dir, category, sess, jpeg_data_tensor, |
| 117 | + decoded_image_tensor, resized_input_tensor, |
| 118 | + bottleneck_tensor); |
| 119 | + } |
| 120 | + |
| 121 | + private void create_bottleneck_file(string bottleneck_path, Dictionary<string, Dictionary<string, string[]>> image_lists, |
| 122 | + string label_name, int index, string image_dir, string category, Session sess, |
| 123 | + Tensor jpeg_data_tensor, Tensor decoded_image_tensor, Tensor resized_input_tensor, Tensor bottleneck_tensor) |
| 124 | + { |
| 125 | + // Create a single bottleneck file. |
| 126 | + print("Creating bottleneck at " + bottleneck_path); |
| 127 | + var image_path = get_image_path(image_lists, label_name, index, image_dir, category); |
| 128 | + if (!File.Exists(image_path)) |
| 129 | + print($"File does not exist {image_path}"); |
| 130 | + |
| 131 | + var image_data = File.ReadAllBytes(image_path); |
| 132 | + var bottleneck_values = run_bottleneck_on_image( |
| 133 | + sess, image_data, jpeg_data_tensor, decoded_image_tensor, |
| 134 | + resized_input_tensor, bottleneck_tensor); |
| 135 | + } |
| 136 | + |
| 137 | + /// <summary> |
| 138 | + /// Runs inference on an image to extract the 'bottleneck' summary layer. |
| 139 | + /// </summary> |
| 140 | + /// <param name="sess">Current active TensorFlow Session.</param> |
| 141 | + /// <param name="image_path">Path of raw JPEG data.</param> |
| 142 | + /// <param name="image_data_tensor">Input data layer in the graph.</param> |
| 143 | + /// <param name="decoded_image_tensor">Output of initial image resizing and preprocessing.</param> |
| 144 | + /// <param name="resized_input_tensor">The input node of the recognition graph.</param> |
| 145 | + /// <param name="bottleneck_tensor">Layer before the final softmax.</param> |
| 146 | + /// <returns></returns> |
| 147 | + private NDArray run_bottleneck_on_image(Session sess, byte[] image_data, Tensor image_data_tensor, |
| 148 | + Tensor decoded_image_tensor, Tensor resized_input_tensor, Tensor bottleneck_tensor) |
| 149 | + { |
| 150 | + // First decode the JPEG image, resize it, and rescale the pixel values. |
| 151 | + var resized_input_values = sess.run(decoded_image_tensor, new FeedItem(image_data_tensor, image_data)); |
| 152 | + // Then run it through the recognition network. |
| 153 | + var bottleneck_values = sess.run(bottleneck_tensor, new FeedItem(resized_input_tensor, resized_input_values)); |
| 154 | + bottleneck_values = np.squeeze(bottleneck_values); |
| 155 | + return bottleneck_values; |
| 156 | + } |
| 157 | + |
| 158 | + private string get_bottleneck_path(Dictionary<string, Dictionary<string, string[]>> image_lists, string label_name, int index, |
| 159 | + string bottleneck_dir, string category, string module_name) |
| 160 | + { |
| 161 | + module_name = (module_name.Replace("://", "~") // URL scheme. |
| 162 | + .Replace('/', '~') // URL and Unix paths. |
| 163 | + .Replace(':', '~').Replace('\\', '~')); // Windows paths. |
| 164 | + return get_image_path(image_lists, label_name, index, bottleneck_dir, |
| 165 | + category) + "_" + module_name + ".txt"; |
| 166 | + } |
| 167 | + |
| 168 | + private string get_image_path(Dictionary<string, Dictionary<string, string[]>> image_lists, string label_name, |
| 169 | + int index, string image_dir, string category) |
| 170 | + { |
| 171 | + if (!image_lists.ContainsKey(label_name)) |
| 172 | + print($"Label does not exist {label_name}"); |
| 173 | + |
| 174 | + var label_lists = image_lists[label_name]; |
| 175 | + if (!label_lists.ContainsKey(category)) |
| 176 | + print($"Category does not exist {category}"); |
| 177 | + var category_list = label_lists[category]; |
| 178 | + if (category_list.Length == 0) |
| 179 | + print($"Label {label_name} has no images in the category {category}."); |
| 180 | + |
| 181 | + var mod_index = index % len(category_list); |
| 182 | + var base_name = category_list[mod_index].Split(Path.DirectorySeparatorChar).Last(); |
| 183 | + var sub_dir = label_name; |
| 184 | + var full_path = Path.Join(image_dir, sub_dir, base_name); |
| 185 | + return full_path; |
109 | 186 | } |
110 | 187 |
|
111 | 188 | public void PrepareData() |
|
0 commit comments