示例#1
0
def GetAppropriateModel(arch, dataset, dumpData=False):
    if arch == "resnet18":
        if dataset == "cifar10" :
            print("Going for cifar10")
            return  resnet18(32, num_classes=10)
        elif dataset == "cifar100":
            print("Going for cifar100")
            return  resnet18(32, num_classes=100)
        
    elif arch == "resnet152":
        if dataset == "cifar10":
            print("Going for resnet152 cifar10")
            return resnet152(num_classes=10)
        elif dataset == "cifar100":
            print("Going for resnet152 cifar100")
            return resnet152(num_classes=100)
        
    elif arch == "resnet50":
        if dataset == "cifar10":
            print("Going for cifar10 resnet50")
            print("DumpData ",dumpData)
            return resnet50( num_classes=10)
        elif dataset == "cifar100":
            print("Going for cifar100 resnet50")
            return resnet50(num_classes=100)    
示例#2
0
def main():
    model = resnet18()
    model.build(input_shape=(None, 32, 32, 3))
    model.summary()
    optimizer = optimizers.Adam(lr=1e-3)
    for epoch in range(50):
        for step, (x, y) in enumerate(train_data):
            with tf.GradientTape() as tape:
                logits = model(x)
                y_onehot = tf.one_hot(y, depth=10)
                loss = tf.losses.categorical_crossentropy(y_onehot,
                                                          logits,
                                                          from_logits=True)
                loss = tf.reduce_mean(loss)
            grads = tape.gradient(loss, model.trainable_variables)
            optimizer.apply_gradients(zip(grads, model.trainable_variables))
            if step % 100 == 0:
                print(epoch, step, 'loss', float(loss))
        total_num = 0
        total_correct = 0
        for x, y in test_data:
            logits = model(x)
            prob = tf.nn.softmax(logits, axis=1)
            pred = tf.argmax(prob, axis=1)
            pred = tf.cast(pred, dtype=tf.int32)
            correct = tf.cast(tf.equal(pred, y), dtype=tf.int32)
            correct = tf.reduce_sum(correct)
            total_num += x.shape[0]
            total_correct += int(correct)
        acc = total_correct / total_num
        print(epoch, 'acc:', acc)
示例#3
0
def GetAppropriateModelFreeze(arch, dataset, dumpData=False, dumpPath="emptyPath", dumpLayer=0):
    if arch == "resnet18":
        if dataset == "cifar10" :
            print("Going for cifar10")
            return  resnet18(32, num_classes=10, dumpData=dumpData, dumpPath=dumpPath, dumpLayer=dumpLayer)
        elif dataset == "cifar100":
            return  resnet18(32, num_classes=100, dumpData=dumpData, dumpPath=dumpPath, dumpLayer=dumpLayer)
    elif arch == "resnet50":
        if dataset == "cifar10" :
            print("Going for cifar10")
            return  resnet50(32, num_classes=10, dumpData=dumpData, dumpPath=dumpPath,dumpLayer=dumpLayer)
        elif dataset == "cifar100":
            print("Going for cifar100")
            return  resnet50(32, num_classes=100, dumpData=dumpData, dumpPath=dumpPath, dumpLayer=dumpLayer)
    elif arch == "resnet152":
        if dataset == "cifar10":
            print("Going for resnet152 cifar10")
            return resnet152(num_classes=10, dumpData=dumpData, dumpPath=dumpPath, dumpLayer=dumpLayer)
        elif dataset == "cifar100":
            print("Going for resnet152 cifar101")
            return resnet152(num_classes=100, dumpData=dumpData, dumpPath=dumpPath, dumpLayer=dumpLayer)
示例#4
0
def main():
    # 输入:[b, 32, 32, 3]
    model = resnet18()
    model.build(input_shape=(None, 32, 32, 3))
    model.summary()
    optimizer = optimizers.Adam(lr=1e-3)


    for epoch in range(500):

        for step, (x, y) in enumerate(train_db):
            with tf.GradientTape() as tape:
                # [b, 32, 32, 3] => [b, 100]
                logits = model(x)
                # [b] => [b, 100]
                y_onehot = tf.one_hot(y, depth=100)
                # compute loss   结果维度[b]
                loss = tf.losses.categorical_crossentropy(y_onehot, logits, from_logits=True)
                loss = tf.reduce_mean(loss)

            # 梯度求解
            grads = tape.gradient(loss, model.trainable_variables)
            # 梯度更新
            optimizer.apply_gradients(zip(grads, model.trainable_variables))

            if step % 50 == 0:
                print(epoch, step, 'loss:', float(loss))

        # 做测试
        total_num = 0
        total_correct = 0
        for x, y in test_db:

            logits = model(x)
            # 预测可能性。
            prob = tf.nn.softmax(logits, axis=1)
            pred = tf.argmax(prob, axis=1)  # 还记得吗pred类型为int64,需要转换一下。
            pred = tf.cast(pred, dtype=tf.int32)

            # 拿到预测值pred和真实值比较。
            correct = tf.cast(tf.equal(pred, y), dtype=tf.int32)
            correct = tf.reduce_sum(correct)

            total_num += x.shape[0]
            total_correct += int(correct)  # 转换为numpy数据

        acc = total_correct / total_num
        print(epoch, 'acc:', acc)
示例#5
0
def load_ms_layer(model_name, classes_nums, pretrain=True, require_grad=True):
    '''
        MS-DeJOR
    '''
    print('==> Building model..')
    if model_name == 'resnet50_ms':
        net = resnet50(pretrained=pretrain)
        for param in net.parameters():
            param.requires_grad = require_grad
        net = MS_resnet_layer(net, 50, 512, classes_nums)
    elif model_name == 'resnet18_ms':
        net = resnet18(pretrained=pretrain)
        for param in net.parameters():
            param.requires_grad = require_grad
        net = MS_resnet_layer(net, 18, 512, classes_nums)
    elif model_name == 'vgg16_ms':
        net = VGG16(pretrained=pretrain)
        for param in net.parameters():
            param.requires_grad = require_grad
        net = MS_vgg16_layer(net, 16, 512, classes_nums)

    return net
示例#6
0
def train(epochs=epochs,
          init_lr=init_lr,
          lr_coefficient=lr_coefficient,
          weight_decay=weight_decay,
          model_num=model_num,
          batch_size=batch_size,
          train_dir=train_dir,
          test_dir=test_dir,
          log_dir=log_dir):

    #loading_data
    print("data loading...\n")
    transform = enhance_transforms()
    transform_std = transform_standard()
    trainset = DataClassify(train_dir, transforms=transform)
    testset = DataClassify(test_dir, transforms=transform_std)
    total_train = len(trainset)
    total_test = len(testset)
    data_loader_train = t.utils.data.DataLoader(dataset=trainset,
                                                batch_size=batch_size,
                                                shuffle=True)
    data_loader_test = t.utils.data.DataLoader(dataset=testset,
                                               batch_size=batch_size,
                                               shuffle=False)
    print("data loading complete\n")

    ##################################
    #TO DO
    ##################################
    if model_num == 0:
        exit(0)
    elif model_num == 18:
        net = resnet18()
    elif model_num == 34:
        net = resnet34()
    elif model_num == 50:
        net = resnet50()
    elif model_num == 101:
        net = resnet101()
    elif model_num == 152:
        net = resnet152()
    ##################################

    #确定网络基于cpu还是gpu
    device = t.device("cuda:0" if t.cuda.is_available() else "cpu")
    net.to(device)

    cost = t.nn.CrossEntropyLoss()
    train_loss_list = []
    train_accurate_list = []
    test_loss_list = []
    test_accurate_list = []

    for epoch in range(epochs):
        print("epoch " + str(epoch + 1) + " start training...\n")

        net.train()

        learning_rate = dloss(train_loss_list, init_lr, lr_coefficient,
                              init_lr)
        optimizer = t.optim.Adam(list(net.parameters()),
                                 lr=learning_rate,
                                 weight_decay=weight_decay)

        run_loss, corr = train_once(data_loader_train, net, optimizer, cost,
                                    device)
        train_loss_list.append(run_loss / total_train)
        train_accurate_list.append(corr / total_train)

        print('epoch %d, training loss %.6f, training accuracy %.4f ------\n' %
              (epoch + 1, run_loss / total_train, corr / total_train))
        print("epoch " + str(epoch + 1) + " finish training\n")
        print("-----------------------------------------------\n")

        print("epoch " + str(epoch + 1) + " start testing...\n")

        net.eval()
        test_corr = evaluate(net, data_loader_test, device)
        test_accurate_list.append(test_corr / total_test)
        print('epoch %d, testing accuracy %.4f ------\n' %
              (epoch + 1, test_corr / total_test))
        print("epoch " + str(epoch + 1) + " finish testing\n")
        print("-----------------------------------------------\n")

    t.save(net, save_trained_net)

    t.save(net.state_dict(), save_trained_net_params)

    curve_draw(train_loss_list, train_accurate_list, test_accurate_list,
               log_dir)

    print("mission complete")