Ejemplo n.º 1
0
def build_graph(num_actions):
    s, q_network = build_network(num_actions=num_actions, agent_history_length=FLAGS.agent_history_length,
                      resized_width=FLAGS.resized_width, resized_height=FLAGS.resized_height, name_scope="q-network")

    network_params = q_network.trainable_weights
    q_values = q_network(s)

    st, target_q_network = build_network(num_actions=num_actions, agent_history_length=FLAGS.agent_history_length,
                      resized_width=FLAGS.resized_width, resized_height=FLAGS.resized_height, name_scope="target-network")
    target_network_params = target_q_network.trainable_weights
    target_q_values = target_q_network(st)

    reset_target_network_params = [target_network_params[i].assign(network_params[i]) for i in range(len(target_network_params))]

    a = tf.placeholder("float", [None, num_actions])
    y = tf.placeholder("float", [None])
    action_q_values = tf.reduce_sum(tf.multiply(q_values, a), reduction_indices=1)
    cost = tf.reduce_mean(tf.square(y - action_q_values))
    optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)

    grad_update = optimizer.minimize(cost, var_list=network_params)

    graph_ops = {"s" : s,
                 "q_values" : q_values,
                 "st" : st,
                 "target_q_values" : target_q_values,
                 "reset_target_network_params" : reset_target_network_params,
                 "a" : a,
                 "y" : y,
                 "grad_update" : grad_update}

    return graph_ops
Ejemplo n.º 2
0
def build_graph(num_actions):
    # Create shared deep q network
    s, q_network = build_network(num_actions=num_actions, agent_history_length=FLAGS.agent_history_length, resized_width=FLAGS.resized_width, resized_height=FLAGS.resized_height)
    network_params = q_network.trainable_weights
    q_values = q_network(s)

    # Create shared target network
    st, target_q_network = build_network(num_actions=num_actions, agent_history_length=FLAGS.agent_history_length, resized_width=FLAGS.resized_width, resized_height=FLAGS.resized_height)
    target_network_params = target_q_network.trainable_weights
    target_q_values = target_q_network(st)

    # Op for periodically updating target network with online network weights
    reset_target_network_params = [target_network_params[i].assign(network_params[i]) for i in range(len(target_network_params))]
    
    # Define cost and gradient update op
    a = tf.placeholder("float", [None, num_actions])
    y = tf.placeholder("float", [None])
    action_q_values = tf.reduce_sum(tf.mul(q_values, a), reduction_indices=1)
    cost = tf.reduce_mean(tf.square(y - action_q_values))
    optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)
    grad_update = optimizer.minimize(cost, var_list=network_params)

    graph_ops = {"s" : s, 
                 "q_values" : q_values,
                 "st" : st, 
                 "target_q_values" : target_q_values,
                 "reset_target_network_params" : reset_target_network_params,
                 "a" : a,
                 "y" : y,
                 "grad_update" : grad_update}

    return graph_ops
Ejemplo n.º 3
0
def predict(model_path, data_path):

    model_dict = torch.load(model_path, map_location='cpu')

    c = model_dict['c']
    net = build_network('LG_LeNet')
    net.load_state_dict(model_dict['net_dict'])
    outlier_dist = model_dict['outlier_dist']
    
    ae_net = build_network('LG_LeNet_Autoencoder')
    ae_net.load_state_dict(model_dict['ae_net_dict'])

    net.to('cuda')

    images = read_data(data_path)
    output = []
    for i in range(len(images)):
        outputs = net(torch.tensor(images[i], dtype=torch.float32).to('cuda'))
        dist = torch.sum((outputs - c) ** 2, dim=1)
        if dist > outlier_dist:
            label = 1
            print(print(f"label:{label}, abnormal"))
        else:
            label = 0
            print(print(f"label:{label}, normal"))

    # print(f"Show output : {output}")

    return output
Ejemplo n.º 4
0
def build_graph(num_actions):
    # Create shared deep q network
    s, q_network = build_network(num_actions=num_actions, agent_history_length=FLAGS.agent_history_length,
                                 resized_width=FLAGS.resized_width, resized_height=FLAGS.resized_height)
    network_params = q_network.trainable_weights
    q_values = q_network(s)

    # Create shared target network
    st, target_q_network = build_network(num_actions=num_actions, agent_history_length=FLAGS.agent_history_length,
                                         resized_width=FLAGS.resized_width, resized_height=FLAGS.resized_height)
    target_network_params = target_q_network.trainable_weights
    target_q_values = target_q_network(st)

    # Op for periodically updating target network with online network weights
    reset_target_network_params = [target_network_params[i].assign(network_params[i]) for i in
                                   range(len(target_network_params))]

    # Define cost and gradient update op
    a = tf.placeholder("float", [None, num_actions])
    y = tf.placeholder("float", [None])
    action_q_values = tf.reduce_sum(tf.multiply(q_values, a), reduction_indices=1)
    cost = tf.reduce_mean(tf.square(y - action_q_values))
    optimizer = tf.train.AdamOptimizer(FLAGS.learning_rate)
    grad_update = optimizer.minimize(cost, var_list=network_params)

    graph_ops = {"s": s,
                 "q_values": q_values,
                 "st": st,
                 "target_q_values": target_q_values,
                 "reset_target_network_params": reset_target_network_params,
                 "a": a,
                 "y": y,
                 "grad_update": grad_update}

    return graph_ops
Ejemplo n.º 5
0
def evaluate(X, y, names):
    tf.logging.info('building model...')
    input_shape = [None, 8 * 8 * 2048]

    t_u = tf.placeholder(tf.float32, input_shape, 'u_inputs')
    t_v = tf.placeholder(tf.float32, input_shape, 'v_inputs')

    t_fu = build_network(t_u)
    t_fv = build_network(t_v, reuse=True)

    distance = tf.sqrt(tf.reduce_sum((t_fu - t_fv)**2, axis=-1))

    optimal_saver = tf.train.Saver(max_to_keep=1)

    config = tf.ConfigProto(allow_soft_placement=True)
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as s:
        tf.logging.info('restoring...')
        optimal_saver.restore(s, FLAGS.ckpt_file)

        tf.logging.info('evaluating model...')

        d = np.array([
            s.run(distance, feed_dict={
                t_u: u,
                t_v: v
            }) for (u, v), y, name in zip(X, y, names)
        ])

        print('avg distance for identities:', d[y == 1].mean())
        print('avg distance for non-identities:', d[y == 0].mean())

        for threshold in (.1, .2, .3, .4, .5):
            labels = (d <= FLAGS.decision_threshold).astype(np.int)

            for strategy in ('contrastive_mean', 'most_frequent'):
                p = strategies.get(strategy)(labels, d, t=threshold)
                accuracy_score = metrics.accuracy_score(y, p)
                print(
                    'score (%s, %.1f): %.2f%%' %
                    (strategy, threshold, 100 * accuracy_score),
                    '\nConfusion matrix:\n', metrics.confusion_matrix(y, p),
                    '\nWrong predictions: %s\n' % names[y != p])
Ejemplo n.º 6
0
def test(time_steps=32, target_cost_dev=0.05):

    test_samples = 32*32

    if not os.path.isdir('test'):
        print('Creating {} Complete Test instances'.format(test_samples), flush=True)
        create_dataset_metric(
            20, 20,
            1, 1,
            bins=10**6,
            samples=test_samples,
            path='test')
    #end

    d                       = 64
    epochs_n                = 100
    batch_size              = 1
    test_batches_per_epoch  = 16*32

    # Create test loader
    test_loader = InstanceLoader("test")

    # Build model
    print("Building model ...", flush=True)
    GNN = build_network(d)

    # Disallow GPU use
    config = tf.ConfigProto( device_count = {"GPU":0})
    with tf.Session(config=config) as sess:

        # Initialize global variables
        print("Initializing global variables ... ", flush=True)
        sess.run( tf.global_variables_initializer() )

        # Restore saved weights
        load_weights(sess,'./TSP-checkpoints-decision-0.05/epoch=200.0')
        
        with open('TSP-log.dat','w') as logfile:
            # Run for a number of epochs
            for epoch_i in range(1):

                test_loader.reset()

                test_loss   = np.zeros(test_batches_per_epoch)
                test_acc    = np.zeros(test_batches_per_epoch)
                test_sat    = np.zeros(test_batches_per_epoch)
                test_pred   = np.zeros(test_batches_per_epoch)

                print("Testing model...", flush=True)
                for (batch_i, batch) in islice(enumerate(test_loader.get_batches(batch_size, target_cost_dev=target_cost_dev)), test_batches_per_epoch):
                    test_loss[batch_i], test_acc[batch_i], test_sat[batch_i], test_pred[batch_i] = run_batch(sess, GNN, batch, batch_i, epoch_i, time_steps, train=False, verbose=True)[:4]
                #end
                summarize_epoch(epoch_i,test_loss,test_acc,test_sat,test_pred,train=False)
Ejemplo n.º 7
0
 def __init__(self,cpkt_file,input_shape):
     self.input_x = tf.placeholder(tf.float32,shape = input_shape,name = "input")
     self.logit = model.build_network(self.input_x,False,True)
     self.input_shape = input_shape
     saver = tf.train.Saver()
     ckpt = tf.train.get_checkpoint_state(cpkt_file)
     self.sess = tf.Session()
     if ckpt and ckpt.model_checkpoint_path:
         global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
         saver.restore(self.sess, ckpt.model_checkpoint_path)
         print('Loading success, global_step is %s' % global_step)
     else:
         print('No checkpoint file found')
Ejemplo n.º 8
0
def eval():
    maxstep, image_batch, label_batch = input_data.get_eval_image(
        VALPATH, BATCH_SIZE, WIDTH, HEIGHT, CHANNELS)
    with tf.Graph().as_default():
        input_x = tf.placeholder(tf.float32,
                                 shape=[BATCH_SIZE, WIDTH, HEIGHT, CHANNELS],
                                 name="input")
        datamax, datamin, logit = model.build_network(input_x, False, True)

        if modeltype == 'NOQUANT': goto.next
        print("create eval quantize")
        tf.contrib.quantize.create_eval_graph()
        label.next

        saver = tf.train.Saver()
        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(TRAIN_LOGS_DIR)
            if ckpt and ckpt.model_checkpoint_path:
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                saver.restore(sess, ckpt.model_checkpoint_path)
                print('Loading success, global_step is %s' % global_step)
            else:
                print('No checkpoint file found')

            graph = graph_util.convert_variables_to_constants(
                sess, sess.graph_def, ["output"])
            modelname = 'frozen_model_NQ.pb'
            if modeltype == 'AWAREQUANT': modelname = 'frozen_model_Q.pb'
            tf.io.write_graph(graph, VAL_LOGS_DIR, modelname, as_text=False)

            if evaltype == 'COUNT':
                datamax_, datamin_ = sess.run(
                    [datamax, datamin], feed_dict={input_x: image_batch[0]})
                print(tf.reduce_max(datamax_).eval())
                print(tf.reduce_min(datamin_).eval())
            else:
                acc_count = 0
                max_count = maxstep * BATCH_SIZE
                for i in range(maxstep):
                    output = sess.run(logit,
                                      feed_dict={input_x: image_batch[i]})
                    prediction = np.argmax(output, axis=1)
                    for j in range(BATCH_SIZE):
                        print("predict label: %s    true label : %s" %
                              (prediction[j], label_batch[i][j]))
                        if str(prediction[j]) == str(label_batch[i][j]):
                            acc_count = acc_count + 1
                print("final accuracy is :{:.2f}%".format(100 * acc_count /
                                                          max_count))
Ejemplo n.º 9
0
def test(epoch, ckpt_dir):
    network = build_network()
    network.load_weights(
        ckpt_dir)  # Comment récupérer les poids d'une epoch donnée ?
    data_test = data_set('test')
    inputs = data_test[0]
    label = data_test[1]
    predicted = network.predict(inputs)
    PSNR = psnr(label, predicted)
    SSIM = ssim(label, predicted)
    print('PSNR :')
    print(PSNR)
    print('SSIM :')
    print(SSIM)
Ejemplo n.º 10
0
def train(lr=0.01,
          batchsize=64,
          epochs=5,
          ckpt_dir="./checkpoint",
          log_dir="log_dir"):
    data, label = data_set(phase="train")
    Model = build_network()
    Model.compile(optimizer=tf.keras.optimizers.Adam(), loss='MSE')
    # metrics= skimage.metrics.peak_signal_noise_ratio(image_true, image_test, *, data_range=None))
    callbacks = make_callback(ckpt_dir, log_dir)
    Model.fit(data,
              label,
              batch_size=batchsize,
              epochs=epochs,
              callbacks=callbacks)
Ejemplo n.º 11
0
def get_skeleton_model(criterion):
    model = build_network()
    model.cuda()
    for module in model.modules():
        if isinstance(module, torch.nn.BatchNorm2d):
            if hasattr(module, 'weight') and module.weight is not None:
                module.weight.data.fill_(1.0)
            module.eps = 0.00001
            module.momentum = 0.1
        else:
            module.half()
        if isinstance(module, torch.nn.Conv2d) and hasattr(module, 'weight'):
            # torch.nn.init.kaiming_uniform_(module.weight, a=math.sqrt(5))  # original
            torch.nn.init.kaiming_uniform_(module.weight,
                                           mode='fan_in',
                                           nonlinearity='linear')
            # torch.nn.init.xavier_uniform_(module.weight, gain=torch.nn.init.calculate_gain('linear'))
        if isinstance(module, torch.nn.Linear) and hasattr(module, 'weight'):
            # torch.nn.init.kaiming_uniform_(module.weight, a=math.sqrt(5))  # original
            torch.nn.init.kaiming_uniform_(module.weight,
                                           mode='fan_in',
                                           nonlinearity='linear')
            # torch.nn.init.xavier_uniform_(module.weight, gain=1.)

    class ModelLoss(torch.nn.Module):
        def __init__(self, model, criterion):
            super(ModelLoss, self).__init__()
            self.model = model
            self.criterion = criterion

        def forward(self, inputs, targets):
            inputs = inputs.cuda().to(dtype=torch.half)
            targets = targets.cuda(non_blocking=True)  #.to(dtype=torch.half)
            logits = self.model(inputs)
            loss = self.criterion(logits, targets)
            return logits, loss

    model = ModelLoss(model, criterion)
    return model
Ejemplo n.º 12
0
def eval():
    maxstep, image_batch, label_batch = input_data.get_eval_image(
        VALPATH, BATCH_SIZE)
    with tf.Graph().as_default():
        input_x = tf.placeholder(tf.float32,
                                 shape=[BATCH_SIZE, WIDTH, HEIGHT, CHANNELS],
                                 name="input")
        logit = model.build_network(input_x, False, True)

        if modeltype == 'NOQUANT': goto.next
        print("create eval quantize")
        tf.contrib.quantize.create_eval_graph()
        label.next

        saver = tf.train.Saver()
        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(TRAIN_LOGS_DIR)
            if ckpt and ckpt.model_checkpoint_path:
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                saver.restore(sess, ckpt.model_checkpoint_path)
                print('Loading success, global_step is %s' % global_step)
            else:
                print('No checkpoint file found')

            acc_count = 0
            max_count = maxstep * BATCH_SIZE
            for i in range(maxstep):
                output = sess.run(logit, feed_dict={input_x: image_batch[i]})
                prediction = np.argmax(output, axis=1)
                for j in range(BATCH_SIZE):
                    print("predict label: %s    true label : %s" %
                          (prediction[j], label_batch[i][j]))
                    if str(prediction[j]) == str(label_batch[i][j]):
                        acc_count = acc_count + 1
            print("final accuracy is :{:.2f}%".format(100 * acc_count /
                                                      max_count))
Ejemplo n.º 13
0
def draw_routes():
    
    d = 64
    n = 20
    bins = 10**6
    connectivity = 1

    # Build model
    print('Building model ...')
    model = build_network(d)

    # Disallow GPU use
    config = tf.ConfigProto( device_count = {'GPU':0})
    with tf.Session(config=config) as sess:

        # Initialize global variables
        print('Initializing global variables ...')
        sess.run( tf.global_variables_initializer() )

        # Restore saved weights
        load_weights(sess,'./TSP-checkpoints-decision-0.05/epoch=200.0')

        target_cost_dev = +0.05

        # Create instance
        while True:
            instance = create_graph_metric(n,bins,connectivity)
            Ma,Mw,route,nodes = instance
            edges = list(zip(np.nonzero(Ma)[0],np.nonzero(Ma)[1]))
            edge_weights = [ Mw[i,j] for (i,j) in edges ]

            _,_, predictions = extract_embeddings_and_predictions(sess, model, instance, time_steps=32, target_cost_dev=target_cost_dev)
        
            if predictions[0] < 1:
                break
            #end
        #end

        # Define timesteps range
        timesteps = np.arange(2,32+1,10)
        # Init figure
        f, axes = plt.subplots(1, len(timesteps), dpi=200, sharex=True, sharey=True)
        # Iterate over timesteps
        for i,(t,ax) in enumerate(zip(timesteps,axes)):
            
            # Fetch embeddings and predictions
            vertex_embeddings, edge_embeddings, predictions = extract_embeddings_and_predictions(sess, model, instance, time_steps=t, target_cost_dev=target_cost_dev)

            # Obtain 2D vertex embedding projections
            vertex_projections = get_projections(vertex_embeddings,2)
            # Obtain 2D edge embedding projections
            edge_projections = get_projections(edge_embeddings,2)

            # Obtain 2-clustering
            clusters, cluster_centers = get_k_cluster(edge_embeddings,2)

            print('#1 Cluster size, #2 Cluster size: {},{}'.format(len(clusters[0]),len(clusters[1])))

            # Set subplot title
            ax.set_title('{t} steps\npred:{pred:.0f}%'.format(t=t,pred=100*predictions[0]))

            if len(clusters[0]) < len(clusters[1]):
                clusters = clusters[::-1]
                cluster_centers = cluster_centers[::-1]
            #end

            # Plot edges
            for k in range(1,2):
                color = ['red','blue'][k]
                for e,(i,j) in enumerate(edges):
                    if e in clusters[k]:
                        x0,y0 = nodes[i,:]
                        x1,y1 = nodes[j,:]
                        ax.plot([x0,x1],[y0,y1], c=color, linewidth=0.5, zorder=1)
                    #end
                #end
            #end

            edge_in_route = []
            edge_not_in_route = []
            for e,(i,j) in enumerate(edges):
                if (i,j) in zip(route,route[:1]+route[1:]):
                    edge_in_route.append(e)
                else:
                    edge_not_in_route.append(e)
                #end
            #end

            ax.scatter(nodes[:,0], nodes[:,1], c='white', edgecolors='black', zorder=2)
            
            #ax.scatter(edge_projections[edge_not_in_route,0],edge_projections[edge_not_in_route,1], c='red', edgecolors='black')
            #ax.scatter(edge_projections[edge_in_route,0],edge_projections[edge_in_route,1], c='blue', edgecolors='black')

            #ax.scatter(edge_projections[clusters[1],0],edge_projections[clusters[1],1], c='red', edgecolors='black')
            #ax.scatter(edge_projections[clusters[0],0],edge_projections[clusters[0],1], c='blue', edgecolors='black')

        #end

        plt.show()
