示例#1
0
    def parser_tfrecord(record):
        parsed = tf.parse_single_example(record,
                                         features={
                                             'image/encoded':
                                             tf.FixedLenFeature(
                                                 (),
                                                 tf.string,
                                                 default_value=''),
                                             'image/height':
                                             tf.FixedLenFeature(
                                                 (), tf.int64,
                                                 default_value=0),
                                             'image/width':
                                             tf.FixedLenFeature(
                                                 (), tf.int64,
                                                 default_value=0),
                                             'label/value':
                                             tf.VarLenFeature(tf.string),
                                         })

        # img = tf.cast(tf.image.decode_jpeg(parsed['image/encoded'], channels=1), tf.float32) # 保存的时候先按jpeg编码
        img = tf.decode_raw(parsed['image/encoded'], tf.float32)  #直接采用bytes编码
        height = tf.cast(parsed['image/height'], tf.int32)
        width = tf.cast(parsed['image/width'], tf.int32)
        img = tf.reshape(img, (height, width, 1))
        img = tf.clip_by_value((img + 0.5), 0, 1.) * 255.
        img = normalize_input_img(img)
        if augment: img = Augment.augment(img)
        label = tf.sparse_tensor_to_dense(parsed['label/value'],
                                          default_value='')

        return img, label
示例#2
0
    def test_resize(self):
        """
		Tests that the resize augmentation has not changed
		"""
        img = cv2.imread("test_data/augmented/org.png", 3)
        img = aug.resize(img, (50, 50))

        np.testing.assert_array_equal(img.shape, (50, 50, 3))
示例#3
0
    def _get_batch(self):
        self._batch = self.rec.next()
        if not self._batch:
            return False
        # modify label
        label = self._batch.label[0].asnumpy()

        for i in range(len(label)):
            #if i==0:
            #print(label[i])
            for j in range(len(label[i])):
                if label[i][j] == 78:
                    label[i][j] = 0
                else:
                    label[i][j] += 1
        self._batch.label = [mx.nd.array(label)]

        buf = self._batch.data[0].asnumpy()
        data = np.zeros((self.batch_size, self.data_shape[0],
                         self.data_shape[1], self.data_shape[2]))

        for i in range(len(buf)):
            im = buf[i]  #
            augment_type = random.randint(0, 2)
            ori_im = im.transpose(1, 2, 0).astype(np.uint8)
            im = cv2.imread("/opt/data/plate/im.jpg")
            # if augment_type == 0:
            # im_Distort = Augment.GenerateDistort(im, 12)
            # cv2.imwrite("/opt/data/plate/im_Distort.jpg", im_Distort)
            # if augment_type == 1:
            im_Stretch = Augment.GenerateStretch(im, 12)
            cv2.imwrite("/opt/data/plate/im_Stretch.jpg", im_Stretch)
            # if augment_type == 2:
            im_Perspective = Augment.GeneratePerspective(im)
            cv2.imwrite("/opt/data/plate/im_Perspective.jpg", im_Perspective)
            data[i] = im.transpose((0, 2, 1))
            # cv2.imwrite("/opt/data/plate/im.jpg", im.transpose((1,2,0)))
            '''
            if i==0:
                print(data[i].shape)
                #cv2.imshow("e",data[i])
                cv2.imwrite("res3.bmp",data[i].transpose((1,2,0)))
                cv2.waitKey(0)'''
        self._batch.data = [mx.nd.array(data)]

        return True
示例#4
0
    def test_sharpen(self):
        """
		Tests that the sharpen augmentation has not changed
		"""
        img = cv2.imread("test_data/augmented/org.png", 3)
        img = aug.sharpen(img)

        load = cv2.imread("test_data/augmented/sharpen.png", 3)

        np.testing.assert_array_equal(img, load)
示例#5
0
    def test_bright_blur(self):
        """
		Tests that the bright blur augmentation has not changed
		"""
        img = cv2.imread("test_data/augmented/org.png", 3)
        img = aug.add_light_and_color_blur(img)

        load = cv2.imread("test_data/augmented/bright_blur.png", 3)

        np.testing.assert_array_equal(img, load)
示例#6
0
    def test_mirror(self):
        """
		Tests that the mirror augmentation has not changed
		"""
        img = cv2.imread("test_data/augmented/org.png", 3)
        img = aug.mirror(img)

        load = cv2.imread("test_data/augmented/mirror.png", 3)

        np.testing.assert_array_equal(img, load)
示例#7
0
    def test_crop(self):
        """
		Tests that the crop augmentation has not changed
		"""
        img = cv2.imread("test_data/augmented/org.png", 3)
        img = aug.crop(img, 1, 0, 0, 0)

        load = cv2.imread("test_data/augmented/crop.png", 3)

        np.testing.assert_array_equal(img, load)
示例#8
0
    def test_rotate(self):
        """
		Tests that the rotate augmentation has not changed
		"""
        img = cv2.imread("test_data/augmented/org.png", 3)
        img = aug.rotate(img, -2)

        load = cv2.imread("test_data/augmented/rotate.png", 3)

        np.testing.assert_array_equal(img, load)
示例#9
0
    def test_darken(self):
        """
		Tests that the darken augmentation has not changed
		"""
        img = cv2.imread("test_data/augmented/org.png", 3)
        img = aug.brighten_darken(img, 0.6)

        load = cv2.imread("test_data/augmented/darken.png", 3)

        np.testing.assert_array_equal(img, load)
示例#10
0
    def test_affine(self):
        """
		Tests that the affine transformation augmentation has not changed
		"""
        img = cv2.imread("test_data/augmented/org.png", 3)
        img = aug.affine_transform(img)

        load = cv2.imread("test_data/augmented/affine.png", 3)

        np.testing.assert_array_equal(img, load)
    def generate_line(self):
        if self.training:
            idx = np.random.randint(self.len)
            image = self.image[idx]
            label = self.label[idx]
        else:
            idx = self.idx
            image = self.image[idx]
            label = self.label[idx]
            self.idx += 1
        if self.idx == self.len:
            self.idx -= self.len

        h, w = image.shape
        imageN = np.ones((self.conH, self.conW)) * 255
        beginH = int(abs(self.conH - h) / 2)
        beginW = int(abs(self.conW - w) / 2)
        if h <= self.conH and w <= self.conW:
            imageN[beginH:beginH + h, beginW:beginW + w] = image
        elif float(h) / w > float(self.conH) / self.conW:
            newW = int(w * self.conH / float(h))
            beginW = int(abs(self.conW - newW) / 2)
            image = cv2.resize(image, (newW, self.conH))
            imageN[:, beginW:beginW + newW] = image
        elif float(h) / w <= float(self.conH) / self.conW:
            newH = int(h * self.conW / float(w))
            beginH = int(abs(self.conH - newH) / 2)
            image = cv2.resize(image, (self.conW, newH))
            imageN[beginH:beginH + newH] = image
        label = self.label[idx]

        if self.augment and self.training:
            imageN = imageN.astype('uint8')
            if torch.rand(1) < 0.3:
                imageN = Augment.GenerateDistort(imageN, random.randint(3, 8))
            if torch.rand(1) < 0.3:
                imageN = Augment.GenerateStretch(imageN, random.randint(3, 8))
            if torch.rand(1) < 0.3:
                imageN = Augment.GeneratePerspective(imageN)

        imageN = imageN.astype('float32')
        imageN = (imageN - 127.5) / 127.5
        return imageN, label
示例#12
0
def create_library(img, path_t, lib_dir, w, Ovr, f, aug):

    fn = get_name(img)

    ### Build subfolders
    if os.path.isdir(lib_dir) is False:
        print('\nCreating training library folders at: ' + lib_dir)
        os.makedirs(lib_dir)
        os.makedirs(lib_dir + '/pics')
        os.makedirs(lib_dir + '/masks')
    pics_dir = lib_dir + '/pics'
    masks_dir = lib_dir + '/masks'

    ### Split mosic into tiles
    print('Splitting image: %s...' % fn)
    with suppress_stdout():  ### suppress the long output
        Split.split_image(input=img,
                          output_dir=pics_dir,
                          patch_w=w,
                          patch_h=w,
                          adj_overlay_x=Ovr,
                          adj_overlay_y=Ovr,
                          out_format=f)
    os.remove('split_image_info.txt')

    truths = gpd.read_file(path_t)
    crs = truths.crs
    # print('\nCascading truths for analysis...')
    truths = gpd.GeoSeries(cascaded_union(truths['geometry']))
    truths = gpd.GeoDataFrame(geometry=truths, crs=crs)

    ### Remove bad tiles from library (usually edge tiles) & Re-number
    Filter.remove(pics_dir, truths)

    ### Create ground truth masks for tiles. Remove non-overlapping tiles
    Masks.create_masks(path_t, lib_dir, pics_dir, masks_dir, img)

    ### Augment tile-mask pairs to grow library
    Augment.augment_images(lib_dir, aug)

    print('\nLibrary build successful\n\n')

    return
示例#13
0
    def test_that_wrong_augment_swtich_paramaters_errors(self):
        """
		Tests that the augment switch returns -1 if the inputs are wrong
		"""
        img = np.array([[1, 1, 1], [2, 2, 2], [3, 3, 3]])
        test = aug.augment_switch(img,
                                  "test_data/tmp/",
                                  "3x3",
                                  1,
                                  3,
                                  print_errors=False)
        assert test == -1
示例#14
0
    def test_augmented_not_same_as_original(self):
        """
		Tests that the augmented images are not the same as the original
		"""
        load = cv2.imread("test_data/augmented/org.png", 3)
        org = cv2.imread("test_data/augmented/org.png", 3)

        #make sure my method of testing is valid
        img = org
        assert np.ndarray.tolist(img) == np.ndarray.tolist(load)

        img = aug.affine_transform(org)
        assert np.ndarray.tolist(img) != np.ndarray.tolist(load)

        img = aug.add_light_and_color_blur(img)
        assert np.ndarray.tolist(img) != np.ndarray.tolist(load)

        img = aug.affine_transform(org)
        assert np.ndarray.tolist(img) != np.ndarray.tolist(load)

        img = aug.brighten_darken(org, 0.6)
        assert np.ndarray.tolist(img) != np.ndarray.tolist(load)

        img = aug.brighten_darken(org, 1.4)
        assert np.ndarray.tolist(img) != np.ndarray.tolist(load)

        img = aug.blur(org)
        assert np.ndarray.tolist(img) != np.ndarray.tolist(load)

        img = aug.sharpen(org)
        assert np.ndarray.tolist(img) != np.ndarray.tolist(load)

        img = aug.rotate(org, -2)
        assert np.ndarray.tolist(img) != np.ndarray.tolist(load)

        img = aug.crop(org, 1, 0, 0, 0)
        assert np.ndarray.tolist(img) != np.ndarray.tolist(load)

        img = aug.mirror(org)
        assert np.ndarray.tolist(img) != np.ndarray.tolist(load)
    def word_generate(self):

        endW = np.random.randint(50)
        label = ''
        imageN = np.ones((self.conH, self.conW)) * 255
        imageList = []
        while True:
            idx = np.random.randint(self.len)
            image = self.image[idx]
            h, w = image.shape
            beginH = int(abs(self.conH - h) / 2)
            imageList.append(image)
            if endW + w > self.conW:
                break
            if h <= self.conH:
                imageN[beginH:beginH + h, endW:endW + w] = image
            else:
                imageN[:, endW:endW + w] = image[beginH:beginH + self.conH]

            endW += np.random.randint(60) + 20 + w
            if label == '':
                label = self.label[idx]
            else:
                label = label + '|' + self.label[idx]

        label = label
        imageN = imageN.astype('uint8')
        if self.augment:
            if torch.rand(1) < 0.3:
                imageN = Augment.GenerateDistort(imageN, random.randint(3, 8))
            if torch.rand(1) < 0.3:
                imageN = Augment.GenerateStretch(imageN, random.randint(3, 8))
            if torch.rand(1) < 0.3:
                imageN = Augment.GeneratePerspective(imageN)

        imageN = imageN.astype('float32')
        imageN = (imageN - 127.5) / 127.5
        return imageN, label
def initiate():
    """
	Function to set all the variables for the classifier to run
	:return: all the variables created.
	"""
    print("Preparing data")
    if augment:
        aug.prepare_data(train_data_directory, image_shape, num_channels,
                         num_augment)
        aug.re_iterate()
        aug.prepare_data(test_data_directory, image_shape, num_channels,
                         num_augment)
        aug.re_iterate()
        # aug.prepare_data(validation_data_directory, image_shape, num_channels, num_augment)
        # aug.re_iterate()

    print("Loading data")
    data.train.images, data.train.labels = load_data(train_data_directory,
                                                     image_size_flat,
                                                     num_channels)
    data.test.images, data.test.labels = load_data(test_data_directory,
                                                   image_size_flat,
                                                   num_channels)
    # data.validation.images, data.validation.labels = load_data(validation_data_directory,
    #                                                            image_size_flat, num_channels)

    print("Initiating data")
    data.train = data.train.init(classes)
    data.test = data.test.init(classes)
    # data.validation = data.validation.init(classes)

    data.train._name = "train"
    data.test._name = "test"
    # data.validation._name = "validation"

    # The labels without one hot encoding
    data.test.cls = np.argmax(data.test.labels, axis=1)

    print(
        "Creating TF placeholder objects- and variables, fully connected layers and convolutional layers"
    )
    # this creates the tf placeholder object for the images.
    if num_channels == 3:
        x = tf.placeholder(tf.float32,
                           shape=[None, image_size_flat, num_channels],
                           name='x')
    else:
        x = tf.placeholder(tf.float32, shape=[None, image_size_flat], name='x')

    # A 4 dimentional tensor is needed for the convolutional layers, and so we have to reshape it to be
    # [-1, image_size, image_size, num_channels]
    # where -1 is the amount of images, and is inferred automatically by tensorflow, image_size which is the size of the
    # images, where this requires height=width, and num channels which is the color channels
    x_image = tf.reshape(x, [-1, image_size, image_size, num_channels])

    # This is the TF placeholder object for the labels and is related to the x placeholder
    y_true = tf.placeholder(tf.float32,
                            shape=[None, num_classes],
                            name='y_true')

    # This is the true labels, in a tensorflow variable
    y_true_cls = tf.argmax(y_true, axis=1)

    # Here the first convolutional layer is created
    layer_conv1, weights_conv1 = new_conv_layer(
        input=x_image,
        num_input_channels=num_channels,
        filter_size=filter_size1,
        num_filters=num_filters1,
        use_pooling=True)

    # Here the second convolutional layer is created
    layer_conv2, weights_conv2 = new_conv_layer(
        input=layer_conv1,
        num_input_channels=num_filters1,
        filter_size=filter_size2,
        num_filters=num_filters2,
        use_pooling=True)

    # This flattens the second convolutional layer
    layer_flat, num_features = flatten_layer(layer_conv2)

    # This creates the first fully connected layer, using ReLU
    layer_fc1 = new_fc_layer(input=layer_flat,
                             num_inputs=num_features,
                             num_outputs=fc_size,
                             use_relu=True)

    # This creates the first fully connected layer, not using ReLU
    layer_fc2 = new_fc_layer(input=layer_fc1,
                             num_inputs=fc_size,
                             num_outputs=num_classes,
                             use_relu=False)

    # The prediction of the all classes, normalized, if you have 70% certainty for class_one and 30% for class_two,
    # this will be something like [0.7, 0.3]
    y_pred = tf.nn.softmax(layer_fc2)

    # Using the y_pred variable, you take the max value, and saves it as one, and everything else as 0, so in previous
    # example of [0.7, 0.3], this would become [1, 0]
    y_pred_cls = tf.argmax(y_pred, axis=1)

    # this is used with back propagation to increase the accuracy of the network
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(
        logits=layer_fc2, labels=y_true)

    # the average of the cross-entropy for all the image classifications.
    cost = tf.reduce_mean(cross_entropy)

    # creates an optimizer for later use using the adam optimizer
    optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)

    #
    correct_prediction = tf.equal(y_pred_cls, y_true_cls)

    # a vector of booleans whether the predicted class equals the true class of each image.
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # this creates a saver, so the model can later be saved
    saver = tf.train.Saver()

    return data, x, x_image, y_true, y_true_cls, layer_conv1, layer_conv2, weights_conv1, weights_conv2, layer_flat, \
           num_features, layer_fc1, layer_fc1, layer_fc2, y_pred, y_pred_cls, cost, optimizer, correct_prediction, \
           accuracy, saver
示例#17
0
    def test_iterate(self):
        """
		Tests that the iteration and re-iteration functions works as expected
		"""
        aug.re_iterate()
        aug.iterate()
        aug.iterate()
        aug.iterate()
        aug.iterate()
        aug.iterate()
        aug.iterate()
        self.assertEqual(aug.iterate(), 6)
        aug.re_iterate()
        aug.iterate()
        self.assertEqual(aug.iterate(), 1)
        self.assertEqual(aug.iterate(6, True), 7)
import Augment
import cv2
import numpy as np
'''
The code is for OpenCV format.
If your data format is PIL.Image, please convert the format by:

import numpy as np
import cv2
from PIL import Image

img = Image.open("The Path to the image")
img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
'''

