avatarhejrene | Rene

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2746

Abstract

n library. This library contains a lot of DNN models. Since the task is a 10 class classification task but the model from the library is based on the ImageNet dataset which is a 1000 class classification task I will be changing the very last fully connected layer to replace the 1000 output nodes to 10.</p><div id="8d8c"><pre>model = torchvision.models.resnet18().to(<span class="hljs-string">'cuda'</span>) model.fc = torch.nn.Linear(in_features=<span class="hljs-number">512</span>, out_features=<span class="hljs-number">10</span>, <span class="hljs-comment"># same number of output units as our number of classes</span> bias=<span class="hljs-literal">True</span>).to(<span class="hljs-string">'cuda'</span>)</pre></div><p id="a557">Here, I changed the number of output nodes to 10 based on the layer name. Most of the tutorials or blogs doesn’t say how you get the name of the layer. For different models it is often hard to find. Also what is the number of in_features is another concern. You can use summary from torchinfo to get these information.</p><div id="c45c"><pre>summary(model=model, input_size=(<span class="hljs-number">32</span>, <span class="hljs-number">3</span>, <span class="hljs-number">32</span>, <span class="hljs-number">32</span>), col_names=[<span class="hljs-string">"input_size"</span>, <span class="hljs-string">"output_size"</span>, <span class="hljs-string">"num_params"</span>, <span class="hljs-string">"trainable"</span>], col_width=<span class="hljs-number">20</span>, row_settings=[<span class="hljs-string">"var_names"</span>] )</pre></div><p id="47b0">You need to pass the model and the input size of the dataset that is going to be passed to the model in the summary. This will help generate a random dataset and show you the name of the layers, number of parameters and whether the layers are trainable or not. <i>The image below is for a VGG model not the ResNet-18 model. It was huge for ResNet-18 when printed out like this, so for reference only I am showing you an example of the VGG model.</i></p><figure id="f175"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*5lr_YZ5eSAGWcbe0R6lIrg.png"><figcaption>Visualizing the model using summary</figcaption></figure><p id="0d6f">Now that we have our model we just train the model using the CIFAR-10 data. Detailed implementation can be found <a href="https://github.com/aminul-huq/medium">here</a>. After training for 10 epochs we will find out that the testing accuracy is 45.80%. We can visualize how the model learned in each epoch using the loss vs epoch and accuracy vs epoch curve shown below.</p><figure id="4f44"><img src="https://cdn-images-1.readmediu

Options

m.com/v2/resize:fit:800/1*A4j7TEBm4nCS468NxnHTMg.png"><figcaption>Without pretraining.</figcaption></figure><p id="1995">Let’s now experiment with pre-training and freezing some layers. Now I will be changing the model initialization slightly. I will be using the pre-training weights of the ImageNet dataset which is available in pytorch and in order to use it we just need to set ‘pretrained=True’. However, there are other ways to do the same thing.</p><div id="3adc"><pre>model = torchvision.models.resnet18(pretrained=<span class="hljs-literal">True</span>).to(<span class="hljs-string">'cuda'</span>) model.fc = torch.nn.Linear(in_features=<span class="hljs-number">512</span>, out_features=<span class="hljs-number">10</span>, bias=<span class="hljs-literal">True</span>).to(<span class="hljs-string">'cuda'</span>)</pre></div><div id="3b60"><pre><span class="hljs-keyword">for</span> name, param <span class="hljs-keyword">in</span> model.named_parameters(): <span class="hljs-keyword">if</span> name[<span class="hljs-number">5</span>] < <span class="hljs-string">'2'</span>: param.requires_grad = <span class="hljs-literal">False</span></pre></div><p id="4d2a">In the code snippet above, based on the the name of each of the layers I am actually freezing several initial layers by setting the ‘requires_grad=False’. What it does is that, this makes sure these layers are not trained and preserve the assigned weights. If you pass the model to summary after these lines of code you will find that now the trainable column has several False values which was not the case previously.</p><p id="d3f5">Now if we retrain the model from the start using the same hyper-parameters we will see that after 10 epochs we are getting 62.01% of accuracy on the testing data which is more than 15% than before. We can visualize the loss and accuracy curve of this model below.</p><figure id="882c"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*aArjzOgvEP6Su6WNEy8Pxw.png"><figcaption>With pre-training and freezing layers</figcaption></figure><p id="d7c1">Based on this graph and the one before we can see the mode with pre-trained weights are doing much better in terms of training and validation data. After 10 epoch the loss value is much lower and the accuracy is much higher.</p><p id="ee64">I hope this blog helped you out in some extend to understand the concept of pre-training and finetuning. Detailed implementation can be found <a href="https://github.com/aminul-huq/medium">here</a>.</p><blockquote id="648f"><p>If you have any difficulty understanding anything or want to reach out to me for any question shoot me an email at [email protected].</p></blockquote></article></body>

Learn how selective subscriptions in tech, wellness, and finance can enrich your life and streamline your day.

Top 5 Must-Have (Apple) Subscriptions: My Picks I Keep Paying For in 2024

As I’ve mentioned previously, I’m generally not fond of subscription-based apps, given my background in purchasing software outright with the option for occasional updates or new versions (yes I am a 90s kid). However, I’ve found myself regularly using a handful of subscription services that enhance my life and daily routines.

Image by the author, created with Dall-E 3.

Over the years, I’ve experimented with numerous apps and services, but as of 2024, here are the five that I continue to invest in due to their significant value:

Headspace

Headspace is a mindfulness app offering a variety of courses on meditation, sleep, focus, work, and physical activity. Narrated by co-founder and former Buddhist monk Andi Puddicombe, it begins with a free trial and provides over 400 meditation sessions upon subscription. Features include guided meditation, sleep assistance, physical exercises integrating breathing techniques, and a focus mode with meditation tracks and music.

Headspace website — screenshot by the author.

I started using Headspace early on, when it was relatively unknown. I don’t quite recall how I first came across it, but it immediately struck me as the ideal service. I’ve extensively used various courses (ranging from 10 to 30 days), but these days I prefer silent meditations now. But I return to my favorite courses occasionally and enjoy the sleep sounds for better rest.

A slight inconvenience with Headspace is that you can’t restart courses mid-way, which isn’t a major issue, but a reset option would be handy. At present, Headspace is available for $12.99 monthly or $69.99 annually.

Blinkist

Blinkist offers 15-minute nonfiction book summaries, called “blinks,” in text and audio format. It covers a wide range of topics across 19 categories, such as business, personal development, and science. The app is designed for busy professionals to get key insights from popular books quickly.

While these summaries serve as a preliminary overview, they have led me to discover numerous valuable books for my Kindle. Even in cases where a full book doesn’t seem worth the purchase, the insights learned from Blinkist’s summaries are often of incredible value.

It’s $12,99 and the annual plan is much cheaper.

Apple One Premier

I’m deeply invested in the Apple ecosystem due to its exceptional integration and ease of use, almost like magic. The continual improvements in Apple’s technology have made their services and devices work seamlessly together. Therefore, subscribing to Apple One Premium, which offers a variety of Apple services at a reduced price, was an obvious choice for me.

Apple One Website — screenshot by the author.

This bundle includes Apple Music, Apple TV+, Apple Arcade, Apple News+, Fitness+, and 2TB iCloud storage. Some of the services are only in select teritorries available though (ie. Apple News+).

I primarily use it for cloud storage and Apple Music for my family, and I’ve grown fond of Fitness+, especially for yoga workouts. After a recent price increase to $37.95, I find it still worthwhile, though any further price rises might lead me to downgrade.

YouTube Premium

Deciding to subscribe to YouTube Premium involved a lengthy debate, but ultimately, I chose to go for it. The service costs $13.99 monthly for an individual plan or $22.99 for a family subscription.

The primary reason for this decision was to avoid the constant ads during daily streaming. YouTube Premium also offers other features such as the ability to download videos, which is particularly useful for travel, and the option to play videos in the background or in picture-in-picture mode. Both are two features we do not want to miss anymore.

getquin

GetQuin, launched in 2020, is a real-time portfolio tracker designed to streamline investment management and decision-making. It offers a range of features including real-time tracking, detailed portfolio analysis, a community of investors, and customizable options.

Getquin website — screenshot by the author.

I primarily use GetQuin to monitor my dividends, a feature my regular broker doesn’t provide, and to engage with other investors. Initially, I didn’t think I needed an app like this, but having used it, I find it essential to my investment strategy. GetQuin has become an daily tool in managing my investments and best of all (as of writing) the basic features are free to use.

Conclusion

In conclusion, despite my initial hesitation towards subscription models (coming from a time where you just “bought” software once), the consistent value these five services provide has made them essential in my daily life. They offer a balance of mental health support, efficient learning, ad-free entertainment, and effective investment tracking.

However, it’s crucial to remember that subscribing to services shouldn’t be done impulsively. It’s important to carefully consider the value each service provides and whether it genuinely meets your needs. Thoughtful consideration ensures that your investments in these services are worthwhile and align with your lifestyle, spending options and goals.

Hej there! Can I ask you a favour (it will really help me out to grow this blog)?

If you find this article insightful and enjoy reading it, consider following me please for more content about Apple, Mem, productivity, and the general landscape of AI, as they help me reach a wider audience, enabling others to benefit from my stories and personal experiences. Thanks for reading and enjoy your day!

More to read:

Apple
Personal Development
Personal Growth
Investment
Apps
Recommended from ReadMedium