前提・実現したいこと
pythonとVSCodeを用いて、自分で学習させたモデルを使用したWebアプリを作ろうとしています。
作成したwebをHerokuでデプロイしたところエラーが出てしまい、正しく動作しませんでした。エラー文からHeroku-20 Stack、runtime.txtなどに問題がありそうでしたが、使用しているpython3.9.1はHeroku-20には対応しており、runtime.txtも問題ないように見えます。他の質問であった、requirements.txtも作成してあります。
他のサイトを見てもあまり有力な情報が得られず、ここで質問させていただきました。
#VSCodeから出力されたメッセージ
Enumerating objects: 27, done. Counting objects: 100% (27/27), done. Delta compression using up to 8 threads Compressing objects: 100% (25/25), done. Writing objects: 14% (4/27), 984.00 KiB | 6Writing objects: 14% (4/27), 1.81 MiB | 908Writing objects: 14% (4/27), 3.89 MiB | 1.2Writing objects: 14% (4/27), 6.74 MiB | 1.6Writing objects: 14% (4/27), 9.23 MiB | 1.8Writing objects: 14% (4/27), 11.49 MiB | 2.Writing objects: 14% (4/27), 13.70 MiB | 2.Writing objects: 14% (4/27), 15.63 MiB | 2.Writing objects: 14% (4/27), 17.77 MiB | 2.Writing objects: 14% (4/27), 19.76 MiB | 2.Writing objects: 14% (4/27), 22.04 MiB | 2.Writing objects: 14% (4/27), 24.27 MiB | 2.Writing objects: 14% (4/27), 26.48 MiB | 2.Writing objects: 14% (4/27), 28.65 MiB | 2.Writing objects: 14% (4/27), 30.80 MiB | 2.Writing objects: 14% (4/27), 33.09 MiB | 2.Writing objects: 14% (4/27), 35.38 MiB | 2.Writing objects: 14% (4/27), 37.63 MiB | 2.Writing objects: 18% (5/27), 38.68 MiB | 2.Writing objects: 22% (6/27), 38.68 MiB | 2.Writing objects: 25% (7/27), 38.68 MiB | 2.Writing objects: 29% (8/27), 38.68 MiB | 2.Writing objects: 33% (9/27), 38.68 MiB | 2.Writing objects: 37% (10/27), 40.96 MiB | 2Writing objects: 40% (11/27), 40.96 MiB | 2Writing objects: 44% (12/27), 40.96 MiB | 2Writing objects: 48% (13/27), 42.06 MiB | 2Writing objects: 51% (14/27), 42.06 MiB | 2Writing objects: 55% (15/27), 42.06 MiB | 2Writing objects: 59% (16/27), 42.06 MiB | 2Writing objects: 62% (17/27), 42.06 MiB | 2Writing objects: 66% (18/27), 42.06 MiB | 2Writing objects: 70% (19/27), 42.06 MiB | 2Writing objects: 74% (20/27), 42.06 MiB | 2Writing objects: 77% (21/27), 42.06 MiB | 2Writing objects: 81% (22/27), 42.06 MiB | 2Writing objects: 85% (23/27), 42.06 MiB | 2Writing objects: 88% (24/27), 42.06 MiB | 2Writing objects: 92% (25/27), 42.06 MiB | 2Writing objects: 96% (26/27), 42.06 MiB | 2Writing objects: 100% (27/27), 42.06 MiB | 2Writing objects: 100% (27/27), 42.96 MiB | 1.87 MiB/s, done. Total 27 (delta 0), reused 0 (delta 0), pack-reused 0 remote: Compressing source files... done. remote: Building source: remote: remote: -----> Building on the Heroku-20 stack remote: -----> Using buildpack: heroku/python remote: -----> Python app detected remote: -----> Using Python version specified in runtime.txt remote: Traceback (most recent call last): remote: File "/tmp/codon/tmp/buildpacks/0f40890b54a617ec2334fac0439a123c6a0c1136/vendor/runtime-fixer", line 8, in <module> remote: r = f.read().strip() remote: File "/usr/lib/python3.8/codecs.py", line 322, in decode remote: (result, consumed) = self._buffer_decode(data, self.errors, final) remote: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte remote: /tmp/codon/tmp/buildpacks/0f40890b54a617ec2334fac0439a123c6a0c1136/bin/steps/python: line 5: warning: command substitution: ignored null byte in input remote: ! Requested runtime (ÿþpython-3remote: ) is not available for this stack (heroku-20). remote: ! Aborting. More info: https://devcenter.heroku.com/articles/python-support remote: ! Push rejected, failed to compile Python app. remote: remote: ! Push failed remote: ! remote: ! ## Warning - The same version of this code has already been built: df20551dc9446d4d40224d43bbd9f05f98b4be1b remote: ! remote: ! We have detected that you have triggered a build from source code with version df20551dc9446d4d40224d43bbd9f05f98b4be1b remote: ! at least twice. One common cause of this behavior is attempting to deploy code from a different branch. remote: ! remote: ! If you are developing on a branch and deploying via git you must run: remote: ! remote: ! git push heroku <branchname>:main remote: ! remote: ! This article goes into details on the behavior: remote: ! https://devcenter.heroku.com/articles/duplicate-build-version remote: remote: Verifying deploy... remote: remote: ! Push rejected to damp-bayou-u-96265. remote: To https://git.heroku.com/damp-bayou-96265.g.git ! [remote rejected] master -> master (pre-r-receive hook declined) error: failed to push some refs to 'https:////git.heroku.com/damp-bayou-96265.git'
### requirements.txt
absl-py==0.13.0 asgiref==3.4.1 astunparse==1.6.3 cachetools==4.2.2 certifi==2021.5.30 charset-normalizer==2.0.3 click==8.0.1 colorama==0.4.4 cycler==0.10.0 dj-database-url==0.5.0 dj-static==0.0.6 Django==3.2.5 django-toolbelt==0.0.1 Flask==2.0.1 flatbuffers==1.12 gast==0.4.0 google-auth==1.33.1 google-auth-oauthlib==0.4.4 google-pasta==0.2.0 grpcio==1.34.1 gunicorn==20.1.0 h5py==3.1.0 idna==3.2 itsdangerous==2.0.1 Jinja2==3.0.1 joblib==1.0.1 keras-nightly==2.5.0.dev2021032900 Keras-Preprocessing==1.1.2 kiwisolver==1.3.1 Markdown==3.3.4 MarkupSafe==2.0.1 matplotlib==3.4.2 numpy==1.19.5 oauthlib==3.1.1 opencv-python==4.5.3.56 opt-einsum==3.3.0 pandas==1.3.0 Pillow==8.3.1 protobuf==3.17.3 psycopg2==2.9.1 pyasn1==0.4.8 pyasn1-modules==0.2.8 pyparsing==2.4.7 python-dateutil==2.8.2 pytz==2021.1 requests==2.26.0 requests-oauthlib==1.3.0 rsa==4.7.2 scikit-learn==0.24.2 scipy==1.7.0 six==1.15.0 sklearn==0.0 sqlparse==0.4.1 static3==0.7.0 tensorboard==2.5.0 tensorboard-data-server==0.6.1 tensorboard-plugin-wit==1.8.0 tensorflow==2.5.0 tensorflow-estimator==2.5.0 termcolor==1.1.0 threadpoolctl==2.2.0 torch==1.7.1+cu110 torchaudio==0.7.2 torchvision==0.8.2+cu110 typing-extensions==3.7.4.3 urllib3==1.26.6 Werkzeug==2.0.1 wrapt==1.12.1 WTForms==2.3.3
### python --versionとruntime.txt
VSCode
1~>python --version 2Python 3.9.1
runtime
1Python 3.9.1 2
### App内のpredict.py
from flask import Flask, render_template, request, redirect, url_for import numpy as np from PIL import Image from datetime import datetime from torchvision import datasets, models, transforms from wtforms import Form, FloatField, SubmitField, validators, ValidationError import joblib import cv2 import torch import torch.nn as nn import tensorflow as tf app = Flask(__name__) @app.route('/', methods = ['GET', 'POST']) def upload_file(): if request.method == 'GET': return render_template('index.html') if request.method == 'POST': # アプロードされたファイルを保存する """ f = request.files['file'] filepath = "./static/" + datetime.now().strftime("%Y%m%d%H%M%S") + ".png" f.save(filepath) """ filepath = "./static/" + datetime.now().strftime("%Y%m%d%H%M%S") + ".png" img_dir = "static/" stream = request.files['img'].stream img_array = np.asarray(bytearray(stream.read()), dtype=np.uint8) img = cv2.imdecode(img_array, 1) #モデルの読み込み model = models.resnet18(pretrained=False).to(torch.device('cpu')) num_ftrs = model.fc.in_features model.fc = nn.Linear(num_ftrs, 8) #出力クラス8 model.load_state_dict(torch.load('./ResNet.pkl')) #入力データ作成 cv2.imwrite(filepath, img) img = cv2.resize(img , (224, 224)) img = img.transpose(2,1,0) params = torch.Tensor(img) x = torch.tensor([img]) x = torch.reshape(params, (1, 3, 224, 224)) predict = model(x) return render_template('index.html', filepath = filepath , predict = torch.argmax(predict).numpy() ) #return render_template('result.html', filepath = filepath, irisName=torch.argmax(predict).numpy()) if __name__ == '__main__': app.run(host="0.0.0.0", port=int("5000"),debug=True)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。