【发布时间】:2019-10-08 01:25:14
【问题描述】:
我正在尝试使用来自 https://www.tensorflow.org/tutorials/images/transfer_learning 的 MobileV2Net 实施迁移学习。
上述教程使用 MobileV2Net 模型作为基础模型,并使用类型为 tensorflow.python.data.ops.dataset_ops._OptionsDataset 的“cats_vs_dog”数据集。
就我而言,我想使用 MobileV2Net 作为基础模型,冻结不同 C.N.N 层的所有权重,添加一个全连接层并对其进行微调。我使用的数据集是 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
])
我从来没有使用过 tensorflow,有什么想法可以让它发挥作用吗?
【问题讨论】:
标签: tensorflow deep-learning conv-neural-network tensorflow-datasets transfer-learning