avatarMarilyn Flower

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>

Week 2, Day 3

Time Travel — the Latest and Greatest Solution to Your and the World’s Problems

Don’t delay — sign up today!

Photo by Taylor Simpson on Unsplash

Hey, Simão, you’re onto something!

Time travel just might be the panacea for what ails you and me and the world! Thanks for mentioning it!

After all, we have a population explosion going on right now. If we could thin things out a bit, the rest of us could breathe a little easier.

If we could relocate about a third of the world’s population to a time and place that has lots of room for them, maybe before cities were a thing, think of the water, electricity, and carbon footprints we’d save!

So maybe, just maybe, time travel is good for the planet.

There are so many fascinating places and times to visit, who wouldn’t want to help the planet by relocating? Can I get an amen?

Yes, conditions were more primitive back then. Even twenty years ago. Sans iPhones, tablets, and insta-everything.

But the air was purer, the produce more nutritious, and life was simpler.

Even a short stint under those conditions might help us slow down, take deeper breaths, relax more, and live longer.

We’d have to do more chores. More work. Just to run a household. Grow our own veggies. Sheer our own sheep. Churn our own butter. Walk into town for soap and ale. But think of what that would do for our waistlines and metabolism.

Photo by Ambitious Creative Co. - Rick Barrett on Unsplash

We’d get more sleep.

No TV’s, no radio, no computers to zap us with whatever that element is that keeps us riveted, distracted, and sleepless. No electricity to extend the day beyond dusk.

Unless we venture to the local tavern, or midsummer festival bonfire, or a star-light vigil of some kind, the logical thing to do would be to hunker- bunker down for the night.

Our bodies would thank us.

While we could never quite make up for the sum total of years of sleep we’ve lost living in the here and now, we’d return refreshed and rejuvenated.

Our circadian rhythms would be reset and we’d have an easier time sleeping — as long as we resist the huge electronic temptations waving at us like sirens of old.

They say it only takes 21 days to undo a bad habit and replace it with a new one. Who’s game?

Photo by Library of Congress on Unsplash

Mother Earth wants you!

So even if it’s a temporary tour of duty, think of it like you would enlisting in the military. Yes, there’s some danger involved, but honestly, there are so many dangers involved in the here and now, you’re no worse off and you might be exponentially better!

Plus, like the military, we can incentivize the opportunity with something like free college tuition or a lifetime subscription to Netflix so you can catch up on all the blockbusters you missed. Unless you succeed in swearing off electronics like mentioned earlier.

Clearly, the benefits outweigh the risks.

But wait! There’s even more!

You have a chance to be a hero or heroine and have a lasting impact on society. Who wouldn’t jump at the chance to go back there with what we know now and enlighten the ignoramuses?

This is way bigger than Peggy in Peggy Sue got Married trying to sew two nylons together and grace 1960 with the girdle-less stockings known as pantyhose.

You can make a significant difference!

How would you like to be part of the third medieval renaissance in Europe where art, poetry, and education flourished? Women like Hildegard of Bingen stood up to popes and emperors, telling them off like an older, clerical version of Greta Thunberg, referring to Christ as the Green Man, urging respect for the planet back in the 1100s.

Photo by Vince Veras on Unsplash

Just think, if you were part of the movement to get us on track with that agenda, our planet might not have to die.

Not only that, she and her sisters of the cloth wrote music and books, painted, ran a hospital practicing herbal medicine, and was respected and sought after for all of the above. Surely you and your talents could blossom in such a time as that was.

Or More Recently…

How would you like to be part of the framing of the U.S. Constitution and tell those puffed-up revolutionary war heroes that King Cotton to the contrary, slavery is not a good idea? It will lead to the worst Civil War in history and damage our rep as a leading light of liberty. Not to mention, it’s just plain wrong!

Photo by Unseen Histories on Unsplash

We sure could have used some more abolitionists. And suffragettes. And freedom fighters. But not too many. We still need a lot of you for unfinished battles in the here and now. Voter’s rights for instance. But you can go back there and learn, then come here and do!

If we had some folks who would be willing to transport back to the early 1930s in Germany with what we know now, maybe history would not have gone the way it did.

It would take a lot of you, but that’s the whole idea. Strength in numbers then, more wiggle room now. We’re all gonna die anyway, why not make it momentous.

Of course, there would be dozens of literary/artistic excursions. Paris in the 20s. Mexico City in the 40s with Frida and Diego.

Learn from ancient cultures: Greece comes to mind, but also Mesopotamia. The Samurais of Japan. Ancient Persia with Rumi. Africa before the European invasion. Anywhere before the European invasion. America before the first or the second British invasions. Lots of choices!

Time travel also includes the future.

Any takers? No? Aren’t you at least curious to see how we get out of COVID, racial violence, and the climate mess we’re in?

Photo by Jordan Opel on Unsplash

Wouldn’t you want to fast forward to a future where an elder Ms. Greta is the head of the United Nations, gas-guzzling jalopies are on display in the Smithsonian, and at the center of every settlement is a huge community organic garden, solar plant, wind farm, and a gym where folks on treadmills generate power for the town while working out?

However, you’ve got to set the dials just right. Too soon and you end up smack dab in the eminent cataclysm we’re hoping to avoid, or are busy denying is coming.

Photo by Hans-Jurgen Mager on Unsplash

That’s that part where the waters rise, extinction goes off the charts, breadbaskets turn into deserts and COVID turns into a multi-generational plague. Sound familiar?

Do try to skip this one. It doesn’t need any more mouths to feed and water.

But with that one small caveat, time travel is a win-win. Who’s ready to sign up?

Thank you, Diana C. and Simão Cunha for this timeless prompt!

Marilyn Flower writes political humor and satire to delight socially and spiritually conscious folks. She’s a regular columnist for the prison newsletter, Freedom Anywhere, where she writes about faith and prayer. Five of her short plays have been produced in San Francisco. Clowning and improvisation strengthen her resolve during these crazy times. Stay in touch!

Time Travel
History
Health
Humor
Satire
Recommended from ReadMedium