PrakhAI commited on
Commit
ad94d7e
·
1 Parent(s): a05416d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -34,29 +34,29 @@ params = cnn.init(jax.random.PRNGKey(0), jnp.ones([1, 50, 50, 3]))['params']
34
 
35
  fs = HfFileSystem(token=hf_key)
36
  with fs.open("PrakhAI/CatVsDog/checkpoint.msgpack", "rb") as f:
37
- params = from_state_dict(params, msgpack_restore(f.read())["params"])
38
 
39
  uploaded_files = st.file_uploader("Input images of cats or dogs (examples in files)", type=['jpg','png','tif'], accept_multiple_files=True)
40
 
41
  if len(uploaded_files) == 0:
42
- st.write("Please upload an image!")
43
  else:
44
- for uploaded_file in uploaded_files:
45
- img = Image.open(uploaded_file)
46
- st.image(img)
47
- input = tf.cast(tf.image.resize(tf.convert_to_tensor(img), [50, 50]), tf.float32) / 255.
48
- st.write("Model Prediction: " + cnn.apply({"params": params}, input))
49
- st.write("Model Prediction type: " + type(cnn.apply({"params": params}, input)))
50
- st.write("Model Prediction type dir: " + dir(cnn.apply({"params": params}, input)))
51
 
52
  def gridify(kernel, grid, kernel_size, scaling=5, padding=1):
53
- 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,))
54
- grid = np.pad(np.array(scaled_and_padded.reshape((kernel_size[0]*scaling+2*padding, kernel_size[1]*scaling+2*padding, 3, grid[0], grid[1])).transpose(3,0,4,1,2).reshape(grid[0]*(kernel_size[0]*scaling+2*padding), grid[1]*(kernel_size[1]*scaling+2*padding), 3)+1)*127., ((padding,),(padding,),(0,)), 'constant', constant_values=(0,))
55
- st.image(Image.fromarray(grid.astype(np.uint8), mode="RGB"))
56
 
57
  with st.expander("See first convolutional layer"):
58
- gridify(params["Conv_0"]["kernel"], grid=(4,8), kernel_size=(3,3))
59
 
60
  with st.expander("See second convolutional layer"):
61
- print(params["Conv_1"]["kernel"].shape)
62
- gridify(params["Conv_1"]["kernel"], grid=(32,64), kernel_size=(3,3))
 
34
 
35
  fs = HfFileSystem(token=hf_key)
36
  with fs.open("PrakhAI/CatVsDog/checkpoint.msgpack", "rb") as f:
37
+ params = from_state_dict(params, msgpack_restore(f.read())["params"])
38
 
39
  uploaded_files = st.file_uploader("Input images of cats or dogs (examples in files)", type=['jpg','png','tif'], accept_multiple_files=True)
40
 
41
  if len(uploaded_files) == 0:
42
+ st.write("Please upload an image!")
43
  else:
44
+ for uploaded_file in uploaded_files:
45
+ img = Image.open(uploaded_file)
46
+ st.image(img)
47
+ input = jnp.array(tf.cast(tf.image.resize(tf.convert_to_tensor(img), [50, 50]), tf.float32) / 255.)
48
+ st.write("Model Prediction: " + cnn.apply({"params": params}, input))
49
+ st.write("Model Prediction type: " + type(cnn.apply({"params": params}, input)))
50
+ st.write("Model Prediction type dir: " + dir(cnn.apply({"params": params}, input)))
51
 
52
  def gridify(kernel, grid, kernel_size, scaling=5, padding=1):
53
+ 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,))
54
+ grid = np.pad(np.array(scaled_and_padded.reshape((kernel_size[0]*scaling+2*padding, kernel_size[1]*scaling+2*padding, 3, grid[0], grid[1])).transpose(3,0,4,1,2).reshape(grid[0]*(kernel_size[0]*scaling+2*padding), grid[1]*(kernel_size[1]*scaling+2*padding), 3)+1)*127., ((padding,),(padding,),(0,)), 'constant', constant_values=(0,))
55
+ st.image(Image.fromarray(grid.astype(np.uint8), mode="RGB"))
56
 
57
  with st.expander("See first convolutional layer"):
58
+ gridify(params["Conv_0"]["kernel"], grid=(4,8), kernel_size=(3,3))
59
 
60
  with st.expander("See second convolutional layer"):
61
+ print(params["Conv_1"]["kernel"].shape)
62
+ gridify(params["Conv_1"]["kernel"], grid=(64,96), kernel_size=(3,3))