Pytorch
2018-05-25 本文已影响0人
Dong_Chen
1. Python format 格式化函数:
#Add the convolutional layer
conv = nn.Conv2d(prev_filters, filters, kernel_size, stride, pad, bias = bias)
module.add_module("conv_{0}".format(index), conv)
#Add the Batch Norm Layer
if batch_normalize:
bn = nn.BatchNorm2d(filters)
module.add_module("batch_norm_{0}".format(index), bn)
here using . format
to automatically increase the name of layers.
. format
, 新增了一种格式化字符串的函数 str.format() the output is like
(0): Sequential(
(conv_0): Conv2d(3, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(batch_norm_0): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(leaky_0): LeakyReLU(negative_slope=0.1, inplace
2. module.add_module
3. torch.cat(seq, dim=0, out=None) → Tensor
Concatenates the given sequence of seq tensors in the given dimension.
4. How to read images sequencetially
https://stackoverflow.com/questions/6773584/how-is-pythons-glob-glob-ordered
import os, glob
def sortKeyFunc(s): #used to sort the list by order
return int(os.path.basename(s)[:-4])
myList = ["c:\tmp\x\123.csv", "c:\tmp\x\44.csv", "c:\tmp\x\101.csv", "c:\tmp\x\102.csv", "c:\tmp\x\12.csv"]
myList.sort(key=sortKeyFunc)
output as: ["c:\tmp\x\12.csv", "c:\tmp\x\44.csv", "c:\tmp\x\101.csv", "c:\tmp\x\102.csv", "c:\tmp\x\123.csv"]
5. How to rename files
https://github.com/sspatil89/renameFile/blob/master/renameFile.py
import os
"""
Renames the multiple file within the same directory with appending number
"""
path = 'Enter the directory path'
files = os.listdir(path)
i = 1
for file in files:
filename, file_extension = os.path.splitext(file)
os.rename(os.path.join(path, file), os.path.join(path, filename + str(i) + file_extension))
i = i+1
6. Video2frames
args = arg_parse()
#### convert video to frames
#videofile = args.video
# cap = cv2.VideoCapture(videofile)
# assert cap.isOpened(), 'Cannot capture source'
#
# success, image = cap.read()
# count = 0
# success = True
#
# if not os.path.exists(args.det):
# os.makedirs(args.det)
# directory = args.det
#
# print("Starting to convert ")
# while success:
# success, image = cap.read()
# cv2.imwrite(os.path.join(directory, "%d.jpg" % count), image) # save frame as JPEG file
# # cv2.imwrite("directory:\\frame%d.jpg" % count, image) # save frame as JPEG file
# if cv2.waitKey(10) == 27: # exit if Escape is hit
# break
# count += 1
# print("Completed video2frames")
7. read a dictionary and sort by the key
a = np.load('/home/biyi/PycharmProjects/SIMD/pytorch-yolo-v3/dic.npy').item()
od = sorted(a.items())
# print(od)