def from_model(cls, emd_path, data=None):
        emd_path = Path(emd_path)
        with open(emd_path) as f:
            emd = json.load(f)

        model_file = Path(emd['ModelFile'])

        if not model_file.is_absolute():
            model_file = emd_path.parent / model_file

        model_params = emd['ModelParameters']
        chip_size = emd["ImageWidth"]

        try:
            class_mapping = {i['Value']: i['Name'] for i in emd['Classes']}
            color_mapping = {i['Value']: i['Color'] for i in emd['Classes']}
        except KeyError:
            class_mapping = {
                i['ClassValue']: i['ClassName']
                for i in emd['Classes']
            }
            color_mapping = {
                i['ClassValue']: i['Color']
                for i in emd['Classes']
            }

        if data is None:
            ranges = (0, 1)
            train_tfms = [
                rotate(degrees=30, p=0.5),
                crop(size=chip_size, p=1., row_pct=ranges, col_pct=ranges),
                dihedral_affine(),
                brightness(change=(0.4, 0.6)),
                contrast(scale=(0.75, 1.5)),
                # rand_zoom(scale=(0.75, 1.5))
            ]
            val_tfms = [crop(size=chip_size, p=1.0, row_pct=0.5, col_pct=0.5)]
            transforms = (train_tfms, val_tfms)

            with warnings.catch_warnings():
                warnings.simplefilter("ignore", UserWarning)

                tempdata = ImageDataBunch.single_from_classes(
                    tempfile.TemporaryDirectory().name,
                    sorted(list(class_mapping.values())),
                    tfms=transforms,
                    size=chip_size).normalize(imagenet_stats)
                tempdata.chip_size = chip_size
                return cls(tempdata,
                           **model_params,
                           pretrained_path=str(model_file))
        else:
            return cls(data, **model_params, pretrained_path=str(model_file))
Beispiel #2
0
def load_model(inference=False):
    if inference:
        data = ImageDataBunch.load_empty(TRAIN_PATH)
    else:
        np.random.seed(1337)  # give consistency to the validation set
        data = ImageDataBunch.from_folder(TRAIN_PATH,
                                          train=".",
                                          valid_pct=0.1,
                                          ds_tfms=transform.get_transforms(),
                                          size=224,
                                          num_workers=4,
                                          bs=32).normalize(imagenet_stats)

        data.export()  # Save the classes used in training for inference

    learn = learner.cnn_learner(data,
                                models.resnet34,
                                metrics=metrics.error_rate)

    if inference:
        learn.load(MODEL_NAME)

    return learn, data
Beispiel #3
0
def load_image_databunch(input_path, classes):
    """
    Code to define a databunch compatible with model
    """
    tfms = get_transforms(
        do_flip=False,
        flip_vert=False,
        max_rotate=0,
        max_lighting=0,
        max_zoom=1,
        max_warp=0,
    )

    data_bunch = ImageDataBunch.single_from_classes(
        Path(input_path), classes, ds_tfms=tfms, size=224
    )

    return data_bunch
Beispiel #4
0
def load_catsvsdog(input_path, batch_size):
    """
    Function to load data from cats vs dog Kaggle competition
    """
    path = Path(input_path)
    fnames = get_image_files(path)

    # Creating Databunch
    data = ImageDataBunch.from_name_re(
        path,
        fnames,
        pat=r"([^/]+)\.\d+.jpg$",
        ds_tfms=get_transforms(),
        valid_pct=0.2,
        size=227,
        bs=batch_size,
    ).normalize()

    return data
Beispiel #5
0
def image_data_bunch_from_data_file(index_path, seed=0, **kwargs):
    """This factory creates a fastai ImageDataBunch from an index
    """
    from fastai.vision.data import ImageDataBunch

    db = DictDataFile(index_path)

    def label_from_key(fpath):
        return db[fpath]

    # Set the numpy seed to we get deterministic splits of traing/validation data
    # then reset it to the previous value so we don't affect the rest of the application
    old_state = np.random.get_state()
    try:
        if seed is not None:
            np.random.seed(seed)
        return ImageDataBunch.from_name_func(fnames=db.keys(),
                                             label_func=label_from_key,
                                             **kwargs)
    finally:
        np.random.set_state(old_state)
Beispiel #6
0
def predict(images):
    #loadig model
    train_dir = "./pipelines/lymph_node/data/data_bunch"
    base_dir = "./pipelines/lymph_node/data"  #base directory
    l = os.listdir(train_dir)
    #random.shuffle(l)
    tfms = get_transforms(do_flip=True)
    #do_flip: if True, a random flip is applied with probability 0.5 to images
    bs = 64  # also the default batch size
    print("loaddd")
    #ImageDataBunch splits out the imnages (in the train sub-folder) into a training set and validation set (defaulting to an 80/20 percent split)
    data = ImageDataBunch.from_csv(base_dir,
                                   ds_tfms=tfms,
                                   size=224,
                                   suffix=".tiff",
                                   folder="data_bunch",
                                   csv_labels="dummy_labels.csv",
                                   bs=bs)
    print("valid ", data.valid_ds)
    print("train ", data.train_ds)
    print("test ", data.test_ds)
    # transform the image values according to the nueral network we are using
    data.normalize(imagenet_stats)

    #cnn_learner loads the model into learn variable`
    learn = cnn_learner(data,
                        models.densenet161,
                        metrics=error_rate,
                        callback_fns=ShowGraph)

    learn = learn.load("./densenet10epochs")
    #predicting labels
    print("prediction ", type(images))
    print(images)
    print("size ", len(images), images[0].shape)
    preds = learn.predict(images[0])
    #preds=learn.pred_batch(np.array(images))  #TO-DO!!!!!!!!
    print(type(preds))
    print("prediction ", preds)
    return preds
Beispiel #7
0
            for idx in range(1, len(label_list) + 1):
                cv2.putText(
                    self.image,
                    LABEL_MAP[label_list[idx - 1]],
                    (X, Y - 14 * idx),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    0.45,
                    (80, 100, 50),
                    2,
                )
            print("Label :", label)


path = Path("Training")
tfm = get_transforms(do_flip=True, max_rotate=35.0, max_zoom=0.6, max_lighting=0.3, max_warp=0.2, p_affine=0.75, p_lighting=0.75)
data = ImageDataBunch.from_folder(path, train=".", valid_pct=0.2, ds_tfms=tfm, num_workers=4, size=224).normalize(imagenet_stats)
# Loading our model
learn = cnn_learner(data, models.resnet50, pretrained=False)
learn.load("stage-3")
cap = cv2.VideoCapture('testet.mp4')
Traffic = cv2.CascadeClassifier('second_2_5.xml')
while True:
    ret, img = cap.read()
    if type(img) == type(None):
        break
    ret = count % 5
    if ret == 0:
        H, W, C = img.shape
        gray = cv2.cvtColor(img.copy(), cv2.COLOR_BGR2GRAY)
        Traffic_sign = Traffic.detectMultiScale(gray, scaleFactor=2, minNeighbors=5, minSize=(90, 90), maxSize=(120, 120))  # 1.05
        if len(Traffic_sign) < 1:
Beispiel #8
0
def get_learner(classes):
    # TODO: Can we make this faster/lighter?
    data = ImageDataBunch.single_from_classes(".", classes, ds_tfms=get_transforms(), size=224).normalize(imagenet_stats)
    learn = create_cnn(data, resnet34, pretrained=False)
    learn.load('makemodel-392')
    return learn
Beispiel #9
0
from fastai.metrics import accuracy
from fastai.vision import models
from PIL import ImageFile
import dill

#defaults.device = torch.device('cuda')
DATA_PATH = '/valohai/inputs/dataset/dataset/'
MODEL_PATH = '/valohai/outputs/'
# Data augmentation: create a list of flip, rotate, zoom, warp, lighting transforms...
tfms = get_transforms()
# Create databunch from imagenet style dataset in path with
# images resized 224x224 and batch size equal to 64
# and validation set about 30% of the dataset
data = ImageDataBunch.from_folder(DATA_PATH,
                                  ds_tfms=tfms,
                                  size=224,
                                  bs=64,
                                  valid_pct=0.3).normalize(imagenet_stats)
# Get a pretrained model (resnet34) with a custom head that is suitable for our data.
learn = cnn_learner(data, models.resnet34, metrics=[accuracy])
learn.model_dir = MODEL_PATH

ImageFile.LOAD_TRUNCATED_IMAGES = True
# Fit a model following the 1cycle policy with 50 epochs
learn.fit_one_cycle(50)

# save model parameters
learn.save('weights')
# Save the model architecture (pytorch form .pt)
torch.save(learn.model, MODEL_PATH + 'my_model.pt', pickle_module=dill)
learn.export(MODEL_PATH + 'export.pkl')