示例#1
0
def quant_model(build_dir, batchsize, evaluate):
    '''
    Quantize the floating-point model
    Save to HDF5 file
    '''

    float_dir = build_dir + '/float_model'
    quant_dir = build_dir + '/quant_model'
    tfrec_dir = build_dir + '/tfrec_val'

    print(DIVIDER)
    print('Make & Save quantized model..')

    # load the floating point trained model
    float_model = load_model(float_dir + '/float_model.h5', compile=False)

    # get input dimensions of the floating-point model
    height = float_model.input_shape[1]
    width = float_model.input_shape[2]
    chans = float_model.input_shape[3]
    print(' Input dimensions: height:', height, ' width:', width, 'channels:',
          chans)

    # make TFRecord dataset and image processing pipeline
    test_dataset = input_fn(tfrec_dir, batchsize, False)

    # run quantization
    quantizer = vitis_quantize.VitisQuantizer(float_model)
    quant_model = quantizer.quantize_model(calib_dataset=test_dataset)

    # saved quantized model
    os.makedirs(quant_dir, exist_ok=True)
    quant_model.save(quant_dir + '/quant_model.h5')
    print(' Saved quantized model to', quant_dir + '/quant_model.h5')

    if (evaluate):
        '''
        Evaluate the quantized model
        '''
        print(DIVIDER)
        print('Evaluating quantized model..')

        quant_model.compile(
            loss=SparseCategoricalCrossentropy(from_logits=True),
            metrics=['sparse_categorical_accuracy'])

        scores = quant_model.evaluate(test_dataset, steps=None, verbose=0)

    print(' Quantized model accuracy: {0:.4f}'.format(scores[1] * 100), '%')
    print(DIVIDER)

    return
示例#2
0
def train(build_dir, batchsize, learnrate, epochs, max_classes):
    '''
    Train MobileNet
    '''
    def step_decay(epoch):
        '''
        Learning rate scheduler used by callback
        Reduces learning rate depending on number of epochs
        '''
        lr = learnrate
        if epoch > 150:
            lr /= 1000
        elif epoch > 10:
            lr /= 100
        elif epoch > 5:
            lr /= 10
        return lr

    float_dir = build_dir + '/float_model'
    tfrec_train = build_dir + '/tfrec_train'
    tfrec_val = build_dir + '/tfrec_val'
    tb_logs = build_dir + '/tb_logs'

    print('\n' + DIVIDER)
    print('Training')
    print(DIVIDER + '\n')
    '''
    Create floating-point MobileNet model
    '''
    print(DIVIDER)
    print('Make & train floating-point model..')

    model = xfer_model(input_shape=(224, 224, 3), classes=max_classes)

    # make directory to hold TFRecords
    shutil.rmtree(float_dir, ignore_errors=True)
    os.makedirs(float_dir)
    print('Directory', float_dir, 'created')

    # print model summary to a file
    from contextlib import redirect_stdout
    with open(float_dir + '/model_summary.txt', 'w') as f:
        with redirect_stdout(f):
            model.summary()
    print(' Printed model summary to', float_dir + '/model_summary.txt')
    '''
    tf.data pipelines
    '''
    train_dataset = input_fn(tfrec_train, batchsize, True)
    test_dataset = input_fn(tfrec_val, batchsize, False)
    '''
    Callbacks
    '''
    chkpt_call = ModelCheckpoint(filepath=os.path.join(float_dir,
                                                       'float_model.h5'),
                                 monitor='val_accuracy',
                                 verbose=1,
                                 save_best_only=True)
    tb_call = TensorBoard(log_dir=tb_logs, update_freq='epoch')
    lr_scheduler_call = LearningRateScheduler(schedule=step_decay, verbose=1)

    callbacks_list = [chkpt_call, lr_scheduler_call, tb_call]
    '''
    Compile model
    Adam optimizer to change weights & biases
    Loss function is sparse categorical crossentropy
    '''
    model.compile(optimizer=Adam(learning_rate=learnrate),
                  loss=SparseCategoricalCrossentropy(from_logits=True),
                  metrics=['accuracy'])
    '''
    Training
    '''
    print('\n' + DIVIDER)
    print(' Training model with training set..')
    print(DIVIDER)

    # make folder for saving trained model checkpoint
    os.makedirs(float_dir, exist_ok=True)

    # clean out TensorBoard logs
    shutil.rmtree(tb_logs, ignore_errors=True)
    os.makedirs(tb_logs)

    # run training
    train_hist = model.fit(train_dataset,
                           epochs=epochs,
                           steps_per_epoch=(max_classes * 1300) // batchsize,
                           validation_data=test_dataset,
                           validation_steps=None,
                           callbacks=callbacks_list,
                           verbose=1)

    print(
        '\nTensorBoard can be opened with the command: tensorboard --logdir=tb_logs --host localhost --port 6006'
    )
    '''
    Evaluate best checkpoint
    '''
    print('\n' + DIVIDER)
    print('Evaluating best checkpoint..')
    print(DIVIDER + '\n')

    # reload the best checkpoint and evaluate it
    eval_model = load_model(float_dir + '/float_model.h5', compile=False)

    eval_model.compile(loss=SparseCategoricalCrossentropy(from_logits=True),
                       metrics=['accuracy'])

    scores = eval_model.evaluate(test_dataset, steps=None, verbose=0)

    print(' Floating-point model accuracy: {0:.4f}'.format(scores[1] * 100),
          '%')
    print('\n' + DIVIDER)

    return
示例#3
0
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.contrib.slim.nets import resnet_v2
from tensorflow.contrib import slim

# os.environ['TF_CPP_MIN_VLOG_LEVEL'] = '3'
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'

if cfg.BASE_ARCHITECTURE == "resnet_v2_50":
    base_model = resnet_v2.resnet_v2_50
else:
    base_model = resnet_v2.resnet_v2_101

tf.reset_default_graph()

# train dataset queue using tf.data
train_datset = dataset_utils.input_fn(is_training=True,
                                      batch_size=cfg.BATCH_SIZE)

# test dataset queue using tf.data
test_dataset = dataset_utils.input_fn(is_training=False,
                                      batch_size=cfg.TEST_BATCH_SIZE)

images = tf.placeholder(tf.float32,
                        shape=[None, cfg.IMG_HIGHT, cfg.IMG_WIDTH, 3])
labels = tf.placeholder(tf.float32, shape=[None, cfg.N_CLASSES])
keep_prob = tf.placeholder(tf.float32, shape=None)
is_training = True
# keep_prob = None

with slim.arg_scope(
        resnet_v2.resnet_arg_scope(batch_norm_decay=cfg.BATCH_NORM_DECAY)):
    fratures, end_points = base_model(images,
import tensorflow as tf
from tensorflow.keras import layers
import dataset_utils
import numpy as np
import matplotlib.pyplot as plt

DATA_DIR = "./dataset"
BATCH_SIZE = 250
N_EPOCHS = 1
n_train = 60000
n_test = 10000
steps_per_epoch = n_train // BATCH_SIZE

# train dataset queue using tf.data
train_datset = dataset_utils.input_fn(data_dir=DATA_DIR,
                                      is_training=True,
                                      batch_size=BATCH_SIZE)

# test dataset queue using tf.data
test_dataset = dataset_utils.input_fn(data_dir=DATA_DIR,
                                      is_training=False,
                                      batch_size=BATCH_SIZE)

# define a convolutional model with 3 convolution and 2 dense layers.
model = tf.keras.Sequential([
    layers.Conv2D(16,
                  3,
                  padding='same',
                  activation='relu',
                  input_shape=(28, 28, 3)),
    layers.MaxPooling2D(),
示例#5
0
def quant_ft(build_dir, batchsize, learnrate, epochs, max_classes):
    '''
    Quantize & fine-tune the floating-point model
    Save to HDF5 file
    '''
    def step_decay(epoch):
        '''
        Learning rate scheduler used by callback
        Reduces learning rate depending on number of epochs
        '''
        lr = learnrate
        if epoch > 65:
            lr /= 10
        return lr

    float_dir = build_dir + '/float_model'
    quant_ft_dir = build_dir + '/quant_ft_model'
    tfrec_train = build_dir + '/tfrec_train'
    tfrec_val = build_dir + '/tfrec_val'

    print('\n' + DIVIDER)
    print('Quantization & Fine-tune')
    print(DIVIDER + '\n')

    # load the floating point trained model
    print(' Loading floating-point model from', float_dir + '/float_model.h5')
    float_model = load_model(float_dir + '/float_model.h5', compile=False)

    # get input dimensions of the floating-point model
    height = float_model.input_shape[1]
    width = float_model.input_shape[2]
    chans = float_model.input_shape[3]
    print(' Input dimensions: height:', height, ' width:', width, 'channels:',
          chans)

    # Quantization-aware training model
    quantizer = vitis_quantize.VitisQuantizer(float_model)
    ft_model = quantizer.get_qat_model()
    '''
    tf.data pipelines
    '''
    train_dataset = input_fn(tfrec_train, batchsize, True)
    test_dataset = input_fn(tfrec_val, batchsize, False)
    '''
    Call backs
    '''
    chkpt_call = ModelCheckpoint(filepath=os.path.join(quant_ft_dir,
                                                       'quant_ft.h5'),
                                 monitor='val_accuracy',
                                 verbose=1,
                                 save_best_only=True)
    lr_scheduler_call = LearningRateScheduler(schedule=step_decay, verbose=1)
    callbacks_list = [chkpt_call, lr_scheduler_call]
    '''
    Compile model
    Adam optimizer to change weights & biases
    Loss function is sparse categorical crossentropy
    '''
    ft_model.compile(optimizer=Adam(learning_rate=learnrate),
                     loss=SparseCategoricalCrossentropy(from_logits=True),
                     metrics=['accuracy'])
    '''
    Training
    '''
    print('\n' + DIVIDER)
    print(' Training model with training set..')
    print(DIVIDER)

    # make folder for saving trained model checkpoint
    os.makedirs(quant_ft_dir, exist_ok=True)

    # run training
    train_hist = ft_model.fit(train_dataset,
                              epochs=epochs,
                              steps_per_epoch=(1300 * max_classes) //
                              batchsize,
                              validation_data=test_dataset,
                              validation_steps=None,
                              callbacks=callbacks_list,
                              verbose=1)
    '''
    Evaluate quantized model
    '''
    print('\n' + DIVIDER)
    print('Evaluating quantized model..')
    print(DIVIDER + '\n')

    # reload the best checkpoint and evaluate it
    with vitis_quantize.quantize_scope():
        eval_model = load_model(quant_ft_dir + '/quant_ft.h5', compile=False)

    eval_model.compile(loss=SparseCategoricalCrossentropy(from_logits=True),
                       metrics=['accuracy'])

    scores = eval_model.evaluate(test_dataset, steps=None, verbose=0)

    print(' Quantized model accuracy: {0:.4f}'.format(scores[1] * 100), '%')
    print('\n' + DIVIDER)

    return