/detection

DLS diploma project - Autumn'21. Module pipe for detection

Primary LanguageJupyter NotebookMIT LicenseMIT

Модульная обработка изображений с использованием OpenCV и генераторов Python с помощью Detectron2. Pipeline, web-приложение и локальное приложение

На данный момент доступно только в Ya.Cloud в связи с атаками и утечками Oath_tokens с Heroku

Содержание:


📝 WEB-Приложение в спящем режим. Для запуска потребуется время - примерно 30 секунд после нажатия перехода по ссылке. Также, возможны сбои в работе - приложение на бесплатном сервере. Загрузка производится с помощью сервиса Cloudinary, так как heroku не позволяет осуществлять зарузку на свои сервера Фото после загрузки удаляются.

2022-01-03-02-23-14

📝 Ошибка может быть связана с превышением памяти, выделенной под приложение. Можно попробовать еще раз (в браузере вернитесь на предыдущую страницу и нажмите Отправить еще раз) или взять другую картинку. С локальной сборкой проблем не возникнет

📝 Update - Для удобства, приложение дополнительно временно в демонстрационных целях, было развернуто на ВМ Ya.Cloud - доступно по этой ссылке


Мотивировка

Модульная обработка изображений с использованием OpenCV и генераторов Python с помощью Detectron2. Идея модульности позволяет программировать промышленный pipeline обработки несколькими специалистами DS. Идея использования генераторов взята у Arosław Gilewski и оформленна под Detectron2, были учтены все изменения в технической составляющей - в итоге получаем полноценный модуль, который может дополняться в нужных нам вариациях.

Целью работы была реализация различных подходов внедрения Detectron2 и OpenCV в проекты - как локальные, так и web с использованием модулей или же в виде скриптов с применением различных вариантов работы моделей, а также продемонстрировать различные варианты установки модулей и зафиксировать различные способы решения проблем, возникающих при работе с torch и Detectron2 в разрезе приложений. Мобильные приложения не учитывались, но оставлена ссылка, если вы вдруг захотите использовать подобный модуль на мобильных устройствах

📝 Если у Вас не открывается ссылка на medium - включите режим инкогнито Google Chrome

  1. Локальное исполнение. В данном исполнении - вы можете работать со случаными изображениями, либо применять обученную модель для детекции оружия, в том числе на видео. При расширении обучающих данных, можно получить более точные результаты. Интерфейс позволяет, не углубляясь в код, производить сегментацию изображений, в том числе и затенять фон. Позволяет использовать GPU

    Доступный функционал:

    • Изображения:

      • Работает с папками! не только единичными изображениями
      • Instance segmentation на изображениях
      • Keypoints
      • Затемнение фона (separate background)
      • Panoptic segmentation
      • Детекция оружия (custom dataset)
    • Видео:

      • Работает с папками! не только единичными видео файлами
      • Instance segmentation на видео
      • Показ обработки в режиме реального времени
      • Детекция оружия на видео

      Поддержка GPU!

2021-12-28-10-43-20

📝 Подойдет тем, кто хочет иметь доступ к коду и обладать большим контролем над приложением.
📝 Примеры работы в папке output и по тексту Readme.md

-- структура модуля --

Изображения: process_img.py -> detection.pipe.py -> detction.capture_imgs.py -> 
-> detection.predict.py -> detection.annotate_img.py или detection.separate_background.py -> 
-> detection.save_img.py

Видео: process_vid.py -> detection.pipe.py -> detection.capture_vid.py -> 
-> detection.predict.py -> detection.annotate_vid.py -> 
-> detection.save_vid.py -> detection.display_vid.py
  1. Web-приложение. Реализовано дополнительно web-приложение с ограниченной функциональностью (только изображения) - Находится по адресу: https://detartyseg.herokuapp.com/ и по этой ссылке в Ya.Cloud. К сожалению - пришлось обрезать функционал, из-за ограничений размера на приложение. Также, возможен запуск через Docker - инструкция в соответствующей части файла Readme.md - локально, либо после клонирования репозитория и настройки окружения - запус через app_local.py

    Доступный функционал:

    • Изображения:

      • Работает с единичными изображениями
      • Instance segmentation на изображениях
      • Keypoints на изображениях
      • Panoptic segmentation на изображениях (УДАЛЕНО из heroku. Доступно в docker и yaCloud)
      • Затемнение фона (separate background)
      • Blur эффект
      • Черно-белое изображение

      только CPU

2022-01-03-11-27-44

📝 Подойдет тем, кто не хочет тратить время на настройки - а сразу предпочитает получить результат. Есть альтернатива в виде Docker сборки

-- структура модуля --

app.py работает с Flask - загрузка css и html из static и templates. 
Загрузка изображений через Api Cloudly

Локальная работа

1. Скрипт process_img.py

Здесь представлено пять вариантов работы модели:

  • instance segmentation на классы COCO
  • instance segmentation для обнаружения оружия на кастомном датасете. Изображения были взяты с репозитория MLWhiz, которые я разметил вручную с помощью supervise.ly.

Обучение вы можете посмотреть в этом ноутбуке, если захотите обучить свой детектор (доступно и в Collab). Веса базовых моделей можно брать из зоопарка - формировать .yaml аналогично представленным в папке config

  • separate background - отделение фона, вместо аннотации. Идея предложена Arosław Gilewski
  • keypoints - для построения ключевых точек людей на изображениях (pose-flow)
  • panoptic segmentation - для паноптической сегментации изображений

Для удобства запуска, данному модулю было добавлено простое GUI, с помощью GOOEY


Установка локально


Рекомендуется устанавливать в virtuenv. Необязательный шаг - можно пропустить

pip install virtualenv
virtualenv detection
source detection/bin/activate

Также рекомендуется дочитать инструкции до конца перед началом установки. Для Win рекомендуется использовать Ubuntu 20.04.3 LTS с WLS2 или Conda терминал. Желательно, чтобы у вас уже стояла Anaconda

Шаги настройки модуля.

git clone https://github.com/ArtyKrafty/detection
pip install -r requirements_loc.txt

Detectron2 необходимо установить в соседний каталог с папкой модуля

Установка Detectron2

cd

git clone https://github.com/facebookresearch/detectron2.git
python -m pip install -e detectron2

# На macOS, необходимо установить так (особенно, если у вас M1)
CC=clang CXX=clang++ ARCHFLAGS="-arch x86_64" python -m pip install -e detectron2

Если возникнут сложности, можно обратиться к официальной документации

наиболее частая проблема - отсутствие С++ compiler 
conda install -c conda-forge compilers

Официальное руководство по установке Detectron2.
Официальное руководство по установке Detectron2. Installation

Приложение должно быть готовым к работе, ниже приведены решения, если возникают трудности

Для Win у вас должно быть установлено Visual C++ 2015 build tools
Если нет - установите отсюда - это необходимо для Detectron2

Также, рекомендуется проверить версию torch и torchvision - проверить можно тут - выбираем Package - копируем команду для установки

123

📝 Если у вас появляется ошибка - Torchvision error: Could not find module image.pyd - рекомендую ознакомиться здесь с решением

Например, для CPU:

pip3 install torch torchvision torchaudio
или
conda install pytorch torchvision cpuonly -c pytorch

Использование модуля


  1. Instance segmentation на классы COCO
Открываем терминал

cd detection
pythonw process_img.py

📝 Обратите внимание, что вызов осуществляется через pythonw, не python или python3

У вас откроется окно:

2021-12-28-10-43-20

Необходимо задать input. Это изображения, на которых будут предсказываться объекты. 
Вы можете воспользоваться моими фотографиями - папка

./examples/images/photo/

также вы можете выбрать порог confidence. 
Нажимаем START и в папке outputs у вас обработанные изображения

0299ce77-30e4-4b31-b8e5-fad85b140111

на стандартных моделях нет проблем с отработкой на основные классы в COCO датасете

  1. Instance segmentation для обнаружения оружия на кастомном датасете. Названиями классов пришлось пренебречь - иначе потеряли в универсальности. Если собрать сет больше - можно использовать подобные модели для обнаружения наличия оружия у человека
Открываем терминал

cd detection
pythonw process_img.py

📝 Обратите внимание, что вызов осуществляется через pythonw, не python или python3

У вас откроется окно:

2021-12-28-10-10-42

Необходимо задать input. Это изображения, на которых будут предсказываться объекты. 
Вы можете воспользоваться моими фотографиями - папка

./examples/images/weapon/

также вы можете выбрать порог confidence. Тут необходимо поменять модель - 
вы найдете нужную в папке config (и веса к ней):   


путь ./detection/configs/COCO-InstanceSegmentation_weapon/mask_rcnn_R_50_FPN_3x.yaml. 
Грузим к ней веса: ./detection/configs/COCO-InstanceSegmentation_weapon/model_final.pth
Нажимаем START и в папке outputs у вас обработанные изображения

new-use-hotel-robber-with-gun-2

здесь пришлось пожертвовать словарем классов, чтобы была универсальность - оружие не было основной целью данного приложения, использовалось в качестве примера, как можно применять модель в реальном мире на реальных задачах, а также показать baseline обучения своего детектора

  1. Отделение фона - работает аналогично - достаточно поставить отметку separate_background
Необходимо задать input. Это изображения, на которых будут предсказываться объекты. 
Вы можете воспользоваться моими фотографиями - папка

./examples/images/photos/

также вы можете выбрать порог confidence. Тут необходимо поменять модель - 
вы найдете нужную в папке config (и веса к ней):   


путь ./detection/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml. 
Ставим отметку - separate background:
Нажимаем START и в папке outputs у вас обработанные изображения

Получаем изображение с blur-эффектом на фон - инвертируем маску и вместо предсказаний получаем выделенный от фона объектр

004

моя любимая особенность данного приложения - помогает без навыков photoshop сделать небольшую обработку своего фото

  1. Построение ключевых точек:
Необходимо задать input. Это изображения, на которых будут предсказываться объекты. 
Вы можете воспользоваться моими фотографиями - папка

./examples/images/key_points/

также вы можете выбрать порог confidence. Тут необходимо поменять модель - 
вы найдете нужную в папке config (и веса к ней):   


путь ./detection/configs/COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml. 
Нажимаем START и в папке outputs у вас обработанные изображения

Веса задавать не надо. 

Получаем изображение с отмеченными ключевыми точками. Предназначено для работы с изображениями с людьми.

predict

здесь есть неточности, модель справляется несовершенно. Возможны улучшения, с помощью внедрения иной технологии pose flow. например, можно взять решение от YuliangXiu здесь

  1. Паноптическая сегментация изображений - достаточно указать адрес к конфигурации
Необходимо задать input. Это изображения, на которых будут предсказываться объекты. 
Вы можете воспользоваться моими фотографиями - папка

./examples/images/pan/

также вы можете выбрать порог confidence. Тут необходимо поменять модель - 
вы найдете нужную в папке config (и веса к ней):   


путь ./detection/configs/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml 
Нажимаем START и в папке outputs у вас обработанные изображения

Веса задавать не надо. 

predict

panoptic segmentation также удается, но не совершенно - можно взять более сложную и глубокую модель либо выбрать другой фреймворк, или же просто - дообучить, собрав сет

Целью было продемонстрировать различные варианты работы модели без углублений в код со стороны пользователя данного модуля


2. Скрипт process_video.py

Данный модуль предназначен для обработки видео и показа результата обработки при необходимости


Использование модуля


Instance segmentation на классы COCO

Открываем терминал

cd detection
pythonw process_video.py

📝 Обратите внимание, что вызов осуществляется через pythonw, не python или python3. Принцип работы аналогичный. Можно детектировать и оружие - по соображениям этики - было принято не добавлять файл с вооруженными нападениями в репозиторий

У вас откроется окно:

2021-12-27-23-26-40

Необходимо задать input. Это видео, на котором будут предсказываться объекты. 
Вы можете воспользоваться моими примером

./examples/videos/cross_roads.mp4

также вы можете выбрать порог confidence. 
Нажимаем START и в папке outputs у вас обработанное видео. Занимает некоторое время

📝 Примечание. Важно! На данный момент, существует bug на класс VideoVisualizer. Если возникает (видео не хочет склеиваться) - чтобы его обойти, можно использовать класс Visualizer, который используется для изображений, но маски от кадра к кадру будут меняться. Возможно bug наблюдается только на M1 silicone. Пример вывода вы можете посмотреть в папке outputs

animated

собственно, из минусов - наблюдаем изменчивость цвета маски, в идеале - маска должна быть статична и не менять цвет. На данный момент, пришлось прибегнуть к хитрости - использовать разметку кадров и склеивать их в видео после обработки


Web-приложение

Скрипт app_local.py - для запуска локально

