Spaces:
Build error
Build error
Update main.py
Browse filesBetter plotting
main.py
CHANGED
|
@@ -2,6 +2,7 @@ import numpy
|
|
| 2 |
import keras
|
| 3 |
import gradio
|
| 4 |
import matplotlib.pyplot
|
|
|
|
| 5 |
|
| 6 |
# Building the neural network
|
| 7 |
model1 = keras.models.Sequential()
|
|
@@ -28,6 +29,9 @@ model1.add(keras.layers.Conv2D(3, (9, 9), activation='tanh', padding='same'))
|
|
| 28 |
#Loading the weights in the architecture (The file should be stored in the same directory as the code)
|
| 29 |
model1.load_weights('modelV13_500trained_1.h5')
|
| 30 |
|
|
|
|
|
|
|
|
|
|
| 31 |
#simple image scaling to (nR x nC) size
|
| 32 |
def scale(im, nR, nC):
|
| 33 |
nR0 = len(im) # source number of rows
|
|
@@ -37,15 +41,22 @@ def scale(im, nR, nC):
|
|
| 37 |
|
| 38 |
|
| 39 |
def predict(mask):
|
| 40 |
-
|
| 41 |
-
cm = matplotlib.pyplot.get_cmap('RdBu')
|
| 42 |
-
|
| 43 |
-
scaled_mask = numpy.round(scale(mask, 101, 636)/255.0)
|
| 44 |
print(scaled_mask)
|
| 45 |
X = scaled_mask[numpy.newaxis, :, :, numpy.newaxis]
|
| 46 |
v = model1.predict(X)
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
with gradio.Blocks() as demo:
|
| 51 |
|
|
@@ -66,7 +77,8 @@ with gradio.Blocks() as demo:
|
|
| 66 |
exx = gradio.Image(label="ε-xx")
|
| 67 |
eyy = gradio.Image(label="ε-yy")
|
| 68 |
exy = gradio.Image(label="ε-xy")
|
|
|
|
| 69 |
|
| 70 |
-
btn.click(fn=predict, inputs=[mask], outputs=[exx, eyy, exy])
|
| 71 |
|
| 72 |
demo.launch(debug=True)
|
|
|
|
| 2 |
import keras
|
| 3 |
import gradio
|
| 4 |
import matplotlib.pyplot
|
| 5 |
+
import matplotlib.colors
|
| 6 |
|
| 7 |
# Building the neural network
|
| 8 |
model1 = keras.models.Sequential()
|
|
|
|
| 29 |
#Loading the weights in the architecture (The file should be stored in the same directory as the code)
|
| 30 |
model1.load_weights('modelV13_500trained_1.h5')
|
| 31 |
|
| 32 |
+
# Get the color map by name:
|
| 33 |
+
cm = matplotlib.pyplot.get_cmap('RdBu')
|
| 34 |
+
|
| 35 |
#simple image scaling to (nR x nC) size
|
| 36 |
def scale(im, nR, nC):
|
| 37 |
nR0 = len(im) # source number of rows
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
def predict(mask):
|
| 44 |
+
scaled_mask = numpy.ones((101, 636)) if mask is None else numpy.round(scale(mask, 101, 636)/255.0)
|
|
|
|
|
|
|
|
|
|
| 45 |
print(scaled_mask)
|
| 46 |
X = scaled_mask[numpy.newaxis, :, :, numpy.newaxis]
|
| 47 |
v = model1.predict(X)
|
| 48 |
+
measure = max(v.max(), -v.min())
|
| 49 |
+
output = (v / measure)
|
| 50 |
+
legend = "<h2>Strain</h2><table style=\"width:100%\"><tr>"
|
| 51 |
+
for i in range(11):
|
| 52 |
+
color = cm(i/10.0)[:3]
|
| 53 |
+
value = -measure + i*2*measure/10
|
| 54 |
+
print(sum(list(color)))
|
| 55 |
+
hex = matplotlib.colors.to_hex(list(color))
|
| 56 |
+
text_color = "black" if sum(list(color)) > 2.0 else "white"
|
| 57 |
+
legend = legend + f"<td style=\"background-color: {hex}; color: {text_color}\">{value:+.2e}</td>"
|
| 58 |
+
legend = legend + "</tr></table>"
|
| 59 |
+
return cm((numpy.multiply(output[0, :, :, 0], scaled_mask)+1.0)/2.0), cm((numpy.multiply(output[0, :, :, 1], scaled_mask)+1.0)/2.0), cm((numpy.multiply(output[0, :, :, 2], scaled_mask)+1.0)/2.0), legend
|
| 60 |
|
| 61 |
with gradio.Blocks() as demo:
|
| 62 |
|
|
|
|
| 77 |
exx = gradio.Image(label="ε-xx")
|
| 78 |
eyy = gradio.Image(label="ε-yy")
|
| 79 |
exy = gradio.Image(label="ε-xy")
|
| 80 |
+
legend = gradio.HTML(label="", value="")
|
| 81 |
|
| 82 |
+
btn.click(fn=predict, inputs=[mask], outputs=[exx, eyy, exy, legend])
|
| 83 |
|
| 84 |
demo.launch(debug=True)
|