コード例 #1
0
def big_picture_eval():
    from visualize_data import show_images, visualize_data
    from itertools import islice, chain
    from spectral_clustering import knn, spectral_clustering
    img_pre_reshape = lambda img: img.reshape(img.shape[0] * img.shape[1], img.
                                              shape[2])
    img_original = lambda img: img.reshape(reader.RES[0], reader.RES[1])

    for img, gt_iter, fname in islice(reader.request_data(), 5):
        print(0)
        output = kmeans(img_pre_reshape(img), k=5)[1]
        visualize_data(img_original(output), gt_iter, fname)

    for img, gt_iter, fname in islice(reader.request_data(), 5):
        print(1)
        spectral_output = spectral_clustering(img_pre_reshape(img),
                                              k=5,
                                              sim_func=knn,
                                              sim_arg=5)[1]
        visualize_data(img_original(spectral_output), gt_iter, fname)

    for img, gt_iter, fname in islice(reader.request_data(), 5):
        print(2)
        spectral_output = spectral_clustering(img_pre_reshape(img),
                                              k=5,
                                              sim_func=knn,
                                              sim_arg=5)[1]
        kmeans_output = kmeans(img_pre_reshape(img), k=5)[1]
        show_images(
            [img_original(kmeans_output),
             img_original(spectral_output)])
コード例 #2
0
def grouping(features_groupDis1, features_groupDis2, T, args):
    # print(features_groupDis1.size())
    criterion = nn.CrossEntropyLoss().cuda()
    # K-way normalized cuts or k-Means. Default: k-Means
    if args.use_kmeans:
        cluster_label1, centroids1 = KMeans(features_groupDis1,
                                            K=args.clusters,
                                            Niters=args.num_iters)
        cluster_label2, centroids2 = KMeans(features_groupDis2,
                                            K=args.clusters,
                                            Niters=args.num_iters)
    else:
        cluster_label1, centroids1 = spectral_clustering(
            features_groupDis1,
            K=args.k_eigen,
            clusters=args.clusters,
            Niters=args.num_iters)
        cluster_label2, centroids2 = spectral_clustering(
            features_groupDis2,
            K=args.k_eigen,
            clusters=args.clusters,
            Niters=args.num_iters)

    # group discriminative learning
    affnity1 = torch.mm(features_groupDis1, centroids2.t())
    CLD_loss = criterion(affnity1.div_(T), cluster_label2)

    affnity2 = torch.mm(features_groupDis2, centroids1.t())
    CLD_loss = (CLD_loss + criterion(affnity2.div_(T), cluster_label1)) / 2

    return CLD_loss
コード例 #3
0
ファイル: main.py プロジェクト: subincm/hierarchical_nw_align
def letsStartSpectralAlgorithm():
    #hungarian.Hungarian_algo()
    #Form networkx representation of both graphs
    subprocess.call("rm -rf ../../Data/SpectralC", shell=True)
    INPUT_FILE_1 = os.path.join(Constants.NAPA_PATH, ways_type, dataset_type,family_type, Constants.INPUT_FILE_1_NAME )
    INPUT_FILE_2 = os.path.join(Constants.NAPA_PATH, ways_type, dataset_type,family_type, Constants.INPUT_FILE_2_NAME )
    G1 = Utils.convertNetToGefx(INPUT_FILE_1 + Constants.NET_FORMAT)
    G2 = Utils.convertNetToGefx(INPUT_FILE_2 + Constants.NET_FORMAT)
    num_alignment_pairs = 0
    #Run Spectral
    num_clusters = 4
    
    #Do initial clustering
    subgraphs1 = spectral_clustering.spectral_clustering(G1, Constants.INPUT_FILE_1_NAME, num_clusters)
    subgraphs2 = spectral_clustering.spectral_clustering(G2, Constants.INPUT_FILE_2_NAME, num_clusters)
         
    #Does SDF on those clusters. Does not need to do it on all clusters because some might already be fixed
    SDF_PATH = Utils.ComputeSpectralDistance(Constants.INPUT_FILE_1_NAME, Constants.INPUT_FILE_2_NAME, "SpectralC")
        
    #Find best Matching for our bipartite graph
    #Compute cluster parameters
    cluster_edge_weight_matrix = compute_cluster_param.find_cluster_spectraledges_SDF(SDF_PATH, num_clusters, num_clusters)
    best_cluster_pairs = hungarian.Hungarian_algo(cluster_edge_weight_matrix)
    for cluster1,cluster2 in best_cluster_pairs:
            newG1 = subgraphs1[cluster1]
            newG2 = subgraphs2[cluster2]
            if len(newG1.nodes()) >= 900 and  len(newG2.nodes()) >= 900:
                #Write these two graphs gefx files
                #nx.write_gexf(newG1, "../../Data/SpectralC/HC/A.gexf")
                #nx.write_gexf(newG2, "../../Data/SpectralC/HC/B.gexf")
                #Cluster these further
                num_alignment_pairs = heirarchical_clustering.heirarchical_clustering_spec(newG1, newG2, num_alignment_pairs,SDF_PATH,num_clusters,
                                                                                      ways_type,dataset_type, family_type )    
            else:
                #Simply generate alignment file for graph
                #Generate alignment score of our cluster graphs
                
                heirarchical_dir = "../../Data/SpectralC/IntermC"
                if os.path.exists(heirarchical_dir):
                    subprocess.call("rm -rf "+heirarchical_dir, shell=True)
                os.makedirs(heirarchical_dir)
                
                subgraphpath1 = os.path.join(heirarchical_dir,"A_"+str(num_alignment_pairs)+".gexf")
                subgraphpath2 = os.path.join(heirarchical_dir,"B_"+str(num_alignment_pairs)+".gexf")
                nx.write_gexf(newG1, subgraphpath1)
                nx.write_gexf(newG2, subgraphpath2)
                generate_alignment.generate_spectralcluster_alignment_score(subgraphpath1, subgraphpath2, "SpectralC", "SDF",
                             ways_type,dataset_type, family_type, num_clusters, num_alignment_pairs)
                num_alignment_pairs = num_alignment_pairs + 1
    #Give final output
    generate_alignment.generateSpectralFinalScore(INPUT_FILE_1, INPUT_FILE_2,ways_type,dataset_type, family_type, num_clusters )
コード例 #4
0
def main():
    # Initialize algorithm parameters
    K, N, d, r, MAX_ITER = initialize()
    if K >= N or K <= 0:
        print("Error in data and/or parameters")
        return 0
    # Generate data
    dataMatrix, y = make_blobs(n_samples=N,
                               centers=K,
                               n_features=d,
                               random_state=None)
    dataList = dataMatrix.tolist()

    # Normalized Spectral Clustering Algorithm
    T, new_K = spectral_clustering(
        dataMatrix, r, K)  # new_k is the 'eigengap heuristic' calculated k
    TList = T.tolist()
    Kfirstcentroids_spectral = k_means_pp(T, new_K).tolist()

    # K-means Algorithm
    Kfirstcentroids_Kmeans = k_means_pp(dataMatrix, new_K).tolist()
    # Call alg.Kmeans
    try:
        Clusters_Spectral = alg.Kmeans(new_K, N, new_K, MAX_ITER, TList,
                                       Kfirstcentroids_spectral)
        Clusters_Kmeans = alg.Kmeans(new_K, N, d, MAX_ITER, dataList,
                                     Kfirstcentroids_Kmeans)
    except NameError:
        return 0  # Print error massages in kmeans.c file

    # Output files methods
    outputTextFiles(dataList, y, Clusters_Spectral, Clusters_Kmeans, new_K, N)
    graphic(dataMatrix, y, N, K, new_K, d, Clusters_Spectral, Clusters_Kmeans)
