Skip to content

Instantly share code, notes, and snippets.

View Yuktha-Majella's full-sized avatar

Yuktha-Majella

View GitHub Profile
@Yuktha-Majella
Yuktha-Majella / Load_Resnet50
Last active August 10, 2021 16:24
Importing Resnet50 model from FastAI library
from fastai.vision import *
from fastai.vision.models import resnet50
@Yuktha-Majella
Yuktha-Majella / Data_augmentation
Last active August 10, 2021 16:26
Data Augmentation using ImageDataBunch to increase the size of Training set
tfms = get_transforms(do_flip=True, flip_vert=True, max_rotate=50, max_lighting=0.1, max_warp=0 )
data = ImageDataBunch.from_df('/content/drive/MyDrive/CV_Vehicle_classification/train_data/images', train, ds_tfms=tfms, label_delim= None, valid_pct=0.2, fn_col=0, label_col=1 , size=299,bs=64).normalize(imagenet_stats)
@Yuktha-Majella
Yuktha-Majella / Model_train
Last active August 10, 2021 17:28
Training the Resnet50 model initially for 5 epochs
t_cnn1 = cnn_learner(data, resnet50, pretrained=True, metrics=[accuracy])
t_cnn1.fit_one_cycle(5)
@Yuktha-Majella
Yuktha-Majella / model_train_unfreeze
Last active August 10, 2021 17:29
Training the model again after unfreezing layers
t_cnn1.unfreeze()
t_cnn1.fit_one_cycle(8)
@Yuktha-Majella
Yuktha-Majella / Finding_LR
Last active August 10, 2021 17:32
Use the Learning Rate finder and LR vs Loss plot to get an idea on what learning rate best suits our model
t_cnn1.lr_find()
t_cnn1.recorder.plot()
@Yuktha-Majella
Yuktha-Majella / freeze_model
Created June 10, 2021 17:11
Freezing the trained model and exporting it
t_cnn1.fit_one_cycle(10,max_lr=slice(1e-5, 1e-4))
t_cnn1.freeze()
t_cnn1.export('/content/drive/MyDrive/CV_Vehicle_classification/model/Bmodel_fastai_resnet50.h5')
@Yuktha-Majella
Yuktha-Majella / Predication
Last active June 10, 2021 18:07
Prediction using the trained model
test_data = ImageList.from_df(test, cols=['image_names'], path='/content/drive/MyDrive/CV_Vehicle_classification/train_data/images')
t_rn50 = load_learner('/content/drive/MyDrive/CV_Vehicle_classification/model/', 'Bmodel_fastai_resnet50.h5', test = test_data)
y_trn50 = t_rn50.TTA(ds_type = DatasetType.Test)
preds = y_trn50[0].argmax(-1)
@Yuktha-Majella
Yuktha-Majella / create_dict
Created August 7, 2021 15:54
Creating a dictionary in Gensim
import gensim
from gensim import corpora
text1 = ["""Gensim is a free open-source Python library for representing documents as semantic vectors,
as efficiently and painlessly as possible. Gensim is designed
to process raw, unstructured digital texts using unsupervised machine learning algorithms."""]
tokens1 = [[item for item in line.split()] for line in text1]
g_dict1 = corpora.Dictionary(tokens1)
@Yuktha-Majella
Yuktha-Majella / create_dict_textfile
Created August 7, 2021 16:03
Create dictionary from text file in Gensim
from gensim.utils import simple_preprocess
from gensim import corpora
text2 = open('sample_text.txt', encoding ='utf-8')
tokens2 =[]
for line in text2.read().split('.'):
tokens2.append(simple_preprocess(line, deacc = True))
g_dict2 = corpora.Dictionary(tokens2)
@Yuktha-Majella
Yuktha-Majella / dict_update
Created August 7, 2021 16:05
Update existing dictionary with new document gensim
g_dict1.add_documents(tokens2)
print("The dictionary has: " +str(len(g_dict1)) + " tokens\n")
print(g_dict1.token2id)