TransWikia.com

Permission Error Loading Pretrained Model, Django, AWS

Stack Overflow Asked by Andy Delworth on November 16, 2021

I’m working on deploying a Django app on AWS. Locally, everything works well. When deployed, the website opens fine, but when i try to run a deep learning model it gives me an Errno 13 Permission Error. /severity is the page of the website where this occured. Any and all help is appreciated 🙂

Error Displayed on Site

Request Method: POST
Request URL: http://radiology-ai-env.eba-wgmpba4k.us-west-2.elasticbeanstalk.com/severity/

Django Version: 3.0.8
Python Version: 3.6.10
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'tinymce',
 'main']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/opt/python/current/app/main/views.py", line 62, in severity
    results = severity_model(image, mask)
  File "/opt/python/current/app/main/ml_models.py", line 11, in severity_model
    return Image_based_severity_prediction.run_model(image, mask)
  File "/opt/python/current/app/main/Image_based_severity_prediction.py", line 70, in run_model
    model_eff = EfficientNet.from_pretrained('efficientnet-b0')
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/efficientnet_pytorch/model.py", line 211, in from_pretrained
    load_pretrained_weights(model, model_name, load_fc=(num_classes == 1000), advprop=advprop)
  File "/opt/python/run/venv/local/lib/python3.6/site-packages/efficientnet_pytorch/utils.py", line 327, in load_pretrained_weights
    state_dict = model_zoo.load_url(url_map_[model_name])
  File "/opt/python/run/venv/local/lib64/python3.6/site-packages/torch/hub.py", line 480, in load_state_dict_from_url
    os.makedirs(model_dir)
  File "/opt/python/run/venv/lib64/python3.6/os.py", line 210, in makedirs
    makedirs(head, mode, exist_ok)
  File "/opt/python/run/venv/lib64/python3.6/os.py", line 210, in makedirs
    makedirs(head, mode, exist_ok)
  File "/opt/python/run/venv/lib64/python3.6/os.py", line 210, in makedirs
    makedirs(head, mode, exist_ok)
  File "/opt/python/run/venv/lib64/python3.6/os.py", line 220, in makedirs
    mkdir(name, mode)

Exception Type: PermissionError at /severity/

Exception Value: [Errno 13] Permission denied: '/home/wsgi'

The code in my file that caused this was:

model_eff = EfficientNet.from_pretrained('efficientnet-b0')

In case it is important…

**wsgi.py**
import os
import sys
from django.core.wsgi import get_wsgi_application

# path = '/opt/python/current/app/mysite'
# if path not in sys.path:
#     sys.path.append(path)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
os.environ['HTTPS'] = "on"
print("I AM IN THE GOOD FILE!!!!!!!!!!")
application = get_wsgi_application()

settings.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'l@q&!z!*#^b_v83bkcbxc2q0c_zk#w=9_6oz9pp38p#!-r&$!1'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['django-env.eba-au7pkejq.us-west-2.elasticbeanstalk.com', '127.0.0.1',
                 'django-env-3-oclock.eba-3bakdn28.us-west-2.elasticbeanstalk.com',
                 'http://django-env-3-oclock.eba-3bakdn28.us-west-2.elasticbeanstalk.com/',
                 'radiograph-models-env.eba-ctu2dpmp.us-east-1.elasticbeanstalk.com',
                 'radiology-ai-env.eba-wgmpba4k.us-west-2.elasticbeanstalk.com']
TINYMCE_DEFAULT_CONFIG = {
    'height': 360,
    'width': 1120,
    'cleanup_on_startup': True,
    'custom_undo_redo_levels': 20,
    'selector': 'textarea',
    'theme': 'modern',
    'plugins': '''
            textcolor save link image media preview codesample contextmenu
            table code lists fullscreen  insertdatetime  nonbreaking
            contextmenu directionality searchreplace wordcount visualblocks
            visualchars code fullscreen autolink lists  charmap print  hr
            anchor pagebreak
            ''',
    'toolbar1': '''
            fullscreen preview bold italic underline | fontselect,
            fontsizeselect  | forecolor backcolor | alignleft alignright |
            aligncenter alignjustify | indent outdent | bullist numlist table |
            | link image media | codesample |
            ''',
    'toolbar2': '''
            visualblocks visualchars |
            charmap hr pagebreak nonbreaking anchor |  code |
            ''',
    'contextmenu': 'formats | link image',
    'menubar': True,
    'statusbar': True,
    }
# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tinymce',
    'main'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.csrf'
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#     }
# }


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'EST'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = 'static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

STATIC_ROOT = os.path.join (BASE_DIR, 'static')

MEDIA_URL = 'media/'

One Answer

It seems your torch package is trying to create a directory and it does not have write access hence the permission denied error. I can't exactly tell where it is trying to create the directory (it seems in root i.e /home/wsgi).

I suggest, if you have access to this os.makedirs(model_dir), create a tmp directory, change its permission to 777 (chmod -R 777 tmp) and point your model_dir to tmp directory(i.e path_to_temp/tmp/home/wsgi)

or you can just create a dir in root and change its permission.(I am not sure, but I think it would solve the problem)

$ mkdir /home
$ chmod -R 777 /home

Answered by mursalin on November 16, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP