Я пытаюсь реализовать обучение переносу с помощью MobileV2Net, следуя указаниям https://www.tensorflow.org/tutorials/images/transfer_learning.
. В приведенном выше руководстве используется модель MobileV2Net в качестве базовой модели и используется набор данных cats_vs_dog, который имеет тип tensorflow.python.data.ops.dataset_ops._OptionsDataset
. В моем случае я хочу использовать MobileV2Net в качестве базовой модели, заморозить все веса для разных слоев CNN, добавить полностью подключенный слой и настроить его. Набор данных, который я использую, - tiny_imagenet. Ниже приведен мой код:
##After pre-processing the data :
(x_train, y_train), (x_valid, y_valid),(x_test, y_test) = data
#type(x_train) = numpy.ndarray
#len(x_train) = 1750
##Converting the data to use the pipleine that comes with tf.Data.Dataset
raw_train = tf.data.Dataset.from_tensor_slices((x_train,y_train))
raw_validation = tf.data.Dataset.from_tensor_slices((x_valid, y_valid))
raw_test = tf.data.Dataset.from_tensor_slices((x_test, y_test))
#print(raw_train) gives
<DatasetV1Adapter shapes: ((64, 64, 3), ()), types: (tf.float64, tf.int64)>
## Now i follow everything from the link (given above in problem statement) :
IMG_SIZE = 160 # All images will be resized to 160x160
def format_example(image, label):
image = tf.cast(image, tf.float32)
image = (image/127.5) - 1
image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
return image, label
train = raw_train.map(format_example)
validation = raw_validation.map(format_example)
test = raw_test.map(format_example
#print(train) gives
#<DatasetV1Adapter shapes: ((160, 160, 3), ()), types: (tf.float32, tf.int64)>
train_batches = train.shuffle(SHUFFLE_BUFFER_SIZE).batch(BATCH_SIZE)
validation_batches = validation.batch(BATCH_SIZE)
test_batches = test.batch(BATCH_SIZE)
#print(train_batches) gives :
<DatasetV1Adapter shapes: ((?, 160, 160, 3), (?,)), types: (tf.float32, tf.int64)>
##The corresponding command in the tutorial (which works on cats vs dogs dataset gives) :
<BatchDataset shapes: ((None, 160, 160, 3), (None,)), types: (tf.float32, tf.int64)>
Я также пытался использовать padded_batch () вместо batch (), но все равно ниже идет бесконечный цикл.
##Goes to infinite loop
for image_batch, label_batch in train_batches.take(1):
print("hello")
pass
image_batch.shape ## Does not reach here
##The same command in the tutorial gives :
hello
TensorShape([32, 160, 160, 3])
##Further in my case :
#print(train_batches.take(1)) gives
<DatasetV1Adapter shapes: ((?, 160, 160, 3), (?,)), types: (tf.float32, tf.int64)>
##In tutorial it gives :
<TakeDataset shapes: ((None, 160, 160, 3), (None,)), types: (tf.float32, tf.int64)>
image_batch используется позже вкод.
##Load the pre trained Model :
IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)
base_model = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
include_top=False,
weights='imagenet')
##This feature extractor converts each 160x160x3 image to a 5x5x1280 block of features. See what ##it does to the example batch of images:
feature_batch = base_model(image_batch)
print(feature_batch.shape) ## ((32, 5, 5, 1280))
##Freezing the convolution base
base_model.trainable = False
##Adding a classification head :
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
feature_batch_average = global_average_layer(feature_batch)
print(feature_batch_average.shape) ## (32, 1280)
prediction_layer = keras.layers.Dense(1)
prediction_batch = prediction_layer(feature_batch_average)
print(prediction_batch.shape) ##(32, 1)
model = tf.keras.Sequential([
base_model,
global_average_layer,
prediction_layer
])
Я никогда особо не работал с tenorflow, есть идеи, как заставить его работать?