Trying to Serve a ML (pytorch) model on a Heroku, slug size too large [duplicate] - heroku

I've been getting the slug size too large warning (Compiled slug size: 789.8M is too large (max is 500M)) from Heroku and I can't figure out why, as my model size (cnn.pth below) is fairly small and my file directory is only 1.1mb in total: screenshot of directory.
It seems like the size increase is caused by running pipenv install torch, as the slug size was 89.1mb before installing torch and 798.8mb after.
My Pipfile currently has these packages installed:
[packages]
flask = "*"
flask-sqlalchemy = "*"
psycopg2 = "*"
psycopg2-binary = "*"
requests = "*"
numpy = "*"
gunicorn = "*"
pillow = "*"
torch = "*"
Is there any workaround for this?
Edit: I'm running Mac OSX 10.10.5, using Flask and pipenv.

The pytorch package that you're installing comes with both cpu and gpu support, thus has a large size. It seems you're using the free version of heroku, and only require the cpu support. The solution is to install the pytorch package for cpu only i.e.
In requirements.txt, write the wheel file path corresponding to the version of pytorch (cpu) that you're interested in. You can find the list of wheel files, which can be installed with pip. For example, for PyTorch 1.3.1, torchvision 0.4.2, Python 3.7, Linux, you can write the following for pytorch and torchvision respectively:
https://download.pytorch.org/whl/cpu/torch-1.3.1%2Bcpu-cp37-cp37m-linux_x86_64.whl
https://download.pytorch.org/whl/cpu/torchvision-0.4.2%2Bcpu-cp37-cp37m-linux_x86_64.whl
The above will download torch-1.3.1+cpu-cp37-cp37m-linux_x86_64.whl (107MB) torchvision-0.4.2+cpu-cp37-cp37m-linux_x86_64.whl (13MB) respectively.

Go to PyTorch Get Started page. Choose Stable version, Linux, pip, python, cpu and you can see:
pip install torch==1.8.1+cpu torchvision==0.9.1+cpu torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
Copy versions to your requirements.txt.
torch==1.8.1+cpu
torchvision==0.9.1+cpu
And add to the beginning of the requirements.txt this:
-f https://download.pytorch.org/whl/torch_stable.html
After that, all packages will be installed on Heroku.
For reference:
torch==1.8.1+cpu (169.1 MB);
torchvision==0.9.1+cpu (13.3 MB)

By today, March 28, 2022, these are the working versions:
torch==1.11.0+cpu
torchvision==0.12.0+cpu

(Aug, 2, 2022) the only solution I found was leaving the requirements.txt like this:
--find-links https://download.pytorch.org/whl/torch_stable.html
torch==1.11.0+cpu
--find-links https://download.pytorch.org/whl/torch_stable.html
torchvision==0.12.0+cpu

Related

requirements.txt pytorch with ">=" greater than?

I have a requirements.txt from a Github repo that contains following lines:
torch>=1.7.0,!=1.12.0
torchvision>=0.8.1,!=0.13.0
// and more
As I search SO and google they say I need to install pytorch with cuda specified, e.g. +cu110; in order to enable GPU and use the installed cuda.
So, for example this command does work on CLI: pip install torch==1.7.1+cu110 -f https://download.pytorch.org/whl/torch_stable.html
But the problem is with requirements.txt.
I looked Install PyTorch from requirements.txt - Stack Overflow and tried some solutions but they didn't work as the following.
// simply added `+cu110`
// didn't work
torch>=1.7.0+cu110,!=1.12.0
torchvision>=0.8.1+cu110,!=0.13.0
// w/ --extra-index-url
// didn't work
--extra-index-url https://download.pytorch.org/whl/cu110
torch>=1.7.0+cu110,!=1.12.0
--extra-index-url https://download.pytorch.org/whl/cu110
torchvision>=0.8.1+cu110,!=0.13.0
// w/ -f
// didn't work
-f https://download.pytorch.org/whl/torch_stable.html
torch>=1.7.0+cu110,!=1.12.0
-f https://download.pytorch.org/whl/torch_stable.html
torchvision>=0.8.1+cu110,!=0.13.0
So, is it possible to work with the combination of >= and +cu110 in requirements.txt?
Edit
It still doesn't work with no +cu110.
The following doesn't install cuda, but instead installs a normal torch.
--extra-index-url https://download.pytorch.org/whl/cu110
torch>=1.7.0+cu110,!=1.12.0
--extra-index-url https://download.pytorch.org/whl/cu110
torchvision>=0.8.1+cu110,!=0.13.0
So the stdout result is like:
Collecting torch!=1.12.0,>=1.7.0
Using cached torch-1.13.0-cp37-cp37m-win_amd64.whl (167.3 MB)
And pip freeze shows:
torch==1.13.0
torchvision==0.14.0
The following also doesn't install cuda.
-f https://download.pytorch.org/whl/cu110
torch>=1.7.0,!=1.12.0
-f https://download.pytorch.org/whl/cu110
torchvision>=0.8.1,!=0.13.0
The stdout and pip freeze result is the same to the above.
The following also doesn't work, it installs cuda, but it installs a random version of cuda. (it's understandable because I didn't specify a version I prefer)
-f https://download.pytorch.org/whl/torch_stable.html
torch>=1.7.0,!=1.12.0
-f https://download.pytorch.org/whl/torch_stable.html
torchvision>=0.8.1,!=0.13.0
The stdout:
Collecting torch!=1.12.0,>=1.7.0
Using cached https://download.pytorch.org/whl/cu117/torch-1.13.0%2Bcu117-cp37-cp37m-win_amd64.whl (2258.1 MB)
Collecting torchvision!=0.13.0,>=0.8.1
Using cached https://download.pytorch.org/whl/cu117/torchvision-0.14.0%2Bcu117-cp37-cp37m-win_amd64.whl (4.8 MB)
pip freeze result:
torch==1.13.0+cu117
torchvision==0.14.0+cu117

pip install dgl failed even with pip search and whl

