@@ -69,6 +69,7 @@ limitations under the License.
6969#include " tensorflow/lite/kernels/internal/reference/quantize.h"
7070#include " tensorflow/lite/kernels/internal/reference/reduce.h"
7171#include " tensorflow/lite/kernels/internal/reference/requantize.h"
72+ #include " tensorflow/lite/kernels/internal/reference/resize_bilinear.h"
7273#include " tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h"
7374#include " tensorflow/lite/kernels/internal/reference/round.h"
7475#include " tensorflow/lite/kernels/internal/reference/softmax.h"
@@ -1055,198 +1056,6 @@ inline void ScatterNd(const RuntimeShape& indices_shape,
10551056 }
10561057}
10571058
1058- inline void ComputeInterpolationValues (const float value, const float scale,
1059- const bool half_pixel_centers,
1060- int32 input_size, float * scaled_value,
1061- int32* lower_bound, int32* upper_bound) {
1062- if (half_pixel_centers) {
1063- *scaled_value = (value + 0 .5f ) * scale - 0 .5f ;
1064- } else {
1065- *scaled_value = value * scale;
1066- }
1067- float scaled_value_floor = std::floor (*scaled_value);
1068- *lower_bound =
1069- std::max (static_cast <int32>(scaled_value_floor), static_cast <int32>(0 ));
1070- *upper_bound =
1071- std::min (static_cast <int32>(std::ceil (*scaled_value)), input_size - 1 );
1072- }
1073-
1074- template <typename T>
1075- inline void ResizeBilinear (const tflite::ResizeBilinearParams& op_params,
1076- const RuntimeShape& unextended_input_shape,
1077- const T* input_data,
1078- const RuntimeShape& unextended_output_size_shape,
1079- const int32* output_size_data,
1080- const RuntimeShape& unextended_output_shape,
1081- T* output_data) {
1082- // If half_pixel_centers is True, align_corners must be False.
1083- TFLITE_DCHECK (!op_params.half_pixel_centers || !op_params.align_corners );
1084- TFLITE_DCHECK_LE (unextended_input_shape.DimensionsCount (), 4 );
1085- TFLITE_DCHECK_LE (unextended_output_size_shape.DimensionsCount (), 4 );
1086- TFLITE_DCHECK_LE (unextended_output_shape.DimensionsCount (), 4 );
1087- const RuntimeShape input_shape =
1088- RuntimeShape::ExtendedShape (4 , unextended_input_shape);
1089- const RuntimeShape output_size_shape =
1090- RuntimeShape::ExtendedShape (4 , unextended_output_size_shape);
1091- const RuntimeShape output_shape =
1092- RuntimeShape::ExtendedShape (4 , unextended_output_shape);
1093-
1094- int32 batches = MatchingDim (input_shape, 0 , output_shape, 0 );
1095- int32 input_height = input_shape.Dims (1 );
1096- int32 input_width = input_shape.Dims (2 );
1097- int32 depth = MatchingDim (input_shape, 3 , output_shape, 3 );
1098-
1099- TFLITE_DCHECK_EQ (output_size_shape.Dims (0 ), 1 );
1100- TFLITE_DCHECK_EQ (output_size_shape.Dims (1 ), 1 );
1101- TFLITE_DCHECK_EQ (output_size_shape.Dims (2 ), 1 );
1102- TFLITE_DCHECK_EQ (output_size_shape.Dims (3 ), 2 );
1103- int32 output_height = output_size_data[Offset (output_size_shape, 0 , 0 , 0 , 0 )];
1104- int32 output_width = output_size_data[Offset (output_size_shape, 0 , 0 , 0 , 1 )];
1105-
1106- float height_scale = static_cast <float >(input_height) / output_height;
1107- float width_scale = static_cast <float >(input_width) / output_width;
1108- if (op_params.align_corners && output_height > 1 ) {
1109- height_scale = static_cast <float >(input_height - 1 ) / (output_height - 1 );
1110- }
1111- if (op_params.align_corners && output_width > 1 ) {
1112- width_scale = static_cast <float >(input_width - 1 ) / (output_width - 1 );
1113- }
1114- const float rounding_offset = std::numeric_limits<T>::is_integer ? .5f : .0f ;
1115-
1116- for (int b = 0 ; b < batches; ++b) {
1117- for (int y = 0 ; y < output_height; ++y) {
1118- float input_y;
1119- int32 y0, y1;
1120- ComputeInterpolationValues (y, height_scale, op_params.half_pixel_centers ,
1121- input_height, &input_y, &y0, &y1);
1122- for (int x = 0 ; x < output_width; ++x) {
1123- float input_x;
1124- int32 x0, x1;
1125- ComputeInterpolationValues (x, width_scale, op_params.half_pixel_centers ,
1126- input_width, &input_x, &x0, &x1);
1127- for (int c = 0 ; c < depth; ++c) {
1128- T interpolation =
1129- static_cast <T>(input_data[Offset (input_shape, b, y0, x0, c)] *
1130- (1 - (input_y - y0)) * (1 - (input_x - x0)) +
1131- input_data[Offset (input_shape, b, y1, x0, c)] *
1132- (input_y - y0) * (1 - (input_x - x0)) +
1133- input_data[Offset (input_shape, b, y0, x1, c)] *
1134- (1 - (input_y - y0)) * (input_x - x0) +
1135- input_data[Offset (input_shape, b, y1, x1, c)] *
1136- (input_y - y0) * (input_x - x0) +
1137- rounding_offset);
1138- output_data[Offset (output_shape, b, y, x, c)] = interpolation;
1139- }
1140- }
1141- }
1142- }
1143- }
1144-
1145- inline void ComputeInterpolationValuesInteger (
1146- const int32 value, const int32 scale_10, const bool half_pixel_centers,
1147- int32 input_size, int32* scaled_value, int32* lower_bound,
1148- int32* upper_bound) {
1149- if (half_pixel_centers) {
1150- *scaled_value = value * scale_10 + scale_10 / 2 - (1 << 9 );
1151- } else {
1152- *scaled_value = value * scale_10;
1153- }
1154- *lower_bound = std::max (*scaled_value / (1 << 10 ), 0 );
1155- *upper_bound =
1156- std::min ((*scaled_value + (1 << 10 ) - 1 ) / (1 << 10 ), input_size - 1 );
1157- }
1158-
1159- // Same as above but doesn't use any floating-point for the resize
1160- template <typename T>
1161- inline void ResizeBilinearInteger (
1162- const tflite::ResizeBilinearParams& op_params,
1163- const RuntimeShape& unextended_input_shape, const T* input_data,
1164- const RuntimeShape& unextended_output_size_shape,
1165- const int32* output_size_data, const RuntimeShape& unextended_output_shape,
1166- T* output_data) {
1167- // If half_pixel_centers is True, align_corners must be False.
1168- TFLITE_DCHECK (!op_params.half_pixel_centers || !op_params.align_corners );
1169- TFLITE_DCHECK_LE (unextended_input_shape.DimensionsCount (), 4 );
1170- TFLITE_DCHECK_LE (unextended_output_size_shape.DimensionsCount (), 4 );
1171- TFLITE_DCHECK_LE (unextended_output_shape.DimensionsCount (), 4 );
1172- const RuntimeShape input_shape =
1173- RuntimeShape::ExtendedShape (4 , unextended_input_shape);
1174- const RuntimeShape output_size_shape =
1175- RuntimeShape::ExtendedShape (4 , unextended_output_size_shape);
1176- const RuntimeShape output_shape =
1177- RuntimeShape::ExtendedShape (4 , unextended_output_shape);
1178-
1179- const int32 batches = MatchingDim (input_shape, 0 , output_shape, 0 );
1180- const int32 input_height = input_shape.Dims (1 );
1181- const int32 input_width = input_shape.Dims (2 );
1182- const int32 depth = MatchingDim (input_shape, 3 , output_shape, 3 );
1183-
1184- TFLITE_DCHECK_EQ (output_size_shape.Dims (0 ), 1 );
1185- TFLITE_DCHECK_EQ (output_size_shape.Dims (1 ), 1 );
1186- TFLITE_DCHECK_EQ (output_size_shape.Dims (2 ), 1 );
1187- TFLITE_DCHECK_EQ (output_size_shape.Dims (3 ), 2 );
1188- const int32 output_height =
1189- output_size_data[Offset (output_size_shape, 0 , 0 , 0 , 0 )];
1190- const int32 output_width =
1191- output_size_data[Offset (output_size_shape, 0 , 0 , 0 , 1 )];
1192-
1193- int32 height_scale_10 =
1194- ((1 << 10 ) * input_height + output_height / 2 ) / output_height;
1195- int32 width_scale_10 =
1196- ((1 << 10 ) * input_width + output_width / 2 ) / output_width;
1197- if (op_params.align_corners && output_height > 1 ) {
1198- height_scale_10 =
1199- ((1 << 10 ) * (input_height - 1 ) + (output_height - 1 ) / 2 ) /
1200- (output_height - 1 );
1201- }
1202- if (op_params.align_corners && output_width > 1 ) {
1203- width_scale_10 = ((1 << 10 ) * (input_width - 1 ) + (output_width - 1 ) / 2 ) /
1204- (output_width - 1 );
1205- }
1206-
1207- for (int b = 0 ; b < batches; ++b) {
1208- for (int y = 0 ; y < output_height; ++y) {
1209- int32 input_y, y0, y1;
1210- ComputeInterpolationValuesInteger (y, height_scale_10,
1211- op_params.half_pixel_centers ,
1212- input_height, &input_y, &y0, &y1);
1213- for (int x = 0 ; x < output_width; ++x) {
1214- int32 input_x, x0, x1;
1215- ComputeInterpolationValuesInteger (x, width_scale_10,
1216- op_params.half_pixel_centers ,
1217- input_width, &input_x, &x0, &x1);
1218- for (int c = 0 ; c < depth; ++c) {
1219- const int64_t output_20_ll =
1220- static_cast <int64_t >(
1221- input_data[Offset (input_shape, b, y0, x0, c)]) *
1222- ((1 << 10 ) - (input_y - (1 << 10 ) * y0)) *
1223- ((1 << 10 ) - (input_x - (1 << 10 ) * x0));
1224- const int64_t output_20_lu =
1225- static_cast <int64_t >(
1226- input_data[Offset (input_shape, b, y1, x0, c)]) *
1227- (input_y - (1 << 10 ) * y0) *
1228- ((1 << 10 ) - (input_x - (1 << 10 ) * x0));
1229- const int64_t output_20_rl =
1230- static_cast <int64_t >(
1231- input_data[Offset (input_shape, b, y0, x1, c)]) *
1232- ((1 << 10 ) - (input_y - (1 << 10 ) * y0)) *
1233- (input_x - (1 << 10 ) * x0);
1234- const int64_t output_20_ru =
1235- static_cast <int64_t >(
1236- input_data[Offset (input_shape, b, y1, x1, c)]) *
1237- (input_y - (1 << 10 ) * y0) * (input_x - (1 << 10 ) * x0);
1238- const int64_t output_20 =
1239- output_20_ll + output_20_lu + output_20_rl + output_20_ru;
1240- const int64_t round = (output_20 > 0 ) ? (1 << 19 ) : -(1 << 19 );
1241- const T interpolation =
1242- static_cast <T>((output_20 + round) / (1 << 20 ));
1243- output_data[Offset (output_shape, b, y, x, c)] = interpolation;
1244- }
1245- }
1246- }
1247- }
1248- }
1249-
12501059template <typename T>
12511060inline void Slice (const tflite::SliceParams& op_params,
12521061 const RuntimeShape& input_shape,
0 commit comments