Ejemplo n.º 1
0
    def train_seg_mem(self, model, epoch, batch):
        maxImageIndex = 100
        print(self)
        try:
            xs = self.validation_data[0]
            ys = self.validation_data[1]
        except:
            xs = K.eval(self.model.inputs[0])
            ys = self.model.targets[0]
        ys_pred = self.model.predict(xs)
        image_size = self.flag.image_size
        #print("Shape xs: " + str(xs.shape) )
        #print("Shape ys: " + str(ys.shape) )
        #print("Shape pred ys: " + str(ys_pred.shape) )

        # Save the predicted output prob, image, and original mask
        for imageIndex in range(min(maxImageIndex, xs.shape[0])):
            x = xs[imageIndex, ...]
            y = ys[imageIndex, ...]
            y_pred = ys_pred[imageIndex, ...]
            #if multi-class y then merge them by giving different values: e.g. cx = 2, cb = 1, bg = 0
            if y.shape[2] > 1:
                y = mergeLabelImage(y).astype(np.float32)
                y_pred = mergeLabelImage(y_pred).astype(np.float32)
                y /= y.max()
                y_pred /= y_pred.max()
            #print( "x min: " + str(x.min()) +", x max: " + str(x.max()) )
            #print( "y min: " + str(y.min()) +", y max: " + str(y.max()) )
            #print( "y_pred min: " + str(y_pred.min()) +", y_pred max: " + str(y_pred.max()) )
            imageName = str(imageIndex)
            imgMask = (y_pred * 255).astype(np.uint8)
            imgMaskOri = (y * 255).astype(np.uint8)
            #print( "y min: " + str(y.min()) +", y max: " + str(y.max()) )
            img = (x).astype(np.uint8)
            imgColor = cv2.cvtColor(x, cv2.COLOR_GRAY2BGR)
            imgMaskColor = cv2.applyColorMap(imgMask, cv2.COLORMAP_JET)
            imgMaskOriColor = cv2.applyColorMap(imgMaskOri, cv2.COLORMAP_JET)
            output_path_combined = os.path.join(
                self.flag.output_images_dir,
                'combined_index-%04d_batch-%04d_epoch-%04d.png' %
                (imageIndex, batch, epoch))
            image_combined = np.concatenate(
                (imgColor, imgMaskColor, imgMaskOriColor), axis=1)
            cv2.imwrite(output_path_combined, image_combined)
            writeImagePathToCsv(self.flag.images_csv_path, imageIndex, batch,
                                epoch, output_path_combined)
Ejemplo n.º 2
0
def saveLabelImage( mask, output_path ):
    y = mergeLabelImage(mask).astype( np.float32 )
    y = y + 0.9
    y /= y.max()
    maskOri = (y*255).astype( np.uint8 )
    maskOriColor = cv2.applyColorMap( maskOri, cv2.COLORMAP_HSV )
    maskColor = np.flip(maskOriColor, 2)
    #maskColor = maskOriColor
    cv2.imwrite( output_path, maskColor )
Ejemplo n.º 3
0
def makeOverlayMaskDifference( img, mask_ref, mask, output_path ):
    
    y = mergeLabelImage(mask).astype( np.float32 )
    y_ref = mergeLabelImage(mask_ref).astype( np.float32 )
    diff = np.zeros( y.shape )
    diff[ y != y_ref ] = 1
    diff /= diff.max()
    diff = (diff*255).astype( np.uint8 )
    diffColor = cv2.applyColorMap( diff, cv2.COLORMAP_JET )
    scaleIntensity = 500
    img = img.astype( np.float32 )
    img /= img.max()
    img = np.fmin( 255.0 * np.ones(img.shape, np.float32 ), scaleIntensity * img )
    img = img.astype( np.uint8 )
    diffColor = np.flip( diffColor, 2)
    imgColor = cv2.cvtColor( img, cv2.COLOR_GRAY2BGR )
    imgOverlay = cv2.addWeighted(imgColor, 0.9, diffColor, 0.4, 0.0)
    cv2.imwrite( output_path, imgOverlay )
