microsoft / CNTK

Microsoft Cognitive Toolkit (CNTK), an open source deep-learning toolkit
https://docs.microsoft.com/cognitive-toolkit/
Other
17.5k stars 4.29k forks source link

Change input dimension without .clone() #3160

Open KBS91 opened 6 years ago

KBS91 commented 6 years ago

I am working now on project in CNTK 2.2 in Python. I made code:

    featureNode = modelCloned.find_by_name('features')
    newNode = {featureNode: cntk.layers.placeholder(name='features')}

    modelFeat = modelCloned.clone(cntk.ops.functions.CloneMethod.clone, newNode)

    labelInput = cntk.ops.input_variable(numClasses)
    imageInput = cntk.ops.input_variable((3, imgH, imgW), name="input")
    input_map = {
        imageInput: trainMinibatches['features'],
        labelInput: trainMinibatches['labels']
    }
    finalModel = modelFeat (inputMap)

to replace input Feature dimensions in CNTK model that I load from file. Later I apply input map, so I scale features layer to dimension like I want. That Model is Resnet18

Problem is that I do not want to use .clone() funtion. It causes to run out my GPU memory.

Does enybody knows how to replace 'feature' layer without clone funtion?

Best regards, Chris

ke1337 commented 6 years ago

Once a model has all input shapes determined, the InferredDimension in parameters are fixed too. So you cannot change just the input. Instead, you need to find out the part of graph that shares the same dimension before and after the input shape change, and replace anything before that. Below is an illustration of the concept:

import cntk as C
ii = C.input_variable(20)
dense_ii = C.layers.Dense(10)(ii)
z = dense_ii + C.parameter(10)
ii2 = C.input_variable(30)
dense_ii2 = C.layers.Dense(10)(ii2)
z_c = z.clone(C.CloneMethod.share, {dense_ii:dense_ii2})
z_c
Composite(Plus): Input('Input189', [#], [30]) -> Output('Plus230_Output_0', [#], [10])