arif-ariff
commited on
Commit
·
e370dc4
1
Parent(s):
184c8e0
tutorial code for food dataset
Browse files- .idea/.gitignore +8 -0
- .idea/assignment-7-image-classifier.iml +12 -0
- .idea/inspectionProfiles/Project_Default.xml +12 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/misc.xml +4 -0
- .idea/modules.xml +8 -0
- .idea/vcs.xml +6 -0
- neural_models.py +149 -0
.idea/.gitignore
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
| 4 |
+
# Editor-based HTTP Client requests
|
| 5 |
+
/httpRequests/
|
| 6 |
+
# Datasource local storage ignored files
|
| 7 |
+
/dataSources/
|
| 8 |
+
/dataSources.local.xml
|
.idea/assignment-7-image-classifier.iml
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$" />
|
| 5 |
+
<orderEntry type="jdk" jdkName="Python 3.9 (venv)" jdkType="Python SDK" />
|
| 6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 7 |
+
</component>
|
| 8 |
+
<component name="PyDocumentationSettings">
|
| 9 |
+
<option name="format" value="PLAIN" />
|
| 10 |
+
<option name="myDocStringFormat" value="Plain" />
|
| 11 |
+
</component>
|
| 12 |
+
</module>
|
.idea/inspectionProfiles/Project_Default.xml
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<profile version="1.0">
|
| 3 |
+
<option name="myName" value="Project Default" />
|
| 4 |
+
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
| 5 |
+
<option name="ignoredErrors">
|
| 6 |
+
<list>
|
| 7 |
+
<option value="N803" />
|
| 8 |
+
</list>
|
| 9 |
+
</option>
|
| 10 |
+
</inspection_tool>
|
| 11 |
+
</profile>
|
| 12 |
+
</component>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<settings>
|
| 3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
| 4 |
+
<version value="1.0" />
|
| 5 |
+
</settings>
|
| 6 |
+
</component>
|
.idea/misc.xml
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (venv)" project-jdk-type="Python SDK" />
|
| 4 |
+
</project>
|
.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectModuleManager">
|
| 4 |
+
<modules>
|
| 5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/assignment-7-image-classifier.iml" filepath="$PROJECT_DIR$/.idea/assignment-7-image-classifier.iml" />
|
| 6 |
+
</modules>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
.idea/vcs.xml
ADDED
|
@@ -0,0 +1,6 @@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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import load_dataset
|
| 2 |
+
from transformers import AutoImageProcessor, create_optimizer, TFAutoModelForImageClassification, KerasMetricCallback, \
|
| 3 |
+
PushToHubCallback, pipeline
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from tensorflow.python import keras
|
| 6 |
+
from keras import layers, losses
|
| 7 |
+
import numpy as np
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from transformers import DefaultDataCollator
|
| 10 |
+
import evaluate
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def convert_to_tf_tensor(image: Image):
|
| 14 |
+
np_image = np.array(image)
|
| 15 |
+
tf_image = tf.convert_to_tensor(np_image)
|
| 16 |
+
|
| 17 |
+
# `expand_dims()` is used to add a batch dimension since
|
| 18 |
+
# the TF augmentation layers operates on batched inputs.
|
| 19 |
+
return tf.expand_dims(tf_image, 0)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def preprocess_train(example_batch):
|
| 23 |
+
"""Apply train_transforms across a batch."""
|
| 24 |
+
images = [
|
| 25 |
+
train_data_augmentation(convert_to_tf_tensor(image.convert("RGB"))) for image in example_batch["image"]
|
| 26 |
+
]
|
| 27 |
+
example_batch["pixel_values"] = [tf.transpose(tf.squeeze(image)) for image in images]
|
| 28 |
+
return example_batch
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def preprocess_val(example_batch):
|
| 32 |
+
"""Apply val_transforms across a batch."""
|
| 33 |
+
images = [
|
| 34 |
+
val_data_augmentation(convert_to_tf_tensor(image.convert("RGB"))) for image in example_batch["image"]
|
| 35 |
+
]
|
| 36 |
+
example_batch["pixel_values"] = [tf.transpose(tf.squeeze(image)) for image in images]
|
| 37 |
+
return example_batch
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def compute_metrics(eval_pred):
|
| 41 |
+
predictions, labels = eval_pred
|
| 42 |
+
predictions = np.argmax(predictions, axis=1)
|
| 43 |
+
return accuracy.compute(predictions=predictions, references=labels)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# load dataset
|
| 47 |
+
food = load_dataset("food101", split="train[:5000]")
|
| 48 |
+
# Split into train/test sets
|
| 49 |
+
food = food.train_test_split(test_size=0.2)
|
| 50 |
+
# an example
|
| 51 |
+
print(food["train"][0])
|
| 52 |
+
|
| 53 |
+
# Map label names to an integer and vice-versa
|
| 54 |
+
labels = food["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(id2label[str(79)])
|
| 62 |
+
|
| 63 |
+
# Pre-processing with ViT
|
| 64 |
+
# Load image processor to process image into tensor
|
| 65 |
+
checkpoint = "google/vit-base-patch16-224-in21k"
|
| 66 |
+
image_processor = AutoImageProcessor.from_pretrained(checkpoint)
|
| 67 |
+
|
| 68 |
+
# To avoid overfitting and make the model more robust, add data augmentation to the training set.
|
| 69 |
+
# User Keras preprocessing layers to define transformations for the training set.
|
| 70 |
+
size = (image_processor.size["height"], image_processor.size["width"])
|
| 71 |
+
|
| 72 |
+
train_data_augmentation = keras.Sequential(
|
| 73 |
+
[
|
| 74 |
+
layers.RandomCrop(size[0], size[1]),
|
| 75 |
+
layers.Rescaling(scale=1.0 / 127.5, offset=-1),
|
| 76 |
+
layers.RandomFlip("horizontal"),
|
| 77 |
+
layers.RandomRotation(factor=0.02),
|
| 78 |
+
layers.RandomZoom(height_factor=0.2, width_factor=0.2),
|
| 79 |
+
],
|
| 80 |
+
name="train_data_augmentation",
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
val_data_augmentation = keras.Sequential(
|
| 84 |
+
[
|
| 85 |
+
layers.CenterCrop(size[0], size[1]),
|
| 86 |
+
layers.Rescaling(scale=1.0 / 127.5, offset=-1),
|
| 87 |
+
],
|
| 88 |
+
name="val_data_augmentation",
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
food["train"].set_transform(preprocess_train)
|
| 92 |
+
food["test"].set_transform(preprocess_val)
|
| 93 |
+
|
| 94 |
+
data_collator = DefaultDataCollator(return_tensors="tf")
|
| 95 |
+
|
| 96 |
+
accuracy = evaluate.load("accuracy")
|
| 97 |
+
|
| 98 |
+
# Set hyperparameters
|
| 99 |
+
batch_size = 16
|
| 100 |
+
num_epochs = 5
|
| 101 |
+
num_train_steps = len(food["train"]) * num_epochs
|
| 102 |
+
learning_rate = 3e-5
|
| 103 |
+
weight_decay_rate = 0.01
|
| 104 |
+
|
| 105 |
+
# define optimizer, learning rate schedule
|
| 106 |
+
optimizer, lr_schedule = create_optimizer(
|
| 107 |
+
init_lr=learning_rate,
|
| 108 |
+
num_train_steps=num_train_steps,
|
| 109 |
+
weight_decay_rate=weight_decay_rate,
|
| 110 |
+
num_warmup_steps=0,
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# Load ViT along with label mappings
|
| 114 |
+
model = TFAutoModelForImageClassification.from_pretrained(
|
| 115 |
+
checkpoint,
|
| 116 |
+
id2label=id2label,
|
| 117 |
+
label2id=label2id,
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
# converting datasets to tf.data.Dataset
|
| 121 |
+
tf_train_dataset = food["train"].to_tf_dataset(
|
| 122 |
+
columns="pixel_values", label_cols="label", shuffle=True, batch_size=batch_size, collate_fn=data_collator
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
tf_eval_dataset = food["test"].to_tf_dataset(
|
| 126 |
+
columns="pixel_values", label_cols="label", shuffle=True, batch_size=batch_size, collate_fn=data_collator
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# Configure model for training
|
| 130 |
+
loss = losses.SparseCategoricalCrossentropy(from_logits=True)
|
| 131 |
+
model.compile(optimizer=optimizer, loss=loss)
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
metric_callback = KerasMetricCallback(metric_fn=compute_metrics, eval_dataset=tf_eval_dataset)
|
| 135 |
+
push_to_hub_callback = PushToHubCallback(
|
| 136 |
+
output_dir="../food_classifier",
|
| 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) #, callback=callbacks)
|
| 144 |
+
|
| 145 |
+
ds = load_dataset("food101", split="validation[:10]")
|
| 146 |
+
image = ds["image"][0]
|
| 147 |
+
classifier = pipeline("image-classification", model="my_awesome_food_model")
|
| 148 |
+
print(classifier(image))
|
| 149 |
+
|