Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2022-174
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Abdurrahumaan A N
2022-174
Commits
1d64ea33
Commit
1d64ea33
authored
Apr 09, 2022
by
nazeerxexagen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commit image classification phase 1
parent
6d68eb3f
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
178 additions
and
0 deletions
+178
-0
backend/ImageSearch.ipynb
backend/ImageSearch.ipynb
+178
-0
No files found.
backend/ImageSearch.ipynb
View file @
1d64ea33
...
...
@@ -452,6 +452,184 @@
"source": [
"## **STEP 2 : Define Loss / Optimizer / Metrics**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"optimizer = optim.Adam(\n",
" model.parameters(), \n",
" lr = learning_rate\n",
" )\n",
"\n",
"class_weights = dataset.compute_class_weights()\n",
"\n",
"criterion = nn.CrossEntropyLoss(weight = class_weights)\n",
"\n",
"def accuracy(Y, Y_hat):\n",
" correct_pairs = torch.sum(Y == Y_hat).item()\n",
" return correct_pairs/ len(Y_hat)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## **STEP 3 : Model Training**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"Train_Loss = []\n",
"Train_Accuracy = []\n",
"\n",
"Test_Loss = []\n",
"Test_Accuracy = []\n",
" \n",
"def train_loop(): \n",
" for epoch in range(epoches):\n",
"\n",
" train_loss = 0\n",
" train_accuracy = 0 \n",
"\n",
" test_loss = 0\n",
" test_accuracy = 0 \n",
"\n",
" model.train()\n",
"\n",
" for Xtrain, Ytrain in tqdm.tqdm(train_loader):\n",
"\n",
" optimizer.zero_grad()\n",
" \n",
" Xtrain = Xtrain.to(device)\n",
" Ytrain = Ytrain.type(torch.LongTensor).to(device)\n",
"\n",
" Ytrain_hat = model(Xtrain).to(device)\n",
"\n",
" loss = criterion(Ytrain_hat, Ytrain) \n",
" loss.backward()\n",
"\n",
" optimizer.step()\n",
"\n",
" Ytrain_hat = torch.argmax(Ytrain_hat, dim=1)\n",
" Ytrain_hat = Ytrain_hat.type(torch.LongTensor).to(device)\n",
"\n",
" train_loss += loss.item()\n",
" train_accuracy += accuracy(Ytrain, Ytrain_hat)\n",
"\n",
" train_loss /= len(train_loader)\n",
" train_accuracy /= len(train_loader)\n",
"\n",
" Train_Loss.append(train_loss)\n",
" Train_Accuracy.append(train_accuracy)\n",
"\n",
" model.eval()\n",
"\n",
" with torch.no_grad():\n",
" for Xtest, Ytest in test_loader:\n",
"\n",
" Xtest = Xtest.to(device)\n",
" Ytest = Ytest.type(torch.LongTensor).to(device)\n",
"\n",
" Ytest_hat = model(Xtest).to(device)\n",
"\n",
" loss = criterion(Ytest_hat, Ytest) \n",
"\n",
" Ytest_hat = torch.argmax(Ytest_hat, dim=1)\n",
" Ytest_hat = Ytest_hat.type(torch.LongTensor).to(device)\n",
"\n",
" test_loss += loss.item()\n",
" test_accuracy += accuracy(Ytest, Ytest_hat)\n",
"\n",
" test_loss /= len(test_loader)\n",
" test_accuracy /= len(test_loader)\n",
"\n",
" Test_Loss.append(test_loss)\n",
" Test_Accuracy.append(test_accuracy)\n",
"\n",
" print('Epoch : {} --> Train Loss : {} --> Train Accuracy --> {} Test Loss --> {} Test Accuracy --> {}'.format(\n",
" epoch + 1, \n",
" round(train_loss, 3),\n",
" round(train_accuracy, 3), \n",
" round(test_loss, 3),\n",
" round(test_accuracy, 3)\n",
" ))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# **STEP 4 : Saving / Loading Model for Inference**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"def save_model(model, PATH):\n",
" torch.save(model.state_dict(), PATH)\n",
" \n",
"def load_model(model, PATH):\n",
" model.load_state_dict(torch.load(PATH))\n",
" model = model.to(device)\n",
" model.eval()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# **STEP 5 : Visualize Performance Metrices**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "plaintext"
}
},
"outputs": [],
"source": [
"def visualize_performance():\n",
" plt.plot(Train_Loss, color = 'r', label='train')\n",
" plt.plot(Test_Loss, color = 'b', label='test')\n",
" plt.xlabel('Epoch ID')\n",
" plt.ylabel('Loss')\n",
" plt.title('Loss Vairation')\n",
" plt.legend()\n",
" plt.show()\n",
" \n",
" plt.plot(Train_Accuracy, color = 'r', label='train')\n",
" plt.plot(Test_Accuracy, color = 'b', label='test')\n",
" plt.xlabel('Epoch ID')\n",
" plt.ylabel('Accuracy')\n",
" plt.title('Accuracy Vairation')\n",
" plt.legend()\n",
" plt.show()"
]
}
],
"metadata": {
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment