Beispiel #1
0
def swap_faces(image_a_path, image_b_path):
    image_a, image_b = load_images([
        os.path.join(IMAGES_FOLDER, 'trump', image_a_path),
        os.path.join(IMAGES_FOLDER, 'cage', image_b_path)]
    ) / 255.0

    image_a += images_B_mean - images_A_mean
    image_b += images_A_mean - images_B_mean

    # Preprocess loaded images
    image_a = cv2.resize(image_a, (64, 64))
    image_b = cv2.resize(image_b, (64, 64))

    image_a = toTensor(image_a).to(device).float()
    image_b = toTensor(image_b).to(device).float()

    # Forward with opposite encoders
    result_a = var_to_np(model(image_a, 'B'))
    result_b = var_to_np(model(image_b, 'A'))
    result_a = np.moveaxis(np.squeeze(result_a), 0, 2)
    result_b = np.moveaxis(np.squeeze(result_b), 0, 2)

    result_a = np.clip(result_a * 255, 0, 255).astype('uint8')
    result_b = np.clip(result_b * 255, 0, 255).astype('uint8')

    image_a_filename = os.path.splitext(image_a_path)[0]
    image_b_filename = os.path.splitext(image_b_path)[0]

    result_a_filename = f'{image_a_filename}-{image_b_filename}.jpg'
    result_b_filename = f'{image_b_filename}-{image_a_filename}.jpg'

    cv2.imwrite(os.path.join(SWAPS_FOLDER, result_a_filename), result_a)
    cv2.imwrite(os.path.join(SWAPS_FOLDER, result_b_filename), result_b)

    return result_a_filename, result_b_filename
Beispiel #2
0
    def read_eval_data(self, filenames):
        import util

        input_dir = ng_config.train_data_path
        data = util.load_images(filenames)
        data, _ = self.preprocess(data, None, None)
        return data
def split_imgs():
    frame_names = os.listdir(args.frame_dir)
    frame_indices = list(
        np.linspace(0, len(frame_names) - 1, num=16, dtype=np.int))
    selected_frames = [frame_names[i] for i in frame_indices]

    RGB_vid, vid = load_images(args.frame_dir, selected_frames)
    return RGB_vid, vid
def detect_image(model, args):

    print('Loading input image(s)...')
    input_size = [int(model.net_info['height']), int(model.net_info['width'])]
    batch_size = 1  #int(model.net_info['batch'])

    imlist, imgs = load_images(args.input)
    print('Input image(s) loaded')
    print("Loaded {} images".format(len(imgs)))

    img_batches = create_batches(imgs, batch_size)

    # load colors and classes
    colors = pkl.load(open("pallete", "rb"))
    classes = load_classes("cfg/garb.names")

    if not osp.exists(args.outdir):
        os.makedirs(args.outdir)

    start_time = datetime.now()
    print('Detecting...')

    detected_trush = []

    for batchi, img_batch in tqdm(enumerate(img_batches)):
        img_tensors = [cv_image2tensor(img, input_size) for img in img_batch]
        img_tensors = torch.stack(img_tensors)
        img_tensors = Variable(img_tensors)
        if args.cuda:
            img_tensors = img_tensors.cuda()
        detections = model(img_tensors, args.cuda).cpu()
        detections = process_result(detections, args.obj_thresh,
                                    args.nms_thresh)

        detected_trush.append(len(detections))
        if len(detections) == 0:
            continue

        detections = transform_result(detections, img_batch, input_size)

        for detection in detections:
            draw_bbox(img_batch, detection, colors, classes, 0, args.outdir)

        for i, img in enumerate(img_batch):
            save_path = osp.join(args.outdir,
                                 osp.basename(imlist[batchi * batch_size + i]))
            cv2.imwrite(save_path, img)
            print(save_path, 'saved')

    end_time = datetime.now()
    print('Detection finished in %s' % (end_time - start_time))

    return detected_trush
Beispiel #5
0
def detect_image(model, args):

    print('Loading input image(s)...')
    input_size = [int(model.net_info['height']), int(model.net_info['width'])]
    batch_size = int(model.net_info['batch'])

    imlist, imgs = load_images(args.input)
    print('Input image(s) loaded')

    img_batches = create_batches(imgs, batch_size)

    # load colors and classes
    colors = pkl.load(
        open(
            "/home/autonomous-car/Desktop/Autonomous_car_ros2/src/stereo_yolo3/stereo_yolo3/pallete",
            "rb"))
    classes = load_classes(
        "/home/autonomous-car/Desktop/Autonomous_car_ros2/src/stereo_yolo3/stereo_yolo3/data/coco.names"
    )

    if not osp.exists(args.outdir):
        os.makedirs(args.outdir)

    start_time = datetime.now()
    print('Detecting...')
    for batchi, img_batch in enumerate(img_batches):
        img_tensors = [cv_image2tensor(img, input_size) for img in img_batch]
        img_tensors = torch.stack(img_tensors)
        img_tensors = Variable(img_tensors)
        if args.cuda:
            img_tensors = img_tensors.cuda()
        detections = model(img_tensors, args.cuda).cpu()
        detections = process_result(detections, args.obj_thresh,
                                    args.nms_thresh)
        if len(detections) == 0:
            continue

        detections = transform_result(detections, img_batch, input_size)
        for detection in detections:
            draw_bbox(img_batch, detection, colors, classes)

        for i, img in enumerate(img_batch):
            save_path = osp.join(
                args.outdir,
                'dete_' + osp.basename(imlist[batchi * batch_size + i]))
            cv2.imwrite(save_path, img)

    end_time = datetime.now()
    print('Detection finished in %s' % (end_time - start_time))

    return
    def detect_image(self,path,colors=[(39, 129, 113), (164, 80, 133), (83, 122, 114)],classes=['container_small', 'garbage_bag', 'cardboard']):

        print('Loading input image(s)...')
        input_size = [int(self.model.net_info['height']), int(self.model.net_info['width'])]
        batch_size = int(self.model.net_info['batch'])

        imlist, imgs = load_images(path)
        print('Input image(s) loaded')

        img_batches = create_batches(imgs, batch_size)


        start_time = datetime.now()
        print('Detecting...')

        all_images_attributes = []

        for batchi, img_batch in enumerate(img_batches):
            img_tensors = [cv_image2tensor(img, input_size) for img in img_batch]
            img_tensors = torch.stack(img_tensors)
            img_tensors = Variable(img_tensors)
            if self.cuda:
                img_tensors = img_tensors.cuda()
            detections = self.model(img_tensors, self.cuda).cpu()
            detections = process_result(detections, self.obj_thresh, self.nms_thresh)
            if len(detections) == 0:
                continue

            detections = transform_result(detections, img_batch, input_size)

            boxes = []
            for detection in detections:
                boxes.append(create_output_json(img_batch, detection, colors, classes))

            images_attributes = {}
            images_attributes['image_meta'] = {'width':input_size[1],'height':input_size[0]}
            images_attributes['boxes'] = boxes

            images_attributes['counts'] = {x:0 for x in classes}
            images_attributes['counts']['total'] = 0
            
            for box in boxes:
                images_attributes['counts'][box['class']] +=1
                images_attributes['counts']['total'] +=1

            all_images_attributes.append(images_attributes)

        end_time = datetime.now()
        print('Detection finished in %s' % (end_time - start_time))
        return all_images_attributes
Beispiel #7
0
def test_rotate_image(image_path, item_size=(256, 256), scale=0.2, item_num=6):
    image_list = util.load_images(image_path)
    image = util.build_image(image_list, item_size, scale, item_num)
    pixels = image.reshape(-1, image.shape[2])
    unique_elements, counts_elements = np.unique(pixels, axis=0, return_counts=True)
    counts_elements = np.sort(counts_elements)[::-1]
    angles = [0, 90, 180, 270]
    while True:
        angle = angles[np.random.randint(0, len(angles))]
        rot_image = np.clip(image * 255, 0, 255).astype(np.uint8)
        rot_image = util.rotate_image(rot_image, angle)
        util.draw_text(rot_image, 'angle: %d' % angle, color=(255, 0, 0))
        cv2.imshow('image', rot_image)
        key = cv2.waitKey(1000000) & 0xFF
        if key == 27: # 'esc
            break
Beispiel #8
0
def detect_image(model, fname, i):

    print('Loading input image(s)...')
    input_size = [int(model.net_info['height']), int(model.net_info['width'])]
    batch_size = int(model.net_info['batch'])

    imlist, imgs = load_images(fname)
    print('Input image(s) loaded')

    img_batches = create_batches(imgs, batch_size)

    # load colors and classes
    colors = pkl.load(open("pallete", "rb"))
    classes = load_classes("darknet/cfg/trash.names")

    start_time = datetime.now()
    print('Detecting...')

    for batchi, img_batch in zip((1, 2), (3, 4)):
        # img_tensors = [cv_image2tensor(img, input_size) for img in rangeimg_batch]
        # img_tensors = torch.stack(img_tensors)
        # img_tensors = Variable(img_tensors)
        return model.new_detect(batchi, i)
        detections = model(img_tensors, False).cpu()
        detections = process_result(detections, 100, 102)
        if len(detections) == 0:
            continue

        detections = transform_result(detections, img_batch, input_size)

        for detection in detections:
            draw_bbox(img_batch, detection, colors, classes, 0, args.outdir)

        for i, img in enumerate(img_batch):
            save_path = osp.join(args.outdir,
                                 osp.basename(imlist[batchi * batch_size + i]))
            cv2.imwrite(save_path, img)
            print(save_path, 'saved')

    end_time = datetime.now()
    print('Detection finished in %s' % (end_time - start_time))

    return
Beispiel #9
0
def test_graph(network_def, model_file, test_folder):
    test_x, test_y = util.load_images(test_folder, letter=None)
    test_x = np.asarray(test_x, dtype=float)
    test_y = np.asarray(test_y)
    xsh = test_x.shape
    test_x = np.reshape(test_x, (xsh[0], xsh[1], xsh[2], 1))
    test_y = np.reshape(test_y, (len(test_y), 1), float)
    session = tf.Session()
    # session.run(init)
    saver = tf.train.import_meta_graph(model_file + ".meta")
    saver.restore(session, tf.train.latest_checkpoint('./'))
    graph = tf.get_default_graph()
    x = graph.get_tensor_by_name("i:0")
    y = graph.get_tensor_by_name("o:0")
    op_to_restore = graph.get_operation_by_name("accuracy-check")
    test_accuracy = session.run(op_to_restore,
                                feed_dict={
                                    x: test_x,
                                    y: test_y
                                })
    print 'Test accuracy:', test_accuracy
    session.close()
Beispiel #10
0
def test_random_atals(image_path, item_size=(256, 256), atlas_size=(1024, 1024), scale=0.2):
    grid = int(atlas_size[0] / item_size[0])
    image_list = util.load_images(image_path)
    while True:
        row_images = []
        for i in range(grid):
            images = []
            for j in range(grid):
                item_num = np.random.randint(1, 20)
                tmp_scale = scale + np.random.random() * 0.05
                img = util.build_image(image_list, item_size, tmp_scale, item_num)
                img = np.clip(img * 255, 0, 255).astype(np.uint8)
                util.draw_text(img, '%d' % item_num, color=(255, 255, 255))
                images.append(img)
            row_images.append(cv2.hconcat(images))
        image = cv2.vconcat(row_images)
        util.draw_text(image, 'press [esc] to exit, other key to refresh.', color=(255, 0, 0))
        cv2.imshow('image', image)
        key = cv2.waitKey(1000000) & 0xFF
        if key == 27: # 'esc
            break
        util.draw_text(image, 'building image, please wait...', pos=(10, 35), color=(0, 0, 255))
        cv2.imshow('image', image)
        cv2.waitKey(50)
Beispiel #11
0
def test(network_def, model_file, test_folder, letter):
    layer_def = util.load_layers_definition(network_def)
    cnn = create_cnn(layer_def)
    predict = tf.greater(cnn, threshold)
    correct_pred = tf.equal(predict, tf.equal(output_holder, 1.0))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32),
                              name="accuracy-check")
    session = tf.Session()
    saver = tf.train.Saver()
    saver.restore(session, model_file)

    test_x, test_y = util.load_images(test_folder, letter)
    test_x = np.asarray(test_x, dtype=float)
    test_y = np.asarray(test_y)
    xsh = test_x.shape
    test_x = np.reshape(test_x, (xsh[0], xsh[1], xsh[2], 1))
    test_y = np.reshape(test_y, (len(test_y), 1), float)

    test_accuracy = session.run(accuracy,
                                feed_dict={
                                    input_holder: test_x,
                                    output_holder: test_y
                                })
    print 'Test accuracy: ', test_accuracy
parser.add_argument(
    "--render",
    "--r",
    default=False,
    type=bool,
    help="if we should render progress",
)

args = parser.parse_args()


if __name__ == "__main__":
    swatch_directory = args.swatch_directory
    image_set_directory = args.train_directory

    images, _ = load_images(image_set_directory)
    image_swatches = extract_candidate_swatches(images, args.num_swatches, render_output=args.render)

    # clear output folder
    for filename in os.listdir(swatch_directory):
        os.remove(swatch_directory+filename)
    for i in range(len(image_swatches)):
        cv2.imwrite(swatch_directory + 'out_' + str(2*i) + '.jpg', image_swatches[i][0])
        cv2.imwrite(swatch_directory + 'out_' + str((2*i) + 1) + '.jpg', image_swatches[i][1])

    # with open('inter_data.json', 'w') as json_file:
    #     match_points = match_images(left_image, right_image, True)
    #     json.dump(match_points, json_file)

    # with open('inter_data.json') as json_file:
    #     match_points = json.load(json_file)
Beispiel #13
0
import combined
import util
import plotter
###
UPLOAD_FOLDER = 'uploads'
RESULTS = 'results'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['RESULTS'] = RESULTS

# zernike_database = util.load_obj('zernike_database_icon_50')
orb_database = util.load_obj('orb_database_icon_50')
sift_database = util.load_obj('sift_database_icon_50')
images = util.load_images("icon_sample")


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
Beispiel #14
0
    net = slim.conv2d(net, 256, [1, 1], stride=1)

    net = slim.max_pool2d(net, [2, 2], scope='pool5')

    net = slim.flatten(net, scope="flat1")

    net = slim.fully_connected(net, 1024, scope='fc1')
    net = slim.dropout(net, keep_prob, scope='drop1')
    net = slim.fully_connected(net, 1024, scope='fc2')
    net = slim.dropout(net, keep_prob, scope='drop2')
    net = slim.fully_connected(net, 2, activation_fn=None, scope='fc3')

    return net


pdata = load_images("t_pdata", 170)
plabel = create_label((pdata.shape[0], 2), 0)

ndata = load_images("t_ndata", 170)
nlabel = create_label((ndata.shape[0], 2), 1)

data = np.concatenate((pdata, ndata), 0)
label = np.concatenate((plabel, nlabel), 0)

print(data.shape)
print(label.shape)


def mobilenet(inputs):
    mobilenet_conv_defs = [{
        "kernel": [3, 3],
Beispiel #15
0
"""
Dataset:
http://vision.ucsd.edu/content/yale-face-database

TODO:
- Perform PCA
- Gen new data

"""

import numpy as np
import util
from train import train_random_projection

ESSEX_PATH = "../data/essexfaces/*"
ESSEX_SHAPE = (180, 200)

if __name__ == "__main__":
    images = util.load_images(ESSEX_PATH)
    images = train_random_projection(images, 16)
    print(images.shape)
    print(images[0])
    util.display_samples(images, 1, ESSEX_SHAPE)
def main():
    """The application's entry point.

    If someone executes this module (instead of importing it, for
    example), this function is called.
    """

    pygame.init()
    display_surface = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
    pygame.display.set_caption('Pygame Flappy Bird')
    score_font = pygame.font.SysFont(None, 32, bold=True)  # default font
    images = load_images()
    scores = list()
    active = True

    while active:
        clock = pygame.time.Clock()
        bird = Bird(50, int(WIN_HEIGHT / 2 - Bird.HEIGHT / 2), 2,
                    (images['bird-wingup'], images['bird-wingdown']))
        pipes = deque()
        frame_clock = 0
        score = 0

        # Current state
        current_state = None
        action = None
        reward = 0
        collision = False
        while True:
            action_on_this_tick = frame_clock % PICK_ACTION_EVERY == 0

            clock.tick(FPS)

            if action_on_this_tick or collision:
                next_state = GameState(bird, pipes)

                # Update the values for the last state (current_state)
                if (current_state is not None and action is not None):
                    # Get the value for the reward
                    if collision:
                        reward = REWARD_FOR_FAILURE
                    elif active and not collision:
                        reward = PICK_ACTION_EVERY * REWARD_SUCCESS_MULTIPLIER

                    # Log current state
                    with current_state.take_action(action) as u:
                        u.update_utility_value(next_state, reward)

                    # Now that we have updated the state, quit the game after a
                    # collision
                    if collision:
                        break

                    # Reset the action
                    action = None

                # The "next" state will by the "current" state next time around
                current_state = next_state

            if not frame_clock % msec_to_frames(PipePair.ADD_INTERVAL):
                pp = PipePair(images['pipe-end'], images['pipe-body'])
                pipes.append(pp)

            # Handle the user quitting the game
            for event in pygame.event.get():
                action = get_action_from_event(event)
                if action is Action.quit:
                    exit()

            if not action and action_on_this_tick:
                action = current_state.max_value_action
                if action is Action.flap:
                    bird.flap()

            for x in (0, WIN_WIDTH / 2):
                display_surface.blit(images['background'], (x, 0))

            while pipes and not pipes[0].visible:
                pipes.popleft()

            for p in pipes:
                p.update()
                display_surface.blit(p.image, p.rect)

            bird.update()
            display_surface.blit(bird.image, bird.rect)

            if bird.check_collisions(pipes):
                collision = True

            # update and display score
            for p in pipes:
                if p.x + PipePair.WIDTH < bird.x and not p.score_counted:
                    score += 1
                    p.score_counted = True

            score_surface = score_font.render(str(score), True,
                                              (255, 255, 255))
            score_x = WIN_WIDTH / 2 - score_surface.get_width() / 2
            display_surface.blit(score_surface,
                                 (score_x, PipePair.PIECE_HEIGHT))

            pygame.display.flip()
            frame_clock += 1
        scores.append(score)
        print('Game #{}:\t{}'.format(len(scores), score))
    print('Best score: %i' % max(scores))
    pygame.quit()
Beispiel #17
0
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
if args.cuda is True:
    print('===> Using GPU to train')
else:
    print('===> Using CPU to train')

torch.manual_seed(args.seed)
if args.cuda:
    torch.cuda.manual_seed(args.seed)

print('===> Loaing datasets')
images_A = get_image_paths("data/trump")
images_B = get_image_paths("data/cage")
images_A = load_images(images_A) / 255.0
images_B = load_images(images_B) / 255.0
images_A += images_B.mean(axis=(0, 1, 2)) - images_A.mean(axis=(0, 1, 2))

model = Autoencoder()

print('===> Try resume from checkpoint')
if os.path.isdir('checkpoint'):
    try:
        checkpoint = torch.load('./checkpoint/autoencoder.t7')
        model.load_state_dict(checkpoint['state'])
        start_epoch = checkpoint['epoch']
        print('===> Load last checkpoint data')
    except FileNotFoundError:
        print('Can\'t found autoencoder.t7')
else:
# -*- coding: utf-8 -*-
import os, sys, time
import numpy as np
from chainer import cuda, Variable
sys.path.append(os.path.split(os.getcwd())[0])
import util
from args import args
from model import conf, vae
from vae_m1 import GaussianM1VAE

dist = "bernoulli"
if isinstance(vae, GaussianM1VAE):
	dist = "gaussian"
dataset = util.load_images(args.train_image_dir, dist=dist)

max_epoch = 1000
num_trains_per_epoch = 2000
batchsize = 100
total_time = 0

for epoch in xrange(max_epoch):
	sum_loss = 0
	epoch_time = time.time()
	for t in xrange(num_trains_per_epoch):
		x = util.sample_x_variable(batchsize, conf.ndim_x, dataset, gpu_enabled=conf.gpu_enabled)

		# train
		loss = vae.train(x, L=1)

		sum_loss += loss
		if t % 10 == 0:
Beispiel #19
0
def main():
    pygame.init()
    window_size = (512, 512)
    screen = pygame.display.set_mode(
        window_size, pygame.OPENGL | pygame.DOUBLEBUF | pygame.RESIZABLE)

    #pygame.display.gl_set_attribute(pygame.GL_DEPTH_SIZE, 32)

    glCullFace(GL_BACK)

    glDepthFunc(GL_LEQUAL)
    glEnable(GL_DEPTH_TEST)

    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

    glClearColor(0.1, 0.2, 0.3, 1.0)

    shader = make_boring_shader()

    images = util.load_images("textures")
    textures = [texture_from_image(image) for image in images]

    #mesh = Mesh3D(shader, util.cube_vertex_positions, util.cube_texture_coordinates)
    meshes = []
    positions = util.split_n(util.cube_vertex_positions, 6)
    tex_coords = util.split_n(util.cube_texture_coordinates, 6)
    for i in range(6):
        mesh = Mesh3D(shader, textures[i], positions[i], tex_coords[i])
        meshes.append(mesh)

    clock = pygame.time.Clock()

    try:
        os.mkdir("frames")
    except FileExistsError:
        pass

    FPS = 60
    frame = 0
    while True:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.VIDEORESIZE:
                window_size = event.size
            if event.type == pygame.QUIT:
                return
            if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
                return

        mvp, model, view, projection = get_mvp(window_size)

        display(window_size, shader, meshes, mvp)

        pygame.display.flip()

        # save frame
        pygame.image.save(screen, "frames/%d.jpg" % frame)
        with open("matrices.npy", "ab") as f:
            np.save(f, model)
            np.save(f, view)
            np.save(f, projection)

        frame += 1
Beispiel #20
0
def main(_):
    if not FLAGS.dataset_dir:
        raise ValueError(
            'You must supply the dataset directory with --dataset_dir')

    root_result_folder = os.path.join(FLAGS.dataset_dir, "features_vectors")

    for dir in os.listdir(FLAGS.dataset_dir):
        dir_path = os.path.join(FLAGS.dataset_dir, dir)
        if not os.path.isdir(dir_path):
            continue

        tf.logging.set_verbosity(tf.logging.INFO)
        with tf.Graph().as_default():
            tf_global_step = slim.get_or_create_global_step(
            )  # ####################
            # # Select the model #
            # ####################
            network_fn = nets_factory.get_network_fn(FLAGS.model_name,
                                                     num_classes=NUM_CLASSES,
                                                     is_training=False)

            image_names, img = util.load_images(
                dir_path, FLAGS, FLAGS.eval_image_size
                or network_fn.default_image_size)
            if not image_names:
                continue

            logits, endpoints = network_fn(img)

            if FLAGS.moving_average_decay:
                variable_averages = tf.train.ExponentialMovingAverage(
                    FLAGS.moving_average_decay, tf_global_step)
                variables_to_restore = variable_averages.variables_to_restore(
                    slim.get_model_variables())
                variables_to_restore[tf_global_step.op.name] = tf_global_step
            else:
                variables_to_restore = slim.get_variables_to_restore()

            predictions = tf.argmax(logits, 1)

            # print(FLAGS.batch_size)
            num_batches = FLAGS.max_num_batches or 1

            if tf.gfile.IsDirectory(FLAGS.checkpoint_path):
                checkpoint_path = tf.train.latest_checkpoint(
                    FLAGS.checkpoint_path)
            else:
                checkpoint_path = FLAGS.checkpoint_path

            tf.logging.info('Evaluating %s' % checkpoint_path)

            before_fc_tensor = endpoints["PreLogitsFlatten"]
            before_fc = slim.evaluation.evaluate_once(
                master=FLAGS.master,
                checkpoint_path=checkpoint_path,
                logdir=FLAGS.eval_dir,
                num_evals=num_batches,
                final_op=before_fc_tensor,
                variables_to_restore=variables_to_restore)

            result_folder = os.path.join(root_result_folder, dir)
            if not os.path.exists(result_folder):
                os.makedirs(result_folder)
            for img_name, arr in zip(image_names, before_fc):
                np.save(os.path.join(result_folder, img_name), np.asarray(arr))
Beispiel #21
0
if args.cuda is True:
    print('===> Using GPU to train')
    device = torch.device('cuda:0')
    cudnn.benchmark = True
else:
    print('===> Using CPU to train')

torch.manual_seed(args.seed)
if args.cuda:
    torch.cuda.manual_seed(args.seed)

print('===> Loaing datasets')
images_A = get_image_paths("./face_A")#获取图片路径
images_B = get_image_paths("./face_B")
images_A = load_images(images_A) / 255.0#将图片加载到数组中,并将NumPy数组规范化到一定范围[0,255]内
images_B = load_images(images_B) / 255.0
images_A += images_B.mean(axis=(0, 1, 2)) - images_A.mean(axis=(0, 1, 2))#将图像A集加上两者图像集的平均差值(RGB三通道差值),axis=0,1,2表示在这三个轴上取平均
                                                                         #来使两个输入图像图像的分布尽可以相近,这样我们的损失函数曲线下降会更快些
model = Autoencoder().to(device)#定义模型

print('===> Try resume from checkpoint')
if os.path.isdir('checkpoint'):
    try:
        checkpoint = torch.load('./checkpoint/autoencoder.t7')
        model.load_state_dict(checkpoint['state'])
        start_epoch = checkpoint['epoch']
        print('===> Load last checkpoint data')
    except FileNotFoundError:
        print('Can\'t found autoencoder.t7')
else:
import matplotlib.patches as mpatches
sys.path.append(os.path.split(os.getcwd())[0])
import util
from args import args
from model import conf1, vae1, conf2, vae2
from vae_m1 import GaussianM1VAE

try:
	os.mkdir(args.vis_dir)
except:
	pass

dist = "bernoulli"
if isinstance(vae1, GaussianM1VAE):
	dist = "gaussian"
dataset = util.load_images(args.test_image_dir, dist=dist)

n_analogies = 10
n_image_channels = 1
image_width = 28
image_height = 28
x = util.sample_x_variable(n_analogies, conf1.ndim_x, dataset, gpu_enabled=conf1.gpu_enabled)
z1 = vae1.encoder(x, test=True)
y = vae2.sample_x_y(z1, test=True)
z2 = vae2.encode_xy_z(z1, y, test=True)

fig = pylab.gcf()
fig.set_size_inches(16.0, 16.0)
pylab.clf()
if n_image_channels == 1:
	pylab.gray()
Beispiel #23
0
    def __init__(self, n, p1, p2):

        # pygame initialization
        pygame.init()
        os.environ['SDL_VIDEO_CENTERED'] = '1'
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))

        # game initialization
        self.n = n
        self.player_1 = p1
        self.player_2 = p2
        self.running = True
        self.game_over = False

        # temporary variables to counter bugs
        # TODO: fix the bug
        self.target_trigger_temp = False  # used for a random bug, in create_map(), the coordinate is fetched twice

        # image loading stuff
        self.background = pygame.image.load("Assets/background/background.png").convert()
        self.player1_img = load_images("Assets/Sprite/flash/*.png")
        self.player2_img = load_images("Assets/Sprite/rev_flash/*.png")
        self.target_img_front = [pygame.image.load("Assets/Sprite/barry_mom/front1.png").convert(),
                                 pygame.image.load("Assets/Sprite/barry_mom/front3.png").convert()]
        self.target_img_back = [pygame.image.load("Assets/Sprite/barry_mom/back1.png").convert(),
                                pygame.image.load("Assets/Sprite/barry_mom/back2.png").convert()]
        self.end_game_images = [pygame.image.load("Assets/background/p1_wins.png").convert(),
                                pygame.image.load("Assets/background/p2_wins.png").convert(),
                                pygame.image.load("Assets/background/p1_lost.png").convert(),
                                pygame.image.load("Assets/background/p2_lost.png").convert()]

        # map and wall stuff
        self.walls = []
        self.map_data = []
        self.walls_coordinate_vector = []  # holds the vector2 object of all wall coordinates
        self.g = WeightedGrid(SCREEN_WIDTH, SCREEN_HEIGHT)
        self.g.walls = self.walls_coordinate_vector
        self.create_map()

        # barry's mom.
        # TODO: optimize code using an array
        self.target_img_back_index = 0
        self.target_img_front_index = 0
        self.target_up = False
        self.target_down = True
        self.target_upper_limit = self.target_pos.y
        self.target_lower_limit = self.target_pos.y + (30 * 2)  # barry's mom will walk up to 3 cells downwards

        # enemy stuff
        enemy_pos = spawn_enemy(self.map_data)

        self.goal = make_vector(self.player_1.rect)

        self.start = make_vector(enemy_pos)
        self.path = dijkstra_search(self.g, self.goal, self.start)

        # spawns enemy and it immediately starts to follow player on a different thread
        self.enemy = Enemy(enemy_pos, make_vector((self.player_1.rect.x, self.player_1.rect.y)), self.g, self.walls)

        # music stuff
        pygame.mixer.music.load('Assets/music/flash_theme.wav')
        pygame.mixer.music.play(-1)

        self.game_loop()
Beispiel #24
0
print('loading elapsed time ' + str(elapsed) + ' second')

# process
cache_tbs = Queue(maxsize=QUEUE_MAXSIZE)
# start a thread to handle image saver
t = Thread(target=util.save_images_fast, args=(
    cache_tbs,
    args.output_dir,
))
t.setDaemon(True)
t.start()
count = 0
every_n = 10
all_images_taget_class = util.load_target_class(args.input_dir)
for filenames, images in util.load_images(args.input_dir,
                                          batch_shape,
                                          max_number=max_number):
    count += 1
    if count % every_n == 0:
        start_time_loop = timeit.default_timer()

    target_cls = ([all_images_taget_class[n]
                   for n in filenames] + [0] * (batch_size - len(filenames)))

    sess.run(init_ops, feed_dict={img: images, dense_labels: target_cls})
    for _ in xrange(itr):
        sess.run([train_op])
        if emphasize is not None:
            sess.run([train_op_eph])
    out_imgs = sess.run(img_input)
import os, sys, time
import numpy as np
from chainer import cuda, Variable

sys.path.append(os.path.abspath(os.path.join(os.getcwd(), "../../")))
import util
from args import args
from model import conf, aae
import sampler

max_epoch = 1000
num_trains_per_epoch = 1000
batchsize = 100
n_steps_to_optimize_dis = 1

dataset = util.load_images(args.train_image_dir)


def sample_data():
    return util.sample_x_variable(batchsize,
                                  conf.ndim_x,
                                  dataset,
                                  gpu_enabled=conf.gpu_enabled)


total_time = 0
for epoch in xrange(1, max_epoch + 1):

    sum_loss_autoencoder = 0
    sum_loss_discriminator = 0
    sum_loss_generator = 0
Beispiel #26
0
import matplotlib.patches as mpatches
sys.path.append(os.path.split(os.getcwd())[0])
import util
from args import args
from model import conf, vae
from vae_m2 import GaussianM2VAE

try:
	os.mkdir(args.vis_dir)
except:
	pass

dist = "bernoulli"
if isinstance(vae, GaussianM2VAE):
	dist = "gaussian"
dataset = util.load_images(args.test_image_dir, dist=dist)

n_analogies = 10
n_image_channels = 1
image_width = 28
image_height = 28
x = util.sample_x_variable(10, conf.ndim_x, dataset, gpu_enabled=conf.gpu_enabled)
y = vae.sample_x_y(x, test=True)
z = vae.encode_xy_z(x, y, test=True)

fig = pylab.gcf()
fig.set_size_inches(16.0, 16.0)
pylab.clf()
if n_image_channels == 1:
	pylab.gray()
xp = np
Beispiel #27
0
    #op.name gives you the name
    #输入,输出结点也是operation,所以,我们可以得到operation的名字
    for op in graph.get_operations():
        print(op.name, op.values())
        # prefix/Placeholder/inputs_placeholder
        # ...
        # prefix/Accuracy/predictions
    #操作有:prefix/Placeholder/inputs_placeholder
    #操作有:prefix/Accuracy/predictions
    #为了预测,我们需要找到我们需要feed的tensor,那么就需要该tensor的名字
    #注意prefix/Placeholder/inputs_placeholder仅仅是操作的名字,prefix/Placeholder/inputs_placeholder:0才是tensor的名字
    x = graph.get_tensor_by_name('prefix/Placeholder:0')
    y = graph.get_tensor_by_name('prefix/softmax/Softmax:0')
    keep_prob = graph.get_tensor_by_name('prefix/Placeholder_2:0')

    v_pdata = load_images("t_pdata", 100)
    v_plabel = create_label((v_pdata.shape[0], 2), 0)

    v_ndata = load_images("t_ndata", 100)
    v_nlabel = create_label((v_ndata.shape[0], 2), 1)

    v_data = np.concatenate((v_pdata, v_ndata), 0)
    v_label = np.concatenate((v_plabel, v_nlabel), 0)

    with tf.Session(graph=graph) as sess:
        s = 0
        for j in range(10):
            ds = 10 * j
            de = 10 * (j + 1)
            #s = s + accuracy.eval(feed_dict={x:v_data[ds:de], y_: v_label[ds:de], keep_prob: 1.0})
            out = sess.run(y, feed_dict={x: v_data[ds:de], keep_prob: 1.0})
Beispiel #28
0
def main():
    print("Starting..")
    output_path = constants.SAVE_PATH
    if not os.path.exists(output_path):
        os.makedirs(output_path)
        print("Made output directory")
    else:
        print("WARNING: starting training with an existing outputs directory")
    if not os.path.exists(output_path + 'weights/'):
        os.makedirs(output_path + 'weights/')
        print("Made weights directory")
    if not os.path.exists(output_path + 'images/'):
        os.makedirs(output_path + 'images/')
        print("Made images directory")
    model_options = constants.MAIN_MODEL_OPTIONS
    # Load map mapping examples to their train/dev/test split
    dataset_map = util.load_dataset_map()
    print("Loading data")
    # Load the caption text vectors
    train_captions, val_captions, test_captions = util.load_text_vec(
        'Data', constants.VEC_OUTPUT_FILE_NAME, dataset_map)

    # Loads and separates images into train, dev, and test sets
    if os.path.exists(constants.FLOWERS_DICTS_PATH):
        image_dicts = torch.load(constants.FLOWERS_DICTS_PATH)
        train_image_dict, val_image_dict, test_image_dict = image_dicts
        print("Loaded images")
    else:
        print("Loading images and separating into train/val/test sets")
        filenames = train_captions.keys() + val_captions.keys(
        ) + test_captions.keys()
        train_image_dict, val_image_dict, test_image_dict = util.load_images(
            'Data/' + constants.DIRECTORY_PATH, filenames, dataset_map)
        image_dicts = [train_image_dict, val_image_dict, test_image_dict]
        torch.save(image_dicts, "Data/flowers_dicts.torch")

    # Creates the model
    if constants.USE_MODEL == 'began':
        generator = CondBeganGenerator(model_options)
        discriminator = CondBeganDiscriminator(model_options)
    elif constants.USE_MODEL == 'wgan':
        generator = WGanGenerator(model_options)
        discriminator = WGanDiscriminator(model_options)
    else:
        generator = Generator(model_options)
        discriminator = Discriminator(model_options)

    # Put G and D on cuda if GPU available
    if torch.cuda.is_available():
        print("CUDA is available")
        generator = generator.cuda()
        discriminator = discriminator.cuda()
        print("Moved models to GPU")

    # Initialize weights
    generator.apply(util.weights_init)
    discriminator.apply(util.weights_init)

    g_optimizer = optim.Adam(generator.parameters(),
                             lr=constants.LR,
                             betas=constants.BETAS)
    # Changes the optimizer to SGD if declared in constants
    if constants.D_OPTIMIZER_SGD:
        d_optimizer = optim.SGD(discriminator.parameters(), lr=constants.LR)
    else:
        d_optimizer = optim.Adam(discriminator.parameters(),
                                 lr=constants.LR,
                                 betas=constants.BETAS)

    print("Added optimizers")

    new_epoch = 0
    train_losses = {"generator": [], "discriminator": []}
    val_losses = {"generator": [], "discriminator": []}
    losses = {'train': train_losses, 'val': val_losses}
    if args.resume:
        print("Resuming from epoch " + args.resume)
        checkpoint = torch.load(constants.SAVE_PATH + 'weights/epoch' +
                                str(args.resume))
        new_epoch = checkpoint['epoch'] + 1
        generator.load_state_dict(checkpoint['g_dict'])
        discriminator.load_state_dict(checkpoint['d_dict'])
        if constants.USE_MODEL == 'began':
            discriminator.began_k = checkpoint['began_k']
        g_optimizer.load_state_dict(checkpoint['g_optimizer'])
        d_optimizer.load_state_dict(checkpoint['d_optimizer'])
        losses = torch.load(constants.SAVE_PATH + 'losses')

    # TODO: MAKE SURE IMAGES ARE OF DIMENSIONS (BATCHSIZE, CHANNELS, H, W)
    # TODO: ADD L1/L2 Regularizaiton
    # TODO: USE DATALOADER FROM TORCH UTILS!!!!!!!!!
    # data_loader = DataLoader(self.dataset, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers)
    # TODO: ADD PARALLELIZATION
    # TODO: ADD IMAGE PREPROCESSING? DO WE NEED TO SUBTRACT/ADD ANYTHING TO IMAGES
    # TODO: Add image aug

    # NOTE: CREATING VARIABLES EARLY, THEN FILL IN LATER
    noise_vec = torch.FloatTensor(constants.BATCH_SIZE, model_options['z_dim'],
                                  1, 1)
    g_text_des = torch.FloatTensor(constants.BATCH_SIZE,
                                   model_options['caption_vec_len'])
    real_img = torch.FloatTensor(constants.BATCH_SIZE, constants.IMAGE_SIZE,
                                 constants.IMAGE_SIZE)
    real_caption = torch.FloatTensor(constants.BATCH_SIZE,
                                     model_options['caption_vec_len'])
    if constants.USE_CLS:
        wrong_img = torch.FloatTensor(constants.BATCH_SIZE,
                                      constants.IMAGE_SIZE,
                                      constants.IMAGE_SIZE)
        wrong_caption = torch.FloatTensor(constants.BATCH_SIZE,
                                          model_options['caption_vec_len'])

    # Add cuda GPU option
    if torch.cuda.is_available():
        noise_vec = noise_vec.cuda()
        g_text_des = g_text_des.cuda()
        real_img = real_img.cuda()
        real_caption = real_caption.cuda()
        if constants.USE_CLS: wrong_img = wrong_img.cuda()

    # Number of total iterations
    num_iterations = 0

    # Loop over dataset N times
    for epoch in range(new_epoch, constants.NUM_EPOCHS):
        print("Epoch %d" % (epoch))
        st = time.time()

        # WGAN trains D number of times more than G
        curr_count = 0
        if constants.USE_MODEL == 'wgan':
            if num_iterations < 25 or num_iterations % 500 == 0:
                d_iters = 100
            else:
                d_iters = model_options['wgan_d_iter']

        for i, batch_iter in enumerate(
                grouper(train_captions.keys(), constants.BATCH_SIZE)):
            batch_keys = [x for x in batch_iter if x is not None]
            curr_batch_size = len(batch_keys)

            discriminator.train()
            generator.train()
            # Zero out gradient
            discriminator.zero_grad()

            # Save computations for gradient calculations
            for p in discriminator.parameters():
                p.requires_grad = True  # Need this to be true to update generator as well

            # Gather batch data
            noise_batch = torch.randn(curr_batch_size, model_options['z_dim'],
                                      1, 1)
            g_text_des_batch = torch.Tensor(
                get_text_description(train_captions, batch_keys))
            real_caption_batch = torch.Tensor(
                get_text_description(train_captions, batch_keys))
            real_img_batch = torch.Tensor(
                choose_real_image(train_image_dict, batch_keys))
            if constants.USE_CLS:
                wrong_img_batch = torch.Tensor(
                    choose_wrong_image(train_image_dict, batch_keys))
            if torch.cuda.is_available():
                noise_batch = noise_batch.cuda()
                g_text_des_batch = g_text_des_batch.cuda()
                real_caption_batch = real_caption_batch.cuda()
                real_img_batch = real_img_batch.cuda()
                if constants.USE_CLS:
                    wrong_img_batch = wrong_img_batch.cuda()

            # Fill in tensors with batch data
            noise_vec.resize_as_(noise_batch).copy_(noise_batch)
            g_text_des.resize_as_(g_text_des_batch).copy_(g_text_des_batch)
            real_caption.resize_as_(g_text_des_batch).copy_(g_text_des_batch)
            real_img.resize_as_(real_img_batch).copy_(real_img_batch)
            if constants.USE_CLS:
                wrong_img.resize_as_(wrong_img_batch).copy_(wrong_img_batch)

            # Run through generator
            gen_image = generator.forward(
                Variable(g_text_des),
                Variable(noise_vec))  # Returns tensor variable holding image

            # Run through discriminator
            real_img_passed = discriminator.forward(Variable(real_img),
                                                    Variable(real_caption))
            fake_img_passed = discriminator.forward(gen_image.detach(),
                                                    Variable(real_caption))
            if constants.USE_CLS:
                wrong_img_passed = discriminator.forward(
                    Variable(wrong_img), Variable(real_caption))

            ##### Train Discriminator #####
            # calc_grad_d calcs gradients and steps backward
            if constants.USE_MODEL == 'began':
                if constants.USE_CLS:
                    d_loss = discriminator.calc_grad_d(Variable(real_img),
                                                       real_img_passed,
                                                       gen_image,
                                                       fake_img_passed,
                                                       Variable(wrong_img),
                                                       wrong_img_passed)
                else:
                    d_loss = discriminator.calc_grad_d(Variable(real_img),
                                                       real_img_passed,
                                                       gen_image,
                                                       fake_img_passed)
            else:
                if constants.USE_CLS:
                    d_loss = discriminator.calc_grad_d(real_img_passed,
                                                       fake_img_passed,
                                                       wrong_img_passed)
                else:
                    d_loss = discriminator.calc_grad_d(real_img_passed,
                                                       fake_img_passed)

            d_optimizer.step()

            # WGAN trains D number of times more than G
            if constants.USE_MODEL == 'wgan':
                if curr_count < d_iters and i < (len(train_captions) /
                                                 constants.BATCH_SIZE) - 1:
                    curr_count += 1
                    num_iterations += 1
                    continue
                else:
                    # Update G after d iterations or after reaching end of epoch
                    curr_count = 0

            ##### Train Generator #####
            for p in discriminator.parameters():
                p.requires_grad = False

            generator.zero_grad()

            # Generate image again if you want to
            if constants.REGEN_IMAGE:
                noise_batch = torch.randn(curr_batch_size,
                                          model_options['z_dim'], 1, 1)
                if torch.cuda.is_available():
                    noise_batch = noise_batch.cuda()
                noise_vec.resize_as_(noise_batch).copy_(noise_batch)

            gen_image = generator.forward(Variable(g_text_des),
                                          Variable(noise_vec))
            new_fake_img_passed = discriminator.forward(
                gen_image, Variable(real_caption))

            if constants.USE_MODEL == 'began':
                g_loss = generator.calc_grad_g(gen_image, new_fake_img_passed)
            else:
                g_loss = generator.calc_grad_g(new_fake_img_passed)

            g_optimizer.step()

            # learning rate decay
            if constants.USE_MODEL == 'began':
                g_optimizer = adjust_learning_rate(g_optimizer, num_iterations)
                d_optimizer = adjust_learning_rate(d_optimizer, num_iterations)

            if i % constants.LOSS_SAVE_IDX == 0:
                losses['train']['generator'].append((g_loss.data[0], epoch, i))
                losses['train']['discriminator'].append(
                    (d_loss.data[0], epoch, i))
            num_iterations += 1

        print('Total number of iterations: ', num_iterations)
        print('Training G Loss: ', g_loss.data[0])
        print('Training D Loss: ', d_loss.data[0])
        epoch_time = time.time() - st
        print("Time: ", epoch_time)

        if epoch == constants.REPORT_EPOCH:
            with open(constants.SAVE_PATH + 'report.txt', 'w') as f:
                f.write(constants.EXP_REPORT)
                f.write("Time per epoch: " + str(epoch_time))
                copyfile("constants.py", constants.SAVE_PATH + 'constants.py')
            print("Saved report")
        '''
        DEV SET
        '''
        # Calculate dev set loss
        # Volatile is true because we are running in inference mode (no need to calculate gradients)
        generator.eval()
        discriminator.eval()
        for i, batch_iter in enumerate(
                grouper(val_captions.keys(), constants.BATCH_SIZE)):
            batch_keys = [x for x in batch_iter if x is not None]
            curr_batch_size = len(batch_keys)

            # Gather batch data
            noise_batch = torch.randn(curr_batch_size, model_options['z_dim'],
                                      1, 1)
            g_text_des_batch = torch.Tensor(
                get_text_description(val_captions, batch_keys))
            real_caption_batch = torch.Tensor(
                get_text_description(val_captions, batch_keys))
            real_img_batch = torch.Tensor(
                choose_real_image(val_image_dict, batch_keys))
            if constants.USE_CLS:
                wrong_img_batch = torch.Tensor(
                    choose_wrong_image(val_image_dict, batch_keys))
            if torch.cuda.is_available():
                noise_batch = noise_batch.cuda()
                g_text_des_batch = g_text_des_batch.cuda()
                real_caption_batch = real_caption_batch.cuda()
                real_img_batch = real_img_batch.cuda()
                if constants.USE_CLS:
                    wrong_img_batch = wrong_img_batch.cuda()

            # Fill in tensors with batch data
            noise_vec.resize_as_(noise_batch).copy_(noise_batch)
            g_text_des.resize_as_(g_text_des_batch).copy_(g_text_des_batch)
            real_caption.resize_as_(g_text_des_batch).copy_(g_text_des_batch)
            real_img.resize_as_(real_img_batch).copy_(real_img_batch)
            if constants.USE_CLS:
                wrong_img.resize_as_(wrong_img_batch).copy_(wrong_img_batch)

            # Run through generator
            gen_image = generator.forward(Variable(
                g_text_des, volatile=True), Variable(
                    noise_vec,
                    volatile=True))  # Returns tensor variable holding image

            # Run through discriminator
            real_img_passed = discriminator.forward(
                Variable(real_img, volatile=True),
                Variable(real_caption, volatile=True))
            fake_img_passed = discriminator.forward(
                gen_image.detach(), Variable(real_caption, volatile=True))
            if constants.USE_CLS:
                wrong_img_passed = discriminator.forward(
                    Variable(wrong_img, volatile=True),
                    Variable(real_caption, volatile=True))

            # Calculate D loss
            if constants.USE_MODEL == 'began':
                if constants.USE_CLS:
                    d_loss = discriminator.loss(Variable(real_img),
                                                real_img_passed, gen_image,
                                                fake_img_passed,
                                                Variable(wrong_img),
                                                wrong_img_passed)
                else:
                    d_loss = discriminator.loss(Variable(real_img),
                                                real_img_passed, gen_image,
                                                fake_img_passed)
            elif constants.USE_MODEL == 'wgan':
                if constants.USE_CLS:
                    d_loss, d_real_loss, d_fake_loss, d_wrong_loss = discriminator.loss(
                        real_img_passed, fake_img_passed, wrong_img_passed)
                else:
                    d_loss, d_real_loss, d_fake_loss = discriminator.loss(
                        real_img_passed, fake_img_passed)
            # Vanilla Model
            else:
                if constants.USE_CLS:
                    d_loss = discriminator.loss(real_img_passed,
                                                fake_img_passed,
                                                wrong_img_passed)
                else:
                    d_loss = discriminator.loss(real_img_passed,
                                                fake_img_passed)

            # Calculate G loss
            if constants.USE_MODEL == 'began':
                g_loss = generator.loss(gen_image, fake_img_passed)
            else:
                g_loss = generator.loss(fake_img_passed)

            if i % constants.LOSS_SAVE_IDX == 0:
                losses['val']['generator'].append((g_loss.data[0], epoch, i))
                losses['val']['discriminator'].append(
                    (d_loss.data[0], epoch, i))

        print('Val G Loss: ', g_loss.data[0])
        print('Val D Loss: ', d_loss.data[0])

        # Save losses
        torch.save(losses, constants.SAVE_PATH + 'losses')

        # Save images
        vutils.save_image(gen_image[0].data.cpu(),
                          constants.SAVE_PATH + 'images/gen0_epoch' +
                          str(epoch) + '.png',
                          normalize=True)
        vutils.save_image(gen_image[1].data.cpu(),
                          constants.SAVE_PATH + 'images/gen1_epoch' +
                          str(epoch) + '.png',
                          normalize=True)
        if constants.USE_MODEL == 'began':
            vutils.save_image(real_img_passed[0].data.cpu(),
                              constants.SAVE_PATH +
                              'images/real_recon0_epoch' + str(epoch) + '.png',
                              normalize=True)
            vutils.save_image(real_img_passed[1].data.cpu(),
                              constants.SAVE_PATH +
                              'images/real_recon1_epoch' + str(epoch) + '.png',
                              normalize=True)
        # Save model
        if epoch % 20 == 0 or epoch == constants.NUM_EPOCHS - 1:
            save_checkpoint = {
                'epoch': epoch,
                'g_dict': generator.state_dict(),
                'd_dict': discriminator.state_dict(),
                'g_optimizer': g_optimizer.state_dict(),
                'd_optimizer': d_optimizer.state_dict(),
            }
            if constants.USE_MODEL == 'began':
                save_checkpoint['began_k'] = discriminator.began_k
            torch.save(save_checkpoint,
                       constants.SAVE_PATH + 'weights/epoch' + str(epoch))
Beispiel #29
0
def train(cost, network_description, epsilon, max_updates, class_letter,
          model_file_name, train_folder_name):
    layer_def = util.load_layers_definition(network_description)
    cnn = create_cnn(layer_def)
    # y_predict = tf.nn.softmax(cnn, name='y_predict')
    # y_predict = tf.nn.sigmoid(cnn, name='y_predict')

    # cost_func = tf.nn.softmax_cross_entropy_with_logits(
    #     logits=cnn, labels=output_holder)
    cost_func = tf.nn.sigmoid_cross_entropy_with_logits(logits=cnn,
                                                        labels=output_holder)

    if cost == supported_modes[1]:  # cross entropy with L1 regularization
        L1 = tf.contrib.layers.l1_regularizer(scale=0.005, scope=None)
        penalty = tf.contrib.layers.apply_regularization(L1,
                                                         weights_list=weights)
        cost_func = cost_func + penalty
    elif cost == supported_modes[2]:  # cross entropy with L2 regularization
        L2 = tf.contrib.layers.l2_regularizer(scale=0.005, scope=None)
        penalty = tf.contrib.layers.apply_regularization(L2,
                                                         weights_list=weights)
        cost_func = cost_func + penalty
    cost = tf.reduce_mean(cost_func)
    optimizer = tf.train.GradientDescentOptimizer(epsilon).minimize(cost)
    #optimizer = tf.train.AdamOptimizer(epsilon).minimize(cost)
    session = tf.Session()
    init = tf.global_variables_initializer()
    session.run(init)

    x, y = util.load_images(train_folder_name, class_letter)
    row = len(x)
    # split data set to 5 partitions for cross validation
    block = row / 5
    train_accuracy = []
    validation_accuracy = []
    cost_history = []
    cost_validation_history = []
    s = []  # step in  graph
    tg = []  # training cost by step
    vg = []  # validation cost by step
    for i in range(5):  # 5-fold
        # Pick the current Si as the subset for testing
        sl_i = slice(i * block, (i + 1) * block)
        test_x = np.asarray(x[sl_i])
        test_y = np.asarray(y[sl_i])
        train_x = np.delete(x, np.s_[i * block:(i + 1) * block], axis=0)
        train_y = np.delete(y, np.s_[i * block:(i + 1) * block], axis=0)
        print 'Training on Si except S[', i, ']'
        cost_per_max_update_history = []
        for e in range(max_updates):  # an update is an epoch

            # shuffle the data before training
            for r in range(0, len(train_x)):
                try:
                    j = random.randint(r + 1, len(train_x) - 1)
                    if r != j:
                        train_x[r], train_x[j] = train_x[j], train_x[r]
                        train_y[r], train_y[j] = train_y[j], train_y[r]
                except ValueError:
                    pass
                    # print 'End of the list when shuffling'

            # slice the training data into mini batches and train
            for b in range(0, len(train_x), batch_size):
                batch_x = train_x[b:b + batch_size]
                batch_y = train_y[b:b + batch_size]
                # Reshape images data from 3d to 4d array before
                #  feeding into tensorflow
                sh = batch_x.shape
                batch_x = np.reshape(batch_x, (sh[0], sh[1], sh[2], 1))
                batch_y = np.reshape(batch_y, (len(batch_y), 1))
                feed_data = {input_holder: batch_x, output_holder: batch_y}
                session.run(optimizer, feed_dict=feed_data)
                predict = tf.greater(cnn, threshold)
                correct_pred = tf.equal(predict, tf.equal(output_holder, 1.0))
                accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
                accuracy = session.run(accuracy, feed_dict=feed_data)
                train_accuracy.append(accuracy)
                cost_value = session.run(cost, feed_dict=feed_data)

                cost_history.append(cost_value)
                cost_per_max_update_history.append(cost_value)
        # Each number of epoch time, save the training cost to draw the graph
        tg.append(np.mean(cost_per_max_update_history))
        s.append((i + 1) * max_updates)
        print 'Training accuracy:', np.mean(train_accuracy)
        print 'Training cost:', np.mean(cost_history)
        print 'Validating on S[', i, '] data'
        predict = tf.greater(cnn, threshold)
        correct_pred = tf.equal(predict, tf.equal(output_holder, 1.0))
        accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32),
                                  name="accuracy-check")
        # Reshape images data from 3d to 4d array before
        #  feeding into tensorflow
        xsh = test_x.shape
        test_x = np.reshape(test_x, (xsh[0], xsh[1], xsh[2], 1))
        test_y = np.reshape(test_y, (len(test_y), 1))
        accuracy = session.run(accuracy,
                               feed_dict={
                                   input_holder: test_x,
                                   output_holder: test_y
                               })
        validation_accuracy.append(accuracy)
        cost_validation = session.run(cost,
                                      feed_dict={
                                          input_holder: test_x,
                                          output_holder: test_y
                                      })
        cost_validation_history.append(cost_validation)
        # Save the validation cost to draw the graph
        vg.append(cost_validation)
        print "Validation Cost: ", cost_validation
        print 'Validation accuracy:', np.mean(validation_accuracy)
        print '-------------------------------'

    # Save the weights at the last fold
    saver = tf.train.Saver()
    saver.save(session, model_file_name)

    print 'Training and validation completed'
    print 'Avg Training accuracy:', np.mean(train_accuracy)
    print 'Avg Training cost:', np.mean(cost_history)
    print 'Avg Validation accuracy:', np.mean(validation_accuracy)
    print 'Avg Validation cost: ', np.mean(cost_validation_history)

    print "Step:", s
    print "Train cost:", tg
    # print "Validation cost:,", cost_validation_history
    session.close()
    graph(s, tg, cost_validation_history)
