Ejemplo n.º 1
0
def write_activations(model_file, x, write_dir):

    if not os.path.exists(write_dir):
        os.mkdir(write_dir)
    xn = normalize(x, np.max(x), np.max(x) - np.min(x))
    tiff.imsave(os.path.join(write_dir, 'input.tif'),
                (xn * 255).astype('uint8'))

    # transform data to cuda tensor
    x = torch.FloatTensor(x[np.newaxis, np.newaxis, ...]).cuda()

    # load network
    net = load_net(model_file=model_file)
    net.eval()
    net.cuda()

    # apply forward prop and extract network activations
    encoder_outputs, encoded_outputs = net.encoder(x)
    decoder_outputs, final_outputs = net.decoder(encoded_outputs,
                                                 encoder_outputs)

    # write random activations
    for i, encoder_output in enumerate(encoder_outputs):
        c = np.random.randint(encoder_output.size(1))
        act = encoder_output[0, c, :, :].data.cpu().numpy()
        act = normalize(act, np.max(act), np.max(act) - np.min(act))
        tiff.imsave(
            os.path.join(
                write_dir,
                'enc_act_' + str(len(encoder_outputs) - i - 1) + '.tif'),
            (act * 255).astype('uint8'))

    c = np.random.randint(encoded_outputs.size(1))
    act = encoded_outputs[0, c, :, :].data.cpu().numpy()
    act = normalize(act, np.max(act), np.max(act) - np.min(act))
    tiff.imsave(
        os.path.join(write_dir,
                     'enc_act_' + str(len(encoder_outputs)) + '.tif'),
        (act * 255).astype('uint8'))

    for i, decoder_output in enumerate(decoder_outputs):
        c = np.random.randint(decoder_output.size(1))
        act = decoder_output[0, c, :, :].data.cpu().numpy()
        act = normalize(act, np.max(act), np.max(act) - np.min(act))
        tiff.imsave(
            os.path.join(
                write_dir,
                'dec_act_' + str(len(decoder_outputs) - i - 1) + '.tif'),
            (act * 255).astype('uint8'))
Ejemplo n.º 2
0
    def __init__(self, data_path, input_shapes, len_epoch=1000, preprocess='z', transform=None, dtype='uint8'):

        self.data_path = data_path
        self.input_shapes = input_shapes
        self.len_epoch = len_epoch
        self.transform = transform

        self.data = read_tif(data_path, dtype=dtype)

        mu, std = self.get_stats()
        self.mu = mu
        self.std = std
        self.preprocess = preprocess
        if preprocess == 'z':
            self.data = normalize(self.data, mu, std)
        elif preprocess == 'unit':
            self.data = normalize(self.data, 0, 255)
Ejemplo n.º 3
0
    def __init__(self,
                 data_path,
                 label_path,
                 input_shape,
                 mode='image-level',
                 split=None,
                 train=None,
                 len_epoch=1000,
                 preprocess='z',
                 transform=None,
                 target_transform=None,
                 dtypes=('uint8', 'uint8')):

        self.data_path = data_path
        self.label_path = label_path
        self.input_shape = input_shape
        self.mode = mode
        self.len_epoch = len_epoch
        self.transform = transform
        self.target_transform = target_transform

        self.data = read_tif(data_path, dtype=dtypes[0])
        self.labels = read_tif(label_path, dtype=dtypes[1])

        mu, std = self.get_stats()
        self.mu = mu
        self.std = std
        self.preprocess = preprocess
        if preprocess == 'z':
            self.data = normalize(self.data, mu, std)
        elif preprocess == 'unit':
            self.data = normalize(self.data, 0, 255)
        self.labels = normalize(self.labels, 0, 255)

        if split is not None:
            if train:
                s = int(split * self.data.shape[2])
                self.data = self.data[:, :, :s]
                self.labels = self.labels[:, :, :s]
            else:
                s = int(split * self.data.shape[2])
                self.data = self.data[:, :, s:]
                self.labels = self.labels[:, :, s:]
Ejemplo n.º 4
0
    def __init__(self, data_path, label_path, input_shape, len_epoch=1000, preprocess='z', transform=None, target_transform=None, dtypes=('uint8','uint8')):

        self.data_path = data_path
        self.label_path = label_path
        self.input_shape = input_shape
        self.len_epoch = len_epoch
        self.transform = transform
        self.target_transform = target_transform

        self.data = read_tif(data_path, dtype=dtypes[0])
        self.labels = read_tif(label_path, dtype=dtypes[1])

        mu, std = self.get_stats()
        self.mu = mu
        self.std = std
        self.preprocess = preprocess
        if preprocess == 'z':
            self.data = normalize(self.data, mu, std)
        elif preprocess == 'unit':
            self.data = normalize(self.data, 0, 255)
        self.labels = normalize(self.labels, 0, 255)
args.input_size = [int(item) for item in args.input_size.split(',')]
"""
    Setup writing directory
"""
print('[%s] Setting up write directories' % (datetime.datetime.now()))
if args.write_dir is not None:
    if not os.path.exists(args.write_dir):
        os.mkdir(args.write_dir)
"""
    Load and normalize the data
"""
print('[%s] Loading and normalizing the data' % (datetime.datetime.now()))
test_data = read_tif(args.data, dtype='uint8')
mu = np.mean(test_data)
std = np.std(test_data)
test_data = normalize(test_data, mu, std)
if len(test_data.shape) < 3:
    test_data = test_data[np.newaxis, ...]
"""
    Load the network
"""
print('[%s] Loading network' % (datetime.datetime.now()))
net = load_net(args.net)
"""
    Segmentation
"""
print('[%s] Starting segmentation' % (datetime.datetime.now()))
segmentation = segment_pixels(test_data,
                              net,
                              args.input_size,
                              batch_size=args.batch_size,