[go: nahoru, domu]

Skip to content

Commit

Permalink
everything except time estimation working for the SysML demo
Browse files Browse the repository at this point in the history
  • Loading branch information
scnakandala committed Mar 30, 2019
1 parent 9d99d92 commit 3425ac6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 27 deletions.
4 changes: 4 additions & 0 deletions code-release/core/python/inception3.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,10 @@ def __initialize_weights(self, gpu):
weights_data = load_dict_from_hdf5(dir_path + "/oct_inception3_ptch.h5", gpu)
self.fc.weight.data = weights_data['482.fc.weight']
self.fc.bias.data = weights_data['483.fc.bias']
if self.dataset == 'chest':
weights_data = load_dict_from_hdf5(dir_path + "/chest_inception3_ptch.h5", gpu)
self.fc.weight.data = weights_data['482.fc.weight']
self.fc.bias.data = weights_data['483.fc.bias']


def __get_tensor(self, name, batch_size, channels, p_height, p_width, k_size, stride, in_size, out_size, truncate=True):
Expand Down
4 changes: 4 additions & 0 deletions code-release/core/python/resnet18.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,10 @@ def __initialize_weights(self, gpu):
weights_data = load_dict_from_hdf5(dir_path + "/oct_resnet18_ptch.h5", gpu)
self.fc.weight.data = weights_data['fc:w']
self.fc.bias.data = weights_data['fc:b']
elif self.dataset == 'chest':
weights_data = load_dict_from_hdf5(dir_path + "/chest_resnet18_ptch.h5", gpu)
self.fc.weight.data = weights_data['fc:w']
self.fc.bias.data = weights_data['fc:b']


if __name__ == '__main__':
Expand Down
14 changes: 8 additions & 6 deletions code-release/ui/krypton_ui/krypton/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt

import numpy as np
import torch, gc
from commons import inc_inference, show_heatmap, full_inference_e2e
from imagenet_classes import class_names
Expand Down Expand Up @@ -139,9 +139,9 @@ def selectedRegion(request):
heatmap, prob, label = inc_inference(dataset, model_class, curr_path, patch_size=patch_size, stride=stride_size, beta=1.0, x_size=299, y_size=299, image_size=299, gpu=True, c=c)
elif mode == "approximate":
if model_class != Inception3:
heatmap, prob, label = inc_inference(dataset, model_class, curr_path, patch_size=patch_size, stride=stride_size, beta=0.55, x_size=224, y_size=224, image_size=224, gpu=True, c=c)
heatmap, prob, label = inc_inference(dataset, model_class, curr_path, patch_size=patch_size, stride=stride_size, beta=0.5, x_size=224, y_size=224, image_size=224, gpu=True, c=c)
else:
heatmap, prob, label = inc_inference(dataset, model_class, curr_path, patch_size=patch_size, stride=stride_size, beta=0.55, x_size=299, y_size=299, image_size=299, gpu=True, c=c)
heatmap, prob, label = inc_inference(dataset, model_class, curr_path, patch_size=patch_size, stride=stride_size, beta=0.7, x_size=299, y_size=299, image_size=299, gpu=True, c=c)
else:
if model_class != Inception3:
heatmap, prob, label = full_inference_e2e(dataset, model_class, curr_path, patch_size=patch_size, stride=stride_size, batch_size=64, image_size=224, gpu=True, c=c)
Expand All @@ -155,14 +155,16 @@ def selectedRegion(request):
heatmap, prob, label = inc_inference(dataset, model_class, curr_path, patch_size=patch_size, stride=stride_size, beta=1.0, x0=calibrated_y1, y0=calibrated_x1, x_size=calibrated_h, y_size=calibrated_w, image_size=299, gpu=True, c=c)
elif mode == "approximate":
if model_class != Inception3:
heatmap, prob, label = inc_inference(dataset, model_class, curr_path, patch_size=patch_size, stride=stride_size, beta=0.55, x0=calibrated_y1, y0=calibrated_x1, x_size=calibrated_h, y_size=calibrated_w, image_size=224, gpu=True, c=c)
heatmap, prob, label = inc_inference(dataset, model_class, curr_path, patch_size=patch_size, stride=stride_size, beta=0.5, x0=calibrated_y1, y0=calibrated_x1, x_size=calibrated_h, y_size=calibrated_w, image_size=224, gpu=True, c=c)
else:
heatmap, prob, label = inc_inference(dataset, model_class, curr_path, patch_size=patch_size, stride=stride_size, beta=0.55, x0=calibrated_y1, y0=calibrated_x1, x_size=calibrated_h, y_size=calibrated_w, image_size=299, gpu=True, c=c)
heatmap, prob, label = inc_inference(dataset, model_class, curr_path, patch_size=patch_size, stride=stride_size, beta=0.7, x0=calibrated_y1, y0=calibrated_x1, x_size=calibrated_h, y_size=calibrated_w, image_size=299, gpu=True, c=c)


