-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_classifier_with_augmented_images.py
161 lines (138 loc) · 9.9 KB
/
train_classifier_with_augmented_images.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
156
157
158
159
160
161
import argparse
import data_for_augmentedimages_for_classifier as dataset
from classification_builder import ExperimentBuilder
import numpy as np
parser = argparse.ArgumentParser(description='Welcome to GAN-Shot-Learning script')
parser.add_argument('--batch_size', nargs="?", type=int, default=32, help='batch_size for experiment')
parser.add_argument('--discriminator_inner_layers', nargs="?", type=int, default=3,
help='discr_number_of_conv_per_layer')
parser.add_argument('--generator_inner_layers', nargs="?", type=int, default=3, help='discr_number_of_conv_per_layer')
parser.add_argument('--experiment_title', nargs="?", type=str, default="densenet_generator_fc", help='Experiment name')
parser.add_argument('--continue_from_epoch', nargs="?", type=int, default=1, help='continue from checkpoint of epoch')
parser.add_argument('--num_of_gpus', nargs="?", type=int, default=1, help='discr_number_of_conv_per_layer')
parser.add_argument('--z_dim', nargs="?", type=int, default=100, help='The dimensionality of the z input')
parser.add_argument('--dropout_rate_value', type=float, default=0.5, help='dropout_rate_value')
parser.add_argument('--use_wide_connections', nargs="?", type=str, default="False",
help='Whether to use wide connections in discriminator')
parser.add_argument('--image_width', nargs="?", type=int, default=128)
parser.add_argument('--image_height', nargs="?", type=int, default=128)
parser.add_argument('--image_channel', nargs="?", type=int, default=3)
parser.add_argument('--matching', nargs="?", type=int, default=1)
parser.add_argument('--fce', nargs="?", type=int, default=0)
parser.add_argument('--full_context_unroll_k', nargs="?", type=int, default=4)
parser.add_argument('--average_per_class_embeddings', nargs="?", type=int, default=0)
parser.add_argument('--dataset', type=str, default='omniglot')
parser.add_argument('--loss_G', type=float, default=1)
parser.add_argument('--loss_D', type=float, default=1)
parser.add_argument('--loss_KL', type=float, default=0.0001)
parser.add_argument('--loss_CLA', type=float, default=1)
parser.add_argument('--loss_FSL', type=float, default=1)
parser.add_argument('--loss_recons_B', type=float, default=0.01)
parser.add_argument('--loss_matching_G', type=float, default=0.01)
parser.add_argument('--loss_matching_D', type=float, default=0.01)
parser.add_argument('--loss_sim', type=float, default=1e2)
parser.add_argument('--strategy', nargs="?", type=int, default=2)
###### generating data
parser.add_argument('--support_number', nargs="?", type=int, default=1, help='num_support')
parser.add_argument('--selected_classes', type=int, default=1)
##### general classifier parameters
parser.add_argument('--classification_total_epoch', type=int, default=20)
parser.add_argument('--general_classification_samples', type=int, default=5) ####=support number
##### fewshot classifier parameters
parser.add_argument('--episodes_number', type=int, default=10)
parser.add_argument('--few_shot_episode_classes', type=int, default=1) ###=selected_images
##### matchingGAN related parameters
parser.add_argument('--is_z2', nargs="?", type=int, default=0)
parser.add_argument('--is_z2_vae', nargs="?", type=int, default=0)
parser.add_argument('--restore_path', nargs="?", type=str, default="omniglot_dagan_experiment",
help='Experiment name')
parser.add_argument('--augmented_number', type=int, default=0)
parser.add_argument('--num_generations', nargs="?", type=int, default=0, help='num_generations')
parser.add_argument('--confidence', type=int, default=1)
parser.add_argument('--loss_d', type=int, default=1)
###### classifier related parameters
parser.add_argument('--pretrained_epoch', type=int, default=0)
parser.add_argument('--restore_classifier_path', nargs="?", type=str, default="omniglot_dagan_experiment",
help='Experiment name')
parser.add_argument('--pretrain', type=int, default=0)
parser.add_argument('--is_training', nargs="?", type=int, default=0)
parser.add_argument('--is_fewshot_setting', nargs="?", type=int, default=0)
args = parser.parse_args()
args_dict = vars(args)
for key in list(args_dict.keys()):
print(key, args_dict[key])
batch_size = args.batch_size
num_gpus = args.num_of_gpus
##### generating the batches data
support_num = args.support_number
selected_classes_num = args.few_shot_episode_classes
if args.dataset == 'omniglot':
print('omniglot')
data = dataset.OmniglotDAGANDataset(batch_size=batch_size, last_training_class_index=900, reverse_channels=True,
num_of_gpus=num_gpus, gen_batches=1000, support_number=support_num,
is_training=args.is_training,
general_classification_samples=args.few_shot_episode_classes,
selected_classes=args.selected_classes, image_size=args.image_width)
elif args.dataset == 'vggface':
print('vggface')
data = dataset.VGGFaceDAGANDataset(batch_size=batch_size, last_training_class_index=1600, reverse_channels=True,
num_of_gpus=num_gpus, gen_batches=1000, support_number=support_num,
is_training=args.is_training,
general_classification_samples=args.few_shot_episode_classes,
selected_classes=args.selected_classes, image_size=args.image_width)
elif args.dataset == 'miniimagenet':
print('miniimagenet')
data = dataset.miniImagenetDAGANDataset(batch_size=batch_size, last_training_class_index=900, reverse_channels=True,
num_of_gpus=num_gpus, gen_batches=1000, support_number=support_num,
is_training=args.is_training,
general_classification_samples=args.few_shot_episode_classes,
selected_classes=args.selected_classes, image_size=args.image_width)
elif args.dataset == 'emnist':
print('emnist')
data = dataset.emnistDAGANDataset(batch_size=batch_size, last_training_class_index=900, reverse_channels=True,
num_of_gpus=num_gpus, gen_batches=1000, support_number=support_num,
is_training=args.is_training,
general_classification_samples=args.few_shot_episode_classes,
selected_classes=args.selected_classes, image_size=args.image_width)
elif args.dataset == 'figr':
print('figr')
data = dataset.FIGRDAGANDataset(batch_size=batch_size, last_training_class_index=900, reverse_channels=True,
num_of_gpus=num_gpus, gen_batches=1000, support_number=support_num,
is_training=args.is_training,
general_classification_samples=args.general_classification_samples,
selected_classes=args.selected_classes, image_size=args.image_width)
elif args.dataset == 'fc100':
data = dataset.FC100DAGANDataset(batch_size=batch_size, last_training_class_index=900, reverse_channels=True,
num_of_gpus=num_gpus, gen_batches=1000, support_number=support_num,
is_training=args.is_training,
general_classification_samples=args.general_classification_samples,
selected_classes=args.selected_classes, image_size=args.image_width)
elif args.dataset == 'animals':
data = dataset.animalsDAGANDataset(batch_size=batch_size, last_training_class_index=900, reverse_channels=True,
num_of_gpus=num_gpus, gen_batches=1000, support_number=support_num,
is_training=args.is_training,
general_classification_samples=args.few_shot_episode_classes,
selected_classes=args.selected_classes, image_size=args.image_width)
elif args.dataset == 'flowers':
data = dataset.flowersDAGANDataset(batch_size=batch_size, last_training_class_index=900, reverse_channels=True,
num_of_gpus=num_gpus, gen_batches=1000, support_number=support_num,
is_training=args.is_training,
general_classification_samples=args.few_shot_episode_classes,
selected_classes=args.selected_classes, image_size=args.image_width)
elif args.dataset == 'flowersselected':
data = dataset.flowersselectedDAGANDataset(batch_size=batch_size, last_training_class_index=900,
reverse_channels=True,
num_of_gpus=num_gpus, gen_batches=1000, support_number=support_num,
is_training=args.is_training,
general_classification_samples=args.general_classification_samples,
selected_classes=args.selected_classes, image_size=args.image_width)
elif args.dataset == 'birds':
data = dataset.birdsDAGANDataset(batch_size=batch_size, last_training_class_index=900, reverse_channels=True,
num_of_gpus=num_gpus, gen_batches=1000, support_number=support_num,
is_training=args.is_training,
general_classification_samples=args.general_classification_samples,
selected_classes=args.selected_classes, image_size=args.image_width)
print('curret test dataset is:', args.dataset)
print('current testing data size is:', np.shape(data.x_test))
experiment = ExperimentBuilder(parser, data=data)
experiment.run_experiment()