Skip to content

Instantly share code, notes, and snippets.

View khalidmeister's full-sized avatar

Irfan Alghani Khalid khalidmeister

View GitHub Profile
@khalidmeister
khalidmeister / import.r
Created May 10, 2020 09:46
Tidy Data: Why We Should Invest On It and How to Do it
library(readr)
confirmed <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")
head(confirmed)
@khalidmeister
khalidmeister / transform-data.py
Created June 23, 2020 14:41
Transform Image Data For The Model
import numpy as np
import time
import copy
import os
import torch
import torch.optim as optim
import torch.nn as nn
import torchvision
import matplotlib.pyplot as plt
from torch.optim import lr_scheduler
# Load the model (Download only at first time loading the model)
model_resnet = models.resnet18(pretrained=True)
model_vgg16 = models.vgg16(pretrained=True)
model_alexnet = models.alexnet(pretrained=True)
# Define the function to train the model
def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
since = time.time()
best_model_wts = copy.deepcopy(model.state_dict())
@khalidmeister
khalidmeister / test-model.py
Last active June 24, 2020 04:17
Testing the model and show the result
# Show the images and the ground truth
dataiter = iter(dataloaders['test'])
images, labels = dataiter.next()
imshow(torchvision.utils.make_grid(images))
print('GroundTruth: ', ', '.join('%5s' % class_names[x] for x in labels))
# Predict the model
images = images.to(device)
labels = images.to(device)
output = model_resnet(images)
@khalidmeister
khalidmeister / save.model.py
Created June 23, 2020 14:55
Save the model
# Save The Model
PATH = './fix_alexnet_net.pth'
torch.save(model_alexnet.state_dict(), PATH)
@khalidmeister
khalidmeister / load-model.py
Created June 23, 2020 14:57
Load the model
# Declare the network
net = Net()
# Set the parameters
net.load_state_dict(torch.load(PATH))
@khalidmeister
khalidmeister / app.py
Created June 24, 2020 07:06
The Main Process on A Web Application
import io
import string
import torch
import torch.nn as nn
import torchvision.transforms as transforms
from torchvision import models
from flask import Flask, jsonify, request, render_template
from PIL import Image
app = Flask(__name__)
@khalidmeister
khalidmeister / layout.html
Created June 24, 2020 07:52
The template for our web page
<!DOCTYPE HTML>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
@khalidmeister
khalidmeister / index.html
Created June 24, 2020 07:55
The Home Page
{% extends "layout.html" %}
{% block content %}
<form class="form-signin" method=post enctype=multipart/form-data>
<h1 class="h2 mb-3 font-weight-normal">Apple Leaf Disease Classifier</h1>
<h2 class="h4 mb-3 font-weight-normal">Please Upload The Image</h2>
<input type="file" name="file" class="form-control-file" id="inputfile" onchange="preview_image(event)">
<br />
<img id="output-image" class="rounded mx-auto d-block"/>
<button class="btn btn-lg btn-primary btn-block" type="submit">Upload</button>
<p class="mt-5 mb-3 text-muted">Powered by PyTorch, Flask, and Bootstrap.</p>
@khalidmeister
khalidmeister / result.html
Created June 24, 2020 07:56
Showing the result of our prediction
{% extends "layout.html" %}
{% block content %}
<form class="form-signin" method=post enctype=multipart/form-data>
{% if name == "healthy"%}
<h1 class="h1 mb-3 font-weight-normal"> Your plant is healthy! </h1>
{% else %}
<h1 class="h1 mb-3 font-weight-normal"> Your leaf got a {{ name }} disease!</h5>
<h3 class="h5 mb-3 font-weight-normal"> {{ description }} </p>
{% endif %}
<p class="mt-5 mb-3 text-muted">Powered by PyTorch, Flask, and Bootstrap.</p>