Esempio n. 1
0
def save_soft_encode(path):
    image = image_util.read_image(path)
    lab = image_util.convert_rgb_to_lab(image)
    se = image_util.soft_encode_lab_img(lab)
    new_path = '../data/soft_encoded/' + path[-16:-5] + '_soft_encoded.npz'
    np.savez_compressed(new_path, se)
    return new_path
def test_weight_loss():
    image = image_util.read_image('../test_images/fish.JPEG')
    image = image_util.convert_rgb_to_lab(image)
    image = image_util.soft_encode_lab_img(image)
    print(image.shape)

    with open('../probabilities/weights.pickle', 'rb') as fp:
        weights = pickle.load(fp)
    print("Shape", image.shape)

    test = np.random.rand(64, 64, 262)
    losses = 0
    for h in range(image.shape[0]):
        loss = np.dot(image[h],
                      np.log(test[h] + 0.000000000000000001).transpose())
        loss = np.diag(loss)
        loss = -loss
        loss = np.sum(loss)
        losses += loss
    print(losses)

    losses = 0
    for h in range(test.shape[0]):
        vs = np.array([weights[np.argmax(x)] for x in image[h]])
        loss = vs[:, np.newaxis] * np.dot(
            image[h],
            np.log(test[h] + 0.000000000000000001).transpose())
        loss = np.diag(loss)
        loss = -loss
        loss = np.sum(loss)
        losses += loss
    print(losses)

    return losses
Esempio n. 3
0
def save_input_output_2classes(path, newpath):
    image = image_util.read_image(path)
    lab = image_util.convert_rgb_to_lab(image)
    se = image_util.soft_encode_lab_img(lab)

    new_path = newpath
    np.savez_compressed(new_path, input=lab, output=se)
Esempio n. 4
0
def load_image_grey_in_softencode_out(image_path):
    """
    Load an image, create the input and output of the network.

    The input is a gray-scale image as a (width*height*1) numpy array
    The output is a soft-encoding of the expected color bin as a
    (width*height*num_bins) numpy array

    :param image_path: the path to the image
    :return: tuple of x and y (input and output)
    """
    filename = os.path.split(image_path)[-1]
    filename = os.path.splitext(filename)[0]
    se_filename = os.path.join(c.soft_encoding_training_and_val_dir,
                               filename + c.soft_encoding_filename_postfix)

    rgb = image_util.read_image(image_path)
    cielab = image_util.convert_rgb_to_lab(rgb)

    gray_channel = cielab[:, :, 0]
    gray_channel = gray_channel[:, :, np.newaxis]

    if os.path.exists(se_filename):
        soft_encoding = np.load(se_filename)['arr_0']
    else:
        soft_encoding = image_util.soft_encode_lab_img(cielab)

    return gray_channel, soft_encoding
Esempio n. 5
0
def load_rgb_in_softencode_out(image_path):
    path_split = os.path.split(image_path)
    fn = path_split[1]

    tag = fn.split("_")[0]
    num = fn.split("_")[1]

    rgb_save_path = os.path.join(
        path_split[0],
        "{}_{}_rgb.npz".format(tag, num)
    )
    if os.path.exists(rgb_save_path):
        rgb = np.load(rgb_save_path)['arr_0']
    else:
        rgb = image_util.read_image(image_path)

    filename = os.path.split(image_path)[-1]
    filename = os.path.splitext(filename)[0]
    se_filename = os.path.join(c.soft_encoding_training_and_val_dir,
                               filename + c.soft_encoding_filename_postfix)
    if os.path.exists(se_filename):
        soft_encoding = np.load(se_filename)['arr_0']
    else:
        cielab = image_util.convert_rgb_to_lab(rgb)
        soft_encoding = image_util.soft_encode_lab_img(cielab)

    return rgb, soft_encoding
