affinity_search/makemodel.py
def create_model(args):
    '''Generate the model defined by args; first line (comment) contains solver/train.py arguments'''
    m = '# --base_lr %f --momentum %f --weight_decay %f -- solver %s\n' % (10**args.base_lr_exp,args.momentum,10**args.weight_decay_exp,args.solver)
    #test input
    m += '''
layer {
  name: "data"
  type: "MolGridData"
  top: "data"
  top: "label"
  top: "affinity"
  include {
    phase: TEST
  }
  molgrid_data_param {
        source: "TESTFILE"
        batch_size: 50
        dimension: 23.5
        resolution: %f
        shuffle: false
        ligmap: "%s"
        recmap: "%s"
        balanced: false
        has_affinity: true
        root_folder: "DATA_ROOT"
    }
  }
  ''' % (args.resolution,args.ligmap,args.recmap)
  
  #train input,
    m += '''
layer {
  name: "data"
  type: "MolGridData"
  top: "data"
  top: "label"
  top: "affinity"
  include {
    phase: TRAIN
  }
  molgrid_data_param {
        source: "TRAINFILE"
        batch_size:  50
        dimension: 23.5
        resolution: %f
        shuffle: true
        balanced: %s
        jitter: %f
        ligmap: "%s"
        recmap: "%s"        
        stratify_receptor: %s
        stratify_affinity_min: %d
        stratify_affinity_max: %d
        stratify_affinity_step: %f
        has_affinity: true
        random_rotation: true
        random_translate: 6
        root_folder: "DATA_ROOT"
    }
}
''' % (args.resolution, boolstr(args.balanced), args.jitter, args.ligmap, args.recmap, boolstr(args.stratify_receptor), 2 if args.stratify_affinity else 0, 10 if args.stratify_affinity else 0, args.stratify_affinity_step)

    #now pool/conv layers
        
    lastlayer = 'data'
    zeroseen = False
    vargs = vars(args)
    for i in range(1,6):
        poolsize = vargs['pool%d_size'%i]
        pooltype = vargs['pool%d_type'%i]
        if poolsize > 0:
            m += poollayer.format(i=i,lastlayer=lastlayer,size=poolsize,pooltype=pooltype)
            lastlayer = 'unit{0}_pool'.format(i)
            
        convwidth = vargs['conv%d_width'%i]
        
        if convwidth == 0:
            zeroseen = True
        elif convwidth > 0:
            if zeroseen:
                print("Invalid convolutional widths - non-zero layer after zero layer")
                sys.exit(-1)
            convlayer = 'unit%d_conv'%i
            ksize = vargs['conv%d_size'%i]
            stride = vargs['conv%d_stride'%i]
            func = vargs['conv%d_func'%i]
            norm = vargs['conv%d_norm'%i]
            init = vargs['conv%d_init'%i]
            pad = int(ksize/2)
            m += convolutionlayer.format(convlayer,lastlayer, convwidth, pad, ksize,stride)
            if norm != 'none':
                m += normlayer.format(convlayer, i, norm)
                convlayer += 'n'
            
            m += funclayer(convlayer, 'unit%d_func'%i, func)
            lastlayer = convlayer
        
        
        
    m += '''
layer {{
    name: "split"
    type: "Split"
    bottom: "{0}"
    top: "split"
}}
'''.format(lastlayer)
    #fully connected
    # pose
    
    if args.fc_pose_hidden == 0 and args.fc_pose_hidden2 != 0:
        print("Invalid pose hidden units")
        sys.exit(1)
        
    fcinfo = [] #tuples of name,numoutput
    if args.fc_pose_hidden > 0:
        fcinfo.append(('pose_fc', args.fc_pose_hidden, args.fc_pose_func))
        if args.fc_pose_hidden2 > 0:
            fcinfo.append(('pose_fc2', args.fc_pose_hidden2, args.fc_pose_func2))
    fcinfo.append(('pose_output',2, None))
            
    lastlayer = 'split'
    for (name,num,func) in fcinfo:        
        m += innerproductlayer.format(lastlayer, name, args.fc_pose_init, num)
        if func:
            m += funclayer(name,name+'_func',func)
        lastlayer = name
        
    # affinity
    
    if args.fc_affinity_hidden == 0 and args.fc_affinity_hidden2 != 0:
        print("Invalid affinity hidden units")
        sys.exit(1)
        
    fcinfo = [] #tuples of name,numoutput
    if args.fc_affinity_hidden > 0:
        fcinfo.append(('affinity_fc', args.fc_affinity_hidden, args.fc_affinity_func))
        if args.fc_affinity_hidden2 > 0:
            fcinfo.append(('affinity_fc2', args.fc_affinity_hidden2, args.fc_affinity_func2))
    fcinfo.append(('affinity_output',1,None))
            
    lastlayer = 'split'
    for (name,num,func) in fcinfo:        
        m += innerproductlayer.format(lastlayer, name, args.fc_affinity_init, num)
        lastlayer = name        
        if func:
            m += funclayer(name,name+'_func',func)
    
    #loss
    #pose
    m += '''layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "pose_output"
  bottom: "label"
  top: "loss"
}

layer {
  name: "output"
  type: "Softmax"
  bottom: "pose_output"
  top: "output"
}
layer {
  name: "labelout"
  type: "Split"
  bottom: "label"
  top: "labelout"
  include {
    phase: TEST
  }
}
'''
    #affinity loss
    m += '''layer {{
  name: "rmsd"
  type: "AffinityLoss"
  bottom: "affinity_output"
  bottom: "affinity"
  top: "rmsd"
  affinity_loss_param {{
    scale: 0.1
    gap: {0}
    pseudohuber: {1}
    delta: {2}
    penalty: {3}
    ranklossmult: {4}
    ranklossneg: {5}    
  }}
}}

layer {{
  name: "predaff"
  type: "Flatten"
  bottom: "affinity_output"
  top: "predaff"
}}

layer {{
  name: "affout"
  type: "Split"
  bottom: "affinity"
  top: "affout"
  include {{
    phase: TEST
  }}
}}
'''.format(args.loss_gap,boolstr(args.loss_pseudohuber),args.loss_delta, args.loss_penalty, args.ranklossmult, args.ranklossneg)
    
    return m
