Ejemplo n.º 1
0
def build_model(input_tensor=None):
    if input_tensor is None:
        input_tensor = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3))

    mvcnn = multiview_cnn.load_model(input_tensor=input_tensor,
                                     include_top=False)

    x = mvcnn.output
    x = Dense(256,
              activation='relu',
              kernel_regularizer=regularizers.l2(0.01),
              bias_regularizer=regularizers.l2(0.01),
              name='fc3')(x)
    x = Dropout(0.5)(x)
    x = Dense(128,
              activation='relu',
              kernel_regularizer=regularizers.l2(0.01),
              bias_regularizer=regularizers.l2(0.01),
              name='fc4')(x)
    x = Dropout(0.5)(x)
    x = Dense(2, activation='softmax', name='saliency')(x)

    # Saliency predictor.
    return Model(inputs=input_tensor, outputs=x)
    # Save matrix to disk.
    if save_file:
        print('Saving to %s.' % save_file)
        np.save(save_file, matrix)
        print('Success.')


if __name__ == '__main__':
    # Data source.
    img_normalize = samplewise_normalize(IMAGE_MEAN, IMAGE_STD)
    fc2_normalize = samplewise_normalize(FC2_MEAN, FC2_STD)
    valid_group = GroupedDatagen(VALID_DIR, preprocess=img_normalize)

    # Use the fc activations as features.
    model = load_model(MODEL_WEIGHTS)
    fc2_layer = model.get_layer('fc2').output
    softmax_layer = model.get_layer('predictions').output

    # Low budget version of pickle.load().
    multiview = MultiviewModel(model.layers[0].input,
                               fc2_layer,
                               softmax_layer,
                               k_features,
                               NUM_CLASSES,
                               preprocess=fc2_normalize,
                               svm_path=svm_path,
                               sort_mode=sort_mode)

    evaluate_loop(multiview, valid_group, save_file=matrix_path)
Ejemplo n.º 3
0
import numpy as np

from geometry_processing.globals import MODEL_WEIGHTS
from geometry_processing.utils.helpers import load_weights
from geometry_processing.models.multiview_cnn import load_model, test

parser = argparse.ArgumentParser(description='Test MVCNN classification.')

parser.add_argument('--matrix_path',
                    required=True,
                    type=str,
                    help='Path to save the confusion matrix.')

args = parser.parse_args()
matrix_path = args.matrix_path

if __name__ == '__main__':
    # Initialize model.
    mvcnn = load_model()
    load_weights(mvcnn, MODEL_WEIGHTS)

    # Run through entire test dataset.
    matrix = test(mvcnn)
    print('Accuracy %.4f' % np.mean(np.diag(matrix) / np.sum(matrix, axis=1)))

    # Save matrix to disk.
    if matrix_path:
        print('Saving to %s.' % matrix_path)
        np.save(matrix_path, matrix)
        print(index)

        activations = layer.predict(x)

        offset = min(num_samples - index, activations.shape[0])
        samples[index:index + offset] = activations[:offset]
        index += offset

    print(samples[-1])

    return np.mean(samples, axis=0), np.std(samples, axis=0)


if __name__ == '__main__':
    # Use the fc activations as features.
    model = load_model(SAVE_FILE)
    fc2 = extract_layer(model, 'fc2')

    # Normalize the image.
    normalize = samplewise_normalize(IMAGE_MEAN, IMAGE_STD)

    # Initialize data generators.
    train_datagen = get_data(TRAIN_DIR, 64, preprocess=normalize)

    # Precompute fc2 center and std.
    mean, std = get_mean_std(fc2, train_datagen, NUM_SAMPLES)

    # Cache for future use.
    with open("fc2_mean.npy", "wb") as fd:
        np.save(fd, mean)
    with open("fc2_std.npy", "wb") as fd:
                    print(path_prediction[0], 0.0)
        else:
            for i in range(predictions.shape[0]):
                if entropy(predictions[i]) <= confidence_threshold:
                    print(full_paths[i], 1.0)
                else:
                    print(full_paths[i], 0.0)


if __name__ == '__main__':
    # Data source and image normalization.
    img_normalize = samplewise_normalize(IMAGE_MEAN, IMAGE_STD)

    # Directory of images.
    if generate_dataset == 'train':
        datagen = FilenameImageDatagen(TRAIN_DIR, preprocess=img_normalize)
    elif generate_dataset == 'test':
        datagen = FilenameImageDatagen(VALID_DIR, preprocess=img_normalize)
    else:
        raise ValueError('Invalid input for --generate_dataset.')

    # # Use the fc activations as features.
    model = load_model()
    load_weights(model, MODEL_WEIGHTS)

    # Wrapper around Tensorflow run operation.
    functor = K.function([model.layers[0].input, K.learning_phase()],
                         [model.get_layer('predictions').output])

    generate(datagen, functor)