def get_model():
        if mode == '':
            # a 25 layers deep VGG-style network with batchnorm
            k = 32
            model = VGG(input_shape=x_train.shape[1:],
                        nbstages=4,
                        nblayers=[6] * 4,
                        nbfilters=[1 * k, 2 * k, 4 * k, 8 * k],
                        nbclasses=y_train.shape[1],
                        use_bias=False,
                        batchnorm_training=False,
                        kernel_initializer='he_uniform')
        elif mode == 'fast':
            k = 16
            # a 13 layers deep VGG-style network with batchnorm
            model = VGG(input_shape=x_train.shape[1:],
                        nbstages=4,
                        nblayers=[3] * 4,
                        nbfilters=[1 * k, 2 * k, 4 * k, 8 * k],
                        nbclasses=y_train.shape[1],
                        use_bias=False,
                        batchnorm_training=False,
                        kernel_initializer='he_uniform')

        weights_location = 'model_initial_weights/cifar10_initial_weights' + mode + '.h5'
        if 'cifar10_initial_weights' + mode + '.h5' not in os.listdir(
                'model_initial_weights'):
            model.save_weights(weights_location)
        else:
            model.load_weights(weights_location)

        return model
    def get_model():
        k = 32
        model = VGG(
            input_shape=x_train.shape[1:],
            nbstages=5,
            nblayers=[2] * 5,
            nbfilters=[1 * k, 2 * k, 4 * k, 8 * k, 16 * k],
            nbclasses=y_train.shape[1],
            use_bias=False,
            batchnorm_training=False,  #use_batchnorm = False,
            kernel_initializer='he_uniform',
            batchnorm_momentum=0.9
        )  ### because training sometimes stops after very few epochs (~15)

        weights_location = 'model_initial_weights/tinyImagenet_initial_weights_batchnorm.h5'
        if 'tinyImagenet_initial_weights_batchnorm.h5' not in os.listdir(
                'model_initial_weights'):
            model.save_weights(weights_location)
        else:
            model.load_weights(weights_location)

        return model
Ejemplo n.º 3
0
        acc_val = train_accuracy.result() * 100
        print(template.format(epoch + 1, loss_val, acc_val), '\n')
        epoch += 1
        if loss_val < hparams.epsilon or epoch > hparams.max_epochs:
            break
        # if abs(loss_val - hparams.epsilon) < 0.01 or epoch > hparams.max_epochs:
        #     # if we've reached the cross-entropy criterion stopping point
        #     # or if we've exceeded the number of permissible epochs
        #     break
    end = time.time()
    total_time = end - start

    print('Stopping criterion reached!\nSaving model...')
    h5_path = os.path.join(os.getcwd(),
                           'weights_{}.h5'.format(hparams.model_num))
    model.save_weights(h5_path)
    info = {}
    train_acc_val = train_accuracy.result() * 100
    test_acc_val = test_accuracy.result() * 100
    info['hparams'] = vars(hparams)
    info['train_acc'] = float(train_acc_val.numpy())
    info['test_acc'] = float(test_acc_val.numpy())
    info['train_time'] = total_time
    json_path = os.path.join(os.getcwd(),
                             'config_{}.json'.format(hparams.model_num))
    with open(json_path, 'w') as file:
        json.dump(info, file)
    with summary_writer.as_default():
        tf.summary.scalar('Train Time (sec)', total_time, step=0)
    print('Training Time Elapsed: {} | Training Accuracy: {}'.format(
        total_time, train_acc_val))