我正在关注本教程:https://medium.com/@yannicksergeobam/plant-disease-classification-with-tensorflow-2-0-268fe7f72c2a 当我在colab上运行这部分代码时:
feature_extractor = hub.KerasLayer(MODULE_HANDLE,
input_shape=IMAGE_SIZE+(3,),
output_shape=[FV_SIZE])
do_fine_tuning = False #@param {type:"boolean"}
if do_fine_tuning:
feature_extractor.trainable = True
# unfreeze some layers of base network for fine-tuning
for layer in feature_extractor.layers[-30:]:
layer.trainable =True
else:
feature_extractor.trainable = False
model = tf.keras.Sequential([
feature_extractor,
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dropout(rate=0.2),
tf.keras.layers.Dense(train_generator.num_classes, activation='softmax',
kernel_regularizer=tf.keras.regularizers.l2(0.0001))
])
我收到此错误:
---------------------------------------------------------------------------
InvalidURL Traceback (most recent call last)
<ipython-input-16-7f860a0e8414> in <module>()
1 feature_extractor = hub.KerasLayer(MODULE_HANDLE,
2 input_shape=IMAGE_SIZE+(3,),
----> 3 output_shape=[FV_SIZE])
4 do_fine_tuning = True #@param {type:"boolean"}
5 if do_fine_tuning:
14 frames
/usr/lib/python3.6/http/client.py in putrequest(self, method, url, skip_host, skip_accept_encoding)
1125 match = _contains_disallowed_url_pchar_re.search(url)
1126 if match:
-> 1127 raise InvalidURL(f"URL can't contain control characters. {url!r} "
1128 f"(found at least {match.group()!r})")
1129 request = '%s %s %s' % (method, url, self._http_vsn_str)
InvalidURL: URL can't contain control characters. '/google/tf2- preview/inception_v3/feature_vector/2?tf-hub-format=compressed' (found at least ' ')
不幸的是,我没有在Google上找到可以帮助我的东西。从我所知道的小python中,我找不到错误...可以帮忙吗?
答案 0 :(得分:1)
改为使用此:
pixels = 299
FV_SIZE = 2048
MODULE_HANDLE = "https://tfhub.dev/google/tf2-
preview/inception_v3/feature_vector/4"
IMAGE_SIZE = (pixels, pixels)
BATCH_SIZE = 64
答案 1 :(得分:1)
在MODULE_HANDLE变量中,其值中有空格:
“ https://tfhub.dev/google/tf2-预览/ inception_v3 / feature_vector / 4”
该空间介于“ tf2-”和“ preview”之间。您只需要删除空间即可。
发生此错误是因为urllib不喜欢url中的空格。