teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

メモリの使用量を追記

2019/10/12 08:31

投稿

salmon_0511
salmon_0511

スコア16

title CHANGED
File without changes
body CHANGED
@@ -139,4 +139,71 @@
139
139
 
140
140
  [Qiita--KerasでGPUメモリの使用量を抑える方法](https://qiita.com/namakemono/items/12ad8a9f6d0561929056)
141
141
 
142
- を参考にkerasのメモリを制限しようと試みたものの、エラーは消えませんでした。
142
+ を参考にkerasのメモリを制限しようと試みたものの、エラーは消えませんでした。
143
+
144
+ ###追記
145
+
146
+ メモリの使用量
147
+ ```
148
+ Line # Mem usage Increment Line Contents
149
+ ================================================
150
+ 51 332.6 MiB 332.6 MiB @app.route('/')
151
+ 52 @profile
152
+ 53 def upload_file():
153
+ 54 332.8 MiB 0.2 MiB return render_template('test.html')
154
+
155
+
156
+ 127.0.0.1 - - [12/Oct/2019 17:15:04] "GET / HTTP/1.1" 200 -
157
+ I1012 17:15:04.182286 123145472167936 _internal.py:122] 127.0.0.1 - - [12/Oct/2019 17:15:04] "GET / HTTP/1.1" 200 -
158
+ Filename: /Users/yuya/PycharmProjects/tongue/predict_web.py
159
+
160
+ Line # Mem usage Increment Line Contents
161
+ ================================================
162
+ 58 332.8 MiB 332.8 MiB @app.route('/predict',methods=['GET', 'POST'])
163
+ 59 @profile
164
+ 60 def predict_file():
165
+ 61 global graph
166
+ 62 332.8 MiB 0.0 MiB with graph.as_default():
167
+ 63 332.8 MiB 0.0 MiB if request.method == 'POST':
168
+ 64 332.8 MiB 0.0 MiB if 'file' not in request.files:
169
+ 65 flash('ファイルがありません')
170
+ 66 return redirect(request.url)
171
+ 67 332.8 MiB 0.0 MiB file = request.files['file'] #ここがよくワカンねぇな
172
+ 68 # if user does not selectfile, browser also
173
+ 69 #submit an empty part without filename
174
+ 70 332.8 MiB 0.0 MiB if file.filename == '':
175
+ 71 flash('ファイルがありません')
176
+ 72 return redirect(request.url)
177
+ 73 332.8 MiB 0.0 MiB if file and allowed_file(file.filename):
178
+ 74 332.8 MiB 0.0 MiB filename = secure_filename(file.filename)
179
+ 75 332.8 MiB 0.0 MiB file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
180
+ 76 332.8 MiB 0.0 MiB filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
181
+ 77
182
+ 78 333.2 MiB 0.4 MiB image = Image.open(filepath)
183
+ 79 337.9 MiB 4.7 MiB image = image.convert('RGB')
184
+ 80 337.9 MiB 0.0 MiB image = image.resize((image_size, image_size))
185
+ 81 337.9 MiB 0.0 MiB data = np.asarray(image)
186
+ 82 337.9 MiB 0.0 MiB X = []
187
+ 83 337.9 MiB 0.0 MiB X.append(data)
188
+ 84 337.9 MiB 0.0 MiB X = np.array(X)
189
+ 85
190
+ 86 #model = load_model('./tongue_cnn_aug.h5')
191
+ 87
192
+ 88 341.7 MiB 3.8 MiB result = model.predict([X])[0]
193
+ 89 341.7 MiB 0.0 MiB predicted = result.argmax()
194
+ 90 341.7 MiB 0.0 MiB percentage = int(result[predicted] * 100)
195
+ 91 #K.clear_session()
196
+ 92 341.7 MiB 0.0 MiB result_title = ""
197
+ 93 341.7 MiB 0.0 MiB result_content=""
198
+ 94 341.7 MiB 0.0 MiB result_solution=""
199
+ 95
200
+ 96
201
+ 97 # return render_template('predict.html', result = classes[predicted], result_content = classes_content[predicted], result_solution = classes_solution[predicted], result_title="解析結果")
202
+ 98 341.8 MiB 0.1 MiB return render_template('predict.html', result = classes[predicted], result_content = classes_content[predicted], result_solution = classes_solution[predicted], result_title="解析結果",img_name=file.filename)
203
+ ```
204
+
205
+ 学習済みモデルがどれぐらいメモリを使用しているのかの確認方法が調べても分かりませんでしたので、memory_profilerを使って各行のメモリ使用量を確認してみました。
206
+
207
+ load_moduleした学習モデル「tongue_cnn_aug.h5」のメモリ使用料の確認方法も教えていただけたらと思います。
208
+
209
+ 訂正依頼でgpuについての言及がありましたが、gpuと当プログラムの関係はなかったものの、もしかしたらメモリが減るのでは?と思い試してみました。