Beispiel #1
0
    def predict(self, predict_data_loader, visualize=True, save_video=False):
        # Read params to check if any params have been changed by user
        self.params = read_config(self.cfg_path)
        #Set model to evaluation mode
        self.model.eval()

        if save_video:
            self.create_video_writer()

        with torch.no_grad():
            for j, images in enumerate(predict_data_loader):
                #Batch operation: depending on batch size more than one image can be processed.
                #Set images to self.device
                images = images.to(self.device)
                #Provide the images as input to the model and save the result in outputs variable.
                outputs = self.model(images)

                #print images.size()
                #for each image in batch
                for i in range(outputs.size(0)):
                    #print(i);
                    image = images[i] / 255
                    output = outputs[i]
                    overlay_img, prob_img = get_output_images(image, output)
                    plt.figure(figsize=(10, 8))
                    plt.subplot(1, 3, 1)
                    plt.imshow(overlay_img)
                    plt.subplot(1, 3, 2)
                    plt.imshow(prob_img.detach().cpu().numpy())
                    plt.subplot(1, 3, 3)
                    prob_img = prob_img > 0.5
                    plt.imshow(prob_img.detach().cpu().numpy())
                    plt.show()

                    #Get overlay image and probability map using function from utils.visualization

                    #Convert image, overlay image and probability image to numpy so that it can be visualized using matplotlib functions later. Use convert_tensor_to_numpy function from below.

                    #if save_video:
                    #Concatentate input and overlay image(along the width axis [axis=1]) to create video frame. Hint:Use concatenate function from numpy
                    #Write video frame

                    #if(visualize):
                    #display_output(image,prob_img,overlay_img)

            if save_video:
                self.writer.close()
Beispiel #2
0
    def predict(self,predict_data_loader,visualize=True,save_video=False):
        # Reads params to check if any params have been changed by user
        self.params = read_config(self.cfg_path)
        #evaluation mode
        self.model_p.eval()
               
        if save_video:
            self.writer = self.create_video_writer()
            
        with torch.no_grad():
            for j, images in enumerate(predict_data_loader):
                #Batch operation: depending on batch size more than one image can be processed.
                images = images.float()                
                images = images.to(self.device)

                outputs = self.model_p(images)
                               
            #for each image in batch
                for i in range(outputs.size(0)):
                    image=images[i]/255
                    output=outputs[i]
                    output = torch.unsqueeze(output, 0)
                    overlay_image, prob_image = get_output_images(image,output)

                    #Converts image, overlay image and probability image to numpy so that it can be visualized using matplotlib functions later.
                    image = self.convert_tensor_to_numpy(image)
                    overlay_image_np = self.convert_tensor_to_numpy(overlay_image)
                    prob_image_np = self.convert_tensor_to_numpy(prob_image)

                    if save_video:
                        #Concatentates input and overlay image(along the width axis [axis=1]) to create video frame.
                        video_frame = np.concatenate((image, overlay_image_np), axis=1)
                        video_frame= video_frame * 255
                        video_frame = video_frame.astype(np.uint8)
                        #Writes video frame
                        self.writer.writeFrame(video_frame)

                    if(visualize):
                        display_output(image, prob_image_np, overlay_image_np)

            if save_video:
                self.writer.close()
                return play_notebook_video(self.params['output_data_path'], self.params['trained_model_name'])
    def __init__(self,
                 dataset_name,
                 size,
                 cfg_path,
                 mode=Mode.TRAIN,
                 dataset_parent_path=DEFAULT_DATA_PATH,
                 augmentation=None,
                 seed=1):
        """
        Args:
            dataset_name (string): Folder name of the dataset.
            size (int):
                No of images to be used from the dataset folder
            mode (enumeration Mode):
                Nature of operation to be done with the data.
                Possibe inputs are Mode.PREDICt,Mode.TRAIN,Mode.TEST
                Default value: Mode.TRAIN
            dataset_parent_path (string):
                Path of the folder where the dataset folder is present
                Default: DEFAULT_DATA_PATH as per config.json
            cfg_path (string):
                Config file path of your experiment

            augmentation(Augmentation object):
                Augmentation to be applied on the dataset. Augmentation is passed using the object
                from Compose class (see augmentation.py)
            seed
                Seed used for random functions
                Default:1

        """
        params = read_config(cfg_path)
        self.cfg_path = params['cfg_path']

        self.detections_file_name = params['detections_file_name']

        self.mode = mode
        self.dataset_path = os.path.join(dataset_parent_path, dataset_name)
        self.datset_name = dataset_name
        self.size = size
        self.augmentation = augmentation
        #Initialize Database inorder to get image list. This is will stored in self.img_list
        self.img_list = img_list
Beispiel #4
0
 def __init__(self,cfg_path):
     self.params = read_config(cfg_path)
     self.cfg_path = cfg_path
     self.setup_cuda()
    def predict(self,predict_data_loader,visualize=True,save_video=False):
        # Read params to check if any params have been changed by user
        self.params = read_config(self.cfg_path)
        #Set model to evaluation mode
        self.model.eval();
        
        if save_video:
            self.create_video_writer()
            

        with torch.no_grad():
            for j, images in enumerate(predict_data_loader):
                #Batch operation: depending on batch size more than one image can be processed.
                #Set images to self.device
                images = images.to(self.device);
                #Provide the images as input to the model and save the result in outputs variable.
                outputs = self.model(images);
               
            #for each image in batch
                for i in range(outputs.size(0)):
                    image=images[i]/255
                    output=outputs[i]

                    
                    #Get overlay image and probability map using function from utils.visualization


                    #Convert image, overlay image and probability image to numpy so that it can be visualized using matplotlib functions later. Use convert_tensor_to_numpy function from below.



                    if save_video:
                        #Concatentate input and overlay image(along the width axis [axis=1]) to create video frame. Hint:Use concatenate function from numpy
                        #Write video frame


                    if(visualize):
                        display_output(image,prob_img,overlay_img)

            if save_video:
                self.writer.close()
                #Uncomment the below line and replace ??? with the appropriate filename
                #return play_notebook_video(self.params['output_data_path'],???)
            
            
    def create_video_writer(self):
        '''Initialize the video writer'''
        filename="outputvideo.webm"
        output_path=self.params['output_data_path']
        self.writer = skvideo.io.FFmpegWriter(os.path.join(output_path, filename).encode('ascii'),
                                              outputdict={'-vcodec':'libvpx-vp9','-r':'25','-pix_fmt':'yuv420p','-quality':'good','-speed':'2','-threads':'6'},
                                             )
       

    def convert_tensor_to_numpy(self,tensor_img):
        '''
        Convert the tensor image to a numpy image

        '''
        #torch has numpy function but it requires the device to be cpu
        np_img = img.transpose((1, 2, 0));

        # np_img image is now in  C X H X W
        # transpose the array to H x W x C
                

        return np_img
Beispiel #6
0
 def __init__(self,cfg_path):
     self.params = read_config(cfg_path)
Beispiel #7
0
class Img_dataset(Dataset):
    """
    Class representing Image Dataset
    This class is used to represent both Simulation datasets and true negative real world image datasets.
    The images are also returned in the HEIGHT and WIDTH obtained from the simulator config.
    Depending on the mode specified by the user, the Dataset returns labels for train and test modes.
    User also has the option of choosing smaller sample from a folder containg large number of images by setting the size 
    parameter    
    """
    DEFAULT_DATA_PATH = read_config('./config.json')['input_data_path']

    def __init__(self,
                 dataset_name,
                 size,
                 cfg_path,
                 mode=Mode.TRAIN,
                 dataset_parent_path=DEFAULT_DATA_PATH,
                 augmentation=None,
                 seed=1):
        """
        Args:
            dataset_name (string): Folder name of the dataset.
            size (int):
                No of images to be used from the dataset folder
            mode (enumeration Mode):
                Nature of operation to be done with the data.
                Possibe inputs are Mode.PREDICt,Mode.TRAIN,Mode.TEST
                Default value: Mode.TRAIN
            dataset_parent_path (string):
                Path of the folder where the dataset folder is present
                Default: DEFAULT_DATA_PATH as per config.json
            cfg_path (string):
                Config file path of your experiment

            augmentation(Augmentation object):
                Augmentation to be applied on the dataset. Augmentation is passed using the object
                from Compose class (see augmentation.py)
            seed
                Seed used for random functions
                Default:1

        """
        params = read_config(cfg_path)
        self.cfg_path = params['cfg_path']
        self.detections_file_name = params['detections_file_name']
        self.mode = mode
        self.dataset_path = os.path.join(dataset_parent_path, dataset_name)
        self.datset_name = dataset_name
        self.size = size
        self.augmentation = augmentation
        self.dataset_parent_path = dataset_parent_path
        self.img_list = self._init_dataset(dataset_name, seed, params)

    def __len__(self):
        '''Returns length of the dataset'''
        return self.size

    def __getitem__(self, idx):
        '''
        Using self.img_list and the argument value idx, return images and labels(if applicable based on Mode)
        The images and labels are returned in torch tensor format.
        '''

        #Reads images using files name availble in self.img_list
        img = imread(self.img_list[idx])
        img = resize(img, (HEIGHT, WIDTH))

        #Conversion to ubyte value range (0...255) is done here, because network needs to be trained and needs to predict using the same datatype.
        img = img_as_ubyte(img)

        if self.mode == Mode.PREDICT:
            img = img.transpose((2, 0, 1))
            img = torch.from_numpy(img)
            return img

        #If mode is not PREDICT, Obtains binary label image
        else:
            label_point_cloud = read_detection(
                os.path.join(self.dataset_path, self.detections_file_name),
                os.path.basename(self.img_list[idx]))
            x = label_point_cloud[0]
            y = label_point_cloud[1]
            label = np.zeros((img.shape[0], img.shape[1]))
            label[x, y] = 1

            #Apply augmentation if applicable

            #Converts image and label to tensor and returns image,label
            img = img.transpose((2, 0, 1))
            img = torch.from_numpy(img)
            label = torch.from_numpy(label)
            return img, label

    def _init_dataset(self, dataset_name, seed, params):
        '''
        If the dataset is found , the size is checked against the number of images in the folder and
        if size is more than number of images in the folder, randomly pick images from the folder so
        that number of images is equal to the user specified size.

        If the dataset is not found, the simulator is run and a folder containing simulation
        images and detection files, is created with the given dataset name.

        Final image list is stored into self.img_list
        '''

        # Checks if the dataset directory exists
        if os.path.isdir(self.dataset_path):
            # If dataset directory found: Collects the file names of all images and stores them to a list
            image_list = os.listdir(self.dataset_path)
            img_list = []
            for item in image_list:
                if ((re.search(".png", str(item)))
                        or (re.search(".jpeg", str(item)))
                        or (re.search(".jpg", str(item)))):
                    img_list.append(os.path.join(self.dataset_path, item))

            if len(img_list) < self.size:
                print(
                    'The size you requested is more than the total available images.'
                )
                self.size = len(img_list)
                self.img_list = img_list

            elif len(img_list) > self.size:
                print(
                    'The size you requested is less than the total available images. The desired number of images randomly will be picked.'
                )
                random.seed(seed)
                random.shuffle(img_list)
                self.img_list = []
                self.img_list = img_list[:self.size]
            # would contain number of images as specified by user.

            elif len(img_list) == self.size:
                self.img_list = img_list

        # If dataset directory not found: Runs the simulator and obtain img list from simulation dataset.
        else:
            self.dataset_path = simulation_pipeline(params, self.size,
                                                    dataset_name, seed)
            image_list = os.listdir(self.dataset_path)
            self.img_list = []
            for item in image_list:
                if ((re.search(".png", str(item)))
                        or (re.search(".jpeg", str(item)))
                        or (re.search(".jpg", str(item)))):
                    self.img_list.append(os.path.join(self.dataset_path, item))

        # Checks if detection file is present in the folder and if it is not present creates a true
        # negative detection file using label_true_negatives function from pipelines.py
        #will be done from pipeline, through the point cloud.
        if not os.path.isfile(
                os.path.join(self.dataset_path, self.detections_file_name)):
            label_true_negatives(self.dataset_path, self.detections_file_name)

        # CODE FOR CONFIG FILE TO RECORD DATASETS USED
        # Saves the dataset information for writing to config file
        if self.mode == Mode.TRAIN:
            params = read_config(self.cfg_path)
            params['Network']['total_dataset_number'] += 1
            dataset_key = 'Traing_Dataset_' + str(
                params['Network']['total_dataset_number'])
            #If augmentation is applied
            if self.augmentation:
                augmenetation_applied = [
                    i.__class__.__name__
                    for i in self.augmentation.augmentation_list
                ]
            else:
                augmenetation_applied = None
            dataset_info = {
                'name': dataset_name,
                'path': self.dataset_path,
                'size': self.size,
                'augmenetation_applied': augmenetation_applied,
                'seed': seed
            }
            params[dataset_key] = dataset_info
            write_config(params, params['cfg_path'], sort_keys=True)
        return self.img_list
