This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_bg_f100m.py
155 lines (110 loc) · 3.77 KB
/
test_bg_f100m.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#! /usr/bin/env python2
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import os
import numpy as np
import sys
import time
import pdb
import cPickle
import argparse
import faiss
import diffusion
import diffusion_dataset
mode, nlabeled, seed, k, ndis = sys.argv[1:6]
niter = 60
lslice = 0
storeLname = None
storeLiters = None
if len(sys.argv) > 6:
niter = int(sys.argv[6])
if len(sys.argv) > 7:
lslice = int(sys.argv[7])
if len(sys.argv) > 8:
storeLname = sys.argv[8]
if len(sys.argv) > 9:
storeLiters = sys.argv[9]
nlabeled = int(nlabeled)
seed = int(seed)
k = int(k)
ndis = int(ndis)
featureversion = 2
if mode.endswith('_fv3'):
featureversion = 3
mode = mode[:-4]
if mode == 'val':
print("========================== run on Validation")
else:
print("========================== run on Test")
class Opt:
pass
opt = Opt()
# graph params
opt.k = k # we compute the graph for that
if True:
Xtr, Ytr, Xte, Yte = diffusion_dataset.load_bharath_traintest(
nlabeled, class_set=1 if mode=='val' else 2, seed=seed,
nnonlabeled=0, pca256=True, include_base_class=True,
featureversion=featureversion)
nclasses = Ytr.max() + 1
print("dataset sizes: Xtr %s (%d labeled), Xte %s, %d classes (eval on %s)" % (
Xtr.shape, (Ytr >= 0).sum(),
Xte.shape, nclasses, Yte))
if ndis > 0:
# add distractors
Xdis = diffusion_dataset.load_bharath_distractors(
ndis, pca256=True, featureversion=featureversion)
print 'distractors shape', Xdis.shape
opt.dis_key = 'bharath256rr_ndis%d' % ndis
opt.indextype = 'GPUIVFFlatShards'
opt.indexnprobe = 256
opt.cachedir = '/mnt/vol/gfsai-east/ai-group/users/matthijs/finetune_750_250/diffusion_results/LRD_cache'
if featureversion == 3:
opt.cachedir += '_fv3'
nclasses = Ytr.max() + 1
I, D, (I2, D2) = diffusion.make_graph_with_precomputed_distractors(
Xtr, Xdis, opt, Xte)
if lslice > 0:
# let's face it: we are not really using D and it consumes RAM
D = None
Ytr = np.hstack((Ytr, -np.ones(Xdis.shape[0], dtype=int)))
else:
opt.externalTest = True
opt.pca = 0
opt.indextype = 'IVFFlat'
opt.indexnprobe = 256
opt.cacheDistractorGraph = False
I, D, (I2, D2) = diffusion.make_graph_from_points(Xtr, opt, Xtest=Xte)
print "storing cache"
# cPickle.dump((Ytr, I, D, I2, D2, Yte), open('/data/local/users/matthijs/tmp/cache.pickle', 'w'), -1)
if False:
outdir = '/mnt/vol/gfsai-east/ai-group/users/matthijs/finetune_750_250/diffusion_results/ITERSTATS//ndis100M_nl2_k30_seed1/'
pdb.set_trace()
np.save(outdir + 'I.npy', I)
np.save(outdir + 'I2.npy', I2)
cPickle.dump((Ytr, I, D, I2, D2, Yte), open(outdir + 'cache.pickle', 'w'), -1)
else:
print "loading cache"
(Ytr, I, D, I2, D2, Yte) = cPickle.load(open('/data/local/users/matthijs/tmp/cache.pickle', 'r'))
print "Performing diffusion"
# diffusion options
opt.weights = '1'
opt.weightsigma = 0
opt.reciprocal = False
opt.diffiters = niter
opt.tau = 1.0
opt.sinkhorn = 4
opt.reset1hot = False
if lslice > 0:
val_L_hist = diffusion.do_diffusion_lslice(opt, Ytr, I, D, (I2, D2, Yte), lslice,
storeLiters=storeLiters)
else:
val_L_hist = diffusion.do_diffusion(opt, Ytr, I, D, (I2, D2, Yte),
storeLiters=storeLiters)
if storeLname is not None:
print 'storing L history in', storeLname
cPickle.dump(val_L_hist, open(storeLname, 'w'), -1)