def test_pipeline(seg_model, cla_model, seg_preprocess_type,
                  cls_preprocess_type, activation, cls_treshold, verbose):
    train2 = util.restructure_data_frame('Severstal_Dataset\\test.csv')
    seg_preprocess = sm.get_preprocessing(seg_preprocess_type)
    cls_preprocess = sm.get_preprocessing(cls_preprocess_type)
    bs = 1
    classes = 4 if activation == 'sigmoid' else 5

    test_batches = SegmentationDataGenerator(train2,
                                             shapes=((bs, 256, 1600), ),
                                             subset='test')

    n_samples = test_batches.__len__()
    dice_res = 0
    iterator = iter(test_batches)
    for _ in tqdm(range(n_samples)):
        images, masks = next(iterator)

        for i in range(len(images)):
            image = images[i].astype(np.int16)
            mask = masks[i]

            seg_x = seg_preprocess(image)
            cls_x = cls_preprocess(image)

            #Predict if the current image is defective or not
            cls_res = cla_model.predict(np.reshape(cls_x, (1, 256, 1600, 3)))

            #If it is most probable defective, the result is a whole zero mask
            if (cls_res < cls_treshold):
                res = np.zeros((256, 1600, classes), dtype=np.uint8)
                if classes == 5:
                    res[:, :, 4] = 1

            #Otherwise apply segmentation
            else:
                res = seg_model.predict(np.reshape(seg_x, (1, 256, 1600, 3)))
                res = np.reshape(res, (256, 1600, classes))

            res[np.where(res < 0.5)] = 0
            res[np.where(res >= 0.5)] = 1

            #Update dice value
            dice_res += dice_coef(mask.astype(np.uint8), res.astype(np.uint8))

            if (verbose):
                coef = dice_coef(mask.astype(np.uint8), res.astype(np.uint8))
                util.show_img_and_def((image, mask, res),
                                      ('orig', 'mask', 'pred ' + str(coef)))

    print(dice_res / (n_samples * bs))
def test_classification(cls_model, cls_preprocess_type):
    train2 = util.restructure_data_frame('Severstal_Dataset\\test.csv')
    cls_preprocess = sm.get_preprocessing(cls_preprocess_type)
    bs = 2

    test_batches = ClassificationDataGenerator(train2,
                                               shapes=((bs, 256, 1600), ),
                                               subset='test')

    n_samples = test_batches.__len__()
    iterator = iter(test_batches)
    for _ in tqdm(range(n_samples)):
        images, trues = next(iterator)

        for i in range(len(images)):
            image = images[i].astype(np.int16)
            true = trues[i]

            cls_x = cls_preprocess(image)

            #Predict if the current image is defective or not
            cls_res = cls_model.predict(np.reshape(cls_x, (1, 256, 1600, 3)))

            update_acc_matrix(true, cls_res)

    print('True positive: ', acc_matrix[0], 'False positive: ', acc_matrix[1])
    print('False negative: ', acc_matrix[2], 'True Negative: ', acc_matrix[3])
Example #3
0
def test_scans_generator(scans_directory_path: Path,
                         batch_size=1,
                         target_size=(256, 256),
                         shuffle=False,
                         backbone="efficientnetb0"):
    """
    Tensorflow dataset test generator

    :param scans_directory_path: Directory with "images" folder containing scan slices
    :param batch_size: batch size
    :param target_size: image target size
    :param shuffle: Whether to shuffle the data (default: True) If set to False, sorts the data in alphanumeric order.
    :param backbone: backbone name of network you will use
    :return: A DirectoryIterator yielding tuples of (x) where x is a numpy array containing
    a batch of images with shape (batch_size, *target_size, channels) and y is a numpy array of corresponding labels
    """
    preprocess_input = get_preprocessing(backbone)

    images_datagen = tf.keras.preprocessing.image.ImageDataGenerator(
        preprocessing_function=preprocess_input)

    test_images_generator = images_datagen.flow_from_directory(
        directory=scans_directory_path,
        target_size=target_size,
        batch_size=batch_size,
        seed=42,
        shuffle=shuffle,
        class_mode=None,
        interpolation="bilinear")
    test_samples = test_images_generator.n

    return test_images_generator, test_samples
def load_dataset_classification(preprocess_type):
    train2 = util.restructure_data_frame('Severstal_Dataset\\train.csv')
    idx = int(0.8 * len(train2))
    print()

    preprocess = sm.get_preprocessing(preprocess_type)
    shapes = ((4, 256, 1600), )

    train_batches = ClassificationDataGenerator(
        train2.iloc[:idx],
        shuffle=True,
        preprocess=preprocess,
        shapes=shapes,
        augmentation_parameters=augmentation_parameters)

    valid_batches = ClassificationDataGenerator(train2.iloc[idx:],
                                                shuffle=True,
                                                preprocess=preprocess)
    """
    iterator = iter(train_batches)
    for _ in range(20):
        images, defect = next(iterator)

        for i in range(len(images)):
            image = images[i].astype(np.uint8)

            print (defect[i])
            util.show_imgs((image,image),('orig','orig'),('',''))
    """

    return train_batches, valid_batches
Example #5
0
def setup_data(x, mean, std, backbone, batch_size=1, image_size=224):

    preprocess_input = sm.get_preprocessing(backbone)

    x = preprocess_input(x)
    fake_y = np.zeros_like(x)[:,:,:,0]

    ds = (
        tf.data.Dataset.from_tensor_slices((x, fake_y))
        .batch(batch_size, drop_remainder=True)
        .map(
            partial(
                processing_data_functions(
                    key="validation",
                    size=image_size,
                    p=None,
                    mean=mean,
                    std=std
                ),
                bs=batch_size,
            ),
            num_parallel_calls=tf.data.AUTOTUNE
        )
        .prefetch(tf.data.AUTOTUNE)
    )
    return ds
    def Predict(self, img_path, vis=True):
        '''
        User function: Run inference on image and visualize it. Output mask saved as output_mask.npy

        Args:
            img_path (str): Relative path to the image file
            vis (bool): If True, predicted mask is displayed.

        Returns:
            list: List of bounding box locations of predicted objects along with classes. 
        '''
        dirPath = "tmp_test"

        if (os.path.isdir(dirPath)):
            shutil.rmtree(dirPath)

        os.mkdir(dirPath)
        os.mkdir(dirPath + "/img_dir")
        os.mkdir(dirPath + "/gt_dir")

        os.system("cp " + img_path + " " + dirPath + "/img_dir")
        os.system("cp " + img_path + " " + dirPath + "/gt_dir")

        x_test_dir = dirPath + "/img_dir"
        y_test_dir = dirPath + "/gt_dir"

        if (self.system_dict["params"]["image_shape"][0] % 32 != 0):
            self.system_dict["params"]["image_shape"][0] += (
                32 - self.system_dict["params"]["image_shape"][0] % 32)

        if (self.system_dict["params"]["image_shape"][1] % 32 != 0):
            self.system_dict["params"]["image_shape"][1] += (
                32 - self.system_dict["params"]["image_shape"][1] % 32)

        preprocess_input = sm.get_preprocessing(
            self.system_dict["params"]["backbone"])
        test_dataset = Dataset(
            x_test_dir,
            y_test_dir,
            self.system_dict["params"]["classes_dict"],
            classes_to_train=self.system_dict["params"]["classes_to_train"],
            augmentation=get_validation_augmentation(
                self.system_dict["params"]["image_shape"][0],
                self.system_dict["params"]["image_shape"][1]),
            preprocessing=get_preprocessing(preprocess_input),
        )

        test_dataloader = Dataloder(test_dataset, batch_size=1, shuffle=False)

        image, gt_mask = test_dataset[0]
        image = np.expand_dims(image, axis=0)
        pr_mask = self.system_dict["local"]["model"].predict(image).round()
        np.save("output_mask.npy", pr_mask)

        if (vis):
            visualize(
                image=denormalize(image.squeeze()),
                pr_mask=pr_mask[..., 0].squeeze(),
            )
Example #7
0
def get_pred_dataset(filenames, images_dir, backbone, target_size):
    # Preprocessing
    prep_fn = preprocessing_fn(custom_fn=sm.get_preprocessing(backbone))

    # Build dataset
    dataset = DatasetImages(filenames, imgs_dir=images_dir, da_fn=ts_da_fn(*target_size),
                            preprocess_fn=prep_fn, target_size=target_size, memory_map=False)
    return dataset
Example #8
0
    def __init__(self, name, models_path, use_page_cache=False):
        if not re.match(r"^[a-z0-9/]+$", name):
            raise ValueError("illegal model name '%s'" % name)

        self._name = name
        self._page_cache = None

        models_path = Path(models_path)
        network_path = models_path / name

        for filename in ("meta.json", "model.h5"):
            asset_path = network_path / filename
            if not asset_path.exists():
                raise RuntimeError("no model file found at %s" % asset_path)

        # Loads the meta.json file in each part of the model. This is used to load and set
        # various attributes dynamically.
        with open(network_path / "meta.json", "r") as f:
            meta = json.loads(f.read())
        # Loads the types of labels that can be applied to the image
        classes = meta["classes"]

        if False:
            # Alternate loading method. Not tested
            model = getattr(segmentation_models,
                            meta["model"])(meta["backbone"],
                                           classes=len(classes),
                                           activation="softmax")
            logging.info("loading model at %s" %
                         str(network_path / "model.h5"))
            model.load_weights(str(network_path / "model.h5"))
        else:
            """Loads a Keras model from the specified path"""
            # see https://github.com/qubvel/segmentation_models/issues/153
            # see https://stackoverflow.com/questions/54835331/how-do-i-load-a-keras-saved-model-with-custom-optimizer
            model = load_model(str(network_path / "model.h5"), compile=False)

        # Loads the pre-processing backend for the segmentation model library.
        # What backend it is loading is defined in the meta.json in each saved model.
        self._preprocess = segmentation_models.get_preprocessing(
            meta["backbone"])

        self._model = model
        self._full_size = tuple(meta["full_size"])
        self._full_shape = tuple(reversed(self._full_size))
        self._tile_size = tuple(meta["tile_size"])

        # Generated a list of of Tile objects via the Tiles object callable method
        self._tiles = list(
            Tiles(self._tile_size, beta0=meta["tile_beta"])(meta["full_size"]))

        # Set the types of classes
        self._classes = enum.Enum(meta["type"] + "Label",
                                  dict((v, i) for i, v in enumerate(classes)))

        # Set the model type, Region or Separator
        self._type = meta["type"]
