Reputation: 3
I don`t speak english well. but I want to solve this error. why heroku memory continually increase?(when i use my web app)
heroku error code is H14 like this :
Process running mem=541M(105.8%)
Error R14 (Memory quota exceeded)
========== after app running ===============
Process running mem=831M(162.4%)
Error R14 (Memory quota exceeded)
========== after app running ===============
Process running mem=856M(167.3%)
Error R14 (Memory quota exceeded)
I want to memory restore(?) (like 541M > 831M > 541M) but, memory always increase ,not decrease ,so I finally run heroku restart.
I don't want to restart, I want to decrease memory. What should I do? sorry, i`m very not good at english. thanks.
this is my python code.
from flask import Flask, render_template, request
import cv2
import numpy
import gc
import face_detection
import predict
from mtcnn.mtcnn import MTCNN
from keras.models import load_model
app = Flask(__name__, static_url_path="/static")
print("시작 ")
detector = MTCNN()
model = load_model('./static/keras_model/1layer_128_1_best(1)-SGD.h5')
print("로딩됨")
@app.route('/')
def render_file():
return render_template('upload.html')
@app.route('/file_uploaded',methods = ['GET', 'POST'])
def upload_file():
if request.method == 'POST': # POST 방식으로 전달된 경우
f = request.files['upload_image'].read()
# # 파일 객체 혹은 파일 스트림을 가져오고, html 파일에서 넘겨지는 값의 이름을 file1으로 했기 때문에 file1임.
# 업로드된 파일을 특정 폴더에저장하고,
# convert string data to numpy array
npimg = numpy.fromstring(f, dtype=numpy.uint8)
# convert numpy array to image
img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
if img.shape[0] > 500 or img.shape[1] > 500:
if img.shape[0] > img.shape[1]:
bigger = img.shape[0]
else :
bigger = img.shape[1]
scale_percent = (bigger - 500) *100 / bigger
width = int(img.shape[1] * (100-scale_percent) / 100)
height = int(img.shape[0] * (100- scale_percent) / 100)
dim = (width, height)
img = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
print(img.shape)
# 이거때문에 face_extract 때문에 올라감
face_extract = face_detection.input_image(detector, img)
print("얼굴추출 완료")
if len(face_extract) == 0:
print("얼굴인식 못했음")
return render_template('fail_back.html')
else:
result, k = predict.prediction(face_extract, model)
iu_percent = round(float(k[0][0] * 100), 3)
suzy_percent = round(float(k[0][1]) * 100, 3)
# return send_file(file_object, mimetype='image/jpeg')
if iu_percent > suzy_percent:
return render_template('result.html', image_file="image/result_iu.jpg", not_similler="수지",
not_similler_percent=suzy_percent, similler="아이유", similler_percent=iu_percent)
else:
return render_template('result.html', image_file="image/result_suzy.jpg", not_similler="아이유",
not_similler_percent=iu_percent, similler="수지", similler_percent=suzy_percent)
else:
return render_template('fail_back.html')
if __name__ == '__main__':
# debug를 True로 세팅하면, 해당 서버 세팅 후에 코드가 바뀌어도 문제없이 실행됨.
app.run(threaded=True)
I post my code. my code pick one person. It tells which one of the two looks more like.
my app site : simstest.herokuapp.com
but, When you use a site, the Heroku memory increases and the site restarts.
oh, I find memory increaseing method.
import gc
def face_eye_trace(data,result_list):
for result in result_list :
x = int(result['box'][0])
y = int(result['box'][1])
if x < 0:
x = 0
if y < 0:
y = 0
width = int(result['box'][2])
height = int(result['box'][3])
img3 = data[y:y + height, x:x + width]
return img3
def input_image(input_detector,pixels_image):
detector = input_detector
pixels = pixels_image
try:
# 이것때문에 memory 증가===============
faces = detector.detect_faces(pixels)
print(len(faces))
if len(faces) == 0:
return []
elif len(faces) == 1 :
face_detection = face_eye_trace(pixels,faces)
else :
print("다수의 얼굴이 인식되어 종료했습니다.")
return []
except:
print("얼굴 인식을 하지 못하였습니다.")
return []
print("detection:" ,gc.collect())
return face_detection
this code method ' face_detection = face_eye_trace(pixels,faces) ' caused memory increase.
why this method increase memory? and Why don't this automatically return the memory after this method?
Upvotes: 0
Views: 386
Reputation: 59
Garbage Collector interface module (gc
) is exactly what you could use here to further debug this problem.
To debug a leaking program call:
gc.set_debug(gc.DEBUG_LEAK)
This will output debugging information about garbage collection into your console.
For further debugging, gc
module provides more facilities, including ability to list out all objects tracked by the garbage collector, disable/enable collector or manually trigger garbage collection.
If you don't have a leak and want to just lower memory usage, try using gc.set_threshold
to trigger garbage collection at a lower memory usage point.
Upvotes: 1