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 get_learner(arch, batch_size=128): """Create a FastAI learner using the given model""" data = load_cinic10(batch_size=batch_size) learn = ConvLearner.from_model_data(arch.cuda(), data) learn.crit = nn.CrossEntropyLoss() learn.metrics = [accuracy] return learn
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 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 predict(cls, photo_path): data = cls.get_data() learn = ConvLearner.pretrained(arch, data) learn.load(model_name) trn_tfms, val_tfms = tfms_from_model(arch, sz) im = val_tfms(open_image(photo_path)) preds = learn.predict_array(im[None]) class_id = np.argmax(preds) class_name = learn.data.classes[class_id] class_value = preds[0][class_id] percentage = round(np.exp(class_value) * 100, 2) return {'car_model': class_name, 'probability': percentage}
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 load_model(): global arch, learn, sz, bs, data print('loading model...') try: arch = resnet50 sz = 224 bs = 16 data = get_classifier_data(sz, bs) learn = ConvLearner.pretrained(arch, data) learn.load('224_all_layers') # NOTE: save models with precompute=False # Uncomment this if you didn't: # learn.precompute = False print('model loaded successfully') except Exception as e: print(f'Error: {e}')
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)