Beispiel #1
0
    def test(self, args):
        self.preTest()

        size, pad = 256, 32
        model_name = kld.basename(args.model_dir)
        suffix = '%s_%d.jpg' % (model_name, args.image_test['size'])

        self.load_model()
        files = kld.get_dir_files(args.input_dir)
        for file in files:

            print('%d - %s' % (args.image_test['size'], file))

            file_name = kld.basename(file)[:-4]
            file_dir = '%s/%s' % (args.input_dir, file_name)
            kld.make_dir(file_dir)

            input = self.load_image(file, args.image_test)
            h, w, c = input.shape
            n = int(np.ceil(max(h, w) / size))
            #            small = scipy.misc.imresize( input , 1.0 / n , interp = 'nearest' )
            small = input

            output = self.sess.run(self.yh,
                                   feed_dict={
                                       self.x: input,
                                       self.xs: small
                                   })

            path = '%s/full_%s_%s' % (file_dir, file_name, suffix)
            kld.save_image(output, path)
Beispiel #2
0
    def test(self, args):
        self.preTest()

        model_name = kld.basename(args.model_dir)
        suffix = '%s_%d.jpg' % (model_name, args.image_test['size'])

        self.load_model()
        files = kld.get_dir_files(args.input_dir)
        for file in files:

            print('%d - %s' % (args.image_test['size'], file))

            file_name = kld.basename(file)[:-4]
            file_dir = '%s/%s' % (args.input_dir, file_name)
            kld.make_dir(file_dir)

            input = self.load_image(file, args.image_test)

            size, pad = 256, 128
            h, w, c = input.shape
            n = int(np.ceil(max(h, w) / size))
            hs, ws = int(h / n), int(w / n)
            canvas = np.zeros(input.shape)
            for i in range(0, h, hs):
                for j in range(0, w, ws):

                    hst, hfn = i, i + hs
                    wst, wfn = j, j + ws

                    hstp, hfnp, wstp, wfnp = 0, 0, 0, 0
                    if i > 0: hstp -= pad
                    if j > 0: wstp -= pad
                    if i < n - 1: hfnp += pad
                    if j < n - 1: wfnp += pad

                    input_ij = input[hst + hstp:hfn + hfnp,
                                     wst + wstp:wfn + wfnp, :]
                    output_ij = self.sess.run(self.yh,
                                              feed_dict={self.x: input_ij})
                    canvas[hst:hfn, wst:wfn, :] = output_ij[-hstp:hs - hstp,
                                                            -wstp:ws - wstp, :]

            path = '%s/split_%s_%s' % (file_dir, file_name, suffix)
            kld.save_image(canvas, path)

            output = self.sess.run(self.yh, feed_dict={self.x: input})
            path = '%s/%s_%s' % (file_dir, file_name, suffix)
            kld.save_image(output, path)

        self.store_model('fast_style_transfer')
    def __init__(self, args):
        self.preInit()

        self.args = args
        self.sess = tf.Session()
        self.load_network()

        args.image_content = kld.prepare_image_dict(args.image_content)
        args.image_test = kld.prepare_image_dict(args.image_test)

        self.ycL = kld.get_dir_files(args.content_dir)
        args.num_iters = len(self.ycL) // args.batch_size

        self.build(args)
        self.test(args)
Beispiel #4
0
    def __init__(self, args):
        self.preInit()

        self.args = args
        self.sess = tf.Session()
        self.load_network()

        args.image_style = kld.prepare_image_dict(args.image_style)
        args.image_content = kld.prepare_image_dict(args.image_content)

        self.ysL = self.load_image(args.style_dir, args.image_style)
        self.ycL = kld.get_dir_files(args.content_dir)
        args.num_iters = len(self.ycL) // args.batch_size

        self.style_layers = kld.ordered_sorted_dict(args.style_layer_ids)
        self.content_layers = kld.ordered_sorted_dict(args.content_layer_ids)

        self.build(args)
        self.train(args)
Beispiel #5
0
    def test( self , args ):
        self.preTest()

        model_name = kld.basename( args.model_dir )
        suffix = '%s_%d.jpg' % ( model_name , args.image_test['size'] )

        self.load_model()

        files = kld.get_dir_files( args.input_dir )
        for file in files:

            print( '%d - %s' % ( args.image_test['size'] , file ) )

            file_name = kld.basename( file )[:-4]
            file_dir = '%s/%s' % ( args.input_dir , file_name )
            kld.make_dir( file_dir )

            input = self.load_image( file , args.image_test )
            size , pad = 256 , 32
            h , w , c = input.shape
            n = int( np.ceil( max( h , w ) / size ) )
            hs , ws = int( h / n ) , int( w / n )

            import scipy
            small = scipy.misc.imresize( input , 1.0 / n , interp = 'nearest' )
            pars = self.sess.run( args.net.pars_calc , feed_dict = { self.x : small } )

            for p in pars:
                print( p.shape )
                print( p )

            pars_dict = {}
            for i in range( len( pars ) ):
                pars_dict[args.net.pars_eval[i]] = pars[i]
            output = self.sess.run( self.yh , feed_dict = { **{ self.x : input } , **pars_dict } )

            path = '%s/full_%s_%s' % ( file_dir , file_name , suffix )
            kld.save_image( output , path )
Beispiel #6
0
    def test(self, args):
        self.preTest()

        model_name = kld.basename(args.model_dir)
        suffix = '%s_%d.jpg' % (model_name, args.image_test['size'])

        self.load_model()
        files = kld.get_dir_files(args.input_dir)
        for file in files:

            print('%d - %s' % (args.image_test['size'], file))

            file_name = kld.basename(file)[:-4]
            file_dir = '%s/%s' % (args.input_dir, file_name)
            kld.make_dir(file_dir)

            input = self.load_image(file, args.image_test)
            output = self.sess.run(self.yh, feed_dict={self.x: input})

            path = '%s/%s_%s' % (file_dir, file_name, suffix)
            kld.save_image(output, path)

        self.store_model('fast_style_transfer')
    def test(self, args):
        self.preTest()

        size1, size2, pad = 1024, 256, 32
        model_name = kld.basename(args.model_dir)
        suffix = '%s_%d.jpg' % (model_name, args.image_test['size'])

        yc = np.zeros([args.batch_size] + args.image_content['shape'],
                      dtype=np.float32)

        yc0 = self.load_image(self.ycL[0], args.image_content)
        hc, wc, cc = yc0.shape
        n1c = int(np.ceil(max(hc, wc) / size1))
        n2c = int(np.ceil(max(hc, wc) / size2))

        yc0 = self.load_image(self.ycL[0], args.image_test)
        ht, wt, ct = yc0.shape
        n1t = int(np.ceil(max(ht, wt) / size1))
        n2t = int(np.ceil(max(ht, wt) / size2))

        self.load_model()
        self.sess.run(self.op_updt)
        for epoch in range(0, args.num_epochs):

            kld.shuffle_list(self.ycL)
            lr = self.calc_learn_rate(epoch)

            files = kld.get_dir_files(args.input_dir)
            for file in files:

                print('%d - %s' % (args.image_test['size'], file))

                file_name = kld.basename(file)[:-4]
                file_dir = '%s/%s' % (args.input_dir, file_name)
                kld.make_dir(file_dir)

                input = self.load_image(file, args.image_test)

                small1 = scipy.misc.imresize(input,
                                             1.0 / n1t,
                                             interp='nearest')
                output1 = self.sess.run(self.yh1,
                                        feed_dict={
                                            self.x: [input],
                                            self.xs1: [small1]
                                        })
                path = '%s/full4A_%02d_%s_%s' % (file_dir, epoch, file_name,
                                                 suffix)
                kld.save_image(output1, path)

                small2 = scipy.misc.imresize(input,
                                             1.0 / n2t,
                                             interp='nearest')
                output2 = self.sess.run(self.yh2,
                                        feed_dict={
                                            self.x: [input],
                                            self.xs2: [small2]
                                        })
                path = '%s/full4B_%02d_%s_%s' % (file_dir, epoch, file_name,
                                                 suffix)
                kld.save_image(output2, path)

                print(input.shape)
                print(small1.shape)
                print(small2.shape)
                print(n1c, n2c, n1t, n2t)

            for iter in range(args.num_iters):

                print(epoch, args.num_epochs, iter, args.num_iters)

                yc_small1, yc_small2 = [], []
                curr, last = self.next_idxs(iter)
                for j, path in enumerate(self.ycL[curr:last]):
                    yc[j] = self.load_image(path, args.image_content)
                    yc_small1.append(
                        scipy.misc.imresize(yc[j], 1.0 / n1c,
                                            interp='nearest'))
                    yc_small2.append(
                        scipy.misc.imresize(yc[j], 1.0 / n2c,
                                            interp='nearest'))

                self.sess.run(
                    [self.optim],
                    feed_dict={
                        self.x: yc,
                        self.xs1: yc_small1,
                        self.xs2: yc_small2,
                        self.lr: lr
                    })
    def test(self, args):
        self.preTest()

        model_name = kld.basename(args.model_dir)
        suffix = '%s_%d.jpg' % (model_name, args.image_test['size'])

        self.load_model()

        files = kld.get_dir_files(args.input_dir)
        for file in files:

            print('%d - %s' % (args.image_test['size'], file))

            file_name = kld.basename(file)[:-4]
            file_dir = '%s/%s' % (args.input_dir, file_name)
            kld.make_dir(file_dir)

            input = self.load_image(file, args.image_test)
            size, pad = 256, 32
            h, w, c = input.shape
            n = int(np.ceil(max(h, w) / size))
            hs, ws = int(h / n), int(w / n)

            print('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA')

            #            print( input.shape )

            #            arr = []
            #            i = 0
            #            while i < input.shape[0]:
            #                arr.append( i )
            #                if ( i + 1 ) % 4 == 0 : i += 13
            #                else: i += 1
            #            arr = np.asarray( arr )
            #            small = input[ arr , : , : ]

            #            print( small.shape )

            #            arr = []
            #            i = 0
            #            while i < input.shape[1]:
            #                arr.append( i )
            #                if ( i + 1 ) % 4 == 0 : i += 13
            #                else: i += 1
            #            arr = np.asarray( arr )
            #            small = small[ : , arr , : ]

            #            print( small.shape )

            #            pars = self.sess.run( args.net.pars , feed_dict = { self.x : small } )

            import scipy
            small = scipy.misc.imresize(input, 1.0 / n, interp='nearest')

            #            arr = np.arange( 2 , input.shape[0] , 4 )
            #            small = input[ arr , : , : ]
            #            arr = np.arange( 2 , input.shape[1] , 4 )
            #            small = small[ : , arr , : ]

            pars = self.sess.run(args.net.pars_calc, feed_dict={self.x: small})

            #            for i in range( len( pars ) ):
            #                if i % 2 == 1: pars[i] = np.sqrt( pars[i] * n * n )

            #            h2 , w2 = int( h / 2 ) , int( w / 2 )
            #            hs2 , ws2 = int( hs / 2 ) , int( ws / 2 )
            #            pars = self.sess.run( args.net.pars , feed_dict = { self.x : input[ h2 - hs2 : h2 + hs2 ,
            #                                                                                w2 - ws2 : w2 + ws2 , : ] } )

            #            small = input[ hs:2*hs , ws:2*ws , : ]
            #            pars = self.sess.run( args.net.pars , feed_dict = { self.x : small } )

            print('BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB')

            #            pars2 = self.sess.run( args.net.pars , feed_dict = { self.x : input } )

            print('CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC')

            #            pars3 = []
            #            for i in range( n ):
            #                for j in range( n ):
            #                    input_ij = input[ i * hs : ( i + 1 ) * hs ,
            #                                      j * ws : ( j + 1 ) * ws , : ]
            #                    tmp = self.sess.run( args.net.pars , feed_dict = { self.x : input_ij } )
            #                    if len( pars3 ) == 0:
            #                        pars3 = tmp
            ##                        for k in range( len( pars3 ) ):
            ##                            if k % 2 == 1: pars3[k] = np.sqrt( pars3[k] )
            #                    else:
            #                        for k in range( len( pars3 ) ):
            #                            if k % 2 == 0: pars3[k] += tmp[k]
            #                            if k % 2 == 1: pars3[k] += tmp[k]
            #            for k in range( len( pars3 ) ):
            #                if k % 2 == 0: pars3[k] /= n ** 2
            #                if k % 2 == 1: pars3[k] /= n ** 2

            print('DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD')

            #            pars = pars2
            #            for i in range( len( pars ) ):
            #                if i % 2 == 0: pars[i] = pars3[i]
            #                if i % 2 == 1: pars[i] = pars3[i]

            #            [ print( p.shape ) for p in pars ]

            print(len(pars))
            print(len(args.net.pars_eval))

            pars_dict = {}
            for i in range(len(pars)):
                pars_dict[args.net.pars_eval[i]] = pars[i]