Generate the model defined by args; first line (comment) contains solver/train.py arguments
Generate the model defined by args; first line (comment) contains solver/train.py arguments
Generate the model defined by args ; first line ( comment ) contains solver / train . py arguments
def create_model(args):
    m = '# --base_lr %f --momentum %f --weight_decay %f -- solver %s\n' % (10**args.base_lr_exp,args.momentum,10**args.weight_decay_exp,args.solver)
    m += '''
layer {
  name: "data"
  type: "MolGridData"
  top: "data"
  top: "label"
  top: "affinity"
  include {
    phase: TEST
  }
  molgrid_data_param {
        source: "TESTFILE"
        batch_size: 50
        dimension: 23.5
        resolution: %f
        shuffle: false
        ligmap: "%s"
        recmap: "%s"
        balanced: false
        has_affinity: true
        root_folder: "DATA_ROOT"
    }
  }
  ''' % (args.resolution,args.ligmap,args.recmap)
    m += '''
layer {
  name: "data"
  type: "MolGridData"
  top: "data"
  top: "label"
  top: "affinity"
  include {
    phase: TRAIN
  }
  molgrid_data_param {
        source: "TRAINFILE"
        batch_size:  50
        dimension: 23.5
        resolution: %f
        shuffle: true
        balanced: %s
        jitter: %f
        ligmap: "%s"
        recmap: "%s"        
        stratify_receptor: %s
        stratify_affinity_min: %d
        stratify_affinity_max: %d
        stratify_affinity_step: %f
        has_affinity: true
        random_rotation: true
        random_translate: 6
        root_folder: "DATA_ROOT"
    }
}
''' % (args.resolution, boolstr(args.balanced), args.jitter, args.ligmap, args.recmap, boolstr(args.stratify_receptor), 2 if args.stratify_affinity else 0, 10 if args.stratify_affinity else 0, args.stratify_affinity_step)
    lastlayer = 'data'
    zeroseen = False
    vargs = vars(args)
    for i in range(1,6):
        poolsize = vargs['pool%d_size'%i]
        pooltype = vargs['pool%d_type'%i]
        if poolsize > 0:
            m += poollayer.format(i=i,lastlayer=lastlayer,size=poolsize,pooltype=pooltype)
            lastlayer = 'unit{0}_pool'.format(i)
        convwidth = vargs['conv%d_width'%i]
        if convwidth == 0:
            zeroseen = True
        elif convwidth > 0:
            if zeroseen:
                print("Invalid convolutional widths - non-zero layer after zero layer")
                sys.exit(-1)
            convlayer = 'unit%d_conv'%i
            ksize = vargs['conv%d_size'%i]
            stride = vargs['conv%d_stride'%i]
            func = vargs['conv%d_func'%i]
            norm = vargs['conv%d_norm'%i]
            init = vargs['conv%d_init'%i]
            pad = int(ksize/2)
            m += convolutionlayer.format(convlayer,lastlayer, convwidth, pad, ksize,stride)
            if norm != 'none':
                m += normlayer.format(convlayer, i, norm)
                convlayer += 'n'
            m += funclayer(convlayer, 'unit%d_func'%i, func)
            lastlayer = convlayer
    m += '''
layer {{
    name: "split"
    type: "Split"
    bottom: "{0}"
    top: "split"
}}
'''.format(lastlayer)
    if args.fc_pose_hidden == 0 and args.fc_pose_hidden2 != 0:
        print("Invalid pose hidden units")
        sys.exit(1)
    fcinfo = [] 
    if args.fc_pose_hidden > 0:
        fcinfo.append(('pose_fc', args.fc_pose_hidden, args.fc_pose_func))
        if args.fc_pose_hidden2 > 0:
            fcinfo.append(('pose_fc2', args.fc_pose_hidden2, args.fc_pose_func2))
    fcinfo.append(('pose_output',2, None))
    lastlayer = 'split'
    for (name,num,func) in fcinfo:        
        m += innerproductlayer.format(lastlayer, name, args.fc_pose_init, num)
        if func:
            m += funclayer(name,name+'_func',func)
        lastlayer = name
    if args.fc_affinity_hidden == 0 and args.fc_affinity_hidden2 != 0:
        print("Invalid affinity hidden units")
        sys.exit(1)
    fcinfo = [] 
    if args.fc_affinity_hidden > 0:
        fcinfo.append(('affinity_fc', args.fc_affinity_hidden, args.fc_affinity_func))
        if args.fc_affinity_hidden2 > 0:
            fcinfo.append(('affinity_fc2', args.fc_affinity_hidden2, args.fc_affinity_func2))
    fcinfo.append(('affinity_output',1,None))
    lastlayer = 'split'
    for (name,num,func) in fcinfo:        
        m += innerproductlayer.format(lastlayer, name, args.fc_affinity_init, num)
        lastlayer = name        
        if func:
            m += funclayer(name,name+'_func',func)
    m += '''layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "pose_output"
  bottom: "label"
  top: "loss"
}
layer {
  name: "output"
  type: "Softmax"
  bottom: "pose_output"
  top: "output"
}
layer {
  name: "labelout"
  type: "Split"
  bottom: "label"
  top: "labelout"
  include {
    phase: TEST
  }
}
'''
    m += '''layer {{
  name: "rmsd"
  type: "AffinityLoss"
  bottom: "affinity_output"
  bottom: "affinity"
  top: "rmsd"
  affinity_loss_param {{
    scale: 0.1
    gap: {0}
    pseudohuber: {1}
    delta: {2}
    penalty: {3}
    ranklossmult: {4}
    ranklossneg: {5}    
  }}
}}
layer {{
  name: "predaff"
  type: "Flatten"
  bottom: "affinity_output"
  top: "predaff"
}}
layer {{
  name: "affout"
  type: "Split"
  bottom: "affinity"
  top: "affout"
  include {{
    phase: TEST
  }}
}}
'''.format(args.loss_gap,boolstr(args.loss_pseudohuber),args.loss_delta, args.loss_penalty, args.ranklossmult, args.ranklossneg)
    return m
def create_model ( args ) : m = '# --base_lr %f --momentum %f --weight_decay %f -- solver %s\n' % ( 10 ** args . base_lr_exp , args . momentum , 10 ** args . weight_decay_exp , args . solver ) m += '''
layer {
  name: "data"
  type: "MolGridData"
  top: "data"
  top: "label"
  top: "affinity"
  include {
    phase: TEST
  }
  molgrid_data_param {
        source: "TESTFILE"
        batch_size: 50
        dimension: 23.5
        resolution: %f
        shuffle: false
        ligmap: "%s"
        recmap: "%s"
        balanced: false
        has_affinity: true
        root_folder: "DATA_ROOT"
    }
  }
  ''' % ( args . resolution , args . ligmap , args . recmap ) m += '''
layer {
  name: "data"
  type: "MolGridData"
  top: "data"
  top: "label"
  top: "affinity"
  include {
    phase: TRAIN
  }
  molgrid_data_param {
        source: "TRAINFILE"
        batch_size:  50
        dimension: 23.5
        resolution: %f
        shuffle: true
        balanced: %s
        jitter: %f
        ligmap: "%s"
        recmap: "%s"        
        stratify_receptor: %s
        stratify_affinity_min: %d
        stratify_affinity_max: %d
        stratify_affinity_step: %f
        has_affinity: true
        random_rotation: true
        random_translate: 6
        root_folder: "DATA_ROOT"
    }
}
''' % ( args . resolution , boolstr ( args . balanced ) , args . jitter , args . ligmap , args . recmap , boolstr ( args . stratify_receptor ) , 2 if args . stratify_affinity else 0 , 10 if args . stratify_affinity else 0 , args . stratify_affinity_step ) lastlayer = 'data' zeroseen = False vargs = vars ( args ) for i in range ( 1 , 6 ) : poolsize = vargs [ 'pool%d_size' % i ] pooltype = vargs [ 'pool%d_type' % i ] if poolsize > 0 : m += poollayer . format ( i = i , lastlayer = lastlayer , size = poolsize , pooltype = pooltype ) lastlayer = 'unit{0}_pool' . format ( i ) convwidth = vargs [ 'conv%d_width' % i ] if convwidth == 0 : zeroseen = True elif convwidth > 0 : if zeroseen : print ( "Invalid convolutional widths - non-zero layer after zero layer" ) sys . exit ( - 1 ) convlayer = 'unit%d_conv' % i ksize = vargs [ 'conv%d_size' % i ] stride = vargs [ 'conv%d_stride' % i ] func = vargs [ 'conv%d_func' % i ] norm = vargs [ 'conv%d_norm' % i ] init = vargs [ 'conv%d_init' % i ] pad = int ( ksize / 2 ) m += convolutionlayer . format ( convlayer , lastlayer , convwidth , pad , ksize , stride ) if norm != 'none' : m += normlayer . format ( convlayer , i , norm ) convlayer += 'n' m += funclayer ( convlayer , 'unit%d_func' % i , func ) lastlayer = convlayer m += '''
layer {{
    name: "split"
    type: "Split"
    bottom: "{0}"
    top: "split"
}}
''' . format ( lastlayer ) if args . fc_pose_hidden == 0 and args . fc_pose_hidden2 != 0 : print ( "Invalid pose hidden units" ) sys . exit ( 1 ) fcinfo = [ ] if args . fc_pose_hidden > 0 : fcinfo . append ( ( 'pose_fc' , args . fc_pose_hidden , args . fc_pose_func ) ) if args . fc_pose_hidden2 > 0 : fcinfo . append ( ( 'pose_fc2' , args . fc_pose_hidden2 , args . fc_pose_func2 ) ) fcinfo . append ( ( 'pose_output' , 2 , None ) ) lastlayer = 'split' for ( name , num , func ) in fcinfo : m += innerproductlayer . format ( lastlayer , name , args . fc_pose_init , num ) if func : m += funclayer ( name , name + '_func' , func ) lastlayer = name if args . fc_affinity_hidden == 0 and args . fc_affinity_hidden2 != 0 : print ( "Invalid affinity hidden units" ) sys . exit ( 1 ) fcinfo = [ ] if args . fc_affinity_hidden > 0 : fcinfo . append ( ( 'affinity_fc' , args . fc_affinity_hidden , args . fc_affinity_func ) ) if args . fc_affinity_hidden2 > 0 : fcinfo . append ( ( 'affinity_fc2' , args . fc_affinity_hidden2 , args . fc_affinity_func2 ) ) fcinfo . append ( ( 'affinity_output' , 1 , None ) ) lastlayer = 'split' for ( name , num , func ) in fcinfo : m += innerproductlayer . format ( lastlayer , name , args . fc_affinity_init , num ) lastlayer = name if func : m += funclayer ( name , name + '_func' , func ) m += '''layer {
  name: "loss"
  type: "SoftmaxWithLoss"
  bottom: "pose_output"
  bottom: "label"
  top: "loss"
}

layer {
  name: "output"
  type: "Softmax"
  bottom: "pose_output"
  top: "output"
}
layer {
  name: "labelout"
  type: "Split"
  bottom: "label"
  top: "labelout"
  include {
    phase: TEST
  }
}
''' m += '''layer {{
  name: "rmsd"
  type: "AffinityLoss"
  bottom: "affinity_output"
  bottom: "affinity"
  top: "rmsd"
  affinity_loss_param {{
    scale: 0.1
    gap: {0}
    pseudohuber: {1}
    delta: {2}
    penalty: {3}
    ranklossmult: {4}
    ranklossneg: {5}    
  }}
}}

layer {{
  name: "predaff"
  type: "Flatten"
  bottom: "affinity_output"
  top: "predaff"
}}

layer {{
  name: "affout"
  type: "Split"
  bottom: "affinity"
  top: "affout"
  include {{
    phase: TEST
  }}
}}
''' . format ( args . loss_gap , boolstr ( args . loss_pseudohuber ) , args . loss_delta , args . loss_penalty , args . ranklossmult , args . ranklossneg ) return m
Generate the model defined by args; first line (comment) contains solver/train.py arguments
Generate the model defined by args ; first line ( comment ) contains solver / train . py arguments
'''Generate the model defined by args; first line (comment) contains solver/train.py arguments''' #test input #train input, #now pool/conv layers #fully connected # pose #tuples of name,numoutput # affinity #tuples of name,numoutput #loss #pose #affinity loss
args