Skip to content

Commit

Permalink
added correct imports and function definitions to neural-style-pytorch
Browse files Browse the repository at this point in the history
  • Loading branch information
bckenstler committed Aug 16, 2017
1 parent df3cc11 commit 20e8524
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 36 deletions.
78 changes: 44 additions & 34 deletions deeplearning2/neural-style-pytorch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@
"%matplotlib inline\n",
"import importlib\n",
"import utils2; importlib.reload(utils2)\n",
"from utils2 import *"
"from utils2 import *\n",
"from vgg16_avg import VGG16_Avg\n",
"from keras import metrics\n",
"from scipy.optimize import fmin_l_bfgs_b\n",
"from scipy.misc import imsave"
]
},
{
Expand Down Expand Up @@ -151,7 +155,7 @@
},
"outputs": [],
"source": [
"deproc = lambda x: x[:, :, :, ::-1] + rn_mean"
"deproc = lambda x,s: np.clip(x.reshape(s)[:, :, :, ::-1] + rn_mean, 0, 255)"
]
},
{
Expand Down Expand Up @@ -1027,9 +1031,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"heading_collapsed": true
},
"metadata": {},
"source": [
"### Style transfer"
]
Expand All @@ -1038,8 +1040,7 @@
"cell_type": "code",
"execution_count": 807,
"metadata": {
"collapsed": true,
"hidden": true
"collapsed": true
},
"outputs": [],
"source": [
Expand All @@ -1054,8 +1055,7 @@
"cell_type": "code",
"execution_count": 994,
"metadata": {
"collapsed": false,
"hidden": true
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -1069,8 +1069,7 @@
"cell_type": "code",
"execution_count": 995,
"metadata": {
"collapsed": false,
"hidden": true
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -1082,8 +1081,7 @@
"cell_type": "code",
"execution_count": 996,
"metadata": {
"collapsed": false,
"hidden": true
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -1094,8 +1092,7 @@
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": true,
"hidden": true
"collapsed": true
},
"outputs": [],
"source": [
Expand All @@ -1106,8 +1103,7 @@
"cell_type": "code",
"execution_count": 1362,
"metadata": {
"collapsed": true,
"hidden": true
"collapsed": true
},
"outputs": [],
"source": [
Expand All @@ -1118,8 +1114,7 @@
"cell_type": "code",
"execution_count": 998,
"metadata": {
"collapsed": false,
"hidden": true
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -1130,8 +1125,7 @@
"cell_type": "code",
"execution_count": 1004,
"metadata": {
"collapsed": false,
"hidden": true
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -1146,8 +1140,7 @@
"cell_type": "code",
"execution_count": 1005,
"metadata": {
"collapsed": false,
"hidden": true
"collapsed": false
},
"outputs": [],
"source": [
Expand All @@ -1158,8 +1151,7 @@
"cell_type": "code",
"execution_count": 1006,
"metadata": {
"collapsed": true,
"hidden": true
"collapsed": true
},
"outputs": [],
"source": [
Expand All @@ -1171,8 +1163,7 @@
"cell_type": "code",
"execution_count": 1010,
"metadata": {
"collapsed": false,
"hidden": true
"collapsed": false
},
"outputs": [
{
Expand Down Expand Up @@ -1200,8 +1191,7 @@
"cell_type": "code",
"execution_count": 1011,
"metadata": {
"collapsed": false,
"hidden": true
"collapsed": false
},
"outputs": [
{
Expand All @@ -1225,7 +1215,6 @@
"execution_count": 932,
"metadata": {
"collapsed": false,
"hidden": true,
"scrolled": true
},
"outputs": [
Expand All @@ -1250,7 +1239,6 @@
"execution_count": 914,
"metadata": {
"collapsed": false,
"hidden": true,
"scrolled": true
},
"outputs": [
Expand Down Expand Up @@ -1289,6 +1277,28 @@
"inp = Input(inp_shape)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"class ReflectionPadding2D(Layer):\n",
" def __init__(self, padding=(1, 1), **kwargs):\n",
" self.padding = tuple(padding)\n",
" self.input_spec = [InputSpec(ndim=4)]\n",
" super(ReflectionPadding2D, self).__init__(**kwargs)\n",
" \n",
" def get_output_shape_for(self, s):\n",
" return (s[0], s[1] + 2 * self.padding[0], s[2] + 2 * self.padding[1], s[3])\n",
"\n",
" def call(self, x, mask=None):\n",
" w_pad,h_pad = self.padding\n",
" return tf.pad(x, [[0,0], [h_pad,h_pad], [w_pad,w_pad], [0,0] ], 'REFLECT')"
]
},
{
"cell_type": "code",
"execution_count": 1224,
Expand All @@ -1309,7 +1319,7 @@
},
"outputs": [],
"source": [
"p = ref_model.predict(arr[50:51])"
"p = ref_model.predict(arr_lr[50:51])"
]
},
{
Expand Down Expand Up @@ -1470,7 +1480,7 @@
"source": [
"loss = Lambda(lambda x: K.sqrt(K.mean((x[0]-x[1])**2, (1,2))))([vgg1, vgg2])\n",
"m_final = Model([inp, vgg_inp], loss)\n",
"targ = np.zeros((arr.shape[0], 128))"
"targ = np.zeros((arr_lr.shape[0], 128))"
]
},
{
Expand Down Expand Up @@ -1766,7 +1776,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
"version": "3.6.2"
}
},
"nbformat": 4,
Expand Down
4 changes: 2 additions & 2 deletions deeplearning2/neural-style.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4676,7 +4676,7 @@
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -4690,7 +4690,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.0"
"version": "3.6.2"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 20e8524

Please sign in to comment.