im = cv2.imread("pic/demo.png")
im = cv2.resize(im, (200, 64))
cv2.imshow("im_CV", im)
for i in range(5000):
    im_Distort = Augment.GenerateDistort(im, 4)
    cv2.imshow("im_Distort", im_Distort)
    im_Stretch = Augment.GenerateStretch(im, 4)
    cv2.imshow("im_Stretch", im_Stretch)
    im_Perspective = Augment.GeneratePerspective(im)
    cv2.imshow("im_Perspective", im_Perspective)
    cv2.waitKey(1)
    def generate_line(self):
        #  random select in training dataset, and order in testing dataset
        if self.is_train:
            idx = int(torch.randint(high = self.len,size = (1,)).item())
        else:
            idx = self.testidx
            self.testidx += 1
            if self.testidx == self.len:
                self.testidx = 0

        path = self.path[idx]
        image = self.image[idx]
        if BINARY:
            if KEEP_RATIO:
                h,w = image.shape
                imageN = np.zeros((self.conH,self.conW))
                if self.is_train:
                    beginH = random.randint(0, int(abs(self.conH-h)))
                    beginW = random.randint(0, int(abs(self.conW-w)))
                else:
                    beginH = int(abs(self.conH-h)/2)
                    beginW = int(abs(self.conW-w)/2)
                if h <= self.conH and w <= self.conW:
                    imageN[beginH:beginH+h, beginW:beginW+w] = image
                elif float(h) / w > float(self.conH) / self.conW:
                    newW = int(w * self.conH / float(h))
                    if self.is_train:
                        beginW = random.randint(0, int(abs(self.conW-w)))
                    beginW = int(abs(self.conW-newW)/2)
                    image = cv2.resize(image, (newW, self.conH))
                    imageN[:,beginW:beginW+newW] = image
                elif float(h) / w <= float(self.conH) / self.conW:
                    newH = int(h * self.conW / float(w))
                    if self.is_train:
                        beginW = random.randint(0, int(abs(self.conW-w)))
                    beginH = int(abs(self.conH-newH)/2)
                    image = cv2.resize(image, (self.conW, newH))
                    imageN[beginH:beginH+newH,:] = image
                else:
                    print("????????????????????????")
            else:
                image = cv2.resize(image, (self.conW, self.conH))
        else:
            if KEEP_RATIO:
                h,w,_ = image.shape
                imageN = np.zeros((self.conH,self.conW,3))
                if self.is_train:
                    beginH = random.randint(0, int(abs(self.conH-h)))
                    beginW = random.randint(0, int(abs(self.conW-w)))
                else:
                    beginH = int(abs(self.conH-h)/2)
                    beginW = int(abs(self.conW-w)/2)
                if h <= self.conH and w <= self.conW:
                    imageN[beginH:beginH+h, beginW:beginW+w, :] = image
                elif float(h) / w > float(self.conH) / self.conW:
                    newW = int(w * self.conH / float(h))
                    if self.is_train:
                        beginW = random.randint(0, int(abs(self.conW-w)))
                    beginW = int(abs(self.conW-newW)/2)
                    image = cv2.resize(image, (newW, self.conH))
                    imageN[:,beginW:beginW+newW,:] = image
                elif float(h) / w <= float(self.conH) / self.conW:
                    newH = int(h * self.conW / float(w))
                    if self.is_train:
                        beginW = random.randint(0, int(abs(self.conW-w)))
                    beginH = int(abs(self.conH-newH)/2)
                    image = cv2.resize(image, (self.conW, newH))
                    imageN[beginH:beginH+newH,:,:] = image
                else:
                    print("????????????????????????")
            else:
                image = cv2.resize(image, (self.conW, self.conH))

        label = self.label[idx]

        if AUG:
            if self.is_train:
                # parts = 4
                parts = random.randint(2, 6)
                imageN = imageN.astype('uint8')
                if torch.rand(1) < AUG_RATIO:
                    imageN = Augment.GenerateDistort(imageN, parts)
                if torch.rand(1) < AUG_RATIO:
                    imageN = Augment.GenerateStretch(imageN, parts)
                if torch.rand(1) < AUG_RATIO:
                    imageN = Augment.GeneratePerspective(imageN)
        if MORE_AUG and self.is_train:
            imageN = imageN.astype('uint8')
            if np.random.rand() < MORE_AUG_RATIO:
                imageN = rot(imageN, r(60) - 30, imageN.shape, 30);
            if np.random.rand() < MORE_AUG_RATIO:
                imageN = rotRandrom(imageN, 10, (imageN.shape[1], imageN.shape[0]));
            if np.random.rand() < MORE_AUG_RATIO:
                imageN = AddSmudginess(imageN, self.smu)
            if np.random.rand() < MORE_AUG_RATIO and not BINARY:
                imageN = tfactor(imageN)    
            if np.random.rand() < MORE_AUG_RATIO:
                imageN = AddGauss(imageN, 1 + r(2));
            imageN = cv2.resize(imageN, (self.conW, self.conH))
            if torch.rand(1) < 0.001 and self.is_train:
                img = cv2.cvtColor(imageN, cv2.COLOR_RGB2BGR) 
                cv2.imwrite("img_show_train/%s.jpg" % label, img)

        return imageN, label, path