コード例 #1
0
def drop_exp_2(r_feat_val, r_feat_train, pred):
    # incep_score, mode_score, fid
    n_mode = len(Counter(pred))
    scores = np.zeros((n_mode, 3))
    t_feat = r_feat_train.clone()
    collapsed_order = torch.randperm(n_mode).long()
    index = torch.arange(0, r_feat_train.size(0)).long()
    collapsed = torch.zeros(r_feat_train.size(0)).byte()
    Mxx = distance(r_feat_val, r_feat_val, sqrt=True)

    for i in range(n_mode):
        # Compute Score
        Mxy = distance(r_feat_val, t_feat, sqrt=True)
        Myy = distance(t_feat, t_feat, sqrt=True)
        scores[i, 0] = inception_score(t_feat)
        scores[i, 1] = mode_score(t_feat, r_feat_val)
        scores[i, 2] = fid(t_feat, r_feat_val)

        # Do drop -- fill dropped slots with remaining samples
        c = collapsed_order[i]
        collapsed[pred.eq(c)] = 1
        cidx = index[collapsed.eq(1)]
        ncidx = index[collapsed.ne(1)]
        if ncidx.dim() == 0 or cidx.dim() == 0 or ncidx.size(0) == 0:
            continue
        for j in cidx:
            copy_idx = np.random.randint(0, ncidx.size(0))
            t_feat[j] = t_feat[ncidx[copy_idx]]

    return scores
コード例 #2
0
def drop_exp_2(r_feat_val, r_feat_train, pred):
    # incep_score, mode_score, fid
    n_mode = len(Counter(pred))
    scores = np.zeros((n_mode, 3))
    t_feat = r_feat_train.clone()
    collapsed_order = torch.randperm(n_mode).long()
    index = torch.arange(0, r_feat_train.size(0)).long()
    collapsed = torch.zeros(r_feat_train.size(0)).byte()
    Mxx = distance(r_feat_val, r_feat_val, sqrt=True)
    
    for i in range(n_mode):
        # Compute Score
        Mxy = distance(r_feat_val, t_feat, sqrt=True)
        Myy = distance(t_feat, t_feat, sqrt=True)
        scores[i, 0] = inception_score(t_feat)
        scores[i, 1] = mode_score(t_feat, r_feat_val)
        scores[i, 2] = fid(t_feat, r_feat_val)
        
        # Do drop -- fill dropped slots with remaining samples
        c = collapsed_order[i]
        collapsed[pred.eq(c)] = 1
        cidx = index[collapsed.eq(1)]
        ncidx = index[collapsed.ne(1)]
        if ncidx.dim() == 0 or cidx.dim() == 0 or ncidx.size(0) == 0:
            continue
        for j in cidx:
            copy_idx = np.random.randint(0, ncidx.size(0))
            t_feat[j] = t_feat[ncidx[copy_idx]]
            
    return scores
コード例 #3
0
def overfit_exp_2(r_feat_val, r_feat_train, step=200):
    # incep_score, mode_score, fid
    n_mode = r_feat_train.size(0) // step
    scores = np.zeros((n_mode + 1, 3))
    t_feat = r_feat_train.clone()
    collapsed_order = torch.randperm(n_mode).long()
    index = torch.arange(0, r_feat_train.size(0)).long()
    collapsed = torch.zeros(r_feat_train.size(0)).byte()
    Mxx = distance(r_feat_val, r_feat_val, sqrt=True)

    for i in range(n_mode + 1):
        # Compute Score
        Mxy = distance(r_feat_val, t_feat, sqrt=True)
        Myy = distance(t_feat, t_feat, sqrt=True)
        scores[i, 0] = inception_score(t_feat)
        scores[i, 1] = mode_score(t_feat, r_feat_val)
        scores[i, 2] = fid(t_feat, r_feat_val)

        # Copy samples so as to overfit
        if i == n_mode: break
        t_feat[i * step:(i + 1) * step] = r_feat_val[i * step:(i + 1) * step]

    return scores
コード例 #4
0
def overfit_exp_2(r_feat_val, r_feat_train, step=200):
    # incep_score, mode_score, fid
    n_mode = r_feat_train.size(0) // step
    scores = np.zeros((n_mode+1, 3))
    t_feat = r_feat_train.clone()
    collapsed_order = torch.randperm(n_mode).long()
    index = torch.arange(0, r_feat_train.size(0)).long()
    collapsed = torch.zeros(r_feat_train.size(0)).byte()
    Mxx = distance(r_feat_val, r_feat_val, sqrt=True)
    
    for i in range(n_mode+1):
        # Compute Score
        Mxy = distance(r_feat_val, t_feat, sqrt=True)
        Myy = distance(t_feat, t_feat, sqrt=True)
        scores[i, 0] = inception_score(t_feat)
        scores[i, 1] = mode_score(t_feat, r_feat_val)
        scores[i, 2] = fid(t_feat, r_feat_val)
        
        # Copy samples so as to overfit
        if i == n_mode: break
        t_feat[i*step:(i+1)*step] = r_feat_val[i*step:(i+1)*step]
    
    return scores
コード例 #5
0
def collapse_exp_2(r_feat_val, r_feat, c_feat, pred):
    # incep_score, mode_score, fid
    n_mode = c_feat.size(0)
    c_feat_repeat = c_feat[pred]
    scores = np.zeros((n_mode, 3))
    t_feat = r_feat.clone()
    index = torch.arange(0, 2000).long()
    collapsed_order = torch.randperm(n_mode).long()
    Mxx = distance(r_feat_val, r_feat_val, sqrt=False)

    for i in range(n_mode):
        # Compute Score
        Mxy = distance(r_feat_val, t_feat, sqrt=False)
        Myy = distance(t_feat, t_feat, sqrt=False)
        scores[i, 0] = inception_score(t_feat)
        scores[i, 1] = mode_score(t_feat, r_feat_val)
        scores[i, 2] = fid(t_feat, r_feat_val)

        # Do collapse
        c = collapsed_order[i]
        cidx = index[pred.eq(c)]
        t_feat[cidx] = c_feat_repeat[cidx]

    return scores
コード例 #6
0
def collapse_exp_2(r_feat_val, r_feat, c_feat, pred):
    # incep_score, mode_score, fid
    n_mode = c_feat.size(0)
    c_feat_repeat = c_feat[pred]
    scores = np.zeros((n_mode, 3))
    t_feat = r_feat.clone()
    index = torch.arange(0, 2000).long()
    collapsed_order = torch.randperm(n_mode).long()
    Mxx = distance(r_feat_val, r_feat_val, sqrt=False)
    
    for i in range(n_mode):
        # Compute Score
        Mxy = distance(r_feat_val, t_feat, sqrt=False)
        Myy = distance(t_feat, t_feat, sqrt=False)
        scores[i, 0] = inception_score(t_feat)
        scores[i, 1] = mode_score(t_feat, r_feat_val)
        scores[i, 2] = fid(t_feat, r_feat_val)
        
        # Do collapse 
        c = collapsed_order[i]
        cidx = index[pred.eq(c)]
        t_feat[cidx] = c_feat_repeat[cidx]
        
    return scores
コード例 #7
0
def run_combine(t):
    print_prop(t)
    if t.mode == "Mix" or t.mode == "Entropy":
        mkdir(g.default_repo_dir + "samples/Mix")
        print("running mix mod..")
        mkdir(g.default_repo_dir + "rlt")
        for sp in range(0, 5 if t.mixOnly else 1):
            def getName():
                ans = 'mix_' if t.mode == "Mix" else 'entropy_'
                ans = ans + t.data + '_' + t.featureType + '_'
                for entry in t.dataList:
                    ans = ans + entry.data + '_' + entry.folder + '_' + \
                        ('%.2f' % entry.fraction) + '_' + \
                        str(entry.dup) + '_' + str(entry.imageMode) + '_'
                return ans + str(t.mixSize) + '_' + str(sp)
            saveFile = g.default_repo_dir + "rlt/" + getName()

            if os.path.exists(saveFile):
                print(saveFile + " already generated. Continue.")
                continue

            if t.mode == "Mix":
                mixDat = getDat(t, t.dataList, g.default_repo_dir +
                                "samples/Mix/M1" + hostname, t.featureType, t.mixOnly)
                dataList = [Ent(1, t.data, 'true', None)]
                print('ftype', t.featureType)
                trueDat = getDat(t, dataList, g.default_repo_dir +
                                 "samples/Mix/M2" + hostname, t.featureType)
                score = compute_score(trueDat, mixDat, 1, 1, True)
            else:
                t.mixSize = t.mixSize * 10
                mixDat = getDat(t, t.dataList, g.default_repo_dir +
                                "samples/Mix/M1" + hostname, t.featureType, t.mixOnly)
                t.mixSize = t.mixSize / 10
                dataList = [Ent(1, t.data, 'true_test', None)]
                trueDat = getDat(t, dataList, g.default_repo_dir +
                                 "samples/Mix/M2" + hostname, t.featureType)
                if t.featureType == 'pix':
                    epsilons = [1000]
                elif t.featureType == 'smax':
                    epsilons = [0.001]
                elif t.featureType == 'class':
                    epsilons = [0.1]
                elif t.featureType == 'conv':
                    epsilons = [1000]
                score = entropy_score(mixDat, trueDat, epsilons)
            f = open(saveFile, "wb")
            pickle.dump(score, f)
            f.close()

    elif t.mode == "Incep":
        mkdir(g.default_repo_dir + "samples/Mix")
        mkdir(g.default_repo_dir + "rlt")
        for sp in range(0, 5):
            def getIncepName():
                ans = 'incep_' + t.data + '_' + t.featureType + '_'
                for entry in t.dataList:
                    ans = ans + entry.data + '_' + entry.folder + '_' + \
                        ('%.2f' % entry.fraction) + '_' + \
                        str(entry.dup) + '_' + str(entry.imageMode) + '_'
                return ans + str(t.mixSize) + "_" + str(sp)
            incep_file_name = g.default_repo_dir + "rlt/" + getIncepName()
            if os.path.exists(incep_file_name):
                print(incep_file_name + " already generated. Continue.")
                return
            X = getDat(t, t.dataList, g.default_repo_dir + "samples/Mix/M1" +
                       hostname, t.featureType)  # get smax feature
            incep_score = inception_score(X)
            f = open(incep_file_name, "wb")
            pickle.dump(incep_score, f)
            f.close()

    elif t.mode == "ModeScore":
        mkdir(g.default_repo_dir + "samples/Mix")
        mkdir(g.default_repo_dir + "rlt")
        for sp in range(0, 5):
            def getMSName():
                ans = 'mode_score_' + t.data + '_' + t.featureType + '_'
                for entry in t.dataList:
                    ans = ans + entry.data + '_' + entry.folder + '_' + \
                        ('%.2f' % entry.fraction) + '_' + \
                        str(entry.dup) + '_' + str(entry.imageMode) + '_'
                return ans + str(t.mixSize) + "_" + str(sp)
            ms_file_name = g.default_repo_dir + "rlt/" + getMSName()

            if os.path.exists(ms_file_name):
                print(ms_file_name + " already generated. Continue.")
                return
            X = getDat(t, t.dataList, g.default_repo_dir +
                       "samples/Mix/M1" + hostname, t.featureType)
            dataList = [Ent(1, t.data, 'true', None)]
            Y = getDat(t, dataList, g.default_repo_dir +
                       "samples/Mix/M2" + hostname, t.featureType)

            modeScore = mode_score(X, Y)
            f = open(ms_file_name, "wb")
            pickle.dump(modeScore, f)
            f.close()

    elif t.mode == "FID":
        mkdir(g.default_repo_dir + "samples/Mix")
        mkdir(g.default_repo_dir + "rlt")
        for sp in range(0, 5):
            def getFidName():
                ans = 'fid_' + t.data + '_' + t.featureType + '_'
                for entry in t.dataList:
                    ans = ans + entry.data + '_' + entry.folder + '_' + \
                        ('%.2f' % entry.fraction) + '_' + \
                        str(entry.dup) + '_' + str(entry.imageMode) + '_'
                return ans + str(t.mixSize) + "_" + str(sp)
            fid_file_name = g.default_repo_dir + "rlt/" + getFidName()

            if os.path.exists(fid_file_name):
                print(fid_file_name + " already generated. Continue.")
                return
            X = getDat(t, t.dataList, g.default_repo_dir +
                       "samples/Mix/M1" + hostname, t.featureType)
            dataList = [Ent(1, t.data, 'true', None)]
            Y = getDat(t, dataList, g.default_repo_dir +
                       "samples/Mix/M2" + hostname, t.featureType)

            FID = fid(X, Y)
            f = open(fid_file_name, "wb")
            pickle.dump(FID, f)
            f.close()

    elif t.mode == 'collapse':
        run_collapse_exp(t.data)

    elif t.mode == 'drop':
        run_drop_exp(t.data)

    elif t.mode == 'overfit':
        run_overfit_exp(t.data)

    elif t.mode == 'Gen':  # generate necessary data..
        if t.data == 'noise':
            print("\nSampling noise...")
            noise_sampler(t)
        elif t.data == 'mnistsub':  # subclass
            print("\nSampling subclass...")
            t.data = 'mnist'
            subclass_sampler(t)
        else:
            t.folderName = t.model + \
                (str(t.epoch) if not t.model.startswith('true') else "")
            if t.model.startswith('true'):
                print("\nSampling " + t.folderName + " ...")
                folder_sampler(t)
            else:
                print("\nSampling " + t.folderName + " ...")
                generator_sampler(t)

    elif t.mode == "Gan":  # generate some fake images..
        if t.model == "DCGAN":
            DCGAN_main(t)
        elif t.model == "WGAN":
            WGAN_main(t)
        elif t.model == "NNGAN":
            NNGAN_main(t)
        elif t.model == "MGGAN":
            MGGAN_main(t)
        elif t.model == "DCGAN_cluster":
            DCGAN_cluster_main(t)
        else:
            print("error.. Unknown model.")

    else:
        print("not designed yet.")
