def validate(model=None): if model is None: model = DilatedCNN() model.load() batch_size = 12 mean = tfe.metrics.Mean() now = time.time() index_start = 0 while True: index_end = min(x_test.shape[0], index_start + batch_size) x = tf.constant(x_test[index_start:index_end], dtype=tf.float32) y = tf.constant(y_test[index_start:index_end], dtype=tf.float32) # x = tf.image.resize_images(x, (160, 200)) # y = tf.image.resize_images(y, (160, 200)) y_hat = model(x).numpy() y_hat = np.where(y_hat >= 0.5, 1, 0).astype("float32") loss = model.loss(y_hat, y) mean(tf.reduce_mean(loss)) # print("Validation loss is:", loss) index_start += batch_size if index_start >= x_test.shape[0]: break duration = time.time() - now print("Validation took:", duration / 60) print("Validation loss is:", mean.result().numpy())
def test_keras_model_v4_model(self): x = np.random.normal(size=(1, 320, 400, 3)) inputs = tf.keras.layers.Input(shape=(320, 400, 3)) out = DilatedCNN()(inputs) model = tf.keras.Model(inputs=inputs, outputs=out) optimizer = tf.keras.optimizers.Adam() model.compile(optimizer, loss='mean_squared_error') # model.summary() iter = 100 now = time.time() for i in range(iter): y = model.predict(x) duration = (time.time() - now) / iter print("Time taken on keras model:", duration)
def test_model(self): model = DilatedCNN() # init weights x = tf.random_normal((1, 600, 800, 3)) x = tf.image.resize_images(x, (320, 400)) out = model(x) print(out.shape) x = tf.random_normal((1, 600, 800, 3)) x = tf.image.resize_images(x, (320, 400)) start = time.time() steps = 100 for i in range(steps): test = model(x) duration = (time.time() - start) / steps print("average duration:", round(duration, 4))
def train(): # load_files() batch_size = 12 from replay_memory import PrioritisedReplayMemory memory = PrioritisedReplayMemory(capacity=batch_size*100, e=0.01) gen = generator(x_train, y_train, memory, batch_size=batch_size) model = DilatedCNN() if args.load: print("loading model") model.load() training_cycle(model, gen, memory, 1e-2, 10) model.save()
from PIL import Image from io import BytesIO, StringIO import tensorflow as tf import tensorflow.contrib.eager as tfe from model_v4 import DilatedCNN as SegModel from UNet import UNet as SegModel # tfe.enable_eager_execution() file = sys.argv[-1] if file == 'demo.py': print("Error loading video") quit model = SegModel.build_keras_model() # model.load_weights("model_v4_weights.h5") model.load_weights("UNet.h5") video = skvideo.io.vread(file) writer = skvideo.io.FFmpegWriter("outputvideo.mp4") answer_key = {} ones = tf.ones((1, 600, 800, 3)) * 255 zeros = tf.zeros((1, 600, 800, 3)) for rgb_frame in video: resized = cv2.resize(rgb_frame, (400, 320)) x = np.expand_dims(resized, axis=0) y_hat = model.predict_on_batch(x)[0]
def init(): batch_size = 12 memory = PrioritisedReplayMemory(capacity=batch_size, e=0.01) gen = generator(x_train, y_train, memory, batch_size=batch_size) model = DilatedCNN() return model, gen, memory
def cv_encode(array): retval, buffer = cv2.imencode('.png', array) return base64.b64encode(buffer).decode("utf-8") # Define encoder function def encode(array): pil_img = Image.fromarray(array) buff = BytesIO() pil_img.save(buff, format="PNG") return base64.b64encode(buff.getvalue()).decode("utf-8") model = DilatedCNN() model.load() video = skvideo.io.vread(file) answer_key = {} # Frame numbering starts at 1 frame = 1 ones = tf.ones((1, 600, 800, 3)) zeros = tf.zeros((1, 600, 800, 3)) for rgb_frame in video: # rgb_frame = cv2.resize(rgb_frame, (400, 320)) x = tf.constant(np.expand_dims(rgb_frame, axis=0), dtype=tf.float32)