-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcnnclassifier.py
More file actions
243 lines (196 loc) · 9.25 KB
/
Copy pathcnnclassifier.py
File metadata and controls
243 lines (196 loc) · 9.25 KB
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
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import transforms
from PIL import Image
from services import FileOps, Universal
from addons import ModelStore, ASTracer, ASReport
# from addons import ArchSmith
from ai import LLMInterface, InteractionContext, Interaction, LMProvider, LMVariant
"""
A CNN-based binary classifier for distinguishing between 'cc' and 'hf' classes.
"""
# === Model Definition ===
class CNNModel(nn.Module):
def __init__(self, dropout_rate=0.5, hidden_size=512, use_batchnorm=True):
super(CNNModel, self).__init__()
self.use_batchnorm = use_batchnorm
self.conv1 = nn.Conv2d(3, 32, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm2d(32) if use_batchnorm else nn.Identity()
self.conv2 = nn.Conv2d(32, 64, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm2d(64) if use_batchnorm else nn.Identity()
self.conv3 = nn.Conv2d(64, 128, kernel_size=3, padding=1)
self.bn3 = nn.BatchNorm2d(128) if use_batchnorm else nn.Identity()
self.conv4 = nn.Conv2d(128, 256, kernel_size=3, padding=1)
self.bn4 = nn.BatchNorm2d(256) if use_batchnorm else nn.Identity()
self.conv5 = nn.Conv2d(256, 256, kernel_size=3, padding=1)
self.bn5 = nn.BatchNorm2d(256) if use_batchnorm else nn.Identity()
self.pool = nn.MaxPool2d(2, 2)
self.dropout = nn.Dropout(dropout_rate)
self.fc1 = nn.Linear(256 * 7 * 7, hidden_size)
self.fc2 = nn.Linear(hidden_size, 1)
def forward(self, x):
x = self.pool(F.relu(self.bn1(self.conv1(x))))
x = self.pool(F.relu(self.bn2(self.conv2(x))))
x = self.pool(F.relu(self.bn3(self.conv3(x))))
x = self.pool(F.relu(self.bn4(self.conv4(x))))
x = self.pool(F.relu(self.bn5(self.conv5(x))))
x = torch.flatten(x, 1)
x = F.relu(self.fc1(x))
x = self.dropout(x)
x = self.fc2(x)
return x
# === Classifier Wrapper ===
class ImageClassifier:
class_names = ["cc", "hf"]
@staticmethod
def load_model(modelPath: str):
model = CNNModel()
checkpoint = torch.load(modelPath, map_location=Universal.getBestDevice())
state_dict = checkpoint.get("model_state_dict", checkpoint)
model.load_state_dict(state_dict, strict=False)
model.to(Universal.getBestDevice())
model.eval()
ImageClassifier.transform = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
])
return model
@staticmethod
def predict(input_path, tracer: ASTracer):
"""
Performs inference on a single image file or directory of images.
Returns a list of (path, predicted_label, confidence).
"""
model_ctx = None
model = None
if os.environ.get("LLM_INFERENCE", "False") != "True":
model_ctx = ModelStore.getModel("cnn")
if model_ctx is None or model_ctx.model is None:
raise RuntimeError("CNN model is not loaded. Ensure it is registered and loaded in ModelStore.")
model = model_ctx.model
# if dir or file path is provided
if isinstance(input_path, str):
# dir
if os.path.isdir(input_path):
image_paths = [
os.path.join(input_path, f)
for f in os.listdir(input_path)
if f.lower().endswith(('.png', '.jpg', '.jpeg'))
]
# file
elif os.path.isfile(input_path):
if input_path.lower().endswith(('.png', '.jpg', '.jpeg')):
image_paths = [input_path]
else:
raise ValueError(f"File is not a supported image type: {input_path}")
else:
raise FileNotFoundError(f"Input path does not exist: {input_path}")
# list
elif isinstance(input_path, list):
invalid_paths = [
path for path in input_path
if not isinstance(path, str) or
not os.path.isfile(path) or
not path.lower().endswith(('.png', '.jpg', '.jpeg'))
]
if invalid_paths:
raise FileNotFoundError(
"Invalid or unsupported image files:\n" + "\n".join(invalid_paths)
)
image_paths = input_path
else:
raise ValueError("Input must be a file path, directory path, or list of file paths")
results = []
for path in image_paths:
if os.environ.get("LLM_INFERENCE", "False") == "True":
cont = InteractionContext(
provider=LMProvider.QWEN,
variant=LMVariant.QWEN_VL_PLUS
)
cont.addInteraction(
Interaction(
role=Interaction.Role.USER,
content=("You are part of an historical artefact digitisation program. "
"Classify the attached image as Chinese Calligraphy (CC), if it contains traditional Chinese Calligraphy like text, "
"or human figures (HF), if it contains pictures of people. Stricly output only either 'hf' or 'cc', and nothing else.\n\nOutput:"),
imagePath=path,
imageFileType="image/{}".format(FileOps.getFileExtension(path))
),
imageMessageAcknowledged=True
)
try:
response = LLMInterface.engage(cont)
if isinstance(response, str):
raise Exception("Unexpected response: {}".format(response))
classification = "hf" in response.content.lower()
results.append((path, "hf" if classification else "cc", 100.0))
tracer.addReport(
ASReport(
source="IMAGECLASSIFIER PREDICT",
message="LLM classification result: {}".format("hf" if classification else "cc"),
extraData={"path": path, "response": response.content}
)
)
continue
except Exception as e:
tracer.addReport(
ASReport(
source="IMAGECLASSIFIER PREDICT ERROR",
message="LLM image classification failed; error: {}".format(e)
)
)
continue
try:
image = Image.open(path).convert("RGB")
image = ImageClassifier.transform(image).unsqueeze(0).to(Universal.getBestDevice())
with torch.no_grad():
output = model(image)
prob = torch.sigmoid(output).item()
label_idx = 1 if prob > 0.5 else 0
label = ImageClassifier.class_names[label_idx]
confidence = prob * 100 if label_idx == 1 else (1 - prob) * 100
results.append((path, label, confidence))
# Log report if tracer is provided
if tracer:
report = ASReport(
source="IMAGECLASSIFIER PREDICT",
message="Prediction: {} ({:.2f}%)".format(label, confidence),
extraData={"probability": prob, "label_index": label_idx}
)
tracer.addReport(report)
except Exception as e:
error_msg = "Error processing {}: {}".format(path, e)
if tracer:
error_report = ASReport(
source="IMAGECLASSIFIER PREDICT ERROR",
message=error_msg,
extraData={"path": path, "exception": str(e)}
)
tracer.addReport(error_report)
return results
# === Entry point for testing the classifier ===
# if __name__ == "__main__":
# ModelStore.setup(
# autoLoad=True,
# cnn=ImageClassifier.load_model
# )
# model_ctx = ModelStore.getModel("cnn")
# if model_ctx is None or model_ctx.model is None:
# raise RuntimeError("CNN model is not loaded. Ensure it is registered and loaded in ModelStore.")
# model = model_ctx.model
# test_tensor = torch.randn(1, 3, 224, 224).to(DEVICE)
# print(f"CNN model output shape: {model(test_tensor).shape}")
# LLMInterface.initDefaultClients()
# print(Universal.getBestDevice())
# t = ArchSmith.newTracer("cnn testing")
# # Predict on a sample image
# results = ImageClassifier.predict("Companydata/63 20231117 ACCCIM hosted a forum with SCCCI.jpg", tracer=t)
# t.end()
# ArchSmith.persist()
# print(results)
# # Print results
# for path, label, confidence in results:
# print(f"{path}: {label} - {confidence:.1f}%")