def train(trial: Trial): context = Optuna.get_optuna_conext('minist_optuna', trial) print("New trial ", trial.number, "++++++++++++++++++++++++++++", context) ENABLE_SUSPEND_RESUME_TRAINING() print(context) Optuna.suggest_float(name='lr', low=1e-6, high=1e-2, log=True) train, train_len = Mnist.get_train_dataset() validation, validation_len = Mnist.get_test_dataset() train = train.map(ImageDatasetUtil.image_reguralization()).map( ImageDatasetUtil.one_hot(CLASS_NUM)) validation = validation.map(ImageDatasetUtil.image_reguralization()).map( ImageDatasetUtil.one_hot(CLASS_NUM)) optimizer = OptimizerBuilder.get_optimizer(name="rmsprop", lr=Optuna.get_value( 'lr', default=0.1)) model = SimpleClassificationModel.get_model(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 1), classes=CLASS_NUM) callbacks = CallbackBuilder.get_callbacks(tensorboard=True, reduce_lr_on_plateau=True, reduce_patience=5, reduce_factor=0.25, early_stopping_patience=16) history = TrainingExecutor.train_classification( train_data=train, train_size=train_len, batch_size=BATCH_SIZE, validation_data=validation, validation_size=validation_len, shuffle_size=SHUFFLE_SIZE, model=model, callbacks=callbacks, optimizer=optimizer, loss="categorical_crossentropy", max_epoch=EPOCHS) return history.history['val_loss'][-1]
if __name__ == '__main__': context = Context.init_context( TRAINING_NAME="20200519141141") # .TRAINING_NAME:}) ENABLE_SUSPEND_RESUME_TRAIN() BATCH_SIZE = 500 CLASS_NUM = 10 IMAGE_SIZE = 28 EPOCHS = 20 SHUFFLE_SIZE = 1000 train, train_len = Mnist.get_train_dataset() validation, validation_len = Mnist.get_test_dataset() train = train.map(ImageDatasetUtil.image_reguralization()).map( ImageDatasetUtil.one_hot(CLASS_NUM)) validation = validation.map(ImageDatasetUtil.image_reguralization()).map( ImageDatasetUtil.one_hot(CLASS_NUM)) optimizer = OptimizerBuilder.get_optimizer(name="rmsprop") model = SimpleClassificationModel.get_model(input_shape=(IMAGE_SIZE, IMAGE_SIZE, 1), classes=CLASS_NUM) callbacks = CallbackBuilder.get_callbacks(tensorboard=False, reduce_lr_on_plateau=True, reduce_patience=3, reduce_factor=0.25, early_stopping_patience=5) ImageTrain.train_image_classification(train_data=train, train_size=train_len, batch_size=BATCH_SIZE, validation_data=validation,
context = Context.init_context( TRAINING_BASE_DIR="tmp", TRAINING_NAME="resume_test" ) ENABLE_SUSPEND_RESUME_TRAIN() BATCH_SIZE = 500 CLASS_NUM = 10 IMAGE_SIZE = 28 EPOCHS = 3 SHUFFLE_SIZE = 1000 BASEDIR = "./tmp" TRAIN_NAME = "resume_test" # if IS_SUSPEND_RESUME_TRAIN() == True and IS_ON_COLABOLATORY_WITH_GOOGLE_DRIVE()== True: train, train_len = Mnist.get_train_dataset() validation, validation_len = Mnist.get_test_dataset() train = train.map(ImageDatasetUtil.image_reguralization()).map(ImageDatasetUtil.one_hot(CLASS_NUM)) validation = validation.map(ImageDatasetUtil.image_reguralization()).map(ImageDatasetUtil.one_hot(CLASS_NUM)) optimizer = OptimizerBuilder.get_optimizer(name="rmsprop") model = SimpleClassificationModel.get_model(input_shape=(IMAGE_SIZE,IMAGE_SIZE,1),classes=CLASS_NUM) callbacks = CallbackBuilder.get_callbacks(tensorboard=False, reduce_lr_on_plateau=True,reduce_patience=5,reduce_factor=0.25,early_stopping_patience=16) ImageTrain.train_image_classification(train_data=train,train_size=train_len,batch_size=BATCH_SIZE,validation_data=validation,validation_size=validation_len,shuffle_size=SHUFFLE_SIZE,model=model,callbacks=callbacks,optimizer=optimizer,loss="categorical_crossentropy",max_epoch=EPOCHS)
BATCH_SIZE = 10 # BATCH_SIZE = 48 CLASS_NUM = 2 IMAGE_SIZE = 150 EPOCHS = 100 train, train_len = ImageLabelFolderDataset.get_train_dataset(name="dogs-vs-cats", manual_dir="tmp") validation, validation_len = ImageLabelFolderDataset.get_validation_dataset(name="dogs-vs-cats", manual_dir="tmp") # dataset,dataset_len = CatsVsDogs.get_train_dataset() # dataset = dataset.map(ImageDatasetUtil.resize_with_crop_or_pad(IMAGE_SIZE,IMAGE_SIZE)) # (train, train_len), (validation, validation_len) = ImageDatasetUtil.devide_train_validation(dataset,dataset_len,0.90) train = train.map(ImageDatasetUtil.resize_with_crop_or_pad(IMAGE_SIZE,IMAGE_SIZE),num_parallel_calls=tf.data.experimental.AUTOTUNE).map(ImageAugument.randaugment_map(1,2)) train = train.map(ImageDatasetUtil.image_reguralization(),num_parallel_calls=tf.data.experimental.AUTOTUNE).map(ImageDatasetUtil.one_hot(CLASS_NUM),num_parallel_calls=tf.data.experimental.AUTOTUNE).apply(ImageAugument.mixup_apply(200,0.1)) validation = validation.map(ImageDatasetUtil.resize_with_crop_or_pad(IMAGE_SIZE,IMAGE_SIZE),num_parallel_calls=tf.data.experimental.AUTOTUNE).map(ImageDatasetUtil.image_reguralization(),num_parallel_calls=tf.data.experimental.AUTOTUNE).map(ImageDatasetUtil.one_hot(CLASS_NUM),num_parallel_calls=tf.data.experimental.AUTOTUNE) optimizer = OptimizerBuilder.get_optimizer(name="rmsprop") model = ResNetD18.get_model(input_shape=(IMAGE_SIZE,IMAGE_SIZE,3),classes=CLASS_NUM,resnest=True,resnet_c=True,resnet_d=True,mish=True) # model = SimpleClassificationModel.get_model(input_shape=(IMAGE_SIZE,IMAGE_SIZE,3),classes=CLASS_NUM) callbacks = CallbackBuilder.get_callbacks(base_dir = "tmp" , tensorboard=True, save_weights=True, consine_annealing=False, reduce_lr_on_plateau=True,reduce_patience=5,reduce_factor=0.25,early_stopping_patience=8) Trainer.train_classification(train_data=train,train_size=train_len,batch_size=BATCH_SIZE,validation_data=validation,validation_size=validation_len,shuffle_size=1000,model=model,callbacks=callbacks,optimizer=optimizer,loss="binary_crossentropy",max_epoch=EPOCHS) """ train, train_len = RockPaperScissors.get_train_dataset() validation, validation_len = Place365Small.get_validation_dataset() train = train.map(ImageDatasetUtil.resize_with_crop_or_pad(IMAGE_SIZE,IMAGE_SIZE)) validation = validation.map(ImageDatasetUtil.resize_with_crop_or_pad(IMAGE_SIZE,IMAGE_SIZE)) dataset, len = MVTecAd.get_train_dataset(type="bottle") print(dataset)
tftk.ENABLE_MIXED_PRECISION() context = Context.init_context(TRAINING_NAME='DogsVsCats') train, train_len = ImageLabelFolderDataset.get_train_dataset( name="dogs-vs-cats", manual_dir="tmp") validation, validation_len = ImageLabelFolderDataset.get_validation_dataset( name="dogs-vs-cats", manual_dir="tmp") train = train.map(ImageDatasetUtil.map_max_square_crop_and_resize( IMAGE_SIZE, IMAGE_SIZE), num_parallel_calls=tf.data.experimental.AUTOTUNE) train = train.map(ImageAugument.randaugment_map(2, 4), num_parallel_calls=tf.data.experimental.AUTOTUNE) train = train.map(ImageDatasetUtil.image_reguralization(), num_parallel_calls=tf.data.experimental.AUTOTUNE) train = train.map(ImageDatasetUtil.one_hot(CLASS_NUM), num_parallel_calls=tf.data.experimental.AUTOTUNE) validation = validation.map( ImageDatasetUtil.map_max_square_crop_and_resize( IMAGE_SIZE, IMAGE_SIZE), num_parallel_calls=tf.data.experimental.AUTOTUNE) validation = validation.map( ImageDatasetUtil.image_reguralization(), num_parallel_calls=tf.data.experimental.AUTOTUNE) validation = validation.map( ImageDatasetUtil.one_hot(CLASS_NUM), num_parallel_calls=tf.data.experimental.AUTOTUNE) optimizer = OptimizerBuilder.get_optimizer(name="rmsprop") model = KerasResNet18.get_model(input_shape=(IMAGE_SIZE, IMAGE_SIZE,
# os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' import tftk from tftk.image.dataset import Mnist from tftk.image.dataset import ImageDatasetUtil from tftk.image.augument import ImageAugument from tftk.image.dataset import ImageLabelFolderDataset if __name__ == '__main__': BATCH_SIZE = 100 CLASS_NUM = 10 IMAGE_SIZE = 28 EPOCHS = 2 SHUFFLE_SIZE = 1000 train, train_len = ImageLabelFolderDataset.get_train_dataset(name="dogs-vs-cats", manual_dir="tmp") train = train.map(ImageDatasetUtil.resize_with_crop_or_pad(224,224)).map(ImageDatasetUtil.one_hot(2)) train = train.map(ImageDatasetUtil.image_reguralization()).apply(ImageAugument.mixup_apply(mixup_size=100, alpha=0.2)) for d in train.take(1): image = d["image"] * 255 image = tf.cast(image, tf.uint8) print(image) image = image.numpy() y = d["label"] print("y",d["label"].numpy()) im = Image.fromarray(image) im.show()