コード例 #5
0
def main():

    #Step 1: Read in image
    #frame_0 = helper.import_im('../src/images/FBMS_marple13/marple13_20.jpg')
    #frame_1 = helper.import_im('../src/images/FBMS_marple13/marple13_21.jpg')

    #Step 2: Threshold
    #Note: This step take lots of time therefore we get result of this threshold for all 75 frames of maple13 in images/Marple13_eig/
    #threshold_frame_0 = threshold.threshold_eigenvalues(frame_0, 50000)
    #threshold_frame_1 = threshold.threshold_eigenvalues(frame_1, 50000)

    #Hardcode frame
    frame_0 = helper.import_im('../src/images/Marple13_eig/eig_marple13_22.jpg')
    frame_1 = helper.import_im('../src/images/Marple13_eig/eig_marple13_23.jpg')

    frames = [frame_0, frame_1]

    #Grab frame of marple13 in order
    path = '../src/images/Marple13_eig'
    directory = os.fsencode(path)

    for file in sorted(os.listdir(directory)):
        filename = os.fsdecode(file)
        if filename.endswith('.jpg'):
            im = helper.import_im(path + '/' + filename)
            #frames.append(im)
        if filename.endswith('20.jpg'):
            break

    #Step 3: Build trajectory and affinity
    trajectories = tracking.create_trajectories(frames)

    A = affinity.calculate_A(trajectories, gamma = 0.1)
    #np.savetxt("A_out.csv", A, delimiter=",")

    #Step 4: Perform spectal clustering
    clustering = spectral_clustering.spectral_clustering(df=A, n_neighbors=3, n_clusters=3)

    for i in range(len(clustering)):
        trajectories[i].label = clustering[i]

    #Display result
    fig = plt.figure(figsize=(7, 7))
    plt.imshow(frame_0, cmap='gray')
    for i in range(len(trajectories)):
        point = trajectories[i].history[0]
        label = trajectories[i].label
        col=point[1]
        row=point[0]
        if label == 0:
            plt.scatter(col,row,c='b')
        if label == 1:
            plt.scatter(col,row,c='y')
        if label == 2:
            plt.scatter(col,row,c='r')
        if label == 3:
            plt.scatter(col,row,c='g')
    plt.show()
コード例 #6
0
def image_segmentation(input_img='fruit_salad.bmp', eig_max=15):
    """
    TO BE COMPLETED

    Function to perform image segmentation.

    :param input_img: name of the image file in /data (e.g. 'four_elements.bmp')
    """
    filename = os.path.join('data', input_img)

    X = io.imread(filename)
    X = (X - np.min(X)) / (np.max(X) - np.min(X))
    #print(X.shape)

    im_side = np.size(X, 1)
    Xr = X.reshape(im_side**2, 3)
    #print(Xr.shape)
    """
    Y_rec should contain an index from 0 to c-1 where c is the     
     number of segments you want to split the image into          
    """
    """
    Choose parameters
    """

    var = 5
    k = 45
    laplacian_normalization = 'unn'

    W = build_similarity_graph(Xr, var=var, k=k)

    L = build_laplacian(W, laplacian_normalization)

    E, U = np.linalg.eig(L)
    indexes = np.argsort(E)
    E = E[indexes]
    U = U[:, indexes]
    chosen_eig_indices = choose_eigenvalues(E, eig_max)
    #chosen_eig_indices = [0,1,2,3]

    num_classes = len(chosen_eig_indices)

    #print(len(chosen_eig_indices))

    Y_rec = spectral_clustering(L, chosen_eig_indices, num_classes=num_classes)

    plt.figure()

    plt.subplot(1, 2, 1)
    plt.imshow(X)

    plt.subplot(1, 2, 2)
    Y_rec = Y_rec.reshape(im_side, im_side)
    plt.imshow(Y_rec)

    plt.show()
コード例 #7
0
def image_segmentation(input_img='four_elements.bmp'):
    """
    TO BE COMPLETED

    Function to perform image segmentation.

    :param input_img: name of the image file in /data (e.g. 'four_elements.bmp')
    """
    filename = os.path.join('data', input_img)

    X = io.imread(filename)
    X = (X - np.min(X)) / (np.max(X) - np.min(X))

    im_side = np.size(X, 1)
    Xr = X.reshape(im_side**2, 3)
    print(Xr.shape)
    """
    Y_rec should contain an index from 0 to c-1 where c is the     
     number of segments you want to split the image into          
    """
    """
    Choose parameters
    """
    var = 10  # Tried multiple values before fixing it to this
    k = 60  # Tried multiple values before fixing it to this
    laplacian_normalization = 'sym'
    chosen_eig_indices = [
        1, 2
    ]  # The eigen vectors to consider for the clustering, not used if using adaptive spectral clustering
    num_classes = 5  # Fixed by hand, we can also look at the values of the eigenvectors to infer this number

    # First we build the graph using KNN (this is what takes few minutes)
    #    W = build_similarity_graph(Xr, var=var, k=0, eps= 0.5)
    W = build_similarity_graph(Xr, var=var, k=0, eps=0.7)
    # Build le laplacian matrix
    L = build_laplacian(W, laplacian_normalization)
    # Perform the spectral clustering where we choose automatically the number
    # of eigenvectors to consider using first order derivatives or by hand

    Y_rec = spectral_clustering(L, chosen_eig_indices, num_classes=num_classes)
    #    Y_rec = spectral_clustering_adaptive(L, num_classes=num_classes)

    plt.figure()

    plt.subplot(1, 2, 1)
    plt.imshow(X)
    #    plt.imshow(X[:30,:30,:])
    plt.subplot(1, 2, 2)
    Y_rec = Y_rec.reshape(im_side, im_side)
    #    Y_rec = Y_rec.reshape(30,30)
    plt.imshow(Y_rec)

    plt.show()
コード例 #8
0
def hog_spectral_bow_svm(cluster_size, svm_param_list, dir='../Caltech20/'):
    dataset = 'training'
    features, labels = feature_and_label_extraction_for_nonflat(
        dir, dataset,
        cell_size=32)  # cell size has to be 32 because of spectral clustering
    no_images = features.shape[0]
    cluster_model, centroids = spectral_clustering(features.reshape((-1, 36)),
                                                   cluster_size)
    cluster_labels = cluster_model.labels_.reshape((no_images, -1))
    hists = bag_of_words(cluster_labels, cluster_size)
    classifier = train_svm(hists, labels, svm_param_list)
    print('Training Accuracy: ' + str(classifier.score(hists, labels)))
    return classifier, centroids
コード例 #9
0
ファイル: WSClustering.py プロジェクト: DwyerKou/WSClustering
def callback():
    wsdl_path = e1.get()
    k = e2.get()
    start = time.time()
    print(start)
    # 建立目录
    wsdl_father_path = wsdl_path[0:wsdl_path.rfind('/')]
    service_name_path = wsdl_father_path + '/serviceName'
    service_name_stemmed_path = wsdl_father_path + '/serviceNameStemmed'
    if not os.path.exists(service_name_path):
        os.mkdir(service_name_path)
    if not os.path.exists(service_name_stemmed_path):
        os.mkdir(service_name_stemmed_path)

    subprocess.call(['java', '-jar', 'ServiceNameParsing.jar', wsdl_path])
    service_num = WordProc.WordProc(service_name_path)
    word2vec.Word2Vec(service_name_stemmed_path)
    service_name_sim.service_name_sim(service_name_stemmed_path, service_num)
    clustering_result = spectral_clustering.spectral_clustering(k, service_num)
    print(clustering_result)
    end = time.time()
    lb0.insert(END, "服务数量: " + str(service_num))
    lb0.insert(END, "聚类数量: " + str(k))
    lb0.insert(END, "消耗时间: " + str(end - start) + "秒")
    for j in range(0, int(k)):
        lb0.insert(
            END,
            "第" + str(j + 1) + "个聚类中服务的数量: " + str(clustering_result.count(j)))

    lb1.insert(END, "服务       所在聚类")
    for i in range(0, service_num):
        if i < 9:
            lb1.insert(
                END,
                str(i + 1) + "              " + str(clustering_result[i] + 1))
        if i >= 9:
            lb1.insert(
                END,
                str(i + 1) + "            " + str(clustering_result[i] + 1))

    for i in range(0, int(k)):
        lb2.insert(END, "聚类" + str(i + 1) + "中的服务")
        re = [j for j, a in enumerate(clustering_result) if a == i]
        for k in range(0, len(re)):
            lb2.insert(END, "  " + str(re[k] + 1))
コード例 #10
0
def image_segmentation(input_img='four_elements.bmp'):
    """
    TO BE COMPLETED

    Function to perform image segmentation.

    :param input_img: name of the image file in /data (e.g. 'four_elements.bmp')
    """
    filename = os.path.join('data', input_img)

    X = io.imread(filename)
    X = (X - np.min(X)) / (np.max(X) - np.min(X))

    im_side = np.size(X, 1)
    Xr = X.reshape(im_side ** 2, 3)
    """
    Y_rec should contain an index from 0 to c-1 where c is the     
     number of segments you want to split the image into          
    """

    """
    Choose parameters
    """
    var = 1.0
    k = 25
    laplacian_normalization = 'rw'
    chosen_eig_indices = [1, 2, 3, 4, 5]
    num_classes = 5

    W = build_similarity_graph(Xr, var=var, k=k)
    L = build_laplacian(W, laplacian_normalization)
    Y_rec = spectral_clustering(L, chosen_eig_indices, num_classes=num_classes)

    plt.figure()

    plt.subplot(1, 2, 1)
    plt.imshow(X)

    plt.subplot(1, 2, 2)
    Y_rec = Y_rec.reshape(im_side, im_side)
    plt.imshow(Y_rec)

    plt.show()
コード例 #11
0
ファイル: WSClustering.py プロジェクト: DwyerKou/WSClustering
def callback():
    wsdl_path = e1.get()
    k = e2.get()
    start = time.time()
    print(start)
    # 建立目录
    wsdl_father_path = wsdl_path[0: wsdl_path.rfind('/')]
    service_name_path = wsdl_father_path + '/serviceName'
    service_name_stemmed_path = wsdl_father_path + '/serviceNameStemmed'
    if not os.path.exists(service_name_path):
        os.mkdir(service_name_path)
    if not os.path.exists(service_name_stemmed_path):
        os.mkdir(service_name_stemmed_path)

    subprocess.call(['java', '-jar', 'ServiceNameParsing.jar', wsdl_path])
    service_num = WordProc.WordProc(service_name_path)
    word2vec.Word2Vec(service_name_stemmed_path)
    service_name_sim.service_name_sim(service_name_stemmed_path, service_num)
    clustering_result = spectral_clustering.spectral_clustering(k, service_num)
    print(clustering_result)
    end = time.time()
    lb0.insert(END, "服务数量: " + str(service_num))
    lb0.insert(END, "聚类数量: " + str(k))
    lb0.insert(END, "消耗时间: " + str(end - start) + "秒")
    for j in range(0, int(k)):
        lb0.insert(END, "第" + str(j + 1) + "个聚类中服务的数量: " + str(clustering_result.count(j)))

    lb1.insert(END, "服务       所在聚类")
    for i in range(0, service_num):
        if i < 9:
            lb1.insert(END, str(i+1) + "              " + str(clustering_result[i] + 1))
        if i >= 9:
            lb1.insert(END, str(i+1) + "            " + str(clustering_result[i] + 1))

    for i in range(0, int(k)):
        lb2.insert(END, "聚类" + str(i + 1) + "中的服务")
        re = [j for j, a in enumerate(clustering_result) if a == i]
        for k in range(0, len(re)):
            lb2.insert(END, "  " + str(re[k] + 1))
コード例 #12
0
    ['image2spectral clustering(k=3, normal)', 'image2spectral clustering(k=4, normal)', 'image2spectral clustering(k=5, normal)'], \
    ['image2spectral clustering(k=3, ratio)', 'image2spectral clustering(k=4, ratio)', 'image2spectral clustering(k=5, ratio)']]

if not os.path.exists('./output'):
    os.mkdir('./output')

for i in range(2):
    print('processing image{}...'.format(i + 1))
    img = imageio.imread(img_path[i])
    spatial_data, color_data = img_formater(img)
    similarity = rbf(spatial_data, spatial_data) * rbf(color_data, color_data)

    max_fram = 0
    for j in range(3):
        sc1 = spectral_clustering(similarity,
                                  k=3 + j,
                                  normalize=True,
                                  keep_log=True)
        sc2 = spectral_clustering(similarity,
                                  k=3 + j,
                                  normalize=False,
                                  keep_log=True)

        (record_norm, iter_norm), eigenvalues1, eigenvectors1 = sc1.run()
        visualizer(record_norm, 'output/' + gif_path[(i * 2)][j] + '.gif')
        (record_ratio, iter_ratio), eigenvalues2, eigenvectors2 = sc2.run()
        visualizer(record_ratio, 'output/' + gif_path[(i * 2) + 1][j] + '.gif')

        max_fram = max(max_fram, max(iter_norm, iter_ratio))
        if iter_norm <= iter_ratio:
            print('faster.....................[\033[94mnormalize\033[0m]')
        else:
コード例 #13
0
def train_moco(epoch, train_loader, model, model_ema, contrast, criterion, optimizer, scheduler, args):
    """
    one epoch training for moco
    """
    model.train()
    set_bn_train(model_ema)

    batch_time = AverageMeter()
    data_time = AverageMeter()
    loss_meter = AverageMeter()
    prob_meter = AverageMeter()

    train_CLD_loss = AverageMeter()
    train_CLD_acc = AverageMeter()
    criterion_cld = nn.CrossEntropyLoss().cuda()

    end = time.time()
    torch.set_num_threads(1)
    for idx, ((x1, x2, x3), targets, index) in enumerate(train_loader):
        data_time.update(time.time() - end)

        bsz = x1.size(0)

        x1 = x1.cuda()
        x2 = x2.cuda()
        x3 = x3.cuda()

        feat_q1, features_groupDis1 = model(x1, two_branch=True)
        feat_q2, features_groupDis2 = model(x2, two_branch=True)

        with torch.no_grad():
            x3_shuffled, backward_inds = DistributedShufle.forward_shuffle(x3, epoch)
            feat_k3, features_groupDis3 = model_ema(x3_shuffled, two_branch=True)
            feat_k_all, feat_k3, features_groupDis3 = DistributedShufle.backward_shuffle(
                feat_k3, backward_inds, return_local=True, branch_two=features_groupDis3)

        # NCE loss
        out = contrast(feat_q1, feat_k3, feat_k_all, update=False)
        loss_1 = criterion(out)

        out = contrast(feat_q2, feat_k3, feat_k_all, update=True)
        loss_2 = criterion(out)
        loss = (loss_1 + loss_2)/2

        prob = F.softmax(out, dim=1)[:, 0].mean()

        # K-way normalized cuts or k-Means. Default: k-Means
        if args.use_kmeans:
            cluster_label1, centroids1 = KMeans(features_groupDis1, K=args.clusters, Niters=args.num_iters)
            cluster_label2, centroids2 = KMeans(features_groupDis2, K=args.clusters, Niters=args.num_iters)
        else:
            cluster_label1, centroids1 = spectral_clustering(features_groupDis1, K=args.k_eigen,
                        clusters=args.clusters, Niters=args.num_iters)
            cluster_label2, centroids2 = spectral_clustering(features_groupDis2, K=args.k_eigen,
                        clusters=args.clusters, Niters=args.num_iters)

        # instance-group discriminative learning
        affnity1 = torch.mm(features_groupDis1, centroids2.t())
        CLD_loss = criterion_cld(affnity1.div_(args.cld_t), cluster_label2)

        affnity2 = torch.mm(features_groupDis2, centroids1.t())
        CLD_loss = (CLD_loss + criterion_cld(affnity2.div_(args.cld_t), cluster_label1))/2

        # get cluster label prediction accuracy
        _, cluster_pred = torch.topk(affnity1, 1)
        cluster_pred = cluster_pred.t()
        correct = cluster_pred.eq(cluster_label2.view(1, -1).expand_as(cluster_pred))
        correct_all = correct[0].view(-1).float().sum(0).mul_(100.0/x1.size(0))
        train_CLD_acc.update(correct_all.item(), x1.size(0))

        # total loss
        loss = loss + args.Lambda*CLD_loss

        if torch.isnan(loss):
            print('INFO loss is nan! Backward skipped')
            continue

        # backward
        optimizer.zero_grad()
        optimizer.zero_grad()
        if args.amp_opt_level != "O0":
            with amp.scale_loss(loss, optimizer) as scaled_loss:
                scaled_loss.backward()
        else:
            loss.backward()
        optimizer.step()
        scheduler.step()

        moment_update(model, model_ema, args.alpha)
        train_CLD_loss.update(CLD_loss.item(), x1.size(0))

        # update meters
        loss_meter.update(loss.item(), bsz)
        prob_meter.update(prob.item(), bsz)
        batch_time.update(time.time() - end)
        end = time.time()

        # print info
        lr = optimizer.param_groups[0]['lr']
        if idx % args.print_freq == 0:
            logger.info(f'Train: [{epoch}][{idx}/{len(train_loader)}] lr: {lr:.5f}\t'
                        f'T {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
                        f'DT {data_time.val:.3f} ({data_time.avg:.3f})\t'
                        f'loss {loss_meter.val:.3f} ({loss_meter.avg:.3f})\t'
                        f'prob {prob_meter.val:.3f} ({prob_meter.avg:.3f})\t'
                        f'CLD loss {train_CLD_loss.val:.3f} ({train_CLD_loss.avg:.3f})\t'
                        f'Top-1 acc {train_CLD_acc.val:.3f} ({train_CLD_acc.avg:.3f})')

    return loss_meter.avg, prob_meter.avg
コード例 #14
0
def train(epoch):
    print('\nEpoch: %d' % epoch)
    torch.set_num_threads(1)
    if args.lr_scheduler == 'cosine':
        trainloader.sampler.set_epoch(epoch)
    train_loss = AverageMeter()
    data_time = AverageMeter()
    batch_time = AverageMeter()
    train_CLD_loss = AverageMeter()
    train_CLD_acc = AverageMeter()

    # switch to train mode
    net.train()

    end = time.time()
    for batch_idx, (inputs, targets, indexes) in enumerate(trainloader):
        data_time.update(time.time() - end)
        targets, indexes = targets.to(device), indexes.to(device)

        # If two_imgs: one is used for F1, another is used for F2. F1 comes from branch1 of net
        # F2 comes from branch1 if only one branch exists else branch2.
        if args.two_imgs:
            inputs1 = inputs[0].to(device)
            inputs2 = inputs[1].to(device)
        else:
            inputs1 = inputs.to(device)
        optimizer.zero_grad()

        features_insDis1, features_batchDis1 = net(inputs1, two_branch=True)
        outputs1 = lemniscate(features_insDis1, indexes)

        # NCE loss
        insDis_loss = criterion(outputs1, indexes)

        if args.two_imgs:
            features_insDis2, features_batchDis2 = net(inputs2,
                                                       two_branch=True)
            outputs2 = lemniscate(features_insDis2, indexes)
            # NCE loss
            loss_nce_2 = criterion(outputs2, indexes)
            insDis_loss = (insDis_loss + loss_nce_2) / 2

        # K-way normalized cuts or k-Means. Default: k-Means
        if args.use_kmeans:
            cluster_label1, centroids1 = KMeans(features_batchDis1,
                                                K=args.clusters,
                                                Niters=args.num_iters)
            cluster_label2, centroids2 = KMeans(features_batchDis2,
                                                K=args.clusters,
                                                Niters=args.num_iters)
        else:
            cluster_label1, centroids1 = spectral_clustering(
                features_batchDis1,
                K=args.k_eigen,
                clusters=args.clusters,
                Niters=args.num_iters)
            cluster_label2, centroids2 = spectral_clustering(
                features_batchDis2,
                K=args.k_eigen,
                clusters=args.clusters,
                Niters=args.num_iters)

        # instance-group discriminative learning
        affnity1 = torch.mm(features_batchDis1, centroids2.t())
        CLD_loss = criterion_cld(affnity1.div_(args.cld_t), cluster_label2)

        affnity2 = torch.mm(features_batchDis2, centroids1.t())
        CLD_loss = (CLD_loss + criterion_cld(affnity2.div_(args.cld_t),
                                             cluster_label1)) / 2

        # get cluster label prediction accuracy
        _, cluster_pred = torch.topk(affnity1, 1)
        cluster_pred = cluster_pred.t()
        correct = cluster_pred.eq(
            cluster_label2.view(1, -1).expand_as(cluster_pred))
        correct_all = correct[0].view(-1).float().sum(0).mul_(100.0 /
                                                              inputs1.size(0))
        train_CLD_acc.update(correct_all.item(), inputs1.size(0))

        # total loss
        loss = insDis_loss + args.Lambda * CLD_loss

        if torch.isnan(loss):
            print('INFO loss is nan! Backward skipped')
            continue
        # loss.backward()
        if args.opt_level != "O0":
            with amp.scale_loss(loss, optimizer) as scaled_loss:
                scaled_loss.backward()
        else:
            loss.backward()
        optimizer.step()
        scheduler.step()

        train_loss.update(loss.item(), inputs1.size(0))
        train_CLD_loss.update(CLD_loss.item(), inputs1.size(0))

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        # print info
        lr = optimizer.param_groups[0]['lr']
        if batch_idx % 10 == 0:
            print(
                'Epoch: [{}][{}/{}]'
                'Time: {batch_time.val:.3f} ({batch_time.avg:.3f}) '
                'Data: {data_time.val:.3f} ({data_time.avg:.3f}) '
                'lr: {:.6f} '
                'Loss: {train_loss.val:.4f} ({train_loss.avg:.4f}) '
                'CLD loss: {train_cld_loss.val:.4f} ({train_cld_loss.avg:.4f}) '
                'Group acc: {train_CLD_acc.val:.4f} ({train_CLD_acc.avg:.4f})'.
                format(epoch,
                       batch_idx,
                       len(trainloader),
                       lr,
                       batch_time=batch_time,
                       data_time=data_time,
                       train_loss=train_loss,
                       train_cld_loss=train_CLD_loss,
                       train_CLD_acc=train_CLD_acc))
コード例 #15
0
ファイル: part_h.py プロジェクト: Patrick233/Machine-Learning
if __name__ == "__main__":
    data = sio.loadmat('ExtYaleB10.mat')
    train_sample = data['train']
    test_sample = data['test']
    x_train_full = np.column_stack(train_sample[0, i][:, :,
                                                      j].reshape(192 * 168, 1)
                                   for i in range(10) for j in range(50))
    x_test_full = np.column_stack(test_sample[0, i][:, :,
                                                    j].reshape(192 * 168, 1)
                                  for i in range(10) for j in range(14))
    I = np.identity(10)
    y_train = np.column_stack(I[:, i] for i in range(10) for j in range(50))
    y_test = np.column_stack(I[:, i] for i in range(10) for j in range(14))

    print("spectral_clustering computing")
    v = spectral.spectral_clustering(x_train_full, 10, 1, 10)
    for i in range(v.shape[1]):
        v[:, i] = v[:, i] / (la.norm(v[:, i]))
    final_c, final_z = km.k_means(np.asarray(v.T), 10, 10)
    y_train = np.zeros(500, )
    cost = 0
    for i in range(10):
        for j in range(50):
            y_train[i * 50 + j] = i
    for i in range(500):
        for j in range(10):
            if final_z[i, j] == 1:
                if y_train[i] != j:
                    cost += 1
    print("Errors of spectral clustering", cost)
コード例 #16
0
ファイル: main.py プロジェクト: humoncy/mlhw5
    # plt.scatter(data[:, 0], data[:, 1], c=kernel_kmeans.fit_predict(data))
    # plt.show()
    #

    # Kernel k-means on my own
    cluster_assignment = kernel_k_means(data, k, gamma=0.03125)
    show_cluster(data, k, cluster_assignment, title="Kernel K-means clusters", data_name=data_name)

    # sklearn spectral clustering
    # spectral_clusters = SpectralClustering(n_clusters=2, gamma=0.1)
    # plt.scatter(data[:, 0], data[:, 1], c=spectral_clusters.fit_predict(data))
    # plt.show()
    #

    # Spectral clustering on my own
    cluster_assignment = spectral_clustering(data, k, gamma=0.1)
    show_cluster(data, k, cluster_assignment, title="Spectral clustering", data_name=data_name)

'''
    The following code is used to run different number of clusters at the same time
'''
    # ks = [2, 3, 5, 10]
    # datas = [data1, data2]
    # # print(datas[1].shape)
    # # data = datas[1]
    # # plt.scatter(data[:, 0], data[:, 1], c=gt2)
    # # plt.title("Ground Truth of " + data_name)
    # # plt.show()
    # for data_index in range(2):
    #     for index, val in enumerate(ks):
    #         data = datas[data_index]