Skip to content

Instantly share code, notes, and snippets.

@rajy4683
Last active May 3, 2020 14:23
Show Gist options
  • Save rajy4683/5883b9d630a96f05f3fe72e6fc20e085 to your computer and use it in GitHub Desktop.
Save rajy4683/5883b9d630a96f05f3fe72e6fc20e085 to your computer and use it in GitHub Desktop.
Sample_S15EVA4_WFlow.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Sample_S15EVA4_WFlow.ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyOhtkWktXievTNBwkI8CWi+",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/rajy4683/5883b9d630a96f05f3fe72e6fc20e085/sample_s15eva4_wflow.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "8TS3SyURpXEy",
"colab_type": "code",
"colab": {}
},
"source": [
"\"\"\"\n",
" Add-on functions to DenseDepth (https://github.com/ialhashim/DenseDepth.git)\n",
"\"\"\"\n",
"\"\"\"\n",
" \n",
" Helper function to load images from a ZipFile\n",
"\n",
"\"\"\"\n",
"def load_execute_names(model, zipfile_orig, name_list,output_dir=\"/content/test\"):\n",
" image_list = [Image.open(BytesIO((zipfile_orig.read(name)))) for name in name_list ] \n",
" inputs = load_images_local(image_list)\n",
" print('Loaded ({0}) images of size {1}.'.format(inputs.shape[0], inputs.shape[1:]))\n",
" outputs = predict(model, inputs)\n",
" converted_img = display_images_new(outputs.copy(),name_list,output_dir=output_dir)\n",
" print(\"Received output shape:{} converted:{}\".format(outputs.shape, converted_img.shape))\n",
"\n",
"\"\"\"\n",
" This function will save the predicted output array as JPEG image under '/content/test'\n",
" Inputs\n",
" outputs: output array \n",
" input_names_list: list of the filenames of the images\n",
" Output:\n",
" Colormapped JPEG files of depth images generated under '/content/test'\n",
" Can be called after the 'predict' function.\n",
" By default it uses the colormap as 'plasma'\n",
"\"\"\"\n",
"\n",
"def display_images_new(outputs, input_names_list,output_dir='/content/test/' ):\n",
" is_colormap=True \n",
" is_rescale=True\n",
" plasma = plt.get_cmap('plasma')\n",
" shape = (outputs[0].shape[0], outputs[0].shape[1], 3)\n",
" \n",
" all_images = []\n",
"\n",
" for i in range(outputs.shape[0]):\n",
" imgs = [] \n",
" if is_colormap:\n",
" rescaled = outputs[i][:,:,0]\n",
" if is_rescale:\n",
" rescaled = rescaled - np.min(rescaled)\n",
" rescaled = rescaled / np.max(rescaled)\n",
" imgs.append(plasma(rescaled)[:,:,:3])\n",
" else:\n",
" imgs.append(to_multichannel(outputs[i]))\n",
"\n",
" img_set = np.hstack(imgs)\n",
" #print(\"Shape:{}\".format(np.array(img_set).shape))\n",
" file_name = os.path.join(output_dir,\"depth_\"+os.path.basename(input_names_list[i]))\n",
" Image.fromarray(np.uint8(np.array(img_set)*255)).save(file_name, 'JPEG',quality=100)\n",
" all_images.append(img_set)"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "Swn6g5eQ5nqG",
"colab_type": "code",
"colab": {}
},
"source": [
"########## Creating defaut output_dir ###########\n",
"!mkdir /content/test"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "fzrB01Pf5tUc",
"colab_type": "code",
"colab": {}
},
"source": [
"#### Sample workflow ######\n",
"\n",
"from DenseDepth import utils #### load_execute_names\n",
"from keras.models import load_model\n",
"from layers import BilinearUpSampling2D\n",
"\n",
"os.environ['TF_CPP_MIN_LOG_LEVEL'] = '5'\n",
"\n",
"\n",
"input_zip = ZipFile(\"/content/drive/My Drive/EVA4/base_images.zip\") ### You can directly read the file from Google-Drive path or from colab. Could't go beyond 300-400 files at a time\n",
"file_list = [ name for name in depth_zip.namelist()][:200]\n",
"\n",
"custom_objects = {'BilinearUpSampling2D': BilinearUpSampling2D, 'depth_loss_function': None}\n",
"print('Loading model...')\n",
"# Load model into GPU / CPU\n",
"model = load_model('/content/nyu.h5', custom_objects=custom_objects, compile=False)\n",
"utils.load_execute_names(model, input_zip, file_list)"
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment