나는 데 문제 적용 데이터 증가 훈련을 할 때 모델입니다. 에 대해 구체적으로 사용하의 fit_generator() 방법입니다.
내가 원래 내 모델 없이 성공적으로 증가를 사용하는 맞() 방법,그러나 그에 따라 다른 사람이 그것을 권장 사용하 fit_generator(). 것과 같은 두 방법을 모두 필요가 동일한 입력된 이미지인 경우 및 라벨지만,나는 다음과 같은 오류가 실행하는 경우 아래 코드:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/tmp/ipykernel_35/139227558.py in <module>
105
106 # train the network
--> 107 model.fit_generator(aug.flow(train_ds, batch_size=batch_size),
108 validation_data=val_ds, steps_per_epoch=len(train_ds[0]) // batch_size,
109 epochs=epochs)
/opt/conda/lib/python3.7/site-packages/keras/preprocessing/image.py in flow(self, x, y, batch_size, shuffle, sample_weight, seed, save_to_dir, save_prefix, save_format, subset)
894 save_prefix=save_prefix,
895 save_format=save_format,
--> 896 subset=subset)
897
898 def flow_from_directory(self,
/opt/conda/lib/python3.7/site-packages/keras/preprocessing/image.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)
472 save_format=save_format,
473 subset=subset,
--> 474 **kwargs)
475
476
/opt/conda/lib/python3.7/site-packages/keras_preprocessing/image/numpy_array_iterator.py in __init__(self, x, y, image_data_generator, batch_size, shuffle, sample_weight, seed, data_format, save_to_dir, save_prefix, save_format, subset, dtype)
119 y = y[split_idx:]
120
--> 121 self.x = np.asarray(x, dtype=self.dtype)
122 self.x_misc = x_misc
123 if self.x.ndim != 4:
/opt/conda/lib/python3.7/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
81
82 """
---> 83 return array(a, dtype, copy=False, order=order)
84
85
TypeError: float() argument must be a string or a number, not 'BatchDataset'
가 완료된 구글에서 해결하기 위해 노력하고 TypeError:플로트()규먼트는 문자열이나 번호가 아닌'BatchDataset' 오류가 있지만,소용이 없었습니다. 누군가는 제안이 있으로 이동하나요?
import pathlib
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
import matplotlib.pyplot as plt
# Set data directory
data_dir = pathlib.Path("../input/validatedweaponsv6/images/")
# Set image size
img_height = 120
img_width = 120
# Hyperparameters
batch_size = 128
epochs = 50
learning_rate = 0.001
# Create the training dataset
train_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
label_mode='categorical',
validation_split=0.2,
subset="training",
shuffle=True,
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
# Create the validation dataset
val_ds = tf.keras.utils.image_dataset_from_directory(
data_dir,
label_mode='categorical',
validation_split=0.2,
subset="validation",
shuffle=True,
seed=123,
image_size=(img_height, img_width),
batch_size=batch_size)
# Create sequential model
model = Sequential([
# Preprocessing
layers.Rescaling(1./127.5, offset=-1,
input_shape=(img_height, img_width, 3)),
# Encoder
layers.Conv2D(8, 3, activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(16, 3, activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, activation='relu'),
# layers.Conv2D(2, 3, activation='relu'), ???
layers.Flatten(),
# Decoder
layers.Dense(64, activation='relu'),
layers.Dropout(0.5),
layers.Dense(2, activation='softmax')
])
# Print the model to see the different output shapes
print(model.summary())
# Compile model
model.compile(loss='categorical_crossentropy',
optimizer=keras.optimizers.SGD(learning_rate=learning_rate), metrics=['accuracy'])
# construct the training image generator for data augmentation
aug = tf.keras.preprocessing.image.ImageDataGenerator(rotation_range=20, zoom_range=0.15,
width_shift_range=0.2, height_shift_range=0.2, shear_range=0.15,
horizontal_flip=True, fill_mode="nearest")
# train the network
model.fit_generator(aug.flow(train_ds, batch_size=batch_size),
validation_data=val_ds, steps_per_epoch=len(train_ds[0]) // batch_size,
epochs=epochs)
# Print scores
score = model.evaluate(train_ds, verbose=0)
print('Validation loss:', score[0])
print('Validation accuracy:', score[1])
# Show loss and accuracy models
show_history(history)
당신을 감사하고 포! :)