예제 #1
0
def get_confusion_matrix(directory, reference_images, test_images):
	if not os.path.exists(directory + 'confusion.npy'):
		correlations, offsets = confusion_matrix.confusion_matrix(reference_images, test_images)
		np.save(directory + 'confusion', correlations)
		np.save(directory + 'offsets', offsets)
	else:
		correlations = np.load(dir_test + 'confusion.npy')
		offsets = np.load(dir_test + 'offsets.npy')
	return correlations, offsets
def test(elm, x, y, type=''):
    sample_num = x.shape[0]
    print('predicting!')
    predict = elm.predict(x)
    counter = .0
    print('-------------------------------------------\n testing')
    for r, t in zip(predict, y):
        if np.argmax(r) == np.argmax(t):
            counter += 1
    print(type + ' right rate:', counter / sample_num)
    con_mat = confusion_matrix(predict, y)
    print(con_mat)
    np.save('./con_mat2.npy', con_mat)
예제 #3
0
def test_adapt_elm(X, Y):
    result = []
    for k in range(K):
        elm = hpelm.HPELM(inputs=input_size, outputs=8)
        elm.add_neurons(hidden_num, 'sigm')
        elm.load('./data/adapt_elm' + str(K) + '/elm_' + str(k))

        predict = elm.predict(X) * elm_weight[k]
        if k == 0:
            result = predict
        else:
            result = result + predict
    counter = 0.0
    eachClassRight = [.0 for i in range(8)]
    for r, t in zip(result, Y):
        if np.argmax(r) == np.argmax(t):
            counter += 1
            eachClassRight[np.argmax(r)] += 1
    con_mat = confusion_matrix(result, Y)
    print(con_mat)
    np.save('./con_mat3.npy', con_mat)
    right_rate = counter / X.shape[0]
    print('adapt elm right rate:' + str(right_rate))
예제 #4
0
def test(test_loader, model_s, model_t, gpu, num_classes):
    model_s.eval()
    model_t.eval()
    sig = nn.Sigmoid()
    preds = []
    labels = []

    pickle_file_p = ''
    pickle_file_l = ''
    #pickle_file_p = 'preds.pkl'
    #pickle_file_l = 'labels.pkl'

    if pickle_file_p and pickle_file_l:
        pickle_file_p = open(pickle_file_p, 'rb')
        pickle_file_l = open(pickle_file_l, 'rb')
        preds = pickle.load(pickle_file_p)
        labels = pickle.load(pickle_file_l)

    else:
        example_count = 0
        test_len = len(test_loader)

        for spatial, temporal, label in test_loader:
            example_count += 1
            print(f'Clip #{example_count} / {test_len}')

            if gpu:
                spatial = spatial.cuda()
                temporal = temporal.cuda()

            with torch.no_grad():
                s = model_s(spatial.squeeze().float())
                t = model_t(temporal.squeeze().float())

            s_num = s.shape[0]
            t_num = t.shape[0]

            s = s.sum(axis=0)
            t = t.sum(axis=0)
            s = s.div(s_num)
            t = t.div(t_num)

            pred = torch.add(s, t)
            pred = pred.div(2)
            pred = sig(pred)

            if gpu == True:
                pred = pred.cpu()

            label = label.squeeze()
            labels.append(label.numpy())
            preds.append(pred.numpy())

        preds = np.array(preds).astype(np.float64)
        labels = np.array(labels).astype(np.int32)

        pickle_file_p = open('preds.pkl', 'wb')
        pickle.dump(preds, pickle_file_p)
        pickle_file_p.close()
        pickle_file_l = open('labels.pkl', 'wb')
        pickle.dump(labels, pickle_file_l)
        pickle_file_l.close()

    print(preds.shape)
    print(labels.shape)

    c = confusion_matrix(num_classes)
    c.mAP(labels, preds)
예제 #5
0
    jpg = os.path.join(image_path,
                       os.path.basename(os.path.splitext(xml)[0]) + ".jpg")
    shutil.copy(jpg, classify_xml_path)

# write auto_labeling info into csv ###############################################################################
print(colorama.Fore.GREEN + "[INFO] write auto_labeling info into csv" +
      colorama.Fore.WHITE)
csv_file_s = os.path.join(output_tif_608s, tif_name + "_s.csv")
dict_to_csv(dict_pic_info, csv_file_s)
csv_file_c = os.path.join(output_tif_608s, tif_name + "_c.csv")
predictions_to_csv(dict_pic_info_all, classes_list, classes_all, csv_file_c)

# generate confusion matrix #######################################################################################
print(colorama.Fore.GREEN + "[INFO] generate confusion matrix" +
      colorama.Fore.WHITE)
matrix = confusion_matrix(classes_all, cell_numpy_index, predictions)
xlsx = os.path.join(output_tif_608s, tif_name + ".xlsx")
generate_xlsx(classes_all, matrix, xlsx)

# generate asap_xml from labelimg_xmls
print(colorama.Fore.GREEN + "[INFO] generate asap xml from labelimg xmls" +
      colorama.Fore.WHITE)
xml_asap_segment = os.path.join(output_tif_608s, tif_name + "_segment.xml")
gen_asap_xml(xml_asap_segment, segment_xml_path)
xml_asap_classify = os.path.join(output_tif_608s, tif_name + "_classify.xml")
gen_asap_xml(xml_asap_classify, classify_xml_path)

# move current directories upwards ################################################################################
print(colorama.Fore.GREEN + "[INFO] move current directories upwards" +
      colorama.Fore.WHITE)
save_path_i = os.path.join(save_path, tif_name)
def main():
	n=int(input("Enter number of folds k : "))
	confusion_matrix(n)
예제 #7
0
def plot_completeness(variable,
                      bins,
                      actual,
                      predicted,
                      num_bins,
                      path_prefix,
                      scale=False):
    accs = []
    pres = []
    recs = []
    F1s = []
    for l in range(num_bins):
        cm, acc, pre, rec, F1 = cf.confusion_matrix(actual[l], predicted[l])
        accs.append(acc)
        pres.append(pre)
        recs.append(rec)
        F1s.append(F1)

    # plot accuracy
    plt.figure()
    plt.xlabel(variable)
    plt.ylabel('Accuracy')
    if scale:
        plt.plot([b for b in bins], accs, 'ko', rasterized=True)
    else:
        plt.plot([str(round(b, 2)) for b in bins], accs, 'ko', rasterized=True)
    filename = '{}_accuracy.pdf'.format(path_prefix, variable)
    plt.tight_layout()
    plt.savefig(filename)
    plt.close()

    #plot precision
    plt.figure()
    plt.xlabel(variable)
    plt.ylabel('Precision')
    if scale:
        plt.plot([b for b in bins], pres, 'ko', rasterized=True)
    else:
        plt.plot([str(round(b, 2)) for b in bins], pres, 'ko', rasterized=True)
    filename = '{}_precision.pdf'.format(path_prefix, variable)
    plt.tight_layout()
    plt.savefig(filename)
    plt.close()

    # plot recall
    plt.figure()
    plt.xlabel(variable)
    plt.ylabel('Recall')
    if scale:
        plt.plot([b for b in bins], recs, 'ko', rasterized=True)
    else:
        plt.plot([str(round(b, 2)) for b in bins], recs, 'ko', rasterized=True)
    filename = '{}_recall.pdf'.format(path_prefix, variable)
    plt.tight_layout()
    plt.savefig(filename)
    plt.close()

    # plot F1
    plt.figure()
    plt.xlabel(variable)
    plt.ylabel('F1')
    if scale:
        plt.plot([b for b in bins], F1s, 'ko', rasterized=True)
    else:
        plt.plot([str(round(b, 2)) for b in bins], F1s, 'ko', rasterized=True)
    filename = '{}_F1.pdf'.format(path_prefix, variable)
    plt.tight_layout()
    plt.savefig(filename)
    plt.close()
예제 #8
0
def main():

    train, test = load_data()

    prior = 1 / 3

    # Num Classes
    c = train.shape[0]
    # Num Dimensions
    d = train[0].shape[1]

    means = np.empty((c, d))
    covs = np.empty(((c, d, d)))

    ###### Part A #######

    for i in range(c):
        means[i] = np.mean(train[i], axis=0)
        covs[i] = np.cov(train[i], rowvar=0)

    predicted = np.array([], dtype=int)

    flat_test, labels_test = flatten_data(test, c)

    disc_values = np.zeros((100, 3))

    ####### Part B ######

    for i, point in enumerate(flat_test):
        for j in range(c):
            m = discriminant(point, means[j], covs[j], d, prior)
            disc_values[i, j] = m

    predicted = np.argmax(disc_values, axis=1)

    ####### Part C #########

    cm, acc = confusion_matrix(labels_test, predicted, c)

    print("Part C Covariance Matrix")
    print(cm)
    print(f"Error = {1 - acc}")

    ####### Part D ##########

    flat_train, labels_train = flatten_data(train, c)

    plot_data(flat_train.T[0], flat_train.T[1], labels_train, c)
    plot_data(flat_test.T[0], flat_test.T[1], labels_test, c)

    r_train, theta_train = cart2pol(flat_train)

    r_test, theta_test = cart2pol(flat_test)

    plot_data(r_train, theta_train, labels_train, c)
    plot_data(r_test, theta_test, labels_test, c)

    means = np.empty(c)
    covs = np.empty(c)
    posterior = np.zeros(c)

    disc_values = np.empty((r_test.shape[0], c))

    for i in range(c):
        means[i], covs[i] = mu_estimate(r_train[labels_train == i], 0, 100,
                                        .25)

    for i, pt in enumerate(r_test):
        for j in range(c):
            disc_values[i, j] = discriminant(pt, means[j], covs[j], d, prior)

    predicted = np.argmax(disc_values, axis=1)

    cm, acc = confusion_matrix(labels_test, predicted, c)

    print("Part D Covariance Matrix")
    print(cm)
    print(f"Error = {1 - acc}")
예제 #9
0
print()

if case == '1':
    Sigma = cases.case_1(covariance)

elif case == '2':
    Sigma = cases.case_2(covariance)

elif case == '3':
    Sigma = cases.case_3(covariance)

elif case == '4':
    Sigma = cases.case_4(covariance)

else:
    print("check the case & case number")

# Generates all possible combination of 2
pairs = list(combinations([0, 1, 2], 2))

# Decision region plot for every pair of classes together with the training data superposed
for combo in pairs:
    class_region.class_region(combo, train, mean, Sigma, probability)

# Decision region plot for all the classes
al_combo = [0, 1, 2]
class_region.class_region(al_combo, train, mean, Sigma, probability)

# Confusion matrix
confusion_matrix.confusion_matrix(mean, Sigma, probability, test)
예제 #10
0
def nn_tests():
    with tensorflow.device('/cpu:0'):

        dropout_arr = [0.1, 0.2, 0.3, 0.4, 0.5]
        for dropout in dropout_arr:
            #filters_arr = [4,8,16,32,64]
            #for filters in filters_arr:

            #print("\nNumber of Filters", filters)
            #print("Dropout:", dropout)
            starttime = datetime.now()

            # Load mnist dataset
            print("\nLoading MNIST dataset.")
            (x_train, y_train), (x_test, y_test) = mnist.load_data()

            # Compute the number of labels
            num_labels = len(np.unique(y_train))

            # Convert to one-hot vectors
            y_train = to_categorical(y_train, num_labels)
            y_test = to_categorical(y_test, num_labels)

            # Get the image dimensions (assumed square)
            image_size = x_train.shape[1]
            input_size = image_size * image_size

            # Resize and normalize
            x_train = np.reshape(x_train, [-1, image_size, image_size, 1])
            x_test = np.reshape(x_test, [-1, image_size, image_size, 1])
            x_train = x_train.astype('float32') / 255
            x_test = x_test.astype('float32') / 255

            # Network parameters
            # image is processed as is (square grayscale)
            input_shape = (image_size, image_size, 1)
            batch_size = 128
            kernel_size = 3
            pool_size = 2
            filters = 16

            #dropout = 0.2

            # model is a stack of CNN-ReLU-MaxPooling
            model = Sequential()
            model.add(
                Conv2D(filters=filters,
                       kernel_size=kernel_size,
                       activation='relu',
                       input_shape=input_shape))
            model.add(MaxPooling2D(pool_size))
            model.add(
                Conv2D(filters=filters,
                       kernel_size=kernel_size,
                       activation='relu'))
            model.add(MaxPooling2D(pool_size))
            model.add(
                Conv2D(filters=filters,
                       kernel_size=kernel_size,
                       activation='relu'))
            model.add(Flatten())

            # dropout added as regularizer
            model.add(Dropout(dropout))

            # output layer is 10-dim one-hot vector
            model.add(Dense(num_labels))
            model.add(Activation('softmax'))

            # Print and plot model
            model.summary()
            plot_model(model, to_file='mp1_nn2.png', show_shapes=True)

            # loss function for one-hot vector
            # use of adam optimizer
            # accuracy is good metric for classification tasks
            model.compile(loss='categorical_crossentropy',
                          optimizer='adam',
                          metrics=['accuracy'])

            # train the network
            model.fit(x_train, y_train, epochs=2, batch_size=batch_size)

            # Compute predictions (test mode) for training data
            y_pred = model.predict(x_train, batch_size=batch_size, verbose=0)

            # Convert one-hot tensors back to class labels (and make them numpy arrays)
            y_train_true_class = K.argmax(y_train).numpy()
            y_train_pred_class = K.argmax(y_pred).numpy()

            # Students, insert code here to create CM and print confusion matrix and stats
            cm_train, cm_train_acc = confusion_matrix(y_train_true_class,
                                                      y_train_pred_class)
            print_confusion_matrix_stats(cm_train, 'Train')

            # Validate the model on test dataset to determine generalization
            y_pred = model.predict(x_test, batch_size=batch_size, verbose=0)

            # Convert one-hot tensors back to class labels (and make them numpy arrays)
            y_test_true_class = K.argmax(y_test).numpy()
            y_test_pred_class = K.argmax(y_pred).numpy()

            # Students, insert code here to create CM and print confusion matrix and stats
            cm_test, cm_test_acc = confusion_matrix(y_test_true_class,
                                                    y_test_pred_class)
            print_confusion_matrix_stats(cm_test, 'Test')

            endtime = datetime.now()

            print("Elapsed time:", endtime - starttime)
    return
예제 #11
0
######################PLACEHOLDER 3 #end #########################

print("trainX.shape", trainX.shape, "\ntrainY.shape", trainY.shape,
      "\ntestX.shape", testX.shape, "\ntestY.shape", testY.shape,
      "\nyHat.shape", yHat.shape)

# step 4: evaluation
# compare predictions yHat and and true labels testy to calculate average error and standard deviation

# sklearn way
testYDiff = np.abs(yHat - testY)
avgErr = np.mean(testYDiff)
stdErr = np.std(testYDiff)
(accuracy, positive_precision, negative_precision, positive_recall,
 negative_recall, conf_matrix) = confusion_matrix(yHat, testY)
