def __init__(self, dtype=np.float32, reshape_torch=False): """ Initializes a new LeNet inspired network Args: dtype: Datatype to be used reshape_torch: set this, if the training parameters came from Pytorch which requires a custom reshape """ self.reshape_torch = reshape_torch r1 = ReshapeLayer(newshape=[-1, 28, 28, 1]) cn1 = Conv2dLayer(in_channels=1, out_channels=16, kernel_size=3, activation='relu', dtype=np.float32) # [? 28 28 16] mp1 = MaxPool2dLayer(size=2) # [? 14 14 16] cn2 = Conv2dLayer(in_channels=16, out_channels=32, kernel_size=3, activation='relu') # [? 14 14 32] mp2 = MaxPool2dLayer(size=2) # [? 7 7 32] if reshape_torch: r2 = CustomReshapeLayer(custom_reshape_func=CustomReshapeLayer.reshape_for_torch) else: r2 = ReshapeLayer(newshape=[-1, 32 * 7 * 7]) fc1 = FullyConnectedLayer(input_size=32 * 7 * 7, output_size=32, activation='relu', dtype=np.float32) fc2 = FullyConnectedLayer(input_size=32, output_size=10, activation='softmax') # Store a reference to each layer self.r1 = r1 self.cn1 = cn1 self.mp1 = mp1 self.cn2 = cn2 self.mp2 = mp2 self.r2 = r2 self.fc1 = fc1 self.fc2 = fc2 self.lenet_layers = [r1, cn1, mp1, cn2, mp2, r2, fc1, fc2] super(LeNet, self).__init__(self.lenet_layers)
def test_conv(self): cl = Conv2dLayer(in_channels=1, out_channels=3, kernel_size=5) test_img = np.random.rand(4, 28, 28, 1) # create 4 test images cl_out = cl.__call__(test_img) # Check Shape self.assertEqual(cl_out.shape, (4, 28, 28, 3))
def test_tf_compare2(self): # kernel = test_kernel_gauss(size=5, sigma=1.6) # kernel = kernel[..., np.newaxis, np.newaxis] kernel = np.random.rand(5, 5, 8, 16) b = np.zeros(16) x = np.random.rand(5, 28, 28, 8) # create 4 test images y_tf = conv2d(x, kernel, strides=1, padding='same') y_tf = y_tf.numpy() # calculate numpy array cl = Conv2dLayer(in_channels=8, out_channels=16, kernel_size=5) cl.kernel = kernel cl.b = b y = cl(x) self.assertEqual(y_tf.shape, y.shape) self.assertTrue(np.allclose(y_tf, y)) for v1, v2 in zip(y_tf.flatten(), y.flatten()): self.assertAlmostEqual(v1, v2, delta=0.001)
def test_tf_compare_zero(self): kernel = np.zeros(shape=(5, 5, 8, 16)) b = np.zeros(16) x = np.random.rand(5, 28, 28, 8) # create 4 test images y_tf = conv2d(x, kernel, strides=1, padding='same') y_tf = y_tf.numpy() # calculate numpy array cl = Conv2dLayer(in_channels=1, out_channels=3, kernel_size=5) cl.kernel = kernel cl.b = b y = cl(x) self.assertEqual(y_tf.shape, y.shape) self.assertTrue(np.allclose(y_tf, y)) for v1, v2 in zip(y_tf.flatten(), y.flatten()): self.assertAlmostEqual(v1, 0, delta=0.0001) self.assertAlmostEqual(v2, 0, delta=0.0001)
def test_blur(self): k = make_gauss_kernel() cl = Conv2dLayer(in_channels=1, out_channels=1, kernel_size=5) loader = MnistDataDownloader("../../test/MNIST/") path_img, path_lbl = loader.get_path(DataSetType.TRAIN) reader = MnistDataReader(path_img, path_lbl) for lbl, img in reader.get_next(4): img = img.astype(np.float) / 255.0 img = np.reshape(img, newshape=[-1, 28, 28, 1]) k = np.reshape(k, newshape=[k.shape[0], k.shape[1], 1, 1]) cl.kernel = k img_out = cl(img) # Check the dimensions self.assertEqual(img_out.shape, (4, 28, 28, 1)) # Uncomment to see the image img_out = np.reshape(img_out, newshape=[1, 4 * 28, 28, 1]) img_out = np.squeeze(img_out) plt.imshow(img_out, cmap='gray', vmin=0.0, vmax=1.0) plt.show() break
def test_tensorflow_parameter_0(self): r1 = ReshapeLayer(newshape=[-1, 28, 28, 1]) cn1 = Conv2dLayer(in_channels=1, out_channels=16, kernel_size=3, activation='relu') # [? 28 28 16] checkpoint_path = "test/training_1/cp.ckpt" checkpoint_dir = os.path.abspath(os.path.dirname(checkpoint_path)) os.path.join(checkpoint_dir, "model_config.json") if not os.path.exists(checkpoint_dir): raise RuntimeError("There is no trained model data!") # Reload the model from the 2 files we saved with open(os.path.join(checkpoint_dir, "model_config.json")) as json_file: json_config = json_file.read() model = keras.models.model_from_json(json_config) model.load_weights(os.path.join(checkpoint_dir, "weights.h5")) # Print a summary Ws = model.get_weights() # See Keras Documentation # https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D # # Default ordering of weights for Conv: (batch, height, width, channels) # Default ordering of weights for Dense self.assertEqual(cn1.kernel.shape, Ws[0].shape) self.assertEqual(cn1.b.shape, Ws[1].shape) # Assign values cn1.kernel = Ws[0] cn1.b = Ws[1] layers = [ r1, cn1, ] interesting_layers = [1] # don't care about reshape layers n = Network(layers) loader = MnistDataDownloader("../../test/MNIST/") path_img, path_lbl = loader.get_path(DataSetType.TRAIN) reader = MnistDataReader(path_img, path_lbl) for lbls, imgs in reader.get_next(10): imgs = imgs.astype(np.float) / 255.0 imgs_r = np.reshape(imgs, newshape=[-1, 28, 28, 1]) # Check the tensorflow model y_keras = model.predict(imgs) # Keras Model Debug inp = model.input # input placeholder outputs = [layer.output for layer in model.layers] # all layer outputs outputs = [outputs[i] for i in (1, 2, 3, 4, 6, 8)] # remove dropout, reshape functors = [K.function([inp], [out]) for out in outputs] # evaluation functions layer_outs = [func([imgs, 1.]) for func in functors] # print(layer_outs) # Check the results of the own made NN y, zs = n.forward_intermediate(imgs_r) zs = [zs[i] for i in interesting_layers] # remove reshape layers eps = 0.1 index = 0 for l_keras_out, l_out in zip(layer_outs, zs): err = np.abs((l_keras_out - l_out).flatten()) # print(l_keras_out - l_out) # err_image = 1.0 * (np.abs(l_keras_out - l_out) > eps) # # err_image = np.reshape(err_image[], newshape=(1, -1, 28, 1)) # err_image = np.squeeze(err_image[0, :, :, 0]) # plt.imshow(err_image, vmin=0.0, vmax=1.0, cmap='gray') # plt.show() right_indices = indices(err < eps, lambda b: b) false_indices = indices(err > eps, lambda b: b) wrong_values = err[false_indices] # print(wrong_values) if not np.all(right_indices): print("error in layer ", index) index += 1 lbls_pred_keras = y_keras.argmax(axis=1) lbls_pred = y.argmax(axis=1) print("Original: ", lbls.reshape(-1)) print("Keras: ", lbls_pred_keras.reshape(-1)) print("Our Model: ", lbls_pred.reshape(-1)) break
def test_tensorflow_parameter(self): r1 = ReshapeLayer(newshape=[-1, 28, 28, 1]) cn1 = Conv2dLayer(in_channels=1, out_channels=16, kernel_size=3, activation='relu') # [? 28 28 16] mp1 = MaxPool2dLayer(size=2) # [? 14 14 16] cn2 = Conv2dLayer(in_channels=16, out_channels=32, kernel_size=3, activation='relu') # [? 14 14 32] mp2 = MaxPool2dLayer(size=2) # [? 7 7 32] r2 = ReshapeLayer(newshape=[-1, 32 * 7 * 7]) fc1 = FullyConnectedLayer(input_size=32 * 7 * 7, output_size=64, activation='relu') fc2 = FullyConnectedLayer(input_size=64, output_size=10, activation='softmax') checkpoint_path = "test/training_1/cp.ckpt" checkpoint_dir = os.path.abspath(os.path.dirname(checkpoint_path)) os.path.join(checkpoint_dir, "model_config.json") if not os.path.exists(checkpoint_dir): raise RuntimeError("There is no trained model data!") # Reload the model from the 2 files we saved with open(os.path.join(checkpoint_dir, "model_config.json")) as json_file: json_config = json_file.read() model = keras.models.model_from_json(json_config) model.load_weights(os.path.join(checkpoint_dir, "weights.h5")) # Print a summary Ws = model.get_weights() # K0 = Ws[0] # plt.imshow(K0[:,:,1,1], vmin=0.0, vmax=1.0, cmap='gray') # plt.show() # See Keras Documentation # https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D # # Default ordering of weights for Conv: (batch, height, width, channels) # Default ordering of weights for Dense self.assertEqual(cn1.kernel.shape, Ws[0].shape) self.assertEqual(cn1.b.shape, Ws[1].shape) self.assertEqual(cn2.kernel.shape, Ws[2].shape) self.assertEqual(cn2.b.shape, Ws[3].shape) self.assertEqual(fc1.W.shape, Ws[4].shape) self.assertEqual(fc1.b.shape, Ws[5].shape) self.assertEqual(fc2.W.shape, Ws[6].shape) self.assertEqual(fc2.b.shape, Ws[7].shape) # Assign values cn1.kernel = Ws[0] cn1.b = Ws[1] cn2.kernel = Ws[2] cn2.b = Ws[3] fc1.W = Ws[4] fc1.b = Ws[5] fc2.W = Ws[6] fc2.b = Ws[7] layers = [r1, cn1, mp1, cn2, mp2, r2, fc1, fc2] interesting_layers = [1, 2, 3, 4, 6, 7] # don't care about reshape layers net = Network(layers) loader = MnistDataDownloader("../../test/MNIST/") path_img, path_lbl = loader.get_path(DataSetType.TRAIN) reader = MnistDataReader(path_img, path_lbl) for lbls, imgs in reader.get_next(20): imgs = imgs.astype(np.float) / 255.0 imgs_r = np.reshape(imgs, newshape=[-1, 28, 28, 1]) # Check the tensorflow model y_keras = model.predict(imgs) # Keras Model Debug inp = model.input # input placeholder outputs = [layer.output for layer in model.layers] # all layer outputs outputs = [outputs[i] for i in (1, 2, 3, 4, 6, 8)] # remove dropout, reshape functors = [K.function([inp], [out]) for out in outputs] # evaluation functions layer_outs = [func([imgs, 1.]) for func in functors] # print(layer_outs) # Check the results of the own made NN y, zs = net.forward_intermediate(imgs_r) zs = [zs[i] for i in interesting_layers] # remove reshape layers eps = 0.1 index = 0 for l_keras_out, l_out in zip(layer_outs, zs): l_keras_out = l_keras_out[0] # why ever err = np.abs((l_keras_out - l_out)) # err_subs = ind2sub(indices(err.flatten(), lambda x: x > 1), l_out.shape) # self.assertTrue(np.allclose(l_out, l_keras_out)) #print(l_keras_out - l_out) # img_keras = np.squeeze() # err_image = 1.0 * (np.abs(l_keras_out - l_out) > eps) # Test: Shift l_out # l_out[:, 0:-1, 0:-1, :] = l_out[:, 1:, 1:, :] # err_image = l_keras_out - l_out # # err_image = np.reshape(err_image, newshape=(1, 28, 28, 1)) # # err_image = np.squeeze(err_image[0, :, :, 0]) # err_image = np.concatenate([np.squeeze(err_image[0, :, :, i]) for i in range(4)], axis=1) # img_keras = np.concatenate([np.squeeze(l_keras_out[0, :, :, i]) for i in range(4)], axis=1) # img_nn = np.concatenate([np.squeeze(l_out[0, :, :, i]) for i in range(4)], axis=1) # img = np.concatenate([img_nn, img_keras, err_image], axis=0) # # fig, ax = plt.subplots() # _im = ax.imshow(img, cmap='gray') # ax.set_title('Computation Layer {}'.format(index)) # ax.set_yticks([14, 14 + 28, 14 + 2 * 28]) # ax.set_yticklabels(['Our NN', 'Keras', 'Difference']) # fig.colorbar(_im) # plt.show() #right_indices = indices(err < eps, lambda b: b) #false_indices = indices(err > eps, lambda b: b) #wrong_values = err[false_indices] #print(wrong_values) if not np.allclose(l_out, l_keras_out, atol=0.0001): print("error in layer ", index) breakpoint() index += 1 lbls_pred_keras = y_keras.argmax(axis=1) lbls_pred = y.argmax(axis=1) print("Original: ", lbls.reshape(-1)) print("Keras: ", lbls_pred_keras.reshape(-1)) print("Our Model: ", lbls_pred.reshape(-1)) break