Skip to content

Instantly share code, notes, and snippets.

@revantteotia
Created December 26, 2020 17:59
Show Gist options
  • Save revantteotia/3d92e43add3c395ef45c5da3d7f664e9 to your computer and use it in GitHub Desktop.
Save revantteotia/3d92e43add3c395ef45c5da3d7f664e9 to your computer and use it in GitHub Desktop.
frcnn-feat-extraction.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "frcnn-feat-extraction.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyNoDeJQAfq5I2Cs3MRbaVsy",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/revantteotia/3d92e43add3c395ef45c5da3d7f664e9/frcnn-feat-extraction.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "pdf9oK5CLC_4"
},
"source": [
"!pip install yacs"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "sumGR0VvIaRH"
},
"source": [
"import sys"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "lNZls5pTHvnd"
},
"source": [
"installing m4c branch of mmf"
]
},
{
"cell_type": "code",
"metadata": {
"id": "lR5MHluMGzHR"
},
"source": [
"%cd /content/\n",
"%rm -rf mmf\n",
"!git clone https://github.com/facebookresearch/mmf.git mmf\n",
"%cd /content/mmf\n",
"!git checkout project/m4c # checkingout m4c project branch\n",
"# Don't modify torch version\n",
"!sed -i '/torch/d' requirements.txt\n",
"!pip install -e .\n",
"import sys\n",
"sys.path.append(\"/content/mmf\")"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "DIOPlVXAH2KU"
},
"source": [
"Installing maskrcnn-benchmark : FRCNN Model to extract features"
]
},
{
"cell_type": "code",
"metadata": {
"id": "w5YjUT3aIWVo"
},
"source": [
"%cd /content\n",
"!git clone https://gitlab.com/meetshah1995/vqa-maskrcnn-benchmark.git\n",
"%cd /content/vqa-maskrcnn-benchmark\n",
"# Compile custom layers and build mask-rcnn backbone\n",
"!python setup.py build\n",
"!python setup.py develop\n",
"sys.path.append('/content/vqa-maskrcnn-benchmark')"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "EGdYt2H1Ilxa"
},
"source": [
"# creating directories for data\n",
"%mkdir /content/mmf/data\n",
"%mkdir /content/mmf/data/imdb"
],
"execution_count": 5,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "fWOMoEOnInhz"
},
"source": [
"# Download detectron weights (pre-trained)\n",
"%cd /content/mmf/data\n",
"!wget http://dl.fbaipublicfiles.com/pythia/data/detectron_weights.tar.gz\n",
"!tar xf detectron_weights.tar.gz\n",
"\n",
"!wget -O /content/mmf/data/detectron_model.pth https://dl.fbaipublicfiles.com/pythia/detectron_model/detectron_model.pth\n",
"!wget -O /content/mmf/data/detectron_model.yaml https://dl.fbaipublicfiles.com/pythia/detectron_model/detectron_model.yaml"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "xuXesaNjIxfh"
},
"source": [
"Now extracting features"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "BLw_Wi7aJKF8",
"outputId": "11cc9214-6c7a-4494-9309-2a385ada93e8"
},
"source": [
"# put images in /content/test_images \n",
"# get output in /content/frcnn_out\n",
"!python /content/mmf/pythia/scripts/features/extract_features_vmb.py --model_file /content/mmf/data/detectron_model.pth --config_file /content/mmf/data/detectron_model.yaml --image_dir /content/test_images --output_folder /content/frcnn_out"
],
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": [
"/content/vqa-maskrcnn-benchmark/maskrcnn_benchmark/structures/boxlist_ops.py:45: UserWarning: This overload of nonzero is deprecated:\n",
"\tnonzero()\n",
"Consider using one of the following signatures instead:\n",
"\tnonzero(*, bool as_tuple) (Triggered internally at /pytorch/torch/csrc/utils/python_arg_parser.cpp:882.)\n",
" keep = ((ws >= min_size) & (hs >= min_size)).nonzero().squeeze(1)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "xUB7H90sJyEZ"
},
"source": [
"Ignore the cells below\n",
"code to unzip test images"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OtX_v9AzJvKR",
"outputId": "f8028a35-3536-414d-9636-ed34a0571283"
},
"source": [
"# unzipping test images\n",
"# first create test_images directory\n",
"%rm -r /content/test_images\n",
"%mkdir /content/test_images\n",
"\n",
"#now unzip images\n",
"!unzip -j /content/test_images.zip -d /content/test_images # -j to remove directory structure"
],
"execution_count": 7,
"outputs": [
{
"output_type": "stream",
"text": [
"rm: cannot remove '/content/test_images': No such file or directory\n"
],
"name": "stdout"
}
]
}
]
}
@revantteotia
Copy link
Author

This notebooks uses fb's mmf and detectron to extract object detection features (frcnn features, bbox, obj class probabilities).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment