Различные выходы модели на Python и FlutterAndroid

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Различные выходы модели на Python и Flutter

Сообщение Anonymous »

У меня проблемы, потому что модель Tensorflow Lite производит очень разные выходы в Python и на Android, даже если я предоставляю один и тот же ввод в обоих. На стороне трепета я использую пакет TFLITE_FLUTTER с TensorFlow версией 2.11. Я также проверил с той же версией (2.11) в Python, но разница остается. Я прочитал много документов и сделал много исследований, но я до сих пор не смог решить проблему. address = 0x7d53888acff60, name: serving_default_input_1: 0, type: float32, форма: [1, 80, 160, 3], данные: 153600}]
i /flutter (7891): [tensor {_tensor: pointer: address = 0x738888. Тип: float32, форма: [1, 80, 160, 1], данные: 51200}]
python code: < /p>
class Lanes():
def __init__(self):
self.recent_fit = []
self.avg_fit = []

def load_tflite_model(model_path):
interpreter = tf.lite.Interpreter("enet_model.tflite")
print(tf.__version__)
interpreter.allocate_tensors()
print("Loaded TensorFlow Lite model successfully.")
print(interpreter.get_input_details())
print(interpreter.get_output_details())
return interpreter

def predict_tflite(interpreter, input_data):
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

print("Input data:", input_data)
#print("Input type:", type(input_data))
#print("Input shape:", input_data.shape)
#print("---------------------------------")

interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()

output_data = interpreter.get_tensor(output_details[0]['index'])
#print(output_data)

return output_data

def road_lines(image, interpreter, lanes):
h, w = image.shape[:2]
image = image.copy()
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Replace skimage.transform.resize with cv2.resize
small_img = cv2.resize(image, (160, 80), interpolation=cv2.INTER_LINEAR)
small_img = np.array(small_img, dtype=np.float32)
small_img = small_img[None, :, :, :]

#print("small image", small_img)
#print("Resized image for model input:", small_img.shape)
prediction = predict_tflite(interpreter, small_img)[0] * 255.0 # eğer normalize edilmişse çarp
lanes.recent_fit.append(prediction)
print("prediction",prediction)

lanes.avg_fit = np.mean(np.array(lanes.recent_fit), axis=0)
print("lanes recent fit:", lanes.recent_fit)
#print("lanes recent fit,shape:", type(lanes.recent_fit))

print("-------------------------------------------------")
# print("lanes avg fit:", lanes.avg_fit[75])
# print("lanes avg fit,shape:", type(lanes.avg_fit))
print("-------------------------------------------------")

lane_avg_uint8 = np.clip(lanes.avg_fit, 0, 255).astype(np.uint8)
print("lane uin8", lane_avg_uint8)
# print("lane uin8,shape", lane_avg_uint8.shape)
#print("lane uin8,type", type(lane_avg_uint8.shape[0]))
# print("lane uint8 y", lane_avg_uint8[75])
# print("type", type(lane_avg_uint8[75]))

left_boundary = []
right_boundary = []
threshold_boundary = 128

for y in range(lane_avg_uint8.shape[0]):
row = lane_avg_uint8[y]
indices = np.where(row > threshold_boundary)[0]
if indices.size > 0:
left_boundary.append((indices[0], y))
right_boundary.append((indices[-1], y))

print("left boundary", left_boundary)
print("right boundary", right_boundary)

scale_x = w / 160.0
scale_y = h / 80.0

left_boundary_scaled = [(int(x * scale_x), int(y * scale_y)) for (x, y) in left_boundary]
right_boundary_scaled = [(int(x * scale_x), int(y * scale_y)) for (x, y) in right_boundary]

print("left boundary scaled", left_boundary_scaled)
print("right boundary scaled", right_boundary_scaled)

if len(left_boundary_scaled) > 1:
cv2.polylines(image, [np.array(left_boundary_scaled, dtype=np.int32)],
isClosed=False, color=(0, 255, 0), thickness=5)

if len(right_boundary_scaled) > 1:
cv2.polylines(image, [np.array(right_boundary_scaled, dtype=np.int32)],
isClosed=False, color=(0, 255, 0), thickness=5)

left_bottom = left_boundary_scaled[-1] if left_boundary_scaled else None
right_bottom = right_boundary_scaled[-1] if right_boundary_scaled else None

print("Lane detection completed.")
return image
< /code>
код DART: < /p>
import 'package:tflite_flutter/tflite_flutter.dart';
import '../../Debug/logFile.dart';

class LaneDetectionModel with LogFile {
late Interpreter? _interpreter;

List output = List.generate(
1,
(index) => List.generate(
80,
(index) =>
List.generate(160, (index) => List.generate(1, (index) => 0.0))));
List leftLane = [];
List rightLane = [];
LaneDetectionModel() {
loadModel();
}

InterpreterOptions interpreterOptions() {
InterpreterOptions _interpreterOptions = InterpreterOptions();

return _interpreterOptions;
}

Future loadModel() async {
try {
_interpreter = await Interpreter.fromAsset(
'assets/models/enet_model.tflite',
);

print(_interpreter!.getInputTensors());
print(_interpreter!.getOutputTensors());
createModelLog();
} catch (e) {
errorModelLog(e);
}
}

Future model_predict(List input_tensor) async {
try {
List reshape = input_tensor.reshape([ 1,80, 160, 3]);
_interpreter?.run(reshape, output);
List outputTo8 = resultToLane();
Map den = extractBoundaries(
output8: outputTo8,
thresholdBoundary: 128,
w: 243,
h: 492,
);
print(den['left']);
print(den['right']);
} catch (e) {
modelPredictErrorLog(e);
}
}
...
}

< /code>
Входные данные модели одинаковы как в коде Python, так и в стороне Android.
Я добавил фотографию ниже, показывающую пример, где переменная Small_image используется в коде Python.
https://i.sstatic.net/tmyvesl " /> https://i.sstatic.net/tmyvesl Вывод:
python model Выход:


Подробнее здесь: https://stackoverflow.com/questions/795 ... nd-flutter
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»