Alif Al Hasan
commited on
Commit
•
e58ca2a
1
Parent(s):
d8e218d
[Task] Model Training
Browse files[Description] Model Training and saved the model.
[Author]
@alifalhasan
- models/football_logo_model.h5 +3 -0
- requirements.txt +4 -1
- src/__init__.py +0 -0
- src/train/__init__.py +0 -0
- src/train/trainer.py +98 -0
models/football_logo_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c9150fe90396b2098a79532af7278286a7aa8ea39fbc99199982349c33f1ffd0
|
3 |
+
size 302849424
|
requirements.txt
CHANGED
@@ -1 +1,4 @@
|
|
1 |
-
gradio
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
numpy
|
3 |
+
tensorflow
|
4 |
+
scipy
|
src/__init__.py
ADDED
File without changes
|
src/train/__init__.py
ADDED
File without changes
|
src/train/trainer.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""train_model.py
|
2 |
+
|
3 |
+
This module trains a simple image classification model using TensorFlow/Keras.
|
4 |
+
|
5 |
+
"""
|
6 |
+
|
7 |
+
import os
|
8 |
+
import tensorflow as tf
|
9 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
10 |
+
from tensorflow.keras.models import Sequential
|
11 |
+
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
|
12 |
+
from tensorflow.keras.optimizers import Adam
|
13 |
+
from tensorflow.keras.preprocessing import image
|
14 |
+
import numpy as np
|
15 |
+
from scipy import misc
|
16 |
+
|
17 |
+
# Constants
|
18 |
+
IMG_SIZE = (224, 224)
|
19 |
+
BATCH_SIZE = 32
|
20 |
+
EPOCHS = 10
|
21 |
+
NUM_CLASSES = 5
|
22 |
+
|
23 |
+
def preprocess_image(img):
|
24 |
+
"""
|
25 |
+
Preprocess the input image for model prediction.
|
26 |
+
|
27 |
+
Parameters:
|
28 |
+
- img: Input image.
|
29 |
+
|
30 |
+
Returns:
|
31 |
+
- img: Preprocessed image.
|
32 |
+
"""
|
33 |
+
img = image.img_to_array(img)
|
34 |
+
img = np.expand_dims(img, axis=0)
|
35 |
+
img /= 255.0
|
36 |
+
return img
|
37 |
+
|
38 |
+
def train_model(dataset_path):
|
39 |
+
"""
|
40 |
+
Train the image classification model.
|
41 |
+
|
42 |
+
Parameters:
|
43 |
+
- dataset_path (str): Path to the dataset.
|
44 |
+
|
45 |
+
Returns:
|
46 |
+
- model: Trained Keras model.
|
47 |
+
"""
|
48 |
+
|
49 |
+
# Ensure the dataset path is correct
|
50 |
+
dataset_path = os.path.abspath(dataset_path)
|
51 |
+
|
52 |
+
# Data preprocessing
|
53 |
+
datagen = ImageDataGenerator(
|
54 |
+
rescale=1./255,
|
55 |
+
validation_split=0.2
|
56 |
+
)
|
57 |
+
|
58 |
+
train_generator = datagen.flow_from_directory(
|
59 |
+
dataset_path,
|
60 |
+
target_size=IMG_SIZE,
|
61 |
+
batch_size=BATCH_SIZE,
|
62 |
+
class_mode='categorical',
|
63 |
+
subset='training'
|
64 |
+
)
|
65 |
+
|
66 |
+
validation_generator = datagen.flow_from_directory(
|
67 |
+
dataset_path,
|
68 |
+
target_size=IMG_SIZE,
|
69 |
+
batch_size=BATCH_SIZE,
|
70 |
+
class_mode='categorical',
|
71 |
+
subset='validation'
|
72 |
+
)
|
73 |
+
|
74 |
+
# Model definition
|
75 |
+
model = Sequential()
|
76 |
+
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(224, 224, 3)))
|
77 |
+
model.add(MaxPooling2D((2, 2)))
|
78 |
+
model.add(Flatten())
|
79 |
+
model.add(Dense(64, activation='relu'))
|
80 |
+
model.add(Dense(NUM_CLASSES, activation='softmax'))
|
81 |
+
|
82 |
+
# Model compilation
|
83 |
+
model.compile(optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
|
84 |
+
|
85 |
+
# Model training
|
86 |
+
history = model.fit(
|
87 |
+
train_generator,
|
88 |
+
epochs=EPOCHS,
|
89 |
+
validation_data=validation_generator
|
90 |
+
)
|
91 |
+
|
92 |
+
return model
|
93 |
+
|
94 |
+
if __name__ == "__main__":
|
95 |
+
script_directory = os.path.dirname(os.path.abspath(__file__))
|
96 |
+
dataset_path = os.path.join(script_directory, '../../data')
|
97 |
+
trained_model = train_model(dataset_path)
|
98 |
+
trained_model.save(os.path.join(script_directory, '../../models/football_logo_model.h5'))
|