-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
142 lines (119 loc) · 4.75 KB
/
main.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
import os
import sys
import random
import copy
import time
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torchvision
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
import torch.nn.functional as F
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_score
from sklearn.metrics import classification_report
from sklearn.metrics import plot_confusion_matrix
from sklearn.metrics import f1_score
from sklearn.metrics import confusion_matrix
from sklearn.svm import SVC as SVM
from utils import feature_selection, transfer_learning
from utils.feature_selection import *
from utils.transfer_learning import *
from AbSCA import *
from local_search import *
import warnings
warnings.filterwarnings('ignore')
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--data_directory', type=str, default = './', help='Directory where the image data is stored')
parser.add_argument('--epochs', type=int, default = 20, help='Number of Epochs of training')
parser.add_argument('--batch_size', type=int, default = 4, help='Batch size for training')
parser.add_argument('--learning_rate', type=float, default = 0.001, help='Learning Rate')
parser.add_argument('--momentum', type=float, default = 0.9, help='Momentum')
args = parser.parse_args()
DIR_PATH = args.data_directory # enter directory path for dataset
if DIR_PATH[-1]=='/':
DIR_PATH = DIR_PATH[:-1]
TRAIN_DIR_PATH = os.path.join(DIR_PATH, 'train')
VAL_DIR_PATH = os.path.join(DIR_PATH, 'val')
# image transformations
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
transformations = {
'train': transforms.Compose([
transforms.Resize((224, 224)),
transforms.RandomHorizontalFlip(),
transforms.RandomVerticalFlip(),
transforms.RandomAffine(
degrees=(-180, 180), translate=(0.1, 0.1), scale=(0.9, 1.1), shear=(-5, 5)),
transforms.ToTensor(),
transforms.Normalize(mean, std)
]),
'val': transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean, std)
])
}
train_dataset = torchvision.datasets.ImageFolder(TRAIN_DIR_PATH, transform=transformations['train'])
val_dataset = torchvision.datasets.ImageFolder(VAL_DIR_PATH, transform=transformations['val'])
classes_to_idx = train_dataset.class_to_idx
# hyperparameters
train_batch_size = args.batch_size
learning_rate = args.learning_rate
num_classes = len(classes_to_idx)
num_epochs = args.epochs
momentum = args.momentum
phases = ['training', 'validation']
device = None
if torch.cuda.is_available():
device = torch.device('cuda')
else:
device = torch.device('cpu')
# dataloaders
data_loader = {
'training': DataLoader(dataset=train_dataset,
batch_size=train_batch_size,
shuffle=True,
num_workers=4),
'validation': DataLoader(dataset=val_dataset,
batch_size=1,
shuffle=False,
num_workers=4)
}
for phase in phases:
print(f'Length of {phase} loader = {len(data_loader[phase])}')
# model, criterion, optimizer
model = torchvision.models.densenet201(pretrained=True)
model = model.to(device)
model = ConvNet(model, num_classes)
model = model.to(device)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate, momentum=momentum)
# training CNN model
start = time.time()
model, history = train_model(model, criterion, optimizer, data_loader, train_batch_size, num_epochs)
duration = time.time() - start
print(f'Training complete in {(duration // 60):.0f}mins {(duration % 60):.0f}s')
# extract features
features = []
true_labels = []
train_loader = DataLoader(dataset=train_dataset,
batch_size=1,
shuffle=False,
num_workers=4)
# training set features
features, true_labels = eval_model_extract_features(features, true_labels, model, dataloader=train_loader, phase='training')
# validation set features
features, true_labels = eval_model_extract_features(features, true_labels, model, dataloader=data_loader['validation'], phase='validation')
# get features
X, y = get_features(features, true_labels)
# Applying Adaptive beta HC embedded Sine-Cosine Optimization Algorithm for FS
soln_AbSCA = AbSCA(num_agents=20, max_iter=20, train_data=X, train_label=y)
# validate AbSCA feature selection
agent = soln_AbSCA.best_agent.copy()
validate_FS(X, y, agent)