本文整理汇总了Python中yolo3.model.yolo_loss方法的典型用法代码示例。如果您正苦于以下问题:Python model.yolo_loss方法的具体用法?Python model.yolo_loss怎么用?Python model.yolo_loss使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yolo3.model
的用法示例。
在下文中一共展示了model.yolo_loss方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: train
# 需要导入模块: from yolo3 import model [as 别名]
# 或者: from yolo3.model import yolo_loss [as 别名]
def train(model, image_data, y_true, log_dir='logs/'):
'''retrain/fine-tune the model'''
model.compile(optimizer='adam', loss={
# use custom yolo_loss Lambda layer.
'yolo_loss': lambda y_true, y_pred: y_pred})
logging = TensorBoard(log_dir=log_dir)
checkpoint = ModelCheckpoint(log_dir + "ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5",
monitor='val_loss', save_weights_only=True, save_best_only=True)
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=5, verbose=1, mode='auto')
model.fit([image_data, *y_true],
np.zeros(len(image_data)),
validation_split=.1,
batch_size=32,
epochs=30,
callbacks=[logging, checkpoint, early_stopping])
model.save_weights(log_dir + 'trained_weights.h5')
# Further training.
示例2: create_model
# 需要导入模块: from yolo3 import model [as 别名]
# 或者: from yolo3.model import yolo_loss [as 别名]
def create_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2,
weights_path='model_data/yolo_weights.h5'):
'''create the training model'''
K.clear_session() # get a new session
image_input = Input(shape=(None, None, 3))
h, w = input_shape
num_anchors = len(anchors)
# y_true = [Input(shape=(416//{0:32, 1:16, 2:8}[l], 416//{0:32, 1:16, 2:8}[l], 9//3, 80+5)) for l in range(3)]
y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], num_anchors//3, num_classes+5)) for l in range(3)]
model_body = yolo_body(image_input, num_anchors//3, num_classes)
print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))
if load_pretrained:
model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
print('Load weights {}.'.format(weights_path))
if freeze_body in [1, 2]:
# Freeze darknet53 body or freeze all but 3 output layers.
num = (185, len(model_body.layers)-3)[freeze_body-1]
for i in range(num): model_body.layers[i].trainable = False
print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))
model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
[*model_body.output, *y_true])
model = Model([model_body.input, *y_true], model_loss)
print('model_body.input: ', model_body.input)
print('model.input: ', model.input)
return model
示例3: create_tiny_model
# 需要导入模块: from yolo3 import model [as 别名]
# 或者: from yolo3.model import yolo_loss [as 别名]
def create_tiny_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2,
weights_path='model_data/tiny_yolo_weights.h5'):
'''create the training model, for Tiny YOLOv3'''
K.clear_session() # get a new session
image_input = Input(shape=(None, None, 3))
h, w = input_shape
num_anchors = len(anchors)
y_true = [Input(shape=(h//{0:32, 1:16}[l], w//{0:32, 1:16}[l], \
num_anchors//2, num_classes+5)) for l in range(2)]
model_body = tiny_yolo_body(image_input, num_anchors//2, num_classes)
print('Create Tiny YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))
if load_pretrained:
model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
print('Load weights {}.'.format(weights_path))
if freeze_body in [1, 2]:
# Freeze the darknet body or freeze all but 2 output layers.
num = (20, len(model_body.layers)-2)[freeze_body-1]
for i in range(num): model_body.layers[i].trainable = False
print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))
model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.7})(
[*model_body.output, *y_true])
model = Model([model_body.input, *y_true], model_loss)
return model
示例4: create_model
# 需要导入模块: from yolo3 import model [as 别名]
# 或者: from yolo3.model import yolo_loss [as 别名]
def create_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=True):
'''create the training model'''
image_input = Input(shape=(None, None, 3))
h, w = input_shape
num_anchors = len(anchors)//3
y_true = [Input(shape=(h//32, w//32, num_anchors, num_classes+5)),
Input(shape=(h//16, w//16, num_anchors, num_classes+5)),
Input(shape=(h//8, w//8, num_anchors, num_classes+5))]
model_body = yolo_body(image_input, num_anchors, num_classes)
if load_pretrained:
weights_path = os.path.join('model_data', 'yolo_weights.h5')
if not os.path.exists(weights_path):
print("CREATING WEIGHTS FILE" + weights_path)
yolo_path = os.path.join('model_data', 'yolo.h5')
orig_model = load_model(yolo_path, compile=False)
orig_model.save_weights(weights_path)
model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
if freeze_body:
# Do not freeze 3 output layers.
for i in range(len(model_body.layers)-3):
model_body.layers[i].trainable = False
model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
arguments={'anchors': anchors, 'num_classes': num_classes})(
[*model_body.output, *y_true])
model = Model([model_body.input, *y_true], model_loss)
return model_body, model
示例5: train
# 需要导入模块: from yolo3 import model [as 别名]
# 或者: from yolo3.model import yolo_loss [as 别名]
def train(model, annotation_path, input_shape, anchors, num_classes, log_dir='logs/'):
model.compile(optimizer='adam', loss={
'yolo_loss': lambda y_true, y_pred: y_pred})
logging = TensorBoard(log_dir=log_dir)
checkpoint = ModelCheckpoint(log_dir + "ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5",
monitor='val_loss', save_weights_only=True, save_best_only=True, period=1)
batch_size = 15
val_split = 0.1
with open(annotation_path, encoding='UTF-8') as f:
lines = f.readlines()
np.random.shuffle(lines)
num_val = int(len(lines)*val_split)
num_train = len(lines) - num_val
print('Train on {} samples, val on {} samples, with batch size {}.'.format(num_train, num_val, batch_size))
try :
#########2、修改epochs为30 ###########
model.fit_generator(data_generator_wrap(lines[:num_train], batch_size, input_shape, anchors, num_classes),
steps_per_epoch = max(1, num_train // batch_size),
validation_data = data_generator_wrap(lines[num_train:], batch_size, input_shape, anchors, num_classes),
validation_steps = max(1, num_val // batch_size), epochs = 30, initial_epoch = 0)
except :
print("error")
finally:
model.save_weights(log_dir + 'trained_weights_except.h5')
model.save_weights(log_dir + 'trained_weights.h5')
示例6: create_model
# 需要导入模块: from yolo3 import model [as 别名]
# 或者: from yolo3.model import yolo_loss [as 别名]
def create_model(input_shape, anchors, num_classes, load_pretrained=False, freeze_body=False,
weights_path='model_data/yolo_weights.h5'):
K.clear_session() # get a new session
image_input = Input(shape=(None, None, 3))
h, w = input_shape
num_anchors = len(anchors)
y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \
num_anchors//3, num_classes+5)) for l in range(3)]
model_body = yolo_body(image_input, num_anchors//3, num_classes)
print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))
if load_pretrained:
model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
print('Load weights {}.'.format(weights_path))
if freeze_body:
# Do not freeze 3 output layers.
num = len(model_body.layers)-7
for i in range(num): model_body.layers[i].trainable = False
print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))
model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
[*model_body.output, *y_true])
model = Model([model_body.input, *y_true], model_loss)
return model
示例7: create_model
# 需要导入模块: from yolo3 import model [as 别名]
# 或者: from yolo3.model import yolo_loss [as 别名]
def create_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2,
weights_path='model_data/yolo_weights.h5'):
'''create the training model'''
K.clear_session() # get a new session
image_input = Input(shape=(None, None, 3))
h, w = input_shape
num_anchors = len(anchors)
y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \
num_anchors//3, num_classes+5)) for l in range(3)]
model_body = yolo_body(image_input, num_anchors//3, num_classes)
print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))
if load_pretrained:
model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
print('Load weights {}.'.format(weights_path))
if freeze_body in [1, 2]:
# Freeze darknet53 body or freeze all but 3 output layers.
num = (185, len(model_body.layers)-3)[freeze_body-1]
for i in range(num): model_body.layers[i].trainable = False
print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))
model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
[*model_body.output, *y_true])
model = Model([model_body.input, *y_true], model_loss)
return model
开发者ID:Akhtar303,项目名称:Vehicle-Detection-and-Tracking-Usig-YOLO-and-Deep-Sort-with-Keras-and-Tensorflow,代码行数:31,代码来源:train.py
示例8: train
# 需要导入模块: from yolo3 import model [as 别名]
# 或者: from yolo3.model import yolo_loss [as 别名]
def train(model, annotation_path, input_shape, anchors, num_classes, log_dir='logs/'):
model.compile(optimizer=Adam(lr=1e-3), loss={
'yolo_loss': lambda y_true, y_pred: y_pred})
# 该回调函数将日志信息写入TensorBorad,使得你可以动态的观察训练和测试指标的图像以及不同层的激活值直方图。
logging = TensorBoard(log_dir=log_dir)
# 该回调函数将在每个epoch后保存模型到filepath
checkpoint = ModelCheckpoint(log_dir + "best_weights.h5",
verbose=1,
save_weights_only=True, save_best_only=True, mode='auto', period=1)
# 当评价指标不在提升时,减少学习率
reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=10, verbose=1)
# 当监测值不再改善时,该回调函数将中止训练
early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1)
callbacks = [checkpoint, logging, reduce_lr]
batch_size = 12
val_split = 0.1
with open(annotation_path) as f:
lines = f.readlines()
np.random.shuffle(lines) # 打乱排序
num_val = int(len(lines) * val_split)
num_train = len(lines) - num_val # 拿出0.1做val集
print('Train on {} samples, val on {} samples, with batch size {}.'.format(num_train, num_val, batch_size))
model.fit_generator(data_generator_wrap(lines[:num_train], batch_size, input_shape, anchors, num_classes),
steps_per_epoch=max(1, num_train // batch_size),
validation_data=data_generator_wrap(lines[num_train:], batch_size, input_shape, anchors,
num_classes),
validation_steps=max(1, num_val // batch_size),
epochs=500,
initial_epoch=0, callbacks=callbacks)
model.save_weights(log_dir + 'trained_weights.h5')
model.save(log_dir + 'model.h5')
示例9: create_model
# 需要导入模块: from yolo3 import model [as 别名]
# 或者: from yolo3.model import yolo_loss [as 别名]
def create_model(input_shape, anchors, num_classes, load_pretrained=False, freeze_body=False,
weights_path='model_data/yolo_weights.h5'):
K.clear_session() # get a new session
image_input = Input(shape=(None, None, 3))
h, w = input_shape
num_anchors = len(anchors)
y_true = [Input(shape=(h // {0: 32, 1: 16, 2: 8}[l], w // {0: 32, 1: 16, 2: 8}[l], \
num_anchors // 3, num_classes + 5)) for l in range(3)]
model_body = yolo_body(image_input, num_anchors // 3, num_classes)
print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))
if load_pretrained:
model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
print('Load weights {}.'.format(weights_path))
if freeze_body:
# Do not freeze 3 output layers.
num = len(model_body.layers) - 7
for i in range(num): model_body.layers[i].trainable = False
print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))
model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
[*model_body.output, *y_true])
model = Model([model_body.input, *y_true], model_loss)
return model
示例10: train
# 需要导入模块: from yolo3 import model [as 别名]
# 或者: from yolo3.model import yolo_loss [as 别名]
def train(model, annotation_path, input_shape, anchors, num_classes, log_dir='logs/'):
for index in range(1,11):
e = index*50
ie = e-50
if True:
model.compile(optimizer='adam', loss={
'yolo_loss': lambda y_true, y_pred: y_pred})
logging = TensorBoard(log_dir=log_dir)
checkpoint = ModelCheckpoint(log_dir + "ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5",
monitor='val_loss', save_weights_only=True, save_best_only=True, period=1)
batch_size = 6
val_split = 0.1
with open(annotation_path) as f:
lines = f.readlines()
np.random.shuffle(lines)
num_val = int(len(lines)*val_split)
num_train = len(lines) - num_val
print('Train on {} samples, val on {} samples, with batch size {}.'.format(num_train, num_val, batch_size))
model.fit_generator(data_generator_wrap(lines[:num_train], batch_size, input_shape, anchors, num_classes),
steps_per_epoch=max(1, num_train//batch_size),
validation_data=data_generator_wrap(lines[num_train:], batch_size, input_shape, anchors, num_classes),
validation_steps=max(1, num_val//batch_size),
epochs=e,
initial_epoch=ie,
callbacks=[logging, checkpoint])
model.save_weights(log_dir + str(index)+'trained_weights.h5')
示例11: create_model
# 需要导入模块: from yolo3 import model [as 别名]
# 或者: from yolo3.model import yolo_loss [as 别名]
def create_model(input_shape, anchors, num_classes, load_pretrained=True, freeze_body=2,
weights_path='model_data/yolo_weights.h5'):
'''create the training model'''
K.clear_session() # get a new session
image_input = Input(shape=(None, None, 3))
h, w = input_shape
num_anchors = len(anchors)
y_true = [Input(shape=(h//{0:32, 1:16, 2:8}[l], w//{0:32, 1:16, 2:8}[l], \
num_anchors//3, num_classes+5)) for l in range(3)]
model_body = yolo_body(image_input, num_anchors//3, num_classes)
print('Create YOLOv3 model with {} anchors and {} classes.'.format(num_anchors, num_classes))
if load_pretrained:
model_body.load_weights(weights_path, by_name=True, skip_mismatch=True)
print('Load weights {}.'.format(weights_path))
if freeze_body in [1, 2]:
# Freeze darknet53 body or freeze all but 3 output layers.
num = (185, len(model_body.layers)-3)[freeze_body-1]
for i in range(num): model_body.layers[i].trainable = False
print('Freeze the first {} layers of total {} layers.'.format(num, len(model_body.layers)))
# get output of second last layers and create bottleneck model of it
out1=model_body.layers[246].output
out2=model_body.layers[247].output
out3=model_body.layers[248].output
bottleneck_model = Model([model_body.input, *y_true], [out1, out2, out3])
# create last layer model of last layers from yolo model
in0 = Input(shape=bottleneck_model.output[0].shape[1:].as_list())
in1 = Input(shape=bottleneck_model.output[1].shape[1:].as_list())
in2 = Input(shape=bottleneck_model.output[2].shape[1:].as_list())
last_out0=model_body.layers[249](in0)
last_out1=model_body.layers[250](in1)
last_out2=model_body.layers[251](in2)
model_last=Model(inputs=[in0, in1, in2], outputs=[last_out0, last_out1, last_out2])
model_loss_last =Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
[*model_last.output, *y_true])
last_layer_model = Model([in0,in1,in2, *y_true], model_loss_last)
model_loss = Lambda(yolo_loss, output_shape=(1,), name='yolo_loss',
arguments={'anchors': anchors, 'num_classes': num_classes, 'ignore_thresh': 0.5})(
[*model_body.output, *y_true])
model = Model([model_body.input, *y_true], model_loss)
return model, bottleneck_model, last_layer_model