我有一个看起来像这样的json文件:
{
"model": "Sequential",
"layers": [
{
"L1": "Conv2D(filters = 64, kernel_size=(2,2), strides=(2,2), padding='same', data_format='channels_last', activation='relu', use_bias=True, kernel_initializer='zeros', bias_initializer='zeros', kernel_regularizer=regularizers.l1(0.), bias_regularizer=regularizers.l1(0.), activity_regularizer=regularizers.l1(0.), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.), input_shape=(224,224,3))",
"L2": "MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same', data_format='channels_last')",
"L3": "Conv2D(filters = 64, kernel_size=(2,2), strides=(2,2), padding='same', data_format='channels_last', activation='relu', use_bias=True, kernel_initializer='zeros', bias_initializer='zeros', kernel_regularizer=regularizers.l1(0.), bias_regularizer=regularizers.l1(0.), activity_regularizer=regularizers.l1(0.), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))",
"L4": "MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same', data_format='channels_last')",
"L5": "Conv2D(filters = 64, kernel_size=(2,2), strides=(2,2), padding='same', data_format='channels_last', activation='relu', use_bias=True, kernel_initializer='zeros', bias_initializer='zeros', kernel_regularizer=regularizers.l1(0.), bias_regularizer=regularizers.l1(0.), activity_regularizer=regularizers.l1(0.), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))",
"L6": "Conv2D(filters = 64, kernel_size=(2,2), strides=(2,2), padding='same', data_format='channels_last', activation='relu', use_bias=True, kernel_initializer='zeros', bias_initializer='zeros', kernel_regularizer=regularizers.l1(0.), bias_regularizer=regularizers.l1(0.), activity_regularizer=regularizers.l1(0.), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))",
"L7": "Conv2D(filters = 64, kernel_size=(2,2), strides=(2,2), padding='same', data_format='channels_last', activation='relu', use_bias=True, kernel_initializer='zeros', bias_initializer='zeros', kernel_regularizer=regularizers.l1(0.), bias_regularizer=regularizers.l1(0.), activity_regularizer=regularizers.l1(0.), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))",
"L8": "MaxPooling2D(pool_size=(2,2), strides=(2,2), padding='same', data_format='channels_last')",
"L9": "Flatten()",
"L10": "Dense(4096, activation='softmax', use_bias=True, kernel_initializer='zeros', bias_initializer='zeros', kernel_regularizer=regularizers.l1(0.), bias_regularizer=regularizers.l1(0.), activity_regularizer=regularizers.l1(0.), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))",
"L11": "Dropout(0.4)",
"L12": "Dense(2048, activation='softmax', use_bias=True, kernel_initializer='zeros', bias_initializer='zeros', kernel_regularizer=regularizers.l1(0.), bias_regularizer=regularizers.l1(0.), activity_regularizer=regularizers.l1(0.), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))",
"L13": "Dropout(0.4)",
"L14": "Dense(1000, activation='softmax', use_bias=True, kernel_initializer='zeros', bias_initializer='zeros', kernel_regularizer=regularizers.l1(0.), bias_regularizer=regularizers.l1(0.), activity_regularizer=regularizers.l1(0.), kernel_constraint=max_norm(2.), bias_constraint=max_norm(2.))",
"L15": "Dropout(0.4)"
}
]
}
我想获取有关json文件中存在哪一层的信息。例如,Conv2D,MaxPooling2D,Flatten()等。
此外,我想知道字符串的值,例如过滤器,kernel_size,步幅,激活等。
我尝试通过以下操作获取图层名称:
with open('model.json','r') as fb:
con = json.load(fb)
con['layers'][0]['L1'].split('(', 1)[0].rstrip()
输出为'Conv2d'
。同样,我得到了其他图层名称。
我需要帮助的是获取过滤器的值(例如L1中的64)。
我尝试这样做:
c = con['layers'][0]['L1'].split('(', 1)[1].rstrip()
c.split(',')
['filters = 8', ' kernel_size=(3', '3)', ' strides=(1', ' 1)', " padding='valid'", " data_format='channels_last'", " activation='relu'", ' use_bias=True', " kernel_initializer='zeros'", " bias_initializer='zeros'", ' kernel_regularizer=regularizers.l1(0.)', ' bias_regularizer=regularizers.l1(0.)', ' activity_regularizer=regularizers.l2(0.)', ' kernel_constraint=max_norm(2.)', ' bias_constraint=max_norm(2.)', ' input_shape=(28', '28', '1))']
但是我仍然没有得到价值。
有人知道如何获取此信息吗?
答案 0 :(得分:1)
更新:使用正则表达式可以提取关键字参数。然后在'='上分割,以查找每一层的每个关键字参数的值。
<html>
<body>
<ol id=#nameList></ol>
</body>
<script src="./script.js"></script>
</html>
答案 1 :(得分:1)
使用正则表达式-documentation供进一步参考
import re
string_lst = ['filters','kernel_size','stride','activation']
my_dict = {}
for key,value in con['layers'][0].items():
my_dict[key] = {}
layer_names = value.split('(')[0].rstrip()
my_dict[key][layer_names] = {}
for i in string_lst:
match = re.search(i+'(.+?), ', value)
if match:
filters = match.group(1).split("=")[1].strip()
my_dict[key][layer_names][i] = filters
if len(my_dict[key][layer_names]) <= 0:
del my_dict[key]
print(my_dict)
O / P:
{
'L1': {'Conv2D': {'filters': '64', 'kernel_size': '(2,2)', 'stride': '(2,2)', 'activation': "'relu'"}}, '
L2': {'MaxPooling2D': {'stride': '(2,2)'}}, 'L3': {'Conv2D':
{'filters': '64', 'kernel_size': '(2,2)', 'stride': '(2,2)', 'activation': "'relu'"}},
'L4': {'MaxPooling2D': {'stride': '(2,2)'}}, 'L5':
{'Conv2D': {'filters': '64', 'kernel_size': '(2,2)', 'stride': '(2,2)', 'activation': "'relu'"}},
'L6': {'Conv2D': {'filters': '64', 'kernel_size': '(2,2)', 'stride': '(2,2)', 'activation': "'relu'"}},
'L7': {'Conv2D': {'filters': '64', 'kernel_size': '(2,2)', 'stride': '(2,2)', 'activation': "'relu'"}},
'L8': {'MaxPooling2D': {'stride': '(2,2)'}}, 'L10': {'Dense': {'activation': "'softmax'"}},
'L12': {'Dense': {'activation': "'softmax'"}}, 'L14': {'Dense': {'activation': "'softmax'"}}
}
JSON包含重复的图层名称,如果要唯一记录,请替换所有行
my_dict[key][layer_names]
要
my_dict[layer_names]
并删除此my_dict[key] = {}
行
答案 2 :(得分:1)
我将分两步执行此操作。首先为外部过滤器名称和内容创建一个正则表达式
re.compile(r"^\s*([^(]*)\s*\((.*)\)\s*$")
这有两组,name
,内容用括号括起来(...)
然后创建一个正则表达式,以分割不在括号内的逗号。您可以深入了解explanation here
re.compile(r',\s*(?![^()]*\))')
演示:
import re
main_regex = re.compile(r"^\s*([^(]*)\s*\((.*)\)\s*$")
split_regex = re.compile(r',\s*(?![^()]*\))')
input = "Conv2D(filters = 64, kernel_size=(2,2), padding='same)"
main_match = main_regex.match(input)
print(main_match.group(1))
parts = split_regex.split(main_match.group(2))
print(parts)
打印:
Conv2D
['filters = 64', 'kernel_size=(2,2)', "padding='same"]