-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrainPatchImagenette.py
53 lines (45 loc) · 1.81 KB
/
TrainPatchImagenette.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
import torch, torchvision
import logging, os
from PatchTrainer import train
from PatchApply import getTransformations
ROOT = r"C:\Users\96585\Desktop\imagenette2-320"
def getCIFAR10Dataset():
"""
Imagenette dataset
:return: return the dataset for using
"""
# FIXME: otherwise, change the ROOT to a new folder and change download to TRUE
DOWNLOAD = False
trans = torchvision.transforms.Compose(
[torchvision.transforms.Resize((224, 224)),
torchvision.transforms.ToTensor()])
_trainingSet = torchvision.datasets.ImageFolder(root=os.path.join(ROOT, 'train'),
transform=trans)
_testingSet = torchvision.datasets.ImageFolder(root=os.path.join(ROOT, 'val'),
transform=trans)
return _trainingSet, _testingSet
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
trainSet, testSet = getCIFAR10Dataset()
if torch.cuda.is_available():
device = 'cuda'
else:
logging.warning('The code is suggested to run in CUDA. No CUDA detected')
device = 'cpu'
side = 40
size = 40
patchOnly, patchAndTrigger = getTransformations(224, side, size)
m = torchvision.models.resnet50(num_classes=10)
anonM = torch.load(r'resnet.model')['model']
m.load_state_dict(anonM)
trans = torchvision.transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
print("SIDE=%d,SIZE=%d" % (side, size))
patch = torch.rand(3, 224, 224, device=device)
train('resnet', trainSet, testSet, m, patch,
transformation=[patchOnly, patchAndTrigger],
norm=trans,
target=[-2, 9],
rounds=10,
valiAccCheck=True,
inProgressShow=True,
batchSize=16, lr=0.001, autoRatio=0.5)