print('SK learn accuracy: {}'.format(accuracy))
print('SK learn true-positive precision: {}'.format(positive_precision))
print('SK learn true-negative precision: {}'.format(negative_precision))
print('SK learn true-positive recall: {}'.format(positive_recall))
print('SK learn true-negative recall: {}'.format(negative_recall))
print('SK learn confusion matrix:\n {}'.format(conf_matrix))
print('sklearn average error: {} ({})'.format(avgErr, stdErr))
print('score Sklearn: ', logReg.score(testX, testY), "\n")

# GD way
testYDiff2 = np.abs(prediction_Y - testY)
avgErr2 = np.mean(testYDiff2)
stdErr2 = np.std(testYDiff2)
(GDaccuracy, GDpositive_precision, GDnegative_precision, GDpositive_recall,
 GDnegative_recall, GDconf_matrix) = confusion_matrix(prediction_Y, testY)
예제 #12
0
def change_hidden_units():
    with tensorflow.device('/cpu:0'):
        
        for hidden_units in hidden_units_arr:
        
            print("\nNumber of Hidden Units", hidden_units)
            starttime = datetime.now()
            # Load mnist dataset
            print("\nLoading MNIST dataset.")
            (x_train, y_train), (x_test, y_test) = mnist.load_data()

       
            # Compute the number of labels
            num_labels = len(np.unique(y_train))

            # Convert to one-hot vectors
            y_train = to_categorical(y_train)
            y_test = to_categorical(y_test)

            # Get the image dimensions (assumed square)
            image_size = x_train.shape[1]
            input_size = image_size * image_size

            # Resize and normalize
            x_train = np.reshape(x_train, [-1, input_size])
            x_train = x_train.astype('float32') / 255
            x_test = np.reshape(x_test, [-1, input_size])
            x_test = x_test.astype('float32') / 255

            # Setup the network parameters
            batch_size = 128
            hidden_units = 256
            dropout = 0.45

            # model is a 3-layer MLP with ReLU and dropout after each layer
            model = Sequential()
            model.add(Dense(hidden_units, input_dim=input_size))
            model.add(Activation('relu'))
            model.add(Dropout(dropout))
            model.add(Dense(hidden_units))
            model.add(Activation('relu'))
            model.add(Dropout(dropout))
            model.add(Dense(num_labels))

            # this is the output for one-hot vector
            model.add(Activation('softmax'))

            # Print model summary and save the network image to the file specified
            model.summary()
            plot_model(model, to_file='mp1_nn1.png', show_shapes=True)

            # loss function for one-hot vector
            # use of adam optimizer
            # accuracy is good metric for classification tasks
            model.compile(loss='categorical_crossentropy',
                          optimizer='adam',
                          metrics=['accuracy'])

            # Train the network
            model.fit(x_train, y_train, epochs=20, batch_size=batch_size)

            # Compute predictions (test mode) for training data
            y_pred = model.predict(x_train,
                                   batch_size=batch_size,
                                   verbose=0)

            # Convert one-hot tensors back to class labels (and make them numpy arrays)
            y_train_true_class = K.argmax(y_train).numpy()
            y_train_pred_class = K.argmax(y_pred).numpy()

            # Students, insert code here to create CM and print confusion matrix and stats
            cm_train, cm_train_acc = confusion_matrix(y_train_true_class, y_train_pred_class)
            print_confusion_matrix_stats(cm_train, 'Train')

            # Validate the model on test dataset to determine generalization
            y_pred = model.predict(x_test,
                                   batch_size=batch_size,
                                   verbose=0)

            # Convert one-hot tensors back to class labels (and make them numpy arrays)
            y_test_true_class = K.argmax(y_test).numpy()
            y_test_pred_class = K.argmax(y_pred).numpy()

            # Students, insert code here to create CM and print confusion matrix and stats
            cm_test, cm_test_acc = confusion_matrix(y_test_true_class, y_test_pred_class)
            print_confusion_matrix_stats(cm_test, 'Test')
            
            endtime = datetime.now()
            
            print("Elapsed time:", endtime - starttime)
    return
