Skip to content

Instantly share code, notes, and snippets.

@AlexDBlack

AlexDBlack/java Secret

Created October 23, 2019 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexDBlack/3a4f58edcf243ef4d3faa73ee176eb04 to your computer and use it in GitHub Desktop.
Save AlexDBlack/3a4f58edcf243ef4d3faa73ee176eb04 to your computer and use it in GitHub Desktop.
import org.datavec.image.loader.NativeImageLoader;
import org.deeplearning4j.nn.graph.ComputationGraph;
import org.junit.Test;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.ops.transforms.Transforms;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
public class Debug8298 {
@Test
public void test() throws Exception {
File f = new File("C:\\Temp\\Issue8298\\model.h5");
ComputationGraph model = KerasModelImport.importKerasModelAndWeights(f.getAbsolutePath(), false);
NativeImageLoader nil = new NativeImageLoader(256, 256, 1);
File imgF = new File("C:\\Temp\\Issue8298\\n8DQ5.png");
INDArray arr = nil.asMatrix(imgF);
arr.divi(255);
INDArray out = model.output(arr)[0];
BufferedImage bi = toBI(out);
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(new JLabel(new ImageIcon(bi)));
frame.pack();
frame.setVisible(true);
frame.setTitle("DL4J");
INDArray kerasPredict = Nd4j.createFromNpyFile(new File("C:/Temp/Issue8298/prediction.npy"));
kerasPredict = kerasPredict.permute(0, 3, 1, 2); //NHWC to NCHW
System.out.println(kerasPredict.shapeInfoToString());
BufferedImage biKeras = toBI(kerasPredict);
JFrame frame2 = new JFrame();
frame2.getContentPane().setLayout(new FlowLayout());
frame2.getContentPane().add(new JLabel(new ImageIcon(biKeras)));
frame2.pack();
frame2.setVisible(true);
frame2.setTitle("Keras");
INDArray absDiff = Transforms.abs(out.sub(kerasPredict));
System.out.println("Min diff: " + absDiff.minNumber());
System.out.println("Max diff: " + absDiff.maxNumber());
System.out.println("Avg diff: " + absDiff.meanNumber());
Thread.sleep(100000);
}
private BufferedImage toBI(INDArray arr){
int h = (int)arr.size(2);
int w = (int)arr.size(3);
BufferedImage bi = new BufferedImage(h, w, BufferedImage.TYPE_BYTE_GRAY);
int[] ia = new int[1];
for( int i=0; i<h; i++ ){
for( int j=0; j<w; j++ ){
int value = (int)(255 * arr.getDouble(0, 0, i, j));
ia[0] = value;
bi.getRaster().setPixel(i,j,ia);
}
}
return bi;
}
}
import keras
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras import optimizers
from keras.models import model_from_json
file = open('C:/Temp/Issue8298/unet.json', 'r')
json = file.read()
model = model_from_json(json)
img = load_img('C:/Temp/Issue8298/n8DQ5.png', target_size=(256, 256), color_mode="grayscale")
print(type(img))
print(img.format)
print(img.mode)
print(img.size)
# img.show()
img_array = img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array /= 255.0
# print(img_array)
print(img_array.dtype)
print(img_array.shape)
print(np.min(img_array))
print(np.max(img_array))
# model.compile(optimizer=optimizers.Adam(lr=0.005), loss='binary_crossentropy')
model.compile(optimizer=optimizers.Adam(lr=0.001), loss='mean_squared_error')
model.fit(img_array, img_array, epochs=20)
model.save("C:/Temp/Issue8298/model.h5")
out = model.predict(img_array)
np.save("C:/Temp/Issue8298/prediction.npy", out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment