This game involves 8 animal pictures in .PNG format, 1 .PNG picture saying "matched".
app.py_
import requests import json import numpy as np import streamlit as st import os import matplotlib.pyplot as plt
URI = 'http://127.0.0.1:5000'
st.title('Neural Network Visualizer') st.sidebar.markdown('# Input Image')
if st.button('Get random predictions'): response = requests.post(URI, data={}) # print(response.text) response = json.loads(response.text) preds = response.get('prediction') image = response.get('image') image = np.reshape(image, (28, 28))
st.sidebar.image(image, width=150)
for layer, p in enumerate(preds):
numbers = np.squeeze(np.array(p))
plt.figure(figsize=(32, 4))
if layer == 2:
row = 1
col = 10
else:
row = 2
col = 16
for i, number in enumerate(numbers):
plt.subplot(row, col, i + 1)
plt.imshow((number * np.ones((8, 8, 3))).astype('float32'), cmap='binary')
plt.xticks([])
plt.yticks([])
if layer == 2:
plt.xlabel(str(i), fontsize=40)
plt.subplots_adjust(wspace=0.05, hspace=0.05)
plt.tight_layout()
st.text('Layer {}'.format(layer + 1), )
st.pyplot()
animal.py
import random import os import game_config as gc
from pygame import image, transform
animals_count = dict((a, 0) for a in gc.ASSET_FILES)
def available_animals(): return [animal for animal, count in animals_count.items() if count < 2]
class Animal: def init(self, index): self.index = index self.name = random.choice(available_animals()) self.image_path = os.path.join(gc.ASSET_DIR, self.name) self.row = index // gc.NUM_TILES_SIDE self.col = index % gc.NUM_TILES_SIDE self.skip = False self.image = image.load(self.image_path) self.image = transform.scale(self.image, (gc.IMAGE_SIZE - 2 * gc.MARGIN, gc.IMAGE_SIZE - 2 * gc.MARGIN)) self.box = self.image.copy() self.box.fill((200, 200, 200))
animals_count[self.name] += 1
import json import tensorflow as tf import numpy as np import os import random import string
from flask import Flask, request
app = Flask(name)
model = tf.keras.models.load_model('model.h5') feature_model = tf.keras.models.Model(model.inputs, [layer.output for layer in model.layers])
_, (x_test, _) = tf.keras.datasets.mnist.load_data() x_test = x_test / 255.
def get_prediction(): index = np.random.choice(x_test.shape[0]) image = x_test[index,:,:] image_arr = np.reshape(image, (1, 784)) return feature_model.predict(image_arr), image
@app.route('/', methods=['GET', 'POST']) def index(): if request.method == 'POST': preds, image = get_prediction() final_preds = [p.tolist() for p in preds] return json.dump({'prediction': final_preds, 'image': image.tolist()}) return 'Welcome to the ml server'
if name == 'main': app.run()
game config.py-
import os
IMAGE_SIZE = 128 SCREEN_SIZE = 512 NUM_TILES_SIDE = 4 NUM_TILES_TOTAL = 16 MARGIN = 8
ASSET_DIR = 'assets' ASSET_FILES = [x for x in os.listdir(ASSET_DIR) if x[-3:].lower() == 'png'] assert len(ASSET_FILES) == 8