Пример #1
0
def compute_time():
    """
    计算100次,取平均值
    :return:
    """
    model_googlenet_bn = googlenet_bn.GoogLeNet_BN(num_classes=1000)
    model_googlenet = googlenet.GoogLeNet(num_classes=1000)
    model_googlenet_bn.eval()
    model_googlenet.eval()

    total_time_googlenet_bn = 0.0
    total_time_googlenet = 0.0

    epoch = 100
    for i in range(epoch):
        data = torch.randn((1, 3, 224, 224))
        start = time.time()
        outputs = model_googlenet_bn.forward(data)
        end = time.time()
        total_time_googlenet_bn += (end - start)

        start = time.time()
        outputs = model_googlenet.forward(data)
        end = time.time()
        total_time_googlenet += (end - start)

    print('[googlenet_bn] time: {:.4f}'.format(total_time_googlenet_bn /
                                               epoch))
    print('[googlenet] time: {:.4f}'.format(total_time_googlenet / epoch))
    print('time_googlenet / time_googlenet_bn: {:.3f}'.format(
        total_time_googlenet / total_time_googlenet_bn))
Пример #2
0
def compute_param():
    model_googlenet_bn = googlenet_bn.GoogLeNet_BN(num_classes=1000)
    model_googlenet = googlenet.GoogLeNet(num_classes=1000)
    model_googlenet_bn.eval()
    model_googlenet.eval()

    num_googlenet_bn = util.num_model(model_googlenet_bn)
    num_googlenet = util.num_model(model_googlenet)

    print('[googlenet_bn] param num: {}'.format(num_googlenet_bn))
    print('[googlenet] param num: {}'.format(num_googlenet))

    print('num_googlenet_bn / num_googlenet: {:.2f}'.format(num_googlenet_bn /
                                                            num_googlenet))
Пример #3
0
def compute_param():
    model_alexnet = AlexNet(num_classes=1000)
    model_googlenet = googlenet.GoogLeNet(num_classes=1000)
    model_alexnet.eval()
    model_googlenet.eval()

    num_alexnet = util.num_model(model_alexnet)
    num_googlenet = util.num_model(model_googlenet)

    print('[alexnet] param num: {}'.format(num_alexnet))
    print('[googlenet] param num: {}'.format(num_googlenet))

    print('num_alexnet / num_googlenet: {:.2f}'.format(num_alexnet /
                                                       num_googlenet))
Пример #4
0
    ############ choose loss and backbone ######################
    if opt.loss == 'focal_loss':
        criterion = focal_loss.FocalLoss(gamma=2)
    else:
        criterion = torch.nn.CrossEntropyLoss()

    if opt.backbone == 'resnet18':
        model = resnet.resnet_face18(use_se=opt.use_se)
    elif opt.backbone == 'resnet50':
        model = resnet.resnet50()
    elif opt.backbone == 'resnet101':
        model = resnet.resnet101()
    elif opt.backbone == 'resnet152':
        model = resnet.resnet152()
    elif opt.backbone == 'googlenet':
        model = googlenet.GoogLeNet()

    if opt.metric == 'add_margin':
        metric_fc = metrics.AddMarginProduct(1024,
                                             opt.num_classes,
                                             s=30,
                                             m=0.35)
    elif opt.metric == 'arc_margin':
        metric_fc = metrics.ArcMarginProduct(512,
                                             opt.num_classes,
                                             s=30,
                                             m=0.5,
                                             easy_margin=opt.easy_margin)
    elif opt.metric == 'sphere':
        metric_fc = metrics.SphereProduct(512, opt.num_classes, m=4)
    else:
Пример #5
0
if __name__ == '__main__':
    device = util.get_device()
    # device = 'cpu'

    data_loaders, data_sizes = load_data('./data/pascal-voc')
    print(data_loaders)
    print(data_sizes)

    res_loss = dict()
    res_acc = dict()
    for name in ['googlenet_bn', 'googlenet']:
        if name == 'googlenet_bn':
            model = googlenet_bn.GoogLeNet_BN(num_classes=20)
        else:
            model = googlenet.GoogLeNet(num_classes=20)
        model.eval()
        # print(model)
        model = model.to(device)

        criterion = nn.CrossEntropyLoss()
        optimizer = optim.Adam(model.parameters(), lr=1e-3)
        lr_schduler = optim.lr_scheduler.StepLR(optimizer,
                                                step_size=8,
                                                gamma=0.96)

        util.check_dir('./data/models/')
        best_model, loss_dict, acc_dict = train_model(data_loaders,
                                                      data_sizes,
                                                      name,
                                                      model,
Пример #6
0
    net = efficientnet.CEfficientNet(num_classes=num_classes,
                                     pretrained=args.pretrain,
                                     model_name=args.model)
elif args.model == "efficientnet-b4":
    net = efficientnet.CEfficientNet(num_classes=num_classes,
                                     pretrained=args.pretrain,
                                     model_name=args.model)
elif args.model == "efficientnet-b5":
    net = efficientnet.CEfficientNet(num_classes=num_classes,
                                     pretrained=args.pretrain,
                                     model_name=args.model)

elif args.model == "resnext50":
    net = resnext.resnext50(num_classes=num_classes)
elif args.model == "googlenet":
    net = googlenet.GoogLeNet(num_classes=num_classes)
elif args.model == "densenet":
    net = densenet.DenseNet_CIFAR(num_classes=num_classes)
else:
    raise "please check model"

# freeze
# count = 0
# for param in net.parameters():
#     count += 1
# for i, param in enumerate(net.parameters()):
#     if i <= count-1 - 10:
#         param.requires_grad = False

str_pretrain = ""
if args.pretrain:
Пример #7
0
def main():
    parser = argparse.ArgumentParser(description='Speech Emotion Recognition')
    parser.add_argument('--hidden_size',
                        type=int,
                        default=512,
                        help='hidden size of model (default: 256)')
    parser.add_argument('--layer_size',
                        type=int,
                        default=3,
                        help='number of layers of model (default: 3)')
    parser.add_argument('--n_class',
                        type=int,
                        default=7,
                        help='number of classes of data (default: 7)')
    parser.add_argument('--dropout',
                        type=float,
                        default=0.2,
                        help='dropout rate in training (default: 0.2')
    parser.add_argument('--bidirectional',
                        default=True,
                        action='store_true',
                        help='use bidirectional RNN (default: False')
    parser.add_argument('--batch_size',
                        type=int,
                        default=4,
                        help='batch size in training (default: 32')
    parser.add_argument(
        '--workers',
        type=int,
        default=4,
        help='number of workers in dataset loader (default: 4)')
    parser.add_argument('--max_epochs',
                        type=int,
                        default=30,
                        help='number of max epochs in training (default: 10)')
    parser.add_argument('--lr',
                        type=float,
                        default=1e-04,
                        help='learning rate (default: 0.0001)')
    parser.add_argument('--no_cuda',
                        action='store_true',
                        default=False,
                        help='disables CUDA training')
    parser.add_argument('--seed',
                        type=int,
                        default=1,
                        help='random seed (default: 1)')
    parser.add_argument('--save_name',
                        type=str,
                        default='model',
                        help='the name of model')
    parser.add_argument('--mode', type=str, default='train')

    args = parser.parse_args()

    random.seed(args.seed)
    torch.manual_seed(args.seed)
    torch.cuda.manual_seed_all(args.seed)

    args.cuda = not args.no_cuda and torch.cuda.is_available()
    device = torch.device('cuda' if args.cuda else 'cpu')

    # N_FFT: defined in loader.py
    feature_size = N_FFT / 2 + 1

    cnn = googlenet.GoogLeNet(feature_size=feature_size)
    rnn = RNN.RNN(cnn.feature_size,
                  args.hidden_size,
                  args.n_class,
                  input_dropout_p=args.dropout,
                  dropout_p=args.dropout,
                  n_layers=args.layer_size,
                  bidirectional=args.bidirectional,
                  rnn_cell='gru',
                  variable_lengths=False)

    model = CRNN.CRNN(cnn, rnn)
    model.flatten_parameters()

    model = nn.DataParallel(model).to(device)

    optimizer = optim.Adam(model.module.parameters(), lr=args.lr)
    criterion = nn.CrossEntropyLoss(reduction='sum').to(device)

    if args.mode != 'train':
        return

    data_download()

    wav_paths = [
        os.path.join('./dataset/wav', fname)
        for fname in os.listdir('./dataset/wav')
    ]

    best_acc = 0
    begin_epoch = 0

    loss_acc = [[], [], [], []]

    train_batch_num, train_dataset_list, valid_dataset, test_dataset = split_dataset(
        args, wav_paths, dataset_ratio=[0.7, 0.1, 0.2])

    logger.info('start')

    train_begin = time.time()

    for epoch in range(begin_epoch, args.max_epochs):

        train_queue = queue.Queue(args.workers * 2)

        train_loader = MultiLoader(train_dataset_list, train_queue,
                                   args.batch_size, args.workers)
        train_loader.start()

        train_loss, train_acc = train(model, train_batch_num, train_queue,
                                      criterion, optimizer, device,
                                      train_begin, args.workers, 10)
        logger.info('Epoch %d (Training) Loss %0.4f Acc %0.4f' %
                    (epoch, train_loss, train_acc))

        train_loader.join()

        loss_acc[0].append(train_loss)
        loss_acc[1].append(train_acc)

        valid_queue = queue.Queue(args.workers * 2)

        valid_loader = BaseDataLoader(valid_dataset, valid_queue,
                                      args.batch_size, 0)
        valid_loader.start()

        eval_loss, eval_acc = evaluate(model, valid_loader, valid_queue,
                                       criterion, device)
        logger.info('Epoch %d (Evaluate) Loss %0.4f Acc %0.4f' %
                    (epoch, eval_loss, eval_acc))

        valid_loader.join()

        loss_acc[2].append(eval_loss)
        loss_acc[3].append(eval_acc)

        best_model = (eval_acc > best_acc)

        if best_model:
            best_acc = eval_acc
            torch.save(model.state_dict(), './save_model/best_model.pt')
            save_epoch = epoch

    model.load_state_dict(torch.load('./save_model/best_model.pt'))

    test_queue = queue.Queue(args.workers * 2)

    test_loader = BaseDataLoader(test_dataset, test_queue, args.batch_size, 0)
    test_loader.start()

    test_loss, test_acc = evaluate(model, test_loader, test_queue, criterion,
                                   device)
    logger.info('Epoch %d (Test) Loss %0.4f Acc %0.4f' %
                (save_epoch, test_loss, test_acc))

    test_loader.join()

    save_data(loss_acc, test_loss, test_acc)
    plot_data(loss_acc, test_loss, test_acc)

    return 0
Пример #8
0
def main(args):
    BATCH_SIZE = args.batch_size
    LR = args.learning_rate
    EPOCH = args.epoch

    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    use_gpu = torch.cuda.is_available()

    data_transforms = {
        transforms.Compose([
            transforms.Resize(320),
            transforms.CenterCrop(299),
            transforms.ToTensor(),
            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
        ])
    }
    transform = transforms.Compose([
        transforms.Resize(size=(227, 227)),
        transforms.RandomRotation(20),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),  # 将图片转换为Tensor,归一化至[0,1]
        transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])
    train_dataset = torchvision.datasets.ImageFolder(root=args.train_images,
                                                     transform=transform)
    train_loader = torch.utils.data.DataLoader(train_dataset,
                                               batch_size=BATCH_SIZE,
                                               shuffle=True)

    # 从文件夹中读取validation数据
    validation_dataset = torchvision.datasets.ImageFolder(
        root=args.test_images, transform=transform)
    print(validation_dataset.class_to_idx)

    test_loader = torch.utils.data.DataLoader(validation_dataset,
                                              batch_size=BATCH_SIZE,
                                              shuffle=True)

    if args.model_name == "densenet":
        Net = densenet.DenseNet().to(device)
    if args.model_name == "alexnet":
        Net = alexnet.AlexNet().to(device)
    if args.model_name == "googlenet":
        Net = googlenet.GoogLeNet().to(device)
    if args.model_name == "mobilenet":
        Net = mobilenet.MobileNetV2().to(device)
    if args.model_name == "mnasnet":
        Net = mnasnet.mnasnet1_0().to(device)
    if args.model_name == "squeezenet":
        Net = squeezenet.SqueezeNet().to(device)
    if args.model_name == "resnet":
        Net = resnet.resnet50().to(device)
    if args.model_name == "vgg":
        Net = vgg.vgg19().to(device)
    if args.model_name == "shufflenetv2":
        Net = shufflenetv2.shufflenet_v2_x1_0().to(device)

    criterion = nn.CrossEntropyLoss()
    opti = torch.optim.Adam(Net.parameters(), lr=LR)

    if __name__ == '__main__':
        Accuracy_list = []
        Loss_list = []

        for epoch in range(EPOCH):
            sum_loss = 0.0
            correct1 = 0

            total1 = 0
            for i, (images, labels) in enumerate(train_loader):
                num_images = images.size(0)

                images = Variable(images.to(device))
                labels = Variable(labels.to(device))

                if args.model_name == 'googlenet':
                    out = Net(images)
                    out = out[0]
                else:
                    out = Net(images)
                _, predicted = torch.max(out.data, 1)

                total1 += labels.size(0)

                correct1 += (predicted == labels).sum().item()

                loss = criterion(out, labels)
                print(loss)
                opti.zero_grad()
                loss.backward()
                opti.step()

                # 每训练100个batch打印一次平均loss
                sum_loss += loss.item()
                if i % 10 == 9:
                    print('train loss [%d, %d] loss: %.03f' %
                          (epoch + 1, i + 1, sum_loss / 2000))
                    print("train acc %.03f" % (100.0 * correct1 / total1))
                    sum_loss = 0.0
            Accuracy_list.append(100.0 * correct1 / total1)
            print('accurary={}'.format(100.0 * correct1 / total1))
            Loss_list.append(loss.item())

        x1 = range(0, EPOCH)
        x2 = range(0, EPOCH)
        y1 = Accuracy_list
        y2 = Loss_list

        total_test = 0
        correct_test = 0
        for i, (images, labels) in enumerate(test_loader):
            start_time = time.time()
            print('time_start', start_time)
            num_images = images.size(0)
            print('num_images', num_images)
            images = Variable(images.to(device))
            labels = Variable(labels.to(device))
            print("GroundTruth", labels)
            if args.model_name == 'googlenet':
                out = Net(images)[0]
                out = out[0]
            else:
                out = Net(images)
            _, predicted = torch.max(out.data, 1)
            print("predicted", predicted)
            correct_test += (predicted == labels).sum().item()
            total_test += labels.size(0)
            print('time_usage', (time.time() - start_time) / args.batch_size)
        print('total_test', total_test)
        print('correct_test', correct_test)
        print('accurary={}'.format(100.0 * correct_test / total_test))

        plt.subplot(2, 1, 1)
        plt.plot(x1, y1, 'o-')
        plt.title('Train accuracy vs. epoches')
        plt.ylabel('Train accuracy')
        plt.subplot(2, 1, 2)
        plt.plot(x2, y2, '.-')
        plt.xlabel('Train loss vs. epoches')
        plt.ylabel('Train loss')
        # plt.savefig("accuracy_epoch" + str(EPOCH) + ".png")
        plt.savefig(args.output_dir + '/' + 'accuracy_epoch' + str(EPOCH) +
                    '.png')
        plt.show()
        torch.save(args.output_dir, args.model_name + '.pth')