Browse Source

Context manager `open(file) as f` fixes (#7289)

* Flask context manager `open()` fix

* Additional read context manager fixes
modifyDataloader
Glenn Jocher GitHub 2 years ago
parent
commit
5f97001ed4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 6 deletions
  1. +2
    -1
      data/VOC.yaml
  2. +2
    -1
      export.py
  3. +2
    -1
      models/common.py
  4. +9
    -3
      utils/flask_rest_api/example_request.py
  5. +2
    -0
      utils/flask_rest_api/restapi.py

+ 2
- 1
data/VOC.yaml View File

@@ -72,7 +72,8 @@ download: |
imgs_path.mkdir(exist_ok=True, parents=True)
lbs_path.mkdir(exist_ok=True, parents=True)

image_ids = open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt').read().strip().split()
with open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt') as f:
image_ids = f.read().strip().split()
for id in tqdm(image_ids, desc=f'{image_set}{year}'):
f = path / f'VOC{year}/JPEGImages/{id}.jpg' # old img path
lb_path = (lbs_path / f.name).with_suffix('.txt') # new label path

+ 2
- 1
export.py View File

@@ -407,7 +407,8 @@ def export_tfjs(keras_model, im, file, prefix=colorstr('TensorFlow.js:')):
f'--output_node_names="Identity,Identity_1,Identity_2,Identity_3" {f_pb} {f}'
subprocess.run(cmd, shell=True)

json = open(f_json).read()
with open(f_json) as j:
json = j.read()
with open(f_json, 'w') as j: # sort JSON Identity_* in ascending order
subst = re.sub(
r'{"outputs": {"Identity.?.?": {"name": "Identity.?.?"}, '

+ 2
- 1
models/common.py View File

@@ -378,7 +378,8 @@ class DetectMultiBackend(nn.Module):
return x.prune(tf.nest.map_structure(ge, inputs), tf.nest.map_structure(ge, outputs))

gd = tf.Graph().as_graph_def() # graph_def
gd.ParseFromString(open(w, 'rb').read())
with open(w, 'rb') as f:
gd.ParseFromString(f.read())
frozen_func = wrap_frozen_graph(gd, inputs="x:0", outputs="Identity:0")
elif tflite or edgetpu: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python
try: # https://coral.ai/docs/edgetpu/tflite-python/#update-existing-tf-lite-code-for-the-edge-tpu

+ 9
- 3
utils/flask_rest_api/example_request.py View File

@@ -1,12 +1,18 @@
"""Perform test request"""
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
"""
Perform test request
"""

import pprint

import requests

DETECTION_URL = "http://localhost:5000/v1/object-detection/yolov5s"
TEST_IMAGE = "zidane.jpg"
IMAGE = "zidane.jpg"

image_data = open(TEST_IMAGE, "rb").read()
# Read image
with open(IMAGE, "rb") as f:
image_data = f.read()

response = requests.post(DETECTION_URL, files={"image": image_data}).json()


+ 2
- 0
utils/flask_rest_api/restapi.py View File

@@ -1,6 +1,8 @@
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
"""
Run a Flask REST API exposing a YOLOv5s model
"""

import argparse
import io


Loading…
Cancel
Save