Skip to content

Instantly share code, notes, and snippets.

@maxpumperla
Last active August 4, 2017 11:47
Show Gist options
  • Save maxpumperla/1d3c67f8c787bdb534c462c314839e63 to your computer and use it in GitHub Desktop.
Save maxpumperla/1d3c67f8c787bdb534c462c314839e63 to your computer and use it in GitHub Desktop.
File completeModelFile = new ClassPathResource("complete.h5").getFile();
String completeModelFileName = completeModelFile.getAbsolutePath();
MultiLayerNetwork network = KerasModelImport.importKerasSequentialModelAndWeights(completeModelFileName);
NativeImageLoader loader = new NativeImageLoader(height, width);
File imageFile = new ClassPathResource("1.jpg").getFile();
INDArray image = loader.asMatrix(imageFile);
image.divi(255); // <== how about that? :)
INDArray output = network.output(image);
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras import backend as K
from scipy.misc import imread
from keras.models import load_model, save_model
np.random.seed(1000) # for reproducibility
nb_classes = 10
img_rows, img_cols = 24, 15
nb_filters = 32
pool_size = (2, 2)
kernel_size = (3, 3)
img = imread("./1.jpg")
X_test = img.reshape(1, img_rows, img_cols)
if K.image_dim_ordering() == 'th':
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
X_test = X_test.astype('float32')
X_test /= 255
model = Sequential()
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1],
border_mode='valid',
input_shape=input_shape))
model.add(Activation('relu'))
model.add(Convolution2D(nb_filters, kernel_size[0], kernel_size[1]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=pool_size))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
model_json = model.to_json()
with open("keras_model.json", "w") as f:
f.write(model_json)
model.save_weights("keras_weights.h5")
print("Saved model to disk")
save_model(model, "./complete.h5")
print(pred)
dl4j: [0.12, 0.11, 0.09, 0.10, 0.08, 0.10, 0.09, 0.10, 0.10, 0.10]
keras: [0.11, 0.11, 0.09, 0.11, 0.06, 0.10, 0.09, 0.11, 0.10 0.09]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment