Save preprocessed image for training speedup

andrecchia: <p>Hi everyone,<br>
I am training a ResNet50 on 18.000 jpeg images and I noticed that most of time resources are taken in image preprocessing:</p>
<pre><code class="lang-auto">weights = ResNet50_Weights.DEFAULT
preprocess = weights.transforms()
prep_img=preprocess(image)
</code></pre>
<p>Then I thought to do a preprocess step, save all the preprocessed images as .jpg with <code>torchvision.utils.save_image</code> and use these preprocessed images as Dataset for training.<br>
There is an actual speedup, but I noticed that <code>save_image</code> change the images, in practice if I 1) Open the original image  (with PIL), 2) transform the image 3) save the transformed image 4) open the transformed image (with PIL), the tensors in 2) and 4) are different (norm of the difference is of the order of 155).<br>
This has at least 3 side effects:</p>
<ul>
<li>I am training on a Dataset that is not what I expect, that is original image+preprocessing;</li>
<li>When I subsequently classify some images I have load them, preprocess, save and reopen, because …</li>
<li>… I do not understand what happens, why 2) and 4) are different.<br>
I tried to remove the normalization <code>T.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])</code> and 2) and 4) are almost equal (norm of the difference is roughly 1.5), but still the tensors are different.<br>
Am I doing something completely wrong trying to save the preprocessed images? If not, what is the proper way to do it?<br>
Do you know why even reverting the normalization the tensors are different?<br>
P.S. saving the tensor directly is ok but the occupied space is roughly the same as the original 18.000 dataset, while the resized and cropped preprocessed jpg occupies much less.</li>
</ul>

eqy: <p>If you are asking why the saved and loaded tensors are different, that is because JPEG compression is lossy (even with the maximum quality level) due to the nature of the DCT-based compression it uses.</p>
<p>Could you post some more details about your setup (e.g., CPU resources) in case are you fully utilizing the CPU already for image preprocessing?</p>

andrecchia: <p>Thanks <a class="mention" href="/u/eqy">@eqy</a> for your answer. You’re right, I tried saving the images (removing the normalization)  as png and the tensors are equal.<br>
Anyway, the training is fine even if I preprocess the images at each epoch, I only noticed that saving the preprocessed image could result in a time optimization.</p>
<p>As far as I get I cannot save directly the preprocessed image (without removing the normalization before) because there is some information loss even with a lossless compression. I do not know exactly why, but I imagine that on adjusting from float → [0,255] something goes lost ( I noticed that negative floats are put to zero, floats &gt; 1 are put to 1). So I have to conclude that I cannot avoid some image preprocessing during the training, is that correct?</p>

eqy: <p>Yes, some rounding would occur and 32-bit floating point has &gt;&gt; 8 bits of mantissa, meaning that far greater than 255 values could exist between [0,1].</p>
<p>I would not say you <em>must</em> preprocess during training, but in practice I do not see it done for common vision models as the preprocessing typically includes some random data augmentations that wouldn’t really make sense to persist to storage. If your preprocessing only creates a constant size amount of data, then it could make sense, and the rounding differences you see may not be significant in terms of impact on the model quality. However, you would have to be careful that during inference or deployment that you are using the same preprocessing steps (the inference data should also be rounded/quantized if this was done for the training data).</p>