本文整理汇总了Python中tqdm.trange方法的典型用法代码示例。如果您正苦于以下问题:Python tqdm.trange方法的具体用法?Python tqdm.trange怎么用?Python tqdm.trange使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tqdm
的用法示例。
在下文中一共展示了tqdm.trange方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: validate_on_lfw
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def validate_on_lfw(model, lfw_160_path):
# Read the file containing the pairs used for testing
pairs = lfw.read_pairs('validation-LFW-pairs.txt')
# Get the paths for the corresponding images
paths, actual_issame = lfw.get_paths(lfw_160_path, pairs)
num_pairs = len(actual_issame)
all_embeddings = np.zeros((num_pairs * 2, 512), dtype='float32')
for k in tqdm.trange(num_pairs):
img1 = cv2.imread(paths[k * 2], cv2.IMREAD_COLOR)[:, :, ::-1]
img2 = cv2.imread(paths[k * 2 + 1], cv2.IMREAD_COLOR)[:, :, ::-1]
batch = np.stack([img1, img2], axis=0)
embeddings = model.eval_embeddings(batch)
all_embeddings[k * 2: k * 2 + 2, :] = embeddings
tpr, fpr, accuracy, val, val_std, far = lfw.evaluate(
all_embeddings, actual_issame, distance_metric=1, subtract_mean=True)
print('Accuracy: %2.5f+-%2.5f' % (np.mean(accuracy), np.std(accuracy)))
print('Validation rate: %2.5f+-%2.5f @ FAR=%2.5f' % (val, val_std, far))
auc = metrics.auc(fpr, tpr)
print('Area Under Curve (AUC): %1.3f' % auc)
eer = brentq(lambda x: 1. - x - interpolate.interp1d(fpr, tpr)(x), 0., 1.)
print('Equal Error Rate (EER): %1.3f' % eer)
示例2: create_training_file
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def create_training_file(docs, tokenizer, args, epoch_num):
epoch_filename = args.output_dir / "epoch_{}.json".format(epoch_num)
num_instances = 0
with epoch_filename.open('w') as epoch_file:
for doc_idx in trange(len(docs), desc="Document"):
doc_instances = create_instances_from_document(
docs, doc_idx, max_seq_length=args.max_seq_len, short_seq_prob=args.short_seq_prob,
masked_lm_prob=args.masked_lm_prob, max_predictions_per_seq=args.max_predictions_per_seq,
whole_word_mask=args.do_whole_word_mask, tokenizer=tokenizer,
next_sent_prediction=args.do_next_sent_prediction)
doc_instances = [json.dumps(instance) for instance in doc_instances]
for instance in doc_instances:
epoch_file.write(instance + '\n')
num_instances += 1
metrics_file = args.output_dir / "epoch_{}_metrics.json".format(epoch_num)
with metrics_file.open('w') as metrics_file:
metrics = {
"num_training_examples": num_instances,
"max_seq_len": args.max_seq_len
}
metrics_file.write(json.dumps(metrics))
示例3: _preprocess
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def _preprocess(self, ids, ids_file):
print("Preprocessing mask, this will take a while. " + \
"But don't worry, it only run once for each split.")
tbar = trange(len(ids))
new_ids = []
for i in tbar:
img_id = ids[i]
cocotarget = self.coco.loadAnns(self.coco.getAnnIds(imgIds=img_id))
img_metadata = self.coco.loadImgs(img_id)[0]
mask = self._gen_seg_mask(cocotarget, img_metadata['height'],
img_metadata['width'])
# more than 1k pixels
if (mask > 0).sum() > 1000:
new_ids.append(img_id)
tbar.set_description('Doing: {}/{}, got {} qualified images'. \
format(i, len(ids), len(new_ids)))
print('Found number of qualified images: ', len(new_ids))
torch.save(new_ids, ids_file)
return new_ids
示例4: start
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def start(self):
"""
Start testing with a progress bar.
"""
if not self._reset_called:
self.ds.reset_state()
itr = self.ds.__iter__()
if self.warmup:
for _ in tqdm.trange(self.warmup, **get_tqdm_kwargs()):
next(itr)
# add smoothing for speed benchmark
with get_tqdm(total=self.test_size,
leave=True, smoothing=0.2) as pbar:
for idx, dp in enumerate(itr):
pbar.update()
if idx == self.test_size - 1:
break
示例5: test
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def test(data):
print('Testing model...')
model = Model(data).to(device)
model.load_state_dict(torch.load(data.model_path))
instances = data.ids
pred_results = []
model.eval()
test_num = len(instances)
total_batch = test_num // data.batch_size + 1
for batch in trange(total_batch):
start, end = slice_set(batch, data.batch_size, test_num)
instance = instances[start:end]
if not instance: continue
_, mask, *model_input, char_recover = load_batch(instance, True)
tag_seq = model(mask, *model_input)
pred_label = seq2label(tag_seq, mask, data.label_alphabet, char_recover)
pred_results += pred_label
return pred_results
示例6: create_and_train_model
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def create_and_train_model(self):
"""
Model training and scoring.
"""
print("\nTraining started.\n")
self.model = SignedGraphConvolutionalNetwork(self.device, self.args, self.X).to(self.device)
self.optimizer = torch.optim.Adam(self.model.parameters(),
lr=self.args.learning_rate,
weight_decay=self.args.weight_decay)
self.model.train()
self.epochs = trange(self.args.epochs, desc="Loss")
for epoch in self.epochs:
start_time = time.time()
self.optimizer.zero_grad()
loss, _ = self.model(self.positive_edges, self.negative_edges, self.y)
loss.backward()
self.epochs.set_description("SGCN (Loss=%g)" % round(loss.item(), 4))
self.optimizer.step()
self.logs["training_time"].append([epoch+1, time.time()-start_time])
if self.args.test_size > 0:
self.score_model(epoch)
示例7: _preprocess
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def _preprocess(self, ids, ids_file):
print("Preprocessing mask, this will take a while." + \
"But don't worry, it only run once for each split.")
tbar = trange(len(ids))
new_ids = []
for i in tbar:
img_id = ids[i]
cocotarget = self.coco.loadAnns(self.coco.getAnnIds(imgIds=img_id))
img_metadata = self.coco.loadImgs(img_id)[0]
mask = self._gen_seg_mask(cocotarget, img_metadata['height'], img_metadata['width'])
# more than 1k pixels
if (mask > 0).sum() > 1000:
new_ids.append(img_id)
tbar.set_description('Doing: {}/{}, got {} qualified images'. \
format(i, len(ids), len(new_ids)))
print('Found number of qualified images: ', len(new_ids))
with open(ids_file, 'wb') as f:
pickle.dump(new_ids, f)
return new_ids
示例8: create_dataset
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def create_dataset(seqs: List[List[str]],
tags: List[List[str]],
word_to_ix: Mapping[str, int],
max_seq_len: int,
pad_ix: int) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""Convert List[str] -> torch.Tensor.
Returns:
seqs_tensor: shape=[num_seqs, max_seq_len].
seqs_mask: shape=[num_seqs, max_seq_len].
tags_tesnor: shape=[num_seqs, max_seq_len].
"""
assert len(seqs) == len(tags)
num_seqs = len(seqs)
seqs_tensor = torch.ones(num_seqs, max_seq_len) * pad_ix
seqs_mask = torch.zeros(num_seqs, max_seq_len)
tags_tesnor = torch.ones(num_seqs, max_seq_len) * pad_ix
for i in trange(num_seqs):
seqs_mask[i, : len(seqs[i])] = 1
for j, word in enumerate(seqs[i]):
seqs_tensor[i, j] = word_to_ix.get(word, word_to_ix['[UNK]'])
for j, tag in enumerate(tags[i]):
tags_tesnor[i, j] = word_to_ix.get(tag, word_to_ix['[UNK]'])
return seqs_tensor.long(), seqs_mask, tags_tesnor.long()
示例9: train
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def train(unet, batch_size, epochs, epoch_lapse, threshold, learning_rate, criterion, optimizer, x_train, y_train, x_val, y_val, width_out, height_out):
epoch_iter = np.ceil(x_train.shape[0] / batch_size).astype(int)
t = trange(epochs, leave=True)
for _ in t:
total_loss = 0
for i in range(epoch_iter):
batch_train_x = torch.from_numpy(x_train[i * batch_size : (i + 1) * batch_size]).float()
batch_train_y = torch.from_numpy(y_train[i * batch_size : (i + 1) * batch_size]).long()
if use_gpu:
batch_train_x = batch_train_x.cuda()
batch_train_y = batch_train_y.cuda()
batch_loss = train_step(batch_train_x , batch_train_y, optimizer, criterion, unet, width_out, height_out)
total_loss += batch_loss
if (_+1) % epoch_lapse == 0:
val_loss = get_val_loss(x_val, y_val, width_out, height_out, unet)
print("Total loss in epoch %f : %f and validation loss : %f" %(_+1, total_loss, val_loss))
gc.collect()
示例10: count_seqs_with_words
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def count_seqs_with_words(seqs, halflength, ming, maxg, alpha, revcomp, desc):
if alpha == 'protein':
ambiguous_character = 'X'
else:
ambiguous_character = 'N'
gapped_kmer_dict = {} # each key is the gapped k-mer word
for g in trange(ming, maxg + 1, 1, desc=desc):
w = g+2*halflength # length of the word
gap = g * ambiguous_character
for seq in seqs:
slen = len(seq)
for i in range(0, slen-w+1):
word = seq[i : i+w]
# skip word if it contains an ambiguous character
if ambiguous_character in word:
continue
# convert word to a gapped word. Only the first and last half-length letters are preserved
word = word[0:halflength] + gap + word[-halflength:]
update_gapped_kmer_dict(gapped_kmer_dict, word, revcomp)
return gapped_kmer_dict
示例11: image_copy_to_dir
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def image_copy_to_dir(mode, x_paths, y_paths):
target_path = '/run/media/tkwoo/myWorkspace/workspace/01.dataset/03.Mask_data/cityscape'
target_path = os.path.join(target_path, mode)
for idx in trange(len(x_paths)):
image = cv2.imread(x_paths[idx], 1)
mask = cv2.imread(y_paths[idx], 0)
image = cv2.resize(image, None, fx=0.25, fy=0.25, interpolation=cv2.INTER_LINEAR)
mask = cv2.resize(mask, None, fx=0.25, fy=0.25, interpolation=cv2.INTER_NEAREST)
cv2.imwrite(os.path.join(target_path, 'image', os.path.basename(x_paths[idx])), image)
cv2.imwrite(os.path.join(target_path, 'mask', os.path.basename(y_paths[idx])), mask)
# show = image.copy()
# mask = (mask.astype(np.float32)*255/33).astype(np.uint8)
# mask_color = cv2.applyColorMap(mask, cv2.COLORMAP_JET)
# show = cv2.addWeighted(show, 0.5, mask_color, 0.5, 0.0)
# cv2.imshow('show', show)
# key = cv2.waitKey(1)
# if key == 27:
# return
示例12: _make_progress_bar
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def _make_progress_bar(self, iterations):
"""
Creates a progress bar using :class:`tqdm`.
Parameters
----------
iterations: `int`
Number of iterations to be performed.
Returns
-------
progress_bar: :class:`tqdm.std.tqdm`
An iterator object.
"""
progress_bar = tqdm.trange(
iterations,
unit_scale=(self._chunksize // 1024),
unit="KiB",
dynamic_ncols=True,
bar_format='{desc}: {percentage:3.0f}%|{bar}| {n_fmt}/{total_fmt}KiB '
'[{elapsed}<{remaining}, {rate_fmt}{postfix}]',
)
return progress_bar
示例13: _preprocess
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def _preprocess(self, ids, ids_file):
print("Preprocessing mask, this will take a while." + \
"But don't worry, it only run once for each split.")
tbar = trange(len(ids))
new_ids = []
for i in tbar:
img_id = ids[i]
cocotarget = self.coco.loadAnns(self.coco.getAnnIds(imgIds=img_id))
img_metadata = self.coco.loadImgs(img_id)[0]
mask = self._gen_seg_mask(cocotarget, img_metadata['height'],
img_metadata['width'])
# more than 1k pixels
if (mask > 0).sum() > 1000:
new_ids.append(img_id)
tbar.set_description('Doing: {}/{}, got {} qualified images'.\
format(i, len(ids), len(new_ids)))
print('Found number of qualified images: ', len(new_ids))
with open(ids_file, 'wb') as f:
pickle.dump(new_ids, f)
return new_ids
示例14: _preprocess
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def _preprocess(self, ids, ids_file):
print("Preprocessing mask, this will take a while." + \
"But don't worry, it only run once for each split.")
tbar = trange(len(ids))
new_ids = []
for i in tbar:
img_id = ids[i]
cocotarget = self.coco.loadAnns(self.coco.getAnnIds(imgIds=img_id))
img_metadata = self.coco.loadImgs(img_id)[0]
mask = self._gen_seg_mask(cocotarget, img_metadata['height'],
img_metadata['width'])
# more than 1k pixels
if (mask > 0).sum() > 1000:
new_ids.append(img_id)
tbar.set_description('Doing: {}/{}, got {} qualified images'.\
format(i, len(ids), len(new_ids)))
print('Found number of qualified images: ', len(new_ids))
torch.save(new_ids, ids_file)
return new_ids
示例15: _filter_idx
# 需要导入模块: import tqdm [as 别名]
# 或者: from tqdm import trange [as 别名]
def _filter_idx(self,
idx,
idx_file,
pixels_thr=1000):
logging.info("Filtering mask index")
tbar = trange(len(idx))
filtered_idx = []
for i in tbar:
img_id = idx[i]
coco_target = self.coco.loadAnns(self.coco.getAnnIds(imgIds=img_id))
img_metadata = self.coco.loadImgs(img_id)[0]
mask = self._gen_seg_mask(
coco_target,
img_metadata["height"],
img_metadata["width"])
if (mask > 0).sum() > pixels_thr:
filtered_idx.append(img_id)
tbar.set_description("Doing: {}/{}, got {} qualified images".format(i, len(idx), len(filtered_idx)))
logging.info("Found number of qualified images: {}".format(len(filtered_idx)))
np.save(idx_file, np.array(filtered_idx, np.int32))
return filtered_idx