Пример #1
0
def main():
    ##
    ext = '.png'
    ##

    path, txt_file, path_converted = process_arguments(sys.argv)

    # Create dir for converted labels
    if not os.path.isdir(path_converted):
        os.makedirs(path_converted)

    with open(txt_file, 'rb') as f:
        for img_name in f:
            img_base_name = img_name.strip()
            img_base_name = str(img_base_name, encoding='utf-8')
            print(path)
            print(img_base_name)
            img_name = os.path.join(path, img_base_name) + ext
            img = imread(img_name)

            if (len(img.shape) > 2):
                img = convert_from_color_segmentation(img)
                imsave(os.path.join(path_converted, img_base_name) + ext, img)
            else:
                print(img_name +
                      " is not composed of three dimensions, therefore "
                      "shouldn't be processed by this script.\n"
                      "Exiting.",
                      file=sys.stderr)

                exit()
Пример #2
0
def outgenf(img_name):
    Prediction_dir = "../../data/Prediction"
    Prediction_name = str(img_name) +".png"
    Prediction_path = os.path.join(Prediction_dir,Prediction_name)
    prediction_img = imread(Prediction_path)
    prediction = utils.convert_from_color_segmentation(prediction_img)
    return prediction
Пример #3
0
def preprocess_label(img, lut, mode, im_sz):
    # If label is three-dimensional image we have to convert it to
    # corresponding labels (0 - 20). Currently anticipated labels are from
    # VOC pascal datasets.
    if (len(img.shape) > 2):
        img = convert_from_color_segmentation(img)

    img = preprocess_data(img, mode, im_sz, 'label')
    img = lut[img]
    img = np.expand_dims(img, axis=0)
    #img = _2D_to_ND(img, len(np.unique(lut)))
    #img = img.transpose((2,0,1))
    return img
Пример #4
0
def contain_class(img_name, class_ids, class_names):
  img = imread(img_name)

  # If label is three-dimensional image we have to convert it to
  # corresponding labels (0 - 20). Currently anticipated labels are from
  # VOC pascal datasets.
  if (len(img.shape) > 2):
    img = convert_from_color_segmentation(img)

  for i,j in enumerate(class_ids):
    if j in np.unique(img):
      return class_names[i]
    
  return False
Пример #5
0
def contain_class(img_name, class_ids, lut):
    img = imread(img_name)

    # If label is three-dimensional image we have to convert it to
    # corresponding labels (0 - 20). Currently anticipated labels are from
    # VOC pascal datasets.
    if (len(img.shape) > 2):
        img = convert_from_color_segmentation(img)

    img_labels = np.unique(img)

    if len(set(img_labels).intersection(class_ids)) >= 1:
        return lut[img]
    else:
        return None
Пример #6
0
def contain_class(img_name, class_ids, class_names):
    '''
  arguments:
    img_name: name of image
    class_ids: interested classes index
    class_names: interested classes name
  '''
    img = imread(img_name)

    # If label is three-dimensional image we have to convert it to
    # corresponding labels (0 - 20). Currently anticipated labels are from
    # VOC pascal datasets.
    # if img is rgb structure, transform img grayscale
    if (len(img.shape) > 2):
        img = convert_from_color_segmentation(img)

    for i, j in enumerate(class_ids):
        if j in np.unique(img):
            # if image pixel have class_id, return calss name.
            return class_names[i]

    return False
Пример #7
0
def evaluate_IoU_class_general(truth_dir, outgenf, imglist_file):
    class_count = len(
        utils.pascal_classes()) + 1  #include background as a class
    int_vec = [list() for i in range(class_count)]
    union_vec = [list() for i in range(class_count)]
    with open(imglist_file, "r") as imf:
        im_names = imf.read().splitlines()

    im_counts = len(im_names)
    #Store the IoU for each specific image
    iou_img_dict = {}
    int_vec = np.zeros((class_count, im_counts))
    union_vec = np.zeros((class_count, im_counts))

    ptr = 0
    for img_name in im_names:
        img_truth_name = os.path.join(truth_dir, img_name) + '.png'
        img_truth = imread(img_truth_name)
        truth_data = utils.convert_from_color_segmentation(img_truth)
        output_data = outgenf(img_name)
        #Calculate IoU for each class
        iou_img = np.zeros(class_count)
        for class_idx in range(0, class_count):
            int_i, uni_i = int_uni_cls(truth_data, output_data, class_idx)
            if (int_i == 0 and uni_i == 0):
                iou_img[class_idx] = np.NaN
            else:
                iou_img[class_idx] = int_i / uni_i
            int_vec[class_idx, ptr] = int_i
            union_vec[class_idx, ptr] = uni_i
        iou_img_dict[
            "img_" +
            img_name] = iou_img  #add "img_" here to ensure variable name is valid in matlab
        ptr = ptr + 1
    iou_cls = [
        np.sum(int_vec[i]) / np.sum(union_vec[i]) for i in range(class_count)
    ]
    return iou_cls, iou_img_dict
Пример #8
0
with open(Name_list, "r") as f:
    lines = f.read().splitlines()

np.random.seed(42)
lines_shuffled = np.random.permutation(lines)
Sample_size = 94
lines_sampled = lines_shuffled[0:Sample_size]
Max_iteration = 20
kl_divergence = np.zeros((Sample_size, Max_iteration))

for i, line in enumerate(lines_sampled):
    Image_name = str(line) + ".jpg"
    Image_path = os.path.join(Image_dir, Image_name)
    Unary_name = str(line) + ".mat"
    Unary_path = os.path.join(Unary_dir, Unary_name)
    img_truth_name = os.path.join(Ground_truth_dir, str(line)) + '.png'
    img_truth = imread(img_truth_name)
    truth_data = utils.convert_from_color_segmentation(img_truth)
    img = imread(Image_path)
    unary = scipy.io.loadmat(Unary_path)
    unary_energy = unary["energy"].reshape(21, img.shape[0] * img.shape[1])
    crf = DenseCRF(img.shape[0], img.shape[1], 21, Ground_truth=truth_data)
    crf.set_unary(-unary_energy)
    crf.add_pairwise_gaussian(1, 1, PottsCompatibility(3))
    crf.add_pairwise_bilateral(61, 61, 11, 11, 11, img, PottsCompatibility(10))
    Q = crf.inference(Max_iteration)
    kl_divergence[i, :] = crf.KL_divergence
    print(kl_divergence[i, :])
scipy.io.savemat(os.path.join(output_path, "kl_divergence"),
                 {"kl_divergence": kl_divergence})