Skip to content

Instantly share code, notes, and snippets.

@briandw
Created December 3, 2017 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briandw/af88a930318926f5bda3f6f4b622919f to your computer and use it in GitHub Desktop.
Save briandw/af88a930318926f5bda3f6f4b622919f to your computer and use it in GitHub Desktop.
25lights part II CNN
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"from keras.layers import Flatten, Input, Dense, Conv2D, MaxPooling2D, AveragePooling2D, ZeroPadding2D, Dropout, BatchNormalization\n",
"from keras.layers.core import Flatten, Dense, Dropout, Lambda\n",
"from keras.models import Model\n",
"from keras import backend as K\n",
"\n",
"# we are using square images 128x128\n",
"imageSize = 128\n",
"# 5 x 5 dot array\n",
"dotsPerSide = 5\n",
"#our loss function will be the 25bit pattern used to create the image\n",
"encoding_dim = dotsPerSide * dotsPerSide\n",
"\n",
"# this is our input placeholder\n",
"input_img = Input(shape=(imageSize, imageSize, 1))\n",
"\n",
"#a convolution layer with padding and pooling functions\n",
"def ConvBlock(input, layers, filters):\n",
" for i in range(layers):\n",
" x = ZeroPadding2D()(input)\n",
" x = Conv2D(filters, (3, 3), activation='relu')(x)\n",
" \n",
" return AveragePooling2D((2, 2), strides=(2, 2))(x)\n",
"\n",
"#a fully connected block and our normalization function\n",
"def FCBlock(input):\n",
" x = Dense(4096, activation='relu')(input)\n",
" x = BatchNormalization()(x)\n",
" return Dropout(0.5)(x)\n",
"\n",
"#use a stack of convolutional layers, like VGG16\n",
"x = ConvBlock(input_img, 2, 64)\n",
"#x = ConvBlock(x, 2, 64)\n",
"x = ConvBlock(x, 2, 128)\n",
"x = ConvBlock(x, 3, 256)\n",
"x = ConvBlock(x, 3, 512)\n",
"#flatten out the convolutional layers, then regularize with batch normalization\n",
"x = Flatten()(x)\n",
"\n",
"x = FCBlock(x)\n",
"#x = FCBlock(x)\n",
"\n",
"# \"loss\" is the bit array for each image\n",
"loss = Dense(encoding_dim, activation='sigmoid')(x)\n",
"\n",
"model = Model(input_img, loss)\n",
"model.compile(optimizer='adadelta', loss='binary_crossentropy')\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment