Example #1
0
 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)]
Example #2
0
    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 __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 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()
Example #5
0
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
Example #6
0
    def __init__(self, modelfilename, verbose=False):
        self.model = torch.load(modelfilename, map_location='cpu').eval()
        _, self.val_tfms = tfms_from_model(resnet34, 224)

        self.verbose = verbose
Example #7
0
import torch
from flask_cors import CORS
from scipy.special import expit
import base64

app = Flask(__name__)

CORS(app)

model = torch.load('data/models/torch.resnet101-val-loss-29.914882',
                   map_location='cpu')
model.eval()

size = 224

trn_tfms, val_tfms = tfms_from_model(resnet101, size, crop_type=CropType.NO)
alphabet = list('abcdefghijklmnopqrstuvwxyz') + ['na']
itoa = {c: l for c, l in enumerate(alphabet)}


def bb_hw(bb):
    ymin, xmin, ymax, xmax = bb
    return np.array([xmin, ymin, xmax - xmin + 1, ymax - ymin + 1])


@app.route("/api/predict", methods=['POST'])
def make_predictions():
    try:
        content = request.get_json(force=True)
    except HTTPException as e:
        return jsonify({'error': 'Request data invalid'}), 400
Example #8
0
    parser.add_argument('--seed', type=int, default=1)
    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)
# <<

# >>
Example #9
0
df_total_val,
df_large_trn,
df_large_val,
df_medium_trn,
df_medium_val,
df_small_trn,
df_small_val) = setup_dataset_dfs(LABELS_CSV, LABELS_BASELINE_CSV)

######################
###################### Network setup
######################

# More settings
arch = resnet34
sz=210
tfms = tfms_from_model(arch, sz, crop_type=CropType.NO)
trn_tfms, val_tfms = tfms

# Training settings
bs = 14
seq_len = 10

# Datasets and Dataloaders
trn_bds = BatchifiedDataset(df_small_trn, bs, seq_len, trn_tfms, PATH, IMAGES_FOLDER)
val_bds = BatchifiedDataset(df_small_val, bs, seq_len, trn_tfms, PATH, IMAGES_FOLDER)

trn_dl = FastaiDataLoader(trn_bds, batch_sampler=trn_bds.batch_sampler(shuffle=True))
val_dl = FastaiDataLoader(val_bds, batch_sampler=val_bds.batch_sampler())

# Model FC
model_folder = "CNNtoFC"
Example #10
0
ROOT = '/media/ssd/datasets/deep-driving-csv/'
PATH = "/media/ssd/datasets/deep-driving-csv/"
IMAGES_FOLDER = "images/"
LABELS_CSV = PATH + "labels.csv"
IMAGES_BASELINE_FOLDER = "images-baseline/"
LABELS_BASELINE_CSV = PATH + "labels-baseline.csv"

(df_baseline, df_total, df_total_tfm, df_total_trn, df_total_val, df_large_trn,
 df_large_val, df_medium_trn, df_medium_val, df_small_trn,
 df_small_val) = setup_dataset_dfs(LABELS_CSV, LABELS_BASELINE_CSV)

# ######################
# ###################### Network setup
# ######################
sz = 210
trn_tfms, _ = tfms_from_model(resnet34, sz, crop_type=CropType.NO)
trn_tfms.tfms.pop(1)  # Remove cropping to keep it rectangular


def get_learner_cnn(model_name):
    bs = 1
    seq_len = 1

    # Datasets and Dataloaders
    trn_ds = BatchifiedDataset(df_large_trn, bs, seq_len, trn_tfms, PATH,
                               IMAGES_FOLDER)
    val_ds = BatchifiedDataset(df_large_val, bs, seq_len, trn_tfms, PATH,
                               IMAGES_FOLDER)
    trn_dl = FastaiDataLoader(trn_ds,
                              batch_sampler=trn_ds.batch_sampler(shuffle=True))
    val_dl = FastaiDataLoader(val_ds, batch_sampler=val_ds.batch_sampler())

config = configparser.ConfigParser()
config.read('setup.cfg')
section = config.default_section
#pixel_spacing = config[section]['pixel_spacing']
model_weight_path =  config[section]['model_weight_path']


sz = 224
bs = 128
model = resnet34



trn_tfms, val_tfms = tfms_from_model(model, sz)
preprocess = val_tfms
model = ConvnetBuilder(model, 2, False, False, pretrained=False).model
sd = torch.load(model_weight_path)
model.load_state_dict(sd)
model.eval();


if __name__ == "__main__":
	img_pth = sys.argv[1]
	output_file = sys.argv[2]

	img = load_img(img_pth)
	preprocessed_img = preprocess(img)
	preprocessed_img = V(preprocessed_img).unsqueeze(0)
	logprob = model(preprocessed_img)