Skip to content

Commit

Permalink
numpy manual seed
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeemzadeh committed Apr 8, 2020
1 parent 44d223b commit d9e3998
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
13 changes: 6 additions & 7 deletions acquisition.py
Expand Up @@ -23,18 +23,18 @@ def acquisition(pool_loader, train_loader, model, opts):

# setting the number of the samples to be pooled
if len(train_loader.dataset.indices) == 0: # initial acquisition
print 'initial selection: n_pool = ', opts.init_train_size
print('initial selection: n_pool = ', opts.init_train_size)
n_pool = opts.init_train_size
else:
print 'n_pool = ', opts.n_pool
print('n_pool = ', opts.n_pool)
n_pool = opts.n_pool

print 'extracting features of the training dataset. '
print('extracting features of the training dataset. ')
train_features, train_labels = extract_features(train_loader_noshuffle, model)
print 'extracting features of the pooling dataset.'
print('extracting features of the pooling dataset.')
pool_features, pool_labels = extract_features(pool_loader_noshuffle, model)

print 'selecting samples.'
print('selecting samples.')
pooled_idx = ipm(train_features, pool_features, n_pool)

pooled_idx_set = set([pool_loader_noshuffle.dataset.indices[i] for i in pooled_idx])
Expand All @@ -47,7 +47,7 @@ def acquisition(pool_loader, train_loader, model, opts):

def extract_features(data_loader, model, label_only=False):
if not label_only:
feature_extractor = nn.Sequential(*list(model.module.children())[:-2])
feature_extractor = nn.Sequential(*list(model.module.children())[:-1])
feature_extractor = feature_extractor.cuda()
feature_extractor = nn.DataParallel(feature_extractor, device_ids=None)
feature_extractor.eval()
Expand All @@ -62,7 +62,6 @@ def extract_features(data_loader, model, label_only=False):
inputs = Variable(inputs)
batch_features = feature_extractor(inputs).data.view(inputs.size(0), -1)

# TODO: convert to numpy more efficiently
features.extend(batch_features.cpu().numpy())

if i % 100 == 0:
Expand Down
19 changes: 10 additions & 9 deletions main.py
Expand Up @@ -50,6 +50,7 @@
json.dump(vars(opt), opt_file)

torch.manual_seed(opt.manual_seed)
np.random.seed(opt.manual_seed)

model, parameters = generate_model(opt)

Expand Down Expand Up @@ -93,12 +94,12 @@
pool_idx_set = set(range(len(labeled_data)))
elif opt.init_selection == 'random':
# split data randomly
print 'initial data selection: random'
print('initial data selection: random')
training_idx_set = set(np.random.permutation(range(len(labeled_data)))[:opt.init_train_size])
pool_idx_set = set(range(len(labeled_data))) - training_idx_set
elif opt.init_selection == 'uniform_random':
# select balanced dataset randomly
print 'initial data selection: uniform_random'
print('initial data selection: uniform_random')
labeled_data_loader = torch.utils.data.DataLoader(
labeled_data,
batch_size=256,
Expand Down Expand Up @@ -187,18 +188,18 @@
# initial selection if necessary
clusters = []
if opt.init_selection == 'same':
print 'initial data selection: same'
print('initial data selection: same')
acquisition(pool_loader, train_loader, model, opt)

cycle_val_acc = []
cycle_test_acc = []

while len(training_data) <= opt.max_train_size:
print('=========================================')
print 'train dataset size: ', len(training_data)
print 'pool dataset size: ', len(pool_data)
print 'max train dataset size: ', opt.max_train_size
print '# pooled data per cycle: ', opt.n_pool
print('train dataset size: ', len(training_data))
print('pool dataset size: ', len(pool_data))
print('max train dataset size: ', opt.max_train_size)
print('# pooled data per cycle: ', opt.n_pool)

print('Training')
max_val_acc = 0
Expand All @@ -216,7 +217,7 @@
scheduler.step(validation_loss)

cycle_val_acc.append(max_val_acc)
print cycle_val_acc
print(cycle_val_acc)

if opt.test:
spatial_transform = Compose([
Expand All @@ -242,7 +243,7 @@
subset='validation', verbose=True, top_k=1)
ucf101.evaluate()
cycle_test_acc.append(ucf101.hit_at_k)
print cycle_test_acc
print(cycle_test_acc)

# pool new labeled data
if len(training_data) + opt.n_pool > opt.max_train_size:
Expand Down
4 changes: 1 addition & 3 deletions opts.py
Expand Up @@ -221,7 +221,7 @@ def parse_opts():
type=int,
help='ResNeXt cardinality')
parser.add_argument(
'--manual_seed', default=0, type=int, help='Manually set random seed')
'--manual_seed', default=1, type=int, help='Manually set random seed')

# acqusition args
parser.add_argument('--init_train_size', type=int, default=101)
Expand All @@ -231,7 +231,5 @@ def parse_opts():
parser.add_argument('--n_pool', type=int, default=101)
parser.add_argument('--max_train_size', type=int, default=404)



args = parser.parse_args()
return args

0 comments on commit d9e3998

Please sign in to comment.