Ejemplo n.º 1
0
    def test_img_utils(self):
        height, width = 10, 8

        # Test th data format
        x = np.random.random((3, height, width))
        img = image.array_to_img(x, data_format='channels_first')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_first')
        assert x.shape == (3, height, width)
        # Test 2D
        x = np.random.random((1, height, width))
        img = image.array_to_img(x, data_format='channels_first')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_first')
        assert x.shape == (1, height, width)

        # Test tf data format
        x = np.random.random((height, width, 3))
        img = image.array_to_img(x, data_format='channels_last')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_last')
        assert x.shape == (height, width, 3)
        # Test 2D
        x = np.random.random((height, width, 1))
        img = image.array_to_img(x, data_format='channels_last')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_last')
        assert x.shape == (height, width, 1)
Ejemplo n.º 2
0
    def test_img_utils(self):
        height, width = 10, 8

        # Test th dim ordering
        x = np.random.random((3, height, width))
        img = image.array_to_img(x, dim_ordering='th')
        assert img.size == (width, height)
        x = image.img_to_array(img, dim_ordering='th')
        assert x.shape == (3, height, width)
        # Test 2D
        x = np.random.random((1, height, width))
        img = image.array_to_img(x, dim_ordering='th')
        assert img.size == (width, height)
        x = image.img_to_array(img, dim_ordering='th')
        assert x.shape == (1, height, width)

        # Test tf dim ordering
        x = np.random.random((height, width, 3))
        img = image.array_to_img(x, dim_ordering='tf')
        assert img.size == (width, height)
        x = image.img_to_array(img, dim_ordering='tf')
        assert x.shape == (height, width, 3)
        # Test 2D
        x = np.random.random((height, width, 1))
        img = image.array_to_img(x, dim_ordering='tf')
        assert img.size == (width, height)
        x = image.img_to_array(img, dim_ordering='tf')
        assert x.shape == (height, width, 1)
Ejemplo n.º 3
0
    def save(self, x, y, index):
        """Image save method."""
        img = array_to_img(x, self.data_format, scale=True)
        mask = array_to_img(y, self.data_format, scale=True)
        img.paste(mask, (0, 0), mask)

        fname = 'img_{prefix}_{index}_{hash}.{format}'.format(
            prefix=self.save_prefix,
            index=index,
            hash=np.random.randint(1e4),
            format=self.save_format)
        img.save(os.path.join(self.save_to_dir, fname))
Ejemplo n.º 4
0
    def make_image_from_dense_softmax(self, neurons):
        from aetros.utils import array_to_img

        img = array_to_img(neurons.reshape((1, len(neurons), 1)))
        img = img.resize((9, len(neurons) * 8))

        return img
Ejemplo n.º 5
0
	def augmentation(self):
		# 读入3通道的train和label, 分别转换成矩阵, 然后将label的第一个通道放在train的第2个通处, 做数据增强
		print("运行 Augmentation")

		# Start augmentation.....
		trains = self.train_imgs
		labels = self.label_imgs
		path_train = self.train_path
		path_label = self.label_path
		path_merge = self.merge_path
		imgtype = self.img_type
		path_aug_merge = self.aug_merge_path
		print(len(trains), len(labels))
		if len(trains) != len(labels) or len(trains) == 0 or len(trains) == 0:
			print("trains can't match labels")
			return 0
		for i in range(len(trains)):
			img_t = load_img(path_train + "/" + str(i) + "." + imgtype)  # 读入train
			img_l = load_img(path_label + "/" + str(i) + "." + imgtype)  # 读入label
			x_t = img_to_array(img_t)                                    # 转换成矩阵
			x_l = img_to_array(img_l)
			x_t[:, :, 2] = x_l[:, :, 0]                                  # 把label当做train的第三个通道
			img_tmp = array_to_img(x_t)
			img_tmp.save(path_merge + "/" + str(i) + "." + imgtype)      # 保存合并后的图像
			img = x_t
			img = img.reshape((1,) + img.shape)                          # 改变shape(1, 512, 512, 3)
			savedir = path_aug_merge + "/" + str(i)                      # 存储合并增强后的图像
			if not os.path.lexists(savedir):
				os.mkdir(savedir)
			self.do_augmentate(img, savedir, str(i))                      # 数据增强
Ejemplo n.º 6
0
    def save_sample_grid(self, samples, filename=None):
        from PIL import Image

        if K.image_dim_ordering() == 'tf':
            num_samples, img_height, img_width, img_channels = samples.shape
        else:
            num_samples, img_channels, img_height, img_width = samples.shape
        num_wide = int(np.sqrt(num_samples))
        num_heigh = int(np.ceil(num_samples / num_wide))
        width = num_wide * img_width
        height = num_heigh * img_height
        img_mode = {1: 'L', 3: 'RGB'}[img_channels]
        output_img = Image.new(img_mode, (width, height), 'black')
        for i in range(num_samples):
            x = (i % num_wide) * img_width
            y = (i / num_wide) * img_height
            sample_arr = samples[i]
            #sample_arr = deprocess_input(samples[i])
            sample_arr = (samples[i] + 1.) * 128.
            #print sample_arr.min(), sample_arr.mean(), sample_arr.max()

            sample_img = array_to_img(sample_arr)
            output_img.paste(sample_img, (x, y))
        if filename is None:
            filename = self.config.sample_output_filename
        output_img.save(filename)
Ejemplo n.º 7
0
	def Augmentation(self):

		"""
		Start augmentation.....
		"""
		trains = self.train_imgs
		labels = self.label_imgs
		path_train = self.train_path
		path_label = self.label_path
		path_merge = self.merge_path
		imgtype = self.img_type
		path_aug_merge = self.aug_merge_path
		if len(trains) != len(labels) or len(trains) == 0 or len(trains) == 0:
			print "trains can't match labels"
			return 0
		for i in range(len(trains)):
			img_t = load_img(path_train+"/"+str(i)+"."+imgtype)
			img_l = load_img(path_label+"/"+str(i)+"."+imgtype)
			x_t = img_to_array(img_t)
			x_l = img_to_array(img_l)
			x_t[:,:,2] = x_l[:,:,0]
			img_tmp = array_to_img(x_t)
			img_tmp.save(path_merge+"/"+str(i)+"."+imgtype)
			img = x_t
			img = img.reshape((1,) + img.shape)
			savedir = path_aug_merge + "/" + str(i)
			if not os.path.lexists(savedir):
				os.mkdir(savedir)
			self.doAugmentate(img, savedir, str(i))
Ejemplo n.º 8
0
def save_sample_grid(samples, filename, activation='tanh'):
    if K.image_dim_ordering() == 'tf':
        num_samples, img_height, img_width, img_channels = samples.shape
    else:
        num_samples, img_channels, img_height, img_width = samples.shape
    num_wide = int(np.sqrt(num_samples))
    num_heigh = int(np.ceil(num_samples / num_wide))
    width = num_wide * img_width
    height = num_heigh * img_height
    img_mode = {1: 'L', 3: 'RGB'}[img_channels]
    output_img = Image.new(img_mode, (width, height), 'black')
    for i in range(num_samples):
        x = (i % num_wide) * img_width
        y = (i / num_wide) * img_height
        if activation == 'relu':
            sample_arr = samples[i]
        elif activation == 'tanh':
            sample_arr = (samples[i] + 1.) * 127.5
        elif activation == 'sigmoid':
            sample_arr = samples[i] * 256.
        sample_img = array_to_img(sample_arr, scale=False)
        output_img.paste(sample_img, (x, y))
    if filename is None:
        filename = self.config.sample_output_filename
    output_img.save(filename)
Ejemplo n.º 9
0
    def test_load_img(self, tmpdir):
        filename = str(tmpdir / 'image.png')

        original_im_array = np.array(255 * np.random.rand(100, 100, 3),
                                     dtype=np.uint8)
        original_im = image.array_to_img(original_im_array, scale=False)
        original_im.save(filename)

        # Test that loaded image is exactly equal to original.

        loaded_im = image.load_img(filename)
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == original_im_array.shape
        assert np.all(loaded_im_array == original_im_array)

        loaded_im = image.load_img(filename, grayscale=True)
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (original_im_array.shape[0],
                                         original_im_array.shape[1], 1)

        # Test that nothing is changed when target size is equal to original.

        loaded_im = image.load_img(filename, target_size=(100, 100))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == original_im_array.shape
        assert np.all(loaded_im_array == original_im_array)

        loaded_im = image.load_img(filename, grayscale=True,
                                   target_size=(100, 100))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (original_im_array.shape[0],
                                         original_im_array.shape[1], 1)

        # Test down-sampling with bilinear interpolation.

        loaded_im = image.load_img(filename, target_size=(25, 25))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (25, 25, 3)

        loaded_im = image.load_img(filename, grayscale=True,
                                   target_size=(25, 25))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (25, 25, 1)

        # Test down-sampling with nearest neighbor interpolation.

        loaded_im_nearest = image.load_img(filename, target_size=(25, 25),
                                           interpolation="nearest")
        loaded_im_array_nearest = image.img_to_array(loaded_im_nearest)
        assert loaded_im_array_nearest.shape == (25, 25, 3)
        assert np.any(loaded_im_array_nearest != loaded_im_array)

        # Check that exception is raised if interpolation not supported.

        loaded_im = image.load_img(filename, interpolation="unsupported")
        with pytest.raises(ValueError):
            loaded_im = image.load_img(filename, target_size=(25, 25),
                                       interpolation="unsupported")
Ejemplo n.º 10
0
def test_pair_crop(crop_function):
    arr1 = np.random.random(500, 800)
    arr2 = np.random.random(500, 800)

    img1 = PILImage.fromarray(arr1)
    img2 = PILImage.fromarray(arr2)

    crop_width = img1.width / 5
    crop_height = img1.height / 5

    result1, result2 = crop_function(img_to_array(img1),
        img_to_array(img2),
        (crop_height, crop_width),
        'channels_last')
    result1 = array_to_img(result1)
    result2 = array_to_img(result2)

    assert result1.width == crop_width == result2.width
    assert result2.height == crop_height == result2.height
Ejemplo n.º 11
0
    def make_image(self, data):
        from keras.preprocessing.image import array_to_img
        try:
            image = array_to_img(data)
        except:
            return None

        # image = image.resize((128, 128))

        return image
Ejemplo n.º 12
0
 def get_scaled_images(self, images):
     results = []
     scaled_height, scaled_width = self.scaled_frame_size
     height, width = self.frame_size
     for image in images:
         img = array_to_img((image + 1.) * 127.5, scale=False)
         img = img.resize((scaled_width, scaled_height))
         img = img.resize((width, height)) # scale back up
         arr = img_to_array(img)
         arr = (arr / 127.5) - 1.
         results.append(arr)
     return np.stack(results)
Ejemplo n.º 13
0
def test_crop(crop_function):
    arr = np.random.random(500, 800)

    img = PILImage.fromarray(arr)

    crop_width = img.width / 5
    crop_height = img.height / 5

    result = crop_function(img_to_array(img), (crop_height, crop_width), 'channels_last')
    result = array_to_img(result)

    assert result.width == crop_width
    assert result.height == crop_height
Ejemplo n.º 14
0
    def make_image_from_dense(self, neurons):
        from aetros.utils import array_to_img
        cols = int(math.ceil(math.sqrt(len(neurons))))

        even_length = cols * cols
        diff = even_length - len(neurons)
        if diff > 0:
            neurons = np.append(neurons, np.zeros(diff, dtype=neurons.dtype))

        img = array_to_img(neurons.reshape((1, cols, cols)))
        img = img.resize((cols * 8, cols * 8))

        return img
Ejemplo n.º 15
0
    def make_image(self, data):
        from keras.preprocessing.image import array_to_img
        try:
            if len(data.shape) == 2:
                # grayscale image, just add once channel
                data = data.reshape((data.shape[0], data.shape[1], 1))

            image = array_to_img(data)
        except Exception:
            return None

        # image = image.resize((128, 128))

        return image
def main():
	# out dims
	out_H = 400
	out_W = 400
	out_dims = (out_H, out_W)

	# load 4 cat images
	img1 = load_data(DIMS, CAT1, view=True)
	img2 = load_data(DIMS, CAT2)
	img3 = load_data(DIMS, CAT3)
	img4 = load_data(DIMS, CAT4)

	# concat into tensor of shape (2, 600, 600, 3)
	input_img = np.concatenate([img1, img2, img3, img4], axis=0)

	# dimension sanity check
	print("Input Img Shape: {}".format(input_img.shape))

	# grab shape
	B, H, W, C = input_img.shape

	# placeholders
	x = tf.placeholder(tf.float32, [None, H, W, C])

	# Create localisation network and convolutional layer
	with tf.variable_scope('spatial_transformer_0'):

		# Create a fully-connected layer with 6 output nodes
		n_fc = 6
		W_fc1 = tf.Variable(tf.zeros([H*W*C, n_fc]), name='W_fc1')

		# identity transform
		theta = np.array([[1., 0, 0], [0, 1., 0]])
		theta = theta.astype('float32')
		theta = theta.flatten()

		b_fc1 = tf.Variable(initial_value=theta, name='b_fc1')
		h_fc1 = tf.matmul(tf.zeros([B, H*W*C]), W_fc1) + b_fc1
		h_trans = spatial_transformer_network(x, h_fc1, out_dims)

	# run session
	sess = tf.Session()
	sess.run(tf.global_variables_initializer())
	y = sess.run(h_trans, feed_dict={x: input_img})
	print(y.shape)
	y = np.reshape(y, (B, out_H, out_W, C))
	y = array_to_img(y[0])
	y.show()
Ejemplo n.º 17
0
def draw_rectangle(boxes_pos, boxes_prob, __image, file_name, fp_name):
    from PIL import ImageDraw
    from keras.preprocessing import image
    import os
    img = image.array_to_img(__image, scale=False)
    draw = ImageDraw.Draw(img)

    for idx, box_pos in enumerate(boxes_pos):
        if boxes_prob[idx] > 0.99:
            pos_tuple = [(box_pos[0], box_pos[1]), (box_pos[2], box_pos[3])]
            draw.rectangle(pos_tuple, fill=None, outline='white')
    del draw
    detected_path = os.path.join(os.getcwd(), "detected_image")
    if not os.path.exists(detected_path):
        os.makedirs(detected_path)
    img.save(os.path.join(detected_path, "detected_image" + str(fp_name)+str(file_name) + ".png"), "PNG")
Ejemplo n.º 18
0
 def update_images(self):
     if not self.generator:
         return
     try:
         r = float(self.radius_entry.get())
     except ValueError:
         r = 0.01
     if r != self.last_radius:
         self.randomize_image_offsets(r)
     offset_positions = self.image_offsets + self.view_position
     samples = self.generator.predict(offset_positions, verbose=False)
     for label, sample in zip(self.images, samples):
         sample = (sample + 1.) * 127.5
         tkimage = ImageTk.PhotoImage(image=array_to_img(sample, scale=False))
         label.configure(image=tkimage)
         label.img = tkimage
     self.frame.update()
Ejemplo n.º 19
0
    def test_img_utils(self):
        height, width = 10, 8

        # Test th data format
        x = np.random.random((3, height, width))
        img = image.array_to_img(x, data_format='channels_first')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_first')
        assert x.shape == (3, height, width)
        # Test 2D
        x = np.random.random((1, height, width))
        img = image.array_to_img(x, data_format='channels_first')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_first')
        assert x.shape == (1, height, width)

        # Test tf data format
        x = np.random.random((height, width, 3))
        img = image.array_to_img(x, data_format='channels_last')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_last')
        assert x.shape == (height, width, 3)
        # Test 2D
        x = np.random.random((height, width, 1))
        img = image.array_to_img(x, data_format='channels_last')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_last')
        assert x.shape == (height, width, 1)

        # Test invalid use case
        with pytest.raises(ValueError):
            x = np.random.random((height, width))  # not 3D
            img = image.array_to_img(x, data_format='channels_first')
        with pytest.raises(ValueError):
            x = np.random.random((height, width, 3))
            img = image.array_to_img(x, data_format='channels')  # unknown data_format
        with pytest.raises(ValueError):
            x = np.random.random((height, width, 5))  # neither RGB nor gray-scale
            img = image.array_to_img(x, data_format='channels_last')
        with pytest.raises(ValueError):
            x = np.random.random((height, width, 3))
            img = image.img_to_array(x, data_format='channels')  # unknown data_format
        with pytest.raises(ValueError):
            x = np.random.random((height, width, 5, 3))  # neither RGB nor gray-scale
            img = image.img_to_array(x, data_format='channels_last')
Ejemplo n.º 20
0
def pyramid(__image, downscale, min_height, min_width):
    from PIL import Image
    from keras.preprocessing import image

    img = image.array_to_img(__image, scale=False)
    pyramid_list = []
    scale_list = []
    scale_factor = float(1)
    width_original = img.size[0]
    height_original = img.size[1]
    while True:
        width_new = int(float(width_original) / scale_factor)
        wpercent = float(width_new) / float(width_original)
        height_new = int((float(height_original) * float(wpercent)))
        if width_new < min_width or height_new < min_height:
            break
        img_downscaled = img.resize(size=(width_new, height_new), resample=Image.ANTIALIAS)
        arr = image.img_to_array(img_downscaled)
        pyramid_list.append(arr)
        scale_list.append(scale_factor)
        scale_factor *= downscale
    return pyramid_list, scale_list
Ejemplo n.º 21
0
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import array_to_img
# 載入圖檔
img = load_img("penguins.png", grayscale=True, target_size=(227, 227))
# 顯示圖片資訊
print(type(img))
# 轉換成 Numpy 陣列
img_array = img_to_array(img)
print(img_array.dtype)
print(img_array.shape)
# 將 Numpy 陣列轉換成 Image
img2 = array_to_img(img_array)
print(type(img2))
# 顯示圖片
import matplotlib.pyplot as plt

