def trainer():
    global load_thread
    load_thread.start()
    X = tf.placeholder("float", [None, 224, 224, 3])
    Y = tf.placeholder("float", [None, n_classes])
    # 构建模型
    oldpred = al.getalex(X, keep_prob)
    sess2 = tf.Session()
    sess2.run(tf.initialize_all_variables())
    saver = tf.train.Saver()
    saver.restore(
        sess2,
        "/home/jess/Disk1/ilsvrc12/tf_resnet_ccnn_model_iter252501.ckpt-252501"
    )
    xx = tf.trainable_variables()
    sess2.close()

    import alexccnnmodel as al2
    pred = al2.getccnn(X, keep_prob, xx, xx)
    cost = -tf.reduce_sum(Y * tf.log(pred))
    #cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, Y))
    optimizer = tf.train.GradientDescentOptimizer(
        learning_rate=learning_rate).minimize(cost)
    correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(Y, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    info2 = tf.argmax(pred, 1)
    # 初始化所有的共享变量
    init = tf.initialize_all_variables()
    # 开启一个训练
    #conv1 = al.conv2d('conv11', x, al.weights['wc1'], al.biases['bc1'], 4, 'VALID')
    saver = tf.train.Saver()
    with tf.Session() as sess:
        sess.run(init)
        global step
        global data_queue
        global lr
        lr = 1e-3
        step = 0

        # Keep training until reach max iterations

        while step * batch_size < training_iters:
            epcho1 = np.floor((step * batch_size) / glen1)
            if (((step * batch_size) % glen1 < batch_size) & (epcho1 > 0) &
                (epcho1 % 10 == 0)):
                lr /= 10

            batch_xs, batch_ys = data_queue.get()
            # 获取批数据
            sess.run(optimizer,
                     feed_dict={
                         X: batch_xs,
                         Y: batch_ys,
                         keep_prob: dropout,
                         learning_rate: lr
                     })
            if (step % 2500 == 1) & (step > 2500):
                save_path = saver.save(
                    sess,
                    "/home/jess/Disk1/ilsvrc12/tf_alex_pre_ccnn_model_iter" +
                    str(step) + ".ckpt",
                    global_step=step)
                print("Model saved in file at iteration %d: %s" %
                      (step * batch_size, save_path))
                acc2 = 0.0
                for kk in range(0, vlen1 / batch_size):
                    batch_xs2, batch_ys2 = val_batch(batch_size, kk)
                    acc = sess.run(accuracy,
                                   feed_dict={
                                       X: batch_xs2,
                                       Y: batch_ys2,
                                       keep_prob: 1.
                                   })
                    acc2 = acc2 + acc
                print "Validation accuracy: " + str(acc2 /
                                                    (vlen1 / batch_size))
            if step % display_step == 1:
                # 计算损失值
                loss = sess.run(cost,
                                feed_dict={
                                    X: batch_xs,
                                    Y: batch_ys,
                                    keep_prob: 1.
                                })
                acc = sess.run(accuracy,
                               feed_dict={
                                   X: batch_xs,
                                   Y: batch_ys,
                                   keep_prob: 1.
                               })
                #info=sess.run(info2,feed_dict={X:batch_xs, keep_prob:1.})
                #print(info)
                print "Iter=" + str(step * batch_size) + "/epcho=" + str(
                    np.floor(
                        (step * batch_size) /
                        glen1)) + ", Loss= " + "{:.6f}".format(
                            loss) + ", Training Accuracy=" + "{:.5f}".format(
                                acc) + ", lr=" + str(lr)
            step += 1
        print "Optimization Finished!"
        #saver.save(sess, 'jigsaw', global_step=step)
        save_path = saver.save(
            sess,
            "/home/jess/Disk1/ilsvrc12/tf_alex_pre_ccnn_model.ckpt",
            global_step=step)
        print("Model saved in file: %s" % save_path)
    threading.Thread._Thread__stop(load_thread)
Пример #2
0
def trainer():
    global load_thread
    global seq1
    global step
    global data_queue
    global lr
    tii = time.time()
    config = tf.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.5

    X = tf.placeholder("float", [None, 224, 224, 3])
    sX = tf.placeholder("float", [None, 7, 7])
    Y = tf.placeholder("float", [None, n_classes])
    X2 = tf.placeholder(tf.float32, [None, 224, 224, 3])

    # 构建模型
    oldpred = al.getalex(X, keep_prob)
    sess2 = tf.Session(config=config)
    sess2.run(tf.initialize_all_variables())
    saver = tf.train.Saver()
    saver.restore(sess2, "/Data/jess/tf_pretrained.ckpt")
    xx = tf.trainable_variables()
    sess2.close()

    import alexccnnmodel as al2
    biasesInd = 44
    weightsInd = 34
    B1 = tf.placeholder("float", al2.biases['bc9'].get_shape().as_list())
    updateB = al2.biases['bc9'].assign(B1)

    W1 = tf.placeholder("float", al2.weights['wc9'].get_shape().as_list())
    updateW = al2.weights['wc9'].assign(W1)

    sess2 = tf.Session(config=config)
    sess2.run(tf.initialize_all_variables())
    fc8 = al2.getccnn(X, keep_prob, xx, xx)
    saver.restore(sess2,
                  "/Data/jess/tf_alex_pre_ccnn_model_iter285001.ckpt-285001")
    xx2 = tf.trainable_variables()
    sess2.close()

    with tf.Session(config=config) as sess:

        w10 = tf.Variable(tf.random_normal([1000, n_classes], stddev=0.01))
        b10 = tf.Variable(tf.random_normal([n_classes], stddev=0.01))
        pred = al2.alex_ccnn10(X, xx2, xx2, keep_prob, w10, b10, o_layer="out")
        fcb = al2.alex_ccnn10(X, xx2, xx2, keep_prob, w10, b10, o_layer="fcb")

        cost = -tf.reduce_sum(Y * tf.log(pred))
        optimizer = tf.train.GradientDescentOptimizer(
            learning_rate=learning_rate).minimize(cost)
        correct_prediction = tf.equal(tf.argmax(pred, 1), tf.argmax(Y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

        #==========Get Init Center====================@
        #~ centers, lab1=val_batch(n_classes, ss=0)
        #~ rn.shuffle(seq1)
        saver = tf.train.Saver()
        gtlab = np.zeros((vlen1))
        predicts = np.zeros((vlen1))

        lr = 1e-3
        step = 1
        len2 = len(xx2)
        myvarlist = [xx2[weightsInd], xx2[biasesInd]]
        myvar = tf.trainable_variables()
        for kk in range(len2):
            print(
                str(kk) + "th data is " + str(xx2[kk].get_shape().as_list()) +
                " with " + str(type(xx2[kk])))

        varxx = tf.gradients(cost, myvarlist)

        print(varxx)
        sess.run(tf.initialize_all_variables())
        lastgrad = []
        # Total epochs we have to reach.
        allRuntime = 0
        saver.restore(sess, '/Data/tf_place_val_step_454791.ckpt')
        step = 454791
        vst = int(np.floor((step * batch_size) / vlen1))
        centers, lab1 = val_batch(n_classes, ss=0)

        for v in range(vst, 1000):
            ts = time.time()

            rn.shuffle(seq1)
            load_thread = threading.Thread(target=load_data)
            load_thread.deamon = True
            load_thread.start()
            cfeat = sess.run(fcb, feed_dict={X: centers, keep_prob: 1.})

            featLen = cfeat.shape[1]
            count1 = np.zeros((n_classes))

            acc_batch_x = np.zeros((batch_size, 224, 224, 3))
            acc_batch_y = np.zeros((batch_size, n_classes))
            bct = 0
            for k in range(0, vlen1, batch_size):
                epcho1 = np.floor((step * batch_size) / vlen1)
                #============Extract feature==================
                batch_sx, batch_ys = data_queue.get()
                fea1 = sess.run(fcb, feed_dict={X: batch_sx, keep_prob: 1.})
                gtlab[k:k + batch_size] = batch_ys.argmax(axis=1)
                b = batch_size
                b2 = 2
                p_lab = np.zeros((b))
                p_lab2 = np.zeros((b, n_classes))
                p_err = np.zeros((b))
                for j in range(b):
                    diff1 = pow(pow(fea1[j, :] - cfeat, 2.0), 0.5)
                    #~ pdb.set_trace()
                    diff1 = diff1.sum(axis=1)
                    p_lab[j] = diff1.argmin()
                    p_lab2[j, int(p_lab[j])] = 1
                    count1[int(p_lab[j])] += 1
                    p_err[j] = min(diff1)

                predicts[k:k + b] = p_lab
                # Use the most realible set to update network parameters
                acc_ind = sorted(range(len(p_err)), key=lambda k: p_err[k])
                myidn = acc_ind[0:b2]
                acc_batch_x[bct:bct + b2, :, :, :] = batch_sx[myidn, :, :, :]
                acc_batch_y[bct:bct + b2, :] = p_lab2[myidn, :]

                #~ acc_batch_x = batch_sx
                #~ acc_batch_y = p_lab2
                bct = bct + b2
                perrave = p_err.sum(axis=0) / batch_size

                #When the size of the collected training sample is larger than batch size, performing network updating -*-
                if (bct >= batch_size):
                    bct = 0
                    #===========Update network====================
                    sess.run(optimizer,
                             feed_dict={
                                 X: acc_batch_x,
                                 Y: acc_batch_y,
                                 keep_prob: dropout,
                                 learning_rate: lr
                             })

                    #===========Update center======================
                    #~ hh=input("Input any word to continue...")
                    var_grad = sess.run(varxx,
                                        feed_dict={
                                            X: acc_batch_x,
                                            Y: acc_batch_y,
                                            keep_prob: 1.
                                        })
                    allvar = sess.run(myvar,
                                      feed_dict={
                                          X: acc_batch_x,
                                          Y: acc_batch_y,
                                          keep_prob: 1.
                                      })
                    if (lastgrad != []):
                        bct = 0
                        sess.run(updateB,
                                 feed_dict={
                                     B1: allvar[biasesInd] + lr * lastgrad[1]
                                 })
                        sess.run(updateW,
                                 feed_dict={
                                     W1: allvar[weightsInd] + lr * lastgrad[0]
                                 })
                        fea1 = sess.run(fcb,
                                        feed_dict={
                                            X: acc_batch_x,
                                            keep_prob: 1.
                                        })
                        sess.run(updateB, feed_dict={B1: allvar[biasesInd]})
                        sess.run(updateW, feed_dict={W1: allvar[weightsInd]})
                        #print  str(k) + "th ==> " + str(lastgrad[1][1:5])
                    lastgrad = var_grad

                for j in myidn:
                    idx1 = int(p_lab[j])
                    lr2 = 1.0 / count1[idx1]
                    cfeat[idx1, :] = (1 -
                                      lr2) * cfeat[idx1, :] + lr2 * fea1[j, :]

                #for j in range(n_classes):
                #cfeat[j, :] = cfeat[j, :] + lr*var_grad

                if step % display_step == 1:
                    # 计算损失值
                    loss = sess.run(cost,
                                    feed_dict={
                                        X: batch_sx,
                                        Y: batch_ys,
                                        keep_prob: 1.
                                    })
                    acc = sess.run(accuracy,
                                   feed_dict={
                                       X: batch_sx,
                                       Y: batch_ys,
                                       keep_prob: 1.
                                   })

                    print("Iter=" + str(step * batch_size) + "/epcho=" +
                          str(np.floor((step * batch_size) / vlen1)) +
                          ", Loss= " + "{:.6f}".format(loss) +
                          ", Training Accuracy=" + "{:.5f}".format(acc) +
                          ", lr=" + str(lr) + ", Sec.=" +
                          "{:.5f}".format(time.time() - tii))
                    #print "This stage has " + str(acc_batch_y.shape[0]) + " samples to be used to update the network."
                    tii = time.time()
                step += 1
            #input()
            nmi = mi(predicts, gtlab)
            td = time.time() - ts
            allRuntime = allRuntime + td
            print(
                str(v) + "th epoch's NMI to mini-batch kmeans is " + str(nmi) +
                ". Time: " + str(td) + " sec.")
            save_path = saver.save(
                sess, "/Data/tf_place_val_step_" + str(step) + ".ckpt")
            print("Model saved in file: %s" % save_path)
            threading.Thread._Thread__stop(load_thread)

    threading.Thread._Thread__stop(load_thread)