コード例 #1
0
    def data_process(self):
        normal = transforms.Normalize([0.485, 0.456, 0.406],
                                      [0.229, 0.224, 0.225])
        if cfg.use_autoaugment:
            train_transform = transforms.Compose([
                transforms.Resize(cfg.image_size),
                transforms.CenterCrop(cfg.crop_image_size),
                # transforms.RandomRotation(cfg.rotate_degree),
                auto_augment.AutoAugment(dataset=cfg.autoaugment_dataset),
                transforms.ToTensor(),
                transforms.RandomErasing(p=0.3,
                                         scale=(0.02, 0.1),
                                         ratio=(0.6, 1.2)),
                normal
            ])
        else:
            train_transform = transforms.Compose([
                transforms.Resize(cfg.image_size),
                transforms.CenterCrop(cfg.crop_image_size),
                transforms.RandomRotation(cfg.rotate_degree),
                transforms.ToTensor(),
                transforms.RandomErasing(p=0.3,
                                         scale=(0.02, 0.1),
                                         ratio=(0.6, 1.2)), normal
            ])

        test_trainsform = transforms.Compose([
            transforms.Resize(cfg.image_size),
            transforms.CenterCrop(cfg.crop_image_size),
            transforms.ToTensor(), normal
        ])

        return train_transform, test_trainsform
コード例 #2
0
    def predict(self, image_path):
        '''
        模型预测返回结果
        :param input:  评估传入样例 {"image_path":".\/data\/input\/BeijingGarbage\/image\/0.png"}
        :return: 模型预测成功之后返回给系统样例 {"label":"0"}
        '''
        # print(image_path)
        img = Image.open(image_path).convert('RGB')
        
        test_trainsform = transforms.Compose([
            transforms.Resize(cfg.image_size),
            transforms.CenterCrop(cfg.crop_image_size),
            auto_augment.AutoAugment(dataset=cfg.autoaugment_dataset),
            transforms.ToTensor(),
            transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
        ])

        # output = 0
        r1, r2, r3, r4, r5 = 0, 0, 0, 0, 0
        for _ in range(cfg.tta_times):
            tensor = test_trainsform(img)
            tensor = torch.unsqueeze(tensor, dim=0).float()

            v1, v2, v3, v4, v5 = self.model1(tensor.to(device))
            t1, t2, t3, t4, t5 = self.model2(tensor.to(device))
            # v1, v2, v3, v4, v5 = self.model1(tensor.to(device))
            r1 += v1
            r2 += v2
            r3 += v3
            r4 += v4
            r5 += v5
            r1 += t1
            r2 += t2
            r3 += t3
            r4 += t4
            r5 += t5
        
        val_pred1 = torch.max(r1, 1)[1]
        val_pred2 = torch.max(r2, 1)[1]
        val_pred3 = torch.max(r3, 1)[1]
        val_pred4 = torch.max(r4, 1)[1]
        val_pred5 = torch.max(r5, 1)[1]
   
        val_pred = torch.stack([val_pred1, val_pred2, val_pred3, val_pred4, val_pred5], 1).squeeze()
        pred = list(val_pred.cpu().numpy())
        pred = [i for i in pred if i != 10]
        temp = ''
        for v in pred:
            temp += str(v)
        return int(temp)
コード例 #3
0
    def __init__(self, args, mode='train'):
        self.args = args
        root_dir = args.dataset
        batch_size = args.batch_size
        augment = args.augment
        img_size = args.img_size

        self.resize_size = (int(img_size / 0.875), int(img_size / 0.875))
        self.crop_size = (img_size, img_size)

        self.root_dir = os.path.join(root_dir, mode)
        self.img_path_list = []
        self.label_list = []
        self.class_names = []
        for class_index, class_name in enumerate(os.listdir(self.root_dir)):
            class_dir = os.path.join(self.root_dir, class_name)
            for img_name in os.listdir(class_dir):
                self.img_path_list.append(os.path.join(class_dir, img_name))
                self.label_list.append(class_index)
            self.class_names.append(class_name)

        if mode == 'train':
            pad_len = len(self.img_path_list) % batch_size
            img_path_list_len = len(self.img_path_list)
            for _ in range(pad_len):
                rand_index = np.random.randint(0, img_path_list_len)
                self.img_path_list.append(self.img_path_list[rand_index])
                self.label_list.append(self.label_list[rand_index])
            self.data_index = np.arange(0, len(self.label_list))
            np.random.shuffle(self.data_index)
        else:
            self.data_index = np.arange(0, len(self.label_list))
        self.img_path_list = np.array(self.img_path_list)
        self.label_list = np.array(self.label_list)
        self.augment = augment
        self.mode = mode
        self.batch_size = batch_size
        self.eppch_index = 0
        self.num_class = len(self.class_names)

        self.random_crop_transform = A.Compose([
            A.RandomCrop(width=self.crop_size[0], height=self.crop_size[1]),
        ])
        self.center_crop_transform = A.Compose([
            A.CenterCrop(width=self.crop_size[0], height=self.crop_size[1]),
        ])

        self.auto_augment = auto_augment.AutoAugment()
        self.rand_augment = rand_augment.RandAugment(magnitude=10.)
コード例 #4
0
        f"/content/tianchiorgame/df_WeatherClassify/output/{config.prefix}_train.csv",
        index=None)
    val_df.to_csv(
        f"/content/tianchiorgame/df_WeatherClassify/output/{config.prefix}_val.csv",
        index=None)
    print(f"train length: {len(train_df)}, val length: {len(val_df)}")

    writer = SummaryWriter(log_dir="output/run/",
                           filename_suffix=config.prefix)

    # 数据加载
    train_augmentation = transforms.Compose([
        transforms.Resize(config.IMG_SIZE + 32),
        transforms.RandomResizedCrop(config.IMG_SIZE),
        transforms.RandomHorizontalFlip(),
        auto_augment.AutoAugment(),
        m_transform.RandomErasing(0.5, [0.02, 0.4], 0.3, 20),
        transforms.ToTensor(),
        transforms.Normalize(config.mean, config.std)
    ])
    val_augmentation = transforms.Compose([
        transforms.Resize(config.IMG_SIZE),
        transforms.CenterCrop(config.IMG_SIZE),
        transforms.ToTensor(),
        transforms.Normalize(config.mean, config.std)
    ])
    train_dataset = m_dataloader.IMetDataset(
        train_df,
        os.path.join("/content/drive/My Drive/train"),
        n_classes=config.NUM_CLASSES,
        transforms=train_augmentation)
コード例 #5
0
#训练中记录三个指标:验证集的最佳acc和对应的loss,验证集上的最低loss
metrics = {'best_acc': 10, 'best_acc_loss': 100, 'best_loss': 100}

train_transform = transforms.Compose([
    transforms.Resize((size + 32, size + 32)),
    transforms.RandomChoice([
        transforms.RandomCrop(size,
                              padding=1,
                              pad_if_needed=True,
                              padding_mode='edge'),
        transforms.RandomResizedCrop(size,
                                     scale=(resize_scale, 1.0),
                                     ratio=(0.8, 1.2))
    ]),
    transforms.RandomHorizontalFlip(),
    auto_augment.AutoAugment(dataset='CIFAR'),  #auto_augment
    transforms.ToTensor(),
    transforms.RandomErasing(p=erasing_prob),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
val_transform = transforms.Compose([
    transforms.Resize((size, size)),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

for fold_idx, (train_idx, val_idx) in enumerate(
        skf.split(train_df['filename'], train_df['label'])):
    print('*******************fold {0}*****************'.format(fold_idx))
    train_data = train_df.iloc[train_idx]
    val_data = train_df.iloc[val_idx]