How to load images with multiple JSON annotation in PyTorchHow to return multiple values from a function?How does Python's super() work with multiple inheritance?How to make a class JSON serializableHow to overcome “datetime.datetime not JSON serializable”?How do I write JSON data to a file?How to prettyprint a JSON file?Pytorch: Can’t load images using ImageFolderImplementing a custom dataset with PyTorchload test data in pytorchhow to load images data into pytorch dataLoader?

Multiple options vs single option UI

How can I practically buy stocks?

Can I grease a crank spindle/bracket without disassembling the crank set?

Classification of surfaces

How exactly does Hawking radiation decrease the mass of black holes?

Can someone publish a story that happened to you?

Implications of cigar-shaped bodies having rings?

Is there really no use for MD5 anymore?

Is there a way to generate a list of distinct numbers such that no two subsets ever have an equal sum?

How to limit Drive Letters Windows assigns to new removable USB drives

What are the characteristics of a typeless programming language?

Can SQL Server create collisions in system generated constraint names?

How much cash can I safely carry into the USA and avoid civil forfeiture?

How to not starve gigantic beasts

Was there a Viking Exchange as well as a Columbian one?

How to pronounce 'c++' in Spanish

can anyone help me with this awful query plan?

Checks user level and limit the data before saving it to mongoDB

Pulling the rope with one hand is as heavy as with two hands?

"You've called the wrong number" or "You called the wrong number"

Map of water taps to fill bottles

What is causing the white spot to appear in some of my pictures

How come there are so many candidates for the 2020 Democratic party presidential nomination?

Check if a string is entirely made of the same substring



How to load images with multiple JSON annotation in PyTorch


How to return multiple values from a function?How does Python's super() work with multiple inheritance?How to make a class JSON serializableHow to overcome “datetime.datetime not JSON serializable”?How do I write JSON data to a file?How to prettyprint a JSON file?Pytorch: Can’t load images using ImageFolderImplementing a custom dataset with PyTorchload test data in pytorchhow to load images data into pytorch dataLoader?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I would like to know how I can use the data loader in PyTorch for the custom file structure of mine. I have gone through PyTorch documentation, but all those are with separate folders with class.



My folder structure consists of 2 folders(called training and validation), each with 2 subfolders(called images and json_annotations). Each image in the "images" folder has multiple objects(like cars, cycles, man etc) and each is annotated and have separate JSON files. Standard coco annotation is followed. My intention is to make a neural network which can do real-time classification from videos.



Sample image, there are around 2k images are there with 2k JSON annotations



corresponding JSON annotation file for the given image. Like this each image has separate JSON file



Edit 1:
I have done the coding as suggested by Fábio Perez.



class lDataSet(data.Dataset):
def __init__(self, path_to_imgs, path_to_json):
self.path_to_imgs = path_to_imgs
self.path_to_json = path_to_json
self.img_ids = os.listdir(path_to_imgs)

def __getitem__(self, idx):
img_id = self.img_ids[idx]
img_id = os.path.splitext(img_id)[0]
img = cv2.imread(os.path.join(self.path_to_imgs, img_id + ".jpg"))
load_json = json.load(open(os.path.join(self.path_to_json, img_id + ".json")))
#n = len(load_json)
#bboxes = load_json['annotation'][n]['segmentation']
return img, load_json

def __len__(self):
return len(self.image_ids)


When I try this



l_data = lDataSet(path_to_imgs = '/home/training/images', path_to_json = '/home/training/json_annotations')


I'm getting l_data with l_data[][0] - images and l_data with json. Now I'm confused. How will I use it with finetuning example availalbe in PyTorch? In that example, dataset and dataloader is done as shown below.
https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html



# Create training and validation datasets
image_datasets = x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x]) for x in ['train', 'val']
# Create training and validation dataloaders
dataloaders_dict = x: torch.utils.data.DataLoader(image_datasets[x], batch_size=batch_size, shuffle=True, num_workers=4) for x in ['train', 'val']









share|improve this question






























    0















    I would like to know how I can use the data loader in PyTorch for the custom file structure of mine. I have gone through PyTorch documentation, but all those are with separate folders with class.



    My folder structure consists of 2 folders(called training and validation), each with 2 subfolders(called images and json_annotations). Each image in the "images" folder has multiple objects(like cars, cycles, man etc) and each is annotated and have separate JSON files. Standard coco annotation is followed. My intention is to make a neural network which can do real-time classification from videos.



    Sample image, there are around 2k images are there with 2k JSON annotations



    corresponding JSON annotation file for the given image. Like this each image has separate JSON file



    Edit 1:
    I have done the coding as suggested by Fábio Perez.



    class lDataSet(data.Dataset):
    def __init__(self, path_to_imgs, path_to_json):
    self.path_to_imgs = path_to_imgs
    self.path_to_json = path_to_json
    self.img_ids = os.listdir(path_to_imgs)

    def __getitem__(self, idx):
    img_id = self.img_ids[idx]
    img_id = os.path.splitext(img_id)[0]
    img = cv2.imread(os.path.join(self.path_to_imgs, img_id + ".jpg"))
    load_json = json.load(open(os.path.join(self.path_to_json, img_id + ".json")))
    #n = len(load_json)
    #bboxes = load_json['annotation'][n]['segmentation']
    return img, load_json

    def __len__(self):
    return len(self.image_ids)


    When I try this



    l_data = lDataSet(path_to_imgs = '/home/training/images', path_to_json = '/home/training/json_annotations')


    I'm getting l_data with l_data[][0] - images and l_data with json. Now I'm confused. How will I use it with finetuning example availalbe in PyTorch? In that example, dataset and dataloader is done as shown below.
    https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html



    # Create training and validation datasets
    image_datasets = x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x]) for x in ['train', 'val']
    # Create training and validation dataloaders
    dataloaders_dict = x: torch.utils.data.DataLoader(image_datasets[x], batch_size=batch_size, shuffle=True, num_workers=4) for x in ['train', 'val']









    share|improve this question


























      0












      0








      0








      I would like to know how I can use the data loader in PyTorch for the custom file structure of mine. I have gone through PyTorch documentation, but all those are with separate folders with class.



      My folder structure consists of 2 folders(called training and validation), each with 2 subfolders(called images and json_annotations). Each image in the "images" folder has multiple objects(like cars, cycles, man etc) and each is annotated and have separate JSON files. Standard coco annotation is followed. My intention is to make a neural network which can do real-time classification from videos.



      Sample image, there are around 2k images are there with 2k JSON annotations



      corresponding JSON annotation file for the given image. Like this each image has separate JSON file



      Edit 1:
      I have done the coding as suggested by Fábio Perez.



      class lDataSet(data.Dataset):
      def __init__(self, path_to_imgs, path_to_json):
      self.path_to_imgs = path_to_imgs
      self.path_to_json = path_to_json
      self.img_ids = os.listdir(path_to_imgs)

      def __getitem__(self, idx):
      img_id = self.img_ids[idx]
      img_id = os.path.splitext(img_id)[0]
      img = cv2.imread(os.path.join(self.path_to_imgs, img_id + ".jpg"))
      load_json = json.load(open(os.path.join(self.path_to_json, img_id + ".json")))
      #n = len(load_json)
      #bboxes = load_json['annotation'][n]['segmentation']
      return img, load_json

      def __len__(self):
      return len(self.image_ids)


      When I try this



      l_data = lDataSet(path_to_imgs = '/home/training/images', path_to_json = '/home/training/json_annotations')


      I'm getting l_data with l_data[][0] - images and l_data with json. Now I'm confused. How will I use it with finetuning example availalbe in PyTorch? In that example, dataset and dataloader is done as shown below.
      https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html



      # Create training and validation datasets
      image_datasets = x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x]) for x in ['train', 'val']
      # Create training and validation dataloaders
      dataloaders_dict = x: torch.utils.data.DataLoader(image_datasets[x], batch_size=batch_size, shuffle=True, num_workers=4) for x in ['train', 'val']









      share|improve this question
















      I would like to know how I can use the data loader in PyTorch for the custom file structure of mine. I have gone through PyTorch documentation, but all those are with separate folders with class.



      My folder structure consists of 2 folders(called training and validation), each with 2 subfolders(called images and json_annotations). Each image in the "images" folder has multiple objects(like cars, cycles, man etc) and each is annotated and have separate JSON files. Standard coco annotation is followed. My intention is to make a neural network which can do real-time classification from videos.



      Sample image, there are around 2k images are there with 2k JSON annotations



      corresponding JSON annotation file for the given image. Like this each image has separate JSON file



      Edit 1:
      I have done the coding as suggested by Fábio Perez.



      class lDataSet(data.Dataset):
      def __init__(self, path_to_imgs, path_to_json):
      self.path_to_imgs = path_to_imgs
      self.path_to_json = path_to_json
      self.img_ids = os.listdir(path_to_imgs)

      def __getitem__(self, idx):
      img_id = self.img_ids[idx]
      img_id = os.path.splitext(img_id)[0]
      img = cv2.imread(os.path.join(self.path_to_imgs, img_id + ".jpg"))
      load_json = json.load(open(os.path.join(self.path_to_json, img_id + ".json")))
      #n = len(load_json)
      #bboxes = load_json['annotation'][n]['segmentation']
      return img, load_json

      def __len__(self):
      return len(self.image_ids)


      When I try this



      l_data = lDataSet(path_to_imgs = '/home/training/images', path_to_json = '/home/training/json_annotations')


      I'm getting l_data with l_data[][0] - images and l_data with json. Now I'm confused. How will I use it with finetuning example availalbe in PyTorch? In that example, dataset and dataloader is done as shown below.
      https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html



      # Create training and validation datasets
      image_datasets = x: datasets.ImageFolder(os.path.join(data_dir, x), data_transforms[x]) for x in ['train', 'val']
      # Create training and validation dataloaders
      dataloaders_dict = x: torch.utils.data.DataLoader(image_datasets[x], batch_size=batch_size, shuffle=True, num_workers=4) for x in ['train', 'val']






      python python-3.x opencv deep-learning pytorch






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 12 at 9:49







      bibinwilson

















      asked Mar 9 at 9:07









      bibinwilsonbibinwilson

      59214




      59214






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You should be able to implement your own dataset with data.Dataset. You just need to implement __len__ and __getitem__ methods.



          In your case, you can iterate through all images in the image folder (then you can store the image ids in a list in your Dataset). Then, you use the index passed to __getitem__ to get the corresponding image id. With this image id, you can read the corresponding JSON file and return the target data that you need.



          Something like this:



          class YourDataLoader(data.Dataset):
          def __init__(self, path_to_imgs, path_to_json):
          self.path_to_imags = path_to_imgs
          self.path_to_json = path_to_json
          self.image_ids = iterate_through_images(path_to_images)

          def __getitem__(self, idx):
          img_id = self.image_ids[idx]
          img = load_image(os.path.join(self.path_to_images, img_id)
          bboxes = load_bboxes(os.path.join(self.path_to_json, img_id)
          return img, bboxes

          def __len__(self):
          return len(self.image_ids)



          In iterate_through_images you get all the ids (e.g. filenames) of images in a directory.
          In load_bboxes you read the JSON and get the information you need.



          I have a JSON loader implementation here if you want a reference.






          share|improve this answer























          • I have gone through your code and it was a big help. I'm a beginner and I'm still confused. I have given those as Edit 1. Please help

            – bibinwilson
            Mar 12 at 9:52











          • You are trying to use a classification network to train an object detector. Please refer to some object detector tutorials github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection.

            – Fábio Perez
            Mar 12 at 11:17











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55075715%2fhow-to-load-images-with-multiple-json-annotation-in-pytorch%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You should be able to implement your own dataset with data.Dataset. You just need to implement __len__ and __getitem__ methods.



          In your case, you can iterate through all images in the image folder (then you can store the image ids in a list in your Dataset). Then, you use the index passed to __getitem__ to get the corresponding image id. With this image id, you can read the corresponding JSON file and return the target data that you need.



          Something like this:



          class YourDataLoader(data.Dataset):
          def __init__(self, path_to_imgs, path_to_json):
          self.path_to_imags = path_to_imgs
          self.path_to_json = path_to_json
          self.image_ids = iterate_through_images(path_to_images)

          def __getitem__(self, idx):
          img_id = self.image_ids[idx]
          img = load_image(os.path.join(self.path_to_images, img_id)
          bboxes = load_bboxes(os.path.join(self.path_to_json, img_id)
          return img, bboxes

          def __len__(self):
          return len(self.image_ids)



          In iterate_through_images you get all the ids (e.g. filenames) of images in a directory.
          In load_bboxes you read the JSON and get the information you need.



          I have a JSON loader implementation here if you want a reference.






          share|improve this answer























          • I have gone through your code and it was a big help. I'm a beginner and I'm still confused. I have given those as Edit 1. Please help

            – bibinwilson
            Mar 12 at 9:52











          • You are trying to use a classification network to train an object detector. Please refer to some object detector tutorials github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection.

            – Fábio Perez
            Mar 12 at 11:17















          1














          You should be able to implement your own dataset with data.Dataset. You just need to implement __len__ and __getitem__ methods.



          In your case, you can iterate through all images in the image folder (then you can store the image ids in a list in your Dataset). Then, you use the index passed to __getitem__ to get the corresponding image id. With this image id, you can read the corresponding JSON file and return the target data that you need.



          Something like this:



          class YourDataLoader(data.Dataset):
          def __init__(self, path_to_imgs, path_to_json):
          self.path_to_imags = path_to_imgs
          self.path_to_json = path_to_json
          self.image_ids = iterate_through_images(path_to_images)

          def __getitem__(self, idx):
          img_id = self.image_ids[idx]
          img = load_image(os.path.join(self.path_to_images, img_id)
          bboxes = load_bboxes(os.path.join(self.path_to_json, img_id)
          return img, bboxes

          def __len__(self):
          return len(self.image_ids)



          In iterate_through_images you get all the ids (e.g. filenames) of images in a directory.
          In load_bboxes you read the JSON and get the information you need.



          I have a JSON loader implementation here if you want a reference.






          share|improve this answer























          • I have gone through your code and it was a big help. I'm a beginner and I'm still confused. I have given those as Edit 1. Please help

            – bibinwilson
            Mar 12 at 9:52











          • You are trying to use a classification network to train an object detector. Please refer to some object detector tutorials github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection.

            – Fábio Perez
            Mar 12 at 11:17













          1












          1








          1







          You should be able to implement your own dataset with data.Dataset. You just need to implement __len__ and __getitem__ methods.



          In your case, you can iterate through all images in the image folder (then you can store the image ids in a list in your Dataset). Then, you use the index passed to __getitem__ to get the corresponding image id. With this image id, you can read the corresponding JSON file and return the target data that you need.



          Something like this:



          class YourDataLoader(data.Dataset):
          def __init__(self, path_to_imgs, path_to_json):
          self.path_to_imags = path_to_imgs
          self.path_to_json = path_to_json
          self.image_ids = iterate_through_images(path_to_images)

          def __getitem__(self, idx):
          img_id = self.image_ids[idx]
          img = load_image(os.path.join(self.path_to_images, img_id)
          bboxes = load_bboxes(os.path.join(self.path_to_json, img_id)
          return img, bboxes

          def __len__(self):
          return len(self.image_ids)



          In iterate_through_images you get all the ids (e.g. filenames) of images in a directory.
          In load_bboxes you read the JSON and get the information you need.



          I have a JSON loader implementation here if you want a reference.






          share|improve this answer













          You should be able to implement your own dataset with data.Dataset. You just need to implement __len__ and __getitem__ methods.



          In your case, you can iterate through all images in the image folder (then you can store the image ids in a list in your Dataset). Then, you use the index passed to __getitem__ to get the corresponding image id. With this image id, you can read the corresponding JSON file and return the target data that you need.



          Something like this:



          class YourDataLoader(data.Dataset):
          def __init__(self, path_to_imgs, path_to_json):
          self.path_to_imags = path_to_imgs
          self.path_to_json = path_to_json
          self.image_ids = iterate_through_images(path_to_images)

          def __getitem__(self, idx):
          img_id = self.image_ids[idx]
          img = load_image(os.path.join(self.path_to_images, img_id)
          bboxes = load_bboxes(os.path.join(self.path_to_json, img_id)
          return img, bboxes

          def __len__(self):
          return len(self.image_ids)



          In iterate_through_images you get all the ids (e.g. filenames) of images in a directory.
          In load_bboxes you read the JSON and get the information you need.



          I have a JSON loader implementation here if you want a reference.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 9 at 12:41









          Fábio PerezFábio Perez

          6,56475078




          6,56475078












          • I have gone through your code and it was a big help. I'm a beginner and I'm still confused. I have given those as Edit 1. Please help

            – bibinwilson
            Mar 12 at 9:52











          • You are trying to use a classification network to train an object detector. Please refer to some object detector tutorials github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection.

            – Fábio Perez
            Mar 12 at 11:17

















          • I have gone through your code and it was a big help. I'm a beginner and I'm still confused. I have given those as Edit 1. Please help

            – bibinwilson
            Mar 12 at 9:52











          • You are trying to use a classification network to train an object detector. Please refer to some object detector tutorials github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection.

            – Fábio Perez
            Mar 12 at 11:17
















          I have gone through your code and it was a big help. I'm a beginner and I'm still confused. I have given those as Edit 1. Please help

          – bibinwilson
          Mar 12 at 9:52





          I have gone through your code and it was a big help. I'm a beginner and I'm still confused. I have given those as Edit 1. Please help

          – bibinwilson
          Mar 12 at 9:52













          You are trying to use a classification network to train an object detector. Please refer to some object detector tutorials github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection.

          – Fábio Perez
          Mar 12 at 11:17





          You are trying to use a classification network to train an object detector. Please refer to some object detector tutorials github.com/sgrvinod/a-PyTorch-Tutorial-to-Object-Detection.

          – Fábio Perez
          Mar 12 at 11:17



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55075715%2fhow-to-load-images-with-multiple-json-annotation-in-pytorch%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

          Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

          Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved