我是第一次使用 Keras 和 Tensorflow。我正在嘗試讓 vgg16 在 imagenette 資料集上進行訓練。
我不確定為什么會出現索引超出范圍錯誤。也許我在做一些明顯錯誤的事情。我已經搞砸了一段時間了,所以任何幫助都會很棒!
附上錯誤和源代碼
我根據 vgg16 檔案將影像大小調整為正確大小:https ://keras.io/api/applications/vgg/#vgg16-function
`
tfds_name = 'imagenette'
(ds_train, ds_validation), ds_info= tfds.load(
name=tfds_name,
split=['train', 'validation'],
with_info = True,
as_supervised=True)
#model from assignment link https://keras.io/api/applications/vgg/#vgg16-function
ourModel = tf.keras.applications.VGG16(
include_top=True, #3 fill layers on top
weights="imagenet", #use imagenet
input_tensor=None, #use another layer as input
input_shape=None, #inly set if include to false
pooling=None, #use with include top false
classes=1000, #number of classes to set, we use imagenet values
classifier_activation="softmax", # classifier on input can only be none or softmax on pretrained
)
#make it so layers frozen
#for layer in ourModel.layers[:-1]:
# layer.trainable = False
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy()
ourModel.compile(optimizer="adam",
loss=loss_fn,
metrics=['accuracy'])
def reshape(img,label):
img = tf.cast(img, tf.float32)
img = tf.image.resize(img, (224,224))
resize_image = tf.reshape(img, [-1, 224, 224, 3])
resize_image = preprocess_input(resize_image)
return resize_image, label
ds_train = ds_train.map(reshape)
ds_validation = ds_validation.map(reshape)
ourModel.fit(ds_train,
epochs=10,
validation_data = ds_validation)
`
錯誤:
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1051, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1040, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1030, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 890, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 949, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 212, in __call__
batch_dim = tf.shape(y_t)[0]
ValueError: slice index 0 of dimension 0 out of bounds. for '{{node strided_slice}} = StridedSlice[Index=DT_INT32, T=DT_INT32, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1](Shape, strided_slice/stack, strided_slice/stack_1, strided_slice/stack_2)' with input shapes: [0], [1], [1], [1] and with computed input tensors: input[1] = <0>, input[2] = <1>, input[3] = <1>.
uj5u.com熱心網友回復:
您的reshape函式正在為影像資料創建一個批次,但不是為標簽資料創建一個批次。所以return resize_image, label[None, ...]應該修復上面的bug。
但是批量訓練(和有效資料集)的正確方法是:ds_train=ds_train.batch(BATCH_SIZE)然后洗掉tf.reshape(img, [-1, 224, 224, 3])
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/524102.html
標籤:张量流喀拉斯图像网
