Example #1
0
def vgg_std16_model(img_rows, img_cols, color_type=1):
    model = Sequential()
    model.add(ZeroPadding2D((1, 1), input_shape=(color_type,
                                                 img_rows, img_cols)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(Flatten())
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1000, activation='softmax'))

    model.load_weights('../input/vgg16_weights.h5')

    # Code above loads pre-trained data and
    model.layers.pop()
    model.outputs = [model.layers[-1].output]
    model.layers[-1].outbound_nodes = []
    model.add(Dense(10, activation='softmax'))
    # Learning rate is changed to 0.001
    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, loss='categorical_crossentropy')
    return model
Example #2
0
def VGG_16_Terrassa(weights_path=""):
	model = Sequential()
	model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))
	model.add(Convolution2D(64, 3, 3, activation='relu',trainable = False))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(64, 3, 3, activation='relu',trainable = False))
	model.add(MaxPooling2D((2,2), strides=(2,2)))

	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(128, 3, 3, activation='relu',trainable = False))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(128, 3, 3, activation='relu',trainable = False))
	model.add(MaxPooling2D((2,2), strides=(2,2)))

	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(256, 3, 3, activation='relu',trainable = False))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(256, 3, 3, activation='relu',trainable = False))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(256, 3, 3, activation='relu',trainable = False))
	model.add(MaxPooling2D((2,2), strides=(2,2)))

	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(512, 3, 3, activation='relu',trainable = False))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(512, 3, 3, activation='relu',trainable = False))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(512, 3, 3, activation='relu',trainable = False))
	model.add(MaxPooling2D((2,2), strides=(2,2)))

	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(512, 3, 3, activation='relu',trainable = False))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(512, 3, 3, activation='relu',trainable = False))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(512, 3, 3, activation='relu',trainable = False))
	model.add(MaxPooling2D((2,2), strides=(2,2)))

	model.add(Flatten())
	model.add(Dense(4096, activation='relu',trainable = False))
	model.add(Dropout(0.5))
	model.add(Dense(4096, activation='relu',trainable = False))
	model.add(Dropout(0.5))
	model.add(Dense(1000, activation='softmax'))

	if weights_path:
		print ("Weights Loaded")
		model.load_weights(weights_path)

	model.layers.pop()
	model.layers.pop()
	model.outputs = [model.layers[-1].output]
	model.layers[-1].outbound_nodes = []
	model.add(Dense(13, activation='softmax'))

	return model
Example #3
0
def CNN(nb_classes, img_dim, pretr_weights_file=None, model_name=None):
    """
    Build Convolution Neural Network

    args : nb_classes (int) number of classes
           img_dim (tuple of int) num_chan, height, width

    returns : model (keras NN) the Neural Net model
    """

    model = Sequential()
    model.add(Convolution2D(32, 3, 3, name="convolution2d_1", input_shape=(3, 224, 224), border_mode="same", activation='relu'))
    model.add(Convolution2D(32, 3, 3, name="convolution2d_2", border_mode="same", activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2), name="maxpooling2d_1"))

    model.add(Convolution2D(64, 3, 3, name="convolution2d_3", border_mode="same", activation='relu'))
    model.add(Convolution2D(64, 3, 3, name="convolution2d_4", border_mode="same", activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2), name="maxpooling2d_2"))

    model.add(Convolution2D(128, 3, 3, name="convolution2d_5", border_mode="same", activation='relu'))
    model.add(Convolution2D(128, 3, 3, name="convolution2d_6", border_mode="same", activation='relu'))
    model.add(Convolution2D(128, 3, 3, name="convolution2d_7", border_mode="same", activation='relu'))
    model.add(MaxPooling2D((2,2), strides=(2, 2), name="maxpooling2d_3"))

    model.add(Flatten(name="flatten_1"))
    model.add(Dense(1024, activation='relu', name="dense_1"))
    model.add(Dropout(0.5, name="dropout_1"))
    model.add(Dense(1024, activation='relu', name="dense_2"))
    model.add(Dropout(0.5, name="dropout_2"))
    model.add(Dense(nb_classes, activation='softmax', name="dense_3"))

    if model_name:
        model.name = model_name
    else:
        model.name = "CNN"

    if pretr_weights_file:
        model.load_weights(pretr_weights_file)
        model.layers.pop()
        model.outputs = [model.layers[-1].output]
        model.layers[-1].outbound_nodes = []
        model.add(Dense(nb_classes, activation='softmax', name="dense_4"))

    return model
Example #4
0
def vgg16_model(img_rows, img_cols, channel=1, num_classes=None):
    """VGG 16 Model for Keras

    Model Schema is based on 
    https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3

    ImageNet Pretrained Weights 
    https://drive.google.com/file/d/0Bz7KyqmuGsilT0J5dmRCM0ROVHc/view?usp=sharing

    Parameters:
      img_rows, img_cols - resolution of inputs
      channel - 1 for grayscale, 3 for color 
      num_classes - number of categories for our classification task
    """
    model = Sequential()
    model.add(ZeroPadding2D((1, 1), input_shape=(channel, img_rows, img_cols)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    # Add Fully Connected Layer
    model.add(Flatten())
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1000, activation='softmax'))

    # Loads ImageNet pre-trained data
    model.load_weights('imagenet_models/vgg16_weights.h5')

    # Truncate and replace softmax layer for transfer learning
    model.layers.pop()
    model.outputs = [model.layers[-1].output]
    model.layers[-1].outbound_nodes = []
    model.add(Dense(num_classes, activation='softmax'))

    # Uncomment below to set the first 10 layers to non-trainable (weights will not be updated)
    #for layer in model.layers[:10]:
    #    layer.trainable = False

    # Learning rate is changed to 0.001
    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])

    return model
Example #5
0
def vgg_std16_model(img_rows, img_cols):
    model = Sequential()
    model.add(ZeroPadding2D((1, 1), input_shape=(3,
                                                 img_rows, img_cols)))

    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(Flatten())
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1000, activation='softmax'))
    model.load_weights('../../models/vgg16_weights.h5')

    model.layers.pop()
    model.layers.pop()
    model.layers.pop()
    model.outputs = [model.layers[-1].output]
    model.layers[-1].outbound_nodes = []
    model.add(Dense(10, activation='softmax'))

    # model.layers.pop() # Get rid of the classification layer
    # model.layers.pop() # Get rid of the dropout layer
    # model.outputs = [model.layers[-1].output]
    # model.layers[-1].outbound_nodes = []

    # for layer in model.layers[:-4]:
    #     layer.trainable = False

    # model.add(Dropout(0.5))

    # model.add(Dense(10, activation='softmax'))

    adam = Adam(lr=1e-5, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
    # Learning rate is changed to 0.001

    # sgd = SGD(lr=1e-5, decay=0, momentum=0, nesterov=False)

    model.compile(optimizer=adam,
    	loss='categorical_crossentropy',
    	metrics=['accuracy'])
    return model
Example #6
0
def VGG_16():
	weights_path = '/home/nishant/Documents/Academics/CS676A/project/cs676project/code/models2/CNN/vgg16_weights.h5'

	from keras.models import Sequential
	from keras.layers.core import Flatten, Dense, Dropout
	from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D

	model = Sequential()
	model.add(ZeroPadding2D((1,1),input_shape=(3,224,224)))
	model.add(Convolution2D(64, 3, 3, activation='relu'))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(64, 3, 3, activation='relu'))
	model.add(MaxPooling2D((2,2), strides=(2,2)))

	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(128, 3, 3, activation='relu'))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(128, 3, 3, activation='relu'))
	model.add(MaxPooling2D((2,2), strides=(2,2)))

	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(256, 3, 3, activation='relu'))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(256, 3, 3, activation='relu'))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(256, 3, 3, activation='relu'))
	model.add(MaxPooling2D((2,2), strides=(2,2)))

	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(512, 3, 3, activation='relu'))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(512, 3, 3, activation='relu'))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(512, 3, 3, activation='relu'))
	model.add(MaxPooling2D((2,2), strides=(2,2)))

	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(512, 3, 3, activation='relu'))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(512, 3, 3, activation='relu'))
	model.add(ZeroPadding2D((1,1)))
	model.add(Convolution2D(512, 3, 3, activation='relu'))
	model.add(MaxPooling2D((2,2), strides=(2,2)))

	model.add(Flatten())
	model.add(Dense(4096, activation='relu'))
	model.add(Dropout(0.5))
	model.add(Dense(4096, activation='relu'))
	model.add(Dropout(0.5))
	model.add(Dense(1000, activation='softmax'))

	if weights_path:
		model.load_weights(weights_path)

	#Remove the last two layers to get the 4096D activations
	model.layers.pop()
	model.layers.pop()

	# Fix required for the output to be 4096D in newer versions of keras
	model.outputs = [model.layers[-1].output]
	model.layers[-1].outbound_nodes = []

	print 'VGG Model loading complete!'

	return model
Example #7
0
def VGG(nb_classes, img_dim, pretr_weights_file=None, model_name=None):
    """
    Build Convolution Neural Network

    args : nb_classes (int) number of classes
           img_dim (tuple of int) num_chan, height, width
           pretr_weights_file (str) file holding pre trained weights

    returns : model (keras NN) the Neural Net model
    """

    model = Sequential()
    model.add(ZeroPadding2D((1, 1), input_shape=img_dim, name="zeropadding2d_1"))
    model.add(Convolution2D(64, 3, 3, activation='relu', name="convolution2d_1"))
    model.add(ZeroPadding2D((1, 1), name="zeropadding2d_2"))
    model.add(Convolution2D(64, 3, 3, activation='relu', name="convolution2d_2"))
    model.add(MaxPooling2D((2, 2), strides=(2, 2), name="maxpooling2d_1"))

    model.add(ZeroPadding2D((1, 1), name="zeropadding2d_3"))
    model.add(Convolution2D(128, 3, 3, activation='relu', name="convolution2d_3"))
    model.add(ZeroPadding2D((1, 1), name="zeropadding2d_4"))
    model.add(Convolution2D(128, 3, 3, activation='relu', name="convolution2d_4"))
    model.add(MaxPooling2D((2, 2), strides=(2, 2), name="maxpooling2d_2"))

    model.add(ZeroPadding2D((1, 1), name="zeropadding2d_5"))
    model.add(Convolution2D(256, 3, 3, activation='relu', name="convolution2d_5"))
    model.add(ZeroPadding2D((1, 1), name="zeropadding2d_6"))
    model.add(Convolution2D(256, 3, 3, activation='relu', name="convolution2d_6"))
    model.add(ZeroPadding2D((1, 1), name="zeropadding2d_7"))
    model.add(Convolution2D(256, 3, 3, activation='relu', name="convolution2d_7"))
    model.add(MaxPooling2D((2, 2), strides=(2, 2), name="maxpooling2d_3"))

    model.add(ZeroPadding2D((1, 1), name="zeropadding2d_8"))
    model.add(Convolution2D(512, 3, 3, activation='relu', name="convolution2d_8"))
    model.add(ZeroPadding2D((1, 1), name="zeropadding2d_9"))
    model.add(Convolution2D(512, 3, 3, activation='relu', name="convolution2d_9"))
    model.add(ZeroPadding2D((1, 1), name="zeropadding2d_10"))
    model.add(Convolution2D(512, 3, 3, activation='relu', name="convolution2d_10"))
    model.add(MaxPooling2D((2, 2), strides=(2, 2), name="maxpooling2d_4"))

    model.add(ZeroPadding2D((1, 1), name="zeropadding2d_11"))
    model.add(Convolution2D(512, 3, 3, activation='relu', name="convolution2d_11"))
    model.add(ZeroPadding2D((1, 1), name="zeropadding2d_12"))
    model.add(Convolution2D(512, 3, 3, activation='relu', name="convolution2d_12"))
    model.add(ZeroPadding2D((1, 1), name="zeropadding2d_13"))
    model.add(Convolution2D(512, 3, 3, activation='relu', name="convolution2d_13"))
    model.add(MaxPooling2D((2, 2), strides=(2, 2), name="maxpooling2d_5"))

    model.add(Flatten(name="flatten_1"))
    model.add(Dense(4096, activation='relu', name="dense_1"))
    model.add(Dropout(0.5, name="dropout_1"))
    model.add(Dense(4096, activation='relu', name="dense_2"))
    model.add(Dropout(0.5, name="dropout_2"))
    model.add(Dense(1000, activation='softmax', name="dense_3"))

    if model_name:
        model.name = model_name
    else:
        model.name = "VGG"

    if pretr_weights_file:
        model.load_weights(pretr_weights_file)
        model.layers.pop()
        model.outputs = [model.layers[-1].output]
        model.layers[-1].outbound_nodes = []
        model.add(Dense(nb_classes, activation='softmax', name="dense_4"))

    # Freeze layers until specified number
    # for k in range(freeze_until):
    #     model.layers[k].trainable = True

    return model
def vgg_std16_model():
    model = Sequential()
    model.add(ZeroPadding2D((1, 1), input_shape=(3, 224, 224)))    # don't change unless you know what you are doing
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(128, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(256, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(512, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))

    model.add(Flatten())
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1000, activation='softmax'))

    model.load_weights('vgg16_weights.h5')
    print "Model Loaded!"

    # Code above loads pre-trained data and
    # old
    # model.layers.pop()
    # model.add(Dense(10, activation='softmax'))
    # Learning rate is changed to 0.001

    # new
    model.layers.pop()
    model.outputs = [model.layers[-1].output]
    model.layers[-1].outbound_nodes = []
    model.add(Dense(10, activation='softmax'))

    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, metrics=['accuracy'], loss='categorical_crossentropy')
    return model
