コード例 #1
0
    def batch_yielder(self):
        """
        batch generator for the training data
        """
        while True:
            indexes = list(range(len(self.label_list)))
            shuffle(indexes)  #shuffling the indexes to have a variety of classes in a batch
            nbr_of_batches = len(indexes) // self.batch_size
            for i in range(nbr_of_batches):
                start = i * self.batch_size
                end = (i + 1) * self.batch_size

                batch = []
                outputs = []
                for j in range(start, end):
                    img_path = (
                        root
                        + "data/train_images/train_images/"
                        + self.label_list[indexes[j]][0]
                        + ".png"
                    )

                    img = cv2.imread(img_path)
                    if not self.mobile:
                        img = colour_to_grey(img)
                    mod_type =randint(0, 7) #randomly choose a transformation to apply to the image at hand
                    img=dataGenerator(img, mod_type, self.datagen)

                    batch.append(img)
                    outputs.append(np.array(self.label_list[indexes[j]][1]))
                batch = np.array(batch)
                outputs = np.array(outputs)
                yield (batch, outputs)

            # A last loop to get the last of the data
            batch = []
            outputs = []
            for j in range(nbr_of_batches * self.batch_size, len(indexes)):

                img_path = (
                    root
                    + "data/train_images/train_images/"
                    + self.label_list[indexes[j]][0]
                    + ".png"
                )
                img = cv2.imread(img_path)
                if not self.mobile:
                    img = colour_to_grey(img)
                mod_type =randint(0, 7) #randomly choose a transformation to apply to the image at hand
                img=dataGenerator(img, mod_type, self.datagen)                
                batch.append(img)
                outputs.append(np.array(self.label_list[indexes[j]][1]))
            batch = np.array(batch)
            outputs = np.array(outputs)
            yield (batch, outputs)
コード例 #2
0
    def validation_batch_yielder(self):
        """
        batch generator for validation data
        This function has practically the same structure as the test_generator only we don't shuffle the indexes nor do we augment the data
        """
        while True:
            indexes = list(range(len(self.validation_list)))
            nbr_of_batches = len(indexes) // self.batch_size
            for i in range(nbr_of_batches):
                start = i * self.batch_size
                end = (i + 1) * self.batch_size

                batch = []
                outputs = []
                for j in range(start, end):
                    img_path = (
                        root
                        + "data/train_images/train_images/"
                        + self.validation_list[j][0]
                        + ".png"
                    )

                    img = cv2.imread(img_path)
                    if not self.mobile:
                        img = colour_to_grey(img)
                    batch.append(img)
                    outputs.append(np.array(self.validation_list[j][1]))
                batch = np.array(batch)
                outputs = np.array(outputs)
                yield (batch, outputs)
            batch = []
            outputs = []
            for j in range(nbr_of_batches * self.batch_size, len(indexes)):

                img_path = (
                    root
                    + "data/train_images/train_images/"
                    + self.validation_list[j][0]
                    + ".png"
                )
                img = cv2.imread(img_path)
                if not self.mobile:
                    img = colour_to_grey(img)
                batch.append(img)
                outputs.append(np.array(self.validation_list[j][1]))
            batch = np.array(batch)
            outputs = np.array(outputs)
            yield (batch, outputs)
コード例 #3
0
def test_generator(test_data, network):
    for i in range(len(test_data)):
        img_path = (root + "data/train_images/train_images/" +
                    test_data[i][0] + ".png")
        img = cv2.imread(img_path)
        if network != "mobilenet":
            img = colour_to_grey(img)
        yield (np.array([img]))
コード例 #4
0
def generator(data, network, outputs_list):
    for i in range(len(data)):
        img_path = (root + "data/train_images/train_images/" + data[i][0] +
                    ".png")
        outputs_list.append([data[i][1]])
        img = cv2.imread(img_path)
        if network != "mobilenet":
            img = colour_to_grey(img)
        yield (np.array([img]))
    outputs_list = np.array(outputs_list)
コード例 #5
0
    def test_batch_yielder(self):
        """
        generator for test data
        """
        test_img_paths = os.listdir(root + "data/test_images/test_images")
        for j in range(len(test_img_paths)):

            batch = []
            img_path = root + "data/test_images/test_images/" + test_img_paths[j]
            img = cv2.imread(img_path)
            if not self.mobile:
                img = colour_to_grey(img)  # we have to turn it to grey img since tht is what our network was trained on
            batch.append(img)
            batch = np.array(batch)
            yield (batch)
コード例 #6
0
    def __getitem__(self, idx):
        start = idx * self.batch_size
        end = (idx + 1) * self.batch_size
        batch = []
        outputs = []
        nbr_pos = 0
        nbr_neg = 0
        for index in self.indexes[start:end]:
            img_path = (root + "data/train_images/train_images/" +
                        self.data[index][0] + ".png")
            img = cv2.imread(img_path)
            if self.network != "mobilenet":
                img = colour_to_grey(img)
            if self.data_type == "train":
                mod_type = randint(
                    0, 7
                )  #randomly choose a transformation to apply to the image at hand
                img = dataGenerator(img, mod_type, self.datagen)
            if self.data[index][1] == 1:
                nbr_pos += 1
            else:
                nbr_neg += 1
            batch.append(img)
            outputs.append(self.data[index][1])
        # #***try to balance batches*******
        # if self.data_type=="train":
        #     print(self.nbr_pos_in_train)
        #     for k in range (2*int(nbr_neg/max(1,nbr_pos))):
        #         position=randint(0,self.nbr_pos_in_train-1)
        #         outputs.append(self.data[index][1])
        #         img_path = (
        #                     root
        #                     + "data/train_images/train_images/"
        #                     + self.data[index][0]
        #                     + ".png"
        #                 )
        #         mod_type =randint(0, 7) #randomly choose a transformation to apply to the image at hand
        #         img=dataGenerator(img, mod_type, self.datagen)
        #         batch.append(img)
        # print (outputs)

        batch = np.array(batch)
        outputs = np.array(outputs)
        return (batch, outputs)