예제 #1
0
파일: main.py 프로젝트: KongBOy/kong_model
def main(_):
    if not os.path.exists(args.checkpoint_dir):
        os.makedirs(args.checkpoint_dir)
    if not os.path.exists(args.sample_dir):
        os.makedirs(args.sample_dir)
        # os.makedirs(args.sample_dir +"/A")
        # os.makedirs(args.sample_dir +"/B")

        ### 因為寫在function裡會一直被呼叫到,所以我才拉出來main寫喔!
        # os.makedirs(args.sample_dir +"/to_curved/big")
        # os.makedirs(args.sample_dir +"/to_curved/big-left-top")
        # os.makedirs(args.sample_dir +"/to_curved/small-seen")
        # os.makedirs(args.sample_dir +"/to_curved/small-unseen")
        # os.makedirs(args.sample_dir +"/to_straight/big")
        # os.makedirs(args.sample_dir +"/to_straight/big-left-top")
        # os.makedirs(args.sample_dir +"/to_straight/small-seen")
        # os.makedirs(args.sample_dir +"/to_straight/small-unseen")

        ### 因為寫在function裡會一直被呼叫到,所以我才拉出來main寫喔!

        os.makedirs(args.sample_dir + "/to_straight/crop-accurate")

    if not os.path.exists(args.test_dir):
        os.makedirs(args.test_dir)

    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    with tf.Session(config=tfconfig) as sess:
        model = cyclegan(sess, args)  ### 記得要進去改 建立模型的地方

        # model.train(args) if args.phase == 'train' else model.test(args)                       ### 有時間待修,有CYCLE的版本
        model.train_kong(args) if args.phase == 'train' else model.test(
            args)  ### 無Cycle,有D,D有concat
예제 #2
0
def main(_):
    if args.phase == 'train':
        args.checkpoint_dir = os.path.join(args.checkpoint_dir, args.exp_id)
        args.sample_dir = os.path.join(args.sample_dir, args.exp_id)
        args.log_dir = os.path.join(args.log_dir, args.exp_id)
        if not os.path.exists(args.log_dir):
            print(args.log_dir)
            os.mkdir(args.log_dir)
        if not os.path.exists(args.checkpoint_dir):
            os.makedirs(args.checkpoint_dir)
        if not os.path.exists(args.sample_dir):
            os.makedirs(args.sample_dir)

        with open(os.path.join(args.log_dir, 'training_config.txt'), 'w') as f:
            dic = vars(args)
            pp = pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=f)
            pp.pprint(dic)
    else:
        if not os.path.exists(args.test_dir):
            os.makedirs(args.test_dir)
        with open(os.path.join(args.test_dir, 'config.txt'), 'w') as f:
            dic = vars(args)
            pp = pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=f)
            pp.pprint(dic)

    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    with tf.Session(config=tfconfig) as sess:
        model = cyclegan(sess, args)
        model.train(args) if args.phase == 'train' \
            else model.test(args)
예제 #3
0
def load_cg():
    global cyclegan_model

    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True

    sess = tf.Session(config=tfconfig)

    cyclegan_model = cyclegan(sess, args)
    cyclegan_model.init_load(args)
예제 #4
0
def main(_):
    if not os.path.exists(args.checkpoint_dir):
        os.makedirs(args.checkpoint_dir)

    with tf.Session() as sess:
        model = cyclegan(sess, args)
        if args.phase == 'train':
            model.train(args)
        else:
            model.test(args)
def main(_):
    if not os.path.exists(args.checkpoint_dir):
        os.makedirs(args.checkpoint_dir)
    if not os.path.exists(args.sample_dir):
        os.makedirs(args.sample_dir)
    if not os.path.exists(args.test_dir):
        os.makedirs(args.test_dir)

    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    with tf.Session(config=tfconfig) as sess:
        model = cyclegan(sess, args)
        model.train(args) if args.phase == 'train' else model.test(args)
예제 #6
0
def main(_):
    if not os.path.exists(args.checkpoint_dir):
        os.makedirs(args.checkpoint_dir)
    if not os.path.exists(args.sample_dir):
        os.makedirs(args.sample_dir)
    if not os.path.exists(args.test_dir):
        os.makedirs(args.test_dir)

    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    with tf.Session(config=tfconfig) as sess:
        model = cyclegan(sess, args)
        model.train(args) if args.phase == 'train' \
            else model.test(args)
예제 #7
0
def modification():

    r = request
    # convert string of image data to uint8s
    #nparr = np.fromstring(r.data, np.uint8)
    # decode image
    #f = r.files['file']
    file = r.files['upload']
    img = Image.open(file)
    img.save('./datasets/makeup2/testA/input.jpg')
    #f = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
    #file.save(f)
    #f.save(secure_filename(f.filename))

    #img = cv2.imdecode(nparr,cv2.IMREAD_COLOR)
    '''
    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    with tf.Session(config=tfconfig) as sess:
        model = cyclegan(sess, args)
        if model.load(args.checkpoint_dir):
            print(" [*] Load SUCCESS")
        else:
            print(" [!] Load failed...")
        init_op = tf.global_variables_initializer()
        model.sess.run(init_op)
        out_var, in_var = (model.testB, model.test_A)
        img = np.expand_dims(img, axis=0)
        img = np.array(img).astype(np.float32)
        fake_img = model.sess.run(out_var, feed_dict={in_var: img})

    save_images(fake_img, [1, 1], "./output.jpg")
    ## merge images
    '''

    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    with tf.Session(config=tfconfig) as sess:
        model = cyclegan(sess, args)
        model.test(args)

    response = {'message': 'image received. size={}x{}'.format(1, 1)}
    print(response)
    # encode response using jsonpickle
    response_pickled = jsonpickle.encode(response)

    return Response(response=response_pickled,
                    status=200,
                    mimetype="application/json")
예제 #8
0
def main(_):
    if not os.path.exists(args.checkpoint_dir):
        os.makedirs(args.checkpoint_dir)
    if not os.path.exists(args.sample_dir):
        os.makedirs(args.sample_dir)
    if not os.path.exists(args.test_dir):
        os.makedirs(args.test_dir)

    with tf.Session() as sess:

        if args.debug == True:
            sess = tf_debug.LocalCLIDebugWrapperSession(sess)
            sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan)

        model = cyclegan(sess, args)
        # model.trainVAE(args) # for pretraining VAE
        model.train(args)  # training full network
예제 #9
0
def main(_):
    if not os.path.exists(args.checkpoint_dir):
        os.makedirs(args.checkpoint_dir)
    if not os.path.exists(args.sample_dir):
        os.makedirs(args.sample_dir)
    if not os.path.exists(args.test_dir):
        os.makedirs(args.test_dir)

    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    # tfconfig.gpu_options.per_process_gpu_memory_fraction = 0.8

    #saver = tf.train.Saver()

    with tf.Session(config=tfconfig) as sess:
        model = cyclegan(sess, args)
        model.train(args) if args.phase == 'train' \
            else model.test(args)
예제 #10
0
def main(_):
    if not os.path.exists(args.checkpoint_dir):
        os.makedirs(args.checkpoint_dir)
    if not os.path.exists(args.sample_dir):
        os.makedirs(args.sample_dir)
    if not os.path.exists(args.test_dir):
        os.makedirs(args.test_dir)

    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    with tf.Session(config=tfconfig) as sess:
        model = cyclegan(sess, args)
        if args.phase == 'train':
            print('Training start')
            model.train(args)
        elif args.phase == 'test':
            model.test(args)
        elif args.phase == 'visualize':
            model.visualize(args.sample_dir, 0, True, args)
예제 #11
0
def main(_):
    #if(not os.path.exists('/data1/ICE_DATA/{}'.format(args.dataset_dir))):
        # download it
    #    download_dataset(data_set=args.dataset_dir)

    if not os.path.exists(args.checkpoint_dir):
        os.makedirs(args.checkpoint_dir)
    if not os.path.exists(args.sample_dir):
        os.makedirs(args.sample_dir)
    if not os.path.exists(args.test_dir):
        os.makedirs(args.test_dir)

    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    with tf.Session(config=tfconfig) as sess:
        with tf.device('/device:GPU:1'):
            model = cyclegan(sess, args)
            model.train(args) if args.phase == 'train' \
                else model.test(args)
예제 #12
0
def main(_):
    if not os.path.exists(args.checkpoint_dir):
        os.makedirs(args.checkpoint_dir)
    if not os.path.exists(args.sample_dir):
        os.makedirs(args.sample_dir)
    smoothenes_seperated = str(args.smootheness).split('.')
    model_dir = "%s_%s_%s_%s_%s" % (args.dataset_dir, args.fine_size, args.h_hops, smoothenes_seperated[0], smoothenes_seperated[1])
    if not os.path.exists(args.sample_dir+"/"+model_dir):
        os.makedirs(args.sample_dir+"/"+model_dir)
    if not os.path.exists(args.test_dir):
        os.makedirs(args.test_dir)

    with tf.device('/gpu:{}'.format(args.gpu)):
        tfconfig = tf.ConfigProto(allow_soft_placement=True)
        tfconfig.gpu_options.allow_growth = True
        with tf.Session(config=tfconfig) as sess:
            model = cyclegan(sess, args)
            model.train(args) if args.phase == 'train' \
                else model.test(args)
예제 #13
0
def main(_):
    if not os.path.exists(args.checkpoint_dir):
        os.makedirs(args.checkpoint_dir)
    if not os.path.exists(args.test_dir):
        os.makedirs(args.test_dir)

    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    with tf.Session(config=tfconfig) as sess:
        model = cyclegan(sess, args)

        #model 불러옴
        OPTIONS = namedtuple(
            'OPTIONS', 'batch_size image_size \
                              gf_dim df_dim output_c_dim is_training')
        model.options = OPTIONS._make(
            (args.batch_size, args.fine_size, args.ngf, args.ndf,
             args.output_nc, args.phase == 'train'))
        model._build_model()
        model.saver = tf.train.Saver()

        #for 문

        while (1):
            print('py ready')

            #동호오빠가 넘겨줄 부분 (0 : 파일이름, 1:확장자, 2:표정, 3: more 버튼이 눌렸는지)
            lines = input().split(',')
            args.new_file = lines[0] + '.' + lines[1]
            args.which_expression = lines[2]
            args.more_button = lines[3]
            args.new_file_name = lines[0] + '_result.' + lines[1]
            if lines[0] == 99:
                break

            if model.load():
                print(" [*] Load SUCCESS")
            else:
                print(" [*] Load Failed...")
            model.test(args)
예제 #14
0
    ### analysis
    gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    for i in range(1):
        gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        if gray_img.mean() < 130:
            img = adjust_gamma(img, 1.5)
        else:
            break
    return img


if video == 'v':
    tfconfig = tf.ConfigProto(allow_soft_placement=True)
    tfconfig.gpu_options.allow_growth = True
    with tf.Session(config=tfconfig) as sess:
        model = cyclegan(sess, args2)
        model.loadModel()
        vc = cv2.VideoCapture('./data/video3.mp4')
        length = int(vc.get(cv2.CAP_PROP_FRAME_COUNT))
        print('length :', length)
        if args["with_draw"] == 'True':
            cv2.namedWindow('show', 0)
        dir_name = str(now.year) + "_" + str(now.month) + "_" + str(
            now.day) + "_" + str(now.minute) + "_" + str(
                now.second)  #+ "_" + str(idx)
        os.makedirs(dir_name)
        print(dir_name)
        for idx in range(length):
            img_bgr = vc.read()[1]
            if img_bgr is None:
                break
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

score = model.evaluate(x_train, y_train.reshape(-1, ), verbose=0)
print('svhn Train loss:', score[0])
print('svhn Train accuracy:', score[1])
score = model.evaluate(x_test, y_test, verbose=0)
print('svhn Test loss:', score[0])
print('svhn Test accuracy:', score[1])

print("begining cycle gan.....")

with tf.Session(config=tfconfig) as sess:
    domain_adapation_model = cyclegan(sess, args)
    x_test_c = np.zeros(x_test.shape)
    batch_size = 2
    batch_num = int(x_test.shape[0] / batch_size)
    for idx in range(batch_num):
        print(idx)
        x_test_c[idx * batch_size:(idx + 1) *
                 batch_size] = domain_adapation_model.pix2pix_cylce_gan(
                     args, x_test[idx * batch_size:(idx + 1) * batch_size])
    score = model.evaluate(x_test_c, y_test, verbose=0)
    print('svhn Test loss after cycle gan:', score[0])
    print('svhn Test accuracy after cycle gan:', score[1])
    #x_train_c = domain_adapation_model.pix2pix_cylce_gan(args, x_train)
    #score = model.evaluate(x_train_c, y_train, verbose=0)
    #print('svhn Train loss after cycle gan:', score[0])
    #print('svhn Train accuracy after cycle gan:', score[1])