本文整理匯總了Python中config.cfg.dataset方法的典型用法代碼示例。如果您正苦於以下問題:Python cfg.dataset方法的具體用法?Python cfg.dataset怎麽用?Python cfg.dataset使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類config.cfg
的用法示例。
在下文中一共展示了cfg.dataset方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: evaluation
# 需要導入模塊: from config import cfg [as 別名]
# 或者: from config.cfg import dataset [as 別名]
def evaluation(model, supervisor, num_label):
teX, teY, num_te_batch = load_data(cfg.dataset, cfg.batch_size, is_training=False)
fd_test_acc = save_to()
with supervisor.managed_session(config=tf.ConfigProto(allow_soft_placement=True)) as sess:
supervisor.saver.restore(sess, tf.train.latest_checkpoint(cfg.logdir))
tf.logging.info('Model restored!')
test_acc = 0
for i in tqdm(range(num_te_batch), total=num_te_batch, ncols=70, leave=False, unit='b'):
start = i * cfg.batch_size
end = start + cfg.batch_size
acc = sess.run(model.accuracy, {model.X: teX[start:end], model.labels: teY[start:end]})
test_acc += acc
test_acc = test_acc / (cfg.batch_size * num_te_batch)
fd_test_acc.write(str(test_acc))
fd_test_acc.close()
print('Test accuracy has been saved to ' + cfg.results + '/test_acc.csv')
示例2: __init__
# 需要導入模塊: from config import cfg [as 別名]
# 或者: from config.cfg import dataset [as 別名]
def __init__(self, is_training=True):
self.graph = tf.Graph()
with self.graph.as_default():
if is_training:
self.X, self.labels = get_batch_data(cfg.dataset, cfg.batch_size, cfg.num_threads)
self.Y = tf.one_hot(self.labels, depth=10, axis=1, dtype=tf.float32)
self.build_arch()
self.loss()
self._summary()
# t_vars = tf.trainable_variables()
self.global_step = tf.Variable(0, name='global_step', trainable=False)
self.optimizer = tf.train.AdamOptimizer()
self.train_op = self.optimizer.minimize(self.total_loss, global_step=self.global_step) # var_list=t_vars)
else:
self.X = tf.placeholder(tf.float32, shape=(cfg.batch_size, 28, 28, 1))
self.labels = tf.placeholder(tf.int32, shape=(cfg.batch_size, ))
self.Y = tf.reshape(self.labels, shape=(cfg.batch_size, 10, 1))
self.build_arch()
tf.logging.info('Seting up the main structure')
示例3: main
# 需要導入模塊: from config import cfg [as 別名]
# 或者: from config.cfg import dataset [as 別名]
def main(_):
model_list = ['baseline', 'vectorCapsNet', 'matrixCapsNet', 'convCapsNet']
# Deciding which model to use
if cfg.model == 'baseline':
model = import_module(cfg.model).Model
elif cfg.model in model_list:
model = import_module(cfg.model).CapsNet
else:
raise ValueError('Unsupported model, please check the name of model:', cfg.model)
# Deciding which dataset to use
if cfg.dataset == 'mnist' or cfg.dataset == 'fashion-mnist':
height = 28
width = 28
channels = 1
num_label = 10
elif cfg.dataset == 'smallNORB':
num_label = 5
height = 32
width = 32
channels = 1
# Initializing model and data loader
net = model(height=height, width=width, channels=channels, num_label=num_label)
dataset = "capslayer.data.datasets." + cfg.dataset
data_loader = import_module(dataset).DataLoader(path=cfg.data_dir,
splitting=cfg.splitting,
num_works=cfg.num_works)
# Deciding to train or evaluate model
if cfg.is_training:
train(net, data_loader)
else:
evaluate(net, data_loader)
示例4: load_mnist
# 需要導入模塊: from config import cfg [as 別名]
# 或者: from config.cfg import dataset [as 別名]
def load_mnist(path, is_training):
fd = open(os.path.join(cfg.dataset, 'train-images-idx3-ubyte'))
loaded = np.fromfile(file=fd, dtype=np.uint8)
trX = loaded[16:].reshape((60000, 28, 28, 1)).astype(np.float)
fd = open(os.path.join(cfg.dataset, 'train-labels-idx1-ubyte'))
loaded = np.fromfile(file=fd, dtype=np.uint8)
trY = loaded[8:].reshape((60000)).astype(np.float)
fd = open(os.path.join(cfg.dataset, 't10k-images-idx3-ubyte'))
loaded = np.fromfile(file=fd, dtype=np.uint8)
teX = loaded[16:].reshape((10000, 28, 28, 1)).astype(np.float)
fd = open(os.path.join(cfg.dataset, 't10k-labels-idx1-ubyte'))
loaded = np.fromfile(file=fd, dtype=np.uint8)
teY = loaded[8:].reshape((10000)).astype(np.float)
# normalization and convert to a tensor [60000, 28, 28, 1]
trX = tf.convert_to_tensor(trX / 255., tf.float32)
# => [num_samples, 10]
trY = tf.one_hot(trY, depth=10, axis=1, dtype=tf.float32)
teY = tf.one_hot(teY, depth=10, axis=1, dtype=tf.float32)
if is_training:
return trX, trY
else:
return teX / 255., teY
示例5: get_batch_data
# 需要導入模塊: from config import cfg [as 別名]
# 或者: from config.cfg import dataset [as 別名]
def get_batch_data():
trX, trY = load_mnist(cfg.dataset, cfg.is_training)
data_queues = tf.train.slice_input_producer([trX, trY])
X, Y = tf.train.shuffle_batch(data_queues, num_threads=cfg.num_threads,
batch_size=cfg.batch_size,
capacity=cfg.batch_size * 64,
min_after_dequeue=cfg.batch_size * 32,
allow_smaller_final_batch=False)
return(X, Y)
示例6: create_inputs_mnist
# 需要導入模塊: from config import cfg [as 別名]
# 或者: from config.cfg import dataset [as 別名]
def create_inputs_mnist(is_train):
tr_x, tr_y = load_mnist(cfg.dataset, is_train)
data_queue = tf.train.slice_input_producer([tr_x, tr_y], capacity=64 * 8)
x, y = tf.train.shuffle_batch(data_queue, num_threads=8, batch_size=cfg.batch_size, capacity=cfg.batch_size * 64,
min_after_dequeue=cfg.batch_size * 32, allow_smaller_final_batch=False)
return (x, y)
示例7: load_mnist
# 需要導入模塊: from config import cfg [as 別名]
# 或者: from config.cfg import dataset [as 別名]
def load_mnist(path, is_training):
fd = open(os.path.join(cfg.dataset, 'train-images-idx3-ubyte'))
loaded = np.fromfile(file=fd, dtype=np.uint8)
trX = loaded[16:].reshape((60000, 28, 28, 1)).astype(np.float32)
fd = open(os.path.join(cfg.dataset, 'train-labels-idx1-ubyte'))
loaded = np.fromfile(file=fd, dtype=np.uint8)
trY = loaded[8:].reshape((60000)).astype(np.int32)
fd = open(os.path.join(cfg.dataset, 't10k-images-idx3-ubyte'))
loaded = np.fromfile(file=fd, dtype=np.uint8)
teX = loaded[16:].reshape((10000, 28, 28, 1)).astype(np.float32)
fd = open(os.path.join(cfg.dataset, 't10k-labels-idx1-ubyte'))
loaded = np.fromfile(file=fd, dtype=np.uint8)
teY = loaded[8:].reshape((10000)).astype(np.int32)
# normalization and convert to a tensor [60000, 28, 28, 1]
# trX = tf.convert_to_tensor(trX, tf.float32)
# teX = tf.convert_to_tensor(teX, tf.float32)
# => [num_samples, 10]
# trY = tf.one_hot(trY, depth=10, axis=1, dtype=tf.float32)
# teY = tf.one_hot(teY, depth=10, axis=1, dtype=tf.float32)
if is_training:
return trX, trY
else:
return teX, teY
示例8: create_inputs
# 需要導入模塊: from config import cfg [as 別名]
# 或者: from config.cfg import dataset [as 別名]
def create_inputs():
trX, trY = load_mnist(cfg.dataset, cfg.is_training)
num_pre_threads = cfg.thread_per_gpu*cfg.num_gpu
data_queue = tf.train.slice_input_producer([trX, trY], capacity=64*num_pre_threads)
X, Y = tf.train.shuffle_batch(data_queue, num_threads=num_pre_threads,
batch_size=cfg.batch_size_per_gpu*cfg.num_gpu,
capacity=cfg.batch_size_per_gpu*cfg.num_gpu * 64,
min_after_dequeue=cfg.batch_size_per_gpu*cfg.num_gpu * 32,
allow_smaller_final_batch=False)
return (X, Y)
示例9: __init__
# 需要導入模塊: from config import cfg [as 別名]
# 或者: from config.cfg import dataset [as 別名]
def __init__(self, is_training=True, height=28, width=28, channels=1, num_label=10):
"""
Args:
height: Integer, the height of inputs.
width: Integer, the width of inputs.
channels: Integer, the channels of inputs.
num_label: Integer, the category number.
"""
self.height = height
self.width = width
self.channels = channels
self.num_label = num_label
self.graph = tf.Graph()
with self.graph.as_default():
if is_training:
self.X, self.labels = get_batch_data(cfg.dataset, cfg.batch_size, cfg.num_threads)
self.Y = tf.one_hot(self.labels, depth=self.num_label, axis=1, dtype=tf.float32)
self.build_arch()
self.loss()
self._summary()
# t_vars = tf.trainable_variables()
self.global_step = tf.Variable(0, name='global_step', trainable=False)
self.optimizer = tf.train.AdamOptimizer()
self.train_op = self.optimizer.minimize(self.total_loss, global_step=self.global_step)
else:
self.X = tf.placeholder(tf.float32, shape=(cfg.batch_size, self.height, self.width, self.channels))
self.labels = tf.placeholder(tf.int32, shape=(cfg.batch_size, ))
self.Y = tf.reshape(self.labels, shape=(cfg.batch_size, self.num_label, 1))
self.build_arch()
tf.logging.info('Seting up the main structure')
示例10: train
# 需要導入模塊: from config import cfg [as 別名]
# 或者: from config.cfg import dataset [as 別名]
def train(model, supervisor, num_label):
trX, trY, num_tr_batch, valX, valY, num_val_batch = load_data(cfg.dataset, cfg.batch_size, is_training=True)
Y = valY[:num_val_batch * cfg.batch_size].reshape((-1, 1))
fd_train_acc, fd_loss, fd_val_acc = save_to()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with supervisor.managed_session(config=config) as sess:
print("\nNote: all of results will be saved to directory: " + cfg.results)
for epoch in range(cfg.epoch):
print('Training for epoch ' + str(epoch) + '/' + str(cfg.epoch) + ':')
if supervisor.should_stop():
print('supervisor stoped!')
break
for step in tqdm(range(num_tr_batch), total=num_tr_batch, ncols=70, leave=False, unit='b'):
start = step * cfg.batch_size
end = start + cfg.batch_size
global_step = epoch * num_tr_batch + step
if global_step % cfg.train_sum_freq == 0:
_, loss, train_acc, summary_str = sess.run([model.train_op, model.total_loss, model.accuracy, model.train_summary])
assert not np.isnan(loss), 'Something wrong! loss is nan...'
supervisor.summary_writer.add_summary(summary_str, global_step)
fd_loss.write(str(global_step) + ',' + str(loss) + "\n")
fd_loss.flush()
fd_train_acc.write(str(global_step) + ',' + str(train_acc / cfg.batch_size) + "\n")
fd_train_acc.flush()
else:
sess.run(model.train_op)
if cfg.val_sum_freq != 0 and (global_step) % cfg.val_sum_freq == 0:
val_acc = 0
for i in range(num_val_batch):
start = i * cfg.batch_size
end = start + cfg.batch_size
acc = sess.run(model.accuracy, {model.X: valX[start:end], model.labels: valY[start:end]})
val_acc += acc
val_acc = val_acc / (cfg.batch_size * num_val_batch)
fd_val_acc.write(str(global_step) + ',' + str(val_acc) + '\n')
fd_val_acc.flush()
if (epoch + 1) % cfg.save_freq == 0:
supervisor.saver.save(sess, cfg.logdir + '/model_epoch_%04d_step_%02d' % (epoch, global_step))
fd_val_acc.close()
fd_train_acc.close()
fd_loss.close()
示例11: train
# 需要導入模塊: from config import cfg [as 別名]
# 或者: from config.cfg import dataset [as 別名]
def train(model, supervisor, num_label):
trX, trY, num_tr_batch, valX, valY, num_val_batch = load_data(cfg.dataset, cfg.batch_size, is_training=True)
Y = valY[:num_val_batch * cfg.batch_size].reshape((-1, 1))
fd_train_acc, fd_loss, fd_val_acc = save_to()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with supervisor.managed_session(config=config) as sess:
print("\nNote: all of results will be saved to directory: " + cfg.results)
for epoch in range(cfg.epoch):
print("Training for epoch %d/%d:" % (epoch, cfg.epoch))
if supervisor.should_stop():
print('supervisor stoped!')
break
for step in tqdm(range(num_tr_batch), total=num_tr_batch, ncols=70, leave=False, unit='b'):
start = step * cfg.batch_size
end = start + cfg.batch_size
global_step = epoch * num_tr_batch + step
if global_step % cfg.train_sum_freq == 0:
_, loss, train_acc, summary_str = sess.run([model.train_op, model.total_loss, model.accuracy, model.train_summary])
assert not np.isnan(loss), 'Something wrong! loss is nan...'
supervisor.summary_writer.add_summary(summary_str, global_step)
fd_loss.write(str(global_step) + ',' + str(loss) + "\n")
fd_loss.flush()
fd_train_acc.write(str(global_step) + ',' + str(train_acc / cfg.batch_size) + "\n")
fd_train_acc.flush()
else:
sess.run(model.train_op)
if cfg.val_sum_freq != 0 and (global_step) % cfg.val_sum_freq == 0:
val_acc = 0
for i in range(num_val_batch):
start = i * cfg.batch_size
end = start + cfg.batch_size
acc = sess.run(model.accuracy, {model.X: valX[start:end], model.labels: valY[start:end]})
val_acc += acc
val_acc = val_acc / (cfg.batch_size * num_val_batch)
fd_val_acc.write(str(global_step) + ',' + str(val_acc) + '\n')
fd_val_acc.flush()
if (epoch + 1) % cfg.save_freq == 0:
supervisor.saver.save(sess, cfg.logdir + '/model_epoch_%04d_step_%02d' % (epoch, global_step))
fd_val_acc.close()
fd_train_acc.close()
fd_loss.close()