Example #9
0
def vgg16_model(input_shape, output_shape, params,filew,patch_dir_store,numbits):
    num_class=output_shape[-1]
    dimpx=input_shape[-1]
    INP_SHAPE = (numbits, dimpx, dimpx)
    dim_org=keras.backend.image_data_format()
    """VGG 16 Model for Keras

    Model Schema is based on 
    https://gist.github.com/baraldilorenzo/07d7802847aaad0a35d3

    ImageNet Pretrained Weights 
    https://drive.google.com/file/d/0Bz7KyqmuGsilT0J5dmRCM0ROVHc/view?usp=sharing

    Parameters:
      img_rows, img_cols - resolution of inputs
      channel - 1 for grayscale, 3 for color 
      num_classes - number of categories for our classification task
    """
    padding='same'
    kernel_size=(3,3)
    model = Sequential()
    model.add(Conv2D(64, kernel_size, activation='relu', padding=padding , data_format=dim_org,input_shape=INP_SHAPE))
    model.add(Conv2D(64, kernel_size, activation='relu', padding=padding,data_format=dim_org ))
    model.add(MaxPooling2D((2,2), strides=(2,2),data_format=dim_org))
    
    model.add(Conv2D(128, kernel_size, activation='relu', padding=padding,data_format=dim_org ))
    model.add(Conv2D(128, kernel_size, activation='relu', padding=padding,data_format=dim_org ))
    model.add(MaxPooling2D((2,2), strides=(2,2),data_format=dim_org))
    
    model.add(Conv2D(256, kernel_size, activation='relu', padding=padding,data_format=dim_org ))
    model.add(Conv2D(256, kernel_size, activation='relu', padding=padding,data_format=dim_org ))
    model.add(MaxPooling2D((2,2), strides=(2,2),data_format=dim_org))
    
    model.add(Conv2D(512, kernel_size, activation='relu', padding=padding,data_format=dim_org ))
    model.add(Conv2D(512, kernel_size, activation='relu', padding=padding,data_format=dim_org ))
    model.add(Conv2D(512, kernel_size, activation='relu', padding=padding,data_format=dim_org ))
    model.add(MaxPooling2D((2,2), strides=(2,2),data_format=dim_org))
    
    model.add(Conv2D(512, kernel_size, activation='relu', padding=padding,data_format=dim_org ))
    model.add(Conv2D(512, kernel_size, activation='relu', padding=padding,data_format=dim_org ))
    model.add(Conv2D(512, kernel_size, activation='relu', padding=padding,data_format=dim_org ))
#    model.add(MaxPooling2D((2,2), strides=(2,2),data_format=dim_org))
    model.add(AveragePooling2D(pool_size=(2,2),data_format=dim_org))
#    
    # Add Fully Connected Layer
    model.add(Flatten())
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1000, activation='softmax'))

    # Loads ImageNet pre-trained data
    model.load_weights('vgg16_weights.h5')

    # Truncate and replace softmax layer for transfer learning
    model.layers.pop()
    model.outputs = [model.layers[-1].output]
    model.layers[-1].outbound_nodes = []
    model.add(Dense(num_class, activation='softmax'))

    for layer in model.layers[:-5]:
        layer.trainable = False

    model.compile(optimizer=Adam(lr=learning_rate), loss='categorical_crossentropy', metrics=['categorical_accuracy'])
    filew.write ('learning rate:'+str(learning_rate)+'\n')
    print ('learning rate:'+str(learning_rate))
    json_string = model.to_json()
    pickle.dump(json_string, open(os.path.join(patch_dir_store,modelname), "wb"),protocol=-1)
    orig_stdout = sys.stdout
    f = open(os.path.join(patch_dir_store,'modelvgg16.txt'), 'w')
    sys.stdout = f
    print(model.summary())
    sys.stdout = orig_stdout
    f.close()
    print model.layers[-1].output_shape #== (None, 16, 16, 21)

    return model
	model.add(BatchNormalization())
	model.add(Activation('relu'))
	model.add(Dropout(0.5))
	model.add(Dense(nb_classes))
	model.add(BatchNormalization())
	model.add(Activation('softmax'))


if(LOAD_WEIGHTS):
	model.load_weights("models_trained/cifar10_cnn_v1_batch/weigths/cifar10_cnn_v1_batch_weights_epoch20.h5")
	print("Loaded model from disk: "+weights_path_to_load + model_name_to_load+"_weights_epoch20.h5")

model.layers.pop()
model.layers.pop()
model.layers.pop()
model.outputs = [model.layers[-1].output]
model.layers[-1].outbound_nodes = []
model.add(Dense(13))
model.add(BatchNormalization())
model.add(Activation('softmax'))


model.summary()

# Train the model using SGD + momentum 
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',
			  optimizer=sgd,
			  metrics=['accuracy'])

# Save model architecture