1

尝试使用 yolov4 构建 webapp,将 yolov4 权重转换为 tensorflow 权重。使用tensorflow==2.3.0rc0 使用 2 个文件helper2.pyapp.py

请解释我需要在此代码中进行哪些更改才能正确运行它

helper2.py文件

import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
if len(physical_devices) > 0:
    tf.config.experimental.set_memory_growth(physical_devices[0], True)
from absl import app, flags, logging
from absl.flags import FLAGS
# import core.utils as utils
from core.utils import *
from core.yolov4 import filter_boxes
from tensorflow.python.saved_model import tag_constants
from PIL import Image
import cv2
import numpy as np
from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession

flags.DEFINE_string('framework', 'tf', '(tf, tflite, trt')
flags.DEFINE_string('weights', './checkpoints/yolov4-416',
                    'path to weights file')
flags.DEFINE_integer('size', 416, 'resize images to')
flags.DEFINE_boolean('tiny', False, 'yolo or yolo-tiny')
flags.DEFINE_string('model', 'yolov4', 'yolov3 or yolov4')
flags.DEFINE_string('image', './data/kite.jpg', 'path to input image')
flags.DEFINE_string('output', 'result.png', 'path to output image')
flags.DEFINE_float('iou', 0.45, 'iou threshold')
flags.DEFINE_float('score', 0.25, 'score threshold')

def get_images(image_path,image_name):
    config = ConfigProto()
    config.gpu_options.allow_growth = True
    session = InteractiveSession(config=config,)
    STRIDES, ANCHORS, NUM_CLASS, XYSCALE = load_config(FLAGS)
    input_size = FLAGS.size
    image_path = FLAGS.image

    original_image = cv2.imread(image_path)
    original_image = cv2.cvtColor(original_image, cv2.COLOR_BGR2RGB)

    # image_data = utils.image_preprocess(np.copy(original_image), [input_size, input_size])
    image_data = cv2.resize(original_image, (input_size, input_size))
    image_data = image_data / 255.
    # image_data = image_data[np.newaxis, ...].astype(np.float32)

    images_data = []
    for i in range(1):
        images_data.append(image_data)
    images_data = np.asarray(images_data).astype(np.float32)

    if FLAGS.framework == 'tflite':
        interpreter = tf.lite.Interpreter(model_path=FLAGS.weights)
        interpreter.allocate_tensors()
        input_details = interpreter.get_input_details()
        output_details = interpreter.get_output_details()
        print(input_details)
        print(output_details)
        interpreter.set_tensor(input_details[0]['index'], images_data)
        interpreter.invoke()
        pred = [interpreter.get_tensor(output_details[i]['index']) for i in range(len(output_details))]
        if FLAGS.model == 'yolov3' and FLAGS.tiny == True:
            boxes, pred_conf = filter_boxes(pred[1], pred[0], score_threshold=0.25, input_shape=tf.constant([input_size, input_size]))
        else:
            boxes, pred_conf = filter_boxes(pred[0], pred[1], score_threshold=0.25, input_shape=tf.constant([input_size, input_size]))
    else:
        saved_model_loaded = tf.saved_model.load(FLAGS.weights, tags=[tag_constants.SERVING])
        infer = saved_model_loaded.signatures['serving_default']
        batch_data = tf.constant(images_data)
        pred_bbox = infer(batch_data)
        for key, value in pred_bbox.items():
            boxes = value[:, :, 0:4]
            pred_conf = value[:, :, 4:]

    boxes, scores, classes, valid_detections = tf.image.combined_non_max_suppression(
        boxes=tf.reshape(boxes, (tf.shape(boxes)[0], -1, 1, 4)),
        scores=tf.reshape(
            pred_conf, (tf.shape(pred_conf)[0], -1, tf.shape(pred_conf)[-1])),
        max_output_size_per_class=50,
        max_total_size=50,
        iou_threshold=FLAGS.iou,
        score_threshold=FLAGS.score
    )
    pred_bbox = [boxes.numpy(), scores.numpy(), classes.numpy(), valid_detections.numpy()]
    image = draw_bbox(original_image, pred_bbox)
    # image = utils.draw_bbox(image_data*255, pred_bbox)
    image = Image.fromarray(image.astype(np.uint8))
    image.show()
    image = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
    cv2.imwrite(FLAGS.output, image)

# if __name__ == '__main__':
#     try:
#         app.run(main)
#     except SystemExit:
#         pass

应用程序.py文件

from flask import Flask, render_template, Response,  request, session, redirect, url_for, send_from_directory,flash,jsonify
from werkzeug.utils import secure_filename

from PIL import Image
import os
import sys
import cv2
from helper2 import *
#from app_helper import *
from flask_cors import CORS,cross_origin

app=Flask(__name__)

upload_folder='./data/images'

app.config['upload_folder'] = upload_folder


@app.route("/")
def index():
    return render_template("index.html")


@app.route("/about")
def about():
    return render_template("about.html")


@app.route('/uploader', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        # create a secure filename
        filename = secure_filename(f.filename)
        print(filename)
        # save file to data/images    #/static/uploads
        filepath = os.path.join(app.config['upload_folder'], filename)
        print(filepath)
        f.save(filepath)
        get_images(filepath,filename)

        return render_template("uploaded.html", display_detection=filepath, fname=filepath)


if __name__ == '__main__':
    app.run(port=7000, debug=True)

谁能解释我需要做哪些改变

4

3 回答 3

4

如果您想使用absl.flags,请确保您的 Python 文件与以下格式相同。

from absl import app, flags

flags.DEFINE_string("name", None, "Your name.")

def main(argv):
    pass

app.run(main)

所以,你可以像python yourfile.py -name=something.

如果你只是想运行这个应用程序,将flags.DEFINEs 作为常量嵌入到你的代码中是一种更好的方法。

于 2021-04-15T05:08:39.990 回答
0

对于这种特殊情况,您必须编辑文件./core/utils.py,第 85 行,

def load_config(FLAGS):
...

并替换所有 FLAGS 提及,只留下cfg.YOLO常量

于 2021-11-09T20:27:18.987 回答
0

这段代码如何更改为常量?

步幅,锚点,NUM_CLASS,XYSCALE = load_config(标志)

于 2021-09-23T08:22:51.523 回答