Skip to content

Commit 409a726

Browse files
fyuapaszke
authored andcommitted
add flush to print (pytorch#81)
With flush, log info can appear immediately when it is directed to a pipe or file.
1 parent 1c16b6c commit 409a726

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

imagenet/main.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from __future__ import print_function
12
import argparse
23
import os
34
import shutil
@@ -59,10 +60,10 @@ def main():
5960

6061
# create model
6162
if args.pretrained:
62-
print("=> using pre-trained model '{}'".format(args.arch))
63+
print("=> using pre-trained model '{}'".format(args.arch), flush=True)
6364
model = models.__dict__[args.arch](pretrained=True)
6465
else:
65-
print("=> creating model '{}'".format(args.arch))
66+
print("=> creating model '{}'".format(args.arch), flush=True)
6667
model = models.__dict__[args.arch]()
6768

6869
if args.arch.startswith('alexnet') or args.arch.startswith('vgg'):
@@ -74,15 +75,16 @@ def main():
7475
# optionally resume from a checkpoint
7576
if args.resume:
7677
if os.path.isfile(args.resume):
77-
print("=> loading checkpoint '{}'".format(args.resume))
78+
print("=> loading checkpoint '{}'".format(args.resume), flush=True)
7879
checkpoint = torch.load(args.resume)
7980
args.start_epoch = checkpoint['epoch']
8081
best_prec1 = checkpoint['best_prec1']
8182
model.load_state_dict(checkpoint['state_dict'])
8283
print("=> loaded checkpoint '{}' (epoch {})"
83-
.format(args.evaluate, checkpoint['epoch']))
84+
.format(args.evaluate, checkpoint['epoch']), flush=True)
8485
else:
85-
print("=> no checkpoint found at '{}'".format(args.resume))
86+
print("=> no checkpoint found at '{}'".format(args.resume),
87+
flush=True)
8688

8789
cudnn.benchmark = True
8890

@@ -189,7 +191,8 @@ def train(train_loader, model, criterion, optimizer, epoch):
189191
'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'
190192
'Prec@5 {top5.val:.3f} ({top5.avg:.3f})'.format(
191193
epoch, i, len(train_loader), batch_time=batch_time,
192-
data_time=data_time, loss=losses, top1=top1, top5=top5))
194+
data_time=data_time, loss=losses, top1=top1, top5=top5),
195+
flush=True)
193196

194197

195198
def validate(val_loader, model, criterion):
@@ -228,10 +231,10 @@ def validate(val_loader, model, criterion):
228231
'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'
229232
'Prec@5 {top5.val:.3f} ({top5.avg:.3f})'.format(
230233
i, len(val_loader), batch_time=batch_time, loss=losses,
231-
top1=top1, top5=top5))
234+
top1=top1, top5=top5), flush=True)
232235

233236
print(' * Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f}'
234-
.format(top1=top1, top5=top5))
237+
.format(top1=top1, top5=top5), flush=True)
235238

236239
return top1.avg
237240

0 commit comments

Comments
 (0)