Skip to content

Instantly share code, notes, and snippets.

@kolygri
Last active October 13, 2021 01:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kolygri/835ccea6b87089fbfd64395c3895c01f to your computer and use it in GitHub Desktop.
Save kolygri/835ccea6b87089fbfd64395c3895c01f to your computer and use it in GitHub Desktop.
Untitled1.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Untitled1.ipynb",
"version": "0.3.2",
"provenance": [],
"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/kolygri/835ccea6b87089fbfd64395c3895c01f/untitled1.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"metadata": {
"id": "ARW5-hBQjRsg",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 401
},
"outputId": "ec25134d-1834-4b00-dae7-a9adf8fc9e6d"
},
"cell_type": "code",
"source": [
"!wget https://www.dropbox.com/s/llpxd14is7gyj0z/model.h5"
],
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": [
"--2018-11-08 18:19:55-- https://www.dropbox.com/s/llpxd14is7gyj0z/model.h5\n",
"Resolving www.dropbox.com (www.dropbox.com)... 162.125.81.1, 2620:100:6031:1::a27d:5101\n",
"Connecting to www.dropbox.com (www.dropbox.com)|162.125.81.1|:443... connected.\n",
"HTTP request sent, awaiting response... 301 Moved Permanently\n",
"Location: /s/raw/llpxd14is7gyj0z/model.h5 [following]\n",
"--2018-11-08 18:19:56-- https://www.dropbox.com/s/raw/llpxd14is7gyj0z/model.h5\n",
"Reusing existing connection to www.dropbox.com:443.\n",
"HTTP request sent, awaiting response... 302 Found\n",
"Location: https://uc0ba05977b21e25b31c6a99eb48.dl.dropboxusercontent.com/cd/0/inline/AVFRsZUq18652t8HGckXeC1NauGw2Zl9WFLCIDG6WRw1Iegdsp78ecq1v97ojBNIElAnOPybX-zThUFOHxwGboTZjOlDrSNBHwmw4huETPCQTEgKIXdbB9rG6254W44TH2-uJCOgomQuwLnkwdR9bUdpTcFWnyH5FCrCYygZ0OnftcAKfbh42IHnDoyFo-Om_bE/file [following]\n",
"--2018-11-08 18:19:56-- https://uc0ba05977b21e25b31c6a99eb48.dl.dropboxusercontent.com/cd/0/inline/AVFRsZUq18652t8HGckXeC1NauGw2Zl9WFLCIDG6WRw1Iegdsp78ecq1v97ojBNIElAnOPybX-zThUFOHxwGboTZjOlDrSNBHwmw4huETPCQTEgKIXdbB9rG6254W44TH2-uJCOgomQuwLnkwdR9bUdpTcFWnyH5FCrCYygZ0OnftcAKfbh42IHnDoyFo-Om_bE/file\n",
"Resolving uc0ba05977b21e25b31c6a99eb48.dl.dropboxusercontent.com (uc0ba05977b21e25b31c6a99eb48.dl.dropboxusercontent.com)... 162.125.9.6, 2620:100:601f:6::a27d:906\n",
"Connecting to uc0ba05977b21e25b31c6a99eb48.dl.dropboxusercontent.com (uc0ba05977b21e25b31c6a99eb48.dl.dropboxusercontent.com)|162.125.9.6|:443... connected.\n",
"HTTP request sent, awaiting response... 200 OK\n",
"Length: 209602136 (200M) [text/plain]\n",
"Saving to: ‘model.h5.2’\n",
"\n",
"model.h5.2 100%[===================>] 199.89M 34.7MB/s in 6.2s \n",
"\n",
"2018-11-08 18:20:04 (32.4 MB/s) - ‘model.h5.2’ saved [209602136/209602136]\n",
"\n"
],
"name": "stdout"
}
]
},
{
"metadata": {
"id": "-FTJHcPqjYad",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
},
"outputId": "d407d864-6d55-4a80-96c0-3eebe75266f8"
},
"cell_type": "code",
"source": [
"import keras\n",
"from keras.models import Sequential\n",
"from keras.models import Model\n",
"from keras.layers import Input, Dense, Activation, Lambda\n",
"from keras.layers.convolutional import Conv2D\n",
"from keras.layers.pooling import MaxPooling2D\n",
"from keras.layers.normalization import BatchNormalization\n",
"from keras.layers.merge import Concatenate\n",
"import scipy\n",
"import math"
],
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"text": [
"Using TensorFlow backend.\n"
],
"name": "stderr"
}
]
},
{
"metadata": {
"id": "h8yUU_0JjZ3M",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"def relu(x): \n",
" return Activation('relu')(x)\n",
"\n",
"def conv(x, nf, ks, name):\n",
" x1 = Conv2D(nf, (ks, ks), padding='same', name=name)(x)\n",
" return x1\n",
"\n",
"def pooling(x, ks, st, name):\n",
" x = MaxPooling2D((ks, ks), strides=(st, st), name=name)(x)\n",
" return x\n",
"\n",
"def vgg_block(x):\n",
" \n",
" # Block 1\n",
" x = conv(x, 64, 3, \"conv1_1\")\n",
" x = relu(x)\n",
" x = conv(x, 64, 3, \"conv1_2\")\n",
" x = relu(x)\n",
" x = pooling(x, 2, 2, \"pool1_1\")\n",
"\n",
" # Block 2\n",
" x = conv(x, 128, 3, \"conv2_1\")\n",
" x = relu(x)\n",
" x = conv(x, 128, 3, \"conv2_2\")\n",
" x = relu(x)\n",
" x = pooling(x, 2, 2, \"pool2_1\")\n",
" \n",
" # Block 3\n",
" x = conv(x, 256, 3, \"conv3_1\")\n",
" x = relu(x) \n",
" x = conv(x, 256, 3, \"conv3_2\")\n",
" x = relu(x) \n",
" x = conv(x, 256, 3, \"conv3_3\")\n",
" x = relu(x) \n",
" x = conv(x, 256, 3, \"conv3_4\")\n",
" x = relu(x) \n",
" x = pooling(x, 2, 2, \"pool3_1\")\n",
" \n",
" # Block 4\n",
" x = conv(x, 512, 3, \"conv4_1\")\n",
" x = relu(x) \n",
" x = conv(x, 512, 3, \"conv4_2\")\n",
" x = relu(x) \n",
" \n",
" # Additional non vgg layers\n",
" x = conv(x, 256, 3, \"conv4_3_CPM\")\n",
" x = relu(x)\n",
" x = conv(x, 128, 3, \"conv4_4_CPM\")\n",
" x = relu(x)\n",
" \n",
" return x\n",
"\n",
"def stage1_block(x, num_p, branch):\n",
" \n",
" # Block 1 \n",
" x = conv(x, 128, 3, \"conv5_1_CPM_L%d\" % branch)\n",
" x = relu(x)\n",
" x = conv(x, 128, 3, \"conv5_2_CPM_L%d\" % branch)\n",
" x = relu(x)\n",
" x = conv(x, 128, 3, \"conv5_3_CPM_L%d\" % branch)\n",
" x = relu(x)\n",
" x = conv(x, 512, 1, \"conv5_4_CPM_L%d\" % branch)\n",
" x = relu(x)\n",
" x = conv(x, num_p, 1, \"conv5_5_CPM_L%d\" % branch)\n",
" \n",
" return x\n",
"\n",
"def stageT_block(x, num_p, stage, branch):\n",
" \n",
" # Block 1 \n",
" x = conv(x, 128, 7, \"Mconv1_stage%d_L%d\" % (stage, branch))\n",
" x = relu(x)\n",
" x = conv(x, 128, 7, \"Mconv2_stage%d_L%d\" % (stage, branch))\n",
" x = relu(x)\n",
" x = conv(x, 128, 7, \"Mconv3_stage%d_L%d\" % (stage, branch))\n",
" x = relu(x)\n",
" x = conv(x, 128, 7, \"Mconv4_stage%d_L%d\" % (stage, branch))\n",
" x = relu(x)\n",
" x = conv(x, 128, 7, \"Mconv5_stage%d_L%d\" % (stage, branch))\n",
" x = relu(x)\n",
" x = conv(x, 128, 1, \"Mconv6_stage%d_L%d\" % (stage, branch))\n",
" x = relu(x)\n",
" x = conv(x, num_p, 1, \"Mconv7_stage%d_L%d\" % (stage, branch))\n",
" \n",
" return x"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "oim1gxVkjgq8",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"weights_path = \"model.h5\" # orginal weights converted from caffe\n",
"#weights_path = \"training/weights.best.h5\" # weights tarined from scratch \n",
"\n",
"input_shape = (None,None,3)\n",
"\n",
"img_input = Input(shape=input_shape)\n",
"\n",
"stages = 6\n",
"np_branch1 = 38\n",
"np_branch2 = 19\n",
"\n",
"img_normalized = Lambda(lambda x: x / 256 - 0.5)(img_input) # [-0.5, 0.5]\n",
"\n",
"# VGG\n",
"stage0_out = vgg_block(img_normalized)\n",
"\n",
"# stage 1\n",
"stage1_branch1_out = stage1_block(stage0_out, np_branch1, 1)\n",
"stage1_branch2_out = stage1_block(stage0_out, np_branch2, 2)\n",
"x = Concatenate()([stage1_branch1_out, stage1_branch2_out, stage0_out])\n",
"\n",
"# stage t >= 2\n",
"for sn in range(2, stages + 1):\n",
" stageT_branch1_out = stageT_block(x, np_branch1, sn, 1)\n",
" stageT_branch2_out = stageT_block(x, np_branch2, sn, 2)\n",
" if (sn < stages):\n",
" x = Concatenate()([stageT_branch1_out, stageT_branch2_out, stage0_out])\n",
"\n",
"model = Model(img_input, [stageT_branch1_out, stageT_branch2_out])\n",
"model.load_weights(weights_path)"
],
"execution_count": 0,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment