| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3e76c19 commit 374ff58
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -60,7 +60,11 @@ public Tensor resize_images(Tensor images, Tensor size, string method = ResizeMe | |||
| 60 | 60 | ||
| 61 | 61 | public Tensor resize_images_v2(Tensor images, TensorShape size, string method = ResizeMethod.BILINEAR, bool preserve_aspect_ratio = false, bool antialias = false, | |
| 62 | 62 | string name = null) | |
| 63 | - => image_ops_impl.resize_images(images, tf.constant(size.dims), method, preserve_aspect_ratio, antialias, name); | ||
| 63 | + => image_ops_impl.resize_images_v2(images, size, method, preserve_aspect_ratio, antialias, name); | ||
| 64 | + | ||
| 65 | + public Tensor resize_images_v2(Tensor images, Tensor size, string method = ResizeMethod.BILINEAR, bool preserve_aspect_ratio = false, bool antialias = false, | ||
| 66 | + string name = null) | ||
| 67 | + => image_ops_impl.resize_images_v2(images, size, method, preserve_aspect_ratio, antialias, name); | ||
| 64 | 68 | ||
| 65 | 69 | public Tensor resize_images_with_pad(Tensor image, int target_height, int target_width, string method, bool antialias) | |
| 66 | 70 | => image_ops_impl.resize_images_with_pad(image, target_height, target_width, method, antialias); | |
@@ -209,6 +213,9 @@ public Tensor extract_glimpse(Tensor input, Tensor size, Tensor offsets, bool ce | |||
| 209 | 213 | public Tensor resize(Tensor image, TensorShape size, string method = ResizeMethod.BILINEAR) | |
| 210 | 214 | => image_ops_impl.resize_images_v2(image, size, method: method); | |
| 211 | 215 | ||
| 216 | + public Tensor resize(Tensor image, Tensor size, string method = ResizeMethod.BILINEAR) | ||
| 217 | + => image_ops_impl.resize_images_v2(image, size, method: method); | ||
| 218 | + | ||
| 212 | 219 | public Tensor resize_bilinear(Tensor images, Tensor size, bool align_corners = false, bool half_pixel_centers = false, string name = null) | |
| 213 | 220 | => gen_image_ops.resize_bilinear(images, size, align_corners: align_corners, half_pixel_centers: half_pixel_centers, name: name); | |
| 214 | 221 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -175,18 +175,32 @@ public static Tensor pad(Tensor input, Tensor paddings, string name = null) | |||
| 175 | 175 | { | |
| 176 | 176 | if (tf.Context.executing_eagerly()) | |
| 177 | 177 | { | |
| 178 | - var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, | ||
| 178 | + /*var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName, | ||
| 179 | 179 | "Pad", name, | |
| 180 | 180 | null, | |
| 181 | 181 | input, paddings); | |
| 182 | - return results[0]; | ||
| 182 | + return results[0];*/ | ||
| 183 | + return pad_eager_fallback(input, paddings, name: name, ctx: tf.Context); | ||
| 183 | 184 | } | |
| 184 | 185 | ||
| 185 | 186 | var _op = tf.OpDefLib._apply_op_helper("Pad", name: name, args: new { input, paddings }); | |
| 186 | 187 | ||
| 187 | 188 | return _op.output; | |
| 188 | 189 | } | |
| 189 | 190 | ||
| 191 | + private static Tensor pad_eager_fallback(Tensor inputs, Tensor padding, string name = null, Context ctx = null) | ||
| 192 | + { | ||
| 193 | + var (_attr_T, input) = tf.Runner.ArgsToMatchingEager(ctx, args: new[] { inputs }); | ||
| 194 | + var (_attr_Tpaddings, paddings) = tf.Runner.ArgsToMatchingEager(ctx, default_dtype: tf.int32, args: new[] { padding }); | ||
| 195 | + var _inputs_flat = input.concat(paddings); | ||
| 196 | + var _attrs = new object[] { "T", _attr_T, "Tpaddings", _attr_Tpaddings }; | ||
| 197 | + | ||
| 198 | + var results = tf.Runner.Execute(ctx, "Pad", 1, _inputs_flat, _attrs, name: name); | ||
| 199 | + if (tf.Runner.MustRecordGradient()) | ||
| 200 | + tf.Runner.RecordGradient("Pad", _inputs_flat, _attrs, results); | ||
| 201 | + return results[0]; | ||
| 202 | + } | ||
| 203 | + | ||
| 190 | 204 | public static Tensor pack(Tensor[] values, int axis = 0, string name = null) | |
| 191 | 205 | { | |
| 192 | 206 | if (tf.Context.executing_eagerly()) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2210,7 +2210,7 @@ public static Tensor convert_image_dtype(Tensor image, TF_DataType dtype, bool s | |||
| 2210 | 2210 | /// <param name="antialias"></param> | |
| 2211 | 2211 | /// <param name="name"></param> | |
| 2212 | 2212 | /// <returns></returns> | |
| 2213 | - public static Tensor resize_images_v2(Tensor images, TensorShape size, string method = ResizeMethod.BILINEAR, | ||
| 2213 | + public static Tensor resize_images_v2<T>(Tensor images, T size, string method = ResizeMethod.BILINEAR, | ||
| 2214 | 2214 | bool preserve_aspect_ratio = false, | |
| 2215 | 2215 | bool antialias = false, | |
| 2216 | 2216 | string name = null) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -183,5 +183,48 @@ public Tensor categorical_crossentropy(Tensor target, Tensor output, bool from_l | |||
| 183 | 183 | ||
| 184 | 184 | throw new NotImplementedException(""); | |
| 185 | 185 | } | |
| 186 | + | ||
| 187 | + /// <summary> | ||
| 188 | + /// Resizes the images contained in a 4D tensor. | ||
| 189 | + /// </summary> | ||
| 190 | + /// <param name="x"></param> | ||
| 191 | + /// <param name="height_factor"></param> | ||
| 192 | + /// <param name="width_factor"></param> | ||
| 193 | + /// <param name="data_format"></param> | ||
| 194 | + /// <param name="interpolation"></param> | ||
| 195 | + /// <returns></returns> | ||
| 196 | + public Tensor resize_images(Tensor x, int height_factor, int width_factor, | ||
| 197 | + string data_format, string interpolation = "nearest") | ||
| 198 | + { | ||
| 199 | + var (rows, cols) = (0, 0); | ||
| 200 | + if (data_format == "channels_first") | ||
| 201 | + (rows, cols) = (2, 3); | ||
| 202 | + else if (data_format == "channels_last") | ||
| 203 | + (rows, cols) = (1, 2); | ||
| 204 | + else | ||
| 205 | + throw new ValueError($"Invalid `data_format` argument: {data_format}"); | ||
| 206 | + | ||
| 207 | + var original_shape = x.shape; | ||
| 208 | + var new_shape = array_ops.shape(x)[new Slice(rows, cols + 1)]; | ||
| 209 | + new_shape *= constant_op.constant(np.array(height_factor, width_factor)); | ||
| 210 | + | ||
| 211 | + if (data_format == "channels_first") | ||
| 212 | + // x = permute_dimensions(x, [0, 2, 3, 1]); | ||
| 213 | + throw new NotImplementedException(""); | ||
| 214 | + if (interpolation == "nearest") | ||
| 215 | + x = tf.image.resize_images_v2(x, new_shape, method: ResizeMethod.NEAREST_NEIGHBOR); | ||
| 216 | + | ||
| 217 | + if (data_format == "channels_first") | ||
| 218 | + // x = permute_dimensions(x, [0, 3, 1, 2]); | ||
| 219 | + throw new NotImplementedException(""); | ||
| 220 | + | ||
| 221 | + int new_height = original_shape[rows] < 0 ? -1 : original_shape[rows] * height_factor; | ||
| 222 | + int new_width = original_shape[cols] < 0 ? -1 : original_shape[cols] * width_factor; | ||
| 223 | + | ||
| 224 | + TensorShape output_shape = data_format == "channels_first" ? | ||
| 225 | + (-1, -1, new_height, new_width) : (-1, new_height, new_width, -1); | ||
| 226 | + x.set_shape(output_shape); | ||
| 227 | + return x; | ||
| 228 | + } | ||
| 186 | 229 | } | |
| 187 | 230 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments