예제 #1
0
def main(config):
    cudnn.benchmark = True

    # Create directories if not exist
    if not os.path.exists(config.model_path):
        os.makedirs(config.model_path)
    if not os.path.exists(config.result_path):
        os.makedirs(config.result_path)
    config.result_path = os.path.join(config.result_path, config.model_type)
    if not os.path.exists(config.result_path):
        os.makedirs(config.result_path)

    lr = random.random() * 0.0005 + 0.0000005
    augmentation_prob = random.random() * 0.7
    # lr = 0.0005
    # augmentation_prob = 0
    epoch = random.choice([100, 150, 200, 250])
    # epoch = 100
    decay_ratio = 0.8
    decay_epoch = int(epoch * decay_ratio)

    config.augmentation_prob = augmentation_prob
    config.num_epochs = epoch
    config.lr = lr
    config.num_epochs_decay = decay_epoch

    print(config)

    batch = config.batch_size
    historyhour = config.historyhour
    batch_filelist, file_dict = my_dataset(batch, historyhour, season='summer')

    batch_test, file_dict_test = my_test_dataset(batch,
                                                 historyhour,
                                                 season=False)
    split_num = 0.9
    valid_num = 1
    train_num = int(len(batch_filelist) * split_num)
    valid_num = int(len(batch_filelist) * valid_num)
    mydataset = {
        'train': batch_filelist[:train_num],
        'valid': batch_filelist[train_num:valid_num],
        'test': batch_test
    }

    solver = Solver(config, mydataset['train'], mydataset['valid'],
                    mydataset['test'])

    # Train and sample the images
    if config.mode == 'train':
        solver.train()
    elif config.mode == 'test':
        solver.test()
예제 #2
0
def main():
    args = parse_args()
    save = True
    use_gpu = args.cuda == 'True'
    load = args.load == 'True'
    train_tfs = transforms.Compose([
        transforms.Resize(299),
        transforms.RandomSizedCrop(299),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])
    ds = my_dataset("train", train_tfs)
    dataset_size = ds.__len__()
    print(dataset_size)
    train_ds, val_ds = torch.utils.data.random_split(ds, [13000, 1463])
    train_loader = torch.utils.data.DataLoader(train_ds,
                                               args.BS,
                                               False,
                                               num_workers=8)
    val_loader = torch.utils.data.DataLoader(val_ds,
                                             args.BS,
                                             False,
                                             num_workers=8)
    print('train: ', len(train_ds))
    print('validation:', len(val_ds))
    print(type(ds), type(train_ds))
    if args.model == 'ResNet18':
        test_model = model.ResNet18()
    if args.model == 'ResNet50':
        test_model = model.ResNet50()
    if args.model == 'Inception':
        test_model = model.Inception()
    if args.model == 'DenseNet':
        test_model = model.DenseNet()
    if use_gpu:
        test_model = test_model.cuda()
    if load:
        test_model.load_state_dict(torch.load('params' + args.model + '.pkl'))
    optimizer = optim.Adam(test_model.parameters(), lr=args.lr)
    print(use_gpu)
    result = train(test_model, args.epoch, optimizer, train_loader, val_loader,
                   args.model, save, use_gpu)
    test(result, val_loader, use_gpu)
def main():
    args = parse_args()
    name = open('pred2.json', 'w')
    train_tfs = transforms.Compose([
        transforms.Resize(299),
        transforms.RandomSizedCrop(299),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])
    ds = my_dataset("test", train_tfs)
    dataset_size = ds.__len__()
    print(dataset_size)
    loader = torch.utils.data.DataLoader(ds, 16, False)

    test_model = model.Inception()
    test_model = test_model.cuda()
    test_model.load_state_dict(torch.load('params' + args.model + '.pkl'))

    preds = {}
    num = 0
    for i, data in enumerate(loader):

        image = data['image']
        print(image.size())

        image = Variable(image.cuda())
        # label = Variable(label.cuda())

        output = test_model(image)

        pred = torch.argmax(output, 1)
        # if i < 10:
        #    print(pred)
        for j in pred:
            if i < 10:
                print(j)
            preds[ds.imgs[num]] = labels[j.int()]
            num = num + 1
    print(json.dumps(preds))
    print(num)
    name.write(json.dumps(preds))
    name.close()
예제 #4
0
def test_task1(root_path):
    '''
    :param root_path: root path of test data, e.g. ./dataset/task1/test/
    :return results: a dict of classification results
    results = {'audio_0000.pkl': 1, ‘audio_0001’: 3, ...}
    class number:
        ‘061_foam_brick’: 0
        'green_basketball': 1
        'salt_cylinder': 2
        'shiny_toy_gun': 3
        'stanley_screwdriver': 4
        'strawberry': 5
        'toothpaste_box': 6
        'toy_elephant': 7
        'whiteboard_spray': 8
        'yellow_block': 9
    '''
    preds = []
    names = []
    ds = my_dataset("test", transform, root_path)
    loader = torch.utils.data.DataLoader(ds, 64, False, num_workers=20)
    model = models.ResNet(block=models.BasicBlock, num_blocks=[3, 3, 3])
    model = nn.DataParallel(model).cuda()
    model.load_state_dict(torch.load("task1resnet18.pkl"))
    model.eval()
    for data in loader:
        image, name = data['image'], data['name']
        image = image.cuda()
        image = torch.autograd.Variable(image)
        output = model(image)
        pred = torch.argmax(output, 1)
        names.extend(name)
        preds.extend(pred)
    preds = [int(i) for i in preds]
    names = [i[-14:] for i in names]
    # dirs = os.listdir(os.path.join(root_path))
    results = dict(zip(names, preds))
    np.save("task1result.npy", results)
    return results
예제 #5
0
def test_task2(root_path):
    '''
    :param root_path: root path of test data, e.g. ./dataset/task2/test/0/
    :return results: a dict of classification results
    results = {'audio_0000.pkl': 23, ‘audio_0001’: 11, ...}
    This means audio 'audio_0000.pkl' is matched to video 'video_0023' and ‘audio_0001’ is matched to 'video_0011'.
    '''
    results = {}

    audio_preds = []
    audio_names = []
    audio_ds = my_dataset("test", transform, root_path)
    audio_loader = torch.utils.data.DataLoader(audio_ds,
                                               4,
                                               False,
                                               num_workers=0)
    audio_model = models.ResNet(block=models.BasicBlock, num_blocks=[3, 3, 3])
    audio_model = nn.DataParallel(audio_model).cuda()
    audio_model.load_state_dict(torch.load("task1resnet18.pkl"))
    audio_model.eval()
    for data in audio_loader:
        image, name = data['image'], data['name']
        image = image.cuda()
        image = torch.autograd.Variable(image)
        output = audio_model(image)
        pred = torch.argmax(output, 1)
        audio_names.extend(name)
        audio_preds.extend(pred)
    audio_preds = [int(i) for i in audio_preds]
    audio_names = [i.split('/')[-1][:-4] for i in audio_names]
    audio_results = dict(zip(audio_names, audio_preds))

    video_preds = []
    video_names = []
    video_ds = video_dataset("test", transform, root_path)
    video_loader = torch.utils.data.DataLoader(video_ds,
                                               4,
                                               False,
                                               num_workers=0)
    video_model = models.ResNet(in_ch=1,
                                in_stride=(1, 1),
                                fc_size=64,
                                block=models.BasicBlock,
                                num_blocks=[3, 3, 3])
    video_model = nn.DataParallel(video_model).cuda()
    video_model.load_state_dict(torch.load("new_video_resnet.pkl"))
    video_model.eval()
    for data in video_loader:
        image, name = data['image'], data['name']
        image = image.cuda()
        image = torch.autograd.Variable(image)
        output = video_model(image)
        pred = torch.argmax(output, 1)
        video_names.extend(name)
        video_preds.extend(pred)
    video_preds = [int(i) for i in video_preds]
    video_names_unique = list(set(video_names))
    video_preds_max = []
    for name in video_names_unique:
        indices = [i for i, x in enumerate(video_names) if x == name]
        pred = [video_preds[i] for i in indices]
        pred = max(pred, key=pred.count)
        video_preds_max.append(pred)
    video_results = dict(zip(video_names_unique, video_preds_max))

    audio_num = len(audio_names)
    for i in range(10):
        class_name = classes[i]
        matching_resnet_model = models.ResNet(block=models.BasicBlock,
                                              num_blocks=[3, 3, 3])
        matching_resnet_model = nn.DataParallel(matching_resnet_model).cuda()
        matching_resnet_model.load_state_dict(
            torch.load(class_name + "_resnet.pkl"))
        matching_resnet_model.eval()
        matching_mlp_model = models.MLP()
        matching_mlp_model = nn.DataParallel(matching_mlp_model).cuda()
        matching_mlp_model.load_state_dict(torch.load(class_name + "_mlp.pkl"))
        matching_mlp_model.eval()

        audio_i = [k for k, v in audio_results.items() if v == i]
        video_i = [k for k, v in video_results.items() if v == i]
        print(audio_i)
        print(video_i)

        matching_ds = matching_dataset(mode="test",
                                       transforms=transform,
                                       test_path=root_path,
                                       test_audio=audio_i,
                                       test_video=video_i)
        matching_loader = torch.utils.data.DataLoader(matching_ds,
                                                      4,
                                                      False,
                                                      num_workers=0)

        distance_matrix = np.zeros((len(audio_i), len(video_i)))

        for data in matching_loader:
            raw, name = data['raw'], data['name']
            image = raw[0]
            video = raw[1]
            image = torch.autograd.Variable(image.cuda())
            video = torch.autograd.Variable(video.cuda())
            video_output = matching_mlp_model(video)
            image_output = matching_resnet_model(image)
            dist = F.pairwise_distance(video_output, image_output)
            for j in range(len(dist)):
                audio_num = audio_i.index(name[0][j])
                video_num = video_i.index(name[1][j])
                distance_matrix[audio_num][video_num] = dist[j]

        print(distance_matrix)
        row_ind, col_ind = linear_sum_assignment(distance_matrix)
        print(row_ind)
        print(col_ind)
        for j in range(len(row_ind)):
            audio_name = audio_i[row_ind[j]]
            video_name = video_i[col_ind[j]]
            results[audio_name] = video_name

    audio_set = list(set(audio_names) - set([k for k, v in results.items()]))
    video_set = list(set(video_names) - set([v for k, v in results.items()]))
    perm = np.random.permutation(len(audio_set))
    for j in perm:
        audio_name = audio_set[j]
        video_name = video_set[j]
        results[audio_name] = video_name
    for k, v in results.items():
        results[k] = int(v[-4:])
    print(results)
    return results
    acc = (100 * correct * 1.0 / total)
    print("accuracy : %.03f" % (acc))
    return acc


