-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_singleview_cifar10.py
256 lines (226 loc) · 10 KB
/
run_singleview_cifar10.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
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
import os
import yaml
import torch
import random
import logging
import argparse
import numpy as np
from datetime import datetime
import pytorch_lightning as pl
import helpers.ks_imageloader_single as kl
from torchvision import transforms
from collections import OrderedDict
from pytorch_lightning.callbacks import EarlyStopping
from helpers.imagenet_imageloader_single import ImagenetImagesLoader
# logging details
now =datetime.now()
current_time =now.strftime("%H%M%S")
logger = logging.getLogger()
logger.setLevel(logging.INFO)
handler = logging.FileHandler(f"single_view_{current_time}.log")
handler.setLevel(logging.INFO)
logger.addHandler(handler)
# logging.basicConfig(filename=f"single_view_{current_time}.log", format='%(asctime)s - %(message)s', level=logging.INFO)
def main(config):
"""
Main process
"""
# Extract information from configuration file.
# The first extracted information is the dataset mean and standard deviation that will be used for the transformation
mean, std = [], []
img_size = 227
for val in config['image_mean'].split(","):
mean.append(float(val))
for val in config['image_std'].split(","):
std.append(float(val))
mean = np.array(mean)
std = np.array(std)
# Accepted models are:
# Single view models:
# * alexnet, inception, vgg16, vgg19
# Multi view models (require a pretrained model):
# * alexnet_mv_max, vgg16_mx_max, inception_mv_max
# Single View
if config['model_to_use'] == "alexnet":
logging.info("Using alexnet model")
from models.alexnet import AlexnetModel
model = AlexnetModel(hparams={"lr": 0.0002}, num_classes=config['num_classes'],
pretrained=True, seed=config['manualSeed'])
img_size = 227
elif config['model_to_use'] == "resnet50":
logging.info("Using resnet50 model")
from models.attention.models.resnet import ResNet50Cbam
model = ResNet50Cbam(hparams={"lr": 0.0002}, num_classes=config['num_classes'],
seed=config['manualSeed'])
img_size = 224
elif config['model_to_use'] == "inception":
logging.info("using inception model")
from models.inception import InceptionModel
model = InceptionModel(hparams={"lr": 0.0002}, num_classes=config['num_classes'],
pretrained=True, seed=config['manualSeed'])
img_size = 224
elif config['model_to_use'] == "vgg16":
logging.info("using vgg16 model")
from models.vgg16 import Vgg16Model
model = Vgg16Model(hparams={"lr": 0.00005}, num_classes=config['num_classes'],
pretrained=True, seed=config['manualSeed'])
img_size = 224
elif config['model_to_use'] == "vgg19":
logging.info("using vgg19 model")
from models.vgg19 import Vgg19Model
model = Vgg19Model(hparams={"lr": 0.00005}, num_classes=config['num_classes'],
pretrained=True, seed=config['manualSeed'])
img_size = 224
# multi view
elif config['model_to_use'] == "alexnet_mv_max":
logging.info("using multiview alexnet max model")
from models.multiview import MultiViewMaxPool
model = MultiViewMaxPool(hparams={"lr": 0.0002}, num_classes=config['num_classes'],
pretrained=True, seed=config['manualSeed'])
checkpoint = torch.load(r'C:\Users\15B38LA\Downloads\mixed_kidney_yelbeze.ckpt',
map_location=lambda storage, loc: storage)
test = OrderedDict({k: v for k, v in checkpoint['state_dict'].items() if 'classifier' not in k})
model.load_state_dict(test, strict=False)
img_size = 227
elif config['model_to_use'] == "vgg16_mv_max":
logging.info("using multiview vgg16 max model")
from models.multiview import MultiViewPoolVGG16
model = MultiViewPoolVGG16(hparams={"lr": 0.00005}, num_classes=config['num_classes'], pretrained=True,
seed=config['manualSeed'])
checkpoint = torch.load(r'C:\Users\15B38LA\Documents\vgg16-mixed.ckpt',
map_location=lambda storage, loc: storage)
test = OrderedDict({k: v for k, v in checkpoint['state_dict'].items() if 'classifier' not in k})
model.load_state_dict(test, strict=False)
img_size = 227
elif config['model_to_use'] == "inception_mv_max":
logging.info("using multi view inception max model")
from models.inception import InceptionModeMulti
model = InceptionModeMulti(hparams={"lr": 0.0002}, num_classes=config['num_classes'], pretrained=True,
seed=config['manualSeed'])
checkpoint = torch.load(r'C:\Users\15B38LA\Documents\inception_mixed.ckpt',
map_location=lambda storage, loc: storage)
test = OrderedDict({k: v for k, v in checkpoint['state_dict'].items() if 'fc' not in k})
model.load_state_dict(test, strict=False)
img_size = 224
# default case
else:
raise ValueError('Model is not implemented')
# Transformations
train_transformations = [
# transforms.ToPILImage(),
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(15),
transforms.ToTensor(),
transforms.Normalize(mean, std)
]
"""
transforms.RandomChoice([
transforms.RandomHorizontalFlip(p=0.5),
transforms.RandomVerticalFlip(p=0.5),
transforms.Pad(50, fill=0, padding_mode="symmetric"),
transforms.RandomPerspective(distortion_scale=0.4, p=0.5),
transforms.RandomAffine(degrees=(-90, 90), translate=(0, 0.2), scale=[0.5, 1]),
# transforms.ColorJitter(brightness=0.35, contrast=0.4, saturation=0.5, hue=0),
transforms.RandomRotation(degrees=(-180, 180)),
]),
transforms.Resize((IMG_SIZE, IMG_SIZE)),
transforms.ToTensor(),
transforms.Normalize(mean, std)
]
"""
test_transformations = [
# transforms.Resize((IMG_SIZE, IMG_SIZE)),
transforms.ToTensor(),
# transforms.Normalize(mean, std)
]
if config['are_images_gray'] == "yes":
train_transformations.insert(0, transforms.Grayscale(num_output_channels=3))
test_transformations.insert(0, transforms.Grayscale(num_output_channels=3))
if config['use_augmentation']:
# with augmentation
image_transforms = {
"train": transforms.Compose(train_transformations),
"test": transforms.Compose(test_transformations)
}
else:
# without augmentation
image_transforms = {
"train": transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((img_size, img_size)),
transforms.ToTensor(),
transforms.Normalize(mean, std)
]),
"test": transforms.Compose([
transforms.ToPILImage(),
transforms.Resize((img_size, img_size)),
transforms.ToTensor(),
transforms.Normalize(mean, std)
])
}
# Early stopping
stopping = EarlyStopping(
monitor='val_loss',
min_delta=0.0,
patience=30,
verbose=False,
mode='min'
)
if config['manualSeed'] != None:
manualSeed = config['manualSeed']
np.random.seed(manualSeed)
random.seed(manualSeed)
torch.manual_seed(manualSeed)
torch.cuda.manual_seed(manualSeed)
torch.cuda.manual_seed_all(manualSeed)
torch.backends.cudnn.enabled = False
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
pl.seed_everything(manualSeed)
os.environ['PYTHONHASHSEED'] = str(manualSeed)
print("STD => ", std)
print("MEAN => ", mean)
print("IMAGE TRANSFORMATIONS => ", image_transforms)
logging.info(f"transformations: {image_transforms}")
# Variable that holds the route to the image zip files. Change this value if
# you wish to run the test on a different set of images. IMPORTANT: The zip file
# must contain images ordered in the same structure ("train" and "test" folders).
loader = ImagenetImagesLoader(images_path='.',
val_percentage=0.2,
train_batch_size=10,
train_transformations=image_transforms["train"],
test_transformations=image_transforms["test"],
seed=manualSeed)
pl.seed_everything(config['manualSeed'])
# Class
logging.info(f"min epochs: {config['min_exec_epochs']}")
logging.info(f"max epochs: {config['max_exec_epochs']}")
trainer = pl.Trainer(gpus=None,
max_epochs=config['max_exec_epochs'],
min_epochs=config['min_exec_epochs'],
# logger=logger,
# callbacks=[stopping],]
progress_bar_refresh_rate=50,
checkpoint_callback=False, # disable checkpoint logs
# auto_lr_find=True,
deterministic=True
)
print('### Model: ###')
print(model)
logging.info(f"model: {model}")
trainer.fit(model, loader, ckpt_path=r'C:\Users\15B38LA\Downloads\attention_resnet50 (1).ckpt')
trainer.test(model, datamodule=loader)
if config['save_model']:
trainer.save_checkpoint(f"saves/{config['model_name']}.ckpt")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--gpu', default='cuda')
parser.add_argument('--yaml', default='config.yaml')
args = parser.parse_args()
config = yaml.load(open(args.yaml, 'r'), Loader=yaml.FullLoader)
# Setting device if cuda is available
cuda = torch.cuda.is_available()
device = torch.device(args.gpu if cuda else 'cpu')
# start the main process
main(config)