Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a dummy model in Keras?

Tags:

I am using keras and trying to train a classifier on 64x64 images.

I am trying to optimise my training pipeline and to catch the bottlenecks.

To this end I'm trying to create the simpler Keras model so that I know what time the whole process (loading image, data augmentation,...) takes with a very low charge on the GPU.

So far I managed to write:

def create_network_dummy():
  INPUT_SHAPE = (64, 64, 1)
  inputs = Input(INPUT_SHAPE)
  out = MaxPooling2D(pool_size = (1,1), strides=(64,64), 1)(inputs)
  model = Model(inputs=[inputs], outputs=[out])
  return model

Is it possible to have an even smaller one ? Returning a constant won't do because it breaks the graph and keras will not allow it.

like image 418
Scratch Avatar asked Nov 30 '18 13:11

Scratch


People also ask

How do you save a functional model in TensorFlow?

The standard way to save a functional model is to call model. save() to save the entire model as a single file. You can later recreate the same model from this file, even if the code that built the model is no longer available.

What is a Keras model?

Keras is a neural network Application Programming Interface (API) for Python that is tightly integrated with TensorFlow, which is used to build machine learning models. Keras' models offer a simple, user-friendly way to define a neural network, which will then be built for you by TensorFlow.


2 Answers

I think there is no need to even use K.identity:

inp = Input((64, 64, 1))
out = Lambda(lambda x: x)(inp)
model = Model(inp, out)
like image 133
today Avatar answered Nov 26 '22 07:11

today


import keras.backend as K
from keras.layers import Input, Lambda
from keras.models import Model

inp = Input((64,64,1))
out = Lambda(lambda x: K.identity(x))(inp)
model = Model(inp,out) #You could even try Model(inp,inp)

??

If the idea is to have a model that does nothing, this seems the best.
You can return a constant too, you don't really need to "train" to see what you proposed, you can just "predict".

model.predict_generator(....)

Another model wich outputs 1 class

inp = Input((64,64,1))
out = Lambda(lambda x: x[:,0,0])(inp)
model = Model(inp,out)
like image 20
Daniel Möller Avatar answered Nov 26 '22 08:11

Daniel Möller