Example #1
0
def setup_best_user_dependent_hyperparameters():
    global USE_CONV_STRIDE_WITHOUT_POOLING, fc_h, fc_w
    global TF_ANG_VAR_SHAPES_DETACHED, TF_FIRST_FC_ID, TF_INPUT_SIZE
    global TF_ANG_STRIDES, TF_ANG_SCOPES
    global TF_INPUT_AFTER_RESIZE
    global BATCH_SIZE, L2_BETA, IN_DROPOUT, LAYER_DROPOUT, START_LR
    global FC1_WEIGHTS_DETACHED

    BATCH_SIZE = 25
    L2_BETA = 0.0001
    IN_DROPOUT = 0.0
    LAYER_DROPOUT = 0.5
    START_LR = 0.00005
    FC1_WEIGHTS_DETACHED = 100

    TF_ANG_SCOPES = ['conv1', 'conv2', 'conv3', 'conv4', 'fc1', 'out']
    TF_ANG_STRIDES = {
        'conv1': [1, 2, 2, 1],
        'conv2': [1, 2, 2, 1],
        'conv3': [1, 2, 2, 1],
        'conv4': [1, 1, 1, 1],
        'conv5': [1, 1, 1, 1]
    }

    fc_h, fc_w = models_utils.get_fc_height_width(TF_INPUT_SIZE, TF_ANG_SCOPES,
                                                  TF_ANG_STRIDES)

    # Best performing model from model search

    TF_ANG_VAR_SHAPES_DETACHED = {
        'conv1': [2, 4, 3, 64],
        'pool1': [1, 4, 8, 1],
        'conv2': [4, 8, 64, 64],
        'pool2': [1, 3, 3, 1],
        'conv3': [2, 4, 64, 128],
        'pool3': [1, 3, 3, 1],
        'conv4': [2, 2, 128, 128],
        'fc1': [fc_h * fc_w * 128, FC1_WEIGHTS_DETACHED],
        'out': [FC1_WEIGHTS_DETACHED, 1]
    }

    TF_FIRST_FC_ID = 'fc1'
Example #2
0
def setup_user_dependent_hyperparameters(no_pooling, square_input):

    global USE_CONV_STRIDE_WITHOUT_POOLING, fc_h, fc_w
    global TF_ANG_VAR_SHAPES_DETACHED, TF_FIRST_FC_ID, TF_INPUT_SIZE
    global TF_ANG_STRIDES, TF_ANG_SCOPES
    global TF_INPUT_AFTER_RESIZE

    if square_input:
        TF_INPUT_AFTER_RESIZE = [64, 64, 3]
        use_square_input = (TF_INPUT_SIZE[0] == TF_INPUT_SIZE[1])

    USE_CONV_STRIDE_WITHOUT_POOLING = no_pooling

    if USE_CONV_STRIDE_WITHOUT_POOLING:
        TF_ANG_SCOPES = ['conv1', 'conv2', 'conv3', 'conv4', 'fc1', 'out']
        TF_ANG_STRIDES = {
            'conv1': [1, 2, 2, 1],
            'conv2': [1, 2, 2, 1],
            'conv3': [1, 2, 2, 1],
            'conv4': [1, 2, 2, 1],
            'conv5': [1, 1, 1, 1]
        }

    else:
        TF_ANG_SCOPES = [
            'conv1', 'pool1', 'conv2', 'pool2', 'conv3', 'pool3', 'conv4',
            'fc1', 'out'
        ]
        #TF_ANG_STRIDES = {'conv1': [1, 1, 1, 1], 'conv2': [1, 1, 1, 1], 'conv3': [1, 1, 1, 1],'conv4':[1,1,1,1],
        #                  'pool1': [1, 1, 2, 1], 'pool2': [1, 2, 2, 1], 'pool3': [1, 2, 4, 1]}
        if use_square_input:
            TF_ANG_STRIDES = {
                'conv1': [1, 1, 1, 1],
                'conv2': [1, 1, 1, 1],
                'conv3': [1, 1, 1, 1],
                'conv4': [1, 1, 1, 1],
                'pool1': [1, 2, 2, 1],
                'pool2': [1, 2, 2, 1],
                'pool3': [1, 2, 2, 1]
            }

        else:
            TF_ANG_STRIDES = {
                'conv1': [1, 1, 1, 1],
                'conv2': [1, 1, 1, 1],
                'conv3': [1, 1, 1, 1],
                'conv4': [1, 1, 1, 1],
                'pool1': [1, 2, 2, 1],
                'pool2': [1, 2, 2, 1],
                'pool3': [1, 2, 2, 1]
            }

    fc_h, fc_w = models_utils.get_fc_height_width(TF_INPUT_SIZE, TF_ANG_SCOPES,
                                                  TF_ANG_STRIDES)

    # Best performing model from model search

    TF_ANG_VAR_SHAPES_DETACHED = {
        'conv1': [4, 8, 3, 32],
        'pool1': [1, 4, 8, 1],
        'conv2': [5, 5, 32, 64],
        'pool2': [1, 3, 3, 1],
        'conv3': [3, 3, 64, 128],
        'pool3': [1, 3, 3, 1],
        'conv4': [3, 3, 128, 128],
        'fc1': [fc_h * fc_w * 128, FC1_WEIGHTS_DETACHED],
        'out': [FC1_WEIGHTS_DETACHED, 1]
    }

    TF_FIRST_FC_ID = 'fc1'
Example #3
0
def do_search_top_bottom():
    global sess
    global sel_filter_hw_dict, sel_strid_dict

    setup_initial_cnn_for_top_bottom()
    sel_scope_list = list(to_try_scope_list) + ['fc1', 'out']

    tmp_filter_hw_dict = dict(sel_filter_hw_dict)
    tmp_strid_dict = dict(sel_strid_dict)

    # make cnns of size 1,2,..., max_2d_layer_count
    for scope in reversed(to_try_scope_list):
        lyr_idx = to_try_scope_list.index(scope)

        logger.info('Trying out CNNs lyr_idx of %d', lyr_idx)
        logger.debug('\t Previous kernel: %s', sel_filter_hw_dict)
        logger.debug('\t Previous stride: %s', sel_strid_dict)
        logger.info('\t Current scope: %s', scope)

        max_accuracy_sofar = 0  # let us try a local max accuracy
        max_col_acc_all, max_noncol_acc_all = 0, 0

        for fi, filter_hw in enumerate(filter_h_and_w_space):

            for si, stride in enumerate(strides_space):

                for di, depth in enumerate(depths_space):
                    logger.debug('depth loop')
                    logger.debug(
                        'Current loop state: %s (filter), %s (stride), %d (depth)',
                        filter_hw, stride, depth)

                    # update the dict only once the depth is fixed
                    # this should be used for all the calculations within the loops
                    # sel_xxxx are only updated in an increased accuracy
                    tmp_filter_hw_dict = dict(sel_filter_hw_dict)
                    tmp_strid_dict = dict(sel_strid_dict)

                    logger.debug('sel to tmp copy point')
                    validate_conv_kernel_sizes(to_try_scope_list,
                                               tmp_filter_hw_dict)
                    logger.debug('sel')
                    validate_conv_kernel_sizes(to_try_scope_list,
                                               sel_filter_hw_dict)

                    tmp_strid_dict[scope] = stride

                    # fix previous and after layer depths
                    if 'conv' in scope:
                        # update entries of the current scope
                        tmp_filter_hw_dict[scope][0] = filter_hw[0]
                        tmp_filter_hw_dict[scope][1] = filter_hw[1]
                        tmp_filter_hw_dict[scope][3] = depth

                        logger.debug(
                            'Fixing depth of previous and later layer')
                        # previous layer
                        if scope == 'conv1':
                            in_depth = 3
                        else:
                            # find the conv scope
                            logger.debug('Fixing layer below %s', scope)
                            for tmp_scope in reversed(
                                    to_try_scope_list[:lyr_idx]):
                                if 'conv' in tmp_scope:
                                    logger.debug(
                                        '\tPrevious kernel (no change) (%s): %s',
                                        tmp_scope,
                                        tmp_filter_hw_dict[tmp_scope])
                                    in_depth = tmp_filter_hw_dict[tmp_scope][3]
                                    # update the in channel depth
                                    tmp_filter_hw_dict[scope][2] = in_depth
                                    break

                        # after layer (except last layer)
                        if scope != to_try_scope_list[-1]:
                            logger.debug('Fixing layer above %s', scope)
                            # find the conv scope
                            for tmp_scope in to_try_scope_list[lyr_idx + 1:]:
                                print(tmp_scope)
                                if 'conv' in tmp_scope:
                                    # update kernel size of the last conv layer
                                    print(tmp_scope)
                                    logger.debug('\tPrevious kernel (%s): %s',
                                                 tmp_scope,
                                                 tmp_filter_hw_dict[tmp_scope])
                                    tmp_filter_hw_dict[tmp_scope][2] = depth
                                    logger.debug('\tAfter kernel (%s): %s',
                                                 tmp_scope,
                                                 tmp_filter_hw_dict[tmp_scope])
                                    break

                    # update kernel of the current layer (pool)
                    if 'pool' in scope:
                        tmp_filter_hw_dict[scope] = [1] + filter_hw + [1]

                    kernel_size = tmp_filter_hw_dict[scope]

                    validate_conv_kernel_sizes(to_try_scope_list,
                                               tmp_filter_hw_dict)
                    logger.debug('sel')
                    validate_conv_kernel_sizes(to_try_scope_list,
                                               sel_filter_hw_dict)

                    # if the last 2d size is smaller than the minimum we want, do not train
                    # continuing at this point causes trouble. So lets run it even if it is too small
                    fc_h, fc_w = models_utils.get_fc_height_width(
                        config.TF_INPUT_AFTER_RESIZE, sel_scope_list,
                        tmp_strid_dict)
                    if fc_h < min_last_2d[0] or fc_w < min_last_2d[1]:
                        speclogger.info('Last 2d input size is too small')
                        continue

                    # Moved this to the inner most loop (instead of second from innermost)
                    # beacase this might cause the depth fixing of cnn layers to go haywire
                    if 'pool' in scope and stride == [1, 1, 1, 1]:
                        speclogger.info(
                            'Not trying this combination pooling with stride 1 depth %d',
                            depth)
                        continue

                    speclogger.info(
                        'Trying 2d layer specs (conv/pool) %s, %s (kernel), %s (stride)\n',
                        scope, kernel_size, stride)

                    logger.info('\n')
                    logger.info(
                        '\t\t Trying specs (%s): %s (kernel), %s (stride)',
                        scope, kernel_size, stride)

                    # deciding the fulcon size first and freeze that for layers above
                    if fi == 0 and si == 0 and scope == to_try_scope_list[-1]:
                        logger.info(
                            'Jointly seaching conv5 and fulcon spaces ...')
                        for fc_size in fulcon_space:

                            speclogger.info(
                                'Trying fulcon layer specs (conv/pool) %d\n',
                                fc_size)
                            # updating fc_h and fc_w as it changes with the stride of convolution layers below
                            fc_h, fc_w = models_utils.get_fc_height_width(
                                config.TF_INPUT_AFTER_RESIZE, sel_scope_list,
                                tmp_strid_dict)
                            logger.debug(
                                '\t\t Obtained height and with for fulcon : %d, %d',
                                fc_h, fc_w)
                            logger.debug(
                                '\t\t selelcted scope %s and strides: %s',
                                sel_scope_list, tmp_strid_dict)
                            tmp_filter_hw_dict['fc1'] = [
                                fc_h * fc_w * depth, fc_size
                            ]
                            tmp_filter_hw_dict['out'] = [
                                fc_size, config.TF_NUM_CLASSES
                            ]

                            logger.info('\n')
                            logger.info(
                                '\t\t Trying specs (%s): %d (fc_size), %s (fc_kernel), %s (out kernel)',
                                scope, fc_size, tmp_filter_hw_dict['fc1'],
                                tmp_filter_hw_dict['out'])

                            tf.reset_default_graph()
                            build_tensorflw_variables_naive(
                                sel_scope_list, tmp_filter_hw_dict)
                            logger.info('\t\t Variable creation successful')

                            cnn_learner_naive.set_scope_kernel_stride_dicts(
                                sel_scope_list, tmp_filter_hw_dict,
                                tmp_strid_dict)

                            define_cnn_ops()
                            logger.info(
                                '\t\t Defining tensorflow ops successful')
                            max_noncol_acc, max_col_acc = train_cnn()
                            logger.info('\n')

                            # measure used to measure goodness of model
                            avg_accuracy = (max_noncol_acc + max_col_acc) / 2.0

                            if avg_accuracy > max_accuracy_sofar:
                                max_accuracy_sofar = avg_accuracy
                                max_noncol_acc_all = max_noncol_acc
                                max_col_acc_all = max_col_acc
                                sel_filter_hw_dict = dict(tmp_filter_hw_dict)
                                sel_strid_dict = dict(tmp_strid_dict)
                                sel_filter_hw_dict['fc1'] = [
                                    fc_h * fc_w * depth, fc_size
                                ]
                                speclogger.info('Best choice (FC)')

                                validate_conv_kernel_sizes(
                                    to_try_scope_list, tmp_filter_hw_dict)
                                logger.debug('sel')
                                validate_conv_kernel_sizes(
                                    to_try_scope_list, sel_filter_hw_dict)

                                # if we have a better choice of FC
                                logger.info(
                                    'Choosing the best specs for conv1 + fulcon'
                                )
                                logger.info('Best selected spec:')
                                logger.info('\t%s', sel_filter_hw_dict)
                                logger.info('\t%s', sel_strid_dict)
                                logger.info('Best FC selected %s',
                                            sel_filter_hw_dict['fc1'])

                            speclogger.info('Layer index %d', lyr_idx)
                            speclogger.info(sel_scope_list)
                            speclogger.info(tmp_filter_hw_dict)
                            speclogger.info(tmp_strid_dict)
                            speclogger.info(
                                'Accuracy measures (fc): %.3f of %.3f (noncol), %.3f of %.3f (col)',
                                max_noncol_acc, max_noncol_acc_all,
                                max_col_acc, max_col_acc_all)
                            speclogger.info('Avg accuracy: %.3f', avg_accuracy)
                            speclogger.info('\n')

                        speclogger.info('(BEST) Layer index %d', lyr_idx)
                        speclogger.info(sel_scope_list)
                        speclogger.info(tmp_filter_hw_dict)
                        speclogger.info(tmp_strid_dict)
                        speclogger.info(
                            'Accuracy measures: %.3f (noncol), %.3f (col)',
                            max_noncol_acc, max_col_acc)
                        speclogger.info('\n')

                    else:

                        logger.info('Calculating FC size...\n')
                        logger.debug('scope: %s', sel_scope_list)
                        logger.debug('filter: %s', tmp_filter_hw_dict)
                        logger.debug('stride: %s', tmp_strid_dict)

                        # everytime we try a new configuration, we need to modify the fc layer input size
                        fc_h, fc_w = models_utils.get_fc_height_width(
                            config.TF_INPUT_AFTER_RESIZE, sel_scope_list,
                            tmp_strid_dict)
                        tmp_filter_hw_dict['fc1'][
                            0] = fc_h * fc_w * tmp_filter_hw_dict[
                                to_try_scope_list[-1]][-1]
                        logger.debug('fc_h: %d, fc_w: %d', fc_h, fc_w)
                        logger.debug('New fulcon layer input size: %d',
                                     tmp_filter_hw_dict['fc1'][0])

                        tf.reset_default_graph()

                        build_tensorflw_variables_naive(
                            sel_scope_list, tmp_filter_hw_dict)
                        logger.info('\t\t Variable creation successful')

                        cnn_learner_naive.set_scope_kernel_stride_dicts(
                            sel_scope_list, tmp_filter_hw_dict, tmp_strid_dict)

                        define_cnn_ops()
                        logger.info('\t\t Defining tensorflow ops successful')
                        max_noncol_acc, max_col_acc = train_cnn()
                        logger.info(
                            '\t\t Accuracy measures: %.3f (noncol), %.3f (col)',
                            max_noncol_acc, max_col_acc)
                        speclogger.info(
                            'Accuracy measures: %.3f of %.3f (noncol), %.3f of %.3f (col)',
                            max_noncol_acc, max_noncol_acc_all, max_col_acc,
                            max_col_acc_all)

                        avg_accuracy = (max_noncol_acc + max_col_acc) / 2.0

                        if avg_accuracy > max_accuracy_sofar:
                            max_accuracy_sofar = avg_accuracy
                            max_noncol_acc_all = max_noncol_acc
                            max_col_acc_all = max_col_acc
                            sel_filter_hw_dict = dict(tmp_filter_hw_dict)
                            sel_strid_dict = dict(tmp_strid_dict)

                            validate_conv_kernel_sizes(to_try_scope_list,
                                                       tmp_filter_hw_dict)
                            logger.debug('sel')
                            validate_conv_kernel_sizes(to_try_scope_list,
                                                       sel_filter_hw_dict)

                            logger.debug(
                                '\t\t\t =============== Found best layer choice. Current layer choices ===============\n'
                            )
                            logger.debug('\t\t\t (current Best) filter: %s',
                                         sel_filter_hw_dict)
                            logger.debug('\t\t\t (current Best) stride: %s',
                                         sel_strid_dict)

                            logger.info('\n')
                            speclogger.info(
                                '=====================Current Best===================\n'
                            )

                        speclogger.info('Layer index %d', lyr_idx)
                        speclogger.info(sel_scope_list)
                        speclogger.info(tmp_filter_hw_dict)
                        speclogger.info(tmp_strid_dict)
                        speclogger.info(
                            'Accuracy measures: %.3f (noncol), %.3f (col)',
                            max_noncol_acc, max_col_acc)
                        speclogger.info('Avg Accuracy measures: %.3f\n',
                                        (max_noncol_acc + max_col_acc) / 2.0)

                        logger.debug('end iteration inner most loop')
                        validate_conv_kernel_sizes(to_try_scope_list,
                                                   tmp_filter_hw_dict)
                        logger.debug('sel')
                        validate_conv_kernel_sizes(to_try_scope_list,
                                                   sel_filter_hw_dict)

        speclogger.info('(outer loop) BEST Results for Layer index %d',
                        lyr_idx)
        speclogger.info(sel_scope_list)
        speclogger.info(sel_filter_hw_dict)
        speclogger.info(sel_strid_dict)
        speclogger.info('Accuracy measures: %.3f (noncol), %.3f (col)',
                        max_noncol_acc, max_col_acc)
        speclogger.info('Avg Accuracy measures: %.3f\n',
                        (max_noncol_acc + max_col_acc) / 2.0)

        logger.info('\n')
        logger.info('Current Summary')
        logger.info(sel_scope_list)
        logger.info(sel_filter_hw_dict)
        logger.info(sel_strid_dict)

        logger.info('\n')

        logger.debug('end one iteration scope')
        validate_conv_kernel_sizes(to_try_scope_list, tmp_filter_hw_dict)
        logger.debug('sel')
        validate_conv_kernel_sizes(to_try_scope_list, sel_filter_hw_dict)

    logger.info('\n')
    logger.info('THE BEST MODEL')
    logger.info(sel_scope_list)
    logger.info(sel_filter_hw_dict)
    logger.info(sel_strid_dict)
