Esempio n. 1
0
def output_at_layer(input_image, model, layer_num, verbose=False):
    """
    This function is used to visualize activations at any layer in a
    Convolutional Neural Network model in Keras. It returns the output image
    for a given input image at the layer_num layer of the model (layer numbers
    starting at 1). The model should be Sequential type. This function will
    not mutate the model.

    Reference: https://github.com/fchollet/keras/issues/431

    The idea is to keep the first layer_num layers of a trained model and
    remove the rest. Then compile the model and use the predict function to get
    the ouput.

    Parameters:
    -----------
        input_image: Image in Numpy format from the dataset
        model: Name to be used with the load_model() function
        layer_num: Layer number between 1 and len(model.layers)
        verbose: Prints layer info

    Returns:
    --------
        output_image: Numpy array of the output at the layer_num layer
    """
    model_temp = Sequential()
    model_temp.layers = model.layers[:layer_num]  # Truncates layers
    model_temp.compile(loss=model.loss,
                       optimizer=model.optimizer)  # Recompiles model_temp
    output_image = model_temp.predict(np.array(
        [input_image]))  # Uses predict to get ouput

    if verbose:
        # Print layer and image info
        layer = model_temp.layers[layer_num - 1]
        print layer
        try:
            print layer.W_shape
        except:
            pass
        print "Image dimensions: " + str(output_image.shape)

    return output_image
Esempio n. 2
0
def output_at_layer(input_image, model, layer_num, verbose=False):
    """
    This function is used to visualize activations at any layer in a
    Convolutional Neural Network model in Keras. It returns the output image
    for a given input image at the layer_num layer of the model (layer numbers
    starting at 1). The model should be Sequential type. This function will
    not mutate the model.

    Reference: https://github.com/fchollet/keras/issues/431

    The idea is to keep the first layer_num layers of a trained model and
    remove the rest. Then compile the model and use the predict function to get
    the ouput.

    Parameters:
    -----------
        input_image: Image in Numpy format from the dataset
        model: Name to be used with the load_model() function
        layer_num: Layer number between 1 and len(model.layers)
        verbose: Prints layer info

    Returns:
    --------
        output_image: Numpy array of the output at the layer_num layer
    """
    model_temp = Sequential()
    model_temp.layers = model.layers[:layer_num]  # Truncates layers
    model_temp.compile(loss=model.loss, optimizer=model.optimizer)  # Recompiles model_temp
    output_image = model_temp.predict(np.array([input_image]))  # Uses predict to get ouput
    
    if verbose:
        # Print layer and image info
        layer = model_temp.layers[layer_num-1]
        print layer
        try:
            print layer.W_shape
        except:
            pass
        print "Image dimensions: " + str(output_image.shape)
        
    return output_image
Esempio n. 3
0
    # if i_iter == 1: #only remove this in step 1
    #     model.layers.pop(-3)

    if i_iter == 2:
        # load json and create model from previous iteration
        json_file = open('Part1.json', 'r')
        loaded_model_json = json_file.read()
        json_file.close()
        # load model
        model = model_from_json(loaded_model_json)
        # load weights from previous run into new model
        model.load_weights('Part1.h5')
        # model = model.load_weights('Part1.h5')
        model2 = Sequential()
        model2.layers = model.layers[0:-2]
        model2.layers.append(Convolution2D(512, 3, 3, border_mode='same'))
        # model.layers.add(Convolution2D(512, 3, 3, border_mode='same'))
        # model.add(Dense(nb_classes))
        # model.add(Activation('softmax'))
        model2.layers.append(model.layers[-2])
        model2.layers.append(model.layers[-1])
        model = model2

    if i_iter == 3:
        adagrad = Adagrad(lr=0.01, epsilon=1e-08, decay=0.0)

    if i_iter == 4:
        ReLU_layers = [
            num for num, l in enumerate(model.layers)
            if l.name.split('_')[0] == Activation('relu').name.split('_')[0]
Esempio n. 4
0
    print("----- Training -----")
    path_to_weights = "{}/pretrain_initial_weights.hdf5".format(run_folder)

    model = Sequential()
    model.add(
        Dense(units=WIDTH,
              activation=ACTIVATION,
              kernel_initializer=INIT,
              input_shape=(train_input_dim, )))
    for i in range(1, DEPTH - 1):
        model.add(
            Dense(units=WIDTH, activation=ACTIVATION, kernel_initializer=INIT))
    model.add(Dense(2, activation='softmax', kernel_initializer=INIT))
    model.load_weights(path_to_weights, by_name=False)
    # remove last n layers
    model.layers = model.layers[0:DEPTH - 1]
    # add new layer
    model.add(Dense(num_classes, activation='softmax',
                    kernel_initializer=INIT))
    assert len(model.layers) == DEPTH

    sgd = keras.optimizers.SGD(lr=LAST_LR,
                               momentum=0.0,
                               decay=DECAY,
                               nesterov=False)
    model.compile(optimizer=sgd,
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])
    model.summary()
    history = model.fit(x_train,
                        y_train,
Esempio n. 5
0
model = Sequential()

model.add(Convolution2D(32, 3, 3, activation='relu', input_shape=(1, 28, 28)))

print model.output_shape

model.add(Convolution2D(32, 3, 3, activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

model.layers()

#compiling model

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

#Fit the Keras model to training data
history = model.fit(X_train,
                    Y_train,
                    validation_split=0.33,
                    batch_size=32,
                    nb_epoch=2,
                    verbose=1)