TF Hub上有数百个训练好的模型,这里选择EfficientNet模型,TF Hub上链接地址为:
https://hub.tensorflow.google.cn/google/efficientnet/b0/classification/1
页面显示如下
注意:TF Hub模型页面上的两个按钮。“Copy URL”为在线调用此模型提供了链接地址;通过点击“Download”按钮,可以将模型保存到本地,离线使用。本节将详细讲解这两种使用方式。
由于选择的EfficientNet模型要求输入的Tensor shape为(96, 96, 3) ,所以我们使用TRAIN_96_FILE和TEST_96_FILE作为训练集和测试集,代码如下
train_set = new AkSourceBatchOp().setFilePath(DATA_DIR + TRAIN_96_FILE); test_set = new AkSourceBatchOp().setFilePath(DATA_DIR + TEST_96_FILE);
完整的模型结构如下,TF Hub的EfficientNet将作为整体模型的一个“层”,即为结构中的keras_layer。
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= tensor (InputLayer) [(None, 96, 96, 3)] 0 _________________________________________________________________ keras_layer (KerasLayer) (None, 1000) 5330564 _________________________________________________________________ flatten (Flatten) (None, 1000) 0 _________________________________________________________________ logits (Dense) (None, 1) 1001 ================================================================= Total params: 5,331,565 Trainable params: 1,001 Non-trainable params: 5,330,564 _________________________________________________________________
直接将模型的URL,作为hub.KerasLayer的参数,完整代码如下:
def efficientnet(train_set, test_set) : if not(os.path.exists(DATA_DIR + MODEL_EFNET_FILE)): train_set\ .link( KerasSequentialClassifierTrainBatchOp()\ .setTensorCol("tensor")\ .setLabelCol("label")\ .setLayers([ "hub.KerasLayer('https://hub.tensorflow.google.cn/google/efficientnet/b0/classification/1')", "Flatten()" ])\ .setNumEpochs(5)\ .setIntraOpParallelism(1)\ .setSaveCheckpointsEpochs(0.5)\ .setValidationSplit(0.1)\ .setSaveBestOnly(True) .setBestMetric("auc") )\ .link( AkSinkBatchOp()\ .setFilePath(DATA_DIR + MODEL_EFNET_FILE) ) BatchOperator.execute() KerasSequentialClassifierPredictBatchOp()\ .setPredictionCol(PREDICTION_COL)\ .setPredictionDetailCol(PREDICTION_DETAIL_COL)\ .setReservedCols(["relative_path", "label"])\ .linkFrom( AkSourceBatchOp().setFilePath(DATA_DIR + MODEL_EFNET_FILE), test_set )\ .lazyPrint(10)\ .lazyPrintStatistics()\ .link( EvalBinaryClassBatchOp()\ .setLabelCol("label")\ .setPredictionDetailCol(PREDICTION_DETAIL_COL)\ .lazyPrintMetrics() ) BatchOperator.execute()
模型评估结果如下,明显优于前面的CNN模型。
Summary: | colName|count|missing|sum|mean|variance|min|max| |-------------|-----|-------|---|----|--------|---|---| |relative_path| 2500| 0|NaN| NaN| NaN|NaN|NaN| | label| 2500| 0|NaN| NaN| NaN|NaN|NaN| | pred| 2500| 0|NaN| NaN| NaN|NaN|NaN| | pred_info| 2500| 0|NaN| NaN| NaN|NaN|NaN| -------------------------------- Metrics: -------------------------------- Auc:0.9895 Accuracy:0.9496 Precision:0.9558 Recall:0.9401 F1:0.9478 LogLoss:0.1301 |Pred\Real| dog| cat| |---------|----|----| | dog|1145| 53| | cat| 73|1229|
在TF Hub页面上点击“Download”,将模型下载到本地,文件名为“1.tar”,解压到文件夹(名称为“1”),该文件夹下内容如下图所示,包含两个子文件夹和两个文件。该文件夹对应的路径为:DATA_DIR + "1"
离线使用TF Hub模型,只需将hub.KerasLayer的参数设为本地保存离线模型的文件夹路径。完整代码如下:
def efficientnet_offline(train_set, test_set) : if not(os.path.exists(DATA_DIR + MODEL_EFNET_OFFLINE_FILE)): train_set\ .link( KerasSequentialClassifierTrainBatchOp()\ .setTensorCol("tensor")\ .setLabelCol("label")\ .setLayers([ "hub.KerasLayer('" + DATA_DIR + "1')", "Flatten()" ])\ .setNumEpochs(5)\ .setIntraOpParallelism(1)\ .setSaveCheckpointsEpochs(0.5)\ .setValidationSplit(0.1)\ .setSaveBestOnly(True)\ .setBestMetric("auc") )\ .link( AkSinkBatchOp()\ .setFilePath(DATA_DIR + MODEL_EFNET_OFFLINE_FILE) ) BatchOperator.execute() KerasSequentialClassifierPredictBatchOp()\ .setPredictionCol(PREDICTION_COL)\ .setPredictionDetailCol(PREDICTION_DETAIL_COL)\ .setReservedCols(["relative_path", "label"])\ .linkFrom( AkSourceBatchOp().setFilePath(DATA_DIR + MODEL_EFNET_OFFLINE_FILE), test_set )\ .lazyPrint(10)\ .lazyPrintStatistics()\ .link( EvalBinaryClassBatchOp()\ .setLabelCol("label")\ .setPredictionDetailCol(PREDICTION_DETAIL_COL)\ .lazyPrintMetrics() ) BatchOperator.execute()
模型评估结果如下,与在线使用TF Hub模型的结果一致。
Summary: | colName|count|missing|sum|mean|variance|min|max| |-------------|-----|-------|---|----|--------|---|---| |relative_path| 2500| 0|NaN| NaN| NaN|NaN|NaN| | label| 2500| 0|NaN| NaN| NaN|NaN|NaN| | pred| 2500| 0|NaN| NaN| NaN|NaN|NaN| | pred_info| 2500| 0|NaN| NaN| NaN|NaN|NaN| -------------------------------- Metrics: -------------------------------- Auc:0.9912 Accuracy:0.952 Precision:0.9645 Recall:0.936 F1:0.95 LogLoss:0.1209 |Pred\Real| dog| cat| |---------|----|----| | dog|1140| 42| | cat| 78|1240|