Answers.Body: <p>Your input image size and memory are not directly related. While using CNN's, there are multiple hyperparameters that effect the video memory(if you are using GPU) or physical memory(if you are using CPU). All the frameworks these days uses a simplified data-loaders, for instance in Tensorflow or PyTorch, you are required to write a data-loader that takes in multiple hyper-parameters that are mentioned below and fit the data into VRAM/RAM, and this is strictly dependent upon you batch size - memory occupied on VRAM has direct relation to the batch size. </p>
  
  <p>Whatever may be your image size, while you are writing the data-loader you have to mention the transformation parameters to your data-loader, during the training phase the data-loader will automatically load required images into your memory according to the batch size you have mentioned. As you have mentioned about image compression, this is an irrelevant parameter at-least for most of the generic use-cases, the most relevant hyperparameters are </p>
  
  <ol>
  <li>Scaling </li>
  <li>Cropping </li>
  <li>Random flip </li>
  <li>Normalization of the RGB values</li>
  <li>ColorJitter</li>
  <li>Padding</li>
  <li>RandomAffine</li>
  </ol>
  
  <p>And many more.</p>
  
  <p>PyTorch provides really good transformers in data-loader, please do check <a href="https://pytorch.org/docs/stable/torchvision/transforms.html" rel="nofollow noreferrer">https://pytorch.org/docs/stable/torchvision/transforms.html</a>.</p>
  
  <p>For Tensorflow, have a look at <a href="https://keras.io/preprocessing/image/" rel="nofollow noreferrer">https://keras.io/preprocessing/image/</a>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-24T13:14:06.327Z
Answers.Text: Although data loaders or generators may partially solve the problem and the information in your answer seems valid, I think the OP is specifically looking for a way of compressing single images. Maybe you could mention something about it. Also, the OP is using TensorFlow, so you may reformulate your answer so that is targets more TF rather than PyTorch.
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-24T21:51:53.220Z
Answers.Text: @nbro, yes, I am looking for ways to compress single images for usage in a reinforcement learning task.
Answers.UserDisplayName: ZeroMaxinumXZ
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-02-24T09:48:51.160Z
Answers.LastActivityDate: 2020-02-24T13:15:46.810Z
Answers.LastEditDate: 2020-02-24T13:15:46.810Z
Answers.UserDisplayName: Stephen Philip
Answers.Body: <p>Tensorflow-Keras provides an effective data transformer and loader. Documentation is at
  <a href="https://keras.io/preprocessing/image/" rel="nofollow noreferrer">https://keras.io/preprocessing/image/</a>. The ImageDataGenerator provides  for many types of possible transforms and also enables use of a user defined pre-process function. Use of the 
  ImageDataGenerator.flow_from_directory provides a means of retrieving images in batches from a directory containing sub directories (classes) of images. and resizing the images.
  Image size can impact the results. Generally the larger the image the better the result but this is subject to the law of diminishing returns (at some point the impact on accuracy becomes minuscule) while the training time can become absorbent.
  When you have large images like 1000 X 1000 where the subject of interest in the image is small say 50 X 50 the best but most painful approach is the crop the image to the subject of interest. Unfortunately this is usually a time consuming drudgery unless you can find some program that can crop the image automatically. For example there are good programs that can crop  images of people automatically where the resultant cropped image is primarily the persons face. Alternatively modules like cv2 can be adapted to provide this capability for certain images.
  The batch_size you select along with the image size directly effect memory usage. If your images are large and your batch_size is too large you will encounter a "resource exhaust" error. You can reduce the batch size but this will extend training time.
  Other techniques for dealing with large images include methods like sliding windows etc. Again these will increase training time because you are taking a large image and breaking it into a series of smaller images that you feed into the network.
  A general though probably risky rule I follow is that if I can visibly see the subject of interest in a resized image then I assume the network will be able to detect it as well.
  Will probably be less accurate than using the full image but should be as we engineers say "good enough"</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-02T22:56:30.857Z
Answers.LastActivityDate: 2020-03-02T22:56:30.857Z
Answers.UserDisplayName: Gerry P
Body: <p>I have a set of images, which are quite large in size (1000x1000), and as such do not easily fit into memory. I'd like to compress these images, such that little information is missing. I am looking to use a CNN for a reinforcement learning task which involves a lot of very small objects which may disappear when downsampling. What is the best approach to handle this without downscaling/downsampling the image and losing information for CNNs?</p>
  
Tags: <deep-learning><reinforcement-learning><convolutional-neural-networks><tensorflow><numpy>
Title: How can I perform lossless compression of images so that they can be stored to train a CNN?
UserDisplayName: ZeroMaxinumXZ
ThreadText: # How can I perform lossless compression of images so that they can be stored to train a CNN?
  
  (Asked by: username_0 on 2020-02-24)
  
  I have a set of images, which are quite large in size (1000x1000), and as such do not easily fit into memory. I'd like to compress these images, such that little information is missing. I am looking to use a CNN for a reinforcement learning task which involves a lot of very small objects which may disappear when downsampling. What is the best approach to handle this without downscaling/downsampling the image and losing information for CNNs?
  
  **Tags:** deep-learning, reinforcement-learning, convolutional-neural-networks, tensorflow, numpy
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-02-24. Score: 1
  
  Your input image size and memory are not directly related. While using CNN's, there are multiple hyperparameters that effect the video memory(if you are using GPU) or physical memory(if you are using CPU). All the frameworks these days uses a simplified data-loaders, for instance in Tensorflow or PyTorch, you are required to write a data-loader that takes in multiple hyper-parameters that are mentioned below and fit the data into VRAM/RAM, and this is strictly dependent upon you batch size - memory occupied on VRAM has direct relation to the batch size.
  
  Whatever may be your image size, while you are writing the data-loader you have to mention the transformation parameters to your data-loader, during the training phase the data-loader will automatically load required images into your memory according to the batch size you have mentioned. As you have mentioned about image compression, this is an irrelevant parameter at-least for most of the generic use-cases, the most relevant hyperparameters are
  
  1. Scaling
  2. Cropping
  3. Random flip
  4. Normalization of the RGB values
  5. ColorJitter
  6. Padding
  7. RandomAffine
  
  And many more.
  
  PyTorch provides really good transformers in data-loader, please do check <https://pytorch.org/docs/stable/torchvision/transforms.html>.
  
  For Tensorflow, have a look at <https://keras.io/preprocessing/image/>.
  
  > **Comments:**
  > **username_2**: Although data loaders or generators may partially solve the problem and the information in your answer seems valid, I think the OP is specifically looking for a way of compressing single images. Maybe you could mention something about it. Also, the OP is using TensorFlow, so you may reformulate your answer so that is targets more TF rather than PyTorch.  
  > **username_0**: @nbro, yes, I am looking for ways to compress single images for usage in a reinforcement learning task.  
  
  ---
  
  ### Answer by username_3 on 2020-03-02. Score: 0
  
  Tensorflow-Keras provides an effective data transformer and loader. Documentation is at
  <https://keras.io/preprocessing/image/>. The ImageDataGenerator provides for many types of possible transforms and also enables use of a user defined pre-process function. Use of the
  ImageDataGenerator.flow\_from\_directory provides a means of retrieving images in batches from a directory containing sub directories (classes) of images. and resizing the images.
  Image size can impact the results. Generally the larger the image the better the result but this is subject to the law of diminishing returns (at some point the impact on accuracy becomes minuscule) while the training time can become absorbent.
  When you have large images like 1000 X 1000 where the subject of interest in the image is small say 50 X 50 the best but most painful approach is the crop the image to the subject of interest. Unfortunately this is usually a time consuming drudgery unless you can find some program that can crop the image automatically. For example there are good programs that can crop images of people automatically where the resultant cropped image is primarily the persons face. Alternatively modules like cv2 can be adapted to provide this capability for certain images.
  The batch\_size you select along with the image size directly effect memory usage. If your images are large and your batch\_size is too large you will encounter a "resource exhaust" error. You can reduce the batch size but this will extend training time.
  Other techniques for dealing with large images include methods like sliding windows etc. Again these will increase training time because you are taking a large image and breaking it into a series of smaller images that you feed into the network.
  A general though probably risky rule I follow is that if I can visibly see the subject of interest in a resized image then I assume the network will be able to detect it as well.
  Will probably be less accurate than using the full image but should be as we engineers say "good enough"
  
  
  ---
  
  