コード例 #8
0
def run_combine(t):
    print_prop(t)
    if t.mode == "Mix" or t.mode == "Entropy":
        mkdir(g.default_repo_dir + "samples/Mix")
        print("running mix mod..")
        mkdir(g.default_repo_dir + "rlt")
        for sp in range(0, 5 if t.mixOnly else 1):

            def getName():
                ans = 'mix_' if t.mode == "Mix" else 'entropy_'
                ans = ans + t.data + '_' + t.featureType + '_'
                for entry in t.dataList:
                    ans = ans + entry.data + '_' + entry.folder + '_' + \
                        ('%.2f' % entry.fraction) + '_' + \
                        str(entry.dup) + '_' + str(entry.imageMode) + '_'
                return ans + str(t.mixSize) + '_' + str(sp)

            saveFile = g.default_repo_dir + "rlt/" + getName()

            if os.path.exists(saveFile):
                print(saveFile + " already generated. Continue.")
                continue

            if t.mode == "Mix":
                mixDat = getDat(
                    t, t.dataList,
                    g.default_repo_dir + "samples/Mix/M1" + hostname,
                    t.featureType, t.mixOnly)
                dataList = [Ent(1, t.data, 'true', None)]
                print('ftype', t.featureType)
                trueDat = getDat(
                    t, dataList,
                    g.default_repo_dir + "samples/Mix/M2" + hostname,
                    t.featureType)
                score = compute_score(trueDat, mixDat, 1, 1, True)
            else:
                t.mixSize = t.mixSize * 10
                mixDat = getDat(
                    t, t.dataList,
                    g.default_repo_dir + "samples/Mix/M1" + hostname,
                    t.featureType, t.mixOnly)
                t.mixSize = t.mixSize / 10
                dataList = [Ent(1, t.data, 'true_test', None)]
                trueDat = getDat(
                    t, dataList,
                    g.default_repo_dir + "samples/Mix/M2" + hostname,
                    t.featureType)
                if t.featureType == 'pix':
                    epsilons = [1000]
                elif t.featureType == 'smax':
                    epsilons = [0.001]
                elif t.featureType == 'class':
                    epsilons = [0.1]
                elif t.featureType == 'conv':
                    epsilons = [1000]
                score = entropy_score(mixDat, trueDat, epsilons)
            f = open(saveFile, "wb")
            pickle.dump(score, f)
            f.close()

    elif t.mode == "Incep":
        mkdir(g.default_repo_dir + "samples/Mix")
        mkdir(g.default_repo_dir + "rlt")
        for sp in range(0, 5):

            def getIncepName():
                ans = 'incep_' + t.data + '_' + t.featureType + '_'
                for entry in t.dataList:
                    ans = ans + entry.data + '_' + entry.folder + '_' + \
                        ('%.2f' % entry.fraction) + '_' + \
                        str(entry.dup) + '_' + str(entry.imageMode) + '_'
                return ans + str(t.mixSize) + "_" + str(sp)

            incep_file_name = g.default_repo_dir + "rlt/" + getIncepName()
            if os.path.exists(incep_file_name):
                print(incep_file_name + " already generated. Continue.")
                return
            X = getDat(t, t.dataList,
                       g.default_repo_dir + "samples/Mix/M1" + hostname,
                       t.featureType)  # get smax feature
            incep_score = inception_score(X)
            f = open(incep_file_name, "wb")
            pickle.dump(incep_score, f)
            f.close()

    elif t.mode == "ModeScore":
        mkdir(g.default_repo_dir + "samples/Mix")
        mkdir(g.default_repo_dir + "rlt")
        for sp in range(0, 5):

            def getMSName():
                ans = 'mode_score_' + t.data + '_' + t.featureType + '_'
                for entry in t.dataList:
                    ans = ans + entry.data + '_' + entry.folder + '_' + \
                        ('%.2f' % entry.fraction) + '_' + \
                        str(entry.dup) + '_' + str(entry.imageMode) + '_'
                return ans + str(t.mixSize) + "_" + str(sp)

            ms_file_name = g.default_repo_dir + "rlt/" + getMSName()

            if os.path.exists(ms_file_name):
                print(ms_file_name + " already generated. Continue.")
                return
            X = getDat(t, t.dataList,
                       g.default_repo_dir + "samples/Mix/M1" + hostname,
                       t.featureType)
            dataList = [Ent(1, t.data, 'true', None)]
            Y = getDat(t, dataList,
                       g.default_repo_dir + "samples/Mix/M2" + hostname,
                       t.featureType)

            modeScore = mode_score(X, Y)
            f = open(ms_file_name, "wb")
            pickle.dump(modeScore, f)
            f.close()

    elif t.mode == "FID":
        mkdir(g.default_repo_dir + "samples/Mix")
        mkdir(g.default_repo_dir + "rlt")
        for sp in range(0, 5):

            def getFidName():
                ans = 'fid_' + t.data + '_' + t.featureType + '_'
                for entry in t.dataList:
                    ans = ans + entry.data + '_' + entry.folder + '_' + \
                        ('%.2f' % entry.fraction) + '_' + \
                        str(entry.dup) + '_' + str(entry.imageMode) + '_'
                return ans + str(t.mixSize) + "_" + str(sp)

            fid_file_name = g.default_repo_dir + "rlt/" + getFidName()

            if os.path.exists(fid_file_name):
                print(fid_file_name + " already generated. Continue.")
                return
            X = getDat(t, t.dataList,
                       g.default_repo_dir + "samples/Mix/M1" + hostname,
                       t.featureType)
            dataList = [Ent(1, t.data, 'true', None)]
            Y = getDat(t, dataList,
                       g.default_repo_dir + "samples/Mix/M2" + hostname,
                       t.featureType)

            FID = fid(X, Y)
            f = open(fid_file_name, "wb")
            pickle.dump(FID, f)
            f.close()

    elif t.mode == 'collapse':
        run_collapse_exp(t.data)

    elif t.mode == 'drop':
        run_drop_exp(t.data)

    elif t.mode == 'overfit':
        run_overfit_exp(t.data)

    elif t.mode == 'Gen':  # generate necessary data..
        if t.data == 'noise':
            print("\nSampling noise...")
            noise_sampler(t)
        elif t.data == 'mnistsub':  # subclass
            print("\nSampling subclass...")
            t.data = 'mnist'
            subclass_sampler(t)
        else:
            t.folderName = t.model + \
                (str(t.epoch) if not t.model.startswith('true') else "")
            if t.model.startswith('true'):
                print("\nSampling " + t.folderName + " ...")
                folder_sampler(t)
            else:
                print("\nSampling " + t.folderName + " ...")
                generator_sampler(t)

    elif t.mode == "Gan":  # generate some fake images..
        if t.model == "DCGAN":
            DCGAN_main(t)
        elif t.model == "WGAN":
            WGAN_main(t)
        elif t.model == "NNGAN":
            NNGAN_main(t)
        elif t.model == "MGGAN":
            MGGAN_main(t)
        elif t.model == "DCGAN_cluster":
            DCGAN_cluster_main(t)
        else:
            print("error.. Unknown model.")

    else:
        print("not designed yet.")