arif-ariff
commited on
Commit
•
55da669
1
Parent(s):
e370dc4
Finalized training parameters
Browse files- neural_models.py +20 -17
neural_models.py
CHANGED
@@ -44,21 +44,22 @@ def compute_metrics(eval_pred):
|
|
44 |
|
45 |
|
46 |
# load dataset
|
47 |
-
|
48 |
# Split into train/test sets
|
49 |
-
|
50 |
# an example
|
51 |
-
print(
|
52 |
|
53 |
# Map label names to an integer and vice-versa
|
54 |
-
labels =
|
55 |
label2id, id2label = dict(), dict()
|
56 |
for i, label in enumerate(labels):
|
57 |
label2id[label] = str(i)
|
58 |
id2label[str(i)] = label
|
59 |
|
60 |
# Should convert label id into a name
|
61 |
-
print(
|
|
|
62 |
|
63 |
# Pre-processing with ViT
|
64 |
# Load image processor to process image into tensor
|
@@ -88,8 +89,8 @@ val_data_augmentation = keras.Sequential(
|
|
88 |
name="val_data_augmentation",
|
89 |
)
|
90 |
|
91 |
-
|
92 |
-
|
93 |
|
94 |
data_collator = DefaultDataCollator(return_tensors="tf")
|
95 |
|
@@ -97,8 +98,8 @@ accuracy = evaluate.load("accuracy")
|
|
97 |
|
98 |
# Set hyperparameters
|
99 |
batch_size = 16
|
100 |
-
num_epochs =
|
101 |
-
num_train_steps = len(
|
102 |
learning_rate = 3e-5
|
103 |
weight_decay_rate = 0.01
|
104 |
|
@@ -118,11 +119,11 @@ model = TFAutoModelForImageClassification.from_pretrained(
|
|
118 |
)
|
119 |
|
120 |
# converting datasets to tf.data.Dataset
|
121 |
-
tf_train_dataset =
|
122 |
columns="pixel_values", label_cols="label", shuffle=True, batch_size=batch_size, collate_fn=data_collator
|
123 |
)
|
124 |
|
125 |
-
tf_eval_dataset =
|
126 |
columns="pixel_values", label_cols="label", shuffle=True, batch_size=batch_size, collate_fn=data_collator
|
127 |
)
|
128 |
|
@@ -133,17 +134,19 @@ model.compile(optimizer=optimizer, loss=loss)
|
|
133 |
|
134 |
metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_eval_dataset)
|
135 |
push_to_hub_callback = PushToHubCallback(
|
136 |
-
output_dir="../
|
137 |
tokenizer=image_processor,
|
138 |
save_strategy="no",
|
139 |
)
|
140 |
|
141 |
callbacks = [metric_callback, push_to_hub_callback]
|
142 |
|
143 |
-
model.fit(tf_train_dataset, validation_data=tf_eval_dataset, epochs=num_epochs
|
144 |
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
|
|
149 |
|
|
|
44 |
|
45 |
|
46 |
# load dataset
|
47 |
+
fashion = load_dataset("fashion_mnist", split="train[:4000]")
|
48 |
# Split into train/test sets
|
49 |
+
fashion = fashion.train_test_split(test_size=0.2)
|
50 |
# an example
|
51 |
+
print(fashion["train"][0])
|
52 |
|
53 |
# Map label names to an integer and vice-versa
|
54 |
+
labels = fashion["train"].features["label"].names
|
55 |
label2id, id2label = dict(), dict()
|
56 |
for i, label in enumerate(labels):
|
57 |
label2id[label] = str(i)
|
58 |
id2label[str(i)] = label
|
59 |
|
60 |
# Should convert label id into a name
|
61 |
+
# print(label2id)
|
62 |
+
# print(id2label)
|
63 |
|
64 |
# Pre-processing with ViT
|
65 |
# Load image processor to process image into tensor
|
|
|
89 |
name="val_data_augmentation",
|
90 |
)
|
91 |
|
92 |
+
fashion["train"].set_transform(preprocess_train)
|
93 |
+
fashion["test"].set_transform(preprocess_val)
|
94 |
|
95 |
data_collator = DefaultDataCollator(return_tensors="tf")
|
96 |
|
|
|
98 |
|
99 |
# Set hyperparameters
|
100 |
batch_size = 16
|
101 |
+
num_epochs = 4
|
102 |
+
num_train_steps = len(fashion["train"]) * num_epochs
|
103 |
learning_rate = 3e-5
|
104 |
weight_decay_rate = 0.01
|
105 |
|
|
|
119 |
)
|
120 |
|
121 |
# converting datasets to tf.data.Dataset
|
122 |
+
tf_train_dataset = fashion["train"].to_tf_dataset(
|
123 |
columns="pixel_values", label_cols="label", shuffle=True, batch_size=batch_size, collate_fn=data_collator
|
124 |
)
|
125 |
|
126 |
+
tf_eval_dataset = fashion["test"].to_tf_dataset(
|
127 |
columns="pixel_values", label_cols="label", shuffle=True, batch_size=batch_size, collate_fn=data_collator
|
128 |
)
|
129 |
|
|
|
134 |
|
135 |
metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_eval_dataset)
|
136 |
push_to_hub_callback = PushToHubCallback(
|
137 |
+
output_dir="../fashion_classifier",
|
138 |
tokenizer=image_processor,
|
139 |
save_strategy="no",
|
140 |
)
|
141 |
|
142 |
callbacks = [metric_callback, push_to_hub_callback]
|
143 |
|
144 |
+
model.fit(tf_train_dataset, validation_data=tf_eval_dataset, epochs=num_epochs, callbacks=callbacks)
|
145 |
|
146 |
+
# model.push_to_hub()
|
147 |
+
|
148 |
+
# ds = load_dataset("fashion_mnist", split="test[:10]")
|
149 |
+
# image = ds["image"][0]
|
150 |
+
# classifier = pipeline("image-classification", model="my_awesome_fashion_model")
|
151 |
+
# print(classifier(image))
|
152 |
|