Skip to content

Commit

Permalink
tiny image classification example added
Browse files Browse the repository at this point in the history
  • Loading branch information
Taniya-Das committed Nov 6, 2024
1 parent 5e6bf4f commit fd71450
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/Examples/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Examples

This folder contains examples of how to use the `openml-tensorflow` extension for different datasets and models.
87 changes: 87 additions & 0 deletions docs/Examples/tf_image_classification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""
Tensorflow image classification model example
==================
An example of a tensorflow network that classifies meta album images.
"""

import openml
import openml_tensorflow
from tensorflow.keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
from tensorflow.keras import layers, models

############################################################################
openml.config.apikey = 'KEY' # Paste your API key here

############################################################################

openml_tensorflow.config.epoch = 1 # small epoch for test runs

datagen = ImageDataGenerator()
openml_tensorflow.config.datagen = datagen
openml_tensorflow.config.dir = openml.config.get_cache_directory()+'/datasets/44312/PNU_Micro/images/'
openml_tensorflow.config.x_col = "FILE_NAME"
openml_tensorflow.config.y_col = 'encoded_labels'
openml_tensorflow.config.datagen = datagen
openml_tensorflow.config.batch_size = 32
openml_tensorflow.config.class_mode = "categorical"

# Perform cross-validation during traning
openml_tensorflow.config.perform_validation = True
openml_tensorflow.config.validation_split = 0.1
openml_tensorflow.config.datagen_valid = ImageDataGenerator()

IMG_SIZE = (128, 128)
IMG_SHAPE = IMG_SIZE + (3,)

# Example tensorflow image classification model.
model = models.Sequential()
model.add(layers.Conv2D(128, (3, 3), activation='relu', input_shape=IMG_SHAPE))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(84, activation='relu'))
model.add(layers.Dense(19, activation='softmax')) # Adjust output size
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['AUC'])

############################################################################

# Download the OpenML task for the Meta_Album_PNU_Micro dataset.
task = openml.tasks.get_task(362071)

# Run the Keras model on the task (requires an API key).
run = openml.runs.run_model_on_task(model, task, avoid_duplicate_runs=False)

"""
If you want to publish the run with the onnx file,
then you must call openml_tensorflow.add_onnx_to_run() immediately before run.publish().
When you publish, onnx file of last trained model is uploaded.
Careful to not call this function when another run_model_on_task is called in between,
as during publish later, only the last trained model (from last run_model_on_task call) is uploaded.
"""
run = openml_tensorflow.add_onnx_to_run(run)

run.publish()

print('URL for run: %s/run/%d?api_key=%s' % (openml.config.server, run.run_id, openml.config.apikey))

############################################################################
# Optional: Visualize model in netron

from urllib.request import urlretrieve

published_run = openml.runs.get_run(run.run_id)
url = 'https://api.openml.org/data/download/{}/model.onnx'.format(published_run.output_files['onnx_model'])

file_path, _ = urlretrieve(url, 'model.onnx')

import netron
# Visualize the ONNX model using Netron
netron.start(file_path)

3 changes: 2 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ nav:
- "OpenML Integration": "API reference/OpenML integration.md"
- Docker Reference:
- "Docker": "Docker reference/Docker.md"
# - Examples:
- Examples:
- "Image Classification Task": "Examples/tf_image_classification.py"
# - "Limitations of the API": "Limitations of the API.md"

0 comments on commit fd71450

Please sign in to comment.