Beispiel #8
0
    def _init_dataset(self, dataset_name, seed, params):
        '''
        If the dataset is found , the size is checked against the number of images in the folder and
        if size is more than number of images in the folder, randomly pick images from the folder so
        that number of images is equal to the user specified size.

        If the dataset is not found, the simulator is run and a folder containing simulation
        images and detection files, is created with the given dataset name.

        Final image list is stored into self.img_list
        '''

        # Checks if the dataset directory exists
        if os.path.isdir(self.dataset_path):
            # If dataset directory found: Collects the file names of all images and stores them to a list
            image_list = os.listdir(self.dataset_path)
            img_list = []
            for item in image_list:
                if ((re.search(".png", str(item)))
                        or (re.search(".jpeg", str(item)))
                        or (re.search(".jpg", str(item)))):
                    img_list.append(os.path.join(self.dataset_path, item))

            if len(img_list) < self.size:
                print(
                    'The size you requested is more than the total available images.'
                )
                self.size = len(img_list)
                self.img_list = img_list

            elif len(img_list) > self.size:
                print(
                    'The size you requested is less than the total available images. The desired number of images randomly will be picked.'
                )
                random.seed(seed)
                random.shuffle(img_list)
                self.img_list = []
                self.img_list = img_list[:self.size]
            # would contain number of images as specified by user.

            elif len(img_list) == self.size:
                self.img_list = img_list

        # If dataset directory not found: Runs the simulator and obtain img list from simulation dataset.
        else:
            self.dataset_path = simulation_pipeline(params, self.size,
                                                    dataset_name, seed)
            image_list = os.listdir(self.dataset_path)
            self.img_list = []
            for item in image_list:
                if ((re.search(".png", str(item)))
                        or (re.search(".jpeg", str(item)))
                        or (re.search(".jpg", str(item)))):
                    self.img_list.append(os.path.join(self.dataset_path, item))

        # Checks if detection file is present in the folder and if it is not present creates a true
        # negative detection file using label_true_negatives function from pipelines.py
        #will be done from pipeline, through the point cloud.
        if not os.path.isfile(
                os.path.join(self.dataset_path, self.detections_file_name)):
            label_true_negatives(self.dataset_path, self.detections_file_name)

        # CODE FOR CONFIG FILE TO RECORD DATASETS USED
        # Saves the dataset information for writing to config file
        if self.mode == Mode.TRAIN:
            params = read_config(self.cfg_path)
            params['Network']['total_dataset_number'] += 1
            dataset_key = 'Traing_Dataset_' + str(
                params['Network']['total_dataset_number'])
            #If augmentation is applied
            if self.augmentation:
                augmenetation_applied = [
                    i.__class__.__name__
                    for i in self.augmentation.augmentation_list
                ]
            else:
                augmenetation_applied = None
            dataset_info = {
                'name': dataset_name,
                'path': self.dataset_path,
                'size': self.size,
                'augmenetation_applied': augmenetation_applied,
                'seed': seed
            }
            params[dataset_key] = dataset_info
            write_config(params, params['cfg_path'], sort_keys=True)
        return self.img_list
Beispiel #9
0
from torch.utils.data import Dataset
import torch
from specs import *
from serde import read_detection, read_config, write_config
from pipelines import simulation_pipeline
from Training import Mode
from pipelines import label_true_negatives
from config import sim_config
import random
import re

sc = sim_config()['simulator']

HEIGHT = sc['height']
WIDTH = sc['width']
DEFAULT_DATA_PATH = read_config('./config.json')['input_data_path']


class Img_dataset(Dataset):
    """
    Class representing Image Dataset
    This class is used to represent both Simulation datasets and true negative real world image datasets.
    The images are also returned in the HEIGHT and WIDTH obtained from the simulator config.
    Depending on the mode specified by the user, the Dataset returns labels for train and test modes.
    User also has the option of choosing smaller sample from a folder containg large number of images by setting the size 
    parameter    
    """
    DEFAULT_DATA_PATH = read_config('./config.json')['input_data_path']

    def __init__(self,
                 dataset_name,
Beispiel #10
0
 def __init__(self, cfg_path):
     self.params = read_config(cfg_path)
     self.cfg_path = cfg_path
     self.model_info = self.params['Network']
     self.model_info['seed'] = self.model_info['seed']
     self.setup_cuda()
class Img_dataset(Dataset):
    """
    Class representing Image Dataset
    This class is used to represent both Simulation datasets and true negative real world image datasets.
    The images are also returned in the HEIGHT and WIDTH obtained from the simulator config.
    Depending on the mode specified by the user, the Dataset return labels for train and test modes.
    User also has the option of choosing smaller sample from a folder containg large number of images by setting the size 
    parameter
    
    Refer: https://pytorch.org/tutorials/beginner/data_loading_tutorial.html
    """
    DEFAULT_DATA_PATH = read_config('./config.json')['input_data_path']

    def __init__(self,
                 dataset_name,
                 size,
                 cfg_path,
                 mode=Mode.TRAIN,
                 dataset_parent_path=DEFAULT_DATA_PATH,
                 augmentation=None,
                 seed=1):
        """
        Args:
            dataset_name (string): Folder name of the dataset.
            size (int):
                No of images to be used from the dataset folder
            mode (enumeration Mode):
                Nature of operation to be done with the data.
                Possibe inputs are Mode.PREDICt,Mode.TRAIN,Mode.TEST
                Default value: Mode.TRAIN
            dataset_parent_path (string):
                Path of the folder where the dataset folder is present
                Default: DEFAULT_DATA_PATH as per config.json
            cfg_path (string):
                Config file path of your experiment

            augmentation(Augmentation object):
                Augmentation to be applied on the dataset. Augmentation is passed using the object
                from Compose class (see augmentation.py)
            seed
                Seed used for random functions
                Default:1

        """
        params = read_config(cfg_path)
        self.cfg_path = params['cfg_path']

        self.detections_file_name = params['detections_file_name']

        self.mode = mode
        self.dataset_path = os.path.join(dataset_parent_path, dataset_name)
        self.datset_name = dataset_name
        self.size = size
        self.augmentation = augmentation
        #Initialize Database inorder to get image list. This is will stored in self.img_list
        self.img_list = img_list

    def __len__(self):
        '''Returns length of the dataset'''
        return self.size

    def __getitem__(self, idx):
        '''
        Using self.img_list and the argument value idx, return images and labels(if applicable based on Mode)
        The images and labels are returned in torch tensor format

        '''

        #Read images using files name availble in self.img_list

        #Resize image if required

        #If mode is PREDICT, convert image to tensor and return the image
        if self.mode == Mode.PREDICT:

            # Convert image to tensor and return image

            return img

        #If mode is not PREDICT,  and retun image ,label
        #Obtain label from the detection file

        #Apply augmentation if applicable

        #Convert image and label to tensor and returm image,label

        return img, label

    def _init_dataset(self, dataset_name, seed, params):
        '''
        Initialize Dataset: Get the list the list of images from the dataset folder.

        If the dataset is found , the size is checked against the number of images in the folder and
        if size is more than number of images in the folder, randomly pick images from the folder so
        that number of images is equal to the user specified size.

        If the dataset is not found, the simulator is run and a folder containing simulation
        images and detection files, is created with the given dataset name.

        Final image list is stored into self.img_list


        '''

        # Check if the dataset directory exists

        # If dataset directory found: Collect the file names of all images and store them to a list

        #Compare number of images in img_list with size provided by user and then assign img_list to self.img_list

        #If number of images < size: inform user about the availabe image count
        #and change value of self.size to number of images in img_list
        #assign img_list to self.img_list without changes

        # if number of images >size : inform user about the availabe image count
        # Randomly select images from img_list and  assign them into self.img_list such that self.img_list
        # would contain number of images as specified by user. (Use the seed specified by user for random function)

        # If number of images = size
        # assign img_list to self.img_list without changes

        # If dataset directory not found: Run the simulator and obtain img list from simulation dataset

        # Check if detection file is present in the folder and if it is not present create a true
        # negative detection file using label_true_negatives function from pipelines.py

        #DO NOT CHANGE: CODE FOR CONFIG FILE TO RECORD DATASETS USED
        #Save the dataset information for writing to config file
        if self.mode == Mode.TRAIN:
            params = read_config(self.cfg_path)
            params['Network']['total_dataset_number'] += 1
            dataset_key = 'Traing_Dataset_' + str(
                params['Network']['total_dataset_number'])
            #If augmentation is applied
            if self.augmentation:
                augmenetation_applied = [
                    i.__class__.__name__
                    for i in self.augmentation.augmentation_list
                ]
            else:
                augmenetation_applied = None
            dataset_info = {
                'name': dataset_name,
                'path': self.dataset_path,
                'size': self.size,
                'augmenetation_applied': augmenetation_applied,
                'seed': seed
            }
            params[dataset_key] = dataset_info
            write_config(params, params['cfg_path'], sort_keys=True)
    def _init_dataset(self, dataset_name, seed, params):
        '''
        Initialize Dataset: Get the list the list of images from the dataset folder.

        If the dataset is found , the size is checked against the number of images in the folder and
        if size is more than number of images in the folder, randomly pick images from the folder so
        that number of images is equal to the user specified size.

        If the dataset is not found, the simulator is run and a folder containing simulation
        images and detection files, is created with the given dataset name.

        Final image list is stored into self.img_list


        '''

        # Check if the dataset directory exists

        # If dataset directory found: Collect the file names of all images and store them to a list

        #Compare number of images in img_list with size provided by user and then assign img_list to self.img_list

        #If number of images < size: inform user about the availabe image count
        #and change value of self.size to number of images in img_list
        #assign img_list to self.img_list without changes

        # if number of images >size : inform user about the availabe image count
        # Randomly select images from img_list and  assign them into self.img_list such that self.img_list
        # would contain number of images as specified by user. (Use the seed specified by user for random function)

        # If number of images = size
        # assign img_list to self.img_list without changes

        # If dataset directory not found: Run the simulator and obtain img list from simulation dataset

        # Check if detection file is present in the folder and if it is not present create a true
        # negative detection file using label_true_negatives function from pipelines.py

        #DO NOT CHANGE: CODE FOR CONFIG FILE TO RECORD DATASETS USED
        #Save the dataset information for writing to config file
        if self.mode == Mode.TRAIN:
            params = read_config(self.cfg_path)
            params['Network']['total_dataset_number'] += 1
            dataset_key = 'Traing_Dataset_' + str(
                params['Network']['total_dataset_number'])
            #If augmentation is applied
            if self.augmentation:
                augmenetation_applied = [
                    i.__class__.__name__
                    for i in self.augmentation.augmentation_list
                ]
            else:
                augmenetation_applied = None
            dataset_info = {
                'name': dataset_name,
                'path': self.dataset_path,
                'size': self.size,
                'augmenetation_applied': augmenetation_applied,
                'seed': seed
            }
            params[dataset_key] = dataset_info
            write_config(params, params['cfg_path'], sort_keys=True)
    def _init_dataset(self, dataset_name, seed, params):
        '''
        Initialize Dataset: Get the list the list of images from the dataset.

        If the dataset is found , the size is checked against the number of images in the folder and
        if size is more than number of images in the folder, randomly pick images from the folder so
        that number of images is equal to the user specified size.

        If the dataset is not found, the simulator is run and a folder containing simulation
        images and detection files, is created with the given dataset name.

        Final image list is stored into self.img_list


        '''

        # Check if the directory exists
        if os.path.isdir(self.dataset_path):
            # Collect the file names of all images and store them to a list
            img_list = [
                name for name in os.listdir(self.dataset_path)
                if name.endswith(('.jpg', '.jpeg', '.png'))
            ]
            image_count_in_folder = len(img_list)
            #Compare number of images in img_list with size provided by user and then assign img_list to self.img_list

            #If number of images < size: inform user about the available image count
            #and change value of self.size to number of images in img_list
            #assign img_list to self.img_list without changes
            if image_count_in_folder < self.size:
                print(
                    '{0}\nImages availabe in folder: \t{1}\nGiven Size: \t\t\t{2}\n'
                    'Images used in dataset:  \t{1}\n\n\n'.format(
                        dataset_name, image_count_in_folder, self.size))
                self.size = len(img_list)
                self.img_list = img_list

            # if number of images >size : inform user about the availabe image count
            #Randomly select images from img_list and  assign them into self.img_list such that self.img_list
            # would contain number of images as specified by user. (Use the seed specified by user for random function)

            elif image_count_in_folder > self.size:
                np.random.seed(seed)
                idx = np.random.choice(image_count_in_folder, self.size)
                self.img_list = [img_list[i] for i in idx]

                print(
                    '{0}\nImages availabe in folder: \t{1}\nGiven Size: \t\t\t{2} \n'
                    'Images used in dataset:  \t{2}\n\n\n'.format(
                        dataset_name, image_count_in_folder, self.size))

                # If number of images = size
                # assign img_list to self.img_list without changes
            else:
                self.img_list = img_list

        else:

            # Run the simulator and save the results in the dataset path
            simulation_pipeline(params, self.size, dataset_name, seed)
            self.img_list = [
                name for name in os.listdir(self.dataset_path)
                if name.endswith(('.jpg', '.jpeg', '.png'))
            ]

        #Check if detection file is present in the folder and if it is not present create a true negative(dummy) detection file using
        #label_true_negatives function from pipelines.py

        if not os.path.exists(
                os.path.join(self.dataset_path, self.detections_file_name)):

            label_true_negatives(self.dataset_path, self.detections_file_name)

        #Save the dataset information for writing to config file
        if self.mode == Mode.TRAIN:
            params = read_config(self.cfg_path)
            params['Network']['total_dataset_number'] += 1
            dataset_key = 'Traing_Dataset_' + str(
                params['Network']['total_dataset_number'])
            #If augmentation is applied
            if self.augmentation:
                augmenetation_applied = [
                    i.__class__.__name__
                    for i in self.augmentation.augmentation_list
                ]
            else:
                augmenetation_applied = None
            dataset_info = {
                'name': dataset_name,
                'path': self.dataset_path,
                'size': self.size,
                'augmenetation_applied': augmenetation_applied,
                'seed': seed
            }
            params[dataset_key] = dataset_info
            write_config(params, params['cfg_path'], sort_keys=True)
Beispiel #14
0
    def _init_dataset(self, dataset_name, seed, params):
        '''
        Initialize Dataset: Get the list the list of images from the dataset folder.

        If the dataset is found , the size is checked against the number of images in the folder and
        if size is more than number of images in the folder, randomly pick images from the folder so
        that number of images is equal to the user specified size.

        If the dataset is not found, the simulator is run and a folder containing simulation
        images and detection files, is created with the given dataset name.

        Final image list is stored into self.img_list


        '''
        dir_path = os.path.join(DEFAULT_DATA_PATH, dataset_name)
        random.seed(seed)
        # Check if the dataset directory exists
        if (os.path.isdir(dir_path)):
            # If dataset directory found: Collect the file names of all images and store them to a list
            im_list = [
                name for name in os.listdir(dir_path)
                if name.endswith(('.jpg', '.jpeg', '.png'))
            ]
            im_num = len(im_list)
            if (im_num < self.size):
                print("Lesser number of images found! Available: ", im_num)
                self.size = im_num
                self.img_list = im_list
                #Compare number of images in img_list with size provided by user and then assign img_list to self.img_list
                #If number of images < size: inform user about the availabe image count
                #and change value of self.size to number of images in img_list
                #assign img_list to self.img_list without changes
            if (im_num > self.size):
                # if number of images >size : inform user about the availabe image count
                # Randomly select images from img_list and  assign them into self.img_list such that self.img_list
                # would contain number of images as specified by user. (Use the seed specified by user for random function)
                print("More number of images available! Available: ", im_num)
                self.img_list = random.sample(im_list, self.size)

            if (im_num == self.size):
                self.img_list = im_list

            else:
                # If dataset directory not found: Run the simulator and obtain img list from simulation dataset
                simulation_pipeline(params, self.size, dataset_name, seed)
                im_list = os.listdir(dir_path)
                im_list = np.ravel(im_list)
                self.img_list = im_list

        dir_path1 = os.path.join(dir_path, self.detections_file_name)
        exs = os.path.isfile(dir_path1)
        if not exs:
            label_true_negatives(dir_path, self.detections_file_name)

        # Check if detection file is present in the folder and if it is not present create a true
        # negative detection file using label_true_negatives function from pipelines.py

        #DO NOT CHANGE: CODE FOR CONFIG FILE TO RECORD DATASETS USED
        #Save the dataset information for writing to config file
        if self.mode == Mode.TRAIN:
            params = read_config(self.cfg_path)
            params['Network']['total_dataset_number'] += 1
            dataset_key = 'Traing_Dataset_' + str(
                params['Network']['total_dataset_number'])
            #If augmentation is applied
            if self.augmentation:
                augmenetation_applied = [
                    i.__class__.__name__
                    for i in self.augmentation.augmentation_list
                ]
            else:
                augmenetation_applied = None
            dataset_info = {
                'name': dataset_name,
                'path': self.dataset_path,
                'size': self.size,
                'augmenetation_applied': augmenetation_applied,
                'seed': seed
            }
            params[dataset_key] = dataset_info
            write_config(params, params['cfg_path'], sort_keys=True)