Example #9
0
    def Predict_Frame(self, path, vis=True):
        '''
        User function: Run inference on image and visualize it. Output mask saved as output_mask.npy

        Args:
            img_path (str): Relative path to the image file
            vis (bool): If True, predicted mask is displayed.

        Returns:
            list: List of bounding box locations of predicted objects along with classes. 
        '''

        preprocess_input = sm.get_preprocessing(
            self.system_dict["params"]["backbone"])

        image_normalize = preprocess_input(path)

        print(image_normalize)

        image = cv2.resize(image_normalize, (int(
            len(image_normalize[0]) * 1024 / len(image_normalize[1])), 1024),
                           interpolation=cv2.INTER_LINEAR)

        image = np.expand_dims(image, axis=0)

        print("bla bla bla bla ")
        print()
        print()
        print()
        print(len(image[0]))

        pr_mask = self.system_dict["local"]["model"].predict(image).round()
        np.save("output_mask.npy", pr_mask)
        '''
        if(vis):
            visualize(
                image=denormalize(image.squeeze()),
                pr_mask=pr_mask[..., 0].squeeze(),
            )
        '''

        img_list = [denormalize(image.squeeze())]
        label_list = ["image"]

        print(img_list)

        for i in range(len(self.system_dict["params"]["classes_to_train"])):
            img_list.append(pr_mask[..., i].squeeze())
            label_list.append(
                self.system_dict["params"]["classes_to_train"][i])

        print(img_list)

        if (vis):
            visualize2(label_list, img_list)

        return (pr_mask, img_list)
Example #10
0
def load_model(path, input_shape, num_classes=4, backbone='resnet18'):

    model = Linknet(backbone_name=backbone,
                    input_shape=input_shape,
                    classes=num_classes,
                    activation='softmax')

    model.load_weights(path)

    return model, sm.get_preprocessing(backbone)
Example #11
0
class Config:
    BACKBONE = 'efficientnetb3'
    BATCH_SIZE = 1

    preprocess_input = sm.get_preprocessing(BACKBONE)
    activation = 'softmax'

    epochs = 50
    threshold = 0.5
    class_names = []
Example #12
0
def get_model():
    """Gets the model used for training the dataset

    Returns:
        model: the model used for training
        preprocess: the preprocessing model
    """

    preprocess = sm.get_preprocessing('resnet34')
    model = sm.Unet('resnet34', input_shape=(128, 800, 3), classes=4, activation='sigmoid')
    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=[dice_coef])
    return model, preprocess
Example #13
0
def makeModel(optimizer='Adam', encoder='resnet18', encoder_freeze=False):
    preprocess_input = sm.get_preprocessing(encoder)

    model = sm.Unet(encoder,
                    classes=1,
                    encoder_weights='imagenet',
                    encoder_freeze=encoder_freeze)
    model.compile(optimizer=optimizer,
                  loss=sm.losses.bce_jaccard_loss,
                  metrics=[sm.metrics.iou_score])

    return model, preprocess_input
Example #14
0
def get_model(backbone):
    if backbone == "resnet34":
        from segmentation_models import Unet
        model = Unet("resnet34",
                     encoder_weights='imagenet',
                     classes=1,
                     activation='sigmoid',
                     encoder_freeze=True)
        prep_fn = preprocessing_fn(custom_fn=sm.get_preprocessing("resnet34"))
        return model, prep_fn

    else:
        raise ValueError("Unknown backbone")
Example #15
0
 def init():
     """
     Initialize Segmenter building the segmentation model (UNet with Efficientnetb7 as backbone) and loading
     its corresponding weights
     """
     print("Initializing Segmenter...")
     model = sm.Unet(config.SEGMENTER_BACKBONE,
                     encoder_weights='imagenet',
                     input_shape=config.SEGMENTER_IM_SIZE)
     model.load_weights(config.SEGMENTER_WEIGHT_PATH)
     Segmenter.__segmentation_model = model
     Segmenter.__preprocess_input = sm.get_preprocessing(
         config.SEGMENTER_BACKBONE)
     print("Done.")
Example #16
0
    def __init__(self, config):
        self.type = config['type']
        self.backbone = config['backbone']
        self.n_class = config['n_class']
        self.activate = config['activation']
        self.encoder_weights = config.get('encoder_weights', None)
        self.preprocessing = sm.get_preprocessing(self.backbone)

        local_device_protos = device_lib.list_local_devices()
        n_available_gpus = len([x.name for x in local_device_protos if x.device_type == 'GPU'])
        if n_available_gpus < 2:
            self.model = self._build()
        else:
            with tf.device('/cpu:0'):
                self.cpu_model = self._build()
            self.model = keras.utils.multi_gpu_model(self.cpu_model, gpus=n_available_gpus)