plt.axis("off")
plt.imshow(img2, cmap="gray")
Ejemplo n.º 22
0
        loss.backward()

        # add epsilon to image
        img_adv = img_adv + epsilon * img_adv.grad.sign_()

        img_adv = img_adv.squeeze(0)
        img_adv = inv_normal(img_adv)
        img_adv = np.clip(img_adv.cpu().detach().numpy(), img_min, img_max)

        #print(img_adv)
        #img_adv = to_pil(img_adv)

    img_adv = np.clip(img_adv, 0, 255)
    '''
    img_try = torch.Tensor(img_adv)
    img_try = normal(img_try)
    img_try = img_try.unsqueeze(0)
    out_try = model(img_try.cuda())
    res_try = np.argmax(out_try.cpu().data.numpy())
    print(res_try) 
    if res_try==target_label:
        error.append(i)
        print('ggggggggggggg')
    '''

    img_adv = np.round(np.rollaxis(img_adv, 0, 3) * 255)
    result = image.array_to_img(img_adv.reshape(224, 224, 3))
    result.save(output_path)
    #plt.imshow(result)
    #plt.show()
Ejemplo n.º 23
0
from keras.preprocessing import image
import numpy as np
import os
from model import generate_upscale2
import cv2
input_dir = "C:\programmering\stylegan\imgs"

output_dir = "altered/scaled"

m = generate_upscale2()

m.load_weights("weights/lastUpScale2.h5")
os.makedirs(output_dir, exist_ok=True)

for path in os.listdir(input_dir):
    noise = np.random.random((1, 512, 512, 1)) * 0.1 - 0.05
    img = image.load_img(os.path.join(input_dir, path))
    img = image.img_to_array(img) / 255. * 2 - 1
    img = cv2.resize(img, (256, 256))
    img = cv2.resize(img, (512, 512), interpolation=cv2.INTER_CUBIC)
    upscaled = m.predict(np.stack([img]))[0]
    scaled = image.array_to_img(upscaled)
    scaled.save(os.path.join(output_dir, path))
    print(path)
Ejemplo n.º 24
0
    labels += 0.05 * np.random.random(labels.shape)

    #train discriminator
    d_loss = discriminator.train_on_batch(combined_images, labels)

    #generate noise
    random_latent_vectors = np.random.normal(size=(batch_size, latent_dim))

    #create label(0=true)
    misleading_targets = np.zeros((batch_size, 1))

    #train gan
    a_loss = gan.train_on_batch(random_latent_vectors, misleading_targets)

    start += batch_size
    if start > len(x_train) - batch_size:
        start = 0

    #save images
    if step % 100 == 0:
        gan.save_weights('gan.h5')

        print('discriminator loss at step %s: %s' % (step, d_loss))
        print('adversarial loss at step %s: %s' % (step, a_loss))

        img = image.array_to_img(generated_images[0] * 255., scale=False)
        img.save(os.path.join(save_dir, 'generated_frog' + str(step) + '.png'))

        img = image.array_to_img(real_images[0] * 255., scale=False)
        img.save(os.path.join(save_dir, 'real_frog' + str(step) + '.png'))
Ejemplo n.º 25
0
def add_noise(imgs, v):
    img = img_to_array(imgs[0])
    img_noise_arr = random_noise(img, mode='s&p',
                                 amount=v, seed=None, clip=False)
    return array_to_img(img_noise_arr), imgs[1]
    def predict_output(self):
        """
        Predicts an output for a given list of files/data.
        """
        for filename in self.filenames:
            print(filename)
            # Open the desired picture
            im = Image.open(filename)
            # Get the image array
            img_to_predict = np.array(im)
            # Be careful -> each pixel value must be a float
            img_to_predict = img_to_predict.astype('float32')
            # Close the file pointer (if possible)
            im.close()
            # Store the real shape for later
            real_shape = img_to_predict.shape

            # At this time we can only use images of shape (m*500, n*500, 3)
            assert real_shape[0] % 500 == 0
            assert real_shape[1] % 500 == 0

            # Predict the segmentation for this picture (its array is stored in data)
            pred = np.zeros(real_shape[:2] + (1, ))
            for i in range(int(real_shape[0] / 500)):
                for j in range(int(real_shape[1] / 500)):
                    print(i, j)
                    # Get a sub-array of the main array
                    sub_array = img_to_predict[i * 500:(i + 1) * 500:,
                                               j * 500:(j + 1) * 500:, :]
                    sub_img = array_to_img(sub_array).resize(
                        self.input_shape[:2])
                    # Because array_to_img is modifying array values to [0,255] we have
                    # to divide each value by 255
                    sub_array = np.array(sub_img) / 255.

                    # Predict the segmentation for this sub-array
                    pred_array = self._model.predict(
                        sub_array.reshape((1, ) + sub_array.shape))
                    pred_img = array_to_img(
                        pred_array.reshape(pred_array.shape[1:])).resize(
                            (500, 500))
                    pred_array = np.array(pred_img).reshape(500, 500, 1)
                    # Add this sub-array to the main array
                    pred[i * 500:(i + 1) * 500:,
                         j * 500:(j + 1) * 500:, :] = pred_array / 255.

            # Reshape the image array to (m, n, 3)
            reshaped_img_array = np.array(
                Image.fromarray(img_to_predict).resize(real_shape[:2][::-1]))
            # If the result for the second value is more than 0.9 -> store a
            # "green" array for this index
            reshaped_img_array[pred[:, :, 0] > 0.9] = [0, 240, 0]
            # Now, for each element in the picture, replace it or not
            img_to_predict[reshaped_img_array[:, :, 1] == 240] = [0, 240, 0]

            # Create a new Image instance with the new_img_array array
            new_img = Image.fromarray(img_to_predict.astype('uint8'))
            # Finally, save this image
            new_img.save(
                basename(splitext(self.__filename)[0]) + "_segmented_img.jpg")
            # Save the unsegmented image
            imsave(
                basename(splitext(self.__filename)[0]) +
                "_unsegmented_img.jpg", np.array(Image.open(self.__filename)))

            # Hold on, close the pointers before leaving
            new_img.close()

            print("Done")
Ejemplo n.º 27
0
def predict():
    output_dir = args.pred_mask_dir
    model = make_model((None, None, 3))
    model.load_weights(args.weights)
    batch_size = args.pred_batch_size
    nbr_test_samples = len(os.listdir(args.test_data_dir))

    filenames = [
        os.path.join(args.test_data_dir, f)
        for f in sorted(os.listdir(args.test_data_dir))
    ]
    ss = pd.read_csv(args.pred_sample_csv)
    start_time = clock()
    for i in range(int(nbr_test_samples / batch_size)):
        x = []
        img_sizes = []
        for j in range(batch_size):
            if i * batch_size + j < len(filenames):
                # img = imread(os.path.join(img_dir, filename))
                # img = load_img(filenames[i * batch_size + j], target_size=(args.img_height, args.img_width))
                img = Image.open(filenames[i * batch_size + j])
                img_size = img.size
                img = img.resize((args.input_height, args.input_width),
                                 Image.ANTIALIAS)
                img_sizes.append(img_size)
                x.append(img_to_array(img))
        x = np.array(x)
        x = imagenet_utils.preprocess_input(x,
                                            mode=args.preprocessing_function)
        # x = imagenet_utils.preprocess_input(x, args.preprocessing_function)
        # x = do_tta(x, args.pred_tta)
        batch_x = x
        # batch_x = np.zeros((x.shape[0], 887, 887, 3))
        # batch_x[:, :, 1:-1, :] = x
        preds = model.predict_on_batch(batch_x)
        # preds = undo_tta(preds, args.pred_tta)
        for j in range(batch_size):
            filename = filenames[i * batch_size + j]
            # prediction = preds[j][:, 1:-1, :]
            prediction = preds[j]
            prediction = prediction > 0.5
            pred_im = array_to_img(prediction * 255).resize(
                img_sizes[j], Image.ANTIALIAS)
            try:
                assert pred_im.size == img_size
            except:
                print('bad')
            # print(filename)
            # pred_im.save(os.path.join(output_dir, filename.split('/')[-1].split('.')[0] + ".png"))
            # print(filename.split('/')[-1].split('.')[0] )
            ss.loc[ss.index[ss['id'] ==
                            filename.split('/')[-1].split('.')[0]].tolist(),
                   'rle_mask'] = RLenc(np.array(pred_im))

        time_spent = clock() - start_time
        print("predicted batch ", str(i))
        print("Time spent: {:.2f}  seconds".format(time_spent))
        print("Speed: {:.2f}  ms per image".format(
            time_spent / (batch_size * (i + 1)) * 1000))
        print("Elapsed: {:.2f} hours  ".format(
            time_spent / (batch_size * (i + 1)) / 3600 *
            (nbr_test_samples - (batch_size * (i + 1)))))
    ss.to_csv(args.submissions_dir + '/submission_1.csv', index=False)
