-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_CNN_3.py
More file actions
141 lines (109 loc) · 4.74 KB
/
train_CNN_3.py
File metadata and controls
141 lines (109 loc) · 4.74 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
"""
CNN Training Code
Version: 2
Function:
Dataset: excluding "stacked cards" data set
accuracy: 0.5517 - loss: 1.3344 - val_accuracy: 0.5412 - val_loss: 1.5510
Epoch: 40/50
Batch-size: 64
"""
import os
import pandas as pd
import cv2
import numpy as np
from sklearn.preprocessing import LabelEncoder
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.utils import Sequence
from tensorflow.keras.callbacks import EarlyStopping
import pickle
# ======================= CONFIG =======================
TARGET_SIZE = (128, 128)
BATCH_SIZE = 64
# ======================= GENERATOR =======================
class CardDatasetGenerator(tf.keras.utils.Sequence):
def __init__(self, dataframe, label_encoder, batch_size=64, image_size=(128, 128), shuffle=True):
self.df = dataframe.copy()
self.label_encoder = label_encoder
self.batch_size = batch_size
self.image_size = image_size
self.shuffle = shuffle
self.indices = np.arange(len(self.df))
self.on_epoch_end()
def __len__(self):
return int(np.ceil(len(self.df) / self.batch_size))
def on_epoch_end(self):
if self.shuffle:
np.random.shuffle(self.indices)
def __getitem__(self, index):
batch_indices = self.indices[index * self.batch_size:(index + 1) * self.batch_size]
batch_df = self.df.iloc[batch_indices]
images, labels = [], []
for _, row in batch_df.iterrows():
filename, label, root = row.get("filename"), row.get("class"), row.get("root")
if pd.isna(filename) or pd.isna(label) or pd.isna(root):
continue
img_path = os.path.join(root, filename)
if not os.path.exists(img_path):
continue
img = cv2.imread(img_path)
if img is None:
continue
if len(img.shape) == 2 or img.shape[2] == 1:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
else:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, self.image_size)
img = img.astype(np.float32) / 255.0
images.append(img)
labels.append(label)
if not images:
raise ValueError(f"No valid images found in batch {index}.")
X = np.array(images)
y = self.label_encoder.transform(labels)
return X, y
# ======================= MAIN =======================
if __name__ == "__main__":
df1 = pd.read_csv(r"C:\Users\zizhu\.ssh\ps_Project\.kaggle\archive\train_labels.csv")
df2 = pd.read_csv(r"C:\Users\zizhu\.ssh\ps_Project\tree_cards_database\cards.csv")
df2 = df2.rename(columns={"filename": "filename", "class": "class"})
df2 = df2[["filename", "class"]]
df1["root"] = df1["filename"].apply(
lambda x: r"C:\Users\zizhu\.ssh\ps_Project\.kaggle\archive\train\train" if os.path.exists(
os.path.join(r"C:\Users\zizhu\.ssh\ps_Project\.kaggle\archive\train\train", x)) else
r"C:\Users\zizhu\.ssh\ps_Project\.kaggle\archive\test\test")
df2["root"] = r"C:\Users\zizhu\.ssh\ps_Project\tree_cards_database"
df = pd.concat([df1, df2], ignore_index=True)
label_encoder = LabelEncoder()
label_encoder.fit(df["class"])
with open("label_encoder.pkl", "wb") as f:
pickle.dump(label_encoder, f)
df = df.sample(frac=1, random_state=42).reset_index(drop=True)
val_split = int(0.8 * len(df))
train_df, val_df = df.iloc[:val_split], df.iloc[val_split:]
train_gen = CardDatasetGenerator(train_df, label_encoder=label_encoder, batch_size=BATCH_SIZE, image_size=TARGET_SIZE)
val_gen = CardDatasetGenerator(val_df, label_encoder=label_encoder, batch_size=BATCH_SIZE, image_size=TARGET_SIZE, shuffle=False)
model = models.Sequential([
layers.Input(shape=(TARGET_SIZE[0], TARGET_SIZE[1], 3)),
layers.Conv2D(32, 3, activation='relu'),
layers.Conv2D(32, 3, activation='relu'),
layers.MaxPooling2D(2),
layers.Conv2D(64, 3, activation='relu'),
layers.Conv2D(64, 3, activation='relu'),
layers.MaxPooling2D(2),
layers.Conv2D(128, 3, activation='relu'),
layers.GlobalAveragePooling2D(),
layers.Dense(128, activation='relu'),
layers.Dropout(0.3),
layers.Dense(len(label_encoder.classes_), activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
early_stop = EarlyStopping(monitor='val_accuracy', patience=5, restore_best_weights=True)
model.fit(train_gen,
validation_data=val_gen,
epochs=50,
callbacks=[early_stop],
verbose=1)
model.save("model_CNN_3.keras")