Ejemplo n.º 1
0
def request_chat():
    request_data = request.get_json()
    from_user = request_data.get('from_user', '')
    to_user = request_data.get('to_user', '')
    to_user_channel = "private-notification_user_%s" % (to_user)
    from_user_channel = "private-notification_user_%s" % (from_user)

    try:
        bot = User.query.filter(User.id == to_user).one()
    except NoResultFound:
        print('Error! No bot (yet).')
    except MultipleResultsFound:
        print('Error! Wait what?')

    try:
        yolo.get_model(bot.username)
    except:
        print('Error! Cannot load model!')

    # check if there is a channel that already exists between this two user
    channel = Channel.query.filter( Channel.from_user.in_([from_user, to_user]) ) \
                            .filter( Channel.to_user.in_([from_user, to_user]) ) \
                            .first()
    if not channel:
        # Generate a channel...
        chat_channel = "private-chat_%s_%s" % (from_user, to_user)

        new_channel = Channel()
        new_channel.from_user = from_user
        new_channel.to_user = to_user
        new_channel.name = chat_channel
        db_session.add(new_channel)
        db_session.commit()
    else:
        # Use the channel name stored on the database
        chat_channel = channel.name

    data = {
        "from_user": from_user,
        "to_user": to_user,
        "from_user_notification_channel": from_user_channel,
        "to_user_notification_channel": to_user_channel,
        "channel_name": chat_channel,
    }

    # Trigger an event to the other user
    pusher.trigger(to_user_channel, 'new_chat', data)

    return jsonify(data)
Ejemplo n.º 2
0
def sample_graph():
    # ======================================
    # =               graph                =
    # ======================================

    if not os.path.exists(py.join(output_dir, args_.generator_pb)):
        # model
        Genc, Gdec, _ = module.get_model(args.model, n_atts, weight_decay=args.weight_decay)

        # placeholders & inputs
        xa = tf.placeholder(tf.float32, shape=[None, args.crop_size, args.crop_size, 3])
        b_ = tf.placeholder(tf.float32, shape=[None, n_atts])

        # sample graph
        x = Gdec(Genc(xa, training=False), b_, training=False)
    else:
        # load freezed model
        with tf.gfile.GFile(py.join(output_dir, args_.generator_pb), 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            tf.import_graph_def(graph_def, name='generator')

        # placeholders & inputs
        xa = sess.graph.get_tensor_by_name('generator/xa:0')
        b_ = sess.graph.get_tensor_by_name('generator/b_:0')

        # sample graph
        x = sess.graph.get_tensor_by_name('generator/xb:0')

    # ======================================
    # =            run function            =
    # ======================================

    save_dir = '%s/output/%s/samples_testing_slide/%s_%s_%s_%s' % \
        (args.flask_path, args.experiment_name, args.test_att_name, '{:g}'.format(args.test_int_min), '{:g}'.format(args.test_int_max), '{:g}'.format(args.test_int_step))
    py.mkdir(save_dir)

    def run():
        cnt = 0
        for _ in tqdm.trange(len_test_dataset):
            # data for sampling
            xa_ipt, a_ipt = sess.run(test_iter.get_next())
            b_ipt = np.copy(a_ipt)
            b__ipt = (b_ipt * 2 - 1).astype(np.float32)  # !!!

            x_opt_list = [xa_ipt]
            for test_int in np.arange(args.test_int_min, args.test_int_max + 1e-5, args.test_int_step):
                b__ipt[:, args.att_names.index(args.test_att_name)] = test_int
                x_opt = sess.run(x, feed_dict={xa: xa_ipt, b_: b__ipt})
                x_opt_list.append(x_opt)
            sample = np.transpose(x_opt_list, (1, 2, 0, 3, 4))
            sample = np.reshape(sample, (sample.shape[0], -1, sample.shape[2] * sample.shape[3], sample.shape[4]))

            for s in sample:
                cnt += 1
                im.imwrite(s, '%s/%d.jpg' % (save_dir, cnt))

    return run
Ejemplo n.º 3
0
def sample_graph():
    # model
    Genc, Gdec, _ = module.get_model(args.model, n_atts, weight_decay=args.weight_decay)

    # placeholders & inputs
    xa = tf.placeholder(tf.float32, shape=[None, args.crop_size, args.crop_size, 3], name='xa')
    b_ = tf.placeholder(tf.float32, shape=[None, n_atts], name='b_')

    # sample graph
    x = Gdec(Genc(xa, training=False), b_, training=False)
    x = tf.identity(x, name='xb')
Ejemplo n.º 4
0
def process_query(bot_id, file, name):
    try:
        bot = User.query.filter(User.id == bot_id).one()
    except NoResultFound:
        print('Error! No bot (yet).')
    except MultipleResultsFound:
        print('Error! Wait what?')

    try:
        model = yolo.get_model(bot.username)
    except:
        print('Error! Cannot get model!')

    inp_dir = './data/input/{}.jpg'.format(name)
    out_dir = './data/output/{}'.format(name)
    file.save(inp_dir)
    r = yolo.detect(model, inp_dir, out_dir=out_dir)

    r = [x for x in r if x['prob'] > 0.3]
    messages = []
    messages.append('I have detected %d object(s) in the images.' % len(r))
    for idx, det in enumerate(r):
        prob = det['prob']
        if prob > 0.8:
            messages.append('[%d] I detected a [%s].' % (idx, det['class']))
        elif prob > 0.5:
            messages.append('[%d] I am not so sure but it may be a [%s]' %
                            (idx, det['class']))
        elif prob > 0.3:
            messages.append(
                '[%d] I really do not know what this is. My best guess is that it is a [%s].'
                % (idx, det['class']))

    inp_token = upload_file('input', inp_dir)
    out_token = upload_file('output', out_dir + '.jpg')

    inp_url = 'https://firebasestorage.googleapis.com/v0/b/narupedia-d9cf9.appspot.com/o/input%2F' + name + '.jpg?alt=media&token=' + inp_token
    out_url = 'https://firebasestorage.googleapis.com/v0/b/narupedia-d9cf9.appspot.com/o/output%2F' + name + '.jpg?alt=media&token=' + out_token

    return inp_url, out_url, messages
Ejemplo n.º 5
0
def sample_graph():
    # ======================================
    # =               graph                =
    # ======================================

    test_next = test_iter.get_next()

    if not os.path.exists(py.join(output_dir, 'generator.pb')):
        # model
        Genc, Gdec, _ = module.get_model(args.model,
                                         n_atts,
                                         weight_decay=args.weight_decay)

        # placeholders & inputs
        xa = tf.placeholder(tf.float32,
                            shape=[None, args.crop_size, args.crop_size, 3])
        b_ = tf.placeholder(tf.float32, shape=[None, n_atts])

        # sample graph
        x = Gdec(Genc(xa, training=False), b_, training=False)
    else:
        # load freezed model
        with tf.gfile.GFile(py.join(output_dir, 'generator.pb'), 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            tf.import_graph_def(graph_def, name='generator')

        # placeholders & inputs
        xa = sess.graph.get_tensor_by_name('generator/xa:0')
        b_ = sess.graph.get_tensor_by_name('generator/b_:0')

        # sample graph
        x = sess.graph.get_tensor_by_name('generator/xb:0')

    # ======================================
    # =            run function            =
    # ======================================

    save_dir = './output/%s/samples_testing_multi' % args.experiment_name
    tmp = ''
    for test_att_name, test_int in zip(args.test_att_names, args.test_ints):
        tmp += '_%s_%s' % (test_att_name, '{:g}'.format(test_int))
    save_dir = py.join(save_dir, tmp[1:])
    py.mkdir(save_dir)

    def run():
        cnt = 0
        for _ in tqdm.trange(len_test_dataset):
            # data for sampling
            xa_ipt, a_ipt = sess.run(test_next)
            b_ipt = np.copy(a_ipt)
            for test_att_name in args.test_att_names:
                i = args.att_names.index(test_att_name)
                b_ipt[..., i] = 1 - b_ipt[..., i]
                b_ipt = data.check_attribute_conflict(b_ipt, test_att_name,
                                                      args.att_names)

            b__ipt = (b_ipt * 2 - 1).astype(np.float32)  # !!!
            for test_att_name, test_int in zip(args.test_att_names,
                                               args.test_ints):
                i = args.att_names.index(test_att_name)
                b__ipt[..., i] = b__ipt[..., i] * test_int

            x_opt_list = [xa_ipt]
            x_opt = sess.run(x, feed_dict={xa: xa_ipt, b_: b__ipt})
            x_opt_list.append(x_opt)
            sample = np.transpose(x_opt_list, (1, 2, 0, 3, 4))
            sample = np.reshape(sample, (sample.shape[0], -1, sample.shape[2] *
                                         sample.shape[3], sample.shape[4]))

            for s in sample:
                cnt += 1
                im.imwrite(s, '%s/%d.jpg' % (save_dir, cnt))

    return run
Ejemplo n.º 6
0
def sample_graph():
    # ======================================
    # =               graph                =
    # ======================================

    if not os.path.exists(py.join(output_dir, 'generator.pb')):
        # model
        Genc, Gdec, _ = module.get_model(args.model,
                                         n_atts,
                                         weight_decay=args.weight_decay)

        # placeholders & inputs
        xa = tf.placeholder(tf.float32,
                            shape=[None, args.crop_size, args.crop_size, 3])
        b_ = tf.placeholder(tf.float32, shape=[None, n_atts])

        # sample graph
        x = Gdec(Genc(xa, training=False), b_, training=False)
    else:
        # load freezed model
        with tf.gfile.GFile(py.join(output_dir, 'generator.pb'), 'rb') as f:
            graph_def = tf.GraphDef()
            graph_def.ParseFromString(f.read())
            tf.import_graph_def(graph_def, name='generator')

        # placeholders & inputs
        xa = sess.graph.get_tensor_by_name('generator/xa:0')
        b_ = sess.graph.get_tensor_by_name('generator/b_:0')

        # sample graph
        x = sess.graph.get_tensor_by_name('generator/xb:0')

    # ======================================
    # =            run function            =
    # ======================================

    save_dir = '%s/output/%s/samples_testing_%s' % (
        args.flask_path, args.experiment_name, '{:g}'.format(args.test_int))
    py.mkdir(save_dir)

    def run():
        cnt = 0
        for _ in tqdm.trange(len_test_dataset):
            # data for sampling
            xa_ipt, a_ipt = sess.run(test_iter.get_next())
            b_ipt_list = [a_ipt]  # the first is for reconstruction
            for i in range(n_atts):
                tmp = np.array(a_ipt, copy=True)
                tmp[:, i] = 1 - tmp[:, i]  # inverse attribute
                tmp = data.check_attribute_conflict(tmp, args.att_names[i],
                                                    args.att_names)
                b_ipt_list.append(tmp)

            x_opt_list = [xa_ipt]
            for i, b_ipt in enumerate(b_ipt_list):
                b__ipt = (b_ipt * 2 - 1).astype(np.float32)  # !!!
                if i > 0:  # i == 0 is for reconstruction
                    b__ipt[..., i - 1] = b__ipt[..., i - 1] * args.test_int
                x_opt = sess.run(x, feed_dict={xa: xa_ipt, b_: b__ipt})
                x_opt_list.append(x_opt)
            sample = np.transpose(x_opt_list, (1, 2, 0, 3, 4))
            sample = np.reshape(sample, (sample.shape[0], -1, sample.shape[2] *
                                         sample.shape[3], sample.shape[4]))

            for s in sample:
                cnt += 1
                im.imwrite(s, '%s/%d.jpg' % (save_dir, cnt))

    return run
Ejemplo n.º 7
0
# ==============================================================================
# =                               data and model                               =
# ==============================================================================

# data
train_dataset, len_train_dataset = data.make_celeba_dataset(args.img_dir, args.train_label_path, args.att_names, args.batch_size,
                                                            load_size=args.load_size, crop_size=args.crop_size,
                                                            training=True, shuffle=True, repeat=None)
val_dataset, len_val_dataset = data.make_celeba_dataset(args.img_dir, args.val_label_path, args.att_names, args.n_samples,
                                                        load_size=args.load_size, crop_size=args.crop_size,
                                                        training=False, shuffle=True, repeat=None)
train_iter = train_dataset.make_one_shot_iterator()
val_iter = val_dataset.make_one_shot_iterator()

# model
Genc, Gdec, D = module.get_model(args.model, n_atts, weight_decay=args.weight_decay)

# loss functions
d_loss_fn, g_loss_fn = tfprob.get_adversarial_losses_fn(args.adversarial_loss_mode)


# ==============================================================================
# =                                   graph                                    =
# ==============================================================================

def D_train_graph():
    # ======================================
    # =               graph                =
    # ======================================

    # placeholders & inputs
Ejemplo n.º 8
0
def send_file():
    request_data = request.form
    from_user = request_data.get('from_user', '')
    to_user = request_data.get('to_user', '')
    channel = request_data.get('channel')

    file = request.files['file']

    bot_id = to_user
    name = channel + datetime.datetime.now().strftime("%Y%m%d%H%M%S")

    inp_dir = './data/input/{}.jpg'.format(name)
    file.save(inp_dir)
    inp_token = upload_file('input', inp_dir)
    inp_url = 'https://firebasestorage.googleapis.com/v0/b/narupedia-d9cf9.appspot.com/o/input%2F' + name + '.jpg?alt=media&token=' + inp_token
    inp_img = create_message(message=inp_url,
                             type='Image',
                             to_user=to_user,
                             from_user=from_user,
                             channel=channel)
    pusher.trigger(channel, 'new_message', inp_img)

    message = create_message(message="I'm onto it, please wait for a bit.",
                             type='Text',
                             to_user=from_user,
                             from_user=to_user,
                             channel=channel)
    pusher.trigger(channel, 'new_message', message)

    try:
        bot = User.query.filter(User.id == bot_id).one()
    except NoResultFound:
        print('Error! No bot (yet).')
    except MultipleResultsFound:
        print('Error! Wait what?')

    try:
        model = yolo.get_model(bot.username)
    except:
        print('Error! Cannot get model!')

    out_dir = './data/output/{}'.format(name)
    r = yolo.detect(model, inp_dir, out_dir=out_dir)
    r = [x for x in r if x['prob'] > 0.5]
    messages = []
    messages.append('I have detected %d object(s) in the images.' % len(r))
    for idx, det in enumerate(r):
        prob = det['prob']
        if prob > 0.8:
            messages.append('[%d] I detected a [%s].' % (idx, det['class']))
        elif prob > 0.5:
            messages.append('[%d] I am not so sure but it may be a [%s]' %
                            (idx, det['class']))
        # elif prob > 0.3:
        # messages.append('[%d] I really do not know what this is. My best guess is that it is a [%s].' % (idx, det['class']))

    out_token = upload_file('output', out_dir + '.jpg')
    out_url = 'https://firebasestorage.googleapis.com/v0/b/narupedia-d9cf9.appspot.com/o/output%2F' + name + '.jpg?alt=media&token=' + out_token

    # inp_url, out_url, messages = process_query(bot_id, file, name)
    out_img = create_message(message=out_url,
                             type='Image',
                             to_user=from_user,
                             from_user=to_user,
                             channel=channel)
    pusher.trigger(channel, 'new_message', out_img)

    for m in messages:
        message = create_message(message=m,
                                 type='Text',
                                 to_user=from_user,
                                 from_user=to_user,
                                 channel=channel)
        pusher.trigger(channel, 'new_message', message)

    return jsonify(messages)