예제 #1
0
def main():
    #Load Data
    _, _, testloader = bank.loadData(arg_load_train=LOAD_TRAIN,
                                     arg_load_val=LOAD_VAL,
                                     arg_load_test=LOAD_TEST)

    #Define Eval Device
    eval_device = par.QUANT_DEVICE if par.EVAL_LOAD_MODEL_IS_QUANTIZED else par.TRAIN_DEVICE

    #Empty GPU Cache before Evaluation starts
    if eval_device == 'cuda:0': torch.cuda.empty_cache()

    #Load Model
    used_model = mod.UsedModel(
        par.MODEL_USED_MODEL_TYPE,
        arg_load_path=par.MODEL_LOAD_MODEL_PATH,
        arg_load=True,
        arg_load_device=eval_device,
        arg_load_quantized=par.EVAL_LOAD_MODEL_IS_QUANTIZED)
    used_model.model.to(eval_device)

    #Eval
    print("\nEvaluation Started")
    printdatasetName()
    used_model.model.eval()
    model_accuracy, _, _ = evaluate(used_model, testloader, eval_device)
    print('Evaluation Accuracy on all test images, %2.2f' %
          (model_accuracy.avg))
    print("Evaluation Finished")
예제 #2
0
def multi():
    _, _, testloader = bank.loadData(arg_load_train=False,
                                     arg_load_val=False,
                                     arg_load_test=True)
    test_device = torch.device(par.TRAIN_DEVICE)

    # Empty GPU Cache before Testing starts
    if par.TRAIN_DEVICE == 'cuda:0': torch.cuda.empty_cache()

    used_model = mod.UsedModel(par.MODEL_USED_MODEL_TYPE,
                               arg_load_path=par.MODEL_LOAD_MODEL_PATH,
                               arg_load=True)
    used_model.to(TEST_DEVICE)
    used_model.model.eval()

    singleBatch = next(iter(testloader))
    input, label, name = singleBatch['image'], singleBatch[
        'class'], singleBatch['name']

    with torch.no_grad():
        output = used_model.model(input)
        _, pred = output.topk(1, 1, True, True)
        pred = pred.t()
        correct = pred.eq(label.contiguous().view(1, -1).expand_as(pred))
        correct_k = correct[:1].reshape(-1).float().sum(0, keepdim=True)
        print(str(correct_k))
예제 #3
0
def main():

    used_model = mod.UsedModel(MODEL_TYPE,
                               arg_load_path=MODEL_PATH,
                               arg_load=True,
                               arg_load_device=TEST_DEVICE)
    used_model.model.to(TEST_DEVICE)
    used_model.model.eval()

    transform_test = trans.TRANSFORM_BLANK

    image = io.imread(Image_PATH)
    image = transform_test(image)
    image = image[None, :, :, :]
    #image = image.to(test_device)

    with torch.no_grad():
        output = used_model.model(image)
        print(output)
        print("classes ", end="")
        print(bank.classes)
        pred_val, pred = output.topk(1, 1, True, True)
        pred_val = pred_val.numpy()
        print("Choosen nr " + str(pred) + " With sertanty = " + str(pred_val))
        suggest = bank.anticlasses.get(pred.item())
        suggest_val = pred_val[0][0]
        correct = Image_Label
        correct_val = output.numpy()[0][0][bank.anticlasses.get(correct)][0]
        hit = pred.eq(bank.classes.get(str(Image_Label)))

        print("Image " + str(Image_PATH) + " recognised as " + str(suggest) +
              " zl, with " + str(suggest_val) + " certainty")

        if hit is True:
            print("This is Correct")
        else:
            print("This is Not Correct")

        print("This image should be " + "recognised as " + str(correct) +
              " zl, " + "( " + str(correct_val) + " )")
def quantMain():

    # Choose quantization engine
    if 'qnnpack' in backquant.supported_engines:
        # This Engine Works ONLY on Linux
        # We will use it
        print("Using qnnpack backend engine")
        BACKEND_ENGINE = 'qnnpack'
    elif 'fbgemm' in backquant.supported_engines:
        # This Engine works on Windows (and Linux?)
        # We won't be using it
        BACKEND_ENGINE = 'fbgemm'
        print(
            "FBGEMM Backend Engine is not supported - are you trying this on windows?"
        )
        exit(-2)
    else:
        BACKEND_ENGINE = 'none'
        print("No Proper Backend Engine found")
        exit(-3)

    # Choose quantization device (cpu/gpu)
    # Static Quantisation works only on cpu
    quantDevice = par.QUANT_DEVICE

    # Load Data
    #TODO: transforms
    transform_for_quant = trans.TRANSFORM_QUANTIZE

    dataset_loader, valset_loader, _ = bank.loadData(
        arg_load_train=True,
        arg_load_val=True,
        arg_load_test=False,
        arg_trans_train=transform_for_quant,
        quantisation_mode=True)

    #Load Our Model
    quant_model = mod.UsedModel(par.MODEL_USED_MODEL_TYPE,
                                arg_load=True,
                                arg_load_path=par.QUANT_MODEL_PATH,
                                arg_load_device=par.QUANT_DEVICE,
                                arg_load_raw=par.DATA_LOAD_RAW_MODEL_ENABLE)
    quant_model.optimizer = torch.optim.Adam(
        quant_model.model.parameters(),
        lr=par.TRAIN_INITIAl_LEARNING_RATE)  ##only if raw load
    quant_model.model.to(par.QUANT_DEVICE)
    print('Loaded trained model')

    quant_model.model.eval()
    quant_model.addQuantStubs()  #needed???? for old 1.6  way

    quant_model.fuzeModel()

    # Evaluate Our Model
    if DO_EVALUATE:
        print("Started Evaluation")
        quant_model.model.eval()
        top1, _, _ = eva.evaluate(quant_model, valset_loader, par.QUANT_DEVICE)
        print('Evaluation accuracy on all val images, %2.2f' % (top1.avg))

    propagation_list = quant.get_default_qconfig_propagation_list()
    propagation_list.remove(torch.nn.modules.linear.Linear)
    q_config_dict = dict()
    for e in propagation_list:
        q_config_dict[e] = quant.get_default_qconfig(BACKEND_ENGINE)
    quant.propagate_qconfig_(quant_model.model, q_config_dict)

    quant.prepare(quant_model.model, inplace=True)

    #Calibrate
    print("\nStarting Quantizising Imputs")
    quant_model.model.eval()
    with torch.no_grad():
        for i, data in enumerate(dataset_loader, 0):
            #if (i+1) % 2 == 0: break
            if i % 1000 == 0: print("Progress = ", i)
            inputs, labels = data['image'], data['class']
            quant_model.model(inputs)
    print("Imputs Quantized")

    #Convert to quantized model
    torch.quantization.convert(quant_model.model, inplace=True)
    print("Model Quantized")

    # Evaluate Our Model

    if DO_EVALUATE:
        print("Started Evaluation")
        quant_model.model.eval()
        top1, _, _ = eva.evaluate(quant_model, valset_loader, par.QUANT_DEVICE)
        print('Evaluation accuracy on all val images, %2.2f' % (top1.avg))

    # save for mobile
    quant_model.saveQuantizedModel(par.QUANT_SAVE_MODEL_PATH, dataset_loader)

    print("Done")
예제 #5
0
from Network.Architecture import modeltype as modtype
import Network.parameters as par
#NOT SAFE TODO
'''Bare bone tool to resave models - atm'''

model_type = modtype.ModelType.Original_Resnet18
model_path = par.DATA_PATH_PREFIX + '/' + 'Models/Quantin/Original_Resnet18_13-10-2021_07-44_Epoch_0380_Acc_89.71.pthEpoch_0240_Acc_93.68.pth'
model_save_path = model_path.rsplit('.', 1)[0] + '_rs.pth'
model_epoch = 240
model_accuracy = 93.68
model_loss = None
model_device = par.TRAIN_DEVICE

used_model = mod.UsedModel(model_type,
                           arg_load=True,
                           arg_load_raw=True,
                           arg_load_path=model_path,
                           arg_load_device=model_device)
used_model.optimizer = optim.Adam(used_model.model.parameters(),
                                  lr=par.TRAIN_INITIAl_LEARNING_RATE)

# Create Savable copy of model
saved_model_states = copy.deepcopy(used_model.model.state_dict())
saved_optim_states = copy.deepcopy(used_model.optimizer.state_dict())

# Create Save Dictionary:
model_save_dict = \
    {
        'title': "This is save file of the model of the VisonNet - the PLN banknote recognition network",
        'name': model_save_path,
        'epoch': model_epoch,
예제 #6
0
def main():
    trainStartTime = time.time()
    #Print params
    print("\nThis is network training script\n")
    printTrainParams()

    # Load Data
    print("\nLoading Data:")
    trainloader, valloader, testloader = bank.loadData(
        single_batch_test=par.DATA_SINGLE_BATCH_TEST_ENABLE)

    # Creating devices  - choosing where will training/eval calculate (gpu or cpu)
    trainDevice = torch.device(par.TRAIN_DEVICE)
    evalDevice = torch.device(par.TRAIN_DEVICE)

    #Empty GPU Cache before Training starts
    if par.TRAIN_DEVICE == 'cuda:0': torch.cuda.empty_cache()

    # Prepare Model
    print("\nPreparing Model:")
    if par.TRAIN_LOAD_MODEL_ENABLE is True:
        used_model = mod.UsedModel(par.MODEL_USED_MODEL_TYPE,
                                   arg_load=par.TRAIN_LOAD_MODEL_ENABLE,
                                   arg_load_path=par.MODEL_LOAD_MODEL_PATH,
                                   arg_load_device=par.TRAIN_DEVICE)
        used_model.model.to(trainDevice)

    else:
        used_model = mod.UsedModel(par.MODEL_USED_MODEL_TYPE,
                                   arg_pretrained=True)
        used_model.model.to(trainDevice)
        #used_model.optimizer.to(trainDevice) - breaks

    best_acc = 0
    # Training Network
    print('\nTraining Started')
    #training_start_time = time.time()
    for nEpoch in range(used_model.start_epoch, par.TRAIN_MAX_EPOCH_NUMBER):
        print('\n' + 'Epoch ' + str(nEpoch + 1) + ':')
        print("Learning rate = %1.5f" %
              used_model.scheduler.get_last_lr().pop())
        # Training Model
        used_model.model.train()
        train_one_epoch(used_model, trainloader, trainDevice, nEpoch)

        # Stepping scheduler
        used_model.scheduler.step()

        # Evaluate in some epochs:
        if (nEpoch + 1) % par.TRAIN_EVAL_PER_EPOCHS == 0:
            evaluation_time = time.time()
            used_model.model.eval()
            mtop1, mtop3, mloss = eva.evaluate(used_model, valloader,
                                               evalDevice)
            # Always Save
            used_model.saveModel(nEpoch, mtop1.avg, mloss.avg)
            if evalDevice == 'cuda:0': torch.cuda.empty_cache()
            print(
                'Evaluation on epoch %d accuracy on all validation images, %2.2f'
                % (nEpoch + 1, mtop1.avg))
            print('Top 3 on epoch %d on all validation images, %2.2f' %
                  (nEpoch + 1, mtop3.avg))
            print('Average loss on epoch %d on all validation images, %2.2f' %
                  (nEpoch + 1, mloss.avg))
            print('Evaluation on epoch %d took %.2f s' %
                  (nEpoch + 1, time.time() - evaluation_time))

        print('Epoch ' + str(nEpoch + 1) + ' completed')

    print("\nTraining concluded\n")
    #Post Training Evaluation
    used_model.model.eval()
    mtop1, mtop3, mloss = eva.evaluate(used_model, valloader, evalDevice)
    print("Evaluation on validation set")
    print('Evaluation accuracy at the end on all validation images, %2.2f' %
          mtop1.avg)
    print('Top 3 at the end on all validation images, %2.2f' % mtop3.avg)
    print('Average loss at the end on all validation images, %2.2f' %
          mloss.avg)
    print("\nEvaluation on test set")
    xtop1, xtop3, xloss = eva.evaluate(used_model, testloader, evalDevice)
    print('Evaluation accuracy on all test images, %2.2f' % xtop1.avg)
    print('Top 3 at the end on all test images, %2.2f' % xtop3.avg)
    print('Average loss on all test images, %2.2f' % xloss.avg)
    print("\nFinished Training\n")

    #Save Last
    used_model.saveModel(par.TRAIN_MAX_EPOCH_NUMBER,
                         mtop1.avg,
                         mloss.avg,
                         arg_last=True)

    print("Whole training took %.2f s" % (time.time() - trainStartTime))
    print("Bye")