コード例 #1
0
def adi(iterations=10000):
    model = None
    if os.path.exists(model_path):
        model = keras.models.load_model(model_path)
    else:
        model = buildModel(20*24)
        compile_model(model, 0.001)

    for it in range(iterations):

        # generate N scrambled cubes
        l = 100
        k = 20
        cube_agent = CubeAgent(number_of_cubes=l, batches=k)

        cube_agent.scramble_cubes_for_data()

        
        cubes = np.array(cube_agent.env).flatten()

        #initialize the training parameters -> marked by X and Y in the paper
        encodedStates = np.empty((k*l, 20*24)) 
        values = np.empty((k*l, 1))
        policies = np.empty(k*l)

        # iterate through the number of cubes and the number of actions
        i = 0
        for stateNumber , state in enumerate(cubes):
            valuesForState = np.zeros(len(state.action_space))

            encodedStates[i] = np.array(state.get_one_hot_state().flatten())
            actions = state.action_space

            start_state = state.cube.copy()

            #1-depth BFS 
            for j, action in enumerate(actions):
                _ , reward = state.step(j)
                childStateEncoded = np.array(state.get_one_hot_state()).flatten()
                state.set_state(start_state) #set back to the original

                value, policy = model.predict(childStateEncoded[None, :])
                valueNumber = value[0][0]
                valuesForState[j] = valueNumber + reward

            values[i] = np.array([valuesForState.max()])
            policies[i] = valuesForState.argmax()
            i += 1
        
        #log into Tensorboad
        log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
        tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

        sample_weight = np.array([[1/(i+1) for i in range(k)] for j in range(l)]).flatten()

        model.fit(encodedStates,
                 { "output_policy": policies, "output_value": values },
                 epochs=15, sample_weight=sample_weight)
        model.save(model_path)
    return model
コード例 #2
0
def create(tokenizer):
    """
    Creates the model.
    :param tokenizer: tokenizer for words
    :return: compiled untrained model
    """
    model = modelim.create_cnn_model(tokenizer.vocab_size)
    modelim.compile_model(model)
    return model
コード例 #3
0
def load_model():
    """
    Loads model from the file system.
    :return: loaded model, tokenizer
    """
    with open(_TOKENIZER_PATH, 'rb') as f:
        tokenizer = pickle.load(f)

    model = modelim.create_cnn_model(tokenizer.vocab_size)
    model.load_weights(_SAVE_PATH)
    modelim.compile_model(model)
    return model, tokenizer
コード例 #4
0
def train_keras():
    train_next_element = get_data(C.TRAIN_RECODER_PATH,C.EPOCHS)
    test_next_element = get_data(C.TEST_RECODER_PATH,C.EPOCHS)

    mymodel = keras_model()
    compile_model(mymodel)
    # 单机多GPU时只需要加上这一行
    # mymodel = tf.keras.utils.multi_gpu_model(mymodel, gpus=4)  # 指定GPU个数

    callbacks = [
        # Interrupt training if `val_loss` stops improving for over 2 epochs
        tf.keras.callbacks.EarlyStopping(patience=10, monitor='val_loss'),
        # Write TensorBoard logs to `./logs` directory
        tf.keras.callbacks.TensorBoard(log_dir='./logs')
        #tf.keras.callbacks.ModelCheckpoint(C.CLASS_MODEL_SAVE_PATH,
        #                                   monitor='val_loss', verbose=1,
        #                                   save_best_only=True,
        #                                    mode='auto')
        ]

    X = [train_next_element[0],train_next_element[1],
         train_next_element[2]]
    y = train_next_element[3]

    test_X = [test_next_element[0],test_next_element[1],
         test_next_element[2]]
    test_y = test_next_element[3]
    """
    注意,tf.keras可以直接接受由tf.data的迭代器,但是此时要指定steps_per_epoch参数
    该参数为每个epoch训练的步数,即训练数据量/batch_size,每个batch是一个step
    可以配合tf.data中的repeat方法进行使用,多个epoch就是对数据进行多轮使用
    
    """
    if C.RESTORE_MODEL:
        print("开始增量训练===>>>")
        # Recreate the exact same model purely from the file:
        mymodel = tf.keras.models.load_model(C.REGTRSSION_MODEL_SAVE_PATH)
        compile_model(mymodel)
    steps_per_epoch = int(C.TRAIN_DATA_SIZE/C.BATCH_SIZE)
    valid_steps = int(C.TEST_DATA_SIZE/C.BATCH_SIZE)

    mymodel.fit(X,y,epochs=C.EPOCHS,
                steps_per_epoch=steps_per_epoch,
                validation_data=[test_X, test_y],
                validation_steps=valid_steps,
                callbacks = callbacks)

    print("开始保存模型===>>>")
    mymodel.save(C.REGTRSSION_MODEL_SAVE_PATH)

    print("All finished!!! ")
コード例 #5
0
    def on_epoch_begin(self, epoch, logs={}):
        if epoch > 0 and epoch % self.eval_frequency == 0:

            # Unhappy hack to work around h5py not being able to write to GCS.
            # Force snapshots and saves to local filesystem, then copy them over to GCS.
            model_path_glob = 'checkpoint.*'
            if not self.job_dir.startswith("gs://"):
                model_path_glob = os.path.join(self.job_dir, model_path_glob)
            checkpoints = glob.glob(model_path_glob)
            if len(checkpoints) > 0:
                checkpoints.sort()
                census_model = load_model(checkpoints[-1])
                census_model = model.compile_model(census_model,
                                                   self.learning_rate)
                loss, acc = census_model.evaluate_generator(
                    model.generator_input(self.eval_files,
                                          chunk_size=CHUNK_SIZE),
                    steps=self.steps)
                print '\nEvaluation epoch[{}] metrics[{:.2f}, {:.2f}] {}'.format(
                    epoch, loss, acc, census_model.metrics_names)
                if self.job_dir.startswith("gs://"):
                    copy_file_to_gcs(self.job_dir, checkpoints[-1])
            else:
                print '\nEvaluation epoch[{}] (no checkpoints found)'.format(
                    epoch)
コード例 #6
0
ファイル: run.py プロジェクト: dextr001/deep-CNN-keras
def train_model(args, params):
    """Trains a model on the training data.

  The test data is used to report validation accuracy after each training epoch.
  The model can be trained from scratch, or existing weights can be updated.
  
  Args:
    args: the arguments from argparse that contains all user-specified options.
    params: a ModelParams object containing the appropriate data file paths,
        data parameters, and training hyperparameters.
  """
    # Set the data parameters and image source paths.
    img_info = ImageInfo(params)
    # Load the model and (possibly) its weights.
    model = get_model(args, img_info)
    # Save the model if that option was specified.
    if args.save_model:
        f = open(args.save_model, 'w')
        f.write(model.to_json())
        f.close()
        print 'Saved model architecture to {}.'.format(args.save_model)
    # Compile the model.
    print('Compiling module...')
    timer = ElapsedTimer()
    compile_model(model, params)
    print 'Done in {}.'.format(timer.get_elapsed_time())
    # Load the images into memory and preprocess appropriately.
    timer.reset()
    img_loader = ImageLoader(img_info)
    img_loader.load_all_images()
    print 'Data successfully loaded in {}.'.format(timer.get_elapsed_time())
    # Train the model.
    timer.reset()
    # TODO: implement data augmentation option.
    model.fit(img_loader.train_data,
              img_loader.train_labels,
              validation_data=(img_loader.test_data, img_loader.test_labels),
              batch_size=params['batch_size'],
              nb_epoch=params['num_epochs'],
              shuffle=True,
              show_accuracy=True,
              verbose=1)
    print 'Finished training in {}.'.format(timer.get_elapsed_time())
    # Save the weights if that option was specified.
    if args.save_weights:
        model.save_weights(args.save_weights)
        print 'Saved trained model weights to {}.'.format(args.save_weights)
コード例 #7
0
ファイル: task.py プロジェクト: xudong-z/jupyter-notebooks
 def on_epoch_begin(self, epoch, logs={}):
   if epoch > 0 and epoch % self.eval_frequency == 0:
     checkpoints = glob.glob(os.path.join(self.job_dir, 'checkpoint.*'))
     checkpoints.sort()
     census_model = load_model(checkpoints[-1])
     census_model = model.compile_model(census_model, self.learning_rate)
     loss, acc = census_model.evaluate_generator(
         model.generator_input(self.eval_files, chunk_size=CHUNK_SIZE),
         steps=self.steps)
     print('\nEvaluation epoch[{}] metrics[{:.2f}, {:.2f}] {}'.format(
         epoch, loss, acc, census_model.metrics_names))
コード例 #8
0
ファイル: task.py プロジェクト: taiseii/D.c.1
 def on_epoch_begin(self, epoch, logs={}):
   if epoch > 0 and epoch % self.eval_frequency == 0:
     # Unhappy hack to work around h5py not being able to write to GCS.
     # Force snapshots and saves to local filesystem, then copy them over to GCS.
     model_path_glob = 'checkpoint.*'
     if not self.job_dir.startswith("gs://"):
       model_path_glob = os.path.join(self.job_dir, model_path_glob)
     checkpoints = glob.glob(model_path_glob)
     if len(checkpoints) > 0:
       checkpoints.sort()
       retinopathy_model = load_model(checkpoints[-1])
       retinopathy_model = model.compile_model(retinopathy_model)
       loss, acc = retinopathy_model.evaluate(self.X_test, self.Y_test)
       print '\nEvaluation epoch[{}] metrics[{:.2f}, {:.2f}] {}'.format(
           epoch, loss, acc, retinopathy_model.metrics_names)
       if self.job_dir.startswith("gs://"):
         copy_file_to_gcs(self.job_dir, checkpoints[-1])
     else:
       print '\nEvaluation epoch[{}] (no checkpoints found)'.format(epoch)
コード例 #9
0
ファイル: eval.py プロジェクト: chmod644/tgs_salt_model
def search_best_threshod(model, sess, iterator, steps):
    # _model = deepcopy(model)
    scores = {}
    for threshold in np.arange(0.0, 1.0001, 0.05):
        sess.run(iterator.initializer)
        model = compile_model(model,
                              optimizer="adam",
                              loss='bce',
                              threshold=threshold,
                              deep_supervised=FLAGS.deep_supervised)
        metrics = model.evaluate(x=iterator, steps=steps)
        if not FLAGS.deep_supervised:
            monitor = 'weighted_mean_score'
        else:
            monitor = 'output_final_weighted_mean_score'
        score = metrics[model.metrics_names.index(monitor)]
        print("score:{} (threshold={})".format(score, threshold))
        scores[threshold] = score

    print(scores)
    threshold_best = max(scores.items(), key=itemgetter(1))[0]
    return threshold_best
コード例 #10
0
    def on_epoch_end(self, epoch, logs={}):
        self.epochs_since_last_save += 1
        if self.epochs_since_last_save >= self.eval_frequency:
            self.epochs_since_last_save = 0
            # Unhappy hack to work around h5py not being able to write to GCS.
            # Force snapshots and saves to local filesystem, then copy them over to GCS.
            model_path_glob = 'checkpoint.*'
            if not self.job_dir.startswith("gs://"):
                model_path_glob = os.path.join(self.job_dir, model_path_glob)
            checkpoints = glob.glob(model_path_glob)
            if len(checkpoints) > 0:
                checkpoints.sort()
                forecast_model = load_model(checkpoints[-1])
                forecast_model = model.compile_model(forecast_model)
                x, y = model.load_features(self.eval_files, self.scaler)
                metrics = forecast_model.evaluate(x, y)
                print('\n*** Evaluation epoch[{}] metrics {}'.format(
                    epoch, metrics, forecast_model.metrics_names))

                y_hat = forecast_model.predict(x)
                y_hat = model.invert_scale_sales(y_hat, self.scaler)
                np.savetxt(
                    os.path.join(self.job_dir,
                                 'preds/yhat_{:06d}.txt'.format(epoch)), y_hat)

                self.tf_logger.append(metrics_dict={
                    name: value
                    for (name,
                         value) in zip(forecast_model.metrics_names, metrics)
                },
                                      epoch=epoch)

                if self.job_dir.startswith("gs://"):
                    copy_file_to_gcs(self.job_dir, checkpoints[-1])
            else:
                print(
                    '\n*** Evaluation epoch[{}] (no checkpoints found)'.format(
                        epoch))
コード例 #11
0
ファイル: scoring.py プロジェクト: slewyh/iot-sec
def get_id_result():

    #get training and testing pair for training
    (x_train,
     y_train) = get_fft_features_from_list_file(c.ENROLL_LIST_FILE, c.MAX_SEC)
    (x_test, y_test) = get_fft_features_from_list_file(c.TEST_LIST_FILE,
                                                       c.MAX_SEC)
    y_train = to_categorical(y_train, num_classes=c.NUM_CLASSES)
    y_test = to_categorical(y_test, num_classes=c.NUM_CLASSES)

    print("Y_train.shape: ", y_train.shape)

    if c.RETRAIN:
        model = retrain(x_train, y_train)
    else:
        baseline_model = vggvox_model()
        model = vggvox_mod_model(baseline_model)
        model.load_weights(c.VGGM_WEIGHTS_FILE)
        model = compile_model(model)

    score = model.evaluate(x_test, y_test, verbose=1)
    print("loss: {}, top-1 accuracy (/%): {}, top-5 accuracy (/%): {}".format(
        score[0], score[1], score[2]))
コード例 #12
0
def main():
    parser = create_parser()
    args = parser.parse_args()

    if args.setup:
        create_directories()

    if args.debug:
        dataset = DATASETS['debug']
        args.dataset = "debug"
        features, _, labels, _ = preprocess_data(args.patch_size,
                                                 args.distribution,
                                                 dataset=dataset)
        #print(features, 'debug')
        #print("length of features: ",type(features), len(features),'element.shape: ',features[0][0])
        features_train, features_test = features[:100], features[100:120]
        labels_train, labels_test = labels[:100], labels[100:120]
    elif args.train_model or args.evaluate_model or args.preprocess_data:
        dataset = DATASETS[args.dataset]
        #print(dataset.values())
        load_from_cache = not args.preprocess_data
        try:
            features_train, features_test, labels_train, labels_test = preprocess_data(
                args.patch_size,
                args.distribution,
                dataset=dataset,
                only_cache=load_from_cache)
            #print(features_train, 'train_model or evaluate_model or preprocess_data')
            print("Length of features_train: ", len(features_train))
        except IOError:
            print("Cache file does not exist. Please run again with -p flag.")
            sys.exit(1)

        if args.visualise:
            visualise_labels(labels_train, args.patch_size, LABELS_DIR)
            visualise_labels(labels_test, args.patch_size, LABELS_DIR)

    if not args.model_id:
        timestamp = time.strftime("%d_%m_%Y_%H%M")
        model_id = "{}_{}_{}".format(timestamp, args.dataset,
                                     args.architecture)
    else:
        model_id = args.model_id

    if args.init_model or args.train_model or args.evaluate_model:
        model_dir = os.path.join(OUTPUT_DIR, model_id)
        save_makedirs(model_dir)

    # Hyperparameters for the model. Since there are so many of them it is
    # more convenient to set them in the source code as opposed to passing
    # them as arguments to the Command Line Interface. We use a list of tuples instead of a
    # dict since we want to print the hyperparameters and for that purpose
    # keep them in the predefined order.
    hyperparameters = [
        ("architecture", args.architecture),
        # Hyperparameters for the first convolutional layer.
        ("nb_filters_1", 64),
        ("filter_size_1", 9),
        ("stride_1", (2, 2)),
        # Hyperparameter for the first pooling layer.
        ("pool_size_1", (2, 2)),
        # Hyperparameter for the second convolutional layer (when
        # two layer architecture is used).
        ("nb_filters_2", 128),
        ("filter_size_2", 5),
        ("stride_2", (1, 1)),
        # Hyperparameters for Stochastic Gradient Descent.
        ("learning_rate", 0.05),
        ("momentum", 0.9),
        ("decay", 0.0)
    ]

    hyperparameters_mnih = [
        ("architecture", args.architecture),
        # Hyperparameters for the first convolutional layer.
        ("nb_filters_1", 64),
        ("filter_size_1", 16),
        ("stride_1", (4, 4)),
        # Hyperparameter for the first pooling layer.
        ("pool_size_1", (2, 2)),
        ("pool_stride", 1),
        # Hyperparameter for the second convolutional layer).
        ("nb_filters_2", 112),
        ("filter_size_2", 4),
        ("stride_2", (1, 1)),
        # Hyperparameter for the third convolutional layer).
        ("nb_filters_3", 80),
        ("filter_size_3", 3),
        ("stride_3", (1, 1)),

        # Hyperparameters for Stochastic Gradient Descent.
        ("learning_rate", 0.05),
        ("momentum", 0.9),
        ("decay", 0.0)
    ]

    if args.init_model:
        model = init_model(args.patch_size, model_id,
                           **dict(hyperparameters_mnih))
        save_model_summary(hyperparameters_mnih, model, model_dir)
    elif args.train_model or args.evaluate_model:
        hyperparameters = dict(hyperparameters_mnih)
        model = load_model(model_id)
        model = compile_model(model, hyperparameters["learning_rate"],
                              hyperparameters['momentum'],
                              hyperparameters["decay"])

    if args.train_model:
        model = train_model(model,
                            features_train,
                            labels_train,
                            args.patch_size,
                            model_id,
                            model_dir,
                            nb_epoch=args.epochs,
                            checkpoints=args.checkpoints,
                            tensorboard=args.tensorboard,
                            earlystop=args.earlystop)

    if args.evaluate_model:
        evaluate_model(model,
                       features_test,
                       labels_test,
                       args.patch_size,
                       model_dir,
                       out_format=args.out_format)
コード例 #13
0
ファイル: train.py プロジェクト: yang-neu/bnn
                                              height=opts.height)

num_test_files = len(os.listdir(opts.test_image_dir))
num_test_steps = num_test_files // opts.batch_size
print("num_test_files=", num_test_files, "batch_size=", opts.batch_size,
      "=> num_test_steps=", num_test_steps)

# training model.
train_model = model.construct_model(
    width=opts.patch_width_height or opts.width,
    height=opts.patch_width_height or opts.height,
    use_skip_connections=not opts.no_use_skip_connections,
    base_filter_size=opts.base_filter_size,
    use_batch_norm=not opts.no_use_batch_norm)
model.compile_model(train_model,
                    learning_rate=opts.learning_rate,
                    pos_weight=opts.pos_weight)
print("TRAIN MODEL")
print(train_model.summary())

# test model.
test_model = model.construct_model(
    width=opts.width,
    height=opts.height,
    use_skip_connections=not opts.no_use_skip_connections,
    base_filter_size=opts.base_filter_size,
    use_batch_norm=not opts.no_use_batch_norm)
model.compile_model(test_model,
                    learning_rate=opts.learning_rate,
                    pos_weight=opts.pos_weight)
print("TEST MODEL")
コード例 #14
0
ファイル: train.py プロジェクト: chmod644/tgs_salt_model
def train(dataset):
    flag_values_dict = FLAGS.flag_values_dict()
    pprint(flag_values_dict, indent=4)
    with open(os.path.join(FLAGS.model, FLAGS_FILENAME), 'w') as f:
        json.dump(flag_values_dict, f, indent=4)

    # FLAGS.weight_ad is parsed to [coverage_min, coverage_max], threshold to apply adaptive weight
    if FLAGS.weight_ad is not None:
        weight_adaptive = [float(x) for x in FLAGS.weight_ad]
    else:
        weight_adaptive = None

    with tf.device('/cpu:0'):
        iter_train, iter_valid = dataset.gen_train_valid(
            n_splits=N_SPLITS,
            idx_kfold=FLAGS.cv,
            batch_size=FLAGS.batch_size,
            adjust=FLAGS.adjust,
            weight_fg=FLAGS.weight_fg,
            weight_bg=FLAGS.weight_bg,
            weight_adaptive=weight_adaptive,
            filter_vert_hori=FLAGS.filter_vert_hori,
            ignore_tiny=FLAGS.ignore_tiny,
            augment_dict=augment_dict(),
            deep_supervised=FLAGS.deep_supervised,
            mask_padding=FLAGS.mask_padding,
            with_depth=FLAGS.with_depth)

    sess = tf.Session(config=tf.ConfigProto(
        allow_soft_placement=True,
        gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.9,
                                  allow_growth=True)))
    K.set_session(sess)

    if FLAGS.debug:
        debug_img_show(iter_train, iter_valid, sess)

    with tf.device('/gpu:0'):
        if FLAGS.restore is not None:
            path_restore = os.path.join(FLAGS.restore, NAME_MODEL)
            print("Restoring model from {}".format(path_restore))
            model = load_model(path_restore, compile=False)
        elif FLAGS.contrib is not None:
            model = build_model_contrib(IM_HEIGHT,
                                        IM_WIDTH,
                                        IM_CHAN,
                                        encoder=FLAGS.contrib,
                                        residual_unit=FLAGS.residual_unit,
                                        spatial_dropout=FLAGS.spatial_dropout,
                                        preprocess=FLAGS.preprocess,
                                        last_kernel=FLAGS.last_kernel,
                                        last_1x1=FLAGS.last_1x1)
        elif FLAGS.pretrained is not None:
            if not FLAGS.deep_supervised:
                model = build_model_pretrained(
                    IM_HEIGHT,
                    IM_WIDTH,
                    IM_CHAN,
                    encoder=FLAGS.pretrained,
                    spatial_dropout=FLAGS.spatial_dropout,
                    retrain=FLAGS.retrain,
                    preprocess=FLAGS.preprocess,
                    renorm=FLAGS.renorm,
                    last_kernel=FLAGS.last_kernel,
                    last_1x1=FLAGS.last_1x1)
            else:
                model = build_model_pretrained_deep_supervised(
                    IM_HEIGHT,
                    IM_WIDTH,
                    IM_CHAN,
                    encoder=FLAGS.pretrained,
                    spatial_dropout=FLAGS.spatial_dropout,
                    retrain=FLAGS.retrain,
                    preprocess=FLAGS.preprocess,
                    last_kernel=FLAGS.last_kernel,
                    last_1x1=FLAGS.last_1x1)
        elif FLAGS.use_ref2:
            model = build_model_ref2(IM_HEIGHT,
                                     IM_WIDTH,
                                     IM_CHAN,
                                     preprocess=FLAGS.preprocess)
        elif FLAGS.use_ref:
            model = build_model_ref(IM_HEIGHT,
                                    IM_WIDTH,
                                    IM_CHAN,
                                    batch_norm=FLAGS.batch_norm,
                                    drop_out=FLAGS.drop_out,
                                    depth=FLAGS.depth,
                                    start_ch=FLAGS.start_ch)
        else:
            model = build_model(IM_HEIGHT,
                                IM_WIDTH,
                                IM_CHAN,
                                batch_norm=FLAGS.batch_norm,
                                drop_out=FLAGS.drop_out)

        if FLAGS.restore_weight is not None:
            path_weight = os.path.join(FLAGS.restore_weight, NAME_MODEL)
            print("Restoring weights from {}".format(path_weight))
            model.load_weights(path_weight, by_name=True)

        model = compile_model(model,
                              optimizer=FLAGS.opt,
                              loss=FLAGS.loss,
                              weight_decay=FLAGS.weight_decay,
                              exclude_bn=FLAGS.exclude_bn,
                              deep_supervised=FLAGS.deep_supervised)
        write_summary(model, os.path.join(FLAGS.model, MODEL_SUMMARY_FILENAME))
        model.summary()

    path_model = os.path.join(FLAGS.model, NAME_MODEL)

    if not FLAGS.deep_supervised:
        monitor = 'val_weighted_mean_score'
    else:
        monitor = 'val_output_final_weighted_mean_score'
    checkpointer = ModelCheckpoint(path_model,
                                   monitor=monitor,
                                   verbose=1,
                                   save_best_only=FLAGS.save_best_only,
                                   mode='max')
    tensorboarder = MyTensorBoard(FLAGS.log, model=model)
    if not FLAGS.cyclic:
        lrscheduler = LearningRateScheduler(StepDecay(FLAGS.lr, FLAGS.lr_decay,
                                                      FLAGS.epochs_decay,
                                                      FLAGS.freeze_once),
                                            verbose=1)
    else:
        lrscheduler = LearningRateScheduler(CLRDecay(
            FLAGS.lr,
            max_lr=FLAGS.max_lr,
            epoch_size=FLAGS.epoch_size,
            mode=FLAGS.mode_clr,
            freeze_once=FLAGS.freeze_once),
                                            verbose=1)

    callbacks = [checkpointer, tensorboarder, lrscheduler]
    if FLAGS.early_stopping:
        callbacks.append(EarlyStopping(patience=5, verbose=1))
    if FLAGS.reduce_on_plateau:
        lrreducer = ReduceLROnPlateau(monitor='val_loss',
                                      factor=0.5,
                                      patience=8,
                                      verbose=1,
                                      mode='min',
                                      epsilon=0.0001,
                                      cooldown=4)
        callbacks.append(lrreducer)

    num_train, num_valid = dataset.len_train_valid(n_splits=N_SPLITS,
                                                   idx_kfold=FLAGS.cv)

    steps_per_epoch = int(num_train / FLAGS.batch_size)
    validation_steps = int(num_valid / FLAGS.batch_size)

    results = model.fit(x=iter_train,
                        validation_data=iter_valid,
                        epochs=FLAGS.epochs,
                        steps_per_epoch=steps_per_epoch,
                        validation_steps=validation_steps,
                        shuffle=True,
                        callbacks=callbacks)
コード例 #15
0
ファイル: main.py プロジェクト: luweijia1013/ReID-2015
def main(dataset_path):
    model = generate_model()
    model = compile_model(model)
    train(model, dataset_path)
コード例 #16
0
ファイル: eval.py プロジェクト: chmod644/tgs_salt_model
def eval(dataset):

    path_model = os.path.join(FLAGS.model, NAME_MODEL)

    config = tf.ConfigProto(allow_soft_placement=True,
                            gpu_options=tf.GPUOptions(
                                per_process_gpu_memory_fraction=0.8,
                                allow_growth=True))

    if FLAGS.weight_ad is not None:
        weight_adaptive = [float(x) for x in FLAGS.weight_ad]
    else:
        weight_adaptive = None

    with tf.Graph().as_default():
        iter_train, iter_valid = dataset.gen_train_valid(
            n_splits=N_SPLITS,
            idx_kfold=FLAGS.cv,
            batch_size=FLAGS.batch_size,
            adjust=FLAGS.adjust,
            weight_fg=FLAGS.weight_fg,
            weight_bg=FLAGS.weight_bg,
            weight_adaptive=weight_adaptive,
            repeat=1,
            filter_vert_hori=False,
            deep_supervised=FLAGS.deep_supervised,
            with_depth=FLAGS.with_depth)

        num_train, num_valid = dataset.len_train_valid(N_SPLITS, FLAGS.cv)

        with tf.Session(config=config) as sess:
            K.set_session(sess)
            steps_train = int(np.ceil(num_train / FLAGS.batch_size))
            steps_valid = int(np.ceil(num_valid / FLAGS.batch_size))

            model = load_model(path_model, compile=False)

            threshold = FLAGS.threshold
            if FLAGS.best_threshold:
                print("Searching best threshold for validation data")
                threshold = search_best_threshod(model, sess, iter_valid,
                                                 steps_valid)
                print("Best threshold is {}".format(threshold))

            model = compile_model(model,
                                  optimizer="adam",
                                  loss=FLAGS.loss,
                                  threshold=threshold,
                                  deep_supervised=FLAGS.deep_supervised)

            sess.run([iter_train.initializer, iter_valid.initializer])
            metrics = model.evaluate(x=iter_train, steps=steps_train)
            print("Training " + ", ".join([
                "{}:{}".format(n, m)
                for n, m in zip(model.metrics_names, metrics)
            ]) + " (threshold={})".format(threshold))
            metrics = model.evaluate(x=iter_valid, steps=steps_valid)
            print("Validation " + ", ".join([
                "{}:{}".format(n, m)
                for n, m in zip(model.metrics_names, metrics)
            ]) + " (threshold={})".format(threshold))
コード例 #17
0
from model import compile_model
from helpers import early_stopping, model_checkpoint, tensor_board, get_data

if __name__ == '__main__':
    model = compile_model()
    callbacks = [early_stopping(), model_checkpoint(), tensor_board()]
    train_batch, val_batch = get_data()
    model.fit_generator(generator=train_batch, steps_per_epoch= len(train_batch),
                        validation_data=val_batch, validation_steps=len(val_batch),
                        callbacks=callbacks, epochs=100, verbose=0, max_queue_size=3)
コード例 #18
0
ファイル: run.py プロジェクト: dextr001/deep-CNN-keras
def test_model(args, params):
    """Tests a model on the test data set.

  Prints out the final accuracy of the predictions on the test data. Also prints
  a normalized confusion matrix if that argument is specified by the user.
  
  Args:
    args: the arguments from argparse that contains all user-specified options.
        The model weights that are to be tested must be provided.
    params: a ModelParams object containing the appropriate data file paths and
        data parameters.
  """
    if not args.load_weights:
        print 'Cannot test model: no weights provided.'
        return
    img_info = ImageInfo(params, test_only=True)
    # Load the model and its weights and compile it.
    model = get_model(args, img_info)
    print('Compiling module...')
    timer = ElapsedTimer()
    compile_model(model, params)
    print 'Done in {}.'.format(timer.get_elapsed_time())
    # Load the test images into memory and preprocess appropriately.
    timer.reset()
    img_loader = ImageLoader(img_info)
    img_loader.load_test_images()
    print 'Test data successfully loaded in {}.'.format(
        timer.get_elapsed_time())
    # Run the evaluation on the test data.
    timer.reset()
    predictions = model.predict_classes(img_loader.test_data,
                                        batch_size=params['batch_size'])
    print 'Finished testing in {}.'.format(timer.get_elapsed_time())
    # Compute the percentage of correct classifications.
    num_predicted = len(predictions)
    num_correct = 0
    num_classes = params['number_of_classes']
    confusion_matrix = np.zeros((num_classes, num_classes))
    misclassified = []
    # Convert the test image dictionary to a flat list.
    test_img_files = []
    for i in range(num_classes):
        for img_file in img_info.test_img_files[i]:
            test_img_files.append(img_file)
    # Compute confusion matrix and find incorrect classifications.
    for i in range(num_predicted):
        predicted_class = predictions[i]
        correct = np.nonzero(img_loader.test_labels[i])
        correct = correct[0][0]
        confusion_matrix[correct][predicted_class] += 1
        if predicted_class == correct:
            num_correct += 1
        else:
            # Save the image file name, its correct class, and its predicted class.
            misclassified.append((test_img_files[i], correct, predicted_class))
    accuracy = round(float(num_correct) / float(num_predicted), 4)
    print 'Predicted classes for {} images with accuracy = {}'.format(
        num_predicted, accuracy)
    if args.confusion_matrix:
        # Normalize and print the matrix.
        per_row_max = confusion_matrix.sum(axis=1)
        confusion_matrix = confusion_matrix.transpose() / per_row_max
        confusion_matrix = confusion_matrix.transpose()
        output = ''
        for row in confusion_matrix:
            row_list = list(row)
            output += ' '.join(map(str, row_list)) + '\n'
        f = open(args.confusion_matrix, 'w')
        f.write(output)
        f.close()
        print 'Saved confusion matrix to {}.'.format(args.confusion_matrix)
    if args.report_misclassified:
        f = open(args.report_misclassified, 'w')
        for example in misclassified:
            img_path, img_class, predicted_class = example
            f.write('{} {} {}\n'.format(img_path, img_class, predicted_class))
        f.close()
        print 'Saved misclassified images report to {}.'.format(
            args.report_misclassified)
    if args.report_scores:
        print 'Computing instance scores...'
        scores = model.predict_proba(img_loader.test_data,
                                     batch_size=params['batch_size'],
                                     verbose=0)
        f = open(args.report_scores, 'w')
        score_template = ' '.join(['{}'] * num_classes)
        for i in range(len(scores)):
            score_list = [round(score, 5) for score in scores[i]]
            score_string = score_template.format(*score_list)
            f.write('{} {}\n'.format(test_img_files[i], score_string))
        f.close()
        print 'Saved scores report to {}.'.format(args.report_scores)
コード例 #19
0
    plt.title('Training and validation loss for ' + str(C.EPOCHS) + ' epochs')
    plt.gca().set_xlabel('Epochs')
    plt.gca().set_ylabel('Loss')
    plt.legend()
    plt.tight_layout()

    plt.gcf().savefig('loss.png')


if __name__ == '__main__':
    # Load the dataset and split them into training and test sets
    X_train, X_test, Y_train, Y_test = get_dataset()

    # Create the model and compile it
    model = create_model()
    compile_model(model)

    print(model.summary())
    print()

    print('Training model...')
    training_history = fit_model(model, X_train, Y_train)
    print()

    print('Evaluating model...')
    metrics = evaluate_model(model, X_test, Y_test)
    print()

    print('Loss on test set is:', metrics[0])
    print('Accuracy on test set is:', metrics[-1])
    print()
コード例 #20
0
def dispatch(data_file, job_dir, num_epochs):

    timestamp = str(time.time())
    job_dir = job_dir + "/run" + timestamp

    x_train, y_train, maxlen, nb_chars, char_indices, indices_char = model.get_training_data(
        data_file[0])

    embedding_dims = 128
    filters = 250
    kernel_size = 3
    hidden_dims = 8
    batch_size = 32
    dropout_rate = 0.3
    epochs = num_epochs

    sentiment_model = model.model_fn(nb_chars=nb_chars,
                                     maxlen=maxlen,
                                     embedding_dims=embedding_dims,
                                     hidden_dims=hidden_dims,
                                     dropout_rate=dropout_rate,
                                     filters=filters,
                                     kernel_size=kernel_size)

    try:
        os.makedirs(job_dir)
    except:
        pass

    # Unhappy hack to work around h5py not being able to write to GCS.
    # Force snapshots and saves to local filesystem, then copy them over to GCS.
    checkpoint_path = FILE_PATH
    if not job_dir.startswith("gs://"):
        checkpoint_path = os.path.join(job_dir, checkpoint_path)

    # Model checkpoint callback
    checkpoint = keras.callbacks.ModelCheckpoint(checkpoint_path,
                                                 monitor='val_loss',
                                                 verbose=1,
                                                 save_best_only=True,
                                                 mode='min')

    timestamp = str(time.time())

    # Tensorboard logs callback
    tblog = keras.callbacks.TensorBoard(log_dir=os.path.join(job_dir, 'logs'),
                                        write_graph=True,
                                        embeddings_freq=0)

    callbacks = [checkpoint, tblog]

    sentiment_model = model.compile_model(sentiment_model)
    sentiment_model.fit(x_train,
                        y_train,
                        epochs=epochs,
                        validation_split=0.3,
                        batch_size=batch_size,
                        callbacks=callbacks)

    # Unhappy hack to work around h5py not being able to write to GCS.
    # Force snapshots and saves to local filesystem, then copy them over to GCS.
    if job_dir.startswith("gs://"):
        sentiment_model.save(SENTIMENT_MODEL)
        copy_file_to_gcs(job_dir, SENTIMENT_MODEL)
    else:
        sentiment_model.save(os.path.join(job_dir, SENTIMENT_MODEL))

    # Convert the Keras model to TensorFlow SavedModel
    model.to_savedmodel(sentiment_model, os.path.join(job_dir, 'export'))