Example #17
0
    def __init__(self, name, models_path, use_page_cache=False):
        if not re.match(r"^[a-z0-9/]+$", name):
            raise ValueError("illegal model name '%s'" % name)

        self._name = name
        self._page_cache = None

        models_path = Path(models_path)
        network_path = models_path / name

        for filename in ("meta.json", "model.h5"):
            asset_path = network_path / filename
            if not asset_path.exists():
                raise RuntimeError("no model file found at %s" % asset_path)

        with open(network_path / "meta.json", "r") as f:
            meta = json.loads(f.read())
        classes = meta["classes"]

        if False:
            model = getattr(segmentation_models,
                            meta["model"])(meta["backbone"],
                                           classes=len(classes),
                                           activation="softmax")
            logging.info("loading model at %s" %
                         str(network_path / "model.h5"))
            model.load_weights(str(network_path / "model.h5"))
        else:
            # see https://github.com/qubvel/segmentation_models/issues/153
            # see https://stackoverflow.com/questions/54835331/how-do-i-load-a-keras-saved-model-with-custom-optimizer
            model = load_model(str(network_path / "model.h5"), compile=False)

        self._preprocess = segmentation_models.get_preprocessing(
            meta["backbone"])

        self._model = model
        self._full_size = tuple(meta["full_size"])
        self._full_shape = tuple(reversed(self._full_size))
        self._tile_size = tuple(meta["tile_size"])

        self._tiles = list(
            Tiles(self._tile_size, beta0=meta["tile_beta"])(meta["full_size"]))

        self._classes = enum.Enum(meta["type"] + "Label",
                                  dict((v, i) for i, v in enumerate(classes)))
        self._type = meta["type"]
def load_dataset_segmentation(preprocess_type,
                              activation='sigmoid',
                              use_balanced_dataset=True,
                              shapes=((5, 256, 384), (5, 256, 512))):
    np.random.seed(15)

    train2 = util.restructure_data_frame('Severstal_Dataset\\train.csv')
    train_idxs, valid_idxs = util.get_random_split(train2)

    preprocess = sm.get_preprocessing(preprocess_type)

    #shapes = ((5,256,384),(5,256,512))

    train_batches = SegmentationDataGenerator(
        train2.iloc[train_idxs],
        shapes=shapes,
        shuffle=True,
        use_balanced_dataset=use_balanced_dataset,
        preprocess=preprocess,
        augmentation_parameters=augmentation_parameters,
        use_defective_only=False,
        activation=activation)

    valid_batches = SegmentationDataGenerator(train2.iloc[valid_idxs],
                                              shuffle=True,
                                              preprocess=preprocess)
    """
    iterator = iter(valid_batches)
    for _ in range(100):
        images, masks = next(iterator)

        for i in range(len(images)):
            image = images[i].astype(np.int16)
            mask = masks[i]
            util.show_img_and_def((image, mask) , ('orig','mask'))
            
            #util.show_imgs((image, mask[:,:,0], mask[:,:,1], mask[:,:,2], mask[:,:,3], mask[:,:,4]),
                            #('img', '0', '1', '2', '3', '4'), ('','','','','',''))

            #util.show_imgs((image, mask[:,:,0], mask[:,:,1], mask[:,:,2], mask[:,:,3]),
                            #('img', '0', '1', '2', '3'), ('','','','',''))
    """

    return train_batches, valid_batches
Example #19
0
def get_model_definition():
    backbone = 'mobilenet'
    n_classes = 2
    lr = 0.001
    activation = 'softmax'
    pre_process_input = sm.get_preprocessing(backbone)
    optimizer = keras.optimizers.Adam(lr)
    metrics = [
        sm.metrics.FScore(threshold=0.5),
    ]
    model = sm.Linknet(backbone,
                       classes=n_classes,
                       activation=activation,
                       encoder_freeze=True)
    if n_classes == 1:
        loss = sm.losses.BinaryFocalLoss()
    else:
        loss = sm.losses.CategoricalFocalLoss()
    model.compile(optimizer, loss, metrics)
    return model, pre_process_input
    def __init__(self,
                 list_IDs,
                 df,
                 target_df=None,
                 mode='fit',
                 base_path='../../dados/train_images',
                 batch_size=32,
                 dim=(1400, 2100),
                 n_channels=3,
                 reshape=None,
                 augment=False,
                 n_classes=4,
                 random_state=2019,
                 shuffle=True,
                 backbone='resnet34',
                 gamma=None,
                 TTA=False,
                 randomcrop=False,
                 classes=None):
        self.dim = dim
        self.batch_size = batch_size
        self.df = df
        self.mode = mode
        self.base_path = base_path
        self.target_df = target_df
        self.list_IDs = list_IDs
        self.reshape = reshape
        self.n_channels = n_channels
        self.augment = augment
        self.n_classes = n_classes
        self.gamma = gamma
        self.shuffle = shuffle
        self.random_state = random_state
        self.preprocess_input = sm.get_preprocessing(backbone)
        self.TTA = TTA
        self.randomcrop = randomcrop
        self.classes = classes
        self.image_name = []

        self.on_epoch_end()
        np.random.seed(self.random_state)
