-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cynder_Model_Trainer.txt
147 lines (119 loc) · 4.65 KB
/
Cynder_Model_Trainer.txt
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
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing import image_dataset_from_directory
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
import cv2
import numpy as np
import os
import colorama
from keras.utils import to_categorical
from tensorflow.keras.callbacks import LearningRateScheduler
from tensorflow.keras.regularizers import l2
#Test Directory r"\Data\Cynder\Test"
#Train directory r"\Data\Cynder\train"
class_names = ['Train_Validate', 'Train_Data', 'Test_Data', 'Test_Validate', "Test_Back", "Train_Back", "Train_Empty", "Test_Empty"]
# Define the base VGG19 model with pre-trained weights
base_model = tf.keras.applications.InceptionV3(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# Freeze the base model layers
for layer in base_model.layers:
layer.trainable = False
# Add a custom classification head
num_classes = 4
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu', kernel_regularizer=l2(0.1))(x)
predictions = Dense(num_classes, activation='sigmoid')(x)
# Define the fine-tuned model
from tensorflow.keras.regularizers import l2
# Add a custom classification head with weight decay
num_classes = 2
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu', kernel_regularizer=l2(0.01))(x)
predictions = Dense(num_classes, activation='sigmoid')(x)
# Define the fine-tuned model with weight decay
model = Model(inputs=base_model.input, outputs=predictions)
# Compile the model with weight decay
model.compile(optimizer=Adam(learning_rate=0.001),
loss='binary_crossentropy',
metrics=['accuracy'])
# Define the data augmentation
train_datagen = ImageDataGenerator(rotation_range=30,
rescale=1./255,
validation_split=0.2)
# Load the training dataset with data augmentation
train_dataset = train_datagen.flow_from_directory(
r"\Data\Cynder\train",
target_size=(224, 234), # update target_size to desired size
batch_size=64,
class_mode='categorical',
shuffle=True,
seed=123,
subset='training')
# Load the validation dataset
validation_dataset = image_dataset_from_directory(
r"\Data\Cynder\Test",
labels='inferred',
label_mode='categorical',
color_mode='rgb',
batch_size=64,
image_size=(224, 224), # update image_size to desired size
shuffle=True,
seed=123,
validation_split=0.2,
subset='validation'
)
# Train the model
# ten epochs works unuslay well., 15 works better!
history = model.fit(
train_dataset,
epochs=5,
validation_data=validation_dataset
)
# save the model!!
model.save('my_model.h5')
# Define the color and thickness of the bounding box
color = (0, 255, 0) # green
thickness = 2
# Use the fine-tuned model for Skylanders figure recognition
cap = cv2.VideoCapture(0)
while True:
# Read the frame from the webcam
ret, frame = cap.read()
if not ret:
print("Failed to capture frame from webcam")
break
# Preprocess the frame for the model
img = cv2.resize(frame, (224, 224))
img = img / 255.0
img = np.expand_dims(img, axis=0)
# Make predictions on the frame
predictions = model.predict(img)
# Convert the predictions to class names
predicted_classes = np.argmax(predictions, axis=1)
class_names = ['Train_Validate', 'Train_Data', 'Test_Data', 'Test_Validate']
prediction = model.predict(img)
predicted_class = class_names[np.argmax(prediction)]
print(f"The peredicted class is {predicted_class}")
if np.max(predictions) > 0.7:
# Convert the image to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Apply thresholding to create a binary image
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
# Find contours in the binary image
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print("Perdicted Classes")
print(predicted_class)
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
cv2.rectangle(frame, (x, y), (x+w, y+h), color, thickness)
# Display the frame with the bounding box
cv2.imshow('Object Detection', frame)
# Exit on ESC
if cv2.waitKey(1) == 27:
break
# Release the webcam and close the window
cap.release()
cv2.destroyAllWindows()