nyrasea commited on
Commit
ec072a7
·
verified ·
1 Parent(s): ccb506a

Delete train_model.py

Browse files
Files changed (1) hide show
  1. train_model.py +0 -57
train_model.py DELETED
@@ -1,57 +0,0 @@
1
- import tensorflow as tf
2
- import numpy as np
3
- from datasets import load_dataset
4
- from sklearn.preprocessing import LabelBinarizer
5
- from tensorflow.keras.applications import MobileNetV2
6
- from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
7
- from tensorflow.keras.models import Model
8
- from tensorflow.keras.optimizers import Adam
9
- from tqdm import tqdm
10
-
11
- # Load dataset dari Hugging Face
12
- from datasets import load_dataset
13
-
14
- ds = load_dataset("tanganke/stanford_cars")
15
- NUM_CLASSES = 196
16
- IMG_SIZE = 224
17
- BATCH_SIZE = 32
18
- EPOCHS = 5
19
-
20
- # Preprocessing fungsi
21
- def preprocess(example):
22
- image = example["image"].resize((IMG_SIZE, IMG_SIZE))
23
- image = np.array(image) / 255.0
24
- return image, example["label"]
25
-
26
- # Apply preprocessing
27
- X_train, y_train = zip(*[preprocess(x) for x in tqdm(ds["train"])])
28
- X_test, y_test = zip(*[preprocess(x) for x in tqdm(ds["test"])])
29
-
30
- X_train = np.array(X_train)
31
- X_test = np.array(X_test)
32
-
33
- # One-hot encoding label
34
- lb = LabelBinarizer()
35
- y_train = lb.fit_transform(y_train)
36
- y_test = lb.transform(y_test)
37
-
38
- # Load base model MobileNetV2
39
- base_model = MobileNetV2(include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3), weights='imagenet')
40
- x = GlobalAveragePooling2D()(base_model.output)
41
- x = Dense(256, activation='relu')(x)
42
- preds = Dense(NUM_CLASSES, activation='softmax')(x)
43
-
44
- model = Model(inputs=base_model.input, outputs=preds)
45
-
46
- # Freeze base layers
47
- for layer in base_model.layers:
48
- layer.trainable = False
49
-
50
- model.compile(optimizer=Adam(1e-4), loss='categorical_crossentropy', metrics=['accuracy'])
51
-
52
- # Training
53
- model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=EPOCHS, batch_size=BATCH_SIZE)
54
-
55
- # Simpan model
56
- model.save("car_model.h5")
57
- print("✅ Model saved to car_model.h5")