add training script and prediction
Browse files- README.md +33 -7
- config.json +5 -1
- pipeline.py +31 -0
- requirements.txt +5 -0
- training/dragon.ipynb +0 -0
README.md
CHANGED
@@ -1,25 +1,42 @@
|
|
1 |
---
|
|
|
2 |
library_name: keras
|
3 |
tags:
|
4 |
- dragon-detection
|
5 |
- Keras
|
6 |
- dragon
|
7 |
-
- image
|
8 |
---
|
9 |
|
10 |
-
##
|
|
|
|
|
11 |
|
12 |
-
|
|
|
|
|
13 |
|
14 |
-
##
|
15 |
|
16 |
-
|
17 |
|
18 |
-
|
|
|
19 |
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
## Training procedure
|
|
|
23 |
|
24 |
### Training hyperparameters
|
25 |
|
@@ -44,3 +61,12 @@ The following hyperparameters were used during training:
|
|
44 |
| amsgrad | False |
|
45 |
| training_precision | float32 |
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
license: mit
|
3 |
library_name: keras
|
4 |
tags:
|
5 |
- dragon-detection
|
6 |
- Keras
|
7 |
- dragon
|
8 |
+
- image-classification
|
9 |
---
|
10 |
|
11 |
+
## Dragon detector with Tensor Flow
|
12 |
+
This is a simple `tensorflow` model to detect dragon in images.
|
13 |
+
If you just want to test the trained model, make sure you have the following packages:
|
14 |
|
15 |
+
```
|
16 |
+
tensorflow keras sklearn-deap datasets transformers[torch] sentencepiece
|
17 |
+
```
|
18 |
|
19 |
+
## Predict
|
20 |
|
21 |
+
To run prediction you need to run below code:
|
22 |
|
23 |
+
```python
|
24 |
+
from huggingface_hub import from_pretrained_keras
|
25 |
|
26 |
+
model = from_pretrained_keras("hadilq/dragon-notdragon")
|
27 |
+
|
28 |
+
img = keras.preprocessing.image.load_img(filename, target_size=(224, 224))
|
29 |
+
x = keras.preprocessing.image.img_to_array(img)
|
30 |
+
x = np.expand_dims(x, axis=0)
|
31 |
+
x = keras.applications.vgg16.preprocess_input(x)
|
32 |
+
prediction = model.predict(x)
|
33 |
+
print("model:", filename, "dragon" if prediction[0][0] >= 0.99 else "notdragon")
|
34 |
+
```
|
35 |
+
|
36 |
+
Additionally, you can check https://replicate.com/hadilq/dragon-notdragon to play around.
|
37 |
|
38 |
## Training procedure
|
39 |
+
I trained it in Google colab, where you can find the original code in `training` directory.
|
40 |
|
41 |
### Training hyperparameters
|
42 |
|
|
|
61 |
| amsgrad | False |
|
62 |
| training_precision | float32 |
|
63 |
|
64 |
+
|
65 |
+
## Model Plot
|
66 |
+
|
67 |
+
<details>
|
68 |
+
<summary>View Model Plot</summary>
|
69 |
+
|
70 |
+
![Model Image](./model.png)
|
71 |
+
|
72 |
+
</details>
|
config.json
CHANGED
@@ -1,5 +1,9 @@
|
|
1 |
{
|
2 |
"name": "sequential_2",
|
|
|
|
|
|
|
|
|
3 |
"layers": [
|
4 |
{
|
5 |
"module": "keras.layers",
|
@@ -959,4 +963,4 @@
|
|
959 |
}
|
960 |
}
|
961 |
]
|
962 |
-
}
|
|
|
1 |
{
|
2 |
"name": "sequential_2",
|
3 |
+
"id2label": {
|
4 |
+
"0": "dragon",
|
5 |
+
"1": "not dragon"
|
6 |
+
},
|
7 |
"layers": [
|
8 |
{
|
9 |
"module": "keras.layers",
|
|
|
963 |
}
|
964 |
}
|
965 |
]
|
966 |
+
}
|
pipeline.py
ADDED
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Dict
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
import json
|
6 |
+
import tensorflow as tf
|
7 |
+
from tensorflow import keras
|
8 |
+
|
9 |
+
class PreTrainedPipeline():
|
10 |
+
def __init__(self, path=""):
|
11 |
+
self.model = keras.saving.load_model("./")
|
12 |
+
with open(os.path.join(path, "config.json")) as config:
|
13 |
+
config = json.load(config)
|
14 |
+
self.id2label = config["id2label"]
|
15 |
+
|
16 |
+
def __call__(self, inputs: "Image.Image")-> Dict[str, str]:
|
17 |
+
"""
|
18 |
+
Args:
|
19 |
+
inputs (:obj:`PIL.Image`):
|
20 |
+
The raw image representation as PIL.
|
21 |
+
No transformation made whatsoever from the input. Make all necessary transformations here.
|
22 |
+
Return:
|
23 |
+
A :obj:`list`:. The list contains items that are dicts should be liked {"label": "XXX", "score": 0.82}
|
24 |
+
It is preferred if the returned list is in decreasing `score` order
|
25 |
+
"""
|
26 |
+
img = keras.preprocessing.image.load_img(input, target_size=(224, 224))
|
27 |
+
x = keras.preprocessing.image.img_to_array(img)
|
28 |
+
x = np.expand_dims(x, axis=0)
|
29 |
+
x = keras.applications.vgg16.preprocess_input(x)
|
30 |
+
prediction = self.model.predict(x)
|
31 |
+
return { 'label': "detected", 'score': "dragon" if prediction[0][0] >= 0.99 else "not-dragon" }
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow==2.13.0
|
2 |
+
keras==2.13.1
|
3 |
+
sklearn-deap==0.3.0
|
4 |
+
pillow==10.3.0
|
5 |
+
|
training/dragon.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|