Example #21
0
def _build_keypoint_preprocessing(input_shape, backbone):
    """Builds the preprocessing function for the Field Keypoint Detector Model.

    """
    sm_preprocessing = sm.get_preprocessing(backbone)

    def preprocessing(input_img, **kwargs):

        to_normalize = False if np.percentile(input_img, 98) > 1.0 else True

        if len(input_img.shape) == 4:
            print(
                "Only preprocessing single image, we will consider the first one of the batch"
            )
            image = input_img[0] * 255.0 if to_normalize else input_img[0] * 1.0
        else:
            image = input_img * 255.0 if to_normalize else input_img * 1.0

        image = cv2.resize(image, input_shape)
        image = sm_preprocessing(image)
        return image

    return preprocessing
Example #22
0
def train_net():
    args = parser.parse_args()
    final_path = args.model_final_path
    checkpoint_path = args.model_checkpoint_path
    dataset_path = args.dataset_path
    with tf.device("/gpu:0"):
        backbone = 'resnet50'
        preprocess_input = get_preprocessing(backbone)

        # load your data
        x_train, y_train, x_val, y_val = from_directory_datagen(dataset_path)

        # preprocess input
        x_train = preprocess_input(x_train)
        x_val = preprocess_input(x_val)

        # define model
        model = Unet(backbone, encoder_weights='imagenet', input_shape=(256, 256, 3))
        model.compile(optimizer=tf.keras.optimizers.Adam(lr=1e-3), loss=dice_loss,
                      metrics=[f1_score, iou_score])

        check_point = [ModelCheckpoint(checkpoint_path + 'model-{epoch:03d}-{val_f1-score:03f}.h5', verbose=1,
                                       monitor='val_f1-score',
                                       save_best_only=True, mode='max')]

        # fit model
        model.fit(
            x=(pair for pair in zip(x_train, y_train)),
            epochs=10,
            steps_per_epoch=x_train.n // x_train.batch_size,
            validation_data=(pair for pair in zip(x_val, y_val)),
            validation_steps=x_val.n // x_val.batch_size,
            verbose=1,
            shuffle=True,
            callbacks=check_point,
        )
        model.save(final_path + 'final_model.h5')
def predict():

    # Delete files from previous runs in inference folder
    files = glob.glob(app.config['INFERENCE_DIR'] + '/*.*')
    for f in files:
        os.remove(f)

    # Option 1: Get user-uploaded image
    if flask.request.files.get('file'):
        image = flask.request.files['file']
        image.save(os.path.join(app.config['INFERENCE_DIR'], 'upload.png'))
        # If we had chosen to save the file under its original filename, for
        # security we would have run: filename = secure_filename(image.filename)

    # Option 2: Get chosen default image
    elif flask.request.form.get('test'):
        image = os.path.join(app.config['APP_STATIC'],
                             flask.request.form.get('test'))

    # Pre-processing: read the image, pad/resize it and apply EfficientNet preprocessing
    image_id = os.path.basename(image)
    image = skimage.io.imread(image, plugin='matplotlib')
    if image.shape[-1] == 4:  # remove alpha channel
        image = image[..., :3]
    HEIGHT = image.shape[0]
    WIDTH = image.shape[1]
    preprocess_input = sm.get_preprocessing(BACKBONE)
    resize_preprocess_transform = A.Compose([
        #A.PadIfNeeded(768, 1024, p=1.0, border_mode=0),
        A.Resize(height=768, width=1024, p=1.0),
        A.Lambda(image=preprocess_input)
    ])
    resized_preprocessed = resize_preprocess_transform(image=image)
    image = resized_preprocessed["image"]
    print("--> Image loaded - going to detection...")

    # Run detection, return to original size and save predicted mask
    image = np.expand_dims(image, axis=0)
    pr_mask = model.predict(image)
    pr_mask = pr_mask.squeeze()
    image = denormalize(image).squeeze()
    inverse_resize_transform = A.Compose([
        #A.PadIfNeeded(HEIGHT, WIDTH, p=1.0, border_mode=0),
        A.Resize(height=HEIGHT, width=WIDTH, p=1.0)
    ])
    inverse_resized = inverse_resize_transform(image=image, mask=pr_mask)
    image, pr_mask = inverse_resized["image"], inverse_resized["mask"]
    skimage.io.imsave(os.path.join(app.config['INFERENCE_DIR'], "pr_mask.png"),
                      pr_mask)
    print("--> Detection successful - now saving results for display...")

    # Post-processing: delineate contours to get polygons, delete smaller ones, save as JSON

    # T = 204
    # formworks = threshold_image(img_as_ubyte(pr_mask[...,0]), threshold = T)
    # pumps = threshold_image(img_as_ubyte(pr_mask[...,1]), threshold = T)
    # contours_formworks = measure.find_contours(formworks, 1)
    # contours_pumps = measure.find_contours(pumps, 1)
    # contours_formworks = [x.round(0).tolist() for x in np.array(contours_formworks)]
    # contours_pumps = [x.round(0).tolist() for x in np.array(contours_pumps)]

    # pr_polygons = json_template
    # try:
    #     for idx, contour in enumerate(contours_formworks.extend(contours_pumps)):
    #         poly = Polygon(contour)
    #         if poly.area < 300: # very few ground truth formworks & pumps are <300px area
    #             continue
    #         _, concave_hull_coords = alpha_shape(poly, alpha=0.01)
    #         details =
    #         if contour is in contours_formworks:
    #            category_id
    #         pr_polygons['annotations'].append(details)
    #         pr_polygons[f'formwork_{idx+1}'] = concave_hull_coords
    # except:
    #     print('Post-processing failed - review alpha_shape function.')

    # with open(os.path.join(app.config['INFERENCE_DIR'],'pr_polygons.json'), 'w') as f:
    #     json.dump(pr_polygons, f)

    grayscale = rgb2gray(pr_mask)
    contours = measure.find_contours(grayscale, 0.1)
    pr_polygons = {}
    try:
        for idx, contour in enumerate(contours):
            poly = Polygon(contour)
            if poly.area < 300:  # very few ground truth formworks & pumps are <300px area
                continue
            _, concave_hull_coords = alpha_shape(poly, alpha=0.01)
            pr_polygons[f'polygon_{idx}'] = concave_hull_coords
            #pr_polygons[f'polygon_{idx}'] = contour
    except:
        print('Post-processing failed - review alpha_shape function.')
    with open(os.path.join(app.config['INFERENCE_DIR'], 'pr_polygons.json'),
              'w') as f:
        json.dump(pr_polygons, f)

    # Overlay detected polygons on original image and save result
    fig, ax = plt.subplots()
    ax.imshow(image)
    colors = random_colors(len(contours))
    for idx, contour in enumerate(contours):
        #ax.plot(contour[:, 1], contour[:, 0], linewidth=2)
        color = colors[idx]
        p = patches.Polygon(list(zip(contour[:, 1], contour[:, 0])),
                            facecolor=color,
                            edgecolor=color,
                            alpha=0.5)
        ax.add_patch(p)
    ax.axis('off')
    plt.savefig(os.path.join(app.config['INFERENCE_DIR'], "result.png"),
                bbox_inches='tight',
                pad_inches=0.0)
    plt.close()

    # Save detection summary
    data = {}
    data["Successful detection"] = True
    try:
        #data["Number of detected formworks"] = len(contours_formworks)
        data["Number of detected formworks"] = len(pr_polygons)
        data["Number of detected pumps"] = 0
    except:
        data["Number of detected objects"] = len(contours)
    data_df = pd.DataFrame(data, index=[0]).T
    data_df.to_excel(os.path.join(app.config['INFERENCE_DIR'],
                                  "detection_summary.xlsx"),
                     header=False)
    print("--> Saving done - showing success page.\n")

    return flask.render_template('success.html', data=data)
