DataGenerator 在 Google 驱动器中找不到文件
DataGenerator does not find the Files in Google Drive
使用此处的代码 https://keras.io/api/utils/python_utils/#sequence-class,我编写了一个自定义 DataGenerator。
# Here, `x_set` is list of path to the images
# and `y_set` are the associated classes.
class DataGenerator(Sequence):
def __init__(self, x_set, y_set, batch_size):
self.x, self.y = x_set, y_set
self.batch_size = batch_size
def __len__(self):
return math.ceil(len(self.x) / self.batch_size)
def __getitem__(self, idx):
batch_x = self.x[idx * self.batch_size:(idx + 1) *
self.batch_size]
batch_y = self.y[idx * self.batch_size:(idx + 1) *
self.batch_size]
return np.array([
resize(imread(file_name), (224, 224))
for file_name in batch_x]), np.array(batch_y)
我有 X_train
和 X_val
,它们是包含我的图像文件的图像路径的列表,y_train 和 y_valwhich 是一个热编码标签并应用了这个数据到 DataGenerator
.
training_generator = DataGenerator(X_train, y_train, batch_size=32)
validation_generator = DataGenerator(X_val, y_val, batch_size=32)
然后拟合模型:
model.fit_generator(generator=training_generator,
validation_data=validation_generator,
steps_per_epoch = num_train_samples // batch_size,
validation_steps = num_val_samples // batch_size,
epochs = 10,
use_multiprocessing=True,
workers=6)
当运行这段代码时,我得到了这个错误:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-54-411e62536182> in <module>()
5 epochs = 10,
6 use_multiprocessing=True,
----> 7 workers=6)
16 frames
/usr/local/lib/python3.6/dist-packages/imageio/core/request.py in _parse_uri(self, uri)
271 # Reading: check that the file exists (but is allowed a dir)
272 if not os.path.exists(fn):
--> 273 raise FileNotFoundError("No such file: '%s'" % fn)
274 else:
275 # Writing: check that the directory to write to does exist
FileNotFoundError: No such file: '/content/gdrive/My Drive/data/2017-IWT4S-CarsReId_LP-dataset/s01_l01/1_1.png'
X_train
是一个列表,其中包含 Google 驱动器中我的数据的绝对文件路径作为字符串。
X_train[0]
'/content/gdrive/My Drive/data/2017-IWT4S-CarsReId_LP-dataset/s01_l01/1_1.png'
如何修改代码才能让生成器找到我的文件?
这可能是因为 Colab Notebook 文件和数据不在同一个文件夹中? -> 我将我的 Colab Jupyter Notebook 放在文件夹“/content/gdrive/My Drive”中,我也得到了 FileNotFoundError”。
或者我是否必须路由到正确的文件夹?
这是由于 Google 驱动超时造成的。我按照此处的建议在我的控制台中编写解决了这个问题:
function ConnectButton(){
console.log("Connect pushed");
document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect").click()
}
setInterval(ConnectButton,600);
使用此处的代码 https://keras.io/api/utils/python_utils/#sequence-class,我编写了一个自定义 DataGenerator。
# Here, `x_set` is list of path to the images
# and `y_set` are the associated classes.
class DataGenerator(Sequence):
def __init__(self, x_set, y_set, batch_size):
self.x, self.y = x_set, y_set
self.batch_size = batch_size
def __len__(self):
return math.ceil(len(self.x) / self.batch_size)
def __getitem__(self, idx):
batch_x = self.x[idx * self.batch_size:(idx + 1) *
self.batch_size]
batch_y = self.y[idx * self.batch_size:(idx + 1) *
self.batch_size]
return np.array([
resize(imread(file_name), (224, 224))
for file_name in batch_x]), np.array(batch_y)
我有 X_train
和 X_val
,它们是包含我的图像文件的图像路径的列表,y_train 和 y_valwhich 是一个热编码标签并应用了这个数据到 DataGenerator
.
training_generator = DataGenerator(X_train, y_train, batch_size=32)
validation_generator = DataGenerator(X_val, y_val, batch_size=32)
然后拟合模型:
model.fit_generator(generator=training_generator,
validation_data=validation_generator,
steps_per_epoch = num_train_samples // batch_size,
validation_steps = num_val_samples // batch_size,
epochs = 10,
use_multiprocessing=True,
workers=6)
当运行这段代码时,我得到了这个错误:
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-54-411e62536182> in <module>()
5 epochs = 10,
6 use_multiprocessing=True,
----> 7 workers=6)
16 frames
/usr/local/lib/python3.6/dist-packages/imageio/core/request.py in _parse_uri(self, uri)
271 # Reading: check that the file exists (but is allowed a dir)
272 if not os.path.exists(fn):
--> 273 raise FileNotFoundError("No such file: '%s'" % fn)
274 else:
275 # Writing: check that the directory to write to does exist
FileNotFoundError: No such file: '/content/gdrive/My Drive/data/2017-IWT4S-CarsReId_LP-dataset/s01_l01/1_1.png'
X_train
是一个列表,其中包含 Google 驱动器中我的数据的绝对文件路径作为字符串。
X_train[0]
'/content/gdrive/My Drive/data/2017-IWT4S-CarsReId_LP-dataset/s01_l01/1_1.png'
如何修改代码才能让生成器找到我的文件? 这可能是因为 Colab Notebook 文件和数据不在同一个文件夹中? -> 我将我的 Colab Jupyter Notebook 放在文件夹“/content/gdrive/My Drive”中,我也得到了 FileNotFoundError”。 或者我是否必须路由到正确的文件夹?
这是由于 Google 驱动超时造成的。我按照此处的建议在我的控制台中编写解决了这个问题:
function ConnectButton(){
console.log("Connect pushed");
document.querySelector("#top-toolbar > colab-connect-button").shadowRoot.querySelector("#connect").click()
}
setInterval(ConnectButton,600);