def main(_): config = ModelConfig("inference") config.keep_prob = 1.0 # desactivate the dropout test_images, test_annotations = inputs.input_pipeline(TFR_DIR, VAL_FILE_PATTERN, num_classes=config.num_classes, batch_size=BATCH_SIZE) data = tf.placeholder(tf.string, []) target = tf.placeholder(tf.float32, [None, config.num_classes]) model = MLClassifier(config, data, target) model.build() sess = tf.Session() sess.run(tf.local_variables_initializer()) model.restore_fc(sess) model.restore_inception(sess) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) num_steps = int((1028)/BATCH_SIZE) # (1 shards * nb examples per shard) for n in range(num_steps): print("Running inference on image %d" % n) image, annotation = sess.run([test_images, test_annotations]) with open(os.path.join(IMAGE_DIR, image[0].decode('utf8')), 'rb') as f: image_data = f.read() fetches = {'auc_ops': model.auc_op} feed_dict = {data: image_data, target: annotation} v = sess.run(fetches, feed_dict) fetches = {'mean_auc': model.mean_auc, 'auc': model.auc} v = sess.run(fetches) print("Mean AUC %f" % v['mean_auc']) print("Labels AUC") print(v['auc']) # store result in csv dataframe = {'model':FLAGS.model_str, 'mean AUC': v['mean_auc']} for i, k in enumerate(v['auc']): label = model.vocabulary.id_to_word(i) dataframe[label] = k if not os.path.isfile('results.csv'): df = pd.DataFrame(dataframe, columns=dataframe.keys(), index=[0]) df.to_csv('results.csv', index=False) else: df = pd.read_csv('results.csv') len_df = len(df) df_buffer = pd.DataFrame(dataframe, columns=dataframe.keys(), index=[len_df+1]) concat = pd.concat([df, df_buffer]) concat.to_csv('results.csv', index=False) coord.request_stop() coord.join(threads) sess.close()
def main(): input = input_pipeline() print("ready input pipeline") net = densenet() _solver = solver(net, input, './log') _solver.train_and_test()
str(args.num_epochs) + '-learningrate_' + str(args.start_lr) + '-decay_' + str(lr_decay) + '-activation_' + args.activation + '-keepprob_' + str(args.keep_prob)) tensor_logdir = os.path.join(file_path, 'summaries', dir_path, str(time.time())) # Load test data. if test_dataset: test_data = inputs.test_data(test_dataset, n_classes, args.label_first_column, args.skip_rows) # Inputs. if input_method == 'pipeline': batches = inputs.input_pipeline(training_datasets, n_features, args.skip_rows, args.batch_size, n_threads, num_epochs=args.num_epochs) x = batches[0] y_ = tf.one_hot(batches[1], n_classes) elif input_method == 'oldschool': x = tf.placeholder(tf.float32, [None, n_features], name='input_x') y_ = tf.placeholder(tf.float32, [None, n_classes], name='input_y') elif input_method == 'dataset': dataset = inputs.get_tfrecord_dataset(training_datasets, args.batch_size, args.num_epochs, args.skip_rows, n_threads=n_threads)
def main(_): if tf.gfile.IsDirectory(LOG_DIR): tf.gfile.DeleteRecursively(LOG_DIR) if not tf.gfile.IsDirectory(LOG_DIR): tf.logging.info("Creating output directory: %s" % LOG_DIR) tf.gfile.MakeDirs(LOG_DIR) config = ModelConfig("train") train_images, train_annotations = inputs.input_pipeline( TFR_DIR, TRAIN_FILE_PATTERN, num_classes=config.num_classes, batch_size=BATCH_SIZE) val_images, val_annotations = inputs.input_pipeline( TFR_DIR, VAL_FILE_PATTERN, num_classes=config.num_classes, batch_size=2056) data = tf.placeholder(tf.float32, [None, config.bottleneck_dim]) target = tf.placeholder(tf.float32, [None, config.num_classes]) model = MLClassifier(config, data, target) model.build() merged = tf.summary.merge_all() saver = tf.train.Saver() sess = tf.Session() sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) train_writer = tf.summary.FileWriter(LOG_DIR + '/train', sess.graph) test_writer = tf.summary.FileWriter(LOG_DIR + '/test') coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) print("%s Start training." % datetime.now()) num_batch_per_epoch = int( (9 * 2056) / BATCH_SIZE) # (nb shards * nb examples per shard) / batch size num_steps = NUM_EPOCH * num_batch_per_epoch for n in range(num_steps): images, annotations = sess.run([train_images, train_annotations]) bottlenecks = inputs.get_bottlenecks(images, BOTTLENECK_DIR) fetches = { 'opt': model.optimize, 'loss': model.loss, 'summary': merged } feed_dict = {data: bottlenecks, target: annotations} v = sess.run(fetches, feed_dict) if n % 10 == 0: print('Loss: %f' % v['loss']) train_writer.add_summary(v['summary'], n) if n % 100 == 0: images, annotations = sess.run([val_images, val_annotations]) bottlenecks = inputs.get_bottlenecks(images, BOTTLENECK_DIR) fetches = {'auc_ops': model.auc_op, 'summary': merged} feed_dict = {data: bottlenecks, target: annotations} v = sess.run(fetches, feed_dict) mean_auc = sess.run(model.mean_auc) print("%s - Validation : Mean AUC : %f" % (datetime.now(), mean_auc)) test_writer.add_summary(v['summary'], n) save_path = saver.save(sess, os.path.join(LOG_DIR, 'model.ckpt')) print("Model saved in file: %s" % save_path) coord.request_stop() coord.join(threads) sess.close()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--epoch', default=10, type=int) #default=0은 input_pipeline에서 batch size를 자동 조정(각 데이터 셋 수의 10%) parser.add_argument('--train_batch_size', default=0, type=int) parser.add_argument('--valid_batch_size', default=0, type=int) parser.add_argument('--test_batch_size', default=0, type=int) args = parser.parse_args() if args.epoch != cfg.MAX_EPOCH: cfg.MAX_EPOCH = args.epoch if args.train_batch_size != cfg.TRAIN_BATCH_SIZE: cfg.TRAIN_BATCH_SIZE = args.train_batch_size if args.valid_batch_size != cfg.VALID_BATCH_SIZE: cfg.VALID_BATCH_SIZE = args.valid_batch_size if args.test_batch_size != cfg.TEST_BATCH_SIZE: cfg.TEST_BATCH_SIZE = args.test_batch_size total_train_size, total_valid_size, total_test_size, train_batch, valid_batch, test_batch = input.input_pipeline( cfg.DATASET_PATH, cfg.NUM_LABELS, cfg.NUM_DIM, train_split_ratio=0.6, valid_split_ratio=0.2, test_split_ratio=0.2) print("total train set size: %d/train batch size:%d\n" % (total_train_size, cfg.TRAIN_BATCH_SIZE)) print("total valid set size: %d/valid batch size:%d\n" % (total_valid_size, cfg.VALID_BATCH_SIZE)) print("total test set size: %d/test batch size:%d\n" % (total_test_size, cfg.TEST_BATCH_SIZE)) print("input pipeline ready") net = model(cfg.NUM_LABELS, cfg.NUM_DIM) print("build_networks") _solver = solver(net, cfg.LOGS_PATH) print("train") max_index = _solver.train_split(cfg.MAX_EPOCH, train_batch, valid_batch, total_train_size, cfg.TRAIN_BATCH_SIZE, total_valid_size, cfg.VALID_BATCH_SIZE) print("test") _solver.test(test_batch, total_test_size, cfg.TEST_BATCH_SIZE, max_index) _solver.close() print("end")
def main(_): config = ModelConfig("train") config.keep_prob = 1.0 # desactivate the dropout test_images, test_annotations = inputs.input_pipeline( TFR_DIR, TEST_FILE_PATTERN, num_classes=config.num_classes, batch_size=BATCH_SIZE) data = tf.placeholder(tf.float32, [None, config.bottleneck_dim]) target = tf.placeholder(tf.float32, [None, config.num_classes]) model = MLClassifier(config, data, target) model.build() sess = tf.Session() sess.run(tf.local_variables_initializer()) model.restore_fc(sess) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) print("%s Running test..." % datetime.now()) num_steps = int( (4 * 1028) / BATCH_SIZE) # (nb shards * nb examples per shard) for n in range(num_steps): images, annotations = sess.run([test_images, test_annotations]) bottlenecks = inputs.get_bottlenecks(images, BOTTLENECK_DIR) fetches = {'auc_ops': model.auc_op} feed_dict = {data: bottlenecks, target: annotations} v = sess.run(fetches, feed_dict) fetches = {'mean_auc': model.mean_auc, 'auc': model.auc} v = sess.run(fetches) print("Mean AUC %f" % v['mean_auc']) print("Labels AUC") print(v['auc']) # store result in csv dataframe = {'model': FLAGS.model_str, 'mean_auc': v['mean_auc']} for i, k in enumerate(v['auc']): label = model.vocabulary.id_to_word(i) dataframe[label] = k if not os.path.isfile('results.csv'): df = pd.DataFrame(dataframe, columns=dataframe.keys(), index=[0]) df.to_csv('results.csv', index=False) else: df = pd.read_csv('results.csv') len_df = len(df) df_buffer = pd.DataFrame(dataframe, columns=dataframe.keys(), index=[len_df + 1]) concat = pd.concat([df, df_buffer]) concat.to_csv('results.csv', index=False) coord.request_stop() coord.join(threads) sess.close()
def main(): input = input_pipeline() print("ready input pipeline") net = softmax_regression() _solver = solver(net, input, './log') _solver.train_and_test()