Ejemplo n.º 4
0
def makeOverlay( img, mask, output_path ):
    
    y = mergeLabelImage(mask).astype( np.float32 )
    y = y + 0.9
    y /= y.max()
    maskOri = (y*255).astype( np.uint8 )
    maskOriColor = cv2.applyColorMap( maskOri, cv2.COLORMAP_HSV )
    scaleIntensity = 500
    img = img.astype( np.float32 )
    img /= img.max()
    img = np.fmin( 255.0 * np.ones(img.shape, np.float32 ), scaleIntensity * img )
    img = img.astype( np.uint8 )
    maskColor = np.flip(maskOriColor, 2)    
    imgColor = cv2.cvtColor( img, cv2.COLOR_GRAY2BGR )
    imgOverlay = cv2.addWeighted(imgColor, 0.9, maskColor, 0.4, 0.0)
    cv2.imwrite( output_path, imgOverlay )
Ejemplo n.º 5
0
def showData(flag):

    img_rows = flag.image_size
    img_cols = flag.image_size

    print('-' * 30)
    print('Loading train data...')
    print('-' * 30)

    imgs_train, imgs_test, imgs_mask_train_all, imgs_mask_test_all, imgs_id_test, imgs_id_train = loadDataParams(
        flag)
    images = imgs_train
    images = preprocess(images, img_rows, img_cols)
    masks_all = imgs_mask_train_all

    print('-' * 30)
    print('Fuse masks to single multi-label image')
    print('-' * 30)
    regionList = flag.region_list
    nClasses = len(regionList) + 1
    s = masks_all[regionList[0]].shape
    nImages = s[0]
    masks = np.zeros((nImages, img_rows, img_cols, nClasses))
    masks[:, :, :, 0] = np.ones((nImages, img_rows, img_cols))
    for regionIndex in range(len(regionList)):
        # put the mask layer of the region index to 1
        masks_region = masks_all[regionList[regionIndex]]
        masks_region = preprocess(masks_region, img_rows, img_cols)
        temp = masks_region[:, :, :, 0]
        masks[:, :, :, regionIndex + 1] = temp
        # and the background layer to 0, for every region pixel
        masks[:, :, :, 0] = (1 - temp) * masks[:, :, :, 0]

    print('-' * 30)
    print('Save masks as images')
    print('-' * 30)
    scaleIntensity = 1
    for imageIndex in range(nImages):
        fileName = 'mask_sample_%d.tif' % (imageIndex)
        filePath = os.path.join(flag.output_masks_feed_dir, fileName)
        maskData = np.transpose(masks[imageIndex, ...], (2, 0, 1))
        maskData = np.expand_dims(maskData, axis=3)
        writeData(filePath, maskData)

        y = masks[imageIndex, ...]
        y = mergeLabelImage(y).astype(np.float32)
        y /= y.max()
        x = images[imageIndex, ...].astype(np.float32)
        x = np.fmin(255.0 * np.ones(x.shape, np.float32), scaleIntensity * x)
        x = x.astype(np.uint8)
        x = np.squeeze(x)
        print(x.shape)
        #x /= x.max()
        #img = (x*255).astype( np.uint8 )
        imgColor = cv2.cvtColor(x, cv2.COLOR_GRAY2BGR)
        imgMaskOri = (y * 255).astype(np.uint8)
        imgMaskOriColor = cv2.applyColorMap(imgMaskOri, cv2.COLORMAP_JET)
        imgOverlay = cv2.addWeighted(imgColor, 0.9, imgMaskOriColor, 0.4, 0.0)
        output_path_mask_ori = os.path.join(
            flag.output_images_dir, 'mask_index-%04d.png' % (imageIndex))
        output_path_overlay = os.path.join(
            flag.output_plots_dir, 'overlay_index-%04d.png' % (imageIndex))
        output_path_image = os.path.join(flag.output_plots_dir,
                                         'image_index-%04d.png' % (imageIndex))
        cv2.imwrite(output_path_image, imgColor)
        cv2.imwrite(output_path_mask_ori, imgMaskOriColor)
        cv2.imwrite(output_path_overlay, imgOverlay)

    print('-' * 30)
    print('Save (preprocessed) images as images')
    print('-' * 30)
    for imageIndex in range(nImages):
        fileName = 'image_sample_%d.tif' % (imageIndex)
        filePath = os.path.join(flag.output_images_feed_dir, fileName)
        imageData = scaleIntensity * np.transpose(images[imageIndex, ...],
                                                  (2, 0, 1))
        imageData = np.expand_dims(imageData, axis=3)
        writeData(filePath, imageData)