def get_data_transforms(self): cfg = self.cfg bbox_params = self.get_bbox_params() transform = Compose([Resize(cfg.data.img_sz, cfg.data.img_sz)], bbox_params=bbox_params) augmentors_dict = { 'Blur': Blur(), 'RandomRotate90': RandomRotate90(), 'HorizontalFlip': HorizontalFlip(), 'VerticalFlip': VerticalFlip(), 'GaussianBlur': GaussianBlur(), 'GaussNoise': GaussNoise(), 'RGBShift': RGBShift(), 'ToGray': ToGray() } aug_transforms = [] for augmentor in cfg.data.augmentors: try: aug_transforms.append(augmentors_dict[augmentor]) except KeyError as e: log.warning( '{0} is an unknown augmentor. Continuing without {0}. \ Known augmentors are: {1}'.format( e, list(augmentors_dict.keys()))) aug_transforms.append(Resize(cfg.data.img_sz, cfg.data.img_sz)) aug_transform = Compose(aug_transforms, bbox_params=bbox_params) return transform, aug_transform
def get_data_transforms(self) -> Tuple[BasicTransform, BasicTransform]: """Get albumentations transform objects for data augmentation. Returns: 1st tuple arg: a transform that doesn't do any data augmentation 2nd tuple arg: a transform with data augmentation """ cfg = self.cfg bbox_params = self.get_bbox_params() transform = Compose([Resize(cfg.data.img_sz, cfg.data.img_sz)], bbox_params=bbox_params) augmentors_dict = { 'Blur': Blur(), 'RandomRotate90': RandomRotate90(), 'HorizontalFlip': HorizontalFlip(), 'VerticalFlip': VerticalFlip(), 'GaussianBlur': GaussianBlur(), 'GaussNoise': GaussNoise(), 'RGBShift': RGBShift(), 'ToGray': ToGray() } aug_transforms = [] for augmentor in cfg.data.augmentors: try: aug_transforms.append(augmentors_dict[augmentor]) except KeyError as e: log.warning( '{0} is an unknown augmentor. Continuing without {0}. \ Known augmentors are: {1}'.format( e, list(augmentors_dict.keys()))) aug_transforms.append(Resize(cfg.data.img_sz, cfg.data.img_sz)) aug_transform = Compose(aug_transforms, bbox_params=bbox_params) return transform, aug_transform
def get_train_transform(image_size, augmentation=None): if augmentation is None: augmentation = 'none' LEVELS = { 'none': get_none_augmentations, 'light': get_light_augmentations, 'medium': get_medium_augmentations, 'hard': get_hard_augmentations, 'hard2': get_hard_augmentations_v2 } assert augmentation in LEVELS.keys() augmentation = LEVELS[augmentation](image_size) longest_size = max(image_size[0], image_size[1]) return A.Compose([ Resize(int(config.img_height * 1.5), int(config.img_weight * 1.5)), CenterCrop(config.img_height, config.img_weight), A.LongestMaxSize(longest_size, interpolation=cv2.INTER_CUBIC), A.PadIfNeeded(image_size[0], image_size[1], border_mode=cv2.BORDER_CONSTANT, value=0), augmentation, A.Normalize(), ToTensor() ])
def main(): # augmentation transform_aug = Compose([ aug.HueSaturationValue(), aug.RandomBrightnessContrast(), aug.CLAHE(), aug.JpegCompression(), aug.GaussNoise(), aug.MedianBlur(), aug.ElasticTransform(), aug.HorizontalFlip(), aug.Rotate(), aug.CoarseDropout(), aug.RandomSizedCrop() ], p=1) # transform for output transform = Compose([ Resize(cons.IMAGE_SIZE, cons.IMAGE_SIZE), Normalize( mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5), max_pixel_value=255.0) ], p=1) # Dataset ''' dataset = UkiyoeTrainDataset( train_images_path='data', train_labels_path='data', valid=False, confidence_boader=0.87, result_path='result/model_effi_b3/efficientnet_b3_980/inference_with_c.csv', test_images_path='data', over_sampling=False, transform_aug=None, augmix=False, mixup=False, transform=transform) img, label = dataset[0] #print(img.shape) #plt.imshow(img) #plt.show() ''' # train data loader loader = load_train_data(train_images_path='data', train_labels_path='data', batch_size=2, valid=False, nfold=0, transform_aug=None, augmix=True, mixup=False, transform=transform, as_numpy=True) image_batch, label_batch = next(loader.__iter__()) print(image_batch[0].shape) print(label_batch[0].shape) '''
def val_transfrom(self, image, image_size): transform = Compose([ Resize(image_size, image_size, interpolation=cv2.INTER_AREA), Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ToTensorV2() ]) image_transform = transform(image=image)['image'] return image_transform
def __init__(self, root_dir, transform=None): self.root_dir = root_dir self.fg_fns = os.listdir(Path(root_dir / 'fg')) self.bg_fns = os.listdir(Path(root_dir / 'bg')) self.rc320 = RandomCrop(height=320, width=320, always_apply=True) self.rc480 = RandomCrop(height=480, width=480, always_apply=True) self.rc640 = RandomCrop(height=640, width=640, always_apply=True) self.resize = Resize(height=320, width=320, always_apply=True) self.flip = Flip(p=.75)
def train_transfrom(image, size=256): transform = Compose([ Resize(size, size, interpolation=cv2.INTER_AREA), Flip(), RandomBrightnessContrast(), HueSaturationValue(), Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ToTensorV2() ]) image_transform = transform(image=image)['image'] return image_transform
def test_transfrom( image, size=256 ): # note that test image size is same as crop_size in get_transfrom transform = Compose([ Resize(size, size, interpolation=cv2.INTER_AREA), Normalize(mean=[0.3835, 0.3737, 0.3698], std=[1.0265, 1.0440, 1.0499]), ToTensorV2() ]) image_transform = transform(image=image)['image'] return image_transform
def base_transform(height, width, mappings, p=2 / 3): return Compose([ OneOf([ JpegCompression(quality_lower=20, quality_upper=70, p=0.5), Downscale(scale_min=0.25, scale_max=0.50, interpolation=1, p=0.5), Resize(height // 4, width // 4, interpolation=1, p=0.5) ], p=1.0), HorizontalFlip(p=0.5) ], p=p, additional_targets=mappings)
def main(argv=None): loader = load_test_data( data_path=FLAGS.test_images_path, batch_size=1, transform=Compose([ Resize(cons.IMAGE_SIZE,cons.IMAGE_SIZE), Normalize(mean=(0.5,0.5,0.5),std=(0.5,0.5,0.5),max_pixel_value=255.0)])) data_len = UkiyoeTestDataset(data_path=FLAGS.test_images_path).__len__() confidence_sum = np.zeros((data_len,cons.NUM_CLASSES)) ratio_sum = 0 for e in range(len(PARAM_LIST)): ratio_sum = ratio_sum + PARAM_LIST[e]['ratio'] PARAM_PATH = FLAGS.params_path + PARAM_LIST[e]['model'] model = models.get_model(model_name=PARAM_LIST[e]['model'].split('/')[2],num_classes=cons.NUM_CLASSES) if PARAM_LIST[e]['fix']: model.load_state_dict(fix_model_state_dict(torch.load(PARAM_PATH))) else: model.load_state_dict(torch.load(PARAM_PATH)) confidence_sum = confidence_sum + inference_loop(model,loader,data_len)*PARAM_LIST[e]['ratio'] pred = np.argmax(confidence_sum, axis=1) confidence = confidence_sum/ratio_sum if FLAGS.adjust_inference: pred = adjust_inference(pred,confidence,data_len) result = pd.DataFrame(columns=['id','y']) if FLAGS.output_confidence: result_with_c = pd.DataFrame(columns=['id','y','confidence']) for idx,feed in enumerate(loader): id,_ = feed result = result.append( pd.Series( [id.data.numpy()[0], pred[idx]], index=result.columns), ignore_index=True) if FLAGS.output_confidence: result_with_c = result_with_c.append( pd.Series( [id.data.numpy()[0], pred[idx], confidence[idx,pred[idx]]], index=result_with_c.columns), ignore_index=True) RESULT_DIR = FLAGS.result_path +'/'+ FLAGS.case os.makedirs(RESULT_DIR,exist_ok=True) result.to_csv(RESULT_DIR +'/inference.csv', index=False) if FLAGS.output_confidence: result_with_c[['id','y']] = result_with_c[['id','y']].astype(int) result_with_c.to_csv(RESULT_DIR +'/inference_with_c.csv', index=False)
def get_transfrom(image, size=512, crop_size=256): transform = Compose([ Resize(size, size, interpolation=cv2.INTER_AREA), RandomCrop(crop_size, crop_size), Cutout(num_holes=4), HueSaturationValue(hue_shift_limit=0.2, sat_shift_limit=0.2, val_shift_limit=0.2), RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2), Flip(), Normalize(mean=[0.3835, 0.3737, 0.3698], std=[1.0265, 1.0440, 1.0499]), ToTensorV2() ]) image_transform = transform(image=image)['image'] return image_transform
def main(argv=None): PARAM_PATH = FLAGS.params_path + '/' + FLAGS.case + '/' + FLAGS.model_name + '/fold' + str( FLAGS.nfold) RESULT_DIR = FLAGS.result_path + '/' + FLAGS.case + '/' + FLAGS.model_name if FLAGS.pseudo_labeling: PARAM_PATH = PARAM_PATH + '/with_pseudo_labeling' RESULT_DIR = RESULT_DIR + '/with_pseudo_labeling' PARAM_PATH = PARAM_PATH + '/' + FLAGS.case RESULT_DIR = RESULT_DIR + '/' + FLAGS.case if FLAGS.is_best_param: PARAM_PATH = PARAM_PATH + '_best' RESULT_DIR = RESULT_DIR + '_best' else: PARAM_PATH = PARAM_PATH + '_' + str(FLAGS.executed_epoch) RESULT_DIR = RESULT_DIR + '_' + str(FLAGS.executed_epoch) if FLAGS.is_final_epoch: PARAM_PATH = PARAM_PATH + '_final' RESULT_DIR = RESULT_DIR + '_final' PARAM_PATH = PARAM_PATH + '.pth' loader = load_test_data(data_path=FLAGS.test_images_path, batch_size=1, transform=Compose([ Resize(cons.IMAGE_SIZE, cons.IMAGE_SIZE), Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5), max_pixel_value=255.0) ])) model = models.get_model(model_name=FLAGS.model_name, num_classes=cons.NUM_CLASSES) if FLAGS.fix_state_dict: model.load_state_dict(fix_model_state_dict(torch.load(PARAM_PATH))) else: model.load_state_dict(torch.load(PARAM_PATH)) result = inference_loop(model, loader, FLAGS.output_confidence) os.makedirs(RESULT_DIR, exist_ok=True) result.to_csv(RESULT_DIR + '/inference.csv', index=False) if FLAGS.output_confidence: result.to_csv(RESULT_DIR + '/inference_with_c.csv', index=False)
def transform(self, img: t.Any) -> t.Any: max_hw = max(img.shape[0:2]) img = PadIfNeeded(max_hw, max_hw, border_mode=cv2.BORDER_REPLICATE)(image=img)[ "image" ] img = Resize(self.resolution, self.resolution)(image=img)["image"] img = Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225],)( image=img )["image"] if self.mode == "Train": img = RandomResizedCrop(self.resolution, self.resolution)(image=img)[ "image" ] img = Cutout(p=0.2)(image=img)["image"] img = HorizontalFlip(p=0.5)(image=img)["image"] img = ShiftScaleRotate( shift_limit=0.1, scale_limit=0.0, rotate_limit=10, p=0.3 )(image=img)["image"] img = ToTensorV2()(image=img)["image"] return img
def even_more_transform(height, width, mappings, p=2 / 3): scale = random.randint(2, 4) return Compose([ OneOf([ JpegCompression(quality_lower=20, quality_upper=70, p=0.5), Downscale(scale_min=0.25, scale_max=0.50, interpolation=1, p=0.5), Resize(height // scale, width // scale, interpolation=1, p=1.0) ], p=0.6), HorizontalFlip(p=0.5), A.augmentations.transforms.GaussNoise(p=0.2), A.RandomBrightnessContrast(p=0.2), A.RandomGamma(p=0.2), A.CLAHE(p=0.2), A.ChannelShuffle(p=0.2), A.MultiplicativeNoise(multiplier=[0.5, 1.5], elementwise=True, p=0.1), A.HueSaturationValue( hue_shift_limit=10, sat_shift_limit=10, val_shift_limit=10, p=0.2), ], p=0.9, additional_targets=mappings)
def preprocess(resize): return Compose([ Resize(height=resize, width=resize, p=1), Lambda(image=preprocess_input) ], p=1) # always apply
def __getitem__(self, index: int): while True: video, img_file, label, ori_video, frame, fold = self.data[index] try: if self.mode == "train": label = np.clip(label, self.label_smoothing, 1 - self.label_smoothing) img_path = os.path.join(self.data_root, self.crops_dir, video, img_file) image = cv2.imread(img_path, cv2.IMREAD_COLOR) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image_width, image_height = image.shape[:2] mask = np.zeros(image.shape[:2], dtype=np.uint8) diff_path = os.path.join(self.data_root, "diffs", video, img_file[:-4] + "_diff.png") landmark_path = os.path.join(self.data_root, "landmarks", ori_video, img_file[:-4] + ".npy") try: msk = cv2.imread(diff_path, cv2.IMREAD_GRAYSCALE) if msk is not None: mask = msk except: print("not found mask", diff_path) pass if self.mode == "train" and self.hardcore and not self.rotation: if os.path.exists(landmark_path) and random.random() < 0.7: landmarks = np.load(landmark_path) image = remove_landmark(image, landmarks) elif random.random() < 0.2: blackout_convex_hull(image) elif random.random() < 0.1: binary_mask = mask > 0.4 * 255 masks = prepare_bit_masks( (binary_mask * 1).astype(np.uint8)) tries = 6 current_try = 1 while current_try < tries: bitmap_msk = random.choice(masks) if label < 0.5 or np.count_nonzero( mask * bitmap_msk) > 20: mask *= bitmap_msk image *= np.expand_dims(bitmap_msk, axis=-1) break current_try += 1 if self.mode == "train" and self.padding_part > 3: image = change_padding(image, self.padding_part) valid_label = np.count_nonzero( mask[mask > 20]) > 32 or label < 0.5 valid_label = 1 if valid_label else 0 rotation = 0 if self.transforms: data = self.transforms(image=image, mask=mask) image = data["image"] mask = data["mask"] if self.mode == "train" and self.hardcore and self.rotation: # landmark_path = os.path.join(self.data_root, "landmarks", ori_video, img_file[:-4] + ".npy") dropout = 0.8 if label > 0.5 else 0.6 if self.rotation: dropout *= 0.7 elif random.random() < dropout: blackout_random(image, mask, label) # # os.makedirs("../images", exist_ok=True) # cv2.imwrite(os.path.join("../images", video+ "_" + str(1 if label > 0.5 else 0) + "_"+img_file), image[...,::-1]) if self.mode == "train" and self.rotation: rotation = random.randint(0, 3) image = rot90(image, rotation) image = img_to_tensor(image, self.normalize) # assuming the image size is 380*380 image_size = 380 image = Resize(image_size, image_size) rect = detector(img)[0] x = rect.left() y = rect.top() w = rect.right() - x h = rect.buttom() - y top_left = np.array([x, y]) top_right = np.array([x + w, y]) bottom_left = np.array([x, y + h]) bottom_right = np.array([x + w, y + h]) # insert the face rectangle points into the landmark data landmark = np.insert(landmark, 0, top_left, axis=0) landmark = np.insert(landmark, 0, top_right, axis=0) landmark = np.insert(landmark, 0, bottom_left, axis=0) landmark = np.insert(landmark, 0, bottom_right, axis=0) # perform scaling for the landmark for i in landmark: i[0] = round(image_size * (i[0] / image_width)) i[1] = round(image_size * (i[1] / image_height)) return { "image": image, "labels": np.array((label, )), "img_name": os.path.join(video, img_file), "valid": valid_label, "rotations": rotation, "landmark": landmark } except Exception as e: traceback.print_exc(file=sys.stdout) print( "Broken image", os.path.join(self.data_root, self.crops_dir, video, img_file)) index = random.randint(0, len(self.data) - 1)
def transform(height, width, mappings, p=1.0): return Compose([ Resize(height//4,width//4, interpolation=1, p=1.0) ], p=p, additional_targets=mappings)
from albumentations import ( HorizontalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90, Transpose, Blur, OpticalDistortion, GridDistortion, HueSaturationValue, IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, IAASharpen, IAAEmboss, RandomContrast, RandomBrightness, Flip, OneOf, Compose, RandomGamma, ElasticTransform, ChannelShuffle,RGBShift, Rotate, Cutout ) # YOUR PATH path = f'/mnt/c/Users/bokhy/Desktop/Python/github/kaggle/bengaliai-cv19/input/' HEIGHT = 137 WIDTH = 236 # Augmentation train_augmentation = Compose([ Resize(HEIGHT,WIDTH, always_apply= True), Normalize(mean, std, always_apply = True), Rotate(), Flip(), OneOf([ IAAAdditiveGaussianNoise(), GaussNoise(), ], p=0.2), OneOf([ MotionBlur(p=0.2), MedianBlur(blur_limit=3, p=0.1), Blur(blur_limit=3, p=0.1), ], p=0.2), ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.2, rotate_limit=45, p=.2), OneOf([ OpticalDistortion(p=0.3),
import albumentations from albumentations.augmentations.transforms import Resize, RandomSizedCrop, ShiftScaleRotate, HorizontalFlip, Normalize, RandomBrightnessContrast, MotionBlur, Blur, GaussNoise, JpegCompression train_transform = albumentations.Compose([ RandomSizedCrop(min_max_height=(input_size//3,input_size//3),height=input_size,width=input_size), ShiftScaleRotate(p=0.3, scale_limit=0.25, border_mode=1, rotate_limit=25), HorizontalFlip(p=0.2), RandomBrightnessContrast(p=0.3, brightness_limit=0.25, contrast_limit=0.5), MotionBlur(p=.2), GaussNoise(p=.2), JpegCompression(p=.2, quality_lower=50), Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) val_transform = albumentations.Compose([ Resize(input_size,input_size), Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) class MyDataset(data.Dataset): def __init__(self,root,transforms = None,is_train=True): """ 主要目标获取所有图片的地址 """ fake_path = os.path.join(root,"fake") real_path = os.path.join(root,"real") fake_imgs = [os.path.join(fake_path,each) for each in os.listdir(fake_path) if each.endswith(".jpg") or each.endswith(".png") or each.endswith(".jpeg") or each.endswith(".JPG")] real_imgs = [os.path.join(real_path,each) for each in os.listdir(real_path) if each.endswith(".jpg") or each.endswith(".png") or each.endswith(".jpeg") or each.endswith(".JPG")] imgs = fake_imgs+real_imgs
def main(argv=None): transform = Compose([ Resize(cons.IMAGE_SIZE, cons.IMAGE_SIZE), Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5), max_pixel_value=255.0) ]) valid_loader = load_train_data(train_images_path=FLAGS.train_images_path, train_labels_path=FLAGS.train_labels_path, batch_size=FLAGS.batch_size, num_worker=FLAGS.num_worker, valid=True, nfold=FLAGS.nfold, transform=transform) model = models.get_model(model_name=FLAGS.model_name, num_classes=cons.NUM_CLASSES) model.cuda() #model = torch.nn.DataParallel(model) DIR = '/' + FLAGS.case + '/' + FLAGS.model_name + '/fold' + str( FLAGS.nfold) RESULT_PATH = '' if FLAGS.confidence_border is not None: DIR = DIR + '/with_pseudo_labeling' RESULT_PATH = RESULT_PATH + FLAGS.result_path if FLAGS.result_case is not None: RESULT_PATH = RESULT_PATH + '/' + FLAGS.result_case RESULT_PATH = RESULT_PATH + '/inference_with_c.csv' PARAM_DIR = FLAGS.params_path + DIR os.makedirs(PARAM_DIR, exist_ok=True) PARAM_NAME = PARAM_DIR + '/' + FLAGS.case if FLAGS.executed_epoch > 0: TRAINED_PARAM_PATH = FLAGS.restart_param_path + '/' + FLAGS.case + str( FLAGS.executed_epoch) restart_epoch = FLAGS.executed_epoch + 1 if FLAGS.restart_from_final: TRAINED_PARAM_PATH = TRAINED_PARAM_PATH + '_final' TRAINED_PARAM_PATH = TRAINED_PARAM_PATH + '.pth' model.load_state_dict(torch.load(TRAINED_PARAM_PATH)) else: restart_epoch = 0 optimizer = optim.Adam(model.parameters(), lr=cons.start_lr) model, optimizer = amp.initialize(model, optimizer, opt_level=FLAGS.opt_level) if FLAGS.add_class_weight: loader = load_train_data(train_images_path=FLAGS.train_images_path, train_labels_path=FLAGS.train_labels_path, batch_size=FLAGS.batch_size, num_worker=FLAGS.num_worker, nfold=FLAGS.nfold) count_label = np.zeros(10, dtype=np.int64) for feed in loader: _, labels = feed count_label += np.sum(labels.numpy().astype(np.int64), axis=0) weight = torch.from_numpy(count_label).cuda() else: weight = None criterion = nn.BCEWithLogitsLoss(weight=weight) writer = SummaryWriter(log_dir=FLAGS.logs_path + DIR + '/tensorboardX/') best_acc = 0 if FLAGS.augmentation and FLAGS.aug_decrease: p = 0.5 for e in range(restart_epoch, FLAGS.final_epoch): p_partical = p * (FLAGS.final_epoch - e) / FLAGS.final_epoch lr = set_lr.cosine_annealing(optimizer, cons.start_lr, e, 100) writer.add_scalar('LearningRate', lr, e) train_loader = load_train_data( train_images_path=FLAGS.train_images_path, train_labels_path=FLAGS.train_labels_path, batch_size=FLAGS.batch_size, num_worker=FLAGS.num_worker, nfold=FLAGS.nfold, confidence_border=FLAGS.confidence_border, result_path=RESULT_PATH, test_images_path=FLAGS.test_images_path, over_sampling=FLAGS.over_sampling, transform_aug=Compose([ aug.HueSaturationValue(p=p_partical), aug.RandomBrightnessContrast(p=p_partical), aug.CLAHE(p=p_partical), aug.JpegCompression(p=p_partical), aug.GaussNoise(p=p), aug.MedianBlur(p=p), aug.ElasticTransform(p=p_partical), aug.HorizontalFlip(p=p), aug.Rotate(p=p), aug.CoarseDropout(p=p_partical), aug.RandomSizedCrop(p=p) ]), mixup=FLAGS.mixup, transform=transform) train_loss = train_loop(model, train_loader, criterion, optimizer) writer.add_scalar('train_loss', train_loss, e) valid_loss, valid_acc = valid_loop(model, valid_loader, criterion) writer.add_scalar('valid_loss', valid_loss, e) writer.add_scalar('valid_acc', valid_acc, e) print( 'Epoch: {}, Train Loss: {:.4f}, Valid Loss: {:.4f}, Valid Accuracy:{:.2f}' .format(e + 1, train_loss, valid_loss, valid_acc)) if e % 10 == 0: torch.save(model.state_dict(), PARAM_NAME + '_' + str(e) + '.pth') if valid_acc > best_acc: best_acc = valid_acc torch.save(model.state_dict(), PARAM_NAME + '_best.pth') else: if FLAGS.augmentation and not FLAGS.augmix: transform_aug = Compose([ aug.HueSaturationValue(), aug.RandomBrightnessContrast(), aug.CLAHE(), aug.JpegCompression(), aug.GaussNoise(), aug.MedianBlur(), aug.ElasticTransform(), aug.HorizontalFlip(), aug.Rotate(), aug.CoarseDropout(), aug.RandomSizedCrop() ]) else: transform_aug = None train_loader = load_train_data( train_images_path=FLAGS.train_images_path, train_labels_path=FLAGS.train_labels_path, batch_size=FLAGS.batch_size, num_worker=FLAGS.num_worker, valid=False, nfold=FLAGS.nfold, over_sampling=FLAGS.over_sampling, transform_aug=transform_aug, augmix=FLAGS.augmix, mixup=FLAGS.mixup, transform=transform) total_time = 0 for e in range(restart_epoch, FLAGS.final_epoch): start = time.time() lr = set_lr.cosine_annealing(optimizer, cons.start_lr, e, 100) writer.add_scalar('LearningRate', lr, e) train_loss = train_loop(model, train_loader, criterion, optimizer) writer.add_scalar('train_loss', train_loss, e) valid_loss, valid_acc = valid_loop(model, valid_loader, criterion) writer.add_scalar('valid_loss', valid_loss, e) writer.add_scalar('valid_acc', valid_acc, e) print( 'Epoch: {}, Train Loss: {:.4f}, Valid Loss: {:.4f}, Valid Accuracy:{:.2f}' .format(e + 1, train_loss, valid_loss, valid_acc)) if e % 10 == 0: torch.save(model.state_dict(), PARAM_NAME + '_' + str(e) + '.pth') if valid_acc > best_acc: best_acc = valid_acc torch.save(model.state_dict(), PARAM_NAME + '_best.pth') total_time = total_time + (time.time() - start) print('average time: {}[sec]'.format(total_time / (e + 1))) torch.save(model.state_dict(), PARAM_NAME + '_' + str(FLAGS.final_epoch - 1) + '_final.pth')
def main(): global args, config, best_loss args = parser.parse_args() with open(args.config) as f: config = yaml.load(f) for k, v in config['common'].items(): setattr(args, k, v) config = EasyDict(config['common']) rank, world_size, device_id = dist_init( os.path.join(args.distributed_path, config.distributed_file)) args.save_path_dated = args.save_path + '/' + args.datetime if args.run_tag != '': args.save_path_dated += '-' + args.run_tag # create model model = model_entry(config.model) model.cuda() model = nn.parallel.DistributedDataParallel(model, device_ids=[device_id]) # create optimizer opt_config = config.optimizer opt_config.kwargs.lr = config.lr_scheduler.base_lr opt_config.kwargs.params = model.parameters() optimizer = optim_entry(opt_config) # optionally resume from a checkpoint last_iter = -1 best_loss = 1e9 if args.load_path: if args.recover: best_loss, last_iter = load_state(args.load_path, model, optimizer=optimizer) else: load_state(args.load_path, model) cudnn.benchmark = True # train augmentation if config.augmentation.get('imgnet_mean', False): model_mean = (0.485, 0.456, 0.406) model_std = (0.229, 0.224, 0.225) else: model_mean = (0.5, 0.5, 0.5) model_std = (0.5, 0.5, 0.5) trans = albumentations.Compose([ RandomResizedCrop(config.augmentation.input_size, config.augmentation.input_size, scale=(config.augmentation.min_scale**2., 1.), ratio=(1., 1.)), HorizontalFlip(p=0.5), RandomBrightnessContrast(brightness_limit=0.25, contrast_limit=0.1, p=0.5), JpegCompression(p=.2, quality_lower=50), MotionBlur(p=0.5), Normalize(mean=model_mean, std=model_std), ToTensorV2() ]) train_dataset = FaceDataset(config.train_root, config.train_source, transform=trans, resize=config.augmentation.input_size, image_format=config.get('image_format', None), random_frame=config.get( 'train_random_frame', False), bgr=config.augmentation.get('bgr', False)) train_sampler = DistributedGivenIterationSampler( train_dataset, config.lr_scheduler.max_iter, config.batch_size, last_iter=last_iter) train_loader = DataLoader(train_dataset, batch_size=config.batch_size, shuffle=False, num_workers=config.workers, pin_memory=True, sampler=train_sampler) # validation augmentation trans = albumentations.Compose([ Resize(config.augmentation.input_size, config.augmentation.input_size), Normalize(mean=model_mean, std=model_std), ToTensorV2() ]) val_multi_loader = [] if args.val_source != '': for dataset_idx in range(len(args.val_source)): val_dataset = FaceDataset( args.val_root[dataset_idx], args.val_source[dataset_idx], transform=trans, output_index=True, resize=config.augmentation.input_size, image_format=config.get('image_format', None), bgr=config.augmentation.get('bgr', False)) val_sampler = DistributedSampler(val_dataset, round_up=False) val_loader = DataLoader(val_dataset, batch_size=config.batch_size, shuffle=False, num_workers=config.workers, pin_memory=True, sampler=val_sampler) val_multi_loader.append(val_loader) config.lr_scheduler['optimizer'] = optimizer config.lr_scheduler['last_iter'] = last_iter lr_scheduler = get_scheduler(config.lr_scheduler) if rank == 0: mkdir(args.save_path) mkdir(args.save_path_dated) tb_logger = SummaryWriter(args.save_path_dated) logger = create_logger('global_logger', args.save_path_dated + '-log.txt') logger.info('{}'.format(args)) logger.info(model) logger.info(parameters_string(model)) logger.info('len(train dataset) = %d' % len(train_loader.dataset)) for dataset_idx in range(len(val_multi_loader)): logger.info( 'len(val%d dataset) = %d' % (dataset_idx, len(val_multi_loader[dataset_idx].dataset))) mkdir(args.save_path_dated + '/saves') else: tb_logger = None positive_weight = config.get('positive_weight', 0.5) weight = torch.tensor([1. - positive_weight, positive_weight]) * 2. if rank == 0: logger.info('using class weights: {}'.format(weight.tolist())) criterion = nn.CrossEntropyLoss(weight=weight).cuda() if args.evaluate: if args.evaluate_path: all_ckpt = get_all_checkpoint(args.evaluate_path, args.range_list, rank) for ckpt in all_ckpt: if rank == 0: logger.info('Testing ckpt: ' + ckpt) last_iter = -1 _, last_iter = load_state(ckpt, model, optimizer=optimizer) for dataset_idx in range(len(val_multi_loader)): validate(dataset_idx, val_multi_loader[dataset_idx], model, criterion, tb_logger, curr_step=last_iter, save_softmax=True) else: for dataset_idx in range(len(val_multi_loader)): validate(dataset_idx, val_multi_loader[dataset_idx], model, criterion, tb_logger, curr_step=last_iter, save_softmax=True) return train(train_loader, val_multi_loader, model, criterion, optimizer, lr_scheduler, last_iter + 1, tb_logger) return
def train_transform(h, w): return A.Compose([ Resize(h, w), A.Normalize(), ToTensorV2(), ])
train_df, val_df = train_test_split(train, test_size=0.1, random_state=seed, stratify=train.iloc[:, 0]) train_df = train_df.reset_index(drop=True) val_df = val_df.reset_index(drop=True) train_augmentations = Compose([ albumentations.OneOf([ GridMask(num_grid=3, mode=0, rotate=15), GridMask(num_grid=3, mode=2, rotate=15), ], p=0.7), RandomAugMix(severity=4, width=3, alpha=1.0, p=0.7), Resize(*img_size), ShiftScaleRotate(shift_limit=0, scale_limit=0, rotate_limit=10, p=0.5), ToFloat(max_value=255), ToTensor() ], p=1) val_augmentations = Compose( [Resize(*img_size), ToFloat(max_value=255), ToTensor()], p=1) train_dataset = digitdataset(data=train_df, transform=train_augmentations) val_dataset = digitdataset(data=val_df, transform=val_augmentations) # ============================================================================= # image , label = train_dataset.__getitem__(15) #