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 tf2th(model): 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 test_conv3d(self): # TH input shape: (samples, input_depth, conv_dim1, conv_dim2, conv_dim3) # TF input shape: (samples, conv_dim1, conv_dim2, conv_dim3, input_depth) # TH kernel shape: (depth, input_depth, x, y, z) # TF kernel shape: (x, y, z, input_depth, depth) # test in dim_ordering = th for input_shape in [(2, 3, 4, 5, 4), (2, 3, 5, 4, 6)]: for kernel_shape in [(4, 3, 2, 2, 2), (4, 3, 3, 2, 4)]: xval = np.random.random(input_shape) xth = KTH.variable(xval) xtf = KTF.variable(xval) kernel_val = np.random.random(kernel_shape) - 0.5 kernel_th = KTH.variable( convert_kernel(kernel_val, dim_ordering='th')) kernel_tf = KTF.variable(kernel_val) zth = KTH.eval(KTH.conv3d(xth, kernel_th, dim_ordering='th')) ztf = KTF.eval(KTF.conv3d(xtf, kernel_tf, dim_ordering='th')) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05) # test in dim_ordering = tf input_shape = (1, 2, 2, 2, 1) kernel_shape = (2, 2, 2, 1, 1) xval = np.random.random(input_shape) xth = KTH.variable(xval) xtf = KTF.variable(xval) kernel_val = np.random.random(kernel_shape) - 0.5 kernel_th = KTH.variable(convert_kernel(kernel_val, dim_ordering='tf')) kernel_tf = KTF.variable(kernel_val) zth = KTH.eval(KTH.conv3d(xth, kernel_th, dim_ordering='tf')) ztf = KTF.eval(KTF.conv3d(xtf, kernel_tf, dim_ordering='tf')) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05)
def tf2th(model): 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) #print original_w.shape converted_w = original_w.transpose(0, 1, 3, 2) K.set_value(layer.W, converted_w) return model
def test_conv3d(self): # TH input shape: (samples, input_depth, conv_dim1, conv_dim2, conv_dim3) # TF input shape: (samples, conv_dim1, conv_dim2, conv_dim3, input_depth) # TH kernel shape: (depth, input_depth, x, y, z) # TF kernel shape: (x, y, z, input_depth, depth) # test in dim_ordering = th for input_shape in [(2, 3, 4, 5, 4), (2, 3, 5, 4, 6)]: for kernel_shape in [(4, 3, 2, 2, 2), (4, 3, 3, 2, 4)]: xval = np.random.random(input_shape) xth = KTH.variable(xval) xtf = KTF.variable(xval) kernel_val = np.random.random(kernel_shape) - 0.5 kernel_th = KTH.variable(convert_kernel(kernel_val)) kernel_tf = KTF.variable(kernel_val) zth = KTH.eval(KTH.conv3d(xth, kernel_th)) ztf = KTF.eval(KTF.conv3d(xtf, kernel_tf)) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05) # test in dim_ordering = tf input_shape = (1, 2, 2, 2, 1) kernel_shape = (2, 2, 2, 1, 1) xval = np.random.random(input_shape) xth = KTH.variable(xval) xtf = KTF.variable(xval) kernel_val = np.random.random(kernel_shape) - 0.5 kernel_th = KTH.variable(convert_kernel(kernel_val, dim_ordering='tf')) kernel_tf = KTF.variable(kernel_val) zth = KTH.eval(KTH.conv3d(xth, kernel_th, dim_ordering='tf')) ztf = KTF.eval(KTF.conv3d(xtf, kernel_tf, dim_ordering='tf')) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05)
def th2tf(model): import tensorflow as tf 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) return model
def VGG_16(weights_path=None): model = Sequential() model.add(ZeroPadding2D((1, 1), input_shape=(3, 224, 224))) model.add(Convolution2D(64, 3, 3, activation="relu")) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(64, 3, 3, activation="relu")) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, 3, 3, activation="relu")) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, 3, 3, activation="relu")) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, 3, 3, activation="relu")) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, 3, 3, activation="relu")) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, 3, 3, activation="relu")) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation="relu")) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation="relu")) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation="relu")) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation="relu")) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation="relu")) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation="relu")) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(Flatten()) model.add(Dense(4096, activation="relu")) model.add(Dropout(0.5)) model.add(Dense(4096, activation="relu")) model.add(Dropout(0.5)) model.add(Dense(1000, activation="softmax")) if weights_path: model.load_weights(weights_path) 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) return model
def VGG_16(weights_path=None): model = Sequential() model.add(ZeroPadding2D((1, 1), input_shape=(3, 224, 224))) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(64, 3, 3, activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(128, 3, 3, activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(256, 3, 3, activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(ZeroPadding2D((1, 1))) model.add(Convolution2D(512, 3, 3, activation='relu')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(Flatten()) model.add(Dense(4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(4096, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1000, activation='softmax')) if weights_path: model.load_weights(weights_path) 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) return model
def tensorflow2theano(model, weightfile=None): from keras.utils.np_utils import convert_kernel if weightfile: model.load_weights(weightfile) 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 convert_all_kernels_in_model(model, dim_ordering): for layer in model.layers: if layer.__class__.__name__ in [ 'Convolution1D', 'Convolution2D', 'Convolution3D', 'AtrousConvolution2D' ]: original_w = layer.get_weights()[0] original_b = layer.get_weights()[1] converted_w = convert_kernel(original_w, dim_ordering) converted_parameter = [converted_w, original_b] layer.set_weights(converted_parameter)
def test_conv2d(self): # TH kernel shape: (depth, input_depth, rows, cols) # TF kernel shape: (rows, cols, input_depth, depth) for input_shape in [(2, 3, 4, 5), (2, 3, 5, 6)]: for kernel_shape in [(4, 3, 2, 2), (4, 3, 3, 4)]: xval = np.random.random(input_shape) xth = KTH.variable(xval) xtf = KTF.variable(xval) kernel_val = np.random.random(kernel_shape) - 0.5 kernel_th = KTH.variable( convert_kernel(kernel_val, dim_ordering='th')) kernel_tf = KTF.variable(kernel_val) zth = KTH.eval(KTH.conv2d(xth, kernel_th, dim_ordering='th')) ztf = KTF.eval(KTF.conv2d(xtf, kernel_tf, dim_ordering='th')) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05) input_shape = (1, 6, 5, 3) kernel_shape = (3, 3, 3, 2) xval = np.random.random(input_shape) xth = KTH.variable(xval) xtf = KTF.variable(xval) kernel_val = np.random.random(kernel_shape) - 0.5 kernel_th = KTH.variable(convert_kernel(kernel_val, dim_ordering='tf')) kernel_tf = KTF.variable(kernel_val) zth = KTH.eval(KTH.conv2d(xth, kernel_th, dim_ordering='tf')) ztf = KTF.eval(KTF.conv2d(xtf, kernel_tf, dim_ordering='tf')) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05)
def test_conv2d(self): # TH kernel shape: (depth, input_depth, rows, cols) # TF kernel shape: (rows, cols, input_depth, depth) for input_shape in [(2, 3, 4, 5), (2, 3, 5, 6)]: for kernel_shape in [(4, 3, 2, 2), (4, 3, 3, 4)]: xval = np.random.random(input_shape) xth = KTH.variable(xval) xtf = KTF.variable(xval) kernel_val = np.random.random(kernel_shape) - 0.5 kernel_th = KTH.variable(convert_kernel(kernel_val)) kernel_tf = KTF.variable(kernel_val) zth = KTH.eval(KTH.conv2d(xth, kernel_th)) ztf = KTF.eval(KTF.conv2d(xtf, kernel_tf)) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05) input_shape = (1, 6, 5, 3) kernel_shape = (3, 3, 3, 2) xval = np.random.random(input_shape) xth = KTH.variable(xval) xtf = KTF.variable(xval) kernel_val = np.random.random(kernel_shape) - 0.5 kernel_th = KTH.variable(convert_kernel(kernel_val, dim_ordering='tf')) kernel_tf = KTF.variable(kernel_val) zth = KTH.eval(KTH.conv2d(xth, kernel_th, dim_ordering='tf')) ztf = KTF.eval(KTF.conv2d(xtf, kernel_tf, dim_ordering='tf')) assert zth.shape == ztf.shape assert_allclose(zth, ztf, atol=1e-05)
def theano2tensorflow(model, weightfile=None): from keras.utils.np_utils import convert_kernel import tensorflow as tf if weightfile: model.load_weights(weightfile) 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) return model
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)
print("Loading model for exporting to Protocol Buffer format...") model_path = 'temp.json' model = model_from_json(open('temp.json', ).read()) model.load_weights('temp.h5') K.set_learning_phase(0) # all new operations will be in test mode from now on ops = [] for layer in model.layers: print("layer name = " + layer.name) 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) print("Hi") K.get_session().run(ops) sess = K.get_session() export_path = "TensorGraphs\\simple.pb" # where to save the exported graph export_version = 1 # version number (integer) tf.global_variables_initializer() #initialize_all_variables(); minimal_graph = convert_variables_to_constants(sess, sess.graph_def, ["dense_3_b/Assign"]) # output tf.train.write_graph(sess.graph_def, './tmp/beginner-export', 'beginner-graph.pb',
@author: gpu2 """ from keras import backend as K from keras.utils.np_utils import convert_kernel import res_net50 # this is just for an example, comment if you want to use this script import h5py model = res_net50.get_resnet50() # load the weight model.load_weights('tf_resnet50.h5') # load tf/ date for layer in model.layers: if layer.__class__.name in ['Convolution2D', 'Convolution1D']: original_w = K.get_value(layer.W) converted_w = convert_kernel(original_w) K.set_value(layer.W, converted_w) model.save_weights('th_resnet50.h5') # save weights """ f_th = h5py.File('thresnet50.h5','w') f_tf = h5py.File('resnet50.h5','r') print f_tf.keys() for k in f_tf.keys(): grp = f_th.create_group(k) if k[:3]=='res' or k[:4]=='conv': grp.create_dataset('weights',data=convert_kernel(f_tf[k]['weights'][:])) else: grp.create_dataset('weights',data=f_tf[k]['weights'][:])