def predict(self): print(self.file) if not self.file and os.path.isfile(self.file): return 0 tfms = tfms_from_model( self.arch, self.sz, aug_tfms=transforms_side_on, max_zoom=1.1 ) data = ImageClassifierData.from_paths( self.PATH, tfms=tfms, bs=self.bs, num_workers=4 ) learn = ConvLearner.pretrained( self.arch, data, precompute=True, ps=0.5 ) learn.unfreeze() learn.precompute = False learn.load(self.MODEL) image = open_image(self.file) trn_tfms, val_tfms = tfms_from_model(self.arch, self.sz) im = val_tfms(image) preds = learn.predict_array(im[None]) return learn.data.classes[np.argmax(preds)]
def __init__(self, metadata): # The temp folder does not exist initially tmp_dir = metadata.basedir_path / 'tmp' if not os.path.exists(tmp_dir): os.mkdir(tmp_dir) mc_csv = tmp_dir / 'mc.csv' # Example annotation: bounding box + category id # print(metadata.training.annotations[12]) # Generate and store the multi-class label csv file self.save_csv(mc_csv, metadata.train.id_category_map, metadata.train.id_filename_map, metadata.train.annotations, metadata.train.annotations) # Prepare the model self.model = resnet34 self.image_size = 224 self.batch_size = 64 # Non-agumented transforms self.non_aug_transforms = tfms_from_model(self.model, self.image_size, crop_type=CropType.NO) # Automatically appends the second 'folder' param to the first - beware! self.md = ImageClassifierData.from_csv(metadata.basedir_path, metadata.train.image_subdir, mc_csv, tfms=self.non_aug_transforms, bs=self.batch_size) self.learner = ConvLearner.pretrained(self.model, self.md) self.learner.opt_fn = optim.Adam
def run(constant_overwrites): x_train, y_train, x_test, y_test = load_cifar10_dataset() # n_classes = 10 classes = [ 'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck' ] print('') print('original:') print('X_train shape:', x_train.shape) print('y_train shape:', y_train.shape) print(y_train[:3]) # not required when using `tfms_from_model` below # x_train = x_train.reshape(x_train.shape[0], -1) x_train = x_train.astype( np.float32) # WTF? float/float64 (default) raises error y_train = np.where(y_train == 1)[1] y_train = y_train.astype(np.int) # uint8 is causing error # x_test = x_test.reshape(x_test.shape[0], -1) x_test = x_test.astype(np.float32) # as above y_test = np.where(y_test == 1)[1] y_test = y_test.astype(np.int) # as above # sample to test on CPU x_train = x_train[:800] y_train = y_train[:800] x_test = x_test[:200] y_test = y_test[:200] print('') print('reshaped:') print('X_train shape:', x_train.shape) print('y_train shape:', y_train.shape) print(y_train[:3]) print('X_train dtype:', x_train.dtype) print('y_train dtype:', y_train.dtype) constants = merge_dict(get_constants(), constant_overwrites) constants['img_shape'] = x_train.shape[1:] arch = resnet34 data = ImageClassifierData.from_arrays(OUTPUT_DIR, trn=(x_train, y_train), val=(x_test, y_test), classes=classes, tfms=tfms_from_model( arch, constants['image_size'])) learn = ConvLearner.pretrained(arch, data, precompute=True) # lrf = learn.lr_find() learn.fit(constants['learning_rate'], constants['n_epochs']) learn.sched.plot_loss() log_preds = learn.predict() preds = np.argmax(log_preds, axis=1) print('Finished')
def main(imgdir, epochs): log.info(f'Cuda available: {torch.cuda.is_available()}') log.info(f'Cuda backend enabled: {torch.backends.cudnn.enabled}') # model = resnet34 model = resnet50 tfms = tfms_from_model(resnet34, IMAGE_SIZE, aug_tfms=transforms_side_on, max_zoom=1.1) data = ImageClassifierData.from_paths(imgdir, tfms=tfms) learn = ConvLearner.pretrained(model, data, precompute=True) learn.sched.plot_lr()
def save_fc1(self): self.get_activations() act, val_act, test_act = self.activations m = self.models.top_model if len(self.activations[0]) != len(self.data.trn_ds): predict_to_bcolz(m, self.data.fix_dl, act) if len(self.activations[1]) != len(self.data.val_ds): predict_to_bcolz(m, self.data.val_dl, val_act) if self.data.test_dl and (len(self.activations[2]) != len(self.data.test_ds)): if self.data.test_dl: predict_to_bcolz(m, self.data.test_dl, test_act) self.fc_data = ImageClassifierData.from_arrays(self.data.path, (act, self.data.trn_y), (val_act, self.data.val_y), self.data.bs, classes=self.data.classes, test=test_act if self.data.test_dl else None, num_workers=8)
def __init__(self, event): self.exit_event = event self.last_notification_sent = 0 self.notification_threshold = 60 * 5 PATH = os.getcwd() self.sz = 224 self.arch = resnet34 self.data = ImageClassifierData.from_paths(PATH, tfms=tfms_from_model( self.arch, sz)) self.learn = ConvLearner.pretrained(self.arch, self.data, precompute=True) self.learn.load('224_all') self.trn_tfms, self.val_tfms = tfms_from_model(self.arch, self.sz)
def load_test_image(PATH, image=None, train_dat=None, valid_dat=None, classes=[0, 1], tfms=None): """ Returns a FastAI dataloader with a single image as its test set. """ test_dat = np.array([image]) if type(image) == np.ndarray else None return ImageClassifierData.from_arrays(PATH, train_dat, valid_dat, bs=1, tfms=tfms, classes=classes, test=test_dat)
def test_image_classifier_data_from_csv_unsorted(root_folder, csv_file, data_folder): val_idxs = [2, 3] tfms = tfms_from_model(resnet34, 224) path = str(root_folder) folder = "tmp_data_folder" csv_fname = Path(str(csv_file)) data = ImageClassifierData.from_csv( path=Path(path), folder=folder, csv_fname=csv_fname, val_idxs=val_idxs, suffix=".png", tfms=tfms, ) val_fnames = ["8.png", "7.png"] assert [os.path.split(o)[-1] for o in data.val_ds.fnames.tolist()] == val_fnames
return parser.parse_args() args = parse_args() # -- # IO # !! Copying from `fastai` for the time being sys.path.append('/home/bjohnson/software/fastai') from fastai.dataset import ImageClassifierData from fastai.transforms import tfms_from_model, transforms_side_on basenet.helpers.set_seeds(args.seed) tfms = tfms_from_model(resnet34, 448, aug_tfms=transforms_side_on, max_zoom=1.1) data = ImageClassifierData.from_paths('_data/cub_splits', tfms=tfms) dataloaders = { "train_fixed" : data.fix_dl, "train" : data.trn_dl, "val" : data.val_dl } # >> # datasets = make_datasets(root='_data/cub_splits', img_size=224) # dataloaders = make_dataloaders(datasets) # << # >> class DelfLayer(basenet.BaseNet):
def get_classifier_data(sz, bs=64): tfms = get_tfms() return ImageClassifierData.from_csv(DATA_PATH, 'train', LABELS_PATH, tfms=tfms)