NASNet: You are trying to load a weight file containing 532 layers into a model with 526 layers.
1.软件环境
Windows 10-10.0.17134-SP0
Keras 2.1.6
Python 3.6.3
Tensorflow 1.7.0
2.问题描述
如题,如果你在使用NASNetLarge时,遇到该问题:
你肯定会很奇怪为什么官方给出的预训练权重不能加载到模型中去,这个报错显示的意思是模型和训练权重的层数不匹配,一个是532个layers一个是526个layers。
3.解决办法
3.1.先不使用预训练权重,使用随机权重来初始化模型
import numpy as np
from MixupImageDataGenerator import MixupImageDataGenerator
from PIL import ImageFile, Image
from sacred import Experiment
from sacred.observers import MongoObserver
from tensorflow.keras import optimizers
from tensorflow.keras import regularizers
from tensorflow.keras.applications import *
from tensorflow.keras.callbacks import LearningRateScheduler, EarlyStopping, ModelCheckpoint, TensorBoard
from tensorflow.keras.layers import *
from tensorflow.keras.losses import *
from tensorflow.keras.metrics import FalseNegatives, FalsePositives, TrueNegatives, TruePositives, BinaryAccuracy, \
CategoricalAccuracy, Precision, Recall
from tensorflow.keras.models import *
from tensorflow.keras.preprocessing.image import ImageDataGenerator
base_model = NASNetLarge(weights=None, include_top=False)
3.2.使用load_weights函数加载预训练权重
base_model.load_weights("你的权重路径", by_name=True)
这样就可以使用带有预训练权重的NASNet了!