Last active
March 9, 2017 15:07
-
-
Save autoiue/d33d2bcc461b22dd3d20fc016d21d85c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Create first network with Keras | |
| from keras.models import Sequential | |
| from keras.layers import * | |
| from keras import backend as K | |
| from keras.engine.topology import Layer | |
| import numpy as np | |
| import codecs, json, math | |
| # fix random seed for reproducibility | |
| seed = 7 | |
| np.random.seed(seed) | |
| def jsonFrom3D(a, file): | |
| vx = [] | |
| s = a.shape | |
| a0 = s[0] | |
| a1 = s[1] | |
| a2 = s[2] | |
| for x in range(a0): | |
| for y in range(a1): | |
| for z in range(a2): | |
| if a[x][y][z] > 0.5: | |
| c = {'x':x,'y':y,'z':z} | |
| vx.append(c) | |
| outdata = { 'dimension' : [{'width': 128, 'height': 128, 'depth': 256}], 'voxels' : vx } | |
| with open('/tmp/'+file, 'w') as outfile: | |
| json.dump(outdata, outfile) | |
| class RepeatVector4D(Layer): | |
| def __init__(self, n, **kwargs): | |
| self.n = n | |
| self.input_spec = [InputSpec(ndim=3)] | |
| super(RepeatVector4D, self).__init__(**kwargs) | |
| def get_output_shape_for(self, input_shape): | |
| return (input_shape[0], self.n, input_shape[1], input_shape[2]) | |
| def call(self, x, mask=None): | |
| x = K.expand_dims(x, 1) | |
| pattern = K.stack([1, self.n, 1, 1]) | |
| out = K.tile(x, pattern) | |
| # jsonFrom3D(out[0], 'RV4in.json') | |
| return out | |
| def loadY(file): | |
| # not included hehe | |
| def loadX(file): | |
| # not included hehe | |
| X = loadX("../index.tsv") # I spare you the content of these | |
| Y = loadY("../index.tsv") # | |
| print("INPUTS: " + str(X.shape)) # > (16,9) | |
| print("OUTPUTS: " + str(Y.shape)) # > (16,128,128,256) | |
| # save training data to json to validate data preprocessing (spoiler: it works) | |
| jsonFrom3D(Y[0], 'validation_0.json') | |
| # create model | |
| model = Sequential() | |
| # input.shape = (*, 9) | |
| model.add(Dense(128, input_dim=9, init='uniform', activation='relu')) # shape = (*, 128) | |
| model.add(RepeatVector(128)) # shape = (*, 128, 128) | |
| model.add(RepeatVector4D(256)) # shape = (*, 256, 128, 128) | |
| model.add(Dense(128)) | |
| model.add(GaussianNoise(0.5)) | |
| model.add(Dense(128)) | |
| model.add(Dropout(0.2)) | |
| model.add(Dense(128)) | |
| model.add(Dense(128, activation='hard_sigmoid')) | |
| model.add(Reshape((128,128,256))) | |
| # Compile model | |
| model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) | |
| # Fit the model | |
| # | |
| model.fit(X, Y, nb_epoch=150, batch_size=16, verbose=2) | |
| # calculate predictions (not yet there) | |
| hopefully_a_chair = model.predict(np.array([[2015,40,3,9,400,6,4,1,0]]))[0] | |
| jsonFrom3D(hopefully_a_chair, 'hopefully_a_chair.json') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment