def get_loaders(train_dir, train_maskdir, val_dir, val_maskdir, batch_size, train_transform, val_transform, num_workers=4, pin_memory=True): train_ds = CarvanaDataset( image_dir=train_dir, mask_dir=train_maskdir, transform=train_transform, ) train_loader = DataLoader(train_ds, batch_size=batch_size, num_workers=num_workers, pin_memory=pin_memory, shuffle=True) val_ds = CarvanaDataset( image_dir=val_dir, mask_dir=val_maskdir, transform=val_transform, ) val_loader = DataLoader(val_ds, batch_size=batch_size, num_workers=num_workers, pin_memory=pin_memory, shuffle=False) return train_loader, val_loader
def get_loaders( train_dir, train_maskdir, val_dir, val_maskdir, batch_size, train_transforms, val_transforms, num_workers=4, pin_memory=True ): train_ds = CarvanaDataset(train_dir, train_maskdir, train_transforms) train_loader = DataLoader( train_ds, batch_size=batch_size, num_workers=num_workers, pin_memory=pin_memory, shuffle=True ) val_ds = CarvanaDataset(val_dir, val_maskdir, val_transforms) val_loader = DataLoader( val_ds, batch_size=batch_size, num_workers=num_workers, pin_memory=pin_memory, shuffle=True ) return train_loader, val_loader
import matplotlib.pyplot as plt from dataset import CarvanaDataset random.seed(42) # %% image_dir = osp.join("data", "val_set", "images") mask_dir = osp.join("data", "val_set", "masks") # ================================================================= # # Load Data your dataset # # ================================================================= # # %% 커스텀 데이터 로드 dataset = CarvanaDataset(image_dir=image_dir, mask_dir=mask_dir, transform=None) # %% 이미지 & 마스크 가져오기 idx = random.randrange(0, len(dataset)) # __len__ 메소드 호출 image, mask = dataset[idx] # __getitem__ 메소드 호출 image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) print(image.shape) print(mask.shape) # %% overlay segmented image on top of main image in python """ Method1 (ref) https://stackoverflow.com/questions/57576686/how-to-overlay-segmented-image-on-top-of-main-image-in-python """
def val_dataloader(self): # OPTIONAL return DataLoader(CarvanaDataset(folds=self.val_folds), batch_size=config.VAL_BATCH_SIZE)
def train_dataloader(self): # REQUIRED return DataLoader(CarvanaDataset(folds=self.train_folds), shuffle=True, batch_size=config.TRAIN_BATCH_SIZE)
parser.add_argument('--momentum', type=float, default=0.5, metavar='M', help='SGD momentum (default: 0.5)') parser.add_argument('--no-cuda', action='store_true', default=False, help='disables CUDA training') parser.add_argument('--seed', type=int, default=1, metavar='S', help='random seed (default: 1)') parser.add_argument('--log-interval', type=int, default=10, metavar='N', help='how many batches to wait before logging training status') args = parser.parse_args() args.cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) if args.cuda: torch.cuda.manual_seed(args.seed) test_ds = CarvanaDataset() test_ds.initialize(args, phase='test') kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {} test_loader = DataLoader(test_ds, batch_size=args.batch_size, drop_last=True, **kwargs) model = CarvanaFvbNet() model.load_state_dict(torch.load('./checkpoints/latest_{}.pth'.format(args.which_epoch))) if args.cuda: model.cuda() def test(): model.eval() print(model) test_loss = 0 correct = 0 for data, target, dsidx in test_loader:
parser.add_argument('--vis-aimg', type=int, default=0, help='index of the image to visualize activations for') parser.add_argument('--vis-alayer', type=int, default=0, help='index of the layer to visualize (0-23)') args = parser.parse_args() args.cuda = not args.no_cuda and torch.cuda.is_available() torch.manual_seed(args.seed) if args.cuda: torch.cuda.manual_seed(args.seed) val_ds = CarvanaDataset() val_ds.initialize(args, phase='val') kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {} val_loader = DataLoader(val_ds, batch_size=args.batch_size, drop_last=True, **kwargs) model = CarvanaFvbNet() print(model) print('\nloading model params') model.load_state_dict( torch.load('./checkpoints/latest_{}.pth'.format(args.which_epoch))) if args.cuda: model.cuda() print('\nload complete!')