meta_trainer.py 17.1 KB
Newer Older
Yaoyao Liu's avatar
Yaoyao Liu committed
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#   Copyright (c) 2020 Yaoyao Liu. All Rights Reserved.
#
#   Licensed under the Apache License, Version 2.0 (the "License").
#   You may not use this file except in compliance with the License.
#   A copy of the License is located at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   or in the "license" file accompanying this file. This file is distributed
#   on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
#   express or implied. See the License for the specific language governing
#   permissions and limitations under the License.
# ==============================================================================

import argparse
import os
import os.path as osp
import numpy as np
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader
import torch.nn as nn
from dataloader.samplers import CategoriesSampler
from utils.misc import *
from utils.gpu_tools import occupy_memory
from tensorboardX import SummaryWriter
import tqdm
import time
import importlib

class MetaTrainer(object):
    def __init__(self, args):
        self.args = args

        if args.dataset == 'miniimagenet':
            from dataloader.mini_imagenet import MiniImageNet as Dataset
            args.num_class = 64
            print('Using dataset: miniImageNet, base class num:', args.num_class)
        elif args.dataset == 'cub':
            from dataloader.cub import CUB as Dataset
            args.num_class = 100
            print('Using dataset: CUB, base class num:', args.num_class)
        elif args.dataset == 'tieredimagenet':
            from dataloader.tiered_imagenet import tieredImageNet as Dataset
            args.num_class = 351
            print('Using dataset: tieredImageNet, base class num:', args.num_class)
        elif args.dataset == 'fc100':
            from dataloader.fc100 import DatasetLoader as Dataset
            args.num_class = 60
            print('Using dataset: FC100, base class num:', args.num_class)
        elif args.dataset == 'cifar_fs':
            from dataloader.cifar_fs import DatasetLoader as Dataset
            args.num_class = 64
            print('Using dataset: CIFAR-FS, base class num:', args.num_class)
        else:
            raise ValueError('Please set the correct dataset.')

        self.Dataset = Dataset

        if args.mode == 'pre_train':
            print('Building pre-train model.')
            self.model = importlib.import_module('model.meta_model').MetaModel(args, dropout=args.dropout, mode='pre')
        else:
            print('Building meta model.')
            self.model = importlib.import_module('model.meta_model').MetaModel(args, dropout=args.dropout, mode='meta')

        if args.mode == 'pre_train':
            print ('Initialize the model for pre-train phase.')
        else:
            args.dir='pretrain_model/%s/%s/max_acc.pth'%(args.dataset,args.backbone)
            if not os.path.exists(args.dir):
                os.system('sh scripts/download_pretrain_model.sh')
            print ('Loading pre-trainrd model from:\n',args.dir)
            model_dict = self.model.state_dict()
            pretrained_dict = torch.load(args.dir)['params']
            pretrained_dict = {'encoder.' + k: v for k, v in pretrained_dict.items()}
            for k,v in pretrained_dict.items():
                model_dict[k]=pretrained_dict[k]
            self.model.load_state_dict(model_dict)

        if self.args.num_gpu>1:
            self.model = nn.DataParallel(self.model,list(range(args.num_gpu)))     
        self.model=self.model.cuda()
        print('Building model finished.')

        if args.mode == 'pre_train':
            args.save_path = 'pre_train/%s-%s' % \
                         (args.dataset, args.backbone)  
        else:          
            args.save_path = 'meta_train/%s-%s-%s-%dway-%dshot' % \
                         (args.dataset, args.backbone, args.meta_update, args.way, args.shot)

        args.save_path=osp.join('logs', args.save_path)

        ensure_path(args.save_path)

        trainset = Dataset('train', args)
        if args.mode == 'pre_train':
            self.train_loader = DataLoader(dataset=trainset,batch_size=args.bs,shuffle=True, num_workers=args.num_workers, pin_memory=True)
        else:
            train_sampler = CategoriesSampler(trainset.label, args.val_frequency*args.bs, args.way, args.shot + args.query)
            self.train_loader = DataLoader(dataset=trainset, batch_sampler=train_sampler, num_workers=args.num_workers, pin_memory=True)

        valset = Dataset(args.set, args)
        val_sampler = CategoriesSampler(valset.label, args.val_episode, args.way, args.shot + args.query)
        self.val_loader = DataLoader(dataset=valset, batch_sampler=val_sampler, num_workers=args.num_workers, pin_memory=True)

        val_loader=[x for x in self.val_loader]

        if args.mode == 'pre_train':
            self.optimizer = torch.optim.SGD([{'params': self.model.encoder.parameters(), 'lr': args.lr }, \
                                        {'params': self.model.fc.parameters(), 'lr': args.lr }], \
                                        momentum=0.9, nesterov=True, weight_decay=0.0005)
            self.lr_scheduler = torch.optim.lr_scheduler.StepLR(self.optimizer, step_size=args.step_size, gamma=args.gamma)
        else:
            if args.meta_update=='mtl':
                new_para = filter(lambda p: p.requires_grad, self.model.encoder.parameters())
            else:
                new_para = self.model.encoder.parameters()

            self.optimizer = torch.optim.SGD([{'params': new_para, 'lr': args.lr}, \
                {'params': self.model.base_learner.parameters(), 'lr': self.args.lr}, \
                {'params': self.model.get_hyperprior_combination_initialization_vars(), 'lr': self.args.lr_combination}, \
                {'params': self.model.get_hyperprior_combination_mapping_vars(), 'lr': self.args.lr_combination_hyperprior}, \
                {'params': self.model.get_hyperprior_basestep_initialization_vars(), 'lr': self.args.lr_basestep}, \
                {'params': self.model.get_hyperprior_stepsize_mapping_vars(), 'lr': self.args.lr_basestep_hyperprior}], \
                lr=args.lr, momentum=0.9, nesterov=True, weight_decay=0.0005)

            self.lr_scheduler = torch.optim.lr_scheduler.StepLR(self.optimizer, step_size=args.step_size, gamma=args.gamma)

    def save_model(self, name):
        torch.save(dict(params=self.model.state_dict()), osp.join(self.args.save_path, name + '.pth'))

    def train(self):
        args = self.args
        model = self.model
        trlog = {}
        trlog['args'] = vars(args)
        trlog['train_loss'] = []
        trlog['val_loss'] = []
        trlog['train_acc'] = []
        trlog['val_acc'] = []
        trlog['max_acc'] = 0.0
        trlog['max_acc_epoch'] = 0
        timer = Timer()
        global_count = 0
        writer = SummaryWriter(osp.join(args.save_path,'tf'))

        label = torch.arange(args.way, dtype=torch.int8).repeat(args.query)
        label = label.type(torch.LongTensor)
        if torch.cuda.is_available():
            label = label.cuda()

        SLEEP(args)

        for epoch in range(1, args.max_epoch + 1):
            print (args.save_path)
            start_time=time.time()

            tl = Averager()
            ta = Averager()

            tqdm_gen = tqdm.tqdm(self.train_loader)
            model.train()
            for i, batch in enumerate(tqdm_gen, 1):

                global_count = global_count + 1
                if torch.cuda.is_available():
                    data, _ = [_.cuda() for _ in batch]
                else:
                    data = batch[0]
                p = args.shot * args.way 
                data_shot, data_query = data[:p], data[p:] 
                data_shot = data_shot.unsqueeze(0).repeat(args.num_gpu, 1, 1, 1, 1)
                logits = model((data_shot, data_query)) 

                loss = F.cross_entropy(logits, label)

                acc = count_acc(logits, label)
                writer.add_scalar('data/loss', float(loss), global_count)
                writer.add_scalar('data/acc', float(acc), global_count)

                total_loss = loss/args.bs
                writer.add_scalar('data/total_loss', float(total_loss), global_count)
                tqdm_gen.set_description('Epoch {}, Total loss={:.4f}, Acc={:.4f}.'
                    .format(epoch, total_loss.item(), acc))

                tl.add(total_loss.item())
                ta.add(acc)

                total_loss.backward()
                if i%args.bs==0:
                    self.optimizer.step()
                    self.optimizer.zero_grad()

            tl = tl.item()
            ta = ta.item()
            vl = Averager()
            va = Averager()

            model.eval()

            tqdm_gen = tqdm.tqdm(self.val_loader)
            for i, batch in enumerate(tqdm_gen, 1):
                if torch.cuda.is_available():
                    data, _ = [_.cuda() for _ in batch]
                else:
                    data = batch[0]
                p = args.shot * args.way
                data_shot, data_query = data[:p], data[p:]
                data_shot = data_shot.unsqueeze(0).repeat(args.num_gpu, 1, 1, 1, 1)
                logits = model((data_shot, data_query))
                loss = F.cross_entropy(logits, label)
                acc = count_acc(logits, label)

                vl.add(loss.item())
                va.add(acc)
                tqdm_gen.set_description('Episode {}: {:.2f}({:.2f})'.format(i, va.item() * 100, acc * 100))

            vl = vl.item()
            va = va.item()
            writer.add_scalar('data/val_loss', float(vl), epoch)
            writer.add_scalar('data/val_acc', float(va), epoch)

            print ('Validation acc:%.4f'%va)
            if va >= trlog['max_acc']:
                print ('********* New best model!!! *********')
                trlog['max_acc'] = va
                trlog['max_acc_epoch'] = epoch
                self.save_model('max_acc')

            trlog['train_loss'].append(tl)
            trlog['train_acc'].append(ta)
            trlog['val_loss'].append(vl)
            trlog['val_acc'].append(va)

            torch.save(trlog, osp.join(args.save_path, 'trlog'))
            if args.save_all:
                self.save_model('epoch-%d'%epoch)
                torch.save(self.optimizer.state_dict(), osp.join(args.save_path,'optimizer_latest.pth'))
            print('Best epoch {}, best val acc={:.4f}.'.format(trlog['max_acc_epoch'], trlog['max_acc']))
            print ('This epoch takes %d seconds.'%(time.time()-start_time),'\nStill need %.2f hour to finish.'%((time.time()-start_time)*(args.max_epoch-epoch)/3600))
            self.lr_scheduler.step()

        writer.close()

    def eval(self):
        model = self.model
        args = self.args
        result_list=[args.save_path]
        trlog = torch.load(osp.join(args.save_path, 'trlog'))
        test_set = self.Dataset('test', args)
        sampler = CategoriesSampler(test_set.label, 3000, args.way, args.shot + args.query)
        loader = DataLoader(test_set, batch_sampler=sampler, num_workers=args.num_workers, pin_memory=True)
        test_acc_record = np.zeros((3000,))

        model.load_state_dict(torch.load(osp.join(args.save_path, 'max_acc' + '.pth'))['params'])
        model.eval()

        ave_acc = Averager()
        label = torch.arange(args.way).repeat(args.query)
        if torch.cuda.is_available():
            label = label.type(torch.cuda.LongTensor)
        else:
            label = label.type(torch.LongTensor)

        tqdm_gen = tqdm.tqdm(loader)
        for i, batch in enumerate(tqdm_gen, 1):
            if torch.cuda.is_available():
                data, _ = [_.cuda() for _ in batch]
            else:
                data = batch[0]
            k = args.way * args.shot
            data_shot, data_query = data[:k], data[k:]
            data_shot = data_shot.unsqueeze(0).repeat(args.num_gpu, 1, 1, 1, 1)
            logits = model((data_shot, data_query))
            acc = count_acc(logits, label)
            ave_acc.add(acc)
            test_acc_record[i-1] = acc
            tqdm_gen.set_description('Episode {}: {:.2f}({:.2f})'.format(i, ave_acc.item() * 100, acc * 100))

        m, pm = compute_confidence_interval(test_acc_record)

        result_list.append('Best validation epoch {},\nbest validation acc {:.4f}, \nbest test acc {:.4f}'.format(trlog['max_acc_epoch'], trlog['max_acc'], ave_acc.item()))
        result_list.append('Test acc {:.4f} + {:.4f}'.format(m, pm))
        print (result_list[-2])
        print (result_list[-1])
        save_list_to_txt(os.path.join(args.save_path,'results.txt'),result_list)

    def pre_train(self):
        model = self.model
        args = self.args
        lr_scheduler = self.lr_scheduler
        optimizer = self.optimizer
        train_loader = self.train_loader
        val_loader = self.val_loader
        trlog = {}
        trlog['args'] = vars(args)
        trlog['train_loss'] = []
        trlog['val_loss'] = []
        trlog['train_acc'] = []
        trlog['val_acc'] = []
        trlog['max_acc'] = 0.0
        trlog['max_acc_epoch'] = 0
        timer = Timer()
        global_count = 0
        writer = SummaryWriter(osp.join(args.save_path,'tf'))

Yaoyao Liu's avatar
Yaoyao Liu committed
309
310
311
312
313
314
        label = torch.arange(args.way).repeat(args.query)
        if torch.cuda.is_available():
            label = label.type(torch.cuda.LongTensor)
        else:
            label = label.type(torch.LongTensor)

Yaoyao Liu's avatar
Yaoyao Liu committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
        SLEEP(args)
        for epoch in range(1, args.max_epoch + 1):
            print (args.save_path)

            start_time=time.time()
            model = model.train()
            model.mode = 'pre'
            tl = Averager()
            ta = Averager()

            tqdm_gen = tqdm.tqdm(train_loader)
            for i, batch in enumerate(tqdm_gen, 1):

                global_count = global_count + 1
                if torch.cuda.is_available():
                    data, train_label = [_.cuda() for _ in batch]
                else:
                    data = batch[0]

                logits = model(data) 
                loss = F.cross_entropy(logits, train_label) 
                acc = count_acc(logits, train_label)

                writer.add_scalar('data/loss', float(loss), global_count)
                writer.add_scalar('data/acc', float(acc), global_count)
                total_loss = loss
                writer.add_scalar('data/total_loss', float(total_loss), global_count)
                tqdm_gen.set_description('Epoch {}, total loss={:.4f} acc={:.4f}'.format(epoch, total_loss.item(), acc))
                tl.add(total_loss.item())
                ta.add(acc)
                optimizer.zero_grad()
                total_loss.backward()
                optimizer.step()

            tl = tl.item()
            ta = ta.item()

            model=model.eval()
            model.mode = 'meta'
            vl = Averager()
            va = Averager()