Ejemplo n.º 28
0
    def train(self,
              max_steps,
              batch_size=16,
              sample_interval=10,
              dataset_dir='',
              save_dir=''):

        # Adversarial ground truths
        valid = np.ones((batch_size, 1))
        fake = np.zeros((batch_size, 1))

        total_size = 50000  # dataset contains 50 000 images
        epoch_size = 2000  # loading 2000 images on each epoch
        epoch_batches = epoch_size // batch_size  # number of selected random batches on each epoch

        resume_index_used = False

        step = self.resume_step_number

        while True:

            start_batch_index = 1  # starting image index = first batch [1, 2000]

            # starting from resume_batch_index, but only once, next cycle from 1
            if not resume_index_used:
                start_batch_index = self.resume_batch_index
                resume_index_used = True

            for start_index in range(start_batch_index, total_size,
                                     epoch_size):

                x_train = self.load_celeba_batch(dataset_dir,
                                                 start_index=start_index,
                                                 stop_index=start_index +
                                                 epoch_size)

                # Normalizes data
                x_train = (x_train.reshape((x_train.shape[0], ) +
                                           (self.img_rows, self.img_cols,
                                            self.channels)).astype('float32') -
                           127.5) / 127.5

                for batch in range(epoch_batches):

                    start = timer()

                    if step >= max_steps:
                        self.combined.save_weights(model_file)
                        print('Successfully finished.')
                        return

                    # ---------------------
                    #  Train Discriminator
                    # ---------------------

                    # Select a random batch of images
                    idx = np.random.randint(0, x_train.shape[0], batch_size)
                    imgs = x_train[idx]

                    noise = np.random.normal(0, 1,
                                             (batch_size, self.latent_dim))

                    # Generate a batch of new images
                    gen_imgs = self.generator.predict(noise)

                    # Trick with little random noise to the labels
                    valid_with_noise = valid + 0.05 * np.random.random(
                        valid.shape)
                    fake_with_noise = fake + 0.05 * np.random.random(
                        fake.shape)

                    # Train the discriminator
                    d_loss_real = self.discriminator.train_on_batch(
                        imgs, valid_with_noise)
                    d_loss_fake = self.discriminator.train_on_batch(
                        gen_imgs, fake_with_noise)
                    d_loss = 0.5 * np.add(np.abs(d_loss_real),
                                          np.abs(d_loss_fake))

                    # ---------------------
                    #  Train Generator
                    # ---------------------

                    noise = np.random.normal(0, 1,
                                             (batch_size, self.latent_dim))

                    # Train the generator (to have the discriminator label samples as valid)
                    g_loss = self.combined.train_on_batch(noise, valid)

                    # Plot the progress
                    if step % 1 == 0:
                        end = timer()
                        elapsed = end - start
                        print(
                            "step {}  (images [{}, {}], batch {}):   D_Loss: {:.4f}, D_Acc: {:.2f}%  |  G_Loss: {:.4f}, G_Acc: {:.2f}%   Time: {:.0f} sec."
                            .format(step, start_index,
                                    start_index + epoch_size - 1, batch,
                                    d_loss[0], 100 * d_loss[1], g_loss[0],
                                    100 * g_loss[1], elapsed))

                    # If at save interval => save generated image samples
                    if step % sample_interval == 0:
                        img = image.array_to_img(
                            gen_imgs[batch_size // 2] * 127.5 + 127.5,
                            scale=False)
                        img.save(
                            os.path.join(save_dir,
                                         'generated_' + str(step) + '.png'))

                    # Save model
                    if (step + 1) % 100 == 0:
                        self.combined.save_weights(self.model_file)

                    step += 1
Ejemplo n.º 29
0
class NumpyArrayIterator2(NumpyArrayIterator):
    def next(self):
 with self.lock:
            # We changed index_array to self.index_array
            self.index_array, current_index, current_batch_size = next(self.index_generator)
        # The transformation of images is not under thread lock so it can be done in parallel
        batch_x = np.zeros(tuple([current_batch_size] + list(self.x.shape)[1:]))
        for i, j in enumerate(self.index_array):
            x = self.x[j]
            x = self.image_data_generator.random_transform(x.astype('float32'))
            x = self.image_data_generator.standardize(x)
            batch_x[i] = x
        if self.save_to_dir:
            for i in range(current_batch_size):
                img = array_to_img(batch_x[i], self.dim_ordering, scale=True)
                fname = '{prefix}_{index}_{hash}.{format}'.format(prefix=self.save_prefix,
                                                                  index=current_index + i,
                                                                  hash=np.random.randint(1e4),
                                                                  format=self.save_format)
                img.save(os.path.join(self.save_to_dir, fname))
        if self.y is None:
            return batch_x
        batch_y = self.y[self.index_array]
        return batch_x, batch_y

print('Creating Data Augmenter...')
imgen = ImageDataGenerator2(
    rotation_range=20,
    zoom_range=0.2,
    horizontal_flip=True,
Ejemplo n.º 30
0
arraylist = []
for i in range(27):
    array0 = dict['R-0-%03d'%i]
    array1 = dict['R-1-%03d'%i]
    array2 = dict['R-2-%03d'%i]
    array3 = dict['R-3-%03d'%i]
    
    array_conc = array_concatenate(array0, array1, array2, array3)
    arraylist.append(array_conc)


# In[60]:


image.array_to_img(arraylist[10])


# In[61]:


array26 = array_26_conc(arraylist)


# In[64]:


array26.shape


# In[65]:
Ejemplo n.º 31
0
def scaling(input_image):
    input_image = input_image / 255.0
    return input_image


# Scale from (0, 255) to (0, 1)
train_ds = train_ds.map(scaling)
valid_ds = valid_ds.map(scaling)
"""
Let's visualize a few sample images:
"""

for batch in train_ds.take(1):
    for img in batch:
        display(array_to_img(img))
"""
We prepare a dataset of test image paths that we will use for
visual evaluation at the end of this example.
"""

dataset = os.path.join(root_dir, "images")
test_path = os.path.join(dataset, "test")

test_img_paths = sorted([
    os.path.join(test_path, fname) for fname in os.listdir(test_path)
    if fname.endswith(".jpg")
])
"""
## Crop and resize images
Ejemplo n.º 32
0
from keras.utils import np_utils

(X_train, Y_train), (X_test, Y_test) = mnist.load_data()

X_train = X_train.reshape((len(X_train), np.prod(X_train.shape[1:])))
X_test = X_test.reshape((len(X_test), np.prod(X_test.shape[1:])))

X_train = np.dstack([X_train] * 3)
X_test = np.dstack([X_test] * 3)

X_train = X_train.reshape(-1, 28, 28, 3)
X_test = X_test.reshape(-1, 28, 28, 3)

from keras.preprocessing.image import img_to_array, array_to_img
X_train = np.asarray([
    img_to_array(array_to_img(im, scale=False).resize((48, 48)))
    for im in X_train
])
X_test = np.asarray([
    img_to_array(array_to_img(im, scale=False).resize((48, 48)))
    for im in X_test
])

X_train = X_train.astype('float32') / 255.
X_test = X_test.astype('float32') / 255.

from keras.applications import VGG16
conv_base = VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(48, 48, 3))
Ejemplo n.º 33
0
(data_train, target_train), (data_test, target_test) = fashion_mnist.load_data()


data_train=np.dstack([data_train] * 3)
data_test=np.dstack([data_test]*3)
data_train.shape
data_test.shape

data_train = data_train.reshape(-1, 28,28,3)
data_test= data_test.reshape (-1,28,28,3)
data_train.shape
data_test.shape


data_train = np.asarray([img_to_array(array_to_img(im, scale=False).resize((48,48))) for im in data_train])
data_test = np.asarray([img_to_array(array_to_img(im, scale=False).resize((48,48))) for im in data_test])
data_train.shape
data_test.shape

data_train = data_train / 255.
data_test = data_test / 255.
data_train = data_train.astype('float32')
data_test = data_test.astype('float32')


train_Y_one_hot = to_categorical(target_train)
test_Y_one_hot = to_categorical(target_test)


Ejemplo n.º 34
0
from keras.preprocessing.image import ImageDataGenerator
from keras.preprocessing.image import array_to_img, img_to_array, load_img
import glob

for i in range(1, 685):
    input_path = "FinalProject_TeluguOCR/TeluguDataset/train/" + str(
        i) + "/" + str(i) + ".JPEG"
    output_path = "FinalProject_TeluguOCR/SmallTelugu/test/" + str(
        i) + "/" + str(i) + 'st.{}.JPEG'
    count = 39

    gen = ImageDataGenerator(rotation_range=0.1,
                             width_shift_range=0.2,
                             height_shift_range=0.2,
                             zoom_range=0.2,
                             horizontal_flip=False)

    # load image to array
    image = img_to_array(load_img(input_path))

    # reshape to array rank 4
    image = image.reshape((1, ) + image.shape)

    # let's create infinite flow of images
    images_flow = gen.flow(image, batch_size=1)
    for c, new_images in enumerate(images_flow):
        # we access only first image because of batch_size=1
        new_image = array_to_img(new_images[0], scale=True)
        new_image.save(output_path.format(c + 1))
        if c >= count:
            break
Ejemplo n.º 35
0
def _encode(msg,
            dataset,
            model_type,
            epochs,
            experiment_id,
            attack_name,
            attack_strength=2.0):
    extension = default_extension
    encoded_msg = _encodeString(msg)
    logger.info("Encode message {}=>{}".format(msg, encoded_msg))
    test_size = len(encoded_msg)
    model, x_train, x_test, y_train, y_test = load_model(dataset=dataset,
                                                         model_type=model_type,
                                                         epochs=epochs,
                                                         use_tensorboard=True)
    num_classes = DATASET_CLASSES[dataset]

    combined = list(zip(x_test, y_test))
    random.shuffle(combined)
    x_test[:], y_test[:] = zip(*combined)

    #keep only correctly predicted inputs
    batch_size = 64
    preds_test = np.argmax(model.predict(x_test, verbose=0), axis=1)
    inds_correct = np.where(preds_test == y_test.argmax(axis=1))[0]
    x, y = x_test[inds_correct], y_test[inds_correct]
    x, y = x[:test_size], y[:test_size]

    chunk_size = int(math.log(num_classes) / math.log(10))
    #groups = _split_msg(encoded_msg, chunk_size)

    targets = np.array(
        to_categorical([int(i) for i in encoded_msg], num_classes), "int32")
    #print(targets)

    class_density = 0.03  # total class * density = total attacked classes
    epsilon = 5.0
    max_iter = 100
    SATA.power = 1.5
    nb_elements = 1000

    adv_x, rate_best = SATA.embed_message(model,
                                          x_test[:nb_elements],
                                          encoded_msg,
                                          epsilon=epsilon,
                                          class_density=class_density)

    #adv_x = craft_attack(model,x,attack_name,y=targets, epsilon=attack_strength)
    yadv = np.argmax(model.predict(adv_x), axis=1)

    pictures_path = default_path.format(experiment_id, attack_name,
                                        experiment_time)
    os.makedirs(pictures_path, exist_ok=True)
    os.makedirs("{}/ref".format(pictures_path), exist_ok=True)
    SSIM = []
    PSNR = []

    for i, _adv in enumerate(adv_x):
        predicted = yadv[i]
        encoded = np.argmax(targets[i])
        truth = np.argmax(y[i])
        adv_path = "{}/{}_predicted{}_encoded{}_truth{}.{}".format(
            pictures_path, i, predicted, encoded, truth, extension)
        real_path = "{}/ref/{}.{}".format(pictures_path, i, extension)

        adv = array_to_img(_adv)
        adv = adv.resize((16, 16), Image.BILINEAR)
        #adv = adv.convert("L")
        adv.save(adv_path)

        adv_loaded = _load_image(adv_path)

        real_img = array_to_img(x[i])
        real_img = real_img.resize((16, 16), Image.BILINEAR)
        #real_img = real_img.convert("L")
        #real = np.squeeze(img_to_array(real_img))
        real = img_to_array(real_img)

        ssim = 1 - np.array(
            list(
                ssim_distance(None,
                              np.array([real]),
                              adv_x=np.array([adv_loaded]),
                              distance_only=True)))
        SSIM.append(ssim)

        psnr = _psnr_loss(adv_loaded, real)
        PSNR.append(psnr)

    return np.array(SSIM).mean(), np.array(PSNR).mean()
Ejemplo n.º 36
0
# read 4th image
normal_images_in_train = [
    os.path.join(train_normal_dir, fname)
    for fname in os.listdir(train_normal_dir)
]
img_path = normal_images_in_train[3]
img = image.load_img(path=img_path, target_size=(150, 150))

x = image.img_to_array(img=img)
x = x.reshape((1, ) + x.shape)
print(x.shape)

i = 0
for batch in datagen.flow(x=x, batch_size=1):
    plt.figure(i)
    img_plot = plt.imshow(image.array_to_img(x=batch[0]))
    i += 1
    if i % 4 == 0:
        break

#%%
from keras import models, layers, optimizers

model = models.Sequential()
model.add(
    layers.Conv2D(filters=32,
                  kernel_size=(3, 3),
                  activation="relu",
                  input_shape=(150, 150, 3)))
model.add(layers.MaxPool2D(pool_size=(2, 2)))
model.add(layers.Conv2D(filters=64, kernel_size=(3, 3), activation="relu"))
Ejemplo n.º 37
0
def train_model(parameters):
    glacier = parameters['GLACIER_NAME']
    n_batch = int(parameters['BATCHES'])
    n_epochs = int(parameters['EPOCHS'])
    n_layers = int(parameters['LAYERS_DOWN'])
    n_init = int(parameters['N_INIT'])
    suffix = parameters['SUFFIX']
    drop = float(parameters['DROPOUT'])
    imb_w = float(parameters['IMBALANCE_RATIO'])
    #-- set up configurations based on parameters
    if parameters['AUGMENT'] in ['Y','y']:
        augment = True
        aug_config = np.int(parameters['AUG_CONFIG'])
        aug_str = '_augment-x%i'%aug_config
    else:
        augment = False
        aug_config = 0
        aug_str = ''
    
    if parameters['CROP'] in ['Y','y']:
        crop_str = '_cropped'
    else:
        crop_str = ''
    
    if parameters['NORMALIZE'] in ['y','Y']:
        normalize = True
        norm_str = '_normalized'
    else:
        normalize = False
        norm_str = ''
    
    if parameters['LINEAR'] in ['Y','Y']:
        linear = True
        lin_str = '_linear'
    else:
        linear = False
        lin_str = ''

    drop_str = ''
    if drop>0:
        drop_str = '_w%.1fdrop'%drop

    #-- plotting
    if parameters['PLOT'] in ['y','Y']:
        PLOT = True
    else:
        PLOT = False

    #-- width of labels (pixels)
    #-- don't label 3-pix width to be consistent with old results
    if parameters['LABEL_WIDTH'] == '3':
        lbl_width = ''
    else:
        lbl_width = '_%ipx'%int(parameters['LABEL_WIDTH'])

    if (normalize) and (drop!=0):
        sys.exit('Both batch normalization and dropout are selecte. Choose one.')

    #-- directory setup
    #- current directory
    current_dir = os.path.dirname(os.path.realpath(__file__))
    main_dir = os.path.join(current_dir,'..','FrontLearning_data')
    glacier_ddir = os.path.join(main_dir,'%s.dir'%glacier)
    data_dir = os.path.join(glacier_ddir, 'data')
    trn_dir = os.path.join(data_dir,'train')
    tst_dir = os.path.join(data_dir,'test')

    #-- load images
    data = load_data(suffix,trn_dir,tst_dir,n_layers,augment,aug_config,crop_str,lbl_width)

    n,height,width,channels=data['trn_img'].shape
    print('width=%i'%width)
    print('height=%i'%height)

    #-- set up sample weight to deal with imbalance
    if parameters['ADD_WEIGHTS'] in ['Y','y']:
        if imb_w == 0:
            ratio = set_ratio(data['trn_lbl'])
        else:
            ratio = imb_w
        sample_weights = set_weights(data['trn_lbl'], ratio)
        imb_str = '_%.2fweight'%ratio
    else:
        imb_str = ''

    #-- import mod
    unet = imp.load_source('unet_model', os.path.join(current_dir,'unet_model.py'))
    if normalize:
        if linear:
            model = unet.unet_model_linear_normalized(height=height,width=width,channels=channels,\
                n_init=n_init,n_layers=n_layers)
            print('importing unet_model_linear_normalized')
        else:
            model = unet.unet_model_double_normalized(height=height,width=width,channels=channels,\
                n_init=n_init,n_layers=n_layers)
            print('importing unet_model_double_normalized')
    else:
        if linear:
            model = unet.unet_model_linear_dropout(height=height,width=width,channels=channels,\
                n_init=n_init,n_layers=n_layers,drop=drop)
            print('importing unet_model_linear_dropout')
        else:
            model = unet.unet_model_double_dropout(height=height,width=width,channels=channels,\
                n_init=n_init,n_layers=n_layers,drop=drop)
            print('importing unet_model_double_dropout')


    #-- compile imported model
    model.compile(loss='binary_crossentropy',optimizer='adam', metrics=['accuracy']\
        ,sample_weight_mode="temporal")

    #-- checkpoint file
    chk_file = os.path.join(glacier_ddir,'unet_model_weights_%ibatches_%iepochs_%ilayers_%iinit%s%s%s%s%s%s%s%s.h5'\
        %(n_batch,n_epochs,n_layers,n_init,lin_str,imb_str,drop_str,norm_str,aug_str,suffix,crop_str,lbl_width))

    #-- if file exists, read model from file
    if os.path.isfile(chk_file):
        print('Check point exists; loading model from file.')
        # load weights
        model.load_weights(chk_file)
        
    #-- if not train model
    if (parameters['RETRAIN'] in ['y','Y']) or (not os.path.isfile(chk_file)):
        print('Training model...')
        #-- create checkpoint
        model_checkpoint = keras.callbacks.ModelCheckpoint(chk_file, monitor='loss',\
            verbose=1, save_best_only=True)
        lr_callback = ReduceLROnPlateau(monitor='acc', factor=0.5, patience=5,
            verbose=1, mode='auto', min_delta=0.0001, cooldown=0, min_lr=0)
        #es_callback = EarlyStopping(monitor='val_loss',min_delta=0.0001, patience=5,
        #    verbose=1, mode='auto')
        #-- now fit the model
        history = model.fit(data['trn_img'], data['trn_lbl'], batch_size=n_batch, epochs=n_epochs, verbose=1,\
            validation_split=0.1, shuffle=True, sample_weight=sample_weights, callbacks=[lr_callback,model_checkpoint])

        #-- save history to file
        outfile = open(os.path.join(glacier_ddir,\
                'training_history_%ibatches_%iepochs_%ilayers_%iinit%s%s%s%s%s%s%s%s.txt'\
                %(n_batch,n_epochs,n_layers,n_init,lin_str,imb_str,drop_str,norm_str,\
                aug_str,suffix,crop_str,lbl_width)),'w')
        outfile.write('Epoch loss\tval_loss\tacc\tval_acc\n')
        for i in range(len(history.history['loss'])):
            outfile.write('%i\t%f\t%f\t%f\t%f\n'%(i,history.history['loss'][i],history.history['val_loss'][i],\
                history.history['acc'][i],history.history['val_acc'][i]))
        outfile.close()

        #-- Make plots for training history
        if PLOT:
            for item,name in zip(['acc','loss'],['Accuracy','Loss']):
                fig = plt.figure(1,figsize=(8,6))
                plt.plot(history.history[item])
                plt.plot(history.history['val_%s'%item])
                plt.title('Model %s'%name)
                plt.ylabel(name)
                plt.xlabel('Epochs')
                plt.legend(['Training', 'Validation'], loc='upper left')
                plt.savefig(os.path.join(glacier_ddir,\
                    'training_history_%s_%ibatches_%iepochs_%ilayers_%iinit%s%s%s%s%s%s%s.pdf'\
                    %(item,n_batch,n_epochs,n_layers,n_init,lin_str,imb_str,drop_str,norm_str,\
                    aug_str,suffix,crop_str,lbl_width)),format='pdf')
                plt.close(fig)

    print('Model is trained. Running on test data...')

    #-- make dictionaries for looping through train and test sets
    in_img = {}
    in_img['train'] = data['trn_orig']
    in_img['test'] = data['tst_img']
    outdir = {}
    outdir['train'] = trn_dir
    outdir['test'] = tst_dir
    names = {}
    names['train'] = data['trn_names']
    names['test'] = data['tst_names']
    #-- Now test the model on both the test data and the train data
    for t in ['test','train']:
        out_imgs = model.predict(in_img[t], batch_size=1, verbose=1)
        out_imgs = out_imgs.reshape(out_imgs.shape[0],height,width,out_imgs.shape[2])
        #-- make output directory
        out_subdir = 'output_%ibatches_%iepochs_%ilayers_%iinit%s%s%s%s%s%s%s%s'\
            %(n_batch,n_epochs,n_layers,n_init,lin_str,imb_str,drop_str,norm_str,aug_str,suffix,crop_str,lbl_width)
        if (not os.path.isdir(os.path.join(outdir[t],out_subdir))):
            os.mkdir(os.path.join(outdir[t],out_subdir))
        #-- save the test image
        for i in range(len(out_imgs)):
            im = image.array_to_img(out_imgs[i])
            print(os.path.join(outdir[t],out_subdir,'%s'%names[t][i].replace('_Subset',''))) 
            im.save(os.path.join(outdir[t],out_subdir,'%s'%names[t][i].replace('_Subset','')))
Ejemplo n.º 38
0
def predict_and_evaluate():
    output_dir = args.pred_mask_dir
    # test_mask_dir = args.test_mask_dir
    model = make_model((None, None, 3))
    model.load_weights(args.weights)
    batch_size = args.pred_batch_size
    nbr_test_samples = len(os.listdir(args.test_data_dir))

    filenames = [
        os.path.join(args.test_data_dir, f)
        for f in sorted(os.listdir(args.test_data_dir))
    ]
    mask_filenames = [
        os.path.join(args.test_mask_dir, f).replace('.jpg', '.png')
        for f in sorted(os.listdir(args.test_data_dir))
    ]

    start_time = clock()
    dices = []
    for i in range(int(nbr_test_samples / batch_size)):
        img_sizes = []
        x = []
        masks = []
        for j in range(batch_size):
            if i * batch_size + j < len(filenames):
                img = Image.open(filenames[i * batch_size + j])
                mask = Image.open(mask_filenames[i * batch_size + j])
                img_size = img.size
                img = img.resize((args.input_height, args.input_width),
                                 Image.ANTIALIAS)
                mask = mask.resize((args.input_height, args.input_width),
                                   Image.ANTIALIAS)
                # mask = img_to_array(mask) > 0
                img_sizes.append(img_size)
                x.append(img_to_array(img))
                masks.append(img_to_array(mask) / 255)
        x = np.array(x)
        masks = np.array(masks)
        x = imagenet_utils.preprocess_input(x,
                                            mode=args.preprocessing_function)
        # x = imagenet_utils.preprocess_input(x, args.preprocessing_function)
        # x = do_tta(x, args.pred_tta)
        batch_x = x
        # batch_x = np.zeros((x.shape[0], 887, 887, 3))
        # batch_x[:, :, 1:-1, :] = x
        preds = model.predict_on_batch(batch_x)
        # preds = undo_tta(preds, args.pred_tta)
        batch_dice = dice_coef(masks, preds)
        dices.append(batch_dice)
        for j in range(batch_size):
            filename = filenames[i * batch_size + j]
            # prediction = preds[j][:, 1:-1, :]
            prediction = preds[j]
            prediction = prediction > 0.5
            pred_im = array_to_img(prediction * 255).resize(
                img_sizes[j], Image.ANTIALIAS)
            pred_im.save(
                os.path.join(output_dir,
                             filename.split('/')[-1][:-4] + ".png"))
        time_spent = clock() - start_time
        print("predicted batch ", str(i))
        print("Time spent: {:.2f}  seconds".format(time_spent))
        print("Speed: {:.2f}  ms per image".format(
            time_spent / (batch_size * (i + 1)) * 1000))
        print("Elapsed: {:.2f} hours  ".format(
            time_spent / (batch_size * (i + 1)) / 3600 *
            (nbr_test_samples - (batch_size * (i + 1)))))
        print("predicted batch dice {}".format(batch_dice))
    print(np.mean(dices))
Ejemplo n.º 39
0
def dump_image(x, filename, format):
    img = image.array_to_img(x, scale=False)
    img.save(filename, format)
    return
#(150,150)で読み込み
#load_img;読み込み、デフォルトではRGBのPIL形式で読み込み
img = image.load_img(img_path, target_size=(150, 150))

#形状が(150,150,3)のnumpy配列に変換
x = image.img_to_array(img)
#(1,150,150,3)に変更
x = x.reshape((1, ) + x.shape)

#ランダムに変換いsた画像のバッチを生成する
#無限ループになるため何らかのタイミングでbreak
i = 0
#flow:拡張されたデータを返す
for batch in datagen.flow(x, batch_size=1):
    plt.figure(i)
    imgplot = plt.imshow(image.array_to_img(batch[0]))
    i += 1
    if i % 4 == 0:
        break

plt.show()

#Dropoutが追加された新たなCNNを定義する
from keras import layers
from keras import models

model = models.Sequential()
model.add(
    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(150, 150, 3)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
Ejemplo n.º 41
0
def Rotate(imgs, _):
    v = random.randrange(0,180,1)
    img1 = array_to_img(rotate(img_to_array(imgs[0]), v, reshape=False))
    img2 = array_to_img(rotate(img_to_array(imgs[1]), v, reshape=False))
    return img1, img2
Ejemplo n.º 42
0
SCRIPT_DIR = os.getcwd()
CALIB_DIR = os.path.join(SCRIPT_DIR, 'calib_dir')
IMAGE_LIST_FILE = 'calib_list.txt'

if (os.path.exists(CALIB_DIR)):
    shutil.rmtree(CALIB_DIR)
os.makedirs(CALIB_DIR)
print('Directory', CALIB_DIR, 'created')

#####################################################
# Get the dataset using Keras
#####################################################

(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# create file for list of calibration images
f = open(os.path.join(CALIB_DIR, IMAGE_LIST_FILE), 'w')

#####################################################
# convert test dataset into image files
#####################################################

for i in range(len(x_test)):
    img = array_to_img(x_test[i])
    save_img(os.path.join(CALIB_DIR, 'x_test_' + str(i) + '.png'), img)
    f.write('x_test_' + str(i) + '.png\n')

f.close()

print('FINISHED GENERATING CALIBRATION IMAGES')
Ejemplo n.º 43
0
 def testAndMap(self,
                imagename,
                imagepath,
                basetilepath,
                maskpath,
                smooth=False,
                savemask=False,
                savemap=True,
                mapname=None,
                labeled=True,
                st_el_size=100):
     model = self.model
     test_datagen = ImageDataGenerator(rescale=1. / 255)
     test_generator = test_datagen.flow_from_directory(
         directory=basetilepath + "test" + str(self.img_size) + "/",
         target_size=(self.img_size, self.img_size),
         color_mode="rgb",
         batch_size=1,
         class_mode='categorical',
         shuffle=False,
     )
     #test_generator = test_datagen.flow(imlist, [], [], batch_size=1)
     print("Testing " + self.name + " with " + imagename + " tiles")
     filenames = test_generator.filenames
     nb_samples = len(filenames)
     test_generator.reset()
     preds = model.predict_generator(test_generator, nb_samples)
     predicted_class_indices = np.argmax(preds, axis=1)
     labels = (test_generator.class_indices)
     labels = dict((v, k) for k, v in labels.items())
     predictions = [labels[k] for k in predicted_class_indices]
     image_predictions_list = []
     for i in range(len(predictions)):
         if imagename in filenames[i]:
             image_predictions_list.append(predictions[i])
     print("Found " + str(len(image_predictions_list)) + " " + imagename +
           " tiles")
     type1_tot = sum([1 for p in image_predictions_list if p == self.type1])
     type2_tot = sum([1 for p in image_predictions_list if p == self.type2])
     dom = self.type1 if type1_tot > type2_tot else self.type2
     print("Dominant type for " + imagename + " is " + dom + " based on " +
           str(type1_tot) + " type 1 (" + self.type1 +
           ") predictions and " + str(type2_tot) + " type 2 (" +
           self.type2 + ") predictions")
     correct = None
     if labeled:
         if dom in imagename:
             print(imagename + " classified correctly")
             correct = True
         else:
             print("Incorrect classification for " + imagename)
             correct = False
     tilelist = [
         name for name in filenames
         if (predictions[filenames.index(name)] == dom and imagename in name
             )
     ]
     print("Mapping with " + str(len(tilelist)) + " tiles")
     if not mapname:
         mapname = self.name + " size " + str(
             self.img_size
         ) + " tiles of " + imagename + " with prediction " + dom
     rgb_img = Image.open(imagepath + imagename + '.jpg')
     grayscale = rgb_img.convert('L')
     grayarray = np.array(grayscale)
     rows, cols = grayarray.shape
     color_mask = Image.fromarray(np.zeros((rows, cols, 3), dtype=np.uint8))
     for t in tilelist:
         name = os.path.basename(t)
         coords = [
             int(x) for x in re.findall('\\((.*?)\\)', name)[0].split(',')
         ]  #get coordinate tuple
         draw = ImageDraw.Draw(color_mask)
         draw.rectangle(coords, fill="red")
         del draw
     if smooth:
         cv2mask = np.array(color_mask)
         cv2colormask = cv2.cvtColor(cv2mask,
                                     cv2.COLOR_RGB2BGR)  #convert to cv2
         kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,
                                            (st_el_size, st_el_size))
         cv2_color_mask = cv2.morphologyEx(cv2colormask,
                                           cv2.MORPH_OPEN,
                                           kernel,
                                           iterations=3)
         color_mask = Image.fromarray(cv2_color_mask)
         if savemask:
             cv2bwmask = cv2.cvtColor(cv2mask,
                                      cv2.COLOR_RGB2GRAY)  #convert to cv2
             cv2_bw_mask = cv2.morphologyEx(cv2bwmask,
                                            cv2.MORPH_OPEN,
                                            kernel,
                                            iterations=3)
             pil_bw_mask = Image.fromarray(cv2_bw_mask)
             pil_bw_mask = pil_bw_mask.point(lambda x: 0
                                             if x < 1 else 255, '1')
             pil_bw_mask.save(maskpath + self.name + '_' + imagename +
                              '.jpg')
             savemask = False
     if savemask:
         cv2mask = np.array(color_mask)
         cv2bwmask = cv2.cvtColor(cv2mask, cv2.COLOR_RGB2GRAY)
         bw_mask = Image.fromarray(cv2bwmask)
         bw_mask = bw_mask.point(lambda x: 0 if x < 1 else 255, '1')
         bw_mask.save(maskpath + self.name + '_' + imagename + '_mask.jpg')
     alpha = 0.75
     img_color = np.dstack((grayscale, grayscale, grayscale))
     # Convert the input image and color mask to Hue Saturation Value (HSV)
     # colorspace
     img_hsv = color.rgb2hsv(img_color)
     color_mask_hsv = color.rgb2hsv(color_mask)
     # Replace the hue and saturation of the original image
     # with that of the color mask
     img_hsv[..., 0] = color_mask_hsv[..., 0]
     img_hsv[..., 1] = color_mask_hsv[..., 1] * alpha
     img_masked = color.hsv2rgb(img_hsv)
     # Use keras to convert, and save
     img_masked = image.array_to_img(img_masked)
     if savemap:
         img_masked.save(self.codepath + mapname + '.jpg')
     return dom, correct
Ejemplo n.º 44
0
def saveDictElement(dict_element, save_dir):
    arr = np.array(dict_element)
    arr = arr.clip(0, 1)
    img = array_to_img(arr)
    img.save(save_dir)
Ejemplo n.º 45
0
		plt.yticks(fontsize=15)
		plt.legend()
		plt.tight_layout()
		pdf_all.savefig(fig)

	datagen = ImageDataGenerator(rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest')
	from keras.preprocessing import image
	fname = [os.path.join(train_cats_dir, fname) for fname in os.listdir(train_cats_dir)]
	img_path = fnames[3]
	img = image.load_img(img_path, target_size=(150, 150))
	x = image.img_to_array(img)
	x = x.reshape((1,) + x.shape)
	i = 0
	for batch in datagen.flow(x, batch_size=1):
		plt.figure(i)
		imgplt = plt.imshow(image.array_to_img(batch[0]))
		i += 1
		if i % 4 == 0:
			break
	#plt.show()

	from keras.preprocessing.image import ImageDataGenerator
	train_datagen = ImageDataGenerator(rescale=1./255, rotation_range=40, width_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True,)
	test_datagen = ImageDataGenerator(rescale=1./255)
	train_generator = train_datagen.flow_from_directory(train_dir, target_size=(150, 150), batch_size=32, class_mode='binary')
	validation_generator = test_datagen.flow_from_directory(validation_dir, target_size=(150, 150), batch_size=32, class_mode='binary')
	history = model.fit_generator(train_generator, steps_per_epoch=100, epochs=100, validation_data=validation_generator, validation_steps=50)
	model.save('cats_and_dogs_small_2.h5')
	#model = models.load_model('cats_and_dogs_mall_1.h5')

Ejemplo n.º 46
0
y_train = y_train[:50]
print(x_train.shape)
# x_train = x_train.astype("float32")/255
# x_test = x_test.astype("float32")/255
x_train = np.array(x_train).reshape((-1, np.prod(x_train.shape[1:])))
x_test = np.array(x_test).reshape((-1, np.prod(x_train.shape[1:])))
x_train = np.dstack([x_train] * 3)
x_test = np.dstack([x_test] * 3)
x_train = x_train.reshape(-1, 28, 28, 3)
x_test = x_test.reshape(-1, 28, 28, 3)
print(x_train.shape)
x_train = x_train.astype("float32") / 255
x_test = x_test.astype("float32") / 255
from keras.preprocessing.image import img_to_array, array_to_img
x_train = np.asarray([
    img_to_array(array_to_img(im, scale=False).resize((71, 71)))
    for im in x_train
])
x_test = np.asarray([
    img_to_array(array_to_img(im, scale=False).resize((71, 71)))
    for im in x_test
])

print(x_train.shape)

y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)
print(y_train)
model = Sequential()
# conv_base = VGG16(weights="imagenet", include_top=False, input_shape=(48,48,3))
# conv_base = VGG19(weights="imagenet", include_top=False, input_shape=(48,48,3))
Ejemplo n.º 47
0
def extract_fp_examples(file_name, ground_truth, boxes, boxes_pos, boxes_prob, including_ignore, fp_name, overlap_thresh=0.1, accThresh=0.5):
    # including_ignore:0   print ground_truth and does NOT save FP examples
    # including_ignore:1   does NOT print ground_truth, and save FP examples

    from PIL import Image
    from keras.preprocessing import image
    import os
    # if there are no boxes, return an empty list
    import numpy as np

    if len(boxes_pos) == 0:
        return 0

    pick = [0] * len(boxes_pos)  # pick : indicator whether the box is in the ground_truth sets or not (1: True 0:FP)

    if including_ignore == 0:
        print ground_truth

    # if the bounding boxes integers, convert them to floats --
    # this is important since we'll be doing a bunch of divisions
    if boxes_pos.dtype.kind == "i":
        boxes_pos = boxes_pos.astype("float")

    # grab the coordinates of the bounding boxes
    x1 = boxes_pos[:, 0]
    y1 = boxes_pos[:, 1]
    x2 = boxes_pos[:, 2]
    y2 = boxes_pos[:, 3]

    # compute the area of the bounding boxes and sort the bounding
    # boxes by the bottom-right y-coordinate of the bounding box
    area = (x2 - x1 + 1) * (y2 - y1 + 1)

    # keep looping while some indexes still remain in the indexes
    # list
    for i in xrange(len(boxes_pos)):
        # grab the last index in the indexes list and add the
        # index value to the list of picked indexes
        for j in range(len(ground_truth)):
            one_gnd_picture = ground_truth[j]  # for efficiency

            xx1 = np.maximum(one_gnd_picture[0], x1[i])
            yy1 = np.maximum(one_gnd_picture[1], y1[i])
            xx2 = np.minimum(one_gnd_picture[2], x2[i])
            yy2 = np.minimum(one_gnd_picture[3], y2[i])

            # compute the width and height of the bounding box
            w = np.maximum(0, xx2 - xx1 + 1)
            h = np.maximum(0, yy2 - yy1 + 1)
            # compute the ratio of overlap
            overlap = (w * h) / area
            if overlap[i] > overlap_thresh and boxes_prob[i] > accThresh:  # Ovelap should over accuracy threshold which is set to 0.5 in default.
                pick[i] = 1

    count_fp = 0  # count for the number of false positive pictures
    check = 0  # existence check for fp saving folder
    for i in xrange(len(pick)):
        if pick[i] == 0 and boxes_prob[i] > 0.9:  # false positive case
            im = image.array_to_img(boxes[i])
            count_fp += 1
            if check == 0 and including_ignore == 1:
                directory = os.path.join(os.getcwd(), "false_positive_set")
                if not os.path.exists(directory):
                    os.makedirs(directory)
                check = 1
            im.save(os.path.join(os.getcwd(), "false_positive_set", fp_name+file_name + str(count_fp + 1) + ".png"), "png")

    # print "False Positive Images are saved with the name '"+file_name+"'"

    return len(boxes_pos) - count_fp
Ejemplo n.º 48
0
def dump_image(x, filename, format="png", scale=False):
    # img = image.array_to_img(x, scale=scale)
    img = image.array_to_img(x)
    img.save(filename, format)
    return
Ejemplo n.º 49
0
 def predict(self, x):
     results = self.model.predict(x, batch_size=1, verbose=1)
     for i in range(results.shape[0]):
         result = results[i]
         result = array_to_img(result)
         result.save(RESULT_PATH + "{}.jpg".format(i))
Ejemplo n.º 50
0
    def __get_indexes(self, index_array):
        index_array = sorted(index_array)
        if self.x_prev_states is not None:
            batch_x_images = np.zeros(tuple([self.batch_size]+ list(self.x_images.shape)[1:]),
                                      dtype=K.floatx())
            batch_x_prev_states = np.zeros(tuple([self.batch_size]+list(self.x_prev_states.shape)[1:]), dtype=K.floatx())
        else:
            batch_x_images = np.zeros(tuple([self.batch_size] + list(self.x_images.shape)[1:]), dtype=K.floatx())

        if self.roi is not None:
            batch_x_images = batch_x_images[:, self.roi[0]:self.roi[1], self.roi[2]:self.roi[3], :]
            
        used_indexes = []
        is_horiz_flipped = []
        for i, j in enumerate(index_array):
            x_images = self.x_images[j]
            
            if self.roi is not None:
                x_images = x_images[self.roi[0]:self.roi[1], self.roi[2]:self.roi[3], :]
            
            transformed = self.image_data_generator.random_transform_with_states(x_images.astype(K.floatx()))
            x_images = transformed[0]
            is_horiz_flipped.append(transformed[1])
            x_images = self.image_data_generator.standardize(x_images)
            batch_x_images[i] = x_images

            if self.x_prev_states is not None:
                x_prev_states = self.x_prev_states[j]
                
                if (transformed[1]):
                    x_prev_states[0] *= -1.0
                
                batch_x_prev_states[i] = x_prev_states
            
            used_indexes.append(j)

        if self.x_prev_states is not None:
            batch_x = [np.asarray(batch_x_images), np.asarray(batch_x_prev_states)]
        else:
            batch_x = np.asarray(batch_x_images)
            
        if self.save_to_dir:
            for i in range(0, self.batch_size, 1):
                hash = np.random.randint(1e4)
               
                img = image.array_to_img(batch_x_images[i], self.data_format, scale=True)
                fname = '{prefix}_{index}_{hash}.{format}'.format(prefix=self.save_prefix,
                                                                        index=1,
                                                                        hash=hash,
                                                                        format=self.save_format)
                img.save(os.path.join(self.save_to_dir, fname))

        batch_y = self.y[list(sorted(used_indexes))]
        idx = []
        for i in range(0, len(is_horiz_flipped), 1):
            if batch_y.shape[1] == 1:
                if (is_horiz_flipped[i]):
                    batch_y[i] *= -1
                    
                if (np.isclose(batch_y[i], 0)):
                    if (np.random.uniform(low=0, high=1) > self.zero_drop_percentage):
                        idx.append(True)
                    else:
                        idx.append(False)
                else:
                    idx.append(True)
            else:
                if (batch_y[i][int(len(batch_y[i])/2)] == 1):
                    if (np.random.uniform(low=0, high=1) > self.zero_drop_percentage):
                        idx.append(True)
                    else:
                        idx.append(False)
                else:
                    idx.append(True)
                
                if (is_horiz_flipped[i]):
                    batch_y[i] = batch_y[i][::-1]

        batch_y = batch_y[idx]
        batch_x[0] = batch_x[0][idx]
        batch_x[1] = batch_x[1][idx]
        
        return batch_x, batch_y
Ejemplo n.º 51
0
validation_imgs = np.array(validation_imgs)
validation_labels = [
    fn.split('\\')[1].split('.')[0].strip() for fn in validation_files
]

print('Train dataset shape:', train_imgs.shape, '\tValidation dataset shape:',
      validation_imgs.shape)

# Scale each image with pixel values between (0, 255) to values between (0, 1)
train_imgs_scaled = train_imgs.astype('float32')
validation_imgs_scaled = validation_imgs.astype('float32')
train_imgs_scaled /= 255
validation_imgs_scaled /= 255

print(train_imgs[0].shape)
array_to_img(train_imgs[0])

# Set up basic configuration parameters and encode our text class labels into numeric values

batch_size = 30
num_classes = 2
epochs = 30
input_shape = (150, 150, 3)

# encode text category labels
from sklearn.preprocessing import LabelEncoder

le = LabelEncoder()
le.fit(train_labels)
train_labels_enc = le.transform(train_labels)
validation_labels_enc = le.transform(validation_labels)
Ejemplo n.º 52
0
def preprocess_image(img):
    """Module to preprocess an image
    """
    img = array_to_img(img, scale=False).resize((224, 224))
    img = img_to_array(img)
    return img
Ejemplo n.º 53
0
def run(TrackNet_input, TrackNet_output):
    load_weights = './preprocessing/Data/TrackNetModel/TrackNet_weight_0906.h5'
    print(TrackNet_input)
    sigma = 5
    mag = 1

    f = open(TrackNet_output, 'w')
    f.write('Frame,Visibility,X,Y,Time\n')

    model = load_model(load_weights, custom_objects={'custom_loss':custom_loss})

    cap = cv2.VideoCapture(TrackNet_input)

    success, image1 = cap.read()
    success, image2 = cap.read()
    success, image3 = cap.read()

    ratio = int(image1.shape[0] / 360)

    count = 3

    while success:
        unit = []
        #adjust BGR format (cv2) to RGB format (PIL)
        x1 = image1[...,::-1]
        x2 = image2[...,::-1]
        x3 = image3[...,::-1]
        #convert np arrays to PIL images
        x1 = array_to_img(x1)
        x2 = array_to_img(x2)
        x3 = array_to_img(x3)
        #resize the images
        x1 = x1.resize(size = (WIDTH, HEIGHT))
        x2 = x2.resize(size = (WIDTH, HEIGHT))
        x3 = x3.resize(size = (WIDTH, HEIGHT))
        #convert images to np arrays and adjust to channels first
        x1 = np.moveaxis(img_to_array(x1), -1, 0)		
        x2 = np.moveaxis(img_to_array(x2), -1, 0)		
        x3 = np.moveaxis(img_to_array(x3), -1, 0)
        #create data
        unit.append(x1[0])
        unit.append(x1[1])
        unit.append(x1[2])
        unit.append(x2[0])
        unit.append(x2[1])
        unit.append(x2[2])
        unit.append(x3[0])
        unit.append(x3[1])
        unit.append(x3[2])
        unit=np.asarray(unit)	
        unit = unit.reshape((1, 9, HEIGHT, WIDTH))
        unit = unit.astype('float32')
        unit /= 255
        y_raw = model.predict(unit, batch_size=BATCH_SIZE)
        y_pred = adjustPredHeatMaps(y_raw, sigma, mag)
        time = custom_time(cap.get(cv2.CAP_PROP_POS_MSEC))
        if np.amax(y_pred[0]) <= 0:
            f.write(str(count)+',0,0,0,'+time+'\n')
        else:	
            pos_pred = np.unravel_index(np.argmax(y_pred[0], axis=None), y_pred[0].shape)
            f.write(str(count)+',1,'+str(pos_pred[1]*ratio)+','+str(pos_pred[0]*ratio)+','+time+'\n')
        image1 = image2
        image2 = image3
        success, image3 = cap.read()
        count += 1

    f.close()