Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed error #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 17 additions & 75 deletions Siamese on Omniglot Dataset.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"C:\\ProgramData\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
" from ._conv import register_converters as _register_converters\n",
"Using TensorFlow backend.\n"
]
}
],
"outputs": [],
"source": [
"import sys\n",
"import numpy as np\n",
Expand All @@ -29,6 +19,7 @@
"import time\n",
"\n",
"import tensorflow as tf\n",
"from keras.initializers import RandomNormal\n",
"from keras.models import Sequential\n",
"from keras.optimizers import Adam\n",
"from keras.layers import Conv2D, ZeroPadding2D, Activation, Input, concatenate\n",
Expand Down Expand Up @@ -282,35 +273,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def initialize_weights(shape, name=None):\n",
" \"\"\"\n",
" The paper, http://www.cs.utoronto.ca/~gkoch/files/msc-thesis.pdf\n",
" suggests to initialize CNN layer weights with mean as 0.0 and standard deviation of 0.01\n",
" \"\"\"\n",
" return np.random.normal(loc = 0.0, scale = 1e-2, size = shape)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def initialize_bias(shape, name=None):\n",
" \"\"\"\n",
" The paper, http://www.cs.utoronto.ca/~gkoch/files/msc-thesis.pdf\n",
" suggests to initialize CNN layer bias with mean as 0.5 and standard deviation of 0.01\n",
" \"\"\"\n",
" return np.random.normal(loc = 0.5, scale = 1e-2, size = shape)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -326,21 +289,20 @@
" # Convolutional Neural Network\n",
" model = Sequential()\n",
" model.add(Conv2D(64, (10,10), activation='relu', input_shape=input_shape,\n",
" kernel_initializer=initialize_weights, kernel_regularizer=l2(2e-4)))\n",
" kernel_initializer= RandomNormal(mean=0.0, stddev=0.01, seed=1), kernel_regularizer=l2(2e-4)))\n",
" model.add(MaxPooling2D())\n",
" model.add(Conv2D(128, (7,7), activation='relu',\n",
" kernel_initializer=initialize_weights,\n",
" bias_initializer=initialize_bias, kernel_regularizer=l2(2e-4)))\n",
" kernel_initializer=RandomNormal(mean=0.0, stddev=0.01, seed=1),\n",
" bias_initializer=RandomNormal(mean=0.5, stddev=0.01, seed=1), kernel_regularizer=l2(2e-4)))\n",
" model.add(MaxPooling2D())\n",
" model.add(Conv2D(128, (4,4), activation='relu', kernel_initializer=initialize_weights,\n",
" bias_initializer=initialize_bias, kernel_regularizer=l2(2e-4)))\n",
" model.add(Conv2D(128, (4,4), activation='relu', kernel_initializer=RandomNormal(mean=0.0, stddev=0.05, seed=1) ,bias_initializer=RandomNormal(mean=0.5, stddev=0.01, seed=1), kernel_regularizer=l2(2e-4)))\n",
" model.add(MaxPooling2D())\n",
" model.add(Conv2D(256, (4,4), activation='relu', kernel_initializer=initialize_weights,\n",
" bias_initializer=initialize_bias, kernel_regularizer=l2(2e-4)))\n",
" model.add(Conv2D(256, (4,4), activation='relu', kernel_initializer=RandomNormal(mean=0.0, stddev=0.05, seed=1) ,bias_initializer=RandomNormal(mean=0.5, stddev=0.01, seed=1), kernel_regularizer=l2(2e-4)))\n",
" model.add(Flatten())\n",
" model.add(Dense(4096, activation='sigmoid',\n",
" kernel_regularizer=l2(1e-3),\n",
" kernel_initializer=initialize_weights,bias_initializer=initialize_bias))\n",
" kernel_initializer=RandomNormal(mean=0.0, stddev=0.01, seed=1),\n",
" bias_initializer=RandomNormal(mean=0.5, stddev=0.01, seed=1)))\n",
" \n",
" # Generate the encodings (feature vectors) for the two images\n",
" encoded_l = model(left_input)\n",
Expand All @@ -351,7 +313,7 @@
" L1_distance = L1_layer([encoded_l, encoded_r])\n",
" \n",
" # Add a dense layer with a sigmoid unit to generate the similarity score\n",
" prediction = Dense(1,activation='sigmoid',bias_initializer=initialize_bias)(L1_distance)\n",
" prediction = Dense(1,activation='sigmoid',bias_initializer=RandomNormal(mean=0.5, stddev=0.01, seed=1))(L1_distance)\n",
" \n",
" # Connect the inputs with the outputs\n",
" siamese_net = Model(inputs=[left_input,right_input],outputs=prediction)\n",
Expand All @@ -362,33 +324,13 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"__________________________________________________________________________________________________\n",
"Layer (type) Output Shape Param # Connected to \n",
"==================================================================================================\n",
"input_1 (InputLayer) (None, 105, 105, 1) 0 \n",
"__________________________________________________________________________________________________\n",
"input_2 (InputLayer) (None, 105, 105, 1) 0 \n",
"__________________________________________________________________________________________________\n",
"sequential_1 (Sequential) (None, 4096) 38947648 input_1[0][0] \n",
" input_2[0][0] \n",
"__________________________________________________________________________________________________\n",
"lambda_1 (Lambda) (None, 4096) 0 sequential_1[1][0] \n",
" sequential_1[2][0] \n",
"__________________________________________________________________________________________________\n",
"dense_2 (Dense) (None, 1) 4097 lambda_1[0][0] \n",
"==================================================================================================\n",
"Total params: 38,951,745\n",
"Trainable params: 38,951,745\n",
"Non-trainable params: 0\n",
"__________________________________________________________________________________________________\n"
]
"name": "stdout",
"text": "WARNING:tensorflow:From C:\\Users\\Hemanth\\AppData\\Local\\Programs\\Python\\Python36\\lib\\site-packages\\keras\\backend\\tensorflow_backend.py:4070: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.\n\nModel: \"model_1\"\n__________________________________________________________________________________________________\nLayer (type) Output Shape Param # Connected to \n==================================================================================================\ninput_7 (InputLayer) (None, 105, 105, 1) 0 \n__________________________________________________________________________________________________\ninput_8 (InputLayer) (None, 105, 105, 1) 0 \n__________________________________________________________________________________________________\nsequential_4 (Sequential) (None, 4096) 38947648 input_7[0][0] \n input_8[0][0] \n__________________________________________________________________________________________________\nlambda_1 (Lambda) (None, 4096) 0 sequential_4[1][0] \n sequential_4[2][0] \n__________________________________________________________________________________________________\ndense_2 (Dense) (None, 1) 4097 lambda_1[0][0] \n==================================================================================================\nTotal params: 38,951,745\nTrainable params: 38,951,745\nNon-trainable params: 0\n__________________________________________________________________________________________________\n"
}
],
"source": [
Expand Down Expand Up @@ -1996,9 +1938,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
"version": "3.6.5-final"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
}