Datasets:
Tasks:
Image Classification
Modalities:
Image
Formats:
imagefolder
Languages:
English
Size:
1K - 10K
Files used to process dataset
Browse files- fo2hub.py +9 -0
- pokemon2fo.py +46 -0
- train_resnet_pokemon.py +82 -0
fo2hub.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import fiftyone as fo
|
| 4 |
+
from fiftyone.utils.huggingface import push_to_hub
|
| 5 |
+
import huggingface_hub
|
| 6 |
+
huggingface_hub.login("XXXXXXXXXXXXXX")
|
| 7 |
+
pokemon = fo.load_dataset("pokemon")
|
| 8 |
+
|
| 9 |
+
push_to_hub(pokemon, "pokemon", chunk_size=500, exist_ok=True)
|
pokemon2fo.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fiftyone as fo
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
# Downloaded the images from hugging face using git clone
|
| 5 |
+
# git clone https://huggingface.co/datasets/fcakyon/pokemon-classification
|
| 6 |
+
POKEMON_DIR = "/home/spousty/data/pokemon-classification/data"
|
| 7 |
+
|
| 8 |
+
# There is a subdir for test, train, valid
|
| 9 |
+
train_pokemon = fo.Dataset.from_dir(
|
| 10 |
+
dataset_dir=POKEMON_DIR + "/train",
|
| 11 |
+
progress=True,
|
| 12 |
+
dataset_type=fo.types.ImageClassificationDirectoryTree,
|
| 13 |
+
name="train_pokemon",
|
| 14 |
+
overwrite=True,
|
| 15 |
+
persistent=True
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
test_pokemon = fo.Dataset.from_dir(
|
| 19 |
+
dataset_dir=POKEMON_DIR + "/test",
|
| 20 |
+
progress=True,
|
| 21 |
+
dataset_type=fo.types.ImageClassificationDirectoryTree,
|
| 22 |
+
name="test_pokemon",
|
| 23 |
+
overwrite=True,
|
| 24 |
+
persistent=True
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
valid_pokemon = fo.Dataset.from_dir(
|
| 28 |
+
dataset_dir=POKEMON_DIR + "/valid",
|
| 29 |
+
progress=True,
|
| 30 |
+
dataset_type=fo.types.ImageClassificationDirectoryTree,
|
| 31 |
+
name="valid_pokemon",
|
| 32 |
+
overwrite=True,
|
| 33 |
+
persistent=True
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
train_pokemon.tag_samples("train")
|
| 37 |
+
test_pokemon.tag_samples("test")
|
| 38 |
+
valid_pokemon.tag_samples("valid")
|
| 39 |
+
|
| 40 |
+
pokemon = train_pokemon.clone(name="pokemon", persistent=True)
|
| 41 |
+
pokemon.merge_samples(test_pokemon)
|
| 42 |
+
pokemon.merge_samples(valid_pokemon)
|
| 43 |
+
pokemon.save()
|
| 44 |
+
|
| 45 |
+
session = fo.launch_app(pokemon)
|
| 46 |
+
session.wait()
|
train_resnet_pokemon.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision.transforms.v2 as T
|
| 3 |
+
|
| 4 |
+
import fiftyone as fo
|
| 5 |
+
from fiftyone import ViewField as F
|
| 6 |
+
import fiftyone.train as fot
|
| 7 |
+
|
| 8 |
+
def main():
|
| 9 |
+
|
| 10 |
+
dataset = fo.load_dataset("pokemon")
|
| 11 |
+
|
| 12 |
+
dataset = dataset.match_tags("train")
|
| 13 |
+
|
| 14 |
+
model_config = fot.model.classification.TorchvisionResnetModelConfig(
|
| 15 |
+
get_item_class_name='ImageClassificationGetItem',
|
| 16 |
+
get_item_inference_class_name='ImageClassificationInferenceGetItem',
|
| 17 |
+
train_transforms_list='standard',
|
| 18 |
+
inference_transforms_list='standard',
|
| 19 |
+
classes=dataset.distinct('ground_truth.label'),
|
| 20 |
+
weights=None
|
| 21 |
+
)
|
| 22 |
+
train_run_config = fot.run.TrainConfig(
|
| 23 |
+
model_config,
|
| 24 |
+
data={
|
| 25 |
+
'dataloader': {
|
| 26 |
+
'batch_size': 128,
|
| 27 |
+
}
|
| 28 |
+
},
|
| 29 |
+
train_regime = {
|
| 30 |
+
"num_steps": 3800, # 100 epochs × 38 steps/epoch
|
| 31 |
+
"validate_every": 38, # Validate every epoch
|
| 32 |
+
"early_stop_after": 456, # 6 epochs without improvement
|
| 33 |
+
"grad_clip": 15,
|
| 34 |
+
},
|
| 35 |
+
optimizer = {
|
| 36 |
+
"name": "SGD",
|
| 37 |
+
"kwargs": {
|
| 38 |
+
"lr": 1e-2, # Lower initial learning rate
|
| 39 |
+
"momentum": 0.9,
|
| 40 |
+
"weight_decay": 1e-3,
|
| 41 |
+
},
|
| 42 |
+
"scheduler": {
|
| 43 |
+
"name": "CosineAnnealingLR",
|
| 44 |
+
"kwargs": {
|
| 45 |
+
"T_max": 3800, # Total steps
|
| 46 |
+
"eta_min": 1e-6,
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
}
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
incidentals={
|
| 53 |
+
"logging" : {
|
| 54 |
+
"wandb" : {
|
| 55 |
+
'project':'resnet-pokemon'
|
| 56 |
+
},
|
| 57 |
+
"log_every" : 6,
|
| 58 |
+
},
|
| 59 |
+
# "gi_kwargs" : {
|
| 60 |
+
# "field_mapping" : {
|
| 61 |
+
# "labels_field" : "ground_truth.label"
|
| 62 |
+
# }
|
| 63 |
+
# }
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
with fot.context(embeddings_model="resnet18-imagenet-torch",
|
| 67 |
+
embeddings_field='embed-resnet18-imagenet',
|
| 68 |
+
checkpoint_dir="./.checkpoints"):
|
| 69 |
+
print(fot.context)
|
| 70 |
+
print(fot.context.checkpoint_dir)
|
| 71 |
+
results = fot.run.train(
|
| 72 |
+
samples=dataset,
|
| 73 |
+
config=train_run_config,
|
| 74 |
+
key=f"resnet18_classifier",
|
| 75 |
+
incidentals=incidentals
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
return results
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
main()
|