Beispiel #1
0
def conv(d, fn, input, name, channels, ksize, strides, **args):

    name = kld.lst.merge_str(name)
    args = kld.aux.merge_dicts(kld.tf.layer.default, args)

    ksize = kld.tf.layer.set_ksize(ksize, d)
    strides = kld.tf.layer.set_strides(strides, d)

    shapein = kld.tf.shape(input)
    wgts_shape = ksize + [shapein[d + 1], channels]
    bias_shape = [channels]

    with tf.variable_scope(name_or_scope=name, reuse=tf.AUTO_REUSE):

        wgts = kld.tf.variable(wgts_shape, 'wgts', 'wgts', **args)
        bias = kld.tf.variable(bias_shape, 'bias', 'bias', **args)

        input = kld.apply(input, args['prev'])
        output = fn(input=input,
                    filter=wgts,
                    strides=strides,
                    padding=args['padding'])
        output = tf.add(output, bias)
        output = kld.apply(output, args['post'])

    return output
Beispiel #2
0
def augment( images , labels ):
    actions = [ ( kld.img.fliplr ) ,
                ( kld.img.fliptb ) ,
                ( kld.img.rotate , { 'angle' : 90  } ) ,
                ( kld.img.rotate , { 'angle' : 180 } ) ,
                ( kld.img.rotate , { 'angle' : 270 } ) ]
    nimages , nlabels = images.copy() , labels.copy()
    for act in actions:
        nimages += kld.apply( images , act )
        nlabels += kld.apply( labels , act )
    return nimages , nlabels
def deconv( input , name , channels , ksizes , convs , ops1 = None , ops2 = None ):
    scale , deconvs = kld.tf.shape( convs ) , []
    for ksize in ksizes:
        deconv = kld.tf.layer.rsconv2d( input , name + str( ksize ) , channels , ksize , scale )
        deconvs.append( deconv )
    deconvs = tf.concat( deconvs , axis = 3 )
    deconvs = kld.apply( deconvs , ops1 )
    deconvs = tf.concat( [ deconvs , convs ] , axis = 3 )
    deconvs = kld.tf.layer.rsconv2d( deconvs , name + 'OUT' , channels , 1 , scale )
    deconvs = kld.apply( deconvs , ops2 )
    return deconvs
def conv( input , name , channels , ksizes , strides , ops1 = None , ops2 = None ):
    convs = []
    for ksize in ksizes:
        conv = kld.tf.layer.conv2d( input , name + str( ksize ) , channels , ksize , strides )
        convs.append( conv )
    if strides == 2:
        conv = kld.tf.layer.conv2d( input , name + 'MP' , channels , 1 , 1 )
        convs.append( kld.apply( conv , pool ) )
    convs = tf.concat( convs , axis = 3 )
    convs = kld.apply( convs , ops1 )
    convs = kld.tf.layer.conv2d( convs , name + 'OUT' , channels , 1 , 1 )
    convs = kld.apply( convs , ops2 )
    return convs
    def evaluate_loop( self , data , epoch , caption , draw_flag ):

        losses = 0

        data.reset()
        for i in self.loopEval( data , caption ):
            input = data.next_batch()
            small = kld.apply( input , self.resize_small )

            losses += self.sess.run( self.loss , { self.x0 : input , self.x1 : small } )

            if draw_flag:
                for k in range( len( input ) ):
                    ik = i * data.batch_size() + k
                    file = '%03d_%04d' % ( ik , epoch )
                    folder = 'evolution/%03d' % ( ik )

                    pars0test = self.sess.run( self.pars0out , { self.x0 : small } )
                    pars1test = self.sess.run( self.pars1out , { self.x1 : small } )

                    out0  = self.sess.run( self.out0 , { self.x0 : input } )[0]
                    out20 = self.sess.run( self.out2 , { self.x2 : input , self.pars2in : pars0test } )[0]
                    out21 = self.sess.run( self.out2 , { self.x2 : input , self.pars2in : pars1test } )[0]

                    full0 , full20 , full21 = self.reconstruct( input[0] , small[0] , pars0test , pars1test )

                    self.saver.image( self.phase , out0   , file + '_out0'   , folder )
                    self.saver.image( self.phase , out20  , file + '_out20'  , folder )
                    self.saver.image( self.phase , out21  , file + '_out21'  , folder )
                    self.saver.image( self.phase , full0  , file + '_full0'  , folder )
                    self.saver.image( self.phase , full20 , file + '_full20' , folder )
                    self.saver.image( self.phase , full21 , file + '_full21' , folder )

        return losses / data.num_batches()
Beispiel #6
0
def load(file, color=None, ops=None):
    if kld.chk.is_lst(file):
        if kld.chk.is_lst(color):
            return [
                kld.img.load(file[i], color[i], ops) for i in range(len(file))
            ]
        else:
            return [kld.img.load(f, color, ops) for f in file]
    else:
        flt = color is not None and color[-1] == 'f'
        norm = color is not None and color[-1] == 'n'
        if norm or flt: color = color[:-1]
        if color in ['bgr', 'rgb', 'hsv', 'lab']:
            mode = cv2.IMREAD_COLOR
        elif color in ['gray']:
            mode = cv2.IMREAD_GRAYSCALE
        else:
            mode = cv2.IMREAD_UNCHANGED

        image = cv2.imread(file, mode)

        if flt or norm: image = image.astype(np.float32)
        if norm: image /= 255.0

        if color == 'rgb': image = image[:, :, ::-1]
        if color == 'hsv': image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
        if color == 'lab': image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)

        return kld.apply(image, ops)
Beispiel #7
0
def colors( images ):
    actions = [ ( kld.img.convert , { 'map' : 'rgb2hsv' } ) ,
                ( kld.img.convert , { 'map' : 'rgb2lab' } ) ]
    nimages = images.copy()
    for act in actions:
        temp = kld.apply( images , act )
        for i in range( len( images ) ):
            nimages[i] = np.concatenate( [ nimages[i] , temp[i] ] , axis = 2 )
    return nimages
    def optimize_loop( self , data , epoch ):

        data.reset( shuffle = True )
        for _ in self.loopEpoch( data , epoch ):
            content = data.next_batch()
            content = kld.img.load( content , 'rgbn' , self.resize_input )
            small = kld.apply( content , self.resize_small )
            self.sess.run( self.optim.run , { self.x0 : content , self.x1 : small ,
                                              self.optim.lrate : self.optim.LRate.next() } )
def dense(input, name, channels, **args):

    name = kld.lst.merge_str(name)
    args = kld.aux.merge_dicts(kld.tf.layer.default, args)

    shapein = kld.tf.shape(input)
    wgts_shape = [shapein[1], channels]
    bias_shape = [channels]

    with tf.variable_scope(name_or_scope=name, reuse=tf.AUTO_REUSE):

        wgts = kld.tf.variable(wgts_shape, 'wgts', 'wgts', **args)
        bias = kld.tf.variable(bias_shape, 'bias', 'bias', **args)

        input = kld.apply(input, args['prev'])
        output = tf.matmul(input, wgts)
        output = tf.add(output, bias)
        output = kld.apply(output, args['post'])

    return output
Beispiel #10
0
def load( file , resize1 , resize2 ):
    input = kld.img.load( file , 'rgbn' , resize1 )
    small = kld.apply( input , resize2 )
    return input , small