我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用keras.backend._BACKEND。
def build_network(deepest=False): dropout = [0., 0.1, 0.2, 0.3, 0.4] conv = [(64, 3, 3), (128, 3, 3), (256, 3, 3), (512, 3, 3), (512, 2, 2)] input= Input(shape=(3, 32, 32) if K._BACKEND == 'theano' else (32, 32,3)) output = fractal_net( c=3, b=5, conv=conv, drop_path=0.15, dropout=dropout, deepest=deepest)(input) output = Flatten()(output) output = Dense(NB_CLASSES, init='he_normal')(output) output = Activation('softmax')(output) model = Model(input=input, output=output) #optimizer = SGD(lr=LEARN_START, momentum=MOMENTUM) #optimizer = SGD(lr=LEARN_START, momentum=MOMENTUM, nesterov=True) optimizer = Adam() #optimizer = Nadam() model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy']) plot(model, to_file='model.png', show_shapes=True) return model
def add_vgg_to_layer(input_layer, trainable=False, pool_mode='max', weights_path='vgg16_weights.h5', last_layer=None): layers = get_layers(pool_mode=pool_mode, last_layer=last_layer, trainable=trainable, weights_path=weights_path) # load the weights of the VGG16 networks assert os.path.exists(weights_path), 'Model weights not found (see "--vgg-weights" parameter).' f = h5py.File(weights_path) last_layer = input_layer for k, layer in zip(range(f.attrs['nb_layers']), layers): g = f['layer_{}'.format(k)] weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])] if isinstance(layer, Convolution2D) and K._BACKEND == 'theano': weights[0] = np.array(weights[0])[:, :, ::-1, ::-1] last_layer = layer(last_layer) layer.trainable = trainable layer.set_weights(weights) f.close() return last_layer
def get_callbacks(config_data, appendix=''): ret_callbacks = [] model_stored = False callbacks = config_data['callbacks'] if K._BACKEND == 'tensorflow': tensor_board = TensorBoard(log_dir=os.path.join('logging', config_data['tb_log_dir']), histogram_freq=10) ret_callbacks.append(tensor_board) for callback in callbacks: if callback['name'] == 'early_stopping': ret_callbacks.append(EarlyStopping(monitor=callback['monitor'], patience=callback['patience'], verbose=callback['verbose'], mode=callback['mode'])) elif callback['name'] == 'model_checkpoit': model_stored = True path = config_data['output_path'] basename = config_data['output_basename'] base_path = os.path.join(path, basename) opath = os.path.join(base_path, 'best_model{}.h5'.format(appendix)) save_best = bool(callback['save_best_only']) ret_callbacks.append(ModelCheckpoint(filepath=opath, verbose=callback['verbose'], save_best_only=save_best, monitor=callback['monitor'], mode=callback['mode'])) return ret_callbacks, model_stored
def convert_weights_theano2tensorflow(model_builder, theano_weights_file, tensorflow_weights_file): """ Theano and Tensorflow implement convolutional layers differently. This functions transforms pretrained weights for a Theano-based CNN to Tensorflow format. check out https://github.com/fchollet/keras/wiki/Converting-convolution-kernels-from-Theano-to-TensorFlow-and-vice-versa """ assert K._BACKEND == 'tensorflow' model = model_builder(theano_weights_file) ops = [] for layer in model.layers: if layer.__class__.__name__ in ['Convolution1D', 'Convolution2D', 'Convolution3D', 'AtrousConvolution2D']: original_w = K.get_value(layer.W) converted_w = convert_kernel(original_w) ops.append(tf.assign(layer.W, converted_w).op) K.get_session().run(ops) model.save_weights(tensorflow_weights_file)
def call(self, x, mask=None): X = x half_n = self.n // 2 input_sqr = K.square(X) if K._BACKEND == 'theano': b, ch, r, c = X.shape extra_channels = T.alloc(0., b, ch + 2*half_n, r, c) input_sqr = T.set_subtensor( extra_channels[:, half_n:half_n+ch, :, :], input_sqr) elif K._BACKEND == 'tensorflow': b, ch, r, c = K.int_shape(X) up_dims = tf.pack([tf.shape(X)[0], half_n, r, c]) up = tf.fill(up_dims, 0.0) middle = input_sqr down_dims = tf.pack([tf.shape(X)[0], half_n, r, c]) down = tf.fill(down_dims, 0.0) input_sqr = K.concatenate([up, middle, down], axis=1) scale = self.k norm_alpha = self.alpha / self.n for i in range(self.n): scale += norm_alpha * input_sqr[:, i:i+ch, :, :] scale = scale ** self.beta result = X / scale return result
def call(self, x, mask=None): if K._BACKEND == 'theano': output = self.conv2d_transpose_th(x, self.W, strides=self.subsample, border_mode=self.border_mode) else: output = self.conv2d_transpose_tf(x, self.W, strides=self.subsample, border_mode=self.border_mode) if self.bias: if self.dim_ordering == 'th': output += K.reshape(self.b, (1, self.nb_filter, 1, 1)) elif self.dim_ordering == 'tf': output += K.reshape(self.b, (1, 1, 1, self.nb_filter)) else: raise Exception('Invalid dim_ordering: ' + self.dim_ordering) output = self.activation(output) return output
def grams(X): dim_ordering = K.image_dim_ordering() if dim_ordering == 'tf': X = K.permute_dimensions(X, (0, 3, 1, 2)) (samples, c, h, w) = get_shape(X) X_reshaped = K.reshape(X, (-1, c, h * w)) X_T = K.permute_dimensions(X_reshaped, (0, 2, 1)) if K._BACKEND == 'theano': X_gram = T.batched_dot(X_reshaped, X_T) else: X_gram = tf.batch_matmul(X_reshaped, X_T) X_gram /= c * h * w return X_gram
def export_model(model, absolute_model_dir, best_weights=None, saver=None, global_step=None): if not os.path.isdir(absolute_model_dir): os.makedirs(absolute_model_dir) model.save_weights(absolute_model_dir + "/last_weights.hdf5", overwrite=True) if K._BACKEND == 'tensorflow' and saver != None: sess = K.get_session() saver.save(sess, absolute_model_dir + '/tf-last_weights', global_step=global_step) if best_weights != None: model.set_weights(best_weights) model.save_weights(absolute_model_dir + "/best_weights.hdf5", overwrite=True) if K._BACKEND == 'tensorflow' and saver != None: saver.save(sess, absolute_model_dir + '/tf-best_weights', global_step=global_step) # Graph json = model.to_json() open(absolute_model_dir + "/archi.json", 'w').write(json) if K._BACKEND == 'tensorflow' and saver != None and global_step == None: graph_def = sess.graph.as_graph_def() tf.train.write_graph(graph_def, absolute_model_dir, 'tf-model_graph') freeze_graph(model, absolute_model_dir, best_weights)
def build(self, input_shape): self.input_spec = [InputSpec(ndim=3)] if K._BACKEND == 'tensorflow': if not input_shape[1]: raise Exception('When using TensorFlow, you should define ' 'explicitly the number of timesteps of ' 'your sequences.\n' 'If your first layer is an Embedding, ' 'make sure to pass it an "input_length" ' 'argument. Otherwise, make sure ' 'the first layer has ' 'an "input_shape" or "batch_input_shape" ' 'argument, including the time axis.') if not self.layer.built: self.layer.build(input_shape) self.layer.built = True super(ProbabilityTensor, self).build()
def sample_noise_layer_input(self): if self._sample_noise_layer_input is None: if self.data is None: raise Exception("data attribute not initialized") if K._BACKEND == 'tensorflow': import tensorflow as tf c_input = tf.constant(self.data) else: c_input = K.variable(self.data) input_ndxs = K_n_choose_k(len(self.data), self.miN) noise_layer_input = K.gather(c_input, input_ndxs) for layerndx, layer in enumerate(self.model_layers): noise_layer_input = layer.call(noise_layer_input) self._sample_noise_layer_input = noise_layer_input return self._sample_noise_layer_input
def build(self, input_shape): assert len(input_shape) >= 3 self.input_spec = [InputSpec(shape=input_shape)] if K._BACKEND == 'tensorflow': if not input_shape[1]: raise Exception('When using TensorFlow, you should define ' 'explicitly the number of timesteps of ' 'your sequences.\n' 'If your first layer is an Embedding, ' 'make sure to pass it an "input_length" ' 'argument. Otherwise, make sure ' 'the first layer has ' 'an "input_shape" or "batch_input_shape" ' 'argument, including the time axis.') child_input_shape = (input_shape[0],) + input_shape[self.first_n:] if not self.layer.built: self.layer.build(child_input_shape) self.layer.built = True super(NTimeDistributed, self).build()
def rand_one_in_array(count, seed=None): if seed is None: seed = np.random.randint(1, 10e6) if K._BACKEND == 'theano': pvals = np.array([[1. / count for _ in range(count)]], dtype='float32') return theano_multinomial(n=1, pvals=pvals, seed=seed)[0] elif K._BACKEND == 'tensorflow': return tensorflow_categorical(count=count, seed=seed) else: raise Exception('Backend: {} not implemented'.format(K._BACKEND))
def fractal_conv(filter, nb_row, nb_col, dropout=None): def f(prev): conv = prev conv = Convolution2D(filter, nb_row=nb_col, nb_col=nb_col, init='he_normal', border_mode='same')(conv) if dropout: conv = Dropout(dropout)(conv) conv = BatchNormalization(mode=0, axis=1 if K._BACKEND == 'theano' else -1)(conv) conv = Activation('relu')(conv) return conv return f # XXX_ It's not clear when to apply Dropout, the paper cited # (arXiv:1511.07289) uses it in the last layer of each stack but in # the code gustav published it is in each convolution block so I'm # copying it.
def call(self, x, mask=None): # input shape: (nb_samples, time (padded with zeros), input_dim) # note that the .build() method of subclasses MUST define # self.input_spec with a complete input shape. input_shape = self.input_spec[0].shape if K._BACKEND == 'tensorflow': if not input_shape[1]: raise Exception('When using TensorFlow, you should define ' 'explicitly the number of timesteps of ' 'your sequences.\n' 'If your first layer is an Embedding, ' 'make sure to pass it an "input_length" ' 'argument. Otherwise, make sure ' 'the first layer has ' 'an "input_shape" or "batch_input_shape" ' 'argument, including the time axis. ' 'Found input shape at layer ' + self.name + ': ' + str(input_shape)) if self.layer.stateful: initial_states = self.layer.states else: initial_states = self.layer.get_initial_states(x) constants = self.get_constants(x) preprocessed_input = self.layer.preprocess_input(x) last_output, outputs, states = K.rnn(self.step, preprocessed_input, initial_states, go_backwards=self.layer.go_backwards, mask=mask, constants=constants, unroll=self.layer.unroll, input_length=input_shape[1]) if self.layer.stateful: self.updates = [] for i in range(len(states)): self.updates.append((self.layer.states[i], states[i])) if self.layer.return_sequences: return outputs else: return last_output
def call(self, x, mask=None): # input shape: (nb_samples, time (padded with zeros), input_dim) # note that the .build() method of subclasses MUST define # self.input_spec with a complete input shape. input_shape = self.input_spec[0].shape if K._BACKEND == 'tensorflow': if not input_shape[1]: raise Exception('When using TensorFlow, you should define ' 'explicitly the number of timesteps of ' 'your sequences.\n' 'If your first layer is an Embedding, ' 'make sure to pass it an "input_length" ' 'argument. Otherwise, make sure ' 'the first layer has ' 'an "input_shape" or "batch_input_shape" ' 'argument, including the time axis. ' 'Found input shape at layer ' + self.name + ': ' + str(input_shape)) if self.layer.stateful: initial_states = self.layer.states else: initial_states = self.layer.get_initial_states(x) constants = self.get_constants(x) preprocessed_input = self.layer.preprocess_input(x) last_output, outputs, states = K.rnn(self.step, preprocessed_input, initial_states, go_backwards=self.layer.go_backwards, mask=mask, constants=constants, unroll=self.layer.unroll, input_length=input_shape[1]) if self.layer.stateful: self.updates = [] for i in range(len(states)): self.updates.append((self.layer.states[i], states[i])) if self.layer.return_sequences: return outputs else:
def angle_loss(y_true, y_pred): if K._BACKEND == 'theano': import theano arctan2 = theano.tensor.arctan2 elif K._BACKEND == 'tensorflow': import tensorflow as tf arctan2 = tf.atan2
def __init__(self, log_dir='./logs', histogram_freq=0, write_graph=True, write_images=False): super(TensorBoard, self).__init__() if K._BACKEND != 'tensorflow': raise Exception('TensorBoard callback only works ' 'with the TensorFlow backend.') self.log_dir = log_dir self.histogram_freq = histogram_freq self.merged = None self.write_graph = write_graph self.write_images = write_images
def check_dtype(var, dtype): if K._BACKEND == 'theano': assert var.dtype == dtype else: assert var.dtype.name == '%s_ref' % dtype
def get_output(self, train=False): # input shape: (nb_samples, time (padded with zeros), input_dim) X = self.get_input(train) if K._BACKEND == 'tensorflow': if not self.input_shape[1]: raise Exception('When using TensorFlow, you should define ' + 'explicitly the number of timesteps of ' + 'your sequences. Make sure the first layer ' + 'has a "batch_input_shape" argument ' + 'including the samples axis.') mask = self.get_output_mask(train) if self.stateful or self.state_input or len(self.state_outputs) > 0: initial_states = self.states else: initial_states = self.get_initial_states(X) last_output, outputs, states = K.rnn(self.step, X, initial_states, go_backwards=self.go_backwards, masking=mask) n = len(states) if self.stateful and not self.state_input: self.updates = [] self.updates = [] for i in range(n): self.updates.append((self.states[i], states[i])) for o in self.state_outputs: o.updates = [] for i in range(n): o.updates.append((o.states[i], states[i])) if self.return_sequences: return outputs else: return last_output
def __init__(self, log_dir='./logs', histogram_freq=0, write_graph=True, write_images=False): super(TensorBoard, self).__init__() if K._BACKEND != 'tensorflow': raise RuntimeError('TensorBoard callback only works ' 'with the TensorFlow backend.') self.log_dir = log_dir self.histogram_freq = histogram_freq self.merged = None self.write_graph = write_graph self.write_images = write_images
def train(self, x_train, x_test, epochs, batch_size, log_dir='/tmp/autoencoder', stop_early=True): callbacks = [] if backend._BACKEND == 'tensorflow': callbacks.append(TensorBoard(log_dir=log_dir)) if stop_early: callbacks.append(EarlyStopping(monitor='val_loss', patience=2, verbose=1, mode='auto')) self.autoencoder.fit(x_train, x_train, nb_epoch=epochs, batch_size=batch_size, shuffle=True, validation_data=(x_test, x_test), callbacks=callbacks)
def call(self, x, mask=None): constants = self.get_constants(x) assert K.ndim(x) == 5 if K._BACKEND == 'tensorflow': if not self.input_shape[1]: raise Exception('When using TensorFlow, you should define ' + 'explicitely the number of timesteps of ' + 'your sequences. Make sure the first layer ' + 'has a "batch_input_shape" argument ' + 'including the samples axis.') if self.stateful: initial_states = self.states else: initial_states = self.get_initial_states(x) last_output, outputs, states = K.rnn(self.step, x, initial_states, go_backwards=self.go_backwards, mask=mask, constants=constants) if self.stateful: self.updates = [] for i in range(len(states)): self.updates.append((self.states[i], states[i])) if self.return_sequences: return outputs else: return last_output
def __init__(self, n_shapelets, X=None, **kwargs): self.n_shapelets = n_shapelets if X is None or K._BACKEND != "tensorflow": self.initializer = "uniform" else: self.initializer = KMeansShapeletInitializer(X) super(LocalSquaredDistanceLayer, self).__init__(**kwargs) self.input_spec = InputSpec(ndim=3)
def check_dtype(var, dtype): if K._BACKEND == 'theano': assert var.dtype == dtype elif K._BACKEND == 'mxnet': assert var.dtype == dtype else: assert var.dtype.name == '%s_ref' % dtype
def weights_file(): assert K._BACKEND in ['theano', 'tensorflow'] if K._BACKEND == 'tensorflow': return os.path.join(current_directory(), 'deep_drive_weights_tensorflow.h5') else: return os.path.join(current_directory(), 'deep_drive_weights_theano.h5')
def weights_file(self): assert K._BACKEND in ['theano', 'tensorflow'] if K._BACKEND == 'tensorflow': return os.path.join(self.current_directory(), 'deep_drive_weights_tensorflow.h5') else: return os.path.join(self.current_directory(), 'deep_drive_weights_theano.h5')
def __init__(self, nb_filter, nb_row, nb_col, rate=2, init='glorot_uniform', activation='linear', weights=None, border_mode='valid', dim_ordering=K.image_dim_ordering(), W_regularizer=None, b_regularizer=None, activity_regularizer=None, W_constraint=None, b_constraint=None, bias=True, **kwargs): if K._BACKEND != 'tensorflow': raise Exception('TensorBoard callback only works ' 'with the TensorFlow backend.') if border_mode not in {'valid', 'same'}: raise Exception('Invalid border mode for Convolution2D:', border_mode) self.nb_filter = nb_filter self.nb_row = nb_row self.nb_col = nb_col self.rate = rate self.init = initializations.get(init, dim_ordering=dim_ordering) self.activation = activations.get(activation) assert border_mode in {'valid', 'same'}, 'border_mode must be in {valid, same}' self.border_mode = border_mode assert dim_ordering in {'tf', 'th'}, 'dim_ordering must be in {tf, th}' self.dim_ordering = dim_ordering self.W_regularizer = regularizers.get(W_regularizer) self.b_regularizer = regularizers.get(b_regularizer) self.activity_regularizer = regularizers.get(activity_regularizer) self.W_constraint = constraints.get(W_constraint) self.b_constraint = constraints.get(b_constraint) self.bias = bias self.input_spec = [InputSpec(ndim=4)] self.initial_weights = weights super(ATrousConvolution2D, self).__init__(**kwargs)
def test_import_model(self): data_model_folder = dir + "/../fixture/model_conv2d_relu" should_convert = K._BACKEND == "theano" model = import_model(data_model_folder, should_convert=should_convert) input_img = np.array([load_image(dir + '/../fixture/blue.png', size=None, preprocess_type='st', verbose=False)]) output = model.predict([input_img]).astype('int32') true_output = np.array([ [ [ [0, 0, 0], [0, 0, 0], [0, 0, 0] ], [ [131, 116, 153], [153, 281, 364], [103, 254, 318] ], [ [52, 1, 0], [0, 0, 0], [0, 0, 0] ] ] ]) self.assertEqual(len(model.layers),3) self.assertEqual(True, (output==true_output).all())
def test_convolution_transpose_th(self): if K._BACKEND != 'tensorflow': return True K.set_image_dim_ordering('th') border_mode = 'valid' batch = 1 height = 10 width = 10 channels_in = 1 channels_out = 2 kernel_size = 3 rate = 2 input_shape = (channels_in, height, width) input = Input(shape=input_shape, dtype=K.floatx()) conv_layer = ATrousConvolution2D(channels_out, kernel_size, kernel_size, rate, dim_ordering=K.image_dim_ordering(), init='one', border_mode=border_mode, activation='linear') output = conv_layer(input) model = Model(input=[input], output=[output]) model.compile(loss='mean_squared_error', optimizer='sgd') x = np.ones((batch,) + input_shape).astype(K.floatx()) kernel = conv_layer.W output_model = model.predict(x) if K._BACKEND == 'tensorflow': x = tf.transpose(x, (0, 2, 3, 1)) kernel = tf.transpose(kernel, (2, 3, 1, 0)) y = tf.nn.atrous_conv2d(x, kernel, rate, padding=border_mode.upper()) y = tf.transpose(y, (0, 3, 1, 2)) output = y.eval(session=K.get_session()) self.assertEqual(output_model.shape, (1, 2, 6, 6)) self.assertEqual(output.shape, (1, 2, 6, 6)) self.assertEqual(True, (output==output_model).all())
def test_convolution_transpose_tf(self): if K._BACKEND != 'tensorflow': return True K.set_image_dim_ordering('tf') border_mode = 'valid' batch = 1 height = 10 width = 10 channels_in = 1 channels_out = 2 kernel_size = 3 # effective kernel size: kernel_size + (kernel_size - 1) * (rate - 1) rate = 2 input_shape = (height, width, channels_in) input = Input(shape=input_shape, dtype=K.floatx()) conv_layer = ATrousConvolution2D(channels_out, kernel_size, kernel_size, rate, dim_ordering=K.image_dim_ordering(), init='one', border_mode=border_mode, activation='linear') output = conv_layer(input) model = Model(input=[input], output=[output]) model.compile(loss='mean_squared_error', optimizer='sgd') x = np.ones((batch,) + input_shape).astype(K.floatx()) kernel = conv_layer.W output_model = model.predict(x) if K._BACKEND == 'tensorflow': y = tf.nn.atrous_conv2d(x, kernel, rate, padding=border_mode.upper()) output = y.eval(session=K.get_session()) self.assertEqual(output_model.shape, (1, 6, 6, 2)) self.assertEqual(output.shape, (1, 6, 6, 2)) self.assertEqual(True, (output==output_model).all())
def test_convolution_transpose_tf_sameborder(self): if K._BACKEND != 'tensorflow': return True K.set_image_dim_ordering('tf') border_mode = 'same' batch = 1 height = 10 width = 10 channels_in = 1 channels_out = 2 kernel_size = 3 # effective kernel size: kernel_size + (kernel_size - 1) * (rate - 1) rate = 2 input_shape = (height, width, channels_in) input = Input(shape=input_shape, dtype=K.floatx()) conv_layer = ATrousConvolution2D(channels_out, kernel_size, kernel_size, rate, dim_ordering=K.image_dim_ordering(), init='one', border_mode=border_mode, activation='linear') output = conv_layer(input) model = Model(input=[input], output=[output]) model.compile(loss='mean_squared_error', optimizer='sgd') x = np.ones((batch,) + input_shape).astype(K.floatx()) kernel = conv_layer.W output_model = model.predict(x) if K._BACKEND == 'tensorflow': y = tf.nn.atrous_conv2d(x, kernel, rate, padding=border_mode.upper()) output = y.eval(session=K.get_session()) self.assertEqual(output_model.shape, (1, 10, 10, 2)) self.assertEqual(output.shape, (1, 10, 10, 2)) self.assertEqual(True, (output==output_model).all())
def copySeqWeights(model, weightsFullPath, outputFilename, offset=1, limit=-1): nbLayer = len(model.layers) f = h5py.File(weightsFullPath, "r") # print(f.name, f.keys()) # print(f['layer_1'].keys(), f['layer_1']['param_0']) nbLayers = f.attrs['nb_layers'] print("Number of layers in the original weight: ", nbLayers) print("Number of layers in the model: ", len(model.layers)) for k in range(nbLayers): if k + offset >= nbLayer or (limit > 0 and k >= limit): break g = f['layer_{}'.format(k)] weights = [g.get('param_{}'.format(p))[()] for p in range(g.attrs['nb_params'])] print(model.layers[k+offset].name) if len(weights): if K._BACKEND == 'theano': # I have to do it for theano because theano flips the convolutional kernel # and i don't have access to the API of the conv ops weights[0] = np.round(weights[0][:, :, ::-1, ::-1], 4) if K.image_dim_ordering() == 'tf': weights[0] = np.transpose(weights[0], [2, 3, 1, 0]) # print(weights[0].shape) model.layers[k+offset].set_weights(weights) f.close() model.save_weights(outputFilename, overwrite=True)
def import_model(absolute_model_dir, best=True, should_convert=False, custom_objects={}): archi_json = open(absolute_model_dir + '/archi.json').read() model = model_from_json(archi_json, custom_objects) if os.path.isfile(absolute_model_dir + '/best_weights.hdf5') and best: model.load_weights(absolute_model_dir + '/best_weights.hdf5') else: model.load_weights(absolute_model_dir + '/last_weights.hdf5') if should_convert == True: if K._BACKEND == 'tensorflow': ops = [] for layer in model.layers: if layer.__class__.__name__ in ['Convolution1D', 'Convolution2D']: original_w = K.get_value(layer.W) converted_w = convert_kernel(original_w) ops.append(tf.assign(layer.W, converted_w).op) K.get_session().run(ops) else: for layer in model.layers: if layer.__class__.__name__ in ['Convolution1D', 'Convolution2D']: original_w = K.get_value(layer.W) converted_w = convert_kernel(original_w) K.set_value(layer.W, converted_w) return model
def get_shape(x): if isinstance(x, (np.ndarray)): return x.shape elif K._BACKEND == 'theano': return K.shape(x) else: try: return K.int_shape(x) except Exception: return K.shape(x)
def __init__(self, model, chkp_dir, nb_step_chkp=100, max_to_keep=10, keep_checkpoint_every_n_hours=1): super(Callback, self).__init__() if K._BACKEND == 'tensorflow': import tensorflow as tf self.saver = tf.train.Saver(var_list=None, max_to_keep=max_to_keep, keep_checkpoint_every_n_hours=keep_checkpoint_every_n_hours) else: self.saver = None self.model = model self.archi = model.to_json() self.chkp_dir = chkp_dir if not os.path.isdir(self.chkp_dir): os.makedirs(self.chkp_dir) self.global_step = 0 self.nb_step_chkp = nb_step_chkp
def __init__(self, merge=True, batch_size=None, *args, **kwargs): self.merge = merge self.batch_size = batch_size if K._BACKEND != "theano": raise NotImplementedError("Check the unbroadcast in TensorFlow") super(MergeSequences, self).__init__(*args, **kwargs)
def arange(n, step=1): if K._BACKEND == 'theano': import theano.tensor as T return T.arange(0, n, step) elif K._BACKEND == 'tensorflow': import tensorflow as tf return tf.range(0, n, step)
def zeros(n): if K._BACKEND == 'theano': import theano.tensor as T return T.zeros(n) elif K._BACKEND == 'tensorflow': import tensorflow as tf return tf.zeros(n)
def set_img_format(): try: if K.backend() == 'theano': K.set_image_data_format('channels_first') else: K.set_image_data_format('channels_last') except AttributeError: if K._BACKEND == 'theano': K.set_image_dim_ordering('th') else: K.set_image_dim_ordering('tf')
def __init__(self, log_dir='./logs', histogram_freq=0, write_graph=True): super(TensorBoard, self).__init__() if K._BACKEND != 'tensorflow': raise Exception('TensorBoard callback only works ' 'with the TensorFlow backend.') self.log_dir = log_dir self.histogram_freq = histogram_freq self.merged = None self.write_graph = write_graph
def build(self, input_shape=None): '''Assumes that self.layer is already set. Should be called at the end of .build() in the children classes. ''' ndim = len(input_shape) assert ndim >= 3 self.input_spec = [InputSpec(ndim=str(ndim)+'+')] #if input_shape is not None: # self.last_two = input_shape[-2:] self._input_shape = input_shape #self.input_spec = [InputSpec(shape=input_shape)] if K._BACKEND == 'tensorflow': if not input_shape[1]: raise Exception('When using TensorFlow, you should define ' 'explicitly the number of timesteps of ' 'your sequences.\n' 'If your first layer is an Embedding, ' 'make sure to pass it an "input_length" ' 'argument. Otherwise, make sure ' 'the first layer has ' 'an "input_shape" or "batch_input_shape" ' 'argument, including the time axis.') #child_input_shape = (np.prod(input_shape[:-2]),) + input_shape[-2:] child_input_shape = (None,)+input_shape[-2:] if not self.layer.built: self.layer.build(child_input_shape) self.layer.built = True self.trainable_weights = getattr(self.layer, 'trainable_weights', []) self.non_trainable_weights = getattr(self.layer, 'non_trainable_weights', []) self.updates = getattr(self.layer, 'updates', []) self.regularizers = getattr(self.layer, 'regularizers', []) self.constraints = getattr(self.layer, 'constraints', {})
def build(self, input_shape=None): '''Assumes that self.layer is already set. Should be called at the end of .build() in the children classes. ''' assert len(input_shape) >= 3 self.input_spec = [InputSpec(shape=input_shape)] if K._BACKEND == 'tensorflow': if not input_shape[1]: raise Exception('When using TensorFlow, you should define ' 'explicitly the number of timesteps of ' 'your sequences.\n' 'If your first layer is an Embedding, ' 'make sure to pass it an "input_length" ' 'argument. Otherwise, make sure ' 'the first layer has ' 'an "input_shape" or "batch_input_shape" ' 'argument, including the time axis.') child_input_shape = (np.prod(input_shape[:-1]),) + input_shape[-1:] if not self.layer.built: self.layer.build(child_input_shape) self.layer.built = True self.trainable_weights = getattr(self.layer, 'trainable_weights', []) self.non_trainable_weights = getattr(self.layer, 'non_trainable_weights', []) self.updates = getattr(self.layer, 'updates', []) self.regularizers = getattr(self.layer, 'regularizers', []) self.constraints = getattr(self.layer, 'constraints', {})
def conv2d_transpose_tf(self, x, kernel, strides=(1, 1), border_mode='valid'): if border_mode == 'same': padding = 'SAME' elif border_mode == 'valid': padding = 'VALID' else: raise Exception('Invalid border mode: ' + str(border_mode)) strides = (1,) + strides + (1,) if K._BACKEND == 'theano': input_shape = K.shape(x) else: try: input_shape = K.int_shape(x) except Exception as e: input_shape = K.shape(x) output_shape = self.get_output_shape_for(input_shape) if _FLOATX == 'float64': # tf conv2d only supports float32 x = tf.cast(x, 'float32') kernel = tf.cast(kernel, 'float32') batch_size = tf.shape(x)[0] if self.dim_ordering == 'th': # TF uses the last dimension as channel dimension, # instead of the 2nd one. # TH input shape: (samples, input_depth, rows, cols) # TF input shape: (samples, rows, cols, input_depth) # TH kernel shape: (depth, input_depth, rows, cols) # TF kernel shape: (rows, cols, input_depth, depth) x = tf.transpose(x, (0, 2, 3, 1)) kernel = tf.transpose(kernel, (2, 3, 1, 0)) output_shape_tensor = tf.pack([batch_size, output_shape[2], output_shape[3], output_shape[1]]) else: output_shape_tensor = tf.pack([batch_size, output_shape[1], output_shape[2], output_shape[3]]) conv_t_out = tf.nn.conv2d_transpose(x, kernel, output_shape_tensor, strides, padding=padding) if self.dim_ordering == 'th': conv_t_out.set_shape((None, output_shape[2], output_shape[3], output_shape[1])) conv_t_out = tf.transpose(conv_t_out, (0, 3, 1, 2)) else: conv_t_out.set_shape((None, output_shape[1], output_shape[2], output_shape[3])) if _FLOATX == 'float64': conv_t_out = tf.cast(conv_t_out, 'float64') return conv_t_out
def test_convolution_transpose_th(self): border_mode = 'valid' K.set_image_dim_ordering('th') batch = 1 height = 2 width = 2 channels_in = 1 channels_out = 2 kernel_size = 3 strides = (1, 1) input_shape = (channels_in, height, width) input = Input(shape=input_shape, dtype=K.floatx()) conv_layer = ConvolutionTranspose2D(channels_out, kernel_size, kernel_size, dim_ordering=K.image_dim_ordering(), init='one', subsample=strides, border_mode=border_mode, activation='linear') output = conv_layer(input) model = Model(input=[input], output=[output]) model.compile(loss='mean_squared_error', optimizer='sgd') x = np.ones((batch,) + input_shape).astype(K.floatx()) kernel = conv_layer.W output_model = model.predict(x) if K._BACKEND == 'theano': output_shape = conv_layer.get_output_shape_for(K.shape(x)) y = T.nnet.abstract_conv.conv2d_grad_wrt_inputs(theano.shared(x), kernel, output_shape, filter_shape=None, border_mode=border_mode, subsample=strides, filter_flip=True) output = y.eval() else: sess = K.get_session() output_shape = conv_layer.get_output_shape_for(K.shape(x)) output_shape = tf.pack([1, output_shape[2], output_shape[3], output_shape[1]]) x = tf.transpose(x, (0, 2, 3, 1)) kernel = tf.transpose(kernel, (2, 3, 1, 0)) y = tf.nn.conv2d_transpose(x, kernel, output_shape, (1, ) + strides + (1, ), padding=border_mode.upper()) y = tf.transpose(y, (0, 3, 1, 2)) output = sess.run(y) self.assertEqual(output_model.shape, (1, 2, 4, 4)) self.assertEqual(output.shape, (1, 2, 4, 4)) self.assertEqual(True, (output==output_model).all()) # model.fit(x, x + 1, nb_epoch=1)
def test_convolution_transpose_tf(self): border_mode = 'valid' K.set_image_dim_ordering('tf') batch = 1 height = 2 width = 2 channels_in = 1 channels_out = 2 kernel_size = 3 strides = (1, 1) input_shape = (height, width, channels_in) input = Input(shape=input_shape, dtype=K.floatx()) conv_layer = ConvolutionTranspose2D(channels_out, kernel_size, kernel_size, dim_ordering=K.image_dim_ordering(), init='one', subsample=strides, border_mode=border_mode, activation='linear') output = conv_layer(input) model = Model(input=[input], output=[output]) model.compile(loss='mean_squared_error', optimizer='sgd') x = np.ones((batch,) + input_shape).astype(K.floatx()) kernel = conv_layer.W output_model = model.predict(x) if K._BACKEND == 'theano': output_shape = conv_layer.get_output_shape_for(K.shape(x)) output_shape = (1, output_shape[3], output_shape[1], output_shape[2]) x = np.transpose(x, (0, 3, 1, 2)) kernel = T.transpose(kernel, (3, 2, 1, 0)) y = T.nnet.abstract_conv.conv2d_grad_wrt_inputs(theano.shared(x), kernel, output_shape, filter_shape=None, border_mode=border_mode, subsample=strides, filter_flip=True) y = T.transpose(y, (0, 2, 3, 1)) output = y.eval() else: sess = K.get_session() output_shape = conv_layer.get_output_shape_for(K.shape(x)) output_shape = tf.pack([1, output_shape[1], output_shape[2], output_shape[3]]) y = tf.nn.conv2d_transpose(x, kernel, output_shape, (1, ) + strides + (1, ), padding=border_mode.upper()) output = sess.run(y) self.assertEqual(output_model.shape, (1, 4, 4, 2)) self.assertEqual(output.shape, (1, 4, 4, 2)) self.assertEqual(True, (output==output_model).all()) # model.fit(x, x + 1, nb_epoch=1)
def call(self, xpind, mask=None): # input shape: (nb_samples, time (padded with zeros), input_dim) # note that the .build() method of subclasses MUST define # self.input_spec with a complete input shape. x, indices = xpind if isinstance(mask, list): mask, _ = mask input_shape = self.input_spec[0].shape if K._BACKEND == 'tensorflow': if not input_shape[1]: raise Exception('When using TensorFlow, you should define ' 'explicitly the number of timesteps of ' 'your sequences.\n' 'If your first layer is an Embedding, ' 'make sure to pass it an "input_length" ' 'argument. Otherwise, make sure ' 'the first layer has ' 'an "input_shape" or "batch_input_shape" ' 'argument, including the time axis. ' 'Found input shape at layer ' + self.name + ': ' + str(input_shape)) if self.stateful: initial_states = self.states else: initial_states = self.get_initial_states(x) constants = self.get_constants(x) preprocessed_input = self.preprocess_input(x) last_output, outputs, states = IKE.dualsignal_rnn(self.step, preprocessed_input, initial_states, indices, go_backwards=self.go_backwards, mask=mask, constants=constants, unroll=self.unroll, input_length=input_shape[1]) last_tree, last_summary = last_output tree_outputs, summary_outputs = outputs if self.stateful: self.updates = [] for i in range(len(states)): self.updates.append((self.states[i], states[i])) self.cached_states = states return [tree_outputs, summary_outputs]
def call(self, xpind, mask=None): # input shape: (nb_samples, time (padded with zeros), input_dim) # note that the .build() method of subclasses MUST define # self.input_spec with a complete input shape. x, indices = xpind if isinstance(mask, list): mask, _ = mask input_shape = self.input_spec[0].shape if K._BACKEND == 'tensorflow': if not input_shape[1]: raise Exception('When using TensorFlow, you should define ' 'explicitly the number of timesteps of ' 'your sequences.\n' 'If your first layer is an Embedding, ' 'make sure to pass it an "input_length" ' 'argument. Otherwise, make sure ' 'the first layer has ' 'an "input_shape" or "batch_input_shape" ' 'argument, including the time axis. ' 'Found input shape at layer ' + self.name + ': ' + str(input_shape)) if self.stateful: initial_states = self.states else: initial_states = self.get_initial_states(x) constants = self.get_constants(x) preprocessed_input = self.preprocess_input(x) last_output, outputs, states = IKE.stack_rnn(self.step, preprocessed_input, initial_states, indices, go_backwards=self.go_backwards, mask=mask, constants=constants, unroll=self.unroll, input_length=input_shape[1]) if self.stateful: self.updates = [] for i in range(len(states)): self.updates.append((self.states[i], states[i])) self.cached_states = states if self.return_sequences: return outputs else: return last_output