コード例 #1
0
def train(args):
    """Train the model.

    # Arguments
        args: Dictionary, command line arguments."""
        

    train_generator, validation_generator, num_training, num_validation, num_classes = generate(args)
    print("{} classes found".format(num_classes))

    model = MobileNetV2((args.input_size, args.input_size, 3), num_classes, args.plot_model)

    opt = tf.keras.optimizers.Adam()
    earlystop = tf.keras.callbacks.EarlyStopping(monitor='val_acc', patience=30, verbose=1, mode='auto')
    model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['acc'])

    hist = (model,
        validation_data=validation_generator,
        steps_per_epoch=num_training // args.batch_size,
        validation_steps=num_validation // args.batch_size,
        epochs=args.epochs,
        callbacks=[earlystop])

    if not os.path.exists('model'):
        os.makedirs('model')

    df = pd.DataFrame.from_dict(hist.history)
    df.to_csv('model/hist.csv', encoding='utf-8', index=False)
    if not os.path.exists('model/output'):
        os.makedirs('model/output')
    model.save('model/output')
コード例 #2
0
def main():

    model = MobileNetV2()
    #model = VGG(depth=16, init_weights=True, cfg=None)
    model = dataParallel_converter(model, "./model_reweighted/epochFinish.pt")
    #model = dataParallel_converter(model, "./model/cifar100_mobilenet_v2_exp3_acc_81.920_adam.pt")
    #model = dataParallel_converter(model, "./model_retrained/cifar10_vgg16_retrained_acc_91.310_config_vgg16_filter.pt")
    #model = dataParallel_converter(model, "./model_retrained/cifar100_mobilenetv217_retrained_acc_79.780_config_mobile_v2_0.7_threshold.pt")


    for name, weight in model.named_parameters():
        if (len(weight.size()) == 4):
            print(name, weight)
    print("\n------------------------------\n")

    test(model, device, test_loader)

    test_column_sparsity(model)
    # test_filter_sparsity(model)

    # plot_heatmap(model)
    plot_distribution(model)

    current_lr = 0.01
    rew_milestone = [100, 200, 300, 400]
    xx = np.linspace(1, 400, 400)
    yy = []
    for x in xx:
        if x - 1 in rew_milestone:
            current_lr *= 1.8
        else:
            current_lr *= 0.988
        yy.append(current_lr)
    plt.plot(xx, yy)
コード例 #3
0
def main():
    #   model = VGG(depth=16, init_weights=True, cfg=None)
    model = MobileNetV2()
    model = dataParallel_converter(
        model,
        "./cifar100_mobilenetv217_retrained_acc_80.510mobilenetv217_quantized_acc_80.000_config_vgg16_threshold.pt"
    )

    input_size = 32
    input_shape = (1, 3, input_size, input_size)

    all_ops = read_model(model, input_shape)
    print('\n'.join(map(str, all_ops)))

    counter = counting.MicroNetCounter(all_ops,
                                       add_bits_base=32,
                                       mul_bits_base=32)

    INPUT_BITS = 32
    ACCUMULATOR_BITS = 32
    PARAMETER_BITS = 32
    SUMMARIZE_BLOCKS = True

    counter.print_summary(0,
                          PARAMETER_BITS,
                          ACCUMULATOR_BITS,
                          INPUT_BITS,
                          summarize_blocks=SUMMARIZE_BLOCKS)

    counter.print_summary(0.5,
                          PARAMETER_BITS,
                          ACCUMULATOR_BITS,
                          INPUT_BITS,
                          summarize_blocks=SUMMARIZE_BLOCKS)
コード例 #4
0
def load_hub_weights(models):
    for alpha, rows in models:

        tf.reset_default_graph()
        print('alpha: ', alpha, 'rows: ', rows)

        WEIGHTS_SAVE_PATH_INCLUDE_TOP = '/home/jon/Documents/keras_mobilenetV2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' + str(alpha) + '_' + str(rows) + '.h5'

        WEIGHTS_SAVE_PATH_NO_TOP = '/home/jon/Documents/keras_mobilenetV2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' + \
            str(alpha) + '_' + str(rows) + '_no_top' + '.h5'

        # Load tf stuff
        img = nets.utils.load_img('cat.png', target_size=256, crop_size=rows)
        img = (img / 128.0) - 1.0
        inputs = tf.placeholder(tf.float32, [None, rows, rows, 3])

        model = hub.Module(
            "https://tfhub.dev/google/imagenet/mobilenet_v2_"
            + map_alpha_to_slim(alpha) + "_"
            + str(rows) + "/classification/1")

        h, w = hub.get_expected_image_size(model)

        features = model(inputs, signature="image_classification", as_dict=True)
        probs = tf.nn.softmax(features['default'])

        # Load local model
        with tf.variable_scope('keras'):
            model2 = MobileNetV2(weights=None, 
                                 alpha = alpha, 
                                 input_shape=(rows, rows, 3))
            model2.load_weights('./old_weights_nonhub/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' + str(alpha) +  '_' +str(rows) + '.h5')
        
        preds1 = model2.predict(img)
        print('preds1: (remote weights) new BN no set w:: ',
              nets.utils.decode_predictions(preds1))

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            weights = tf.get_collection(
                tf.GraphKeys.GLOBAL_VARIABLES, scope='module/MobilenetV2')
            values = sess.run(weights)
            values[-2] = np.delete(np.squeeze(values[-2]), 0, axis=-1)
            values[-1] = np.delete(values[-1], 0, axis=-1)
            sess.close()

        # Save weights no top and model
        model2.set_weights(values)
        model2.save_weights(WEIGHTS_SAVE_PATH_INCLUDE_TOP)
        model2_no_top = Model(input = model2.input, output = model2.get_layer('out_relu').output)
        model2_no_top.save_weights(WEIGHTS_SAVE_PATH_NO_TOP)

        # Predictions with new BN, new weights
        preds2 = model2.predict(img)

        print('preds2: (after set weights) ',
              nets.utils.decode_predictions(preds2))
コード例 #5
0
    def __init__(self):
        super(ThunderNet, self).__init__()
        self.backbone = MobileNetV2(frozen_stages=-1,
                                    width_mult=1.0,
                                    norm_eval=False,
                                    out_indices=(2, 3))

        self.neck = CEM(
            in_channels=[96, 320],
            out_channels=245,
        )
        self.rpn_head = LightRPNHead(245, 256)
コード例 #6
0
ファイル: model2.py プロジェクト: lightforever/aione
 def __init__(self,
              input_size,
              action_size,
              seed,
              cnn_out_size=1280,
              fc1_size=128):
     super(Critic, self).__init__()
     self.seed = torch.manual_seed(seed)
     self.cnn = MobileNetV2(input_size=input_size)
     self.cnn.load_state_dict(
         torch.load(open('mobilenet_v2.pth.tar', 'rb'),
                    map_location=map_location))
     self.fc1 = nn.Linear(cnn_out_size + action_size, fc1_size)
     self.fc2 = nn.Linear(fc1_size, 1)
     self.reset_parameters()
コード例 #7
0
    def __init__(self, loadweights=True, downsample=4, model_path='pretrained_model/mobilenetv2_1.0-0c6065bc.pth'):
        super(mobilenetv2_base, self).__init__()

        model = MobileNetV2(width_mult=1.0)

        if loadweights:
            model.load_state_dict(torch.load(model_path))

        #if downsample == 4:
        #    self.feature = nn.Sequential(model.features[:14])
        #elif downsample == 5:
        #    self.feature = nn.Sequential(model.features)

        self.feature3 = nn.Sequential(model.features[:7])
        self.feature4 = nn.Sequential(model.features[7:14])
        self.feature5 = nn.Sequential(model.features[14:])
コード例 #8
0
    def __init__(self, count, memory_size):
        super(CifarClassifier, self).__init__()
        self.memory = Memory(
            hidden_size=memory_size * 2,
            memory_size=memory_size,
            glimpse_size=32,
            g_down=1280,
            c_down=2304,
            context_net=ContextNet(),
            glimpse_net=MobileNetV2()
        )

        self.count = count
        self.drop = nn.Dropout(0.5)
        self.qdown = nn.Linear(2304, memory_size)
        self.classifier = nn.Linear(memory_size, 10)
        self.soft = nn.LogSoftmax(dim=1)
コード例 #9
0
def get_vector(image_name):
    weight_path = 'weight/mobilenet_v2.pth.tar'

    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    image = Image.open(image_name)

    model = MobileNetV2()
    model.load_state_dict(torch.load(weight_path, map_location=lambda storage, loc: storage))
    
    print(model)

    feature_net = model.features.to(device)
    
    feature_net_5 = feature_net[:-14]
    feature_net_10 = feature_net[:-9]
    feature_net_15 = feature_net[:-4]
    
    model.eval()

    transforms = T.Compose([
        T.Resize((224,224)),
        T.ToTensor(),
        T.Normalize(mean=[0.5, 0.5, 0.5], std=[0.229, 0.224, 0.225])
        ])
    
    image_tensor = transforms(image).unsqueeze(0)
    
    image_tensor = image_tensor.to(device)
    
    output_5 = feature_net_5(image_tensor)
    output_5 = output_5.view(output_5.size(0), 32*28*28)
    output_5 = nn.Linear(32*28*28, 1024)(output_5)

    output_10 = feature_net_10(image_tensor)
    output_10 = output_10.view(output_10.size(0), 64*14*14)
    output_10 = nn.Linear(64*14*14, 1024)(output_10)

    output_15 = feature_net_15(image_tensor)
    output_15 = output_15.view(output_15.size(0), 160*7*7)
    output_15 = nn.Linear(160*7*7, 1024)(output_15)

    output = (output_5 + output_10 + output_15) / 3 
    print(output.shape)
コード例 #10
0
ファイル: generate_mask.py プロジェクト: Roll920/CURL
def main():
    global args, best_prec1
    args = parser.parse_args()

    # create model
    model_weight = torch.load(FLAGS['pretrain_model'])  # 78.77%
    model = MobileNetV2(model_weight, num_classes=FLAGS['class_num'])
    model = model.cuda()
    cudnn.benchmark = True
    test_accuracy(model, FLAGS)

    # Data loading code from lmdb
    data_transforms = {
        'train':
        transforms.Compose([
            transforms.RandomResizedCrop(224),
            # transforms.Resize(256),
            # transforms.RandomCrop((224, 224)),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
        'val':
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
    }

    print("| Preparing model...")
    dset_train = MyDataSet(data_transforms['val'], FLAGS['proxy_dataset_size'])
    train_loader = torch.utils.data.DataLoader(
        dset_train,
        batch_size=FLAGS['proxy_dataset_size'],
        shuffle=False,
        num_workers=8,
        pin_memory=True)
    print('data_loader_success!')

    # evaluate and train
    validate(train_loader, model)
コード例 #11
0
def run_training(config,
                 n_classes,
                 train_loader,
                 valid_loader,
                 width=1,
                 mb_version=1):
    """
    Whole training procedure with fine-tune after regular training
    """
    # defining model
    if width > 1:
        model = tvm.resnet18(num_classes=n_classes)
    else:
        if mb_version == 1:
            model = MobileNet(n_classes=n_classes, width_mult=width)
        else:
            model = MobileNetV2(n_classes=n_classes, width_mult=width)
    model = model.to(config['device'])

    # print out number of parameters
    num_params = 0
    for p in model.parameters():
        num_params += np.prod(p.size())
    print(f"width={width}, num_params {num_params}")

    # defining loss criterion, optimizer and learning rate scheduler
    criterion = t.nn.CrossEntropyLoss()
    opt = t.optim.Adam(model.parameters(), config['lr'])
    sched = t.optim.lr_scheduler.MultiStepLR(opt, [3, 6])

    # training process with Adam
    tr_loss, tr_accuracy, valid_loss, valid_accuracy = train(
        config, model, train_loader, valid_loader, criterion, opt, sched)
    # training process with SGDR
    opt = t.optim.SGD(model.parameters(), config['lr'] / 10, momentum=0.9)
    sched = SGDR(opt, 3, 1.2)
    tr_loss_finetune, tr_accuracy_finetune, valid_loss_finetune, valid_accuracy_finetune = train(
        config, model, train_loader, valid_loader, criterion, opt, sched)
    return [
        tr_loss + tr_loss_finetune, tr_accuracy + tr_accuracy_finetune,
        valid_loss + valid_loss_finetune,
        valid_accuracy + valid_accuracy_finetune
    ]
コード例 #12
0
def predict_keras(img, alpha, rows):
    """
    params: img: an input image with shape (1, 224, 224, 3)
            note: Image has been preprocessed (x /= 127.5 - 1)
    Runs forward pass on network and returns logits and the inference time
    """
    input_tensor = Input(shape=(rows, rows, 3))

    # Note: you could also provide an input_shape
    model = MobileNetV2(input_tensor=input_tensor,
                        include_top=True,
                        weights='imagenet',
                        alpha=alpha)

    tic = time.time()
    y_pred = model.predict(img.astype(np.float32))
    y_pred = y_pred[0].ravel()
    toc = time.time()
    return y_pred, toc - tic
コード例 #13
0
def main():

    parser = argparse.ArgumentParser()
    # Required arguments.
    parser.add_argument(
        "--train_file",
        default="./tfrecord/train.tfrecord",
        help="path to train.tfrecord file")
    parser.add_argument(
        "--valid_file",
        default="./tfrecord/val.tfrecord",
        help="path to validation file")

    args = parser.parse_args()

    myModel=MobileNetV2((224, 224, 3), 1, loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(), metrics=['accuracy'])

    createdModel=myModel.create_model()

    print("MobileNetV2 summary")
    print(createdModel.summary())

    trained_model_dir = os.path.join(os.getcwd(), "mobV2")
    os.makedirs(trained_model_dir, exist_ok=True)
    print("trained_model_dir: ", trained_model_dir)
    input_name = createdModel.input_names[0]
    #with CustomObjectScope({'relu6': relu6}):
    #    createdModel= createdModel
    #    createdModel.compile(optimizer=optimizer, loss=tf.keras.losses.BinaryCrossentropy(),  metrics=metrics)
    #mobilenet_estimator = tf.keras.estimator.model_to_estimator(keras_model=keras_mobilenet)
    mobilenetModel = tf.keras.estimator.model_to_estimator(keras_model=createdModel, model_dir=trained_model_dir)



    train_spec = tf.estimator.TrainSpec(input_fn=lambda: imgs_input_fn(args.train_file ,perform_shuffle=True,repeat_count=5,batch_size=20), max_steps=500)
    eval_spec = tf.estimator.EvalSpec(input_fn=lambda: imgs_input_fn(args.valid_file, perform_shuffle=False, batch_size=1))
    tf.estimator.train_and_evaluate(mobilenetModel, train_spec, eval_spec)
コード例 #14
0
def train():
    height = args.height
    width = args.width
    _step = 0

    if True:
        #glob_pattern = os.path.join(args.dataset_dir,"*_train.tfrecord")
        #tfrecords_list = glob.glob(glob_pattern)
        #filename_queue = tf.train.string_input_producer(tfrecords_list, num_epochs=None)
        train_img_batch, train_label_batch = get_batch(
            "gesture/gesture_train.tfrecord",
            args.batch_size,
            shuffle=True,
            is_train=True,
            num_classes=args.num_classes)

        test_img_batch, test_label_batch = get_batch(
            "gesture/gesture_validation.tfrecord",
            args.batch_size,
            shuffle=False,
            is_train=False,
            num_classes=args.num_classes)

        input_x = tf.placeholder(tf.float32,
                                 [None, args.height, args.width, 3],
                                 name="input-x")
        input_y = tf.placeholder(tf.uint8, [None, args.num_classes],
                                 name="input-y")

        mobilenet = MobileNetV2(input_x, num_classes=args.num_classes)

        logits = mobilenet.logits
        pred = mobilenet.predictions

        cross = tf.nn.softmax_cross_entropy_with_logits(labels=input_y,
                                                        logits=logits)
        loss = tf.reduce_mean(cross)

        # L2 regularization
        list_reg = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
        if len(list_reg) > 0:
            l2_loss = tf.add_n(list_reg)
            total_loss = loss + l2_loss
        else:
            total_loss = loss

        # evaluate model, for classification
        preds = tf.argmax(pred, 1)
        labels = tf.argmax(input_y, 1)
        #correct_pred = tf.equal(tf.argmax(pred, 1), tf.cast(label_batch, tf.int64))
        correct_pred = tf.equal(preds, labels)
        acc = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

        # learning rate decay
        base_lr = tf.constant(args.learning_rate)
        global_step = tf.Variable(0)
        lr = tf.train.exponential_decay(args.learning_rate,
                                        global_step=global_step,
                                        decay_steps=args.lr_decay_step,
                                        decay_rate=args.lr_decay,
                                        staircase=True)

        # optimizer
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        with tf.control_dependencies(update_ops):
            train_op = tf.train.AdamOptimizer(learning_rate=lr,
                                              beta1=args.beta1).minimize(
                                                  loss,
                                                  global_step=global_step)
            #train_op = tf.train.RMSPropOptimizer(learning_rate=lr, decay=0.00004,momentum=0.999).minimize(loss,global_step=global_step)

        max_steps = int(args.num_samples / int(args.batch_size) *
                        int(args.epoch))

        # summary
        tf.summary.scalar('total_loss', total_loss)
        tf.summary.scalar('accuracy', acc)
        tf.summary.scalar('learning_rate', lr)
        summary_op = tf.summary.merge_all()

        _step = 0

        with tf.Session() as sess:

            # summary writer
            writer = tf.summary.FileWriter(args.logs_dir, sess.graph)

            print("the start run init")
            sess.run(tf.global_variables_initializer())
            print("the end  run init")

            saver = tf.train.Saver()
            _, _step = load(sess, saver, args.checkpoint_dir)

            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)

            #images = sess.run(img_batch)
            #print(images.shape)

            for step in range(_step + 1, max_steps + 1):
                start_time = time.time()
                images, label = sess.run([train_img_batch, train_label_batch])
                _, _lr = sess.run([train_op, lr],
                                  feed_dict={
                                      input_x: images,
                                      input_y: label
                                  })
                #print("the loss is %f"%lr)
                if step % args.num_log == 0:
                    summ, _loss, _acc = sess.run([summary_op, total_loss, acc],
                                                 feed_dict={
                                                     input_x: images,
                                                     input_y: label
                                                 })
                    writer.add_summary(summ, step)
                    print(
                        'global_step:{0}, time:{1:.3f}, lr:{2:.8f}, acc:{3:.6f}, loss:{4:.6f}'
                        .format(step,
                                time.time() - start_time, _lr, _acc, _loss))

                if step % args.num_log == 0:
                    save_path = saver.save(sess,
                                           os.path.join(
                                               args.checkpoint_dir,
                                               args.model_name),
                                           global_step=step)

                if step % 500 == 0:  #will to eval model
                    start_time = time.time()
                    totalloss = 0.0
                    totalacc = 0.0
                    for e_step in range(300):
                        t_images, t_labels = sess.run(
                            [test_img_batch, test_label_batch])
                        _loss, _acc = sess.run([total_loss, acc],
                                               feed_dict={
                                                   input_x: t_images,
                                                   input_y: t_labels
                                               })
                        totalloss = totalloss + _loss
                        totalacc = totalacc + _acc
                    print('number to eval:%g, time:%g, acc:%g, loss:%g' %
                          ((e_step + 1) * args.batch_size,
                           time.time() - start_time, totalacc /
                           (e_step + 1), totalloss / (e_step + 1)))

            tf.train.write_graph(sess.graph_def, args.checkpoint_dir,
                                 args.model_name + '.pb')
            save_path = saver.save(sess,
                                   os.path.join(args.checkpoint_dir,
                                                args.model_name),
                                   global_step=max_steps)

            coord.request_stop()
            coord.join(threads)
コード例 #15
0
ファイル: main.py プロジェクト: Roll920/CURL
def main():
    global args, best_prec1
    args = parser.parse_args()

    # create model
    model_weight = torch.load(FLAGS['pretrain_model'])  # 77.58%
    model = MobileNetV2(model_weight, num_classes=FLAGS['class_num'])
    model.eval()
    summary(model, torch.zeros((1, 3, 224, 224)))
    model = model.cuda()
    cudnn.benchmark = True

    model_teacher = OriMobileNetV2(num_classes=FLAGS['class_num'],
                                   width_mult=1.)
    model_teacher.load_state_dict(model_weight)
    model_teacher.cuda()

    # define loss function (criterion) and optimizer
    criterion = nn.CrossEntropyLoss().cuda()

    optimizer = torch.optim.SGD(filter(lambda p: p.requires_grad,
                                       model.parameters()),
                                args.lr,
                                momentum=args.momentum,
                                weight_decay=args.weight_decay)

    # Data loading code from lmdb
    data_transforms = {
        'train':
        transforms.Compose([
            transforms.RandomResizedCrop(224),
            # transforms.Resize(256),
            # transforms.RandomCrop((224, 224)),
            transforms.RandomHorizontalFlip(),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
        'val':
        transforms.Compose([
            transforms.Resize(256),
            transforms.CenterCrop(224),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ]),
    }

    data_dir = FLAGS['data_base']
    print("| Preparing model...")
    dsets = {
        x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x])
        for x in ['train', 'val']
    }
    train_loader = torch.utils.data.DataLoader(dsets['train'],
                                               batch_size=FLAGS['batch_size'],
                                               shuffle=True,
                                               num_workers=8,
                                               pin_memory=True)
    val_loader = torch.utils.data.DataLoader(dsets['val'],
                                             batch_size=4 *
                                             FLAGS['batch_size'],
                                             shuffle=False,
                                             num_workers=8,
                                             pin_memory=True)
    print('data_loader_success!')

    # evaluate and train
    validate(val_loader, model, criterion)
    for epoch in range(args.start_epoch, args.epochs):

        # train for one epoch
        train(train_loader, model_teacher, model, criterion, optimizer, epoch)

        # evaluate on validation set
        prec1 = validate(val_loader, model, criterion)

        # remember best prec@1 and save checkpoint
        best_prec1 = max(prec1, best_prec1)
        folder_path = 'checkpoint/fine_tune'
        if not os.path.exists(folder_path):
            os.makedirs(folder_path)
        torch.save(model.state_dict(), folder_path + '/model.pth')
        print('best acc is %.3f' % best_prec1)
コード例 #16
0
#coding:utf-8
import numpy as np
import os
import glob
import cv2
import shutil
import time
import torch
import torch.nn as nn

from mobilenetv2 import MobileNetV2


# 获取模型实例
model = MobileNetV2()
model.classifier = nn.Sequential(nn.Linear(1280, 8), nn.Sigmoid())
#model.load_state_dict(torch.load("latest.pt"))

img_size = 224
# 生成一个样本供网络前向传播 forward()
example = torch.rand(1, 3, img_size, img_size)

# 使用 torch.jit.trace 生成 torch.jit.ScriptModule 来跟踪
traced_script_module = torch.jit.trace(model, example)

#test_path = "/home/lishundong/Desktop/torch_project/pytorch-regress/data/"
#img_list = glob.glob(test_path + "*.jpg")[:1000]
img_list = ["test.jpg"]

s = time.time()
for i in img_list:
コード例 #17
0
                                        train=False,
                                        download=True,
                                        transform=transform_test)
test_loader = torch.utils.data.DataLoader(test_set,
                                          batch_size=batch_size,
                                          shuffle=False,
                                          num_workers=1)
reference_memristor = memtorch.bh.memristor.VTEAM
r_on = 14000
r_off = 300000
reference_memristor_params = {
    'time_series_resolution': 1e-10,
    'r_off': r_off,
    'r_on': r_on
}
model = MobileNetV2().to(device)
model.load_state_dict(torch.load('trained_model.pt'), strict=False)
model.eval()
patched_model = patch_model(
    model,
    memristor_model=reference_memristor,
    memristor_model_params=reference_memristor_params,
    module_parameters_to_patch=[torch.nn.Linear, torch.nn.Conv2d],
    mapping_routine=naive_map,
    transistor=True,
    programming_routine=None,
    scheme=memtorch.bh.Scheme.DoubleColumn,
    tile_shape=(128, 128),
    max_input_voltage=0.3,
    ADC_resolution=8,
    ADC_overflow_rate=0.,
コード例 #18
0
    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count


def accuracy(output, target, topk=(1, )):
    """Computes the precision@k for the specified values of k"""
    maxk = max(topk)
    batch_size = target.size(0)

    _, pred = output.topk(maxk, 1, True, True)
    pred = pred.t()
    correct = pred.eq(target.view(1, -1).expand_as(pred))

    res = []
    for k in topk:
        correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
        res.append(correct_k.mul_(100.0 / batch_size))
    return res


if __name__ == '__main__':
    from mobilenetv2 import MobileNetV2
    model_weight = torch.load(
        '/opt/luojh/pretrained_models/cub200/mobilenet_v2.pth')  # 77.58%
    model = MobileNetV2(model_weight, num_classes=200)
    model = model.cuda()
    test_accuracy(model)
コード例 #19
0
def load_weights(models):

    for alpha, rows in models:

        WEIGHTS_SAVE_PATH_INCLUDE_TOP = '/home/jon/Documents/keras_mobilenetV2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' + str(
            alpha) + '_' + str(rows) + '.h5'
        WEIGHTS_SAVE_PATH_NO_TOP = '/home/jon/Documents/keras_mobilenetV2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_' + str(
            alpha) + '_' + str(rows) + '_no_top' + '.h5'

        with open('layer_guide' + str(alpha) + str(rows) + '.p',
                  'rb') as pickle_file:
            layer_guide = pickle.load(pickle_file)

        # layers 0-4 are first conv
        # layers 4 - 16 are expanded

        # load mobilenet
        input_tensor = Input(shape=(rows, rows, 3))
        model = MobileNetV2(input_tensor=input_tensor,
                            include_top=True,
                            alpha=alpha,
                            weights=None)

        not_expanded_layers = [
            'bn_Conv1', 'Conv_1_bn', 'Conv1', 'Logits', 'bn_Conv1', 'Logits',
            'Conv_1', 'Conv_1_bn', 'Conv_1_bn', 'bn_Conv1', 'bn_Conv1',
            'Conv_1_bn'
        ]

        def find_not_exp_conv_layer_match(keras_layer, kind=None):
            """
			This function takes in layer parameters and finds the associated conv layer from the weights layer guide
			Returns the weights layer guide object , found boolean
			"""
            for lobj in layer_guide:
                if keras_layer.name == lobj['layer']:
                    if keras_layer.weights[0]._keras_shape == lobj['shape']:
                        return lobj, True
                    else:
                        return {}, False

        def find_not_exp_bn_layers_match(keras_layer, kind=None):
            """
			This function takes in layer parameters and finds the associated conv layer from the weights layer guide
			Returns the weights layer guide object , found boolean
			"""
            meta_full = 4
            metas_found = 0

            for lobj in layer_guide:
                if keras_layer.name == lobj['layer']:
                    if lobj['meta'] == 'gamma' and keras_layer.weights[
                            0]._keras_shape == lobj['shape']:
                        gamma = lobj
                        metas_found += 1
                    elif lobj['meta'] == 'beta' and keras_layer.weights[
                            1]._keras_shape == lobj['shape']:
                        beta = lobj
                        metas_found += 1
                    elif lobj['meta'] == 'moving_mean' and keras_layer.weights[
                            2]._keras_shape == lobj['shape']:
                        moving_mean = lobj
                        metas_found += 1
                    elif lobj[
                            'meta'] == 'moving_variance' and keras_layer.weights[
                                3]._keras_shape == lobj['shape']:
                        moving_variance = lobj
                        metas_found += 1

            if metas_found == meta_full:
                return [gamma, beta, moving_mean, moving_variance], True
            else:
                return [], False

        def find_not_exp_dense_layers_match(keras_layer, kind=None):
            meta_full = 2
            metas_found = 0
            for lobj in layer_guide:
                if keras_layer.name == lobj['layer']:
                    if lobj['meta'] == 'weights':
                        weights = lobj
                        metas_found += 1
                    elif lobj['meta'] == 'biases':
                        bias = lobj
                        metas_found += 1
            if metas_found == meta_full:
                return [weights, bias], True
            else:
                return [], False

        def find_conv_layer_match(keras_layer,
                                  block_id=None,
                                  mod=None,
                                  kind=None):
            """
			This function takes in layer parameters and finds the associated conv layer from the weights layer guide
			Returns the weights layer guide object , found boolean
			"""
            for lobj in layer_guide:
                if int(lobj['block_id']) == int(block_id):
                    if lobj['layer'] == kind:
                        if lobj['mod'] == mod:
                            if keras_layer.weights[0]._keras_shape == lobj[
                                    'shape']:
                                return lobj, True
                            else:
                                return {}, False

        def find_bn_layers_match(keras_layer,
                                 block_id=None,
                                 mod=None,
                                 kind=None):
            """
			This function takes in layer parameters and returns a list of the four batch norm parameters as well as a boolean indicating success
			"""
            meta_full = 4
            metas_found = 0

            for lobj in layer_guide:
                if int(lobj['block_id']) == int(block_id):
                    if lobj['layer'] == kind:
                        if lobj['mod'] == mod:
                            if lobj['meta'] == 'gamma' and keras_layer.weights[
                                    0]._keras_shape == lobj['shape']:
                                gamma = lobj
                                metas_found += 1
                            elif lobj['meta'] == 'beta' and keras_layer.weights[
                                    1]._keras_shape == lobj['shape']:
                                beta = lobj
                                metas_found += 1
                            elif lobj[
                                    'meta'] == 'moving_mean' and keras_layer.weights[
                                        2]._keras_shape == lobj['shape']:
                                moving_mean = lobj
                                metas_found += 1
                            elif lobj[
                                    'meta'] == 'moving_variance' and keras_layer.weights[
                                        3]._keras_shape == lobj['shape']:
                                moving_variance = lobj
                                metas_found += 1
            if metas_found == meta_full:
                return [gamma, beta, moving_mean, moving_variance], True
            else:
                return [], False

        # Calculate loaded number
        set_weights = 0
        for keras_layer in model.layers:
            name = keras_layer.name.split('_')
            # If it not an expandable layer
            if keras_layer.name in not_expanded_layers:
                print('keras_layer : ', keras_layer.name, ' Is not expandable')
                if isinstance(keras_layer, BatchNorm):
                    bn_layers, isfound = find_not_exp_bn_layers_match(
                        keras_layer=keras_layer, kind='BatchNorm')
                    if isfound:
                        arrs = [np.load(lobj['file']) for lobj in bn_layers]
                        keras_layer.set_weights(arrs)
                        set_weights += 1  # can add ()
                    else:
                        print(
                            'possible error not match found if isinstance BatchNorm and not expandable'
                        )

                elif isinstance(keras_layer, DepthwiseConv2D):
                    lobj, isfound = find_not_exp_conv_layer_match(
                        keras_layer=keras_layer, kind='DepthwiseConv2D')
                    if isfound:
                        if lobj['meta'] == 'weights':
                            arr = np.load(lobj['file'])
                            keras_layer.set_weights([arr])
                            set_weights += 1
                    else:
                        print(
                            ' You probably wont see this but just in case possible error finding weights for not expandable DepthwiseConv2D: '
                        )
                elif isinstance(keras_layer, Conv2D):
                    # get mods

                    lobj, isfound = find_not_exp_conv_layer_match(
                        keras_layer=keras_layer, kind='Conv2D')
                    if isfound:
                        if lobj['meta'] == 'weights':
                            arr = np.load(lobj['file'])
                            keras_layer.set_weights([arr])
                            set_weights += 1
                    else:
                        print(
                            'possible error finding weights for not expandable Conv2D: '
                        )
                        # if layer['meta'] == 'biases'
                elif isinstance(keras_layer, Dense):
                    dense_layers, isfound = find_not_exp_dense_layers_match(
                        keras_layer=keras_layer, kind='Dense')
                    if isfound:
                        weights = np.load(dense_layers[0]['file'])
                        bias = np.load(dense_layers[1]['file'])

                        # squeeze
                        weights = np.squeeze(weights)

                        # Remove background class
                        weights = weights[:, 1:]
                        bias = bias[1:]

                        keras_layer.set_weights([weights, bias])
                        set_weights += 1
                    else:
                        print(
                            'possible error with dense layer in not expandable'
                        )

            else:
                if isinstance(keras_layer, BatchNorm):
                    _, _, num, _, mod = name
                    bn_layers, isfound = find_bn_layers_match(
                        keras_layer=keras_layer,
                        block_id=num,
                        mod=mod,
                        kind='BatchNorm')
                    # note: BatchNorm layers have 4 weights
                    # This will return a list of the 4 objects corresponding to the 4 weights in the right order
                    # Then go through the list and grab each numpy array, make sure it is wrapped inside a standard list
                    if isfound:
                        arrs = [np.load(lobj['file']) for lobj in bn_layers]
                        keras_layer.set_weights(arrs)
                        set_weights += 1
                    else:
                        print(
                            'possible error not match found on expandable batchNorm layers'
                        )
                        print('=== dump: ')
                        print('keras_layer: ', keras_layer.name)

                # Check for DepthwiseConv2D
                elif isinstance(keras_layer, DepthwiseConv2D):
                    _, _, num, mod = name
                    # check layer meta to be depthwise_weights
                    layer, isfound = find_conv_layer_match(
                        keras_layer=keras_layer,
                        block_id=num,
                        mod=mod,
                        kind='DepthwiseConv2D')
                    if isfound:
                        if layer['meta'] == 'depthwise_weights':
                            arr = np.load(layer['file'])
                            keras_layer.set_weights([arr])
                            set_weights += 1
                    else:
                        print(
                            'possible error not match found for DepthwiseConv2D in not expandable'
                        )

                # Check for Conv2D
                elif isinstance(keras_layer, Conv2D):
                    _, _, num, mod = name
                    layer, isfound = find_conv_layer_match(
                        keras_layer=keras_layer,
                        block_id=num,
                        mod=mod,
                        kind='Conv2D')
                    if isfound:
                        if layer['meta'] == 'weights':
                            arr = np.load(layer['file'])
                            keras_layer.set_weights([arr])
                    else:
                        print('possible error not match found')

        # Set first block

        # Organize batch norm layers into [gamma, beta, moving_mean, moving_std]
        # Organize expand conv block layers into [expand, depthwise, project]
        print('set_weights: ', set_weights)
        model.save_weights(WEIGHTS_SAVE_PATH_INCLUDE_TOP)

        # Save no top model
        out_relu = model.get_layer('out_relu').output
        from keras.models import Model
        model_no_top = Model(input=input_tensor, output=out_relu)
        model_no_top.save(WEIGHTS_SAVE_PATH_NO_TOP)

        trainable_layers = [l.weights for l in model.layers]
        print('len trainable: ', len(trainable_layers))

        # TODO test predict with detect api
        print('model: ', model)
        print('set_weights: ', set_weights)
        assert (set_weights == 72)
        print('alpha: ', alpha)
        print('rows: ', rows)
コード例 #20
0
def test_model(model_name):
    import cv2
    import torch
    from mobilenetv2 import MobileNetV2
    checkpoint_path = '/data1/exp/ctdet/default/model_best.pth'
    image = cv2.imread('540e3f90874dfa66.jpg')
    # image = np.random.randn(112, 112, 3)*255
    # image = image.astype(np.uint8)
    input = cv2.cvtColor(image.copy(), cv2.COLOR_BGR2RGB)
    # input = image.copy()[...,::-1]
    input = cv2.resize(input, (512, 512))
    # debug
    # input = input[:8, :8, :]
    input = input.astype(np.float32) / 256.0
    input = np.expand_dims(input, 0)
    torch_input = input.copy().transpose((0, 3, 1, 2))
    tensor_input = torch.from_numpy(torch_input)
    input_ = input.copy()
    heads = {'hm': 6, 'wh': 2, 'reg': 2}
    num_layers = 34
    model3 = MobileNetV2(heads, head_conv=64)
    data_dict = torch.load(checkpoint_path)['state_dict']
    # data_dict.pop('bn1.weight')
    # data_dict.pop('bn1.bias')
    model3.load_state_dict(data_dict)
    model3.train(False)

    pytorch_result = model3(tensor_input)

    net = caffe.Net('./%s.prototxt' % model_name,
                    './%s.caffemodel' % model_name, caffe.TEST)
    input_ = input.transpose((0, 3, 1, 2))

    net.blobs['data'].data[...] = input_
    output_ = net.forward()
    # output_ = net.forward(end='deconv1')  # 获取指定层的输出
    # print(output_)
    keys = list(output_.keys())
    print(output_[keys[0]].shape)
    caffe_output = output_[keys[0]]

    def cal_MPA(caffe_output, cmp_output):
        try:
            error = np.abs(caffe_output - cmp_output)
        except:
            cmp_output = cmp_output.transpose((0, 3, 1, 2))
            error = np.abs(caffe_output - cmp_output)
        zeros = np.zeros_like(error)
        error = np.where(np.less(error, 1e-5), zeros, error)
        print('error: ', np.sum(error))
        MPA = np.max(error) / np.max(np.abs(cmp_output)) * 100.
        print('MPA: %f' % MPA)

    # cmp_output = pytorch_result.cpu().detach().numpy()
    # cal_MPA(caffe_output, cmp_output)
    for k, val in output_.items():
        cmp_output = pytorch_result[0][k].cpu().detach().numpy()
        cal_MPA(val, cmp_output)

    # bin_file = '/data2/SharedVMs/nfs_sync/model_speed_test/mobileResult.bin'
    # hisi_result = np.fromfile(bin_file, dtype=np.float32)
    # hisi_result = np.reshape(hisi_result, [1, 196])
    # cal_MPA(caffe_output, hisi_result)
    #
    # caffe_output.astype(dtype=np.float32)
    # caffe_output.tofile('./data/caffe_varify_output.bin')

    print('Done.')