예제 #1
0
def test_accuracy_multi(args):
    # Compute Accuracy of images from input file
    # Code predict many images at one time. The value is set by max_value
    # Same value should be set in proto file
    # It is done because of speed reason

    pred = PredictionAugmented(args.proto_path, args.bin_path)
    max_value = 512
    curr_value = 0
    list_all_result = list()
    list_good_class_all = list()

    with open(args.images, 'r') as file_image:
        list_images = list()
        list_good_class = list()
        for line in file_image:
            splitted = line.split(' ')
            # if not os.path.isfile(splitted[0].strip()):
            # continue
            list_good_class.append(int(splitted[1]))
            list_images.append(splitted[0].strip())
            curr_value = curr_value + 1
            if curr_value < max_value:
                continue
            else:
                # predict using value
                predictions = pred.predict_multi(list_images)
                list_all_result.append(predictions)
                list_good_class_all.extend(list_good_class)
                list_good_class = list()
                list_images = list()
                curr_value = 0

        # predict last package of data, which is smaller than max_value
        if len(list_images) > 0:
            predictions = pred.predict_multi(list_images)
            list_all_result.append(predictions)

    # test loss
    list_good_class_all.extend(list_good_class)
    y_truth = np.asarray(list_good_class_all)
    #y_pred = np.vstack(list_all_result)
    #y_pred_label = np.argmax(y_pred, axis=1)
    #print y_truth.shape, y_pred.shape
    #print "Accuracy: ", accuracy_score(y_truth, y_pred_label)
    #print "Loss: ", multiclass_log_loss(y_truth, y_pred)
    # plot_confusion_matrix(y_truth, y_pred_label)
    np.save('labels_image.npy',y_truth)
    save_cPickle(list_all_result, "amnie_val_all.npy")
예제 #2
0
def prob_image(args):
    list_predictions = list()
    pred = PredictionAugmented(args.proto_path, args.bin_path)
    max_value = 512
    curr_value = 0
    batch = 0

    with open(args.images, 'r') as file_image:
        list_images = list()
        list_good_class = list()
        for line in file_image:
            splitted = line.split('\t')
            list_images.append(splitted[2].strip())
            curr_value = curr_value + 1
            if curr_value < max_value:
                continue
            else:
                # predict using value
                print "==== Predicting for batch {0} ====".format(batch)
                start = time.time()
                predictions = pred.predict_multi(list_images)
                list_images = list()
                list_good_class = list()
                curr_value = 0
                list_predictions.extend(predictions)
                end = time.time()
                print "==== End batch {0} - took {1} ====".format(end - start, batch)
                batch += 1

        if len(list_images) > 0:
            # predict using value
            predictions = pred.predict_multi(list_images)
            list_predictions.extend(predictions)

    all_pred = np.vstack(list_predictions)
    np.save('probabilities_kaggle_casia_cnn_aug.npy',all_pred)
    np.savetxt('probabilities_kaggle_casia_cnn_aug.txt', all_pred, fmt='%1.9e')