resnet = models.ResNet(block=models.BasicBlock, num_blocks=[3, 3, 3])
resnet = nn.DataParallel(resnet).cuda()
resnet.load_state_dict(torch.load("task1resnet18.pkl"))
# resnet = resnet.resnet50()
mlp = models.MLP()
criterion = nn.CrossEntropyLoss().cuda()
# optimizer = torch.optim.Adam(resnet.parameters(), lr = lr)
optimizer = torch.optim.SGD(resnet.parameters(),
                            lr=lr,
                            momentum=0.9,
                            weight_decay=1e-4)
ds = my_dataset("train", transform_train)

train_ds, val_ds = torch.utils.data.random_split(ds, [2000, 249])
train_loader = torch.utils.data.DataLoader(train_ds, 32, False, num_workers=20)
val_loader = torch.utils.data.DataLoader(val_ds, 16, False, num_workers=20)

# print(len(task2_ds))

# dataset2 = matching_dataset(mode="train")
# x = dataset2.__getitem__(5, 6)
# print(x['raw'][1].shape)

test1(resnet, val_loader)
# train(train_loader, val_loader, resnet, criterion, optimizer, 500, use_cuda=True, save = False)
예제 #7
0
def main(config):
    cudnn.benchmark = True
    #     if config.model_type not in ['U_Net','R2U_Net','AttU_Net','R2AttU_Net']:
    #         print('ERROR!! model_type should be selected in U_Net/R2U_Net/AttU_Net/R2AttU_Net')
    #         print('Your input for model_type was %s'%config.model_type)
    #         return

    # Create directories if not exist
    if not os.path.exists(config.model_path):
        os.makedirs(config.model_path)
    if not os.path.exists(config.result_path):
        os.makedirs(config.result_path)
    config.result_path = os.path.join(config.result_path, config.model_type)
    if not os.path.exists(config.result_path):
        os.makedirs(config.result_path)


#     lr = random.random()*0.0005 + 0.0000005
#     augmentation_prob= random.random()*0.7
    lr = 0.0005
    augmentation_prob = 0
    #     epoch = random.choice([100,150,200,250])
    epoch = 100
    decay_ratio = 0.8
    decay_epoch = int(epoch * decay_ratio)

    config.augmentation_prob = augmentation_prob
    config.num_epochs = epoch
    config.lr = lr
    config.num_epochs_decay = decay_epoch

    print(config)

    # train_loader = get_loader(image_path=config.train_path,
    #                         image_size=config.image_size,
    #                         batch_size=config.batch_size,
    #                         num_workers=config.num_workers,
    #                         mode='train',
    #                         augmentation_prob=config.augmentation_prob)
    # valid_loader = get_loader(image_path=config.valid_path,
    #                         image_size=config.image_size,
    #                         batch_size=config.batch_size,
    #                         num_workers=config.num_workers,
    #                         mode='valid',
    #                         augmentation_prob=0.)
    # test_loader = get_loader(image_path=config.test_path,
    #                         image_size=config.image_size,
    #                         batch_size=config.batch_size,
    #                         num_workers=config.num_workers,
    #                         mode='test',
    #                         augmentation_prob=0.)

    batch = config.batch_size
    historyhour = config.historyhour
    batch_filelist, file_dict = my_dataset(batch, historyhour, season='summer')

    batch_test, file_dict_test = my_test_dataset(batch,
                                                 historyhour,
                                                 season=False)
    split_num = 0.9
    valid_num = 1
    train_num = int(len(batch_filelist) * split_num)
    valid_num = int(len(batch_filelist) * valid_num)
    mydataset = {
        'train': batch_filelist[:train_num],
        'valid': batch_filelist[train_num:valid_num],
        'test': batch_test
    }

    # for filelist in mydataset['train']:
    #     tt = time.time()
    #     ruitudata, gaugedata, histgaugedata = load_data2(filelist,file_dict,historyhour, binary=True)

    solver = Solver(config, mydataset['train'], mydataset['valid'],
                    mydataset['test'])

    # Train and sample the images
    if config.mode == 'train':
        solver.train()
    elif config.mode == 'test':
        solver.test()