Esempio n. 1
0
def test(opt):
    exp_name = opt.exp_name + '/' + os.path.basename(
        opt.datapath)[:-4] + '_' + opt.exp_id

    with open(os.path.join(opt.outdir, exp_name, 'config.yml'), 'r') as f:
        cfg = yaml.load(f)
        for k, v in cfg.items():
            if not hasattr(opt, k):
                setattr(opt, k, v)

    real_img = Image.open(opt.datapath).convert('RGB')
    w, h = real_img.size
    if w < h:
        h_resize = min(opt.load_size, h)
        w_resize = round(w * h_resize / h)
    else:
        w_resize = min(opt.load_size, w)
        h_resize = round(h * w_resize / w)

    real_img = img2arr(real_img, (w_resize, h_resize))

    real_images, nets, _, _ = build_models(real_img, opt, train=False)

    ckpt_dir = os.path.join(opt.outdir, exp_name, 'checkpoints')
    test_dir = os.path.join(opt.outdir, exp_name, 'sr_exp')

    gpu_options = tf.GPUOptions(visible_device_list=str(opt.gpu_id),
                                allow_growth=True)
    config = tf.ConfigProto(gpu_options=gpu_options)
    with tf.Session(config=config) as sess:

        load_scale_ind = -3
        load_net = 'ebm{}x{}'.format(real_images[load_scale_ind].shape[1],
                                     real_images[load_scale_ind].shape[0])
        netE = nets[load_net]

        saver = tf.train.Saver()
        saver.restore(sess, os.path.join(ckpt_dir, load_net + '.ckpt'))
        os.makedirs(test_dir, exist_ok=True)

        write_image(real_img, os.path.join(test_dir, 'init_scale.png'))
        num_scales = int(math.log(opt.up_scale, 1. / opt.scale_factor))

        cur_img = real_img
        final_shape = cur_img.shape[1] * opt.up_scale, cur_img.shape[
            0] * opt.up_scale
        write_image(imresize(real_img, new_shape=final_shape),
                    os.path.join(test_dir, 'bicubic.png'))

        for scale_ind in range(num_scales):
            if scale_ind == num_scales - 1:
                cur_img = imresize(cur_img, new_shape=final_shape)
            else:
                cur_img = imresize(cur_img, 1. / opt.scale_factor)
            cur_img = test_single_scale(sess, netE, cur_img, test_dir, opt)
            write_image(
                cur_img,
                os.path.join(
                    test_dir, 'syn{}x{}.png'.format(cur_img.shape[0],
                                                    cur_img.shape[1])))
def sample(hps, sess):
    if hps.label_file is not None:
        label_list, total_classes = build_label_list_from_file(hps.label_file)
        if hps.conditional_type != "acgan":
            label_list = None
        class_latent_str = input("Enter %d class values (comma separated):" % total_classes)
        if class_latent_str == "":
            class_latent_str = "1." + ",0."*(total_classes-1)
        class_latent = [float(v) for v in class_latent_str.split(",")]
        tiled_class_latent_batch = [class_latent] * int(hps.batch_size)
        tiled_class_latent_many = [class_latent] * 10000
    else:
        label_list = None
        total_classes = 0
        tiled_class_latent_batch = None
        tiled_class_latent_many = None
    _, mapping_network, _, sampling_model = build_models(hps,
                                                         hps.current_res_w,
                                                         use_ema_sampling=True,
                                                         num_classes=total_classes,
                                                         label_list=label_list)

    sample_latent = tf.random_normal([int(hps.batch_size), 512], 0., 1.)
    if hps.map_cond:
        sample_latent = tf.concat([sample_latent, tiled_class_latent_batch], axis=-1)

    many_latent = tf.random_normal([10000, 512], 0., 1)
    if hps.map_cond:
        many_latent = tf.concat([many_latent, tiled_class_latent_many], axis=-1)
    average_w = tf.reduce_mean(mapping_network(many_latent), axis=0)
    intermediate_w = average_w + hps.psi_w*(mapping_network(sample_latent) - average_w)
    sample_img = sampling_model(1., intermediate_ws=intermediate_w)
    restore_models_and_optimizers(sess, None, None, mapping_network,
                                  sampling_model, None, None, None, hps.save_paths)
    return sample_img, intermediate_w
