PrakhAI commited on
Commit
c50541c
·
1 Parent(s): 40dee89

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import jax
4
+ import numpy as np
5
+ import jax.numpy as jnp # JAX NumPy
6
+ from flax.training import train_state # Useful dataclass to keep train state
7
+ from flax import linen as nn # Linen API
8
+ from huggingface_hub import HfFileSystem
9
+ from flax.serialization import msgpack_restore, from_state_dict
10
+ import os
11
+ import tensorflow as tf
12
+
13
+ hf_key = text_input = st.text_input("Access token")
14
+
15
+ class CNN(nn.Module):
16
+ """A simple CNN model."""
17
+
18
+ @nn.compact
19
+ def __call__(self, x):
20
+ x = nn.Conv(features=32, kernel_size=(3, 3))(x)
21
+ x = nn.relu(x)
22
+ x = nn.avg_pool(x, window_shape=(2, 2), strides=(2, 2))
23
+ x = nn.Conv(features=64, kernel_size=(3, 3))(x)
24
+ x = nn.relu(x)
25
+ x = nn.avg_pool(x, window_shape=(2, 2), strides=(2, 2))
26
+ x = x.reshape((x.shape[0], -1)) # flatten
27
+ x = nn.Dense(features=256)(x)
28
+ x = nn.relu(x)
29
+ x = nn.Dense(features=16)(x)
30
+ x = nn.relu(x)
31
+ x = nn.Dense(features=2)(x)
32
+ return x
33
+
34
+ cnn = CNN()
35
+ params = cnn.init(jax.random.PRNGKey(0), jnp.ones([1, 50, 50, 3]))['params']
36
+
37
+ fs = HfFileSystem(token=hf_key)
38
+ with fs.open("PrakhAI/CatVsDog/checkpoint.msgpack", "rb") as f:
39
+ params = from_state_dict(params, msgpack_restore(f.read())["params"])
40
+
41
+ uploaded_files = st.file_uploader("Input images of cats or dogs (examples in files)", type=['jpg','png','tif'], accept_multiple_files=True)
42
+
43
+ if len(uploaded_files) == 0:
44
+ st.write("Please upload an image!")
45
+ else:
46
+ for uploaded_file in uploaded_files:
47
+ img = Image.open(uploaded_file)
48
+ st.image(img)
49
+ input = tf.image_resize(tf.convert_to_tensor(img), [50, 50])
50
+ st.write("Model Prediction: " + cnn.apply({"params": params}, input))
51
+ st.write("Model Prediction type: " + type(cnn.apply({"params": params}, input)))
52
+ st.write("Model Prediction type dir: " + dir(cnn.apply({"params": params}, input)))
53
+
54
+ def gridify(kernel, grid, kernel_size, scaling=5, padding=1):
55
+ scaled_and_padded = np.pad(np.repeat(np.repeat(kernel, repeats=scaling, axis=0), repeats=scaling, axis=1), ((padding,),(padding,),(0,),(0,)), 'constant', constant_values=(-1,))
56
+ grid = np.pad(np.array(scaled_and_padded.reshape((kernel_size[0]*scaling+2*padding, kernel_size[1]*scaling+2*padding, grid[0], grid[1])).transpose(2,0,3,1).reshape(grid[0]*(kernel_size[0]*scaling+2*padding), grid[1]*(kernel_size[1]*scaling+2*padding))+1)*127., (padding,), 'constant', constant_values=(0,))
57
+ st.image(Image.fromarray(np.repeat(np.expand_dims(grid, axis=0), repeats=3, axis=0).astype(np.uint8).transpose(1,2,0), mode="RGB"))
58
+
59
+ with st.expander("See first convolutional layer"):
60
+ gridify(params["Conv_0"]["kernel"], grid=(4,8), kernel_size=(3,3))
61
+
62
+ with st.expander("See second convolutional layer"):
63
+ print(params["Conv_1"]["kernel"].shape)
64
+ gridify(params["Conv_1"]["kernel"], grid=(32,64), kernel_size=(3,3))