Example #24
0
    def __init__(self, model_name, models_path, name=None, grayscale=False):
        if not re.match(r"^[a-z0-9/]+$", model_name):
            raise ValueError("illegal model name '%s'" % model_name)

        if name is None:
            name = model_name
        self._name = name
        _check_predictor_name(self._name)

        models_path = Path(models_path)
        network_path = models_path / model_name

        for filename in ("meta.json", "model.h5"):
            asset_path = network_path / filename
            if not asset_path.exists():
                raise FileNotFoundError("no model file found at %s" %
                                        asset_path)

        with open(network_path / "meta.json", "r") as f:
            meta = json.loads(f.read())
        classes = meta["classes"]

        import segmentation_models

        # the following commented code fails to work.
        # see https://github.com/qubvel/segmentation_models/issues/153
        # and https://stackoverflow.com/questions/54835331/how-do-i-load-a-keras-saved-model-with-custom-optimizer
        '''
		model = getattr(segmentation_models, meta["model"])(
			meta["backbone"],
			classes=len(classes),
			activation="softmax")
		logging.info("loading model at %s" % str(network_path / "model.h5"))
		model.load_weights(str(network_path / "model.h5"))
		'''

        # note that we need keras.models.load_model, as
        # tensorflow.keras.models.load_model won't work,
        # see https://github.com/keras-team/keras-contrib/issues/488
        # and https://github.com/tensorflow/tensorflow/issues/25200
        # so we need a separate keras installation here.
        from keras.models import load_model

        model = load_model(str(network_path / "model.h5"), compile=False)

        self._preprocess = segmentation_models.get_preprocessing(
            meta["backbone"])

        self._model = model
        self._full_size = tuple(meta["full_size"])
        self._full_shape = tuple(reversed(self._full_size))
        self._tile_size = tuple(meta["tile_size"])

        self._tiles = list(
            Tiles(self._tile_size, beta0=meta["tile_beta"])(meta["full_size"]))

        self._type = PredictorType[meta["type"].upper()]
        self._classes = self._type.classes(
            dict((v, i) for i, v in enumerate(classes)))

        self._grayscale = grayscale
		preprocessing_fn (callbale): data normalization function 
			(can be specific for each pretrained neural network)
	Return:
		transform: albumentations.Compose

	"""

	_transform = [
		A.Lambda(image=preprocessing_fn),
	]
	return A.Compose(_transform)

# network
best_weight = model_folder+'/best_model-{:03d}.h5'.format(epoch)
CLASSES = []
preprocess_input = sm.get_preprocessing(backbone)

#create model
# CLASSES = ['live', 'inter', 'dead']
CLASSES = ['live', 'dead']
n_classes = len(CLASSES) + 1
activation = 'softmax'
net_func = globals()[net_arch]
decoder_filters=(int(nb_filters),int(nb_filters/2), int(nb_filters/4), int(nb_filters/8), int(nb_filters/16))
model = net_func(backbone, classes=n_classes, encoder_weights = None, activation=activation,\
		decoder_block_type = upsample, feature_version = feature_version, decoder_filters = decoder_filters)
#model = net_func(backbone, classes=n_classes, encoder_weights = None, activation=activation,\
#		decoder_block_type = upsample, decoder_filters = decoder_filters)
# model = net_func(backbone, classes=n_classes, activation=activation)
model.summary()
#load best weights
df_val['Has_Hemorrhage'].value_counts()

df_diag.to_csv('df_data.csv.gz', compression='gzip', index=False)

df_train.to_csv('df_train.csv.gz', compression='gzip', index=False)
df_val.to_csv('df_val.csv.gz', compression='gzip', index=False)

df_test.to_csv('df_test.csv.gz', compression='gzip', index=False)

# check if the files were saved
!ls

from segmentation_models import  get_preprocessing # this line has an error in the docs

BACKBONE = 'densenet121'
preprocess_input = get_preprocessing(BACKBONE)

# We are only using brain images for training.
# These are originally single channel images but cv2 will read them with 3 channels.

def train_generator(batch_size=30):
    
    while True:
        
        # load the data in chunks (batches)
        for df in pd.read_csv('df_train.csv.gz', chunksize=batch_size):
            
            # get the list of images
            image_id_list = list(df['new_image_fname'])
            mask_id_list = list(df['new_mask_fname'])
            
Example #27
0
def main():
    #sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(log_device_placement=True))

    # paths
    file_root = os.path.join("/opt", "data", "gaul_severstal_data")
    project_dir = os.path.join("/opt", "project")
    log_dir = os.path.join(project_dir, "src", "logs", "fit",
                           datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
    images_dir = os.path.join(file_root, "train_images")
    df_path = os.path.join(file_root, "train.csv")
    local_tune_dir = os.path.join(project_dir, "src", "tune")
    plot_dir = os.path.join(project_dir, "src", "plots")

    # dataset split
    training_split = 0.6
    validation_split = 0.2
    testing_split = 1.0 - (training_split + validation_split)

    # unet encoder backbone
    backbone = 'vgg16'

    # data options
    classes = [3]
    n_classes = len(classes)
    use_greyscale = False
    batch_size = 1

    # load in damage labels
    train_df, val_df, test_df = load_dataframe_split(df_path,
                                                     classes=classes,
                                                     val_size=validation_split,
                                                     test_size=testing_split)

    # image resizing parameters
    image_scale_down = 5
    height = int(np.floor(256 / image_scale_down / 32) * 32)
    width = int(np.floor(1600 / image_scale_down / 32) * 32)
    image_channels = 1 if use_greyscale else 3
    resize_shape = (
        height, width, image_channels
    )  # original is (256, 1600, 3), needs to be divisible by 32
    mask_shape = (height, width, n_classes)

    # image preprocessing
    preprocessing_input = sm.get_preprocessing(backbone)

    # loss
    dice_loss = sm.losses.DiceLoss()

    # metrics
    iou_score = sm.metrics.IOUScore(threshold=0.5)
    metrics = [iou_score]

    # datasets and dataloaders
    train_dataset, val_dataset, test_dataset = get_datasets(
        images_dir=images_dir,
        preprocessing_input=preprocessing_input,
        resize_shape=resize_shape,
        train_df=train_df,
        val_df=val_df,
        test_df=test_df,
        use_greyscale=use_greyscale)
    train_dataloader, val_dataloader, test_dataloader = get_dataloaders(
        train_dataset=train_dataset,
        val_dataset=val_dataset,
        test_dataset=test_dataset,
        train_batch_size=batch_size)

    # tuner config
    config = {
        "dropout":
        tune.grid_search([0.1, 0.2, 0.3, 0.4, 0.5]),
        "learning_rate":
        tune.grid_search([1e-5, 1e-4, 1e-3, 1e-2]),
        "optimizer":
        tune.grid_search([keras.optimizers.Adam, keras.optimizers.RMSprop]),
        "encoder_freeze":
        tune.grid_search([True, False]),
    }

    # best model config
    best_config = {
        "dropout": 0.1,
        "learning_rate": 1e-4,
        "optimizer": keras.optimizers.Adam,
        "encoder_freeze": False,
    }

    use_best_config = True
    config = best_config if use_best_config else config

    # search algorithm
    # hyperopt = HyperOptSearch(metric="val_iou_score", mode="max")

    # trial scheduler
    hyperband = HyperBandScheduler(metric="val_iou_score", mode="max")

    # tune model or load best_model.h5
    tune_model = True
    model_name = "best_model.h5"

    if tune_model:
        analysis = tune.run(
            tune.with_parameters(train_unet,
                                 train_dataloader=train_dataloader,
                                 val_dataloader=val_dataloader,
                                 loss=dice_loss,
                                 metrics=metrics),
            resources_per_trial={"gpu": 1},
            config=config,
            # search_alg=hyperopt,
            scheduler=hyperband,
            local_dir=local_tune_dir)
    else:
        model = keras.models.load_model(model_name,
                                        custom_objects={
                                            "dice_loss": dice_loss,
                                            "iou_score": iou_score
                                        })

        use_test = True
        evaluate_dataset = test_dataset if use_test else val_dataset
        evaluate_dataloader = test_dataloader if use_test else val_dataloader

        make_plots = False
        if make_plots is True:
            for i in range(len(evaluate_dataset)):
                imageId = evaluate_dataset.ids[i]
                image, true_mask = evaluate_dataset[i]
                image = np.expand_dims(image, axis=0)
                pr_mask = model.predict(image)

                image = denormalize(image[0])
                pr_mask = denormalize(pr_mask[0]).astype('uint8')

                visualize(image,
                          true_mask,
                          pr_mask,
                          name=imageId,
                          save_dir=plot_dir)

        # model evaluation and baseline comparison
        evaluate_results = model.evaluate(evaluate_dataloader, batch_size=1)
        for i, val in enumerate(evaluate_results):
            print(f'{model.metrics_names[i]}: {val}')

        # baseline is a mask covering the entire left half of the image
        baseline_mask = np.zeros(mask_shape)
        baseline_mask[:, :np.int(width / 2), :] = 1
        baseline_iou_scores = []
        for i in range(len(evaluate_dataset)):
            image, true_mask = evaluate_dataset[i]
            image = denormalize(image)
            iou = iou_score(true_mask.astype('float32'), baseline_mask)
            #visualize(image, true_mask, baseline_mask.astype('uint8'))
            baseline_iou_scores.append(iou)
        average_baseline_iou = np.average(baseline_iou_scores)
        print(f'baseline iou_score: {average_baseline_iou}')
                          classes=2 if border else 1,
                          activation='sigmoid',
                          encoder_weights='imagenet',
                          encoder_freeze=not trainable_encoder,
                          downsample_factor=8,
                          psp_conv_filters=512,
                          psp_pooling_type='avg',
                          psp_use_batchnorm=True,
                          psp_dropout=None)
    else:
        print('Invalid segmentation model type')
        exit(0)
    return model


preprocessing = sm.get_preprocessing(backbone)
iou = sm.metrics.IOUScore(per_image=False)


def bce_dice_loss(y_true, y_pred):
    bce = tf.keras.losses.binary_crossentropy(y_true, y_pred)
    y_true_ = y_true[..., 0]  # !!! only first channel
    y_pred_ = y_pred[..., 0]  # !!! only first channel
    dice = sm.losses.dice_loss(y_true_, y_pred_)
    return bce + dice


def iou_fc(y_true, y_pred):
    y_true_ = y_true[..., 0]  # !!! only first channel
    y_pred_ = y_pred[..., 0]  # !!! only first channel
    return iou(y_true_, y_pred_)
    'shuffle': True
}

X_train = np.array(X_train)
X_train = X_train.reshape(X_train.shape[0])

X_val = np.array(X_val)
X_val = X_val.reshape(X_val.shape[0])

training_generator = DataGenerator(X_train, mask_data, **params)
validation_generator = DataGenerator(X_val, mask_data, **params)

import segmentation_models as sm

BACKBONE = 'resnet34'
preprocess_input = sm.get_preprocessing(BACKBONE)

rooftopsolar_model1 = sm.Unet(BACKBONE, encoder_weights='imagenet')
rooftopsolar_model1.compile(
    'Adam',
    loss=sm.losses.bce_jaccard_loss,
    metrics=[sm.metrics.iou_score],
)

epochs = 80

early_stopping_callback = EarlyStopping(monitor='val_loss', patience=5)
checkpointer = ModelCheckpoint('rooftopsolar_model6.h5',
                               monitor='loss',
                               verbose=1,
                               save_best_only=True)
Example #30
0
# In[11]:

BACKBONE = 'efficientnetb3'
BATCH_SIZE = 1
CLASSES = [
    'road', 'sidewalk', 'building', 'traffic light', 'traffic sign',
    'vegetation', 'person', 'car'
]
CLASS_WEIGHTS = np.array([2, 2, 1, 2, 2, 1, 2, 2,
                          0.5])  # last weight is added for backgound (0.5)
# CLASSES = ['road', 'car']
# CLASS_WEIGHTS = np.array([2, 2, 0.5]) # last weight is added for backgound (0.5)
LR = 0.001
EPOCHS = 100

preprocess_input = sm.get_preprocessing(BACKBONE)

# In[12]:

# define network parameters
n_classes = 1 if len(CLASSES) == 1 else (
    len(CLASSES) + 1)  # case for binary and multiclass segmentation
activation = 'sigmoid' if n_classes == 1 else 'softmax'

#create model
model = sm.Unet(BACKBONE, classes=n_classes, activation=activation)

# In[13]:

# define optomizer
optim = keras.optimizers.Adam(LR)