Example #4
0
def do_search_bottom_top():
    global sess

    in_depth = 0
    last_conv_layer = None  # used for fc calculation and pool layer doesn't have depth

    # make cnns of size 1,2,..., max_2d_layer_count
    for lyr_idx, scope in enumerate(to_try_scope_list):

        max_accuracy_sofar = 0  # let us try a local max accuracy

        logger.info('Trying out CNNs of %d layers', lyr_idx)
        logger.debug('\t Previous scope: %s', sel_scope_list)
        logger.debug('\t Previous kernel: %s', sel_filter_hw_dict)
        logger.debug('\t Previous stride: %s', sel_strid_dict)
        if lyr_idx == 0:
            sel_scope_list.append(scope)
        else:
            del sel_scope_list[-1]
            del sel_scope_list[-1]
            sel_scope_list.append(scope)

        # add fc layer in the 1st conv exploration
        sel_scope_list.extend(['fc1', 'out'])

        logger.info('\t Current scope: %s', sel_scope_list)
        lyr_choices = []
        lyr_choices_0 = []  # use only for 0th layer
        for filter_hw in filter_h_and_w_space:
            for stride in strides_space:
                for depth in depths_space:

                    if lyr_idx == 0:
                        in_depth = 3

                    if 'conv' in scope:
                        kernel_size = filter_hw + [in_depth, depth]
                        last_conv_layer = scope
                    elif 'pool' in scope:
                        kernel_size = [1] + filter_hw + [1]

                    logger.info('\n')
                    logger.info('\t\t Trying specs: %s (kernel), %s (stride)',
                                kernel_size, stride)

                    # Temporarily add current kernel and strid to dicts
                    sel_filter_hw_dict[scope] = kernel_size
                    sel_strid_dict[scope] = stride

                    # deciding the fulcon size first and freeze that for layers above
                    if lyr_idx == 0:
                        logger.info(
                            'Jointly seaching conv1 and fulcon spaces ...')
                        for fc_size in fulcon_space:

                            # updating fc_h and fc_w as it changes with the stride of convolution layers below
                            fc_h, fc_w = models_utils.get_fc_height_width(
                                config.TF_INPUT_AFTER_RESIZE, sel_scope_list,
                                sel_strid_dict)
                            logger.debug(
                                '\t\t Obtained height and with for fulcon : %d, %d',
                                fc_h, fc_w)
                            logger.debug(
                                '\t\t selelcted scope %s and strides: %s',
                                sel_scope_list, sel_strid_dict)
                            sel_filter_hw_dict['fc1'] = [
                                fc_h * fc_w * depth, fc_size
                            ]
                            sel_filter_hw_dict['out'] = [
                                fc_size, config.TF_NUM_CLASSES
                            ]

                            logger.info('\n')
                            logger.info(
                                '\t\t Trying specs: %d (fc_size), %s (fc_kernel), %s (out kernel)',
                                fc_size, sel_filter_hw_dict['fc1'],
                                sel_filter_hw_dict['out'])

                            tf.reset_default_graph()
                            build_tensorflw_variables_naive(
                                sel_scope_list, sel_filter_hw_dict)
                            logger.info('\t\t Variable creation successful')

                            cnn_learner_naive.set_scope_kernel_stride_dicts(
                                sel_scope_list, sel_filter_hw_dict,
                                sel_strid_dict)

                            define_cnn_ops()
                            logger.info(
                                '\t\t Defining tensorflow ops successful')
                            max_noncol_acc, max_col_acc = train_cnn()
                            logger.info(
                                '\t\t Accuracy measures: %.3f (noncol), %.3f (col)',
                                max_noncol_acc, max_col_acc)
                            logger.info('\n')

                            avg_accuracy = (max_noncol_acc + max_col_acc) / 2.0

                            if avg_accuracy > max_accuracy_sofar:
                                max_accuracy_sofar = avg_accuracy
                                lyr_choices_0.append([{
                                    'fc_size': fc_size
                                }, (max_col_acc + max_noncol_acc) / 2.0])
                                lyr_choices.append([{
                                    'kernel': kernel_size,
                                    'stride': stride
                                }, (max_col_acc + max_noncol_acc) / 2.0])
                                speclogger.info('Best choice (FC)')

                            speclogger.info('Layer index %d', lyr_idx)
                            speclogger.info(sel_scope_list)
                            speclogger.info(sel_filter_hw_dict)
                            speclogger.info(sel_strid_dict)
                            speclogger.info(
                                'Accuracy measures: %.3f (noncol), %.3f (col)',
                                max_noncol_acc, max_col_acc)
                            speclogger.info('\n')

                        # if we have a better choice of FC
                        if len(lyr_choices_0) > 0:
                            logger.info(
                                'Choosing the best specs for conv1 + fulcon')
                            logger.info(lyr_choices_0)
                            selected_spec = choose_best_layer_choice(
                                lyr_choices_0)
                            logger.info('Best selected spec: %s',
                                        selected_spec)
                            sel_filter_hw_dict['fc1'] = [
                                fc_h * fc_w * depth, selected_spec['fc_size']
                            ]
                            sel_filter_hw_dict['out'] = [
                                selected_spec['fc_size'], config.TF_NUM_CLASSES
                            ]
                            logger.info('Best FC selected %s',
                                        sel_filter_hw_dict['fc1'])

                            speclogger.info('(BEST) Layer index %d', lyr_idx)
                            speclogger.info(sel_scope_list)
                            speclogger.info(sel_filter_hw_dict)
                            speclogger.info(sel_strid_dict)
                            speclogger.info(
                                'Accuracy measures: %.3f (noncol), %.3f (col)',
                                max_noncol_acc, max_col_acc)
                            speclogger.info('\n')
                        else:
                            # this should not happen
                            raise NotImplementedError
                    else:

                        logger.info('Calculating FC size...\n')
                        logger.debug('scope: %s', sel_scope_list)
                        logger.debug('filter: %s', sel_filter_hw_dict)
                        logger.debug('stride: %s', sel_strid_dict)
                        logger.debug('last_depth: %d',
                                     sel_filter_hw_dict[last_conv_layer][-1])

                        # everytime we try a new configuration, we need to modify the fc layer input size
                        fc_h, fc_w = models_utils.get_fc_height_width(
                            config.TF_INPUT_AFTER_RESIZE, sel_scope_list,
                            sel_strid_dict)
                        sel_filter_hw_dict['fc1'][
                            0] = fc_h * fc_w * sel_filter_hw_dict[
                                last_conv_layer][-1]
                        logger.debug('fc_h: %d, fc_w: %d', fc_h, fc_w)
                        logger.debug('New fulcon layer input size: %d',
                                     sel_filter_hw_dict['fc1'][0])

                        tf.reset_default_graph()
                        build_tensorflw_variables_naive(
                            sel_scope_list, sel_filter_hw_dict)
                        logger.info('\t\t Variable creation successful')

                        cnn_learner_naive.set_scope_kernel_stride_dicts(
                            sel_scope_list, sel_filter_hw_dict, sel_strid_dict)

                        define_cnn_ops()
                        logger.info('\t\t Defining tensorflow ops successful')
                        max_noncol_acc, max_col_acc = train_cnn()
                        logger.info(
                            '\t\t\t Accuracy measures: %.3f (noncol), %.3f (col)',
                            max_noncol_acc, max_col_acc)

                        avg_accuracy = (max_noncol_acc + max_col_acc) / 2.0

                        if avg_accuracy > max_accuracy_sofar:
                            max_accuracy_sofar = avg_accuracy
                            lyr_choices.append([{
                                'kernel': kernel_size,
                                'stride': stride
                            }, (max_col_acc + max_noncol_acc) / 2.0])
                            logger.debug(
                                '\t\t\t Found best layer choice. Current layer choices'
                            )
                            logger.debug('\t\t\t %s', lyr_choices)
                            logger.info('\n')
                            speclogger.info('Current Best')

                        speclogger.info('Layer index %d', lyr_idx)
                        speclogger.info(sel_scope_list)
                        speclogger.info(sel_filter_hw_dict)
                        speclogger.info(sel_strid_dict)
                        speclogger.info(
                            'Accuracy measures: %.3f (noncol), %.3f (col)',
                            max_noncol_acc, max_col_acc)
                        speclogger.info('Avg Accuracy measures: %.3f\n',
                                        (max_noncol_acc + max_col_acc) / 2.0)

                    logger.info('Removing temoporarily added scope')
                    # Remove current kernel and strid from dicts
                    del sel_filter_hw_dict[scope]
                    del sel_strid_dict[scope]

                    # no point in traversing in the most inner loop as the pooling layer doesn't change with depth
                    if 'pool' in scope:
                        break

        # if we have some choice that acctually increase our maximum performance
        if len(lyr_choices) > 0:
            selected_spec = choose_best_layer_choice(lyr_choices)
            sel_filter_hw_dict[scope] = selected_spec['kernel']
            sel_strid_dict[scope] = selected_spec['stride']
            if 'conv' in scope:
                in_depth = selected_spec['kernel'][-1]

            speclogger.info('(BEST) Layer index %d', lyr_idx)
            speclogger.info(sel_scope_list)
            speclogger.info(sel_filter_hw_dict)
            speclogger.info(sel_strid_dict)
            speclogger.info('Accuracy measures: %.3f (noncol), %.3f (col)',
                            max_noncol_acc, max_col_acc)
            speclogger.info('Avg Accuracy measures: %.3f\n',
                            (max_noncol_acc + max_col_acc) / 2.0)

        else:
            del sel_scope_list[-1]  # out
            del sel_scope_list[-1]  # fc1
            del sel_scope_list[-1]  # last layer

            # if last layer did not improve performance update last_conv_layer
            for tmp_scope in reversed(sel_scope_list):
                if 'conv' in tmp_scope:
                    last_conv_layer = tmp_scope
                    break

            sel_scope_list.extend(['fc1', 'out'])
            logger.info('Adding scope %s did not help', scope)
            speclogger.info('Adding scope %s did not help\n', scope)

        logger.info('\n')
        logger.info('Current Summary')
        logger.info(sel_scope_list)
        logger.info(sel_filter_hw_dict)
        logger.info(sel_strid_dict)
        logger.info(lyr_choices)

        logger.info('\n')

        # to check if the last 2d size is too small
        fc_h, fc_w = models_utils.get_fc_height_width(
            config.TF_INPUT_AFTER_RESIZE, sel_scope_list, sel_strid_dict)
        if fc_h < min_last_2d[0] or fc_w < min_last_2d[1]:
            break

    logger.info('\n')
    logger.info('THE BEST MODEL')
    logger.info(sel_scope_list)
    logger.info(sel_filter_hw_dict)
    logger.info(sel_strid_dict)
Example #5
0
        if USE_DILATION:
            TF_ANG_SCOPES = ['conv1', 'conv2', 'conv4', 'pool1', 'fc1', 'out']
            TF_DILATION = {'conv1': [2, 2], 'conv2': [4, 4], 'conv3': [8, 8], 'conv4': [8, 8]}
            TF_ANG_STRIDES = {'conv1': [1, 1, 1, 1], 'conv2': [1, 1, 1, 1], 'conv3': [1, 1, 1, 1],
                              'conv4': [1, 1, 1, 1],
                              'pool1': [1, 8, 8, 1]}
        else:
            TF_ANG_STRIDES = {'conv1': [1, 1, 1, 1], 'conv2': [1, 1, 1, 1], 'conv3': [1, 1, 1, 1], 'conv4': [1, 1, 1, 1],
                                                'pool1': [1, 2, 2, 1], 'pool2': [1, 2, 2, 1], 'pool3': [1, 2, 2, 1]}

    else:
        TF_ANG_STRIDES = {'conv1': [1, 1, 1, 1], 'conv2': [1, 1, 1, 1], 'conv3': [1, 1, 1, 1], 'conv4': [1, 1, 1, 1],
                                            'pool1': [1, 2, 2, 1], 'pool2': [1, 2, 2, 1], 'pool3': [1, 2, 2, 1]}
        TF_DILATION = {}

fc_h, fc_w = models_utils.get_fc_height_width(TF_INPUT_AFTER_RESIZE, TF_ANG_SCOPES, TF_ANG_STRIDES)


# Best performing model from model search

if use_square_input:
    TF_ANG_VAR_SHAPES_NAIVE = {
        'conv1': [4, 4, 3, 96], 'pool1':[1,2,2,1], 'conv2': [4, 4, 96, 96], 'pool2':[1,2,2,1],
        'conv3': [4, 4, 96, 96],'pool3':[1,2,2,1], 'conv4': [4,4,96,96],
        'fc1': [fc_h * fc_w * 96, FC1_WEIGHTS],
        'out': [FC1_WEIGHTS, TF_NUM_CLASSES//3]
    }

    if not FIVE_WAY_EXPERIMENT:
        TF_ANG_VAR_SHAPES_MULTIPLE = {'conv1': [4, 4, 3, 32], 'pool1': [1, 2, 2, 1], 'conv2': [4, 4, 96, 32],
                                      'pool2': [1, 2, 2, 1],