Ejemplo n.º 14
0
def evaluate():
    vangogh = VanGogh(base_dir=FLAGS.data_dir,
                      image_shape=[299, 299, 3],
                      n_jobs=FLAGS.n_jobs,
                      train_n_patches=FLAGS.nb_patches_loaded,
                      valid_n_patches=FLAGS.nb_patches_loaded,
                      test_n_patches=FLAGS.nb_patches_loaded,
                      random_state=FLAGS.dataset_seed)
    train, valid, test = vangogh.load_patches_from_full_images()
    X_train, y_train, n_train = map(np.concatenate, zip(train, valid))
    del valid
    X_test, y_test, n_test = test
    X_train /= 255.
    X_test /= 255.
    X, y, names = combine_pairs_for_evaluation(
        X_train,
        y_train,
        n_train,
        X_test,
        y_test,
        n_test,
        anchor_label=1,
        patches_used=FLAGS.nb_patches_compared)
    del X_train, y_train, n_train, X_test, y_test, n_test, train, test

    tf.logging.info('building model...')
    image_shape = [299, 299, 3]
    batch_shape = [None] + image_shape

    t_u = tf.placeholder(tf.float32, batch_shape, 'u_inputs')
    t_v = tf.placeholder(tf.float32, batch_shape, 'v_inputs')

    t_fu = build_network(t_u)
    t_fv = build_network(t_v, reuse=True)

    distance = tf.reduce_sum((t_fu - t_fv)**2, axis=-1)

    optimal_saver = tf.train.Saver(max_to_keep=1)

    config = tf.ConfigProto(allow_soft_placement=True)
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as s:
        tf.global_variables_initializer().run()

        tf.logging.info('restoring...')
        optimal_saver.restore(s, FLAGS.ckpt_file)

        tf.logging.info('evaluating model...')

        d = np.array(
            [s.run(distance, feed_dict={
                t_u: u,
                t_v: v
            }) for u, v in X])

        print('avg distance for identities:', d[y == 1].mean())
        print('avg distance for non-identities:', d[y == 0].mean())

        labels = (d <= FLAGS.decision_threshold).astype(np.int)

        for threshold in (7.6e-6, 8e-6, 8.4e-6, 8.6e-6):
            for strategy in ('contrastive_mean', ):
                p = strategies.get(strategy)(labels, d, t=threshold)
                accuracy_score = metrics.accuracy_score(y, p)
                print(
                    'score using', strategy, 'strategy, threshold %f: %.2f%%' %
                    (threshold, 100 * accuracy_score), '\nConfusion matrix:\n',
                    metrics.confusion_matrix(y, p),
                    '\nWrong predictions: %s' % names[y != p])
Ejemplo n.º 15
0
def train(X_train, y_train, names_train, X_valid, y_valid, names_valid):
    os.makedirs('/work/ckpt/vangogh/1-triplets/frozen-base/opt/',
                exist_ok=True)
    os.makedirs('/work/ckpt/vangogh/1-triplets/frozen-base/progress/',
                exist_ok=True)

    tf.logging.info('building model...')

    input_shape = [None, 8 * 8 * 2048]

    with tf.device(FLAGS.device):
        i_embedding = tf.placeholder(tf.float32, input_shape,
                                     'input_embedding')
        i_a = tf.placeholder(tf.float32, input_shape, 'input_anchors')
        i_p = tf.placeholder(tf.float32, input_shape, 'input_positives')
        i_n = tf.placeholder(tf.float32, input_shape, 'input_negatives')

        with tf.name_scope('embedding_net'):
            f_embedding = build_network(i_embedding)
        with tf.name_scope('anchor_leg_net'):
            f_a = build_network(i_a, is_training=True, reuse=True)
        with tf.name_scope('positive_leg_net'):
            f_p = build_network(i_p, is_training=True, reuse=True)
        with tf.name_scope('negative_leg_net'):
            f_n = build_network(i_n, is_training=True, reuse=True)

        with tf.name_scope('triplet_loss'):
            loss = triplet_loss(f_a, f_p, f_n)

        tf.summary.scalar("loss", loss)
        merged_summary_op = tf.summary.merge_all()

        gs = tf.Variable(0, trainable=False)

        with tf.name_scope('train'):
            lr = tf.train.exponential_decay(0.001, gs, 250, .5)
            optimizer = tf.train.AdamOptimizer(lr).minimize(loss)

    opt_saver = tf.train.Saver(max_to_keep=1)
    progress_saver = tf.train.Saver()
    best_val_loss = np.inf

    train_data = triplets_gen(X_train,
                              y_train,
                              names_train,
                              i_embedding,
                              f_embedding,
                              batch_size=FLAGS.batch_size,
                              window_size=FLAGS.window_size)
    valid_data = triplets_gen(X_valid,
                              y_valid,
                              names_valid,
                              i_embedding,
                              f_embedding,
                              batch_size=FLAGS.batch_size,
                              window_size=FLAGS.window_size)

    config = tf.ConfigProto(allow_soft_placement=True)
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as s:
        tf.global_variables_initializer().run()

        summary_writer = tf.summary.FileWriter(FLAGS.logs_dir,
                                               graph=tf.get_default_graph())

        tf.logging.info('training...')

        for it in range(FLAGS.nb_iterations):
            a, p, n = next(train_data)

            _, _train_loss, summary = s.run(
                [optimizer, loss, merged_summary_op],
                feed_dict={
                    i_a: a,
                    i_p: p,
                    i_n: n
                })

            summary_writer.add_summary(summary, it)

            if not it % FLAGS.nb_val_interval:
                print('#%i train_loss: %f' % (it, _train_loss), end=' ')

                a, p, n = next(valid_data)
                _val_loss = s.run(loss, feed_dict={i_a: a, i_p: p, i_n: n})
                print('val_loss: %f' % _val_loss)

                if _val_loss < best_val_loss:
                    tf.logging.info('val_acc improved from %.4f to %.4f',
                                    best_val_loss, _val_loss)
                    opt_saver.save(
                        s,
                        '/work/ckpt/vangogh/1-triplets/frozen-base/opt/opt.ckpt',
                        global_step=gs)
                    best_val_loss = _val_loss

            if not it % FLAGS.nb_save_interval:
                progress_saver.save(
                    s,
                    '/work/ckpt/vangogh/1-triplets/frozen-base/progress/progress.ckpt',
                    global_step=gs)
