How to profile using a Python script processor?

Ideally, I want to record the use of a Python script processor that runs a Keras deep neural network . I am looking for the equivalent of CPU memory_profiler that provides process memory consumption.

I examined the use of psutil (suggested in this answer ) which indicates that my script may contain some option

p = psutil.Process(current_pid)
p.cpu_percent()

but the problem is an important function call that I really want to capture, this will be the stage of model output

model.predict(x_test)

and if I run psutil before / after this step, the recorded CPU usage will not be a true reflection of the CPU usage in the process.

So, I was thinking if I can use something like top / htop to register script processor usage in some file, capturing the fluctuating CPU usage while the process is running and then calculating the average (or something like that) after the fact. However, the problem I see with this is that I do not need to know the PID in order to use the top one, since I can use top to monitor the script before it is executed (and even the PID was not assigned)?

I see this high level cProfile answer that gives the runtime of functions in a script. Although this is not quite what I want, I notice that it returns the total number of processor seconds, which, at least, would allow me to compare CPU usage in this regard.

+4
1

model.predict(x_test) . ,

import time
import multiprocessing as mp
import psutil
import numpy as np
from keras.models import load_model

def run_predict():
    model = load_model('1.h5')
    x_test = np.random.rand(10000, 1000)
    time.sleep(1)

    for _ in range(3):
        model.predict(x_test)
        time.sleep(0.5)

def monitor(target):
    worker_process = mp.Process(target=target)
    worker_process.start()
    p = psutil.Process(worker_process.pid)

    # log cpu usage of `worker_process` every 10 ms
    cpu_percents = []
    while worker_process.is_alive():
        cpu_percents.append(p.cpu_percent())
        time.sleep(0.01)

    worker_process.join()
    return cpu_percents

cpu_percents = monitor(target=run_predict)

cpu_percents script :

+1

Source: https://habr.com/ru/post/1694692/


All Articles