我的代碼片段如下 -
from tensorflow.keras.applications import InceptionResNetV2
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout, Input, Resizing
inputs = Input(shape=x_train.shape[1:])
x = Conv2D(filters=3, kernel_size=(1,1), activation='relu')(inputs)
x = Resizing(height=75, width=75)(x)
base_model = InceptionResNetV2(input_shape=(75, 75, 3), include_top=False, input_tensor=x, weights='imagenet')
我得到的期望是-
ValueError Traceback (most recent call last)
/var/folders/26/0fbl62dn7zsd3x5t5f3zh1mr0000gn/T/ipykernel_90240/1249320150.py in <module>
7 # x = Resizing(height=75, width=75)(x)
8
----> 9 base_model = InceptionResNetV2(input_shape=(75, 75, 3), include_top=False, input_tensor=x, weights='imagenet')
10 base_model.trainable = False
11
/opt/homebrew/Caskroom/miniforge/base/envs/pattern-recognition/lib/python3.9/site-packages/keras/applications/inception_resnet_v2.py in InceptionResNetV2(include_top, weights, input_tensor, input_shape, pooling, classes, classifier_activation, **kwargs)
246 cache_subdir='models',
247 file_hash='d19885ff4a710c122648d3b5c3b684e4')
--> 248 model.load_weights(weights_path)
249 elif weights is not None:
250 model.load_weights(weights)
/opt/homebrew/Caskroom/miniforge/base/envs/pattern-recognition/lib/python3.9/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb
/opt/homebrew/Caskroom/miniforge/base/envs/pattern-recognition/lib/python3.9/site-packages/keras/saving/hdf5_format.py in load_weights_from_hdf5_group(f, model)
717 layer_names = filtered_layer_names
718 if len(layer_names) != len(filtered_layers):
--> 719 raise ValueError(
720 f'Layer count mismatch when loading weights from file. '
721 f'Model expected {len(filtered_layers)} layers, found '
ValueError: Layer count mismatch when loading weights from file. Model expected 449 layers, found 448 saved layers.
我已經用這里找到的多個模型進行了嘗試,但是,它們都因相同的錯誤而失敗,即:保存的層比預期的少一個。請讓我知道是否有解決方法。
uj5u.com熱心網友回復:
有趣的是,錯誤來自缺少Input層。例如,這將起作用:
import tensorflow as tf
x_train = tf.random.normal((100, 128, 128, 3))
inputs = tf.keras.layers.Input(shape=x_train.shape[1:])
base_model = tf.keras.applications.InceptionResNetV2(include_top=False, input_tensor=inputs, weights='imagenet')
model = tf.keras.Model(inputs, base_model.output)
所以也許嘗試這樣的事情:
import tensorflow as tf
x_train = tf.random.normal((100, 128, 128, 3))
inputs = tf.keras.layers.Input(shape=x_train.shape[1:])
x = tf.keras.layers.Conv2D(filters=3, kernel_size=(1,1), activation='relu')(inputs)
x = tf.keras.layers.Resizing(height=75, width=75)(x)
base_model = tf.keras.applications.InceptionResNetV2(input_shape=(75, 75, 3), include_top=False, weights='imagenet')
outputs = base_model(x)
model = tf.keras.Model(inputs, outputs)
另外,使用引數input_shape并input_tensor沒有多大意義。
uj5u.com熱心網友回復:
我假設您正在進行影像分類并且您的標簽是分類的。然后試試這個
img_shape= (128,128,3) # specify the image shape you have
inputs =tf.keras.Input(shape=img_shape)
x = Conv2D(filters=3, kernel_size=(1,1), activation='relu')(inputs)
x =tf.keras.layers.Resizing(height=75, width=75)(x)
x=base_model=tf.keras.applications.InceptionResNetV2(include_top=False,
weights="imagenet", pooling='max') (x)
# with pooling ='max base_model ouput is a vector you can use as input to a dense layer
num_of_classes = 10 # set this to the number of classes in the data set
output=Dense(num_of_classes, activation='softmax') (x)
model=Model(inputs=inputs, outputs=output)
optimizer=tf.keras.optimizers.Adam( learning_rate=0.001)
loss=tf.keras.losses.CategoricalCrossentropy ()
model.compile(optimizer, loss=loss, metrics=['accuracy'])
print (model.summary())
為什么要包括第一個 Conv2D 層?似乎沒有這一層,基本模型就更有能力了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/435300.html