end_time = time.time()

heatmap_path = './media/photos/heatmap_' + ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(20)) + '.png'
low_prob = np.min(heatmap)
high_prob = np.max(heatmap)

plt.imsave(heatmap_path, heatmap, cmap=plt.cm.jet_r)
img = Image.open(heatmap_path)
Expand All @@ -182,7 +184,7 @@ def selectedRegion(request):
else:
class_names = imagenet_class_names

return JsonResponse({'url':url, 'heatmap_url': heatmap_path[1:], 'prediction': class_names[label], 'estimate_time': estTime, 'actual_time': actTime})
return JsonResponse({'url':url, 'heatmap_url': heatmap_path[1:], 'prediction': class_names[label], 'estimate_time': estTime, 'actual_time': actTime, 'low_prob':str(low_prob), 'high_prob':str(high_prob)})


def time_estimate(slope, intercept, stride, patch, width, height):
Expand Down
63 changes: 42 additions & 21 deletions code-release/ui/krypton_ui/krypton_ui/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
float: left;
width: 49%;
padding: 5px;
display: flex;
align-items: center;
justify-content: center;
}

/* Clear floats after image containers */
/* Clear floats after image containers */
.row::after {
content: "";
clear: both;
Expand Down Expand Up @@ -153,32 +156,32 @@

</head>
<body color>
<div id="response"></div>

<div class="input-group mb-3" style="padding-right:30%;">
<div class="input-group mb-3" style="padding-right:10%;">
<div class="input-group-prepend">
<span class="btn btn-outline-primary" id="inputGroupFileAddon01">Upload</span>
</div>
<div class="custom-file">
<input type="file" class="custom-file-input btn-primary" id="imageSelection" aria-describedby="inputGroupFileAddon01" onchange="imageDisplay(this);">
<label class="custom-file-label" for="imageSelection">Choose file</label>
</div>
<div class="group">
<span class="btn" id="prediction" style="fontsize:30px;"></span>
</div>
</div>
<div class="input-group mb-3">
Download &nbsp;<a href="http://supun.ucsd.edu/krypton/images.zip">sample images</a>
</div>

<!-- Comment out if want to use the button only solution -->
<div id="elementsAfterImage" style="display:none;">
<div class="bar">
<h5 style="font-weight: lighter;">Probability legend</h5>

<div id="prediction_bar" class="bar" style="visibility:hidden">
<h5 id="prediction" style="font-weight: lighter;">Predicted Class:</h5>
<div class="legend-gradient"></div>
<div class="legend">
<div class="legend__item">0.0</div>
<div class="legend__item">0.2</div>
<div class="legend__item">0.5</div>
<div class="legend__item">0.8</div>
<div class="legend__item">1.0</div>
<div id="low_prob" class="legend__item"></div>
<div class="legend__item"></div>
<div class="legend__item"></div>
<div class="legend__item"></div>
<div id="high_prob" class="legend__item"></div>
</div>
</div>
<div class="row">
Expand Down Expand Up @@ -206,7 +209,7 @@ <h4 id="actual_time" style="font-weight: lighter;"></h4>
</div>
</section>
<br><br>
<div class="container" style="border-style: dashed; border-width: thin; margin-left:190px;">
<div class="container" style="border-style: dashed; border-width: thin;display:flex;align-items: center;justify-content: center;">
<!-- <div class="row" style="text-align: center;">
<div class="btn-group-toggle" data-toggle="buttons">
<label class="btn btn-success custom">
Expand All @@ -228,7 +231,7 @@ <h4 id="actual_time" style="font-weight: lighter;"></h4>
<form id="coords"
class="coords"
onsubmit="return false;"
action="http://example.com/post.php" style="display:none;">
action="" style="display:none;">

<div class="inline-labels">
<label>X1 <input type="text" size="4" id="x1" name="x1" /></label>
Expand All @@ -240,7 +243,7 @@ <h4 id="actual_time" style="font-weight: lighter;"></h4>
</div>
</form>

<section>
<section style="display:flex;align-items: center;justify-content: center;">
<div class="form-inline">
<div class="input-group" style="margin-left:; padding-bottom:1rem;">
<form method='post' id ='submit'>
Expand All @@ -252,7 +255,7 @@ <h4 id="actual_time" style="font-weight: lighter;"></h4>
<!-- <button id="submit">Submit</button> -->
<button class="btn btn-danger btn-sm" id="resetSelect">reset</button>
</div>
<div class="input-group mb-3" style="margin-left:10rem;">
<div class="input-group mb-2" style="margin-left:5rem;">
<div class="input-group-prepend">
<label class="input-group-text" for="patch" style="background-color:#00b33c;">Dataset</label>
</div>
Expand All @@ -263,7 +266,7 @@ <h4 id="actual_time" style="font-weight: lighter;"></h4>
<option value="chest">Chest X-Ray</option>
</select>
</div>
<div class="input-group mb-3" style="margin-left:10rem;">
<div class="input-group mb-2" style="margin-left:5rem;">
<div class="input-group-prepend">
<label class="input-group-text" for="patch" style="background-color:#00b33c;">Patch size</label>
</div>
Expand All @@ -276,7 +279,7 @@ <h4 id="actual_time" style="font-weight: lighter;"></h4>
<option value="16">Sixteen</option>
</select>
</div>
<div class="input-group mb-3" style="margin-left:8.5rem;">
<div class="input-group mb-2" style="margin-left:5rem;">
<div class="input-group-prepend">
<label class="input-group-text" for="stride" style="background-color:#00b33c;">Stride size</label>
</div>
Expand All @@ -289,7 +292,7 @@ <h4 id="actual_time" style="font-weight: lighter;"></h4>
<option value="16">Sixteen</option>
</select>
</div>
<div class="input-group mb-3" style="margin-left:9.7rem;">
<div class="input-group mb-2" style="margin-left:5rem;">
<div class="input-group-prepend">
<label class="input-group-text" for="model" style="background-color:#00b33c;">CNN Model</label>
</div>
Expand All @@ -300,6 +303,17 @@ <h4 id="actual_time" style="font-weight: lighter;"></h4>
<option value="Inception">Inception3</option>
</select>
</div>
<div class="input-group mb-2" style="margin-left:5rem;">
<div class="input-group-prepend">
<label class="input-group-text" for="color" style="background-color:#00b33c;">Patch Color</label>
</div>
<select class="custom-select" id="color">
<option selected>Choose...</option>
<option value="0.4" selected="selected">Gray</option>
<option value="0.0">Black</option>
<option value="1.0">White</option>
</select>
</div>
</div>
</section>

Expand Down Expand Up @@ -335,6 +349,7 @@ <h4 id="actual_time" style="font-weight: lighter;"></h4>
$('#target')
.attr('src', e.target.result)
$('#resetSelect').click()
$('#actual_time').text("");
$('#heatmap').attr('src', "https://via.placeholder.com/700x472.png?text=Heatmap+will+be+displayed+here")
$('#heatmap').show();
$('#original').attr('src', '')
Expand All @@ -346,6 +361,7 @@ <h4 id="actual_time" style="font-weight: lighter;"></h4>
$('#elementsAfterImage').show()
$('#firstTitle').invisible()
$('#prediction').text("")
$("#prediction_bar").invisible()
}
}

Expand Down Expand Up @@ -421,6 +437,7 @@ <h4 id="actual_time" style="font-weight: lighter;"></h4>
var csrf_token = $('#submit [name="csrfmiddlewaretoken"]').val();
var image = document.getElementById("imageSelection").files[0];
var dataset = document.getElementById("dataset").value;
var color = document.getElementById("color").value;
var ePatch = document.getElementById("patch").value;
var eStride = document.getElementById("stride").value;
var eModel = document.getElementById("model").value;
Expand Down Expand Up @@ -466,6 +483,7 @@ <h4 id="actual_time" style="font-weight: lighter;"></h4>
fd.append('strideSize', eStride);
fd.append('model', eModel);
fd.append('mode', mode);
fd.append('color', color);
$.ajax({
type:"POST",
url:"/selectedRegion/",
Expand All @@ -481,10 +499,13 @@ <h4 id="actual_time" style="font-weight: lighter;"></h4>
$('#heatmap').show();
$('#original').attr('src', message['url']);
$('#original').show();
$('#prediction').text("Prediction: " + message['prediction']);
$('#prediction').text("Predicted Class: " + message['prediction']);
// $('#estimate_time').text("Estimated time: " + message['estimate_time'] + " s");
$('#actual_time').text("Actual time: " + message['actual_time'] + " s");
//$("#resetSelect").click();
$('#low_prob').text(message['low_prob']);
$('#high_prob').text(message['high_prob']);
$("#prediction_bar").css("visibility", "visible");
},
error : function(xhr,errmsg,err) {
console.log(xhr.status + ": " + xhr.responseText);
Expand Down

0 comments on commit 3425ac6

Please sign in to comment.