예제 #1
0
def generate_predictions_with_augmented_images(net, X, combinations):
    t0 = time()
    n = X.shape[0]
    num_features = 121
    num_combination = len(combinations)
    predictions = np.empty((n, num_features))
    # TODO optimize by vectorizing the for loop
    # (although X_aug will be very very big and probably won't fit in memory?)
    for i, x in enumerate(X):
        if i % 1000 == 0:
            print('Finished predicting %i images. Took %i seconds so far' % (i, time() - t0))

        # Image only has one channel (grayscale)
        # im_affine_transform only works for images with shape of (height, width)
        x = x[0]
        x_aug = np.array([
            im_affine_transform(x, scale=scale, rotation=rotation, translate_y=translate_y, translate_x=translate_x)
            for scale, rotation, translate_y, translate_x in combinations
        ])

        x_aug = x_aug.reshape(num_combination, 1, args.hw, args.hw).astype(np.float32)
        # TODO do we need to normalize this back to 0?
        pred = net.predict_proba(x_aug).mean(axis=0)
        predictions[i] = pred
    return predictions
예제 #2
0
    t0 = time()

    # For each image generate a number of variation of the same images using
    # affine transform and average out prediction
    predictions = np.empty((X.shape[0], num_combination, 121))
    for i, x in enumerate(X):
        if i % 2500 == 0:
            print('Finished predicting %i images. Took %i seconds so far' %
                  (i, time() - t0))

        # Image only has one channel (grayscale)
        # im_affine_transform only works for images with shape of (height, width)
        x = x[0]
        x_aug = np.array([
            im_affine_transform(x,
                                scale=scale,
                                rotation=rotation,
                                translate_y=translate_y,
                                translate_x=translate_x)
            for scale, rotation, translate_y, translate_x in combinations
        ])
        x_aug = x_aug.reshape(num_combination, 1, hw, hw).astype(np.float32)
        pred = net.predict_proba(x_aug)
        predictions[i] = pred
    print('Finished predicting all %i images. Took %i seconds in total' %
          (X.shape[0], time() - t0))

    np.save(args.pred_aug_fname, predictions)
    print('Predictions saved to %s' % args.pred_aug_fname)
    print(predictions.shape)