예제 #1
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))
예제 #2
0
    def archi_1(self):
        with tf.name_scope("Archi-1"):
            # input size is batch_sizex20x20x6
            keep_prob = tf.placeholder(tf.float32)
            learning_rate = 0.3

            x = tf.placeholder(tf.float32, [None, 6, 20, 20])
            x_image = tf.reshape(x, [-1, 6, 20, 20, 1])

            # 5x5x3 is the kernel size of conv1,1 is the input depth,64 is the number output channel
            w_conv1 = tf.Variable(tf.random_normal([3, 5, 5, 1, 64],
                                                   stddev=0.001),
                                  dtype=tf.float32,
                                  name='w_conv1')
            b_conv1 = tf.Variable(tf.constant(0.01, shape=[64]),
                                  dtype=tf.float32,
                                  name='b_conv1')
            out_conv1 = tf.nn.relu(
                tf.add(
                    tf.nn.conv3d(x_image,
                                 w_conv1,
                                 strides=[1, 1, 1, 1, 1],
                                 padding='VALID'), b_conv1))
            out_conv1 = tf.nn.dropout(out_conv1, keep_prob)

            out_conv1_shape = tf.shape(out_conv1)

            # max pooling ,pooling layer has no effect on the data size
            hidden_conv1 = tf.nn.max_pool3d(out_conv1,
                                            strides=[1, 1, 1, 1, 1],
                                            ksize=[1, 1, 1, 1, 1],
                                            padding='SAME')

            # after conv1 ,the output size is batch_sizex4x16x16x64([batch_size,in_deep,width,height,output_deep])
            w_conv2 = tf.Variable(tf.random_normal([3, 5, 5, 64, 64],
                                                   stddev=0.001),
                                  dtype=tf.float32,
                                  name='w_conv2')
            b_conv2 = tf.Variable(tf.constant(0.01, shape=[64]),
                                  dtype=tf.float32,
                                  name='b_conv2')
            out_conv2 = tf.nn.relu(
                tf.add(
                    tf.nn.conv3d(hidden_conv1,
                                 w_conv2,
                                 strides=[1, 1, 1, 1, 1],
                                 padding='VALID'), b_conv2))
            out_conv2 = tf.nn.dropout(out_conv2, keep_prob)

            out_conv2_shape = tf.shape(out_conv2)

            # after conv2 ,the output size is batch_sizex2x12x12x64([batch_size,in_deep,width,height,output_deep])
            w_conv3 = tf.Variable(tf.random_normal([1, 5, 5, 64, 64],
                                                   stddev=0.001),
                                  dtype=tf.float32,
                                  name='w_conv3')
            b_conv3 = tf.Variable(tf.constant(0.01, shape=[64]),
                                  dtype=tf.float32,
                                  name='b_conv3')
            out_conv3 = tf.nn.relu(
                tf.add(
                    tf.nn.conv3d(out_conv2,
                                 w_conv3,
                                 strides=[1, 1, 1, 1, 1],
                                 padding='VALID'), b_conv3))
            out_conv3 = tf.nn.dropout(out_conv3, keep_prob)

            out_conv3_shape = tf.shape(out_conv3)
            tf.summary.scalar('out_conv3_shape', out_conv3_shape[0])

            # after conv2 ,the output size is batch_sizex2x8x8x64([batch_size,in_deep,width,height,output_deep])
            # all feature map flatten to one dimension vector,this vector will be much long
            out_conv3 = tf.reshape(out_conv3, [-1, 64 * 8 * 8 * 2])
            w_fc1 = tf.Variable(tf.random_normal([64 * 8 * 8 * 2, 150],
                                                 stddev=0.001),
                                name='w_fc1')
            out_fc1 = tf.nn.relu(
                tf.add(tf.matmul(out_conv3, w_fc1),
                       tf.constant(0.001, shape=[150])))
            out_fc1 = tf.nn.dropout(out_fc1, keep_prob)

            out_fc1_shape = tf.shape(out_fc1)
            tf.summary.scalar('out_fc1_shape', out_fc1_shape[0])

            w_fc2 = tf.Variable(tf.random_normal([150, 2], stddev=0.001),
                                name='w_fc2')
            out_fc2 = tf.nn.relu(
                tf.add(tf.matmul(out_fc1, w_fc2), tf.constant(0.001,
                                                              shape=[2])))
            out_fc2 = tf.nn.dropout(out_fc2, keep_prob)
            w_fc2_print = tf.Print(w_fc2, [out_fc2], "output of fc2")
            test_print = tf.reshape(w_fc2_print, [-1, 2])

            out_fc2_shape = tf.shape(out_fc2)
            tf.summary.scalar('out_fc2_shape', out_fc2_shape[0])

            # softmax layer
            real_label = tf.placeholder(tf.float32, [None, 2])
            w_sm = tf.Variable(tf.random_normal([2, 2], stddev=0.001),
                               name='w_sm')
            b_sm = tf.constant(0.001, shape=[2])
            out_sm = tf.nn.softmax(tf.add(tf.matmul(out_fc2, w_sm), b_sm))
            #cross_entropy = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(out_sm, real_label))
            cross_entropy = -tf.reduce_sum(real_label * tf.log(out_sm))
            loss = tf.reduce_mean(cross_entropy)

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

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

            path = '/data/LUNA2016/cubic_normalization_npy'
            highest_acc = 0.0
            highest_iterator = 1
            batch_size = 32

            saver = tf.train.Saver()  # default to save all variable
            #with tf.InteractiveSession() as sess:

            #sess = tf_debug.LocalCLIDebugWrapperSession(sess)
            merged = tf.summary.merge_all()

            #config = tf.ConfigProto(allow_soft_placement=True)
            #sess = tf.InteractiveSession(config=config)
            with tf.Session() as sess:

                sess.run(tf.global_variables_initializer())
                train_writer = tf.summary.FileWriter('./tensorboard/',
                                                     sess.graph)
                for i in range(40):
                    batch_data, batch_label = get_train_batch(
                        path, i, batch_size, 10)
                    if len(batch_data) > 0:
                        # tf.cast(batch_data,tf.float32)
                        # tf.cast(batch_label,tf.float32)
                        if i % 10 == 0:
                            acc_val = sess.run(accruacy,
                                               feed_dict={
                                                   x: batch_data,
                                                   real_label: batch_label,
                                                   keep_prob: 0.7
                                               })
                            saver.save(sess,
                                       './ckpt/archi-1.ckpt',
                                       global_step=i + 1)
                            if acc_val > highest_acc:
                                highest_acc = acc_val
                                highest_iterator = i
                            print('accuracy  is %f' % acc_val)
                        _, summary = sess.run([train_step, merged],
                                              feed_dict={
                                                  x: batch_data,
                                                  real_label: batch_label,
                                                  keep_prob: 0.7
                                              })
                        #= sess.run(,feed_dict={x: batch_data, real_label: batch_label,keep_prob:0.7})
                        print("training process..", i)
                        print("loss is ", loss)
                        train_writer.add_summary(summary, i)
            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 = 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))
예제 #4
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))