<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Coding Tips on Paco's Log</title><link>http://pacocp.es/blog/category/coding-tips/</link><description>Recent content in Coding Tips on Paco's Log</description><generator>Hugo</generator><language>en-US</language><copyright>Copyright © 2025, Francisco Carrillo-Perez.</copyright><lastBuildDate>Sun, 30 Jan 2022 00:00:00 +0000</lastBuildDate><atom:link href="http://pacocp.es/blog/category/coding-tips/index.xml" rel="self" type="application/rss+xml"/><item><title>Ommiting corrupted images inside a Pytorch dataloader</title><link>http://pacocp.es/blog/blog/ommiting-corrupted-images-inside-a-pytorch-dataloader/</link><pubDate>Sun, 30 Jan 2022 00:00:00 +0000</pubDate><guid>http://pacocp.es/blog/blog/ommiting-corrupted-images-inside-a-pytorch-dataloader/</guid><description>&lt;p&gt;Sometimes our data is not as clean as it should be. Maybe some images are corruped, or the entries inside the database were not as expected. In that case, iterating through them using the Pytorch dataloader can be hustle. Recently I learned that you can clean your data after it has been loaded, but before you are iterating throught in your training lops. In this case I am going to give an example with images, but it can be applied to any kind of data.&lt;/p&gt;</description></item><item><title>PIL converts NaNs images to zero value images</title><link>http://pacocp.es/blog/blog/pil-converts-nans-images-to-zero-value-images/</link><pubDate>Sat, 07 Mar 2020 00:00:00 +0000</pubDate><guid>http://pacocp.es/blog/blog/pil-converts-nans-images-to-zero-value-images/</guid><description>&lt;p&gt;I was having problems with missing information in my problem. There were&#10;missing images which were read as NaNs image shape matrixes but at the end they&#10;were fed to the CNN.&lt;/p&gt;&#10;&lt;p&gt;Debugging the Python code I realized that the function&#10;&lt;code&gt;transforms.ToPILImage()&lt;/code&gt;&#10;was converting the NaNs to zero values.&lt;/p&gt;</description></item><item><title>Count NaN values that are not NaN in Pandas DataFrame</title><link>http://pacocp.es/blog/blog/count-nan-values-that-are-not-nan-in-pandas-dataframe/</link><pubDate>Mon, 13 Jan 2020 00:00:00 +0000</pubDate><guid>http://pacocp.es/blog/blog/count-nan-values-that-are-not-nan-in-pandas-dataframe/</guid><description>&lt;p&gt;I am working with a dataset that contains lots of missing values. However, when a value is missed it is not represented as NaN but as &amp;ldquo;&amp;ndash;&amp;rdquo;. In order to find how many missing values there are in an specific column I cannot use typical Pandas functions. Henceforth, I have worked it out by count the number of appereances of this character in the column and substract it to the total number of entries in the column:&lt;/p&gt;</description></item><item><title>How to set Python3 as default version on MacOS</title><link>http://pacocp.es/blog/blog/how-to-set-python3-as-default-version-on-macos/</link><pubDate>Fri, 10 Jan 2020 00:00:00 +0000</pubDate><guid>http://pacocp.es/blog/blog/how-to-set-python3-as-default-version-on-macos/</guid><description>&lt;p&gt;In order not to have to type everytime you want to run Python 3&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;python3&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;I wanted to link to the default version of Python. Then you need to use the following commands:&lt;/p&gt;&#10;&lt;p&gt;Install again Python 3 with brew:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;brew install python&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Look how it is installed:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;ls -l /usr/local/bin/python*&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Different versions should appear. We would create a new default python symlink to the version we want to use:&lt;/p&gt;&#10;&lt;pre tabindex="0"&gt;&lt;code&gt;ln -s -f /user/local/bin/python3.7 /usr/local/bin/python&#10;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Exit the terminal. Now, if we type&lt;/p&gt;</description></item><item><title>Combine images vertically</title><link>http://pacocp.es/blog/blog/combine-images-vertically/</link><pubDate>Tue, 17 Dec 2019 00:00:00 +0000</pubDate><guid>http://pacocp.es/blog/blog/combine-images-vertically/</guid><description>&lt;p&gt;Recently I needed to combine a bunch of images for a book. Since I didn&amp;rsquo;t want to use a software I thought it would be easier to combine them using Python with the following script (thanks to &lt;a href="https://pythonprogramming.altervista.org/join-all-images-vertically-or-horizontally/?doing_wp_cron=1576619477.2015759944915771484375"&gt;Python Programming&lt;/a&gt;):&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nn"&gt;np&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;PIL&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Image&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;glob&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;list_im&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;glob&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;glob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;*.png&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;imgs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;Image&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;list_im&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# choose the minimum size and resize the other ones&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;min_shape&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;[(&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;imgs&lt;/span&gt;&lt;span class="p"&gt;])[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;imgs_comb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;vstack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;asarray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;min_shape&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;imgs&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;imgs_comb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Image&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fromarray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="n"&gt;imgs_comb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;imgs_comb&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;imagename.png&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item><item><title>Moving from Tensorflow 2.0 to Pytorch</title><link>http://pacocp.es/blog/blog/moving-from-tensorflow-2.0-to-pytorch/</link><pubDate>Tue, 17 Dec 2019 00:00:00 +0000</pubDate><guid>http://pacocp.es/blog/blog/moving-from-tensorflow-2.0-to-pytorch/</guid><description>&lt;p&gt;I was having many issues with GPU memory management in a Transfer Learning problem when using Tensorflow 2.0 with Keras module. Even though I was using Inception V3 model with all the weights of the architecture freezed, the code ran out of memory. The weights of the model occupied only 98 MB therefore that wasn&amp;rsquo;t the problem. Input shape was (299, 299, 3) so not huge images were being used, but I couldn&amp;rsquo;t use a batch size higher than 16.&lt;/p&gt;</description></item><item><title>Parallel training in Tensorflow 2.0</title><link>http://pacocp.es/blog/blog/parallel-training-in-tensorflow-2.0/</link><pubDate>Tue, 17 Dec 2019 00:00:00 +0000</pubDate><guid>http://pacocp.es/blog/blog/parallel-training-in-tensorflow-2.0/</guid><description>&lt;p&gt;Recently I have been able to use 2 GPUs for model training. I was having problems while training with both of them (only one of them was being used).&lt;/p&gt;&#10;&lt;p&gt;At the end, thanks to this &lt;a href="https://github.com/tensorflow/tensorflow/issues/30321"&gt;issue thread&lt;/a&gt;, I knew that with the new eager mode, there exist a bug for multi gpu training.&lt;/p&gt;&#10;&lt;p&gt;In order to fix it, you need to disable eager mode using the following command:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;tensorflow&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nn"&gt;tf&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;tf&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compat&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;disable_eager_execution&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description></item><item><title>Using glob for file retrieval</title><link>http://pacocp.es/blog/blog/using-glob-for-file-retrieval/</link><pubDate>Thu, 12 Dec 2019 00:00:00 +0000</pubDate><guid>http://pacocp.es/blog/blog/using-glob-for-file-retrieval/</guid><description>&lt;h1 id="when-is-glob-useful"&gt;When is &lt;em&gt;glob&lt;/em&gt; useful?&lt;/h1&gt;&#10;&lt;p&gt;Usually I need to iterate over lots of CSV files. Imaging that you have loads of CSV files which contains the EMG (declared as an array of floats) information of different patients. You want to read them with Python&amp;rsquo;s library &lt;a href="https://pandas.pydata.org/"&gt;Pandas&lt;/a&gt;. Having to declare each CSV by hand and then pass it to Pandas would be utterly painfull. Insted, there exist an amazing Python library called &lt;a href="https://docs.python.org/3.6/library/glob.html"&gt;glob&lt;/a&gt; that helps with this task.&lt;/p&gt;</description></item></channel></rss>