コード例 #1
0
ファイル: train-tiny.py プロジェクト: ShenDezhou/CAIL2021
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('--batch_size', type=int, default=32)
    parser.add_argument('--epochs', type=int, default=50)
    parser.add_argument('--num_classes', type=int, default=130)

    parser.add_argument('--lr', type=float, default=2e-3)
    parser.add_argument('--weight_decay', type=float, default=1e-5)

    parser.add_argument('--resume', type=bool, default=False)
    parser.add_argument('--eval', type=bool, default=False)

    parser.add_argument('--dataroot', type=str, default='/content/drive/MyDrive/dogflg/data2/')
    parser.add_argument('--model_path', type=str, default='./best_model.bin')

    parser.add_argument('--sampleratio', type=float, default=0.8)

    args = parser.parse_args()
    
    transform_train = transform.Compose([
        transform.Resize((256, 256)),
        transform.CenterCrop(224),
        transform.RandomHorizontalFlip(),
        transform.ToTensor(),
        transform.ImageNormalize(0.485, 0.229),
        # transform.ImageNormalize(0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
    ])

    root_dir = args.dataroot
    train_loader = TsinghuaDog(root_dir, batch_size=args.batch_size, train=True, part='train', shuffle=True, transform=transform_train, sample_rate=args.sampleratio)

    transform_test = transform.Compose([
        transform.Resize((256, 256)),
        transform.CenterCrop(224),
        transform.ToTensor(),
        transform.ImageNormalize(0.485, 0.229),
        # transform.ImageNormalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
    ])
    val_loader = TsinghuaDog(root_dir, batch_size=args.batch_size, train=False, part='val', shuffle=False, transform=transform_test, sample_rate=args.sampleratio)

    epochs = args.epochs
    model = Net(num_classes=args.num_classes)
    lr = args.lr
    weight_decay = args.weight_decay
    optimizer = SGD(model.parameters(), lr=lr, momentum=0.99)
    if args.resume:
        model.load(args.model_path)
        print('model loaded', args.model_path)

    #random save for test
    #model.save(args.model_path)
    if args.eval:
        evaluate(model, val_loader, save_path=args.model_path)
        return 
    for epoch in range(epochs):
        train(model, train_loader, optimizer, epoch)
        evaluate(model, val_loader, epoch, save_path=args.model_path)
コード例 #2
0
    def test_crop(self):
        height = random.randint(10, 32) * 2
        width = random.randint(10, 32) * 2
        oheight = random.randint(5, (height - 2) / 2) * 2
        owidth = random.randint(5, (width - 2) / 2) * 2

        img = np.ones([height, width, 3])
        oh1 = (height - oheight) // 2
        ow1 = (width - owidth) // 2
        # imgnarrow = img[oh1:oh1 + oheight, ow1:ow1 + owidth, :]
        # imgnarrow.fill(0)
        img[oh1:oh1 + oheight, ow1:ow1 + owidth, :] = 0
        # img = jt.array(img)
        result = transform.Compose([
            transform.ToPILImage(),
            transform.CenterCrop((oheight, owidth)),
            transform.ToTensor(),
        ])(img)
        self.assertEqual(
            result.sum(), 0,
            f"height: {height} width: {width} oheight: {oheight} owdith: {owidth}"
        )
        oheight += 1
        owidth += 1
        result = transform.Compose([
            transform.ToPILImage(),
            transform.CenterCrop((oheight, owidth)),
            transform.ToTensor(),
        ])(img)
        sum1 = result.sum()
        # TODO: not pass
        # self.assertGreater(sum1, 1,
        #                    f"height: {height} width: {width} oheight: {oheight} owdith: {owidth}")
        oheight += 1
        owidth += 1
        result = transform.Compose([
            transform.ToPILImage(),
            transform.CenterCrop((oheight, owidth)),
            transform.ToTensor(),
        ])(img)
        sum2 = result.sum()
        self.assertGreater(
            sum2, 0,
            f"height: {height} width: {width} oheight: {oheight} owdith: {owidth}"
        )
        self.assertGreaterEqual(
            sum2, sum1,
            f"height: {height} width: {width} oheight: {oheight} owdith: {owidth}"
        )
コード例 #3
0
    def test_random_choice(self):
        random_state = random.getstate()
        random.seed(42)
        random_choice_transform = transform.RandomChoice([
            transform.Resize(15),
            transform.Resize(20),
            transform.CenterCrop(10)
        ])
        img = transform.ToPILImage()(jt.random((25, 25, 3)))
        num_samples = 250
        num_resize_15 = 0
        num_resize_20 = 0
        num_crop_10 = 0
        for _ in range(num_samples):
            out = random_choice_transform(img)
            if out.size == (15, 15):
                num_resize_15 += 1
            elif out.size == (20, 20):
                num_resize_20 += 1
            elif out.size == (10, 10):
                num_crop_10 += 1

        p_value = stats.binom_test(num_resize_15, num_samples, p=0.33333)
        self.assertGreater(p_value, 0.0001)
        p_value = stats.binom_test(num_resize_20, num_samples, p=0.33333)
        self.assertGreater(p_value, 0.0001)
        p_value = stats.binom_test(num_crop_10, num_samples, p=0.33333)
        self.assertGreater(p_value, 0.0001)

        random.setstate(random_state)
コード例 #4
0
    def test_random_order(self):
        random_state = random.getstate()
        random.seed(42)
        random_order_transform = transform.RandomOrder(
            [transform.Resize(20),
             transform.CenterCrop(10)])
        img = transform.ToPILImage()(jt.random((3, 25, 25)))
        num_samples = 250
        num_normal_order = 0
        resize_crop_out = transform.CenterCrop(10)(transform.Resize(20)(img))
        for _ in range(num_samples):
            out = random_order_transform(img)
            if out == resize_crop_out:
                num_normal_order += 1

        p_value = stats.binom_test(num_normal_order, num_samples, p=0.5)
        random.setstate(random_state)
        self.assertGreater(p_value, 0.0001)
コード例 #5
0
 def transform(self, img, hflip=False):
     if self.crop is not None:
         if isinstance(self.crop, int):
             img = tfs.CenterCrop(self.crop)(img)
         else:
             assert len(self.crop) == 4, 'Crop size must be an integer for center crop, or a list of 4 integers (y0,x0,h,w)'
             img = tfs.crop(img, *self.crop)
     img = tfs.resize(img, (self.image_size, self.image_size))
     if hflip:
         img = tfs.hflip(img)
     return tfs.to_tensor(img)
コード例 #6
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('--batch_size', type=int, default=8)
    parser.add_argument('--epochs', type=int, default=50)
    parser.add_argument('--num_classes', type=int, default=130)

    parser.add_argument('--lr', type=float, default=2e-3)
    parser.add_argument('--weight_decay', type=float, default=1e-5)

    parser.add_argument('--resume', type=bool, default=True)
    parser.add_argument('--eval', type=bool, default=False)

    parser.add_argument('--dataroot',
                        type=str,
                        default='/content/drive/MyDrive/dogfl/data/TEST_A/')
    parser.add_argument('--model_path', type=str, default='./best_model.bin')
    parser.add_argument('--out_file', type=str, default='./result.json')

    args = parser.parse_args()

    root_dir = args.dataroot

    transform_test = transform.Compose([
        transform.Resize((512, 512)),
        transform.CenterCrop(448),
        transform.ToTensor(),
        transform.ImageNormalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
    ])

    name_list = []
    for _, _, _name_list in os.walk(root_dir):
        name_list = _name_list
    val_loader = TsinghuaDogExam(root_dir,
                                 batch_size=args.batch_size,
                                 train=False,
                                 name_list=name_list,
                                 shuffle=False,
                                 transform=transform_test)

    model = Net(num_classes=args.num_classes)
    if args.resume:
        model.load(args.model_path)
        print('model loaded', args.model_path)

    top5_class_list = evaluate(model, val_loader)
    # label start from 1, however it doesn't
    pred_result = dict(zip(name_list, top5_class_list))

    with open(args.out_file, 'w') as fout:
        json.dump(pred_result, fout, ensure_ascii=False, indent=4)
コード例 #7
0
ファイル: main.py プロジェクト: jysqqqq/TsinghuaDogBaseline
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('--batch_size', type=int, default=8)
    parser.add_argument('--epochs', type=int, default=50)
    parser.add_argument('--num_classes', type=int, default=130)

    parser.add_argument('--lr', type=float, default=2e-3)
    parser.add_argument('--weight_decay', type=float, default=1e-5)

    parser.add_argument('--resume', type=bool, default=False)
    parser.add_argument('--eval', type=bool, default=False)

    parser.add_argument('--dataroot', type=str, default='/home/gmh/dataset/TsinghuaDog/')
    parser.add_argument('--model_path', type=str, default='./best_model.pkl')

    args = parser.parse_args()
    
    transform_train = transform.Compose([
        transform.Resize((512, 512)),
        transform.RandomCrop(448),
        transform.RandomHorizontalFlip(),
        transform.ToTensor(),
        transform.ImageNormalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
    ])

    root_dir = args.dataroot
    train_loader = TsinghuaDog(root_dir, batch_size=16, train=True, part='train', shuffle=True, transform=transform_train)
    
    transform_test = transform.Compose([
        transform.Resize((512, 512)),
        transform.CenterCrop(448),
        transform.ToTensor(),
        transform.ImageNormalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
    ])
    val_loader = TsinghuaDog(root_dir, batch_size=16, train=False, part='val', shuffle=False, transform=transform_test)
    epochs = args.epochs
    model = Net(num_classes=args.num_classes)
    lr = args.lr
    weight_decay = args.weight_decay
    optimizer = SGD(model.parameters(), lr=lr, momentum=0.9) 
    if args.resume:
        model.load(args.model_path)
    if args.eval:
        evaluate(model, val_loader)
        return 
    for epoch in range(epochs):
        train(model, train_loader, optimizer, epoch)
        evaluate(model, val_loader, epoch)
コード例 #8
0
def transforms_imagenet_eval(img_size=224,
                             crop_pct=0.9,
                             interpolation=Image.BICUBIC,
                             mean=(0.485, 0.456, 0.406),
                             std=(0.229, 0.224, 0.225)):
    crop_pct = crop_pct or 0.875

    if isinstance(img_size, tuple):
        assert len(img_size) == 2
        if img_size[-1] == img_size[-2]:
            # fall-back to older behaviour so Resize scales to shortest edge if target is square
            scale_size = int(math.floor(img_size[0] / crop_pct))
        else:
            scale_size = tuple([int(x / crop_pct) for x in img_size])
    else:
        scale_size = int(math.floor(img_size / crop_pct))
    return transforms.Compose([
        Resize(scale_size, interpolation),
        transforms.CenterCrop(img_size),
        transforms.ToTensor(),
        transforms.ImageNormalize(mean=mean, std=std)
    ])
コード例 #9
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('--batch_size', type=int, default=8)
    parser.add_argument('--epochs', type=int, default=50)
    parser.add_argument('--num_classes', type=int, default=130)

    parser.add_argument('--lr', type=float, default=2e-3)
    parser.add_argument('--weight_decay', type=float, default=1e-5)

    parser.add_argument('--resume', type=bool, default=True)
    parser.add_argument('--eval', type=bool, default=False)

    parser.add_argument('--dataroot', type=str, default='/content/drive/MyDrive/dogfl/data/TEST_A/')
    parser.add_argument('--model_path', type=str, default='model/res152_8/model.bin:model/res152_10/model.bin:model/seres152_8/model.bin:model/seres152_10/model.bin')
    parser.add_argument('--model_name', type=str, default='Net1_z:Net1_z:Net10_z:Net10_z')

    parser.add_argument('--out_file', type=str, default='./result.json')


    args = parser.parse_args()

    root_dir = args.dataroot

    transform_test = transform.Compose([
        transform.Resize((256, 256)),
        transform.CenterCrop(224),
        transform.ToTensor(),
        transform.ImageNormalize(0.485, 0.229),
    ])

    model_zoo = args.model_path.split(':')
    model_name_class = args.model_name.split(':')
    print(model_zoo, model_name_class)
    zoo_pred_result = []
    i = 0
    for model_path_ in model_zoo:
        name_list = []
        for _, _, _name_list in os.walk(root_dir):
            name_list = _name_list
        val_loader = TsinghuaDogExam(root_dir, batch_size=args.batch_size, train=False, name_list=name_list, shuffle=False, transform=transform_test)

        model = eval(model_name_class[i])(num_classes=args.num_classes)
        if args.resume:
            model.load(model_path_)


        top5_class_list = evaluate(model, val_loader)
        # label start from 1, however it doesn't
        pred_result = dict(zip(name_list, top5_class_list))
        zoo_pred_result.append(pred_result)
        i += 1

    # vote the best
    president = zoo_pred_result[0]
    vote_result = {}
    for key in president.keys():
        val_list = []
        for i in range(5):
            candiates = [model[key][i] for model in zoo_pred_result]
            val, n = Counter(candiates).most_common()[0]
            val_list.append(val)
        vote_result[key] = val_list



    with open(args.out_file, 'w') as fout:
        json.dump(vote_result, fout, ensure_ascii=False, indent=4)