Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

æpk #83

Open
wants to merge 77 commits into
base: master
Choose a base branch
from
Open

æpk #83

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
90b67f2
fixed to use wingr
anderf2706 Sep 4, 2020
c6cf78f
fixed to use wingr
anderf2706 Sep 4, 2020
2c516f7
fixed to use wingr
anderf2706 Sep 7, 2020
7977cdb
fixed to use wingr
anderf2706 Sep 7, 2020
51e81a6
fixed to use wingr
anderf2706 Sep 7, 2020
48d47d2
changing to ML_project
anderf2706 Nov 2, 2020
774d2cc
changing to ML_project
anderf2706 Nov 2, 2020
b02f1fa
changing to ML_project
anderf2706 Nov 2, 2020
76ce67a
changing to ML_project
anderf2706 Nov 2, 2020
d491af5
changing to ML_project
anderf2706 Nov 2, 2020
acb019f
changing to ML_project
anderf2706 Nov 2, 2020
b603a3f
changing to ML_project
anderf2706 Nov 2, 2020
6aebc83
changing to ML_project
anderf2706 Nov 2, 2020
c08f366
changing to ML_project
anderf2706 Nov 2, 2020
6854642
changing to ML_project
anderf2706 Nov 2, 2020
582b4d6
Update README.md
anderf2706 Nov 3, 2020
e677975
better CNN model
anderf2706 Nov 6, 2020
18b4ded
Merge remote-tracking branch 'origin/master'
anderf2706 Nov 6, 2020
76ea5f4
trying to ad KNN to website
anderf2706 Nov 7, 2020
18bd32f
trying to ad KNN to website
anderf2706 Nov 7, 2020
7d0bc28
trying to ad KNN to website
anderf2706 Nov 7, 2020
62764d2
trying to ad KNN to website
anderf2706 Nov 7, 2020
b9e9813
rollback
anderf2706 Nov 7, 2020
3cfa83f
rollback
anderf2706 Nov 7, 2020
6e65a1a
rollback
anderf2706 Nov 9, 2020
435050f
rollback
anderf2706 Nov 9, 2020
4cb330c
rollback
anderf2706 Nov 9, 2020
dbaa4ce
rollback
anderf2706 Nov 9, 2020
7a71067
rollback
anderf2706 Nov 9, 2020
8ce5476
integrating knn
anderf2706 Nov 10, 2020
e5fcc7a
integrating knn
anderf2706 Nov 10, 2020
ed9d566
Revert "integrating knn"
anderf2706 Nov 10, 2020
6593f4b
Revert "integrating knn"
anderf2706 Nov 10, 2020
e09724b
Revert "integrating knn"
anderf2706 Nov 10, 2020
9bbaaf7
Revert "integrating knn"
anderf2706 Nov 10, 2020
54059bc
Revert "integrating knn"
anderf2706 Nov 10, 2020
49e2b00
Revert "integrating knn"
anderf2706 Nov 10, 2020
1aede98
Revert "integrating knn"
anderf2706 Nov 10, 2020
22c3ace
Revert "integrating knn"
anderf2706 Nov 10, 2020
16dd187
Revert "integrating knn"
anderf2706 Nov 10, 2020
32f97e2
Revert "Revert "integrating knn""
anderf2706 Nov 10, 2020
c192158
Revert "Revert "integrating knn""
anderf2706 Nov 10, 2020
7dc71a7
Revert "Revert "integrating knn""
anderf2706 Nov 10, 2020
a5a013a
knn
anderf2706 Nov 10, 2020
4b27878
knn
anderf2706 Nov 10, 2020
c4146b8
knn
anderf2706 Nov 10, 2020
5ec37ec
knn
anderf2706 Nov 10, 2020
cd6c69c
Made the website pretty
Nov 16, 2020
2224afe
Merge conflict solved
Nov 16, 2020
1cc7c0e
Fixed analyze button
Nov 16, 2020
e30c6fb
testingfesting
Nov 16, 2020
55a91ec
update to analyze button
anderf2706 Nov 17, 2020
85d32ea
update to analyze button
anderf2706 Nov 17, 2020
5a0febe
update to analyze button
anderf2706 Nov 17, 2020
f03f681
update to analyze button
anderf2706 Nov 17, 2020
6b472a8
update to analyze button
anderf2706 Nov 17, 2020
0de0ce0
update to analyze button
anderf2706 Nov 17, 2020
51bab73
fixes to UI, looks
anderf2706 Nov 26, 2020
797f3d5
fixes to UI, looks
anderf2706 Nov 26, 2020
bdaf6e4
fixes to UI, looks
anderf2706 Nov 26, 2020
8a2c13b
fixes to UI, looks
anderf2706 Nov 26, 2020
234041e
fixes to UI, looks
anderf2706 Nov 26, 2020
95edbd5
fixes to UI, looks
anderf2706 Nov 26, 2020
1b1e4a7
fixes to UI, looks
anderf2706 Nov 26, 2020
643bdf9
fixes to UI, looks
anderf2706 Nov 26, 2020
e570403
fixes to UI, looks
anderf2706 Nov 28, 2020
a20e2a6
fixes to UI, looks
anderf2706 Nov 28, 2020
bad4a00
fixes to UI, looks
anderf2706 Dec 14, 2020
e2538f0
fixes to UI, looks
anderf2706 Dec 14, 2020
f4a28ac
fixes to UI, looks
anderf2706 Dec 14, 2020
624268a
fixes to UI, looks
anderf2706 Dec 15, 2020
229a1c3
fixes to UI, looks
anderf2706 Dec 15, 2020
64afa5a
fixes to UI, looks
anderf2706 Dec 15, 2020
f7a39c3
fixes to UI, looks
anderf2706 Dec 16, 2020
9265a1e
fixes to UI, looks
anderf2706 Dec 20, 2020
ede7f41
fixes to UI, looks
anderf2706 Dec 20, 2020
6e364db
fixes to UI, looks
anderf2706 Dec 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore

This file was deleted.

3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions .idea/fastai-v3.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/libraries/R_User_Library.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1 @@
# Starter for deploying [fast.ai](https://www.fast.ai) models on [Render](https://render.com)

This repo can be used as a starting point to deploy [fast.ai](https://github.com/fastai/fastai) models on Render.

The sample app described here is up at https://fastai-v3.onrender.com. Test it out with bear images!

You can test your changes locally by installing Docker and using the following command:

```
docker build -t fastai-v3 . && docker run --rm -it -p 5000:5000 fastai-v3
```

The guide for production deployment to Render is at https://course.fast.ai/deployment_render.html.

Please use [Render's fast.ai forum thread](https://forums.fast.ai/t/deployment-platform-render/33953) for questions and support.
Binary file added app/models/export.pkl
Binary file not shown.
Binary file added app/models/model.sav
Binary file not shown.
105 changes: 91 additions & 14 deletions app/server.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,60 @@
import aiohttp
import asyncio
import uvicorn
from fastai import *
from fastai.vision import *
from io import BytesIO
from starlette.applications import Starlette
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import HTMLResponse, JSONResponse
from starlette.staticfiles import StaticFiles
from PIL import Image as IM

export_file_url = 'https://www.dropbox.com/s/6bgq8t6yextloqp/export.pkl?raw=1'
##rollback
##try
export_file_url = 'https://drive.google.com/uc?export=download&id=1QVxKqLSZwYS42hEoVYztp1awvZX44ppC' #https://drive.google.com/u/0/uc?export=download&confirm=W7Y1&id=1BSva5kuYeZVnsE8M_kwO0QSILdFIgbQC
export_file_name = 'export.pkl'
export_file_name_2 = 'model.sav'

classes = ['black', 'grizzly', 'teddys']
classes = ['NORMAL', 'PNEUMONIA']

"""
['AFRICAN FIREFINCH', 'ALBATROSS', 'ALEXANDRINE PARAKEET', 'AMERICAN AVOCET', 'AMERICAN BITTERN', 'AMERICAN COOT', 'AMERICAN GOLDFINCH',
'AMERICAN KESTREL', 'AMERICAN PIPIT', 'AMERICAN REDSTART', 'ANHINGA', 'ANNAS HUMMINGBIRD', 'ANTBIRD', 'ARARIPE MANAKIN', 'ASIAN CRESTED IBIS',
'BALD EAGLE', 'BALI STARLING', 'BALTIMORE ORIOLE', 'BANANAQUIT', 'BAR-TAILED GODWIT', 'BARN OWL', 'BARN SWALLOW', 'BARRED PUFFBIRD',
'BAY-BREASTED WARBLER', 'BEARDED BARBET', 'BELTED KINGFISHER', 'BIRD OF PARADISE', 'BLACK FRANCOLIN', 'BLACK SKIMMER', 'BLACK SWAN',
'BLACK THROATED WARBLER', 'BLACK VULTURE', 'BLACK-CAPPED CHICKADEE', 'BLACK-NECKED GREBE', 'BLACK-THROATED SPARROW', 'BLACKBURNIAM WARBLER',
'BLUE GROUSE', 'BLUE HERON', 'BOBOLINK', 'BROWN NOODY', 'BROWN THRASHER', 'CACTUS WREN', 'CALIFORNIA CONDOR', 'CALIFORNIA GULL', 'CALIFORNIA QUAIL',
'CANARY', 'CAPE MAY WARBLER', 'CAPUCHINBIRD', 'CARMINE BEE-EATER', 'CASPIAN TERN', 'CASSOWARY', 'CHARA DE COLLAR', 'CHIPPING SPARROW', 'CHUKAR PARTRIDGE',
'CINNAMON TEAL', 'COCK OF THE ROCK', 'COCKATOO', 'COMMON GRACKLE', 'COMMON HOUSE MARTIN', 'COMMON LOON', 'COMMON POORWILL', 'COMMON STARLING',
'COUCHS KINGBIRD', 'CRESTED AUKLET', 'CRESTED CARACARA', 'CROW', 'CROWNED PIGEON', 'CUBAN TODY', 'CURL CRESTED ARACURI', 'D-ARNAUDS BARBET',
'DARK EYED JUNCO', 'DOWNY WOODPECKER', 'EASTERN BLUEBIRD', 'EASTERN MEADOWLARK', 'EASTERN ROSELLA', 'EASTERN TOWEE', 'ELEGANT TROGON',
'ELLIOTS PHEASANT', 'EMPEROR PENGUIN', 'EMU', 'EURASIAN MAGPIE', 'EVENING GROSBEAK', 'FLAME TANAGER', 'FLAMINGO', 'FRIGATE', 'GAMBELS QUAIL',
'GILA WOODPECKER', 'GILDED FLICKER', 'GLOSSY IBIS', 'GOLD WING WARBLER', 'GOLDEN CHEEKED WARBLER', 'GOLDEN CHLOROPHONIA', 'GOLDEN EAGLE',
'GOLDEN PHEASANT', 'GOLDEN PIPIT', 'GOULDIAN FINCH', 'GRAY CATBIRD', 'GRAY PARTRIDGE', 'GREEN JAY', 'GREY PLOVER', 'GUINEAFOWL', 'GYRFALCON',
'HARPY EAGLE', 'HAWAIIAN GOOSE', 'HOODED MERGANSER', 'HOOPOES', 'HORNBILL', 'HORNED GUAN', 'HORNED SUNGEM', 'HOUSE FINCH', 'HOUSE SPARROW',
'IMPERIAL SHAQ', 'INCA TERN', 'INDIAN BUSTARD', 'INDIGO BUNTING', 'JABIRU', 'JAVAN MAGPIE', 'KAKAPO', 'KILLDEAR', 'KING VULTURE', 'KIWI',
'KOOKABURRA', 'LARK BUNTING', 'LEARS MACAW', 'LILAC ROLLER', 'LONG-EARED OWL', 'MALABAR HORNBILL', 'MALACHITE KINGFISHER', 'MALEO', 'MALLARD DUCK',
'MANDRIN DUCK', 'MARABOU STORK', 'MASKED BOOBY', 'MIKADO PHEASANT', 'MOURNING DOVE', 'MYNA', 'NICOBAR PIGEON', 'NORTHERN CARDINAL',
'NORTHERN FLICKER', 'NORTHERN GANNET', 'NORTHERN GOSHAWK', 'NORTHERN JACANA', 'NORTHERN MOCKINGBIRD', 'NORTHERN PARULA', 'NORTHERN RED BISHOP',
'OCELLATED TURKEY', 'OKINAWA RAIL', 'OSPREY', 'OSTRICH', 'PAINTED BUNTIG', 'PALILA', 'PARADISE TANAGER', 'PARUS MAJOR', 'PEACOCK', 'PELICAN',
'PEREGRINE FALCON', 'PHILIPPINE EAGLE', 'PINK ROBIN', 'PUFFIN', 'PURPLE FINCH', 'PURPLE GALLINULE', 'PURPLE MARTIN', 'PURPLE SWAMPHEN', 'QUETZAL',
'RAINBOW LORIKEET', 'RAZORBILL', 'RED FACED CORMORANT', 'RED FACED WARBLER', 'RED HEADED DUCK', 'RED HEADED WOODPECKER', 'RED HONEY CREEPER',
'RED THROATED BEE EATER', 'RED WINGED BLACKBIRD', 'RED WISKERED BULBUL', 'RING-NECKED PHEASANT', 'ROADRUNNER', 'ROBIN', 'ROCK DOVE', 'ROSY FACED LOVEBIRD',
'ROUGH LEG BUZZARD', 'RUBY THROATED HUMMINGBIRD', 'RUFOUS KINGFISHER', 'RUFUOS MOTMOT', 'SAND MARTIN', 'SCARLET IBIS', 'SCARLET MACAW', 'SHOEBILL',
'SMITHS LONGSPUR', 'SNOWY EGRET', 'SNOWY OWL', 'SORA', 'SPANGLED COTINGA', 'SPLENDID WREN', 'SPOON BILED SANDPIPER', 'SPOONBILL', 'STEAMER DUCK',
'STORK BILLED KINGFISHER', 'STRAWBERRY FINCH', 'STRIPPED SWALLOW', 'SUPERB STARLING', 'TAIWAN MAGPIE', 'TAKAHE', 'TASMANIAN HEN', 'TEAL DUCK',
'TIT MOUSE', 'TOUCHAN', 'TOWNSENDS WARBLER', 'TREE SWALLOW', 'TRUMPTER SWAN', 'TURKEY VULTURE', 'TURQUOISE MOTMOT', 'VARIED THRUSH',
'VENEZUELIAN TROUPIAL', 'VERMILION FLYCATHER', 'VIOLET GREEN SWALLOW', 'WATTLED CURASSOW', 'WHIMBREL', 'WHITE CHEEKED TURACO',
'WHITE NECKED RAVEN', 'WHITE TAILED TROPIC', 'WILD TURKEY', 'WILSONS BIRD OF PARADISE', 'WOOD DUCK', 'YELLOW CACIQUE', 'YELLOW HEADED BLACKBIRD']
"""
path = Path(__file__).parent

app = Starlette()
app.add_middleware(CORSMiddleware, allow_origins=['*'], allow_headers=['X-Requested-With', 'Content-Type'])
app.mount('/static', StaticFiles(directory='app/static'))


"""
async def download_file(url, dest):
if dest.exists(): return
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
data = await response.read()
Expand All @@ -30,9 +63,9 @@ async def download_file(url, dest):


async def setup_learner():
await download_file(export_file_url, path / export_file_name)
await download_file(export_file_url, path / export_file_name_2)
try:
learn = load_learner(path, export_file_name)
learn = pickle.load(open((path / 'models/model.sav'), 'rb'))
return learn
except RuntimeError as e:
if len(e.args) > 0 and 'CPU-only machine' in e.args[0]:
Expand All @@ -47,23 +80,67 @@ async def setup_learner():
tasks = [asyncio.ensure_future(setup_learner())]
learn = loop.run_until_complete(asyncio.gather(*tasks))[0]
loop.close()


"""
#hey
#rool
@app.route('/')
async def homepage(request):
html_file = path / 'view' / 'index.html'
return HTMLResponse(html_file.open().read())

@app.route('/aboutus')
async def homepage(request):
html_file = path / 'view' / 'aboutus.html'
return HTMLResponse(html_file.open().read())

@app.route('/aboutp')
async def homepage(request):
html_file = path / 'view' / 'aboutp.html'
return HTMLResponse(html_file.open().read())

@app.route('/analyze', methods=['POST'])
async def analyze(request):
@app.route('/classifier')
async def homepage(request):
html_file = path / 'view' / 'classifier.html'
return HTMLResponse(html_file.open().read())

@app.route('/analyze_cnn', methods=['POST'])
async def analyze_cnn(request):
img_data = await request.form()
img_bytes = await (img_data['file'].read())
img = open_image(BytesIO(img_bytes))
prediction = learn.predict(img)[0]
return JSONResponse({'result': str(prediction)})
learn2 = load_learner(path / 'models', export_file_name)
pred_class, pred_idx, outputs = learn2.predict(img) #[0]
prediction = learn2.predict(img)[0]
pred_probs = outputs/sum(outputs)
pred_probs = pred_probs.tolist()
predictions = []
for image_class, output, prob in zip(learn2.data.classes, outputs.tolist(), pred_probs):
output = round(output, 1)
prob = round(prob, 2)
predictions.append(
{"class": image_class.replace("_", " "), "output": output, "prob": prob}
)

predictions = sorted(predictions, key=lambda x: x["output"], reverse=True)
predictions = predictions[0:2]
#print({"class": str(pred_class), "predictions": predictions})
predictions = (predictions)[0]
return JSONResponse({'result': [str(predictions['class']), str(predictions['prob']*100)]})

@app.route('/analyze_knn', methods=['POST'])
async def analyze_knn(request):
img_data = await request.form()
img_bytes = await (img_data['file'].read())
img = IM.open(BytesIO(img_bytes))
img = img.resize((224, 224))
img = np.array(img)
img = img.reshape(1, 50176)
learn = pickle.load(open(path / 'models/model.sav', 'rb'))
output_class = learn.predict(img)[0]

return JSONResponse({'result': str(output_class)})


if __name__ == '__main__':
if 'serve' in sys.argv:
uvicorn.run(app=app, host='0.0.0.0', port=5000, log_level="info")
uvicorn.run(app=app, host='0.0.0.0', port=5000, log_level="info")
Binary file added app/static/CNN_pic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/NTNU_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 60 additions & 3 deletions app/static/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,85 @@ function showPicked(input) {
reader.readAsDataURL(input.files[0]);
}

function analyze() {
function analyze() {
console.log("Hei Elise <3")
var uploadFiles = el("file-input").files;
if (uploadFiles.length !== 1) alert("Please select a file to analyze!");

el("analyze-button").innerHTML = "Analyzing...";


var xhr = new XMLHttpRequest();
var loc = window.location;
xhr.open("POST", `${loc.protocol}//${loc.hostname}:${loc.port}/analyze`,
xhr.open("POST", `${loc.protocol}//${loc.hostname}:${loc.port}/analyze_cnn`,
true);
xhr.onerror = function() {
alert(xhr.responseText);
};
xhr.onload = function(e) {
if (this.readyState === 4) {
var response = JSON.parse(e.target.responseText);
el("result-label").innerHTML = `Result = ${response["result"]}`;
// var response = {"result":[{'class': 'PNEUMONIA', 'output': 0.9, 'prob': 0.93}, {'class': 'NORMAL', 'output': 0.1, 'prob': 0.07}]}
var result = response["result"]
el("result-label").innerHTML = `Result = ${result}`;
var modal = document.getElementById("myModal");
modal.style.display = "block";
el("state").innerHTML = `${result[0]}`;
// state.innerHTML = response["result"][0].class
el("percent").innerHTML = result[1];
}
el("analyze-button").innerHTML = "Analyze";
};


var fileData = new FormData();
fileData.append("file", uploadFiles[0]);
xhr.send(fileData);
}
// Top navigation bar
$(function(){
var str = '#len'; //increment by 1 up to 1-nelemnts
$(document).ready(function(){
var i, stop;
i = 1;
stop = 4; //num elements
setInterval(function(){
if (i > stop){
return;
}
$('#len'+(i++)).toggleClass('bounce');
}, 500)
});
});

// Modal for results

// When the user clicks the button, open the modal
// External modal-button for testing locally
// function openModal(){
// var response = JSON.parse(e.target.responseText);
// var response = {"result":[{'class': 'PNEUMONIA', 'output': 0.9, 'prob': 0.93}, {'class': 'NORMAL', 'output': 0.1, 'prob': 0.07}]}
// var response = {"result":[{'class': 'NORMAL', 'output': 1.0, 'prob': 1.0}, {'class': 'PNEUMONIA', 'output': 0.0, 'prob': 0.0}]}
// el("result-label").innerHTML = `Result = ${response["result"]}`;
// var modal = document.getElementById("myModal");
// modal.style.display = "block";
// el("state").innerHTML = response["result"][0].class
// state.innerHTML = response["result"][0].class
// el("percent").innerHTML = response["result"][0].prob * 100
// }


// When the user clicks on <span> (x), close the modal
function closeModal(){
var modal = document.getElementById("myModal");
modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it

window.onclick = function(event) {
var modal = document.getElementById("myModal");
if (event.target === modal) {
modal.style.display = "none";
}
}
Binary file added app/static/grupo ml.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/logo1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/logo2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/logo3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/static/pneumonia.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading