From 94ffbbf0ea1d6106d78d8dc577249d06d3b17468 Mon Sep 17 00:00:00 2001 From: Malinda Date: Sat, 6 Aug 2022 02:45:01 -0700 Subject: [PATCH 1/3] Using with stmt --- run_a_pair.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/run_a_pair.py b/run_a_pair.py index 0e6aea23..d796b3f3 100644 --- a/run_a_pair.py +++ b/run_a_pair.py @@ -32,13 +32,12 @@ # save flow, I reference the code in scripts/run-flownet.py in flownet2-caffe project def writeFlow(name, flow): - f = open(name, 'wb') - f.write('PIEH'.encode('utf-8')) - np.array([flow.shape[1], flow.shape[0]], dtype=np.int32).tofile(f) - flow = flow.astype(np.float32) - flow.tofile(f) - f.flush() - f.close() + with open(name, 'wb') as f: + f.write('PIEH'.encode('utf-8')) + np.array([flow.shape[1], flow.shape[0]], dtype=np.int32).tofile(f) + flow = flow.astype(np.float32) + flow.tofile(f) + f.flush() data = result.data.cpu().numpy().transpose(1, 2, 0) From 6690417a7343d95abfa6944fa10756f5a5a5f8f5 Mon Sep 17 00:00:00 2001 From: Malinda Date: Sat, 6 Aug 2022 02:46:00 -0700 Subject: [PATCH 2/3] Using with stmt --- utils/flow_utils.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/utils/flow_utils.py b/utils/flow_utils.py index c0b04956..2b68365d 100755 --- a/utils/flow_utils.py +++ b/utils/flow_utils.py @@ -44,17 +44,16 @@ def writeFlow(filename,uv,v=None): assert(u.shape == v.shape) height,width = u.shape - f = open(filename,'wb') - # write the header - f.write(TAG_CHAR) - np.array(width).astype(np.int32).tofile(f) - np.array(height).astype(np.int32).tofile(f) - # arrange into matrix form - tmp = np.zeros((height, width*nBands)) - tmp[:,np.arange(width)*2] = u - tmp[:,np.arange(width)*2 + 1] = v - tmp.astype(np.float32).tofile(f) - f.close() + with open(filename,'wb') as f: + # write the header + f.write(TAG_CHAR) + np.array(width).astype(np.int32).tofile(f) + np.array(height).astype(np.int32).tofile(f) + # arrange into matrix form + tmp = np.zeros((height, width*nBands)) + tmp[:,np.arange(width)*2] = u + tmp[:,np.arange(width)*2 + 1] = v + tmp.astype(np.float32).tofile(f) # ref: https://github.com/sampepose/flownet2-tf/ From 593359c8fd581cb4e6f3694b4e1e5fdcab948047 Mon Sep 17 00:00:00 2001 From: Malinda Date: Sat, 6 Aug 2022 02:46:47 -0700 Subject: [PATCH 3/3] Using with stmt --- utils/tools.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/utils/tools.py b/utils/tools.py index 0de5ee71..b299338b 100755 --- a/utils/tools.py +++ b/utils/tools.py @@ -48,9 +48,8 @@ def log(self, string): print((" [{:.3f}{}] {}".format(duration, units, string))) def log2file(self, fid, string): - fid = open(fid, 'a') - fid.write("%s\n"%(string)) - fid.close() + with open(fid, 'a') as fid: + fid.write("%s\n"%(string)) def add_arguments_for_module(parser, module, argument_for_class, default, skip_params=[], parameter_defaults={}): argument_group = parser.add_argument_group(argument_for_class.capitalize())