Beispiel #1
0
def convert_randoms(paths, labels, resize_dims=None, warp_ok=False):
    # ** Reads images from paths, randomly augments them and returns the images and #   labels as a list. **

    # paths - list of image file paths as strings
    # labels - list or numpy array of labels as any data type
    # resize_dims - integer tuple of dimensions to resize the images to
    # warp_ok - optional boolean denoting if risizing should maintain aspect ratio

    images = []
    for i, path in enumerate(paths):
        try:
            if resize_dims and not warp_ok:
                img = imanip.resize(path, maxsizes=resize_dims)
            else:
                img = mpimg.imread(path)
                if resize_dims:
                    img = sci.imresize(img, resize_dims)

            img = imanip.random_augment(img)

        except OSError:
            # Uses augmented version of next image in list
            if i == 0:
                if resize_dims and not warp_ok:
                    img = imanip.resize(paths[i + 1], maxsizes=resize_dims)
                else:
                    img = mpimg.imread(paths[i + 1])
                    if resize_dims:
                        img = sci.imresize(img, resize_dims)
                img = imanip.random_augment(img)
                labels[i] = labels[i + 1]

            # Uses most recent original image
            elif i > 0:
                img = imanip.random_augment(images[-1])
                labels[i] = labels[i - 1]

        images.append(img)

    return images, labels
Beispiel #2
0
def convert_images(paths,
                   labels,
                   resize_dims=None,
                   warp_ok=False,
                   rpaths=[],
                   rlabels=[]):
    # ** Reads in images from their paths, returns the images with their
    #   corresponding labels. **

    # paths - list of file paths to the images as strings
    # labels - a numpy array or list of the corresponding labels to the images
    # resize_dims - tuple of integer output dimensions to resize image.
    #               (does not maintain aspect ratio)
    # warp_ok - boolean to select if image resize should maintain the aspect ratio
    # randomly_augment - optional boolean to add randomly rotated,
    #                    translated, and scaled images to the output
    # rpaths - list of image file paths as strings to be randomly augmented
    # rlabels - list of corresponding labels to rpaths

    if len(rpaths) > 0 and len(rlabels) > 0:
        # pool = Pool(processes=1)
        # result = pool.apply_async(convert_randoms, (rpaths,rlabels,resize_dims))
        rand_imgs, rand_labels = convert_randoms(rpaths, rlabels, resize_dims)

    images = []
    for i, path in enumerate(paths):
        try:
            if resize_dims and not warp_ok:
                img = imanip.resize(path, maxsizes=resize_dims)
            else:
                img = mpimg.imread(path)
                if resize_dims:
                    img = sci.imresize(img, resize_dims)

        except OSError:
            # Uses augmented version of next image in list
            if i == 0:
                if resize_dims and not warp_ok:
                    img = imanip.resize(paths[i + 1], maxsizes=resize_dims)
                else:
                    img = mpimg.imread(paths[i + 1])
                    if resize_dims:
                        img = sci.imresize(img, resize_dims)
                img = imanip.random_augment(img)
                labels[i] = labels[i + 1]

            # Uses most recent original image
            elif i > 0:
                sub_index = -1
                if randomly_augment:
                    sub_index = -2
                img = imanip.random_augment(images[sub_index])
                labels[i] = labels[i - sub_index]

        images.append(img)

    if len(rpaths) > 0 and len(rlabels) > 0:
        # result.wait()
        # rand_imgs, rand_labels = result.get()
        images = images + rand_imgs
        labels = np.concatenate([labels, rand_labels], axis=0)
        return np.array(images, dtype=np.float32), labels
    return np.array(images, dtype=np.float32), labels
Beispiel #3
0
## Resizing the image
new_img_shapes = [(299,299,3),(256,256,3)]
dest_folders = ['resized']
for new_img_shape,dest_folder in zip(new_img_shapes,dest_folders):
    for i,path,label in zip(count(),image_paths,labels):
        split_path = path.split('/')
        new_path = 'size_'+str(new_img_shape[0])+'_'+split_path[-1]
        new_path = '/'.join(['data/resized']+[str(label)]+[new_path])
        add_flip = True
        if label == 1:
            add_flip = False

        # Used to exclude corrupt data
        try:
            imanip.resize(path, maxsizes=new_img_shape,
                                save_path=new_path,
                                add_flip=add_flip)
        except OSError:
            print("Error at path " + path)

## Using resized image
resized_image_locations = ['data/resized/']

image_paths = []
labels = []
for i,root_path in enumerate(resized_image_locations):
    new_paths, new_labels, n_classes = read_paths(root_path)
    if len(new_paths) > 0:
        image_paths += new_paths
        labels += new_labels