-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_classification.py
226 lines (177 loc) · 6.43 KB
/
image_classification.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
import json
import os
from argparse import ArgumentParser
from datetime import datetime
from pathlib import Path
from typing import Union, Tuple, Dict, List, Any
import torch
import torchvision.transforms.v2 as transforms
import yaml
from einops import repeat
from rich.progress import track
from torch import Tensor, nn
from torch.utils.data import Dataset, DataLoader
from configs.config import ConfigDiffusion, ConfigLatentDiffusion
from configs.settings import GEN_DATASET_DIR, CONFIGS
from data.utils import get_image
from models.ConvNeXt.model import ConvNeXt_M
from models.InceptionV3.model import InceptionV3_M
def cli_main():
"""
Command-line interface for initializing and parsing arguments.
Returns:
Namespace: A namespace object containing parsed arguments.
"""
parser = ArgumentParser()
parser.add_argument(
"-c",
"--config",
help="Type of model",
choices=["Diffusion", "LatentDiffusion"],
type=str,
required=True,
)
parser.add_argument(
"-cf",
"--config-file",
help="Filename for configs",
type=str,
default="base_gpu.yaml",
)
parser.add_argument(
"--new-model",
help="Whether to initialize a new model (ConvNeXt_M) or load an old one (InceptionV3_M)",
action="store_true",
)
return parser.parse_args()
class ImageDataset(Dataset):
def __init__(
self,
config: Union[ConfigDiffusion, ConfigLatentDiffusion],
) -> None:
super().__init__()
self.__config = config
self._diffusion = isinstance(config, ConfigDiffusion)
self.transforms = transforms.Compose(
[transforms.ToImage(), transforms.ToDtype(torch.float32, scale=True)]
)
self.img_height = config.get("img_height", 90)
self.img_width = config.get("img_width", 1400)
self.__load_dataset__()
print(
f"Size of dataset: {len(self.dataset)} || Length of writer styles -- {len(self.map_writer_id)}"
)
def __load_dataset__(self) -> None:
json_file_path = Path(
f"./data/json_writer_ids/train_writer_ids_{'iamondb' if self._diffusion else 'iamdb'}.json"
)
json_writer_id_info_path = Path(
f"{GEN_DATASET_DIR}/{'Diffusion' if self._diffusion else 'LatentDiffusion'}/writer_id_info.json"
)
with open(json_file_path, mode="r") as fp:
self.__map_writer_id = json.load(fp)
with open(json_writer_id_info_path, mode="r") as fp:
map_images = json.load(fp)
centering = (0.0, 0.5) if self._diffusion else (0.5, 0.5)
dataset = []
for img_name, writer_id in track(
map_images.items(), description="Loading generated dataset..."
):
img_path = Path(
f"{GEN_DATASET_DIR}/{'Diffusion' if self._diffusion else 'LatentDiffusion'}/{img_name}"
)
image = get_image(
img_path,
width=self.img_width,
height=self.img_height,
latent=self._diffusion,
centering=centering,
)
dataset.append(
{
"writer": writer_id,
"image": image,
}
)
self.__dataset = dataset
@property
def config(self) -> Union[ConfigDiffusion, ConfigLatentDiffusion]:
return self.__config
@property
def dataset(self) -> List[Dict[str, Any]]:
return self.__dataset
@property
def map_writer_id(self) -> Dict[str, int]:
return self.__map_writer_id
def __getitem__(self, index: int) -> Tuple[Tensor, Tensor]:
image = self.transforms(self.dataset[index]["image"])
writer_id = torch.tensor(self.dataset[index]["writer"], dtype=torch.int32)
if not self._diffusion:
image = repeat(image, "1 h w -> 3 h w")
return writer_id, image
def __len__(self) -> int:
return len(self.dataset)
def evaluate_model(classifier: nn.Module, data: DataLoader) -> float:
classifier.eval()
performance_score = 0.0
with torch.inference_mode():
for batch in track(data):
writer_id, image = batch
image = image.to(device)
writer_id = writer_id.type(torch.LongTensor).to(device)
outputs = classifier(image)
performance_score += compute_accuracy(outputs, writer_id)
performance_score /= len(data)
return performance_score
def compute_accuracy(predictions: Tensor, labels: Tensor) -> float:
classes = torch.argmax(predictions, dim=1)
return torch.mean((classes == labels).float()).item()
if __name__ == "__main__":
args = cli_main()
config_file = f"configs/{args.config}/{args.config_file}"
config = CONFIGS[args.config].from_yaml_file(
file=config_file, decoder=yaml.load, Loader=yaml.Loader
)
device = torch.device(config.device)
dataset = ImageDataset(config=config)
num_class = len(dataset.map_writer_id)
if args.new_model:
model = ConvNeXt_M(num_class, device=device)
model.load_state_dict(
torch.load(
f"./model_checkpoints/ConvNeXt/ConvNeXt_M-{args.config.lower()}.pth",
map_location=device,
)
)
else:
model = InceptionV3_M(num_class, device=device)
model.load_state_dict(
torch.load(
f"./model_checkpoints/InceptionV3/InceptionV3_M-{args.config.lower()}.pth",
map_location=device,
)
)
dataloader = DataLoader(
dataset,
batch_size=config.batch_size,
shuffle=True,
pin_memory=True,
num_workers=os.cpu_count(),
)
accuracy = evaluate_model(model, dataloader)
print(
f"Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} "
f"|| Model: {'ConvNeXt' if args.new_model else 'InceptionV3'} "
f"|| Model Type: {args.config} "
f"|| Accuracy: {accuracy * 100:.4f}%\n"
)
with open(
f"./model_checkpoints/{'ConvNeXt' if args.new_model else 'InceptionV3'}/results.txt",
mode="a+",
) as file:
file.write(
f"Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} "
f"|| Model: {'ConvNeXt' if args.new_model else 'InceptionV3'} "
f"|| Model Type: {args.config} "
f"|| Accuracy: {accuracy * 100:.4f}%\n"
)