-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.py
137 lines (113 loc) · 5.52 KB
/
test.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
### execute this function to train and test the vae-model
from test_model import Model
import numpy as np
import pickle
import torch
import os
import argparse
import time
import warnings
warnings.filterwarnings("ignore")
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
parser = argparse.ArgumentParser()
parser.add_argument('--dataset')
parser.add_argument('--latent_size',type=int)
parser.add_argument('--generalized', type = str2bool)
args = parser.parse_args()
########################################
# the basic hyperparameters
########################################
hyperparameters = {
'num_shots': 0,
'device': 'cuda',
'model_specifics': {'cross_reconstruction': True,
'name': 'CADA',
'distance': 'wasserstein',
'warmup': {'beta': {'factor': 0.25,
'end_epoch': 90,
'start_epoch': 0},
'cross_reconstruction': {'factor': 2.37,
'end_epoch': 75,
'start_epoch': 21},
'distance': {'factor': 8.0,
'end_epoch': 25,
'start_epoch': 0}}},
'lr_gen_model': 0.00015,
'generalized': True,
'batch_size': 50,
'samples_per_class': {'SUN': (200, 0, 400, 0),
'APY': (200, 0, 400, 0),
'CUB': (200, 0, 400, 0),
'AWA2': (200, 0, 400, 0),
'FLO': (200, 0, 400, 0),
'AWA1': (200, 0, 400, 0)},
'epochs': 200,
'loss': 'l1',
'auxiliary_data_source' : 'attributes',
'lr_cls': 0.001,
'dataset': 'CUB',
'hidden_size_rule': {'resnet_features': (4096, 4096),
'attributes': (4096, 4096),
'sentences': (4096, 4096) },
'coarse_latent_size': 2048,
'latent_size': 64,
'recon_x_cyc_w': 0.5,
'adapt_mode': 'SWD', #MCD or SWD
'classifier': 'softmax', #knn or softmax
'result_root': '/home/shimingchen/ZSL/Feature-Matching/model/result'
}
# The training epochs for the final classifier, for early stopping,
# as determined on the validation spit
cls_train_steps = [
{'dataset': 'SUN', 'latent_size': 128, 'generalized': True, 'cls_train_steps': 40},
{'dataset': 'SUN', 'latent_size': 128, 'generalized': False, 'cls_train_steps': 30},
{'dataset': 'AWA1', 'latent_size': 64, 'generalized': True, 'cls_train_steps': 33},
{'dataset': 'AWA1', 'latent_size': 64, 'generalized': False, 'cls_train_steps': 25},
{'dataset': 'CUB', 'latent_size': 64, 'generalized': True, 'cls_train_steps': 23},
{'dataset': 'CUB', 'latent_size': 64, 'generalized': False, 'cls_train_steps': 22},
{'dataset': 'AWA2', 'latent_size': 64, 'generalized': True, 'cls_train_steps': 50},
{'dataset': 'AWA2', 'latent_size': 64, 'generalized': False, 'cls_train_steps': 39},
]
##################################
# change some hyperparameters here
##################################
hyperparameters['dataset'] = args.dataset
hyperparameters['latent_size']= args.latent_size
hyperparameters['generalized']= args.generalized
hyperparameters['cls_train_steps'] = [x['cls_train_steps'] for x in cls_train_steps
if all([hyperparameters['dataset']==x['dataset'],
hyperparameters['latent_size']==x['latent_size'],
hyperparameters['generalized']==x['generalized'] ])][0]
print('***')
print(hyperparameters['cls_train_steps'] )
if hyperparameters['generalized']:
if hyperparameters['num_shots']==0:
hyperparameters['samples_per_class'] = {'CUB': (200, 0, 400, 0), 'SUN': (200, 0, 400, 0),
'APY': (200, 0, 400, 0), 'AWA1': (200, 0, 400, 0),
'AWA2': (200, 0, 400, 0), 'FLO': (200, 0, 400, 0)}
else:
hyperparameters['samples_per_class'] = {'CUB': (200, 0, 200, 200), 'SUN': (200, 0, 200, 200),
'APY': (200, 0, 200, 200), 'AWA1': (200, 0, 200, 200),
'AWA2': (200, 0, 200, 200), 'FLO': (200, 0, 200, 200)}
else:
if hyperparameters['num_shots']==0:
hyperparameters['samples_per_class'] = {'CUB': (0, 0, 400, 0), 'SUN': (0, 0, 200, 0),
'APY': (0, 0, 200, 0), 'AWA1': (0, 0, 800, 0),
'AWA2': (0, 0, 200, 0), 'FLO': (0, 0, 200, 0)}
else:
hyperparameters['samples_per_class'] = {'CUB': (0, 0, 200, 200), 'SUN': (0, 0, 200, 200),
'APY': (0, 0, 200, 200), 'AWA1': (0, 0, 200, 200),
'AWA2': (0, 0, 200, 200), 'FLO': (0, 0, 200, 200)}
model = Model( hyperparameters)
model.to(hyperparameters['device'])
model.test()
print(time.strftime('ending time:%Y-%m-%d %H:%M:%S',time.localtime(time.time())))
print("dataset", args.dataset)
print(hyperparameters['classifier'])
print("**********END*******************")