mmcv提速

需求

機(jī)械硬盤(pán)太慢,但有大的內(nèi)存128G,將數(shù)據(jù)集緩存到內(nèi)存里,用flask作server, requests作client

server

#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright ? 2020 2020-07-15 carryhjr
#
# Distributed under terms of the MIT license.

import base64
from flask import request
from flask import Flask
import os
 
app=Flask(__name__)


def multiprocess_run(process, obj_list, notebook=False):
    import sys
    if notebook is True:
        from tqdm import tqdm_notebook as tqdm
    else:
        from tqdm import tqdm
    from multiprocessing import Pool
    import multiprocessing
    import time
    cpus = multiprocessing.cpu_count()
    pool = Pool(processes=cpus)
    result_list = []
    tic = time.time()
    for result in tqdm(pool.imap(process, obj_list)):
        result_list.append(result)
        sys.stdout.flush()
    toc = time.time()
    print('time waste', toc - tic)
    return result_list
def process(img_path):
    with open(img_path, 'rb') as f:
        string = f.read()
    return (img_path, string)

def save_to_ram(root):
    import glob
    image_paths = glob.glob(root + '/*')[:]
    result_list = multiprocess_run(process, image_paths)
    v = {}
    for result in result_list:
        img_path, byte = result
        v[img_path] = byte
    return v

                            
root = '/home/yons/data/kaggle/global-wheat-detection/global-wheat-detection-coco-format/images'
v = save_to_ram(root)

 
@app.route("/data", methods=['GET'])
def get_frame():
    img_path = request.args.get("img_path")
    #  print(img_path)
    if img_path:
        return v[img_path]

 
if __name__ == "__main__":
    app.run()

client

import glob
img_paths = glob.glob(root + '/*')
import random
img_path = random.choice(img_paths)

import requests
url = "http://127.0.0.1:5000/data"
res = requests.get(url=url, params={'img_path': img_path})
plt.figure()
img = cv2.imdecode(np.frombuffer(res.content, np.uint8), 1)
plt.imshow(img[:,:,::-1])

plt.figure()
img = cv2.imread(img_path)
plt.imshow(img[:,:,::-1])
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容