nwo
stringlengths 6
76
| sha
stringlengths 40
40
| path
stringlengths 5
118
| language
stringclasses 1
value | identifier
stringlengths 1
89
| parameters
stringlengths 2
5.4k
| argument_list
stringclasses 1
value | return_statement
stringlengths 0
51.1k
| docstring
stringlengths 1
17.6k
| docstring_summary
stringlengths 0
7.02k
| docstring_tokens
sequence | function
stringlengths 30
51.1k
| function_tokens
sequence | url
stringlengths 85
218
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
zzzkk2009/casia-surf-2019-codes | b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b | data/im2rec.py | python | make_list | (args) | Generates .lst file.
Parameters
----------
args: object that contains all the arguments | Generates .lst file.
Parameters
----------
args: object that contains all the arguments | [
"Generates",
".",
"lst",
"file",
".",
"Parameters",
"----------",
"args",
":",
"object",
"that",
"contains",
"all",
"the",
"arguments"
] | def make_list(args):
"""Generates .lst file.
Parameters
----------
args: object that contains all the arguments
"""
image_list = list_image(args.root, args.recursive, args.exts)
image_list = list(image_list)
if args.shuffle is True:
random.seed(100)
random.shuffle(image_list)
N = len(image_list)
chunk_size = (N + args.chunks - 1) // args.chunks
for i in range(args.chunks):
chunk = image_list[i * chunk_size:(i + 1) * chunk_size]
if args.chunks > 1:
str_chunk = '_%d' % i
else:
str_chunk = ''
sep = int(chunk_size * args.train_ratio)
sep_test = int(chunk_size * args.test_ratio)
if args.train_ratio == 1.0:
write_list(args.prefix + str_chunk + '.lst', chunk)
else:
if args.test_ratio:
write_list(args.prefix + str_chunk + '_test.lst', chunk[:sep_test])
if args.train_ratio + args.test_ratio < 1.0:
write_list(args.prefix + str_chunk + '_val.lst', chunk[sep_test + sep:])
write_list(args.prefix + str_chunk + '_train.lst', chunk[sep_test:sep_test + sep]) | [
"def",
"make_list",
"(",
"args",
")",
":",
"image_list",
"=",
"list_image",
"(",
"args",
".",
"root",
",",
"args",
".",
"recursive",
",",
"args",
".",
"exts",
")",
"image_list",
"=",
"list",
"(",
"image_list",
")",
"if",
"args",
".",
"shuffle",
"is",
"True",
":",
"random",
".",
"seed",
"(",
"100",
")",
"random",
".",
"shuffle",
"(",
"image_list",
")",
"N",
"=",
"len",
"(",
"image_list",
")",
"chunk_size",
"=",
"(",
"N",
"+",
"args",
".",
"chunks",
"-",
"1",
")",
"//",
"args",
".",
"chunks",
"for",
"i",
"in",
"range",
"(",
"args",
".",
"chunks",
")",
":",
"chunk",
"=",
"image_list",
"[",
"i",
"*",
"chunk_size",
":",
"(",
"i",
"+",
"1",
")",
"*",
"chunk_size",
"]",
"if",
"args",
".",
"chunks",
">",
"1",
":",
"str_chunk",
"=",
"'_%d'",
"%",
"i",
"else",
":",
"str_chunk",
"=",
"''",
"sep",
"=",
"int",
"(",
"chunk_size",
"*",
"args",
".",
"train_ratio",
")",
"sep_test",
"=",
"int",
"(",
"chunk_size",
"*",
"args",
".",
"test_ratio",
")",
"if",
"args",
".",
"train_ratio",
"==",
"1.0",
":",
"write_list",
"(",
"args",
".",
"prefix",
"+",
"str_chunk",
"+",
"'.lst'",
",",
"chunk",
")",
"else",
":",
"if",
"args",
".",
"test_ratio",
":",
"write_list",
"(",
"args",
".",
"prefix",
"+",
"str_chunk",
"+",
"'_test.lst'",
",",
"chunk",
"[",
":",
"sep_test",
"]",
")",
"if",
"args",
".",
"train_ratio",
"+",
"args",
".",
"test_ratio",
"<",
"1.0",
":",
"write_list",
"(",
"args",
".",
"prefix",
"+",
"str_chunk",
"+",
"'_val.lst'",
",",
"chunk",
"[",
"sep_test",
"+",
"sep",
":",
"]",
")",
"write_list",
"(",
"args",
".",
"prefix",
"+",
"str_chunk",
"+",
"'_train.lst'",
",",
"chunk",
"[",
"sep_test",
":",
"sep_test",
"+",
"sep",
"]",
")"
] | https://github.com/zzzkk2009/casia-surf-2019-codes/blob/b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b/data/im2rec.py#L150-L178 |
||
zzzkk2009/casia-surf-2019-codes | b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b | data/im2rec.py | python | read_list | (path_in) | Reads the .lst file and generates corresponding iterator.
Parameters
----------
path_in: string
Returns
-------
item iterator that contains information in .lst file | Reads the .lst file and generates corresponding iterator.
Parameters
----------
path_in: string
Returns
-------
item iterator that contains information in .lst file | [
"Reads",
"the",
".",
"lst",
"file",
"and",
"generates",
"corresponding",
"iterator",
".",
"Parameters",
"----------",
"path_in",
":",
"string",
"Returns",
"-------",
"item",
"iterator",
"that",
"contains",
"information",
"in",
".",
"lst",
"file"
] | def read_list(path_in):
"""Reads the .lst file and generates corresponding iterator.
Parameters
----------
path_in: string
Returns
-------
item iterator that contains information in .lst file
"""
with open(path_in) as fin:
while True:
line = fin.readline()
if not line:
break
line = [i.strip() for i in line.strip().split('\t')]
line_len = len(line)
# check the data format of .lst file
if line_len < 3:
print('lst should have at least has three parts, but only has %s parts for %s' % (line_len, line))
continue
try:
item = [int(line[0])] + [line[-1]] + [float(i) for i in line[1:-1]]
except Exception as e:
print('Parsing lst met error for %s, detail: %s' % (line, e))
continue
yield item | [
"def",
"read_list",
"(",
"path_in",
")",
":",
"with",
"open",
"(",
"path_in",
")",
"as",
"fin",
":",
"while",
"True",
":",
"line",
"=",
"fin",
".",
"readline",
"(",
")",
"if",
"not",
"line",
":",
"break",
"line",
"=",
"[",
"i",
".",
"strip",
"(",
")",
"for",
"i",
"in",
"line",
".",
"strip",
"(",
")",
".",
"split",
"(",
"'\\t'",
")",
"]",
"line_len",
"=",
"len",
"(",
"line",
")",
"# check the data format of .lst file",
"if",
"line_len",
"<",
"3",
":",
"print",
"(",
"'lst should have at least has three parts, but only has %s parts for %s'",
"%",
"(",
"line_len",
",",
"line",
")",
")",
"continue",
"try",
":",
"item",
"=",
"[",
"int",
"(",
"line",
"[",
"0",
"]",
")",
"]",
"+",
"[",
"line",
"[",
"-",
"1",
"]",
"]",
"+",
"[",
"float",
"(",
"i",
")",
"for",
"i",
"in",
"line",
"[",
"1",
":",
"-",
"1",
"]",
"]",
"except",
"Exception",
"as",
"e",
":",
"print",
"(",
"'Parsing lst met error for %s, detail: %s'",
"%",
"(",
"line",
",",
"e",
")",
")",
"continue",
"yield",
"item"
] | https://github.com/zzzkk2009/casia-surf-2019-codes/blob/b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b/data/im2rec.py#L180-L205 |
||
zzzkk2009/casia-surf-2019-codes | b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b | data/im2rec.py | python | image_encode | (args, i, item, q_out) | Reads, preprocesses, packs the image and put it back in output queue.
Parameters
----------
args: object
i: int
item: list
q_out: queue | Reads, preprocesses, packs the image and put it back in output queue.
Parameters
----------
args: object
i: int
item: list
q_out: queue | [
"Reads",
"preprocesses",
"packs",
"the",
"image",
"and",
"put",
"it",
"back",
"in",
"output",
"queue",
".",
"Parameters",
"----------",
"args",
":",
"object",
"i",
":",
"int",
"item",
":",
"list",
"q_out",
":",
"queue"
] | def image_encode(args, i, item, q_out):
"""Reads, preprocesses, packs the image and put it back in output queue.
Parameters
----------
args: object
i: int
item: list
q_out: queue
"""
fullpath = os.path.join(args.root, item[1])
if len(item) > 3 and args.pack_label:
header = mx.recordio.IRHeader(0, item[2:], item[0], 0)
else:
header = mx.recordio.IRHeader(0, item[2], item[0], 0)
if args.pass_through:
try:
with open(fullpath, 'rb') as fin:
img = fin.read()
s = mx.recordio.pack(header, img)
q_out.put((i, s, item))
except Exception as e:
traceback.print_exc()
print('pack_img error:', item[1], e)
q_out.put((i, None, item))
return
try:
img = cv2.imread(fullpath, args.color)
except:
traceback.print_exc()
print('imread error trying to load file: %s ' % fullpath)
q_out.put((i, None, item))
return
if img is None:
print('imread read blank (None) image for file: %s' % fullpath)
q_out.put((i, None, item))
return
if args.center_crop:
if img.shape[0] > img.shape[1]:
margin = (img.shape[0] - img.shape[1]) // 2
img = img[margin:margin + img.shape[1], :]
else:
margin = (img.shape[1] - img.shape[0]) // 2
img = img[:, margin:margin + img.shape[0]]
if args.resize:
if img.shape[0] > img.shape[1]:
newsize = (args.resize, img.shape[0] * args.resize // img.shape[1])
else:
newsize = (img.shape[1] * args.resize // img.shape[0], args.resize)
img = cv2.resize(img, newsize)
try:
s = mx.recordio.pack_img(header, img, quality=args.quality, img_fmt=args.encoding)
q_out.put((i, s, item))
except Exception as e:
traceback.print_exc()
print('pack_img error on file: %s' % fullpath, e)
q_out.put((i, None, item))
return | [
"def",
"image_encode",
"(",
"args",
",",
"i",
",",
"item",
",",
"q_out",
")",
":",
"fullpath",
"=",
"os",
".",
"path",
".",
"join",
"(",
"args",
".",
"root",
",",
"item",
"[",
"1",
"]",
")",
"if",
"len",
"(",
"item",
")",
">",
"3",
"and",
"args",
".",
"pack_label",
":",
"header",
"=",
"mx",
".",
"recordio",
".",
"IRHeader",
"(",
"0",
",",
"item",
"[",
"2",
":",
"]",
",",
"item",
"[",
"0",
"]",
",",
"0",
")",
"else",
":",
"header",
"=",
"mx",
".",
"recordio",
".",
"IRHeader",
"(",
"0",
",",
"item",
"[",
"2",
"]",
",",
"item",
"[",
"0",
"]",
",",
"0",
")",
"if",
"args",
".",
"pass_through",
":",
"try",
":",
"with",
"open",
"(",
"fullpath",
",",
"'rb'",
")",
"as",
"fin",
":",
"img",
"=",
"fin",
".",
"read",
"(",
")",
"s",
"=",
"mx",
".",
"recordio",
".",
"pack",
"(",
"header",
",",
"img",
")",
"q_out",
".",
"put",
"(",
"(",
"i",
",",
"s",
",",
"item",
")",
")",
"except",
"Exception",
"as",
"e",
":",
"traceback",
".",
"print_exc",
"(",
")",
"print",
"(",
"'pack_img error:'",
",",
"item",
"[",
"1",
"]",
",",
"e",
")",
"q_out",
".",
"put",
"(",
"(",
"i",
",",
"None",
",",
"item",
")",
")",
"return",
"try",
":",
"img",
"=",
"cv2",
".",
"imread",
"(",
"fullpath",
",",
"args",
".",
"color",
")",
"except",
":",
"traceback",
".",
"print_exc",
"(",
")",
"print",
"(",
"'imread error trying to load file: %s '",
"%",
"fullpath",
")",
"q_out",
".",
"put",
"(",
"(",
"i",
",",
"None",
",",
"item",
")",
")",
"return",
"if",
"img",
"is",
"None",
":",
"print",
"(",
"'imread read blank (None) image for file: %s'",
"%",
"fullpath",
")",
"q_out",
".",
"put",
"(",
"(",
"i",
",",
"None",
",",
"item",
")",
")",
"return",
"if",
"args",
".",
"center_crop",
":",
"if",
"img",
".",
"shape",
"[",
"0",
"]",
">",
"img",
".",
"shape",
"[",
"1",
"]",
":",
"margin",
"=",
"(",
"img",
".",
"shape",
"[",
"0",
"]",
"-",
"img",
".",
"shape",
"[",
"1",
"]",
")",
"//",
"2",
"img",
"=",
"img",
"[",
"margin",
":",
"margin",
"+",
"img",
".",
"shape",
"[",
"1",
"]",
",",
":",
"]",
"else",
":",
"margin",
"=",
"(",
"img",
".",
"shape",
"[",
"1",
"]",
"-",
"img",
".",
"shape",
"[",
"0",
"]",
")",
"//",
"2",
"img",
"=",
"img",
"[",
":",
",",
"margin",
":",
"margin",
"+",
"img",
".",
"shape",
"[",
"0",
"]",
"]",
"if",
"args",
".",
"resize",
":",
"if",
"img",
".",
"shape",
"[",
"0",
"]",
">",
"img",
".",
"shape",
"[",
"1",
"]",
":",
"newsize",
"=",
"(",
"args",
".",
"resize",
",",
"img",
".",
"shape",
"[",
"0",
"]",
"*",
"args",
".",
"resize",
"//",
"img",
".",
"shape",
"[",
"1",
"]",
")",
"else",
":",
"newsize",
"=",
"(",
"img",
".",
"shape",
"[",
"1",
"]",
"*",
"args",
".",
"resize",
"//",
"img",
".",
"shape",
"[",
"0",
"]",
",",
"args",
".",
"resize",
")",
"img",
"=",
"cv2",
".",
"resize",
"(",
"img",
",",
"newsize",
")",
"try",
":",
"s",
"=",
"mx",
".",
"recordio",
".",
"pack_img",
"(",
"header",
",",
"img",
",",
"quality",
"=",
"args",
".",
"quality",
",",
"img_fmt",
"=",
"args",
".",
"encoding",
")",
"q_out",
".",
"put",
"(",
"(",
"i",
",",
"s",
",",
"item",
")",
")",
"except",
"Exception",
"as",
"e",
":",
"traceback",
".",
"print_exc",
"(",
")",
"print",
"(",
"'pack_img error on file: %s'",
"%",
"fullpath",
",",
"e",
")",
"q_out",
".",
"put",
"(",
"(",
"i",
",",
"None",
",",
"item",
")",
")",
"return"
] | https://github.com/zzzkk2009/casia-surf-2019-codes/blob/b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b/data/im2rec.py#L207-L267 |
||
zzzkk2009/casia-surf-2019-codes | b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b | data/im2rec.py | python | read_worker | (args, q_in, q_out) | Function that will be spawned to fetch the image
from the input queue and put it back to output queue.
Parameters
----------
args: object
q_in: queue
q_out: queue | Function that will be spawned to fetch the image
from the input queue and put it back to output queue.
Parameters
----------
args: object
q_in: queue
q_out: queue | [
"Function",
"that",
"will",
"be",
"spawned",
"to",
"fetch",
"the",
"image",
"from",
"the",
"input",
"queue",
"and",
"put",
"it",
"back",
"to",
"output",
"queue",
".",
"Parameters",
"----------",
"args",
":",
"object",
"q_in",
":",
"queue",
"q_out",
":",
"queue"
] | def read_worker(args, q_in, q_out):
"""Function that will be spawned to fetch the image
from the input queue and put it back to output queue.
Parameters
----------
args: object
q_in: queue
q_out: queue
"""
while True:
deq = q_in.get()
if deq is None:
break
i, item = deq
image_encode(args, i, item, q_out) | [
"def",
"read_worker",
"(",
"args",
",",
"q_in",
",",
"q_out",
")",
":",
"while",
"True",
":",
"deq",
"=",
"q_in",
".",
"get",
"(",
")",
"if",
"deq",
"is",
"None",
":",
"break",
"i",
",",
"item",
"=",
"deq",
"image_encode",
"(",
"args",
",",
"i",
",",
"item",
",",
"q_out",
")"
] | https://github.com/zzzkk2009/casia-surf-2019-codes/blob/b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b/data/im2rec.py#L269-L283 |
||
zzzkk2009/casia-surf-2019-codes | b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b | data/im2rec.py | python | write_worker | (q_out, fname, working_dir) | Function that will be spawned to fetch processed image
from the output queue and write to the .rec file.
Parameters
----------
q_out: queue
fname: string
working_dir: string | Function that will be spawned to fetch processed image
from the output queue and write to the .rec file.
Parameters
----------
q_out: queue
fname: string
working_dir: string | [
"Function",
"that",
"will",
"be",
"spawned",
"to",
"fetch",
"processed",
"image",
"from",
"the",
"output",
"queue",
"and",
"write",
"to",
"the",
".",
"rec",
"file",
".",
"Parameters",
"----------",
"q_out",
":",
"queue",
"fname",
":",
"string",
"working_dir",
":",
"string"
] | def write_worker(q_out, fname, working_dir):
"""Function that will be spawned to fetch processed image
from the output queue and write to the .rec file.
Parameters
----------
q_out: queue
fname: string
working_dir: string
"""
pre_time = time.time()
count = 0
fname = os.path.basename(fname)
fname_rec = os.path.splitext(fname)[0] + '.rec'
fname_idx = os.path.splitext(fname)[0] + '.idx'
record = mx.recordio.MXIndexedRecordIO(os.path.join(working_dir, fname_idx),
os.path.join(working_dir, fname_rec), 'w')
buf = {}
more = True
while more:
deq = q_out.get()
if deq is not None:
i, s, item = deq
buf[i] = (s, item)
else:
more = False
while count in buf:
s, item = buf[count]
del buf[count]
if s is not None:
record.write_idx(item[0], s)
if count % 1000 == 0:
cur_time = time.time()
print('time:', cur_time - pre_time, ' count:', count)
pre_time = cur_time
count += 1 | [
"def",
"write_worker",
"(",
"q_out",
",",
"fname",
",",
"working_dir",
")",
":",
"pre_time",
"=",
"time",
".",
"time",
"(",
")",
"count",
"=",
"0",
"fname",
"=",
"os",
".",
"path",
".",
"basename",
"(",
"fname",
")",
"fname_rec",
"=",
"os",
".",
"path",
".",
"splitext",
"(",
"fname",
")",
"[",
"0",
"]",
"+",
"'.rec'",
"fname_idx",
"=",
"os",
".",
"path",
".",
"splitext",
"(",
"fname",
")",
"[",
"0",
"]",
"+",
"'.idx'",
"record",
"=",
"mx",
".",
"recordio",
".",
"MXIndexedRecordIO",
"(",
"os",
".",
"path",
".",
"join",
"(",
"working_dir",
",",
"fname_idx",
")",
",",
"os",
".",
"path",
".",
"join",
"(",
"working_dir",
",",
"fname_rec",
")",
",",
"'w'",
")",
"buf",
"=",
"{",
"}",
"more",
"=",
"True",
"while",
"more",
":",
"deq",
"=",
"q_out",
".",
"get",
"(",
")",
"if",
"deq",
"is",
"not",
"None",
":",
"i",
",",
"s",
",",
"item",
"=",
"deq",
"buf",
"[",
"i",
"]",
"=",
"(",
"s",
",",
"item",
")",
"else",
":",
"more",
"=",
"False",
"while",
"count",
"in",
"buf",
":",
"s",
",",
"item",
"=",
"buf",
"[",
"count",
"]",
"del",
"buf",
"[",
"count",
"]",
"if",
"s",
"is",
"not",
"None",
":",
"record",
".",
"write_idx",
"(",
"item",
"[",
"0",
"]",
",",
"s",
")",
"if",
"count",
"%",
"1000",
"==",
"0",
":",
"cur_time",
"=",
"time",
".",
"time",
"(",
")",
"print",
"(",
"'time:'",
",",
"cur_time",
"-",
"pre_time",
",",
"' count:'",
",",
"count",
")",
"pre_time",
"=",
"cur_time",
"count",
"+=",
"1"
] | https://github.com/zzzkk2009/casia-surf-2019-codes/blob/b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b/data/im2rec.py#L285-L320 |
||
zzzkk2009/casia-surf-2019-codes | b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b | data/im2rec.py | python | parse_args | () | return args | Defines all arguments.
Returns
-------
args object that contains all the params | Defines all arguments.
Returns
-------
args object that contains all the params | [
"Defines",
"all",
"arguments",
".",
"Returns",
"-------",
"args",
"object",
"that",
"contains",
"all",
"the",
"params"
] | def parse_args():
"""Defines all arguments.
Returns
-------
args object that contains all the params
"""
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description='Create an image list or \
make a record database by reading from an image list')
parser.add_argument('prefix', help='prefix of input/output lst and rec files.')
parser.add_argument('root', help='path to folder containing images.')
cgroup = parser.add_argument_group('Options for creating image lists')
cgroup.add_argument('--list', action='store_true',
help='If this is set im2rec will create image list(s) by traversing root folder\
and output to <prefix>.lst.\
Otherwise im2rec will read <prefix>.lst and create a database at <prefix>.rec')
cgroup.add_argument('--exts', nargs='+', default=['.jpeg', '.jpg', '.png'],
help='list of acceptable image extensions.')
cgroup.add_argument('--chunks', type=int, default=1, help='number of chunks.')
cgroup.add_argument('--train-ratio', type=float, default=1.0,
help='Ratio of images to use for training.')
cgroup.add_argument('--test-ratio', type=float, default=0,
help='Ratio of images to use for testing.')
cgroup.add_argument('--recursive', action='store_true',
help='If true recursively walk through subdirs and assign an unique label\
to images in each folder. Otherwise only include images in the root folder\
and give them label 0.')
cgroup.add_argument('--recursive-label-n', type=int, default=1, help='root folder下面第一层文件夹作为label标签')
cgroup.add_argument('--no-shuffle', dest='shuffle', action='store_false',
help='If this is passed, \
im2rec will not randomize the image order in <prefix>.lst')
rgroup = parser.add_argument_group('Options for creating database')
rgroup.add_argument('--pass-through', action='store_true',
help='whether to skip transformation and save image as is')
rgroup.add_argument('--resize', type=int, default=0,
help='resize the shorter edge of image to the newsize, original images will\
be packed by default.')
rgroup.add_argument('--center-crop', action='store_true',
help='specify whether to crop the center image to make it rectangular.')
rgroup.add_argument('--quality', type=int, default=95,
help='JPEG quality for encoding, 1-100; or PNG compression for encoding, 1-9')
rgroup.add_argument('--num-thread', type=int, default=1,
help='number of thread to use for encoding. order of images will be different\
from the input list if >1. the input list will be modified to match the\
resulting order.')
rgroup.add_argument('--color', type=int, default=1, choices=[-1, 0, 1],
help='specify the color mode of the loaded image.\
1: Loads a color image. Any transparency of image will be neglected. It is the default flag.\
0: Loads image in grayscale mode.\
-1:Loads image as such including alpha channel.')
rgroup.add_argument('--encoding', type=str, default='.jpg', choices=['.jpg', '.png'],
help='specify the encoding of the images.')
rgroup.add_argument('--pack-label', action='store_true',
help='Whether to also pack multi dimensional label in the record file')
args = parser.parse_args()
args.prefix = os.path.abspath(args.prefix)
args.root = os.path.abspath(args.root)
return args | [
"def",
"parse_args",
"(",
")",
":",
"parser",
"=",
"argparse",
".",
"ArgumentParser",
"(",
"formatter_class",
"=",
"argparse",
".",
"ArgumentDefaultsHelpFormatter",
",",
"description",
"=",
"'Create an image list or \\\n make a record database by reading from an image list'",
")",
"parser",
".",
"add_argument",
"(",
"'prefix'",
",",
"help",
"=",
"'prefix of input/output lst and rec files.'",
")",
"parser",
".",
"add_argument",
"(",
"'root'",
",",
"help",
"=",
"'path to folder containing images.'",
")",
"cgroup",
"=",
"parser",
".",
"add_argument_group",
"(",
"'Options for creating image lists'",
")",
"cgroup",
".",
"add_argument",
"(",
"'--list'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"'If this is set im2rec will create image list(s) by traversing root folder\\\n and output to <prefix>.lst.\\\n Otherwise im2rec will read <prefix>.lst and create a database at <prefix>.rec'",
")",
"cgroup",
".",
"add_argument",
"(",
"'--exts'",
",",
"nargs",
"=",
"'+'",
",",
"default",
"=",
"[",
"'.jpeg'",
",",
"'.jpg'",
",",
"'.png'",
"]",
",",
"help",
"=",
"'list of acceptable image extensions.'",
")",
"cgroup",
".",
"add_argument",
"(",
"'--chunks'",
",",
"type",
"=",
"int",
",",
"default",
"=",
"1",
",",
"help",
"=",
"'number of chunks.'",
")",
"cgroup",
".",
"add_argument",
"(",
"'--train-ratio'",
",",
"type",
"=",
"float",
",",
"default",
"=",
"1.0",
",",
"help",
"=",
"'Ratio of images to use for training.'",
")",
"cgroup",
".",
"add_argument",
"(",
"'--test-ratio'",
",",
"type",
"=",
"float",
",",
"default",
"=",
"0",
",",
"help",
"=",
"'Ratio of images to use for testing.'",
")",
"cgroup",
".",
"add_argument",
"(",
"'--recursive'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"'If true recursively walk through subdirs and assign an unique label\\\n to images in each folder. Otherwise only include images in the root folder\\\n and give them label 0.'",
")",
"cgroup",
".",
"add_argument",
"(",
"'--recursive-label-n'",
",",
"type",
"=",
"int",
",",
"default",
"=",
"1",
",",
"help",
"=",
"'root folder下面第一层文件夹作为label标签')",
"",
"cgroup",
".",
"add_argument",
"(",
"'--no-shuffle'",
",",
"dest",
"=",
"'shuffle'",
",",
"action",
"=",
"'store_false'",
",",
"help",
"=",
"'If this is passed, \\\n im2rec will not randomize the image order in <prefix>.lst'",
")",
"rgroup",
"=",
"parser",
".",
"add_argument_group",
"(",
"'Options for creating database'",
")",
"rgroup",
".",
"add_argument",
"(",
"'--pass-through'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"'whether to skip transformation and save image as is'",
")",
"rgroup",
".",
"add_argument",
"(",
"'--resize'",
",",
"type",
"=",
"int",
",",
"default",
"=",
"0",
",",
"help",
"=",
"'resize the shorter edge of image to the newsize, original images will\\\n be packed by default.'",
")",
"rgroup",
".",
"add_argument",
"(",
"'--center-crop'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"'specify whether to crop the center image to make it rectangular.'",
")",
"rgroup",
".",
"add_argument",
"(",
"'--quality'",
",",
"type",
"=",
"int",
",",
"default",
"=",
"95",
",",
"help",
"=",
"'JPEG quality for encoding, 1-100; or PNG compression for encoding, 1-9'",
")",
"rgroup",
".",
"add_argument",
"(",
"'--num-thread'",
",",
"type",
"=",
"int",
",",
"default",
"=",
"1",
",",
"help",
"=",
"'number of thread to use for encoding. order of images will be different\\\n from the input list if >1. the input list will be modified to match the\\\n resulting order.'",
")",
"rgroup",
".",
"add_argument",
"(",
"'--color'",
",",
"type",
"=",
"int",
",",
"default",
"=",
"1",
",",
"choices",
"=",
"[",
"-",
"1",
",",
"0",
",",
"1",
"]",
",",
"help",
"=",
"'specify the color mode of the loaded image.\\\n 1: Loads a color image. Any transparency of image will be neglected. It is the default flag.\\\n 0: Loads image in grayscale mode.\\\n -1:Loads image as such including alpha channel.'",
")",
"rgroup",
".",
"add_argument",
"(",
"'--encoding'",
",",
"type",
"=",
"str",
",",
"default",
"=",
"'.jpg'",
",",
"choices",
"=",
"[",
"'.jpg'",
",",
"'.png'",
"]",
",",
"help",
"=",
"'specify the encoding of the images.'",
")",
"rgroup",
".",
"add_argument",
"(",
"'--pack-label'",
",",
"action",
"=",
"'store_true'",
",",
"help",
"=",
"'Whether to also pack multi dimensional label in the record file'",
")",
"args",
"=",
"parser",
".",
"parse_args",
"(",
")",
"args",
".",
"prefix",
"=",
"os",
".",
"path",
".",
"abspath",
"(",
"args",
".",
"prefix",
")",
"args",
".",
"root",
"=",
"os",
".",
"path",
".",
"abspath",
"(",
"args",
".",
"root",
")",
"return",
"args"
] | https://github.com/zzzkk2009/casia-surf-2019-codes/blob/b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b/data/im2rec.py#L322-L381 |
|
zzzkk2009/casia-surf-2019-codes | b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b | symbols/densenet.py | python | BasicBlock | (data, growth_rate, stride, name, bottle_neck=True, drop_out=0.0, bn_mom=0.9, workspace=512) | Return BaiscBlock Unit symbol for building DenseBlock
Parameters
----------
data : str
Input data
growth_rate : int
Number of output channels
stride : tupe
Stride used in convolution
drop_out : float
Probability of an element to be zeroed. Default = 0.2
name : str
Base name of the operators
workspace : int
Workspace used in convolution operator | Return BaiscBlock Unit symbol for building DenseBlock
Parameters
----------
data : str
Input data
growth_rate : int
Number of output channels
stride : tupe
Stride used in convolution
drop_out : float
Probability of an element to be zeroed. Default = 0.2
name : str
Base name of the operators
workspace : int
Workspace used in convolution operator | [
"Return",
"BaiscBlock",
"Unit",
"symbol",
"for",
"building",
"DenseBlock",
"Parameters",
"----------",
"data",
":",
"str",
"Input",
"data",
"growth_rate",
":",
"int",
"Number",
"of",
"output",
"channels",
"stride",
":",
"tupe",
"Stride",
"used",
"in",
"convolution",
"drop_out",
":",
"float",
"Probability",
"of",
"an",
"element",
"to",
"be",
"zeroed",
".",
"Default",
"=",
"0",
".",
"2",
"name",
":",
"str",
"Base",
"name",
"of",
"the",
"operators",
"workspace",
":",
"int",
"Workspace",
"used",
"in",
"convolution",
"operator"
] | def BasicBlock(data, growth_rate, stride, name, bottle_neck=True, drop_out=0.0, bn_mom=0.9, workspace=512):
"""Return BaiscBlock Unit symbol for building DenseBlock
Parameters
----------
data : str
Input data
growth_rate : int
Number of output channels
stride : tupe
Stride used in convolution
drop_out : float
Probability of an element to be zeroed. Default = 0.2
name : str
Base name of the operators
workspace : int
Workspace used in convolution operator
"""
# import pdb
# pdb.set_trace()
if bottle_neck:
# the same as https://github.com/facebook/fb.resnet.torch#notes, a bit difference with origin paper
bn1 = mx.sym.BatchNorm(data=data, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn1')
act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1')
conv1 = mx.sym.Convolution(data=act1, num_filter=int(growth_rate*4), kernel=(1,1), stride=(1,1), pad=(0,0),
no_bias=True, workspace=workspace, name=name + '_conv1')
if drop_out > 0:
conv1 = mx.symbol.Dropout(data=conv1, p=drop_out, name=name + '_dp1')
bn2 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn2')
act2 = mx.sym.Activation(data=bn2, act_type='relu', name=name + '_relu2')
conv2 = mx.sym.Convolution(data=act2, num_filter=int(growth_rate), kernel=(3,3), stride=stride, pad=(1,1),
no_bias=True, workspace=workspace, name=name + '_conv2')
if drop_out > 0:
conv2 = mx.symbol.Dropout(data=conv2, p=drop_out, name=name + '_dp2')
#return mx.symbol.Concat(data, conv2, name=name + '_concat0')
return conv2
else:
bn1 = mx.sym.BatchNorm(data=data, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn1')
act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1')
conv1 = mx.sym.Convolution(data=act1, num_filter=int(growth_rate), kernel=(3,3), stride=(1,1), pad=(1,1),
no_bias=True, workspace=workspace, name=name + '_conv1')
if drop_out > 0:
conv1 = mx.symbol.Dropout(data=conv1, p=drop_out, name=name + '_dp1')
#return mx.symbol.Concat(data, conv1, name=name + '_concat0')
return conv1 | [
"def",
"BasicBlock",
"(",
"data",
",",
"growth_rate",
",",
"stride",
",",
"name",
",",
"bottle_neck",
"=",
"True",
",",
"drop_out",
"=",
"0.0",
",",
"bn_mom",
"=",
"0.9",
",",
"workspace",
"=",
"512",
")",
":",
"# import pdb",
"# pdb.set_trace()",
"if",
"bottle_neck",
":",
"# the same as https://github.com/facebook/fb.resnet.torch#notes, a bit difference with origin paper",
"bn1",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"data",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"name",
"+",
"'_bn1'",
")",
"act1",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"bn1",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"name",
"+",
"'_relu1'",
")",
"conv1",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"act1",
",",
"num_filter",
"=",
"int",
"(",
"growth_rate",
"*",
"4",
")",
",",
"kernel",
"=",
"(",
"1",
",",
"1",
")",
",",
"stride",
"=",
"(",
"1",
",",
"1",
")",
",",
"pad",
"=",
"(",
"0",
",",
"0",
")",
",",
"no_bias",
"=",
"True",
",",
"workspace",
"=",
"workspace",
",",
"name",
"=",
"name",
"+",
"'_conv1'",
")",
"if",
"drop_out",
">",
"0",
":",
"conv1",
"=",
"mx",
".",
"symbol",
".",
"Dropout",
"(",
"data",
"=",
"conv1",
",",
"p",
"=",
"drop_out",
",",
"name",
"=",
"name",
"+",
"'_dp1'",
")",
"bn2",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"conv1",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"name",
"+",
"'_bn2'",
")",
"act2",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"bn2",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"name",
"+",
"'_relu2'",
")",
"conv2",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"act2",
",",
"num_filter",
"=",
"int",
"(",
"growth_rate",
")",
",",
"kernel",
"=",
"(",
"3",
",",
"3",
")",
",",
"stride",
"=",
"stride",
",",
"pad",
"=",
"(",
"1",
",",
"1",
")",
",",
"no_bias",
"=",
"True",
",",
"workspace",
"=",
"workspace",
",",
"name",
"=",
"name",
"+",
"'_conv2'",
")",
"if",
"drop_out",
">",
"0",
":",
"conv2",
"=",
"mx",
".",
"symbol",
".",
"Dropout",
"(",
"data",
"=",
"conv2",
",",
"p",
"=",
"drop_out",
",",
"name",
"=",
"name",
"+",
"'_dp2'",
")",
"#return mx.symbol.Concat(data, conv2, name=name + '_concat0')",
"return",
"conv2",
"else",
":",
"bn1",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"data",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"name",
"+",
"'_bn1'",
")",
"act1",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"bn1",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"name",
"+",
"'_relu1'",
")",
"conv1",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"act1",
",",
"num_filter",
"=",
"int",
"(",
"growth_rate",
")",
",",
"kernel",
"=",
"(",
"3",
",",
"3",
")",
",",
"stride",
"=",
"(",
"1",
",",
"1",
")",
",",
"pad",
"=",
"(",
"1",
",",
"1",
")",
",",
"no_bias",
"=",
"True",
",",
"workspace",
"=",
"workspace",
",",
"name",
"=",
"name",
"+",
"'_conv1'",
")",
"if",
"drop_out",
">",
"0",
":",
"conv1",
"=",
"mx",
".",
"symbol",
".",
"Dropout",
"(",
"data",
"=",
"conv1",
",",
"p",
"=",
"drop_out",
",",
"name",
"=",
"name",
"+",
"'_dp1'",
")",
"#return mx.symbol.Concat(data, conv1, name=name + '_concat0')",
"return",
"conv1"
] | https://github.com/zzzkk2009/casia-surf-2019-codes/blob/b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b/symbols/densenet.py#L7-L51 |
||
zzzkk2009/casia-surf-2019-codes | b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b | symbols/densenet.py | python | DenseBlock | (units_num, data, growth_rate, name, bottle_neck=True, drop_out=0.0, bn_mom=0.9, workspace=512) | return data | Return DenseBlock Unit symbol for building DenseNet
Parameters
----------
units_num : int
the number of BasicBlock in each DenseBlock
data : str
Input data
growth_rate : int
Number of output channels
drop_out : float
Probability of an element to be zeroed. Default = 0.2
workspace : int
Workspace used in convolution operator | Return DenseBlock Unit symbol for building DenseNet
Parameters
----------
units_num : int
the number of BasicBlock in each DenseBlock
data : str
Input data
growth_rate : int
Number of output channels
drop_out : float
Probability of an element to be zeroed. Default = 0.2
workspace : int
Workspace used in convolution operator | [
"Return",
"DenseBlock",
"Unit",
"symbol",
"for",
"building",
"DenseNet",
"Parameters",
"----------",
"units_num",
":",
"int",
"the",
"number",
"of",
"BasicBlock",
"in",
"each",
"DenseBlock",
"data",
":",
"str",
"Input",
"data",
"growth_rate",
":",
"int",
"Number",
"of",
"output",
"channels",
"drop_out",
":",
"float",
"Probability",
"of",
"an",
"element",
"to",
"be",
"zeroed",
".",
"Default",
"=",
"0",
".",
"2",
"workspace",
":",
"int",
"Workspace",
"used",
"in",
"convolution",
"operator"
] | def DenseBlock(units_num, data, growth_rate, name, bottle_neck=True, drop_out=0.0, bn_mom=0.9, workspace=512):
"""Return DenseBlock Unit symbol for building DenseNet
Parameters
----------
units_num : int
the number of BasicBlock in each DenseBlock
data : str
Input data
growth_rate : int
Number of output channels
drop_out : float
Probability of an element to be zeroed. Default = 0.2
workspace : int
Workspace used in convolution operator
"""
# import pdb
# pdb.set_trace()
for i in range(units_num):
Block = BasicBlock(data, growth_rate=growth_rate, stride=(1,1), name=name + '_unit%d' % (i+1),
bottle_neck=bottle_neck, drop_out=drop_out,
bn_mom=bn_mom, workspace=workspace)
data = mx.symbol.Concat(data, Block, name=name + '_concat%d' % (i+1))
return data | [
"def",
"DenseBlock",
"(",
"units_num",
",",
"data",
",",
"growth_rate",
",",
"name",
",",
"bottle_neck",
"=",
"True",
",",
"drop_out",
"=",
"0.0",
",",
"bn_mom",
"=",
"0.9",
",",
"workspace",
"=",
"512",
")",
":",
"# import pdb",
"# pdb.set_trace()",
"for",
"i",
"in",
"range",
"(",
"units_num",
")",
":",
"Block",
"=",
"BasicBlock",
"(",
"data",
",",
"growth_rate",
"=",
"growth_rate",
",",
"stride",
"=",
"(",
"1",
",",
"1",
")",
",",
"name",
"=",
"name",
"+",
"'_unit%d'",
"%",
"(",
"i",
"+",
"1",
")",
",",
"bottle_neck",
"=",
"bottle_neck",
",",
"drop_out",
"=",
"drop_out",
",",
"bn_mom",
"=",
"bn_mom",
",",
"workspace",
"=",
"workspace",
")",
"data",
"=",
"mx",
".",
"symbol",
".",
"Concat",
"(",
"data",
",",
"Block",
",",
"name",
"=",
"name",
"+",
"'_concat%d'",
"%",
"(",
"i",
"+",
"1",
")",
")",
"return",
"data"
] | https://github.com/zzzkk2009/casia-surf-2019-codes/blob/b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b/symbols/densenet.py#L53-L76 |
|
zzzkk2009/casia-surf-2019-codes | b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b | symbols/densenet.py | python | TransitionBlock | (num_stage, data, num_filter, stride, name, drop_out=0.0, bn_mom=0.9, workspace=512) | return mx.symbol.Pooling(conv1, global_pool=False, kernel=(2,2), stride=(2,2), pool_type='avg', name=name + '_pool%d' % (num_stage+1)) | Return TransitionBlock Unit symbol for building DenseNet
Parameters
----------
num_stage : int
Number of stage
data : str
Input data
num : int
Number of output channels
stride : tupe
Stride used in convolution
name : str
Base name of the operators
drop_out : float
Probability of an element to be zeroed. Default = 0.2
workspace : int
Workspace used in convolution operator | Return TransitionBlock Unit symbol for building DenseNet
Parameters
----------
num_stage : int
Number of stage
data : str
Input data
num : int
Number of output channels
stride : tupe
Stride used in convolution
name : str
Base name of the operators
drop_out : float
Probability of an element to be zeroed. Default = 0.2
workspace : int
Workspace used in convolution operator | [
"Return",
"TransitionBlock",
"Unit",
"symbol",
"for",
"building",
"DenseNet",
"Parameters",
"----------",
"num_stage",
":",
"int",
"Number",
"of",
"stage",
"data",
":",
"str",
"Input",
"data",
"num",
":",
"int",
"Number",
"of",
"output",
"channels",
"stride",
":",
"tupe",
"Stride",
"used",
"in",
"convolution",
"name",
":",
"str",
"Base",
"name",
"of",
"the",
"operators",
"drop_out",
":",
"float",
"Probability",
"of",
"an",
"element",
"to",
"be",
"zeroed",
".",
"Default",
"=",
"0",
".",
"2",
"workspace",
":",
"int",
"Workspace",
"used",
"in",
"convolution",
"operator"
] | def TransitionBlock(num_stage, data, num_filter, stride, name, drop_out=0.0, bn_mom=0.9, workspace=512):
"""Return TransitionBlock Unit symbol for building DenseNet
Parameters
----------
num_stage : int
Number of stage
data : str
Input data
num : int
Number of output channels
stride : tupe
Stride used in convolution
name : str
Base name of the operators
drop_out : float
Probability of an element to be zeroed. Default = 0.2
workspace : int
Workspace used in convolution operator
"""
bn1 = mx.sym.BatchNorm(data=data, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn1')
act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1')
conv1 = mx.sym.Convolution(data=act1, num_filter=num_filter,
kernel=(1,1), stride=stride, pad=(0,0), no_bias=True,
workspace=workspace, name=name + '_conv1')
if drop_out > 0:
conv1 = mx.symbol.Dropout(data=conv1, p=drop_out, name=name + '_dp1')
return mx.symbol.Pooling(conv1, global_pool=False, kernel=(2,2), stride=(2,2), pool_type='avg', name=name + '_pool%d' % (num_stage+1)) | [
"def",
"TransitionBlock",
"(",
"num_stage",
",",
"data",
",",
"num_filter",
",",
"stride",
",",
"name",
",",
"drop_out",
"=",
"0.0",
",",
"bn_mom",
"=",
"0.9",
",",
"workspace",
"=",
"512",
")",
":",
"bn1",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"data",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"name",
"+",
"'_bn1'",
")",
"act1",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"bn1",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"name",
"+",
"'_relu1'",
")",
"conv1",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"act1",
",",
"num_filter",
"=",
"num_filter",
",",
"kernel",
"=",
"(",
"1",
",",
"1",
")",
",",
"stride",
"=",
"stride",
",",
"pad",
"=",
"(",
"0",
",",
"0",
")",
",",
"no_bias",
"=",
"True",
",",
"workspace",
"=",
"workspace",
",",
"name",
"=",
"name",
"+",
"'_conv1'",
")",
"if",
"drop_out",
">",
"0",
":",
"conv1",
"=",
"mx",
".",
"symbol",
".",
"Dropout",
"(",
"data",
"=",
"conv1",
",",
"p",
"=",
"drop_out",
",",
"name",
"=",
"name",
"+",
"'_dp1'",
")",
"return",
"mx",
".",
"symbol",
".",
"Pooling",
"(",
"conv1",
",",
"global_pool",
"=",
"False",
",",
"kernel",
"=",
"(",
"2",
",",
"2",
")",
",",
"stride",
"=",
"(",
"2",
",",
"2",
")",
",",
"pool_type",
"=",
"'avg'",
",",
"name",
"=",
"name",
"+",
"'_pool%d'",
"%",
"(",
"num_stage",
"+",
"1",
")",
")"
] | https://github.com/zzzkk2009/casia-surf-2019-codes/blob/b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b/symbols/densenet.py#L78-L104 |
|
zzzkk2009/casia-surf-2019-codes | b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b | symbols/densenet.py | python | DenseNet | (units, num_stage, growth_rate, num_class, data_type, reduction=0.5, drop_out=0., bottle_neck=True, bn_mom=0.9, workspace=512) | return mx.symbol.SoftmaxOutput(data=fc1, name='softmax') | Return DenseNet symbol of imagenet
Parameters
----------
units : list
Number of units in each stage
num_stage : int
Number of stage
growth_rate : int
Number of output channels
num_class : int
Ouput size of symbol
data_type : str
the type of dataset
reduction : float
Compression ratio. Default = 0.5
drop_out : float
Probability of an element to be zeroed. Default = 0.2
workspace : int
Workspace used in convolution operator | Return DenseNet symbol of imagenet
Parameters
----------
units : list
Number of units in each stage
num_stage : int
Number of stage
growth_rate : int
Number of output channels
num_class : int
Ouput size of symbol
data_type : str
the type of dataset
reduction : float
Compression ratio. Default = 0.5
drop_out : float
Probability of an element to be zeroed. Default = 0.2
workspace : int
Workspace used in convolution operator | [
"Return",
"DenseNet",
"symbol",
"of",
"imagenet",
"Parameters",
"----------",
"units",
":",
"list",
"Number",
"of",
"units",
"in",
"each",
"stage",
"num_stage",
":",
"int",
"Number",
"of",
"stage",
"growth_rate",
":",
"int",
"Number",
"of",
"output",
"channels",
"num_class",
":",
"int",
"Ouput",
"size",
"of",
"symbol",
"data_type",
":",
"str",
"the",
"type",
"of",
"dataset",
"reduction",
":",
"float",
"Compression",
"ratio",
".",
"Default",
"=",
"0",
".",
"5",
"drop_out",
":",
"float",
"Probability",
"of",
"an",
"element",
"to",
"be",
"zeroed",
".",
"Default",
"=",
"0",
".",
"2",
"workspace",
":",
"int",
"Workspace",
"used",
"in",
"convolution",
"operator"
] | def DenseNet(units, num_stage, growth_rate, num_class, data_type, reduction=0.5, drop_out=0., bottle_neck=True, bn_mom=0.9, workspace=512):
"""Return DenseNet symbol of imagenet
Parameters
----------
units : list
Number of units in each stage
num_stage : int
Number of stage
growth_rate : int
Number of output channels
num_class : int
Ouput size of symbol
data_type : str
the type of dataset
reduction : float
Compression ratio. Default = 0.5
drop_out : float
Probability of an element to be zeroed. Default = 0.2
workspace : int
Workspace used in convolution operator
"""
num_unit = len(units)
assert(num_unit == num_stage)
init_channels = 2 * growth_rate
n_channels = init_channels
data = mx.sym.Variable(name='data')
data = mx.sym.BatchNorm(data=data, fix_gamma=True, eps=2e-5, momentum=bn_mom, name='bn_data')
if data_type == 'imagenet':
body = mx.sym.Convolution(data=data, num_filter=growth_rate*2, kernel=(7, 7), stride=(2,2), pad=(3, 3),
no_bias=True, name="conv0", workspace=workspace)
body = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, momentum=bn_mom, name='bn0')
body = mx.sym.Activation(data=body, act_type='relu', name='relu0')
body = mx.symbol.Pooling(data=body, kernel=(3, 3), stride=(2,2), pad=(1,1), pool_type='max')
elif data_type == 'casia-surf':
body = mx.sym.Convolution(data=data, num_filter=growth_rate*2, kernel=(7, 7), stride=(2,2), pad=(3, 3),
no_bias=True, name="conv0", workspace=workspace)
body = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, momentum=bn_mom, name='bn0')
body = mx.sym.Activation(data=body, act_type='relu', name='relu0')
# body = mx.symbol.Pooling(data=body, kernel=(3, 3), stride=(2,2), pad=(1,1), pool_type='max')
elif data_type == 'vggface':
body = mx.sym.Convolution(data=data, num_filter=growth_rate*2, kernel=(7, 7), stride=(2,2), pad=(3, 3),
no_bias=True, name="conv0", workspace=workspace)
body = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, momentum=bn_mom, name='bn0')
body = mx.sym.Activation(data=body, act_type='relu', name='relu0')
body = mx.symbol.Pooling(data=body, kernel=(3, 3), stride=(2,2), pad=(1,1), pool_type='max')
elif data_type == 'msface':
body = mx.sym.Convolution(data=data, num_filter=growth_rate*2, kernel=(7, 7), stride=(2,2), pad=(3, 3),
no_bias=True, name="conv0", workspace=workspace)
body = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, momentum=bn_mom, name='bn0')
body = mx.sym.Activation(data=body, act_type='relu', name='relu0')
body = mx.symbol.Pooling(data=body, kernel=(3, 3), stride=(2,2), pad=(1,1), pool_type='max')
else:
raise ValueError("do not support {} yet".format(data_type))
for i in range(num_stage-1):
body = DenseBlock(units[i], body, growth_rate=growth_rate, name='DBstage%d' % (i + 1), bottle_neck=bottle_neck, drop_out=drop_out, bn_mom=bn_mom, workspace=workspace)
n_channels += units[i]*growth_rate
n_channels = int(math.floor(n_channels*reduction))
body = TransitionBlock(i, body, n_channels, stride=(1,1), name='TBstage%d' % (i + 1), drop_out=drop_out, bn_mom=bn_mom, workspace=workspace)
body = DenseBlock(units[num_stage-1], body, growth_rate=growth_rate, name='DBstage%d' % (num_stage), bottle_neck=bottle_neck, drop_out=drop_out, bn_mom=bn_mom, workspace=workspace)
bn1 = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, momentum=bn_mom, name='bn1')
relu1 = mx.sym.Activation(data=bn1, act_type='relu', name='relu1')
pool1 = mx.symbol.Pooling(data=relu1, global_pool=True, kernel=(7, 7), pool_type='avg', name='pool1')
flat = mx.symbol.Flatten(data=pool1)
fc1 = mx.symbol.FullyConnected(data=flat, num_hidden=num_class, name='fc1')
return mx.symbol.SoftmaxOutput(data=fc1, name='softmax') | [
"def",
"DenseNet",
"(",
"units",
",",
"num_stage",
",",
"growth_rate",
",",
"num_class",
",",
"data_type",
",",
"reduction",
"=",
"0.5",
",",
"drop_out",
"=",
"0.",
",",
"bottle_neck",
"=",
"True",
",",
"bn_mom",
"=",
"0.9",
",",
"workspace",
"=",
"512",
")",
":",
"num_unit",
"=",
"len",
"(",
"units",
")",
"assert",
"(",
"num_unit",
"==",
"num_stage",
")",
"init_channels",
"=",
"2",
"*",
"growth_rate",
"n_channels",
"=",
"init_channels",
"data",
"=",
"mx",
".",
"sym",
".",
"Variable",
"(",
"name",
"=",
"'data'",
")",
"data",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"data",
",",
"fix_gamma",
"=",
"True",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"'bn_data'",
")",
"if",
"data_type",
"==",
"'imagenet'",
":",
"body",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"data",
",",
"num_filter",
"=",
"growth_rate",
"*",
"2",
",",
"kernel",
"=",
"(",
"7",
",",
"7",
")",
",",
"stride",
"=",
"(",
"2",
",",
"2",
")",
",",
"pad",
"=",
"(",
"3",
",",
"3",
")",
",",
"no_bias",
"=",
"True",
",",
"name",
"=",
"\"conv0\"",
",",
"workspace",
"=",
"workspace",
")",
"body",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"body",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"'bn0'",
")",
"body",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"body",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"'relu0'",
")",
"body",
"=",
"mx",
".",
"symbol",
".",
"Pooling",
"(",
"data",
"=",
"body",
",",
"kernel",
"=",
"(",
"3",
",",
"3",
")",
",",
"stride",
"=",
"(",
"2",
",",
"2",
")",
",",
"pad",
"=",
"(",
"1",
",",
"1",
")",
",",
"pool_type",
"=",
"'max'",
")",
"elif",
"data_type",
"==",
"'casia-surf'",
":",
"body",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"data",
",",
"num_filter",
"=",
"growth_rate",
"*",
"2",
",",
"kernel",
"=",
"(",
"7",
",",
"7",
")",
",",
"stride",
"=",
"(",
"2",
",",
"2",
")",
",",
"pad",
"=",
"(",
"3",
",",
"3",
")",
",",
"no_bias",
"=",
"True",
",",
"name",
"=",
"\"conv0\"",
",",
"workspace",
"=",
"workspace",
")",
"body",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"body",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"'bn0'",
")",
"body",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"body",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"'relu0'",
")",
"# body = mx.symbol.Pooling(data=body, kernel=(3, 3), stride=(2,2), pad=(1,1), pool_type='max')",
"elif",
"data_type",
"==",
"'vggface'",
":",
"body",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"data",
",",
"num_filter",
"=",
"growth_rate",
"*",
"2",
",",
"kernel",
"=",
"(",
"7",
",",
"7",
")",
",",
"stride",
"=",
"(",
"2",
",",
"2",
")",
",",
"pad",
"=",
"(",
"3",
",",
"3",
")",
",",
"no_bias",
"=",
"True",
",",
"name",
"=",
"\"conv0\"",
",",
"workspace",
"=",
"workspace",
")",
"body",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"body",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"'bn0'",
")",
"body",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"body",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"'relu0'",
")",
"body",
"=",
"mx",
".",
"symbol",
".",
"Pooling",
"(",
"data",
"=",
"body",
",",
"kernel",
"=",
"(",
"3",
",",
"3",
")",
",",
"stride",
"=",
"(",
"2",
",",
"2",
")",
",",
"pad",
"=",
"(",
"1",
",",
"1",
")",
",",
"pool_type",
"=",
"'max'",
")",
"elif",
"data_type",
"==",
"'msface'",
":",
"body",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"data",
",",
"num_filter",
"=",
"growth_rate",
"*",
"2",
",",
"kernel",
"=",
"(",
"7",
",",
"7",
")",
",",
"stride",
"=",
"(",
"2",
",",
"2",
")",
",",
"pad",
"=",
"(",
"3",
",",
"3",
")",
",",
"no_bias",
"=",
"True",
",",
"name",
"=",
"\"conv0\"",
",",
"workspace",
"=",
"workspace",
")",
"body",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"body",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"'bn0'",
")",
"body",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"body",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"'relu0'",
")",
"body",
"=",
"mx",
".",
"symbol",
".",
"Pooling",
"(",
"data",
"=",
"body",
",",
"kernel",
"=",
"(",
"3",
",",
"3",
")",
",",
"stride",
"=",
"(",
"2",
",",
"2",
")",
",",
"pad",
"=",
"(",
"1",
",",
"1",
")",
",",
"pool_type",
"=",
"'max'",
")",
"else",
":",
"raise",
"ValueError",
"(",
"\"do not support {} yet\"",
".",
"format",
"(",
"data_type",
")",
")",
"for",
"i",
"in",
"range",
"(",
"num_stage",
"-",
"1",
")",
":",
"body",
"=",
"DenseBlock",
"(",
"units",
"[",
"i",
"]",
",",
"body",
",",
"growth_rate",
"=",
"growth_rate",
",",
"name",
"=",
"'DBstage%d'",
"%",
"(",
"i",
"+",
"1",
")",
",",
"bottle_neck",
"=",
"bottle_neck",
",",
"drop_out",
"=",
"drop_out",
",",
"bn_mom",
"=",
"bn_mom",
",",
"workspace",
"=",
"workspace",
")",
"n_channels",
"+=",
"units",
"[",
"i",
"]",
"*",
"growth_rate",
"n_channels",
"=",
"int",
"(",
"math",
".",
"floor",
"(",
"n_channels",
"*",
"reduction",
")",
")",
"body",
"=",
"TransitionBlock",
"(",
"i",
",",
"body",
",",
"n_channels",
",",
"stride",
"=",
"(",
"1",
",",
"1",
")",
",",
"name",
"=",
"'TBstage%d'",
"%",
"(",
"i",
"+",
"1",
")",
",",
"drop_out",
"=",
"drop_out",
",",
"bn_mom",
"=",
"bn_mom",
",",
"workspace",
"=",
"workspace",
")",
"body",
"=",
"DenseBlock",
"(",
"units",
"[",
"num_stage",
"-",
"1",
"]",
",",
"body",
",",
"growth_rate",
"=",
"growth_rate",
",",
"name",
"=",
"'DBstage%d'",
"%",
"(",
"num_stage",
")",
",",
"bottle_neck",
"=",
"bottle_neck",
",",
"drop_out",
"=",
"drop_out",
",",
"bn_mom",
"=",
"bn_mom",
",",
"workspace",
"=",
"workspace",
")",
"bn1",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"body",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"'bn1'",
")",
"relu1",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"bn1",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"'relu1'",
")",
"pool1",
"=",
"mx",
".",
"symbol",
".",
"Pooling",
"(",
"data",
"=",
"relu1",
",",
"global_pool",
"=",
"True",
",",
"kernel",
"=",
"(",
"7",
",",
"7",
")",
",",
"pool_type",
"=",
"'avg'",
",",
"name",
"=",
"'pool1'",
")",
"flat",
"=",
"mx",
".",
"symbol",
".",
"Flatten",
"(",
"data",
"=",
"pool1",
")",
"fc1",
"=",
"mx",
".",
"symbol",
".",
"FullyConnected",
"(",
"data",
"=",
"flat",
",",
"num_hidden",
"=",
"num_class",
",",
"name",
"=",
"'fc1'",
")",
"return",
"mx",
".",
"symbol",
".",
"SoftmaxOutput",
"(",
"data",
"=",
"fc1",
",",
"name",
"=",
"'softmax'",
")"
] | https://github.com/zzzkk2009/casia-surf-2019-codes/blob/b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b/symbols/densenet.py#L106-L170 |
|
zzzkk2009/casia-surf-2019-codes | b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b | symbols/resnet.py | python | residual_unit | (data, num_filter, stride, dim_match, name, bottle_neck=True, bn_mom=0.9, workspace=256, memonger=False) | Return ResNet Unit symbol for building ResNet
Parameters
----------
data : str
Input data
num_filter : int
Number of output channels
bnf : int
Bottle neck channels factor with regard to num_filter
stride : tuple
Stride used in convolution
dim_match : Boolean
True means channel number between input and output is the same, otherwise means differ
name : str
Base name of the operators
workspace : int
Workspace used in convolution operator | Return ResNet Unit symbol for building ResNet
Parameters
----------
data : str
Input data
num_filter : int
Number of output channels
bnf : int
Bottle neck channels factor with regard to num_filter
stride : tuple
Stride used in convolution
dim_match : Boolean
True means channel number between input and output is the same, otherwise means differ
name : str
Base name of the operators
workspace : int
Workspace used in convolution operator | [
"Return",
"ResNet",
"Unit",
"symbol",
"for",
"building",
"ResNet",
"Parameters",
"----------",
"data",
":",
"str",
"Input",
"data",
"num_filter",
":",
"int",
"Number",
"of",
"output",
"channels",
"bnf",
":",
"int",
"Bottle",
"neck",
"channels",
"factor",
"with",
"regard",
"to",
"num_filter",
"stride",
":",
"tuple",
"Stride",
"used",
"in",
"convolution",
"dim_match",
":",
"Boolean",
"True",
"means",
"channel",
"number",
"between",
"input",
"and",
"output",
"is",
"the",
"same",
"otherwise",
"means",
"differ",
"name",
":",
"str",
"Base",
"name",
"of",
"the",
"operators",
"workspace",
":",
"int",
"Workspace",
"used",
"in",
"convolution",
"operator"
] | def residual_unit(data, num_filter, stride, dim_match, name, bottle_neck=True, bn_mom=0.9, workspace=256, memonger=False):
"""Return ResNet Unit symbol for building ResNet
Parameters
----------
data : str
Input data
num_filter : int
Number of output channels
bnf : int
Bottle neck channels factor with regard to num_filter
stride : tuple
Stride used in convolution
dim_match : Boolean
True means channel number between input and output is the same, otherwise means differ
name : str
Base name of the operators
workspace : int
Workspace used in convolution operator
"""
if bottle_neck:
# the same as https://github.com/facebook/fb.resnet.torch#notes, a bit difference with origin paper
bn1 = mx.sym.BatchNorm(data=data, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn1')
act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1')
conv1 = mx.sym.Convolution(data=act1, num_filter=int(num_filter*0.25), kernel=(1,1), stride=(1,1), pad=(0,0),
no_bias=True, workspace=workspace, name=name + '_conv1')
bn2 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn2')
act2 = mx.sym.Activation(data=bn2, act_type='relu', name=name + '_relu2')
conv2 = mx.sym.Convolution(data=act2, num_filter=int(num_filter*0.25), kernel=(3,3), stride=stride, pad=(1,1),
no_bias=True, workspace=workspace, name=name + '_conv2')
bn3 = mx.sym.BatchNorm(data=conv2, fix_gamma=False, eps=2e-5, momentum=bn_mom, name=name + '_bn3')
act3 = mx.sym.Activation(data=bn3, act_type='relu', name=name + '_relu3')
conv3 = mx.sym.Convolution(data=act3, num_filter=num_filter, kernel=(1,1), stride=(1,1), pad=(0,0), no_bias=True,
workspace=workspace, name=name + '_conv3')
if dim_match:
shortcut = data
else:
shortcut = mx.sym.Convolution(data=act1, num_filter=num_filter, kernel=(1,1), stride=stride, no_bias=True,
workspace=workspace, name=name+'_sc')
if memonger:
shortcut._set_attr(mirror_stage='True')
return conv3 + shortcut
else:
bn1 = mx.sym.BatchNorm(data=data, fix_gamma=False, momentum=bn_mom, eps=2e-5, name=name + '_bn1')
act1 = mx.sym.Activation(data=bn1, act_type='relu', name=name + '_relu1')
conv1 = mx.sym.Convolution(data=act1, num_filter=num_filter, kernel=(3,3), stride=stride, pad=(1,1),
no_bias=True, workspace=workspace, name=name + '_conv1')
bn2 = mx.sym.BatchNorm(data=conv1, fix_gamma=False, momentum=bn_mom, eps=2e-5, name=name + '_bn2')
act2 = mx.sym.Activation(data=bn2, act_type='relu', name=name + '_relu2')
conv2 = mx.sym.Convolution(data=act2, num_filter=num_filter, kernel=(3,3), stride=(1,1), pad=(1,1),
no_bias=True, workspace=workspace, name=name + '_conv2')
if dim_match:
shortcut = data
else:
shortcut = mx.sym.Convolution(data=act1, num_filter=num_filter, kernel=(1,1), stride=stride, no_bias=True,
workspace=workspace, name=name+'_sc')
if memonger:
shortcut._set_attr(mirror_stage='True')
return conv2 + shortcut | [
"def",
"residual_unit",
"(",
"data",
",",
"num_filter",
",",
"stride",
",",
"dim_match",
",",
"name",
",",
"bottle_neck",
"=",
"True",
",",
"bn_mom",
"=",
"0.9",
",",
"workspace",
"=",
"256",
",",
"memonger",
"=",
"False",
")",
":",
"if",
"bottle_neck",
":",
"# the same as https://github.com/facebook/fb.resnet.torch#notes, a bit difference with origin paper",
"bn1",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"data",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"name",
"+",
"'_bn1'",
")",
"act1",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"bn1",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"name",
"+",
"'_relu1'",
")",
"conv1",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"act1",
",",
"num_filter",
"=",
"int",
"(",
"num_filter",
"*",
"0.25",
")",
",",
"kernel",
"=",
"(",
"1",
",",
"1",
")",
",",
"stride",
"=",
"(",
"1",
",",
"1",
")",
",",
"pad",
"=",
"(",
"0",
",",
"0",
")",
",",
"no_bias",
"=",
"True",
",",
"workspace",
"=",
"workspace",
",",
"name",
"=",
"name",
"+",
"'_conv1'",
")",
"bn2",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"conv1",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"name",
"+",
"'_bn2'",
")",
"act2",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"bn2",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"name",
"+",
"'_relu2'",
")",
"conv2",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"act2",
",",
"num_filter",
"=",
"int",
"(",
"num_filter",
"*",
"0.25",
")",
",",
"kernel",
"=",
"(",
"3",
",",
"3",
")",
",",
"stride",
"=",
"stride",
",",
"pad",
"=",
"(",
"1",
",",
"1",
")",
",",
"no_bias",
"=",
"True",
",",
"workspace",
"=",
"workspace",
",",
"name",
"=",
"name",
"+",
"'_conv2'",
")",
"bn3",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"conv2",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"name",
"+",
"'_bn3'",
")",
"act3",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"bn3",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"name",
"+",
"'_relu3'",
")",
"conv3",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"act3",
",",
"num_filter",
"=",
"num_filter",
",",
"kernel",
"=",
"(",
"1",
",",
"1",
")",
",",
"stride",
"=",
"(",
"1",
",",
"1",
")",
",",
"pad",
"=",
"(",
"0",
",",
"0",
")",
",",
"no_bias",
"=",
"True",
",",
"workspace",
"=",
"workspace",
",",
"name",
"=",
"name",
"+",
"'_conv3'",
")",
"if",
"dim_match",
":",
"shortcut",
"=",
"data",
"else",
":",
"shortcut",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"act1",
",",
"num_filter",
"=",
"num_filter",
",",
"kernel",
"=",
"(",
"1",
",",
"1",
")",
",",
"stride",
"=",
"stride",
",",
"no_bias",
"=",
"True",
",",
"workspace",
"=",
"workspace",
",",
"name",
"=",
"name",
"+",
"'_sc'",
")",
"if",
"memonger",
":",
"shortcut",
".",
"_set_attr",
"(",
"mirror_stage",
"=",
"'True'",
")",
"return",
"conv3",
"+",
"shortcut",
"else",
":",
"bn1",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"data",
",",
"fix_gamma",
"=",
"False",
",",
"momentum",
"=",
"bn_mom",
",",
"eps",
"=",
"2e-5",
",",
"name",
"=",
"name",
"+",
"'_bn1'",
")",
"act1",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"bn1",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"name",
"+",
"'_relu1'",
")",
"conv1",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"act1",
",",
"num_filter",
"=",
"num_filter",
",",
"kernel",
"=",
"(",
"3",
",",
"3",
")",
",",
"stride",
"=",
"stride",
",",
"pad",
"=",
"(",
"1",
",",
"1",
")",
",",
"no_bias",
"=",
"True",
",",
"workspace",
"=",
"workspace",
",",
"name",
"=",
"name",
"+",
"'_conv1'",
")",
"bn2",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"conv1",
",",
"fix_gamma",
"=",
"False",
",",
"momentum",
"=",
"bn_mom",
",",
"eps",
"=",
"2e-5",
",",
"name",
"=",
"name",
"+",
"'_bn2'",
")",
"act2",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"bn2",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"name",
"+",
"'_relu2'",
")",
"conv2",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"act2",
",",
"num_filter",
"=",
"num_filter",
",",
"kernel",
"=",
"(",
"3",
",",
"3",
")",
",",
"stride",
"=",
"(",
"1",
",",
"1",
")",
",",
"pad",
"=",
"(",
"1",
",",
"1",
")",
",",
"no_bias",
"=",
"True",
",",
"workspace",
"=",
"workspace",
",",
"name",
"=",
"name",
"+",
"'_conv2'",
")",
"if",
"dim_match",
":",
"shortcut",
"=",
"data",
"else",
":",
"shortcut",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"act1",
",",
"num_filter",
"=",
"num_filter",
",",
"kernel",
"=",
"(",
"1",
",",
"1",
")",
",",
"stride",
"=",
"stride",
",",
"no_bias",
"=",
"True",
",",
"workspace",
"=",
"workspace",
",",
"name",
"=",
"name",
"+",
"'_sc'",
")",
"if",
"memonger",
":",
"shortcut",
".",
"_set_attr",
"(",
"mirror_stage",
"=",
"'True'",
")",
"return",
"conv2",
"+",
"shortcut"
] | https://github.com/zzzkk2009/casia-surf-2019-codes/blob/b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b/symbols/resnet.py#L27-L84 |
||
zzzkk2009/casia-surf-2019-codes | b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b | symbols/resnet.py | python | resnet | (units, num_stages, filter_list, num_classes, image_shape, bottle_neck=True, bn_mom=0.9, workspace=256, dtype='float32', memonger=False) | return mx.sym.SoftmaxOutput(data=fc1, name='softmax') | Return ResNet symbol of
Parameters
----------
units : list
Number of units in each stage
num_stages : int
Number of stage
filter_list : list
Channel size of each stage
num_classes : int
Ouput size of symbol
dataset : str
Dataset type, only cifar10 and imagenet supports
workspace : int
Workspace used in convolution operator
dtype : str
Precision (float32 or float16) | Return ResNet symbol of
Parameters
----------
units : list
Number of units in each stage
num_stages : int
Number of stage
filter_list : list
Channel size of each stage
num_classes : int
Ouput size of symbol
dataset : str
Dataset type, only cifar10 and imagenet supports
workspace : int
Workspace used in convolution operator
dtype : str
Precision (float32 or float16) | [
"Return",
"ResNet",
"symbol",
"of",
"Parameters",
"----------",
"units",
":",
"list",
"Number",
"of",
"units",
"in",
"each",
"stage",
"num_stages",
":",
"int",
"Number",
"of",
"stage",
"filter_list",
":",
"list",
"Channel",
"size",
"of",
"each",
"stage",
"num_classes",
":",
"int",
"Ouput",
"size",
"of",
"symbol",
"dataset",
":",
"str",
"Dataset",
"type",
"only",
"cifar10",
"and",
"imagenet",
"supports",
"workspace",
":",
"int",
"Workspace",
"used",
"in",
"convolution",
"operator",
"dtype",
":",
"str",
"Precision",
"(",
"float32",
"or",
"float16",
")"
] | def resnet(units, num_stages, filter_list, num_classes, image_shape, bottle_neck=True, bn_mom=0.9, workspace=256, dtype='float32', memonger=False):
"""Return ResNet symbol of
Parameters
----------
units : list
Number of units in each stage
num_stages : int
Number of stage
filter_list : list
Channel size of each stage
num_classes : int
Ouput size of symbol
dataset : str
Dataset type, only cifar10 and imagenet supports
workspace : int
Workspace used in convolution operator
dtype : str
Precision (float32 or float16)
"""
num_unit = len(units)
assert(num_unit == num_stages)
data = mx.sym.Variable(name='data')
if dtype == 'float32':
data = mx.sym.identity(data=data, name='id')
else:
if dtype == 'float16':
data = mx.sym.Cast(data=data, dtype=np.float16)
data = mx.sym.BatchNorm(data=data, fix_gamma=True, eps=2e-5, momentum=bn_mom, name='bn_data')
(nchannel, height, width) = image_shape
if height <= 32: # such as cifar10
body = mx.sym.Convolution(data=data, num_filter=filter_list[0], kernel=(3, 3), stride=(1,1), pad=(1, 1),
no_bias=True, name="conv0", workspace=workspace)
else: # often expected to be 224 such as imagenet
body = mx.sym.Convolution(data=data, num_filter=filter_list[0], kernel=(7, 7), stride=(2,2), pad=(3, 3),
no_bias=True, name="conv0", workspace=workspace)
body = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, momentum=bn_mom, name='bn0')
body = mx.sym.Activation(data=body, act_type='relu', name='relu0')
body = mx.sym.Pooling(data=body, kernel=(3, 3), stride=(2,2), pad=(1,1), pool_type='max')
for i in range(num_stages):
body = residual_unit(body, filter_list[i+1], (1 if i==0 else 2, 1 if i==0 else 2), False,
name='stage%d_unit%d' % (i + 1, 1), bottle_neck=bottle_neck, workspace=workspace,
memonger=memonger)
for j in range(units[i]-1):
body = residual_unit(body, filter_list[i+1], (1,1), True, name='stage%d_unit%d' % (i + 1, j + 2),
bottle_neck=bottle_neck, workspace=workspace, memonger=memonger)
bn1 = mx.sym.BatchNorm(data=body, fix_gamma=False, eps=2e-5, momentum=bn_mom, name='bn1')
relu1 = mx.sym.Activation(data=bn1, act_type='relu', name='relu1')
# Although kernel is not used here when global_pool=True, we should put one
pool1 = mx.sym.Pooling(data=relu1, global_pool=True, kernel=(7, 7), pool_type='avg', name='pool1')
flat = mx.sym.Flatten(data=pool1)
fc1 = mx.sym.FullyConnected(data=flat, num_hidden=num_classes, name='fc1')
if dtype == 'float16':
fc1 = mx.sym.Cast(data=fc1, dtype=np.float32)
return mx.sym.SoftmaxOutput(data=fc1, name='softmax') | [
"def",
"resnet",
"(",
"units",
",",
"num_stages",
",",
"filter_list",
",",
"num_classes",
",",
"image_shape",
",",
"bottle_neck",
"=",
"True",
",",
"bn_mom",
"=",
"0.9",
",",
"workspace",
"=",
"256",
",",
"dtype",
"=",
"'float32'",
",",
"memonger",
"=",
"False",
")",
":",
"num_unit",
"=",
"len",
"(",
"units",
")",
"assert",
"(",
"num_unit",
"==",
"num_stages",
")",
"data",
"=",
"mx",
".",
"sym",
".",
"Variable",
"(",
"name",
"=",
"'data'",
")",
"if",
"dtype",
"==",
"'float32'",
":",
"data",
"=",
"mx",
".",
"sym",
".",
"identity",
"(",
"data",
"=",
"data",
",",
"name",
"=",
"'id'",
")",
"else",
":",
"if",
"dtype",
"==",
"'float16'",
":",
"data",
"=",
"mx",
".",
"sym",
".",
"Cast",
"(",
"data",
"=",
"data",
",",
"dtype",
"=",
"np",
".",
"float16",
")",
"data",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"data",
",",
"fix_gamma",
"=",
"True",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"'bn_data'",
")",
"(",
"nchannel",
",",
"height",
",",
"width",
")",
"=",
"image_shape",
"if",
"height",
"<=",
"32",
":",
"# such as cifar10",
"body",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"data",
",",
"num_filter",
"=",
"filter_list",
"[",
"0",
"]",
",",
"kernel",
"=",
"(",
"3",
",",
"3",
")",
",",
"stride",
"=",
"(",
"1",
",",
"1",
")",
",",
"pad",
"=",
"(",
"1",
",",
"1",
")",
",",
"no_bias",
"=",
"True",
",",
"name",
"=",
"\"conv0\"",
",",
"workspace",
"=",
"workspace",
")",
"else",
":",
"# often expected to be 224 such as imagenet",
"body",
"=",
"mx",
".",
"sym",
".",
"Convolution",
"(",
"data",
"=",
"data",
",",
"num_filter",
"=",
"filter_list",
"[",
"0",
"]",
",",
"kernel",
"=",
"(",
"7",
",",
"7",
")",
",",
"stride",
"=",
"(",
"2",
",",
"2",
")",
",",
"pad",
"=",
"(",
"3",
",",
"3",
")",
",",
"no_bias",
"=",
"True",
",",
"name",
"=",
"\"conv0\"",
",",
"workspace",
"=",
"workspace",
")",
"body",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"body",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"'bn0'",
")",
"body",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"body",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"'relu0'",
")",
"body",
"=",
"mx",
".",
"sym",
".",
"Pooling",
"(",
"data",
"=",
"body",
",",
"kernel",
"=",
"(",
"3",
",",
"3",
")",
",",
"stride",
"=",
"(",
"2",
",",
"2",
")",
",",
"pad",
"=",
"(",
"1",
",",
"1",
")",
",",
"pool_type",
"=",
"'max'",
")",
"for",
"i",
"in",
"range",
"(",
"num_stages",
")",
":",
"body",
"=",
"residual_unit",
"(",
"body",
",",
"filter_list",
"[",
"i",
"+",
"1",
"]",
",",
"(",
"1",
"if",
"i",
"==",
"0",
"else",
"2",
",",
"1",
"if",
"i",
"==",
"0",
"else",
"2",
")",
",",
"False",
",",
"name",
"=",
"'stage%d_unit%d'",
"%",
"(",
"i",
"+",
"1",
",",
"1",
")",
",",
"bottle_neck",
"=",
"bottle_neck",
",",
"workspace",
"=",
"workspace",
",",
"memonger",
"=",
"memonger",
")",
"for",
"j",
"in",
"range",
"(",
"units",
"[",
"i",
"]",
"-",
"1",
")",
":",
"body",
"=",
"residual_unit",
"(",
"body",
",",
"filter_list",
"[",
"i",
"+",
"1",
"]",
",",
"(",
"1",
",",
"1",
")",
",",
"True",
",",
"name",
"=",
"'stage%d_unit%d'",
"%",
"(",
"i",
"+",
"1",
",",
"j",
"+",
"2",
")",
",",
"bottle_neck",
"=",
"bottle_neck",
",",
"workspace",
"=",
"workspace",
",",
"memonger",
"=",
"memonger",
")",
"bn1",
"=",
"mx",
".",
"sym",
".",
"BatchNorm",
"(",
"data",
"=",
"body",
",",
"fix_gamma",
"=",
"False",
",",
"eps",
"=",
"2e-5",
",",
"momentum",
"=",
"bn_mom",
",",
"name",
"=",
"'bn1'",
")",
"relu1",
"=",
"mx",
".",
"sym",
".",
"Activation",
"(",
"data",
"=",
"bn1",
",",
"act_type",
"=",
"'relu'",
",",
"name",
"=",
"'relu1'",
")",
"# Although kernel is not used here when global_pool=True, we should put one",
"pool1",
"=",
"mx",
".",
"sym",
".",
"Pooling",
"(",
"data",
"=",
"relu1",
",",
"global_pool",
"=",
"True",
",",
"kernel",
"=",
"(",
"7",
",",
"7",
")",
",",
"pool_type",
"=",
"'avg'",
",",
"name",
"=",
"'pool1'",
")",
"flat",
"=",
"mx",
".",
"sym",
".",
"Flatten",
"(",
"data",
"=",
"pool1",
")",
"fc1",
"=",
"mx",
".",
"sym",
".",
"FullyConnected",
"(",
"data",
"=",
"flat",
",",
"num_hidden",
"=",
"num_classes",
",",
"name",
"=",
"'fc1'",
")",
"if",
"dtype",
"==",
"'float16'",
":",
"fc1",
"=",
"mx",
".",
"sym",
".",
"Cast",
"(",
"data",
"=",
"fc1",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
"return",
"mx",
".",
"sym",
".",
"SoftmaxOutput",
"(",
"data",
"=",
"fc1",
",",
"name",
"=",
"'softmax'",
")"
] | https://github.com/zzzkk2009/casia-surf-2019-codes/blob/b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b/symbols/resnet.py#L86-L140 |
|
zzzkk2009/casia-surf-2019-codes | b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b | symbols/resnet.py | python | get_symbol | (num_classes, num_layers, image_shape, conv_workspace=256, dtype='float32', **kwargs) | return resnet(units = units,
num_stages = num_stages,
filter_list = filter_list,
num_classes = num_classes,
image_shape = image_shape,
bottle_neck = bottle_neck,
workspace = conv_workspace,
dtype = dtype) | Adapted from https://github.com/tornadomeet/ResNet/blob/master/train_resnet.py
Original author Wei Wu | Adapted from https://github.com/tornadomeet/ResNet/blob/master/train_resnet.py
Original author Wei Wu | [
"Adapted",
"from",
"https",
":",
"//",
"github",
".",
"com",
"/",
"tornadomeet",
"/",
"ResNet",
"/",
"blob",
"/",
"master",
"/",
"train_resnet",
".",
"py",
"Original",
"author",
"Wei",
"Wu"
] | def get_symbol(num_classes, num_layers, image_shape, conv_workspace=256, dtype='float32', **kwargs):
"""
Adapted from https://github.com/tornadomeet/ResNet/blob/master/train_resnet.py
Original author Wei Wu
"""
image_shape = [int(l) for l in image_shape.split(',')]
(nchannel, height, width) = image_shape
if height <= 28:
num_stages = 3
if (num_layers-2) % 9 == 0 and num_layers >= 164:
per_unit = [(num_layers-2)//9]
filter_list = [16, 64, 128, 256]
bottle_neck = True
elif (num_layers-2) % 6 == 0 and num_layers < 164:
per_unit = [(num_layers-2)//6]
filter_list = [16, 16, 32, 64]
bottle_neck = False
else:
raise ValueError("no experiments done on num_layers {}, you can do it yourself".format(num_layers))
units = per_unit * num_stages
else:
if num_layers >= 50:
filter_list = [64, 256, 512, 1024, 2048]
bottle_neck = True
else:
filter_list = [64, 64, 128, 256, 512]
bottle_neck = False
num_stages = 4
if num_layers == 18:
units = [2, 2, 2, 2]
elif num_layers == 34:
units = [3, 4, 6, 3]
elif num_layers == 50:
units = [3, 4, 6, 3]
elif num_layers == 101:
units = [3, 4, 23, 3]
elif num_layers == 152:
units = [3, 8, 36, 3]
elif num_layers == 200:
units = [3, 24, 36, 3]
elif num_layers == 269:
units = [3, 30, 48, 8]
else:
raise ValueError("no experiments done on num_layers {}, you can do it yourself".format(num_layers))
return resnet(units = units,
num_stages = num_stages,
filter_list = filter_list,
num_classes = num_classes,
image_shape = image_shape,
bottle_neck = bottle_neck,
workspace = conv_workspace,
dtype = dtype) | [
"def",
"get_symbol",
"(",
"num_classes",
",",
"num_layers",
",",
"image_shape",
",",
"conv_workspace",
"=",
"256",
",",
"dtype",
"=",
"'float32'",
",",
"*",
"*",
"kwargs",
")",
":",
"image_shape",
"=",
"[",
"int",
"(",
"l",
")",
"for",
"l",
"in",
"image_shape",
".",
"split",
"(",
"','",
")",
"]",
"(",
"nchannel",
",",
"height",
",",
"width",
")",
"=",
"image_shape",
"if",
"height",
"<=",
"28",
":",
"num_stages",
"=",
"3",
"if",
"(",
"num_layers",
"-",
"2",
")",
"%",
"9",
"==",
"0",
"and",
"num_layers",
">=",
"164",
":",
"per_unit",
"=",
"[",
"(",
"num_layers",
"-",
"2",
")",
"//",
"9",
"]",
"filter_list",
"=",
"[",
"16",
",",
"64",
",",
"128",
",",
"256",
"]",
"bottle_neck",
"=",
"True",
"elif",
"(",
"num_layers",
"-",
"2",
")",
"%",
"6",
"==",
"0",
"and",
"num_layers",
"<",
"164",
":",
"per_unit",
"=",
"[",
"(",
"num_layers",
"-",
"2",
")",
"//",
"6",
"]",
"filter_list",
"=",
"[",
"16",
",",
"16",
",",
"32",
",",
"64",
"]",
"bottle_neck",
"=",
"False",
"else",
":",
"raise",
"ValueError",
"(",
"\"no experiments done on num_layers {}, you can do it yourself\"",
".",
"format",
"(",
"num_layers",
")",
")",
"units",
"=",
"per_unit",
"*",
"num_stages",
"else",
":",
"if",
"num_layers",
">=",
"50",
":",
"filter_list",
"=",
"[",
"64",
",",
"256",
",",
"512",
",",
"1024",
",",
"2048",
"]",
"bottle_neck",
"=",
"True",
"else",
":",
"filter_list",
"=",
"[",
"64",
",",
"64",
",",
"128",
",",
"256",
",",
"512",
"]",
"bottle_neck",
"=",
"False",
"num_stages",
"=",
"4",
"if",
"num_layers",
"==",
"18",
":",
"units",
"=",
"[",
"2",
",",
"2",
",",
"2",
",",
"2",
"]",
"elif",
"num_layers",
"==",
"34",
":",
"units",
"=",
"[",
"3",
",",
"4",
",",
"6",
",",
"3",
"]",
"elif",
"num_layers",
"==",
"50",
":",
"units",
"=",
"[",
"3",
",",
"4",
",",
"6",
",",
"3",
"]",
"elif",
"num_layers",
"==",
"101",
":",
"units",
"=",
"[",
"3",
",",
"4",
",",
"23",
",",
"3",
"]",
"elif",
"num_layers",
"==",
"152",
":",
"units",
"=",
"[",
"3",
",",
"8",
",",
"36",
",",
"3",
"]",
"elif",
"num_layers",
"==",
"200",
":",
"units",
"=",
"[",
"3",
",",
"24",
",",
"36",
",",
"3",
"]",
"elif",
"num_layers",
"==",
"269",
":",
"units",
"=",
"[",
"3",
",",
"30",
",",
"48",
",",
"8",
"]",
"else",
":",
"raise",
"ValueError",
"(",
"\"no experiments done on num_layers {}, you can do it yourself\"",
".",
"format",
"(",
"num_layers",
")",
")",
"return",
"resnet",
"(",
"units",
"=",
"units",
",",
"num_stages",
"=",
"num_stages",
",",
"filter_list",
"=",
"filter_list",
",",
"num_classes",
"=",
"num_classes",
",",
"image_shape",
"=",
"image_shape",
",",
"bottle_neck",
"=",
"bottle_neck",
",",
"workspace",
"=",
"conv_workspace",
",",
"dtype",
"=",
"dtype",
")"
] | https://github.com/zzzkk2009/casia-surf-2019-codes/blob/b9dcccc0984d3c0ad9d70e1c0c453f13694ef14b/symbols/resnet.py#L142-L194 |