I tried to install dgl(https://github.com/dmlc/dgl)
There were several ways to install it.(https://docs.dgl.ai/install/index.html#install-from-source)
pip
conda
from git source
from whl
and I failed with error message when I tried pip
$ pip install dgl-cu101
ERROR: Could not find a version that satisfies the requirement dgl-cu101 (from versions: none)
ERROR: No matching distribution found for dgl-cu101
even pip search spot the package
$ pip search dgl
dgl (0.4.1) - Deep Graph Library
dgl-bots.py (1.1.0) - A python wrapper for https://bots.discord.gl
dgl-cu100 (0.4.1) - Deep Graph Library
dgl-cu92 (0.4.1) - Deep Graph Library
dgl-cu90 (0.4.1) - Deep Graph Library
dgl-cu101 (0.4.1) - Deep Graph Library
dgl-cu102 (0.5a200108) - Deep Graph Library
conda also does not work
$ conda install -c dglteam dgl-cuda10.1
Solving environment: failed
PackagesNotFoundError:
The following packages are not available from current channels:
- dgl-cuda10.1
Current channels:
- https://conda.anaconda.org/dglteam/linux-ppc64le
- https://conda.anaconda.org/dglteam/noarch
- https://repo.anaconda.com/pkgs/main/linux-ppc64le
- https://repo.anaconda.com/pkgs/main/noarch
- https://repo.anaconda.com/pkgs/free/linux-ppc64le
- https://repo.anaconda.com/pkgs/free/noarch
- https://repo.anaconda.com/pkgs/r/linux-ppc64le
- https://repo.anaconda.com/pkgs/r/noarch
- https://repo.anaconda.com/pkgs/pro/linux-ppc64le
- https://repo.anaconda.com/pkgs/pro/noarch
- https://conda.anaconda.org/conda-forge/linux-ppc64le
- https://conda.anaconda.org/conda-forge/noarch
To search for alternate channels that may provide the conda package you're
looking for, navigate to
https://anaconda.org
and use the search bar at the top of the page.
install with source code is not available option because I am the remote client to the server and has no root access
install with whl seems nice but also occurred error.(https://pypi.org/project/dgl/#files)
$ pip install dgl_cu101-0.4.1-cp37-cp37m-manylinux1_x86_64.whl
ERROR: dgl_cu101-0.4.1-cp37-cp37m-manylinux1_x86_64.whl is not a supported wheel on this platform.
I read almost every articles and most of them said it would be the environment problem,
but as far as I know, they match!
My env server
CentOS 7
python 3.7
64 bit
minsky
4 GPUs
designed for ML
My env client
macos
iterm2
no root access
access from different city
How can I solve this problem?
Please help.
Your conda channel
https://conda.anaconda.org/dglteam/linux-ppc64le
gives the clue. Your system seems to be based on a ppc64le CPU, not the most frequently seen x86_64.
As you can see On the conda page, only linux-64 (i.e. x86_64) is available. Same goes for the pypi project.
So your setup does not match.
install with source code is not available option because I am the remote client to the server and has no root access
You should not need root access to compile the source code. The requirements listed in the guide are
gcc-c++ python3-devel make cmake
which, if not available yet could be installed using conda:
conda install -c conda-forge make cmake libgcc

No available image handler could decode this transfer syntax JPEG Lossless when read DICOM and ploting using matplotlib

When i use pydicom in python3.6, there are some problem:
import pydicom
import matplotlib.pyplot as plt
import os
import pylab
filePath = "/Users/zhuangrui/Documents/Python/Dicom/dicoms/zhang_bo/0001.dcm"
dataSet_1 = pydicom.dcmread(filePath)
plt.imshow(dataSet_1.pixel_array)
plt.show()
here is the problem:
How can this problem be solved? Thank you very much!
I've faced with the same problem, after doing some research on the suggested link above. I've managed to solve it by updating to the latest pydicom module "1.2.0" and installing gdcm. You can update the pydicom with
pip install -U git+https://github.com/pydicom/pydicom.git
You can find the latest gdcm here and this link explains the installation.
I use anaconda and it's easier to install the gdcm package and solve the problem. If you use anaconda
just type inside from your environment:
conda install pydicom --channel conda-forge to get pydicom's latest and
conda install -c conda-forge gdcm
to get the gdcm. This resolves the problem. Hope these will help.
With pydicom, you need an appropriate image handler also installed to handle compressed image types.
For JPEG lossless, in theory the following should work: jpeg_ls, gdcm, or Pillow with jpeg plugin. All of these also require Numpy to be installed. See the discussion at https://github.com/pydicom/pydicom/issues/532.
There is also a pull request in progress to add more descriptive error messages for what image handlers are needed for different images.
Problem:
I was trying to read medical images with .dcm extension. But was getting an error on Windows as well as on Ubuntu. I find a solution which will work on both the machined.
The error I got on Ubuntu is: NotImplementedError: this transfer syntax JPEG 2000 Image Compression (Lossless Only), can not be read because Pillow lacks the jpeg 2000 decoder plugin
(Note for Windows I was getting a different error but I am sure it's because of the same issue i.e. Pillow does not support JPEG 2000 format)
Platforma Information:
I am using: Python 3.6, Anaconda and Ubuntu, 15 GB RAM
RAM is important:
The solution I applied is the same as Ali explained above. But I want to add this installation may take time (depending on RAM you are using). On ubuntu where I am using 15 GB RAM on Cloud platform taken less time and on Windows on a local machine having 4 GB RAM taken a lot of time.
Solution
Anaconda is necessary. Why?
Please check the official doc of pydicom (https://pydicom.github.io/pydicom/dev/getting_started.html) its mentioned "To install pydicom along with image handlers for compressed pixel data, we encourage you to use Miniconda or Anaconda" (Note for Windows I was getting a different error)
If you are using Ubuntu directly open Terminal. If you are using Windows then on Anaconda Navigator go to Environment from here start terminal. Execute the following commands on it:
pip install -U git+https://github.com/pydicom/pydicom.git
conda install pydicom --channel conda-forge
conda install -c conda-forge gdcm
Cross Check:
Now use .dcm file for which we got the Error. Try to use the following code in Python notebook
filename = 'FileName.dcm'
ds = pydicom.dcmread(filename)
plt.imshow(ds.pixel_array, cmap=plt.cm.bone)
It should print the output. Also try this code:
ds.pixel_array
This will give you the array containing values.

pyethapp installation issue on OSX 10.13.2

Installing pyethapp in virtual environment on OSX 10.13.2 with following properties:
Python 2.7.14 pip (9.0.1) setuptools (38.2.4) wheel (0.30.0)
and get following error message:
Collecting pyethapp
Using cached pyethapp-1.5.0-py2.py3-none-any.whl
Collecting ethereum>=1.5.1 (from pyethapp)
Using cached ethereum-2.2.0.tar.gz
Complete output from command python setup.py egg_info:
error in ethereum setup command: 'tests_require' must be a string or list of strings containing valid project/version requirement specifiers; Unordered types are not allowed
Any idea how to resolve this?
I have the same issue on Arch Linux.
It looks like setup.py tests_require uses a set which is not support in setuptools > 38.0.
It should be.
pip install setuptools==37
This trick works for me.
Greetings.

Installing numpy on mac with pip: "requirements already satisfied" but "No module numpy"

I have python2.7.8 on mac, things I did:
sudo easy_install pip - worked.
pip install numpy:
Requirement already satisfied (use --upgrade to upgrade): numpy in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
I also did "pip upgrade numpy" - no luck. What's wrong?
Your problem is a conflict of different Python versions.
I would recommend installing Python and all the packages, such as numpy, scipy, matplotlib, pandas, etc via Brew
See this tutorial: https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/Homebrew-and-Python.md
You can verify which Python you're running with which python or which python3 in Terminal.
This solution is more flexible and cleaner in my opinion than using Conda/Miniconda. However it is also a bit more lengthy to install, as you need to have Xcode, devtools installed to build everything
Could it be that you have multiple versions of python installed? What happens if you run python using the full path like this:
$ /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2
instead of just python2?
In my experience on Mac (and other OS too) it is best to go with Anaconda / Miniconda. This is especially true for packages like NumPy and others from scientific stack.
While Anaconda is a full-blown distribution with about 200 packages, Miniconda is just Python with a few basic libraries. The big advantage is that all packages install as binary. Further, it makes it very simple and stable to install multiple Python versions side by side. For example:
conda create -n py27 python=2.7
creates a new environment with Python 2.7. Activate with:
source activate py27
Now:
conda install numpy
installs NumPy cleanly.
You can do the same for Python 3.5 and switch between environments with source activate.
After jumping from one stackoverflow answer to another I found the solution!
my problems were:
numpy at different location( actually at right, expected-to-be location). It was the IDLE that looks for its own default folder where python2.7 installed.
I checked that my numpy is working like this, run this script to check it is working:
import os
import sys
import pygame
sys.path.insert(0, '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python')
import numpy
pygame.init()
print "( using __version__): " + numpy.__version__
print numpy.version.version
user_paths = os.environ['PYTHONPATH']
print(user_paths)
sys.path insertion adds additional path to IDLE, so it knows where to look for numpy.
Then I check if numpy truly imported - i just print its version. Right now it is 1.8.0rc.
I want to find a way to avoid using this syspath insertion all the time.
So far so good - for now.
I had a similiar problem with numpy. However, it was resolved by choosing the right environment. If you are using VScode, open the command palette (ctrl+shift+p) and type
Python: Select Interpreter.
From there, try choosing the right virtual environment/Interpreter.

Resources