def main(args): model = models.__dict__[args.arch](pretrained=True) model = model.eval() model, device = UtilsFactory.prepare_model(model) labels = json.loads(open(args.labels).read()) i2k = Images2Keywords(model, args.n_keywords, labels) images_df = pd.read_csv(args.in_csv) images_df = images_df.reset_index().drop("index", axis=1) images_df = list(images_df.to_dict("index").values()) open_fn = ImageReader(input_key=args.img_col, output_key="image", datapath=args.datapath) dataloader = UtilsFactory.create_loader(images_df, open_fn, batch_size=args.batch_size, workers=args.n_workers, dict_transform=dict_transformer) keywords = [] dataloader = tqdm(dataloader) if args.verbose else dataloader with torch.no_grad(): for batch in dataloader: keywords_batch = i2k(batch["image"].to(device)) keywords += keywords_batch input_csv = pd.read_csv(args.in_csv) input_csv[args.keywords_col] = keywords input_csv.to_csv(args.out_csv, index=False)
def create_reader(root, num_classes): # ReaderCompose collects different Readers into one pipeline open_fn = ReaderCompose([ # Reads images from the `datapath` folder # using the key `input_key =" filepath "` (here should be the filename) # and writes it to the output dictionary by `output_key="features"` key ImageReader(input_key="filepath", output_key="features", datapath=root), # Reads a number from our dataframe # by the key `input_key =" label "` to np.long # and writes it to the output dictionary by `output_key="targets"` key ScalarReader(input_key="label", output_key="targets", default_value=-1, dtype=np.int64), # Same as above, but with one encoding ScalarReader(input_key="label", output_key="targets_one_hot", default_value=-1, dtype=np.int64, one_hot_classes=num_classes) ]) return open_fn
def main(args, _=None): global IMG_SIZE IMG_SIZE = (args.img_size, args.img_size) model = ResnetEncoder(arch=args.arch, pooling=args.pooling) model = model.eval() model, _, _, _, device = utils.process_components(model=model) images_df = pd.read_csv(args.in_csv) images_df = images_df.reset_index().drop("index", axis=1) images_df = list(images_df.to_dict("index").values()) open_fn = ImageReader(input_key=args.img_col, output_key="image", datapath=args.datapath) dataloader = utils.get_loader(images_df, open_fn, batch_size=args.batch_size, num_workers=args.num_workers, dict_transform=dict_transformer) features = [] dataloader = tqdm(dataloader) if args.verbose else dataloader with torch.no_grad(): for batch in dataloader: features_ = model(batch["image"].to(device)) features_ = features_.cpu().detach().numpy() features.append(features_) features = np.concatenate(features, axis=0) np.save(args.out_npy, features)
def _get_train_open_fn(train_folder): return ReaderCompose( readers=[RowsReader(reader=ReaderCompose( readers=[ ImageReader(row_key="Image", dict_key="Image", datapath=train_folder), TextReader(row_key="Id", dict_key="Id"), TextReader(row_key="Image", dict_key="ImageFile")]))], mixins=[SiameseLabelMixin( dict_first_id_key="Id0", dict_second_id_key="Id1")] )
def get_loaders(train_transforms_fn, valid_transforms_fn, config, batch_size: int = 8, num_workers: int = 20, sampler=None) -> OrderedDict: train_data, valid_data = get_datasets(config) open_fn = ReaderCompose([ ImageReader(input_key="filepath", output_key="features", rootpath=config.root_images), ScalarReader(input_key="disease_type", output_key="targets", default_value=-1, dtype=np.int64), ScalarReader(input_key="disease_type", output_key="targets_one_hot", default_value=-1, dtype=np.int64, one_hot_classes=config.num_classes) ]) train_loader = utils.get_loader( train_data, open_fn=open_fn, dict_transform=train_transforms_fn, batch_size=batch_size, num_workers=num_workers, shuffle=sampler is None, # shuffle data only if Sampler is not specified (PyTorch requirement) sampler=sampler, drop_last=True, ) valid_loader = utils.get_loader( valid_data, open_fn=open_fn, dict_transform=valid_transforms_fn, batch_size=batch_size, num_workers=num_workers, shuffle=False, sampler=None, drop_last=True, ) loaders = OrderedDict() loaders["train"] = train_loader loaders["valid"] = valid_loader return loaders
def get_open_fn(data_root, num_class): open_fn = ReaderCompose([ ImageReader(input_key="filepath", output_key="features", datapath=data_root), ScalarReader(input_key="label", output_key="targets", default_value=-1, dtype=np.int64), ScalarReader(input_key="label", output_key="targets_one_hot", default_value=-1, dtype=np.int64, one_hot_classes=num_class) ]) return open_fn
def main(args, _=None): global IMG_SIZE utils.set_global_seed(args.seed) utils.prepare_cudnn(args.deterministic, args.benchmark) IMG_SIZE = (args.img_size, args.img_size) if args.traced_model is not None: device = utils.get_device() model = torch.jit.load(str(args.traced_model), map_location=device) else: model = ResnetEncoder(arch=args.arch, pooling=args.pooling) model = model.eval() model, _, _, _, device = utils.process_components(model=model) df = pd.read_csv(args.in_csv) df = df.reset_index().drop("index", axis=1) df = list(df.to_dict("index").values()) open_fn = ImageReader(input_key=args.img_col, output_key="image", datapath=args.datapath) dataloader = utils.get_loader(df, open_fn, batch_size=args.batch_size, num_workers=args.num_workers, dict_transform=dict_transformer) features = [] dataloader = tqdm(dataloader) if args.verbose else dataloader with torch.no_grad(): for batch in dataloader: features_ = model(batch["image"].to(device)) features_ = features_.cpu().detach().numpy() features.append(features_) features = np.concatenate(features, axis=0) np.save(args.out_npy, features)
def prepare_loaders(*, mode: str, stage: str = None, n_workers: int = None, batch_size: int = None, datapath=None, in_csv=None, in_csv_train=None, in_csv_valid=None, in_csv_infer=None, train_folds=None, valid_folds=None, tag2class=None, class_column=None, tag_column=None, folds_seed=42, n_folds=5): loaders = collections.OrderedDict() df, df_train, df_valid, df_infer = parse_in_csvs( in_csv=in_csv, in_csv_train=in_csv_train, in_csv_valid=in_csv_valid, in_csv_infer=in_csv_infer, train_folds=train_folds, valid_folds=valid_folds, tag2class=tag2class, class_column=class_column, tag_column=tag_column, folds_seed=folds_seed, n_folds=n_folds) open_fn = [ ImageReader(input_key="filepath", output_key="image", datapath=datapath), ScalarReader(input_key="class", output_key="targets", default_value=-1, dtype=np.int64) ] open_fn = ReaderCompose(readers=open_fn) if len(df_train) > 0: labels = [x["class"] for x in df_train] sampler = BalanceClassSampler(labels, mode="upsampling") train_loader = UtilsFactory.create_loader( data_source=df_train, open_fn=open_fn, dict_transform=DataSource.prepare_transforms(mode="train", stage=stage), dataset_cache_prob=-1, batch_size=batch_size, workers=n_workers, shuffle=sampler is None, sampler=sampler) print("Train samples", len(train_loader) * batch_size) print("Train batches", len(train_loader)) loaders["train"] = train_loader if len(df_valid) > 0: sampler = None valid_loader = UtilsFactory.create_loader( data_source=df_valid, open_fn=open_fn, dict_transform=DataSource.prepare_transforms(mode="valid", stage=stage), dataset_cache_prob=-1, batch_size=batch_size, workers=n_workers, shuffle=False, sampler=sampler) print("Valid samples", len(valid_loader) * batch_size) print("Valid batches", len(valid_loader)) loaders["valid"] = valid_loader if len(df_infer) > 0: infer_loader = UtilsFactory.create_loader( data_source=df_infer, open_fn=open_fn, dict_transform=DataSource.prepare_transforms(mode="infer", stage=None), dataset_cache_prob=-1, batch_size=batch_size, workers=n_workers, shuffle=False, sampler=None) print("Infer samples", len(infer_loader) * batch_size) print("Infer batches", len(infer_loader)) loaders["infer"] = infer_loader return loaders
def get_datasets(self, stage: str, datapath: str = None, in_csv: str = None, in_csv_train: str = None, in_csv_valid: str = None, in_csv_infer: str = None, train_folds: str = None, valid_folds: str = None, tag2class: str = None, class_column: str = None, tag_column: str = None, folds_seed: int = 42, n_folds: int = 5, one_hot_classes: int = None, image_size: int = 224): datasets = collections.OrderedDict() tag2class = json.load(open(tag2class)) \ if tag2class is not None \ else None df, df_train, df_valid, df_infer = read_csv_data( in_csv=in_csv, in_csv_train=in_csv_train, in_csv_valid=in_csv_valid, in_csv_infer=in_csv_infer, train_folds=train_folds, valid_folds=valid_folds, tag2class=tag2class, class_column=class_column, tag_column=tag_column, seed=folds_seed, n_folds=n_folds) open_fn = [ ImageReader(input_key="filepath", output_key="image", datapath=datapath), ScalarReader(input_key="class", output_key="targets", default_value=-1, dtype=np.int64) ] if one_hot_classes: open_fn.append( ScalarReader(input_key="class", output_key="targets_one_hot", default_value=-1, dtype=np.int64, one_hot_classes=one_hot_classes)) open_fn = ReaderCompose(readers=open_fn) for source, mode in zip((df_train, df_valid, df_infer), ("train", "valid", "infer")): if len(source) > 0: dataset = ListDataset( source, open_fn=open_fn, dict_transform=self.get_transforms( stage=stage, mode=mode, image_size=image_size, one_hot_classes=one_hot_classes), ) if mode == "train": labels = [x["class"] for x in source] sampler = BalanceClassSampler(labels, mode="upsampling") dataset = {"dataset": dataset, "sampler": sampler} datasets[mode] = dataset return datasets
train_df, left_on='image_id', right_on='image_id') df_with_labels["disease_type"] = df_with_labels["healthy"] * 0 + df_with_labels["multiple_diseases"] * 1 + \ df_with_labels["rust"] * 2 + df_with_labels["scab"] * 3 df_with_labels.head(10) train_data, valid_data = split_dataframe_train_test(df_with_labels, test_size=0.3, random_state=config.seed) train_data, valid_data = train_data.to_dict('records'), valid_data.to_dict( 'records') open_fn = ReaderCompose([ ImageReader(input_key="filepath", output_key="features", rootpath=config.root_images), ScalarReader(input_key="disease_type", output_key="targets", default_value=-1, dtype=np.int64), ScalarReader(input_key="disease_type", output_key="targets_one_hot", default_value=-1, dtype=np.int64, one_hot_classes=config.num_classes) ]) train_transforms = compose( [pre_transforms(config.size), hard_transforms(),
def _get_infer_open_fn(train_folder, infer_folder): return RowsReader( reader=TextReader(row_key="Image", dict_key="ImageFile"), readers=[ ImageReader(row_key="Image", dict_key="Image", datapath=train_folder), ImageReader(row_key="Image", dict_key="Image", datapath=infer_folder)])