зеркало для app.py, который находится по адресу: https://detartyseg.herokuapp.com/ и дополнительно в yaCloud К сожалению - пришлось обрезать функционал, из-за ограничений размера на приложение. Пришлось убрать паноптическую сегментацию - превышения памяти на приложение, на бесплатном сервере. Остался функционал в локальном приложении и docker. Если захотите воспользоваться сервисом heroku - Вам достаточно создать репозиторий с Вашим приложением, и вам будет доступно - deploy from git

2022-01-06-00-11-48

📝 Update - Для удобства, приложение дополнительно временно, в демонстрационных целях, было развернуто на ВМ ya.Cloud - доступно по этой ссылке

Если хотите запустить локально - запускаем app_local.py. но у вас должно быть настроено уже окружение (как мы настраивали выше):

 git clone https://github.com/ArtyKrafty/detection
 cd detection
 python app_local.py
 

Запустится приложение с ограниченным функционалом по адресу - http://ВАШ_URL_из_терминала:8080/

2022-01-03-11-27-44

Здесь вы можете загрузить изображение с рабочего стола, выделить маски на нем или затемнить фон. Также возможно загрузка изображений по URL - только instance segmentation


Dockerfile

Другой вариант - запуск через Docker (локальная версия https://detartyseg.herokuapp.com/) - вес образа ~14.1 GB (у вас должен быть установлен Docker - дополнительно ничего не требуется):

git clone https://github.com/ArtyKrafty/detection

cd detection

docker build . -f Dockerfile -t detectron2
docker run --name detectron2 -p 127.0.0.1:8080:8080 detectron2


затем - после того, как закончили:

docker stop detectron2
docker rm $(docker ps -qa)

или использовать docker-desktop приложение, что может быть удобнее

посмотреть директории внутри: docker run -t -i detectron2 /bin/bash

123

Также, вы можете с помощью Dockerfile запустить приложение на вашей ВМ

📝 Если у вас MacOS - зайдите в настройки docker -> advanced и увеличьте память RAM с 2 до 4 ГБ - иначе С++ компилятор не сможет собрать torch


Виртуальная машина

С помощью Dockerfile Вы можете установить web-приложение на виртуальную машину. Минимальные требования к Vm - 8gb Ram, vCpu - 50%, 20Gb HDD - стоимость такой машины в месяц - примерно 1700 - 1900 руб. Gpu подключать нет смысла - работаем с единичными изображениями

Регистрируемся на любом сервисе, предоставляющим услуги ВМ - например, я воспользовался Ya.Cloud

В консоли выбираем - Compute Cloud:

2022-01-05-23-54-18

Выбираем в качестве операционной системы нашей ВМ - Ubuntu:

2022-01-05-23-54-30

Нам необходимо создать ssh-key для обмена с виртуальной машиной и ввести его в это поле:

2022-01-05-23-54-36

Для того, чтобы создать ssh ключ, на MacOS достаточно зайти в терминал, на Windows - вы можете скачать git - в который входит улитита, необходимая для создания ключа.

cd .ssh

mkdir <your_app_name>
cd <your_app_name>
ssh-keygen -t rsa

Вам будет предложено создать имя ключа и создать пароли. Дальше вам просто необходимо прочитать ваш публичный ключ и скопировать вывод в специальное поле виртуальной машины

cat test_key.pub

Копируем вывод и нажимаем создать ВМ Теперь мы можем к ней подключить в терминале:

ssh <ЛОГИН>@<публичный_IP-адрес_виртуальной машины> 

Публичный адрес Вам будет доступен после создания. Дальше, идем сюда и устанавливаем на вашу ВМ Docker. После установки. все как и у вас локально:

git clone https://github.com/ArtyKrafty/detection

cd detection

docker build . -f Dockerfile -t detectron2
docker run --rm --name detectron2 -p 8080:8080 detectron2

Если при сборке Docker возникает ошибка docker: Got permission denied issue - просто выполняем команду: sudo chmod 666 /var/run/docker.sock

Приложение будет доступно тут:

публичный_IP-адрес_виртуальной машины>:8080

Техническая поддержка

Проблемы возникают не с приложением, а могут возникнуть при установки сопутствующего окружения - Detectron2, torch и так далее. Здесь собрал решения, которые помогут вам в установке, если вы решите работать не через docker. Тестировалось на MacOs и Windows - на Win - основная проблема - отсутствие Microsoft Visual C++. Данный сборник поможет решить основные проблемы по установке torch и Detectron2. Также, рекомендую, чтобы у вас уже была Anaconda

Источник Описание Решение
Приложение Web-app Появляется ошибка heroku - Application Error Ошибка может быть связана с превышением памяти. Можно попробовать еще раз выполнить действие или взять другую картинку. С локальной сборкой проблем не возникнет
torch, Detectron C++ ошибка на Windows при установке на pycatools У вас не установлен Visual Studio C++ - отсутствует компилятор. Установите отсюда
torch, torchvision устаревшая версия или установлена не соответствющая вашему ПК проверить можно тут
torch Torchvision error: Could not find module image.pyd ознакомиться здесь с решением
Docker ошибка с С++ при сборке или os зайдите в настройки docker -> advanced и увеличьте память RAM с 2 до 4 ГБ

Вывод

По результатам работы, удалось построить два полноценных модуля: локальный с поддержкой GPU и работой с группой файлов и web-версию, которое работает с единичными изображениями без необходимости настройки и установки. Данный модуль является достаточно гибким, в него можно добавлять новые модели, улучшать их качество посредством дообучения или перенастраивать под нужды бизнеса или иных заинтересованных лиц с помощью обучения на свое сете и встраивании моделей через добавление новых конфигураций

Данный модуль может быть использован и с другими моделями и фреймворками, не только detectron - необходимо лишь заменить модели в исполнительных файлах.

В локальной версии, модуль может быть использован продвинутыми разработчиками или заинтересованными лицами, а в web-версии - пользователям не важно уметь владеть навыками работы с детекторами - без значительных усилией - может быть получен некоторый результат. Для удобства, была реализована поддержка Docker и добавлена подробная инструкция по настройке модуля. Если у вас появится желание реализовать подобное приложение для мобильных телефонов - вы можете воспользоваться D2go, репозиторий тут


Bonus В этом ноутбуке, вы найдете baseline применения детектора для переноса стиля. В комбинации с VGG19. Встраивать данный модуль не стали - обязательно требуется наличие GPU - иначе процесс обучения достаточно долгий

Спасибо за прочтение


Ссылки

  1. Курс Deep learning school - обучают и направляют.
  2. Arosław Gilewski - инженер CV. Идея использования генераторов python и за baseline удаления фона.
  3. Detectron2 - библиотека Meta для работы с изображениями с огромным зоопарком обученных моделей.
  4. OpenCV - средство обработки видео и изображений. Без этой библиотеки было бы сложнее.
  5. Heroku - быстрый способ разработки и размещения приложений.
  6. pyTorch - фреймворк работы с нейронными сетями
  7. Docker - средство упаковки и перемещения приложений
  8. MLWhiz - предоставлены образцы изображений для разметки







Modular image and video processing with OpenCV and Detectron2. Pipeline, web-app and local-app

Table of content:


📝 Web-Application is in sleep mode. It will take time to launch - about 30 sec. Also, malfunctions are possible - the application is on a free server. Photos are deleted after upload. The download is performed using the Cloudinary, since heroku does not allow downloading the download to their servers. Photos are deleted after upload.

2022-01-03-02-23-14

📝 The error may be related to out of memory. You can try again (push back in your browser and again push Отправить) or take another picture. There will be no problems with local assembly

📝 Update - For convenience, the application has been additionally temporarily for demonstration purposes deployed to VM Ya.Cloud - available at this link


Motivation

Modular image processing using OpenCV and Python generators using Detectron2. The idea of modularity allows the industrial processing pipeline to be programmed by several DS specialists. The idea of using generators is taken from Arosław Gilewski - as a result, we get a full-fledged module that can be supplemented in the variations we need.

The aim of the work was to implement various approaches to integrating Detectron2 and OpenCV into projects - both local and web using modules or in the form of scripts using different versions of the models, as well as use various options for installing modules and fix various ways to solve problems, contributing to the work with torch and Detectron2 in the context of applications

📝 If you do not have a link to medium - turn on incognito mode

  1. Local execution. In this version, you can work with random images, or use a trained model for detection weapons, including video. By expanding the training data, you can get more accurate results. The interface allows, without delving into the code, to perform image segmentation, including shading the background.

Available functionality:

  - Images:
    - Works with groups of image, not only single
    - Instance segmentation on images
    - Key points
    - Panoptic segmentation
    - Separate background
    - Weapon detection (custom data set)
  - Video:
    - Works with groups of vidoe, not only single
    - Instance segmentation on video
    - Show processing in real time
    - Weapon detection on video

2021-12-28-10-43-20

-- module structure --

Images: process_img.py -> detection.pipe.py -> detction.capture_imgs.py ->
-> detection.predict.py -> detection.annotate_img.py or detection.separate_background.py ->
-> detection.save_img.py

Video: process_vid.py -> detection.pipe.py -> detection.capture_vid.py ->
-> detection.predict.py -> detection.annotate_vid.py ->
-> detection.save_vid.py -> detection.display_vid.py
  1. Web Application. An additional web application with limited functionality has been implemented (only images) - Located at: https://detartyseg.herokuapp.com/ and at this link in Ya.Cloud. Unfortunately, I had to cut the functionality due to the size restrictions for the application. Also, launching via Docker is possible - instructions in the corresponding part of the file Readme.md - locally, or after cloning the repository - launching via app_local.py

Available functionality:

  - Images:
    - Works with single image
    - Instance segmentation on images
    - Keypoints on images
    - Panoptic segmentation (`Deleted from heroku. Available in docker and yaCloud`)
    - Separate background
    - Blur effect
    - Black and white image

2022-01-03-11-27-44

-- module structure --

app.py works with Flask - loading css and html from static and templates. 
Uploading images via Api Cloudly

Local execution

1. Script process_img.py

Here are five options for how the model works:

  • instance segmentation into classes COCO
  • instance segmentation for detecting weapons on a custom dataset. The images were taken from the MLWhiz repository, which I mapped out manually using supervise.ly.

You can watch the training in this notebook, if you want to train your detector (available in Collab). The weights of the base models can be taken from the zoo - form .yaml similarly to those presented in the config folder

  • separate background - separation of the background, instead of annotation. Idea suggested by Arosław Gilewski
  • keypoints - for building key points of people on images ( pose-flow).

For ease of launch, a simple GUI was added to this module, using GOOEY


Local Installation


It is recommended to install to virtuenv. Optional step - can be skipped

pip install virtualenv
virtualenv detection
source detection/bin/activate

It is also recommended to read the instructions to the end before starting the installation. For Win it is recommended to use Ubuntu 20.04.3 LTS with WLS2 or Conda terminal. It is desirable that you already have Anaconda

Module configuration steps.

git clone https://github.com/ArtyKrafty/detection
pip install -r requirements_loc.txt

Detectron2 must be installed in the adjacent directory with the module folder

Detectron2 installation

cd

git clone https://github.com/facebookresearch/detectron2.git
python -m pip install -e detectron2

# On macOS, you need to install like this (especially if you have M1
CC=clang CXX=clang++ ARCHFLAGS="-arch x86_64" python -m pip install -e detectron2

If you have any difficulties, you can refer to the official documentation

the most common problem is the lack of a C ++ compiler:
conda install -c conda-forge compilers

Detectron2 Official Installation Guide.
Official Installation Guide for Detectron2. Installation

For Win, you must have Visual C++ 2015 build tools installed If not, install from here - this is required for Detectron2

Also, it is recommended to check the version of torch and torchvision - you can check here - select Package - copy the command to install

123

📝 If you get an error - Torchvision error: Could not find module image.pyd - I recommend reading here with solution

For example, for CPU:

pip3 install torch torchvision torchaudio
или
conda install pytorch torchvision cpuonly -c pytorch

Module usage


  1. Instance segmentation with COCO
Open terminal

cd detection
pythonw process_img.py

📝 Note that the call is made through pythonw, not python or python3

A window will open:

2021-12-28-10-43-20

You must set input. These are the images on which objects will be predicted.
You can use my photos - folder

./examples/images/photo/

also you can choose the confidence threshold.
Press START and in the outputs folder you have processed images

0299ce77-30e4-4b31-b8e5-fad85b140111

on standard models of problems with working out the main classes in the COCO dataset

  1. Instance segmentation for detecting weapons on a custom dataset. The class names had to be neglected - otherwise they lost in universality. If you collect a larger set, you can use similar models to detect whether a person has a weapon
Open terminal

cd detection
pythonw process_img.py

📝 Note that the call is made through pythonw, not python or python3

A window will open:

2021-12-28-10-10-42

You must set input. These are the images on which objects will be predicted.
You can use my photos - folder

./examples/images/weapon/

also you can choose the confidence threshold. Here you need to change the model -
you will find the needed one in the config folder (and weights to it):


path ./detection/configs/COCO-InstanceSegmentation_weapon/mask_rcnn_R_50_FPN_3x.yaml.
We load weights to it: ./detection/configs/COCO-InstanceSegmentation_weapon/model_final.pth
Press START and in the outputs folder you have processed images

new-use-hotel-robber-with-gun-2

here I had to sacrifice the class dictionary to have universality - weapons were not the main purpose of this application, was used as an example of how the model can be applied in the real world to real-world problems, as well as to show baseline training your detector

  1. Separating the background - works the same way - just check the separate_background
You must set input. These are the images on which objects will be predicted.
You can use my photos - folder

./examples/images/photos/

also you can choose the confidence threshold. Here you need to change the model -
you will find the needed one in the config folder (and weights to it):


path ./detection/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml.
We put a mark - separate background:
Press START and in the outputs folder you have processed images

Getting an image with a blur effect on the background

004

my favorite feature of this application - it helps to do a little processing of your photo without photoshop skills

  1. Building key points - works similarly to weapons - just specify the address to the configuration
You must set input. These are the images on which objects will be predicted.
You can use my photos - folder

./examples/images/key_points/

also you can choose the confidence threshold. Here you need to change the model -
you will find the needed one in the config folder (and weights to it):


path ./detection/configs/COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml.
Press START and in the outputs folder you have processed images

There is no need to set weights.

We get an image with marked key points.

predict

there are inaccuracies here, the model does not do it perfectly. Improvements are possible through the introduction of another pose flow technology. for example, you can take a solution from YuliangXiu here

  1. Panoptic group of images - just specify the address to the configuration
You must set input. These are the images on which objects will be predicted.
You can use my photos - folder

./examples/images/pan/

also you can choose the confidence threshold. Here you need to change the model -
you will find the needed one in the config folder (and weights to it):


path ./detection/configs/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml
Press START and in the outputs folder you have processed images

There is no need to set weights.

predict

panoptic segmentation is also successful, but not completely - you can take a more complex and deeper model either choose another framework, or simply retrain by putting together a set

The goal was to demonstrate various options for the model without delving into the code on the part of the user of this module - it is enough to specify the address to the configuration


2. Script process_video.py

This module is designed to process video and display the processing result, if necessary.


Module usage


Instance segmentation with COCO

Open terminal

cd detection
pythonw process_video.py

📝 Note that the call is made through pythonw, not python or python3. The principle of operation is similar. Weapons can also be detected - for ethical reasons - it was customary not to add the file with armed attacks to the repository

A window will open:

2021-12-27-23-26-40

You must set input. This is a video in which objects will be predicted.
You can use my example

./examples/videos/cross_roads.mp4

also you can choose the confidence threshold.
Press START and in the outputs folder you have the processed video. Takes some time

📝 Important! At the moment, there is a bug on the class VideoVisualizer. If it occurs (the video does not want to stick together) - to get around it, you can use the Visualizer class, which is used for images, but the masks will change from frame to frame. Perhaps the bug is observed only on M1 silicone. Sample output you can look in the outputs folder

animated

in fact, of the minuses - we observe the variability of the color of the mask, ideally - the mask should be static and not change color. Presently, had to resort to a trick - to use frame markup and glue them in the video after processing


Web-application

Script app_local.py - for local work

mirror for app.py, which is located at: https://detartyseg.herokuapp.com/ and in yaCloud Unfortunately - I had to cut the functionality, due to the size restrictions for the application. I had to remove the panoptic segmentation - memory excess on the application, on a free server. Remained functional in the local application and docker If you want to use the heroku service - you just need to create a repository with your application, and you will have access to - deploy from git

2022-01-06-00-11-48

If you want to run locally, run app_local.py, but you need torch and detectron2:

 git clone https://github.com/ArtyKrafty/detection
 cd detection
 python app_local.py
 

An application with limited functionality will start at the address - http: // YOUR_URL_from_terminal: 8080 /

2022-01-03-11-27-44

Here you can load an image from the desktop, select masks on it or darken the background. It is also possible to download images by URL - only instance segmentation


Dockerfile

Another option is to run via Docker (local copy of https://detartyseg.herokuapp.com/) - weight of Image 14.4 GB:

git clone https://github.com/ArtyKrafty/detection
cd detection

docker build . -f Dockerfile -t detectron2
docker run --name detectron2 -p 127.0.0.1:8080:8080 detectron2


and after finish:

docker stop detectron2
docker rm $(docker ps -a -q)

or you can use docker-desktop app
look files inside: docker run -t -i detectron2 /bin/bash

123

You can also run the application on your VM using the Dockerfile.

📝 If you MacOS user - go to docker -> advanced settings and increase RAM from 2 to 4 GB - otherwiseC ++compiler can't build torch.


Virtual machine

With the Dockerfile you can install a web application on a virtual machine. Minimum requirements for Vm - 8gb Ram, vCpu - 50%, 20Gb HDD.

We register on any service that provides VM services - for example, I used Ya.Cloud

In the console, select - Compute Cloud:

2022-01-05-23-54-18

We select Ubuntu as the operating system of our VM:

2022-01-05-23-54-30

We need to create an ssh-key to communicate with the virtual machine and enter it in this field:

2022-01-05-23-54-36

In order to create an ssh key, on MacOS just go to the terminal, on Windows - you can download git - which includes the utility needed to create the key.

cd .ssh

mkdir <your_app_name>
cd <your_app_name>
ssh-keygen -t rsa

You will be prompted to create a key name and create passwords. Then you just need to read your public key and copy the output in a special field of the virtual machine

cat test_key.pub

Copy the output and click create VM. Now we can connect to it in the terminal:

ssh <LOGIN>@<public_IP_VM> 

The public address will be available to you after creation. Next, go here and install Docker on your VM. After installation. everything is the same as yours locally:

git clone https://github.com/ArtyKrafty/detection

cd detection

docker build . -f Dockerfile -t detectron2
docker run --rm --name detectron2 -p 8080:8080 detectron2

If during building Docker the error docker: Got permission denied issue occurs - just execute the command: sudo chmod 666/var/run/docker.sock

The application will be available here:

public_IP_VM:8080

Help desk

Problems do not arise with the application, but problems may arise when problems arise - Detectron2, torch and so on. Here are some solutions to help you install if you choose to work outside of docker. Tested on MacOs and Windows - on Win - the main problem is the lack ofMicrosoft Visual C++. This collection will help to solve the main problems when installing torch and Detectron2. Also, it is recommended to have Anaconda on board

Problem Description Solution
Web-app Error heroku - Application Error appears The error may be related to out of memory. You can try again to perform the action or take another picture. There will be no problems with local assembly
torch, Detectron C ++ error on Windows when installing on pycatools You do not have Visual Studio C ++ installed - the compiler is missing. Install from here
torch, torchvision outdated version or installed not matching your PC you can check here
torch Torchvision error: Could not find module image.pyd read here with a solution
Docker error with C ++ on assembly or os go to docker -> advanced settings and increase the RAM memory from 2 GB to 4 GB

Summary

Based on the results of the work, it was possible to build two full-fledged modules: a local one with GPU support and work with a group of files and a web version, Which works with single images without the need for setup and installation. This module is flexible enough, it is possible to add new models to it that improve their quality through additional training or overstretch for the needs of business or other individuals through training on their own network. and embedding models by adding new configurations

This module can be used with other models and frameworks, not only detectron - you just need to replace the models in the executable files.

In the local version, the module can be used by advanced developers or interested parties, and in the web version - by users who do not it is important to be proficient with detectors - without significant effort - some result may be obtained. For comfort, Docker support has been implemented and detailed instructions for configuring the module have been added. If you have a desire to implement the application for mobiles - you can use D2go repository here


Bonus In this notebook app you will find a basic style transfer detector. As part of VGG19. intervene the presence of the module did not become - a GPU is required - otherwise the learning process is quite long

Thanks for reading


Credits

  1. Deep learning school - educate and guide.
  2. Arosław Gilewski - CV engineer. Idea of using pipelines and separate background baseline.
  3. Detectron2 - Meta library for working with images
  4. OpenCV - video and image processing tool
  5. Heroku - a fast way to develop and host applications
  6. pyTorch - framework for working with neural networks
  7. Docker - packaging and image transfer tool
  8. MLWhiz - provided sample images for markup