Ejemplo n.º 16
0
def draw_projections():
    
    d = 64
    n = 20
    bins = 10**6
    connectivity = 1

    # Build model
    print('Building model ...')
    model = build_network(d)

    # Disallow GPU use
    config = tf.ConfigProto( device_count = {'GPU':0})
    with tf.Session(config=config) as sess:

        # Initialize global variables
        print('Initializing global variables ...')
        sess.run( tf.global_variables_initializer() )

        # Restore saved weights
        load_weights(sess,'./TSP-checkpoints-decision-0.05/epoch=100.0')

        target_cost_dev = +0.1

        # Create instance
        while True:
            instance = create_graph_metric(n,bins,connectivity)
            Ma,Mw,_,nodes = instance
            edges = list(zip(np.nonzero(Ma)[0],np.nonzero(Ma)[1]))
            edge_weights = [ Mw[i,j] for (i,j) in edges ]

            _,_, predictions = extract_embeddings_and_predictions(sess, model, instance, time_steps=32, target_cost_dev=target_cost_dev)
        
            if predictions[0] > 0.7:
                break
            #end
        #end

        # Define timesteps range
        timesteps = np.arange(20,32+1,4)
        # Init figure
        f, axes = plt.subplots(1, len(timesteps), dpi=200, sharex=True, sharey=True)
        # Iterate over timesteps
        for i,(t,ax) in enumerate(zip(timesteps,axes)):
            
            # Fetch embeddings and predictions
            vertex_embeddings, edge_embeddings, predictions = extract_embeddings_and_predictions(sess, model, instance, time_steps=t, target_cost_dev=target_cost_dev)

            # Compute accuracy
            acc = 100*( (target_cost_dev > 0) == (predictions[0] > 0.5) ).astype(float)

            # Obtain 2D vertex embedding projections
            vertex_projections = get_projections(vertex_embeddings,2)
            # Obtain 2D edge embedding projections
            edge_projections = get_projections(edge_embeddings,2)

            # Set subplot title
            ax.set_title('{t} steps\npred:{pred:.0f}%'.format(t=t,acc=acc,pred=100*predictions[0]))

            # Plot projections
            #ax.scatter(vertex_projections[:,0],vertex_projections[:,1], edgecolors='black')
            ax.scatter(edge_projections[:,0],edge_projections[:,1], edgecolors='black', c=edge_weights, cmap='jet')

        #end

        plt.show()
Ejemplo n.º 17
0
def predict_supervised(model_path, data_path):
    net = build_network('resnet')
    net.load_state_dict(torch.load(model_path))
    net.eval()

    # file open
    label_true = []
    label_pred = []
    f = open(data_path, 'r')
    header = f.readline() # ignore first line if header
    t_new = t_old = time.time()
    idx = 0
    while 1: 
        line = f.readline()
        if not line:
            break
        
        #label
        label_true.append(int(line[0]))

        # data
        line = line[1:].strip().split('\t')
        line[1] = int(line[1][1])
        data = np.array(line, dtype=np.float32)

        # preprocess each line
        freqs_image = preprocess(data)
        # freqs_image = preprocess_spectrogram(data) # for spectrogram
        freqs_image = torch.Tensor(freqs_image).unsqueeze(0)

        # predict
        output = net(freqs_image)
        output = round(float(output[0][0]))
        label_pred.append(int(output))
        # print(int(output))
        idx += 1
        if idx % 1000 == 0:
            t_new = time.time()
            print(idx, f'{(t_new - t_old):.2f}초 / 1000개, 초당 {(1000 / (t_new - t_old)):.2f}개')
            t_old = time.time()

    print('label_true : ', label_true)
    print('label_pred : ', label_pred)
    # ploting confusion matrix
    matrix = confusion_matrix(label_true, label_pred)
    print('confusion matrix : ', matrix)

    total=sum(sum(matrix))
    #####from confusion matrix calculate accuracy
    accuracy=(matrix[0,0]+matrix[1,1])/total
    print ('Accuracy : ', accuracy)

    sensitivity = matrix[0,0]/(matrix[0,0]+matrix[0,1])
    print('Sensitivity(recall) : ', sensitivity )

    specificity = matrix[1,1]/(matrix[1,0]+matrix[1,1])
    print('Specificity : ', specificity)

    precision = matrix[0,0]/(matrix[0,0]+matrix[1,0])
    print('Precision : ', precision)

    f1 = 2 * (precision * sensitivity) /  (precision+ sensitivity)
    print('F1 Score : ', f1)

    print("\n\nSensitivity or recall: TP / (TP + FN)     // 맞는 케이스에 대해 얼마나 많이 맞다고 실제로 예측했나?")
    print("Specificity: TN / (FP + TN)     // 틀린 케이스에 대해 얼마나 틀리다고 실제로 예측했나?")
    print("Precision: TP / (TP + FP)     // 맞다고 예측 한 것 중에 실제로 정답이 얼마나 되나?")

    fig, ax = plot_confusion_matrix(conf_mat=matrix,
                                show_absolute=True,
                                show_normed=True,
                                colorbar=True)
    plt.plot()
    plt.savefig('/workspace/confusion.png')



    y = np.array([1, 1, 2, 2])
    scores = np.array([0.1, 0.4, 0.35, 0.8])
    fpr, tpr, thresholds = roc_curve(label_true, label_pred, pos_label=2)
    roc_auc = auc(fpr, tpr)
    # fig, ax = plot_roc_curve(1, label_true, label_pred)
    plt.plot()
    plt.savefig('/workspace/roc_curve.png')
Ejemplo n.º 18
0
def train_net():
    #------进入计算图--------
    x_train, y_train, x_val, y_val = input_data.read_img(
        FILEPATH, WIDTH, HEIGHT, CHANNELS, ratio)
    x_train_batch, y_train_batch = input_data.bulid_batch(
        x_train, y_train, BATCH_SIZE)
    x_val_batch, y_val_batch = input_data.bulid_batch(x_val, y_val, BATCH_SIZE)
    batch_train_len = x_train_batch.shape[0]
    batch_val_len = x_val_batch.shape[0]

    #定义网络    x为输入占位符      y为输出占位符
    #image_max = tf.reduce_max(x_train, name='image_max')
    #image_min = tf.reduce_min(x_train,name='image_min')

    x = tf.placeholder(tf.float32,
                       shape=[BATCH_SIZE, HEIGHT, WIDTH, CHANNELS],
                       name='input')
    y = tf.placeholder(tf.int64, shape=[BATCH_SIZE], name='labels_placeholder')
    _, _, softmax_linear = model.build_network(x, True, False)
    cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits\
                        (logits=softmax_linear, labels=y, name='xentropy_per_example')
    train_loss = tf.reduce_mean(cross_entropy, name='loss')

    if modeltype == 'NOQUANT': goto.end
    #fake quant插入到op之前
    tf.contrib.quantize.create_training_graph(
        input_graph=tf.get_default_graph(), quant_delay=2000)

    label.end

    train_step = trainning(train_loss, LEARNING_RATE)

    #准确略计算
    correct = tf.nn.in_top_k(softmax_linear, y, 1)
    correct = tf.cast(correct, tf.float16)
    train_acc = tf.reduce_mean(correct)

    #------------结束计算图-------------

    with tf.Session() as sess:

        saver = tf.compat.v1.train.Saver()
        sess.run(tf.global_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        valstep = 0

        #max = sess.run(image_max)
        #min = sess.run(image_min)
        #训练
        try:
            ckpt = tf.train.get_checkpoint_state(TRAIN_LOGS_DIR)
            global_step = 0
            if ckpt and ckpt.model_checkpoint_path:
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split(
                    '-')[-1]
                saver.restore(sess, ckpt.model_checkpoint_path)
                print('Loading success, global_step is %s' % global_step)

            for i in range(MAX_STEP + 1):

                #if_train = True
                pos = i % batch_train_len
                _, acc, loss = sess.run([train_step, train_acc, train_loss],
                                        feed_dict={
                                            x: x_train_batch[pos],
                                            y: y_train_batch[pos]
                                        })

                #每50步打印一次准确率和损失函数
                if i % 50 == 0:
                    print(
                        'Step %d, train loss = %.2f, train accuracy = %.2f%%' %
                        (i, loss, acc * 100.0))

                #每200步用验证集的数据进行验证
                if i % 200 == 0:
                    #if_train = False    #量化模式下用变量替代占位符.注意 如果要用tflite的话,if_train不要用占位符!
                    vpos = valstep % batch_val_len
                    val_loss, val_acc = sess.run([train_loss, train_acc],
                                                 feed_dict={
                                                     x: x_val_batch[vpos],
                                                     y: y_val_batch[vpos]
                                                 })

                    valstep = valstep + 1
                    print(
                        '**  Step %d, val loss = %.2f, val accuracy = %.2f%%  **'
                        % (i, val_loss, val_acc * 100.0))

                #每500步保存一次变量值
                if i % 500 == 0:
                    checkpoint_path = os.path.join(TRAIN_LOGS_DIR,
                                                   'saved_model.ckpt')
                    tmpstep = i + int(global_step)
                    saver.save(sess, checkpoint_path, global_step=tmpstep)

        except tf.errors.OutOfRangeError:
            print('Done training -- epoch limit reached')
        finally:
            coord.request_stop()
        coord.join(threads)
Ejemplo n.º 19
0
 def _build_network(self):
     return build_network(self.state_shape, self.nb_actions, self.device, self.network_size)
Ejemplo n.º 20
0
def main(xp_path,
         network,
         optimizer_name,
         c,
         eta,
         lr,
         n_epochs,
         batch_size,
         lr_milestones,
         weight_decay,
         ae_optimizer_name,
         ae_lr,
         ae_n_epochs,
         ae_lr_milestone,
         ae_batch_size,
         ae_weight_decay,
         device,
         n_jobs_dataloader,
         stage_n_degc=None):
    """
        xp_path : 결과물 출력할 폴더의 절대 경로
    """

    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)
    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    log_file = xp_path + '/log.txt'
    export_model = xp_path + '/models/DeepSADModel.tar'
    file_handler = logging.FileHandler(log_file)
    file_handler.setLevel(logging.INFO)
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)

    train_set = LGDataset(root='/workspace/eddie/ai_championship/data',
                          dataset_name='lg_train',
                          train=True,
                          random_state=None,
                          stage_n_degc=False)

    idx, _, semi_targets = create_semisupervised_setting(
        train_set.targets.cpu().data.numpy(),
        normal_classes=(0, ),
        outlier_classes=(1, ),
        known_outlier_classes=(),
        ratio_known_normal=0,
        ratio_known_outlier=0,
        ratio_pollution=0)

    train_set.semi_targets[idx] = torch.tensor(semi_targets, dtype=torch.int32)
    train_set = Subset(train_set, idx)

    test_set = LGDataset(root='/workspace/eddie/ai_championship/data',
                         dataset_name='lg_train',
                         train=False,
                         random_state=None,
                         stage_n_degc=False)

    ae_train = AETrainer(ae_optimizer_name, ae_lr, ae_n_epochs,
                         ae_lr_milestone, ae_batch_size, ae_weight_decay,
                         device, n_jobs_dataloader)

    deep_sad_train = DeepSADTrainer(c, eta, optimizer_name, lr, n_epochs,
                                    lr_milestones, batch_size, weight_decay,
                                    device, n_jobs_dataloader)

    # Register your network in model.py
    ae_net = build_network('LG_LeNet_Autoencoder')
    net = build_network('LG_LeNet')
    ae_net = ae_train.train(train_set, ae_net)

    net_dict = net.state_dict()
    ae_net_dict = ae_net.state_dict()

    ae_net_dict = {k: v for k, v in ae_net_dict.items() if k in net_dict}
    # Overwrite values in the existing state_dict
    net_dict.update(ae_net_dict)
    # Load the new state_dict
    net.load_state_dict(net_dict)

    net, c = deep_sad_train.train(train_set, net)

    outlier_dist = deep_sad_train.test(test_set, net)

    # save model
    net_dict = net.state_dict()
    ae_net_dict = ae_net.state_dict()

    torch.save(
        {
            'c': c,
            'net_dict': net_dict,
            'ae_net_dict': ae_net_dict,
            'outlier_dist': outlier_dist
        }, export_model)
Ejemplo n.º 21
0
def main(argv):
    #load data
    test_data_1 = np.load(FLAGS.data_dir + 'test_x_1.npy')
    test_data_2 = np.load(FLAGS.data_dir + 'test_x_2.npy')
    test_data_3 = np.load(FLAGS.data_dir + 'test_x_3.npy')
    test_data_4 = np.load(FLAGS.data_dir + 'test_x_4.npy')
    test_data = [test_data_1, test_data_2, test_data_3, test_data_4]

    test_labels_1 = np.load(FLAGS.data_dir + 'test_y_1.npy')
    test_labels_2 = np.load(FLAGS.data_dir + 'test_y_2.npy')
    test_labels_3 = np.load(FLAGS.data_dir + 'test_y_3.npy')
    test_labels_4 = np.load(FLAGS.data_dir + 'test_y_4.npy')
    test_labels = [test_labels_1, test_labels_2, test_labels_3, test_labels_4]

    train_data_1 = np.load(FLAGS.data_dir + 'train_x_1.npy')
    train_data_2 = np.load(FLAGS.data_dir + 'train_x_2.npy')
    train_data_3 = np.load(FLAGS.data_dir + 'train_x_3.npy')
    train_data_4 = np.load(FLAGS.data_dir + 'train_x_4.npy')
    train_data = [train_data_1, train_data_2, train_data_3, train_data_4]

    train_labels_1 = np.load(FLAGS.data_dir + 'train_y_1.npy')
    train_labels_2 = np.load(FLAGS.data_dir + 'train_y_2.npy')
    train_labels_3 = np.load(FLAGS.data_dir + 'train_y_3.npy')
    train_labels_4 = np.load(FLAGS.data_dir + 'train_y_4.npy')
    train_labels = [
        train_labels_1, train_labels_2, train_labels_3, train_labels_4
    ]

    #count data
    test_count = [
        test_data[0].shape[0], test_data[1].shape[0], test_data[2].shape[0],
        test_data[3].shape[0]
    ]
    train_count = [
        train_data[0].shape[0], train_data[1].shape[0], train_data[2].shape[0],
        train_data[3].shape[0]
    ]

    #specify model
    input_placeholder = tf.placeholder(tf.float32, [None, 16641],
                                       name='input_placeholder')
    my_network = tf.identity(model.build_network(input_placeholder),
                             name='output2')

    #define classification loss
    #code adapted from Paul Quint's hackathon 3
    REG_COEFF = 0.0001
    labels = tf.placeholder(tf.float32, [None, 7], name='labels')
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=labels,
                                                            logits=my_network)
    confusion_matrix_op = tf.confusion_matrix(tf.argmax(labels, axis=1),
                                              tf.argmax(my_network, axis=1),
                                              num_classes=7)
    regularization_losses = tf.get_collection(
        tf.GraphKeys.REGULARIZATION_LOSSES)
    total_loss = cross_entropy + REG_COEFF * sum(regularization_losses)

    #set up training and saving
    #code adapted from Paul Quint's hackathon 3
    global_step_tensor = tf.get_variable('global_step',
                                         trainable=False,
                                         shape=[],
                                         initializer=tf.zeros_initializer)
    optimizer = tf.train.AdamOptimizer()
    train_op = optimizer.minimize(total_loss, global_step=global_step_tensor)
    saver = tf.train.Saver()
    sum_cross_entropy = tf.reduce_mean(cross_entropy)

    EPOCHS_BEFORE_STOPPING = 12

    #run the actual training
    #code adapted from Paul Quint's hackathon 3
    with tf.Session() as session:
        session.run(tf.global_variables_initializer())
        best_test_conf_mxs = []
        best_epoch = [0, 0, 0, 0]
        best_test_ce = [10, 10, 10, 10]
        best_train_ce = [0, 0, 0, 0]
        best_classification_rate = [0, 0, 0, 0]
        epochs_since_best = [0, 0, 0, 0]

        for k in range(0, 4):
            session.run(tf.global_variables_initializer())
            batch_size = FLAGS.batch_size
            print("\n !!!!! NEW K (" + str(k) + ") !!!!!\n")
            for epoch in range(FLAGS.max_epoch_num):
                print("################### EPOCH " + str(epoch) +
                      " #####################")
                print("##################################################\n")

                ce_vals = []
                for i in range(train_count[k] // batch_size):
                    batch_data = train_data[k][i * batch_size:(i + 1) *
                                               batch_size, :]
                    batch_labels = train_labels[k][i * batch_size:(i + 1) *
                                                   batch_size]
                    _, train_ce = session.run([train_op, sum_cross_entropy], {
                        input_placeholder: batch_data,
                        labels: batch_labels
                    })
                    ce_vals.append(train_ce)
                avg_train_ce = sum(ce_vals) / len(ce_vals)
                best_train_ce[k] = avg_train_ce
                print('TRAIN CROSS ENTROPY: ' + str(avg_train_ce))

                print("\n##################################################")

            # run gradient steps and report mean loss on train data
            ce_vals = []
            conf_mxs = []
            for i in range(test_count[k] // batch_size):
                batch_data = test_data[k][i * batch_size:(i + 1) *
                                          batch_size, :]
                batch_labels = test_labels[k][i * batch_size:(i + 1) *
                                              batch_size]
                test_ce, conf_matrix = session.run(
                    [sum_cross_entropy, confusion_matrix_op], {
                        input_placeholder: batch_data,
                        labels: batch_labels
                    })
                ce_vals.append(test_ce)
                conf_mxs.append(conf_matrix)
            avg_test_ce = sum(ce_vals) / len(ce_vals)
            classification_rate = util.classification_rate(sum(conf_mxs), 7)
            print('TEST CROSS ENTROPY: ' + str(avg_test_ce))
            print('TEST CONFUSION MATRIX:')
            print(str(sum(conf_mxs)))
            print('TEST CLASSIFICATION RATE:' + str(classification_rate))
            best_test_conf_mxs.append(sum(conf_mxs))
            best_test_ce[k] = avg_test_ce
            best_classification_rate[k] = classification_rate

        print('Confusion Matrix: ')
        print(str(sum(best_test_conf_mxs)))
        print('Avg Test CE: ' + str(np.average(best_test_ce)))
        print('Avg Train CE: ' + str(np.average(best_train_ce)))
        print('Avg Classification Rate: ' +
              str(np.average(best_classification_rate)))
        print('Generating model now...')
        session.run(tf.global_variables_initializer())

        for j in range(0, 4):
            for epoch in range(FLAGS.max_epoch_num):
                for i in range(train_count[j] // batch_size):
                    batch_data = train_data[j][i * batch_size:(i + 1) *
                                               batch_size, :]
                    batch_labels = train_labels[j][i * batch_size:(i + 1) *
                                                   batch_size]
                    _, train_ce = session.run([train_op, sum_cross_entropy], {
                        input_placeholder: batch_data,
                        labels: batch_labels
                    })

        saver.save(session, FLAGS.save_dir)
        print('Model is generated and saved')
Ejemplo n.º 22
0
    parser.add_argument('-checkpoint', default='training/dev=0.02/checkpoints/epoch=100', help='Path for the checkpoint of the trained model')

    # Parse arguments from command line
    args = parser.parse_args()

    # Setup parameters
    d                       = vars(args)['d']
    time_steps              = vars(args)['time_steps']
    target_cost_dev         = vars(args)['dev']

    # Create instance loader
    loader = InstanceLoader(vars(args)['instances'])

    # Build model
    print('Building model ...', flush=True)
    GNN = build_network(d)

    # Disallow GPU use
    config = tf.ConfigProto( device_count = {'GPU':0})
    with tf.Session(config=config) as sess:

        # Initialize global variables
        print("Initializing global variables ... ", flush=True)
        sess.run( tf.global_variables_initializer() )

        # Restore saved weights
        load_weights(sess,vars(args)['checkpoint']);

        n_instances = len(loader.filenames)
        stats = { k:np.zeros(n_instances) for k in ['loss','acc','sat','pred','TP','FP','TN','FN'] }
Ejemplo n.º 23
0
                losses.append((d_loss.item(), g_loss.item()))
                # print discriminator and generator loss
                print(
                    'Epoch [{:5d}/{:5d}] | d_loss: {:6.4f} | g_loss: {:6.4f}'.
                    format(epoch + 1, n_epochs, d_loss.item(), g_loss.item()))

        ## AFTER EACH EPOCH##
        # this code assumes your generator is named G, feel free to change the name
        # generate and save sample, fake images
        G.eval()  # for generating samples
        samples_z = G(fixed_z)
        samples.append(samples_z)
        G.train()  # back to training mode

    # Save training generator samples
    with open('train_samples.pkl', 'wb') as f:
        pkl.dump(samples, f)

    # finally return losses
    return losses


if __name__ == '__main__':
    D, G = build_network(args.d_conv_dim, args.g_conv_dim, args.z_size)
    d_optimizer = optim.Adam(D.parameters(), args.lr, (args.beta1, args.beta2))
    g_optimizer = optim.Adam(G.parameters(), args.lr, (args.beta1, args.beta2))

    celeba_train_loader = get_dataloader(args.batch_size, args.img_size)

    losses = train(D, G, n_epochs=args.n_epochs)
Ejemplo n.º 24
0
if args.logLevel:
    logging.basicConfig()
    logging.getLogger().setLevel(logging.DEBUG)
else:
    logging.basicConfig(level=getattr(logging, 'INFO'))

###############################################################################
# Begin actual work
###############################################################################

BATCH_SIZE = args.BATCH_SIZE
NUM_WORKERS = args.NUM_WORKERS

# Build the model
model = build_network()


def generator():
    for s1, s2, s3 in zip(ds_a, ds_p, ds_n):
        yield {
            "anchor": format_example(s1, img_size=args.patch_size),
            "pos_img": format_example(s2, img_size=args.patch_size),
            "neg_img": format_example(s3, img_size=args.patch_size)
        }, [1, 1, 0]


def vgenerator():
    for s1, s2, s3 in zip(vds_a, vds_p, vds_n):
        yield {
            "anchor": format_example(s1, img_size=args.patch_size),
Ejemplo n.º 25
0
def train():
    os.makedirs('/work/ckpt/1-triplets/training-from-scratch/opt/', exist_ok=True)
    os.makedirs('/work/ckpt/1-triplets/training-from-scratch/progress/', exist_ok=True)

    tf.logging.info('building model...')

    image_shape = [299, 299, 3]
    batch_shape = [None] + image_shape

    with tf.device(FLAGS.device):
        i_embedding = tf.placeholder(tf.float32, batch_shape, 'input_embedding')
        i_a = tf.placeholder(tf.float32, batch_shape, 'input_anchors')
        i_p = tf.placeholder(tf.float32, batch_shape, 'input_positives')
        i_n = tf.placeholder(tf.float32, batch_shape, 'input_negatives')

        f_embedding = build_network(i_embedding, is_training=True)
        f_a = build_network(i_a, is_training=True, reuse=True)
        f_p = build_network(i_p, is_training=True, reuse=True)
        f_n = build_network(i_n, is_training=True, reuse=True)
        loss_op = triplet_loss(f_a, f_p, f_n)

        tf.summary.scalar("loss", loss_op)
        merged_summary_op = tf.summary.merge_all()

        gs = tf.Variable(0, trainable=False)
        lr = tf.train.exponential_decay(0.001, gs, 20000, .5)
        opt_op = tf.train.AdamOptimizer(lr).minimize(loss_op)

    optimal_saver = tf.train.Saver(max_to_keep=1)
    progress_saver = tf.train.Saver()
    best_val_loss = np.inf

    g = ImageDataGenerator(rescale=1. / 255.)
    train_data = g.flow_from_directory(
        os.path.join(FLAGS.data_dir, 'extracted_patches', 'train'),
        target_size=image_shape[:2], augmentations=('brightness', 'contrast'),
        batch_size=FLAGS.window_size, shuffle=True, seed=None)
    valid_data = g.flow_from_directory(
        os.path.join(FLAGS.data_dir, 'extracted_patches', 'valid'),
        target_size=image_shape[:2], augmentations=('brightness', 'contrast'),
        batch_size=FLAGS.window_size, shuffle=True, seed=None)

    train_data = triplets_gen_from_gen(train_data, i_embedding, f_embedding,
                                       batch_size=FLAGS.batch_size)
    valid_data = triplets_gen_from_gen(valid_data, i_embedding, f_embedding,
                                       batch_size=FLAGS.batch_size)

    config = tf.ConfigProto(allow_soft_placement=True)
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as s:
        tf.global_variables_initializer().run()

        summary_writer = tf.summary.FileWriter(FLAGS.logs_dir, graph=tf.get_default_graph())

        tf.logging.info('training...')

        for it in range(FLAGS.nb_iterations):
            a, p, n = next(train_data)

            _, _train_loss, summary = s.run([opt_op, loss_op, merged_summary_op],
                                            feed_dict={i_a: a, i_p: p, i_n: n})

            summary_writer.add_summary(summary, it)

            if not it % FLAGS.nb_val_interval:
                print('#%i train_loss: %f' % (it, _train_loss), end=' ')

                val_loss = val_samples_seen = 0
                val_batches_seen = 0
                while val_samples_seen < FLAGS.nb_val_samples:
                    a, p, n = next(valid_data)
                    val_loss += s.run(loss_op, feed_dict={i_a: a, i_p: p, i_n: n})
                    val_batches_seen += 1
                    val_samples_seen += len(a)
                val_loss /= val_batches_seen

                print('val_loss: %f' % val_loss)

                if val_loss < best_val_loss:
                    tf.logging.info('val_acc improved from %.4f to %.4f', best_val_loss, val_loss)
                    optimal_saver.save(s, '/work/ckpt/1-triplets/training-from-scratch/opt/'
                                          'opt.ckpt', global_step=gs)
                    best_val_loss = val_loss

            if not it % FLAGS.nb_save_interval:
                progress_saver.save(s, '/work/ckpt/1-triplets/training-from-scratch/progress/'
                                       'progress.ckpt', global_step=gs)