{"id":234,"date":"2023-02-02T11:01:09","date_gmt":"2023-02-02T11:01:09","guid":{"rendered":"https:\/\/cristianku.com\/?p=234"},"modified":"2025-07-07T17:34:09","modified_gmt":"2025-07-07T17:34:09","slug":"deep-learning-huge-amount-1","status":"publish","type":"post","link":"https:\/\/cristianku.com\/it\/2023\/02\/02\/deep-learning-huge-amount-1\/","title":{"rendered":"Deep Learning . Training with huge amount of data .PART1"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"234\" class=\"elementor elementor-234\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-1e6ea64 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"1e6ea64\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-ba0fa1f\" data-id=\"ba0fa1f\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-dae4295 elementor-widget elementor-widget-text-editor\" data-id=\"dae4295\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Performance considerations<br \/>I would like to put some consideration about Performance in Keras , in order to obtain the max GPU consideration.<br \/>What we are going to do , is train a Keras Model.<br \/>Here the number of images :<br \/>Now we define a generator used to train in batch of 32 ( can be an other number ), and we put this generator in the following call:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-9c4f7f6 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"9c4f7f6\" data-element_type=\"section\" data-e-type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-12504f7\" data-id=\"12504f7\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-4b1142c elementor-widget elementor-widget-kerge-quote\" data-id=\"4b1142c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"kerge-quote.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<blockquote class=\"quote\">\r\n\t\t    model.fit_generator(train_generator, steps_per_epoch= \\\nnumper_of_train_samples\/batch_size, validation_data=validation_generator, \\\nvalidation_steps=number_of_validation_samples\/batch_size, epochs=epochs, verbose = 1)\t\t\t\t\t<\/blockquote>\r\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-a2d6e31 elementor-widget elementor-widget-text-editor\" data-id=\"a2d6e31\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"ab27\" class=\"pw-post-body-paragraph ym yn uc yo b yp yq yr ys yt yu yv yw yx yy yz za rx zb zc zd ze zf zg zh zi ky bx\" data-selectable-paragraph=\"\">The generator is so defined that reads image from disk, n-images exactly how the batch size is:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-141e483 elementor-widget elementor-widget-kerge-quote\" data-id=\"141e483\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"kerge-quote.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<blockquote class=\"quote\">\r\n\t\t    def generator(generator_samples, batch_size, shape ):\nnum_samples = len(generator_samples)\nwhile 1: # Loop forever so the generator never terminates\n\u2026 read from disk and append info images and angles array\n# end of the batch, yield the images gathered\nyield sklearn.utils.shuffle(np.array(images) , np.array(angles))\t\t\t\t\t<\/blockquote>\r\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-4d45e11 elementor-widget elementor-widget-text-editor\" data-id=\"4d45e11\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"ab27\" class=\"pw-post-body-paragraph ym yn uc yo b yp yq yr ys yt yu yv yw yx yy yz za rx zb zc zd ze zf zg zh zi ky bx\" data-selectable-paragraph=\"\">Now we start the fit_generator\u2026 and take a closer look at the CPU and GPU utilization to understand how the data is flushing in between , from CPU to GPU and back\u2026.. Look at the CPU working at full speed in order to handle the sparse disk reads and the GPU waiting for job\u2026 and sometimes doing nothing<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b245e2b elementor-widget elementor-widget-video\" data-id=\"b245e2b\" data-element_type=\"widget\" data-e-type=\"widget\" data-settings=\"{&quot;youtube_url&quot;:&quot;https:\\\/\\\/www.youtube.com\\\/watch?v=n-_94TMZEYQ&amp;t=2s&quot;,&quot;video_type&quot;:&quot;youtube&quot;,&quot;controls&quot;:&quot;yes&quot;}\" data-widget_type=\"video.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"elementor-wrapper elementor-open-inline\">\n\t\t\t<div class=\"elementor-video\"><\/div>\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-9ea3f2d elementor-widget elementor-widget-text-editor\" data-id=\"9ea3f2d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Training Statistic:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-d71ba9d elementor-widget elementor-widget-image\" data-id=\"d71ba9d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"279\" src=\"https:\/\/cristianku.com\/wp-content\/uploads\/2025\/07\/1bO3Eb_BNYj9Jdg919bhcDA-1024x279.webp\" class=\"attachment-large size-large wp-image-621\" alt=\"\" srcset=\"https:\/\/cristianku.com\/wp-content\/uploads\/2025\/07\/1bO3Eb_BNYj9Jdg919bhcDA-1024x279.webp 1024w, https:\/\/cristianku.com\/wp-content\/uploads\/2025\/07\/1bO3Eb_BNYj9Jdg919bhcDA-300x82.webp 300w, https:\/\/cristianku.com\/wp-content\/uploads\/2025\/07\/1bO3Eb_BNYj9Jdg919bhcDA-768x209.webp 768w, https:\/\/cristianku.com\/wp-content\/uploads\/2025\/07\/1bO3Eb_BNYj9Jdg919bhcDA-18x5.webp 18w, https:\/\/cristianku.com\/wp-content\/uploads\/2025\/07\/1bO3Eb_BNYj9Jdg919bhcDA.webp 1400w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-da153f7 elementor-widget elementor-widget-text-editor\" data-id=\"da153f7\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"f478\" class=\"pw-post-body-paragraph ym yn uc yo b yp yq yr ys yt yu yv yw yx yy yz za rx zb zc zd ze zf zg zh zi ky bx\" data-selectable-paragraph=\"\">So .. whats the next idea ?<\/p>\n<p id=\"4056\" class=\"pw-post-body-paragraph ym yn uc yo b yp yq yr ys yt yu yv yw yx yy yz za rx zb zc zd ze zf zg zh zi ky bx\" data-selectable-paragraph=\"\">Very simple ! load ALL the images in MEMORY !!<\/p>\n<p id=\"fe1e\" class=\"pw-post-body-paragraph ym yn uc yo b yp yq yr ys yt yu yv yw yx yy yz za rx zb zc zd ze zf zg zh zi ky bx\" data-selectable-paragraph=\"\">Is a good idea ?? Yes and now\u2026 if we have a lot of memory\u2026 but its not a scalable option because if we add in the future new images.. it can happen that the program explodes\u2026in out of memory exception\u2026<\/p>\n<p id=\"b8d9\" class=\"pw-post-body-paragraph ym yn uc yo b yp yq yr ys yt yu yv yw yx yy yz za rx zb zc zd ze zf zg zh zi ky bx\" data-selectable-paragraph=\"\">but lets take a look at the performance now :<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-44e7079 elementor-widget elementor-widget-image\" data-id=\"44e7079\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"1024\" height=\"647\" data-src=\"https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1by_c3u0zIkFDNrRy1shL2A-1024x647.webp\" class=\"attachment-large size-large wp-image-622 lazyload\" alt=\"\" data-srcset=\"https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1by_c3u0zIkFDNrRy1shL2A-1024x647.webp 1024w, https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1by_c3u0zIkFDNrRy1shL2A-300x189.webp 300w, https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1by_c3u0zIkFDNrRy1shL2A-768x485.webp 768w, https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1by_c3u0zIkFDNrRy1shL2A-18x12.webp 18w, https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1by_c3u0zIkFDNrRy1shL2A.webp 1400w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/647;\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-67440d2 elementor-widget elementor-widget-image\" data-id=\"67440d2\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"1024\" height=\"281\" data-src=\"https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1sPgjWjUBrmRb7yl9C5huPQ-1024x281.webp\" class=\"attachment-large size-large wp-image-623 lazyload\" alt=\"\" data-srcset=\"https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1sPgjWjUBrmRb7yl9C5huPQ-1024x281.webp 1024w, https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1sPgjWjUBrmRb7yl9C5huPQ-300x82.webp 300w, https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1sPgjWjUBrmRb7yl9C5huPQ-768x211.webp 768w, https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1sPgjWjUBrmRb7yl9C5huPQ-18x5.webp 18w, https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1sPgjWjUBrmRb7yl9C5huPQ.webp 1400w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/281;\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f592f14 elementor-widget elementor-widget-text-editor\" data-id=\"f592f14\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"f478\" class=\"pw-post-body-paragraph ym yn uc yo b yp yq yr ys yt yu yv yw yx yy yz za rx zb zc zd ze zf zg zh zi ky bx\" data-selectable-paragraph=\"\">As you can see the GPU is at full speed. Although it was not a full speed all the time. We see later to resolve this with a threaded generator.<br \/>IS FITTING EVERYTHING IN MEMORY A GOOD IDEA ??<br \/>The answer is : NO!!!!<br \/>Ideally we should try to keep a buffer in memory where a \u201cworker\u201d can feed the GPU very fast, and .. the remaining data on a SSD waiting to be put in the Memory buffer by an other \u201cworker\u201d<br \/>Lets give a try to the Pytables, saved in a .hdf5 file.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-3922fbf elementor-widget elementor-widget-text-editor\" data-id=\"3922fbf\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<pre class=\"zx zy zz aba abb abp abq abr abs bh abt bx\"><span id=\"0309\" class=\"abu xz uc abq b fn abv abw y qc abx\" data-selectable-paragraph=\"\"><strong class=\"abq gr\">def generator(samples, labels, batch_size, shape ):<br \/><\/strong>    num_samples = len(samples)<br \/>    while 1: # Loop forever so the generator never terminates<br \/>#         sklearn.utils.shuffle(samples)<br \/>        for offset in range(0, num_samples, batch_size):<br \/>            batch_samples = samples[offset:offset+batch_size]<br \/>            batch_labels  = labels[offset:offset+batch_size]<\/span><span id=\"24ae\" class=\"abu xz uc abq b fn aby abw y qc abx\" data-selectable-paragraph=\"\">images = []<br \/>            <mark class=\"agv agw av\">angles = []<\/mark><br \/>            for batch_sample, batch_label in zip(batch_samples, batch_labels):<br \/>                <br \/>                images.append(batch_sample)<br \/>                angles.append(batch_label)<\/span><span id=\"f0a9\" class=\"abu xz uc abq b fn aby abw y qc abx\" data-selectable-paragraph=\"\"># end of the batch, yield the images gathered<br \/>#             print (batch_sample.shape)<br \/>            yield sklearn.utils.shuffle(np.array(images) , np.array(angles))<br \/>#             return sklearn.utils.shuffle(np.array(images)  , np.array(angles))<\/span><span id=\"6637\" class=\"abu xz uc abq b fn aby abw y qc abx\" data-selectable-paragraph=\"\"><br \/>py_training_samples.append(output[0]<strong class=\"abq gr\">[None]<\/strong>)<\/span><\/pre>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-59fbe05 elementor-widget elementor-widget-text-editor\" data-id=\"59fbe05\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"f478\" class=\"pw-post-body-paragraph ym yn uc yo b yp yq yr ys yt yu yv yw yx yy yz za rx zb zc zd ze zf zg zh zi ky bx\" data-selectable-paragraph=\"\">The None is needed to creare a additional first dimension, as the output[0] is the image size, so we end to have a (1,128,128,3 ) that is the same as defined above: shape=( 0,resized_shape, resized_shape, 3))<br \/>If you try to define the table fieldshape=( resized_shape, resized_shape, 3)), without the first dimension, you will get this error:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-c38dea8 elementor-widget elementor-widget-text-editor\" data-id=\"c38dea8\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<pre class=\"zx zy zz aba abb abp abq abr abs bh abt bx\">ValueError: When creating EArrays, you need to set one of the dimensions of the Atom instance to zero.<\/pre>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2efec9f elementor-widget elementor-widget-text-editor\" data-id=\"2efec9f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"f478\" class=\"pw-post-body-paragraph ym yn uc yo b yp yq yr ys yt yu yv yw yx yy yz za rx zb zc zd ze zf zg zh zi ky bx\" data-selectable-paragraph=\"\">What about the file dimension ? , 5 Gigabytes!!!!:<br \/>-rw-r \u2014 r \u2014 1 cristianku cristianku 4.993.236.504 Oct 9 23:51 samples.hdf5<br \/>Now lets configure the generator to use the Pytable \u2026 No! there is no adjustment here.. it works exactly like a Numpy Array !:<br \/>Here my generator:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6e4a331 elementor-widget elementor-widget-text-editor\" data-id=\"6e4a331\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<pre class=\"zx zy zz aba abb abp abq abr abs bh abt bx\"><span id=\"21dd\" class=\"abu xz uc abq b fn abv abw y qc abx\" data-selectable-paragraph=\"\">def generator(samples, labels, batch_size, shape ):<br \/>    num_samples = len(samples)<br \/>    while 1: # Loop forever so the generator never terminates<br \/>#         sklearn.utils.shuffle(samples)<br \/>        for offset in range(0, num_samples, batch_size):<br \/>            batch_samples = samples[offset:offset+batch_size]<br \/>            batch_labels  = labels[offset:offset+batch_size]<\/span><span id=\"2672\" class=\"abu xz uc abq b fn aby abw y qc abx\" data-selectable-paragraph=\"\">images = []<br \/>            angles = []<br \/>            for batch_sample, batch_label in zip(batch_samples, batch_labels):<br \/>                <br \/>                images.append(batch_sample)<br \/>                angles.append(batch_label)<\/span><span id=\"f25b\" class=\"abu xz uc abq b fn aby abw y qc abx\" data-selectable-paragraph=\"\"># end of the batch, yield the images gathered<br \/>#             print (batch_sample.shape)<br \/>            yield sklearn.utils.shuffle(np.array(images) , np.array(angles))<br \/>#             return sklearn.utils.shuffle(np.array(images)  , np.array(angles))<\/span><\/pre>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ba2183b elementor-widget elementor-widget-text-editor\" data-id=\"ba2183b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<pre class=\"zx zy zz aba abb abp abq abr abs bh abt bx\">Epoch 1\/1<br \/>2541\/2540 [==============================] - 35s - loss: 0.0679 - val_loss: 0.0407<br \/><br \/>Total number of train samples: 81282 ( shape 128x128)<br \/><br \/>Batch Size : 32<br \/><br \/>Duration : 0:00:35.566706<\/pre>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f6cda30 elementor-widget elementor-widget-text-editor\" data-id=\"f6cda30\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"f478\" class=\"pw-post-body-paragraph ym yn uc yo b yp yq yr ys yt yu yv yw yx yy yz za rx zb zc zd ze zf zg zh zi ky bx\" data-selectable-paragraph=\"\">And now the verdict ! The Performance ???<br \/>Its even better than the entire numpy array in memory !!!<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-2d99d5f elementor-widget elementor-widget-text-editor\" data-id=\"2d99d5f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p id=\"f478\" class=\"pw-post-body-paragraph ym yn uc yo b yp yq yr ys yt yu yv yw yx yy yz za rx zb zc zd ze zf zg zh zi ky bx\" data-selectable-paragraph=\"\">We still have a problem.<br \/>Normally , for each epoch, we do Shuffle the data , in order to have more \u201crandomly distributed training data\u201d<br \/>how we can shuffle 5 giga of data ??<br \/>So\u2026 we have a batch size of 32 sample for example\u2026 we can read from Pytable more than that in a single fetch.. lets say 10 times more\u2026 and shuffle this chuck of data:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-ba07894 elementor-widget elementor-widget-text-editor\" data-id=\"ba07894\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<pre class=\"zx zy zz aba abb abp abq abr abs bh abt bx\"><span id=\"c8ce\" class=\"abu xz uc abq b fn abv abw y qc abx\" data-selectable-paragraph=\"\">def generator(samples, labels, batch_size, shape ):<br \/> num_samples = len(samples)<br \/> while 1: # Loop forever so the generator never terminates<br \/># sklearn.utils.shuffle(samples)<br \/> for offset in range(0, num_samples, batch_size*100):<br \/> # loading into memory a BIG chunk of data ( 3200 samples )<br \/> chunk_batch_samples = samples[offset:offset+batch_size*100]<br \/> chunk_batch_labels = labels[offset:offset+batch_size*100]<br \/> # shuffling them <br \/> chunk_batch_samples,chunk_batch_labels = sklearn.utils.shuffle(chunk_batch_samples,chunk_batch_labels)<br \/> <br \/># print (\u201c offset = {}, len(chunk_batch_samples) {}\u201d.format(offset, len(chunk_batch_samples)))<br \/> images = []<br \/> angles = []<br \/> # looping on the Chunk of 3200 samples , extracting 32 elements ( batch_size) every time<br \/> for offset2 in range(0, len(chunk_batch_samples), batch_size):<br \/> chunk_batch_samples = chunk_batch_samples[offset2:offset2+batch_size]<br \/> chunk_batch_labels = chunk_batch_labels[offset2:offset2+batch_size]<\/span><span id=\"5331\" class=\"abu xz uc abq b fn aby abw y qc abx\" data-selectable-paragraph=\"\">for batch_sample, batch_label in zip(chunk_batch_samples, chunk_batch_labels):<\/span><span id=\"ea58\" class=\"abu xz uc abq b fn aby abw y qc abx\" data-selectable-paragraph=\"\">images.append(batch_sample)<br \/> angles.append(batch_label)<\/span><span id=\"4885\" class=\"abu xz uc abq b fn aby abw y qc abx\" data-selectable-paragraph=\"\">yield sklearn.utils.shuffle(np.array(images) , np.array(angles))<\/span><\/pre>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-8a4262e elementor-widget elementor-widget-text-editor\" data-id=\"8a4262e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-selectable-paragraph=\"\">With this method, we do Shuffle the data, maintain a Memory at only 25%<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-498265c elementor-widget elementor-widget-image\" data-id=\"498265c\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"274\" height=\"156\" data-src=\"https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1djG9bwiIVYVSQshqjL9EhQ.webp\" class=\"attachment-large size-large wp-image-634 lazyload\" alt=\"\" data-srcset=\"https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1djG9bwiIVYVSQshqjL9EhQ.webp 274w, https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1djG9bwiIVYVSQshqjL9EhQ-18x10.webp 18w\" data-sizes=\"(max-width: 274px) 100vw, 274px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 274px; --smush-placeholder-aspect-ratio: 274\/156;\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-0f24132 elementor-widget elementor-widget-text-editor\" data-id=\"0f24132\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-selectable-paragraph=\"\">and we have more or less the same good performance of having everything in memory:<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-961b213 elementor-widget elementor-widget-image\" data-id=\"961b213\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"image.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<img decoding=\"async\" width=\"844\" height=\"239\" data-src=\"https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1xR1ExkW6QftBEGCeFjL1ng.webp\" class=\"attachment-large size-large wp-image-635 lazyload\" alt=\"\" data-srcset=\"https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1xR1ExkW6QftBEGCeFjL1ng.webp 844w, https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1xR1ExkW6QftBEGCeFjL1ng-300x85.webp 300w, https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1xR1ExkW6QftBEGCeFjL1ng-768x217.webp 768w, https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/1xR1ExkW6QftBEGCeFjL1ng-18x5.webp 18w\" data-sizes=\"(max-width: 844px) 100vw, 844px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 844px; --smush-placeholder-aspect-ratio: 844\/239;\" \/>\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-b19225f elementor-widget elementor-widget-text-editor\" data-id=\"b19225f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p data-selectable-paragraph=\"\">Our Training is NOW SCALABLE, FAST, and RELIABLE is indipendent from the number of Images to train\u2026 we can have a million of images and the machanism is the same !<br \/>Whats next ? Next time I will go deep and further improve the mechanism, by introducing two different threads :<br \/>CPU1: reads a big chunk from disk ( 3200 samples ), shuffle it, and insert a memory Buffer ( Queue)<br \/>CPU2: the second one is the generator that extract a small amount from this buffer ( the batch size = 32 samples)<br \/>GPU: last one is the Keras\/Tensorflow calculations done in the GPU<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Performance considerationsI would like to put some consideration about Performance in Keras , in order to obtain the max GPU consideration.What we are going to do , is train a Keras Model.Here the number of images :Now we define a generator used to train in batch of 32 ( can be an other number ),&#8230;<\/p>\n<p><a class=\"read-more\" href=\"https:\/\/cristianku.com\/it\/2023\/02\/02\/deep-learning-huge-amount-1\/\">Read More<\/a><\/p>","protected":false},"author":1,"featured_media":640,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[38],"tags":[35,34,36,37],"class_list":["post-234","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-deep-learning","tag-ai","tag-deep-learning","tag-neural-networks","tag-python"],"jetpack_featured_media_url":"https:\/\/cristianku.com\/wp-content\/uploads\/2023\/02\/Screenshot-2025-07-07-at-10.54.15-scaled.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/cristianku.com\/it\/wp-json\/wp\/v2\/posts\/234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cristianku.com\/it\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cristianku.com\/it\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cristianku.com\/it\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cristianku.com\/it\/wp-json\/wp\/v2\/comments?post=234"}],"version-history":[{"count":13,"href":"https:\/\/cristianku.com\/it\/wp-json\/wp\/v2\/posts\/234\/revisions"}],"predecessor-version":[{"id":638,"href":"https:\/\/cristianku.com\/it\/wp-json\/wp\/v2\/posts\/234\/revisions\/638"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cristianku.com\/it\/wp-json\/wp\/v2\/media\/640"}],"wp:attachment":[{"href":"https:\/\/cristianku.com\/it\/wp-json\/wp\/v2\/media?parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cristianku.com\/it\/wp-json\/wp\/v2\/categories?post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cristianku.com\/it\/wp-json\/wp\/v2\/tags?post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}