def train(self): train_data = ak.image_dataset_from_directory(self.data_path) self.model = self._create_model() logger.info(f"executing a {self.model.__class__.__name__} algorithm...") logger.info(f"Training started...") self.model.fit(train_data, **self.training_args) logger.info("finished training!") self.save_desc_file() saved = self.save_model() if saved: logger.info(f"model saved successfully")
import autokeras as ak import tensorflow as tf import os from pathlib import Path HERE = Path(__file__).parent.absolute() train_data = ak.image_dataset_from_directory(HERE.joinpath( 'traindata', 'digits'), image_size=(120, 120), batch_size=64) clf = ak.ImageClassifier(overwrite=True, max_trials=1, project_name='digitsTrainer') clf.fit(train_data, epochs=5) print(clf.evaluate(train_data)) clf.export_model().save("digits", save_format="tf") # TODO: use tesseract to verify images for traindata and train on many more digits
sunflowers/ tulips/ ``` We can split the data into training and testing as we load them. """ batch_size = 32 img_height = 180 img_width = 180 train_data = ak.image_dataset_from_directory( data_dir, # Use 20% data as testing data. validation_split=0.2, subset="training", # Set seed to ensure the same split when loading testing data. seed=123, image_size=(img_height, img_width), batch_size=batch_size, ) test_data = ak.image_dataset_from_directory( data_dir, validation_split=0.2, subset="validation", seed=123, image_size=(img_height, img_width), batch_size=batch_size, ) """ Then we just do one quick demo of AutoKeras to make sure the dataset works.
def predict(self): trained_model = self.load_model() pred_data = ak.image_dataset_from_directory(self.data_path) trained_model.predict(pred_data)
def evaluate(self): trained_model = self.load_model() test_data = ak.image_dataset_from_directory(self.data_path) trained_model.evaluate(test_data)