尝试通过运行
在TPU上运行EffecientNeteffnet = efn.EfficientNetB5(weights='imagenet', include_top=False)
# Replace all Batch Normalization layers by Group Normalization layers
for i, layer in enumerate(effnet.layers):
if "batch_normalization" in layer.name:
effnet.layers[i] = GroupNormalization(groups=32, axis=-1, epsilon=0.00001)
model = Sequential()
model.add(effnet)
model.add(GlobalAveragePooling2D())
model.add(Dropout(0.5))
model.add(Dense(8, activation=elu))
model.compile(loss='mse', optimizer=RAdam(lr=0.00005), metrics=['mse', 'acc'])
print(model.summary())
model_json = model.to_json()
with open("model_ef7_fn.json", "w") as json_file:
json_file.write(model_json)
tpu_model = tf.contrib.tpu.keras_to_tpu_model(
model,
strategy=tf.contrib.tpu.TPUDistributionStrategy(
tf.contrib.cluster_resolver.TPUClusterResolver(
tpu='grpc://' + os.environ['COLAB_TPU_ADDR'])
)
)
但是我得到这个错误
ValueError: 图层在非批量尺寸中具有可变的形状。 TPU型号必须 所有操作的形状都是不变的。
You may have to specify `input_length` for RNN/TimeDistributed layers.
Layer: <keras.engine.training.Model object at 0x7f34e5c06f60>
Input shape: (None, None, None, 3)
Output shape: (None, None, None, 2048)