-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures_classification.py
More file actions
64 lines (56 loc) · 2.6 KB
/
features_classification.py
File metadata and controls
64 lines (56 loc) · 2.6 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
import os
import numpy as np
import pandas as pd
from keras.applications.vgg16 import VGG16, preprocess_input
from keras.preprocessing import image
from sklearn.cluster import KMeans
import tensorflow as tf
def load_pretrained_model():
device = '/GPU:0' if tf.config.list_physical_devices('GPU') else '/CPU:0'
print(f"Using device: {device}")
with tf.device(device):
model = VGG16(weights='imagenet', include_top=False, pooling='avg')
return model
def extract_dnn_features(img_path, model):
img = image.load_img(img_path, target_size=(224, 224))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)
device = '/GPU:0' if tf.config.list_physical_devices('GPU') else '/CPU:0'
with tf.device(device):
features = model.predict(img_array)
return features.flatten()
def classify_images_with_dnn(base_folder, n_clusters=2):
model = load_pretrained_model()
subfolder_labels = []
subfolder_names = []
for subfolder in os.listdir(base_folder):
subfolder_path = os.path.join(base_folder, subfolder)
if os.path.isdir(subfolder_path):
image_features = []
for filename in os.listdir(subfolder_path):
if filename.endswith('.jpg') or filename.endswith('.png'):
image_path = os.path.join(subfolder_path, filename)
features = extract_dnn_features(image_path, model)
image_features.append(features)
if len(image_features) >= n_clusters:
kmeans = KMeans(n_clusters=n_clusters, init='k-means++', random_state=42)
image_labels = kmeans.fit_predict(image_features)
label_counts = np.bincount(image_labels)
if len(label_counts) == 1 or label_counts[0] != label_counts[1]:
folder_label = np.argmax(label_counts)
else:
folder_label = -1
subfolder_labels.append(folder_label)
subfolder_names.append(subfolder)
else:
subfolder_labels.append(-1)
subfolder_names.append(subfolder)
return subfolder_names, subfolder_labels
def save_results_to_csv(folder_names, labels, output_file):
results_df = pd.DataFrame({'Folder': folder_names, 'Cluster_Label': labels})
results_df.to_csv(output_file, index=False)
base_folder = r'cropped_output_frames'
output_file = 'DNN_classification_results.csv'
folder_names, labels = classify_images_with_dnn(base_folder)
save_results_to_csv(folder_names, labels, output_file)