日萌社

人工智能AI:Keras PyTorch MXNet TensorFlow PaddlePaddle 深度学习实战(不定时更新)


EfficientNet中的每个模型要求的输入形状大小

每个网络要求的输入形状大小:   EfficientNetB0 - (224, 224, 3)EfficientNetB1 - (240, 240, 3)EfficientNetB2 - (260, 260, 3)EfficientNetB3 - (300, 300, 3)EfficientNetB4 - (380, 380, 3)EfficientNetB5 - (456, 456, 3)EfficientNetB6 - (528, 528, 3)EfficientNetB7 - (600, 600, 3)

EfficientNet模型迁移的使用注意事项:1.因为该模型的源码是在tensorflow 1.x的版本,并非是tensorflow 2.0的版本,因此在tensorflow 2.0环境中使用的话,需要用到tf.compat.v1.disable_eager_execution(),表示关闭默认的eager模式,但要注意的是,如果关闭默认的eager模式了的话,那么同时还使用tf.keras.callbacks.TensorBoard的话会报错,tf.keras.callbacks.ModelCheckpoint不会报错,那么解决的方式要么此时不使用TensorBoard,或者不关闭默认的eager模式。2.layers.py中的class Swish中的call()函数返回值的修改建议。如果使用了tf.compat.v1.disable_eager_execution()之后,报错No registered 'swish_f32' OpKernel for GPU devices compatible with node的话,把 layers.py中的class Swish中的call()函数返回值 return tf.nn.swish(inputs) 修改为 return inputs * tf.math.sigmoid(inputs) 即可解决,实际上底层是 tf.nn.swish(x) 封装了 x* tf.math.sigmoid(x),不使用tf.nn.swish之后,即可也把tf.compat.v1.disable_eager_execution()给注释掉,即不需要关闭默认的eager模式了,那么此时也可以正常同时使用TensorBoard。

4.10 综合案例:垃圾分类之模型构建与训练

学习目标

  • 目标
    • 掌握EfficientNet模型原理
    • 掌握warmup以及余弦退火学习率原理
  • 应用
    • 应用完成垃圾分类的训练过程
    • 应用完成余弦退火与warmup的实现

4.10.1 EfficientNet模型介绍

先再来看一遍efficientnet的取得的成绩

可以看出 EfficientNet 系列完胜了其他所有的卷积网络。其中EfficientNet-B7实现了ImageNet的state-of-the-art率,达到了 84.4%。但是它的参数量相比 GPipe 减少了 8.4 倍,并且推理速度达到了 GPipe 的 6.1 倍。更加细节的数据可以参考后面的实验部分。

论文地址:https://arxiv.org/pdf/1905.11946.pdf

4.10.1.1 摘要

作者系统地研究了模型缩放并且仔细验证了网络深度、宽度和分辨率之间的平衡可以导致更好的性能表现。提出了一种新的缩放方法——使用一个简单高效的复合系数来完成对深度/宽度/分辨率所有维度的统一缩放。在MobileNets和ResNet上展示了这种缩放方法的高效性。为了进一步研究,我们使用神经架构搜索设计了一个baseline网络。使用神经架构搜索来设计新的baseline并进行扩展以获得一系列模型,称EfficientNets。

引入:

一般ConvNets的精度随着它的size增加,有很多工作通过增加ConvNets的宽度、深度或者图像分辨率去提升网络的性能。尽管可以任意缩放二维或三维,但任意缩放需要繁琐的手动调整,并且仍然经常产生次优的精度和效率。

作者重新思考和研究了ConvNets的缩放问题,是否存在一种原则性的方法缩放ConvNets,从而实现更高的精度和效率?作者的实证研究表明,平衡网络宽度/深度/分辨率的所有维度是至关重要的,且可通过简单地按比例缩放每个维度来实现这种平衡。基于此,提出了一种简单而有效的复合缩放方法。

4.10.1.2 原理介绍

1、复合模型缩放-问题建模

表示比较了不同网络深度和分辨率下的宽度缩放,如下图所示。在不改变深度(d = 1.0)和分辨率(r = 1.0)的情况下缩放网络宽度w,则精度会很快达到饱和。 随着更深(d = 2.0)和更高分辨率(r = 2.0),宽度缩放在相同的FLOPS成本下实现了更好的精度。

2、新的复合方法

4.10.1.3 Efficientnet 架构

所以通过上面的方法,作者找到了一个新的baseline((MBConv),类似于MobileNetV2和MnasNet)来评估,称为EfficientNet-B0。

4.10.1.4 实验

  • 对MobileNets 和 ResNets进行缩放

这个复合缩放方法提高了所有这些模型的准确性,表明了缩放方法对现有的卷积网络结构有效性。

  • 类激活图说明了具有复合缩放的模型倾向于关注具有更多对象细节的更相关区域,而其他模型要么缺少对象细节,要么无法捕获图像中的所有对象。

  • 在各比赛中做迁移学习得成就

  • 与公开可用模型相比,EfficientNet模型减少了平均4.7倍(最多21倍)的参数,同时实现了更高的精度。
  • 与最先进的模型相比,EfficientNet模型在8个数据集中有5个仍然超过了它们的准确度,且使用的参数减少了9.6倍。

最终各种模型的精度 - 参数曲线,红色为EfficientNet的结果,明显比各个模型精度高,参数量少。

注:最终的效果比率,都是在大量的设备和模型上计算得来的,资源消耗不可想象。Google有足够的资源和设备(TPU)去做。

4.10.2 垃圾分类开源EfficientNet实现介绍

4.10.2.1 模型目录

TensorFlow2.0 可进行迁移学习的实现版本不存在efficientnet,需要第三方实现的模型两种选择:

1、可迁移学习的TF低版本能使用(部分操作不支持默认Eager 模式)

https://github.com/calmisential/Basic_CNNs_TensorFlow2

我们选择的是这个版本,可以在与训练模型上迁移,使用简单

from efficientnet import EfficientNetB0
EfficientNetB0(weights=None, include_top=False, input_shape=(336, 336, 3))

2、TF2.0实现版本不能使用在imagenet上的与训练模型,版本简单易懂,项目中也含有其他的模型

https://github.com/Tony607/efficientnet_keras_transfer_learning

去工程中看看指定模型。其中参数include_top,指定了我们可以进行迁移学习:

    if include_top:x = KL.GlobalAveragePooling2D(data_format=global_params.data_format)(x)if global_params.dropout_rate > 0:x = KL.Dropout(global_params.dropout_rate)(x)x = KL.Dense(global_params.num_classes, kernel_initializer=DenseKernalInitializer())(x)x = KL.Activation('softmax')(x)else:if pooling == 'avg':x = KL.GlobalAveragePooling2D(data_format=global_params.data_format)(x)elif pooling == 'max':x = KL.GlobalMaxPooling2D(data_format=global_params.data_format)(x)

4.10.3 优化算法以及学习率trick

4.10.2.1 Rectified Adam(Adam with warm up)

RAdam能根据方差分散度,动态地打开或者 关闭自适应学习率,并且提供了一种不需要可 调参数学习率预热的方法。

 

上述结果表明使用原始Adam必须预热,否则正态分布会变得扭曲

4.10.2.2 Warmup

  • 定义:学习率预热就是在刚开始训练的时候先使用一个较小的学习率,训练一些epoches或iterations,等模型稳定时再修改为预先设置的学习率进行训练。

学习率是神经网络训练中最重要的超参数之一,针对学习率的技巧有很多。Warm up是在ResNet论文中提到的一种学习率预热的方法。

  • 原因:由于刚开始训练时模型的权重(weights)是随机初始化的,此时选择一个较大的学习率,可能会带来模型的不稳定。
    • 论文中使用一个110层的ResNet在cifar10上训练时,先用0.01的学习率训练直到训练误差低于80%(大概训练了400个iterations),然后使用0.1的学习率进行训练。

理解:刚开始模型对数据的“分布”理解为零,或者是说“均匀分布”;在第一轮训练的时候,每个数据点对模型来说都是新的,模型会很快地进行数据分布修正,如果这时候学习率就很大,极有可能导致开始的时候就对该数据“过拟合”,后面要通过多轮训练才能拉回来,浪费时间。

4.10.2.3 余弦学习率衰减(Cosine Learning rate decay)

余弦学习率衰减的方式,Cosine Learning rate decay。公式如下:

下图是逐步衰减学习率与余弦学习率衰减的方式对比:

  • 现象:当Step Decay方法的学习率已经较小的时候,Cos Decay方法的学习仍比较大,因而能够加速整个训练的过程。但是看图中b,很明显Step Decay再衰减之后,准确率就上来了,说明衰减之前的数量级效用已经不强了,这个时候Cos decay还是那个数量级,所以速度就比Step Decay慢了
  • 结果:
    • 1、cos的学习率的前期是warm-up阶段,这个时候是以线性增长的方式增长到初始学习率,然后开始执行cos的学习率变化,最终两种学习率达到一致。从准确性的角度来看,使用step的方式似乎学习的更快一些。而且其变化的拐点和其学习率的拐点是对应着的,即学习率降了之后,验证的准确性也跟着开始提升,而cos学习率的整个过程中准确性都很平稳,最终两者的准确性也是一致。
    • 2、区别在于中间的学习过程。而且step的方式有一定的随机性,不知道要以多大的step来改变学习率,如果这个’step’可以根据某种方式量化

定义:常用的Learning Rate Decay是Step Decay,即每隔N个Epoch,learning rate乘上一个固定decay系数。

  • 但是Step Decay不好的地方在于学习率衰减的时候,跳跃变化较大,带来了较大的冲量Momentum

4.10.2.4 TensorFlow实现

Keras 的 callbacks 中有 ReduceLROnPlateau() 和 LearningRateScheduler() 函数可以动态的调整学习率。但是前者只在验证误差停止衰减的时候减小学习率,后者只能在每个 Epoch 开始或结束的时候,改变学习率两者使用参考文档:https://www.tensorflow.org/versions/r2.0/api_docs/python/tf/keras/callbacks/LearningRateScheduler#class_learningratescheduler

如果需要在训练的时候每批次更加细致的控制学习率,需要自定义回调方法

1、tf.keras.callbacks.Callback

该类在Model 的.fit()方法中会调用一下回调方法

  • on_batch_begin( batch, logs=None)
    • 一批次数据开始时的处理
  • on_batch_end(batch, logs=None)
    • 一批次数据处理结束
  • on_epoch_begin和on_epoch_end

4.10.3 垃圾分类带有warmup的余弦退火学习率调度实现

4.10.3.1 流程分析

  • 分为两个结算
    • warmup阶段
    • 余弦退火阶段

4.10.3.2 完整代码过程实现

参考文件,并运行测试

  • 步骤:
    • 1、自定义WarmUpCosineDecayScheduler调度器,实现批次前后的处理逻辑
    • 2、实现warmup的余弦退火学习率计算方法

1、自定义WarmUpCosineDecayScheduler调度器,实现批次前后的处理逻辑

import numpy as np
import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"class WarmUpCosineDecayScheduler(tf.keras.callbacks.Callback):"""带有warmup的余弦退火学习率调度"""def __init__(self,learning_rate_base,total_steps,global_step_init=0,warmup_learning_rate=0.0,warmup_steps=0,hold_base_rate_steps=0,verbose=0):"""初始化参数:param learning_rate_base: 基础学习率:param total_steps: 总共迭代的批次步数 epoch * num_samples / batch_size:param global_step_init: 初始:param warmup_learning_rate: 预热学习率默认0.0:param warmup_steps:预热的步数默认0:param hold_base_rate_steps::param verbose:"""super(WarmUpCosineDecayScheduler, self).__init__()self.learning_rate_base = learning_rate_baseself.total_steps = total_stepsself.global_step = global_step_initself.warmup_learning_rate = warmup_learning_rateself.warmup_steps = warmup_stepsself.hold_base_rate_steps = hold_base_rate_steps# 是否在每次训练结束打印学习率self.verbose = verbose# 记录所有批次下来的每次准确的学习率,可以用于打印显示self.learning_rates = []def on_batch_end(self, batch, logs=None):# 1、批次开始前当前步数+1self.global_step = self.global_step + 1# 2、获取优化器上一次的学习率,并记录lr = K.get_value(self.model.optimizer.lr)self.learning_rates.append(lr)def on_batch_begin(self, batch, logs=None):# 1、通过参数以及记录的次数和上次学习率lr = cosine_decay_with_warmup(global_step=self.global_step,learning_rate_base=self.learning_rate_base,total_steps=self.total_steps,warmup_learning_rate=self.warmup_learning_rate,warmup_steps=self.warmup_steps,hold_base_rate_steps=self.hold_base_rate_steps)# 2、设置优化器本次的学习率K.set_value(self.model.optimizer.lr, lr)if self.verbose > 0:print('\n批次数 %05d: 设置学习率为'' %s.' % (self.global_step + 1, lr))

这里面涉及到一个设置当前优化器学习率的使用,会用到keras.backend这个模块。keras是一种基于模块的高级深度学习开发框架,它并没有仅依赖于某一种高速底层张量库,而是对各种底层张量库进行高层模块封装,让底层库完成诸如张量积、卷积操作。在keras中,各种底层库(Google开发的TensorFlow、蒙特利尔大学实验室开发的Theano、微软开发的CNTK)都可以作为后端(backend)引擎为keras模块提供服务。

问题:如何修改Keras使用的backend

(1)通过修改keras配置文件来修改backend

一旦运行过一次Keras,就会在$HOME/.keras下生成配置文件keras.json,该文件的"backend"字段的值即为keras所使用的后端库,默认情况下,该值为"tensorflow"。用户可以根据需要选择另外两个库"theano"、"cntk",甚至自己写的底层库。

(2)通过运行Python脚本时增加配置项指定backend

$ KERAS_BACKEND=theano python -c "from keras import backend"
Using Theano backend.

导入使用:

from keras import backend as K

其中两个方法可以设置张量的值:

lr = K.get_value(self.model.optimizer.lr)
K.set_value(self.model.optimizer.lr, lr)

2、实现warmup的余弦退火学习率计算方法

  • 步骤:
    • 1、余弦退火学习率计算
    • 2、warmup之后的学习率计算
      • 如果预留大于0,判断目前步数是否 > warmup步数+预留步数,是的话返回刚才上面计算的学习率,不是的话使用warmup之后的基础学习率
    • 3、warmup学习率计算,并判断大小
    • 4、如果最后当前到达的步数大于总步数,则归0,否则返回当前的计算出来的学习率(可能是warmup学习率也可能是余弦衰减结果)

代码如下

def cosine_decay_with_warmup(global_step,learning_rate_base,total_steps,warmup_learning_rate=0.0,warmup_steps=0,hold_base_rate_steps=0):"""每批次带有warmup余弦退火学习率计算:param global_step: 当前到达的步数:param learning_rate_base: warmup之后的基础学习率:param total_steps: 总需要批次数:param warmup_learning_rate: warmup开始的学习率:param warmup_steps:warmup学习率 步数:param hold_base_rate_steps: 预留总步数和warmup步数间隔:return:"""if total_steps < warmup_steps:raise ValueError('总步数必须大于warmup')# 1、余弦退火学习率计算# 从warmup结束之后计算# 0.5 * 0.01 * (1 + cos(pi*(1-5-0)/(10 - 5 - 0))learning_rate = 0.5 * learning_rate_base * (1 + np.cos(np.pi *(global_step - warmup_steps - hold_base_rate_steps) / float(total_steps - warmup_steps - hold_base_rate_steps)))# 2、warmup之后的学习率计算# 如果预留大于0,判断目前步数是否 > warmup步数+预留步数,是的话返回刚才上面计算的学习率,不是的话使用warmup之后的基础学习率if hold_base_rate_steps > 0:learning_rate = np.where(global_step > warmup_steps + hold_base_rate_steps,learning_rate, learning_rate_base)# 3、warmup步数是大于0的if warmup_steps > 0:if learning_rate_base < warmup_learning_rate:raise ValueError('warmup后学习率必须大于warmup开始学习率')# 1、计算一个0.01和0.000006的差距/warmup_steps,得到warmup结束前增加多少slope = (learning_rate_base - warmup_learning_rate) / warmup_steps# 2、计算warmup下一步第global_step的学习率warmup_rate = slope * global_step + warmup_learning_rate# 3、判断global_step小于warmup_steps的话,返回这个warmup当时的学习率,否则直接返回余弦退火计算的learning_rate = np.where(global_step < warmup_steps, warmup_rate,learning_rate)# 4、如果最后当前到达的步数大于总步数,则归0,否则返回当前的计算出来的学习率(可能是warmup学习率也可能是余弦衰减结果)return np.where(global_step > total_steps, 0.0, learning_rate)

通过以下代码进行测试:

if __name__ == '__main__':# 1、创建模型model = Sequential()model.add(Dense(32, activation='relu', input_dim=100))model.add(Dense(10, activation='softmax'))model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])# 2、参数设置sample_count = 1000  # 样本数epochs = 4  # 总迭代次数warmup_epoch = 3  # warmup 迭代次数batch_size = 16  # 批次大小learning_rate_base = 0.0001  # warmup后的初始学习率total_steps = int(epochs * sample_count / batch_size)  # 总迭代批次步数warmup_steps = int(warmup_epoch * sample_count / batch_size)  # warmup总批次数# 3、创建测试数据data = np.random.random((sample_count, 100))labels = np.random.randint(10, size=(sample_count, 1))# 转换目标类别one_hot_labels = tf.keras.utils.to_categorical(labels, num_classes=10)# 5、创建余弦warmup调度器warm_up_lr = WarmUpCosineDecayScheduler(learning_rate_base=learning_rate_base,total_steps=total_steps,warmup_learning_rate=4e-06,  # warmup开始学习率warmup_steps=warmup_steps,hold_base_rate_steps=0,)# 训练模型model.fit(data, one_hot_labels, epochs=epochs, batch_size=batch_size, verbose=0, callbacks=[warm_up_lr])print(warm_up_lr.learning_rates)

结果:

[4e-06, 4.513369e-06, ....,  7.281053e-05, 7.0564354e-05, 6.826705e-05, 6.592433e-05, 6.354202e-05, 6.112605e-05, 5.868241e-05, 5.6217184e-05, 5.3736505e-05, 5.1246534e-05, 4.8753467e-05, 4.6263496e-05, 4.3782813e-05, 4.131759e-05, 3.8873954e-05, 3.6457976e-05, 3.4075667e-05, 3.173295e-05, 2.9435645e-05, 2.7189468e-05, 2.5e-05, 2.2872688e-05, 2.0812817e-05, 1.882551e-05, 1.6915708e-05, 1.5088159e-05, 1.3347407e-05, 1.1697778e-05, 1.0143374e-05, 8.688061e-06, 7.335456e-06, 6.0889215e-06, 4.9515565e-06, 3.9261895e-06, 3.015369e-06, 2.2213596e-06, 1.5461356e-06, 9.913756e-07, 5.584587e-07, 2.4846122e-07, 6.215394e-08, 0.0, 0.0]

4.10.4 模型训练过程实现

  • 步骤:
    • 1、建立读取数据的sequence
    • 2、建立模型,指定模型训练相关参数
      • 模型修改
      • 模型训练优化器指定
    • 3、指定训练的callbacks,并进行模型的训练
    • 4、训练指定

其中代码运行逻辑

if __name__ == '__main__':args = parser.parse_args()train_model(args)

参数指定使用argparse工具:pip install argparse

parser = argparse.ArgumentParser()
parser.add_argument("data_url", type=str, default='./data/garbage_classify/train_data', help="data dir", nargs='?')
parser.add_argument("train_url", type=str, default='./garbage_ckpt/', help="save model dir", nargs='?')
parser.add_argument("num_classes", type=int, default=40, help="num_classes", nargs='?')
parser.add_argument("input_size", type=int, default=300, help="input_size", nargs='?')
parser.add_argument("batch_size", type=int, default=16, help="batch_size", nargs='?')
parser.add_argument("learning_rate", type=float, default=0.0001, help="learning_rate", nargs='?')
parser.add_argument("max_epochs", type=int, default=30, help="max_epochs", nargs='?')
parser.add_argument("deploy_script_path", type=str, default='', help="deploy_script_path", nargs='?')
parser.add_argument("test_data_url", type=str, default='', help="test_data_url", nargs='?')

其中nargs是为了在pycharm中运行时,不输入命令行参数值也能直接运行。否则需要命令行运行

python train.py data_url .....

1、建立读取数据的sequence

import multiprocessing
import numpy as np
import argparse
import tensorflow as tf
from tensorflow.keras.callbacks import TensorBoard, Callback
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam, RMSpropfrom efficientnet import model as EfficientNet
from data_gen import data_from_sequence
from utils.lr_scheduler import WarmUpCosineDecayScheduler
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"
# 注意关闭默认的eager模式
tf.compat.v1.disable_eager_execution()def train_model(param):"""训练模型:param param: 传入的命令参数:return:"""# 1、建立读取数据的sequencetrain_sequence, validation_sequence = data_from_sequence(param.data_url, param.batch_size,param.num_classes, param.input_size)

2、建立模型,指定模型训练相关参数

# 2、建立模型,指定模型训练相关参数
model = model_fn(param)optimizer = Adam(lr=param.learning_rate)
objective = 'categorical_crossentropy'
metrics = ['accuracy']
# 模型修改
# 模型训练优化器指定
model.compile(loss=objective, optimizer=optimizer, metrics=metrics)
model.summary()# 判断模型是否加载历史模型
if os.path.exists(param.train_url):filenames = os.listdir(param.train_url)model.load_weights(filenames[-1])print("加载完成!!!")def model_fn(param):"""迁移学习修改模型函数:param param::return:"""base_model = EfficientNet.EfficientNetB3(include_top=False, input_shape=(param.input_size, param.input_size, 3),classes=param.num_classes)x = base_model.outputx = GlobalAveragePooling2D(name='avg_pool')(x)predictions = Dense(param.num_classes, activation='softmax')(x)model = Model(inputs=base_model.input, outputs=predictions)return model

3、指定训练的callbacks,并进行模型的训练

sample_count = len(train_sequence) * param.batch_size
epochs = param.max_epochs
warmup_epoch = 5
batch_size = param.batch_size
learning_rate_base = param.learning_rate
total_steps = int(epochs * sample_count / batch_size)
warmup_steps = int(warmup_epoch * sample_count / batch_size)warm_up_lr = WarmUpCosineDecayScheduler(learning_rate_base=learning_rate_base,total_steps=total_steps,warmup_learning_rate=0,warmup_steps=warmup_steps,hold_base_rate_steps=0,)
#(3)模型保存相关参数
check = tf.keras.callbacks.ModelCheckpoint(param.train_url+'weights_{epoch:02d}-{val_acc:.2f}.h5',monitor='val_acc',save_best_only=True,save_weights_only=False,mode='auto',period=1)

4、训练

这里使用model.fit_generator函数,因为填入参数的是一个迭代序列,指定工作线程数(multiprocessing.cpu_count() * 0.7。

model.fit_generator(train_sequence,steps_per_epoch=len(train_sequence),epochs=param.max_epochs,verbose=1,callbacks=[check, tensorboard, warm_up_lr],validation_data=validation_sequence,max_queue_size=10,workers=int(multiprocessing.cpu_count() * 0.7),use_multiprocessing=True,shuffle=True)

4.10.5 总结

  • EfficientNet模型原理
  • warmup以及余弦退火学习率原理
  • 完成垃圾分类的训练过程
  • 完成余弦退火与warmup的实现

garbage_clssify 垃圾分类

data_gen文件夹 processing_data.py

import math
import os
import random
import numpy as np
from PIL import Image
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.utils import to_categorical, Sequence
from sklearn.model_selection import train_test_splitfrom data_gen.random_eraser import get_random_eraserclass GarbageDataSequence(Sequence):"""垃圾分类数据流,每次batch返回batch_size大小数据model.fit_generator使用"""def __init__(self, img_paths, labels, batch_size, img_size, use_aug):# 1、获取训练特征与目标值的合并结果 [batch_size, 1],  [batch_size, 40]   [batch_size, 41]self.x_y = np.hstack((np.array(img_paths).reshape(len(img_paths), 1),np.array(labels)))self.batch_size = batch_sizeself.img_size = img_size  # (300, 300)self.use_aug = use_augself.alpha = 0.2# 随机擦出方法self.eraser = get_random_eraser(s_h=0.3, pixel_level=True)def __len__(self):return math.ceil(len(self.x_y) / self.batch_size)@staticmethoddef center_img(img, size=None, fill_value=255):"""改变图片尺寸到300x300,并且做填充使得图像处于中间位置"""h, w = img.shape[:2]if size is None:size = max(h, w)shape = (size, size) + img.shape[2:]background = np.full(shape, fill_value, np.uint8)center_x = (size - w) // 2center_y = (size - h) // 2background[center_y:center_y + h, center_x:center_x + w] = imgreturn backgrounddef preprocess_img(self, img_path):"""处理每张图片,大小, 数据增强:param img_path::return:"""# 1、读取图片对应内容,做形状,内容处理, (h, w)img = Image.open(img_path)# [180, 200, 3]scale = self.img_size[0] / max(img.size[:2])img = img.resize((int(img.size[0] * scale), int(img.size[1] * scale)))img = img.convert('RGB')img = np.array(img)# 2、数据增强:如果是训练集进行数据增强操作if self.use_aug:# 1、随机擦处img = self.eraser(img)# 2、翻转datagen = ImageDataGenerator(width_shift_range=0.05,height_shift_range=0.05,horizontal_flip=True,vertical_flip=True,)img = datagen.random_transform(img)# 4、处理一下形状 【300, 300, 3】# 改变到[300, 300] 建议不要进行裁剪操作,变形操作,保留数据增强之后的效果,填充到300x300img = self.center_img(img, self.img_size[0])return imgdef __getitem__(self, idx):# 1、获取当前批次idx对应的特征值和目标值batch_x = self.x_y[idx * self.batch_size: self.batch_size * (idx + 1), 0]batch_y = self.x_y[idx * self.batch_size: self.batch_size * (idx + 1), 1:]batch_x = np.array([self.preprocess_img(img_path) for img_path in batch_x])batch_y = np.array(batch_y).astype(np.float32)# 2、mixupbatch_x, batch_y = self.mixup(batch_x, batch_y)# 3、归一化处理batch_x = self.preprocess_input(batch_x)return batch_x, batch_ydef on_epoch_end(self):np.random.shuffle(self.x_y)def mixup(self, batch_x, batch_y):"""数据混合mixup:param batch_x: 要mixup的batch_X:param batch_y: 要mixup的batch_y:return: mixup后的数据"""size = self.batch_sizel = np.random.beta(self.alpha, self.alpha, size)X_l = l.reshape(size, 1, 1, 1)y_l = l.reshape(size, 1)X1 = batch_xY1 = batch_yX2 = batch_x[::-1]Y2 = batch_y[::-1]X = X1 * X_l + X2 * (1 - X_l)Y = Y1 * y_l + Y2 * (1 - y_l)return X, Ydef preprocess_input(self, x):"""归一化处理样本特征值:param x::return:"""assert x.ndim in (3, 4)assert x.shape[-1] == 3MEAN_RGB = [0.485 * 255, 0.456 * 255, 0.406 * 255]STDDEV_RGB = [0.229 * 255, 0.224 * 255, 0.225 * 255]x = x - np.array(MEAN_RGB)x = x / np.array(STDDEV_RGB)return xdef smooth_labels(y, smooth_factor=0.1):assert len(y.shape) == 2if 0 <= smooth_factor <= 1:y *= 1 - smooth_factory += smooth_factor / y.shape[1]else:raise Exception('Invalid label smoothing factor: ' + str(smooth_factor))return ydef data_from_sequence(train_data_dir, batch_size, num_classes, input_size):"""读取本地数据到sequence:param train_data_dir: 训练数据目录:param batch_size: 批次大小:param num_classes: 总类别书40:param input_size: 输入图片大小(300, 300):return:"""# 1、读取txt文件,打乱文件顺序, .jpg, .txtlabel_files = [os.path.join(train_data_dir, filename) for filenamein os.listdir(train_data_dir) if filename.endswith('.txt')]random.shuffle(label_files)# 2、解析txt文件当中 特征值以及目标值(标签)img_paths = []labels = []for index, file_path in enumerate(label_files):with open(file_path, 'r') as f:line = f.readline()line_split = line.strip().split(', ')# line '*.jpg, 0'if len(line_split) != 2:print("% 文件格式出错", (file_path))continueimg_name = line_split[0]label = int(line_split[1])# 最后保存到所有的列表当中img_paths.append(os.path.join(train_data_dir, img_name))labels.append(label)# print(img_paths, labels)# 3、目标标签类别ont_hot编码转换, 平滑处理labels = to_categorical(labels, num_classes)labels = smooth_labels(labels)# 分割训练集合验证集合train_img_paths, validation_img_paths, train_labels, validation_labels \= train_test_split(img_paths, labels, test_size=0.15, random_state=0)# print(validation_img_paths)# print(train_labels, validation_labels)print("总共样本数: %d , 训练样本数: %d, 验证样本数: %d" % (len(img_paths), len(train_img_paths), len(validation_img_paths)))# 4、Sequence调用测试train_sequence = GarbageDataSequence(train_img_paths, train_labels, batch_size, [input_size, input_size], use_aug=True)validation_sequence = GarbageDataSequence(validation_img_paths, validation_labels, batch_size, [input_size, input_size], use_aug=False)return train_sequence, validation_sequenceif __name__ == '__main__':train_data_dir = "../data/garbage_classify/train_data"batch_size = 32train_sequence, validation_sequence = data_from_sequence(train_data_dir, batch_size, num_classes=40, input_size=300)for i in range(100):print("第 %d 批次数据" % i)batch_x, batch_y = train_sequence.__getitem__(i)print(batch_x, batch_y)

data_gen文件夹 random_eraser.py

import numpy as np
import tensorflow as tfdef get_random_eraser(p=0.5, s_l=0.02, s_h=0.4, r_1=0.3, r_2=1/0.3, v_l=0, v_h=255, pixel_level=False):def eraser(input_img):img_h, img_w, img_c = input_img.shapep_1 = np.random.rand()if p_1 > p:return input_imgwhile True:s = np.random.uniform(s_l, s_h) * img_h * img_wr = np.random.uniform(r_1, r_2)w = int(np.sqrt(s / r))h = int(np.sqrt(s * r))left = np.random.randint(0, img_w)top = np.random.randint(0, img_h)if left + w <= img_w and top + h <= img_h:breakif pixel_level:c = np.random.uniform(v_l, v_h, (h, w, img_c))else:c = np.random.uniform(v_l, v_h)input_img[top:top + h, left:left + w, :] = creturn input_imgreturn eraser

utils文件夹 lr_scheduler.py

import numpy as np
import tensorflow as tf
from tensorflow.keras import backend as K
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2"def cosine_decay_with_warmup(global_step,learning_rate_base,total_steps,warmup_learning_rate=0.0,warmup_steps=0,hold_base_rate_steps=0):"""每批次带有warmup余弦退火学习率计算:param global_step: 当前到达的步数:param learning_rate_base: warmup之后的基础学习率:param total_steps: 总需要批次数:param warmup_learning_rate: warmup开始的学习率:param warmup_steps:warmup学习率 步数:param hold_base_rate_steps: 预留总步数和warmup步数间隔:return:"""if total_steps < warmup_steps:raise ValueError("总步数要大于wamup步数")# 1、余弦退火学习率计算learning_rate = 0.5 * learning_rate_base * (1 + np.cos(np.pi * (global_step - warmup_steps - hold_base_rate_steps) / float(total_steps - warmup_steps - hold_base_rate_steps)))# 2、warmup之后的学习率计算# 预留步数阶段# 如果预留大于0,判断目前步数是否 > warmup步数+预留步数,是的话返回刚才上面计算的学习率,不是的话使用warmup之后的基础学习率learning_rate = np.where(global_step > warmup_steps + hold_base_rate_steps, learning_rate, learning_rate_base)# 3、warmup学习率计算,并判断大小# 第一个阶段的学习率计算if warmup_steps > 0:if learning_rate_base < warmup_learning_rate:raise ValueError("第二阶段学习率要大于第一阶段学习率")slope = (learning_rate_base - warmup_learning_rate) / warmup_stepswarmup_rate = slope * global_step + warmup_learning_ratelearning_rate = np.where(global_step < warmup_steps, warmup_rate, learning_rate)# 4、如果最后当前到达的步数大于总步数,则归0,否则返回当前的计算出来的学习率(可能是warmup学习率也可能是余弦衰减结果)return np.where(global_step > total_steps, 0.0, learning_rate)class WarmUpCosineDecayScheduler(tf.keras.callbacks.Callback):"""带有warnup的余弦退火学习率实现"""def __init__(self,learning_rate_base,total_steps,global_step_init=0,warmup_learning_rate=0.0,warmup_steps=0,hold_base_rate_steps=0,verbose=0):super(WarmUpCosineDecayScheduler, self).__init__()self.learning_rate_base = learning_rate_baseself.total_steps = total_stepsself.global_step = global_step_initself.warmup_learning_rate = warmup_learning_rateself.warmup_steps = warmup_stepsself.hold_base_rate_steps = hold_base_rate_steps# 是否在每次训练结束打印学习率self.verbose = verbose# 记录所有批次下来的每次准确的学习率,可以用于打印显示self.learning_rates = []def on_batch_end(self, batch, logs=None):# 记录当前训练到走到第几步数self.global_step = self.global_step + 1# 记录下所有每次的学习到列表,要统计画图可以使用lr = K.get_value(self.model.optimizer.lr)self.learning_rates.append(lr)def on_batch_begin(self, batch, logs=None):# 计算这批次开始的学习率 lrlr = cosine_decay_with_warmup(global_step=self.global_step,learning_rate_base=self.learning_rate_base,total_steps=self.total_steps,warmup_learning_rate=self.warmup_learning_rate,warmup_steps=self.warmup_steps,hold_base_rate_steps=self.hold_base_rate_steps)# 设置模型的学习率为lrK.set_value(self.model.optimizer.lr, lr)if self.verbose > 0:print('\n批次数 %05d: 设置学习率为'' %s.' % (self.global_step + 1, lr))if __name__ == '__main__':# 1、创建模型model = Sequential()model.add(Dense(32, activation='relu', input_dim=100))model.add(Dense(10, activation='softmax'))model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])# 2、参数设置sample_count = 1000  # 样本数epochs = 4  # 总迭代次数warmup_epoch = 3  # warmup 迭代次数batch_size = 16  # 批次大小learning_rate_base = 0.0001  # warmup后的初始学习率total_steps = int(epochs * sample_count / batch_size)  # 总迭代批次步数warmup_steps = int(warmup_epoch * sample_count / batch_size)  # warmup总批次数# 3、创建测试数据data = np.random.random((sample_count, 100))labels = np.random.randint(10, size=(sample_count, 1))# 转换目标类别one_hot_labels = tf.keras.utils.to_categorical(labels, num_classes=10)# 5、创建余弦warmup调度器warm_up_lr = WarmUpCosineDecayScheduler(learning_rate_base=learning_rate_base,total_steps=total_steps,warmup_learning_rate=4e-06,  # warmup开始学习率warmup_steps=warmup_steps,hold_base_rate_steps=0,)# 训练模型model.fit(data, one_hot_labels, epochs=epochs, batch_size=batch_size, verbose=0, callbacks=[warm_up_lr])print(warm_up_lr.learning_rates)

EfficientNet模型B0~B7 源码

initializers.py

import numpy as np
import tensorflow as tf
import keras.backend as Kfrom keras.initializers import Initializer
from keras.utils.generic_utils import get_custom_objectsclass EfficientConv2DKernelInitializer(Initializer):"""Initialization for convolutional kernels.The main difference with tf.variance_scaling_initializer is thattf.variance_scaling_initializer uses a truncated normal with an uncorrectedstandard deviation, whereas here we use a normal distribution. Similarly,tf.contrib.layers.variance_scaling_initializer uses a truncated normal witha corrected standard deviation.Args:shape: shape of variabledtype: dtype of variablepartition_info: unusedReturns:an initialization for the variable"""def __call__(self, shape, dtype=K.floatx(), **kwargs):kernel_height, kernel_width, _, out_filters = shapefan_out = int(kernel_height * kernel_width * out_filters)return tf.random_normal(shape, mean=0.0, stddev=np.sqrt(2.0 / fan_out), dtype=dtype)class EfficientDenseKernelInitializer(Initializer):"""Initialization for dense kernels.This initialization is equal totf.variance_scaling_initializer(scale=1.0/3.0, mode='fan_out',distribution='uniform').It is written out explicitly here for clarity.Args:shape: shape of variabledtype: dtype of variableReturns:an initialization for the variable"""def __call__(self, shape, dtype=K.floatx(), **kwargs):"""Initialization for dense kernels.This initialization is equal totf.variance_scaling_initializer(scale=1.0/3.0, mode='fan_out',distribution='uniform').It is written out explicitly here for clarity.Args:shape: shape of variabledtype: dtype of variableReturns:an initialization for the variable"""init_range = 1.0 / np.sqrt(shape[1])return tf.random_uniform(shape, -init_range, init_range, dtype=dtype)conv_kernel_initializer = EfficientConv2DKernelInitializer()
dense_kernel_initializer = EfficientDenseKernelInitializer()get_custom_objects().update({'EfficientDenseKernelInitializer': EfficientDenseKernelInitializer,'EfficientConv2DKernelInitializer': EfficientConv2DKernelInitializer,
})

 

layers.py

"""
import tensorflow as tf
import numpy as np

x=np.array([[1.,8.,7.],[10.,14.,3.],[1.,2.,4.]])
tf.math.sigmoid(x)
<tf.Tensor: id=22, shape=(3, 3), dtype=float64, numpy=
array([[0.73105858, 0.99966465, 0.99908895],
[0.9999546 , 0.99999917, 0.95257413],
[0.73105858, 0.88079708, 0.98201379]])>

tf.compat.v1.disable_eager_execution()
x=np.array([[1.,8.,7.],[10.,14.,3.],[1.,2.,4.]])     
tf.nn.swish(x)
----------------------------------------------------------------
1.tf.nn.swish(x) 等同于把 x * tf.sigmoid(beta * x) 封装了。
如果使用了tf.nn.swish(x) 则需要同时使用tf.compat.v1.disable_eager_execution()。
如果使用x * tf.sigmoid(beta * x)来代替tf.nn.swish(x)的话,则可以不使用tf.compat.v1.disable_eager_execution()。
2.但注意此处可能环境问题使用tf.nn.swish(x)的话会报错,所以此处使用x * tf.sigmoid(beta * x)来代替tf.nn.swish(x)
报错信息如下:
tensorflow/core/grappler/utils/graph_view.cc:830] No registered 'swish_f32' OpKernel for GPU devices compatible with node
{{node swish_75/swish_f32}}  Registered:  <no registered kernels>
"""

import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as KL
from tensorflow.keras.utils import get_custom_objects"""
import tensorflow as tf
import numpy as npx=np.array([[1.,8.,7.],[10.,14.,3.],[1.,2.,4.]])
tf.math.sigmoid(x)<tf.Tensor: id=22, shape=(3, 3), dtype=float64, numpy=array([[0.73105858, 0.99966465, 0.99908895],[0.9999546 , 0.99999917, 0.95257413],[0.73105858, 0.88079708, 0.98201379]])>tf.compat.v1.disable_eager_execution()
x=np.array([[1.,8.,7.],[10.,14.,3.],[1.,2.,4.]])	 
tf.nn.swish(x)
----------------------------------------------------------------
1.tf.nn.swish(x) 等同于把 x * tf.sigmoid(beta * x) 封装了。如果使用了tf.nn.swish(x) 则需要同时使用tf.compat.v1.disable_eager_execution()。如果使用x * tf.sigmoid(beta * x)来代替tf.nn.swish(x)的话,则可以不使用tf.compat.v1.disable_eager_execution()。
2.但注意此处可能环境问题使用tf.nn.swish(x)的话会报错,所以此处使用x * tf.sigmoid(beta * x)来代替tf.nn.swish(x)报错信息如下:tensorflow/core/grappler/utils/graph_view.cc:830] No registered 'swish_f32' OpKernel for GPU devices compatible with node{{node swish_75/swish_f32}}  Registered:  <no registered kernels>
"""
class Swish(KL.Layer):def __init__(self, **kwargs):super().__init__(**kwargs)def call(self, inputs, **kwargs):# return tf.nn.swish(inputs)return inputs * tf.math.sigmoid(inputs)class DropConnect(KL.Layer):def __init__(self, drop_connect_rate=0., **kwargs):super().__init__(**kwargs)self.drop_connect_rate = drop_connect_ratedef call(self, inputs, training=None):def drop_connect():keep_prob = 1.0 - self.drop_connect_rate# Compute drop_connect tensorbatch_size = tf.shape(inputs)[0]random_tensor = keep_probrandom_tensor += tf.random.uniform([batch_size, 1, 1, 1], dtype=inputs.dtype)binary_tensor = tf.floor(random_tensor)output = tf.math.divide(inputs, keep_prob) * binary_tensorreturn outputreturn K.in_train_phase(drop_connect, inputs, training=training)def get_config(self):config = super().get_config()config['drop_connect_rate'] = self.drop_connect_ratereturn configget_custom_objects().update({'DropConnect': DropConnect,'Swish': Swish,
})

 

model.py

# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Contains definitions for EfficientNet model.
[1] Mingxing Tan, Quoc V. LeEfficientNet: Rethinking Model Scaling for Convolutional Neural Networks.ICML'19, https://arxiv.org/abs/1905.11946
"""from __future__ import absolute_import
from __future__ import division
from __future__ import print_functionimport collections
import math
import numpy as np
import six
from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tfimport tensorflow.keras.backend as K
import tensorflow.keras.models as KM
import tensorflow.keras.layers as KL
from tensorflow.keras.utils import get_file
from tensorflow.keras.initializers import Initializer
from .layers import Swish, DropConnect
from .params import get_model_params, IMAGENET_WEIGHTS
from .initializers import conv_kernel_initializer, dense_kernel_initializer__all__ = ['EfficientNet', 'EfficientNetB0', 'EfficientNetB1', 'EfficientNetB2', 'EfficientNetB3','EfficientNetB4', 'EfficientNetB5', 'EfficientNetB6', 'EfficientNetB7']class ConvKernalInitializer(Initializer):def __call__(self, shape, dtype=K.floatx(), partition_info=None):"""Initialization for convolutional kernels.The main difference with tf.variance_scaling_initializer is thattf.variance_scaling_initializer uses a truncated normal with an uncorrectedstandard deviation, whereas here we use a normal distribution. Similarly,tf.contrib.layers.variance_scaling_initializer uses a truncated normal witha corrected standard deviation.Args:shape: shape of variabledtype: dtype of variablepartition_info: unusedReturns:an initialization for the variable"""del partition_infokernel_height, kernel_width, _, out_filters = shapefan_out = int(kernel_height * kernel_width * out_filters)return tf.random.normal(shape, mean=0.0, stddev=np.sqrt(2.0 / fan_out), dtype=dtype)class DenseKernalInitializer(Initializer):def __call__(self, shape, dtype=K.floatx(), partition_info=None):"""Initialization for dense kernels.This initialization is equal totf.variance_scaling_initializer(scale=1.0/3.0, mode='fan_out',distribution='uniform').It is written out explicitly here for clarity.Args:shape: shape of variabledtype: dtype of variablepartition_info: unusedReturns:an initialization for the variable"""del partition_infoinit_range = 1.0 / np.sqrt(shape[1])return tf.random_uniform(shape, -init_range, init_range, dtype=dtype)def round_filters(filters, global_params):"""Round number of filters based on depth multiplier."""orig_f = filtersmultiplier = global_params.width_coefficientdivisor = global_params.depth_divisormin_depth = global_params.min_depthif not multiplier:return filtersfilters *= multipliermin_depth = min_depth or divisornew_filters = max(min_depth, int(filters + divisor / 2) // divisor * divisor)# Make sure that round down does not go down by more than 10%.if new_filters < 0.9 * filters:new_filters += divisor# print('round_filter input={} output={}'.format(orig_f, new_filters))return int(new_filters)def round_repeats(repeats, global_params):"""Round number of filters based on depth multiplier."""multiplier = global_params.depth_coefficientif not multiplier:return repeatsreturn int(math.ceil(multiplier * repeats))def SEBlock(block_args, global_params):num_reduced_filters = max(1, int(block_args.input_filters * block_args.se_ratio))filters = block_args.input_filters * block_args.expand_ratioif global_params.data_format == 'channels_first':channel_axis = 1spatial_dims = [2, 3]else:channel_axis = -1spatial_dims = [1, 2]def block(inputs):x = inputsx = KL.Lambda(lambda a: K.mean(a, axis=spatial_dims, keepdims=True))(x)x = KL.Conv2D(num_reduced_filters,kernel_size=[1, 1],strides=[1, 1],kernel_initializer=ConvKernalInitializer(),padding='same',use_bias=True)(x)x = Swish()(x)# Excitex = KL.Conv2D(filters,kernel_size=[1, 1],strides=[1, 1],kernel_initializer=ConvKernalInitializer(),padding='same',use_bias=True)(x)x = KL.Activation('sigmoid')(x)out = KL.Multiply()([x, inputs])return outreturn blockdef MBConvBlock(block_args, global_params, drop_connect_rate=None):batch_norm_momentum = global_params.batch_norm_momentumbatch_norm_epsilon = global_params.batch_norm_epsilonif global_params.data_format == 'channels_first':channel_axis = 1spatial_dims = [2, 3]else:channel_axis = -1spatial_dims = [1, 2]has_se = (block_args.se_ratio is not None) and (block_args.se_ratio > 0) and (block_args.se_ratio <= 1)filters = block_args.input_filters * block_args.expand_ratiokernel_size = block_args.kernel_sizedef block(inputs):if block_args.expand_ratio != 1:x = KL.Conv2D(filters,kernel_size=[1, 1],strides=[1, 1],kernel_initializer=ConvKernalInitializer(),padding='same',use_bias=False)(inputs)x = KL.BatchNormalization(axis=channel_axis,momentum=batch_norm_momentum,epsilon=batch_norm_epsilon)(x)x = Swish()(x)else:x = inputsx = KL.DepthwiseConv2D([kernel_size, kernel_size],strides=block_args.strides,depthwise_initializer=ConvKernalInitializer(),padding='same',use_bias=False)(x)x = KL.BatchNormalization(axis=channel_axis,momentum=batch_norm_momentum,epsilon=batch_norm_epsilon)(x)x = Swish()(x)if has_se:x = SEBlock(block_args, global_params)(x)# output phasex = KL.Conv2D(block_args.output_filters,kernel_size=[1, 1],strides=[1, 1],kernel_initializer=ConvKernalInitializer(),padding='same',use_bias=False)(x)x = KL.BatchNormalization(axis=channel_axis,momentum=batch_norm_momentum,epsilon=batch_norm_epsilon)(x)if block_args.id_skip:if all(s == 1 for s in block_args.strides) and block_args.input_filters == block_args.output_filters:# only apply drop_connect if skip presents.if drop_connect_rate:x = DropConnect(drop_connect_rate)(x)x = KL.Add()([x, inputs])return xreturn blockdef EfficientNet(input_shape, block_args_list, global_params, include_top=True, pooling=None):batch_norm_momentum = global_params.batch_norm_momentumbatch_norm_epsilon = global_params.batch_norm_epsilonif global_params.data_format == 'channels_first':channel_axis = 1else:channel_axis = -1# Stem partinputs = KL.Input(shape=input_shape)x = inputsx = KL.Conv2D(filters=round_filters(32, global_params),kernel_size=[3, 3],strides=[2, 2],kernel_initializer=ConvKernalInitializer(),padding='same',use_bias=False)(x)x = KL.BatchNormalization(axis=channel_axis,momentum=batch_norm_momentum,epsilon=batch_norm_epsilon)(x)x = Swish()(x)# Blocks partblock_idx = 1n_blocks = sum([block_args.num_repeat for block_args in block_args_list])drop_rate = global_params.drop_connect_rate or 0drop_rate_dx = drop_rate / n_blocksfor block_args in block_args_list:assert block_args.num_repeat > 0# Update block input and output filters based on depth multiplier.block_args = block_args._replace(input_filters=round_filters(block_args.input_filters, global_params),output_filters=round_filters(block_args.output_filters, global_params),num_repeat=round_repeats(block_args.num_repeat, global_params))# The first block needs to take care of stride and filter size increase.x = MBConvBlock(block_args, global_params,drop_connect_rate=drop_rate_dx * block_idx)(x)block_idx += 1if block_args.num_repeat > 1:block_args = block_args._replace(input_filters=block_args.output_filters, strides=[1, 1])for _ in xrange(block_args.num_repeat - 1):x = MBConvBlock(block_args, global_params,drop_connect_rate=drop_rate_dx * block_idx)(x)block_idx += 1# Head partx = KL.Conv2D(filters=round_filters(1280, global_params),kernel_size=[1, 1],strides=[1, 1],kernel_initializer=ConvKernalInitializer(),padding='same',use_bias=False)(x)x = KL.BatchNormalization(axis=channel_axis,momentum=batch_norm_momentum,epsilon=batch_norm_epsilon)(x)x = Swish()(x)if include_top:x = KL.GlobalAveragePooling2D(data_format=global_params.data_format)(x)if global_params.dropout_rate > 0:x = KL.Dropout(global_params.dropout_rate)(x)x = KL.Dense(global_params.num_classes, kernel_initializer=DenseKernalInitializer())(x)x = KL.Activation('softmax')(x)else:if pooling == 'avg':x = KL.GlobalAveragePooling2D(data_format=global_params.data_format)(x)elif pooling == 'max':x = KL.GlobalMaxPooling2D(data_format=global_params.data_format)(x)outputs = xmodel = KM.Model(inputs, outputs)return modeldef _get_model_by_name(model_name, input_shape=None, include_top=True, weights=None, classes=1000, pooling=None):"""Re-Implementation of EfficientNet for KerasReference:https://arxiv.org/abs/1807.11626Args:input_shape: optional, if ``None`` default_input_shape is usedEfficientNetB0 - (224, 224, 3)EfficientNetB1 - (240, 240, 3)EfficientNetB2 - (260, 260, 3)EfficientNetB3 - (300, 300, 3)EfficientNetB4 - (380, 380, 3)EfficientNetB5 - (456, 456, 3)EfficientNetB6 - (528, 528, 3)EfficientNetB7 - (600, 600, 3)include_top: whether to include the fully-connectedlayer at the top of the network.weights: one of `None` (random initialization),'imagenet' (pre-training on ImageNet).classes: optional number of classes to classify imagesinto, only to be specified if `include_top` is True, andif no `weights` argument is specified.pooling: optional [None, 'avg', 'max'], if ``include_top=False``add global pooling on top of the network- avg: GlobalAveragePooling2D- max: GlobalMaxPooling2DReturns:A Keras model instance."""if weights not in {None, 'imagenet'}:raise ValueError('Parameter `weights` should be one of [None, "imagenet"]')if weights == 'imagenet' and model_name not in IMAGENET_WEIGHTS:raise ValueError('There are not pretrained weights for {} model.'.format(model_name))if weights == 'imagenet' and include_top and classes != 1000:raise ValueError('If using `weights` and `include_top`'' `classes` should be 1000')block_agrs_list, global_params, default_input_shape = get_model_params(model_name, override_params={'num_classes': classes})if input_shape is None:input_shape = (default_input_shape, default_input_shape, 3)model = EfficientNet(input_shape, block_agrs_list, global_params, include_top=include_top, pooling=pooling)model._name = model_nameif weights:if not include_top:weights_name = model_name + '-notop'else:weights_name = model_nameweights = IMAGENET_WEIGHTS[weights_name]weights_path = get_file(weights['name'],weights['url'],cache_subdir='models',md5_hash=weights['md5'],)model.load_weights(weights_path)return modeldef EfficientNetB0(include_top=True, input_shape=None, weights=None, classes=1000, pooling=None):return _get_model_by_name('efficientnet-b0', include_top=include_top, input_shape=input_shape,weights=weights, classes=classes, pooling=pooling)def EfficientNetB1(include_top=True, input_shape=None, weights=None, classes=1000, pooling=None):return _get_model_by_name('efficientnet-b1', include_top=include_top, input_shape=input_shape,weights=weights, classes=classes, pooling=pooling)def EfficientNetB2(include_top=True, input_shape=None, weights=None, classes=1000, pooling=None):return _get_model_by_name('efficientnet-b2', include_top=include_top, input_shape=input_shape,weights=weights, classes=classes, pooling=pooling)def EfficientNetB3(include_top=True, input_shape=None, weights=None, classes=1000, pooling=None):return _get_model_by_name('efficientnet-b3', include_top=include_top, input_shape=input_shape,weights=weights, classes=classes, pooling=pooling)def EfficientNetB4(include_top=True, input_shape=None, weights=None, classes=1000, pooling=None):return _get_model_by_name('efficientnet-b4', include_top=include_top, input_shape=input_shape,weights=weights, classes=classes, pooling=pooling)def EfficientNetB5(include_top=True, input_shape=None, weights=None, classes=1000, pooling=None):return _get_model_by_name('efficientnet-b5', include_top=include_top, input_shape=input_shape,weights=weights, classes=classes, pooling=pooling)def EfficientNetB6(include_top=True, input_shape=None, weights=None, classes=1000, pooling=None):return _get_model_by_name('efficientnet-b6', include_top=include_top, input_shape=input_shape,weights=weights, classes=classes, pooling=pooling)def EfficientNetB7(include_top=True, input_shape=None, weights=None, classes=1000, pooling=None):return _get_model_by_name('efficientnet-b7', include_top=include_top, input_shape=input_shape,weights=weights, classes=classes, pooling=pooling)EfficientNetB0.__doc__ = _get_model_by_name.__doc__
EfficientNetB1.__doc__ = _get_model_by_name.__doc__
EfficientNetB2.__doc__ = _get_model_by_name.__doc__
EfficientNetB3.__doc__ = _get_model_by_name.__doc__
EfficientNetB4.__doc__ = _get_model_by_name.__doc__
EfficientNetB5.__doc__ = _get_model_by_name.__doc__
EfficientNetB6.__doc__ = _get_model_by_name.__doc__
EfficientNetB7.__doc__ = _get_model_by_name.__doc__

 

params.py

import os
import re
import collectionsIMAGENET_WEIGHTS = {'efficientnet-b0': {'name': 'efficientnet-b0_imagenet_1000.h5','url': 'https://github.com/qubvel/efficientnet/releases/download/v0.0.1/efficientnet-b0_imagenet_1000.h5','md5': 'bca04d16b1b8a7c607b1152fe9261af7',},'efficientnet-b0-notop': {'name': 'efficientnet-b0_imagenet_1000_notop.h5','url': 'https://github.com/qubvel/efficientnet/releases/download/v0.0.1/efficientnet-b0_imagenet_1000_notop.h5','md5': '45d2f3b6330c2401ef66da3961cad769',},'efficientnet-b1': {'name': 'efficientnet-b1_imagenet_1000.h5','url': 'https://github.com/qubvel/efficientnet/releases/download/v0.0.1/efficientnet-b1_imagenet_1000.h5','md5': 'bd4a2b82f6f6bada74fc754553c464fc',},'efficientnet-b1-notop': {'name': 'efficientnet-b1_imagenet_1000_notop.h5','url': 'https://github.com/qubvel/efficientnet/releases/download/v0.0.1/efficientnet-b1_imagenet_1000_notop.h5','md5': '884aed586c2d8ca8dd15a605ec42f564',},'efficientnet-b2': {'name': 'efficientnet-b2_imagenet_1000.h5','url': 'https://github.com/qubvel/efficientnet/releases/download/v0.0.1/efficientnet-b2_imagenet_1000.h5','md5': '45b28b26f15958bac270ab527a376999',},'efficientnet-b2-notop': {'name': 'efficientnet-b2_imagenet_1000_notop.h5','url': 'https://github.com/qubvel/efficientnet/releases/download/v0.0.1/efficientnet-b2_imagenet_1000_notop.h5','md5': '42fb9f2d9243d461d62b4555d3a53b7b',},'efficientnet-b3': {'name': 'efficientnet-b3_imagenet_1000.h5','url': 'https://github.com/qubvel/efficientnet/releases/download/v0.0.1/efficientnet-b3_imagenet_1000.h5','md5': 'decd2c8a23971734f9d3f6b4053bf424',},'efficientnet-b3-notop': {'name': 'efficientnet-b3_imagenet_1000_notop.h5','url': 'https://github.com/qubvel/efficientnet/releases/download/v0.0.1/efficientnet-b3_imagenet_1000_notop.h5','md5': '1f7d9a8c2469d2e3d3b97680d45df1e1',},}GlobalParams = collections.namedtuple('GlobalParams', ['batch_norm_momentum', 'batch_norm_epsilon', 'dropout_rate', 'data_format','num_classes', 'width_coefficient', 'depth_coefficient','depth_divisor', 'min_depth', 'drop_connect_rate',
])
GlobalParams.__new__.__defaults__ = (None,) * len(GlobalParams._fields)BlockArgs = collections.namedtuple('BlockArgs', ['kernel_size', 'num_repeat', 'input_filters', 'output_filters','expand_ratio', 'id_skip', 'strides', 'se_ratio'
])
# defaults will be a public argument for namedtuple in Python 3.7
# https://docs.python.org/3/library/collections.html#collections.namedtuple
BlockArgs.__new__.__defaults__ = (None,) * len(BlockArgs._fields)def efficientnet_params(model_name):"""Get efficientnet params based on model name."""params_dict = {# (width_coefficient, depth_coefficient, resolution, dropout_rate)'efficientnet-b0': (1.0, 1.0, 224, 0.2),'efficientnet-b1': (1.0, 1.1, 240, 0.2),'efficientnet-b2': (1.1, 1.2, 260, 0.3),'efficientnet-b3': (1.2, 1.4, 300, 0.3),'efficientnet-b4': (1.4, 1.8, 380, 0.4),'efficientnet-b5': (1.6, 2.2, 456, 0.4),'efficientnet-b6': (1.8, 2.6, 528, 0.5),'efficientnet-b7': (2.0, 3.1, 600, 0.5),}return params_dict[model_name]class BlockDecoder(object):"""Block Decoder for readability."""def _decode_block_string(self, block_string):"""Gets a block through a string notation of arguments."""assert isinstance(block_string, str)ops = block_string.split('_')options = {}for op in ops:splits = re.split(r'(\d.*)', op)if len(splits) >= 2:key, value = splits[:2]options[key] = valueif 's' not in options or len(options['s']) != 2:raise ValueError('Strides options should be a pair of integers.')return BlockArgs(kernel_size=int(options['k']),num_repeat=int(options['r']),input_filters=int(options['i']),output_filters=int(options['o']),expand_ratio=int(options['e']),id_skip=('noskip' not in block_string),se_ratio=float(options['se']) if 'se' in options else None,strides=[int(options['s'][0]), int(options['s'][1])])def _encode_block_string(self, block):"""Encodes a block to a string."""args = ['r%d' % block.num_repeat,'k%d' % block.kernel_size,'s%d%d' % (block.strides[0], block.strides[1]),'e%s' % block.expand_ratio,'i%d' % block.input_filters,'o%d' % block.output_filters]if block.se_ratio > 0 and block.se_ratio <= 1:args.append('se%s' % block.se_ratio)if block.id_skip is False:args.append('noskip')return '_'.join(args)def decode(self, string_list):"""Decodes a list of string notations to specify blocks inside the network.Args:string_list: a list of strings, each string is a notation of block.Returns:A list of namedtuples to represent blocks arguments."""assert isinstance(string_list, list)blocks_args = []for block_string in string_list:blocks_args.append(self._decode_block_string(block_string))return blocks_argsdef encode(self, blocks_args):"""Encodes a list of Blocks to a list of strings.Args:blocks_args: A list of namedtuples to represent blocks arguments.Returns:a list of strings, each string is a notation of block."""block_strings = []for block in blocks_args:block_strings.append(self._encode_block_string(block))return block_stringsdef efficientnet(width_coefficient=None,depth_coefficient=None,dropout_rate=0.2,drop_connect_rate=0.2):"""Creates a efficientnet model."""blocks_args = ['r1_k3_s11_e1_i32_o16_se0.25', 'r2_k3_s22_e6_i16_o24_se0.25','r2_k5_s22_e6_i24_o40_se0.25', 'r3_k3_s22_e6_i40_o80_se0.25','r3_k5_s11_e6_i80_o112_se0.25', 'r4_k5_s22_e6_i112_o192_se0.25','r1_k3_s11_e6_i192_o320_se0.25',]global_params = GlobalParams(batch_norm_momentum=0.99,batch_norm_epsilon=1e-3,dropout_rate=dropout_rate,drop_connect_rate=drop_connect_rate,data_format='channels_last',num_classes=1000,width_coefficient=width_coefficient,depth_coefficient=depth_coefficient,depth_divisor=8,min_depth=None)decoder = BlockDecoder()return decoder.decode(blocks_args), global_paramsdef get_model_params(model_name, override_params=None):"""Get the block args and global params for a given model."""if model_name.startswith('efficientnet'):width_coefficient, depth_coefficient, input_shape, dropout_rate = (efficientnet_params(model_name))blocks_args, global_params = efficientnet(width_coefficient, depth_coefficient, dropout_rate)else:raise NotImplementedError('model name is not pre-defined: %s' % model_name)if override_params:# ValueError will be raised here if override_params has fields not included# in global_params.global_params = global_params._replace(**override_params)#print('global_params= %s', global_params)#print('blocks_args= %s', blocks_args)return blocks_args, global_params, input_shape

 

preprocessing.py

import numpy as np
from skimage.transform import resizeMEAN_RGB = [0.485 * 255, 0.456 * 255, 0.406 * 255]
STDDEV_RGB = [0.229 * 255, 0.224 * 255, 0.225 * 255]MAP_INTERPOLATION_TO_ORDER = {'nearest': 0,'bilinear': 1,'biquadratic': 2,'bicubic': 3,
}def center_crop_and_resize(image, image_size, crop_padding=32, interpolation='bicubic'):assert image.ndim in {2, 3}assert interpolation in MAP_INTERPOLATION_TO_ORDER.keys()h, w = image.shape[:2]padded_center_crop_size = int((image_size / (image_size + crop_padding)) * min(h, w))offset_height = ((h - padded_center_crop_size) + 1) // 2offset_width = ((w - padded_center_crop_size) + 1) // 2image_crop = image[offset_height:padded_center_crop_size + offset_height,offset_width:padded_center_crop_size + offset_width]resized_image = resize(image_crop,(image_size, image_size),order=MAP_INTERPOLATION_TO_ORDER[interpolation],preserve_range=True,)return resized_imagedef preprocess_input(x):assert x.ndim in (3, 4)assert x.shape[-1] == 3x = x - np.array(MEAN_RGB)x = x / np.array(STDDEV_RGB)return x

warmup以及余弦退火学习率原理图

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

查看全文
如若内容造成侵权/违法违规/事实不符,请联系编程学习网邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

相关文章

  1. 计算机组成原理————通道

    经常用于大型计算机的控制方式是通道方式。通道方式,即能够根据程序控制多个外部设备并提供了DMA共享的功能,而DMA只能进行固定的数据传输操作。根据数据传送方式,通道可分成字节多路通道、选择通道和数组多路通道三种类型DMA专用通道处理器与设备(大型计算机),是通过通道…...

    2024/4/30 17:00:27
  2. UnsupportedClassVersionError

    linux调好了代码,配好了依赖,并写好了 shell 启动脚本。测试成功 。 之后通过windows 的idea 写java 调用 linux 上 shell 脚本执行程序 报错信息如下: Exception in thread "main" java.lang.UnsupportedClassVersionError: com/boco/querymr/task/ActiveDomain…...

    2024/4/30 21:28:53
  3. leetcode 136. 只出现一次的数字

    【题目】 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。 说明:你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗? 示例 1: 输入: [2,2,1] 输出: 1示例 2: 输入: [4,1,2,1,2] 输出: 4【解题思路1】…...

    2024/4/30 20:46:46
  4. 齐次变换矩阵(改进型)

    相对于参考坐标系是左乘,相对于当前坐标系是右成,因此图中的就可以理解为,i-1坐标系沿xi-1旋转到R坐标,再基于R坐标沿着杆长方向(ai-1)移动到Q坐标,再基于Q坐标沿zi旋转到P坐标,P坐标再沿着di移动到zi坐标,所以是相当于右乘。 值得注意的是,在整个某个p在zi的坐标系下…...

    2024/4/30 22:15:26
  5. cadence allegro番外:制作含表贴焊盘封装

    前言Allegro的封装制作较ad比较为特殊,每个封装步骤如下:1.制作该封装的各种焊盘、插件,以.pad文件保存。2.新建真正的封装文件.dra,将先前做好的.pad导入,随后进行安置和丝印等元素添加。也就是说,要想做封装(.dra)需要先做好焊盘插件(.pad);如果有一个完整的.dra文…...

    2024/4/30 20:41:34
  6. python抓取新浪微博评论并分析

    https://blog.csdn.net/xiaolanzao/article/details/31749621...

    2024/5/1 1:05:20
  7. 计算机组成原理————指令

    1、指令划分为操作码和地址码字段,由二进制数字组成指令系统中采用不同寻址方式的目的主要是缩短指令长度,扩大寻址空间,提高编程灵活性。2、运算型指令的寻址与转移型指令的寻址不同点在于运算型指今寻址的是操作数,而转移性指令寻址的则是下次欲执行的指令的地址。3、零地…...

    2024/5/1 2:35:56
  8. vue学习之MVVM理解

    。。。。此处暂时省略2000字...

    2024/4/30 19:32:14
  9. 假如你忘记了SVN的账号密码怎么办?

    身为程序员,肯定不能再问技术经理去要一次,那么就用这个工具吧: 传送门: http://www.leapbeyond.com/ric/TSvnPD/如图所示,直接下载打开,即可获取到你使用过的全部SVN账号和密码...

    2024/4/30 19:27:06
  10. Chrome不能搜索问题

    Chrome浏览器的概述 Google Chrome是一款由Google公司开发的网页浏览器,该浏览器基于其他开源软件撰写,包括WebKit,目标是提升稳定性、速度和安全性,并创造出简单且有效率的使用者界面,当然基本很多开发者,都喜欢使用谷歌浏览器,原因:我使用Chrome也有一年时间了,我觉…...

    2024/5/1 2:17:26
  11. Github从入门到精通(三)

    上一节我们已经创建了GitHub账号,这一节课我们创建一个属于自己的仓库吧目录创建仓库仓库主页说明仓库管理新建文件修改文件删除文件上传文件搜索仓库文件下载/检出文件 创建仓库仓库主页说明仓库管理 新建文件填写每次提交的目的 原因:为了方便其他开发者知道本次添加或修改…...

    2024/4/23 14:17:13
  12. SSM中多容器/父子容器概念的理解

    概念 Spring框架允许在一个应用中创建多个上下文容器。但是建议容器之间有父子关系。可以通过ConfigurableApplicationContext接口定义的setParent方法设置父容器。一旦设置父子关系,则可以通过子容器获取父容器中除PropertyPlaceHolder以外的所有资源,父容器不能获取子容器中的…...

    2024/4/23 14:17:05
  13. 3月23知识点梳理

    列表属性 1、定义列表符号样式 list-style-type:disc(实心圆)/circle(空心圆)/square(实心方块)/none(去掉列表符号);list-style type:none===list-style:none; 2、使用图片作为列表符号 list-style-image:url(所使用图片的路径及全称); 3、定义列表符号的位置 list-style-…...

    2024/4/23 14:17:05
  14. 大数据-hadoop基本理论知识

    Hadoop基本理论知识HDFS(Hadoop Distributed File System)基于Google发布的论文设计开发 具备其它分布式文件系统相同特性外,还有特有的特性: 高容错性:认为硬件总是不可靠的 高吞吐量:认为大量数据访问的应用提供高吞吐量支持 大文件存储:支持存储TB-PB级别的数据 HDFS适…...

    2024/4/23 14:17:10
  15. let和var

    for (var i = 0; i < 3; i++) {setTimeout(() => {console.log(i) //3,3,3}, 3000)}for (let i = 0; i < 3; i++) {setTimeout(() => {console.log(i) //0,1,2}, 3000)}由于JavaScript中的事件执行机制,setTimeout函数真正被执行时,循环已经走完。 由于第一个循环…...

    2024/4/23 14:17:10
  16. C++中的映射map——小示例

    1、根据key值取value值的方法,和数组根据下标取值一个方法,amap[key]2、根据value取key值,用迭代器从amap.begin()到amap.end()一一遍历,比对iter->second 与 value是否相等3、以下就是一个小例子#include<iostream> #include<string> #include<map> …...

    2024/4/17 8:22:58
  17. EDA数据探索

    EDA数据探索EDA的价值主要在于熟悉数据集,了解数据集,对数据集进行验证来确定所获得数据集可以用于机器学习或者深度学习使用。当了解了数据集之后我们下一步就是要去了解变量间的相互关系以及变量与预测值之间的存在关系。引导数据科学从业者进行数据处理以及特征工程的步骤…...

    2024/4/20 23:09:35
  18. 图的最短路径--单源、多源最短路径

    最短路径 –在网络中,求两个不同顶点之间的所有路径中,边的权值之和最小的路劲。 单源最短路径 –从某固定源点出发的最短路径 无权图的最短路径按照路径长度递增的顺序找出源点到各个顶点的最短路径类似于BFS-宽度优先遍历,可以通过队列来实现,先让顶点入队,循环执行下列…...

    2024/4/20 14:18:27
  19. zsh: command not found问题解决

    出现这个问题,一种的path路径不对,路径不对,修改路径;路径修改传送门:zsh: command not found解决方法:配置环境变量 另一种原因可能是没有该命令的相关安装包: 没有安装包时,采用brew命令安装. 如安装wget brew install wgetps: 如果brew安装出现以下错误: Updating …...

    2024/4/17 8:22:52
  20. BIO,NIO,AIO的区别与选择

    Java 中的 BIO、NIO和 AIO 理解为是 Java 语言对操作系统的各种 IO 模型的封装。程序员在使用这些 API 的时候,不需要关心操作系统层面的知识,也不需要根据不同操作系统编写不同的代码。只需要使用Java的API就可以了。在讲 BIO,NIO,AIO 之前先来回顾一下这样几个概念:同步与…...

    2024/4/23 20:25:45

最新文章

  1. C#逻辑运算符

    C#中逻辑运算符分为: 或、与、非 或||: 对两个bool值进行逻辑运算 有真则真 同假则假 与&&: 对两个布尔值进行运算 有假则假 同真为真 非&#xff01;: 对两个bool值进行取反 真变假 假变真 或 || 符号 &#xff1a; || <u>*对两个bool值进行逻辑运算 有真则…...

    2024/5/1 3:46:02
  2. 梯度消失和梯度爆炸的一些处理方法

    在这里是记录一下梯度消失或梯度爆炸的一些处理技巧。全当学习总结了如有错误还请留言&#xff0c;在此感激不尽。 权重和梯度的更新公式如下&#xff1a; w w − η ⋅ ∇ w w w - \eta \cdot \nabla w ww−η⋅∇w 个人通俗的理解梯度消失就是网络模型在反向求导的时候出…...

    2024/3/20 10:50:27
  3. ASP.NET Core 标识(Identity)框架系列(一):如何使用 ASP.NET Core 标识(Identity)框架创建用户和角色?

    前言 ASP.NET Core 内置的标识&#xff08;identity&#xff09;框架&#xff0c;采用的是 RBAC&#xff08;role-based access control&#xff0c;基于角色的访问控制&#xff09;策略&#xff0c;是一个用于管理用户身份验证、授权和安全性的框架。 它提供了一套工具和库&…...

    2024/4/30 2:01:46
  4. 三防笔记本丨工业笔记本电脑丨车辆检修的应用以及优势

    伴随着汽车技术的不断更新迭代以及车辆复杂性的增加&#xff0c;现代车辆检修工作需要更高效、更精确的方法来确保车辆的安全和性能。在这过程中&#xff0c;工业笔记本电脑作为一种强大的工具&#xff0c;为车辆检修提供了诊断、记录、分析和解决问题的核心功能 故障诊断与维修…...

    2024/4/30 1:52:31
  5. 【外汇早评】美通胀数据走低,美元调整

    原标题:【外汇早评】美通胀数据走低,美元调整昨日美国方面公布了新一期的核心PCE物价指数数据,同比增长1.6%,低于前值和预期值的1.7%,距离美联储的通胀目标2%继续走低,通胀压力较低,且此前美国一季度GDP初值中的消费部分下滑明显,因此市场对美联储后续更可能降息的政策…...

    2024/4/29 23:16:47
  6. 【原油贵金属周评】原油多头拥挤,价格调整

    原标题:【原油贵金属周评】原油多头拥挤,价格调整本周国际劳动节,我们喜迎四天假期,但是整个金融市场确实流动性充沛,大事频发,各个商品波动剧烈。美国方面,在本周四凌晨公布5月份的利率决议和新闻发布会,维持联邦基金利率在2.25%-2.50%不变,符合市场预期。同时美联储…...

    2024/4/30 18:14:14
  7. 【外汇周评】靓丽非农不及疲软通胀影响

    原标题:【外汇周评】靓丽非农不及疲软通胀影响在刚结束的周五,美国方面公布了新一期的非农就业数据,大幅好于前值和预期,新增就业重新回到20万以上。具体数据: 美国4月非农就业人口变动 26.3万人,预期 19万人,前值 19.6万人。 美国4月失业率 3.6%,预期 3.8%,前值 3…...

    2024/4/29 2:29:43
  8. 【原油贵金属早评】库存继续增加,油价收跌

    原标题:【原油贵金属早评】库存继续增加,油价收跌周三清晨公布美国当周API原油库存数据,上周原油库存增加281万桶至4.692亿桶,增幅超过预期的74.4万桶。且有消息人士称,沙特阿美据悉将于6月向亚洲炼油厂额外出售更多原油,印度炼油商预计将每日获得至多20万桶的额外原油供…...

    2024/4/30 18:21:48
  9. 【外汇早评】日本央行会议纪要不改日元强势

    原标题:【外汇早评】日本央行会议纪要不改日元强势近两日日元大幅走强与近期市场风险情绪上升,避险资金回流日元有关,也与前一段时间的美日贸易谈判给日本缓冲期,日本方面对汇率问题也避免继续贬值有关。虽然今日早间日本央行公布的利率会议纪要仍然是支持宽松政策,但这符…...

    2024/4/27 17:58:04
  10. 【原油贵金属早评】欧佩克稳定市场,填补伊朗问题的影响

    原标题:【原油贵金属早评】欧佩克稳定市场,填补伊朗问题的影响近日伊朗局势升温,导致市场担忧影响原油供给,油价试图反弹。此时OPEC表态稳定市场。据消息人士透露,沙特6月石油出口料将低于700万桶/日,沙特已经收到石油消费国提出的6月份扩大出口的“适度要求”,沙特将满…...

    2024/4/27 14:22:49
  11. 【外汇早评】美欲与伊朗重谈协议

    原标题:【外汇早评】美欲与伊朗重谈协议美国对伊朗的制裁遭到伊朗的抗议,昨日伊朗方面提出将部分退出伊核协议。而此行为又遭到欧洲方面对伊朗的谴责和警告,伊朗外长昨日回应称,欧洲国家履行它们的义务,伊核协议就能保证存续。据传闻伊朗的导弹已经对准了以色列和美国的航…...

    2024/4/28 1:28:33
  12. 【原油贵金属早评】波动率飙升,市场情绪动荡

    原标题:【原油贵金属早评】波动率飙升,市场情绪动荡因中美贸易谈判不安情绪影响,金融市场各资产品种出现明显的波动。随着美国与中方开启第十一轮谈判之际,美国按照既定计划向中国2000亿商品征收25%的关税,市场情绪有所平复,已经开始接受这一事实。虽然波动率-恐慌指数VI…...

    2024/4/30 9:43:09
  13. 【原油贵金属周评】伊朗局势升温,黄金多头跃跃欲试

    原标题:【原油贵金属周评】伊朗局势升温,黄金多头跃跃欲试美国和伊朗的局势继续升温,市场风险情绪上升,避险黄金有向上突破阻力的迹象。原油方面稍显平稳,近期美国和OPEC加大供给及市场需求回落的影响,伊朗局势并未推升油价走强。近期中美贸易谈判摩擦再度升级,美国对中…...

    2024/4/27 17:59:30
  14. 【原油贵金属早评】市场情绪继续恶化,黄金上破

    原标题:【原油贵金属早评】市场情绪继续恶化,黄金上破周初中国针对于美国加征关税的进行的反制措施引发市场情绪的大幅波动,人民币汇率出现大幅的贬值动能,金融市场受到非常明显的冲击。尤其是波动率起来之后,对于股市的表现尤其不安。隔夜美国股市出现明显的下行走势,这…...

    2024/4/25 18:39:16
  15. 【外汇早评】美伊僵持,风险情绪继续升温

    原标题:【外汇早评】美伊僵持,风险情绪继续升温昨日沙特两艘油轮再次发生爆炸事件,导致波斯湾局势进一步恶化,市场担忧美伊可能会出现摩擦生火,避险品种获得支撑,黄金和日元大幅走强。美指受中美贸易问题影响而在低位震荡。继5月12日,四艘商船在阿联酋领海附近的阿曼湾、…...

    2024/4/28 1:34:08
  16. 【原油贵金属早评】贸易冲突导致需求低迷,油价弱势

    原标题:【原油贵金属早评】贸易冲突导致需求低迷,油价弱势近日虽然伊朗局势升温,中东地区几起油船被袭击事件影响,但油价并未走高,而是出于调整结构中。由于市场预期局势失控的可能性较低,而中美贸易问题导致的全球经济衰退风险更大,需求会持续低迷,因此油价调整压力较…...

    2024/4/26 19:03:37
  17. 氧生福地 玩美北湖(上)——为时光守候两千年

    原标题:氧生福地 玩美北湖(上)——为时光守候两千年一次说走就走的旅行,只有一张高铁票的距离~ 所以,湖南郴州,我来了~ 从广州南站出发,一个半小时就到达郴州西站了。在动车上,同时改票的南风兄和我居然被分到了一个车厢,所以一路非常愉快地聊了过来。 挺好,最起…...

    2024/4/29 20:46:55
  18. 氧生福地 玩美北湖(中)——永春梯田里的美与鲜

    原标题:氧生福地 玩美北湖(中)——永春梯田里的美与鲜一觉醒来,因为大家太爱“美”照,在柳毅山庄去寻找龙女而错过了早餐时间。近十点,向导坏坏还是带着饥肠辘辘的我们去吃郴州最富有盛名的“鱼头粉”。说这是“十二分推荐”,到郴州必吃的美食之一。 哇塞!那个味美香甜…...

    2024/4/30 22:21:04
  19. 氧生福地 玩美北湖(下)——奔跑吧骚年!

    原标题:氧生福地 玩美北湖(下)——奔跑吧骚年!让我们红尘做伴 活得潇潇洒洒 策马奔腾共享人世繁华 对酒当歌唱出心中喜悦 轰轰烈烈把握青春年华 让我们红尘做伴 活得潇潇洒洒 策马奔腾共享人世繁华 对酒当歌唱出心中喜悦 轰轰烈烈把握青春年华 啊……啊……啊 两…...

    2024/4/26 23:04:58
  20. 扒开伪装医用面膜,翻六倍价格宰客,小姐姐注意了!

    原标题:扒开伪装医用面膜,翻六倍价格宰客,小姐姐注意了!扒开伪装医用面膜,翻六倍价格宰客!当行业里的某一品项火爆了,就会有很多商家蹭热度,装逼忽悠,最近火爆朋友圈的医用面膜,被沾上了污点,到底怎么回事呢? “比普通面膜安全、效果好!痘痘、痘印、敏感肌都能用…...

    2024/4/27 23:24:42
  21. 「发现」铁皮石斛仙草之神奇功效用于医用面膜

    原标题:「发现」铁皮石斛仙草之神奇功效用于医用面膜丽彦妆铁皮石斛医用面膜|石斛多糖无菌修护补水贴19大优势: 1、铁皮石斛:自唐宋以来,一直被列为皇室贡品,铁皮石斛生于海拔1600米的悬崖峭壁之上,繁殖力差,产量极低,所以古代仅供皇室、贵族享用 2、铁皮石斛自古民间…...

    2024/4/28 5:48:52
  22. 丽彦妆\医用面膜\冷敷贴轻奢医学护肤引导者

    原标题:丽彦妆\医用面膜\冷敷贴轻奢医学护肤引导者【公司简介】 广州华彬企业隶属香港华彬集团有限公司,专注美业21年,其旗下品牌: 「圣茵美」私密荷尔蒙抗衰,产后修复 「圣仪轩」私密荷尔蒙抗衰,产后修复 「花茵莳」私密荷尔蒙抗衰,产后修复 「丽彦妆」专注医学护…...

    2024/4/30 9:42:22
  23. 广州械字号面膜生产厂家OEM/ODM4项须知!

    原标题:广州械字号面膜生产厂家OEM/ODM4项须知!广州械字号面膜生产厂家OEM/ODM流程及注意事项解读: 械字号医用面膜,其实在我国并没有严格的定义,通常我们说的医美面膜指的应该是一种「医用敷料」,也就是说,医用面膜其实算作「医疗器械」的一种,又称「医用冷敷贴」。 …...

    2024/4/30 9:43:22
  24. 械字号医用眼膜缓解用眼过度到底有无作用?

    原标题:械字号医用眼膜缓解用眼过度到底有无作用?医用眼膜/械字号眼膜/医用冷敷眼贴 凝胶层为亲水高分子材料,含70%以上的水分。体表皮肤温度传导到本产品的凝胶层,热量被凝胶内水分子吸收,通过水分的蒸发带走大量的热量,可迅速地降低体表皮肤局部温度,减轻局部皮肤的灼…...

    2024/4/30 9:42:49
  25. 配置失败还原请勿关闭计算机,电脑开机屏幕上面显示,配置失败还原更改 请勿关闭计算机 开不了机 这个问题怎么办...

    解析如下&#xff1a;1、长按电脑电源键直至关机&#xff0c;然后再按一次电源健重启电脑&#xff0c;按F8健进入安全模式2、安全模式下进入Windows系统桌面后&#xff0c;按住“winR”打开运行窗口&#xff0c;输入“services.msc”打开服务设置3、在服务界面&#xff0c;选中…...

    2022/11/19 21:17:18
  26. 错误使用 reshape要执行 RESHAPE,请勿更改元素数目。

    %读入6幅图像&#xff08;每一幅图像的大小是564*564&#xff09; f1 imread(WashingtonDC_Band1_564.tif); subplot(3,2,1),imshow(f1); f2 imread(WashingtonDC_Band2_564.tif); subplot(3,2,2),imshow(f2); f3 imread(WashingtonDC_Band3_564.tif); subplot(3,2,3),imsho…...

    2022/11/19 21:17:16
  27. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机...

    win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”问题的解决方法在win7系统关机时如果有升级系统的或者其他需要会直接进入一个 等待界面&#xff0c;在等待界面中我们需要等待操作结束才能关机&#xff0c;虽然这比较麻烦&#xff0c;但是对系统进行配置和升级…...

    2022/11/19 21:17:15
  28. 台式电脑显示配置100%请勿关闭计算机,“准备配置windows 请勿关闭计算机”的解决方法...

    有不少用户在重装Win7系统或更新系统后会遇到“准备配置windows&#xff0c;请勿关闭计算机”的提示&#xff0c;要过很久才能进入系统&#xff0c;有的用户甚至几个小时也无法进入&#xff0c;下面就教大家这个问题的解决方法。第一种方法&#xff1a;我们首先在左下角的“开始…...

    2022/11/19 21:17:14
  29. win7 正在配置 请勿关闭计算机,怎么办Win7开机显示正在配置Windows Update请勿关机...

    置信有很多用户都跟小编一样遇到过这样的问题&#xff0c;电脑时发现开机屏幕显现“正在配置Windows Update&#xff0c;请勿关机”(如下图所示)&#xff0c;而且还需求等大约5分钟才干进入系统。这是怎样回事呢&#xff1f;一切都是正常操作的&#xff0c;为什么开时机呈现“正…...

    2022/11/19 21:17:13
  30. 准备配置windows 请勿关闭计算机 蓝屏,Win7开机总是出现提示“配置Windows请勿关机”...

    Win7系统开机启动时总是出现“配置Windows请勿关机”的提示&#xff0c;没过几秒后电脑自动重启&#xff0c;每次开机都这样无法进入系统&#xff0c;此时碰到这种现象的用户就可以使用以下5种方法解决问题。方法一&#xff1a;开机按下F8&#xff0c;在出现的Windows高级启动选…...

    2022/11/19 21:17:12
  31. 准备windows请勿关闭计算机要多久,windows10系统提示正在准备windows请勿关闭计算机怎么办...

    有不少windows10系统用户反映说碰到这样一个情况&#xff0c;就是电脑提示正在准备windows请勿关闭计算机&#xff0c;碰到这样的问题该怎么解决呢&#xff0c;现在小编就给大家分享一下windows10系统提示正在准备windows请勿关闭计算机的具体第一种方法&#xff1a;1、2、依次…...

    2022/11/19 21:17:11
  32. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”的解决方法...

    今天和大家分享一下win7系统重装了Win7旗舰版系统后&#xff0c;每次关机的时候桌面上都会显示一个“配置Windows Update的界面&#xff0c;提示请勿关闭计算机”&#xff0c;每次停留好几分钟才能正常关机&#xff0c;导致什么情况引起的呢&#xff1f;出现配置Windows Update…...

    2022/11/19 21:17:10
  33. 电脑桌面一直是清理请关闭计算机,windows7一直卡在清理 请勿关闭计算机-win7清理请勿关机,win7配置更新35%不动...

    只能是等着&#xff0c;别无他法。说是卡着如果你看硬盘灯应该在读写。如果从 Win 10 无法正常回滚&#xff0c;只能是考虑备份数据后重装系统了。解决来方案一&#xff1a;管理员运行cmd&#xff1a;net stop WuAuServcd %windir%ren SoftwareDistribution SDoldnet start WuA…...

    2022/11/19 21:17:09
  34. 计算机配置更新不起,电脑提示“配置Windows Update请勿关闭计算机”怎么办?

    原标题&#xff1a;电脑提示“配置Windows Update请勿关闭计算机”怎么办&#xff1f;win7系统中在开机与关闭的时候总是显示“配置windows update请勿关闭计算机”相信有不少朋友都曾遇到过一次两次还能忍但经常遇到就叫人感到心烦了遇到这种问题怎么办呢&#xff1f;一般的方…...

    2022/11/19 21:17:08
  35. 计算机正在配置无法关机,关机提示 windows7 正在配置windows 请勿关闭计算机 ,然后等了一晚上也没有关掉。现在电脑无法正常关机...

    关机提示 windows7 正在配置windows 请勿关闭计算机 &#xff0c;然后等了一晚上也没有关掉。现在电脑无法正常关机以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;关机提示 windows7 正在配…...

    2022/11/19 21:17:05
  36. 钉钉提示请勿通过开发者调试模式_钉钉请勿通过开发者调试模式是真的吗好不好用...

    钉钉请勿通过开发者调试模式是真的吗好不好用 更新时间:2020-04-20 22:24:19 浏览次数:729次 区域: 南阳 > 卧龙 列举网提醒您:为保障您的权益,请不要提前支付任何费用! 虚拟位置外设器!!轨迹模拟&虚拟位置外设神器 专业用于:钉钉,外勤365,红圈通,企业微信和…...

    2022/11/19 21:17:05
  37. 配置失败还原请勿关闭计算机怎么办,win7系统出现“配置windows update失败 还原更改 请勿关闭计算机”,长时间没反应,无法进入系统的解决方案...

    前几天班里有位学生电脑(windows 7系统)出问题了&#xff0c;具体表现是开机时一直停留在“配置windows update失败 还原更改 请勿关闭计算机”这个界面&#xff0c;长时间没反应&#xff0c;无法进入系统。这个问题原来帮其他同学也解决过&#xff0c;网上搜了不少资料&#x…...

    2022/11/19 21:17:04
  38. 一个电脑无法关闭计算机你应该怎么办,电脑显示“清理请勿关闭计算机”怎么办?...

    本文为你提供了3个有效解决电脑显示“清理请勿关闭计算机”问题的方法&#xff0c;并在最后教给你1种保护系统安全的好方法&#xff0c;一起来看看&#xff01;电脑出现“清理请勿关闭计算机”在Windows 7(SP1)和Windows Server 2008 R2 SP1中&#xff0c;添加了1个新功能在“磁…...

    2022/11/19 21:17:03
  39. 请勿关闭计算机还原更改要多久,电脑显示:配置windows更新失败,正在还原更改,请勿关闭计算机怎么办...

    许多用户在长期不使用电脑的时候&#xff0c;开启电脑发现电脑显示&#xff1a;配置windows更新失败&#xff0c;正在还原更改&#xff0c;请勿关闭计算机。。.这要怎么办呢&#xff1f;下面小编就带着大家一起看看吧&#xff01;如果能够正常进入系统&#xff0c;建议您暂时移…...

    2022/11/19 21:17:02
  40. 还原更改请勿关闭计算机 要多久,配置windows update失败 还原更改 请勿关闭计算机,电脑开机后一直显示以...

    配置windows update失败 还原更改 请勿关闭计算机&#xff0c;电脑开机后一直显示以以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;配置windows update失败 还原更改 请勿关闭计算机&#x…...

    2022/11/19 21:17:01
  41. 电脑配置中请勿关闭计算机怎么办,准备配置windows请勿关闭计算机一直显示怎么办【图解】...

    不知道大家有没有遇到过这样的一个问题&#xff0c;就是我们的win7系统在关机的时候&#xff0c;总是喜欢显示“准备配置windows&#xff0c;请勿关机”这样的一个页面&#xff0c;没有什么大碍&#xff0c;但是如果一直等着的话就要两个小时甚至更久都关不了机&#xff0c;非常…...

    2022/11/19 21:17:00
  42. 正在准备配置请勿关闭计算机,正在准备配置windows请勿关闭计算机时间长了解决教程...

    当电脑出现正在准备配置windows请勿关闭计算机时&#xff0c;一般是您正对windows进行升级&#xff0c;但是这个要是长时间没有反应&#xff0c;我们不能再傻等下去了。可能是电脑出了别的问题了&#xff0c;来看看教程的说法。正在准备配置windows请勿关闭计算机时间长了方法一…...

    2022/11/19 21:16:59
  43. 配置失败还原请勿关闭计算机,配置Windows Update失败,还原更改请勿关闭计算机...

    我们使用电脑的过程中有时会遇到这种情况&#xff0c;当我们打开电脑之后&#xff0c;发现一直停留在一个界面&#xff1a;“配置Windows Update失败&#xff0c;还原更改请勿关闭计算机”&#xff0c;等了许久还是无法进入系统。如果我们遇到此类问题应该如何解决呢&#xff0…...

    2022/11/19 21:16:58
  44. 如何在iPhone上关闭“请勿打扰”

    Apple’s “Do Not Disturb While Driving” is a potentially lifesaving iPhone feature, but it doesn’t always turn on automatically at the appropriate time. For example, you might be a passenger in a moving car, but your iPhone may think you’re the one dri…...

    2022/11/19 21:16:57