def run(model_id): """Run experiment.""" config = configs[model_id] logger.info('\n\n\ntrain model {}'.format(model_id)) # prepare data if config['preprocess_fn'] is not None: function = getattr(data_generator, config['preprocess_fn']) preprocess_fn = partial(function, **config['preprocess']) else: preprocess_fn = None generator = Generator(path=PATH_TRAIN, IDs=meta_train.index.tolist(), labels=meta_train[['target']], preprocessing_fn=preprocess_fn, shuffle=False, batch_size=64, **config['generator']) X, y = generate_train_data(generator, meta_train) logger.info('X shape: {}, y shape: {}'.format(X.shape, y.shape)) # define model model_function = getattr(models, config['model_name']) nn_model = partial(model_function, input_shape=(X.shape[1:]), **config['model_params']) nn_model().summary(print_fn=logger.info) model = KerasModel(nn_model, logger=logger, **config['train']) # train and save model cross_val = CrossValidation(X=X, y=y, Xtest=X[:100], logger=logger, **config['cv']) pred, pred_test, metrics, trained_models = cross_val.run_cv(model) for i, model in enumerate(trained_models): path = os.path.join(MODELS_PATH, 'model_{}_{}.h5'.format(model_id, i)) model.save(path)
print("train_x", train_x.shape, train_x.dtype) input_shape = (img_width, img_height, img_num_channels) logdir = "logs/" + datetime.now().strftime("%Y%m%d-%H%M%S") tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir) model = KerasModel(input_shape) # tf.keras.utils.plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True) model.compile( loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), optimizer=keras.optimizers.Adam(0.001), metrics=[tf.keras.metrics.SparseCategoricalAccuracy()]) # Fit data to model history = model.fit(train_x, train_y, callbacks=[tensorboard_callback], batch_size=50, epochs=6, verbose=True, validation_split=0.2) model.summary() score = model.evaluate(test_x, test_y, verbose=0) model.save('saved_model/model') probabilities = model.predict(test_x, verbose=True) print(probabilities)