h5py: OSError: Unable to open file (unable to open file: name = 'C:\Users\(name blocked)\Scripts\Neural_Network.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)

  • Windows 10
  • Python version 3.7
  • Miniconda
  • h5py version: latest I was trying to load a model with Keras and train it again.

Here is my code:

import gym
import random
import numpy as np
import tflearn
import os
import h5py
import tensorflow as tf
from tensorflow import keras
from keras.models import load_model
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
from statistics import mean, median
from collections import Counter

LR = 1e-3

env = gym.make('CartPole-v0')
env.reset()

goal_steps = 300

score_requirement = 50

initial_games = 10000

def some_random_games_first():
    for episode in range (5):
        env.reset()
        for t in range(goal_steps):
            env.render()
            action = env.action_space.sample()
            observation, reward, done, info = env.step(action)
            if done:
                break

def intial_population():
	training_data = []
	scores = []
	accepted_scores = [] 
	for _ in range (initial_games):
		score = 0
		game_memory = []
		prev_observation = []
		for _ in range (goal_steps):
			action = random.randrange (0,2)
			observation, reward, done, info = env.step(action)

			if len(prev_observation) > 0:
				game_memory.append([prev_observation, action])

			prev_observation = observation
			score += reward
			if done:
				break
		if score >= score_requirement:
			accepted_scores.append(score)
			for data in game_memory:
				if data [1] == 1:
					output = [0, 1]
				elif data [1] == 0:
					output = [1, 0]

				training_data.append([data[0], output])
		env.reset()
		scores.append(score)
	training_data_save = np.array (training_data)
	np.save ("saved.npy", training_data_save)
	print ("Average accepted scores", mean(accepted_scores))
	print("Median accepted score: ", median(accepted_scores))
	print (Counter (accepted_scores))

	return training_data
intial_population()

def neural_network_model (input_size):
	network = input_data (shape=[None, input_size, 1], name="input")
	network = fully_connected(network, 128, activation="relu")
	network = dropout(network, 0.8)

	network = fully_connected(network, 256, activation="relu")
	network = dropout(network, 0.8)

	network = fully_connected(network, 512, activation="relu")
	network = dropout(network, 0.8)

	network = fully_connected(network, 256, activation="relu")
	network = dropout(network, 0.8)

	network = fully_connected(network, 128, activation="relu")
	network = dropout(network, 0.8)

	network = fully_connected(network, 2, activation="softmax")
	network = regression(network, optimizer="adam", learning_rate = LR, loss = "categorical_crossentropy", name = 'targets')
	model = tflearn.DNN (network, tensorboard_dir='log")

	return model

def train_model(training_data, model=False):
	X = np.array ([i [0] for i in training_data]).reshape(-1, len(training_data[0][0]), 1)
	y = [i [1] for i in training_data]

	if not model:
		model = neural_network_model (input_size = len(X[0]))
	model.fit({'input' :X}, {'targets' :y}, n_epoch=3, snapshot_step=500, show_metric=True,
		run_id='openaistuff')
	return model

training_data = intial_population()
model = train_model(training_data)


scores = [] 
choices = []

for each_game in range(1):
	score = 0
	game_memory= []
	prev_obs = []
	env.reset()
	for _ in range (goal_steps):
		env.render()
		if len(prev_obs) == 0:
			action = random.randrange (0,2)
		else:
			action = np.argmax(model.predict(prev_obs.reshape(-1, len(prev_obs),1)) [0])
		choices.append(action)

		new_observation, reward, done, info = env.step(action)
		prev_obs = new_observation
		game_memory.append ([new_observation, action])
		score += reward
		if done:
			break
	scores.append(score)
	model.save("C:\\Users\\William\\Scripts\\Neural_Network.h5")
Average_Score = sum(scores)/len(scores)
print("Average Score: ", Average_Score)
print("choice 1:{}  choice 0:{}".format(choices.count(1)/len(choices),choices.count(0)/len(choices)))
print(score_requirement)
if Average_Score > 299:
	print("Solved")
else:
	print("Not solved, trying again")
	new_model = load_model("C:\\Users\\William\\Scripts\\Neural_Network.h5")'

And when I would run the program I would get this error:

Traceback (most recent call last):
  File "C:\Users\William\Scripts\Cartpole.py", line 157, in <module>
    new_model = load_model('C:\\Users\\William\\Scripts\\Neural_Network.h5')
  File "C:\Users\William\Miniconda3\envs\tensorflow\lib\site-packages\keras\engine\saving.py", line 417, in load_model
    f = h5dict(filepath, 'r')
  File "C:\Users\William\Miniconda3\envs\tensorflow\lib\site-packages\keras\utils\io_utils.py", line 186, in __init__
    self.data = h5py.File(path, mode=mode)
  File "C:\Users\William\Miniconda3\envs\tensorflow\lib\site-packages\h5py\_hl\files.py", line 394, in __init__
    swmr=swmr)
  File "C:\Users\William\Miniconda3\envs\tensorflow\lib\site-packages\h5py\_hl\files.py", line 170, in make_fid
    fid = h5f.open(name, flags, fapl=fapl)
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py\h5f.pyx", line 85, in h5py.h5f.open
OSError: Unable to open file (unable to open file: name = 'C:\Users\William\Scripts\Neural_Network.h5', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)

I have looked at the file and it generates the files: checkpoint,(thing used for the file type ‘file’), Neural_Network.h5.data-00000-of-00001, Neural_Network.h5, and Neural_Network.h5.meta. I see the ‘Neural_Network.h5’ so I am not sure why it cannot load it. Any answers are appreciated!

Edit: sorry for bad code formatting I saw it and cannot seem to fix it

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 6
  • Comments: 24 (5 by maintainers)

Most upvoted comments

  1. If you run a.py got the error, but you load the model.h5 in b.py
  2. Please cp model.h5 to the same level directory with a.py
  3. Edit b.py’s load(‘xx/model.h5’) to load(‘model.h5’)

It worked for me.

try to replace the Backslash ‘' in your path by Forward slash ’ /’. C:\Users\William\Scripts\Neural_Network.h5 =============> C:/Users\William/Scripts/Neural_Network.h5

Could this be a race condition between when it is saved and when it is attempted to be read back?

我也遇到了这个错误,我是因为创建h5文件的path路径上的文件夹不存在,所以出现此报错。当我把路径的文件夹都创建好就没问题了

Thanks for the help Ivan, I encountered similar problem: File “h5py/_objects.pyx”, line 54, in h5py._objects.with_phil.wrapper File “h5py/_objects.pyx”, line 55, in h5py._objects.with_phil.wrapper File “h5py/h5f.pyx”, line 88, in h5py.h5f.open OSError: Unable to open file (unable to lock file, errno = 35, error message = ‘Resource temporarily unavailable’)

As suggested, I moved the input file to the working folder, and didn’t specify any path to it when I call it, only the file name, then it worked.

for people on Google colab, add the following to your string if reading from drive:

r’/content/gdrive/My Drive/NAME_OF_FILE’

I had the same issue. No such h5 file found. " Provided the absolute path and Voila! it worked" Edit: I am using Colab.

The code snippet @Cookiedough611 posted already has the slashes doubled up "C:\\Users...", which should have the same effect as r"C:\Users...".

A number of people seem to have reported similar issues to Keras:

https://github.com/keras-team/keras/issues/8661 https://github.com/keras-team/keras/issues/10592

I don’t see any obvious cause, though I’d suspect something to do with Windows paths.