예제 #13
0
def main():
    parser = argparse.ArgumentParser(description="Testing the HORCNN Model!")
    parser.add_argument("--bbmatfile",
                        help="Path to the HICO-DET bounding box matfile",
                        default='../Dataset/images/anno_bbox.mat',
                        nargs='?')
    #parser.add_argument("train_path", help="Path to the file containing training images", default='../Dataset/images/train2015', nargs='?')
    parser.add_argument("--test_path",
                        help="Path to the file containing Testing images",
                        default='../Dataset/images/test2015',
                        nargs='?')
    #parser.add_argument("det_prop_file", help="Path of the object detection proposals pickle file. This file should contian detected objects from the images. If none is specified this program will run the FastRCNN Object detector over the training images to create the file",
    #	default='../Dataset/images/pkl_files/fullTrain.pkl', nargs='?')
    parser.add_argument(
        "--det_prop_file",
        help=
        "Path of the object detection proposals pickle file. This file should contian detected objects from the images. If none is specified this program will run the FastRCNN Object detector over the training images to create the file",
        default='../Dataset/images/pkl_files/full_test2015.pkl',
        nargs='?')
    parser.add_argument("--batch_size",
                        help="batch_size for training",
                        type=int,
                        default=4,
                        nargs='?')
    parser.add_argument("--gpu",
                        help="Runninng on GPU?",
                        type=bool,
                        default=True,
                        nargs='?')
    parser.add_argument("--model_path",
                        help="Model Path To Evaluate",
                        default='./saved_models/Final_Trained',
                        nargs='?')
    parser.add_argument("--threshold",
                        help="prediction threshold",
                        default=0.001,
                        nargs='?',
                        type=float)
    args = parser.parse_args()

    bbox_mat = loadmat('../Dataset/images/anno_bbox.mat')

    test_data = HICODET_test(args.test_path,
                             args.bbmatfile,
                             props_file=args.det_prop_file,
                             proposal_count=10)

    print('Length of Testing dataset: ' + str(len(test_data)))
    print('Num props ' + str(test_data.proposal_count))
    test_data_loader = torch.utils.data.DataLoader(dataset=test_data,
                                                   batch_size=4,
                                                   shuffle=False)

    print('------------DATASET SETUP COMPLETE-------------')
    print('\n')
    print('------------Setting Up Models------------------')
    human_model = models.alexnet(pretrained=True)
    human_model.classifier[6] = nn.Linear(4096, 600)
    human_model = human_model.eval()

    object_model = models.alexnet(pretrained=True)
    object_model.classifier[6] = nn.Linear(4096, 600)
    object_model = object_model.eval()

    pairwise_model = HO_RCNN_Pair()
    pairwise_model = pairwise_model.eval()

    human_model.load_state_dict(
        torch.load(os.path.join(args.model_path, 'human.pth')))
    object_model.load_state_dict(
        torch.load(os.path.join(args.model_path, 'object.pth')))
    pairwise_model.load_state_dict(
        torch.load(os.path.join(args.model_path, 'pairwise.pth')))

    if args.gpu == True:
        human_model.cuda()
        object_model.cuda()
        pairwise_model.cuda()

    criterion = nn.BCEWithLogitsLoss()
    print('--------------MODEL SETUP COMPLETE-------------')
    print('---------------TESTING MODEL-------------------')

    predictions = []
    p_h = []
    p_o = []
    p_p = []
    p_ho = []
    p_hp = []
    outs = []

    batch_count = 0
    losses = []

    for human_crop, object_crop, int_pattern, outputs in test_data_loader:
        batch_count += 1
        human_crop = torch.stack([j for i in human_crop for j in i])
        object_crop = torch.stack([j for i in object_crop for j in i])
        int_pattern = torch.stack([j for i in int_pattern for j in i])
        outputs = torch.stack([j for i in outputs for j in i])

        human_crop = human_crop.float().cuda()
        object_crop = object_crop.float().cuda()
        int_pattern = int_pattern.float().cuda()

        #human_crop = human_crop.float().cuda()
        #object_crop = object_crop.float().cuda()
        #int_pattern = int_pattern.float().cuda()

        with torch.no_grad():
            human_pred = human_model(human_crop)
            object_pred = object_model(object_crop)
            pairwise_pred = pairwise_model(int_pattern)

        total_pred = torch.add(torch.add(human_pred, object_pred),
                               pairwise_pred)

        #total_pred_cpu = total_pred.cpu()
        predictions.append(total_pred)
        p_h.append(human_pred)
        p_o.append(object_pred)
        p_p.append(pairwise_pred)
        p_ho.append(torch.add(human_pred, object_pred))
        p_hp.append(torch.add(human_pred, pairwise_pred))
        #predictions.append(total_pred)

        #outs.append(outputs.numpy().astype(int))
        outs.append(outputs.cuda())
        #outs.append(outputs.cuda())

        #batch_loss = criterion(total_pred, outputs.unsqueeze(0).float().cuda())
        #batch_loss = criterion(total_pred, outputs.float().cuda())
        #losses.append(batch_loss.item())

        if batch_count % 100 == 0:
            #print('EVAL : Batch #' + str(batch_count) + ': Loss = ' + str(batch_loss.item()))
            print('EVAL : Batch #' + str(batch_count))

    #final_loss = sum(losses) / len(test_data)

    #print('<-------------------Final Validation Loss = '+str(final_loss) + '-------------------->')
    #outs = np.asarray(outs)
    sig = nn.Sigmoid()

    import sklearn.metrics as skm
    m_preds = []
    m_labels = []
    m_h = []
    m_o = []
    m_p = []
    m_ho = []
    m_hp = []

    for i in range(
            len(predictions)):  #<---- NUmber of batches passed to the network
        batch_pred = predictions[i]
        batch_labels = outs[i]
        b_h = p_h[i]
        b_o = p_o[i]
        b_p = p_p[i]
        b_ho = p_ho[i]
        b_hp = p_hp[i]

        for j in range(len(batch_pred)):  #<------ Images per batch
            pred = batch_pred[j]
            pred = sig(pred)
            #pred = (pred>args.threshold).int()
            pred = pred.cpu().numpy()
            #-------------------------
            pr_h = b_h[j]
            pr_h = sig(pr_h)
            pr_h = pr_h.cpu().numpy()
            #-------------------------
            pr_o = b_o[j]
            pr_o = sig(pr_o)
            pr_o = pr_o.cpu().numpy()
            #-------------------------
            pr_p = b_p[j]
            pr_p = sig(pr_p)
            pr_p = pr_p.cpu().numpy()
            #-------------------------
            pr_ho = b_ho[j]
            pr_ho = sig(pr_ho)
            pr_ho = pr_ho.cpu().numpy()
            #-------------------------
            pr_hp = b_hp[j]
            pr_hp = sig(pr_hp)
            pr_hp = pr_hp.cpu().numpy()

            labels = batch_labels[j].cpu().numpy().astype(int)

            m_preds.append(pred)
            m_labels.append(labels)
            m_h.append(pr_h)
            m_o.append(pr_o)
            m_p.append(pr_p)
            m_ho.append(pr_ho)
            m_hp.append(pr_hp)

    # get rare categories:
    rare = []
    with open('rare_list.txt', 'r') as f:
        for line in f:
            idx = line[:-1]
            rare.append(int(idx))
    #rare = np.asarray(rare, dtype=np.int32)

    #Missing classes:
    l = np.zeros(600)
    p = np.random.uniform(low=0., high=0.6, size=(600))
    for i in range(600):
        p[i] = 0.8
        l[i] = 1

    m_preds.append(p)
    m_labels.append(l)
    m_h.append(p)
    m_o.append(p)
    m_p.append(p)
    m_ho.append(p)
    m_hp.append(p)

    m_preds = np.stack(m_preds, axis=0).astype(np.float64)
    m_labels = np.stack(m_labels, axis=0).astype(np.int32)

    m_h = np.stack(m_h, axis=0).astype(np.float64)
    m_o = np.stack(m_o, axis=0).astype(np.float64)
    m_p = np.stack(m_p, axis=0).astype(np.float64)
    m_ho = np.stack(m_ho, axis=0).astype(np.float64)
    m_hp = np.stack(m_hp, axis=0).astype(np.float64)

    print("TOTAL PREDS:")
    c1 = confusion_matrix(600)
    c1.mAP(m_labels, m_preds, rare)
    del c1

    print("HUMAN")
    c2 = confusion_matrix(600)
    c2.mAP(m_labels, m_h, rare)
    del c2

    print("OBJECT")
    c3 = confusion_matrix(600)
    c3.mAP(m_labels, m_o, rare)
    del c3
    print("PAIR")
    c4 = confusion_matrix(600)
    c4.mAP(m_labels, m_p, rare)
    del c4
    print("HUMAN_OBJECT")
    c5 = confusion_matrix(600)
    c5.mAP(m_labels, m_ho, rare)
    del c5
    print("HUMAN_PAIR")
    c6 = confusion_matrix(600)
    c6.mAP(m_labels, m_hp, rare)
예제 #14
0
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(x=j * 0.5 + 0.25 + k,
                 y=i * 0.5 + 0.25,
                 s=cm[i, j],
                 horizontalalignment="center",
                 verticalalignment="center",
                 color="white" if cm[i, j] > thresh else "black")
        k = k * -1

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel(
        'Predicted label\n\n accuracy={:0.4f}\n Precision={:0.4f}\n Recall={:0.4f}\n F1={:0.4f} '
        .format(Accuracy, Precision, Recall, f1))
    plt.show()


training_images = np.load('Features_data.npy')
gender_labels = np.load('Gender_labels.npy')
tr_X, te_X, tr_Y, te_Y = data_preprocessing(training_images, gender_labels)
"param = hypeparameters_determination(tr_X, tr_Y)"
"validation_curve_for_different_parameters_and_figures(tr_X, tr_Y)"
"plot_learning_curve(tr_X, tr_Y)"
pred, model = img_SVM(tr_X, tr_Y, te_X, te_Y)
plot_confusion_matrix(confusion_matrix.confusion_matrix(te_Y, pred, [1, -1]),
                      target_names=['male', 'female'],
                      title="Confusion Matrix")

img_SVM(tr_X, tr_Y, te_X, te_Y)
"img_new_SVM(tr_X, tr_Y, te_X, te_Y)"
    label   = net.blobs['label'].data
    score   = np.array([np.where(i==max(i))[0][0] for i in net.blobs['fc8'].data]).astype(np.float32)

    score   = np.zeros((batch,2), dtype=np.float32)
    for item in range(2):
	score[:,item]  = np.array([np.where(i==max(i))[0][0] for i in net.blobs['fc8_'+str(item+1)].data])

#    score   = np.array([np.where(i==max(i))[0][0] for i in net.blobs['fc8'].data])

    label   = net.blobs['label'].data[:,:,0,0]
    score   = ((net.blobs['fc8'].data>0.5)*1.0).astype(np.float32)

    for n_img in range(batch):
	groundtruth.append(np.where(label[n_img]==1.0)[0].tolist())
	prediction.append(np.where(score[n_img]==1.0)[0].tolist())

cm = confusion_matrix(prediction, groundtruth)
"""
print "label:\n%s"%(str(label[:20]))
print "Prediction:\n%s"%(str((pred[:20]>0)*1.0))
#print "Prediction:\n%s"%(str(pred))

mean_value = [104, 116, 122]#Imagenet mean value
for channel in range(img.shape[3]):
    img[:,:,:,channel] += mean_value[channel]

for n in range(img.shape[0]):
    imshow(img[n],0)
    time.sleep(0.3)
"""