Exemplo n.º 1
0
def init():
    network = MobileNet(alpha=1.0)
    params = network.get_weights()

    graph = tf.Graph()
    with graph.as_default():
        images = np.random.rand(1, 224, 224, 3)

        inference(images, False)

        model_checkpoint_path = 'log/model_dump/model.ckpt'
        var_list = tf.get_collection('params')
        assert len(var_list) == len(params)
        saver = tf.train.Saver(var_list)

        with tf.Session(graph=graph) as sess:
            sess.run(tf.global_variables_initializer())
            for i in range(len(var_list)):
                if 'depthwise' in var_list[i].name and len(
                        params[i].shape) == 4:
                    params[i] = np.transpose(params[i], (0, 1, 3, 2))
                if len(params[i].shape) == 2:
                    params[i] = np.expand_dims(params[i], 0)
                    params[i] = np.expand_dims(params[i], 0)
                print(var_list[i].name, var_list[i].shape, params[i].shape)
                sess.run(tf.assign(var_list[i], params[i]))

            saver.save(sess,
                       model_checkpoint_path,
                       write_meta_graph=False,
                       write_state=False)
Exemplo n.º 2
0
tf.app.flags.DEFINE_integer('version', '1', 'Model Version')
tf.app.flags.DEFINE_string('model_type', '',
                           'which model do you want to train')

FLAGS = tf.app.flags.FLAGS

# very important to do this as a first thing
K.set_learning_phase(0)

#if FLAGS.model_type == 'mobilenet_1_228_bottleneck':
#model = MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224, 3), pooling='avg')
model = MobileNet(weights='imagenet', alpha=0.75)

# The creation of a new model might be optional depending on the goal
config = model.get_config()
weights = model.get_weights()

#Solution from https://github.com/fchollet/keras/issues/7431#issuecomment-334959500
# with CustomObjectScope({'relu6': keras.applications.mobilenet.relu6,'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
#     model2 = Model.from_config(config)
#     model2.set_weights(weights)

sess = K.get_session()


def freeze_models(sess, out_name, fpath=None):
    #https://stackoverflow.com/questions/34343259/is-there-an-example-on-how-to-generate-protobuf-files-holding-trained-tensorflow
    #https://gist.github.com/tokestermw/795cc1fd6d0c9069b20204cbd133e36b

    frozen_graph_def = convert_variables_to_constants(sess, sess.graph_def,
                                                      [out_name])