Yaoyao Liu's avatar
Yaoyao Liu committed
357
            if epoch < args.val_epoch:
Yaoyao Liu's avatar
Yaoyao Liu committed
358
359
360
                vl=0
                va=0
            else:
Yaoyao Liu's avatar
Yaoyao Liu committed
361
362
363
364
365
366
367
368
                tqdm_gen = tqdm.tqdm(val_loader)
                for i, batch in enumerate(tqdm_gen, 1):
                    if torch.cuda.is_available():
                        data, _ = [_.cuda() for _ in batch]
                    else:
                        data = batch[0]
                    p = args.shot * args.way
                    data_shot, data_query = data[:p], data[p:]  
Yaoyao Liu's avatar
Yaoyao Liu committed
369
370
                    data_shot = data_shot.unsqueeze(0).repeat(args.num_gpu, 1, 1, 1, 1)
                    logits = model.preval_forward(data_shot, data_query)
Yaoyao Liu's avatar
Yaoyao Liu committed
371
372
373
374
                    loss = F.cross_entropy(logits, label)
                    acc = count_acc(logits, label)
                    vl.add(loss.item())
                    va.add(acc)
Yaoyao Liu's avatar
Yaoyao Liu committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407

                vl = vl.item()
                va = va.item()
            writer.add_scalar('data/val_loss', float(vl), epoch)
            writer.add_scalar('data/val_acc', float(va), epoch)
            tqdm_gen.set_description('epo {}, val, loss={:.4f} acc={:.4f}'.format(epoch, vl, va))

            if va >= trlog['max_acc']:
                print ('********* New best model!!! *********')
                trlog['max_acc'] = va
                trlog['max_acc_epoch'] = epoch
                self.save_model('max_acc')
                torch.save(optimizer.state_dict(), osp.join(args.save_path, 'optimizer_best.pth'))

            trlog['train_loss'].append(tl)
            trlog['train_acc'].append(ta)
            trlog['val_loss'].append(vl)
            trlog['val_acc'].append(va)

            torch.save(trlog, osp.join(args.save_path, 'trlog'))

            if args.save_all:

                self.save_model('epoch-%d'%epoch)
                torch.save(optimizer.state_dict(), osp.join(args.save_path,'optimizer_latest.pth'))

            print('Best epoch {}, best val acc={:.4f}'.format(trlog['max_acc_epoch'], trlog['max_acc']))
            print ('This epoch takes %d seconds'%(time.time()-start_time),'\nStill need %.2f hour to finish'%((time.time()-start_time)*(args.max_epoch-epoch)/3600))
            lr_scheduler.step()

        writer.close()
        result_list=['Best validation epoch {},\nbest val Acc {:.4f}'.format(trlog['max_acc_epoch'], trlog['max_acc'],)]
        save_list_to_txt(os.path.join(args.save_path,'results.txt'),result_list)