Created
February 19, 2021 03:44
-
-
Save nanlliu/ccaa9beacfbb627b2da15790c0275fa6 to your computer and use it in GitHub Desktop.
clevr_data_generation.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "clevr_data_generation.ipynb", | |
"private_outputs": true, | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyOiaq0p/OYpNIjNqEMZTVCj", | |
"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/nanlliu/ccaa9beacfbb627b2da15790c0275fa6/clevr_data_generation.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "y0RtoucqCRZc" | |
}, | |
"source": [ | |
"from google.colab import drive\n", | |
"drive.mount('/content/gdrive')" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "iumxS_FEDL3q" | |
}, | |
"source": [ | |
"%cd /content/gdrive/MyDrive/clevr-dataset-gen" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ErCuPfVSEpgp" | |
}, | |
"source": [ | |
"%cd /content/gdrive/MyDrive/clevr-dataset-gen/image_generation" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "NT-LW2iBQ2sJ" | |
}, | |
"source": [ | |
"!apt install blender\n", | |
"!apt install libboost-all-dev\n", | |
"!apt install libgl1-mesa-dev" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "ojIwubdnROwZ" | |
}, | |
"source": [ | |
"blender_version = 'blender2.79' #@param [\"blender2.79\", \"blender2.80\", \"blender2.81\", \"blender2.82\", \"blender2.83\", \"blender2.90.1\", \"blender2.91.2\"] {allow-input: false}\n", | |
"gpu_enabled = True #@param {type:\"boolean\"}\n", | |
"cpu_enabled = False #@param {type:\"boolean\"}" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "BxAsAPz9SBUa" | |
}, | |
"source": [ | |
"if blender_version == \"blender2.79\":\n", | |
" download_path=\"https://download.blender.org/release/Blender2.79/blender-2.79-linux-glibc219-x86_64.tar.bz2\"\n", | |
"else:\n", | |
" raise NotImplementedError" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "AAX05CUVYyM9" | |
}, | |
"source": [ | |
"!mkdir $blender_version\n", | |
"if blender_version == \"blender2.79\":\n", | |
" !wget -O '{blender_version}.tar.xz' -nc $download_path\n", | |
" !tar xf '{blender_version}.tar.xz' -C ./$blender_version --strip-components=1\n", | |
"else:\n", | |
" raise NotImplementedError" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "Bw4p_etoY5LE" | |
}, | |
"source": [ | |
"import os\n", | |
"\n", | |
"os.environ[\"LD_PRELOAD\"] = \"\"\n", | |
"\n", | |
"!apt update\n", | |
"!apt remove libtcmalloc-minimal4\n", | |
"!apt install libtcmalloc-minimal4\n", | |
"os.environ[\"LD_PRELOAD\"] = \"/usr/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4.3.0\"\n", | |
"\n", | |
"!echo $LD_PRELOAD" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "mo2mXUKbZPTN" | |
}, | |
"source": [ | |
"!apt install libboost-all-dev\n", | |
"!apt install libgl1-mesa-dev\n", | |
"!apt install libglu1-mesa libsm-dev" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "KG-U_Ym9aGm6" | |
}, | |
"source": [ | |
"%cd /content/gdrive/MyDrive/clevr-dataset-gen/image_generation/ \n", | |
"!echo $PWD >> blender2.79/2.79/python/lib/python3.5/site-packages/clevr.pth" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "zpVm5OzsUJ8h" | |
}, | |
"source": [ | |
"output_image_dir = YOUR_IMAGE_DIR\n", | |
"output_scene_dir = YOUR_SCENE_DIR\n", | |
"output_scene_file_path = YOUR_OVERALL_SCENE_FILE_PATH\n", | |
"output_blend_dir = YOUR_OUTPUT_BLENDER_DIR[OPTIONAL]" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "3Dc2q2_aDrit" | |
}, | |
"source": [ | |
"!sudo ./$blender_version/blender \\\n", | |
"--background --python render_images.py -- --num_images=9 --width=128 --height=128 \\\n", | |
"--render_num_samples=64 --min_objects=1 --max_objects=5 --min_pixels_per_object=100 \\\n", | |
"--output_image_dir=$output_image_dir \\\n", | |
"--output_scene_dir=$output_scene_dir \\\n", | |
"--output_scene_file=$output_scene_file_path \\\n", | |
"--output_blend_dir=$output_blend_dir \\\n", | |
"--min_dist=1 --margin=1 --use_gpu=1 &> /dev/null" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "jMKmx3nNwY5g" | |
}, | |
"source": [ | |
"import os\n", | |
"import json\n", | |
"import numpy as np\n", | |
"from PIL import Image\n", | |
"from matplotlib import pyplot as plt\n", | |
"\n", | |
"img_id = 0\n", | |
"img_path = os.path.join(output_image_dir, 'CLEVR_new_{:06}.png'.format(img_id))\n", | |
"im = Image.open(img_path).convert('RGB')\n", | |
"im = np.array(im)\n", | |
"\n", | |
"scene_path = os.path.join(output_scene_dir, 'CLEVR_new_{:06}.json'.format(img_id))\n", | |
"with open(scene_path, 'r') as f:\n", | |
" scene = json.load(f)\n", | |
"\n", | |
"obj_names = []\n", | |
"for obj in scene['objects']:\n", | |
" name = ' '.join([obj['size'], obj['color'], obj['material'], obj['shape']])\n", | |
" obj_names.append(name)\n", | |
"\n", | |
"for relation, indices in scene['relationships'].items():\n", | |
" for i, js in enumerate(indices):\n", | |
" for j in js:\n", | |
" print(obj_names[j], relation, obj_names[i])\n", | |
"\n", | |
"if len(obj_names) == 1:\n", | |
" print(obj_names[0])\n", | |
"\n", | |
"plt.figure()\n", | |
"plt.imshow(im)\n", | |
"plt.show()" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "8NDj0rURX0c1" | |
}, | |
"source": [ | |
"from PIL import Image\n", | |
"import torchvision\n", | |
"import torch\n", | |
"import numpy as np\n", | |
"from matplotlib import pyplot as plt\n", | |
"\n", | |
"images = []\n", | |
"\n", | |
"for i in range(9):\n", | |
" img_path = os.path.join(output_image_dir, 'CLEVR_new_{:06}.png'.format(i))\n", | |
" im = Image.open(img_path).convert('RGB')\n", | |
" im = np.array(im).transpose(2, 0, 1)\n", | |
" images.append(im)\n", | |
"\n", | |
"images = torch.from_numpy(np.array(images))\n", | |
"grid = torchvision.utils.make_grid(images, nrow=3)\n", | |
"\n", | |
"plt.figure(figsize = (20,20))\n", | |
"plt.imshow(grid.permute(1, 2, 0), interpolation='nearest')" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, thanks for providing the convenient colab code. I tried to run your code to render Clevr images, however after following the steps and to the point where I ran this chunk:
!sudo ./$blender_version/blender
--background --python render_images.py -- --num_images=9 --width=128 --height=128
--render_num_samples=64 --min_objects=1 --max_objects=5 --min_pixels_per_object=100
--output_image_dir= a
output_scene_dir
--output_scene_file= b
output_blend_dir
--min_dist=1 --margin=1 --use_gpu=1 &> /dev/null
it actually didn't render any images at all, but it didn't prompt me any error messages either, could you assist me with this issue? Thanks.