def create_rgb_to_bin():
    '''
    Create a dictionary of all rgb colours to the corresponding bin size
    '''

    path_bins = '../np/bins.npz'
    bins = np.load(path_bins)['arr_0']
    n_bins = len(bins)

    path_bincenters = '../np/bincenters.npz'
    bincenters = np.load(path_bincenters)['arr_0']

    rgb_to_bin = {}

    for rall in range(256):
        print(rall)
        for gall in range(256):
            for ball in range(256):
                r = rall / 256
                g = gall / 256
                b = ball / 256

                r = np.ones((1, 1)) * r
                g = np.ones((1, 1)) * g
                bb = np.ones((1, 1)) * b

                rgb_pixel = np.stack((r, g, bb), axis=-1)
                cie_pixel = image_util.convert_rgb_to_lab(rgb_pixel)

                a = cie_pixel[:, :, 1].flatten()
                b = cie_pixel[:, :, 2].flatten()

                ab = np.stack((a, b), axis=-1)
                d = np.zeros(n_bins)

                for idx, c in enumerate(bincenters):
                    dist = np.linalg.norm(ab - c, axis=-1)
                    d[idx] = dist

                bin = np.argmin(d)

                r *= 256
                g *= 256
                bb *= 256
                rgb_string = str(r[0][0]) + ', ' + str(g[0][0]) + ', ' + str(
                    bb[0][0])
                print(rgb_string)
                rgb_to_bin[rgb_string] = bin
Esempio n. 7
0
def convert(image_paths, out_path):
    '''
    This functions creates tf record object.
    Inputs are the paths to the rgb pictures. This functions saves
    the different images in their cielab format and in their soft encoded format
    :param image_path: Path to the images to convert into a tfrecord object
    :param out_path: Path where to save the tfrecord object
    :return:
    '''

    print("Converting: " + out_path)
    # Number of images. Used when printing the progress.
    num_images = len(image_paths)

    # Open a TFRecordWriter for the output-file.
    with tf.python_io.TFRecordWriter(out_path) as writer:
        # Iterate over all the image-paths
        for i, path in enumerate(image_paths):
            if i % 1000 == 0:
                print('Serialised ', i, 'files')
            # Read the images
            rgb = np.array(image_util.read_image(path))
            cie = np.array(image_util.convert_rgb_to_lab(rgb))
            se = np.array(image_util.soft_encode_lab_img(cie))

            # Convert them into raw bytes
            cie_bytes = cie.tostring()
            se_bytes = se.tostring()

            # Create a dict with the data saved in the record files

            data = \
                {
                    'cie': wrap_bytes(cie_bytes),
                    'label': wrap_bytes(se_bytes)
                }

            # Wrap the data as TensorFlow Features.
            feature = tf.train.Features(feature=data)

            # Wrap again as a TensorFlow Example.
            example = tf.train.Example(features=feature)

            # Serialize the data.
            serialized = example.SerializeToString()

            # Write the serialized data to the TFRecords file.
            writer.write(serialized)
Esempio n. 8
0
    def on_train_begin(self, logs=None):
        # create batch and store rgb and grey image
        for idx, path in enumerate(self.image_paths):
            rgb = image_util.read_image(path)

            save_path = "img_{}_epoch_{}.png".format(idx, "0_ground_truth")
            save_path = os.path.join(self.root_dir, save_path)
            image_util.save_image(save_path, rgb)

            grey = image_util.read_image(path, as_gray=True)
            save_path = "img_{}_epoch_{}.png".format(idx, "0_grey")
            save_path = os.path.join(self.root_dir, save_path)
            image_util.save_image(save_path, grey)

            lab = image_util.convert_rgb_to_lab(rgb)
            grey = lab[:, :, 0:1]
            self.batch[idx,] = grey

        self.save_images('0_initial_prediction')
Esempio n. 9
0
def load_image_grey_in_ab_out(image_path):
    """
    Load an image, create the input and output of the network.

    The input is a gray-scale image as a (width*height*1) numpy array
    The output is a soft-encoding of the expected color bin as a
    (width*height*num_bins) numpy array

    :param image_path: the path to the image
    :return: tuple of x and y (input and output)
    """
    rgb = image_util.read_image(image_path)
    cielab = image_util.convert_rgb_to_lab(rgb)

    gray_channel = cielab[:, :, 0]
    gray_channel = gray_channel[:, :, np.newaxis]

    ab_channel = cielab[:, :, 1:]

    return gray_channel, ab_channel