예제 #1
0
def train_model(arch_index,npy_path,test_path,batch_size = 32):

        highest_acc = 0.0
        highest_iterator = 1
        all_filenames = get_all_filename(npy_path,cubic_shape[arch_index][1])
        print("file size is :\t",len(all_filenames))
        # how many time should one epoch should loop to feed all data
        times = int(len(all_filenames) / batch_size)
        if (len(all_filenames) % batch_size) != 0:
            times = times + 1

        # keep_prob used for dropout
        keep_prob = tf.placeholder(tf.float32)
        # take placeholder as input
        x = tf.placeholder(tf.float32, [None, cubic_shape[arch_index][0], cubic_shape[arch_index][1], cubic_shape[arch_index][2]])
        x_image = tf.reshape(x, [-1, cubic_shape[arch_index][0], cubic_shape[arch_index][1], cubic_shape[arch_index][2], 1])
        if arch_index == 1:
            net = arch1(x_image)
        elif arch_index == 2:
            net = arch2(x_image)
        elif arch_index == 3:
            net = arch3(x_image)
        else:
            print("model architecture index must be 1 or 2 or 3,current is %s which is not supported"%(str(arch_index)))
            return

        saver = tf.train.Saver()  # default to save all variable,save mode or restore from path
        # softmax layer
        real_label = tf.placeholder(tf.float32, [None, 2])
        cross_entropy = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(logits=net, labels=real_label))
        net_loss = tf.reduce_mean(cross_entropy)
        train_step = tf.train.MomentumOptimizer(learning_rate, 0.9).minimize(net_loss)
        correct_prediction = tf.equal(tf.argmax(net, 1), tf.argmax(real_label, 1))
        accruacy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        merged = tf.summary.merge_all()

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            train_writer = tf.summary.FileWriter('./arch-%d-tensorboard/'%(arch_index), sess.graph)
            # loop epoches
            for i in range(epoch):
                epoch_start =time.time()
                #  the data will be shuffled by every epoch
                random.shuffle(all_filenames)
                for t in range(times):
                    batch_files = all_filenames[t*batch_size:(t+1)*batch_size]
                    batch_data, batch_label = get_train_batch(batch_files)
                    feed_dict = {x: batch_data, real_label: batch_label,keep_prob:0.8}
                    _,summary = sess.run([train_step, merged],feed_dict =feed_dict)
                    train_writer.add_summary(summary, i)
                    saver.save(sess, './arch-%d-ckpt/arch-%d'%(arch_index,arch_index), global_step=i + 1)

                epoch_end = time.time()
                test_batch,test_label = get_test_batch(test_path)
                print("test batch data:\t",test_batch)
                print("test batch label:\t", test_label)
                test_dict = {x: test_batch, real_label: test_label, keep_prob:1.0}
                acc_test,loss = sess.run([accruacy,net_loss],feed_dict=test_dict)
                print('accuracy  is %f' % acc_test)
                print("loss is ", loss)
                print(" epoch %d time consumed %f seconds"%(i,(epoch_end-epoch_start)))

            print("training finshed..highest accuracy is %f,the iterator is %d " % (highest_acc, highest_iterator))
예제 #2
0
    def inference(self,npy_path,test_path,model_index,train_flag=True):

        # some statistic index
        highest_acc = 0.0
        highest_iterator = 1

        all_filenames = get_all_filename(npy_path,self.cubic_shape[model_index][1])
        # how many time should one epoch should loop to feed all data
        times = len(all_filenames) / self.batch_size
        if (len(all_filenames) % self.batch_size) != 0:
            times = times + 1

        # keep_prob used for dropout
        keep_prob = tf.placeholder(tf.float32)
        # take placeholder as input
        x = tf.placeholder(tf.float32, [None, self.cubic_shape[model_index][0], self.cubic_shape[model_index][1], self.cubic_shape[model_index][2]])
        x_image = tf.reshape(x, [-1, self.cubic_shape[model_index][0], self.cubic_shape[model_index][1], self.cubic_shape[model_index][2], 1])
        net_out = self.archi_1(x_image,keep_prob)

        saver = tf.train.Saver()  # default to save all variable,save mode or restore from path

        if train_flag:
            # softmax layer
            real_label = tf.placeholder(tf.float32, [None, 2])
            cross_entropy = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(net_out, real_label))
            #cross_entropy = -tf.reduce_sum(real_label * tf.log(net_out))
            net_loss = tf.reduce_mean(cross_entropy)

            train_step = tf.train.MomentumOptimizer(self.learning_rate, 0.9).minimize(net_loss)

            correct_prediction = tf.equal(tf.argmax(net_out, 1), tf.argmax(real_label, 1))
            accruacy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

            merged = tf.summary.merge_all()

            with tf.Session() as sess:
                #sess = tf_debug.LocalCLIDebugWrapperSession(sess)
                #sess.add_tensor_filter("has_inf_or_nan", tf_debug.has_inf_or_nan)
                sess.run(tf.global_variables_initializer())
                train_writer = tf.summary.FileWriter('./tensorboard/', sess.graph)
                # loop epoches
                for i in range(self.epoch):
                    epoch_start =time.time()
                    #  the data will be shuffled by every epoch
                    random.shuffle(all_filenames)
                    for t in range(times):
                        batch_files = all_filenames[t*self.batch_size:(t+1)*self.batch_size]
                        batch_data, batch_label = get_train_batch(batch_files)
                        feed_dict = {x: batch_data, real_label: batch_label,
                                     keep_prob: self.keep_prob}
                        _,summary = sess.run([train_step, merged],feed_dict =feed_dict)
                        train_writer.add_summary(summary, i)
                        saver.save(sess, './ckpt/archi-1', global_step=i + 1)

                    epoch_end = time.time()
                    test_batch,test_label = get_test_batch(test_path)
                    test_dict = {x: test_batch, real_label: test_label, keep_prob:self.keep_prob}
                    acc_test,loss = sess.run([accruacy,net_loss],feed_dict=test_dict)
                    print('accuracy  is %f' % acc_test)
                    print("loss is ", loss)
                    print(" epoch %d time consumed %f seconds"%(i,(epoch_end-epoch_start)))

            print("training finshed..highest accuracy is %f,the iterator is %d " % (highest_acc, highest_iterator))
예제 #3
0
def train_model(arch_index, npy_path, test_path, batch_size=256):

    highest_acc = 0.0
    highest_iterator = 1
    initial_learning_rate = 0.9

    all_train_filenames = get_all_filename(npy_path,
                                           cubic_shape[arch_index][1])
    # ensure that number of real nodule sample and fake nodule are equal
    real_file_list = []
    fake_file_list = []
    real_num = 0
    for file in all_train_filenames:
        if "real" in file:
            real_num = real_num + 1
            real_file_list.append(file)
        elif "fake" in file:
            fake_file_list.append(file)
    print("size of real file ", len(real_file_list))
    print("size of fake file ", len(fake_file_list))
    fake_file_list = random.sample(fake_file_list, real_num)
    print("size of fake file(after random choice) ", len(fake_file_list))
    all_train_filenames = real_file_list + fake_file_list

    all_test_filenames = get_all_filename(test_path,
                                          cubic_shape[arch_index][1])
    all_train_filenames = all_train_filenames[:20000]
    print("file size is :\t", len(all_train_filenames))
    # how many time should one epoch should loop to feed all data
    times = int(len(all_train_filenames) / batch_size)
    if (len(all_train_filenames) % batch_size) != 0:
        times = times + 1
    # keep_prob used for dropout
    keep_prob = tf.placeholder(tf.float32)
    # take placeholder as input
    x = tf.placeholder(tf.float32, [
        None, cubic_shape[arch_index][0], cubic_shape[arch_index][1],
        cubic_shape[arch_index][2]
    ])
    x_image = tf.reshape(x, [
        -1, cubic_shape[arch_index][0], cubic_shape[arch_index][1],
        cubic_shape[arch_index][2], 1
    ])
    if arch_index == 0:
        net = arch1(x_image, keep_prob)
    elif arch_index == 1:
        net = arch2(x_image, keep_prob)
    elif arch_index == 2:
        net = arch3(x_image, keep_prob)
    else:
        print(
            "model architecture index must be 0 or 1 or 2,current is %s which is not supported"
            % (str(arch_index)))
        return

    saver = tf.train.Saver(
    )  # default to save all variable,save mode or restore from path
    # softmax layer
    real_label = tf.placeholder(tf.float32, [None, 2])
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=net,
                                                            labels=real_label)
    #cross_entropy = tf.reduce_mean(-tf.reduce_sum(real_label * tf.log(net), axis=1))
    net_loss = tf.reduce_mean(cross_entropy)
    tf.summary.scalar('net loss', net_loss)
    global_step = tf.Variable(0, trainable=False)
    # decayed_learning_rate = learning_rate *decay_rate^ (global_step / decay_steps)
    learning_rate = tf.train.exponential_decay(initial_learning_rate,
                                               global_step=global_step,
                                               decay_steps=5000,
                                               decay_rate=0.95)
    train_step = tf.train.MomentumOptimizer(learning_rate,
                                            momentum=0.9).minimize(net_loss)
    tf.summary.scalar("learing_rate", learning_rate)
    correct_prediction = tf.equal(tf.argmax(net, 1), tf.argmax(real_label, 1))
    accruacy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
    tf.summary.scalar("training accuracy", accruacy)
    merged = tf.summary.merge_all()

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        train_writer = tf.summary.FileWriter(
            './arch-%d-tensorboard/' % (arch_index), sess.graph)
        # loop epoches
        for i in range(epoch):
            epoch_start = time.time()
            #  the data will be shuffled by every epoch
            random.shuffle(all_train_filenames)
            random.shuffle(all_test_filenames)
            all_test_filenames = all_test_filenames[:1000]
            for t in range(times):
                batch_files = all_train_filenames[t * batch_size:(t + 1) *
                                                  batch_size]
                batch_data, batch_label = get_train_batch(batch_files)
                # print("training data ...")
                # print(batch_data.shape)
                # print("training label...")
                # print(batch_label.shape)
                feed_dict = {
                    x: batch_data,
                    real_label: batch_label,
                    keep_prob: 0.8,
                    global_step: i * times + t
                }
                summary, _ = sess.run([merged, train_step],
                                      feed_dict=feed_dict)
                train_writer.add_summary(summary, i * times + t)
                saver.save(sess,
                           './arch-%d-ckpt/arch-%d' % (arch_index, arch_index),
                           global_step=i + 1)
                #print("training in epoch:%d of %d,times %d in %d "%(i,epoch,t,times))
            epoch_end = time.time()

            test_batch, test_label = get_test_batch(all_test_filenames[0:1000])
            print("type of test batch ,label", type(test_batch),
                  type(test_label))
            print("length of test batch data:", test_batch.shape)
            print("length of  test batch label:\t", test_label.shape)
            test_dict = {x: test_batch, real_label: test_label, keep_prob: 1.0}
            acc_test, loss = sess.run([accruacy, net_loss],
                                      feed_dict=test_dict)
            if acc_test > highest_acc:
                highest_acc = acc_test
            print('accuracy  is %f' % acc_test)
            print("loss is ", loss)
            print(" epoch %d time consumed %f seconds" %
                  (i, (epoch_end - epoch_start)))
        print("training finshed..highest accuracy is %f,the iterator is %d " %
              (highest_acc, highest_iterator))