Esempio n. 1
0
def vis_square(data, name_fig):
    """Take an array of shape (n, height, width) or (n, height, width, 3)
       and visualize each (height, width) thing in a grid of size approx. sqrt(n) by sqrt(n)"""

    # normalize data for display
    data = (data - data.min()) / (data.max() - data.min())

    # force the number of filters to be square
    n = int(np.ceil(np.sqrt(data.shape[0])))
    padding = (((0, n**2 - data.shape[0]), (0, 1),
                (0, 1))  # add some space between filters
               + ((0, 0), ) * (data.ndim - 3)
               )  # don't pad the last dimension (if there is one)
    data = np.pad(data, padding, mode='constant',
                  constant_values=1)  # pad with ones (white)

    # tile the filters into an image
    data = data.reshape((n, n) + data.shape[1:]).transpose(
        (0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
    data = data.reshape((n * data.shape[1], n * data.shape[3]) +
                        data.shape[4:])

    plt.imshow(data)
    plt.show()
    plt.imsave(name_fig, data)
    return data
Esempio n. 2
0
def crop_images(input_folder, crop_percentage):
    im_files = glob.glob(os.path.join(input_folder, '*.png'))
    print(im_files)
    for imf in im_files:
        im = plt.imread(imf)
        orig_shape = np.array(im.shape[0:2])
        new_shape = np.round(orig_shape * crop_percentage).astype(
            'int')  #crop to 60% of the image size
        offset = np.round(0.5 * (orig_shape - new_shape)).astype('int')
        im_out = im[offset[0]:offset[0] + new_shape[0],
                    offset[1]:offset[1] + new_shape[1]]
        plt.imsave(imf, im_out)
def download_tiles_for_area(x_tile_min, x_tile_max,
                            y_tile_min, y_tile_max,
                            zoom, url='https://maps.wikimedia.org/osm-intl'):
    """
    Download the tiles specified by the x, y and zoom values
    The tileservers are accessed using the pattern {url}/{zoom}/{x_tile}/{y_tile}.png and the tiles saved as
    {zoom}_{x_tile}_{y_tile}.png
    :param x_tile_min: Minimum x_tile value
    :param x_tile_max: Maximum x_tile value
    :param y_tile_min: Minimum y_tile value
    :param y_tile_max: Maximum y_tile value
    :param zoom: Zoom at which to download the tiles
    :param url: The url used to download the tiles.
    :return: No return value
    """
    # total number of tiles
    tile_count = (x_tile_max-x_tile_min+1)*(y_tile_max-y_tile_min+1)
    # Restrict the number of tiles that are downloaded
    if tile_count > MAX_TILE_COUNT:
        print('ERROR zoom value too high')
        return
    # download tiles
    if not os.path.exists('tiles'):
        os.makedirs('tiles')
    i = 0
    with progressbar.ProgressBar(max_value=tile_count) as bar:
        for x in range(x_tile_min, x_tile_max+1):
            for y in range(y_tile_min, y_tile_max+1):
                tile_url = '/'.join([url, str(zoom), str(x), str(y) + '.png'])
                tile_file = '_'.join(['tiles/tile', str(zoom), str(x), str(y) + '.png'])
                # check if tile already downloaded
                if not glob.glob(tile_file):
                    i += 1
                    bar.update(i)
                    if not download_tile_file(tile_url, tile_file):
                        tile_image = np.ones((OSM_TILE_SIZE, OSM_TILE_SIZE, 3))
                        plt.imsave(tile_file, tile_image)
Esempio n. 4
0
def vis_square_seg(data, name_fig, index_lum_ker):
    # Function that displays kernels of the first layer as images. kernels classified as color agnostic are surrounded by black.
    # Take an array of shape (n, height, width) or (n, height, width, 3)
    # and visualize each (height, width) thing in a grid of size approx. sqrt(n) by sqrt(n)"""
    # normalize data for display
    data = (data - data.min()) / (data.max() - data.min())
    gp_top = data[0:48]
    gp_bot = data[48:]
    # force the number of filters to be square
    n = int(np.ceil(np.sqrt(data.shape[0])))
    padding = (((0, 0), (1, 2), (1, 2)) + ((0, 0), ) * (data.ndim - 3))
    gp_top = np.pad(gp_top, padding, mode='constant',
                    constant_values=1)  # pad with ones (white)
    gp_bot = np.pad(gp_bot, padding, mode='constant',
                    constant_values=1)  # pad with ones (white)
    data = np.concatenate((gp_top, gp_bot), axis=0)
    for i in index_lum_ker:
        if i < 48:
            data[i, :-1, (0, -2), :] = 0
            data[i, (0, -2), :-1, :] = 0
        else:
            data[i, :-1, (0, -2), :] = 0
            data[i, (0, -2), :-1, :] = 0
    # tile the filters into an image
    data = data.reshape((12, 8) + data.shape[1:]).transpose(
        (0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
    data = data.reshape((12 * data.shape[1], 8 * data.shape[3]) +
                        data.shape[4:])
    data = np.insert(data, data.shape[0] / 2, np.ones((3, data.shape[1], 3)),
                     0)
    data = data[:-1, :-1]
    # print data.shape
    plt.imshow(data)
    plt.show()
    plt.imsave(name_fig + '.png', data)
    plt.imsave(name_fig + '.eps', data)
    return data
def main():

    #create pcd object list to save the reconstructed patch per capsule
    pcd_list = []
    for i in range(opt.latent_caps_size):
        pcd_ = o3d.geometry.PointCloud()
        pcd_list.append(pcd_)
    colors = plt.cm.tab20((np.arange(20)).astype(int))
    #random selected viz capsules
    hight_light_caps = [
        np.random.randint(0, opt.latent_caps_size) for r in range(10)
    ]

    USE_CUDA = True
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    capsule_net = PointCapsNet(opt.prim_caps_size, opt.prim_vec_size,
                               opt.latent_caps_size, opt.latent_caps_size,
                               opt.num_points)

    if opt.model != '':
        capsule_net.load_state_dict(torch.load(opt.model))
    else:
        print('pls set the model path')

    if USE_CUDA:
        print("Let's use", torch.cuda.device_count(), "GPUs!")
        capsule_net = torch.nn.DataParallel(capsule_net)
        capsule_net.to(device)

    if opt.dataset == 'shapenet_part':
        test_dataset = shapenet_part_loader.PartDataset(classification=True,
                                                        npoints=opt.num_points,
                                                        split='test')
        test_dataloader = torch.utils.data.DataLoader(
            test_dataset,
            batch_size=opt.batch_size,
            shuffle=True,
            num_workers=4)
    elif opt.dataset == 'shapenet_core13':
        test_dataset = shapenet_core13_loader.ShapeNet(normal=False,
                                                       npoints=opt.num_points,
                                                       train=False)
        test_dataloader = torch.utils.data.DataLoader(
            test_dataset,
            batch_size=opt.batch_size,
            shuffle=True,
            num_workers=4)
    elif opt.dataset == 'shapenet_core55':
        test_dataset = shapenet_core55_loader.Shapnet55Dataset(
            batch_size=opt.batch_size,
            npoints=opt.num_points,
            shuffle=True,
            train=False)

    capsule_net.eval()
    if 'test_dataloader' in locals().keys():
        test_loss_sum = 0
        for batch_id, data in enumerate(test_dataloader):
            points, _ = data
            if (points.size(0) < opt.batch_size):
                break
            points = Variable(points)
            points = points.transpose(2, 1)
            if USE_CUDA:
                points = points.cuda()
            latent_caps, reconstructions = capsule_net(points)

            for pointset_id in range(opt.batch_size):
                prc_r_all = reconstructions[pointset_id].transpose(
                    1, 0).contiguous().data.cpu()
                prc_r_all_point = o3d.geometry.PointCloud()
                prc_r_all_point.points = o3d.utility.Vector3dVector(prc_r_all)
                colored_re_pointcloud = o3d.geometry.PointCloud()
                jc = 0
                for j in range(opt.latent_caps_size):
                    current_patch = torch.zeros(
                        int(opt.num_points / opt.latent_caps_size), 3)
                    for m in range(int(opt.num_points / opt.latent_caps_size)):
                        current_patch[m, ] = prc_r_all[
                            opt.latent_caps_size * m + j,
                        ]  # the reconstructed patch of the capsule m is not saved continuesly in the output reconstruction.
                    pcd_list[j].points = o3d.utility.Vector3dVector(
                        current_patch)
                    if (j in hight_light_caps):
                        pcd_list[j].paint_uniform_color(
                            [colors[jc, 0], colors[jc, 1], colors[jc, 2]])
                        jc += 1
                    else:
                        pcd_list[j].paint_uniform_color([0.8, 0.8, 0.8])
                    colored_re_pointcloud += pcd_list[j]
                #o3d.visualization.draw_geometries([colored_re_pointcloud])
                o3d.io.write_point_cloud(
                    f'../../output/{batch_id}_{pointset_id}.pcd',
                    colored_re_pointcloud)
                vis = o3d.visualization.Visualizer()
                vis.create_window(visible=False)
                vis.add_geometry(colored_re_pointcloud)
                img = vis.capture_screen_float_buffer(True)
                plt.imsave(f'../../output/{batch_id}_{pointset_id}.png',
                           np.asarray(img))


# test process for 'shapenet_core55'
    else:
        test_loss_sum = 0
        while test_dataset.has_next_batch():
            batch_id, points_ = test_dataset.next_batch()
            points = torch.from_numpy(points_)
            if (points.size(0) < opt.batch_size):
                break
            points = Variable(points)
            points = points.transpose(2, 1)
            if USE_CUDA:
                points = points.cuda()
            latent_caps, reconstructions = capsule_net(points)
            for pointset_id in range(opt.batch_size):
                prc_r_all = reconstructions[pointset_id].transpose(
                    1, 0).contiguous().data.cpu()
                prc_r_all_point = o3d.geometry.PointCloud()
                prc_r_all_point.points = o3d.utility.Vector3dVector(prc_r_all)
                colored_re_pointcloud = o3d.geometry.PointCloud()
                jc = 0
                for j in range(opt.latent_caps_size):
                    current_patch = torch.zeros(
                        int(opt.num_points / opt.latent_caps_size), 3)
                    for m in range(int(opt.num_points / opt.latent_caps_size)):
                        current_patch[m, ] = prc_r_all[
                            opt.latent_caps_size * m + j,
                        ]  # the reconstructed patch of the capsule m is not saved continuesly in the output reconstruction.
                    pcd_list[j].points = o3d.utility.Vector3dVector(
                        current_patch)
                    if (j in hight_light_caps):
                        pcd_list[j].paint_uniform_color(
                            [colors[jc, 0], colors[jc, 1], colors[jc, 2]])
                        jc += 1
                    else:
                        pcd_list[j].paint_uniform_color([0.8, 0.8, 0.8])
                    colored_re_pointcloud += pcd_list[j]

                #o3d.visualization.draw_geometries([colored_re_pointcloud])
                o3d.io.write_point_cloud(
                    f'../../output/{batch_id}_{pointset_id}.pcd',
                    colored_re_pointcloud)
                vis = o3d.visualization.Visualizer()
                vis.create_window(visible=False)
                vis.add_geometry(colored_re_pointcloud)
                img = vis.capture_screen_float_buffer(True)
                plt.imsave(f'../../output/{batch_id}_{pointset_id}.png',
                           np.asarray(img))
Esempio n. 6
0
import os
import PIL
from tensorflow.keras import layers
import time

import tensorflow as tf


def load_images_from_folder(folder):
    images = []
    for filename in os.listdir(folder):
        bgr_img = cv2.imread(os.path.join(folder, filename))
        b, g, r = cv2.split(bgr_img)
        rgb_img = cv2.merge([r, g, b])
        if rgb_img is not None:
            images.append(rgb_img)
    return images


images = load_images_from_folder(
    r'C:\Users\Kevork\Documents\Histology-GAM\BACH_images')

# %%
# images=np.array(images)
image1 = images[0]

plt.imshow(image1)
plt.imsave('hist.tiff', image1)

# %%
os.mkdir('dataset/train')
os.mkdir('dataset/test')

# 10=bowl 29=dinosaur 50=mouse 63=porcupine 90=train 91=trout
classNumber = [10, 29, 50, 63, 90, 91]

for num in classNumber:
    path = os.path.join('dataset/train', str(num))
    os.mkdir(path)
    path = os.path.join('dataset/test', str(num))
    os.mkdir(path)

for i in range(50000):
    if int(y_train[i]) in classNumber:
        path = 'dataset/train/' + str(int(y_train[i])) + '/' + str(i) + '.png'
        plt.imsave(path, x_train[i])

for i in range(10000):
    if int(y_test[i]) in classNumber:
        path = 'dataset/test/' + str(int(y_test[i])) + '/' + str(i) + '.png'
        plt.imsave(path, x_test[i])

train_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = ImageDataGenerator().flow_from_directory(
    'dataset/train/',
    target_size=(32, 32),
    batch_size=20,
    class_mode='categorical')

test_datagen = ImageDataGenerator(rescale=1. / 255)
test_generator = ImageDataGenerator().flow_from_directory(