import cv2
import numpy as np
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from tensorflow.keras.utils import to_categorical
# Dummy dataset creation: 100 images of 64x64 with 3 channels (RGB)
np.random.seed(42)
X_rgb = np.random.randint(0, 256, (100, 64, 64, 3), dtype=np.uint8)
y = np.random.randint(0, 2, 100) # Binary classification
def convert_to_grayscale(images):
return np.array([cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) for img in images])
def convert_to_hsv(images):
return np.array([cv2.cvtColor(img, cv2.COLOR_RGB2HSV) for img in images])
# Convert images
X_gray = convert_to_grayscale(X_rgb)
X_hsv = convert_to_hsv(X_rgb)
# Reshape grayscale to add channel dimension
X_gray = X_gray.reshape(-1, 64, 64, 1)
# Normalize all images
X_rgb = X_rgb / 255.0
X_gray = X_gray / 255.0
X_hsv = X_hsv / 255.0
# Prepare labels
y_cat = to_categorical(y, 2)
# Split data
X_train_rgb, X_val_rgb, y_train, y_val = train_test_split(X_rgb, y_cat, test_size=0.2, random_state=42)
X_train_gray, X_val_gray, _, _ = train_test_split(X_gray, y_cat, test_size=0.2, random_state=42)
X_train_hsv, X_val_hsv, _, _ = train_test_split(X_hsv, y_cat, test_size=0.2, random_state=42)
# Define simple CNN model
def create_model(input_shape):
model = Sequential([
Conv2D(16, (3,3), activation='relu', input_shape=input_shape),
MaxPooling2D((2,2)),
Flatten(),
Dense(32, activation='relu'),
Dense(2, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
return model
# Train and evaluate on RGB
model_rgb = create_model((64,64,3))
history_rgb = model_rgb.fit(X_train_rgb, y_train, epochs=5, batch_size=16, validation_data=(X_val_rgb, y_val), verbose=0)
# Train and evaluate on Grayscale
model_gray = create_model((64,64,1))
history_gray = model_gray.fit(X_train_gray, y_train, epochs=5, batch_size=16, validation_data=(X_val_gray, y_val), verbose=0)
# Train and evaluate on HSV
model_hsv = create_model((64,64,3))
history_hsv = model_hsv.fit(X_train_hsv, y_train, epochs=5, batch_size=16, validation_data=(X_val_hsv, y_val), verbose=0)
# Collect final validation accuracies
val_acc_rgb = history_rgb.history['val_accuracy'][-1] * 100
val_acc_gray = history_gray.history['val_accuracy'][-1] * 100
val_acc_hsv = history_hsv.history['val_accuracy'][-1] * 100
print(f"Validation Accuracy RGB: {val_acc_rgb:.2f}%")
print(f"Validation Accuracy Grayscale: {val_acc_gray:.2f}%")
print(f"Validation Accuracy HSV: {val_acc_hsv:.2f}%")