-
Notifications
You must be signed in to change notification settings - Fork 1
/
xception_detector.py
99 lines (83 loc) · 3.47 KB
/
xception_detector.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
'''
Reference:
@inproceedings{rossler2019faceforensics++,
title={Faceforensics++: Learning to detect manipulated facial images},
author={Rossler, Andreas and Cozzolino, Davide and Verdoliva, Luisa and Riess, Christian and Thies, Justus and Nie{\ss}ner, Matthias},
booktitle={Proceedings of the IEEE/CVF international conference on computer vision},
pages={1--11},
year={2019}
}
'''
import os
import datetime
import logging
import numpy as np
from sklearn import metrics
from typing import Union
from collections import defaultdict
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.nn import DataParallel
from torch.utils.tensorboard import SummaryWriter
from metrics.base_metrics_class import calculate_metrics_for_train
from .base_detector import AbstractDetector
from detectors import DETECTOR
from networks import BACKBONE
from loss import LOSSFUNC
logger = logging.getLogger(__name__)
@DETECTOR.register_module(module_name='xception')
class XceptionDetector(AbstractDetector):
def __init__(self):
super().__init__()
self.backbone = self.build_backbone()
self.loss_func = self.build_loss()
self.prob, self.label = [], []
self.correct, self.total = 0, 0
def build_backbone(self):
# prepare the backbone
backbone_class = BACKBONE['xception']
backbone = backbone_class({'mode': 'original',
'num_classes': 1, 'inc': 3, 'dropout': False})
# if donot load the pretrained weights, fail to get good results
state_dict = torch.load('./pretrained/xception-b5690688.pth')
for name, weights in state_dict.items():
if 'pointwise' in name:
state_dict[name] = weights.unsqueeze(-1).unsqueeze(-1)
state_dict = {k:v for k, v in state_dict.items() if 'fc' not in k}
backbone.load_state_dict(state_dict, False)
print('Load pretrained model successfully!')
return backbone
def build_loss(self):
# prepare the loss function
loss_class = LOSSFUNC['bce']
loss_func = loss_class()
return loss_func
def features(self, data_dict: dict) -> torch.tensor:
return self.backbone.features(data_dict['image']) #32,3,256,256
def classifier(self, features: torch.tensor) -> torch.tensor:
return self.backbone.classifier(features)
def get_losses(self, data_dict: dict, pred_dict: dict) -> dict:
label = data_dict['label']
pred = pred_dict['cls']
loss = self.loss_func(pred, label)
loss_dict = {'overall': loss}
return loss_dict
def get_train_metrics(self, data_dict: dict, pred_dict: dict) -> dict:
label = data_dict['label']
pred = pred_dict['cls']
pred = pred.squeeze(1)
# compute metrics for batch data
auc, eer, acc, ap = calculate_metrics_for_train(label.detach(), pred.detach())
metric_batch_dict = {'acc': acc, 'auc': auc, 'eer': eer, 'ap': ap}
return metric_batch_dict
def get_test_metrics(self):
pass
def forward(self, data_dict: dict, inference=False) -> dict:
# get the features by backbone
features = self.features(data_dict)
# get the prediction by classifier
pred = self.classifier(features)
pred_dict = {'cls': pred}
return pred_dict