Esempio n. 1
0
def reco_image(image_path):
    image_test = Image.open(image_path)
    resized_image = image_test.resize((weight, height), Image.BICUBIC)
    image_data = np.array(resized_image, dtype='float32')

    imgs_holder = tf.placeholder(tf.float32,
                                 shape=[1, weight, height, dimension])

    # todo
    # 与 训练用的 model 的 CNN 相同
    # logits, pred = cnn(input_sensor=imgs_holder, class_size=class_size)
    logits, pred = interface(input_tensor=imgs_holder, class_size=class_size)

    saver = tf.train.Saver()

    with tf.Session() as sess:
        ckpt = tf.train.get_checkpoint_state(ckpt_dir)
        saver.restore(sess, ckpt.model_checkpoint_path)
        classes_ = sess.run(pred,
                            feed_dict={
                                imgs_holder:
                                np.reshape(image_data,
                                           [1, weight, height, dimension])
                            })

    num = np.argmax(classes_)
    return 'class is :' + str(classes[int(num)]) + '  Probability is :' + str(
        classes_[0][int(num)])
def evaluate():
    with tf.Graph().as_default():
        # 训练日志文件夹
        logs_train_dir = '/'
        # 验证文件夹
        verification_dir = "/"
        # 数据总数
        n_test = 0
        train, train_label = gbd.get_Data(verification_dir, ROW, COLUMN)
        train_X, train_Y, one_hot_train_Y = gbd.get_batch_data(
            train, train_label, batch_size=BATCH_SIZE)
        train_logits, train_v_length = model.interface(
            inputs=train_X,
            Y=one_hot_train_Y,
            batch_size=BATCH_SIZE,
            vec_len=VEC_LEN,
            temp_batch_size=BATCH_SIZE)
        top_k_op = tf.nn.in_top_k(train_logits, train_Y, 1)
        saver = tf.train.Saver(tf.global_variables())
        with tf.Session() as sess:
            print("Reading checkpoints...")
            ckpt = tf.train.get_checkpoint_state(logs_train_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")
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess, coord=coord)
            try:
                num_iter = int(n_test / BATCH_SIZE)
                true_count = 0
                total_sample_count = num_iter * BATCH_SIZE
                step = 0
                while step < num_iter and not coord.should_stop():
                    prediction = sess.run([top_k_op])
                    true_count += np.sum(prediction)
                    step += 1
                    precision = float(true_count) / total_sample_count
                print("precision = %3f" % precision)
            except Exception as e:
                coord.request_stop(e)
            finally:
                coord.request_stop()
                coord.join(threads)
def evaluate_one_base(ct):
    """
    返回一条碱基的各部位对应(.)的概率
    input:
        ct: 文件夹,内部应只有一个ct文件,多了后果自负
    outputs:
        prediction: 二维张量,第一维因为batch_size = 1,一般索引数为碱基数;第二维索引数3,对应该部位为( . )的概率
    """
    with tf.Graph().as_default():
        train, train_label = gbd.get_Data(ct, ROW, COLUMN)
        train_X, train_Y, one_hot_train_Y = gbd.get_batch_data(train, train_label, batch_size = BATCH_SIZE)
        train_logits, train_v_length = model.interface(inputs = train_X,
                                                Y = one_hot_train_Y,
                                                batch_size = BATCH_SIZE,
                                                vec_len = VEC_LEN,
                                                temp_batch_size = BATCH_SIZE)
        softmax = tf.nn.softmax(train_logits,dim=-1,name=None)   
        saver = tf.train.Saver(tf.global_variables())
        with tf.Session() as sess:
            print("Reading checkpoints...")
            ckpt = tf.train.get_checkpoint_state(logs_train_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")              
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess,coord = coord)
            try:
                if not coord.should_stop():
                    prediction = sess.run(softmax)
                    return prediction
            except Exception as e:
                coord.request_stop(e)
            finally:
                coord.request_stop()
                coord.join(threads)
def evaluate_one_base(train, train_label):
    """
    返回一个碱基的各部位对应(.)的概率
    input:
        train: 数据集,维度为[size,19,128,1]
        label: 标签集,还未经过ONE-HOT编码,维度为[size]
    outputs:
        prediction: 二维张量,第一维batch_size=1;第二维索引数3,对应该部位为( . )的概率
    """
    with tf.Graph().as_default():
        train_X, train_Y, one_hot_train_Y = gbd.get_batch_data(train, train_label, batch_size = BATCH_SIZE)
        train_logits, train_v_length = model.interface(inputs = train_X,
                                                Y = one_hot_train_Y,
                                                batch_size = BATCH_SIZE,
                                                vec_len = VEC_LEN,
                                                temp_batch_size = BATCH_SIZE)
        softmax = tf.nn.softmax(train_logits,dim=-1,name=None)   
        saver = tf.train.Saver(tf.global_variables())
        with tf.Session() as sess:
            ckpt = tf.train.get_checkpoint_state(logs_train_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)
            else:
                print("no checkpoint file found")              
            coord = tf.train.Coordinator()
            threads = tf.train.start_queue_runners(sess=sess,coord = coord)
            try:
                if not coord.should_stop():
                    prediction = sess.run(softmax)
                    return prediction
            except Exception as e:
                coord.request_stop(e)
            finally:
                coord.request_stop()
                coord.join(threads)
Esempio n. 5
0
def main():
    _IMG_SIZE = 32
    # _IMG_SIZE = 28
    _IMG_CHANNEL = 3
    _IMG_CLASS = 10

    parser = argparse.ArgumentParser(description='alexnet')
    parser.add_argument('--data_dir', type=str, default='./cifar-10-batches-py/')
    parser.add_argument('--learning_rate', type=float, default=0.01)
    parser.add_argument('--batch_size', type=int, default=100) # must be integer times of total images_num
    parser.add_argument('--keepPro', type=float, default=0.5)
    parser.add_argument('--summary_dir', type=str, default='./summary/alexnetlog/')
    parser.add_argument('--max_epoch', type=int, default=40)
    parser.add_argument('--eval_freq', type=int, default=1)
    parser.add_argument('--save_freq', type=int, default=100)
    args = parser.parse_args()
    print(args)
    with tf.device('/gpu:1'):
        # -----------------------------------------------------------------------------
        #       BUILD GRAPH
        # -----------------------------------------------------------------------------
        inputs, labels, dropout_keep_prob, learning_rate, is_training = alexnet.input_placeholder(_IMG_SIZE, _IMG_CHANNEL, _IMG_CLASS)
        logits = alexnet.interface(inputs, args.keepPro, _IMG_CLASS, is_training)

        accuracy = alexnet.accuracy(logits, labels)
        loss = alexnet.loss(logits, labels)
        train = alexnet.train(loss, learning_rate, 'RMSProp')
        
        # ready for summary or save
        merged = tf.summary.merge_all()
        saver = tf.train.Saver()
        print("[BUILD GRAPH] memory_usage=%f" % (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024), file=sys.stderr)

        # -----------------------------------------------------------------------------
        #       LOAD DATA
        # -----------------------------------------------------------------------------
        train_images, train_labels, test_images, test_labels = loader.load_batch_data(args.data_dir, args.batch_size)
        print("[LOAD DATA] memory_usage=%f" % (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024), file=sys.stderr)

        # -----------------------------------------------------------------------------
        #       START THE SESSION
        # -----------------------------------------------------------------------------
        cur_lr = args.learning_rate # current learning rate
        config = tf.ConfigProto(allow_soft_placement=True)
        config.gpu_options.allow_growth = True
        with tf.Session(config=config) as sess:
            sess.run(tf.global_variables_initializer())
            train_writer = tf.summary.FileWriter(logdir=args.summary_dir + 'norm' + '/train/', 
                                                 graph=sess.graph)
            test_writer = tf.summary.FileWriter(logdir=args.summary_dir + 'norm' + '/test/')

            for epoch in range(args.max_epoch):
                start = timeit.default_timer()
                train_accs = []
                train_losses = []
                # train
                for index, images_batch in enumerate(train_images):
                    _, summary, train_loss, train_acc = sess.run(fetches = [train, merged, loss, accuracy], 
                                                                 feed_dict = { inputs: images_batch,
                                                                               labels: train_labels[index],
                                                                               dropout_keep_prob: args.keepPro,
                                                                               learning_rate: cur_lr,
                                                                               is_training: True })
                    train_accs.append(train_acc)
                    train_losses.append(train_loss)
                    print('[batch] {} done'.format(index), end='\r')
                train_avg_acc  = float(np.mean(np.asarray(train_accs)))
                train_avg_loss = float(np.mean(np.asarray(train_losses)))
                train_summary = tf.Summary(value=[tf.Summary.Value(tag="accuracy", simple_value=train_avg_acc), 
                                                  tf.Summary.Value(tag="avg_loss", simple_value=train_avg_loss),
                                                  tf.Summary.Value(tag="learning_rate", simple_value=cur_lr)])
                train_writer.add_summary(summary, epoch)
                train_writer.add_summary(train_summary, epoch)
                print('=' * 20 + 'EPOCH {} [TRAIN]'.format(epoch) + '=' * 20)
                print('cost time: {:.3f}s'.format(timeit.default_timer()-start))
                print('acc: {0}, avg_loss: {1}'.format(train_avg_acc, train_avg_loss))
                # evaluate
                if (epoch + 1) % args.eval_freq == 0:
                    test_accs = []
                    test_losses = []
                    for index, test_images_batch in enumerate(test_images):
                        test_loss, test_acc = sess.run(fetches = [loss, accuracy], 
                                                       feed_dict = { inputs: test_images_batch,
                                                                     labels: test_labels[index],
                                                                     dropout_keep_prob: args.keepPro,
                                                                     learning_rate: cur_lr,
                                                                     is_training: False})
                        test_accs.append(test_acc)
                        test_losses.append(test_loss)
                    test_avg_acc  = float(np.mean(np.asarray(test_accs)))
                    test_avg_loss = float(np.mean(np.asarray(test_losses)))
                    test_summary = tf.Summary(value=[tf.Summary.Value(tag="accuracy", simple_value=test_avg_acc), 
                                                     tf.Summary.Value(tag="avg_loss", simple_value=test_avg_loss)])
                    test_writer.add_summary(test_summary, epoch)
                    print('=' * 20 + 'EPOCH {} [EVAL]'.format(epoch) + '=' * 20)
                    print('acc: {0}, avg_loss: {1}'.format(test_avg_acc, test_avg_loss))
                # lr decay
                cur_lr = lr(cur_lr, epoch)
                # save
                if (epoch + 1) % args.save_freq == 0:
                    checkpoint_file = args.summary_dir + 'model.ckpt'
                    saver.save(sess, checkpoint_file, global_step=epoch)
                    print('Saved checkpoint')
            
            train_writer.close()
            test_writer.close()
Esempio n. 6
0
def run_trainning():
    """
    对神经网络进行训练
    """
    PATH = "Cleaned_5sRNA_test/"
    logs_train_dir="Net_model/"  # 保存训练得来的模型的文件夹
    row = 19
    column = 128
    vec_len = 8
    temp_batch_size = 64
    inputs, Labels= gbd.get_Data(PATH=PATH,
                                  row=row,
                                  column=column)

    train_X, train_Y, one_hot_train_Y = gbd.get_batch_data(inputs,Labels, batch_size=temp_batch_size)

    train_logits,train_v_length=model.interface(inputs=train_X,
                                                Y=one_hot_train_Y,
                                                batch_size=temp_batch_size,
                                                vec_len=vec_len,
                                                temp_batch_size=temp_batch_size)
    train_loss=model.loss(logits=train_logits,
                          v_length=train_v_length,
                          labels=train_Y,
                          Y=one_hot_train_Y,
                          temp_batch_size=temp_batch_size)
    train_op = model.trainning(train_loss,learning_rate)
    train_acc = model.evalution(train_logits,train_Y)
    
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())

    saver = tf.train.Saver()

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess,coord = coord)

    try:
        for step in np.arange(MAX_STEP):
            if coord.should_stop():
                break
            _, tra_loss, tra_acc=sess.run([train_op, train_loss, train_acc])

            if step % 100 == 0:
                print("Step %d,train loss = %.2f,train accuracy = %.2f" %(step, tra_loss, tra_acc))
                print(train_X.shape)
            if step % 200 == 0:
                # 每两百轮保存一次训练数据
                checkpoint_path = os.path.join(logs_train_dir, 'model.ckpt')
                saver.save(sess,
                           save_path=checkpoint_path,
                           global_step=step)

    except tf.errors.OutOfRangeError:
        print('Done Trainning')
    finally:
        coord.request_stop()
        
    coord.join(threads)
    sess.close()