Created
October 17, 2016 18:56
-
-
Save YoelShoshan/3ed8f827685d130cd01fbf787821ee87 to your computer and use it in GitHub Desktop.
reproducible image_dim_ordering convergence issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras import backend as K | |
import keras.optimizers | |
from keras.layers import Convolution2D, MaxPooling2D | |
from keras.layers import Activation, Dropout, Flatten, Dense, Input | |
from keras.models import Model | |
import numpy as np | |
def make_model(input_dim_size): | |
if K.image_dim_ordering() == 'tf': | |
input_shape = (input_dim_size, input_dim_size,1) | |
else: | |
input_shape = (1, input_dim_size, input_dim_size) | |
img_input = Input(shape=input_shape) | |
x = Convolution2D(64,5,5,border_mode='same')(img_input) | |
x = Activation('relu')(x) | |
x = MaxPooling2D((2,2),strides=(2,2))(x) | |
x = Convolution2D(64, 5, 5, border_mode='same')(x) | |
x = Activation('relu')(x) | |
x = MaxPooling2D((2, 2), strides=(2, 2))(x) | |
x = Convolution2D(64, 5, 5, border_mode='same')(x) | |
x = Activation('relu')(x) | |
x = MaxPooling2D((2, 2), strides=(2, 2))(x) | |
x = Convolution2D(128, 5, 5, border_mode='same')(x) | |
x = Activation('relu')(x) | |
x = MaxPooling2D((2, 2), strides=(2, 2))(x) | |
x = Convolution2D(128, 5, 5, border_mode='same')(x) | |
x = Activation('relu')(x) | |
x = MaxPooling2D((2, 2), strides=(2, 2))(x) | |
x = Flatten()(x) | |
x = Dense(1024*2)(x) | |
x = Activation('relu')(x) | |
x = Dropout(0.5)(x) | |
x = Dense(1024 * 2)(x) | |
x = Activation('relu')(x) | |
x = Dropout(0.75)(x) | |
x = Dense(200)(x) | |
x = Activation('relu')(x) | |
x = Dropout(0.75)(x) | |
x = Dense(1,activation='sigmoid')(x) | |
model = Model(img_input, x) | |
learning_rate = 0.01 | |
sgd = keras.optimizers.sgd(lr=learning_rate, momentum=0.9, nesterov=True) | |
model.summary() | |
model.compile(loss='binary_crossentropy', | |
optimizer=sgd, | |
metrics=['accuracy'] | |
) | |
return model | |
np.random.seed(456) | |
def dummy_generator(mini_batch_size=64, block_size=100): | |
if K.image_dim_ordering() == 'tf': | |
tensor_X_shape = (mini_batch_size,block_size, block_size,1) | |
else: | |
tensor_X_shape = (mini_batch_size, 1, block_size, block_size) | |
X = np.zeros(tensor_X_shape, dtype=np.float32) | |
y = np.zeros((mini_batch_size, 1)) | |
while True: | |
for b in range(mini_batch_size): | |
X[b, :, :, :] = (float(b % 2) * 2.0) - 1.0 | |
y[b, :] = float(b % 2) | |
yield X,y | |
with K.tf.device('/gpu:0'): | |
K.set_session(K.tf.Session(config=K.tf.ConfigProto(allow_soft_placement=True, log_device_placement=False))) | |
MINI_BATCH_SIZE = 64 | |
PATCH_SIZE = 100 | |
model = make_model(PATCH_SIZE) | |
gen = dummy_generator(mini_batch_size=MINI_BATCH_SIZE,block_size=PATCH_SIZE) | |
model.fit_generator(gen, MINI_BATCH_SIZE*10, | |
100, verbose=1, | |
callbacks=[], | |
validation_data=None, | |
nb_val_samples=None, | |
max_q_size=1, | |
nb_worker=1, pickle_safe=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment