In [3]:
import h2o
h2o.init()
data = h2o.import_file("towerbridge.csv")
In [4]:
data
Out[4]:
In [70]:
y = "b"
x = ["x","y"]
train, valid, test = data.split_frame([0.75, 0.15])
from h2o.estimators import H2ODeepLearningEstimator
m = H2ODeepLearningEstimator(model_id="DL_defaults", hidden=[20,20,20,20,20,20,20,20,20,20], activation='tanh',epochs=10000)
m.train(x,y,train)
m
Out[70]:
In [86]:
import numpy as np
import pandas as pd
from h2o.frame import H2OFrame
from PIL import Image
IM_SIZE = 256
IM_CHANNELS = 1
def save_pixels(path_to_image_file, image_array, mode):
im_out = Image.fromarray(image_array, mode )
im_out.save(path_to_image_file)
# Takes care of clipping, casting to int8, etc.
def save_ndarray(path_to_outfile, x, width = IM_SIZE, height = IM_SIZE, channels = IM_CHANNELS):
out_arr = np.clip(x, 0, 255)
if channels == 3:
out_arr = np.reshape(out_arr, (width, height, channels), 1)
else:
assert(channels == 1)
out_arr = np.reshape(out_arr, (width, height), 1)
out_arr = np.rot90(out_arr, k=3)
out_arr = np.fliplr(out_arr)
if channels == 3:
save_pixels(path_to_outfile, out_arr.astype(np.int8), 'RGB')
else:
save_pixels(path_to_outfile, out_arr.astype(np.int8), 'L')
# Create suitable training matrix
def gen_input_tuples(pixels_width, pixels_height, scale, translate_x, translate_y):
image_height = pixels_height
image_width = pixels_width
# One row per pixel
X = np.zeros((image_width * image_height, 2))
# Fill in y values
X[:,1] = np.repeat(range(0, image_height), image_width, 0)
# Fill in x values
X[:,0] = np.tile(range(0, image_width), image_height)
# Normalize X
X = X - X.mean()
X = X / X.var()
X[:,0] += translate_x
X[:,1] += translate_y
X = X / scale
return (X)
def render(mdl, image_size, scale, tx, ty, outfile):
pixel_coords = gen_input_tuples(image_size, image_size, scale, tx, ty)
df_pixels_to_render = pd.DataFrame({'x':pixel_coords[:,0], 'y':pixel_coords[:,1]})
h2o_pixels = H2OFrame(df_pixels_to_render)
pixel_intensities = m.predict(h2o_pixels)
save_ndarray(outfile, pixel_intensities.as_data_frame().as_matrix(), image_size, image_size, 1)
render(m, IM_SIZE, 1, 0, 0, "modelled_tower_bridge.png")
In [89]:
render(m, 1024, 1/8, 0, 0, "modelled_tower_bridge_x2.png")
In [ ]: