Exemplo n.º 1
0
def main():
    D, cp, ca = loadData()
    #num = 500
    #clfP,clfA = fitting(D[:num,:],cp[:num],ca[:num])
    clfP, clfA = fitting(D, cp, ca)
    joblib.dump(clfP, 'svmmodels/test/SVMl_clfP.pkl')
    joblib.dump(clfA, 'svmmodels/test/SVMl_clfA.pkl')
Exemplo n.º 2
0
def makefit():
    log('training linear model')
    t = time.time()
    D, cp, ca = loadData()
    clfP, clfA, tt = fitting(D, cp, ca)
    dirname = 'linearModel'
    if not os.path.isdir('svmmodels/' + dirname):
        os.mkdir('svmmodels/' + dirname)
    joblib.dump(clfP, 'svmmodels/' + dirname + '/clfP.pkl')
    joblib.dump(clfA, 'svmmodels/' + dirname + '/clfA.pkl')
    log('model trained and saved in %.2f sec' % (time.time() - t))
Exemplo n.º 3
0
def train_and_save(NUM=200):
    tD = np.load('data_4096s/ALLDATAR.npy')
    cp = (tD[:NUM, 2] == 1) | (tD[:NUM, 2] == 3)
    ca = (tD[:NUM, 2] == 2) | (tD[:NUM, 2] == 3)
    D = tD[:NUM, 3:]

    kwargs = {'kernel': 'linear', 'C': 1.0, 'probability': True}

    clfP = svm.SVC(**kwargs)
    clfA = svm.SVC(**kwargs)

    log('fitting plastic')
    t = time.time()
    clfP.fit(D, cp)
    ttime = time.time() - t
    log('in %f sec' % (time.time() - t))
    log('fitting animals')
    t = time.time()
    clfA.fit(D, ca)
    ttime += (time.time() - t)
    log('in %f sec' % (time.time() - t))
    joblib.dump(clfP, 'svmmodels/allprob/clfP.pkl')
    joblib.dump(clfA, 'svmmodels/allprob/clfA.pkl')
Exemplo n.º 4
0
    # Train the svm on this labeled data and pickle it.

    print "Pickling data and labels..."
    Dfile = open(
        "/home/ely/projects/faces/data/features/poselet_data/poselet_data_" +
        str(num_poselets_trained) + ".pkl", 'w')
    Lfile = open(
        "/home/ely/projects/faces/data/features/poselet_labels/poselet_labels_"
        + str(num_poselets_trained) + ".pkl", 'w')

    pickle.dump(DATA, Dfile)
    pickle.dump(LABELS, Lfile)

    Dfile.close()
    Lfile.close()

    print "Training and pickling the SVM..."
    clf = train_svm(DATA, LABELS)
    #classifier_output_file = open("/home/ely/projects/faces/data/classifiers/poselets/poselet_" + str(num_poselets_trained) + "-svm.pkl",'w')
    classifier_output_file = "/home/ely/projects/faces/data/classifiers/poselets/poselet_" + str(
        num_poselets_trained) + "-svm.pkl"
    joblib.dump(clf, classifier_output_file)
    #classifier_output_file.close()

    poselet_end_time = time.time() - poselet_start_time
    print "Finished training poselet classifier %d of %d..." % (
        num_poselets_trained, total_number_of_poselets - 1)
    print "Required %f seconds." % (poselet_end_time)

######################################################
Exemplo n.º 5
0
    
    print "Pickling data and labels..."
    Dfile = open("/home/ely/projects/faces/data/features/poselet_data/poselet_data_"+str(num_poselets_trained)+".pkl",'w')
    Lfile = open("/home/ely/projects/faces/data/features/poselet_labels/poselet_labels_"+str(num_poselets_trained)+".pkl",'w')
    
    pickle.dump(DATA,Dfile)
    pickle.dump(LABELS,Lfile)
    
    Dfile.close()
    Lfile.close()
    
    print "Training and pickling the SVM..."
    clf = train_svm(DATA,LABELS)
    #classifier_output_file = open("/home/ely/projects/faces/data/classifiers/poselets/poselet_" + str(num_poselets_trained) + "-svm.pkl",'w')
    classifier_output_file = "/home/ely/projects/faces/data/classifiers/poselets/poselet_" + str(num_poselets_trained) + "-svm.pkl"
    joblib.dump(clf, classifier_output_file)
    #classifier_output_file.close()
    
    poselet_end_time = time.time() - poselet_start_time
    print "Finished training poselet classifier %d of %d..."%(num_poselets_trained,total_number_of_poselets-1)
    print "Required %f seconds."%(poselet_end_time)
    

######################################################






Exemplo n.º 6
0
    def batchtrain(self, njob=1, phase=None, shared_memory='no', verbose='on'):
        """Batch training which is called for rought training as well as
        finetuning.
        """
        t0 = time()
        nnodes = getattr(self, 'nnodes')
        dlen = getattr(self, 'dlen')
        dim = getattr(self, 'dim')
        mapsize = getattr(self, 'mapsize')

        #############################################
        # Seting the parameters
        initmethod = getattr(self, 'initmethod')
        mn = np.min(mapsize)
        if mn == 1:
            mpd = float(nnodes * 10) / float(dlen)
        else:
            mpd = float(nnodes) / float(dlen)

        ms = max(mapsize[0], mapsize[1])
        if mn == 1:
            ms = ms / 5.
        # Based on somtoolbox, Matlab
        # case 'train', sTrain.trainlen = ceil(50 * mpd);
        # case 'rough', sTrain.trainlen = ceil(10 * mpd);
        # case 'finetune', sTrain.trainlen = ceil(40 * mpd);
        if phase == 'rough':
            # training length
            trainlen = int(np.ceil(10 * mpd))
            # radius for updating
            if initmethod == 'random':
                # trainlen = int(np.ceil(15 * mpd))
                radiusin = max(1, np.ceil(ms / 2.))
                radiusfin = max(1, radiusin / 8.)
            elif initmethod == 'pca':
                radiusin = max(1, np.ceil(ms / 8.))
                radiusfin = max(1, radiusin / 4.)
        elif phase == 'finetune':
            # train lening length
            trainlen = int(np.ceil(40 * mpd))
            # radius for updating
            if initmethod == 'random':
                # trainlen = int(np.ceil(50 * mpd))
                radiusin = max(1, ms / 8.)  # from radius fin in rough training
                radiusfin = max(1, radiusin / 16.)
            elif initmethod == 'pca':
                radiusin = max(1, np.ceil(ms / 8.) / 4)
                radiusfin = 1  # max(1, ms / 128)

        radius = np.linspace(radiusin, radiusfin, trainlen)
        ##################################################

        UD2 = getattr(self, 'UD2')
        New_Codebook_V = np.empty((nnodes, dim))
        New_Codebook_V = getattr(self, 'codebook')

        # print 'data is in shared memory?', shared_memory
        if shared_memory == 'yes':
            data = getattr(self, 'data')
            Data_folder = tempfile.mkdtemp()
            data_name = os.path.join(Data_folder, 'data')
            dump(data, data_name)
            data = load(data_name, mmap_mode='r')
        else:
            data = getattr(self, 'data')
        # X2 is part of euclidean distance (x-y)^2 = x^2 +y^2 - 2xy that we use for
        # each data row in bmu finding.
        # Since it is a fixed value we can skip it during bmu finding for each data
        # point, but later we need it calculate quantification error.
        X2 = np.einsum('ij, ij->i', data, data)
        if verbose == 'on':
            print '%s training...' % phase
            print 'radius_ini: {}, radius_final: {}, trainlen: {}'.format(
                radiusin, radiusfin, trainlen)
        for i in range(trainlen):
            # In case of Guassian neighborhood
            H = np.exp(-1.0 * UD2 / (2.0 * radius[i]**2)).reshape(nnodes, nnodes)
            t1 = time()
            bmu = None
            bmu = self.para_bmu_find(data, New_Codebook_V, njb=njob)
            if verbose == 'on':
                print
            # Updating the codebook
            t2 = time()
            New_Codebook_V = self.update_codebook_voronoi(data, bmu, H, radius)
            # print 'updating nodes: ', round (time() - t2, 3)
            if verbose == 'on':
                print "epoch: {} ---> elapsed time:  {}, quantization error: {}".format(
                    i + 1, round(time() - t1, 3), np.mean(np.sqrt(bmu[1] + X2)))
        self.codebook = New_Codebook_V
        bmu[1] = np.sqrt(bmu[1] + X2)
        self.bmu = bmu
Exemplo n.º 7
0
 def save_model(self):
     joblib.dump(self.model, os.sep.join([settings.RELATED_SEARCH_MODEL_BASE_DIR, self.model_file ]) )