def task_train_filter(args): ff, mapper = getattr(__import__('dataset'), args.dataset)() dataset = Dataset(root_path=args.path, verbose=args.verbose, name='base-model', mapper=mapper, filename_filter=ff, rotation=False) dataset_batches = BatchGenerator(dataset, args.batch) from mitosis import model_base TT.debug("Compile base model.") model = model_base(args.lr) model_saved_weights_path = os.path.join(args.path, 'base-model.weights.npy') if os.path.exists(model_saved_weights_path): TT.info("Loading weights from %s" % model_saved_weights_path) model.load_weights(model_saved_weights_path) train_start = time.time() log = LearnLog("filter", args.path) for epoch in xrange(args.epoch): TT.debug(epoch + 1, "of", args.epoch, "epochs") log.on_dataset_epoch_begin(epoch + 1) for x, y in dataset_batches: model.fit(x, y, batch_size=args.mini_batch, nb_epoch=1, validation_split=.1, callbacks=[log], show_accuracy=True, shuffle=True) log.on_dataset_epoch_end(epoch + 1) log.on_dataset_train_end() TT.success("Training finished in %.2f hours." % ((time.time() - train_start) / 3600.))
def task_train_cnn(args): ff, mapper = getattr(__import__('dataset'), args.dataset)() dataset = Dataset(root_path=args.path, verbose=args.verbose, name='cnn', mapper=mapper, filename_filter=ff, ratio=9) dataset_batches = BatchGenerator(dataset, args.batch) from mitosis import model_base, model_1, model_2 TT.debug("Compile base model.") model = model_base(lr=0) TT.debug("Compile model 1.") model1 = model_1(args.lr) TT.debug("Compile model 2.") model2 = model_2(args.lr) model_saved_weights_path = os.path.join(args.path, 'base-model.weights.npy') model1_saved_weights_path = os.path.join(args.path, 'model1.weights.npy') model2_saved_weights_path = os.path.join(args.path, 'model2.weights.npy') if os.path.exists(model_saved_weights_path): TT.info("Loading weights from %s" % model_saved_weights_path) model.load_weights(model_saved_weights_path) if os.path.exists(model1_saved_weights_path): TT.info("Loading weights from %s" % model1_saved_weights_path) model1.load_weights(model1_saved_weights_path) if os.path.exists(model2_saved_weights_path): TT.info("Loading weights from %s" % model2_saved_weights_path) model2.load_weights(model2_saved_weights_path) train_start = time.time() log1 = LearnLog("model1", args.path) log2 = LearnLog("model2", args.path) for epoch in xrange(args.epoch): TT.debug(epoch + 1, "of", args.epoch, "epochs") log1.on_dataset_epoch_begin(epoch + 1) log2.on_dataset_epoch_begin(epoch + 1) for x, y in dataset_batches: outputs = model.predict(x, batch_size=args.mini_batch, verbose=args.verbose) # Multiply each window with it's prediction and then pass it to the next layer # x_new = [] # y_new = [] x = 1. - x for i in range(len(outputs)): if y[i][0] < 1.: # x_new.append(x[i]) # y_new.append(y[i]) x[i] *= outputs[i][0] TT.debug("Model 1 on epoch %d" % (epoch + 1)) model1.fit(numpy.asarray(x), numpy.asarray(y), batch_size=args.mini_batch, nb_epoch=1, validation_split=.1, callbacks=[log1], show_accuracy=True, shuffle=True) TT.debug("Model 2 on epoch %d" % (epoch + 1)) model2.fit(numpy.asarray(x), numpy.asarray(y), batch_size=args.mini_batch, nb_epoch=1, validation_split=.1, callbacks=[log2], show_accuracy=True, shuffle=True) log1.on_dataset_epoch_end(epoch + 1) log2.on_dataset_epoch_end(epoch + 1) log1.on_dataset_train_end() log2.on_dataset_train_end() TT.success("Training finished in %.2f hours." % ((time.time() - train_start) / 3600.))