def detect_image(model, args):
    countbin = 0
    countgar = 0
    counttrash = 0
    a=[]
    print('Loading input image(s)...')
    input_size = [int(model.net_info['height']), int(model.net_info['width'])]
    batch_size = int(model.net_info['batch'])

    imlist, imgs = load_images(args.input)
    print('Input image(s) loaded')

    img_batches = create_batches(imgs, batch_size)

    # load colors and classes
    colors = pkl.load(open("pallete", "rb"))
    classes = load_classes("cfg/obj.names")

    if not osp.exists(args.outdir):
        os.makedirs(args.outdir)

    start_time = datetime.now()
    print('Detecting...')
    for batchi, img_batch in enumerate(img_batches):
        img_tensors = [cv_image2tensor(img, input_size) for img in img_batch]
        img_tensors = torch.stack(img_tensors)
        img_tensors = Variable(img_tensors)
        if args.cuda:
            img_tensors = img_tensors.cuda()
        detections = model(img_tensors, args.cuda).cpu()
        detections = process_result(detections, args.obj_thresh, args.nms_thresh)
        if len(detections) == 0:
            continue

        detections = transform_result(detections, img_batch, input_size)
        for detection in detections:
            draw_bbox(img_batch, detection, colors, classes)

            #la = classes[int(detection[-1])]
            #if la == 'bin':
                #countbin = countbin +1
            #if (la == 'plastic_bag' ):
                #countgar = countgar +1
            #if (la == 'trash'):
                #counttrash = counttrash+1
            if (countgar >= 3 or counttrash >= 5):
                p1 = tuple(detection[1:3].int())
                p2 = tuple(detection[3:5].int())
                combined_array=np.append(p1, p2)
                combined_list=combined_array.tolist()
                a.append(combined_list)
                result=cv2.groupRectangles(a,1,0.85)
                for (x,y,w,h) in result[0]:
  
                    img = img_batch[int(detection[0])]
     
        #print("---------Sum Amount-----------" )        
        #print("bin = " , countbin)
        #print("plastic_bag = " , countgar)
        #print("counttrash = " , counttrash)


        for i, img in enumerate(img_batch):
            save_path = osp.join(args.outdir, 'det_' + osp.basename(imlist[batchi*batch_size + i]))
            cv2.imwrite(save_path, img)
            print(save_path, 'saved')

    end_time = datetime.now()
    print('Detection finished in %s' % (end_time - start_time))

    return
Beispiel #31
0
    with tf.variable_scope(name):
      weight = self.get_weight_var([n, m])
      bias = self.get_bias_var([m])
      out = tf.nn.relu(tf.matmul(inputs, weight) + bias)
    return out


  def build_net(self, inputs):

    self.conv11 = self.conv_layer(inputs, 3, 3, 64, "conv1-1")
    self.pool11 = self.maxpool_layer(self.conv11, "pool1-1")
    self.conv12 = self.conv_layer(self.pool11, 3, 64, 64, "conv1-2")
    self.pool12 = self.maxpool_layer(self.conv12, "pool1-2")


pdata = load_images("t_pdata", 1700)
plabel = create_label((pdata.shape[0], 2), 0)

ndata = load_images("t_ndata", 1700)
nlabel = create_label((ndata.shape[0], 2), 1)

data = np.concatenate((pdata, ndata), 0)
label = np.concatenate((plabel, nlabel), 0)

print(data.shape)
print(label.shape)

sess = tf.InteractiveSession() 
x = tf.placeholder(tf.float32, shape=[None, 224, 224, 3])
y_ = tf.placeholder(tf.float32, shape=[None, 2])
keep_prob = tf.placeholder(tf.float32)
Beispiel #32
0
    type=str,
    help="Where to get image swatches that we think co-occur frequently.",
)
parser.add_argument(
    "--render",
    "--r",
    default=False,
    type=bool,
    help="if we should render progress",
)

args = parser.parse_args()

if __name__ == "__main__":

    # assumption is that the swatches contain different
    # versions of the single object that we are interested in
    swatches, _ = load_images(args.swatch_directory)
    test_images, image_names = load_images(args.test_directory)

    result_confidences = match_swatches(swatches,
                                        test_images,
                                        render_output=args.render)

    print('Swatch Match Results:')
    for i in range(len(result_confidences)):
        print('=====================================')
        print('Image: ' + image_names[i])
        print('Confidence: ' + str(result_confidences[i]))
        print('=====================================')