def sample_style_mix(hps, sess, mix_layer):
    tiled_class_latent_batch = None
    tiled_class_latent_many = None
    _, mapping_network, _, sampling_model = build_models(hps,
                                                         hps.current_res_w,
                                                         use_ema_sampling=True)

    sample_latent1 = tf.random_normal([int(hps.batch_size), 512], 0., 1.)
    if hps.map_cond:
        sample_latent1 = tf.concat([sample_latent1, tiled_class_latent_batch], axis=-1)
    sample_latent2 = tf.random_normal([int(hps.batch_size), 512], 0., 1.)
    if hps.map_cond:
        sample_latent2 = tf.concat([sample_latent2, tiled_class_latent_batch], axis=-1)

    many_latent = tf.random_normal([10000, 512], 0., 1)
    if hps.map_cond:
        many_latent = tf.concat([many_latent, tiled_class_latent_many], axis=-1)
    average_w = tf.reduce_mean(mapping_network(many_latent), axis=0)
    intermediate_w1 = average_w + hps.psi_w*(mapping_network(sample_latent1) - average_w)
    sample_img1 = sampling_model(1., intermediate_ws=intermediate_w1)
    intermediate_w2 = mapping_network(sample_latent2) #average_w + hps.psi_w*(mapping_network(sample_latent2) - average_w)
    sample_img2 = sampling_model(1., intermediate_ws=intermediate_w2)
    sample_img_mix = sampling_model(1.,
                                    intermediate_ws=[intermediate_w1, intermediate_w2],
                                    crossover_list=[mix_layer])
    restore_models_and_optimizers(sess, None, None, mapping_network,
                                  sampling_model, None, None, None, hps.save_paths)
    return [sample_img1, sample_img2, sample_img_mix], [intermediate_w1, intermediate_w2]
Esempio n. 4
0
def save_sampling_graph(hps, save_paths, graph_dir):
    _, mapping_network, _, sampling_model = build_models(hps,
                                                         hps.current_res_w,
                                                         use_ema_sampling=True,
                                                         num_classes=0,
                                                         label_list=None)

    #sample_latent = tf.placeholder(tf.float32, shape=[1, 512], name="input_latent")  # tf.random_normal
    sample_latent = tf.random_normal([1, 512], 0., 1., name="z")
    intermediate_w = tf.identity(mapping_network(sample_latent), "w")
    sample_img_tensor = sampling_model(1., intermediate_ws=intermediate_w)
    saver = tf.train.Saver()
    with tf.Session() as sess:
        alpha = restore_models_and_optimizers_and_alpha(
            sess, None, None, mapping_network, sampling_model, None, None,
            None, save_paths)
        sample_img_tensor = tf.clip_by_value(
            sample_img_tensor, -1.,
            1.)  # essential due to how tf.summary.image scales values
        sample_img_tensor = tf.cast((sample_img_tensor + 1) * 127.5,
                                    tf.uint8,
                                    name="out")
        #png_tensor = tf.image.encode_png(tf.squeeze(sample_img_tensor, axis=0), name="output")
        tf.saved_model.simple_save(sess,
                                   graph_dir,
                                   inputs={'z': sample_latent},
                                   outputs={'out': sample_img_tensor})
    """
Esempio n. 5
0
def sample_with_intermediate(hps_path, intermediate, save_paths):
    with open(hps_path, "r") as f:
        hps_dict = json.load(f)
    hps = TrainHps(**hps_dict)
    print("******************************")
    print("Resolution (w): %d, Alpha %.02f" % (hps.current_res_w, 1.0))
    print("******************************")

    # if hps.label_file is not None:
    #     label_list, total_classes = build_label_list_from_file(hps.label_file)
    #     if hps.conditional_type != "acgan":
    #         label_list = None
    #     class_latent_str = input("Enter %d class values (comma separated):" % total_classes)
    #     if class_latent_str == "":
    #         class_latent_str = "1." + ",0."*(total_classes-1)
    #     class_latent = [float(v) for v in class_latent_str.split(",")]
    #     tiled_class_latent_batch = [class_latent]
    #     tiled_class_latent_many = [class_latent] * 10000
    # else:
    #     label_list = None
    #     total_classes = 0
    #     tiled_class_latent_batch = None
    #     tiled_class_latent_many = None
    _, _, _, sampling_model = build_models(hps,
                                           hps.current_res_w,
                                           use_ema_sampling=True,
                                           num_classes=0,
                                           label_list=None)

    sample_img_tensor = sampling_model(1., intermediate_ws=intermediate)
    with tf.Session() as sess:
        alpha = restore_models_and_optimizers_and_alpha(
            sess, None, None, None, sampling_model, None, None, None,
            save_paths)

        sample_img_tensor = tf.clip_by_value(
            sample_img_tensor, -1.,
            1.)  # essential due to how tf.summary.image scales values
        sample_img_tensor = tf.cast((sample_img_tensor + 1) * 127.5, tf.uint8)
        images = sess.run(sample_img_tensor)
    return images
Esempio n. 6
0
def predict_images():
    """
    Detects the drones in the image set given in project's path TO_PREDICT_PATH.
    """
    pred_list = os.listdir(TO_PREDICT_PATH)

    classifier, regressor = build_models()
    classifier.load_weights(CLASS_MODEL_PATH)
    regressor.load_weights(REGRESSOR_MODEL_PATH)
    (y_resized, x_resized) = UNIFORM_IMG_SIZE

    IoU_values = []
    for image in pred_list:
        sample_image1 = cv2.imread(TO_PREDICT_PATH + '/' + image)
        sample_image = sample_image1.astype(np.float32) / 255.0

        (y_sample_shape, x_sample_shape, _) = sample_image.shape
        sample_image_resized = cv2.resize(sample_image, UNIFORM_IMG_SIZE, interpolation=cv2.INTER_CUBIC)
        x_ratio = x_sample_shape / x_resized
        y_ratio = y_sample_shape / y_resized

        anchors_along_x = int((x_resized - FIRST_ANCHOR_X) / ANCHOR_STEP_X) + 1  # On scaled image
        anchors_along_y = int((y_resized - FIRST_ANCHOR_Y) / ANCHOR_STEP_Y) + 1  

        Drones = []
        Drones_marks = []

        boxes = []
        iterator = 0
        keras_input = []
        iterator_max = 100

        remaining_anchors = anchors_along_x * anchors_along_y * len(BOX_SCALES) * len(BOX_SIZES)
        print("Initial remaining anchors to iterate: ", remaining_anchors)

        for i in range(anchors_along_x):
            for j in range(anchors_along_y):
                for boxSize in BOX_SIZES:
                    for boxScale in BOX_SCALES:
                        width = round(boxSize * boxScale)
                        height = round(boxSize / boxScale)

                        anchor_x = FIRST_ANCHOR_X + i * ANCHOR_STEP_X
                        anchor_y = FIRST_ANCHOR_Y + j * ANCHOR_STEP_Y

                        box = cut_on_edges(UNIFORM_IMG_SIZE, [anchor_x - width/2, anchor_x + width/2, anchor_y - height/2, anchor_y + height/2])
                        boxes.append(box)

                        keras_input.append(cv2.resize(sample_image_resized[box[2]:box[3], box[0]:box[1], 0:3],
                                                      KERAS_IMG_SIZE,
                                                      interpolation=cv2.INTER_CUBIC))
                        iterator += 1

                        if iterator == min(remaining_anchors, iterator_max):
                            k = 0
                            classifier_pred = classifier.predict_on_batch(np.array(keras_input))
                            regressor_pred = regressor.predict_on_batch(np.array(keras_input))

                            for pred in classifier_pred:
                                if pred[1] >= 0.90:
                                    [box_x_min, box_x_max, box_y_min, box_y_max] = boxes[k]
                                    box_w = box_x_max - box_x_min 
                                    box_h = box_y_max - box_y_min

                                    [reg_box_x_min, reg_box_x_max, reg_box_y_min, reg_box_y_max] = \
                                        regressor_pred[k]
                                    reg_box_x_min *= box_w
                                    reg_box_x_max *= box_w
                                    reg_box_y_min *= box_h
                                    reg_box_y_max *= box_h

                                    reg_box_x_min += box_x_min
                                    reg_box_x_max += box_x_min
                                    reg_box_y_min += box_y_min
                                    reg_box_y_max += box_y_min

                                    Drones.append([reg_box_x_min, reg_box_x_max, reg_box_y_min, reg_box_y_max])
                                    Drones_marks.append(pred[1])
                                k += 1
                            keras_input = []
                            boxes = []
                            remaining_anchors -= iterator
                            iterator = 0
                            print("Remaining anchors to iterate: ", remaining_anchors)

        Drones = NMS(Drones, Drones_marks, IoU_threshold=0.1)

        # Draw the predicted rectangles
        for drone in Drones:
            drone = [int(drone[0] * x_ratio), int(drone[1] * x_ratio), int(drone[2] * y_ratio), int(drone[3] * y_ratio)]
            cv2.rectangle(sample_image1, (drone[0], drone[2]), (drone[1], drone[3]), (255, 0, 0), 2)

        labeled_anchor_boxes = get_label_anchor_box(image)
        for labeled_anchor_box in labeled_anchor_boxes:
            cv2.rectangle(sample_image1, (labeled_anchor_box[0], labeled_anchor_box[2]), (labeled_anchor_box[1], labeled_anchor_box[3]), (0, 255, 0), 2)
        cv2.imwrite(PREDICTED_PATH + '/' + image, sample_image1)

        # Calculate IoU values
        for drone in Drones:
            drone = [int(drone[0] * x_ratio), int(drone[1] * x_ratio), int(drone[2] * y_ratio), int(drone[3] * y_ratio)]
            current_IoU = []
            for labeled_anchor_box in labeled_anchor_boxes:
                current_IoU.append(calculate_IoU(drone, labeled_anchor_box))
            IoU_values.append(max(np.array(current_IoU)))

        print("Length of Drones: " + str(len(Drones)))
        print("Next image...")

    print("Mean IoU value: " + str(mean(np.array(IoU_values))))
    print("All images predicted!")