#            for i in range( 1 , len(pars) , 2 ):
#                print( '###############################################' , i )
#                print( pars[i] / pars2[i] )

            print('EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE')

            canvasfull = np.zeros(input.shape)

            hst, hfn, wst, wfn = 0, 0, 0, 0

            #            for i in range( n ):

            #                for j in range( n ):

            #                    hst , hfn = i * hs , ( i + 1 ) * hs
            #                    wst , wfn = j * ws , ( j + 1 ) * ws

            #                    hstp = 0 if i == 0     else - pad
            #                    wstp = 0 if j == 0     else - pad
            #                    hfnp = 0 if i == n - 1 else   pad
            #                    wfnp = 0 if j == n - 1 else   pad

            #                    input_ij = input[ hst + hstp : hfn + hfnp , wst + wstp : wfn + wfnp , : ]
            #                    output_ij = self.sess.run( self.yh , feed_dict = { **{ self.x : input_ij } , **pars_dict } )

            #                    canvasfull[ hst : hfn , wst : wfn , : ] = output_ij[ - hstp : hs - hstp , - wstp : ws - wstp , : ]

            #                    canvas = np.zeros( input.shape )
            #                    canvas[ hst + hstp : hfn + hfnp , wst + wstp : wfn + wfnp , : ] = output_ij

            #                    path = '%s/split_%2d-%2d_%s_%s' % ( file_dir , i , j , file_name , suffix )
            #                    kld.save_image( canvas , path )

            #            path = '%s/fullsplit_%s_%s' % ( file_dir , file_name , suffix )
            #            kld.save_image( canvasfull , path )

            output = self.sess.run(self.yh,
                                   feed_dict={
                                       **{
                                           self.x: input
                                       },
                                       **pars_dict
                                   })

            path = '%s/full_%s_%s' % (file_dir, file_name, suffix)
            kld.save_image(output, path)