[PYTHON] "Trash classification by image!" App creation diary day8 ~ heroku deployment ~

Introduction

"Trash classification by image!" I would like to finally deploy today on the 8th day of the application creation diary. This time, we will use heroku as PaaS (a service that provides a database and execution environment for running application software).


Article list

-"Trash classification by image!" App creation diary day1 ~ Data set creation ~ -"Trash classification by image!" App creation diary day2-Fine-tuning with VGG16- -"Trash classification by image!" App creation diary day3 ~ Web application with Django ~ -"Trash classification by image!" App creation diary day4 ~ Prepare the front end with Bootstrap ~ -"Trash classification by image!" App creation diary day5-Prepare front end with Bootstrap 2- -"Trash classification by image!" App creation diary day6 ~ Correction of directory structure ~ -"Trash classification by image!" App creation diary day7 ~ Sidebar slide menu ~

Introduction

In this deployment, I use Heroku CLI (command line interface), but since it's a big deal (because there are so many linux followers at my university), I built an environment where linux commands work and that I'd like to do it in an environment (I think you can download it immediately by looking at The Heroku CLI without such annoying things).

Add Ubuntu

Put ubuntu so that linux commands can be executed. First, install ubuntu from the microsoft store. Ubuntu Then install windows terminal. (You can write commands directly in ubuntu, but this one that can be used with the command prompt is more convenient) windows terminal

Now let's enable ubuntu on windows terminal. [How to add Ubuntu tabs to Windows Terminal in Windows 10](https://soundartifacts.com/en/how-to/280-how-to-add-ubuntu-tab-to-windows-terminal-in-windows -10.html) was used as a reference. First, open ubuntu and get the GUID with the following command.

$ uuidgen

Then open the settings from the down arrow next to the windows terminal tab and add the following to ist in profiles.

setting.json


            {
                "acrylicOpacity":0.75,
                "closeOnExit":true,
                "colorScheme":"Campbell",
                "commandline":"wsl.exe -d Ubuntu",
                "cursorColor":"#FFFFFF",
                "cursorShape":"bar",
                "fontFace":"Consolas",
                "fontSize":12,
                "guid":"{GUID obtained by the above procedure}",
                "historySize":9001,
                "icon":"C:/Dummy/image.png ",
                "name":"Ubuntu",
                "padding":"0, 0, 0, 0",
                "snapOnInput":true,
                "startingDirectory":"%USERPROFILE%",
                "useAcrylic":true
            }

You should now have ubuntu added.

brew command

I didn't have a brew command in my environment, so I'll start by installing it. Before that, let's update the package.

sudo apt update

Then install brew.

sudo apt install linuxbrew-wrapper

Originally it should be possible to put it in, but since an error occurred here, I introduced Homebrew by referring to the following article. I want to use brew on Windows too! Until WSL is installed, home directory is changed, and brew is available

Install #brew

Basically, you can follow the Documentation.

First, execute the following command.

sh -c "$(curl -fsSL https://raw.githubusercontent.com/Linuxbrew/install/master/install.sh)"

After this is complete, run the following command:

test -d ~/.linuxbrew && eval $(~/.linuxbrew/bin/brew shellenv)
test -d /home/linuxbrew/.linuxbrew && eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
test -r ~/.bash_profile && echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.bash_profile
echo "eval \$($(brew --prefix)/bin/brew shellenv)" >>~/.profile

Finally, install the packages that brew depends on. This time it's Ubuntu, so do the following:

sudo apt-get install build-essential curl file git

Now you can use brew.

Based on this article, I ran the following command.

$ brew doctor

Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: "config" scripts exist outside your system or Homebrew directories.
`./configure` scripts often look for *-config scripts to determine if
software packages are installed, and which additional flags to use when
compiling and linking.

Having additional scripts in your path can confuse software installed via
Homebrew if the config script overrides a system or Homebrew-provided
script of the same name. We found the following "config" scripts:
  /mnt/c/Strawberry/c/bin/gdlib-config
  /mnt/c/Strawberry/c/bin/libpng-config
  /mnt/c/Strawberry/c/bin/libpng16-config
  /mnt/c/Strawberry/c/bin/xml2-config
  /mnt/c/Strawberry/c/bin/xslt-config
  /mnt/c/Strawberry/perl/bin/pkg-config
  /mnt/c/Users/muto/Anaconda3/Library/bin/icu-config

Then, I got a warning that I could ignore, so I was able to install it according to the above article.

Deploy to heroku

Now we will finally connect to heroku. In ubuntu terminal on windows terminal do the following:

brew tap heroku/brew && brew install heroku
cd Documents/programing/django/garbage
echo web: gunicorn --pythonpath garbage_proj garbage_proj.wsgi --log-file -
echo python-3.7.4 > runtime.txt

The command on the third line didn't work if the project directory and the git admin directory were different, so I added the --pythonpath garbage_proj part. The structure was like this. garbage (git admin directory) └garbage_proj (project directory)

We will also change the settings for deployment. For the time being, I am tweaking the settings so that it can be deployed at a minimum, so security is insufficient for production operation.

# TEMPLATES>context_Added to handle images in processors
                'django.template.context_processors.media',

#Add settings for heroku
import django_heroku #add to
django_heroku.settings(locals()) #add to

#SQLite3 doesn't work on Heroku, so change it or leave it empty if you don't need it
DATABASES = {}

ALLOWED_HOSTS = ['.herokuapp.com']

Originally, DEBUG = False should also be set. However, since Server Error (500) has occurred, it is being executed as it is.

Also, install the python library required for deployment. In addition, since ubuntu terminal contains python for ubuntu system (?), It is necessary to execute it in a normal terminal.

pip install gunicorn
pip install django-heroku
pip install dj-database-url
pip freeze > requirements.txt

Since requirements.txt did not build a virtual environment, all the local libraries were written out, so I made only the necessary ones (reflection point). It looks like the following.

bootstrap4==0.1.0
Django==3.0.8
django-bootstrap4==2.2.0
django-heroku==0.3.1
django-mathfilters==1.0.0
numpy==1.18.2
pandas==1.0.3
Pillow==7.1.1
gunicorn==20.0.4
dj-database-url==0.5.0
matplotlib==3.2.1
tensorflow==2.0.0
Keras==2.4.0

Now let's connect to heroku.

heroku login
heroku create garbageeycjur(app name)
heroku config:set SECRET_KEY="(setting.secret key in py)
git push heroku master

heroku ps:scale web=1
(No database settings ok)
heroku open

References: How to deploy a Django app on Heroku

If it works, you should be able to go with the above code alone, but of course you will get an error, so fix it in various ways.

First of all, if you do not need collectstatic, which has a function to handle static files (for details, copy the static directory for each application to the specified directory), you will get a message asking you to set the environment variable DISABLE_COLLECTSTATIC. , Enter the following command as told.

heroku config:set DISABLE_COLLECTSTATIC=1

If you get any errors around here, take a look at the code below

heroku logs --tail

When you execute this code, a link like activity on heroku will appear, so you can see the detailed error contents by looking at it.

In addition, you can update after deploying once with the following command.

heroku login
git push heroku master
heroku ps:scale web=1
heroku open

I wrote that it seemed to work so far, but unfortunately I died with the following error.

Compiled slug size: 〇〇M is too large (max is 500M).

In short, it's eating too much capacity. The cause of this is mostly tensolflow, but when I downgrade tensolflow (and keras), the model doesn't work this time.

So, the model doesn't work, but I deployed it for the time being, so please pay particular attention to the sidebar on the smartphone screen.

URL: Classify garbage by image! Classify garbage by image! It won't end as it is, so I will redeploy it around python anywhere at a later date. Also, I would be grateful if anyone with the knowledge to solve the error by means other than charging and recreating the model.

References

-[How to add Ubuntu tab to Windows Terminal in Windows 10](https://soundartifacts.com/en/how-to/280-how-to-add-ubuntu-tab-to-windows-terminal-in- windows-10.html) -I want to use brew on Windows! Until WSL is installed, home directory is changed, and brew is available -How to deploy the Django app to Heroku

Recommended Posts

"Trash classification by image!" App creation diary day8 ~ heroku deployment ~
"Trash classification by image!" App creation diary day3 ~ Web application with Django ~
"Trash classification by image!" App creation diary day6 ~ Correction of directory structure ~
"Garbage classification by image!" App creation diary day1 ~ Data set creation ~
"Classify garbage by image!" App creation diary day5 ~ Prepare front end with Bootstrap 2 ~
"Classify garbage by image!" App creation diary day4 ~ Prepare the front end with Bootstrap ~
Deep learning learned by implementation 2 (image classification)