def _runner(layer_class): """ All the recurrent layers share the same interface, so we can run through them with a single function. """ for ret_seq in [True, False]: layer = layer_class(output_dim, return_sequences=ret_seq, weights=None, input_shape=(timesteps, input_dim)) layer.input = K.variable(np.ones((nb_samples, timesteps, input_dim))) layer.get_config() for train in [True, False]: out = K.eval(layer.get_output(train)) # Make sure the output has the desired shape if ret_seq: assert out.shape == (nb_samples, timesteps, output_dim) else: assert out.shape == (nb_samples, output_dim) mask = layer.get_output_mask(train) # check statefulness layer = layer_class( output_dim, return_sequences=False, weights=None, batch_input_shape=(nb_samples, timesteps, input_dim) ) layer.input = K.variable(np.ones((nb_samples, timesteps, input_dim))) out = K.eval(layer.get_output(train)) assert out.shape == (nb_samples, output_dim)
def test_zero_padding_3d(): num_samples = 2 stack_size = 2 input_len_dim1 = 4 input_len_dim2 = 5 input_len_dim3 = 3 inputs = np.ones((num_samples, input_len_dim1, input_len_dim2, input_len_dim3, stack_size)) # basic test for data_format in ['channels_first', 'channels_last']: layer_test(convolutional.ZeroPadding3D, kwargs={'padding': (2, 2, 2), 'data_format': data_format}, input_shape=inputs.shape) layer_test(convolutional.ZeroPadding3D, kwargs={'padding': ((1, 2), (3, 4), (0, 2)), 'data_format': data_format}, input_shape=inputs.shape) # correctness test layer = convolutional.ZeroPadding3D(padding=(2, 2, 2), data_format=data_format) layer.build(inputs.shape) outputs = layer(K.variable(inputs)) np_output = K.eval(outputs) if data_format == 'channels_last': for offset in [0, 1, -1, -2]: assert_allclose(np_output[:, offset, :, :, :], 0.) assert_allclose(np_output[:, :, offset, :, :], 0.) assert_allclose(np_output[:, :, :, offset, :], 0.) assert_allclose(np_output[:, 2:-2, 2:-2, 2:-2, :], 1.) elif data_format == 'channels_first': for offset in [0, 1, -1, -2]: assert_allclose(np_output[:, :, offset, :, :], 0.) assert_allclose(np_output[:, :, :, offset, :], 0.) assert_allclose(np_output[:, :, :, :, offset], 0.) assert_allclose(np_output[:, :, 2:-2, 2:-2, 2:-2], 1.) layer = convolutional.ZeroPadding3D(padding=((1, 2), (3, 4), (0, 2)), data_format=data_format) layer.build(inputs.shape) outputs = layer(K.variable(inputs)) np_output = K.eval(outputs) if data_format == 'channels_last': for dim1_offset in [0, -1, -2]: assert_allclose(np_output[:, dim1_offset, :, :, :], 0.) for dim2_offset in [0, 1, 2, -1, -2, -3, -4]: assert_allclose(np_output[:, :, dim2_offset, :, :], 0.) for dim3_offset in [-1, -2]: assert_allclose(np_output[:, :, :, dim3_offset, :], 0.) assert_allclose(np_output[:, 1:-2, 3:-4, 0:-2, :], 1.) elif data_format == 'channels_first': for dim1_offset in [0, -1, -2]: assert_allclose(np_output[:, :, dim1_offset, :, :], 0.) for dim2_offset in [0, 1, 2, -1, -2, -3, -4]: assert_allclose(np_output[:, :, :, dim2_offset, :], 0.) for dim3_offset in [-1, -2]: assert_allclose(np_output[:, :, :, :, dim3_offset], 0.) assert_allclose(np_output[:, :, 1:-2, 3:-4, 0:-2], 1.)
def test_convolution_2d_dim_ordering(): nb_filter = 4 nb_row = 3 nb_col = 2 stack_size = 3 np.random.seed(1337) weights = [np.random.random((nb_filter, stack_size, nb_row, nb_col)), np.random.random(nb_filter)] input = np.random.random((1, stack_size, 10, 10)) layer = convolutional.Convolution2D( nb_filter, nb_row, nb_col, weights=weights, input_shape=input.shape[1:], dim_ordering='th') layer.input = K.variable(input) out_th = K.eval(layer.get_output(False)) input = np.transpose(input, (0, 2, 3, 1)) weights[0] = np.transpose(weights[0], (2, 3, 1, 0)) layer = convolutional.Convolution2D( nb_filter, nb_row, nb_col, weights=weights, input_shape=input.shape[1:], dim_ordering='tf') layer.input = K.variable(input) out_tf = K.eval(layer.get_output(False)) assert_allclose(out_tf, np.transpose(out_th, (0, 2, 3, 1)), atol=1e-05)
def test_zero_padding_1d(): num_samples = 2 input_dim = 2 num_steps = 5 shape = (num_samples, num_steps, input_dim) input = np.ones(shape) # basic test layer_test(convolutional.ZeroPadding1D, kwargs={'padding': 2}, input_shape=input.shape) layer_test(convolutional.ZeroPadding1D, kwargs={'padding': (1, 2)}, input_shape=input.shape) # correctness test layer = convolutional.ZeroPadding1D(padding=2) layer.build(shape) output = layer(K.variable(input)) np_output = K.eval(output) for offset in [0, 1, -1, -2]: assert_allclose(np_output[:, offset, :], 0.) assert_allclose(np_output[:, 2:-2, :], 1.) layer = convolutional.ZeroPadding1D(padding=(1, 2)) layer.build(shape) output = layer(K.variable(input)) np_output = K.eval(output) for left_offset in [0]: assert_allclose(np_output[:, left_offset, :], 0.) for right_offset in [-1, -2]: assert_allclose(np_output[:, right_offset, :], 0.) assert_allclose(np_output[:, 1:-2, :], 1.) layer.get_config()
def save(self, fname: str = None) -> None: """ Save the model parameters into <<fname>>_opt.json (or <<ser_file>>_opt.json) and model weights into <<fname>>.h5 (or <<ser_file>>.h5) Args: fname: file_path to save model. If not explicitly given seld.opt["ser_file"] will be used Returns: None """ if not fname: fname = self.save_path else: fname = Path(fname).resolve() if not fname.parent.is_dir(): raise ConfigError("Provided save path is incorrect!") else: opt_path = f"{fname}_opt.json" weights_path = f"{fname}.h5" log.info(f"[saving model to {opt_path}]") self.model.save_weights(weights_path) # if model was loaded from one path and saved to another one # then change load_path to save_path for config self.opt["epochs_done"] = self.epochs_done self.opt["final_lear_rate"] = K.eval(self.optimizer.lr) / (1. + K.eval(self.optimizer.decay) * self.batches_seen) if self.opt.get("load_path") and self.opt.get("save_path"): if self.opt.get("save_path") != self.opt.get("load_path"): self.opt["load_path"] = str(self.opt["save_path"]) save_json(self.opt, opt_path)
def test_sparse_concat(self): x_d = np.array([0, 7, 2, 3], dtype=np.float32) x_r = np.array([0, 2, 2, 3], dtype=np.int64) x_c = np.array([4, 3, 2, 3], dtype=np.int64) x_sparse_1 = sparse.csr_matrix((x_d, (x_r, x_c)), shape=(4, 5)) x_d = np.array([0, 7, 2, 3], dtype=np.float32) x_r = np.array([0, 2, 2, 3], dtype=np.int64) x_c = np.array([4, 3, 2, 3], dtype=np.int64) x_sparse_2 = sparse.csr_matrix((x_d, (x_r, x_c)), shape=(4, 5)) x_dense_1 = x_sparse_1.toarray() x_dense_2 = x_sparse_2.toarray() backends = [KTF] if KTH.th_sparse_module: # Theano has some dependency issues for sparse backends.append(KTH) for K in backends: k_s = K.concatenate([K.variable(x_sparse_1), K.variable(x_sparse_2)]) assert K.is_sparse(k_s) k_s_d = K.eval(k_s) k_d = K.eval(K.concatenate([K.variable(x_dense_1), K.variable(x_dense_2)])) assert k_s_d.shape == k_d.shape assert_allclose(k_s_d, k_d, atol=1e-05)
def test_prelu(): from keras.layers.advanced_activations import PReLU np.random.seed(1337) inp = get_standard_values() for train in [True, False]: # test with custom weights alphas = np.random.random(inp.shape) layer = PReLU(weights=alphas, input_shape=inp.flatten().shape) # calling build here causes an error, unclear if this is a bug # layer.build() layer.input = K.variable(inp) outp = K.eval(layer.get_output(train)) assert_allclose(inp, outp) layer.input = K.variable(-inp) outp = K.eval(layer.get_output(train)) assert_allclose(-alphas*inp, outp) # test with default weights layer = PReLU(input_shape=inp.flatten().shape) # layer.build() layer.input = K.variable(inp) outp = K.eval(layer.get_output(train)) assert_allclose(inp, outp) layer.input = K.variable(-inp) outp = K.eval(layer.get_output(train)) assert_allclose(0., alphas*outp) layer.get_config()
def test_vgg_deconv(): if K.image_data_format() == 'channels_first': x1 = K.variable(np.random.random((1, 512, 8, 8))) y1_shape = (1, 21, 18, 18) x2 = K.variable(np.random.random((1, 512, 27, 27))) y2_shape = (1, 21, 38, 38) x3 = K.variable(np.random.random((1, 256, 53, 53))) y3_shape = (1, 21, 312, 312) else: x1 = K.variable(np.random.random((1, 8, 8, 512))) y1_shape = (1, 18, 18, 21) x2 = K.variable(np.random.random((1, 27, 27, 512))) y2_shape = (1, 38, 38, 21) x3 = K.variable(np.random.random((1, 53, 53, 256))) y3_shape = (1, 312, 312, 21) upscore1 = vgg_deconv(classes=21)(x1, None) assert K.int_shape(upscore1) == y1_shape assert not np.any(np.isnan(K.eval(upscore1))) upscore2 = vgg_deconv(classes=21)(x2, upscore1) assert K.int_shape(upscore2) == y2_shape assert not np.any(np.isnan(K.eval(upscore2))) upscore3 = vgg_deconv(classes=21, kernel_size=(16, 16), strides=(8, 8))(x3, upscore2) assert K.int_shape(upscore3) == y3_shape assert not np.any(np.isnan(K.eval(upscore3)))
def test_zero_padding_2d(): num_samples = 2 stack_size = 2 input_num_row = 4 input_num_col = 5 for data_format in ['channels_first', 'channels_last']: inputs = np.ones((num_samples, input_num_row, input_num_col, stack_size)) inputs = np.ones((num_samples, stack_size, input_num_row, input_num_col)) # basic test layer_test(convolutional.ZeroPadding2D, kwargs={'padding': (2, 2), 'data_format': data_format}, input_shape=inputs.shape) layer_test(convolutional.ZeroPadding2D, kwargs={'padding': ((1, 2), (3, 4)), 'data_format': data_format}, input_shape=inputs.shape) # correctness test layer = convolutional.ZeroPadding2D(padding=(2, 2), data_format=data_format) layer.build(inputs.shape) output = layer(K.variable(inputs)) np_output = K.eval(output) if data_format == 'channels_last': for offset in [0, 1, -1, -2]: assert_allclose(np_output[:, offset, :, :], 0.) assert_allclose(np_output[:, :, offset, :], 0.) assert_allclose(np_output[:, 2:-2, 2:-2, :], 1.) elif data_format == 'channels_first': for offset in [0, 1, -1, -2]: assert_allclose(np_output[:, :, offset, :], 0.) assert_allclose(np_output[:, :, :, offset], 0.) assert_allclose(np_output[:, 2:-2, 2:-2, :], 1.) layer = convolutional.ZeroPadding2D(padding=((1, 2), (3, 4)), data_format=data_format) layer.build(inputs.shape) output = layer(K.variable(inputs)) np_output = K.eval(output) if data_format == 'channels_last': for top_offset in [0]: assert_allclose(np_output[:, top_offset, :, :], 0.) for bottom_offset in [-1, -2]: assert_allclose(np_output[:, bottom_offset, :, :], 0.) for left_offset in [0, 1, 2]: assert_allclose(np_output[:, :, left_offset, :], 0.) for right_offset in [-1, -2, -3, -4]: assert_allclose(np_output[:, :, right_offset, :], 0.) assert_allclose(np_output[:, 1:-2, 3:-4, :], 1.) elif data_format == 'channels_first': for top_offset in [0]: assert_allclose(np_output[:, :, top_offset, :], 0.) for bottom_offset in [-1, -2]: assert_allclose(np_output[:, :, bottom_offset, :], 0.) for left_offset in [0, 1, 2]: assert_allclose(np_output[:, :, :, left_offset], 0.) for right_offset in [-1, -2, -3, -4]: assert_allclose(np_output[:, :, :, right_offset], 0.) assert_allclose(np_output[:, :, 1:-2, 3:-4], 1.)
def test_cropping_3d(): num_samples = 2 stack_size = 2 input_len_dim1 = 8 input_len_dim2 = 8 input_len_dim3 = 8 cropping = ((2, 2), (3, 3), (2, 3)) for data_format in ['channels_last', 'channels_first']: if data_format == 'channels_first': inputs = np.random.rand(num_samples, stack_size, input_len_dim1, input_len_dim2, input_len_dim3) else: inputs = np.random.rand(num_samples, input_len_dim1, input_len_dim2, input_len_dim3, stack_size) # basic test layer_test(convolutional.Cropping3D, kwargs={'cropping': cropping, 'data_format': data_format}, input_shape=inputs.shape) # correctness test layer = convolutional.Cropping3D(cropping=cropping, data_format=data_format) layer.build(inputs.shape) output = layer(K.variable(inputs)) np_output = K.eval(output) # compare with numpy if data_format == 'channels_first': expected_out = inputs[:, :, cropping[0][0]: -cropping[0][1], cropping[1][0]: -cropping[1][1], cropping[2][0]: -cropping[2][1]] else: expected_out = inputs[:, cropping[0][0]: -cropping[0][1], cropping[1][0]: -cropping[1][1], cropping[2][0]: -cropping[2][1], :] assert_allclose(np_output, expected_out) for data_format in ['channels_last', 'channels_first']: if data_format == 'channels_first': inputs = np.random.rand(num_samples, stack_size, input_len_dim1, input_len_dim2, input_len_dim3) else: inputs = np.random.rand(num_samples, input_len_dim1, input_len_dim2, input_len_dim3, stack_size) # another correctness test (no cropping) cropping = ((0, 0), (0, 0), (0, 0)) layer = convolutional.Cropping3D(cropping=cropping, data_format=data_format) layer.build(inputs.shape) output = layer(K.variable(inputs)) np_output = K.eval(output) # compare with input assert_allclose(np_output, inputs)
def test_cce_one_hot(): y_a = K.variable(np.random.randint(0, 7, (5, 6))) y_b = K.variable(np.random.random((5, 6, 7))) objective_output = objectives.sparse_categorical_crossentropy(y_a, y_b) assert K.eval(objective_output).shape == (5, 6) y_a = K.variable(np.random.randint(0, 7, (6,))) y_b = K.variable(np.random.random((6, 7))) assert K.eval(objectives.sparse_categorical_crossentropy(y_a, y_b)).shape == (6,)
def test_sparse_categorical_accuracy_correctness(): y_a = K.variable(np.random.randint(0, 7, (6,)), dtype=K.floatx()) y_b = K.variable(np.random.random((6, 7)), dtype=K.floatx()) # use one_hot embedding to convert sparse labels to equivalent dense labels y_a_dense_labels = K.cast(K.one_hot(K.cast(y_a, dtype='int32'), num_classes=7), dtype=K.floatx()) sparse_categorical_acc = metrics.sparse_categorical_accuracy(y_a, y_b) categorical_acc = metrics.categorical_accuracy(y_a_dense_labels, y_b) assert np.allclose(K.eval(sparse_categorical_acc), K.eval(categorical_acc))
def test_sparse_softmax_categorical_crossentropy(): y_labels = K.variable(np.random.randint(0, 7, (5, 6))) y_logits = K.variable(np.random.random((5, 6, 7)) * 12) objective_value = K.eval(objectives.sparse_softmax_categorical_crossentropy(y_labels, y_logits)) assert objective_value.shape == (5, 6) y_a = K.variable(np.random.randint(0, 7, (6,))) y_b = K.variable(np.random.random((6, 7))) objective_value = K.eval(objectives.sparse_softmax_categorical_crossentropy(y_a, y_b)) assert objective_value.shape == (6,)
def test_regularizer(layer_class): layer = layer_class(output_dim, return_sequences=False, weights=None, batch_input_shape=(nb_samples, timesteps, embedding_dim), W_regularizer=regularizers.WeightRegularizer(l1=0.01), U_regularizer=regularizers.WeightRegularizer(l1=0.01), b_regularizer='l2') shape = (nb_samples, timesteps, embedding_dim) layer.set_input(K.variable(np.ones(shape)), shape=shape) K.eval(layer.output)
def test_mode_1(self): norm_m1 = normalization.BatchNormalization(input_shape=(10,), mode=1) for inp in [self.input_1, self.input_2, self.input_3]: norm_m1.input = K.variable(inp) out = (norm_m1.get_output(train=True) - norm_m1.beta) / norm_m1.gamma self.assertAlmostEqual(K.eval(K.mean(out)), 0.0) if inp.std() > 0.: self.assertAlmostEqual(K.eval(K.std(out)), 1.0, places=2) else: self.assertAlmostEqual(K.eval(K.std(out)), 0.0, places=2)
def test_batchnorm_mode_1(): norm_m1 = normalization.BatchNormalization(input_shape=(10,), mode=1) norm_m1.build(input_shape=(None, 10)) for inp in [input_1, input_2, input_3]: out = (norm_m1.call(K.variable(inp)) - norm_m1.beta) / norm_m1.gamma assert_allclose(K.eval(K.mean(out)), 0.0, atol=1e-1) if inp.std() > 0.: assert_allclose(K.eval(K.std(out)), 1.0, atol=1e-1) else: assert_allclose(K.eval(K.std(out)), 0.0, atol=1e-1)
def test_top_k_categorical_accuracy(): y_pred = K.variable(np.array([[0.3, 0.2, 0.1], [0.1, 0.2, 0.7]])) y_true = K.variable(np.array([[0, 1, 0], [1, 0, 0]])) success_result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, k=3)) assert success_result == 1 partial_result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, k=2)) assert partial_result == 0.5 failure_result = K.eval(metrics.top_k_categorical_accuracy(y_true, y_pred, k=1)) assert failure_result == 0
def test_foldr(self): # This test aims to make sure that we walk the array from right to left # and checks it in the following way: multiplying left to right 1e-40 # cannot be held into a float32 so it causes an underflow while from # right to left we have no such problem and the result is larger x = np.array([1e-20, 1e-20, 10, 10, 10], dtype=np.float32) for K in [KTF, KTH]: p1 = K.eval(K.foldl(lambda a, b: a * b, x)) p2 = K.eval(K.foldr(lambda a, b: a * b, x)) assert p1 < p2 assert 9e-38 < p2 <= 1e-37
def test_maxpooling_1d(self): nb_samples = 9 nb_steps = 7 input_dim = 10 input = np.ones((nb_samples, nb_steps, input_dim)) for stride in [1, 2]: layer = convolutional.MaxPooling1D(stride=stride, border_mode="valid") layer.input = K.variable(input) for train in [True, False]: K.eval(layer.get_output(train)) layer.get_config()
def test_input_output(): nb_samples = 10 input_dim = 5 layer = core.Layer() # Once an input is provided, it should be reachable through the # appropriate getters input = np.ones((nb_samples, input_dim)) layer.input = K.variable(input) for train in [True, False]: assert_allclose(K.eval(layer.get_input(train)), input) assert_allclose(K.eval(layer.get_output(train)), input)
def test_batchnorm_mode_1(): np.random.seed(1337) norm_m1 = normalization.BatchNormalization(input_shape=(10,), mode=1) for inp in [input_1, input_2, input_3]: norm_m1.input = K.variable(inp) out = (norm_m1.get_output(train=True) - norm_m1.beta) / norm_m1.gamma assert_allclose(K.eval(K.mean(out)), 0.0, atol=1e-1) if inp.std() > 0.0: assert_allclose(K.eval(K.std(out)), 1.0, atol=1e-1) else: assert_allclose(K.eval(K.std(out)), 0.0, atol=1e-1)
def normals_metric(y_true, y_pred): y_true = K.variable(y_true) y_pred = K.variable(y_pred) y_true = K.expand_dims(y_true,0) filter_y = K.variable(np.array([[ 0., -0.5 , 0.], [0., 0., 0.], [0., 0.5, 0.]]).reshape(3, 3, 1, 1)) filter_x = K.variable(np.array([ [0, 0., 0.], [0.5, 0., -0.5], [0., 0., 0.]]).reshape(3, 3, 1, 1)) dzdx = K.conv2d(K.exp(y_true), filter_x, padding='same') dzdy = K.conv2d(K.exp(y_true), filter_y, padding='same') dzdx_ = dzdx * -1.0#K.constant(-1.0, shape=[batch_size,K.int_shape(y_pred)[1],K.int_shape(y_pred)[2],K.int_shape(y_pred)[3]]) #K.constant(-1.0, shape=K.int_shape(dzdx)) dzdy_ = dzdy * -1.0#K.constant(-1.0, shape=[batch_size,K.int_shape(y_pred)[1],K.int_shape(y_pred)[2],K.int_shape(y_pred)[3]]) #K.constant(-1.0, shape=K.int_shape(dzdy)) mag_norm = K.pow(dzdx,2) + K.pow(dzdy,2) + 1.0#K.constant(1.0, shape=[batch_size,K.int_shape(y_pred)[1],K.int_shape(y_pred)[2],K.int_shape(y_pred)[3]]) #K.constant(1.0, shape=K.int_shape(dzdx)) mag_norm = K.sqrt(mag_norm) N3 = 1.0 / mag_norm #K.constant(1.0, shape=K.int_shape(dzdx)) / mag_norm N1 = dzdx_ / mag_norm N2 = dzdy_ / mag_norm normals = K.concatenate(tensors=[N1,N2,N3],axis=-1) dzdx_pred = K.conv2d(K.exp(y_pred), filter_x, padding='same') dzdy_pred = K.conv2d(K.exp(y_pred), filter_y, padding='same') mag_norm_pred = K.pow(dzdx_pred,2) + K.pow(dzdy_pred,2) + 1.0 mag_norm_pred = K.sqrt(mag_norm_pred) grad_x = K.concatenate(tensors=[1.0/ mag_norm_pred, 0.0/ mag_norm_pred, dzdx_pred/ mag_norm_pred],axis=-1) grad_y = K.concatenate(tensors=[0.0/ mag_norm_pred, 1.0/ mag_norm_pred, dzdy_pred/ mag_norm_pred],axis=-1) dot_term_x = K.mean(K.sum(normals[0,:,:,:] * grad_x[0,:,:,:], axis=-1, keepdims=True), axis=-1) dot_term_y = K.mean(K.sum(normals[0,:,:,:] * grad_y[0,:,:,:], axis=-1, keepdims=True), axis=-1) dot_term_x = K.abs(dot_term_x) dot_term_y = K.abs(dot_term_y) return K.eval(K.mean(dot_term_x)),K.eval(K.mean(dot_term_y))
def on_batch_end(self, batch, logs=None): # evaluate the variables and save them into lists x_input = K.eval(self.var_x_input) y_true = K.eval(self.var_y_true) y_pred = K.eval(self.var_y_pred) # print(y_true) # print(y_pred) # print(mat2uint8(y_true)) # print(mat2uint8(y_pred)) if showImages: self.train_plotObj.showTrainResults(x_input[0, :, :, :], y_true[0, :, :, 0], y_pred[0, :, :, 0])
def test_mode_0(self): model = Sequential() norm_m0 = normalization.BatchNormalization(input_shape=(10,)) model.add(norm_m0) model.compile(loss='mse', optimizer='sgd') # centered on 5.0, variance 10.0 X = np.random.normal(loc=5.0, scale=10.0, size=(1000, 10)) model.fit(X, X, nb_epoch=5, verbose=0) norm_m0.input = K.variable(X) out = (norm_m0.get_output(train=True) - norm_m0.beta) / norm_m0.gamma self.assertAlmostEqual(K.eval(K.mean(out)), 0.0, places=1) self.assertAlmostEqual(K.eval(K.std(out)), 1.0, places=1)
def test_maxpooling_2d(self): nb_samples = 9 stack_size = 7 input_nb_row = 11 input_nb_col = 12 pool_size = (3, 3) input = np.ones((nb_samples, stack_size, input_nb_row, input_nb_col)) for strides in [(1, 1), (2, 2)]: layer = convolutional.MaxPooling2D(strides=strides, border_mode="valid", pool_size=pool_size) layer.input = K.variable(input) for train in [True, False]: K.eval(layer.get_output(train)) layer.get_config()
def test_connections(): nb_samples = 10 input_dim = 5 layer1 = core.Layer() layer2 = core.Layer() input = np.ones((nb_samples, input_dim)) layer1.input = K.variable(input) # After connecting, input of layer1 should be passed through layer2.set_previous(layer1) for train in [True, False]: assert_allclose(K.eval(layer2.get_input(train)), input) assert_allclose(K.eval(layer2.get_output(train)), input)
def test_load_layers(): from keras.layers import ConvLSTM2D, TimeDistributed, Bidirectional, Conv2D, Input from keras.models import Model if K.backend() == 'tensorflow' or K.backend() == 'cntk': inputs = Input(shape=(10, 20, 20, 1)) else: inputs = Input(shape=(10, 1, 20, 20)) td_conv = TimeDistributed(Conv2D(15, (5, 5)))(inputs) bi_convlstm2d = Bidirectional(ConvLSTM2D(10, (3, 3)), merge_mode='concat')(td_conv) model = Model(inputs=inputs, outputs=bi_convlstm2d) weight_value_tuples = [] # TimeDistributed Conv2D layer # use 'channels_first' data format to check that the function is being called correctly for Conv2D # old: (filters, stack_size, kernel_rows, kernel_cols) # new: (kernel_rows, kernel_cols, stack_size, filters) weight_tensor_td_conv_old = list() weight_tensor_td_conv_old.append(np.zeros((15, 1, 5, 5))) weight_tensor_td_conv_old.append(np.zeros((15,))) td_conv_layer = model.layers[1] td_conv_layer.layer.data_format = 'channels_first' weight_tensor_td_conv_new = topology.preprocess_weights_for_loading( td_conv_layer, weight_tensor_td_conv_old, original_keras_version='1') symbolic_weights = td_conv_layer.weights assert (len(symbolic_weights) == len(weight_tensor_td_conv_new)) weight_value_tuples += zip(symbolic_weights, weight_tensor_td_conv_new) # Bidirectional ConvLSTM2D layer # old ConvLSTM2D took a list of 12 weight tensors, returns a list of 3 concatenated larger tensors. weight_tensor_bi_convlstm_old = [] for j in range(2): # bidirectional for i in range(4): weight_tensor_bi_convlstm_old.append(np.zeros((3, 3, 15, 10))) # kernel weight_tensor_bi_convlstm_old.append(np.zeros((3, 3, 10, 10))) # recurrent kernel weight_tensor_bi_convlstm_old.append(np.zeros((10,))) # bias bi_convlstm_layer = model.layers[2] weight_tensor_bi_convlstm_new = topology.preprocess_weights_for_loading( bi_convlstm_layer, weight_tensor_bi_convlstm_old, original_keras_version='1') symbolic_weights = bi_convlstm_layer.weights assert (len(symbolic_weights) == len(weight_tensor_bi_convlstm_new)) weight_value_tuples += zip(symbolic_weights, weight_tensor_bi_convlstm_new) K.batch_set_value(weight_value_tuples) assert np.all(K.eval(model.layers[1].weights[0]) == weight_tensor_td_conv_new[0]) assert np.all(K.eval(model.layers[1].weights[1]) == weight_tensor_td_conv_new[1]) assert np.all(K.eval(model.layers[2].weights[0]) == weight_tensor_bi_convlstm_new[0]) assert np.all(K.eval(model.layers[2].weights[1]) == weight_tensor_bi_convlstm_new[1]) assert np.all(K.eval(model.layers[2].weights[2]) == weight_tensor_bi_convlstm_new[2]) assert np.all(K.eval(model.layers[2].weights[3]) == weight_tensor_bi_convlstm_new[3]) assert np.all(K.eval(model.layers[2].weights[4]) == weight_tensor_bi_convlstm_new[4]) assert np.all(K.eval(model.layers[2].weights[5]) == weight_tensor_bi_convlstm_new[5])
def test_batchnorm_shapes(): """ Test batch normalization with various input shapes """ for inp in input_shapes: norm_m0 = normalization.BatchNormalization(batch_input_shape=inp.shape, mode=0) norm_m0.input = K.variable(inp) out = norm_m0.get_output(train=True) K.eval(out) norm_m1 = normalization.BatchNormalization(batch_input_shape=inp.shape, mode=1) norm_m1.input = K.variable(inp) out = norm_m1.get_output(train=True) K.eval(out)
def test_cropping_3d(): nb_samples = 2 stack_size = 2 input_len_dim1 = 8 input_len_dim2 = 8 input_len_dim3 = 8 cropping = ((2, 2), (3, 3), (2, 3)) dim_ordering = K.image_dim_ordering() if dim_ordering == 'th': input = np.random.rand(nb_samples, stack_size, input_len_dim1, input_len_dim2, input_len_dim3) else: input = np.random.rand(nb_samples, input_len_dim1, input_len_dim2, input_len_dim3, stack_size) # basic test layer_test(convolutional.Cropping3D, kwargs={'cropping': cropping, 'dim_ordering': dim_ordering}, input_shape=input.shape) # correctness test layer = convolutional.Cropping3D(cropping=cropping, dim_ordering=dim_ordering) layer.build(input.shape) output = layer(K.variable(input)) np_output = K.eval(output) # compare with numpy if dim_ordering == 'th': expected_out = input[:, :, cropping[0][0]: -cropping[0][1], cropping[1][0]: -cropping[1][1], cropping[2][0]: -cropping[2][1]] else: expected_out = input[:, cropping[0][0]: -cropping[0][1], cropping[1][0]: -cropping[1][1], cropping[2][0]: -cropping[2][1], :] assert_allclose(np_output, expected_out) # another correctness test (no cropping) cropping = ((0, 0), (0, 0), (0, 0)) layer = convolutional.Cropping3D(cropping=cropping, dim_ordering=dim_ordering) layer.build(input.shape) output = layer(K.variable(input)) np_output = K.eval(output) # compare with input assert_allclose(np_output, input)
def test_maxnorm(): for m in test_values: norm_instance = constraints.maxnorm(m) normed = norm_instance(K.variable(example_array)) assert(np.all(K.eval(normed) < m)) # a more explicit example norm_instance = constraints.maxnorm(2.0) x = np.array([[0, 0, 0], [1.0, 0, 0], [3, 0, 0], [3, 3, 3]]).T x_normed_target = np.array([[0, 0, 0], [1.0, 0, 0], [2.0, 0, 0], [2. / np.sqrt(3), 2. / np.sqrt(3), 2. / np.sqrt(3)]]).T x_normed_actual = K.eval(norm_instance(K.variable(x))) assert_allclose(x_normed_actual, x_normed_target, rtol=1e-05)
def test_upsampling_3d(): num_samples = 2 stack_size = 2 input_len_dim1 = 10 input_len_dim2 = 11 input_len_dim3 = 12 for data_format in ['channels_first', 'channels_last']: if data_format == 'channels_first': inputs = np.random.rand(num_samples, stack_size, input_len_dim1, input_len_dim2, input_len_dim3) else: # tf inputs = np.random.rand(num_samples, input_len_dim1, input_len_dim2, input_len_dim3, stack_size) # basic test layer_test(convolutional.UpSampling3D, kwargs={ 'size': (2, 2, 2), 'data_format': data_format }, input_shape=inputs.shape) for length_dim1 in [2, 3]: for length_dim2 in [2]: for length_dim3 in [3]: layer = convolutional.UpSampling3D(size=(length_dim1, length_dim2, length_dim3), data_format=data_format) layer.build(inputs.shape) output = layer(K.variable(inputs)) np_output = K.eval(output) if data_format == 'channels_first': assert np_output.shape[ 2] == length_dim1 * input_len_dim1 assert np_output.shape[ 3] == length_dim2 * input_len_dim2 assert np_output.shape[ 4] == length_dim3 * input_len_dim3 else: # tf assert np_output.shape[ 1] == length_dim1 * input_len_dim1 assert np_output.shape[ 2] == length_dim2 * input_len_dim2 assert np_output.shape[ 3] == length_dim3 * input_len_dim3 # compare with numpy if data_format == 'channels_first': expected_out = np.repeat(inputs, length_dim1, axis=2) expected_out = np.repeat(expected_out, length_dim2, axis=3) expected_out = np.repeat(expected_out, length_dim3, axis=4) else: # tf expected_out = np.repeat(inputs, length_dim1, axis=1) expected_out = np.repeat(expected_out, length_dim2, axis=2) expected_out = np.repeat(expected_out, length_dim3, axis=3) assert_allclose(np_output, expected_out)
index_array=np.arange(x_train.shape[0]) batch_num=np.int(np.ceil(x_train.shape[0]/batch_size)) for i in np.arange(user_epochs): np.random.shuffle(index_array) for j in np.arange(batch_num): x_batch=x_train[index_array[(j%batch_num)*batch_size:min((j%batch_num+1)*batch_size,x_train.shape[0])],:] y_batch=y_train[index_array[(j%batch_num)*batch_size:min((j%batch_num+1)*batch_size,x_train.shape[0])],:] model.train_on_batch(x_batch,y_batch) #if (i+1)%150==0: #decay the learning rate by 0.1 # K.set_value(model.optimizer.lr,K.eval(model.optimizer.lr*0.1)) # print("Learning rate: {}".format(K.eval(model.optimizer.lr))) K.set_value(model.optimizer.lr, lr_schedule(i)) if (i+1)%5==0: print("Epochs: {}".format(i)) print("Learning rate: {}".format(K.eval(model.optimizer.lr))) scores_test = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', scores_test[0]) print('Test accuracy:', scores_test[1]) scores_train = model.evaluate(x_train, y_train, verbose=0) print('Train loss:', scores_train[0]) print('Train accuracy:', scores_train[1]) ##save the model if save_model: weights=model.get_weights() if not os.path.exists(result_folder): os.makedirs(result_folder) if not os.path.exists(result_folder+"/models"): os.makedirs(result_folder+"/models") np.savez(result_folder+"/models/"+"epoch_{}_weights_user.npz".format(user_epochs),x=weights)
def on_epoch_end(self, epoch, logs=None): lr = self.model.optimizer.lr print("lr = {}".format(K.eval(lr)))
def evaluate_keras_metric(y_true, y_pred, metric): objective_function = metrics.get(metric) objective = objective_function(y_true, y_pred) return K.eval(objective)
projection_after = pca.transform(afterCalib) # The PCs most correlated with the batch are 3 and 5 pc1 = 3 pc2 = 5 sh.scatterHist(target[:, pc1], target[:, pc2], source[:, pc1], source[:, pc2]) sh.scatterHist(target[:, pc1], target[:, pc2], afterCalib[:, pc1], afterCalib[:, pc2]) ##################################### quantitative evaluation: MMD ##################################### # MMD with the scales used for training sourceInds = np.random.randint(low=0, high=source.shape[0], size=1000) targetInds = np.random.randint(low=0, high=target.shape[0], size=1000) mmd_before = K.eval( cf.MMD(block2_output, target).cost(K.variable(value=source[sourceInds]), K.variable(value=target[targetInds]))) mmd_after = K.eval( cf.MMD(block2_output, target).cost(K.variable(value=afterCalib[sourceInds]), K.variable(value=target[targetInds]))) print('MMD before calibration: ' + str(mmd_before)) print('MMD after calibration: ' + str(mmd_after)) ''' this script gives: MMD before calibration: 0.384037 MMD after calibration: 0.142719 ''' ############################ save results ######################################## calibratedSource = calibMMDNet.predict(source)
def test_zero_padding_2d(): num_samples = 2 stack_size = 2 input_num_row = 4 input_num_col = 5 for data_format in ['channels_first', 'channels_last']: inputs = np.ones( (num_samples, input_num_row, input_num_col, stack_size)) inputs = np.ones( (num_samples, stack_size, input_num_row, input_num_col)) # basic test layer_test(convolutional.ZeroPadding2D, kwargs={ 'padding': (2, 2), 'data_format': data_format }, input_shape=inputs.shape) layer_test(convolutional.ZeroPadding2D, kwargs={ 'padding': ((1, 2), (3, 4)), 'data_format': data_format }, input_shape=inputs.shape) # correctness test layer = convolutional.ZeroPadding2D(padding=(2, 2), data_format=data_format) layer.build(inputs.shape) output = layer(K.variable(inputs)) np_output = K.eval(output) if data_format == 'channels_last': for offset in [0, 1, -1, -2]: assert_allclose(np_output[:, offset, :, :], 0.) assert_allclose(np_output[:, :, offset, :], 0.) assert_allclose(np_output[:, 2:-2, 2:-2, :], 1.) elif data_format == 'channels_first': for offset in [0, 1, -1, -2]: assert_allclose(np_output[:, :, offset, :], 0.) assert_allclose(np_output[:, :, :, offset], 0.) assert_allclose(np_output[:, 2:-2, 2:-2, :], 1.) layer = convolutional.ZeroPadding2D(padding=((1, 2), (3, 4)), data_format=data_format) layer.build(inputs.shape) output = layer(K.variable(inputs)) np_output = K.eval(output) if data_format == 'channels_last': for top_offset in [0]: assert_allclose(np_output[:, top_offset, :, :], 0.) for bottom_offset in [-1, -2]: assert_allclose(np_output[:, bottom_offset, :, :], 0.) for left_offset in [0, 1, 2]: assert_allclose(np_output[:, :, left_offset, :], 0.) for right_offset in [-1, -2, -3, -4]: assert_allclose(np_output[:, :, right_offset, :], 0.) assert_allclose(np_output[:, 1:-2, 3:-4, :], 1.) elif data_format == 'channels_first': for top_offset in [0]: assert_allclose(np_output[:, :, top_offset, :], 0.) for bottom_offset in [-1, -2]: assert_allclose(np_output[:, :, bottom_offset, :], 0.) for left_offset in [0, 1, 2]: assert_allclose(np_output[:, :, :, left_offset], 0.) for right_offset in [-1, -2, -3, -4]: assert_allclose(np_output[:, :, :, right_offset], 0.) assert_allclose(np_output[:, :, 1:-2, 3:-4], 1.)
plt.legend(['before calib.', 'ResNet calib.', 'MLP calib.'], loc=2) plt.yticks([]) plt.show() ##################################### quantitative evaluation: MMD ##################################### # MMD with the scales used for training mmd_before = np.zeros(5) mmd_after_resNet = np.zeros(5) mmd_after_MLP = np.zeros(5) mmd_target_target = np.zeros(5) for i in range(5): sourceInds = np.random.randint(low=0, high=source.shape[0], size=1000) targetInds = np.random.randint(low=0, high=target.shape[0], size=1000) targetInds1 = np.random.randint(low=0, high=target.shape[0], size=1000) mmd_before[i] = K.eval( cf.MMD(source, target).cost(K.variable(value=source[sourceInds]), K.variable(value=target[targetInds]))) mmd_after_resNet[i] = K.eval( cf.MMD(calibratedSource_resNet, target).cost( K.variable(value=calibratedSource_resNet[sourceInds]), K.variable(value=target[targetInds]))) mmd_after_MLP[i] = K.eval( cf.MMD(calibratedSource_MLP, target).cost(K.variable(value=calibratedSource_MLP[sourceInds]), K.variable(value=target[targetInds]))) mmd_target_target[i] = K.eval( cf.MMD(target, target).cost(K.variable(value=target[targetInds]), K.variable(value=target[targetInds1]))) print('MMD before calibration: ' + str(np.mean(mmd_before)) + 'pm ' + str(np.std(mmd_before)))
# for each class: how many where classified as said class pred_cnt = K.sum(y_pred_ones, axis=0) # for each class: how many are true members of said class gold_cnt = K.sum(y_true, axis=0) # precision for each class precision = K.switch(K.equal(pred_cnt, 0), 0, y_true_pred / pred_cnt) # recall for each class recall = K.switch(K.equal(gold_cnt, 0), 0, y_true_pred / gold_cnt) # f1 for each class f1_class = K.switch(K.equal(precision + recall, 0), 0, 2 * (precision * recall) / (precision + recall)) # return average f1 score over all classes return f1_class[1] if __name__ == '__main__': y_true = np.array([0.0, 0.2, 0.4, 0.4]) y_pred = np.array([0.0, 0.2, 0.4, 0.4]) print(pearsonr(y_true, y_pred)) y_true = K.variable(value=y_true) y_pred = K.variable(value=y_pred) print(K.eval(pearsons_correlation(y_true, y_pred)))
def main(): dataset = load_dataset() train_data = np.asarray(dataset['train']['data']) train_labels = dataset['train']['label'] num_classes = len(np.unique(train_labels)) # mask = (np.std(train_data, axis=0) > 5e-3).astype(int).flatten() test_data = np.asarray(dataset['test']['data']) test_labels = dataset['test']['label'] train_labels = to_categorical(train_labels, num_classes=num_classes) test_labels = to_categorical(test_labels, num_classes=num_classes) generator = dataset['generator'] generator_fs = dataset['generator_fs'] generator_kwargs = {'batch_size': batch_size} print('reps : ', reps) name = 'fashion_mnist_' + classifier_network + '_r_' + str(regularization) print(name) model_kwargs = {'nclasses': num_classes, 'regularization': regularization} total_features = int(np.prod(train_data.shape[1:])) model_filename = directory + classifier_network + '_trained_model.h5' fs_filename = directory + fs_network + '_trained_model.h5' for network_name in (fs_network, classifier_network): filename = directory + network_name + '_trained_model.h5' if not os.path.isdir(directory): os.makedirs(directory) if not os.path.exists(filename) and warming_up: np.random.seed(1001) tf.set_random_seed(1001) model = getattr(network_models, network_name)(input_shape=train_data.shape[1:], **model_kwargs) print('training_model') model.fit_generator( generator.flow(train_data, train_labels, **generator_kwargs), steps_per_epoch=train_data.shape[0] // batch_size, epochs=80, callbacks=[ callbacks.LearningRateScheduler(scheduler(factor=1.)) ], validation_data=(test_data, test_labels), validation_steps=test_data.shape[0] // batch_size, verbose=verbose) model.save(filename) del model K.clear_session() for e2efs_class in e2efs_classes: nfeats = [] accuracies = [] times = [] cont_seed = 0 for r in range(reps): temp_filename = temp_directory + fs_network + '_' + e2efs_class.__name__ + \ '_e2efs_heatmap_iter_' + str(r) + '.npy' if os.path.exists(temp_filename): heatmap = np.load(temp_filename) else: heatmap = np.zeros(np.prod(train_data.shape[1:])) start_time = time.time() for fs_r in range(fs_reps): print('rep : ', fs_r) np.random.seed(cont_seed) tf.set_random_seed(cont_seed) cont_seed += 1 classifier = load_model( fs_filename) if warming_up else getattr( network_models, fs_network)( input_shape=train_data.shape[1:], **model_kwargs) e2efs_layer = e2efs_class( 1, input_shape=train_data.shape[1:], ) # kernel_initializer=initializers.constant(mask)) model = e2efs_layer.add_to_model( classifier, input_shape=train_data.shape[1:]) optimizer = custom_optimizers.E2EFS_SGD( e2efs_layer=e2efs_layer, lr=1e-1) # optimizers.adam(lr=1e-2) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc']) model.fs_layer = e2efs_layer model.classifier = classifier model.summary() model.fit_generator( generator_fs.flow(train_data, train_labels, **generator_kwargs), steps_per_epoch=train_data.shape[0] // batch_size, epochs=20000, callbacks=[ E2EFSCallback(units=int(total_features * 0.05), verbose=verbose) ], validation_data=(test_data, test_labels), validation_steps=test_data.shape[0] // batch_size, verbose=verbose) heatmap += K.eval(model.heatmap) del model K.clear_session() if not os.path.isdir(temp_directory): os.makedirs(temp_directory) np.save(temp_filename, heatmap) times.append(time.time() - start_time) fs_rank = np.argsort(heatmap)[::-1] for i, factor in enumerate([.05, .1, .25, .5]): print('factor : ', factor, ' , rep : ', r) n_features = int(total_features * factor) mask = np.zeros(train_data.shape[1:]) mask.flat[fs_rank[:n_features]] = 1. np.random.seed(cont_seed) tf.set_random_seed(cont_seed) cont_seed += 1 model = load_model(model_filename) if warming_up else getattr( network_models, classifier_network)( input_shape=train_data.shape[1:], **model_kwargs) optimizer = optimizers.SGD(lr=1e-1) # optimizers.adam(lr=1e-2) model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['acc']) model.fit_generator( generator.flow(mask * train_data, train_labels, **generator_kwargs), steps_per_epoch=train_data.shape[0] // batch_size, epochs=80, callbacks=[ callbacks.LearningRateScheduler(scheduler()), ], validation_data=(mask * test_data, test_labels), validation_steps=test_data.shape[0] // batch_size, verbose=verbose) acc = model.evaluate(mask * test_data, test_labels, verbose=0)[-1] if i < len(accuracies): accuracies[i].append(acc) else: accuracies.append([acc]) nfeats.append(n_features) del model K.clear_session() print('n_features : ', n_features, ', acc : ', acc, ', time : ', times[-1]) output_filename = directory + fs_network + '_' + classifier_network + '_' + e2efs_class.__name__ + \ '_results_warming_' + str(warming_up) + '.json' try: with open(output_filename) as outfile: info_data = json.load(outfile) except: info_data = {} if name not in info_data: info_data[name] = [] info_data[name].append({ 'regularization': regularization, 'reps': reps, 'classification': { 'n_features': nfeats, 'accuracy': accuracies, 'times': times } }) with open(output_filename, 'w') as outfile: json.dump(info_data, outfile)
def on_epoch_begin(self, batch, logs={}): self.lr.append(K.eval(self.model.optimizer.lr))
def test_transfer(dset='mnist', random_seed=9, epochs=10, data_augmentation=False, batch_size = 512,ntrn=None,ntst=None,mod='focusing'): import os import numpy as np #os.environ['CUDA_VISIBLE_DEVICES']="0" #os.environ['TF_FORCE_GPU_ALLOW_GROWTH']="true" import keras from keras.losses import mse from keras.optimizers import SGD, RMSprop from keras.datasets import mnist,fashion_mnist, cifar10 from keras.models import Sequential, Model from keras.layers import Input, Dense, Dropout, Flatten,Conv2D, BatchNormalization from keras.layers import Activation, Permute,Concatenate,GlobalAveragePooling2D from skimage import filters from keras import backend as K from keras_utils import WeightHistory as WeightHistory from keras_utils import RecordVariable, \ PrintLayerVariableStats, PrintAnyVariable, \ SGDwithLR, eval_Kdict, standarize_image_025 from keras_preprocessing.image import ImageDataGenerator from Kfocusing import FocusedLayer1D from keras.engine.topology import Layer from keras import activations, regularizers, constraints from keras import initializers from keras.engine import InputSpec import tensorflow as tf from keras.applications.inception_v3 import InceptionV3 #import keras.applications.resnet50 as resnet #from keras.applications.resnet50 import preprocess_input from keras.applications import VGG16 from keras.applications.vgg16 import preprocess_input #Load the VGG model config = tf.ConfigProto() config.gpu_options.per_process_gpu_memory_fraction = 0.3 # Create a session with the above options specified. K.tensorflow_backend.set_session(tf.Session(config=config)) K.clear_session() sid = random_seed #test_acnn = True np.random.seed(sid) tf.random.set_random_seed(sid) tf.compat.v1.random.set_random_seed(sid) from datetime import datetime now = datetime.now() if dset=='mnist': # input image dimensions img_rows, img_cols = 28, 28 # the data, split between train and test sets (x_train, y_train), (x_test, y_test) = mnist.load_data() NTRN = ntrn if ntrn else x_train.shape[0] NTST = ntst if ntst else x_test.shape[0] n_channels=1 lr_dict = {'all':0.1, 'focus-1/Sigma:0': 0.01,'focus-1/Mu:0': 0.01,'focus-1/Weights:0': 0.1, 'focus-2/Sigma:0': 0.01,'focus-2/Mu:0': 0.01,'focus-2/Weights:0': 0.1} mom_dict = {'all':0.9,'focus-1/Sigma:0': 0.9,'focus-1/Mu:0': 0.9, 'focus-2/Sigma:0': 0.9,'focus-2/Mu:0': 0.9} decay_dict = {'all':0.9, 'focus-1/Sigma:0': 0.1,'focus-1/Mu:0':0.1, 'focus-2/Sigma:0': 0.1,'focus-2/Mu:0': 0.1} clip_dict = {'focus-1/Sigma:0':(0.01,1.0),'focus-1/Mu:0':(0.0,1.0), 'focus-2/Sigma:0':(0.01,1.0),'focus-2/Mu:0':(0.0,1.0)} e_i = x_train.shape[0] // batch_size decay_epochs =np.array([e_i*100], dtype='int64') elif dset=='cifar10': img_rows, img_cols = 32,32 n_channels=3 (x_train, y_train), (x_test, y_test) = cifar10.load_data() NTRN = ntrn if ntrn else x_train.shape[0] NTST = ntst if ntst else x_test.shape[0] lr_dict = {'all':1e-3, 'focus-1/Sigma:0': 1e-3,'focus-1/Mu:0': 1e-3,'focus-1/Weights:0':1e-3, 'focus-2/Sigma:0': 1e-3,'focus-2/Mu:0': 1e-3,'focus-2/Weights:0': 1e-3, 'dense_1/Weights:0':1e-3} # 1e-3 'all' reaches 91.43 at 250 epochs # 1e-3 'all' reached 90.75 at 100 epochs mom_dict = {'all':0.9,'focus-1/Sigma:0': 0.9,'focus-1/Mu:0': 0.9, 'focus-2/Sigma:0': 0.9,'focus-2/Mu:0': 0.9} #decay_dict = {'all':0.9} decay_dict = {'all':0.9, 'focus-1/Sigma:0': 0.9,'focus-1/Mu:0':0.9, 'focus-2/Sigma:0': 0.9,'focus-2/Mu:0': 0.9} clip_dict = {'focus-1/Sigma:0':(0.01,1.0),'focus-1/Mu:0':(0.0,1.0), 'focus-2/Sigma:0':(0.01,1.0),'focus-2/Mu:0':(0.0,1.0)} e_i = NTRN // batch_size #decay_epochs =np.array([e_i*10], dtype='int64') #for 20 epochs #decay_epochs =np.array([e_i*10,e_i*80,e_i*120,e_i*160], dtype='int64') decay_epochs =np.array([e_i*10, e_i*80, e_i*120, e_i*180], dtype='int64') num_classes = np.unique(y_train).shape[0] if K.image_data_format() == 'channels_first': x_train = x_train.reshape(x_train.shape[0], n_channels, img_rows, img_cols) x_test = x_test.reshape(x_test.shape[0], n_channels, img_rows, img_cols) input_shape = (n_channels, img_rows, img_cols) else: x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, n_channels) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, n_channels) input_shape = (img_rows, img_cols, n_channels) x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, n_channels) x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, n_channels) input_shape = (img_rows, img_cols, n_channels) print('x_train shape:', x_train.shape) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') #FRAME_SIZE=(299,299,3) FRAME_SIZE = (224,224,3) idx = np.random.permutation(x_train.shape[0]) x_train = x_train[idx[0:NTRN]] y_train = y_train[idx[0:NTRN]] idx = np.random.permutation(x_test.shape[0]) x_test = x_test[idx[0:NTST]] y_test = y_test[idx[0:NTST]] # convert class vectors to binary class matrices y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) x_train = x_train.astype('float32') x_test = x_test.astype('float32') #x_train, _, x_test = paddataset(x_train,None,x_test,FRAME_SIZE,False,5) #x_train, _, x_test = standarize_image_025(x_train,tst=x_test) x_train=preprocess_input(x_train) x_test=preprocess_input(x_test) import matplotlib.pyplot as plt plt.imshow(x_train[0]) print(np.max(x_train[0]),np.mean(x_train[0])) plt.show() plt.imshow(x_test[0]) print(np.max(x_train[0]),np.mean(x_train[0])) plt.show() print(x_train.shape, 'train samples') print(np.mean(x_train)) print(np.var(x_train)) print(x_test.shape, 'test samples') print(np.mean(x_test)) print(np.var(x_test)) # create the base pre-trained model base_in = Input(shape=input_shape, name='inputlayer') base_model = VGG16(weights='imagenet', include_top=False, input_shape=input_shape, input_tensor=base_in) x=base_model.output x = GlobalAveragePooling2D()(x) pad_input =True if pad_input: print("PADDING LAYER OUPUT") paddings = tf.constant([[0, 0,], [3, 3]]) padding_layer = keras.layers.Lambda(lambda x: tf.pad(x,paddings,"CONSTANT")) x = padding_layer(x) #x = Dropout(0.1)(x) # let's add a fully-connected layer focusing=mod=='focused' if focusing: nf = 40#init_sigma=np.exp(-(np.linspace(0.1, 0.9, nf)-0.5)**2/0.07), x = FocusedLayer1D(units=nf, name='focus-1', activation='linear', init_sigma=0.08, init_mu='spread', init_w= None, train_sigma=True, train_weights=True, train_mu = True, si_regularizer=None, normed=2)(x) elif mod=='dense': x = Dense(40, activation='linear')(x) else: print('unknown mod') return x = BatchNormalization()(x) x = Activation('relu')(x) x = Dropout(0.2)(x) # and a logistic layer -- let's say we have 200 classes predictions = Dense(10, activation='softmax')(x) model = Model(inputs=base_in, outputs=[predictions]) # opt= SGDwithLR(lr_dict, mom_dict,decay_dict,clip_dict, decay_epochs)#, decay=None) optimizer_s = 'SGDwithLR' if optimizer_s == 'SGDwithLR': opt = SGDwithLR(lr_dict, mom_dict,decay_dict,clip_dict, decay_epochs)#, decay=None) elif optimizer_s=='RMSprob': opt = RMSprop(lr=0.01, rho=0.9, epsilon=None, decay=0.0) else: # opt= SGDwithLR({'all': 0.01},{'all':0.9})#, decay=None) opt= SGD(lr=0.01, momentum=0.9)#, decay=None) # compile the model (should be done *after* setting layers to non-trainable) model.compile(loss=keras.losses.categorical_crossentropy, optimizer=opt, metrics=['accuracy']) model.summary() stat_func_name = ['max: ', 'mean: ', 'min: ', 'var: ', 'std: '] stat_func_list = [np.max, np.mean, np.min, np.var, np.std] callbacks = [] if focusing: pr_1 = PrintLayerVariableStats("focus-1","Weights:0",stat_func_list,stat_func_name) pr_2 = PrintLayerVariableStats("focus-1","Sigma:0",stat_func_list,stat_func_name) pr_3 = PrintLayerVariableStats("focus-1","Mu:0",stat_func_list,stat_func_name) callbacks+=[pr_1,pr_2,pr_3] recordvariables=False if recordvariables: rv_weights_1 = RecordVariable("focus-1","Weights:0") rv_sigma_1 = RecordVariable("focus-1","Sigma:0") callbacks+=[rv_weights_1,rv_sigma_1] if optimizer_s =='SGDwithLR': print_lr_rates_callback = keras.callbacks.LambdaCallback( on_epoch_end=lambda epoch, logs: print("iter: ", K.eval(model.optimizer.iterations), " LR RATES :", eval_Kdict(model.optimizer.lr))) callbacks.append(print_lr_rates_callback) if not data_augmentation: print('Not using data augmentation.') history=model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test), shuffle=True, callbacks=callbacks) else: print('Using real-time data augmentation.') # This will do preprocessing and realtime data augmentation: datagen = ImageDataGenerator( # set input mean to 0 over the dataset featurewise_center=False, # set each sample mean to 0 samplewise_center=False, # divide inputs by std of dataset featurewise_std_normalization=False, # divide each input by its std samplewise_std_normalization=False, # apply ZCA whitening zca_whitening=False, # epsilon for ZCA whitening zca_epsilon=1e-06, # randomly rotate images in the range (deg 0 to 180) rotation_range=0, # randomly shift images horizontally width_shift_range=0.2, # randomly shift images vertically height_shift_range=0.2, # set range for random shear shear_range=0.1, # set range for random zoom zoom_range=0.1, # set range for random channel shifts channel_shift_range=0., # set mode for filling points outside the input boundaries fill_mode='nearest', # value used for fill_mode = "constant" cval=0., # randomly flip images horizontal_flip=True, # randomly flip images vertical_flip=False, # set rescaling factor (applied before any other transformation) rescale=None, # set function that will be applied on each input preprocessing_function=None, # image data format, either "channels_first" or "channels_last" data_format='channels_last', # fraction of images reserved for validation (strictly between 0 and 1) validation_split=0.0) # Compute quantities required for featurewise normalization # (std, mean, and principal components if ZCA whitening is applied). datagen.fit(x_train) #x_test,_,_ = paddataset(x_test,None, None,frame_size=FRAME_SIZE, random_pos=False) # Fit the model on the batches generated by datagen.flow(). history=model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), validation_data=(x_test, y_test), workers=4, use_multiprocessing=False,epochs=epochs, verbose=2, callbacks=callbacks, steps_per_epoch=x_train.shape[0]//batch_size) score = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', score[0]) print('Test accuracy:', score[1]) return score,history,model
def test_zero_padding_3d(): num_samples = 2 stack_size = 2 input_len_dim1 = 4 input_len_dim2 = 5 input_len_dim3 = 3 inputs = np.ones((num_samples, input_len_dim1, input_len_dim2, input_len_dim3, stack_size)) # basic test for data_format in ['channels_first', 'channels_last']: layer_test(convolutional.ZeroPadding3D, kwargs={ 'padding': (2, 2, 2), 'data_format': data_format }, input_shape=inputs.shape) layer_test(convolutional.ZeroPadding3D, kwargs={ 'padding': ((1, 2), (3, 4), (0, 2)), 'data_format': data_format }, input_shape=inputs.shape) # correctness test layer = convolutional.ZeroPadding3D(padding=(2, 2, 2), data_format=data_format) layer.build(inputs.shape) output = layer(K.variable(inputs)) np_output = K.eval(output) if data_format == 'channels_last': for offset in [0, 1, -1, -2]: assert_allclose(np_output[:, offset, :, :, :], 0.) assert_allclose(np_output[:, :, offset, :, :], 0.) assert_allclose(np_output[:, :, :, offset, :], 0.) assert_allclose(np_output[:, 2:-2, 2:-2, 2:-2, :], 1.) elif data_format == 'channels_first': for offset in [0, 1, -1, -2]: assert_allclose(np_output[:, :, offset, :, :], 0.) assert_allclose(np_output[:, :, :, offset, :], 0.) assert_allclose(np_output[:, :, :, :, offset], 0.) assert_allclose(np_output[:, :, 2:-2, 2:-2, 2:-2], 1.) layer = convolutional.ZeroPadding3D(padding=((1, 2), (3, 4), (0, 2)), data_format=data_format) layer.build(inputs.shape) output = layer(K.variable(inputs)) np_output = K.eval(output) if data_format == 'channels_last': for dim1_offset in [0, -1, -2]: assert_allclose(np_output[:, dim1_offset, :, :, :], 0.) for dim2_offset in [0, 1, 2, -1, -2, -3, -4]: assert_allclose(np_output[:, :, dim2_offset, :, :], 0.) for dim3_offset in [-1, -2]: assert_allclose(np_output[:, :, :, dim3_offset, :], 0.) assert_allclose(np_output[:, 1:-2, 3:-4, 0:-2, :], 1.) elif data_format == 'channels_first': for dim1_offset in [0, -1, -2]: assert_allclose(np_output[:, :, dim1_offset, :, :], 0.) for dim2_offset in [0, 1, 2, -1, -2, -3, -4]: assert_allclose(np_output[:, :, :, dim2_offset, :], 0.) for dim3_offset in [-1, -2]: assert_allclose(np_output[:, :, :, :, dim3_offset], 0.) assert_allclose(np_output[:, :, 1:-2, 3:-4, 0:-2], 1.)
def test_cropping_3d(): num_samples = 2 stack_size = 2 input_len_dim1 = 8 input_len_dim2 = 8 input_len_dim3 = 8 cropping = ((2, 2), (3, 3), (2, 3)) for data_format in ['channels_last', 'channels_first']: if data_format == 'channels_first': inputs = np.random.rand(num_samples, stack_size, input_len_dim1, input_len_dim2, input_len_dim3) else: inputs = np.random.rand(num_samples, input_len_dim1, input_len_dim2, input_len_dim3, stack_size) # basic test layer_test(convolutional.Cropping3D, kwargs={ 'cropping': cropping, 'data_format': data_format }, input_shape=inputs.shape) # correctness test layer = convolutional.Cropping3D(cropping=cropping, data_format=data_format) layer.build(inputs.shape) outputs = layer(K.variable(inputs)) np_output = K.eval(outputs) # compare with numpy if data_format == 'channels_first': expected_out = inputs[:, :, cropping[0][0]:-cropping[0][1], cropping[1][0]:-cropping[1][1], cropping[2][0]:-cropping[2][1]] else: expected_out = inputs[:, cropping[0][0]:-cropping[0][1], cropping[1][0]:-cropping[1][1], cropping[2][0]:-cropping[2][1], :] assert_allclose(np_output, expected_out) for data_format in ['channels_last', 'channels_first']: if data_format == 'channels_first': inputs = np.random.rand(num_samples, stack_size, input_len_dim1, input_len_dim2, input_len_dim3) else: inputs = np.random.rand(num_samples, input_len_dim1, input_len_dim2, input_len_dim3, stack_size) # another correctness test (no cropping) cropping = ((0, 0), (0, 0), (0, 0)) layer = convolutional.Cropping3D(cropping=cropping, data_format=data_format) layer.build(inputs.shape) outputs = layer(K.variable(inputs)) np_output = K.eval(outputs) # compare with input assert_allclose(np_output, inputs) # Test invalid use cases with pytest.raises(ValueError): layer = convolutional.Cropping3D(cropping=((1, 1), )) with pytest.raises(ValueError): layer = convolutional.Cropping3D(cropping=lambda x: x)
def train_nets(gan, cnn, caps, x, y, p, name, adversarial_w): options = parse_inputs() c = color_codes() # Data stuff patient_path = '/'.join(p[0].rsplit('/')[:-1]) train_data, train_labels = get_names_from_path(options) # Prepare the net hyperparameters epochs = options['epochs'] patch_width = options['patch_width'] patch_size = (patch_width, patch_width, patch_width) batch_size = options['batch_size'] preload = options['preload'] print(c['c'] + '[' + strftime("%H:%M:%S") + '] ' + c['g'] + 'Training the networks ' + c['nc'] + c['lgy'] + '(' + 'CNN' + c['nc'] + '/' + c['r'] + 'CAPS' + c['nc'] + '/' + c['y'] + 'GAN' + c['nc'] + ': ' + c['b'] + '%d' % gan.count_params() + c['nc'] + '/' + c['b'] + '%d ' % cnn.count_params() + c['nc'] + 'parameters)') net_name = os.path.join(patient_path, name) checkpoint_name = os.path.join(patient_path, net_name + '.weights') try: cnn.load_weights(checkpoint_name + '.net.e%d' % epochs) caps.load_weights(checkpoint_name + '.caps.e%d' % epochs) gan.load_weights(checkpoint_name + '.gan.e%d' % epochs) except IOError: x_disc, y_disc = load_patches_gandisc_by_batches( source_names=train_data, target_names=[p], n_centers=len(x), size=patch_size, preload=preload, batch_size=51200 ) print(' '.join([''] * 15) + c['g'] + 'Starting the training process' + c['nc']) for e in range(epochs): print(' '.join([''] * 16) + c['g'] + 'Epoch ' + c['b'] + '%d' % (e + 1) + c['nc'] + c['g'] + '/%d' % epochs + c['nc']) try: cnn.load_weights(checkpoint_name + '.net.e%d' % (e + 1)) except IOError: print(c['lgy'], end='\r') cnn.fit(x, y, batch_size=batch_size, epochs=1) try: caps.load_weights(checkpoint_name + '.caps.e%d' % (e + 1)) except IOError: print(c['r'], end='\r') caps.fit(x, y, batch_size=batch_size, epochs=1) try: gan.load_weights(checkpoint_name + '.gan.e%d' % (e + 1)) except IOError: print(c['y'], end='\r') gan.fit([x, x_disc], [y, y_disc], batch_size=batch_size, epochs=1) print(c['nc'], end='\r') cnn.save_weights(checkpoint_name + '.net.e%d' % (e + 1)) caps.save_weights(checkpoint_name + '.caps.e%d' % (e + 1)) gan.save_weights(checkpoint_name + '.gan.e%d' % (e + 1)) adversarial_weight = min([np.array(K.eval(adversarial_w)) + 0.1, 1.0]) K.set_value(adversarial_w, adversarial_weight)
def on_epoch_end(self, epoch, logs=None): logs.update({'lr': K.eval(self.model.optimizer.lr)}) super().on_epoch_end(epoch, logs)
def test_load_layers(): from keras.layers import ConvLSTM2D, TimeDistributed, Bidirectional, Conv2D, Input from keras.models import Model from keras.engine.topology import preprocess_weights_for_loading if K.backend() == 'tensorflow' or K.backend() == 'cntk': inputs = Input(shape=(10, 20, 20, 1)) else: inputs = Input(shape=(10, 1, 20, 20)) td_conv = TimeDistributed(Conv2D(15, (5, 5)))(inputs) bi_convlstm2d = Bidirectional(ConvLSTM2D(10, (3, 3)), merge_mode='concat')(td_conv) model = Model(inputs=inputs, outputs=bi_convlstm2d) weight_value_tuples = [] # TimeDistributed Conv2D layer # use 'channels_first' data format to check that the function is being called correctly for Conv2D # old: (filters, stack_size, kernel_rows, kernel_cols) # new: (kernel_rows, kernel_cols, stack_size, filters) weight_tensor_td_conv_old = list() weight_tensor_td_conv_old.append(np.zeros((15, 1, 5, 5))) weight_tensor_td_conv_old.append(np.zeros((15, ))) td_conv_layer = model.layers[1] td_conv_layer.layer.data_format = 'channels_first' weight_tensor_td_conv_new = preprocess_weights_for_loading( td_conv_layer, weight_tensor_td_conv_old, original_keras_version='1') symbolic_weights = td_conv_layer.weights assert (len(symbolic_weights) == len(weight_tensor_td_conv_new)) weight_value_tuples += zip(symbolic_weights, weight_tensor_td_conv_new) # Bidirectional ConvLSTM2D layer # old ConvLSTM2D took a list of 12 weight tensors, returns a list of 3 concatenated larger tensors. weight_tensor_bi_convlstm_old = [] for j in range(2): # bidirectional for i in range(4): weight_tensor_bi_convlstm_old.append(np.zeros( (3, 3, 15, 10))) # kernel weight_tensor_bi_convlstm_old.append(np.zeros( (3, 3, 10, 10))) # recurrent kernel weight_tensor_bi_convlstm_old.append(np.zeros((10, ))) # bias bi_convlstm_layer = model.layers[2] weight_tensor_bi_convlstm_new = preprocess_weights_for_loading( bi_convlstm_layer, weight_tensor_bi_convlstm_old, original_keras_version='1') symbolic_weights = bi_convlstm_layer.weights assert (len(symbolic_weights) == len(weight_tensor_bi_convlstm_new)) weight_value_tuples += zip(symbolic_weights, weight_tensor_bi_convlstm_new) K.batch_set_value(weight_value_tuples) assert np.all( K.eval(model.layers[1].weights[0]) == weight_tensor_td_conv_new[0]) assert np.all( K.eval(model.layers[1].weights[1]) == weight_tensor_td_conv_new[1]) assert np.all( K.eval(model.layers[2].weights[0]) == weight_tensor_bi_convlstm_new[0]) assert np.all( K.eval(model.layers[2].weights[1]) == weight_tensor_bi_convlstm_new[1]) assert np.all( K.eval(model.layers[2].weights[2]) == weight_tensor_bi_convlstm_new[2]) assert np.all( K.eval(model.layers[2].weights[3]) == weight_tensor_bi_convlstm_new[3]) assert np.all( K.eval(model.layers[2].weights[4]) == weight_tensor_bi_convlstm_new[4]) assert np.all( K.eval(model.layers[2].weights[5]) == weight_tensor_bi_convlstm_new[5])
def on_epoch_begin(self, epoch, logs={}): optimizer = self.model.optimizer print("lr:", epoch, k.eval(optimizer.lr), k.eval(optimizer.decay), k.eval(optimizer.iterations))
metrics=['accuracy']) model.summary() index_array = np.arange(x_train.shape[0]) batch_num = np.int(np.ceil(x_train.shape[0] / batch_size)) for i in np.arange(user_epochs): np.random.shuffle(index_array) for j in np.arange(batch_num): x_batch = x_train[index_array[(j % batch_num) * batch_size:min( (j % batch_num + 1) * batch_size, x_train.shape[0])], :] y_batch = y_train[index_array[(j % batch_num) * batch_size:min( (j % batch_num + 1) * batch_size, x_train.shape[0])], :] model.train_on_batch(x_batch, y_batch) if (i + 1) % 150 == 0: #decay the learning rate by 0.1 K.set_value(model.optimizer.lr, K.eval(model.optimizer.lr * 0.1)) print("Learning rate: {}".format(K.eval(model.optimizer.lr))) if (i + 1) % 100 == 0: print("Epochs: {}".format(i)) scores_test = model.evaluate(x_test, y_test, verbose=0) print('Test loss:', scores_test[0]) print('Test accuracy:', scores_test[1]) scores_train = model.evaluate(x_train, y_train, verbose=0) print('Train loss:', scores_train[0]) print('Train accuracy:', scores_train[1]) ##save the model if save_model: weights = model.get_weights() if not os.path.exists(result_folder): os.makedirs(result_folder)
def seg_metrics(y_true, y_pred, metric_name, metric_type='standard', drop_last=True, mean_per_class=False, verbose=False): """ Compute mean metrics of two segmentation masks, via Keras. IoU(A,B) = |A & B| / (| A U B|) Dice(A,B) = 2*|A & B| / (|A| + |B|) Args: y_true: true masks, one-hot encoded. y_pred: predicted masks, either softmax outputs, or one-hot encoded. metric_name: metric to be computed, either 'iou' or 'dice'. metric_type: one of 'standard' (default), 'soft', 'naive'. In the standard version, y_pred is one-hot encoded and the mean is taken only over classes that are present (in y_true or y_pred). The 'soft' version of the metrics are computed without one-hot encoding y_pred. The 'naive' version return mean metrics where absent classes contribute to the class mean as 1.0 (instead of being dropped from the mean). drop_last = True: boolean flag to drop last class (usually reserved for background class in semantic segmentation) mean_per_class = False: return mean along batch axis for each class. verbose = False: print intermediate results such as intersection, union (as number of pixels). Returns: IoU/Dice of y_true and y_pred, as a float, unless mean_per_class == True in which case it returns the per-class metric, averaged over the batch. Inputs are B*W*H*N tensors, with B = batch size, W = width, H = height, N = number of classes """ flag_soft = (metric_type == 'soft') flag_naive_mean = (metric_type == 'naive') # always assume one or more classes num_classes = K.shape(y_true)[-1] if not flag_soft: # get one-hot encoded masks from y_pred (true masks should already be one-hot) y_pred = K.one_hot(K.argmax(y_pred), num_classes) y_true = K.one_hot(K.argmax(y_true), num_classes) # if already one-hot, could have skipped above command # keras uses float32 instead of float64, would give error down (but numpy arrays or keras.to_categorical gives float64) y_true = K.cast(y_true, 'float32') y_pred = K.cast(y_pred, 'float32') # intersection and union shapes are batch_size * n_classes (values = area in pixels) axes = (1, 2) # W,H axes of each image intersection = K.sum(K.abs(y_true * y_pred), axis=axes) mask_sum = K.sum(K.abs(y_true), axis=axes) + K.sum(K.abs(y_pred), axis=axes) union = mask_sum - intersection # or, np.logical_or(y_pred, y_true) for one-hot smooth = .001 iou = (intersection + smooth) / (union + smooth) dice = 2 * (intersection + smooth) / (mask_sum + smooth) metric = {'iou': iou, 'dice': dice}[metric_name] # define mask to be 0 when no pixels are present in either y_true or y_pred, 1 otherwise mask = K.cast(K.not_equal(union, 0), 'float32') if drop_last: metric = metric[:, :-1] mask = mask[:, :-1] if verbose: print('intersection, union') print(K.eval(intersection), K.eval(union)) print(K.eval(intersection / union)) # return mean metrics: remaining axes are (batch, classes) if flag_naive_mean: return K.mean(metric) # take mean only over non-absent classes class_count = K.sum(mask, axis=0) non_zero = tf.greater(class_count, 0) non_zero_sum = tf.boolean_mask(K.sum(metric * mask, axis=0), non_zero) non_zero_count = tf.boolean_mask(class_count, non_zero) if verbose: print( 'Counts of inputs with class present, metrics for non-absent classes' ) print(K.eval(class_count), K.eval(non_zero_sum / non_zero_count)) return K.mean(non_zero_sum / non_zero_count)
def lr_poly_decay(model, base_lr, curr_iter, max_iter, power=0.5): lrate = base_lr * (1.0 - (curr_iter / float(max_iter)))**power K.set_value(model.optimizer.lr, lrate) return K.eval(model.optimizer.lr)
img_array= img_to_array(imgResized).reshape(1,256,256,1)/255.0 print(img_array) out=model.predict(img_array) print(out.shape) out=out.reshape(256,256,1) print('min='+str(np.min(out))) print('max='+str(np.max(out))) print('mean='+str(np.mean(out))) label_array = img_to_array(labelResized)/255.0 print(label_array) y_true = K.variable(label_array) y_pred = K.variable(out) error = K.eval(binary_crossentropy(y_true,y_pred)) plt.figure() plt.hist(out.ravel(),256,[0,255]) plt.title('Histogram for output picture') plt.figure() plt.imshow(array_to_img(out),cmap="gray") print(error) print(error.shape) print(np.min(error)) print(np.max(error)) print(np.mean(error)) outResized = array_to_img(out).resize((512, 512), Image.BICUBIC) #save_img('predict.png',outResized)
def test_interpolate_multiply_elementwise_depthwise(): interpolation = interpolate(K.ones((8, 8, 16)), K.zeros((8, 8, 16)), K.expand_dims(K.eye(8))) assert (K.eval(interpolation) == np.repeat(np.eye(8)[..., np.newaxis], 16, axis=-1)).all()
def on_epoch_end(self, epoch, logs=None): learning_rates.append(K.eval(self.model.optimizer.lr)) print(K.eval(self.model.optimizer.lr))
def on_epoch_end(self, epoch, logs=None): print("Learning rate: ",K.eval(self.model.optimizer.lr))
def on_epoch_end(self, epoch, logs={}): optimizer = self.model.optimizer lr = K.eval(optimizer.lr * (1. / (1. + optimizer.decay * optimizer.iterations))) print str('\nLR: {:.6f}\n').format(float(lr))
x_train_noisy = K.cast_to_floatx(x_train_noisy) x_train = K.cast_to_floatx(x_train) # ------------------------------------------------------------------------------ #TRAIN vae.fit(x_train_noisy, x_train, shuffle=True, batch_size=batch_size, nb_epoch=num_epochs, verbose=2, validation_data=(x_test_noisy, x_test)) print('--------learning rate : ', K.eval(vae.optimizer.lr)) # ---------------------------------------------------------------------------- x_train_encoded = encoder.predict(x_train) x_train_decoded = decoder.predict(x_train_encoded) x_test_encoded = encoder.predict(x_test) x_test_decoded = decoder.predict(x_test_encoded) np.save(DataDir + 'encoded_xtrain_' + str(totalFiles) + '.npy', x_train_encoded) np.save(DataDir + 'encoded_xtest_' + str(totalFiles) + '.npy', x_test_encoded) # np.save(DataDir+'para5_'+str(totalFiles)+'.npy', y_train) # -------------------- Save model/weights --------------------------
def schedule(epoch): """ Update the learning rate of the two optimisers. """ if 0 < damp and damp < 1: K_.set_value(adv_optim.lr, damp * K_.get_value(adv_optim.lr)) pass return float(K_.eval(adv_optim.lr))
if __name__ == "__main__": print(pairwise_interaction_mat().summary()) a = [[1, 1, 0, 0], [1, 1, 0, 0], [0, 0, 1, 1], [0, 0, 1, 1]] b = [[0.9, 0.1, 0.2, 0.2], [0.1, 0.9, 0.2, 0.2], [0.2, 0.2, 0.9, 0.8], [0.2, 0.2, 0.8, 0.9]] a = np.array(a).astype(np.float32) b = np.array(b).astype(np.float32) a = np.expand_dims(a, axis=0) b = np.expand_dims(b, axis=0) y_true = kb.variable(a) y_pred = kb.variable(b) print("y_true shape is: ", kb.eval(tf.shape(y_true))) print(kb.expand_dims(tf.shape(y_true)[0], axis=0)) diag = tf.eye(num_rows=tf.shape(y_true)[2], batch_shape=kb.expand_dims(tf.shape(y_true)[0], axis=0)) print('diag shape is: ', kb.eval(tf.shape(diag))) in_frame_row = kb.max(y_true, axis=1, keepdims=True) print(np.squeeze(kb.eval(in_frame_row))) in_frame_col = kb.max(y_true, axis=2, keepdims=True) print(np.squeeze(kb.eval(in_frame_col))) mask = kb.batch_dot(in_frame_col, in_frame_row, axes=(2, 1)) print(np.squeeze(kb.eval(mask)))
def test(self, batch, use_ctc=False, return_result=False): ''' 要求self.base_model 接受的输入必须是xs(根据模型不同维度可以任意),输出必须是ys_pred(未经过argmax的,[batch,timestamp,vector]) 当然,你也可以自行实现自己的try_predict方法 :param batch: 要求格式必须是 [xs, ys, feature_len, label_len], placehold 其中,由于不同模型的要求不同,ys同时支持catagries维(batch,timestamp,num_catagries)和index向量(batch,indexs),会根据维度自动进行判断 result 的输出维度必须符合 [batch,timestamp,num_categries] :param return_result: 是在控制台输出还是返回结果,与model_summary.py对应 :return: ''' [xs, ys, feature_len, label_len], placehold = batch result = self.base_model.predict(xs) # assert use_ctc and result.ndim == 3,"when use_ctc is True, result.ndim must be 3. please check params." if use_ctc: argres = self.ctc_decoder.ctc_decode(result, feature_len) else: argres = K.argmax(result) argres = K.eval(argres) if ys.ndim == 3: y_true = K.argmax(ys) y_true = K.eval(y_true) else: y_true = ys pylist_pred = self.pymap.batch_vector2pylist(argres, return_word_list=True, return_list=True) pylist_true = self.pymap.batch_vector2pylist(y_true, return_word_list=True, return_list=True) print("===================") all_count = 0 all_norm = 0 ignore_num = 0 i = 0 err_dict = {} for pred, true, llen in zip(pylist_pred, pylist_true, label_len.squeeze()): true = true[:llen] count, count_norm = self.evaluate.compare_sent(pred, true) if count == 0: self.pysets.update(pred) if len(pred) == len(true): for a, b in zip(true, true): if a != b: errlist = err_dict.setdefault(a, []) errlist.append(b) print(a, b) else: # print(" ".join(pred)) # print(" ".join(true)) ignore_num += 1 all_count += count all_norm += count_norm i += 1 if not return_result: print(" ".join(pred)) print(" ".join(true)) print("-------------------") print( f"[test*] compare result:{count} differences. After norm:{count_norm}. " ) print("===================") if not return_result: print( f"[test*] all differences:{all_count}.Whole norm:{all_norm/i}") print(f"[info*] pinyin can recognition:{len(self.pysets)}") print(err_dict, ignore_num) return { "all_count": all_count, "all_norm": all_norm / i, "err_pylist": err_dict, "ignore": ignore_num, }
[-0.06273715 - 0.00214314j], [-0.09198901 + 0.04865882j], [0.03957726 - 0.03430344j], [0.01923412 + 0.j], [-0.03957726 - 0.03430344j], [-0.09198901 - 0.04865882j], [0.06273715 - 0.00214314j], [0.00772077 + 0.03679032j]] x = K.constant(x, dtype='float32') y = K.constant(y, dtype='complex64') #w = _setup_wigner(2, nl=2, weighted=not False) #w = K.eval(w) #w = K.constant(w, dtype='complex64') #output = so3_rfft(x) output = so3_rotation(x, alpha, beta, gamma) output = K.eval(output) print(output) #output = so3_rfft(x) '''device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") #kernel = cuda_kernel(16, 16, 10, False) x = torch.randn(20, 6, 6, 6, 2, device=device) x = torch.fft(x, 2) w = _setup_wigner(3, nl=3, weighted=not False, device_type=x.device.type, device_index=x.device.index) print(x.size()) print(w.size()) print(w) #output = x.new_empty((10, 10, 2))