python try except else

2019-08-26  本文已影响0人  dc_3

参考

try:
    a=c
except:
    print('error')
else:
    print('no error')
print('继续执行')

执行结果:

error
继续执行

如果try下面的语句有问题,那么就会执行except,不执行else。相反,如果没有问题,那么不执行except,执行else。只要try和else语句里没有任何结束程序运行的代码,那么都会执行接下来的语句。
执行else情况如下:

try:
    a=3
except:
    print('error')
else:
    print('no error')
print('继续执行')

执行结果:

no error
继续执行

涉及到yolo.py中的代码:

try:
#先尝试加载模型的结构和权重
            self.yolo_model = load_model(model_path, compile=False)
        except:
#如果没有加载成功,说明只保存了模型的权重。那么需要重新创建模型的结构,使用 yolo_body 函数,然后再加载模型的权重
            self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors//2, num_classes) \
                if is_tiny_version else yolo_body(Input(shape=(None,None,3)), num_anchors//3, num_classes)
            self.yolo_model.load_weights(self.model_path) # make sure model, anchors and classes match
        else:
            #self.yolo_model.layers[-1].output_shape[-1]=255
            #len(self.yolo_model.output)=3
#如果没有错误,那么判断输出的形状和我们要的形状是否匹配,不匹配的话报出给定错误。
            assert self.yolo_model.layers[-1].output_shape[-1] == \
                num_anchors/len(self.yolo_model.output) * (num_classes + 5), \
                'Mismatch between model and given anchor and class sizes'

        print('{} model, anchors, and classes loaded.'.format(model_path))
上一篇 下一篇

猜你喜欢

热点阅读